git.delta.rocks / jrsonnet / refs/commits / 1c69f1ac7855

difftreelog

refactor move state to global

touwqtkwYaroslav Bolyukin2026-02-08parent: #5585b94.patch.diff
in: master

9 files changed

modifiedbindings/jsonnet/src/lib.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -247,7 +247,7 @@
 	match vm
 		.state
 		.import(filename)
-		.and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))
+		.and_then(|val| apply_tla(&vm.tla_args, val))
 		.and_then(|val| val.manifest(&vm.manifest_format))
 	{
 		Ok(v) => {
@@ -282,7 +282,7 @@
 	match vm
 		.state
 		.evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())
-		.and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))
+		.and_then(|val| apply_tla(&vm.tla_args, val))
 		.and_then(|val| val.manifest(&vm.manifest_format))
 	{
 		Ok(v) => {
@@ -340,7 +340,7 @@
 	match vm
 		.state
 		.import(filename)
-		.and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))
+		.and_then(|val| apply_tla(&vm.tla_args, val))
 		.and_then(|val| val_to_multi(val, &vm.manifest_format))
 	{
 		Ok(v) => {
@@ -369,7 +369,7 @@
 	match vm
 		.state
 		.evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())
-		.and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))
+		.and_then(|val| apply_tla(&vm.tla_args, val))
 		.and_then(|val| val_to_multi(val, &vm.manifest_format))
 	{
 		Ok(v) => {
@@ -422,7 +422,7 @@
 	match vm
 		.state
 		.import(filename)
-		.and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))
+		.and_then(|val| apply_tla(&vm.tla_args, val))
 		.and_then(|val| val_to_stream(val, &vm.manifest_format))
 	{
 		Ok(v) => {
@@ -451,7 +451,7 @@
 	match vm
 		.state
 		.evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())
-		.and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))
+		.and_then(|val| apply_tla(&vm.tla_args, val))
 		.and_then(|val| val_to_stream(val, &vm.manifest_format))
 	{
 		Ok(v) => {
modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -173,6 +173,7 @@
 	let mut s = State::builder();
 	s.import_resolver(import_resolver).context_initializer(std);
 	let s = s.build();
+	let _s = s.enter();
 
 	let input = opts.input.input.ok_or(Error::MissingInputArgument)?;
 	let val = if opts.input.exec {
@@ -192,7 +193,7 @@
 		unused_mut,
 		clippy::redundant_clone,
 	)]
-	let mut val = apply_tla(s.clone(), &tla, val)?;
+	let mut val = apply_tla(&tla, val)?;
 
 	#[cfg(feature = "exp-apply")]
 	for apply in opts.input.exp_apply {
modifiedcrates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -6,12 +6,11 @@
 
 use crate::{
 	error::ErrorKind::*, gc::WithCapacityExt as _, map::LayeredHashMap, ObjValue, Pending, Result,
-	State, Thunk, Val,
+	Thunk, Val,
 };
 
 #[derive(Trace)]
 struct ContextInternals {
-	state: Option<State>,
 	dollar: Option<ObjValue>,
 	sup: Option<ObjValue>,
 	this: Option<ObjValue>,
@@ -33,13 +32,6 @@
 		Pending::new()
 	}
 
-	pub fn state(&self) -> &State {
-		self.0
-			.state
-			.as_ref()
-			.expect("used state from dummy context")
-	}
-
 	pub fn dollar(&self) -> Option<&ObjValue> {
 		self.0.dollar.as_ref()
 	}
@@ -112,7 +104,6 @@
 			ctx.bindings.clone().extend(new_bindings)
 		};
 		Self(Cc::new(ContextInternals {
-			state: ctx.state.clone(),
 			dollar,
 			sup,
 			this,
@@ -128,34 +119,22 @@
 }
 
 pub struct ContextBuilder {
-	state: Option<State>,
 	bindings: FxHashMap<IStr, Thunk<Val>>,
 	extend: Option<Context>,
 }
 
 impl ContextBuilder {
-	/// # Panics
-	/// Panics aren't directly caused by this function, but if state from resulting context is used
-	pub fn dangerous_empty_state() -> Self {
-		Self {
-			state: None,
-			bindings: FxHashMap::new(),
-			extend: None,
-		}
+	pub fn new() -> Self {
+		Self::with_capacity(0)
 	}
-	pub fn new(state: State) -> Self {
-		Self::with_capacity(state, 0)
-	}
-	pub fn with_capacity(state: State, capacity: usize) -> Self {
+	pub fn with_capacity(capacity: usize) -> Self {
 		Self {
-			state: Some(state),
 			bindings: FxHashMap::with_capacity(capacity),
 			extend: None,
 		}
 	}
 	pub fn extend(parent: Context) -> Self {
 		Self {
-			state: parent.0.state.clone(),
 			bindings: FxHashMap::new(),
 			extend: Some(parent),
 		}
@@ -173,7 +152,6 @@
 			parent.extend(self.bindings, None, None, None)
 		} else {
 			Context(Cc::new(ContextInternals {
-				state: self.state,
 				bindings: LayeredHashMap::new(self.bindings),
 				dollar: None,
 				sup: None,
modifiedcrates/jrsonnet-evaluator/src/function/arglike.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function/arglike.rs
+++ b/crates/jrsonnet-evaluator/src/function/arglike.rs
@@ -4,7 +4,7 @@
 use jrsonnet_interner::IStr;
 use jrsonnet_parser::{ArgsDesc, LocExpr, SourceFifo, SourcePath};
 
-use crate::{evaluate, typed::Typed, Context, Result, Thunk, Val};
+use crate::{evaluate, typed::Typed, with_state, Context, Result, Thunk, Val};
 
 /// Marker for arguments, which can be evaluated with context set to None
 pub trait OptionalContext {}
@@ -47,28 +47,59 @@
 	ImportStr(String),
 	InlineCode(String),
 }
-impl ArgLike for TlaArg {
-	fn evaluate_arg(&self, ctx: Context, _tailstrict: bool) -> Result<Thunk<Val>> {
+impl TlaArg {
+	pub fn evaluate_tailstrict(&self) -> Result<Val> {
 		match self {
+			Self::String(s) => Ok(Val::string(s.clone())),
+			Self::Val(val) => Ok(val.clone()),
+			Self::Lazy(lazy) => Ok(lazy.evaluate()?),
+			Self::Import(p) => with_state(|s| {
+				let resolved = s.resolve_from_default(&p.as_str())?;
+				s.import_resolved(resolved)
+			}),
+			Self::ImportStr(p) => with_state(|s| {
+				let resolved = s.resolve_from_default(&p.as_str())?;
+				s.import_resolved_str(resolved).map(Val::string)
+			}),
+			Self::InlineCode(p) => with_state(|s| {
+				let resolved =
+					SourcePath::new(SourceFifo("<inline code>".to_owned(), p.as_bytes().into()));
+				s.import_resolved(resolved)
+			}),
+		}
+	}
+	pub fn evaluate(&self) -> Result<Thunk<Val>> {
+		match self {
 			Self::String(s) => Ok(Thunk::evaluated(Val::string(s.clone()))),
 			Self::Val(val) => Ok(Thunk::evaluated(val.clone())),
 			Self::Lazy(lazy) => Ok(lazy.clone()),
-			Self::Import(p) => {
-				let resolved = ctx.state().resolve_from_default(&p.as_str())?;
-				Ok(Thunk!(move || ctx.state().import_resolved(resolved)))
-			}
-			Self::ImportStr(p) => {
-				let resolved = ctx.state().resolve_from_default(&p.as_str())?;
-				Ok(Thunk!(move || ctx
-					.state()
+			Self::Import(p) => with_state(|s| {
+				let resolved = s.resolve_from_default(&p.as_str())?;
+				Ok(Thunk!(move || s.import_resolved(resolved)))
+			}),
+			Self::ImportStr(p) => with_state(|s| {
+				let resolved = s.resolve_from_default(&p.as_str())?;
+				Ok(Thunk!(move || s
 					.import_resolved_str(resolved)
 					.map(Val::string)))
-			}
-			Self::InlineCode(p) => {
+			}),
+			Self::InlineCode(p) => with_state(|s| {
 				let resolved =
 					SourcePath::new(SourceFifo("<inline code>".to_owned(), p.as_bytes().into()));
-				Ok(Thunk!(move || ctx.state().import_resolved(resolved)))
-			}
+				Ok(Thunk!(move || s.import_resolved(resolved)))
+			}),
+		}
+	}
+}
+
+// TODO: Is this implementation really required, as there is no Context to use?
+// Maybe something a bit stricter is possible to add, especially with precompiled calls?
+impl ArgLike for TlaArg {
+	fn evaluate_arg(&self, _ctx: Context, tailstrict: bool) -> Result<Thunk<Val>> {
+		if tailstrict {
+			self.evaluate_tailstrict().map(Thunk::evaluated)
+		} else {
+			self.evaluate()
 		}
 	}
 }
modifiedcrates/jrsonnet-evaluator/src/function/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function/mod.rs
+++ b/crates/jrsonnet-evaluator/src/function/mod.rs
@@ -207,7 +207,7 @@
 		tailstrict: bool,
 	) -> Result<Val> {
 		self.evaluate(
-			ContextBuilder::dangerous_empty_state().build(),
+			ContextBuilder::new().build(),
 			CallLocation::native(),
 			args,
 			tailstrict,
modifiedcrates/jrsonnet-evaluator/src/tla.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/tla.rs
+++ b/crates/jrsonnet-evaluator/src/tla.rs
@@ -1,21 +1,24 @@
 use jrsonnet_interner::IStr;
 use jrsonnet_parser::Source;
+use rustc_hash::FxHashMap;
 
 use crate::{
-	function::{ArgsLike, CallLocation},
-	in_description_frame, Result, State, Val,
+	function::{CallLocation, TlaArg},
+	in_description_frame, with_state, Result, Val,
 };
 
-pub fn apply_tla<A: ArgsLike>(s: State, args: &A, val: Val) -> Result<Val> {
+pub fn apply_tla(args: &FxHashMap<IStr, TlaArg>, val: Val) -> Result<Val> {
 	Ok(if let Val::Func(func) = val {
 		in_description_frame(
 			|| "during TLA call".to_owned(),
 			|| {
 				func.evaluate(
-					s.create_default_context(Source::new_virtual(
-						"<top-level-arg>".into(),
-						IStr::empty(),
-					)),
+					with_state(|s| {
+						s.create_default_context(Source::new_virtual(
+							"<top-level-arg>".into(),
+							IStr::empty(),
+						))
+					}),
 					CallLocation::native(),
 					args,
 					false,
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -335,11 +335,6 @@
 	pub path_resolver: PathResolver,
 }
 
-fn extvar_source(name: &str, code: impl Into<IStr>) -> Source {
-	let source_name = format!("<extvar:{name}>");
-	Source::new_virtual(source_name.into(), code.into())
-}
-
 #[derive(Trace, Clone)]
 pub struct ContextInitializer {
 	/// std without applied thisFile overlay
modifiedcrates/jrsonnet-stdlib/src/misc.rsdiffbeforeafterboth
3use jrsonnet_evaluator::{3use jrsonnet_evaluator::{
4 bail,4 bail,
5 error::{ErrorKind::*, Result},5 error::{ErrorKind::*, Result},
6 function::{builtin, ArgLike, CallLocation, FuncVal},6 function::{builtin, CallLocation, FuncVal},
7 manifest::JsonFormat,7 manifest::JsonFormat,
8 typed::{Either2, Either4},8 typed::{Either2, Either4},
9 val::{equals, ArrValue},9 val::{equals, ArrValue},
10 Context, Either, IStr, ObjValue, ObjValueBuilder, ResultExt, Thunk, Val,10 Either, IStr, ObjValue, ObjValueBuilder, ResultExt, Thunk, Val,
11};11};
12use jrsonnet_gcmodule::Cc;12use jrsonnet_gcmodule::Cc;
1313
14use crate::{extvar_source, Settings};14use crate::Settings;
1515
16#[builtin]16#[builtin]
17pub fn builtin_length(x: Either![IStr, ArrValue, ObjValue, FuncVal]) -> usize {17pub fn builtin_length(x: Either![IStr, ArrValue, ObjValue, FuncVal]) -> usize {
50#[builtin(fields(50#[builtin(fields(
51 settings: Cc<RefCell<Settings>>,51 settings: Cc<RefCell<Settings>>,
52))]52))]
53pub fn builtin_ext_var(this: &builtin_ext_var, ctx: Context, x: IStr) -> Result<Val> {53pub fn builtin_ext_var(this: &builtin_ext_var, x: IStr) -> Result<Val> {
54 let ctx = ctx.state().create_default_context(extvar_source(&x, ""));
55 this.settings54 this.settings
56 .borrow()55 .borrow()
57 .ext_vars56 .ext_vars
58 .get(&x)57 .get(&x)
59 .cloned()58 .cloned()
60 .ok_or_else(|| UndefinedExternalVariable(x))?59 .ok_or_else(|| UndefinedExternalVariable(x))?
61 .evaluate_arg(ctx, true)?
62 .evaluate()60 .evaluate_tailstrict()
63}61}
6462
65#[builtin(fields(63#[builtin(fields(
modifiedtests/tests/builtin.rsdiffbeforeafterboth
--- a/tests/tests/builtin.rs
+++ b/tests/tests/builtin.rs
@@ -19,7 +19,7 @@
 fn basic_function() -> Result<()> {
 	let a: a = a {};
 	let v = u32::from_untyped(a.call(
-		ContextBuilder::dangerous_empty_state().build(),
+		ContextBuilder::new().build(),
 		CallLocation::native(),
 		&(),
 	)?)?;