Add TrackedMut<Sink> as an argument

This commit is contained in:
+merlan #flirora 2025-04-02 11:21:16 -04:00
parent 9682d40c32
commit 562c3c31e1
5 changed files with 19 additions and 12 deletions

View File

@ -2,6 +2,7 @@ use comemo::Track;
use ecow::{eco_format, EcoString}; use ecow::{eco_format, EcoString};
use serde::Serialize; use serde::Serialize;
use typst::diag::{bail, HintedStrResult, StrResult, Warned}; use typst::diag::{bail, HintedStrResult, StrResult, Warned};
use typst::engine::Sink;
use typst::foundations::{Content, IntoValue, LocatableSelector, Scope}; use typst::foundations::{Content, IntoValue, LocatableSelector, Scope};
use typst::layout::PagedDocument; use typst::layout::PagedDocument;
use typst::syntax::Span; use typst::syntax::Span;
@ -62,9 +63,9 @@ fn retrieve(
Span::detached(), Span::detached(),
EvalMode::Code, EvalMode::Code,
Scope::default(), Scope::default(),
)
// TODO: propagate warnings // TODO: propagate warnings
.map(|(result, _sink)| result) Sink::new().track_mut(),
)
.map_err(|errors| { .map_err(|errors| {
let mut message = EcoString::from("failed to evaluate selector"); let mut message = EcoString::from("failed to evaluate selector");
for (i, error) in errors.into_iter().enumerate() { for (i, error) in errors.into_iter().enumerate() {

View File

@ -105,7 +105,8 @@ pub fn eval_string(
span: Span, span: Span,
mode: EvalMode, mode: EvalMode,
scope: Scope, scope: Scope,
) -> SourceResult<(Value, Sink)> { sink: TrackedMut<Sink>,
) -> SourceResult<Value> {
let mut root = match mode { let mut root = match mode {
EvalMode::Code => parse_code(string), EvalMode::Code => parse_code(string),
EvalMode::Markup => parse(string), EvalMode::Markup => parse(string),
@ -121,7 +122,6 @@ pub fn eval_string(
} }
// Prepare the engine. // Prepare the engine.
let mut sink = Sink::new();
let introspector = Introspector::default(); let introspector = Introspector::default();
let traced = Traced::default(); let traced = Traced::default();
let engine = Engine { let engine = Engine {
@ -129,7 +129,7 @@ pub fn eval_string(
world, world,
introspector: introspector.track(), introspector: introspector.track(),
traced: traced.track(), traced: traced.track(),
sink: sink.track_mut(), sink,
route: Route::default(), route: Route::default(),
}; };
@ -158,7 +158,7 @@ pub fn eval_string(
bail!(flow.forbidden()); bail!(flow.forbidden());
} }
Ok((output, sink)) Ok(output)
} }
/// Evaluate an expression. /// Evaluate an expression.

View File

@ -68,6 +68,7 @@ pub use self::target_::*;
pub use self::ty::*; pub use self::ty::*;
pub use self::value::*; pub use self::value::*;
pub use self::version::*; pub use self::version::*;
use comemo::Track;
pub use typst_macros::{scope, ty}; pub use typst_macros::{scope, ty};
#[rustfmt::skip] #[rustfmt::skip]
@ -82,6 +83,7 @@ use typst_syntax::Spanned;
use crate::diag::{bail, SourceResult, StrResult}; use crate::diag::{bail, SourceResult, StrResult};
use crate::engine::Engine; use crate::engine::Engine;
use crate::engine::Sink;
use crate::routines::EvalMode; use crate::routines::EvalMode;
use crate::{Feature, Features}; use crate::{Feature, Features};
@ -297,13 +299,15 @@ pub fn eval(
for (key, value) in dict { for (key, value) in dict {
scope.bind(key.into(), Binding::new(value, span)); scope.bind(key.into(), Binding::new(value, span));
} }
let (result, sink) = (engine.routines.eval_string)( let mut sink = Sink::new();
let result = (engine.routines.eval_string)(
engine.routines, engine.routines,
engine.world, engine.world,
&text, &text,
span, span,
mode, mode,
scope, scope,
sink.track_mut(),
)?; )?;
for warning in sink.warnings() { for warning in sink.warnings() {

View File

@ -6,7 +6,7 @@ use std::num::NonZeroUsize;
use std::path::Path; use std::path::Path;
use std::sync::{Arc, LazyLock}; use std::sync::{Arc, LazyLock};
use comemo::Tracked; use comemo::{Track, Tracked};
use ecow::{eco_format, EcoString, EcoVec}; use ecow::{eco_format, EcoString, EcoVec};
use hayagriva::archive::ArchivedStyle; use hayagriva::archive::ArchivedStyle;
use hayagriva::io::BibLaTeXError; use hayagriva::io::BibLaTeXError;
@ -20,7 +20,7 @@ use typst_syntax::{Span, Spanned};
use typst_utils::{Get, ManuallyHash, NonZeroExt, PicoStr}; use typst_utils::{Get, ManuallyHash, NonZeroExt, PicoStr};
use crate::diag::{bail, error, At, FileError, HintedStrResult, SourceResult, StrResult}; use crate::diag::{bail, error, At, FileError, HintedStrResult, SourceResult, StrResult};
use crate::engine::Engine; use crate::engine::{Engine, Sink};
use crate::foundations::{ use crate::foundations::{
elem, Bytes, CastInfo, Content, Derived, FromValue, IntoValue, Label, NativeElement, elem, Bytes, CastInfo, Content, Derived, FromValue, IntoValue, Label, NativeElement,
OneOrMultiple, Packed, Reflect, Scope, Show, ShowSet, Smart, StyleChain, Styles, OneOrMultiple, Packed, Reflect, Scope, Show, ShowSet, Smart, StyleChain, Styles,
@ -1003,9 +1003,10 @@ impl ElemRenderer<'_> {
self.span, self.span,
EvalMode::Math, EvalMode::Math,
Scope::new(), Scope::new(),
)
// TODO: propagate warnings // TODO: propagate warnings
.map(|(result, _sink)| Value::display(result)) Sink::new().track_mut(),
)
.map(Value::display)
.unwrap_or_else(|_| TextElem::packed(math).spanned(self.span)) .unwrap_or_else(|_| TextElem::packed(math).spanned(self.span))
} }

View File

@ -59,7 +59,8 @@ routines! {
span: Span, span: Span,
mode: EvalMode, mode: EvalMode,
scope: Scope, scope: Scope,
) -> SourceResult<(Value, Sink)> sink: TrackedMut<Sink>,
) -> SourceResult<Value>
/// Call the closure in the context with the arguments. /// Call the closure in the context with the arguments.
fn eval_closure( fn eval_closure(