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
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -126,20 +126,29 @@
 /// During import, this trait will be called to create initial context for file.
 /// It may initialize global variables, stdlib for example.
 pub trait ContextInitializer: Trace {
+	/// For which size the builder should be preallocated
+	fn reserve_vars(&self) -> usize {
+		0
+	}
 	/// Initialize default file context.
-	fn initialize(&self, state: State, for_file: Source) -> Context;
+	/// Has default implementation, which calls `populate`.
+	/// Prefer to always implement `populate` instead.
+	fn initialize(&self, state: State, for_file: Source) -> Context {
+		let mut builder = ContextBuilder::with_capacity(state, self.reserve_vars());
+		self.populate(for_file, &mut builder);
+		builder.build()
+	}
+	/// For composability: extend builder. May panic if this initialization is not supported,
+	/// and the context may only be created via `initialize`.
+	fn populate(&self, for_file: Source, builder: &mut ContextBuilder);
 	/// Allows upcasting from abstract to concrete context initializer.
 	/// jrsonnet by itself doesn't use this method, it is allowed for it to panic.
 	fn as_any(&self) -> &dyn Any;
 }
 
 /// Context initializer which adds nothing.
-#[derive(Trace)]
-pub struct DummyContextInitializer;
-impl ContextInitializer for DummyContextInitializer {
-	fn initialize(&self, state: State, _for_file: Source) -> Context {
-		ContextBuilder::new(state).build()
-	}
+impl ContextInitializer for () {
+	fn populate(&self, _for_file: Source, _builder: &mut ContextBuilder) {}
 	fn as_any(&self) -> &dyn Any {
 		self
 	}
@@ -157,7 +166,7 @@
 impl Default for EvaluationSettings {
 	fn default() -> Self {
 		Self {
-			context_initializer: tb!(DummyContextInitializer),
+			context_initializer: tb!(()),
 			import_resolver: tb!(DummyImportResolver),
 		}
 	}
@@ -396,6 +405,46 @@
 	pub fn settings_mut(&self) -> RefMut<'_, EvaluationSettings> {
 		self.0.settings.borrow_mut()
 	}
+	pub fn add_global(&self, name: IStr, value: Thunk<Val>) {
+		#[derive(Trace)]
+		struct GlobalsCtx {
+			globals: RefCell<GcHashMap<IStr, Thunk<Val>>>,
+			inner: TraceBox<dyn ContextInitializer>,
+		}
+		impl ContextInitializer for GlobalsCtx {
+			fn reserve_vars(&self) -> usize {
+				self.inner.reserve_vars() + self.globals.borrow().len()
+			}
+			fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {
+				self.inner.populate(for_file, builder);
+				for (name, val) in self.globals.borrow().iter() {
+					builder.bind(name.clone(), val.clone());
+				}
+			}
+
+			fn as_any(&self) -> &dyn Any {
+				self
+			}
+		}
+		let mut settings = self.settings_mut();
+		let initializer = &mut settings.context_initializer;
+		match initializer.as_any().downcast_ref::<GlobalsCtx>() {
+			Some(glob) => {
+				glob.globals.borrow_mut().insert(name, value);
+			}
+			None => {
+				let inner = std::mem::replace(&mut settings.context_initializer, tb!(()));
+				settings.context_initializer = tb!(GlobalsCtx {
+					globals: {
+						let mut out = GcHashMap::with_capacity(1);
+						out.insert(name, value);
+						RefCell::new(out)
+					},
+					inner
+				})
+			}
+		}
+	}
 }
 
 /// Raw methods evaluate passed values but don't perform TLA execution
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
7use jrsonnet_evaluator::{7use jrsonnet_evaluator::{
8 error::{ErrorKind::*, Result},8 error::{ErrorKind::*, Result},
9 function::{builtin::Builtin, CallLocation, FuncVal, TlaArg},9 function::{builtin::Builtin, CallLocation, FuncVal, TlaArg},
10 gc::{GcHashMap, TraceBox},10 gc::TraceBox,
11 tb,11 tb,
12 trace::PathResolver,12 trace::PathResolver,
13 Context, ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,13 ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,
14};14};
15use jrsonnet_gcmodule::{Cc, Trace};15use jrsonnet_gcmodule::{Cc, Trace};
16use jrsonnet_parser::Source;16use jrsonnet_parser::Source;
231 pub ext_vars: HashMap<IStr, TlaArg>,231 pub ext_vars: HashMap<IStr, TlaArg>,
232 /// Used for `std.native`232 /// Used for `std.native`
233 pub ext_natives: HashMap<IStr, Cc<TraceBox<dyn Builtin>>>,233 pub ext_natives: HashMap<IStr, Cc<TraceBox<dyn Builtin>>>,
234 /// Helper to add globals without implementing custom ContextInitializer
235 pub globals: GcHashMap<IStr, Thunk<Val>>,
236 /// Used for `std.trace`234 /// Used for `std.trace`
237 pub trace_printer: Box<dyn TracePrinter>,235 pub trace_printer: Box<dyn TracePrinter>,
238 /// Used for `std.thisFile`236 /// Used for `std.thisFile`
246244
247#[derive(Trace, Clone)]245#[derive(Trace, Clone)]
248pub struct ContextInitializer {246pub struct ContextInitializer {
249 // When we don't need to support legacy-this-file, we can reuse same context for all files247 /// When we don't need to support legacy-this-file, we can reuse same context for all files
250 #[cfg(not(feature = "legacy-this-file"))]248 #[cfg(not(feature = "legacy-this-file"))]
251 context: Context,249 context: jrsonnet_evaluator::Context,
250 /// For `populate`
251 #[cfg(not(feature = "legacy-this-file"))]
252 stdlib_thunk: Thunk<Val>,
252 // Otherwise, we can only keep first stdlib layer, and then stack thisFile on top of it253 /// Otherwise, we can only keep first stdlib layer, and then stack thisFile on top of it
253 #[cfg(feature = "legacy-this-file")]254 #[cfg(feature = "legacy-this-file")]
254 stdlib_obj: ObjValue,255 stdlib_obj: ObjValue,
255 settings: Rc<RefCell<Settings>>,256 settings: Rc<RefCell<Settings>>,
259 let settings = Settings {260 let settings = Settings {
260 ext_vars: Default::default(),261 ext_vars: Default::default(),
261 ext_natives: Default::default(),262 ext_natives: Default::default(),
262 globals: Default::default(),
263 trace_printer: Box::new(StdTracePrinter::new(resolver.clone())),263 trace_printer: Box::new(StdTracePrinter::new(resolver.clone())),
264 path_resolver: resolver,264 path_resolver: resolver,
265 };265 };
266 let settings = Rc::new(RefCell::new(settings));266 let settings = Rc::new(RefCell::new(settings));
267 let stdlib_obj = stdlib_uncached(settings.clone());
268 #[cfg(not(feature = "legacy-this-file"))]
269 let stdlib_thunk = Thunk::evaluated(Val::Obj(stdlib_obj));
267 Self {270 Self {
268 #[cfg(not(feature = "legacy-this-file"))]271 #[cfg(not(feature = "legacy-this-file"))]
269 context: {272 context: {
270 let mut context = ContextBuilder::with_capacity(_s, 1);273 let mut context = ContextBuilder::with_capacity(_s, 1);
271 context.bind(274 context.bind("std".into(), stdlib_thunk.clone());
272 "std".into(),
273 Thunk::evaluated(Val::Obj(stdlib_uncached(settings.clone()))),
274 );
275 context.build()275 context.build()
276 },276 },
277 #[cfg(not(feature = "legacy-this-file"))]
278 stdlib_thunk,
277 #[cfg(feature = "legacy-this-file")]279 #[cfg(feature = "legacy-this-file")]
278 stdlib_obj: stdlib_uncached(settings.clone()),280 stdlib_obj,
279 settings,281 settings,
280 }282 }
281 }283 }
321 }323 }
322}324}
323impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {325impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {
326 fn reserve_vars(&self) -> usize {
327 1
328 }
324 #[cfg(not(feature = "legacy-this-file"))]329 #[cfg(not(feature = "legacy-this-file"))]
325 fn initialize(&self, _s: State, _source: Source) -> jrsonnet_evaluator::Context {330 fn initialize(&self, _s: State, _source: Source) -> jrsonnet_evaluator::Context {
331 self.context.clone()
332 }
326 let out = self.context.clone();333 #[cfg(not(feature = "legacy-this-file"))]
327 let globals = &self.settings().globals;334 fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) {
328 if globals.is_empty() {
329 return out;
330 }
331
332 let mut out = ContextBuilder::extend(out);
333 for (k, v) in globals.iter() {
334 out.bind(k.clone(), v.clone());335 builder.bind("std".into(), self.stdlib_thunk.clone());
335 }336 }
336 out.build()
337 }
338 #[cfg(feature = "legacy-this-file")]337 #[cfg(feature = "legacy-this-file")]
339 fn initialize(&self, s: State, source: Source) -> Context {338 fn populate(&self, source: Source, builder: &mut ContextBuilder) {
340 use jrsonnet_evaluator::val::StrValue;339 use jrsonnet_evaluator::val::StrValue;
341340
342 let mut builder = ObjValueBuilder::new();341 let mut std = ObjValueBuilder::new();
343 builder.with_super(self.stdlib_obj.clone());342 std.with_super(self.stdlib_obj.clone());
344 builder
345 .member("thisFile".into())343 std.member("thisFile".into())
346 .hide()344 .hide()
347 .value(Val::Str(StrValue::Flat(345 .value(Val::Str(StrValue::Flat(
348 match source.source_path().path() {346 match source.source_path().path() {
351 },349 },
352 )))350 )))
353 .expect("this object builder is empty");351 .expect("this object builder is empty");
354 let stdlib_with_this_file = builder.build();352 let stdlib_with_this_file = std.build();
355353
356 let mut context = ContextBuilder::with_capacity(s, 1);
357 context.bind(354 builder.bind(
358 "std".into(),355 "std".into(),
359 Thunk::evaluated(Val::Obj(stdlib_with_this_file)),356 Thunk::evaluated(Val::Obj(stdlib_with_this_file)),
360 );357 );
361 for (k, v) in self.settings().globals.iter() {
362 context.bind(k.clone(), v.clone());
363 }
364 context.build()
365 }358 }
366 fn as_any(&self) -> &dyn std::any::Any {359 fn as_any(&self) -> &dyn std::any::Any {
367 self360 self
371pub trait StateExt {364pub trait StateExt {
372 /// This method was previously implemented in jrsonnet-evaluator itself365 /// This method was previously implemented in jrsonnet-evaluator itself
373 fn with_stdlib(&self);366 fn with_stdlib(&self);
374 fn add_global(&self, name: IStr, value: Thunk<Val>);
375}367}
376368
377impl StateExt for State {369impl StateExt for State {
378 fn with_stdlib(&self) {370 fn with_stdlib(&self) {
379 let initializer = ContextInitializer::new(self.clone(), PathResolver::new_cwd_fallback());371 let initializer = ContextInitializer::new(self.clone(), PathResolver::new_cwd_fallback());
380 self.settings_mut().context_initializer = tb!(initializer)372 self.settings_mut().context_initializer = tb!(initializer)
381 }373 }
382 fn add_global(&self, name: IStr, value: Thunk<Val>) {
383 self.settings()
384 .context_initializer
385 .as_any()
386 .downcast_ref::<ContextInitializer>()
387 .expect("not standard context initializer")
388 .settings_mut()
389 .globals
390 .insert(name, value);
391 }
392}374}
393375
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 {