Skip to content

Commit

Permalink
Use RC to avoid cloning functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Quaqqer committed Jan 7, 2024
1 parent 62b6298 commit e3356de
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
15 changes: 10 additions & 5 deletions crates/saft-eval/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use saft_ast::{Block, Expr, Ident, Item, Module, Statement};
use saft_common::span::{Span, Spanned};
use std::borrow::Borrow;
use std::collections::HashMap;
use std::rc::Rc;

#[macro_export]
macro_rules! exotic {
Expand Down Expand Up @@ -141,10 +142,10 @@ impl Interpreter {
params,
body,
}) => {
let fun = Value::Function(Function::SaftFunction(SaftFunction {
let fun = Value::Function(Rc::new(Function::SaftFunction(SaftFunction {
params: params.clone(),
body: body.clone(),
}));
})));
self.env.declare(ident, fun);
Ok(())
}
Expand Down Expand Up @@ -316,8 +317,10 @@ impl Interpreter {
arg_vals.push(self.eval_expr(arg)?);
}

match fun.v {
Value::Function(Function::SaftFunction(SaftFunction { params, body })) => {
match &fun.v {
Value::Function(fun)
if let Function::SaftFunction(SaftFunction { params, body }) = fun.as_ref() =>
{
match self.scoped(|interpreter| {
if arg_vals.len() != params.len() {
return Err(Exception::ArgMismatch {
Expand All @@ -339,7 +342,9 @@ impl Interpreter {
Err(e) => return Err(e),
}
}
Value::Function(Function::NativeFunction(NativeFuncData { f, .. })) => {
Value::Function(fun)
if let Function::NativeFunction(NativeFuncData { f, .. }) = fun.as_ref() =>
{
f(self, &s, arg_vals)?
}
_ => {
Expand Down
4 changes: 3 additions & 1 deletion crates/saft-eval/src/natives.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::rc::Rc;

use crate::interpreter::{ControlFlow, Interpreter};
use crate::{cast_error, exotic};
use saft_common::span::{Span, Spanned};
Expand Down Expand Up @@ -69,6 +71,6 @@ fn add_native<N: NativeFunc>(env: &mut Env) {
let data = N::data();
env.declare_unspanned(
&data.name.into(),
Value::Function(Function::NativeFunction(data)),
Value::Function(Rc::new(Function::NativeFunction(data))),
);
}
10 changes: 6 additions & 4 deletions crates/saft-eval/src/value.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::interpreter::{ControlFlow, Interpreter};
use std::borrow::Borrow;
use std::{borrow::Borrow, rc::Rc};

use saft_ast::Block;
use saft_common::span::{Span, Spanned};
Expand All @@ -8,7 +8,7 @@ use saft_common::span::{Span, Spanned};
pub enum Value {
Nil,
Num(Num),
Function(Function),
Function(Rc<Function>),
String(String),
Vec(Vec<Value>),
}
Expand All @@ -18,8 +18,10 @@ impl Value {
match self {
Value::Nil => "nil".into(),
Value::Num(num) => num.repr(),
Value::Function(Function::SaftFunction(..)) => "<function>".into(),
Value::Function(Function::NativeFunction(..)) => "<builtin function>".into(),
Value::Function(fun) => match fun.as_ref() {
Function::SaftFunction(_) => "<function>".into(),
Function::NativeFunction(_) => "<builtin function>".into(),
},
Value::String(s) => format!("\"{}\"", s),
Value::Vec(vals) => {
let mut buf = String::new();
Expand Down

0 comments on commit e3356de

Please sign in to comment.