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
--- 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
before · tests/tests/common.rs
1use std::borrow::Cow;23use jrsonnet_evaluator::{4	error::Result,5	function::{builtin, FuncVal},6	throw, ObjValueBuilder, State, Thunk, Val,7};8use jrsonnet_stdlib::StateExt;910#[macro_export]11macro_rules! ensure_eq {12	($a:expr, $b:expr $(,)?) => {{13		let a = &$a;14		let b = &$b;15		if a != b {16			::jrsonnet_evaluator::throw!("assertion failed: a != b\na={:#?}\nb={:#?}", a, b)17		}18	}};19}2021#[macro_export]22macro_rules! ensure {23	($v:expr $(,)?) => {24		if !$v {25			::jrsonnet_evaluator::throw!("assertion failed: {}", stringify!($v))26		}27	};28}2930#[macro_export]31macro_rules! ensure_val_eq {32	($a:expr, $b:expr) => {{33		if !::jrsonnet_evaluator::val::equals(&$a.clone(), &$b.clone())? {34			use ::jrsonnet_evaluator::manifest::JsonFormat;35			::jrsonnet_evaluator::throw!(36				"assertion failed: a != b\na={:#?}\nb={:#?}",37				$a.manifest(JsonFormat::default())?,38				$b.manifest(JsonFormat::default())?,39			)40		}41	}};42}4344#[builtin]45fn assert_throw(lazy: Thunk<Val>, message: String) -> Result<bool> {46	match lazy.evaluate() {47		Ok(_) => {48			throw!("expected argument to throw on evaluation, but it returned instead")49		}50		Err(e) => {51			let error = format!("{}", e.error());52			ensure_eq!(message, error);53		}54	}55	Ok(true)56}5758#[builtin]59fn param_names(fun: FuncVal) -> Vec<String> {60	match fun {61		FuncVal::Id => vec!["x".to_string()],62		FuncVal::Normal(func) => func63			.params64			.iter()65			.map(|p| p.0.name().unwrap_or_else(|| "<unnamed>".into()).to_string())66			.collect(),67		FuncVal::StaticBuiltin(b) => b68			.params()69			.iter()70			.map(|p| {71				p.name72					.as_ref()73					.unwrap_or(&Cow::Borrowed("<unnamed>"))74					.to_string()75			})76			.collect(),77		FuncVal::Builtin(b) => b78			.params()79			.iter()80			.map(|p| {81				p.name82					.as_ref()83					.unwrap_or(&Cow::Borrowed("<unnamed>"))84					.to_string()85			})86			.collect(),87	}88}8990#[allow(dead_code)]91pub fn with_test(s: &State) {92	let mut bobj = ObjValueBuilder::new();93	bobj.member("assertThrow".into())94		.hide()95		.value_unchecked(Val::Func(FuncVal::StaticBuiltin(assert_throw::INST)));96	bobj.member("paramNames".into())97		.hide()98		.value_unchecked(Val::Func(FuncVal::StaticBuiltin(param_names::INST)));99100	s.add_global("test".into(), Thunk::evaluated(Val::Obj(bobj.build())))101}
after · tests/tests/common.rs
1use std::borrow::Cow;23use jrsonnet_evaluator::{4	error::Result,5	function::{builtin, FuncVal},6	throw, ObjValueBuilder, State, Thunk, Val,7};89#[macro_export]10macro_rules! ensure_eq {11	($a:expr, $b:expr $(,)?) => {{12		let a = &$a;13		let b = &$b;14		if a != b {15			::jrsonnet_evaluator::throw!("assertion failed: a != b\na={:#?}\nb={:#?}", a, b)16		}17	}};18}1920#[macro_export]21macro_rules! ensure {22	($v:expr $(,)?) => {23		if !$v {24			::jrsonnet_evaluator::throw!("assertion failed: {}", stringify!($v))25		}26	};27}2829#[macro_export]30macro_rules! ensure_val_eq {31	($a:expr, $b:expr) => {{32		if !::jrsonnet_evaluator::val::equals(&$a.clone(), &$b.clone())? {33			use ::jrsonnet_evaluator::manifest::JsonFormat;34			::jrsonnet_evaluator::throw!(35				"assertion failed: a != b\na={:#?}\nb={:#?}",36				$a.manifest(JsonFormat::default())?,37				$b.manifest(JsonFormat::default())?,38			)39		}40	}};41}4243#[builtin]44fn assert_throw(lazy: Thunk<Val>, message: String) -> Result<bool> {45	match lazy.evaluate() {46		Ok(_) => {47			throw!("expected argument to throw on evaluation, but it returned instead")48		}49		Err(e) => {50			let error = format!("{}", e.error());51			ensure_eq!(message, error);52		}53	}54	Ok(true)55}5657#[builtin]58fn param_names(fun: FuncVal) -> Vec<String> {59	match fun {60		FuncVal::Id => vec!["x".to_string()],61		FuncVal::Normal(func) => func62			.params63			.iter()64			.map(|p| p.0.name().unwrap_or_else(|| "<unnamed>".into()).to_string())65			.collect(),66		FuncVal::StaticBuiltin(b) => b67			.params()68			.iter()69			.map(|p| {70				p.name71					.as_ref()72					.unwrap_or(&Cow::Borrowed("<unnamed>"))73					.to_string()74			})75			.collect(),76		FuncVal::Builtin(b) => b77			.params()78			.iter()79			.map(|p| {80				p.name81					.as_ref()82					.unwrap_or(&Cow::Borrowed("<unnamed>"))83					.to_string()84			})85			.collect(),86	}87}8889#[allow(dead_code)]90pub fn with_test(s: &State) {91	let mut bobj = ObjValueBuilder::new();92	bobj.member("assertThrow".into())93		.hide()94		.value_unchecked(Val::Func(FuncVal::StaticBuiltin(assert_throw::INST)));95	bobj.member("paramNames".into())96		.hide()97		.value_unchecked(Val::Func(FuncVal::StaticBuiltin(param_names::INST)));9899	s.add_global("test".into(), Thunk::evaluated(Val::Obj(bobj.build())))100}