From c010cbc17dcbb2f0d6005d21530143bf57cb5871 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Wed, 25 May 2022 13:50:33 +0200 Subject: Move route from context to VM --- src/library/graphics/hide.rs | 2 +- src/library/graphics/image.rs | 6 +++--- src/library/graphics/line.rs | 2 +- src/library/graphics/shape.rs | 2 +- src/library/graphics/transform.rs | 4 ++-- src/library/layout/align.rs | 2 +- src/library/layout/columns.rs | 4 ++-- src/library/layout/container.rs | 4 ++-- src/library/layout/grid.rs | 2 +- src/library/layout/pad.rs | 2 +- src/library/layout/page.rs | 10 +++++----- src/library/layout/place.rs | 2 +- src/library/layout/spacing.rs | 4 ++-- src/library/layout/stack.rs | 2 +- src/library/math/mod.rs | 2 +- src/library/prelude.rs | 4 ++-- src/library/structure/heading.rs | 6 +++--- src/library/structure/list.rs | 6 +++--- src/library/structure/table.rs | 9 +++------ src/library/text/deco.rs | 2 +- src/library/text/link.rs | 2 +- src/library/text/mod.rs | 12 ++++++------ src/library/text/par.rs | 6 +++--- src/library/text/raw.rs | 2 +- src/library/text/repeat.rs | 2 +- src/library/utility/color.rs | 4 ++-- src/library/utility/math.rs | 18 +++++++++--------- src/library/utility/mod.rs | 23 +++++++---------------- src/library/utility/string.rs | 14 +++++++------- 29 files changed, 74 insertions(+), 86 deletions(-) (limited to 'src/library') diff --git a/src/library/graphics/hide.rs b/src/library/graphics/hide.rs index 85971c36..28afe320 100644 --- a/src/library/graphics/hide.rs +++ b/src/library/graphics/hide.rs @@ -6,7 +6,7 @@ pub struct HideNode(pub LayoutNode); #[node] impl HideNode { - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { Ok(Content::inline(Self(args.expect("body")?))) } } diff --git a/src/library/graphics/image.rs b/src/library/graphics/image.rs index 6fd465cb..d0bcfa44 100644 --- a/src/library/graphics/image.rs +++ b/src/library/graphics/image.rs @@ -11,12 +11,12 @@ impl ImageNode { /// How the image should adjust itself to a given area. pub const FIT: ImageFit = ImageFit::Cover; - fn construct(ctx: &mut Context, args: &mut Args) -> TypResult { + fn construct(vm: &mut Machine, args: &mut Args) -> TypResult { let Spanned { v: path, span } = args.expect::>("path to image file")?; - let full = ctx.locate(&path).at(span)?; - let id = ctx.images.load(&full).map_err(|err| match err.kind() { + let full = vm.locate(&path).at(span)?; + let id = vm.ctx.images.load(&full).map_err(|err| match err.kind() { std::io::ErrorKind::NotFound => { error!(span, "file not found (searched at {})", full.display()) } diff --git a/src/library/graphics/line.rs b/src/library/graphics/line.rs index de2e4aa1..8e5ceae1 100644 --- a/src/library/graphics/line.rs +++ b/src/library/graphics/line.rs @@ -15,7 +15,7 @@ impl LineNode { #[property(resolve, fold)] pub const STROKE: RawStroke = RawStroke::default(); - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { let origin = args.named("origin")?.unwrap_or_default(); let delta = match args.named::>>("to")? { diff --git a/src/library/graphics/shape.rs b/src/library/graphics/shape.rs index bc768628..9da8d8df 100644 --- a/src/library/graphics/shape.rs +++ b/src/library/graphics/shape.rs @@ -37,7 +37,7 @@ impl ShapeNode { #[property(skip, resolve, fold)] pub const RADIUS: Sides>> = Sides::splat(Relative::zero()); - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { let size = match S { SQUARE => args.named::("size")?.map(Relative::from), CIRCLE => args.named::("radius")?.map(|r| 2.0 * Relative::from(r)), diff --git a/src/library/graphics/transform.rs b/src/library/graphics/transform.rs index a4aa20db..9fcf7ebb 100644 --- a/src/library/graphics/transform.rs +++ b/src/library/graphics/transform.rs @@ -12,7 +12,7 @@ pub struct MoveNode { #[node] impl MoveNode { - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { let dx = args.named("dx")?.unwrap_or_default(); let dy = args.named("dy")?.unwrap_or_default(); Ok(Content::inline(Self { @@ -62,7 +62,7 @@ impl TransformNode { #[property(resolve)] pub const ORIGIN: Spec> = Spec::default(); - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { let transform = match T { ROTATE => { let angle = args.named_or_find("angle")?.unwrap_or_default(); diff --git a/src/library/layout/align.rs b/src/library/layout/align.rs index c050d2a4..c0a7d16c 100644 --- a/src/library/layout/align.rs +++ b/src/library/layout/align.rs @@ -12,7 +12,7 @@ pub struct AlignNode { #[node] impl AlignNode { - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { let aligns: Spec> = args.find()?.unwrap_or_default(); let body: Content = args.expect("body")?; Ok(match (body, aligns) { diff --git a/src/library/layout/columns.rs b/src/library/layout/columns.rs index 8e523694..4c842261 100644 --- a/src/library/layout/columns.rs +++ b/src/library/layout/columns.rs @@ -17,7 +17,7 @@ impl ColumnsNode { #[property(resolve)] pub const GUTTER: Relative = Ratio::new(0.04).into(); - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { Ok(Content::block(Self { columns: args.expect("column count")?, child: args.expect("body")?, @@ -106,7 +106,7 @@ pub struct ColbreakNode; #[node] impl ColbreakNode { - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { let weak = args.named("weak")?.unwrap_or(false); Ok(Content::Colbreak { weak }) } diff --git a/src/library/layout/container.rs b/src/library/layout/container.rs index 5264f258..df03ac91 100644 --- a/src/library/layout/container.rs +++ b/src/library/layout/container.rs @@ -5,7 +5,7 @@ pub struct BoxNode; #[node] impl BoxNode { - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { let width = args.named("width")?; let height = args.named("height")?; let body: LayoutNode = args.eat()?.unwrap_or_default(); @@ -18,7 +18,7 @@ pub struct BlockNode; #[node] impl BlockNode { - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { Ok(Content::Block(args.eat()?.unwrap_or_default())) } } diff --git a/src/library/layout/grid.rs b/src/library/layout/grid.rs index 8ecac636..5b621732 100644 --- a/src/library/layout/grid.rs +++ b/src/library/layout/grid.rs @@ -13,7 +13,7 @@ pub struct GridNode { #[node] impl GridNode { - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { let columns = args.named("columns")?.unwrap_or_default(); let rows = args.named("rows")?.unwrap_or_default(); let base_gutter: Vec = args.named("gutter")?.unwrap_or_default(); diff --git a/src/library/layout/pad.rs b/src/library/layout/pad.rs index aff0e8b0..97b760e1 100644 --- a/src/library/layout/pad.rs +++ b/src/library/layout/pad.rs @@ -11,7 +11,7 @@ pub struct PadNode { #[node] impl PadNode { - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { let all = args.named("rest")?.or(args.find()?); let x = args.named("x")?; let y = args.named("y")?; diff --git a/src/library/layout/page.rs b/src/library/layout/page.rs index 8db1329f..7d91aa6d 100644 --- a/src/library/layout/page.rs +++ b/src/library/layout/page.rs @@ -35,7 +35,7 @@ impl PageNode { #[property(referenced)] pub const FOOTER: Marginal = Marginal::None; - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { Ok(Content::Page(Self(args.expect("body")?))) } @@ -109,7 +109,7 @@ impl PageNode { let w = size.x - padding.left - padding.right; let area = Size::new(w, h); let pod = Regions::one(area, area, area.map(Length::is_finite)); - let sub = Layout::layout(&content, ctx, &pod, styles)?.remove(0); + let sub = content.layout(ctx, &pod, styles)?.remove(0); Arc::make_mut(frame).push_frame(pos, sub); } } @@ -134,7 +134,7 @@ pub struct PagebreakNode; #[node] impl PagebreakNode { - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { let weak = args.named("weak")?.unwrap_or(false); Ok(Content::Pagebreak { weak }) } @@ -158,8 +158,8 @@ impl Marginal { Self::None => None, Self::Content(content) => Some(content.clone()), Self::Func(func, span) => { - let args = Args::from_values(*span, [Value::Int(page as i64)]); - Some(func.call(ctx, args)?.display()) + let args = Args::new(*span, [Value::Int(page as i64)]); + Some(func.call_detached(ctx, args)?.display()) } }) } diff --git a/src/library/layout/place.rs b/src/library/layout/place.rs index e74776db..408ca129 100644 --- a/src/library/layout/place.rs +++ b/src/library/layout/place.rs @@ -7,7 +7,7 @@ pub struct PlaceNode(pub LayoutNode); #[node] impl PlaceNode { - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { let aligns = args.find()?.unwrap_or(Spec::with_x(Some(RawAlign::Start))); let dx = args.named("dx")?.unwrap_or_default(); let dy = args.named("dy")?.unwrap_or_default(); diff --git a/src/library/layout/spacing.rs b/src/library/layout/spacing.rs index 8a96e378..da4a96b6 100644 --- a/src/library/layout/spacing.rs +++ b/src/library/layout/spacing.rs @@ -8,7 +8,7 @@ pub struct HNode; #[node] impl HNode { - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { let amount = args.expect("spacing")?; let weak = args.named("weak")?.unwrap_or(false); Ok(Content::Horizontal { amount, weak }) @@ -20,7 +20,7 @@ pub struct VNode; #[node] impl VNode { - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { let amount = args.expect("spacing")?; let weak = args.named("weak")?.unwrap_or(false); Ok(Content::Vertical { amount, weak, generated: false }) diff --git a/src/library/layout/stack.rs b/src/library/layout/stack.rs index bbfeeab0..828ff8e3 100644 --- a/src/library/layout/stack.rs +++ b/src/library/layout/stack.rs @@ -15,7 +15,7 @@ pub struct StackNode { #[node] impl StackNode { - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { Ok(Content::block(Self { dir: args.named("dir")?.unwrap_or(Dir::TTB), spacing: args.named("spacing")?, diff --git a/src/library/math/mod.rs b/src/library/math/mod.rs index b1dabbe5..6ed759b7 100644 --- a/src/library/math/mod.rs +++ b/src/library/math/mod.rs @@ -28,7 +28,7 @@ impl MathNode { #[property(resolve, shorthand(around))] pub const BELOW: Option = Some(Ratio::one().into()); - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { Ok(Content::show(Self { formula: args.expect("formula")?, display: args.named("display")?.unwrap_or(false), diff --git a/src/library/prelude.rs b/src/library/prelude.rs index 99bff51a..371d6776 100644 --- a/src/library/prelude.rs +++ b/src/library/prelude.rs @@ -9,8 +9,8 @@ pub use typst_macros::node; pub use crate::diag::{with_alternative, At, Error, StrResult, TypError, TypResult}; pub use crate::eval::{ - Arg, Args, Array, Cast, Dict, Func, Node, RawAlign, RawLength, RawStroke, Scope, - Smart, Value, + Arg, Args, Array, Cast, Dict, Func, Machine, Node, RawAlign, RawLength, RawStroke, + Scope, Smart, Value, }; pub use crate::frame::*; pub use crate::geom::*; diff --git a/src/library/structure/heading.rs b/src/library/structure/heading.rs index 1cfcb6d0..a0973b90 100644 --- a/src/library/structure/heading.rs +++ b/src/library/structure/heading.rs @@ -55,7 +55,7 @@ impl HeadingNode { pub const BELOW: Leveled> = Leveled::Value(Some(Ratio::new(0.55).into())); - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { Ok(Content::show(Self { body: args.expect("body")?, level: args.named("level")?.unwrap_or(NonZeroUsize::new(1).unwrap()), @@ -142,8 +142,8 @@ impl Leveled { Self::Value(value) => value.clone(), Self::Mapping(mapping) => mapping(level), Self::Func(func, span) => { - let args = Args::from_values(*span, [Value::Int(level.get() as i64)]); - func.call(ctx, args)?.cast().at(*span)? + let args = Args::new(*span, [Value::Int(level.get() as i64)]); + func.call_detached(ctx, args)?.cast().at(*span)? } }) } diff --git a/src/library/structure/list.rs b/src/library/structure/list.rs index 7686a3f4..84603eb3 100644 --- a/src/library/structure/list.rs +++ b/src/library/structure/list.rs @@ -56,7 +56,7 @@ impl ListNode { #[property(resolve)] pub const SPACING: BlockSpacing = Ratio::one().into(); - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { Ok(Content::show(Self { start: args.named("start")?.unwrap_or(1), tight: args.named("tight")?.unwrap_or(true), @@ -216,8 +216,8 @@ impl Label { } Self::Content(content) => content.clone(), Self::Func(func, span) => { - let args = Args::from_values(*span, [Value::Int(number as i64)]); - func.call(ctx, args)?.display() + let args = Args::new(*span, [Value::Int(number as i64)]); + func.call_detached(ctx, args)?.display() } }) } diff --git a/src/library/structure/table.rs b/src/library/structure/table.rs index f39ea978..cd70db30 100644 --- a/src/library/structure/table.rs +++ b/src/library/structure/table.rs @@ -30,7 +30,7 @@ impl TableNode { #[property(resolve, shorthand(around))] pub const BELOW: Option = Some(Ratio::one().into()); - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { let columns = args.named("columns")?.unwrap_or_default(); let rows = args.named("rows")?.unwrap_or_default(); let base_gutter: Vec = args.named("gutter")?.unwrap_or_default(); @@ -128,11 +128,8 @@ impl Celled { Ok(match self { Self::Value(value) => value.clone(), Self::Func(func, span) => { - let args = Args::from_values(*span, [ - Value::Int(x as i64), - Value::Int(y as i64), - ]); - func.call(ctx, args)?.cast().at(*span)? + let args = Args::new(*span, [Value::Int(x as i64), Value::Int(y as i64)]); + func.call_detached(ctx, args)?.cast().at(*span)? } }) } diff --git a/src/library/text/deco.rs b/src/library/text/deco.rs index dedaa6e8..cec4ca9e 100644 --- a/src/library/text/deco.rs +++ b/src/library/text/deco.rs @@ -35,7 +35,7 @@ impl DecoNode { /// with the glyphs. Does not apply to strikethrough. pub const EVADE: bool = true; - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { Ok(Content::show(Self(args.expect("body")?))) } } diff --git a/src/library/text/link.rs b/src/library/text/link.rs index 284f0b1f..728b594f 100644 --- a/src/library/text/link.rs +++ b/src/library/text/link.rs @@ -19,7 +19,7 @@ impl LinkNode { /// Whether to underline link. pub const UNDERLINE: bool = true; - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { Ok(Content::show(Self { url: args.expect::("url")?, body: args.eat()?, diff --git a/src/library/text/mod.rs b/src/library/text/mod.rs index 1b83b0f4..bbe397ca 100644 --- a/src/library/text/mod.rs +++ b/src/library/text/mod.rs @@ -127,7 +127,7 @@ impl TextNode { #[property(skip, fold)] pub const DECO: Decoration = vec![]; - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { // The text constructor is special: It doesn't create a text node. // Instead, it leaves the passed argument structurally unchanged, but // styles all text in it. @@ -443,12 +443,12 @@ impl Fold for Vec<(Tag, u32)> { } /// Convert text to lowercase. -pub fn lower(_: &mut Context, args: &mut Args) -> TypResult { +pub fn lower(_: &mut Machine, args: &mut Args) -> TypResult { case(Case::Lower, args) } /// Convert text to uppercase. -pub fn upper(_: &mut Context, args: &mut Args) -> TypResult { +pub fn upper(_: &mut Machine, args: &mut Args) -> TypResult { case(Case::Upper, args) } @@ -482,7 +482,7 @@ impl Case { } /// Display text in small capitals. -pub fn smallcaps(_: &mut Context, args: &mut Args) -> TypResult { +pub fn smallcaps(_: &mut Machine, args: &mut Args) -> TypResult { let body: Content = args.expect("content")?; Ok(Value::Content(body.styled(TextNode::SMALLCAPS, true))) } @@ -514,7 +514,7 @@ pub struct StrongNode(pub Content); #[node(showable)] impl StrongNode { - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { Ok(Content::show(Self(args.expect("body")?))) } } @@ -539,7 +539,7 @@ pub struct EmphNode(pub Content); #[node(showable)] impl EmphNode { - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { Ok(Content::show(Self(args.expect("body")?))) } } diff --git a/src/library/text/par.rs b/src/library/text/par.rs index 6c274e7e..1269ffed 100644 --- a/src/library/text/par.rs +++ b/src/library/text/par.rs @@ -51,7 +51,7 @@ impl ParNode { #[property(resolve)] pub const LINEBREAKS: Smart = Smart::Auto; - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { // The paragraph constructor is special: It doesn't create a paragraph // node. Instead, it just ensures that the passed content lives is in a // separate paragraph and styles it. @@ -172,7 +172,7 @@ pub struct ParbreakNode; #[node] impl ParbreakNode { - fn construct(_: &mut Context, _: &mut Args) -> TypResult { + fn construct(_: &mut Machine, _: &mut Args) -> TypResult { Ok(Content::Parbreak) } } @@ -182,7 +182,7 @@ pub struct LinebreakNode; #[node] impl LinebreakNode { - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { let justified = args.named("justified")?.unwrap_or(false); Ok(Content::Linebreak { justified }) } diff --git a/src/library/text/raw.rs b/src/library/text/raw.rs index f44877ca..fe1f9a99 100644 --- a/src/library/text/raw.rs +++ b/src/library/text/raw.rs @@ -35,7 +35,7 @@ impl RawNode { #[property(resolve, shorthand(around))] pub const BELOW: Option = Some(Ratio::one().into()); - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { Ok(Content::show(Self { text: args.expect("text")?, block: args.named("block")?.unwrap_or(false), diff --git a/src/library/text/repeat.rs b/src/library/text/repeat.rs index 68036be7..9ee8286b 100644 --- a/src/library/text/repeat.rs +++ b/src/library/text/repeat.rs @@ -6,7 +6,7 @@ pub struct RepeatNode(pub LayoutNode); #[node] impl RepeatNode { - fn construct(_: &mut Context, args: &mut Args) -> TypResult { + fn construct(_: &mut Machine, args: &mut Args) -> TypResult { Ok(Content::inline(Self(args.expect("body")?))) } } diff --git a/src/library/utility/color.rs b/src/library/utility/color.rs index 75410380..5857c4c7 100644 --- a/src/library/utility/color.rs +++ b/src/library/utility/color.rs @@ -3,7 +3,7 @@ use std::str::FromStr; use crate::library::prelude::*; /// Create an RGB(A) color. -pub fn rgb(_: &mut Context, args: &mut Args) -> TypResult { +pub fn rgb(_: &mut Machine, args: &mut Args) -> TypResult { Ok(Value::from( if let Some(string) = args.find::>()? { match RgbaColor::from_str(&string.v) { @@ -37,7 +37,7 @@ pub fn rgb(_: &mut Context, args: &mut Args) -> TypResult { } /// Create a CMYK color. -pub fn cmyk(_: &mut Context, args: &mut Args) -> TypResult { +pub fn cmyk(_: &mut Machine, args: &mut Args) -> TypResult { struct Component(u8); castable! { diff --git a/src/library/utility/math.rs b/src/library/utility/math.rs index 63ec5e55..05c706ca 100644 --- a/src/library/utility/math.rs +++ b/src/library/utility/math.rs @@ -3,7 +3,7 @@ use std::cmp::Ordering; use crate::library::prelude::*; /// Convert a value to a integer. -pub fn int(_: &mut Context, args: &mut Args) -> TypResult { +pub fn int(_: &mut Machine, args: &mut Args) -> TypResult { let Spanned { v, span } = args.expect("value")?; Ok(Value::Int(match v { Value::Bool(v) => v as i64, @@ -18,7 +18,7 @@ pub fn int(_: &mut Context, args: &mut Args) -> TypResult { } /// Convert a value to a float. -pub fn float(_: &mut Context, args: &mut Args) -> TypResult { +pub fn float(_: &mut Machine, args: &mut Args) -> TypResult { let Spanned { v, span } = args.expect("value")?; Ok(Value::Float(match v { Value::Int(v) => v as f64, @@ -32,7 +32,7 @@ pub fn float(_: &mut Context, args: &mut Args) -> TypResult { } /// The absolute value of a numeric value. -pub fn abs(_: &mut Context, args: &mut Args) -> TypResult { +pub fn abs(_: &mut Machine, args: &mut Args) -> TypResult { let Spanned { v, span } = args.expect("numeric value")?; Ok(match v { Value::Int(v) => Value::Int(v.abs()), @@ -48,12 +48,12 @@ pub fn abs(_: &mut Context, args: &mut Args) -> TypResult { } /// The minimum of a sequence of values. -pub fn min(_: &mut Context, args: &mut Args) -> TypResult { +pub fn min(_: &mut Machine, args: &mut Args) -> TypResult { minmax(args, Ordering::Less) } /// The maximum of a sequence of values. -pub fn max(_: &mut Context, args: &mut Args) -> TypResult { +pub fn max(_: &mut Machine, args: &mut Args) -> TypResult { minmax(args, Ordering::Greater) } @@ -79,17 +79,17 @@ fn minmax(args: &mut Args, goal: Ordering) -> TypResult { } /// Whether an integer is even. -pub fn even(_: &mut Context, args: &mut Args) -> TypResult { +pub fn even(_: &mut Machine, args: &mut Args) -> TypResult { Ok(Value::Bool(args.expect::("integer")? % 2 == 0)) } /// Whether an integer is odd. -pub fn odd(_: &mut Context, args: &mut Args) -> TypResult { +pub fn odd(_: &mut Machine, args: &mut Args) -> TypResult { Ok(Value::Bool(args.expect::("integer")? % 2 != 0)) } /// The modulo of two numbers. -pub fn mod_(_: &mut Context, args: &mut Args) -> TypResult { +pub fn mod_(_: &mut Machine, args: &mut Args) -> TypResult { let Spanned { v: v1, span: span1 } = args.expect("integer or float")?; let Spanned { v: v2, span: span2 } = args.expect("integer or float")?; @@ -119,7 +119,7 @@ pub fn mod_(_: &mut Context, args: &mut Args) -> TypResult { } /// Create a sequence of numbers. -pub fn range(_: &mut Context, args: &mut Args) -> TypResult { +pub fn range(_: &mut Machine, args: &mut Args) -> TypResult { let first = args.expect::("end")?; let (start, end) = match args.eat::()? { Some(second) => (first, second), diff --git a/src/library/utility/mod.rs b/src/library/utility/mod.rs index 6f0f9a4c..051f8195 100644 --- a/src/library/utility/mod.rs +++ b/src/library/utility/mod.rs @@ -8,19 +8,17 @@ pub use color::*; pub use math::*; pub use string::*; -use std::mem; - use crate::eval::{Eval, Machine, Scopes}; use crate::library::prelude::*; use crate::source::SourceFile; /// The name of a value's type. -pub fn type_(_: &mut Context, args: &mut Args) -> TypResult { +pub fn type_(_: &mut Machine, args: &mut Args) -> TypResult { Ok(args.expect::("value")?.type_name().into()) } /// Ensure that a condition is fulfilled. -pub fn assert(_: &mut Context, args: &mut Args) -> TypResult { +pub fn assert(_: &mut Machine, args: &mut Args) -> TypResult { let Spanned { v, span } = args.expect::>("condition")?; if !v { bail!(span, "assertion failed"); @@ -29,28 +27,21 @@ pub fn assert(_: &mut Context, args: &mut Args) -> TypResult { } /// Evaluate a string as Typst markup. -pub fn eval(ctx: &mut Context, args: &mut Args) -> TypResult { +pub fn eval(vm: &mut Machine, args: &mut Args) -> TypResult { let Spanned { v: src, span } = args.expect::>("source")?; // Parse the source and set a synthetic span for all nodes. let source = SourceFile::synthesized(src, span); let ast = source.ast()?; - // Save the old route, then detach it. - let prev_route = mem::take(&mut ctx.route); - // Evaluate the source. - let std = ctx.config.std.clone(); + let std = vm.ctx.config.std.clone(); let scopes = Scopes::new(Some(&std)); - let mut vm = Machine::new(ctx, scopes); - let result = ast.eval(&mut vm); - let flow = vm.flow; - - // Restore the old route. - ctx.route = prev_route; + let mut sub = Machine::new(vm.ctx, vec![], scopes); + let result = ast.eval(&mut sub); // Handle control flow. - if let Some(flow) = flow { + if let Some(flow) = sub.flow { return Err(flow.forbidden()); } diff --git a/src/library/utility/string.rs b/src/library/utility/string.rs index 13a6bbd8..2f80a5cb 100644 --- a/src/library/utility/string.rs +++ b/src/library/utility/string.rs @@ -4,12 +4,12 @@ use crate::eval::Regex; use crate::library::prelude::*; /// The string representation of a value. -pub fn repr(_: &mut Context, args: &mut Args) -> TypResult { +pub fn repr(_: &mut Machine, args: &mut Args) -> TypResult { Ok(args.expect::("value")?.repr().into()) } /// Cconvert a value to a string. -pub fn str(_: &mut Context, args: &mut Args) -> TypResult { +pub fn str(_: &mut Machine, args: &mut Args) -> TypResult { let Spanned { v, span } = args.expect("value")?; Ok(Value::Str(match v { Value::Int(v) => format_eco!("{}", v), @@ -20,29 +20,29 @@ pub fn str(_: &mut Context, args: &mut Args) -> TypResult { } /// Create blind text. -pub fn lorem(_: &mut Context, args: &mut Args) -> TypResult { +pub fn lorem(_: &mut Machine, args: &mut Args) -> TypResult { let words: usize = args.expect("number of words")?; Ok(Value::Str(lipsum_from_seed(words, 97).into())) } /// Create a regular expression. -pub fn regex(_: &mut Context, args: &mut Args) -> TypResult { +pub fn regex(_: &mut Machine, args: &mut Args) -> TypResult { let Spanned { v, span } = args.expect::>("regular expression")?; Ok(Regex::new(&v).at(span)?.into()) } /// Converts an integer into one or multiple letters. -pub fn letter(_: &mut Context, args: &mut Args) -> TypResult { +pub fn letter(_: &mut Machine, args: &mut Args) -> TypResult { convert(Numbering::Letter, args) } /// Converts an integer into a roman numeral. -pub fn roman(_: &mut Context, args: &mut Args) -> TypResult { +pub fn roman(_: &mut Machine, args: &mut Args) -> TypResult { convert(Numbering::Roman, args) } /// Convert a number into a symbol. -pub fn symbol(_: &mut Context, args: &mut Args) -> TypResult { +pub fn symbol(_: &mut Machine, args: &mut Args) -> TypResult { convert(Numbering::Symbol, args) } -- cgit v1.2.3