Skip to content

Commit

Permalink
dev(compiler): compile with env on stack
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin committed Nov 13, 2023
1 parent 2c68f76 commit 2a21468
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 32 deletions.
6 changes: 4 additions & 2 deletions cli/src/query_repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -230,7 +232,7 @@ impl ReplContext {
let compiled = self.driver.borrow_mut().with_stage_diag::<false, _>(
"compiling",
|driver: &mut CompileDriver| {
let doc = driver.compile()?;
let doc = driver.compile(&mut Default::default())?;
driver.query(line, &doc)
},
);
Expand Down
12 changes: 9 additions & 3 deletions compiler/src/service/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ where
if !self.enable_watch {
let compiled = self
.compiler
.with_stage_diag::<false, _>("compiling", |driver| driver.compile());
.with_stage_diag::<false, _>("compiling", |driver| {
driver.compile(&mut Default::default())
});
return compiled.is_some();
}

Expand All @@ -154,7 +156,9 @@ where
pub async fn spawn(mut self) -> Option<JoinHandle<()>> {
if !self.enable_watch {
self.compiler
.with_stage_diag::<false, _>("compiling", |driver| driver.compile());
.with_stage_diag::<false, _>("compiling", |driver| {
driver.compile(&mut Default::default())
});
return None;
}

Expand Down Expand Up @@ -226,7 +230,9 @@ where
// Compile the document.
self.latest_doc = self
.compiler
.with_stage_diag::<true, _>("compiling", |driver| driver.compile());
.with_stage_diag::<true, _>("compiling", |driver| {
driver.compile(&mut Default::default())
});

// Evict compilation cache.
comemo::evict(30);
Expand Down
17 changes: 9 additions & 8 deletions compiler/src/service/export.rs
Original file line number Diff line number Diff line change
@@ -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<typst::doc::Document>) -> SourceResult<()>;
Expand Down Expand Up @@ -53,8 +54,8 @@ impl<C: Compiler> CompileMiddleware for CompileExporter<C> {
&mut self.compiler
}

fn wrap_compile(&mut self) -> SourceResult<Arc<typst::doc::Document>> {
let doc = self.inner_mut().compile()?;
fn wrap_compile(&mut self, env: &mut CompileEnv) -> SourceResult<Arc<typst::doc::Document>> {
let doc = self.inner_mut().compile(env)?;
self.export(doc.clone())?;

Ok(doc)
Expand Down Expand Up @@ -167,7 +168,7 @@ impl<C: Compiler + ShadowApi> WorldExporter for DynamicLayoutCompiler<C> {

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 {:?}, {}",
Expand Down Expand Up @@ -204,12 +205,12 @@ impl<C: Compiler + ShadowApi> CompileMiddleware for DynamicLayoutCompiler<C> {
&mut self.compiler
}

fn wrap_compile(&mut self) -> SourceResult<Arc<TypstDocument>> {
fn wrap_compile(&mut self, env: &mut CompileEnv) -> SourceResult<Arc<TypstDocument>> {
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)
Expand Down
33 changes: 21 additions & 12 deletions compiler/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ pub trait WorkspaceProvider {
fn set_main_id(&mut self, id: TypstFileId);
}

#[derive(Clone, Default)]
pub struct CompileEnv {
pub tracer: Option<Tracer>,
}

pub trait Compiler {
type World: World;

Expand All @@ -67,7 +72,7 @@ pub trait Compiler {
fn reset(&mut self) -> SourceResult<()>;

/// Compile once from scratch.
fn pure_compile(&mut self) -> SourceResult<Arc<Document>> {
fn pure_compile(&mut self, env: &mut CompileEnv) -> SourceResult<Arc<Document>> {
self.reset()?;

let main_id = self.main_id();
Expand All @@ -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.
Expand All @@ -88,8 +97,8 @@ pub trait Compiler {
}

/// Compile once from scratch.
fn compile(&mut self) -> SourceResult<Arc<Document>> {
self.pure_compile()
fn compile(&mut self, env: &mut CompileEnv) -> SourceResult<Arc<Document>> {
self.pure_compile(env)
}

/// With **the compilation state**, query the matches for the selector.
Expand Down Expand Up @@ -189,8 +198,8 @@ pub trait CompileMiddleware {
}

/// Hooked compile once from scratch.
fn wrap_compile(&mut self) -> SourceResult<Arc<Document>> {
self.inner_mut().compile()
fn wrap_compile(&mut self, env: &mut CompileEnv) -> SourceResult<Arc<Document>> {
self.inner_mut().compile(env)
}

/// With **the compilation state**, hooked query the matches for the
Expand Down Expand Up @@ -227,8 +236,8 @@ impl<T: CompileMiddleware> Compiler for T {
}

#[inline]
fn pure_compile(&mut self) -> SourceResult<Arc<Document>> {
self.inner_mut().pure_compile()
fn pure_compile(&mut self, env: &mut CompileEnv) -> SourceResult<Arc<Document>> {
self.inner_mut().pure_compile(env)
}

#[inline]
Expand All @@ -237,8 +246,8 @@ impl<T: CompileMiddleware> Compiler for T {
}

#[inline]
fn compile(&mut self) -> SourceResult<Arc<Document>> {
self.wrap_compile()
fn compile(&mut self, env: &mut CompileEnv) -> SourceResult<Arc<Document>> {
self.wrap_compile(env)
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion fuzzers/incremental/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
15 changes: 12 additions & 3 deletions packages/compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}"))?;
Expand Down Expand Up @@ -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:?}"))?;
Expand All @@ -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<typst::model::Content> = self
.compiler
.query(selector, &doc)
Expand Down
4 changes: 3 additions & 1 deletion server/dev/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ fn compile_corpus(args: CompileCorpusArgs) {
driver.set_exporter(exporter);
driver.inner_mut().set_entry_file(entry);

driver.with_stage_diag::<true, _>("compiling", |driver| driver.compile());
driver.with_stage_diag::<true, _>("compiling", |driver| {
driver.compile(&mut Default::default())
});

// if status.code().unwrap() != 0 {
// eprintln!("compile corpus failed.");
Expand Down
2 changes: 1 addition & 1 deletion tests/heap-profile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl ArtifactCompiler {
],
);

driver.compile().unwrap();
driver.compile(&mut Default::default()).unwrap();

ArtifactBundle {
driver,
Expand Down

0 comments on commit 2a21468

Please sign in to comment.