From 1247c6d8e102cc40c3ec0d913a28ea904e4e4dec Mon Sep 17 00:00:00 2001 From: PgBiel <9021226+PgBiel@users.noreply.github.com> Date: Tue, 30 Apr 2024 09:49:18 -0300 Subject: Add `std` module for names in the standard library (#4038) --- crates/typst/src/foundations/scope.rs | 24 +++++++++++++++++++++--- crates/typst/src/lib.rs | 8 ++++++-- 2 files changed, 27 insertions(+), 5 deletions(-) (limited to 'crates') diff --git a/crates/typst/src/foundations/scope.rs b/crates/typst/src/foundations/scope.rs index caa82e13..870fa5b0 100644 --- a/crates/typst/src/foundations/scope.rs +++ b/crates/typst/src/foundations/scope.rs @@ -48,8 +48,14 @@ impl<'a> Scopes<'a> { pub fn get(&self, var: &str) -> HintedStrResult<&Value> { std::iter::once(&self.top) .chain(self.scopes.iter().rev()) - .chain(self.base.map(|base| base.global.scope())) .find_map(|scope| scope.get(var)) + .or_else(|| { + self.base.and_then(|base| match base.global.scope().get(var) { + Some(value) => Some(value), + None if var == "std" => Some(&base.std), + None => None, + }) + }) .ok_or_else(|| unknown_variable(var)) } @@ -57,8 +63,14 @@ impl<'a> Scopes<'a> { pub fn get_in_math(&self, var: &str) -> HintedStrResult<&Value> { std::iter::once(&self.top) .chain(self.scopes.iter().rev()) - .chain(self.base.map(|base| base.math.scope())) .find_map(|scope| scope.get(var)) + .or_else(|| { + self.base.and_then(|base| match base.math.scope().get(var) { + Some(value) => Some(value), + None if var == "std" => Some(&base.std), + None => None, + }) + }) .ok_or_else(|| unknown_variable(var)) } @@ -69,13 +81,19 @@ impl<'a> Scopes<'a> { .find_map(|scope| scope.get_mut(var)) .ok_or_else(|| { match self.base.and_then(|base| base.global.scope().get(var)) { - Some(_) => eco_format!("cannot mutate a constant: {}", var).into(), + Some(_) => cannot_mutate_constant(var), + _ if var == "std" => cannot_mutate_constant(var), _ => unknown_variable(var), } })? } } +#[cold] +fn cannot_mutate_constant(var: &str) -> HintedString { + eco_format!("cannot mutate a constant: {}", var).into() +} + /// The error message when a variable is not found. #[cold] fn unknown_variable(var: &str) -> HintedString { diff --git a/crates/typst/src/lib.rs b/crates/typst/src/lib.rs index 35915e2b..f41fb605 100644 --- a/crates/typst/src/lib.rs +++ b/crates/typst/src/lib.rs @@ -67,7 +67,7 @@ use crate::diag::{warning, FileResult, SourceDiagnostic, SourceResult}; use crate::engine::{Engine, Route}; use crate::eval::Tracer; use crate::foundations::{ - Array, Bytes, Content, Datetime, Dict, Module, Scope, StyleChain, Styles, + Array, Bytes, Content, Datetime, Dict, Module, Scope, StyleChain, Styles, Value, }; use crate::introspection::{Introspector, Locator}; use crate::layout::{Alignment, Dir, LayoutRoot}; @@ -297,6 +297,9 @@ pub struct Library { /// The default style properties (for page size, font selection, and /// everything else configurable via set and show rules). pub styles: Styles, + /// The standard library as a value. + /// Used to provide the `std` variable. + pub std: Value, } impl Library { @@ -333,7 +336,8 @@ impl LibraryBuilder { let math = math::module(); let inputs = self.inputs.unwrap_or_default(); let global = global(math.clone(), inputs); - Library { global, math, styles: Styles::new() } + let std = Value::Module(global.clone()); + Library { global, math, styles: Styles::new(), std } } } -- cgit v1.2.3