git.delta.rocks / jrsonnet / refs/commits / e0d3ba219c30

difftreelog

feat composable ContextInitializer

Yaroslav Bolyukin2023-07-13parent: #e98be8b.patch.diff
in: master

5 files changed

modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
126/// During import, this trait will be called to create initial context for file.126/// During import, this trait will be called to create initial context for file.
127/// It may initialize global variables, stdlib for example.127/// It may initialize global variables, stdlib for example.
128pub trait ContextInitializer: Trace {128pub trait ContextInitializer: Trace {
129 /// For which size the builder should be preallocated
130 fn reserve_vars(&self) -> usize {
131 0
132 }
129 /// Initialize default file context.133 /// Initialize default file context.
134 /// Has default implementation, which calls `populate`.
135 /// Prefer to always implement `populate` instead.
130 fn initialize(&self, state: State, for_file: Source) -> Context;136 fn initialize(&self, state: State, for_file: Source) -> Context {
137 let mut builder = ContextBuilder::with_capacity(state, self.reserve_vars());
138 self.populate(for_file, &mut builder);
139 builder.build()
140 }
141 /// For composability: extend builder. May panic if this initialization is not supported,
142 /// and the context may only be created via `initialize`.
143 fn populate(&self, for_file: Source, builder: &mut ContextBuilder);
131 /// Allows upcasting from abstract to concrete context initializer.144 /// Allows upcasting from abstract to concrete context initializer.
132 /// jrsonnet by itself doesn't use this method, it is allowed for it to panic.145 /// jrsonnet by itself doesn't use this method, it is allowed for it to panic.
133 fn as_any(&self) -> &dyn Any;146 fn as_any(&self) -> &dyn Any;
134}147}
135148
136/// Context initializer which adds nothing.149/// Context initializer which adds nothing.
137#[derive(Trace)]
138pub struct DummyContextInitializer;
139impl ContextInitializer for DummyContextInitializer {150impl ContextInitializer for () {
140 fn initialize(&self, state: State, _for_file: Source) -> Context {151 fn populate(&self, _for_file: Source, _builder: &mut ContextBuilder) {}
141 ContextBuilder::new(state).build()
142 }
143 fn as_any(&self) -> &dyn Any {152 fn as_any(&self) -> &dyn Any {
144 self153 self
157impl Default for EvaluationSettings {166impl Default for EvaluationSettings {
158 fn default() -> Self {167 fn default() -> Self {
159 Self {168 Self {
160 context_initializer: tb!(DummyContextInitializer),169 context_initializer: tb!(()),
161 import_resolver: tb!(DummyImportResolver),170 import_resolver: tb!(DummyImportResolver),
162 }171 }
163 }172 }
396 pub fn settings_mut(&self) -> RefMut<'_, EvaluationSettings> {405 pub fn settings_mut(&self) -> RefMut<'_, EvaluationSettings> {
397 self.0.settings.borrow_mut()406 self.0.settings.borrow_mut()
398 }407 }
408 pub fn add_global(&self, name: IStr, value: Thunk<Val>) {
409 #[derive(Trace)]
410 struct GlobalsCtx {
411 globals: RefCell<GcHashMap<IStr, Thunk<Val>>>,
412 inner: TraceBox<dyn ContextInitializer>,
413 }
414 impl ContextInitializer for GlobalsCtx {
415 fn reserve_vars(&self) -> usize {
416 self.inner.reserve_vars() + self.globals.borrow().len()
417 }
418 fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {
419 self.inner.populate(for_file, builder);
420 for (name, val) in self.globals.borrow().iter() {
421 builder.bind(name.clone(), val.clone());
422 }
423 }
424
425 fn as_any(&self) -> &dyn Any {
426 self
427 }
428 }
429 let mut settings = self.settings_mut();
430 let initializer = &mut settings.context_initializer;
431 match initializer.as_any().downcast_ref::<GlobalsCtx>() {
432 Some(glob) => {
433 glob.globals.borrow_mut().insert(name, value);
434 }
435 None => {
436 let inner = std::mem::replace(&mut settings.context_initializer, tb!(()));
437 settings.context_initializer = tb!(GlobalsCtx {
438 globals: {
439 let mut out = GcHashMap::with_capacity(1);
440 out.insert(name, value);
441 RefCell::new(out)
442 },
443 inner
444 })
445 }
446 }
447 }
399}448}
400449
401/// Raw methods evaluate passed values but don't perform TLA execution450/// Raw methods evaluate passed values but don't perform TLA execution
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -7,10 +7,10 @@
 use jrsonnet_evaluator::{
 	error::{ErrorKind::*, Result},
 	function::{builtin::Builtin, CallLocation, FuncVal, TlaArg},
-	gc::{GcHashMap, TraceBox},
+	gc::TraceBox,
 	tb,
 	trace::PathResolver,
-	Context, ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,
+	ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,
 };
 use jrsonnet_gcmodule::{Cc, Trace};
 use jrsonnet_parser::Source;
@@ -231,8 +231,6 @@
 	pub ext_vars: HashMap<IStr, TlaArg>,
 	/// Used for `std.native`
 	pub ext_natives: HashMap<IStr, Cc<TraceBox<dyn Builtin>>>,
-	/// Helper to add globals without implementing custom ContextInitializer
-	pub globals: GcHashMap<IStr, Thunk<Val>>,
 	/// Used for `std.trace`
 	pub trace_printer: Box<dyn TracePrinter>,
 	/// Used for `std.thisFile`
@@ -246,10 +244,13 @@
 
 #[derive(Trace, Clone)]
 pub struct ContextInitializer {
-	// When we don't need to support legacy-this-file, we can reuse same context for all files
+	/// When we don't need to support legacy-this-file, we can reuse same context for all files
 	#[cfg(not(feature = "legacy-this-file"))]
-	context: Context,
-	// Otherwise, we can only keep first stdlib layer, and then stack thisFile on top of it
+	context: jrsonnet_evaluator::Context,
+	/// For `populate`
+	#[cfg(not(feature = "legacy-this-file"))]
+	stdlib_thunk: Thunk<Val>,
+	/// Otherwise, we can only keep first stdlib layer, and then stack thisFile on top of it
 	#[cfg(feature = "legacy-this-file")]
 	stdlib_obj: ObjValue,
 	settings: Rc<RefCell<Settings>>,
@@ -259,23 +260,24 @@
 		let settings = Settings {
 			ext_vars: Default::default(),
 			ext_natives: Default::default(),
-			globals: Default::default(),
 			trace_printer: Box::new(StdTracePrinter::new(resolver.clone())),
 			path_resolver: resolver,
 		};
 		let settings = Rc::new(RefCell::new(settings));
+		let stdlib_obj = stdlib_uncached(settings.clone());
+		#[cfg(not(feature = "legacy-this-file"))]
+		let stdlib_thunk = Thunk::evaluated(Val::Obj(stdlib_obj));
 		Self {
 			#[cfg(not(feature = "legacy-this-file"))]
 			context: {
 				let mut context = ContextBuilder::with_capacity(_s, 1);
-				context.bind(
-					"std".into(),
-					Thunk::evaluated(Val::Obj(stdlib_uncached(settings.clone()))),
-				);
+				context.bind("std".into(), stdlib_thunk.clone());
 				context.build()
 			},
+			#[cfg(not(feature = "legacy-this-file"))]
+			stdlib_thunk,
 			#[cfg(feature = "legacy-this-file")]
-			stdlib_obj: stdlib_uncached(settings.clone()),
+			stdlib_obj,
 			settings,
 		}
 	}
@@ -321,28 +323,24 @@
 	}
 }
 impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {
+	fn reserve_vars(&self) -> usize {
+		1
+	}
 	#[cfg(not(feature = "legacy-this-file"))]
 	fn initialize(&self, _s: State, _source: Source) -> jrsonnet_evaluator::Context {
-		let out = self.context.clone();
-		let globals = &self.settings().globals;
-		if globals.is_empty() {
-			return out;
-		}
-
-		let mut out = ContextBuilder::extend(out);
-		for (k, v) in globals.iter() {
-			out.bind(k.clone(), v.clone());
-		}
-		out.build()
+		self.context.clone()
+	}
+	#[cfg(not(feature = "legacy-this-file"))]
+	fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) {
+		builder.bind("std".into(), self.stdlib_thunk.clone());
 	}
 	#[cfg(feature = "legacy-this-file")]
-	fn initialize(&self, s: State, source: Source) -> Context {
+	fn populate(&self, source: Source, builder: &mut ContextBuilder) {
 		use jrsonnet_evaluator::val::StrValue;
 
-		let mut builder = ObjValueBuilder::new();
-		builder.with_super(self.stdlib_obj.clone());
-		builder
-			.member("thisFile".into())
+		let mut std = ObjValueBuilder::new();
+		std.with_super(self.stdlib_obj.clone());
+		std.member("thisFile".into())
 			.hide()
 			.value(Val::Str(StrValue::Flat(
 				match source.source_path().path() {
@@ -351,17 +349,12 @@
 				},
 			)))
 			.expect("this object builder is empty");
-		let stdlib_with_this_file = builder.build();
+		let stdlib_with_this_file = std.build();
 
-		let mut context = ContextBuilder::with_capacity(s, 1);
-		context.bind(
+		builder.bind(
 			"std".into(),
 			Thunk::evaluated(Val::Obj(stdlib_with_this_file)),
 		);
-		for (k, v) in self.settings().globals.iter() {
-			context.bind(k.clone(), v.clone());
-		}
-		context.build()
 	}
 	fn as_any(&self) -> &dyn std::any::Any {
 		self
@@ -371,22 +364,11 @@
 pub trait StateExt {
 	/// This method was previously implemented in jrsonnet-evaluator itself
 	fn with_stdlib(&self);
-	fn add_global(&self, name: IStr, value: Thunk<Val>);
 }
 
 impl StateExt for State {
 	fn with_stdlib(&self) {
 		let initializer = ContextInitializer::new(self.clone(), PathResolver::new_cwd_fallback());
 		self.settings_mut().context_initializer = tb!(initializer)
-	}
-	fn add_global(&self, name: IStr, value: Thunk<Val>) {
-		self.settings()
-			.context_initializer
-			.as_any()
-			.downcast_ref::<ContextInitializer>()
-			.expect("not standard context initializer")
-			.settings_mut()
-			.globals
-			.insert(name, value);
 	}
 }
modifiedflake.lockdiffbeforeafterboth
--- a/flake.lock
+++ b/flake.lock
@@ -5,11 +5,11 @@
         "systems": "systems"
       },
       "locked": {
-        "lastModified": 1681202837,
-        "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=",
+        "lastModified": 1689068808,
+        "narHash": "sha256-6ixXo3wt24N/melDWjq70UuHQLxGV8jZvooRanIHXw0=",
         "owner": "numtide",
         "repo": "flake-utils",
-        "rev": "cfacdce06f30d2b68473a46042957675eebb3401",
+        "rev": "919d646de7be200f3bf08cb76ae1f09402b6f9b4",
         "type": "github"
       },
       "original": {
@@ -20,11 +20,11 @@
     },
     "nixpkgs": {
       "locked": {
-        "lastModified": 1683574088,
-        "narHash": "sha256-RjE7UXfyYBV3vkpjL5irZOF+4ZgTQlvEWYJsFL2Hig0=",
+        "lastModified": 1689162265,
+        "narHash": "sha256-kdW79sfwX2TTX8yFBNUsEYOG+gQuAOHU+WcUtxMUnlc=",
         "owner": "nixos",
         "repo": "nixpkgs",
-        "rev": "05b1a97381588ba98d98f8725b2137fce0ab45cb",
+        "rev": "1941c7d8f1219c615a1d6dae826e0d6fab89acca",
         "type": "github"
       },
       "original": {
@@ -50,11 +50,11 @@
         ]
       },
       "locked": {
-        "lastModified": 1683512408,
-        "narHash": "sha256-QMJGp/37En+d5YocJuSU89GL14bBYkIJQ6mqhRfqkkc=",
+        "lastModified": 1689129196,
+        "narHash": "sha256-/z/Al4sFcIh5oPQWA9MclQmJR9g3RO8UDiHGaj/T9R8=",
         "owner": "oxalica",
         "repo": "rust-overlay",
-        "rev": "75b07756c3feb22cf230e75fb064c1b4c725b9bc",
+        "rev": "db8d909c9526d4406579ee7343bf2d7de3d15eac",
         "type": "github"
       },
       "original": {
modifiedflake.nixdiffbeforeafterboth
--- a/flake.nix
+++ b/flake.nix
@@ -16,7 +16,7 @@
           inherit system;
           overlays = [ rust-overlay.overlays.default ];
         };
-        rust = ((pkgs.rustChannelOf { date = "2023-05-07"; channel = "nightly"; }).default.override {
+        rust = ((pkgs.rustChannelOf { date = "2023-06-26"; channel = "nightly"; }).default.override {
           extensions = [ "rust-src" "miri" "rust-analyzer" ];
         });
       in
modifiedtests/tests/common.rsdiffbeforeafterboth
--- a/tests/tests/common.rs
+++ b/tests/tests/common.rs
@@ -5,7 +5,6 @@
 	function::{builtin, FuncVal},
 	throw, ObjValueBuilder, State, Thunk, Val,
 };
-use jrsonnet_stdlib::StateExt;
 
 #[macro_export]
 macro_rules! ensure_eq {