diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-02-22 14:31:09 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-02-23 14:53:55 +0100 |
| commit | e1f29d6cb9437a4afb2e4fc4ee10a5b8717ab9fa (patch) | |
| tree | 1ce5f2bd858f6665d3867a2939d4b474c1b70377 /src/library | |
| parent | 2bf32c51bceb2f3a8b7ebea3d7c7d6d96757591b (diff) | |
Rework the core context
Diffstat (limited to 'src/library')
| -rw-r--r-- | src/library/align.rs | 6 | ||||
| -rw-r--r-- | src/library/columns.rs | 13 | ||||
| -rw-r--r-- | src/library/container.rs | 4 | ||||
| -rw-r--r-- | src/library/deco.rs | 6 | ||||
| -rw-r--r-- | src/library/flow.rs | 10 | ||||
| -rw-r--r-- | src/library/grid.rs | 61 | ||||
| -rw-r--r-- | src/library/heading.rs | 12 | ||||
| -rw-r--r-- | src/library/hide.rs | 6 | ||||
| -rw-r--r-- | src/library/image.rs | 10 | ||||
| -rw-r--r-- | src/library/link.rs | 15 | ||||
| -rw-r--r-- | src/library/list.rs | 12 | ||||
| -rw-r--r-- | src/library/math.rs | 6 | ||||
| -rw-r--r-- | src/library/mod.rs | 2 | ||||
| -rw-r--r-- | src/library/numbering.rs | 6 | ||||
| -rw-r--r-- | src/library/pad.rs | 6 | ||||
| -rw-r--r-- | src/library/page.rs | 16 | ||||
| -rw-r--r-- | src/library/par.rs | 18 | ||||
| -rw-r--r-- | src/library/place.rs | 6 | ||||
| -rw-r--r-- | src/library/raw.rs | 6 | ||||
| -rw-r--r-- | src/library/shape.rs | 8 | ||||
| -rw-r--r-- | src/library/spacing.rs | 4 | ||||
| -rw-r--r-- | src/library/stack.rs | 10 | ||||
| -rw-r--r-- | src/library/table.rs | 6 | ||||
| -rw-r--r-- | src/library/text.rs | 14 | ||||
| -rw-r--r-- | src/library/transform.rs | 6 | ||||
| -rw-r--r-- | src/library/utility.rs | 40 |
26 files changed, 158 insertions, 151 deletions
diff --git a/src/library/align.rs b/src/library/align.rs index 8ea9ddaf..2fd734d3 100644 --- a/src/library/align.rs +++ b/src/library/align.rs @@ -14,7 +14,7 @@ pub struct AlignNode { #[class] impl AlignNode { - fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> { let aligns: Spec<_> = args.find()?.unwrap_or_default(); let body: LayoutNode = args.expect("body")?; Ok(Template::block(body.aligned(aligns))) @@ -24,7 +24,7 @@ impl AlignNode { impl Layout for AlignNode { fn layout( &self, - vm: &mut Vm, + ctx: &mut Context, regions: &Regions, styles: StyleChain, ) -> TypResult<Vec<Arc<Frame>>> { @@ -39,7 +39,7 @@ impl Layout for AlignNode { } // Layout the child. - let mut frames = self.child.layout(vm, &pod, passed.chain(&styles))?; + let mut frames = self.child.layout(ctx, &pod, passed.chain(&styles))?; for (region, frame) in regions.iter().zip(&mut frames) { // Align in the target size. The target size depends on whether we // should expand. diff --git a/src/library/columns.rs b/src/library/columns.rs index bae23dd3..3ce9b6e5 100644 --- a/src/library/columns.rs +++ b/src/library/columns.rs @@ -18,7 +18,7 @@ impl ColumnsNode { /// The size of the gutter space between each column. pub const GUTTER: Linear = Relative::new(0.04).into(); - fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> { Ok(Template::block(Self { columns: args.expect("column count")?, child: args.expect("body")?, @@ -29,14 +29,14 @@ impl ColumnsNode { impl Layout for ColumnsNode { fn layout( &self, - vm: &mut Vm, + ctx: &mut Context, regions: &Regions, styles: StyleChain, ) -> TypResult<Vec<Arc<Frame>>> { // Separating the infinite space into infinite columns does not make // much sense. if regions.first.x.is_infinite() { - return self.child.layout(vm, regions, styles); + return self.child.layout(ctx, regions, styles); } // Determine the width of the gutter and each column. @@ -52,14 +52,13 @@ impl Layout for ColumnsNode { .chain(regions.backlog.as_slice()) .flat_map(|&height| std::iter::repeat(height).take(columns)) .skip(1) - .collect::<Vec<_>>() - .into_iter(), + .collect(), last: regions.last, expand: Spec::new(true, regions.expand.y), }; // Layout the children. - let mut frames = self.child.layout(vm, &pod, styles)?.into_iter(); + let mut frames = self.child.layout(ctx, &pod, styles)?.into_iter(); let dir = styles.get(ParNode::DIR); let total_regions = (frames.len() as f32 / columns as f32).ceil() as usize; @@ -108,7 +107,7 @@ pub struct ColbreakNode; #[class] impl ColbreakNode { - fn construct(_: &mut Vm, _: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, _: &mut Args) -> TypResult<Template> { Ok(Template::Colbreak) } } diff --git a/src/library/container.rs b/src/library/container.rs index fafa7103..5517d927 100644 --- a/src/library/container.rs +++ b/src/library/container.rs @@ -7,7 +7,7 @@ pub struct BoxNode; #[class] impl BoxNode { - fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> { let width = args.named("width")?; let height = args.named("height")?; let body: LayoutNode = args.find()?.unwrap_or_default(); @@ -20,7 +20,7 @@ pub struct BlockNode; #[class] impl BlockNode { - fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> { Ok(Template::Block(args.find()?.unwrap_or_default())) } } diff --git a/src/library/deco.rs b/src/library/deco.rs index aac79d05..8c2b5a9a 100644 --- a/src/library/deco.rs +++ b/src/library/deco.rs @@ -26,15 +26,15 @@ impl<const L: DecoLine> DecoNode<L> { /// with the glyphs. Does not apply to strikethrough. pub const EVADE: bool = true; - fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> { Ok(Template::show(Self(args.expect::<Template>("body")?))) } } impl<const L: DecoLine> Show for DecoNode<L> { - fn show(&self, vm: &mut Vm, styles: StyleChain) -> TypResult<Template> { + fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Template> { Ok(styles - .show(self, vm, [Value::Template(self.0.clone())])? + .show(self, ctx, [Value::Template(self.0.clone())])? .unwrap_or_else(|| { self.0.clone().styled(TextNode::LINES, vec![Decoration { line: L, diff --git a/src/library/flow.rs b/src/library/flow.rs index bc3bf0f1..9e039d3a 100644 --- a/src/library/flow.rs +++ b/src/library/flow.rs @@ -28,7 +28,7 @@ pub enum FlowChild { impl Layout for FlowNode { fn layout( &self, - vm: &mut Vm, + ctx: &mut Context, regions: &Regions, styles: StyleChain, ) -> TypResult<Vec<Arc<Frame>>> { @@ -56,7 +56,7 @@ impl Layout for FlowNode { layouter.layout_spacing(*kind); } FlowChild::Node(ref node) => { - layouter.layout_node(vm, node, styles)?; + layouter.layout_node(ctx, node, styles)?; } } } @@ -163,7 +163,7 @@ impl FlowLayouter { /// Layout a node. pub fn layout_node( &mut self, - vm: &mut Vm, + ctx: &mut Context, node: &LayoutNode, styles: StyleChain, ) -> TypResult<()> { @@ -176,7 +176,7 @@ impl FlowLayouter { // aligned later. if let Some(placed) = node.downcast::<PlaceNode>() { if placed.out_of_flow() { - let frame = node.layout(vm, &self.regions, styles)?.remove(0); + let frame = node.layout(ctx, &self.regions, styles)?.remove(0); self.items.push(FlowItem::Placed(frame)); return Ok(()); } @@ -193,7 +193,7 @@ impl FlowLayouter { .unwrap_or(Align::Top), ); - let frames = node.layout(vm, &self.regions, styles)?; + let frames = node.layout(ctx, &self.regions, styles)?; let len = frames.len(); for (i, frame) in frames.into_iter().enumerate() { // Grow our size, shrink the region and save the frame for later. diff --git a/src/library/grid.rs b/src/library/grid.rs index fc62f8eb..2d8cb462 100644 --- a/src/library/grid.rs +++ b/src/library/grid.rs @@ -15,7 +15,7 @@ pub struct GridNode { #[class] impl GridNode { - fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> { let columns = args.named("columns")?.unwrap_or_default(); let rows = args.named("rows")?.unwrap_or_default(); let base_gutter: Vec<TrackSizing> = args.named("gutter")?.unwrap_or_default(); @@ -35,7 +35,7 @@ impl GridNode { impl Layout for GridNode { fn layout( &self, - vm: &mut Vm, + ctx: &mut Context, regions: &Regions, styles: StyleChain, ) -> TypResult<Vec<Arc<Frame>>> { @@ -49,7 +49,7 @@ impl Layout for GridNode { ); // Measure the columns and layout the grid row-by-row. - layouter.layout(vm) + layouter.layout(ctx) } } @@ -198,19 +198,19 @@ impl<'a> GridLayouter<'a> { } /// Determines the columns sizes and then layouts the grid row-by-row. - pub fn layout(mut self, vm: &mut Vm) -> TypResult<Vec<Arc<Frame>>> { - self.measure_columns(vm)?; + pub fn layout(mut self, ctx: &mut Context) -> TypResult<Vec<Arc<Frame>>> { + self.measure_columns(ctx)?; for y in 0 .. self.rows.len() { // Skip to next region if current one is full, but only for content // rows, not for gutter rows. if y % 2 == 0 && self.regions.is_full() { - self.finish_region(vm)?; + self.finish_region(ctx)?; } match self.rows[y] { - TrackSizing::Auto => self.layout_auto_row(vm, y)?, - TrackSizing::Linear(v) => self.layout_linear_row(vm, v, y)?, + TrackSizing::Auto => self.layout_auto_row(ctx, y)?, + TrackSizing::Linear(v) => self.layout_linear_row(ctx, v, y)?, TrackSizing::Fractional(v) => { self.lrows.push(Row::Fr(v, y)); self.fr += v; @@ -218,12 +218,12 @@ impl<'a> GridLayouter<'a> { } } - self.finish_region(vm)?; + self.finish_region(ctx)?; Ok(self.finished) } /// Determine all column sizes. - fn measure_columns(&mut self, vm: &mut Vm) -> TypResult<()> { + fn measure_columns(&mut self, ctx: &mut Context) -> TypResult<()> { // Sum of sizes of resolved linear tracks. let mut linear = Length::zero(); @@ -248,7 +248,7 @@ impl<'a> GridLayouter<'a> { let available = self.regions.first.x - linear; if available >= Length::zero() { // Determine size of auto columns. - let (auto, count) = self.measure_auto_columns(vm, available)?; + let (auto, count) = self.measure_auto_columns(ctx, available)?; // If there is remaining space, distribute it to fractional columns, // otherwise shrink auto columns. @@ -271,7 +271,7 @@ impl<'a> GridLayouter<'a> { /// Measure the size that is available to auto columns. fn measure_auto_columns( &mut self, - vm: &mut Vm, + ctx: &mut Context, available: Length, ) -> TypResult<(Length, usize)> { let mut auto = Length::zero(); @@ -298,7 +298,7 @@ impl<'a> GridLayouter<'a> { pod.base.y = v.resolve(self.regions.base.y); } - let frame = node.layout(vm, &pod, self.styles)?.remove(0); + let frame = node.layout(ctx, &pod, self.styles)?.remove(0); resolved.set_max(frame.size.x); } } @@ -352,7 +352,7 @@ impl<'a> GridLayouter<'a> { /// Layout a row with automatic height. Such a row may break across multiple /// regions. - fn layout_auto_row(&mut self, vm: &mut Vm, y: usize) -> TypResult<()> { + fn layout_auto_row(&mut self, ctx: &mut Context, y: usize) -> TypResult<()> { let mut resolved: Vec<Length> = vec![]; // Determine the size for each region of the row. @@ -367,7 +367,7 @@ impl<'a> GridLayouter<'a> { } let mut sizes = node - .layout(vm, &pod, self.styles)? + .layout(ctx, &pod, self.styles)? .into_iter() .map(|frame| frame.size.y); @@ -390,7 +390,7 @@ impl<'a> GridLayouter<'a> { // Layout into a single region. if let &[first] = resolved.as_slice() { - let frame = self.layout_single_row(vm, first, y)?; + let frame = self.layout_single_row(ctx, first, y)?; self.push_row(frame); return Ok(()); } @@ -405,12 +405,12 @@ impl<'a> GridLayouter<'a> { } // Layout into multiple regions. - let frames = self.layout_multi_row(vm, &resolved, y)?; + let frames = self.layout_multi_row(ctx, &resolved, y)?; let len = frames.len(); for (i, frame) in frames.into_iter().enumerate() { self.push_row(frame); if i + 1 < len { - self.finish_region(vm)?; + self.finish_region(ctx)?; } } @@ -419,14 +419,19 @@ impl<'a> GridLayouter<'a> { /// Layout a row with linear height. Such a row cannot break across multiple /// regions, but it may force a region break. - fn layout_linear_row(&mut self, vm: &mut Vm, v: Linear, y: usize) -> TypResult<()> { + fn layout_linear_row( + &mut self, + ctx: &mut Context, + v: Linear, + y: usize, + ) -> TypResult<()> { let resolved = v.resolve(self.regions.base.y); - let frame = self.layout_single_row(vm, resolved, y)?; + let frame = self.layout_single_row(ctx, resolved, y)?; // Skip to fitting region. let height = frame.size.y; while !self.regions.first.y.fits(height) && !self.regions.in_last() { - self.finish_region(vm)?; + self.finish_region(ctx)?; // Don't skip multiple regions for gutter and don't push a row. if y % 2 == 1 { @@ -442,7 +447,7 @@ impl<'a> GridLayouter<'a> { /// Layout a row with fixed height and return its frame. fn layout_single_row( &self, - vm: &mut Vm, + ctx: &mut Context, height: Length, y: usize, ) -> TypResult<Frame> { @@ -460,7 +465,7 @@ impl<'a> GridLayouter<'a> { .select(self.regions.base, size); let pod = Regions::one(size, base, Spec::splat(true)); - let frame = node.layout(vm, &pod, self.styles)?.remove(0); + let frame = node.layout(ctx, &pod, self.styles)?.remove(0); output.push_frame(pos, frame); } @@ -473,7 +478,7 @@ impl<'a> GridLayouter<'a> { /// Layout a row spanning multiple regions. fn layout_multi_row( &self, - vm: &mut Vm, + ctx: &mut Context, heights: &[Length], y: usize, ) -> TypResult<Vec<Frame>> { @@ -486,7 +491,7 @@ impl<'a> GridLayouter<'a> { // Prepare regions. let size = Size::new(self.used.x, heights[0]); let mut pod = Regions::one(size, self.regions.base, Spec::splat(true)); - pod.backlog = heights[1 ..].to_vec().into_iter(); + pod.backlog = heights[1 ..].to_vec(); // Layout the row. let mut pos = Point::zero(); @@ -500,7 +505,7 @@ impl<'a> GridLayouter<'a> { } // Push the layouted frames into the individual output frames. - let frames = node.layout(vm, &pod, self.styles)?; + let frames = node.layout(ctx, &pod, self.styles)?; for (output, frame) in outputs.iter_mut().zip(frames) { output.push_frame(pos, frame); } @@ -520,7 +525,7 @@ impl<'a> GridLayouter<'a> { } /// Finish rows for one region. - fn finish_region(&mut self, vm: &mut Vm) -> TypResult<()> { + fn finish_region(&mut self, ctx: &mut Context) -> TypResult<()> { // Determine the size of the grid in this region, expanding fully if // there are fr rows. let mut size = self.used; @@ -539,7 +544,7 @@ impl<'a> GridLayouter<'a> { Row::Fr(v, y) => { let remaining = self.full - self.used.y; let height = v.resolve(self.fr, remaining); - self.layout_single_row(vm, height, y)? + self.layout_single_row(ctx, height, y)? } }; diff --git a/src/library/heading.rs b/src/library/heading.rs index 49975655..0c9c8e27 100644 --- a/src/library/heading.rs +++ b/src/library/heading.rs @@ -37,7 +37,7 @@ impl HeadingNode { /// Whether the heading is block-level. pub const BLOCK: Leveled<bool> = Leveled::Value(true); - fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> { Ok(Template::show(Self { body: args.expect("body")?, level: args.named("level")?.unwrap_or(1), @@ -46,16 +46,16 @@ impl HeadingNode { } impl Show for HeadingNode { - fn show(&self, vm: &mut Vm, styles: StyleChain) -> TypResult<Template> { + fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Template> { macro_rules! resolve { ($key:expr) => { - styles.get_cloned($key).resolve(vm, self.level)? + styles.get_cloned($key).resolve(ctx, self.level)? }; } // Resolve the user recipe. let mut body = styles - .show(self, vm, [ + .show(self, ctx, [ Value::Int(self.level as i64), Value::Template(self.body.clone()), ])? @@ -124,13 +124,13 @@ pub enum Leveled<T> { impl<T: Cast> Leveled<T> { /// Resolve the value based on the level. - pub fn resolve(self, vm: &mut Vm, level: usize) -> TypResult<T> { + pub fn resolve(self, ctx: &mut Context, level: usize) -> TypResult<T> { Ok(match self { Self::Value(value) => value, Self::Mapping(mapping) => mapping(level), Self::Func(func, span) => { let args = Args::from_values(span, [Value::Int(level as i64)]); - func.call(vm, args)?.cast().at(span)? + func.call(ctx, args)?.cast().at(span)? } }) } diff --git a/src/library/hide.rs b/src/library/hide.rs index dcc73088..89aea6d3 100644 --- a/src/library/hide.rs +++ b/src/library/hide.rs @@ -8,7 +8,7 @@ pub struct HideNode(pub LayoutNode); #[class] impl HideNode { - fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> { Ok(Template::inline(Self(args.expect("body")?))) } } @@ -16,11 +16,11 @@ impl HideNode { impl Layout for HideNode { fn layout( &self, - vm: &mut Vm, + ctx: &mut Context, regions: &Regions, styles: StyleChain, ) -> TypResult<Vec<Arc<Frame>>> { - let mut frames = self.0.layout(vm, regions, styles)?; + let mut frames = self.0.layout(ctx, regions, styles)?; // Clear the frames. for frame in &mut frames { diff --git a/src/library/image.rs b/src/library/image.rs index 000aec73..c1220734 100644 --- a/src/library/image.rs +++ b/src/library/image.rs @@ -14,10 +14,10 @@ impl ImageNode { /// How the image should adjust itself to a given area. pub const FIT: ImageFit = ImageFit::Cover; - fn construct(vm: &mut Vm, args: &mut Args) -> TypResult<Template> { + fn construct(ctx: &mut Context, args: &mut Args) -> TypResult<Template> { let path = args.expect::<Spanned<EcoString>>("path to image file")?; - let full = vm.resolve(&path.v); - let id = vm.images.load(&full).map_err(|err| { + let full = ctx.resolve(&path.v); + let id = ctx.images.load(&full).map_err(|err| { Error::boxed(path.span, match err.kind() { std::io::ErrorKind::NotFound => "file not found".into(), _ => format!("failed to load image ({})", err), @@ -36,11 +36,11 @@ impl ImageNode { impl Layout for ImageNode { fn layout( &self, - vm: &mut Vm, + ctx: &mut Context, regions: &Regions, styles: StyleChain, ) -> TypResult<Vec<Arc<Frame>>> { - let img = vm.images.get(self.0); + let img = ctx.images.get(self.0); let pxw = img.width() as f64; let pxh = img.height() as f64; let px_ratio = pxw / pxh; diff --git a/src/library/link.rs b/src/library/link.rs index 41209549..4a153d46 100644 --- a/src/library/link.rs +++ b/src/library/link.rs @@ -21,7 +21,7 @@ impl LinkNode { /// Whether to underline link. pub const UNDERLINE: bool = true; - fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> { Ok(Template::show(Self { url: args.expect::<EcoString>("url")?, body: args.find()?, @@ -30,12 +30,15 @@ impl LinkNode { } impl Show for LinkNode { - fn show(&self, vm: &mut Vm, styles: StyleChain) -> TypResult<Template> { + fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Template> { let mut body = styles - .show(self, vm, [Value::Str(self.url.clone()), match &self.body { - Some(body) => Value::Template(body.clone()), - None => Value::None, - }])? + .show(self, ctx, [ + Value::Str(self.url.clone()), + match &self.body { + Some(body) => Value::Template(body.clone()), + None => Value::None, + }, + ])? .or_else(|| self.body.clone()) .unwrap_or_else(|| { let url = &self.url; diff --git a/src/library/list.rs b/src/library/list.rs index 37dda843..b757e4b2 100644 --- a/src/library/list.rs +++ b/src/library/list.rs @@ -37,7 +37,7 @@ impl<const L: ListKind> ListNode<L> { /// The space between the label and the body of each item. pub const BODY_INDENT: Linear = Relative::new(0.5).into(); - fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> { Ok(Template::show(Self { start: args.named("start")?.unwrap_or(0), wide: args.named("wide")?.unwrap_or(false), @@ -51,10 +51,10 @@ impl<const L: ListKind> ListNode<L> { } impl<const L: ListKind> Show for ListNode<L> { - fn show(&self, vm: &mut Vm, styles: StyleChain) -> TypResult<Template> { + fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Template> { if let Some(template) = styles.show( self, - vm, + ctx, self.items.iter().map(|item| Value::Template((*item.body).clone())), )? { return Ok(template); @@ -72,7 +72,7 @@ impl<const L: ListKind> Show for ListNode<L> { } children.push(LayoutNode::default()); - children.push(label.resolve(vm, L, number)?.pack()); + children.push(label.resolve(ctx, L, number)?.pack()); children.push(LayoutNode::default()); children.push((*item.body).clone().pack()); number += 1; @@ -135,7 +135,7 @@ impl Label { /// Resolve the value based on the level. pub fn resolve( &self, - vm: &mut Vm, + ctx: &mut Context, kind: ListKind, number: usize, ) -> TypResult<Template> { @@ -152,7 +152,7 @@ impl Label { Self::Template(template) => template.clone(), Self::Func(func, span) => { let args = Args::from_values(*span, [Value::Int(number as i64)]); - func.call(vm, args)?.cast().at(*span)? + func.call(ctx, args)?.cast().at(*span)? } }) } diff --git a/src/library/math.rs b/src/library/math.rs index 40a1990e..5d0ae41b 100644 --- a/src/library/math.rs +++ b/src/library/math.rs @@ -13,7 +13,7 @@ pub struct MathNode { #[class] impl MathNode { - fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> { Ok(Template::show(Self { formula: args.expect("formula")?, display: args.named("display")?.unwrap_or(false), @@ -22,9 +22,9 @@ impl MathNode { } impl Show for MathNode { - fn show(&self, vm: &mut Vm, styles: StyleChain) -> TypResult<Template> { + fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Template> { Ok(styles - .show(self, vm, [ + .show(self, ctx, [ Value::Str(self.formula.clone()), Value::Bool(self.display), ])? diff --git a/src/library/mod.rs b/src/library/mod.rs index 980f45c8..590dd331 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -74,7 +74,7 @@ pub mod prelude { pub use crate::geom::*; pub use crate::syntax::{Span, Spanned}; pub use crate::util::{EcoString, OptionExt}; - pub use crate::Vm; + pub use crate::Context; } use prelude::*; diff --git a/src/library/numbering.rs b/src/library/numbering.rs index f9031274..05a39e36 100644 --- a/src/library/numbering.rs +++ b/src/library/numbering.rs @@ -93,17 +93,17 @@ impl Numbering { } /// Converts an integer into one or multiple letters. -pub fn letter(_: &mut Vm, args: &mut Args) -> TypResult<Value> { +pub fn letter(_: &mut Context, args: &mut Args) -> TypResult<Value> { convert(Numbering::Letter, args) } /// Converts an integer into a roman numeral. -pub fn roman(_: &mut Vm, args: &mut Args) -> TypResult<Value> { +pub fn roman(_: &mut Context, args: &mut Args) -> TypResult<Value> { convert(Numbering::Roman, args) } /// Convert a number into a symbol. -pub fn symbol(_: &mut Vm, args: &mut Args) -> TypResult<Value> { +pub fn symbol(_: &mut Context, args: &mut Args) -> TypResult<Value> { convert(Numbering::Symbol, args) } diff --git a/src/library/pad.rs b/src/library/pad.rs index 05b658bd..174322e4 100644 --- a/src/library/pad.rs +++ b/src/library/pad.rs @@ -13,7 +13,7 @@ pub struct PadNode { #[class] impl PadNode { - fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> { let all = args.find()?; let hor = args.named("horizontal")?; let ver = args.named("vertical")?; @@ -30,13 +30,13 @@ impl PadNode { impl Layout for PadNode { fn layout( &self, - vm: &mut Vm, + ctx: &mut Context, regions: &Regions, styles: StyleChain, ) -> TypResult<Vec<Arc<Frame>>> { // Layout child into padded regions. let pod = regions.map(|size| shrink(size, self.padding)); - let mut frames = self.child.layout(vm, &pod, styles)?; + let mut frames = self.child.layout(ctx, &pod, styles)?; for frame in &mut frames { // Apply the padding inversely such that the grown size padded diff --git a/src/library/page.rs b/src/library/page.rs index 718234c6..5281c501 100644 --- a/src/library/page.rs +++ b/src/library/page.rs @@ -35,7 +35,7 @@ impl PageNode { /// The page's footer. pub const FOOTER: Marginal = Marginal::None; - fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> { Ok(Template::Page(Self(args.expect("body")?))) } @@ -70,7 +70,7 @@ impl PageNode { /// Layout the page run into a sequence of frames, one per page. pub fn layout( &self, - vm: &mut Vm, + ctx: &mut Context, mut page: usize, styles: StyleChain, ) -> TypResult<Vec<Arc<Frame>>> { @@ -115,7 +115,7 @@ impl PageNode { // Layout the child. let regions = Regions::repeat(size, size, size.map(Length::is_finite)); - let mut frames = child.layout(vm, ®ions, styles)?; + let mut frames = child.layout(ctx, ®ions, styles)?; let header = styles.get_ref(Self::HEADER); let footer = styles.get_ref(Self::FOOTER); @@ -128,12 +128,12 @@ impl PageNode { (Length::zero(), padding.top, header), (size.y - padding.bottom, padding.bottom, footer), ] { - if let Some(template) = marginal.resolve(vm, page)? { + if let Some(template) = marginal.resolve(ctx, page)? { let pos = Point::new(padding.left, y); 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 = template.layout(vm, &pod, styles)?.remove(0); + let sub = Layout::layout(&template, ctx, &pod, styles)?.remove(0); Arc::make_mut(frame).push_frame(pos, sub); } } @@ -158,7 +158,7 @@ pub struct PagebreakNode; #[class] impl PagebreakNode { - fn construct(_: &mut Vm, _: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, _: &mut Args) -> TypResult<Template> { Ok(Template::Pagebreak) } } @@ -176,13 +176,13 @@ pub enum Marginal { impl Marginal { /// Resolve the marginal based on the page number. - pub fn resolve(&self, vm: &mut Vm, page: usize) -> TypResult<Option<Template>> { + pub fn resolve(&self, ctx: &mut Context, page: usize) -> TypResult<Option<Template>> { Ok(match self { Self::None => None, Self::Template(template) => Some(template.clone()), Self::Func(func, span) => { let args = Args::from_values(*span, [Value::Int(page as i64)]); - func.call(vm, args)?.cast().at(*span)? + func.call(ctx, args)?.cast().at(*span)? } }) } diff --git a/src/library/par.rs b/src/library/par.rs index eab67c53..e5122166 100644 --- a/src/library/par.rs +++ b/src/library/par.rs @@ -37,7 +37,7 @@ impl ParNode { /// The extra spacing between paragraphs (dependent on scaled font size). pub const SPACING: Linear = Relative::new(0.55).into(); - fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> { // The paragraph constructor is special: It doesn't create a paragraph // since that happens automatically through markup. Instead, it just // lifts the passed body to the block level so that it won't merge with @@ -83,7 +83,7 @@ impl ParNode { impl Layout for ParNode { fn layout( &self, - vm: &mut Vm, + ctx: &mut Context, regions: &Regions, styles: StyleChain, ) -> TypResult<Vec<Arc<Frame>>> { @@ -94,8 +94,8 @@ impl Layout for ParNode { // Prepare paragraph layout by building a representation on which we can // do line breaking without layouting each and every line from scratch. - let par = ParLayout::new(vm, self, bidi, regions, &styles)?; - let fonts = &mut *vm.fonts; + let par = ParLayout::new(ctx, self, bidi, regions, &styles)?; + let fonts = &mut ctx.fonts; let em = styles.get(TextNode::SIZE).abs; let align = styles.get(ParNode::ALIGN); let leading = styles.get(ParNode::LEADING).resolve(em); @@ -242,7 +242,7 @@ pub struct ParbreakNode; #[class] impl ParbreakNode { - fn construct(_: &mut Vm, _: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, _: &mut Args) -> TypResult<Template> { Ok(Template::Parbreak) } } @@ -252,7 +252,7 @@ pub struct LinebreakNode; #[class] impl LinebreakNode { - fn construct(_: &mut Vm, _: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, _: &mut Args) -> TypResult<Template> { Ok(Template::Linebreak) } } @@ -286,7 +286,7 @@ enum ParItem<'a> { impl<'a> ParLayout<'a> { /// Prepare initial shaped text and layouted children. fn new( - vm: &mut Vm, + ctx: &mut Context, par: &'a ParNode, bidi: BidiInfo<'a>, regions: &Regions, @@ -307,7 +307,7 @@ impl<'a> ParLayout<'a> { cursor += count; let subrange = start .. cursor; let text = &bidi.text[subrange.clone()]; - let shaped = shape(vm.fonts, text, styles, level.dir()); + let shaped = shape(&mut ctx.fonts, text, styles, level.dir()); items.push(ParItem::Text(shaped)); ranges.push(subrange); } @@ -326,7 +326,7 @@ impl<'a> ParLayout<'a> { ParChild::Node(node) => { let size = Size::new(regions.first.x, regions.base.y); let pod = Regions::one(size, regions.base, Spec::splat(false)); - let frame = node.layout(vm, &pod, styles)?.remove(0); + let frame = node.layout(ctx, &pod, styles)?.remove(0); items.push(ParItem::Frame(Arc::take(frame))); ranges.push(range); } diff --git a/src/library/place.rs b/src/library/place.rs index 34746d5b..00e72dfb 100644 --- a/src/library/place.rs +++ b/src/library/place.rs @@ -9,7 +9,7 @@ pub struct PlaceNode(pub LayoutNode); #[class] impl PlaceNode { - fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> { let aligns = args.find()?.unwrap_or(Spec::with_x(Some(Align::Left))); let tx = args.named("dx")?.unwrap_or_default(); let ty = args.named("dy")?.unwrap_or_default(); @@ -23,7 +23,7 @@ impl PlaceNode { impl Layout for PlaceNode { fn layout( &self, - vm: &mut Vm, + ctx: &mut Context, regions: &Regions, styles: StyleChain, ) -> TypResult<Vec<Arc<Frame>>> { @@ -37,7 +37,7 @@ impl Layout for PlaceNode { Regions::one(regions.base, regions.base, expand) }; - let mut frames = self.0.layout(vm, &pod, styles)?; + let mut frames = self.0.layout(ctx, &pod, styles)?; // If expansion is off, zero all sizes so that we don't take up any // space in our parent. Otherwise, respect the expand settings. diff --git a/src/library/raw.rs b/src/library/raw.rs index bb4e2c96..785eeac5 100644 --- a/src/library/raw.rs +++ b/src/library/raw.rs @@ -31,7 +31,7 @@ impl RawNode { /// The language to syntax-highlight in. pub const LANG: Option<EcoString> = None; - fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> { Ok(Template::show(Self { text: args.expect("text")?, block: args.named("block")?.unwrap_or(false), @@ -40,10 +40,10 @@ impl RawNode { } impl Show for RawNode { - fn show(&self, vm: &mut Vm, styles: StyleChain) -> TypResult<Template> { + fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Template> { let lang = styles.get_ref(Self::LANG).as_ref(); - if let Some(template) = styles.show(self, vm, [ + if let Some(template) = styles.show(self, ctx, [ Value::Str(self.text.clone()), match lang { Some(lang) => Value::Str(lang.clone()), diff --git a/src/library/shape.rs b/src/library/shape.rs index 955aef56..bf159c52 100644 --- a/src/library/shape.rs +++ b/src/library/shape.rs @@ -20,7 +20,7 @@ impl<const S: ShapeKind> ShapeNode<S> { /// How much to pad the shape's content. pub const PADDING: Linear = Linear::zero(); - fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> { let size = match S { SQUARE => args.named::<Length>("size")?.map(Linear::from), CIRCLE => args.named::<Length>("radius")?.map(|r| 2.0 * Linear::from(r)), @@ -46,7 +46,7 @@ impl<const S: ShapeKind> ShapeNode<S> { impl<const S: ShapeKind> Layout for ShapeNode<S> { fn layout( &self, - vm: &mut Vm, + ctx: &mut Context, regions: &Regions, styles: StyleChain, ) -> TypResult<Vec<Arc<Frame>>> { @@ -61,7 +61,7 @@ impl<const S: ShapeKind> Layout for ShapeNode<S> { let child = child.clone().padded(Sides::splat(padding)); let mut pod = Regions::one(regions.first, regions.base, regions.expand); - frames = child.layout(vm, &pod, styles)?; + frames = child.layout(ctx, &pod, styles)?; // Relayout with full expansion into square region to make sure // the result is really a square or circle. @@ -77,7 +77,7 @@ impl<const S: ShapeKind> Layout for ShapeNode<S> { pod.first = Size::splat(length); pod.expand = Spec::splat(true); - frames = child.layout(vm, &pod, styles)?; + frames = child.layout(ctx, &pod, styles)?; } } else { // The default size that a shape takes on if it has no child and diff --git a/src/library/spacing.rs b/src/library/spacing.rs index f9676d4c..6f57c71c 100644 --- a/src/library/spacing.rs +++ b/src/library/spacing.rs @@ -7,7 +7,7 @@ pub struct HNode; #[class] impl HNode { - fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> { Ok(Template::Horizontal(args.expect("spacing")?)) } } @@ -17,7 +17,7 @@ pub struct VNode; #[class] impl VNode { - fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> { Ok(Template::Vertical(args.expect("spacing")?)) } } diff --git a/src/library/stack.rs b/src/library/stack.rs index 6768a0a0..8e463f6a 100644 --- a/src/library/stack.rs +++ b/src/library/stack.rs @@ -16,7 +16,7 @@ pub struct StackNode { #[class] impl StackNode { - fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> { Ok(Template::block(Self { dir: args.named("dir")?.unwrap_or(Dir::TTB), spacing: args.named("spacing")?, @@ -28,7 +28,7 @@ impl StackNode { impl Layout for StackNode { fn layout( &self, - vm: &mut Vm, + ctx: &mut Context, regions: &Regions, styles: StyleChain, ) -> TypResult<Vec<Arc<Frame>>> { @@ -48,7 +48,7 @@ impl Layout for StackNode { layouter.layout_spacing(kind); } - layouter.layout_node(vm, node, styles)?; + layouter.layout_node(ctx, node, styles)?; deferred = self.spacing; } } @@ -165,7 +165,7 @@ impl StackLayouter { /// Layout an arbitrary node. pub fn layout_node( &mut self, - vm: &mut Vm, + ctx: &mut Context, node: &LayoutNode, styles: StyleChain, ) -> TypResult<()> { @@ -179,7 +179,7 @@ impl StackLayouter { .and_then(|node| node.aligns.get(self.axis)) .unwrap_or(self.dir.start().into()); - let frames = node.layout(vm, &self.regions, styles)?; + let frames = node.layout(ctx, &self.regions, styles)?; let len = frames.len(); for (i, frame) in frames.into_iter().enumerate() { // Grow our size, shrink the region and save the frame for later. diff --git a/src/library/table.rs b/src/library/table.rs index f4de0f55..07f28737 100644 --- a/src/library/table.rs +++ b/src/library/table.rs @@ -27,7 +27,7 @@ impl TableNode { /// How much to pad the cells's content. pub const PADDING: Linear = Length::pt(5.0).into(); - fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> { let columns = args.named("columns")?.unwrap_or_default(); let rows = args.named("rows")?.unwrap_or_default(); let base_gutter: Vec<TrackSizing> = args.named("gutter")?.unwrap_or_default(); @@ -55,10 +55,10 @@ impl TableNode { } impl Show for TableNode { - fn show(&self, vm: &mut Vm, styles: StyleChain) -> TypResult<Template> { + fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Template> { if let Some(template) = styles.show( self, - vm, + ctx, self.children.iter().map(|child| Value::Template(child.clone())), )? { return Ok(template); diff --git a/src/library/text.rs b/src/library/text.rs index ca903d1f..ba7f4c20 100644 --- a/src/library/text.rs +++ b/src/library/text.rs @@ -103,7 +103,7 @@ impl TextNode { #[skip] pub const LINK: Option<EcoString> = None; - fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> { // 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. @@ -117,15 +117,15 @@ pub struct StrongNode(pub Template); #[class] impl StrongNode { - fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> { Ok(Template::show(Self(args.expect("body")?))) } } impl Show for StrongNode { - fn show(&self, vm: &mut Vm, styles: StyleChain) -> TypResult<Template> { + fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Template> { Ok(styles - .show(self, vm, [Value::Template(self.0.clone())])? + .show(self, ctx, [Value::Template(self.0.clone())])? .unwrap_or_else(|| self.0.clone().styled(TextNode::STRONG, true))) } } @@ -136,15 +136,15 @@ pub struct EmphNode(pub Template); #[class] impl EmphNode { - fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> { Ok(Template::show(Self(args.expect("body")?))) } } impl Show for EmphNode { - fn show(&self, vm: &mut Vm, styles: StyleChain) -> TypResult<Template> { + fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Template> { Ok(styles - .show(self, vm, [Value::Template(self.0.clone())])? + .show(self, ctx, [Value::Template(self.0.clone())])? .unwrap_or_else(|| self.0.clone().styled(TextNode::EMPH, true))) } } diff --git a/src/library/transform.rs b/src/library/transform.rs index 84553fb5..897f3144 100644 --- a/src/library/transform.rs +++ b/src/library/transform.rs @@ -17,7 +17,7 @@ impl<const T: TransformKind> TransformNode<T> { /// The origin of the transformation. pub const ORIGIN: Spec<Option<Align>> = Spec::default(); - fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> { let transform = match T { MOVE => { let tx = args.named("x")?.unwrap_or_default(); @@ -46,12 +46,12 @@ impl<const T: TransformKind> TransformNode<T> { impl<const T: TransformKind> Layout for TransformNode<T> { fn layout( &self, - vm: &mut Vm, + ctx: &mut Context, regions: &Regions, styles: StyleChain, ) -> TypResult<Vec<Arc<Frame>>> { let origin = styles.get(Self::ORIGIN).unwrap_or(Align::CENTER_HORIZON); - let mut frames = self.child.layout(vm, regions, styles)?; + let mut frames = self.child.layout(ctx, regions, styles)?; for frame in &mut frames { let Spec { x, y } = origin.zip(frame.size).map(|(o, s)| o.resolve(s)); diff --git a/src/library/utility.rs b/src/library/utility.rs index f7f46a8f..051dd885 100644 --- a/src/library/utility.rs +++ b/src/library/utility.rs @@ -7,7 +7,7 @@ use super::prelude::*; use crate::eval::Array; /// Ensure that a condition is fulfilled. -pub fn assert(_: &mut Vm, args: &mut Args) -> TypResult<Value> { +pub fn assert(_: &mut Context, args: &mut Args) -> TypResult<Value> { let Spanned { v, span } = args.expect::<Spanned<bool>>("condition")?; if !v { bail!(span, "assertion failed"); @@ -16,17 +16,17 @@ pub fn assert(_: &mut Vm, args: &mut Args) -> TypResult<Value> { } /// The name of a value's type. -pub fn type_(_: &mut Vm, args: &mut Args) -> TypResult<Value> { +pub fn type_(_: &mut Context, args: &mut Args) -> TypResult<Value> { Ok(args.expect::<Value>("value")?.type_name().into()) } /// The string representation of a value. -pub fn repr(_: &mut Vm, args: &mut Args) -> TypResult<Value> { +pub fn repr(_: &mut Context, args: &mut Args) -> TypResult<Value> { Ok(args.expect::<Value>("value")?.repr().into()) } /// Join a sequence of values, optionally interspersing it with another value. -pub fn join(_: &mut Vm, args: &mut Args) -> TypResult<Value> { +pub fn join(_: &mut Context, args: &mut Args) -> TypResult<Value> { let span = args.span; let sep = args.named::<Value>("sep")?.unwrap_or(Value::None); @@ -46,7 +46,7 @@ pub fn join(_: &mut Vm, args: &mut Args) -> TypResult<Value> { } /// Convert a value to a integer. -pub fn int(_: &mut Vm, args: &mut Args) -> TypResult<Value> { +pub fn int(_: &mut Context, args: &mut Args) -> TypResult<Value> { let Spanned { v, span } = args.expect("value")?; Ok(Value::Int(match v { Value::Bool(v) => v as i64, @@ -61,7 +61,7 @@ pub fn int(_: &mut Vm, args: &mut Args) -> TypResult<Value> { } /// Convert a value to a float. -pub fn float(_: &mut Vm, args: &mut Args) -> TypResult<Value> { +pub fn float(_: &mut Context, args: &mut Args) -> TypResult<Value> { let Spanned { v, span } = args.expect("value")?; Ok(Value::Float(match v { Value::Int(v) => v as f64, @@ -75,7 +75,7 @@ pub fn float(_: &mut Vm, args: &mut Args) -> TypResult<Value> { } /// Try to convert a value to a string. -pub fn str(_: &mut Vm, args: &mut Args) -> TypResult<Value> { +pub fn str(_: &mut Context, args: &mut Args) -> TypResult<Value> { let Spanned { v, span } = args.expect("value")?; Ok(Value::Str(match v { Value::Int(v) => format_eco!("{}", v), @@ -86,7 +86,7 @@ pub fn str(_: &mut Vm, args: &mut Args) -> TypResult<Value> { } /// Create an RGB(A) color. -pub fn rgb(_: &mut Vm, args: &mut Args) -> TypResult<Value> { +pub fn rgb(_: &mut Context, args: &mut Args) -> TypResult<Value> { Ok(Value::from( if let Some(string) = args.find::<Spanned<EcoString>>()? { match RgbaColor::from_str(&string.v) { @@ -120,7 +120,7 @@ pub fn rgb(_: &mut Vm, args: &mut Args) -> TypResult<Value> { } /// Create a CMYK color. -pub fn cmyk(_: &mut Vm, args: &mut Args) -> TypResult<Value> { +pub fn cmyk(_: &mut Context, args: &mut Args) -> TypResult<Value> { struct Component(u8); castable! { @@ -141,7 +141,7 @@ pub fn cmyk(_: &mut Vm, args: &mut Args) -> TypResult<Value> { } /// The absolute value of a numeric value. -pub fn abs(_: &mut Vm, args: &mut Args) -> TypResult<Value> { +pub fn abs(_: &mut Context, args: &mut Args) -> TypResult<Value> { let Spanned { v, span } = args.expect("numeric value")?; Ok(match v { Value::Int(v) => Value::Int(v.abs()), @@ -156,27 +156,27 @@ pub fn abs(_: &mut Vm, args: &mut Args) -> TypResult<Value> { } /// The minimum of a sequence of values. -pub fn min(_: &mut Vm, args: &mut Args) -> TypResult<Value> { +pub fn min(_: &mut Context, args: &mut Args) -> TypResult<Value> { minmax(args, Ordering::Less) } /// The maximum of a sequence of values. -pub fn max(_: &mut Vm, args: &mut Args) -> TypResult<Value> { +pub fn max(_: &mut Context, args: &mut Args) -> TypResult<Value> { minmax(args, Ordering::Greater) } /// Whether an integer is even. -pub fn even(_: &mut Vm, args: &mut Args) -> TypResult<Value> { +pub fn even(_: &mut Context, args: &mut Args) -> TypResult<Value> { Ok(Value::Bool(args.expect::<i64>("integer")? % 2 == 0)) } /// Whether an integer is odd. -pub fn odd(_: &mut Vm, args: &mut Args) -> TypResult<Value> { +pub fn odd(_: &mut Context, args: &mut Args) -> TypResult<Value> { Ok(Value::Bool(args.expect::<i64>("integer")? % 2 != 0)) } /// The modulo of two numbers. -pub fn modulo(_: &mut Vm, args: &mut Args) -> TypResult<Value> { +pub fn modulo(_: &mut Context, args: &mut Args) -> TypResult<Value> { let Spanned { v: v1, span: span1 } = args.expect("integer or float")?; let Spanned { v: v2, span: span2 } = args.expect("integer or float")?; @@ -227,7 +227,7 @@ fn minmax(args: &mut Args, goal: Ordering) -> TypResult<Value> { } /// Create a sequence of numbers. -pub fn range(_: &mut Vm, args: &mut Args) -> TypResult<Value> { +pub fn range(_: &mut Context, args: &mut Args) -> TypResult<Value> { let first = args.expect::<i64>("end")?; let (start, end) = match args.eat::<i64>()? { Some(second) => (first, second), @@ -252,17 +252,17 @@ pub fn range(_: &mut Vm, args: &mut Args) -> TypResult<Value> { } /// Convert a string to lowercase. -pub fn lower(_: &mut Vm, args: &mut Args) -> TypResult<Value> { +pub fn lower(_: &mut Context, args: &mut Args) -> TypResult<Value> { Ok(args.expect::<EcoString>("string")?.to_lowercase().into()) } /// Convert a string to uppercase. -pub fn upper(_: &mut Vm, args: &mut Args) -> TypResult<Value> { +pub fn upper(_: &mut Context, args: &mut Args) -> TypResult<Value> { Ok(args.expect::<EcoString>("string")?.to_uppercase().into()) } /// The length of a string, an array or a dictionary. -pub fn len(_: &mut Vm, args: &mut Args) -> TypResult<Value> { +pub fn len(_: &mut Context, args: &mut Args) -> TypResult<Value> { let Spanned { v, span } = args.expect("collection")?; Ok(Value::Int(match v { Value::Str(v) => v.len() as i64, @@ -277,7 +277,7 @@ pub fn len(_: &mut Vm, args: &mut Args) -> TypResult<Value> { } /// The sorted version of an array. -pub fn sorted(_: &mut Vm, args: &mut Args) -> TypResult<Value> { +pub fn sorted(_: &mut Context, args: &mut Args) -> TypResult<Value> { let Spanned { v, span } = args.expect::<Spanned<Array>>("array")?; Ok(Value::Array(v.sorted().at(span)?)) } |
