From 2a214689ebbd20ddd64d0b1bea9d25498a87ed21 Mon Sep 17 00:00:00 2001 From: Myriad-Dreamin Date: Sat, 11 Nov 2023 16:45:13 +0800 Subject: [PATCH] dev(compiler): compile with env on stack --- cli/src/query_repl.rs | 6 ++++-- compiler/src/service/compile.rs | 12 +++++++++--- compiler/src/service/export.rs | 17 +++++++++-------- compiler/src/service/mod.rs | 33 +++++++++++++++++++++------------ fuzzers/incremental/src/main.rs | 2 +- packages/compiler/src/lib.rs | 15 ++++++++++++--- server/dev/src/main.rs | 4 +++- tests/heap-profile/src/lib.rs | 2 +- tests/integration/src/lib.rs | 2 +- 9 files changed, 61 insertions(+), 32 deletions(-) diff --git a/cli/src/query_repl.rs b/cli/src/query_repl.rs index 2835697b..3d3de496 100644 --- a/cli/src/query_repl.rs +++ b/cli/src/query_repl.rs @@ -124,7 +124,9 @@ impl Completer for ReplContext { driver.world.reset(); let typst_completions = driver .with_shadow_file_by_id(main_id, dyn_content.as_bytes().into(), |driver| { - let frames = driver.compile().map(|d| d.take().pages); + let frames = driver + .compile(&mut Default::default()) + .map(|d| d.take().pages); let frames = frames.as_ref().map(|v| v.as_slice()).unwrap_or_default(); let source = driver.world.main(); Ok(autocomplete(&driver.world, frames, &source, cursor, true)) @@ -230,7 +232,7 @@ impl ReplContext { let compiled = self.driver.borrow_mut().with_stage_diag::( "compiling", |driver: &mut CompileDriver| { - let doc = driver.compile()?; + let doc = driver.compile(&mut Default::default())?; driver.query(line, &doc) }, ); diff --git a/compiler/src/service/compile.rs b/compiler/src/service/compile.rs index 62ee23ab..9b5e793d 100644 --- a/compiler/src/service/compile.rs +++ b/compiler/src/service/compile.rs @@ -137,7 +137,9 @@ where if !self.enable_watch { let compiled = self .compiler - .with_stage_diag::("compiling", |driver| driver.compile()); + .with_stage_diag::("compiling", |driver| { + driver.compile(&mut Default::default()) + }); return compiled.is_some(); } @@ -154,7 +156,9 @@ where pub async fn spawn(mut self) -> Option> { if !self.enable_watch { self.compiler - .with_stage_diag::("compiling", |driver| driver.compile()); + .with_stage_diag::("compiling", |driver| { + driver.compile(&mut Default::default()) + }); return None; } @@ -226,7 +230,9 @@ where // Compile the document. self.latest_doc = self .compiler - .with_stage_diag::("compiling", |driver| driver.compile()); + .with_stage_diag::("compiling", |driver| { + driver.compile(&mut Default::default()) + }); // Evict compilation cache. comemo::evict(30); diff --git a/compiler/src/service/export.rs b/compiler/src/service/export.rs index e95f8af0..02a15ea1 100644 --- a/compiler/src/service/export.rs +++ b/compiler/src/service/export.rs @@ -1,10 +1,11 @@ use std::{path::PathBuf, sync::Arc}; use crate::ShadowApi; -use typst::diag::SourceResult; +use ecow::EcoVec; +use typst::diag::{SourceDiagnostic, SourceResult}; use typst_ts_core::{exporter_builtins::GroupExporter, DynExporter, TypstDocument}; -use super::{CompileMiddleware, Compiler}; +use super::{CompileEnv, CompileMiddleware, Compiler}; pub trait WorldExporter { fn export(&mut self, output: Arc) -> SourceResult<()>; @@ -53,8 +54,8 @@ impl CompileMiddleware for CompileExporter { &mut self.compiler } - fn wrap_compile(&mut self) -> SourceResult> { - let doc = self.inner_mut().compile()?; + fn wrap_compile(&mut self, env: &mut CompileEnv) -> SourceResult> { + let doc = self.inner_mut().compile(env)?; self.export(doc.clone())?; Ok(doc) @@ -167,7 +168,7 @@ impl WorldExporter for DynamicLayoutCompiler { self.with_shadow_file_by_id(variable_file, variables.as_bytes().into(), |this| { // compile and export document - let output = this.inner_mut().compile()?; + let output = this.inner_mut().compile(&mut Default::default())?; svg_exporter.render(current_width, output); log::trace!( "rerendered {} at {:?}, {}", @@ -204,12 +205,12 @@ impl CompileMiddleware for DynamicLayoutCompiler { &mut self.compiler } - fn wrap_compile(&mut self) -> SourceResult> { + fn wrap_compile(&mut self, env: &mut CompileEnv) -> SourceResult> { if !self.enable_dynamic_layout { - return self.inner_mut().compile(); + return self.inner_mut().compile(env); } - let pure_doc = self.inner_mut().compile()?; + let pure_doc = self.inner_mut().compile(env)?; self.export(pure_doc.clone())?; Ok(pure_doc) diff --git a/compiler/src/service/mod.rs b/compiler/src/service/mod.rs index 2ec6fc0c..10c7fae4 100644 --- a/compiler/src/service/mod.rs +++ b/compiler/src/service/mod.rs @@ -54,6 +54,11 @@ pub trait WorkspaceProvider { fn set_main_id(&mut self, id: TypstFileId); } +#[derive(Clone, Default)] +pub struct CompileEnv { + pub tracer: Option, +} + pub trait Compiler { type World: World; @@ -67,7 +72,7 @@ pub trait Compiler { fn reset(&mut self) -> SourceResult<()>; /// Compile once from scratch. - fn pure_compile(&mut self) -> SourceResult> { + fn pure_compile(&mut self, env: &mut CompileEnv) -> SourceResult> { self.reset()?; let main_id = self.main_id(); @@ -77,9 +82,13 @@ pub trait Compiler { .hint(AtFile(main_id)) .at(Span::detached())?; - let mut tracer = Tracer::default(); - // compile and export document - typst::compile(self.world(), &mut tracer).map(Arc::new) + let res = match env.tracer.as_mut() { + Some(tracer) => typst::compile(self.world(), tracer), + None => typst::compile(self.world(), &mut Tracer::default()), + }; + + // compile document + res.map(Arc::new) } /// With **the compilation state**, query the matches for the selector. @@ -88,8 +97,8 @@ pub trait Compiler { } /// Compile once from scratch. - fn compile(&mut self) -> SourceResult> { - self.pure_compile() + fn compile(&mut self, env: &mut CompileEnv) -> SourceResult> { + self.pure_compile(env) } /// With **the compilation state**, query the matches for the selector. @@ -189,8 +198,8 @@ pub trait CompileMiddleware { } /// Hooked compile once from scratch. - fn wrap_compile(&mut self) -> SourceResult> { - self.inner_mut().compile() + fn wrap_compile(&mut self, env: &mut CompileEnv) -> SourceResult> { + self.inner_mut().compile(env) } /// With **the compilation state**, hooked query the matches for the @@ -227,8 +236,8 @@ impl Compiler for T { } #[inline] - fn pure_compile(&mut self) -> SourceResult> { - self.inner_mut().pure_compile() + fn pure_compile(&mut self, env: &mut CompileEnv) -> SourceResult> { + self.inner_mut().pure_compile(env) } #[inline] @@ -237,8 +246,8 @@ impl Compiler for T { } #[inline] - fn compile(&mut self) -> SourceResult> { - self.wrap_compile() + fn compile(&mut self, env: &mut CompileEnv) -> SourceResult> { + self.wrap_compile(env) } #[inline] diff --git a/fuzzers/incremental/src/main.rs b/fuzzers/incremental/src/main.rs index 02116db0..3ff4661f 100644 --- a/fuzzers/incremental/src/main.rs +++ b/fuzzers/incremental/src/main.rs @@ -94,7 +94,7 @@ pub fn test_compiler( let doc = driver .with_shadow_file_by_id(main_id, content.as_bytes().into(), |driver| { - driver.compile() + driver.compile(&mut Default::default()) }) .unwrap(); diff --git a/packages/compiler/src/lib.rs b/packages/compiler/src/lib.rs index de36a295..10a982aa 100644 --- a/packages/compiler/src/lib.rs +++ b/packages/compiler/src/lib.rs @@ -186,7 +186,10 @@ impl TypstCompiler { ); // compile and export document - let doc = self.compiler.compile().map_err(|e| format!("{e:?}"))?; + let doc = self + .compiler + .compile(&mut Default::default()) + .map_err(|e| format!("{e:?}"))?; let data = ast_exporter .export(self.compiler.world(), doc) .map_err(|e| format!("{e:?}"))?; @@ -262,7 +265,10 @@ impl TypstCompiler { } }; - let doc = self.compiler.compile().map_err(|e| format!("{e:?}"))?; + let doc = self + .compiler + .compile(&mut Default::default()) + .map_err(|e| format!("{e:?}"))?; let artifact_bytes = vec_exporter .export(self.compiler.world(), doc) .map_err(|e| format!("{e:?}"))?; @@ -278,7 +284,10 @@ impl TypstCompiler { self.compiler .set_entry_file(Path::new(&main_file_path).to_owned()); - let doc = self.compiler.compile().map_err(|e| format!("{e:?}"))?; + let doc = self + .compiler + .compile(&mut Default::default()) + .map_err(|e| format!("{e:?}"))?; let elements: Vec = self .compiler .query(selector, &doc) diff --git a/server/dev/src/main.rs b/server/dev/src/main.rs index 0c0db8cb..01f3b0a2 100644 --- a/server/dev/src/main.rs +++ b/server/dev/src/main.rs @@ -67,7 +67,9 @@ fn compile_corpus(args: CompileCorpusArgs) { driver.set_exporter(exporter); driver.inner_mut().set_entry_file(entry); - driver.with_stage_diag::("compiling", |driver| driver.compile()); + driver.with_stage_diag::("compiling", |driver| { + driver.compile(&mut Default::default()) + }); // if status.code().unwrap() != 0 { // eprintln!("compile corpus failed."); diff --git a/tests/heap-profile/src/lib.rs b/tests/heap-profile/src/lib.rs index e26dd027..2afa0bb4 100644 --- a/tests/heap-profile/src/lib.rs +++ b/tests/heap-profile/src/lib.rs @@ -45,7 +45,7 @@ pub fn test_compiler( driver .with_shadow_file_by_id(main_id, content.as_bytes().into(), |driver| { - driver.compile() + driver.compile(&mut Default::default()) }) .unwrap(); diff --git a/tests/integration/src/lib.rs b/tests/integration/src/lib.rs index fd09a887..bea7a7eb 100644 --- a/tests/integration/src/lib.rs +++ b/tests/integration/src/lib.rs @@ -89,7 +89,7 @@ impl ArtifactCompiler { ], ); - driver.compile().unwrap(); + driver.compile(&mut Default::default()).unwrap(); ArtifactBundle { driver,