From e1f29d6cb9437a4afb2e4fc4ee10a5b8717ab9fa Mon Sep 17 00:00:00 2001 From: Laurenz Date: Tue, 22 Feb 2022 14:31:09 +0100 Subject: [PATCH] Rework the core context --- benches/oneshot.rs | 37 ++-- src/eval/class.rs | 11 +- src/eval/func.rs | 32 ++-- src/eval/layout.rs | 46 +++-- src/eval/mod.rs | 360 ++++++++++++++++++++------------------- src/eval/module.rs | 20 +++ src/eval/scope.rs | 5 +- src/eval/show.rs | 8 +- src/eval/styles.rs | 12 +- src/eval/template.rs | 67 ++++---- src/export/pdf.rs | 2 +- src/lib.rs | 201 +++++++++------------- src/library/align.rs | 6 +- src/library/columns.rs | 13 +- src/library/container.rs | 4 +- src/library/deco.rs | 6 +- src/library/flow.rs | 10 +- src/library/grid.rs | 61 ++++--- src/library/heading.rs | 12 +- src/library/hide.rs | 6 +- src/library/image.rs | 10 +- src/library/link.rs | 15 +- src/library/list.rs | 12 +- src/library/math.rs | 6 +- src/library/mod.rs | 2 +- src/library/numbering.rs | 6 +- src/library/pad.rs | 6 +- src/library/page.rs | 16 +- src/library/par.rs | 18 +- src/library/place.rs | 6 +- src/library/raw.rs | 6 +- src/library/shape.rs | 8 +- src/library/spacing.rs | 4 +- src/library/stack.rs | 10 +- src/library/table.rs | 6 +- src/library/text.rs | 14 +- src/library/transform.rs | 6 +- src/library/utility.rs | 40 ++--- src/source.rs | 132 ++++++++------ tests/typ/style/show.typ | 7 +- tests/typeset.rs | 5 +- 41 files changed, 627 insertions(+), 627 deletions(-) create mode 100644 src/eval/module.rs diff --git a/benches/oneshot.rs b/benches/oneshot.rs index 5dbf993fe..9bce185c0 100644 --- a/benches/oneshot.rs +++ b/benches/oneshot.rs @@ -5,7 +5,7 @@ use iai::{black_box, main, Iai}; use typst::loading::MemLoader; use typst::parse::{parse, Scanner, TokenMode, Tokens}; use typst::source::SourceId; -use typst::{Context, Vm}; +use typst::Context; const SRC: &str = include_str!("bench.typ"); const FONT: &[u8] = include_bytes!("../fonts/IBMPlexSans-Regular.ttf"); @@ -26,7 +26,6 @@ main!( bench_eval, bench_layout, bench_highlight, - bench_byte_to_utf16, bench_render, ); @@ -67,37 +66,21 @@ fn bench_edit(iai: &mut Iai) { iai.run(|| black_box(ctx.sources.edit(id, 1168 .. 1171, "_Uhr_"))); } -fn bench_eval(iai: &mut Iai) { - let (mut ctx, id) = context(); - let mut vm = Vm::new(&mut ctx); - iai.run(|| vm.evaluate(id).unwrap()); -} - -fn bench_layout(iai: &mut Iai) { - let (mut ctx, id) = context(); - let mut vm = Vm::new(&mut ctx); - let module = vm.evaluate(id).unwrap(); - iai.run(|| module.template.layout_pages(&mut vm)); -} - fn bench_highlight(iai: &mut Iai) { let (ctx, id) = context(); let source = ctx.sources.get(id); iai.run(|| source.highlight(0 .. source.len_bytes(), |_, _| {})); } -fn bench_byte_to_utf16(iai: &mut Iai) { - let (ctx, id) = context(); - let source = ctx.sources.get(id); - let mut ranges = vec![]; - source.highlight(0 .. source.len_bytes(), |range, _| ranges.push(range)); - iai.run(|| { - ranges - .iter() - .map(|range| source.byte_to_utf16(range.start) - .. source.byte_to_utf16(range.end)) - .collect::>() - }); +fn bench_eval(iai: &mut Iai) { + let (mut ctx, id) = context(); + iai.run(|| ctx.evaluate(id).unwrap()); +} + +fn bench_layout(iai: &mut Iai) { + let (mut ctx, id) = context(); + let module = ctx.evaluate(id).unwrap(); + iai.run(|| module.template.layout(&mut ctx)); } fn bench_render(iai: &mut Iai) { diff --git a/src/eval/class.rs b/src/eval/class.rs index 5601fb0b6..5e1857d7b 100644 --- a/src/eval/class.rs +++ b/src/eval/class.rs @@ -2,8 +2,9 @@ use std::any::TypeId; use std::fmt::{self, Debug, Formatter, Write}; use std::hash::{Hash, Hasher}; -use super::{Args, Func, StyleMap, Template, Value, Vm}; +use super::{Args, Func, StyleMap, Template, Value}; use crate::diag::TypResult; +use crate::Context; /// A class of nodes. /// @@ -38,7 +39,7 @@ use crate::diag::TypResult; pub struct Class { name: &'static str, id: TypeId, - construct: fn(&mut Vm, &mut Args) -> TypResult, + construct: fn(&mut Context, &mut Args) -> TypResult, set: fn(&mut Args, &mut StyleMap) -> TypResult<()>, } @@ -81,8 +82,8 @@ impl Class { /// This parses both property and data arguments (in this order), styles the /// template constructed from the data with the style properties and wraps /// it in a value. - pub fn construct(&self, vm: &mut Vm, mut args: Args) -> TypResult { - let value = (self.construct)(vm, &mut args)?; + pub fn construct(&self, ctx: &mut Context, mut args: Args) -> TypResult { + let value = (self.construct)(ctx, &mut args)?; args.finish()?; Ok(value) } @@ -125,7 +126,7 @@ pub trait Construct { /// /// This is passed only the arguments that remain after execution of the /// class's set rule. - fn construct(vm: &mut Vm, args: &mut Args) -> TypResult