Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/src/door.rs

18.3 KiB, 106 runs

created by r1870400018:66074, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! The door: every compile the wasm surface offers, as plain Rust, so that a native test drives exactly what
2//! the browser calls. [`crate::wasm`] holds only the translation between JavaScript values and these types.
3//!
4//! The evaluator serves every door, the live view's changed-only delta included. A project's files are sealed
5//! into the source map as the browser has them, so nothing the project does not hold resolves from the real
6//! filesystem, whatever the host. The source map, the image base directory and the package store are process
7//! globals, so a call takes one turn at a time. Columns on this surface count UTF-16 code units, as JavaScript
8//! counts them, and start at 1.
9
10use crate::caches::{
11 Budgets,
12 Caches,
13 Counters,
14};
15use crate::compile::{
16 Cols,
17 Diagnostic,
18 Report,
19 Session,
20 Severity,
21};
22use crate::delta::{
23 self,
24 Changed,
25 Kept,
26 PageAt,
27 PageMiss,
28 Window,
29};
30use crate::diag::DiagnosticKind;
31use crate::emit::sinks::{
32 Chunks,
33 DeltaSink,
34 PdfSink,
35 VectorSink,
36};
37use crate::eval::{
38 Engine,
39 World,
40};
41use crate::eval::eval::{
42 eval_string,
43 EvalMode,
44};
45use crate::eval::fixpoint::PageSink;
46use crate::eval::lib::data;
47use crate::eval::package::{
48 self,
49 PackageSpec,
50};
51use crate::eval::scope::Scope;
52use crate::eval::select;
53use crate::eval::value::Value;
54use crate::flow::text::FontStore;
55use crate::fonts::FontBook;
56use crate::syntax::Span;
57use crate::vfs;
58
59use oxedyne_fe2o3_core::prelude::*;
60
61use std::collections::HashMap;
62use std::collections::hash_map::DefaultHasher;
63use std::hash::{
64 Hash,
65 Hasher,
66};
67use std::path::{
68 Path,
69 PathBuf,
70};
71use std::sync::{
72 Arc,
73 Mutex,
74 MutexGuard,
75};
76
77// One call at a time: the source map, the image base directory and the package store are process globals.
78static TURN: Mutex<()> = Mutex::new(());
79
80/// Takes this call's turn; a turn a panicked call poisoned is still a turn.
81fn turn() -> MutexGuard<'static, ()> {
82 TURN.lock().unwrap_or_else(|p| p.into_inner())
83}
84
85/// The path a project's main file takes when it names none.
86pub const MAIN: &str = "/main.typ";
87
88/// The loop iterations a compile may spend, per layout pass, before the door refuses it. The host's own
89/// thread cannot stop a compile that has begun, so the door bounds the loops of one unless the host says
90/// otherwise with [`Instance::set_loop_budget`]. The command line has no such bound, as Typst has none.
91pub const LOOP_BUDGET: u64 = 10_000_000;
92
93/// What a call is given: the project's files by path in its own shadow file system, as Daimond sends them.
94#[derive(Clone, Debug, Default)]
95pub struct Project {
96 pub main: String, // empty for `/main.typ`
97 pub sources: Vec<(String, String)>, // path, text
98 pub assets: Vec<(String, Vec<u8>)>,
99 pub fonts: Vec<(String, Vec<u8>)>, // the name is not read: the face declares its own family
100 pub strict: bool,
101 pub known: Vec<u64>, // the delta's page ids the consumer already holds
102 pub want: Option<Window>, // the span of the delta's `order` whose changed pages are sent, all when none
103}
104
105impl Project {
106 /// A one-file project, `/main.typ`, not strict.
107 pub fn single(source: &str) -> Self {
108 Self { sources: vec![(MAIN.to_string(), source.to_string())], ..Self::default() }
109 }
110
111 fn main_path(&self) -> PathBuf {
112 PathBuf::from(if self.main.is_empty() { MAIN } else { self.main.as_str() })
113 }
114}
115
116/// A compile that produced its artefact: the artefact, every site not set as written, the packages the
117/// project asked for that nobody supplied, and what the session's caches did.
118pub struct Made<T> {
119 pub product: T,
120 pub report: Report,
121 pub needs: Vec<String>,
122 pub counters: Counters,
123}
124
125/// A compile that did not: the error's own site first, then the sites that follow it.
126#[derive(Debug)]
127pub struct Failure {
128 pub head: Diagnostic,
129 pub rest: Vec<Diagnostic>,
130 pub pages: Option<usize>, // set when the compile got as far as laying pages out
131 pub skipped: Option<String>,
132 pub needs: Vec<String>,
133}
134
135impl Failure {
136 /// An error with no report behind it, placed at the site it was raised with, else at `0:0` in `main`.
137 fn of(e: &Error<ErrTag>, main: &Path) -> Self {
138 Self {
139 head: Diagnostic::from_error(e, main),
140 rest: Vec::new(),
141 pages: None,
142 skipped: None,
143 needs: Vec::new(),
144 }
145 }
146
147 /// A fault of the engine's own, at `0:0` in `main`.
148 fn internal(main: &Path, message: &str) -> Self {
149 Self {
150 head: Diagnostic {
151 file: main.display().to_string(),
152 line: 0,
153 col: 0,
154 message: fmt!("internal: {}", message),
155 severity: Severity::Error,
156 kind: DiagnosticKind::Internal,
157 hint: None,
158 },
159 rest: Vec::new(),
160 pages: None,
161 skipped: None,
162 needs: Vec::new(),
163 }
164 }
165
166 /// A strict refusal: its head restates the first site, then every site follows.
167 fn refused(head: Diagnostic, report: &Report, needs: Vec<String>) -> Self {
168 Self {
169 head,
170 rest: report.diagnostics.clone(),
171 pages: Some(report.pages),
172 skipped: report.skipped.clone(),
173 needs,
174 }
175 }
176}
177
178/// One element a query found.
179#[derive(Clone, Debug, PartialEq)]
180pub struct Row {
181 pub kind: String, // the element's function: `heading`, `figure`, `metadata`
182 pub label: Option<String>,
183 pub page: Option<u32>, // 1-based, from the introspector
184 pub value: Option<String>, // JSON text of the field asked for, none where the element has none
185 pub title: Option<String>, // a heading's plain text
186 pub level: Option<i64>, // a heading's depth
187}
188
189/// A long-lived compiler: a [`Session`] holding the embedded faces parsed once and the introspector the
190/// next compile starts from, whether the last compile answers queries, and the delta's version tick.
191pub struct Instance {
192 session: Session,
193 fonts: Vec<u64>, // the fingerprints of the project fonts the session's store holds
194 good: bool, // the last compile laid pages without a refusal, so queries answer
195 version: u32, // the delta's tick
196 budget: Option<u64>, // the loop budget of a compile, none for no bound
197 kept: Option<Kept>, // the last delta's pages, drawn on request
198}
199
200// A font file's fingerprint, to tell a project's fonts unchanged from one call to the next.
201fn fingerprint(bytes: &[u8]) -> u64 {
202 let mut h = DefaultHasher::new();
203 bytes.hash(&mut h);
204 h.finish()
205}
206
207impl Default for Instance {
208 fn default() -> Self { Self::new() }
209}
210
211impl Instance {
212 /// Parses the embedded faces once. A face that will not parse is reported by the first compile, never
213 /// here, so a constructor cannot fail.
214 pub fn new() -> Self {
215 let fonts = match FontBook::embedded() {
216 Ok(book) => FontStore::with_base(Arc::new(book)),
217 Err(_) => FontStore::default(),
218 };
219 Self {
220 session: Session::new(fonts),
221 fonts: Vec::new(),
222 good: false,
223 version: 0,
224 budget: Some(LOOP_BUDGET),
225 kept: None,
226 }
227 }
228
229 /// Sets the loop iterations a compile may spend, per layout pass: a number, or `None` for no bound, by
230 /// which the host declares it can stop a compile itself. It is the host's option, held here and never
231 /// read from a project, which is document data and may arrive by sync.
232 pub fn set_loop_budget(&mut self, budget: Option<u64>) {
233 self.budget = budget;
234 }
235
236 /// The loop budget in force: [`LOOP_BUDGET`] until the host sets another.
237 pub fn loop_budget(&self) -> Option<u64> { self.budget }
238
239 /// Sets the bytes each of the session's caches may hold, from the next compile. Like the loop budget it
240 /// is the host's option and never a project's: a lower one slows a compile and changes nothing it makes.
241 /// It outlasts a panic, which drops the session's caches and keeps the budgets.
242 pub fn set_cache_budget(&mut self, budgets: Budgets) {
243 self.session.set_budgets(budgets);
244 }
245
246 /// The cache budgets in force: [`Budgets::default`] until the host sets others.
247 pub fn cache_budget(&self) -> Budgets { self.session.budgets() }
248
249 /// The session's caches, for the counters of the last compile and the configuration they were made under.
250 pub fn caches(&self) -> &Caches { self.session.caches() }
251
252 /// Compiles to PDF through the evaluator, cold, as an explicit output is: the file never depends on the
253 /// compiles before it. The chunks are the file; a host copies them out one by one.
254 pub fn compile_pdf(&mut self, p: &Project) -> Result<Made<Chunks>, Failure> {
255 let sink = match PdfSink::new() {
256 Ok(s) => s,
257 Err(e) => return Err(Failure::of(&e, &p.main_path())),
258 };
259 self.run(p, sink, true, |s| s.into_output())
260 }
261
262 /// Compiles to one SVG document per page through the evaluator, from the kept introspector.
263 pub fn compile_svg(&mut self, p: &Project) -> Result<Made<Vec<String>>, Failure> {
264 self.run(p, VectorSink::default(), false, |s| s.into_pages())
265 }
266
267 fn run<S, T, F>(&mut self, p: &Project, mut sink: S, cold: bool, take: F) -> Result<Made<T>, Failure>
268 where
269 S: PageSink,
270 F: FnOnce(S) -> Option<T>,
271 {
272 let _turn = turn();
273 let budget = self.budget;
274 let budgets = self.session.budgets(); // read before the compile lends its caches, budgets included, to the engine
275 let main = p.main_path();
276 // A failed compile answers no query, so the answer goes before the compile begins.
277 self.good = false;
278 let base = match self.session.fonts().base() {
279 Some(b) => b.clone(),
280 None => return Err(Failure::internal(&main, "the embedded font set could not be built")),
281 };
282 if let Err(e) = install(p, &main) {
283 let _ = vfs::clear();
284 return Err(Failure::of(&e, &main));
285 }
286 // The store is kept while the project's fonts are the same, with its parsed faces and shaping cache,
287 // and made afresh over the embedded faces when they are not, so that no project's fonts reach another's.
288 let held: Vec<u64> = p.fonts.iter().map(|(_, bytes)| fingerprint(bytes)).collect();
289 if held != self.fonts {
290 let mut fonts = FontStore::with_base(base.clone());
291 for (_, bytes) in &p.fonts {
292 fonts.add_bytes(bytes.clone());
293 }
294 self.session.set_fonts(fonts);
295 self.fonts = held;
296 }
297 let caught = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
298 self.session.compile(&main, Path::new("/"), &mut sink, None, budget, cold)
299 }));
300 let _ = vfs::clear();
301 let done = match caught {
302 Ok(Ok(d)) => d,
303 Ok(Err(e)) => return Err(Failure::of(&e, &main)),
304 Err(_) => {
305 // A panic leaves the session in a state nobody has examined, its caches included, so the
306 // whole session goes and the next compile begins from the embedded faces and the budgets.
307 // The project's fonts are added again then.
308 self.session = Session::with_budgets(FontStore::with_base(base), budgets);
309 self.fonts = Vec::new();
310 return Err(Failure::internal(&main,
311 "the compiler panicked while compiling, so no document was produced"));
312 },
313 };
314 let needs = done.needs();
315 let report = done.report_in(Cols::Utf16);
316 let counters = done.counters;
317 if let Err(e) = &done.laid {
318 // The first error is the one the fixpoint stopped on; the sites after it follow.
319 let mut rest = report.diagnostics.clone();
320 let head = match done.engine.diags.iter().position(|d| d.is_error()) {
321 Some(i) => rest.remove(i),
322 None => Diagnostic::from_error(e, &main),
323 };
324 return Err(Failure { head, rest, pages: None, skipped: report.skipped.clone(), needs });
325 }
326 // A pack family the host has not yet supplied (a build without the embedded faces) refuses the compile
327 // as a missing package does: pages set without their face would only mislead, and `needs` names it.
328 if let Some(i) = done.engine.diags.iter().position(|d| d.is_error() && d.need.is_some()) {
329 let mut rest = report.diagnostics.clone();
330 let head = rest.remove(i);
331 return Err(Failure { head, rest, pages: None, skipped: report.skipped.clone(), needs });
332 }
333 if p.strict {
334 if let Some(head) = report.strict_failure(&main) {
335 return Err(Failure::refused(head, &report, needs));
336 }
337 }
338 self.good = true;
339 match take(sink) {
340 Some(product) => Ok(Made { product, report, needs, counters }),
341 None => Err(Failure::internal(&main, "the fixpoint ended with no output")),
342 }
343 }
344
345 /// The families a compile of `p` can set by name, sorted: the embedded faces and the family every face
346 /// of every project font declares. It is read from the font book a compile resolves `font:` against, so
347 /// a family is listed exactly when a compile finds it.
348 pub fn font_families(&self, p: Option<&Project>) -> Vec<String> {
349 let base = match self.session.fonts().base() {
350 Some(b) => b.clone(),
351 None => return Vec::new(),
352 };
353 let mut store = FontStore::with_base(base);
354 if let Some(p) = p {
355 for (_, bytes) in &p.fonts {
356 store.add_bytes(bytes.clone());
357 }
358 }
359 match store.book() {
360 Ok(book) => book.family_names(),
361 Err(_) => Vec::new(),
362 }
363 }
364
365 /// What `selector`, a Typst selector expression, finds in the last good compile, as `typst query` finds
366 /// it. `field` names the field each row's `value` carries, `value` where empty. It answers `None`, never
367 /// a wrong answer, when nothing has compiled, the selector does not evaluate, or a field cannot be
368 /// written as Typst writes it.
369 pub fn query(&self, selector: &str, field: &str) -> Option<Vec<Row>> {
370 let intro = match (self.good, self.session.intro()) {
371 (true, Some(i)) => i.clone(),
372 _ => return None,
373 };
374 let _turn = turn();
375 // A fresh engine over the kept introspector evaluates the selector as `typst query` does.
376 let mut engine = Engine::new(World::new(PathBuf::from("/")));
377 engine.intro = intro.clone();
378 let span = Span::detached();
379 let found = match eval_string(&mut engine, selector, EvalMode::Code, Scope::default(), span) {
380 Ok(v) => v,
381 Err(_) => return None,
382 };
383 let sel = match select::cast(&mut engine, span, found) {
384 Ok(s) => s,
385 Err(_) => return None,
386 };
387 let hits = match intro.query(&sel) {
388 Ok(h) => h,
389 Err(_) => return None,
390 };
391 let want = if field.is_empty() { "value" } else { field };
392 let mut rows = Vec::with_capacity(hits.len());
393 for c in hits {
394 let kind = match c.kind() {
395 Some(k) => k.name().to_string(),
396 None => return None,
397 };
398 // A field that cannot be written as Typst writes it spoils the whole answer, not just its row.
399 let value = match c.field(want) {
400 Some(v) => match data::plain_json(v) {
401 Some(j) => Some(j),
402 None => return None,
403 },
404 None => None,
405 };
406 let (title, level) = if kind == "heading" {
407 let title = match c.field("body") {
408 Some(Value::Content(b)) => b.plain_text(),
409 _ => String::new(),
410 };
411 let level = match c.field("level") {
412 Some(Value::Int(l)) => *l,
413 _ => 1,
414 };
415 (Some(title), Some(level))
416 } else {
417 (None, None)
418 };
419 rows.push(Row {
420 kind,
421 label: c.label().map(|l| l.as_str().to_string()),
422 page: c.location().and_then(|l| intro.page(l)),
423 value,
424 title,
425 level,
426 });
427 }
428 Some(rows)
429 }
430
431 /// The changed-only page delta through the evaluator, the live view's path, warm: it starts from the
432 /// introspector of the compile before it. `known` is the page ids the
433 /// consumer holds; the version is this instance's tick, stepped by a compile that produced a delta and
434 /// left where it was by one that did not, a strict refusal included, so a consumer that dispatches
435 /// compiles without awaiting each can discard a stale return by its version.
436 pub fn compile_delta(&mut self, p: &Project) -> Result<Made<delta::PageDelta>, Failure> {
437 self.compile_delta_into(p, Vec::new()).map(|m| Made {
438 product: delta::PageDelta::of(m.product.0, m.product.1),
439 report: m.report,
440 needs: m.needs,
441 counters: m.counters,
442 })
443 }
444
445 /// As [`compile_delta`](Self::compile_delta), each changed page's SVG handed to `out` the moment it is
446 /// drawn, so the compile holds none of them: the browser door pushes each into a JavaScript array.
447 ///
448 /// A delta keeps the laid-out pages of its compile, every one of them, for [`page`](Self::page) to draw
449 /// on request. They replace the pages the delta before kept; a compile that fails leaves those standing,
450 /// since the consumer still shows them.
451 pub fn compile_delta_into<C: Changed>(&mut self, p: &Project, out: C)
452 -> Result<Made<(delta::Head, C)>, Failure>
453 {
454 let sink = DeltaSink::new(&p.known, self.version, p.want, out);
455 let made = self.run(p, sink, false, |s| s.into_delta());
456 match made {
457 Ok(m) => {
458 let (head, out, kept) = m.product;
459 self.version = head.version;
460 self.kept = Some(kept);
461 Ok(Made { product: (head, out), report: m.report, needs: m.needs, counters: m.counters })
462 },
463 Err(f) => Err(f),
464 }
465 }
466
467 /// The id, index in `order` and SVG of one page of the last delta, drawn from the pages it kept, byte
468 /// for byte the SVG the delta sends for that id. The inner error says why there is no such page.
469 pub fn page(&self, at: PageAt) -> Result<Result<(u64, usize, String), PageMiss>, Failure> {
470 let kept = match &self.kept {
471 Some(k) => k,
472 None => return Ok(Err(PageMiss::Uncompiled)),
473 };
474 let _turn = turn();
475 let main = Path::new(MAIN);
476 match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| kept.render(at))) {
477 Ok(Ok(r)) => Ok(r),
478 Ok(Err(e)) => Err(Failure::of(&e, main)),
479 Err(_) => Err(Failure::internal(main, "the compiler panicked while drawing a page")),
480 }
481 }
482
483 /// The pages the last delta kept, none before the first.
484 pub fn kept(&self) -> Option<&Kept> { self.kept.as_ref() }
485}
486
487/// Hands a package's files to the engine, `spec` as `@namespace/name:version`; the count of files held.
488pub fn supply_package(spec: &str, files: Vec<(String, Vec<u8>)>) -> Outcome<usize> {
489 let _turn = turn();
490 let spec = res!(package::parse_spec(spec));
491 package::supply(&spec, files)
492}
493
494/// Every package supplied, as `@namespace/name:version`, in order.
495pub fn packages() -> Outcome<Vec<String>> {
496 let _turn = turn();
497 Ok(res!(package::supplied()).iter().map(|s| s.to_string()).collect())
498}
499
500/// Withdraws a supplied package; false when none was held under `spec`.
501pub fn withdraw_package(spec: &str) -> Outcome<bool> {
502 let _turn = turn();
503 let spec: PackageSpec = res!(package::parse_spec(spec));
504 package::withdraw(&spec)
505}
506
507/// Seals the project's sources and assets into the source map. A project that does not hold its main
508/// file is an error before any compile.
509fn install(p: &Project, main: &Path) -> Outcome<()> {
510 let mut files: HashMap<PathBuf, Vec<u8>> = HashMap::new();
511 for (path, text) in &p.sources {
512 files.insert(PathBuf::from(path), text.clone().into_bytes());
513 }
514 for (path, bytes) in &p.assets {
515 files.insert(PathBuf::from(path), bytes.clone());
516 }
517 if !files.keys().any(|k| k == main) {
518 return Err(err!("The project has no source for its main file {:?}.", main; Input, Missing, File));
519 }
520 vfs::install_sealed(files)
521}