From 0ba99ab8aa523645e2f0a0d9f6333ad4e48f5daa Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sun, 19 Mar 2023 10:19:24 +0100 Subject: Measurement and introspection rework --- src/eval/library.rs | 3 ++- src/eval/methods.rs | 33 ++++++++++++++++++++------------- 2 files changed, 22 insertions(+), 14 deletions(-) (limited to 'src/eval') diff --git a/src/eval/library.rs b/src/eval/library.rs index 485a766b..eae342c2 100644 --- a/src/eval/library.rs +++ b/src/eval/library.rs @@ -6,7 +6,7 @@ use comemo::Tracked; use ecow::EcoString; use once_cell::sync::OnceCell; -use super::{Args, Dynamic, Module, Value}; +use super::{Args, Dynamic, Module, Value, Vm}; use crate::diag::SourceResult; use crate::doc::Document; use crate::geom::{Abs, Dir}; @@ -92,6 +92,7 @@ pub struct LangItems { pub math_frac: fn(num: Content, denom: Content) -> Content, /// Dispatch a method on a library value. pub library_method: fn( + vm: &mut Vm, dynamic: &Dynamic, method: &str, args: Args, diff --git a/src/eval/methods.rs b/src/eval/methods.rs index eef825ff..324191ab 100644 --- a/src/eval/methods.rs +++ b/src/eval/methods.rs @@ -4,6 +4,7 @@ use ecow::EcoString; use super::{Args, Str, Value, Vm}; use crate::diag::{At, SourceResult}; +use crate::model::StableId; use crate::syntax::Span; /// Call a method on a value. @@ -73,8 +74,11 @@ pub fn call( "func" => Value::Func(content.id().into()), "has" => Value::Bool(content.has(&args.expect::("field")?)), "at" => content.at(&args.expect::("field")?).at(span)?.clone(), - "page" => content.page(&vm.vt).at(span)?.into(), - "location" => content.location(&vm.vt).at(span)?.into(), + "id" => content + .stable_id() + .ok_or("this method can only be called on content returned by query()") + .at(span)? + .into(), _ => return missing(), }, @@ -137,7 +141,15 @@ pub fn call( }, Value::Dyn(dynamic) => { - return (vm.items.library_method)(&dynamic, method, args, span); + if let Some(&id) = dynamic.downcast::() { + match method { + "page" => vm.vt.introspector.page(id).into(), + "location" => vm.vt.introspector.location(id).into(), + _ => return missing(), + } + } else { + return (vm.items.library_method)(vm, &dynamic, method, args, span); + } } _ => return missing(), @@ -251,13 +263,7 @@ pub fn methods_on(type_name: &str) -> &[(&'static str, bool)] { ("starts-with", true), ("trim", true), ], - "content" => &[ - ("func", false), - ("has", true), - ("at", true), - ("page", false), - ("location", false), - ], + "content" => &[("func", false), ("has", true), ("at", true), ("id", false)], "array" => &[ ("all", true), ("any", true), @@ -293,14 +299,15 @@ pub fn methods_on(type_name: &str) -> &[(&'static str, bool)] { ], "function" => &[("where", true), ("with", true)], "arguments" => &[("named", false), ("pos", false)], + "stable id" => &[("page", false), ("location", false)], "counter" => &[ - ("get", true), + ("display", true), + ("at", true), ("final", true), - ("both", true), ("step", true), ("update", true), ], - "state" => &[("get", true), ("final", true), ("update", true)], + "state" => &[("display", true), ("at", true), ("final", true), ("update", true)], _ => &[], } } -- cgit v1.2.3