git.delta.rocks / jrsonnet / refs/commits / 284612bfbf1c

difftreelog

refactor! implement stack size limit using thread local

Yaroslav Bolyukin2022-10-25parent: #7b8b823.patch.diff
in: master

18 files changed

modifiedbindings/jsonnet/src/lib.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -85,8 +85,8 @@
 
 /// Set the maximum stack depth.
 #[no_mangle]
-pub extern "C" fn jsonnet_max_stack(vm: &State, v: c_uint) {
-	vm.settings_mut().max_stack = v as usize;
+pub extern "C" fn jsonnet_max_stack(_vm: &State, _v: c_uint) {
+	todo!()
 }
 
 /// Set the number of objects required before a garbage collection cycle is allowed.
modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -5,7 +5,7 @@
 
 use clap::{AppSettings, IntoApp, Parser};
 use clap_complete::Shell;
-use jrsonnet_cli::{ConfigureState, GcOpts, GeneralOpts, ManifestOpts, OutputOpts};
+use jrsonnet_cli::{ConfigureState, GeneralOpts, ManifestOpts, OutputOpts};
 use jrsonnet_evaluator::{error::LocError, State};
 
 #[cfg(feature = "mimalloc")]
@@ -60,8 +60,6 @@
 	output: OutputOpts,
 	#[clap(flatten)]
 	debug: DebugOpts,
-	#[clap(flatten)]
-	gc: GcOpts,
 }
 
 fn main() {
@@ -113,7 +111,6 @@
 }
 
 fn main_catch(opts: Opts) -> bool {
-	let _printer = opts.gc.stats_printer();
 	let s = State::default();
 	if let Err(e) = main_real(&s, opts) {
 		if let Error::Evaluation(e) = e {
@@ -127,7 +124,7 @@
 }
 
 fn main_real(s: &State, opts: Opts) -> Result<(), Error> {
-	opts.general.configure(s)?;
+	let _guards = opts.general.configure(s)?;
 	opts.manifest.configure(s)?;
 
 	let input = opts.input.input.ok_or(Error::MissingInputArgument)?;
modifiedcrates/jrsonnet-cli/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/lib.rs
+++ b/crates/jrsonnet-cli/src/lib.rs
@@ -3,10 +3,12 @@
 mod tla;
 mod trace;
 
-use std::{env, path::PathBuf};
+use std::{env, marker::PhantomData, path::PathBuf};
 
 use clap::Parser;
-use jrsonnet_evaluator::{error::Result, FileImportResolver, State};
+use jrsonnet_evaluator::{
+	error::Result, stack::StackDepthLimitOverrideGuard, FileImportResolver, State,
+};
 use jrsonnet_gcmodule::with_thread_object_space;
 pub use manifest::*;
 pub use stdlib::*;
@@ -14,7 +16,9 @@
 pub use trace::*;
 
 pub trait ConfigureState {
-	fn configure(&self, s: &State) -> Result<()>;
+	type Guards;
+
+	fn configure(&self, s: &State) -> Result<Self::Guards>;
 }
 
 #[derive(Parser)]
@@ -44,7 +48,8 @@
 	jpath: Vec<PathBuf>,
 }
 impl ConfigureState for MiscOpts {
-	fn configure(&self, s: &State) -> Result<()> {
+	type Guards = StackDepthLimitOverrideGuard;
+	fn configure(&self, s: &State) -> Result<Self::Guards> {
 		let mut library_paths = self.jpath.clone();
 		library_paths.reverse();
 		if let Some(path) = env::var_os("JSONNET_PATH") {
@@ -53,8 +58,8 @@
 
 		s.set_import_resolver(Box::new(FileImportResolver::new(library_paths)));
 
-		s.set_max_stack(self.max_stack);
-		Ok(())
+		let _depth_limit = jrsonnet_evaluator::stack::limit_stack_depth(self.max_stack);
+		Ok(_depth_limit)
 	}
 }
 
@@ -72,16 +77,24 @@
 
 	#[clap(flatten)]
 	trace: TraceOpts,
+
+	#[clap(flatten)]
+	gc: GcOpts,
 }
 
 impl ConfigureState for GeneralOpts {
-	fn configure(&self, s: &State) -> Result<()> {
+	type Guards = (
+		<MiscOpts as ConfigureState>::Guards,
+		<GcOpts as ConfigureState>::Guards,
+	);
+	fn configure(&self, s: &State) -> Result<Self::Guards> {
 		// Configure trace first, because tla-code/ext-code can throw
 		self.trace.configure(s)?;
-		self.misc.configure(s)?;
+		let misc_guards = self.misc.configure(s)?;
 		self.tla.configure(s)?;
 		self.std.configure(s)?;
-		Ok(())
+		let gc_guards = self.gc.configure(s)?;
+		Ok((misc_guards, gc_guards))
 	}
 }
 
@@ -100,20 +113,22 @@
 	#[clap(long)]
 	gc_collect_before_printing_stats: bool,
 }
-impl GcOpts {
-	pub fn stats_printer(&self) -> (Option<GcStatsPrinter>, Option<LeakSpace>) {
+impl ConfigureState for GcOpts {
+	type Guards = (Option<GcStatsPrinter>, Option<LeakSpace>);
+
+	fn configure(&self, _s: &State) -> Result<Self::Guards> {
 		// Constructed structs have side-effects in Drop impl
 		#[allow(clippy::unnecessary_lazy_evaluations)]
-		(
+		Ok((
 			self.gc_print_stats.then(|| GcStatsPrinter {
 				collect_before_printing_stats: self.gc_collect_before_printing_stats,
 			}),
-			(!self.gc_collect_on_exit).then(|| LeakSpace {}),
-		)
+			(!self.gc_collect_on_exit).then(|| LeakSpace(PhantomData)),
+		))
 	}
 }
 
-pub struct LeakSpace {}
+pub struct LeakSpace(PhantomData<()>);
 
 impl Drop for LeakSpace {
 	fn drop(&mut self) {
modifiedcrates/jrsonnet-cli/src/manifest.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/manifest.rs
+++ b/crates/jrsonnet-cli/src/manifest.rs
@@ -47,6 +47,7 @@
 	exp_preserve_order: bool,
 }
 impl ConfigureState for ManifestOpts {
+	type Guards = ();
 	fn configure(&self, s: &State) -> Result<()> {
 		if self.string {
 			s.set_manifest_format(ManifestFormat::String);
modifiedcrates/jrsonnet-cli/src/stdlib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/stdlib.rs
+++ b/crates/jrsonnet-cli/src/stdlib.rs
@@ -106,6 +106,7 @@
 	ext_code_file: Vec<ExtFile>,
 }
 impl ConfigureState for StdOpts {
+	type Guards = ();
 	fn configure(&self, s: &State) -> Result<()> {
 		if self.no_stdlib {
 			return Ok(());
modifiedcrates/jrsonnet-cli/src/tla.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/tla.rs
+++ b/crates/jrsonnet-cli/src/tla.rs
@@ -47,6 +47,7 @@
 	tla_code_file: Vec<ExtFile>,
 }
 impl ConfigureState for TLAOpts {
+	type Guards = ();
 	fn configure(&self, s: &State) -> Result<()> {
 		for tla in self.tla_str.iter() {
 			s.add_tla_str((&tla.name as &str).into(), (&tla.value as &str).into());
modifiedcrates/jrsonnet-cli/src/trace.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/trace.rs
+++ b/crates/jrsonnet-cli/src/trace.rs
@@ -41,6 +41,7 @@
 	max_trace: usize,
 }
 impl ConfigureState for TraceOpts {
+	type Guards = ();
 	fn configure(&self, s: &State) -> Result<()> {
 		let resolver = PathResolver::new_cwd_fallback();
 		match self
modifiedcrates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -20,8 +20,8 @@
 exp-preserve-order = []
 # Implements field destructuring
 exp-destruct = ["jrsonnet-parser/exp-destruct"]
-# Provide Typed for conversions to/from serde_json::Value type
-serde_json = ["dep:serde_json"]
+# Improves performance, and implements some useful things using nightly-only features
+nightly = []
 
 [dependencies]
 jrsonnet-interner = { path = "../jrsonnet-interner", version = "0.4.2" }
modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -2,11 +2,11 @@
 
 use jrsonnet_gcmodule::Trace;
 use jrsonnet_interner::IStr;
-use jrsonnet_parser::{BinaryOpType, ExprLocation, Source, SourcePath, UnaryOpType};
+use jrsonnet_parser::{BinaryOpType, ExprLocation, LocExpr, Source, SourcePath, UnaryOpType};
 use jrsonnet_types::ValType;
 use thiserror::Error;
 
-use crate::{stdlib::format::FormatError, typed::TypeLocError};
+use crate::{function::CallLocation, stdlib::format::FormatError, typed::TypeLocError};
 
 fn format_found(list: &[IStr], what: &str) -> String {
 	if list.is_empty() {
@@ -262,7 +262,72 @@
 	}
 }
 
+pub trait ErrorSource {
+	fn to_location(self) -> Option<ExprLocation>;
+}
+impl ErrorSource for &LocExpr {
+	fn to_location(self) -> Option<ExprLocation> {
+		Some(self.1.clone())
+	}
+}
+impl ErrorSource for &ExprLocation {
+	fn to_location(self) -> Option<ExprLocation> {
+		Some(self.clone())
+	}
+}
+impl ErrorSource for CallLocation<'_> {
+	fn to_location(self) -> Option<ExprLocation> {
+		self.0.cloned()
+	}
+}
+
 pub type Result<V, E = LocError> = std::result::Result<V, E>;
+pub trait ResultExt: Sized {
+	#[must_use]
+	fn with_description<O: Into<String>>(self, msg: impl FnOnce() -> O) -> Self;
+	#[must_use]
+	fn description(self, msg: &str) -> Self {
+		self.with_description(|| msg)
+	}
+
+	#[must_use]
+	fn with_description_src<O: Into<String>>(
+		self,
+		src: impl ErrorSource,
+		msg: impl FnOnce() -> O,
+	) -> Self;
+	#[must_use]
+	fn description_src(self, src: impl ErrorSource, msg: &str) -> Self {
+		self.with_description_src(src, || msg)
+	}
+}
+impl<T> ResultExt for Result<T, LocError> {
+	fn with_description<O: Into<String>>(mut self, msg: impl FnOnce() -> O) -> Self {
+		if let Err(e) = &mut self {
+			let trace = e.trace_mut();
+			trace.0.push(StackTraceElement {
+				location: None,
+				desc: msg().into(),
+			});
+		}
+		self
+	}
+
+	fn with_description_src<O: Into<String>>(
+		mut self,
+		src: impl ErrorSource,
+		msg: impl FnOnce() -> O,
+	) -> Self {
+		if let Err(e) = &mut self {
+			let trace = e.trace_mut();
+			trace.0.push(StackTraceElement {
+				location: src.to_location(),
+				desc: msg().into(),
+			});
+		}
+		self
+	}
+}
 
 #[macro_export]
 macro_rules! throw {
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -34,7 +34,7 @@
 pub fn evaluate_field_name(s: State, ctx: Context, field_name: &FieldName) -> Result<Option<IStr>> {
 	Ok(match field_name {
 		FieldName::Fixed(n) => Some(n.clone()),
-		FieldName::Dyn(expr) => s.push(
+		FieldName::Dyn(expr) => State::push(
 			CallLocation::new(&expr.1),
 			|| "evaluating field name".to_string(),
 			|| {
@@ -184,14 +184,11 @@
 					.with_add(*plus)
 					.with_visibility(*visibility)
 					.with_location(value.1.clone())
-					.bindable(
-						s.clone(),
-						tb!(UnboundValue {
-							uctx: uctx.clone(),
-							value: value.clone(),
-							name: name.clone()
-						}),
-					)?;
+					.bindable(tb!(UnboundValue {
+						uctx: uctx.clone(),
+						value: value.clone(),
+						name: name.clone()
+					}))?;
 			}
 			Member::Field(FieldMember {
 				name,
@@ -233,15 +230,12 @@
 					.member(name.clone())
 					.hide()
 					.with_location(value.1.clone())
-					.bindable(
-						s.clone(),
-						tb!(UnboundMethod {
-							uctx: uctx.clone(),
-							value: value.clone(),
-							params: params.clone(),
-							name: name.clone()
-						}),
-					)?;
+					.bindable(tb!(UnboundMethod {
+						uctx: uctx.clone(),
+						value: value.clone(),
+						params: params.clone(),
+						name: name.clone()
+					}))?;
 			}
 			Member::BindStmt(_) => {}
 			Member::AssertStmt(stmt) => {
@@ -324,13 +318,10 @@
 							.member(n)
 							.with_location(obj.value.1.clone())
 							.with_add(obj.plus)
-							.bindable(
-								s.clone(),
-								tb!(UnboundValue {
-									uctx,
-									value: obj.value.clone(),
-								}),
-							)?;
+							.bindable(tb!(UnboundValue {
+								uctx,
+								value: obj.value.clone(),
+							}))?;
 					}
 					v => throw!(FieldMustBeStringGot(v.value_type())),
 				}
@@ -364,7 +355,7 @@
 			if tailstrict {
 				body()?
 			} else {
-				s.push(loc, || format!("function <{}> call", f.name()), body)?
+				State::push(loc, || format!("function <{}> call", f.name()), body)?
 			}
 		}
 		v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),
@@ -374,13 +365,13 @@
 pub fn evaluate_assert(s: State, ctx: Context, assertion: &AssertStmt) -> Result<()> {
 	let value = &assertion.0;
 	let msg = &assertion.1;
-	let assertion_result = s.push(
+	let assertion_result = State::push(
 		CallLocation::new(&value.1),
 		|| "assertion condition".to_owned(),
 		|| bool::from_untyped(evaluate(s.clone(), ctx.clone(), value)?, s.clone()),
 	)?;
 	if !assertion_result {
-		s.push(
+		State::push(
 			CallLocation::new(&value.1),
 			|| "assertion failure".to_owned(),
 			|| {
@@ -432,7 +423,7 @@
 		Num(v) => Val::new_checked_num(*v)?,
 		BinaryOp(v1, o, v2) => evaluate_binary_op_special(s, ctx, v1, *o, v2)?,
 		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(s, ctx, v)?)?,
-		Var(name) => s.push(
+		Var(name) => State::push(
 			CallLocation::new(loc),
 			|| format!("variable <{name}> access"),
 			|| ctx.binding(name.clone())?.evaluate(s.clone()),
@@ -442,7 +433,7 @@
 				evaluate(s.clone(), ctx.clone(), value)?,
 				evaluate(s.clone(), ctx, index)?,
 			) {
-				(Val::Obj(v), Val::Str(key)) => s.push(
+				(Val::Obj(v), Val::Str(key)) => State::push(
 					CallLocation::new(loc),
 					|| format!("field <{key}> access"),
 					|| match v.get(s.clone(), key.clone()) {
@@ -571,7 +562,7 @@
 			evaluate_assert(s.clone(), ctx.clone(), assert)?;
 			evaluate(s, ctx, returned)?
 		}
-		ErrorStmt(e) => s.push(
+		ErrorStmt(e) => State::push(
 			CallLocation::new(loc),
 			|| "error statement".to_owned(),
 			|| {
@@ -585,7 +576,7 @@
 			cond_then,
 			cond_else,
 		} => {
-			if s.push(
+			if State::push(
 				CallLocation::new(loc),
 				|| "if condition".to_owned(),
 				|| bool::from_untyped(evaluate(s.clone(), ctx.clone(), &cond.0)?, s.clone()),
@@ -607,7 +598,7 @@
 				desc: &'static str,
 			) -> Result<Option<T>> {
 				if let Some(value) = expr {
-					Ok(Some(s.push(
+					Ok(Some(State::push(
 						loc,
 						|| format!("slice {desc}"),
 						|| T::from_untyped(evaluate(s.clone(), ctx.clone(), value)?, s.clone()),
@@ -630,7 +621,7 @@
 			let tmp = loc.clone().0;
 			let resolved_path = s.resolve_from(tmp.source_path(), path as &str)?;
 			match i {
-				Import(_) => s.push(
+				Import(_) => State::push(
 					CallLocation::new(loc),
 					|| format!("import {:?}", path.clone()),
 					|| s.import_resolved(resolved_path),
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -1,5 +1,5 @@
 //! jsonnet interpreter implementation
-
+#![cfg_attr(feature = "nightly", feature(thread_local))]
 #![deny(unsafe_op_in_unsafe_fn)]
 #![warn(
 	clippy::all,
@@ -51,6 +51,7 @@
 mod integrations;
 mod map;
 mod obj;
+pub mod stack;
 pub mod stdlib;
 pub mod trace;
 pub mod typed;
@@ -67,7 +68,7 @@
 
 pub use ctx::*;
 pub use dynamic::*;
-use error::{Error::*, LocError, Result, StackTraceElement};
+use error::{Error::*, LocError, Result, ResultExt};
 pub use evaluate::*;
 use function::{CallLocation, TlaArg};
 use gc::{GcHashMap, TraceBox};
@@ -78,6 +79,7 @@
 pub use jrsonnet_parser as parser;
 use jrsonnet_parser::*;
 pub use obj::*;
+use stack::check_depth;
 use trace::{CompactFormat, TraceFormat};
 pub use val::{ManifestFormat, Thunk, Val};
 
@@ -143,8 +145,6 @@
 
 /// Dynamically reconfigurable evaluation settings
 pub struct EvaluationSettings {
-	/// Limits recursion by limiting the number of stack frames
-	pub max_stack: usize,
 	/// Limits amount of stack trace items preserved
 	pub max_trace: usize,
 	/// TLA vars
@@ -162,7 +162,6 @@
 impl Default for EvaluationSettings {
 	fn default() -> Self {
 		Self {
-			max_stack: 200,
 			max_trace: 20,
 			context_initializer: Box::new(DummyContextInitializer),
 			tla_vars: HashMap::default(),
@@ -179,19 +178,7 @@
 		}
 	}
 }
-
-#[derive(Default)]
-struct EvaluationData {
-	/// Used for stack overflow detection, stacktrace is populated on unwind
-	stack_depth: usize,
-	/// Updated every time stack entry is popt
-	stack_generation: usize,
 
-	breakpoints: Breakpoints,
-
-	/// Contains file source codes and evaluation results for imports and pretty-printed stacktraces
-	files: GcHashMap<SourcePath, FileData>,
-}
 struct FileData {
 	string: Option<IStr>,
 	bytes: Option<IBytes>,
@@ -217,46 +204,14 @@
 			parsed: None,
 			evaluated: None,
 			evaluating: false,
-		}
-	}
-}
-
-#[allow(clippy::type_complexity)]
-pub struct Breakpoint {
-	loc: ExprLocation,
-	collected: RefCell<HashMap<usize, (usize, Vec<Result<Val>>)>>,
-}
-#[derive(Default)]
-struct Breakpoints(Vec<Rc<Breakpoint>>);
-impl Breakpoints {
-	fn insert(
-		&self,
-		stack_depth: usize,
-		stack_generation: usize,
-		loc: &ExprLocation,
-		result: Result<Val>,
-	) -> Result<Val> {
-		if self.0.is_empty() {
-			return result;
-		}
-		for item in &self.0 {
-			if item.loc.belongs_to(loc) {
-				let mut collected = item.collected.borrow_mut();
-				let (depth, vals) = collected.entry(stack_generation).or_default();
-				if stack_depth > *depth {
-					vals.clear();
-				}
-				vals.push(result.clone());
-			}
 		}
-		result
 	}
 }
 
 #[derive(Default)]
 pub struct EvaluationStateInternals {
 	/// Internal state
-	data: RefCell<EvaluationData>,
+	file_cache: RefCell<GcHashMap<SourcePath, FileData>>,
 	/// Settings, safe to change at runtime
 	settings: RefCell<EvaluationSettings>,
 }
@@ -268,8 +223,8 @@
 impl State {
 	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise
 	pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {
-		let mut data = self.data_mut();
-		let mut file = data.files.raw_entry_mut().from_key(&path);
+		let mut file_cache = self.file_cache();
+		let mut file = file_cache.raw_entry_mut().from_key(&path);
 
 		let file = match file {
 			RawEntryMut::Occupied(ref mut d) => d.get_mut(),
@@ -303,8 +258,8 @@
 	}
 	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise
 	pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {
-		let mut data = self.data_mut();
-		let mut file = data.files.raw_entry_mut().from_key(&path);
+		let mut file_cache = self.file_cache();
+		let mut file = file_cache.raw_entry_mut().from_key(&path);
 
 		let file = match file {
 			RawEntryMut::Occupied(ref mut d) => d.get_mut(),
@@ -330,8 +285,8 @@
 	}
 	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise
 	pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {
-		let mut data = self.data_mut();
-		let mut file = data.files.raw_entry_mut().from_key(&path);
+		let mut file_cache = self.file_cache();
+		let mut file = file_cache.raw_entry_mut().from_key(&path);
 
 		let file = match file {
 			RawEntryMut::Occupied(ref mut d) => d.get_mut(),
@@ -383,16 +338,16 @@
 			throw!(InfiniteRecursionDetected)
 		}
 		file.evaluating = true;
-		// Dropping file here, as it borrows data, which may be used in evaluation
-		drop(data);
+		// Dropping file cache guard here, as evaluation may use this map too
+		drop(file_cache);
 		let res = evaluate(
 			self.clone(),
 			self.create_default_context(file_name),
 			&parsed,
 		);
 
-		let mut data = self.data_mut();
-		let mut file = data.files.raw_entry_mut().from_key(&path);
+		let mut file_cache = self.file_cache();
+		let mut file = file_cache.raw_entry_mut().from_key(&path);
 
 		let file = match file {
 			RawEntryMut::Occupied(ref mut d) => d.get_mut(),
@@ -426,35 +381,13 @@
 
 	/// Executes code creating a new stack frame
 	pub fn push<T>(
-		&self,
 		e: CallLocation<'_>,
 		frame_desc: impl FnOnce() -> String,
 		f: impl FnOnce() -> Result<T>,
 	) -> Result<T> {
-		{
-			let mut data = self.data_mut();
-			let stack_depth = &mut data.stack_depth;
-			if *stack_depth > self.max_stack() {
-				// Error creation uses data, so i drop guard here
-				drop(data);
-				throw!(StackOverflow);
-			}
-			*stack_depth += 1;
-		}
-		let result = f();
-		{
-			let mut data = self.data_mut();
-			data.stack_depth -= 1;
-			data.stack_generation += 1;
-		}
-		if let Err(mut err) = result {
-			err.trace_mut().0.push(StackTraceElement {
-				location: e.0.cloned(),
-				desc: frame_desc(),
-			});
-			return Err(err);
-		}
-		result
+		let _guard = check_depth()?;
+
+		f().with_description_src(e, frame_desc)
 	}
 
 	/// Executes code creating a new stack frame
@@ -464,64 +397,18 @@
 		frame_desc: impl FnOnce() -> String,
 		f: impl FnOnce() -> Result<Val>,
 	) -> Result<Val> {
-		{
-			let mut data = self.data_mut();
-			let stack_depth = &mut data.stack_depth;
-			if *stack_depth > self.max_stack() {
-				// Error creation uses data, so i drop guard here
-				drop(data);
-				throw!(StackOverflow);
-			}
-			*stack_depth += 1;
-		}
-		let mut result = f();
-		{
-			let mut data = self.data_mut();
-			data.stack_depth -= 1;
-			data.stack_generation += 1;
-			result = data
-				.breakpoints
-				.insert(data.stack_depth, data.stack_generation, e, result);
-		}
-		if let Err(mut err) = result {
-			err.trace_mut().0.push(StackTraceElement {
-				location: Some(e.clone()),
-				desc: frame_desc(),
-			});
-			return Err(err);
-		}
-		result
+		let _guard = check_depth()?;
+
+		f().with_description_src(e, frame_desc)
 	}
 	/// Executes code creating a new stack frame
 	pub fn push_description<T>(
-		&self,
 		frame_desc: impl FnOnce() -> String,
 		f: impl FnOnce() -> Result<T>,
 	) -> Result<T> {
-		{
-			let mut data = self.data_mut();
-			let stack_depth = &mut data.stack_depth;
-			if *stack_depth > self.max_stack() {
-				// Error creation uses data, so i drop guard here
-				drop(data);
-				throw!(StackOverflow);
-			}
-			*stack_depth += 1;
-		}
-		let result = f();
-		{
-			let mut data = self.data_mut();
-			data.stack_depth -= 1;
-			data.stack_generation += 1;
-		}
-		if let Err(mut err) = result {
-			err.trace_mut().0.push(StackTraceElement {
-				location: None,
-				desc: frame_desc(),
-			});
-			return Err(err);
-		}
-		result
+		let _guard = check_depth()?;
+
+		f().with_description(frame_desc)
 	}
 
 	/// # Panics
@@ -536,7 +423,7 @@
 	}
 
 	pub fn manifest(&self, val: Val) -> Result<IStr> {
-		self.push_description(
+		Self::push_description(
 			|| "manifestification".to_string(),
 			|| val.manifest(self.clone(), &self.manifest_format()),
 		)
@@ -551,7 +438,7 @@
 	/// If passed value is function then call with set TLA
 	pub fn with_tla(&self, val: Val) -> Result<Val> {
 		Ok(match val {
-			Val::Func(func) => self.push_description(
+			Val::Func(func) => State::push_description(
 				|| "during TLA call".to_owned(),
 				|| {
 					func.evaluate(
@@ -573,8 +460,8 @@
 
 /// Internals
 impl State {
-	fn data_mut(&self) -> RefMut<'_, EvaluationData> {
-		self.0.data.borrow_mut()
+	fn file_cache(&self) -> RefMut<'_, GcHashMap<SourcePath, FileData>> {
+		self.0.file_cache.borrow_mut()
 	}
 	pub fn settings(&self) -> Ref<'_, EvaluationSettings> {
 		self.0.settings.borrow()
@@ -675,12 +562,5 @@
 	}
 	pub fn set_max_trace(&self, trace: usize) {
 		self.settings_mut().max_trace = trace;
-	}
-
-	pub fn max_stack(&self) -> usize {
-		self.settings().max_stack
-	}
-	pub fn set_max_stack(&self, trace: usize) {
-		self.settings_mut().max_stack = trace;
 	}
 }
modifiedcrates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -577,22 +577,29 @@
 
 pub struct ValueBuilder<'v>(&'v mut ObjValueBuilder);
 impl ObjMemberBuilder<ValueBuilder<'_>> {
-	pub fn value(self, s: State, value: Val) -> Result<()> {
-		self.binding(s, MaybeUnbound::Bound(Thunk::evaluated(value)))
+	/// Inserts value, replacing if it is already defined
+	pub fn value_unchecked(self, value: Val) {
+		let (receiver, name, member) =
+			self.build_member(MaybeUnbound::Bound(Thunk::evaluated(value)));
+		let entry = receiver.0.map.entry(name);
+		entry.insert(member);
 	}
-	pub fn bindable(
-		self,
-		s: State,
-		bindable: TraceBox<dyn Unbound<Bound = Thunk<Val>>>,
-	) -> Result<()> {
-		self.binding(s, MaybeUnbound::Unbound(Cc::new(bindable)))
+
+	pub fn value(self, value: Val) -> Result<()> {
+		self.thunk(Thunk::evaluated(value))
+	}
+	pub fn thunk(self, value: Thunk<Val>) -> Result<()> {
+		self.binding(MaybeUnbound::Bound(value))
+	}
+	pub fn bindable(self, bindable: TraceBox<dyn Unbound<Bound = Thunk<Val>>>) -> Result<()> {
+		self.binding(MaybeUnbound::Unbound(Cc::new(bindable)))
 	}
-	pub fn binding(self, s: State, binding: MaybeUnbound) -> Result<()> {
+	pub fn binding(self, binding: MaybeUnbound) -> Result<()> {
 		let (receiver, name, member) = self.build_member(binding);
 		let location = member.location.clone();
 		let old = receiver.0.map.insert(name.clone(), member);
 		if old.is_some() {
-			s.push(
+			State::push(
 				CallLocation(location.as_ref()),
 				|| format!("field <{}> initializtion", name.clone()),
 				|| throw!(DuplicateFieldName(name.clone())),
addedcrates/jrsonnet-evaluator/src/stack.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-evaluator/src/stack.rs
@@ -0,0 +1,109 @@
+use std::{cell::Cell, marker::PhantomData};
+
+use crate::error::{Error, LocError};
+
+struct StackLimit {
+	max_stack_size: Cell<usize>,
+	current_depth: Cell<usize>,
+}
+
+#[cfg(feature = "nightly")]
+#[thread_local]
+static STACK_LIMIT: StackLimit = StackLimit {
+	max_stack_size: Cell::new(200),
+	current_depth: Cell::new(0),
+};
+#[cfg(not(feature = "nightly"))]
+thread_local! {
+	static STACK_LIMIT: StackLimit = StackLimit {
+		max_stack_size: Cell::new(200),
+		current_depth: Cell::new(0),
+	};
+}
+
+pub struct StackOverflowError;
+impl From<StackOverflowError> for Error {
+	fn from(_: StackOverflowError) -> Self {
+		Error::StackOverflow
+	}
+}
+impl From<StackOverflowError> for LocError {
+	fn from(_: StackOverflowError) -> Self {
+		Error::StackOverflow.into()
+	}
+}
+
+/// Used to implement stack depth limitation
+pub struct StackDepthGuard(PhantomData<()>);
+impl Drop for StackDepthGuard {
+	#[cfg(feature = "nightly")]
+	fn drop(&mut self) {
+		STACK_LIMIT
+			.current_depth
+			.set(STACK_LIMIT.current_depth.get() - 1)
+	}
+	#[cfg(not(feature = "nightly"))]
+	fn drop(&mut self) {
+		STACK_LIMIT.with(|limit| limit.current_depth.set(limit.current_depth.get() - 1));
+	}
+}
+
+// #[cfg(feature = "nightly")]
+pub fn check_depth() -> Result<StackDepthGuard, StackOverflowError> {
+	fn internal(limit: &StackLimit) -> Result<StackDepthGuard, StackOverflowError> {
+		let current = limit.current_depth.get();
+		if current < limit.max_stack_size.get() {
+			limit.current_depth.set(current + 1);
+			Ok(StackDepthGuard(PhantomData))
+		} else {
+			Err(StackOverflowError)
+		}
+	}
+	#[cfg(feature = "nightly")]
+	{
+		internal(&STACK_LIMIT)
+	}
+	#[cfg(not(feature = "nightly"))]
+	{
+		STACK_LIMIT.with(internal)
+	}
+}
+
+pub struct StackDepthLimitOverrideGuard {
+	old_limit: usize,
+}
+impl Drop for StackDepthLimitOverrideGuard {
+	#[cfg(feature = "nightly")]
+	fn drop(&mut self) {
+		STACK_LIMIT.max_stack_size.set(self.old_limit)
+	}
+	#[cfg(not(feature = "nightly"))]
+	fn drop(&mut self) {
+		STACK_LIMIT.with(|limit| limit.max_stack_size.set(self.old_limit));
+	}
+}
+
+pub fn limit_stack_depth(depth_limit: usize) -> StackDepthLimitOverrideGuard {
+	fn internal(limit: &StackLimit, depth_limit: usize) -> StackDepthLimitOverrideGuard {
+		let old_limit = limit.max_stack_size.get();
+		let current_depth = limit.current_depth.get();
+
+		limit.max_stack_size.set(current_depth + depth_limit);
+		StackDepthLimitOverrideGuard { old_limit }
+	}
+	#[cfg(feature = "nightly")]
+	{
+		internal(&STACK_LIMIT, depth_limit)
+	}
+	#[cfg(not(feature = "nightly"))]
+	{
+		STACK_LIMIT.with(|limit| internal(limit, depth_limit))
+	}
+}
+
+/// Like [`limit_stack_depth`], but set depth is not guarded, and will be kept
+///
+/// Used to implement `set_max_stack` in C api, prefer to use [`limit_stack_depth`] instead
+pub fn set_stack_depth_limit(depth_limit: usize) {
+	std::mem::forget(limit_stack_depth(depth_limit));
+}
modifiedcrates/jrsonnet-evaluator/src/stdlib/manifest.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/stdlib/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/manifest.rs
@@ -111,7 +111,7 @@
 					buf.push_str(cur_padding);
 					escape_string_json_buf(&field, buf);
 					buf.push_str(options.key_val_sep);
-					s.push_description(
+					State::push_description(
 						|| format!("field <{}> manifestification", field.clone()),
 						|| {
 							let value = obj.get(s.clone(), field.clone())?.unwrap();
modifiedcrates/jrsonnet-evaluator/src/stdlib/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/stdlib/mod.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/mod.rs
@@ -10,7 +10,7 @@
 pub mod manifest;
 
 pub fn std_format(s: State, str: IStr, vals: Val) -> Result<String> {
-	s.push(
+	State::push(
 		CallLocation::native(),
 		|| format!("std.format of {str}"),
 		|| {
modifiedcrates/jrsonnet-evaluator/src/typed/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/typed/mod.rs
+++ b/crates/jrsonnet-evaluator/src/typed/mod.rs
@@ -85,12 +85,11 @@
 }
 
 fn push_type_description(
-	s: State,
 	error_reason: impl Fn() -> String,
 	path: impl Fn() -> ValuePathItem,
 	item: impl Fn() -> Result<()>,
 ) -> Result<()> {
-	s.push_description(error_reason, || match item() {
+	State::push_description(error_reason, || match item() {
 		Ok(_) => Ok(()),
 		Err(mut e) => {
 			if let Error::TypeError(e) = &mut e.error_mut() {
@@ -170,7 +169,6 @@
 				Val::Arr(a) => {
 					for (i, item) in a.iter(s.clone()).enumerate() {
 						push_type_description(
-							s.clone(),
 							|| format!("array index {i}"),
 							|| ValuePathItem::Index(i as u64),
 							|| elem_type.check(s.clone(), &item.clone()?),
@@ -184,7 +182,6 @@
 				Val::Arr(a) => {
 					for (i, item) in a.iter(s.clone()).enumerate() {
 						push_type_description(
-							s.clone(),
 							|| format!("array index {i}"),
 							|| ValuePathItem::Index(i as u64),
 							|| elem_type.check(s.clone(), &item.clone()?),
@@ -199,7 +196,6 @@
 					for (k, v) in elems.iter() {
 						if let Some(got_v) = obj.get(s.clone(), (*k).into())? {
 							push_type_description(
-								s.clone(),
 								|| format!("property {k}"),
 								|| ValuePathItem::Field((*k).into()),
 								|| v.check(s.clone(), &got_v),
modifiedcrates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -299,7 +299,7 @@
 				cfg_attrs,
 			} => {
 				let name = name.as_ref().map(|v| v.as_str()).unwrap_or("<unnamed>");
-				let eval = quote! {s.push_description(
+				let eval = quote! {jrsonnet_evaluator::State::push_description(
 					|| format!("argument <{}> evaluation", #name),
 					|| <#ty>::from_untyped(value.evaluate(s.clone())?, s.clone()),
 				)?};
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-stdlib/src/lib.rs
1use std::{2	cell::{Ref, RefCell, RefMut},3	collections::HashMap,4	rc::Rc,5};67use jrsonnet_evaluator::{8	error::{Error::*, Result},9	function::{builtin::Builtin, CallLocation, FuncVal, TlaArg},10	gc::{GcHashMap, TraceBox},11	tb,12	trace::PathResolver,13	Context, ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,14};15use jrsonnet_gcmodule::Cc;16use jrsonnet_parser::Source;1718mod expr;19mod types;20pub use types::*;21mod arrays;22pub use arrays::*;23mod math;24pub use math::*;25mod operator;26pub use operator::*;27mod sort;28pub use sort::*;29mod hash;30pub use hash::*;31mod encoding;32pub use encoding::*;33mod objects;34pub use objects::*;35mod manifest;36pub use manifest::*;37mod parse;38pub use parse::*;39mod strings;40pub use strings::*;41mod misc;42pub use misc::*;4344pub fn stdlib_uncached(s: State, settings: Rc<RefCell<Settings>>) -> ObjValue {45	let mut builder = ObjValueBuilder::new();4647	let expr = expr::stdlib_expr();48	let eval = jrsonnet_evaluator::evaluate(s.clone(), Context::default(), &expr)49		.expect("stdlib.jsonnet should have no errors")50		.as_obj()51		.expect("stdlib.jsonnet should evaluate to object");5253	builder.with_super(eval);5455	for (name, builtin) in [56		// Types57		("type", builtin_type::INST),58		("isString", builtin_is_string::INST),59		("isNumber", builtin_is_number::INST),60		("isBoolean", builtin_is_boolean::INST),61		("isObject", builtin_is_object::INST),62		("isArray", builtin_is_array::INST),63		("isFunction", builtin_is_function::INST),64		// Arrays65		("makeArray", builtin_make_array::INST),66		("slice", builtin_slice::INST),67		("map", builtin_map::INST),68		("flatMap", builtin_flatmap::INST),69		("filter", builtin_filter::INST),70		("foldl", builtin_foldl::INST),71		("foldr", builtin_foldr::INST),72		("range", builtin_range::INST),73		("join", builtin_join::INST),74		("reverse", builtin_reverse::INST),75		("any", builtin_any::INST),76		("all", builtin_all::INST),77		("member", builtin_member::INST),78		("count", builtin_count::INST),79		// Math80		("modulo", builtin_modulo::INST),81		("floor", builtin_floor::INST),82		("ceil", builtin_ceil::INST),83		("log", builtin_log::INST),84		("pow", builtin_pow::INST),85		("sqrt", builtin_sqrt::INST),86		("sin", builtin_sin::INST),87		("cos", builtin_cos::INST),88		("tan", builtin_tan::INST),89		("asin", builtin_asin::INST),90		("acos", builtin_acos::INST),91		("atan", builtin_atan::INST),92		("exp", builtin_exp::INST),93		("mantissa", builtin_mantissa::INST),94		("exponent", builtin_exponent::INST),95		// Operator96		("mod", builtin_mod::INST),97		("primitiveEquals", builtin_primitive_equals::INST),98		("equals", builtin_equals::INST),99		("format", builtin_format::INST),100		// Sort101		("sort", builtin_sort::INST),102		// Hash103		("md5", builtin_md5::INST),104		// Encoding105		("encodeUTF8", builtin_encode_utf8::INST),106		("decodeUTF8", builtin_decode_utf8::INST),107		("base64", builtin_base64::INST),108		("base64Decode", builtin_base64_decode::INST),109		("base64DecodeBytes", builtin_base64_decode_bytes::INST),110		// Objects111		("objectFieldsEx", builtin_object_fields_ex::INST),112		("objectHasEx", builtin_object_has_ex::INST),113		// Manifest114		("escapeStringJson", builtin_escape_string_json::INST),115		("manifestJsonEx", builtin_manifest_json_ex::INST),116		("manifestYamlDoc", builtin_manifest_yaml_doc::INST),117		// Parsing118		("parseJson", builtin_parse_json::INST),119		("parseYaml", builtin_parse_yaml::INST),120		// Strings121		("codepoint", builtin_codepoint::INST),122		("substr", builtin_substr::INST),123		("char", builtin_char::INST),124		("strReplace", builtin_str_replace::INST),125		("splitLimit", builtin_splitlimit::INST),126		("asciiUpper", builtin_ascii_upper::INST),127		("asciiLower", builtin_ascii_lower::INST),128		("findSubstr", builtin_find_substr::INST),129		// Misc130		("length", builtin_length::INST),131		("startsWith", builtin_starts_with::INST),132		("endsWith", builtin_ends_with::INST),133	]134	.iter()135	.cloned()136	{137		builder138			.member(name.into())139			.hide()140			.value(s.clone(), Val::Func(FuncVal::StaticBuiltin(builtin)))141			.expect("no conflict");142	}143144	builder145		.member("extVar".into())146		.hide()147		.value(148			s.clone(),149			Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_ext_var {150				settings: settings.clone()151			})))),152		)153		.expect("no conflict");154	builder155		.member("native".into())156		.hide()157		.value(158			s.clone(),159			Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_native {160				settings: settings.clone()161			})))),162		)163		.expect("no conflict");164	builder165		.member("trace".into())166		.hide()167		.value(168			s.clone(),169			Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_trace { settings })))),170		)171		.expect("no conflict");172173	builder174		.member("id".into())175		.hide()176		.value(s, Val::Func(FuncVal::Id))177		.expect("no conflict");178179	builder.build()180}181182pub trait TracePrinter {183	fn print_trace(&self, s: State, loc: CallLocation, value: IStr);184}185186pub struct StdTracePrinter {187	resolver: PathResolver,188}189impl StdTracePrinter {190	pub fn new(resolver: PathResolver) -> Self {191		Self { resolver }192	}193}194impl TracePrinter for StdTracePrinter {195	fn print_trace(&self, _s: State, loc: CallLocation, value: IStr) {196		eprint!("TRACE:");197		if let Some(loc) = loc.0 {198			let locs = loc.0.map_source_locations(&[loc.1]);199			eprint!(200				" {}:{}",201				match loc.0.source_path().path() {202					Some(p) => self.resolver.resolve(p),203					None => loc.0.source_path().to_string(),204				},205				locs[0].line206			);207		}208		eprintln!(" {}", value);209	}210}211212pub struct Settings {213	/// Used for `std.extVar`214	pub ext_vars: HashMap<IStr, TlaArg>,215	/// Used for `std.native`216	pub ext_natives: HashMap<IStr, Cc<TraceBox<dyn Builtin>>>,217	/// Helper to add globals without implementing custom ContextInitializer218	pub globals: GcHashMap<IStr, Thunk<Val>>,219	/// Used for `std.trace`220	pub trace_printer: Box<dyn TracePrinter>,221	/// Used for `std.thisFile`222	pub path_resolver: PathResolver,223}224225pub fn extvar_source(name: &str, code: impl Into<IStr>) -> Source {226	let source_name = format!("<extvar:{}>", name);227	Source::new_virtual(source_name.into(), code.into())228}229230pub struct ContextInitializer {231	// When we don't need to support legacy-this-file, we can reuse same context for all files232	#[cfg(not(feature = "legacy-this-file"))]233	context: Context,234	// Otherwise, we can only keep first stdlib layer, and then stack thisFile on top of it235	#[cfg(feature = "legacy-this-file")]236	stdlib_obj: ObjValue,237	settings: Rc<RefCell<Settings>>,238}239impl ContextInitializer {240	pub fn new(s: State, resolver: PathResolver) -> Self {241		let settings = Settings {242			ext_vars: Default::default(),243			ext_natives: Default::default(),244			globals: Default::default(),245			trace_printer: Box::new(StdTracePrinter::new(resolver.clone())),246			path_resolver: resolver,247		};248		let settings = Rc::new(RefCell::new(settings));249		Self {250			#[cfg(not(feature = "legacy-this-file"))]251			context: {252				let mut context = ContextBuilder::with_capacity(1);253				context.bind(254					"std".into(),255					Thunk::evaluated(Val::Obj(stdlib_uncached(s, settings.clone()))),256				);257				context.build()258			},259			#[cfg(feature = "legacy-this-file")]260			stdlib_obj: stdlib_uncached(s, settings.clone()),261			settings,262		}263	}264	pub fn settings(&self) -> Ref<Settings> {265		self.settings.borrow()266	}267	pub fn settings_mut(&self) -> RefMut<Settings> {268		self.settings.borrow_mut()269	}270	pub fn add_ext_var(&self, name: IStr, value: Val) {271		self.settings_mut()272			.ext_vars273			.insert(name, TlaArg::Val(value));274	}275	pub fn add_ext_str(&self, name: IStr, value: IStr) {276		self.settings_mut()277			.ext_vars278			.insert(name, TlaArg::String(value));279	}280	pub fn add_ext_code(&self, name: &str, code: impl Into<IStr>) -> Result<()> {281		let code = code.into();282		let source = extvar_source(name, code.clone());283		let parsed = jrsonnet_parser::parse(284			&code,285			&jrsonnet_parser::ParserSettings {286				file_name: source.clone(),287			},288		)289		.map_err(|e| ImportSyntaxError {290			path: source,291			error: Box::new(e),292		})?;293		// self.data_mut().volatile_files.insert(source_name, code);294		self.settings_mut()295			.ext_vars296			.insert(name.into(), TlaArg::Code(parsed));297		Ok(())298	}299	pub fn add_native(&self, name: IStr, cb: Cc<TraceBox<dyn Builtin>>) {300		self.settings_mut().ext_natives.insert(name, cb);301	}302}303impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {304	#[cfg(not(feature = "legacy-this-file"))]305	fn initialize(&self, _s: State, _source: Source) -> jrsonnet_evaluator::Context {306		let out = self.context.clone();307		let globals = &self.settings().globals;308		if globals.is_empty() {309			return out;310		}311312		let mut out = ContextBuilder::extend(out);313		for (k, v) in globals.iter() {314			out.bind(k.clone(), v.clone());315		}316		out.build()317	}318	#[cfg(feature = "legacy-this-file")]319	fn initialize(&self, s: State, source: Source) -> jrsonnet_evaluator::Context {320		let mut builder = ObjValueBuilder::new();321		builder.with_super(self.stdlib_obj.clone());322		builder323			.member("thisFile".into())324			.hide()325			.value(326				s,327				Val::Str(match source.source_path().path() {328					Some(p) => self.settings().path_resolver.resolve(p).into(),329					None => source.source_path().to_string().into(),330				}),331			)332			.expect("this object builder is empty");333		let stdlib_with_this_file = builder.build();334335		let mut context = ContextBuilder::with_capacity(1);336		context.bind(337			"std".into(),338			Thunk::evaluated(Val::Obj(stdlib_with_this_file)),339		);340		for (k, v) in self.settings().globals.iter() {341			context.bind(k.clone(), v.clone());342		}343		context.build()344	}345	fn as_any(&self) -> &dyn std::any::Any {346		self347	}348}349350pub trait StateExt {351	/// This method was previously implemented in jrsonnet-evaluator itself352	fn with_stdlib(&self);353	fn add_global(&self, name: IStr, value: Thunk<Val>);354}355356impl StateExt for State {357	fn with_stdlib(&self) {358		let initializer = ContextInitializer::new(self.clone(), PathResolver::new_cwd_fallback());359		self.settings_mut().context_initializer = Box::new(initializer)360	}361	fn add_global(&self, name: IStr, value: Thunk<Val>) {362		self.settings()363			.context_initializer364			.as_any()365			.downcast_ref::<ContextInitializer>()366			.expect("not standard context initializer")367			.settings_mut()368			.globals369			.insert(name, value);370	}371}