use comemo::{Tracked, TrackedMut};
use ecow::{eco_format, EcoString, EcoVec};
use typst_library::diag::{
bail, error, At, HintedStrResult, HintedString, SourceDiagnostic, SourceResult,
Trace, Tracepoint,
};
use typst_library::engine::{Engine, Sink, Traced};
use typst_library::foundations::{
Arg, Args, Binding, Capturer, Closure, Content, Context, Func, NativeElement, Scope,
Scopes, SymbolElem, Value,
};
use typst_library::introspection::Introspector;
use typst_library::math::LrElem;
use typst_library::routines::Routines;
use typst_library::World;
use typst_syntax::ast::{self, AstNode, Ident};
use typst_syntax::{Span, Spanned, SyntaxNode};
use typst_utils::LazyHash;
use crate::{call_method_mut, is_mutating_method, Access, Eval, FlowEvent, Route, Vm};
impl Eval for ast::FuncCall<'_> {
type Output = Value;
fn eval(self, vm: &mut Vm) -> SourceResult<Self::Output> {
let span = self.span();
let callee = self.callee();
let in_math = in_math(callee);
let callee_span = callee.span();
let args = self.args();
let trailing_comma = args.trailing_comma();
vm.engine.route.check_call_depth().at(span)?;
let (callee, args) = if let ast::Expr::FieldAccess(access) = callee {
let target = access.target();
let field = access.field();
match eval_field_call(target, field, args, span, vm)? {
FieldCall::Normal(callee, args) => (callee, args),
FieldCall::Resolved(value) => return Ok(value),
}
} else {
(callee.eval(vm)?, args.eval(vm)?.spanned(span))
};
let func_result = callee.clone().cast::<Func>();
if in_math && func_result.is_err() {
return wrap_args_in_math(callee, callee_span, args, trailing_comma);
}
let func = func_result
.map_err(|err| hint_if_shadowed_std(vm, &self.callee(), err))
.at(callee_span)?;
let point = || Tracepoint::Call(func.name().map(Into::into));
let f = || {
func.call(&mut vm.engine, vm.context, args)
.trace(vm.world(), point, span)
};
#[cfg(target_arch = "wasm32")]
return f();
#[cfg(not(target_arch = "wasm32"))]
stacker::maybe_grow(32 * 1024, 2 * 1024 * 1024, f)
}
}
impl Eval for ast::Args<'_> {
type Output = Args;
fn eval(self, vm: &mut Vm) -> SourceResult<Self::Output> {
let mut items = EcoVec::with_capacity(self.items().count());
for arg in self.items() {
let span = arg.span();
match arg {
ast::Arg::Pos(expr) => {
items.push(Arg {
span,
name: None,
value: Spanned::new(expr.eval(vm)?, expr.span()),
});
}
ast::Arg::Named(named) => {
let expr = named.expr();
items.push(Arg {
span,
name: Some(named.name().get().clone().into()),
value: Spanned::new(expr.eval(vm)?, expr.span()),
});
}
ast::Arg::Spread(spread) => match spread.expr().eval(vm)? {
Value::None => {}
Value::Array(array) => {
items.extend(array.into_iter().map(|value| Arg {
span,
name: None,
value: Spanned::new(value, span),
}));
}
Value::Dict(dict) => {
items.extend(dict.into_iter().map(|(key, value)| Arg {
span,
name: Some(key),
value: Spanned::new(value, span),
}));
}
Value::Args(args) => items.extend(args.items),
v => bail!(spread.span(), "cannot spread {}", v.ty()),
},
}
}
Ok(Args { span: Span::detached(), items })
}
}
impl Eval for ast::Closure<'_> {
type Output = Value;
fn eval(self, vm: &mut Vm) -> SourceResult<Self::Output> {
let mut defaults = Vec::new();
for param in self.params().children() {
if let ast::Param::Named(named) = param {
defaults.push(named.expr().eval(vm)?);
}
}
let captured = {
let mut visitor = CapturesVisitor::new(Some(&vm.scopes), Capturer::Function);
visitor.visit(self.to_untyped());
visitor.finish()
};
let closure = Closure {
node: self.to_untyped().clone(),
defaults,
captured,
num_pos_params: self
.params()
.children()
.filter(|p| matches!(p, ast::Param::Pos(_)))
.count(),
};
Ok(Value::Func(Func::from(closure).spanned(self.params().span())))
}
}
#[comemo::memoize]
#[allow(clippy::too_many_arguments)]
pub fn eval_closure(
func: &Func,
closure: &LazyHash<Closure>,
routines: &Routines,
world: Tracked<dyn World + '_>,
introspector: Tracked<Introspector>,
traced: Tracked<Traced>,
sink: TrackedMut<Sink>,
route: Tracked<Route>,
context: Tracked<Context>,
mut args: Args,
) -> SourceResult<Value> {
let (name, params, body) = match closure.node.cast::<ast::Closure>() {
Some(node) => (node.name(), node.params(), node.body()),
None => (None, ast::Params::default(), closure.node.cast().unwrap()),
};
let mut scopes = Scopes::new(None);
scopes.top = closure.captured.clone();
let engine = Engine {
routines,
world,
introspector,
traced,
sink,
route: Route::extend(route),
};
let mut vm = Vm::new(engine, context, scopes, body.span());
if let Some(name) = name {
vm.define(name, func.clone());
}
let num_pos_args = args.to_pos().len();
let sink_size = num_pos_args.checked_sub(closure.num_pos_params);
let mut sink = None;
let mut sink_pos_values = None;
let mut defaults = closure.defaults.iter();
for p in params.children() {
match p {
ast::Param::Pos(pattern) => match pattern {
ast::Pattern::Normal(ast::Expr::Ident(ident)) => {
vm.define(ident, args.expect::<Value>(&ident)?)
}
pattern => {
crate::destructure(
&mut vm,
pattern,
args.expect::<Value>("pattern parameter")?,
)?;
}
},
ast::Param::Spread(spread) => {
sink = Some(spread.sink_ident());
if let Some(sink_size) = sink_size {
sink_pos_values = Some(args.consume(sink_size)?);
}
}
ast::Param::Named(named) => {
let name = named.name();
let default = defaults.next().unwrap();
let value =
args.named::<Value>(&name)?.unwrap_or_else(|| default.clone());
vm.define(name, value);
}
}
}
if let Some(sink) = sink {
let mut remaining_args = args.take();
if let Some(sink_name) = sink {
if let Some(sink_pos_values) = sink_pos_values {
remaining_args.items.extend(sink_pos_values);
}
vm.define(sink_name, remaining_args);
}
}
args.finish()?;
let output = body.eval(&mut vm)?;
match vm.flow {
Some(FlowEvent::Return(_, Some(explicit), _)) => return Ok(explicit),
Some(FlowEvent::Return(_, None, _)) => {}
Some(flow) => bail!(flow.forbidden()),
None => {}
}
Ok(output)
}
enum FieldCall {
Normal(Value, Args),
Resolved(Value),
}
fn eval_field_call(
target_expr: ast::Expr,
field: Ident,
args: ast::Args,
span: Span,
vm: &mut Vm,
) -> SourceResult<FieldCall> {
let (target, mut args) = if is_mutating_method(&field) {
let args = args.eval(vm)?.spanned(span);
match target_expr.access(vm)? {
target @ (Value::Array(_) | Value::Dict(_)) => {
let value = call_method_mut(target, &field, args, span);
let point = || Tracepoint::Call(Some(field.get().clone()));
return Ok(FieldCall::Resolved(value.trace(vm.world(), point, span)?));
}
target => (target.clone(), args),
}
} else {
let target = target_expr.eval(vm)?;
let args = args.eval(vm)?.spanned(span);
(target, args)
};
let field_span = field.span();
let sink = (&mut vm.engine, field_span);
if let Some(callee) = target.ty().scope().get(&field) {
args.insert(0, target_expr.span(), target);
Ok(FieldCall::Normal(callee.read_checked(sink).clone(), args))
} else if let Value::Content(content) = &target {
if let Some(callee) = content.elem().scope().get(&field) {
args.insert(0, target_expr.span(), target);
Ok(FieldCall::Normal(callee.read_checked(sink).clone(), args))
} else {
bail!(missing_field_call_error(target, field))
}
} else if matches!(
target,
Value::Symbol(_) | Value::Func(_) | Value::Type(_) | Value::Module(_)
) {
let value = target.field(&field, sink).at(field_span)?;
Ok(FieldCall::Normal(value, args))
} else {
bail!(missing_field_call_error(target, field))
}
}
fn missing_field_call_error(target: Value, field: Ident) -> SourceDiagnostic {
let mut error = match &target {
Value::Content(content) => error!(
field.span(),
"element {} has no method `{}`",
content.elem().name(),
field.as_str(),
),
_ => error!(
field.span(),
"type {} has no method `{}`",
target.ty(),
field.as_str()
),
};
match target {
Value::Dict(ref dict) if matches!(dict.get(&field), Ok(Value::Func(_))) => {
error.hint(eco_format!(
"to call the function stored in the dictionary, surround \
the field access with parentheses, e.g. `(dict.{})(..)`",
field.as_str(),
));
}
_ if target.field(&field, ()).is_ok() => {
error.hint(eco_format!(
"did you mean to access the field `{}`?",
field.as_str(),
));
}
_ => {}
}
error
}
fn in_math(expr: ast::Expr) -> bool {
match expr {
ast::Expr::MathIdent(_) => true,
ast::Expr::FieldAccess(access) => in_math(access.target()),
_ => false,
}
}
fn wrap_args_in_math(
callee: Value,
callee_span: Span,
mut args: Args,
trailing_comma: bool,
) -> SourceResult<Value> {
let mut body = Content::empty();
for (i, arg) in args.all::<Content>()?.into_iter().enumerate() {
if i > 0 {
body += SymbolElem::packed(',');
}
body += arg;
}
if trailing_comma {
body += SymbolElem::packed(',');
}
Ok(Value::Content(
callee.display().spanned(callee_span)
+ LrElem::new(SymbolElem::packed('(') + body + SymbolElem::packed(')'))
.pack()
.spanned(args.span),
))
}
fn hint_if_shadowed_std(
vm: &mut Vm,
callee: &ast::Expr,
mut err: HintedString,
) -> HintedString {
if let ast::Expr::Ident(ident) = callee {
let ident = ident.get();
if vm.scopes.check_std_shadowed(ident) {
err.hint(eco_format!(
"use `std.{ident}` to access the shadowed standard library function",
));
}
}
err
}
pub struct CapturesVisitor<'a> {
external: Option<&'a Scopes<'a>>,
internal: Scopes<'a>,
captures: Scope,
capturer: Capturer,
}
impl<'a> CapturesVisitor<'a> {
pub fn new(external: Option<&'a Scopes<'a>>, capturer: Capturer) -> Self {
Self {
external,
internal: Scopes::new(None),
captures: Scope::new(),
capturer,
}
}
pub fn finish(self) -> Scope {
self.captures
}
pub fn visit(&mut self, node: &SyntaxNode) {
match node.cast() {
Some(ast::Expr::Ident(ident)) => self.capture(ident.get(), Scopes::get),
Some(ast::Expr::MathIdent(ident)) => {
self.capture(ident.get(), Scopes::get_in_math)
}
Some(ast::Expr::Code(_) | ast::Expr::Content(_)) => {
self.internal.enter();
for child in node.children() {
self.visit(child);
}
self.internal.exit();
}
Some(ast::Expr::FieldAccess(access)) => {
self.visit(access.target().to_untyped());
}
Some(ast::Expr::Closure(expr)) => {
for param in expr.params().children() {
if let ast::Param::Named(named) = param {
self.visit(named.expr().to_untyped());
}
}
self.internal.enter();
if let Some(name) = expr.name() {
self.bind(name);
}
for param in expr.params().children() {
match param {
ast::Param::Pos(pattern) => {
for ident in pattern.bindings() {
self.bind(ident);
}
}
ast::Param::Named(named) => self.bind(named.name()),
ast::Param::Spread(spread) => {
if let Some(ident) = spread.sink_ident() {
self.bind(ident);
}
}
}
}
self.visit(expr.body().to_untyped());
self.internal.exit();
}
Some(ast::Expr::Let(expr)) => {
if let Some(init) = expr.init() {
self.visit(init.to_untyped());
}
for ident in expr.kind().bindings() {
self.bind(ident);
}
}
Some(ast::Expr::For(expr)) => {
self.visit(expr.iterable().to_untyped());
self.internal.enter();
let pattern = expr.pattern();
for ident in pattern.bindings() {
self.bind(ident);
}
self.visit(expr.body().to_untyped());
self.internal.exit();
}
Some(ast::Expr::Import(expr)) => {
self.visit(expr.source().to_untyped());
if let Some(ast::Imports::Items(items)) = expr.imports() {
for item in items.iter() {
self.bind(item.bound_name());
}
}
}
_ => {
if let Some(named) = node.cast::<ast::Named>() {
self.visit(named.expr().to_untyped());
return;
}
for child in node.children() {
self.visit(child);
}
}
}
}
fn bind(&mut self, ident: ast::Ident) {
self.internal
.top
.bind(ident.get().clone(), Binding::detached(Value::None));
}
fn capture(
&mut self,
ident: &EcoString,
getter: impl FnOnce(&'a Scopes<'a>, &str) -> HintedStrResult<&'a Binding>,
) {
if self.internal.get(ident).is_ok() {
return;
}
let binding = match self.external {
Some(external) => match getter(external, ident) {
Ok(binding) => binding.capture(self.capturer),
Err(_) => return,
},
None => Binding::detached(Value::None),
};
self.captures.bind(ident.clone(), binding);
}
}
#[cfg(test)]
mod tests {
use typst_syntax::parse;
use super::*;
#[track_caller]
fn test(scopes: &Scopes, text: &str, result: &[&str]) {
let mut visitor = CapturesVisitor::new(Some(scopes), Capturer::Function);
let root = parse(text);
visitor.visit(&root);
let captures = visitor.finish();
let mut names: Vec<_> = captures.iter().map(|(k, ..)| k).collect();
names.sort();
assert_eq!(names, result);
}
#[test]
fn test_captures() {
let mut scopes = Scopes::new(None);
scopes.top.define("f", 0);
scopes.top.define("x", 0);
scopes.top.define("y", 0);
scopes.top.define("z", 0);
let s = &scopes;
test(s, "#let x = x", &["x"]);
test(s, "#let x; #(x + y)", &["y"]);
test(s, "#let f(x, y) = x + y", &[]);
test(s, "#let f(x, y) = f", &[]);
test(s, "#let f = (x, y) => f", &["f"]);
test(s, "#((x, y) => x + z)", &["z"]);
test(s, "#((x: y, z) => x + z)", &["y"]);
test(s, "#((..x) => x + y)", &["y"]);
test(s, "#((x, y: x + z) => x + y)", &["x", "z"]);
test(s, "#{x => x; x}", &["x"]);
test(s, "#show y: x => x", &["y"]);
test(s, "#show y: x => x + z", &["y", "z"]);
test(s, "#show x: x => x", &["x"]);
test(s, "#for x in y { x + z }", &["y", "z"]);
test(s, "#for (x, y) in y { x + y }", &["y"]);
test(s, "#for x in y {} #x", &["x", "y"]);
test(s, "#import z: x, y", &["z"]);
test(s, "#import x + y: x, y, z", &["x", "y"]);
test(s, "#{ let x = 1; { let y = 2; y }; x + y }", &["y"]);
test(s, "#[#let x = 1]#x", &["x"]);
test(s, "#x.y.f(z)", &["x", "z"]);
test(s, "#f(x: 1)", &["f"]);
test(s, "#(x: 1)", &[]);
test(s, "#(x = 1)", &["x"]);
test(s, "#(x += y)", &["x", "y"]);
test(s, "#{ (x, z) = (y, 1) }", &["x", "y", "z"]);
test(s, "#(x.at(y) = 5)", &["x", "y"]);
}
#[test]
fn test_captures_in_math() {
let mut scopes = Scopes::new(None);
scopes.top.define("f", 0);
scopes.top.define("x", 0);
scopes.top.define("y", 0);
scopes.top.define("z", 0);
scopes.top.define("foo", 0);
scopes.top.define("bar", 0);
scopes.top.define("x-bar", 0);
scopes.top.define("x_bar", 0);
let s = &scopes;
test(s, "$ x f(z) $", &[]); test(s, "$ #x #f(z) $", &["f", "x", "z"]);
test(s, "$ foo f(bar) $", &["bar", "foo"]);
test(s, "$ #foo[#$bar$] $", &["bar", "foo"]);
test(s, "$ #let foo = x; foo $", &["x"]);
test(s, "$ x-y x_y foo-x x_bar $", &["bar", "foo"]);
test(s, "$ #x-bar #x_bar $", &["x-bar", "x_bar"]);
test(s, "$ foo(bar: y) $", &["foo"]);
test(s, "$ foo(x-y: 1, bar-z: 2) $", &["foo"]);
test(s, "$ foo.bar $", &["foo"]);
test(s, "$ foo.x $", &["foo"]);
test(s, "$ x.foo $", &["foo"]);
test(s, "$ foo . bar $", &["bar", "foo"]);
test(s, "$ foo.x.y.bar(z) $", &["foo"]);
test(s, "$ foo.x-bar $", &["bar", "foo"]);
test(s, "$ foo.x_bar $", &["bar", "foo"]);
test(s, "$ #x_bar.x-bar $", &["x_bar"]);
}
}