mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Change how functions are called
This commit is contained in:
parent
ba6b91e2ee
commit
cb0aab3cfa
@ -1,5 +1,4 @@
|
||||
use std::fmt::{self, Debug, Display, Formatter, Write};
|
||||
use std::ops::Deref;
|
||||
use std::rc::Rc;
|
||||
|
||||
use super::{Cast, EvalContext, Str, Value};
|
||||
@ -34,13 +33,10 @@ impl Function {
|
||||
pub fn name(&self) -> Option<&EcoString> {
|
||||
self.repr.name.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for Function {
|
||||
type Target = Func;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.repr.func
|
||||
/// Call the function in the context with the arguments.
|
||||
pub fn call(&self, ctx: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> {
|
||||
(&self.repr.func)(ctx, args)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -377,7 +377,7 @@ impl Eval for CallExpr {
|
||||
|
||||
Value::Func(func) => {
|
||||
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()?;
|
||||
Ok(value)
|
||||
}
|
||||
@ -520,13 +520,13 @@ impl Eval for WithExpr {
|
||||
type Output = Value;
|
||||
|
||||
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 name = callee.name().cloned();
|
||||
let name = wrapped.name().cloned();
|
||||
let func = Function::new(name, move |ctx, args| {
|
||||
args.items.splice(.. 0, applied.items.iter().cloned());
|
||||
callee(ctx, args)
|
||||
wrapped.call(ctx, args)
|
||||
});
|
||||
|
||||
Ok(Value::Func(func))
|
||||
|
Loading…
x
Reference in New Issue
Block a user