Pass arguments to call and construct directly by value

This commit is contained in:
Laurenz 2022-02-02 16:32:30 +01:00
parent d3ccd55d4b
commit 88e50a55af
3 changed files with 38 additions and 37 deletions

View File

@ -6,15 +6,15 @@ use crate::diag::TypResult;
/// A class of nodes. /// A class of nodes.
/// ///
/// You can [construct] an instance of a class in Typst code by invoking the /// You can [construct] an instance of a class in Typst code by invoking the
/// class as a callable. This always produces a template, but not necessarily a /// class as a callable. This always produces a template value, but not
/// simple inline or block node. For example, the `text` constructor does not /// necessarily a simple inline or block node. For example, the `text`
/// actually create a [`TextNode`]. Instead it applies styling to whatever node /// constructor does not actually create a [`TextNode`]. Instead it applies
/// you pass in and returns it structurally unchanged. /// styling to whatever node you pass in and returns it structurally unchanged.
/// ///
/// The arguments you can pass to a class constructor fall into two categories: /// The arguments you can pass to a class constructor fall into two categories:
/// Data that is inherent to the instance (e.g. the text of a heading) and style /// Data that is inherent to the instance (e.g. the text/content of a heading)
/// properties (e.g. the fill color of a heading). As the latter are often /// and style properties (e.g. the fill color of a heading). As the latter are
/// shared by many instances throughout a document, they can also be /// often shared by many instances throughout a document, they can also be
/// conveniently configured through class's [`set`] rule. Then, they apply to /// conveniently configured through class's [`set`] rule. Then, they apply to
/// all nodes that are instantiated into the template where the `set` was /// all nodes that are instantiated into the template where the `set` was
/// executed. /// executed.
@ -62,25 +62,30 @@ impl Class {
self.name self.name
} }
/// Return the class constructor as a function.
pub fn constructor(&self) -> Func {
Func::native(self.name, self.construct)
}
/// Construct an instance of the class. /// Construct an instance of the class.
/// ///
/// This parses both property and data arguments (in this order) and styles /// This parses both property and data arguments (in this order), styles the
/// the template constructed from the data with the style properties. /// template constructed from the data with the style properties and wraps
pub fn construct(&self, ctx: &mut EvalContext, args: &mut Args) -> TypResult<Value> { /// it in a value.
(self.construct)(ctx, args) pub fn construct(&self, ctx: &mut EvalContext, mut args: Args) -> TypResult<Value> {
let value = (self.construct)(ctx, &mut args)?;
args.finish()?;
Ok(value)
} }
/// Execute the class's set rule. /// Execute the class's set rule.
/// ///
/// This parses property arguments and writes the resulting styles into the /// This parses property arguments and return the resulting styles.
/// given style map. There are no further side effects. pub fn set(&self, mut args: Args) -> TypResult<StyleMap> {
pub fn set(&self, args: &mut Args, styles: &mut StyleMap) -> TypResult<()> { let mut styles = StyleMap::new();
(self.set)(args, styles) (self.set)(&mut args, &mut styles)?;
} args.finish()?;
Ok(styles)
/// Return the class constructor as a function.
pub fn constructor(&self) -> Func {
Func::native(self.name, self.construct)
} }
} }

View File

@ -45,15 +45,17 @@ impl Func {
} }
/// Call the function in the context with the arguments. /// Call the function in the context with the arguments.
pub fn call(&self, ctx: &mut EvalContext, args: &mut Args) -> TypResult<Value> { pub fn call(&self, ctx: &mut EvalContext, mut args: Args) -> TypResult<Value> {
match self.0.as_ref() { let value = match self.0.as_ref() {
Repr::Native(native) => (native.func)(ctx, args), Repr::Native(native) => (native.func)(ctx, &mut args)?,
Repr::Closure(closure) => closure.call(ctx, args), Repr::Closure(closure) => closure.call(ctx, &mut args)?,
Repr::With(wrapped, applied) => { Repr::With(wrapped, applied) => {
args.items.splice(.. 0, applied.items.iter().cloned()); args.items.splice(.. 0, applied.items.iter().cloned());
wrapped.call(ctx, args) return wrapped.call(ctx, args);
} }
} };
args.finish()?;
Ok(value)
} }
/// Apply the given arguments to the function. /// Apply the given arguments to the function.

View File

@ -189,10 +189,8 @@ fn eval_markup(
MarkupNode::Expr(Expr::Set(set)) => { MarkupNode::Expr(Expr::Set(set)) => {
let class = set.class(); let class = set.class();
let class = class.eval(ctx)?.cast::<Class>().at(class.span())?; let class = class.eval(ctx)?.cast::<Class>().at(class.span())?;
let mut args = set.args().eval(ctx)?; let args = set.args().eval(ctx)?;
let mut styles = StyleMap::new(); let styles = class.set(args)?;
class.set(&mut args, &mut styles)?;
args.finish()?;
let tail = eval_markup(ctx, nodes)?; let tail = eval_markup(ctx, nodes)?;
tail.styled_with_map(styles) tail.styled_with_map(styles)
} }
@ -590,7 +588,7 @@ impl Eval for CallExpr {
fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> { fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> {
let span = self.callee().span(); let span = self.callee().span();
let callee = self.callee().eval(ctx)?; let callee = self.callee().eval(ctx)?;
let mut args = self.args().eval(ctx)?; let args = self.args().eval(ctx)?;
match callee { match callee {
Value::Array(array) => { Value::Array(array) => {
@ -603,16 +601,12 @@ impl Eval for CallExpr {
Value::Func(func) => { Value::Func(func) => {
let point = || Tracepoint::Call(func.name().map(ToString::to_string)); let point = || Tracepoint::Call(func.name().map(ToString::to_string));
let value = func.call(ctx, &mut args).trace(point, self.span())?; func.call(ctx, args).trace(point, self.span())
args.finish()?;
Ok(value)
} }
Value::Class(class) => { Value::Class(class) => {
let point = || Tracepoint::Call(Some(class.name().to_string())); let point = || Tracepoint::Call(Some(class.name().to_string()));
let value = class.construct(ctx, &mut args).trace(point, self.span())?; class.construct(ctx, args).trace(point, self.span())
args.finish()?;
Ok(value)
} }
v => bail!( v => bail!(