Change how functions are called

This commit is contained in:
Laurenz 2021-08-16 18:14:58 +02:00
parent ba6b91e2ee
commit cb0aab3cfa
2 changed files with 7 additions and 11 deletions

View File

@ -1,5 +1,4 @@
use std::fmt::{self, Debug, Display, Formatter, Write}; use std::fmt::{self, Debug, Display, Formatter, Write};
use std::ops::Deref;
use std::rc::Rc; use std::rc::Rc;
use super::{Cast, EvalContext, Str, Value}; use super::{Cast, EvalContext, Str, Value};
@ -34,13 +33,10 @@ impl Function {
pub fn name(&self) -> Option<&EcoString> { pub fn name(&self) -> Option<&EcoString> {
self.repr.name.as_ref() self.repr.name.as_ref()
} }
}
impl Deref for Function { /// Call the function in the context with the arguments.
type Target = Func; pub fn call(&self, ctx: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> {
(&self.repr.func)(ctx, args)
fn deref(&self) -> &Self::Target {
&self.repr.func
} }
} }

View File

@ -377,7 +377,7 @@ impl Eval for CallExpr {
Value::Func(func) => { Value::Func(func) => {
let point = || Tracepoint::Call(func.name().map(Into::into)); let point = || Tracepoint::Call(func.name().map(Into::into));
let value = func(ctx, &mut args).trace(point, self.span)?; let value = func.call(ctx, &mut args).trace(point, self.span)?;
args.finish()?; args.finish()?;
Ok(value) Ok(value)
} }
@ -520,13 +520,13 @@ impl Eval for WithExpr {
type Output = Value; type Output = Value;
fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> { fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> {
let callee = self.callee.eval(ctx)?.cast::<Function>().at(self.callee.span())?; let wrapped = self.callee.eval(ctx)?.cast::<Function>().at(self.callee.span())?;
let applied = self.args.eval(ctx)?; let applied = self.args.eval(ctx)?;
let name = callee.name().cloned(); let name = wrapped.name().cloned();
let func = Function::new(name, move |ctx, args| { let func = Function::new(name, move |ctx, args| {
args.items.splice(.. 0, applied.items.iter().cloned()); args.items.splice(.. 0, applied.items.iter().cloned());
callee(ctx, args) wrapped.call(ctx, args)
}); });
Ok(Value::Func(func)) Ok(Value::Func(func))