typst_shim/stable/
eval.rsuse comemo::Track;
use typst::diag::SourceResult;
use typst::engine::{Engine, Route, Sink, Traced};
use typst::foundations::{Context, Func, Module, Value};
use typst::introspection::Introspector;
use typst::syntax::Source;
use typst::World;
pub use typst_eval::*;
pub fn eval_compat(world: &dyn World, source: &Source) -> SourceResult<Module> {
let route = Route::default();
let traced = Traced::default();
let mut sink = Sink::default();
typst_eval::eval(
&typst::ROUTINES,
world.track(),
traced.track(),
sink.track_mut(),
route.track(),
source,
)
}
pub struct TypstEngine<'a> {
pub introspector: Introspector,
pub traced: Traced,
pub route: Route<'static>,
pub sink: Sink,
pub world: &'a dyn World,
}
impl<'a> TypstEngine<'a> {
pub fn new(world: &'a dyn World) -> Self {
Self {
introspector: Introspector::default(),
traced: Traced::default(),
route: Route::default(),
sink: Sink::default(),
world,
}
}
pub fn as_engine(&'a mut self) -> Engine<'a> {
Engine {
routines: &typst::ROUTINES,
world: self.world.track(),
introspector: self.introspector.track(),
traced: self.traced.track(),
sink: self.sink.track_mut(),
route: self.route.clone(),
}
}
pub fn apply(&'a mut self, func: &Func, ctx: Context, args: Vec<Value>) -> SourceResult<Value> {
func.call(&mut self.as_engine(), ctx.track(), args)
}
pub fn call(&'a mut self, func: &Func, ctx: Context) -> SourceResult<Value> {
self.apply(func, ctx, vec![])
}
}