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
before · crates/jrsonnet-evaluator/src/lib.rs
1//! jsonnet interpreter implementation2#![cfg_attr(feature = "nightly", feature(thread_local, type_alias_impl_trait))]3#![deny(unsafe_op_in_unsafe_fn)]4#![warn(5	clippy::all,6	clippy::nursery,7	clippy::pedantic,8	// missing_docs,9	elided_lifetimes_in_paths,10	explicit_outlives_requirements,11	noop_method_call,12	single_use_lifetimes,13	variant_size_differences,14	rustdoc::all15)]16#![allow(17	macro_expanded_macro_exports_accessed_by_absolute_paths,18	clippy::ptr_arg,19	// Too verbose20	clippy::must_use_candidate,21	// A lot of functions pass around errors thrown by code22	clippy::missing_errors_doc,23	// A lot of pointers have interior Rc24	clippy::needless_pass_by_value,25	// Its fine26	clippy::wildcard_imports,27	clippy::enum_glob_use,28	clippy::module_name_repetitions,29	// TODO: fix individual issues, however this works as intended almost everywhere30	clippy::cast_precision_loss,31	clippy::cast_possible_wrap,32	clippy::cast_possible_truncation,33	clippy::cast_sign_loss,34	// False positives35	// https://github.com/rust-lang/rust-clippy/issues/690236	clippy::use_self,37	// https://github.com/rust-lang/rust-clippy/issues/853938	clippy::iter_with_drain,39	clippy::type_repetition_in_bounds,40	// ci is being run with nightly, but library should work on stable41	clippy::missing_const_for_fn,42)]4344// For jrsonnet-macros45extern crate self as jrsonnet_evaluator;4647mod arr;48#[cfg(feature = "async-import")]49pub mod async_import;50mod ctx;51mod dynamic;52pub mod error;53mod evaluate;54pub mod function;55pub mod gc;56mod import;57mod integrations;58pub mod manifest;59mod map;60mod obj;61pub mod stack;62pub mod stdlib;63mod tla;64pub mod trace;65pub mod typed;66pub mod val;6768use std::{69	any::Any,70	cell::{Ref, RefCell, RefMut},71	fmt::{self, Debug},72	path::Path,73};7475pub use ctx::*;76pub use dynamic::*;77pub use error::{Error, ErrorKind::*, Result, ResultExt};78pub use evaluate::*;79use function::CallLocation;80use gc::{GcHashMap, TraceBox};81use hashbrown::hash_map::RawEntryMut;82pub use import::*;83use jrsonnet_gcmodule::{Cc, Trace};84pub use jrsonnet_interner::{IBytes, IStr};85pub use jrsonnet_parser as parser;86use jrsonnet_parser::*;87pub use obj::*;88use stack::check_depth;89pub use tla::apply_tla;90pub use val::{Thunk, Val};9192/// Thunk without bound `super`/`this`93/// object inheritance may be overriden multiple times, and will be fixed only on field read94pub trait Unbound: Trace {95	/// Type of value after object context is bound96	type Bound;97	/// Create value bound to specified object context98	fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Self::Bound>;99}100101/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code102/// Standard jsonnet fields are always unbound103#[derive(Clone, Trace)]104pub enum MaybeUnbound {105	/// Value needs to be bound to `this`/`super`106	Unbound(Cc<TraceBox<dyn Unbound<Bound = Val>>>),107	/// Value is object-independent108	Bound(Thunk<Val>),109}110111impl Debug for MaybeUnbound {112	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {113		write!(f, "MaybeUnbound")114	}115}116impl MaybeUnbound {117	/// Attach object context to value, if required118	pub fn evaluate(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {119		match self {120			Self::Unbound(v) => v.bind(sup, this),121			Self::Bound(v) => Ok(v.evaluate()?),122		}123	}124}125126/// During import, this trait will be called to create initial context for file.127/// It may initialize global variables, stdlib for example.128pub trait ContextInitializer: Trace {129	/// Initialize default file context.130	fn initialize(&self, state: State, for_file: Source) -> Context;131	/// Allows upcasting from abstract to concrete context initializer.132	/// jrsonnet by itself doesn't use this method, it is allowed for it to panic.133	fn as_any(&self) -> &dyn Any;134}135136/// Context initializer which adds nothing.137#[derive(Trace)]138pub struct DummyContextInitializer;139impl ContextInitializer for DummyContextInitializer {140	fn initialize(&self, state: State, _for_file: Source) -> Context {141		ContextBuilder::new(state).build()142	}143	fn as_any(&self) -> &dyn Any {144		self145	}146}147148/// Dynamically reconfigurable evaluation settings149#[derive(Trace)]150pub struct EvaluationSettings {151	/// Context initializer, which will be used for imports and everything152	/// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`153	pub context_initializer: TraceBox<dyn ContextInitializer>,154	/// Used to resolve file locations/contents155	pub import_resolver: TraceBox<dyn ImportResolver>,156}157impl Default for EvaluationSettings {158	fn default() -> Self {159		Self {160			context_initializer: tb!(DummyContextInitializer),161			import_resolver: tb!(DummyImportResolver),162		}163	}164}165166#[derive(Trace)]167struct FileData {168	string: Option<IStr>,169	bytes: Option<IBytes>,170	parsed: Option<LocExpr>,171	evaluated: Option<Val>,172173	evaluating: bool,174}175impl FileData {176	fn new_string(data: IStr) -> Self {177		Self {178			string: Some(data),179			bytes: None,180			parsed: None,181			evaluated: None,182			evaluating: false,183		}184	}185	fn new_bytes(data: IBytes) -> Self {186		Self {187			string: None,188			bytes: Some(data),189			parsed: None,190			evaluated: None,191			evaluating: false,192		}193	}194	pub(crate) fn get_string(&mut self) -> Option<IStr> {195		if self.string.is_none() {196			self.string = Some(197				self.bytes198					.as_ref()199					.expect("either string or bytes should be set")200					.clone()201					.cast_str()?,202			);203		}204		Some(self.string.clone().expect("just set"))205	}206}207208#[derive(Default, Trace)]209pub struct EvaluationStateInternals {210	/// Internal state211	file_cache: RefCell<GcHashMap<SourcePath, FileData>>,212	/// Settings, safe to change at runtime213	settings: RefCell<EvaluationSettings>,214}215216/// Maintains stack trace and import resolution217#[derive(Default, Clone, Trace)]218pub struct State(Cc<EvaluationStateInternals>);219220impl State {221	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise222	pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {223		let mut file_cache = self.file_cache();224		let mut file = file_cache.raw_entry_mut().from_key(&path);225226		let file = match file {227			RawEntryMut::Occupied(ref mut d) => d.get_mut(),228			RawEntryMut::Vacant(v) => {229				let data = self.settings().import_resolver.load_file_contents(&path)?;230				v.insert(231					path.clone(),232					FileData::new_string(233						std::str::from_utf8(&data)234							.map_err(|_| ImportBadFileUtf8(path.clone()))?235							.into(),236					),237				)238				.1239			}240		};241		Ok(file242			.get_string()243			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?)244	}245	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise246	pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {247		let mut file_cache = self.file_cache();248		let mut file = file_cache.raw_entry_mut().from_key(&path);249250		let file = match file {251			RawEntryMut::Occupied(ref mut d) => d.get_mut(),252			RawEntryMut::Vacant(v) => {253				let data = self.settings().import_resolver.load_file_contents(&path)?;254				v.insert(path.clone(), FileData::new_bytes(data.as_slice().into()))255					.1256			}257		};258		if let Some(str) = &file.bytes {259			return Ok(str.clone());260		}261		if file.bytes.is_none() {262			file.bytes = Some(263				file.string264					.as_ref()265					.expect("either string or bytes should be set")266					.clone()267					.cast_bytes(),268			);269		}270		Ok(file.bytes.as_ref().expect("just set").clone())271	}272	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise273	pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {274		let mut file_cache = self.file_cache();275		let mut file = file_cache.raw_entry_mut().from_key(&path);276277		let file = match file {278			RawEntryMut::Occupied(ref mut d) => d.get_mut(),279			RawEntryMut::Vacant(v) => {280				let data = self.settings().import_resolver.load_file_contents(&path)?;281				v.insert(282					path.clone(),283					FileData::new_string(284						std::str::from_utf8(&data)285							.map_err(|_| ImportBadFileUtf8(path.clone()))?286							.into(),287					),288				)289				.1290			}291		};292		if let Some(val) = &file.evaluated {293			return Ok(val.clone());294		}295		let code = file296			.get_string()297			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?;298		let file_name = Source::new(path.clone(), code.clone());299		if file.parsed.is_none() {300			file.parsed = Some(301				jrsonnet_parser::parse(302					&code,303					&ParserSettings {304						source: file_name.clone(),305					},306				)307				.map_err(|e| ImportSyntaxError {308					path: file_name.clone(),309					error: Box::new(e),310				})?,311			);312		}313		let parsed = file.parsed.as_ref().expect("just set").clone();314		if file.evaluating {315			throw!(InfiniteRecursionDetected)316		}317		file.evaluating = true;318		// Dropping file cache guard here, as evaluation may use this map too319		drop(file_cache);320		let res = evaluate(self.create_default_context(file_name), &parsed);321322		let mut file_cache = self.file_cache();323		let mut file = file_cache.raw_entry_mut().from_key(&path);324325		let RawEntryMut::Occupied(file) = &mut file else {326			unreachable!("this file was just here!")327		};328		let file = file.get_mut();329		file.evaluating = false;330		match res {331			Ok(v) => {332				file.evaluated = Some(v.clone());333				Ok(v)334			}335			Err(e) => Err(e),336		}337	}338339	/// Has same semantics as `import 'path'` called from `from` file340	pub fn import_from(&self, from: &SourcePath, path: &str) -> Result<Val> {341		let resolved = self.resolve_from(from, path)?;342		self.import_resolved(resolved)343	}344	pub fn import(&self, path: impl AsRef<Path>) -> Result<Val> {345		let resolved = self.resolve(path)?;346		self.import_resolved(resolved)347	}348349	/// Creates context with all passed global variables350	pub fn create_default_context(&self, source: Source) -> Context {351		let context_initializer = &self.settings().context_initializer;352		context_initializer.initialize(self.clone(), source)353	}354355	/// Executes code creating a new stack frame356	pub fn push<T>(357		e: CallLocation<'_>,358		frame_desc: impl FnOnce() -> String,359		f: impl FnOnce() -> Result<T>,360	) -> Result<T> {361		let _guard = check_depth()?;362363		f().with_description_src(e, frame_desc)364	}365366	/// Executes code creating a new stack frame367	pub fn push_val(368		&self,369		e: &ExprLocation,370		frame_desc: impl FnOnce() -> String,371		f: impl FnOnce() -> Result<Val>,372	) -> Result<Val> {373		let _guard = check_depth()?;374375		f().with_description_src(e, frame_desc)376	}377	/// Executes code creating a new stack frame378	pub fn push_description<T>(379		frame_desc: impl FnOnce() -> String,380		f: impl FnOnce() -> Result<T>,381	) -> Result<T> {382		let _guard = check_depth()?;383384		f().with_description(frame_desc)385	}386}387388/// Internals389impl State {390	fn file_cache(&self) -> RefMut<'_, GcHashMap<SourcePath, FileData>> {391		self.0.file_cache.borrow_mut()392	}393	pub fn settings(&self) -> Ref<'_, EvaluationSettings> {394		self.0.settings.borrow()395	}396	pub fn settings_mut(&self) -> RefMut<'_, EvaluationSettings> {397		self.0.settings.borrow_mut()398	}399}400401/// Raw methods evaluate passed values but don't perform TLA execution402impl State {403	/// Parses and evaluates the given snippet404	pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {405		let code = code.into();406		let source = Source::new_virtual(name.into(), code.clone());407		let parsed = jrsonnet_parser::parse(408			&code,409			&ParserSettings {410				source: source.clone(),411			},412		)413		.map_err(|e| ImportSyntaxError {414			path: source.clone(),415			error: Box::new(e),416		})?;417		evaluate(self.create_default_context(source), &parsed)418	}419}420421/// Settings utilities422impl State {423	// Only panics in case of [`ImportResolver`] contract violation424	#[allow(clippy::missing_panics_doc)]425	pub fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {426		self.import_resolver().resolve_from(from, path.as_ref())427	}428429	// Only panics in case of [`ImportResolver`] contract violation430	#[allow(clippy::missing_panics_doc)]431	pub fn resolve(&self, path: impl AsRef<Path>) -> Result<SourcePath> {432		self.import_resolver().resolve(path.as_ref())433	}434	pub fn import_resolver(&self) -> Ref<'_, dyn ImportResolver> {435		Ref::map(self.settings(), |s| &*s.import_resolver)436	}437	pub fn set_import_resolver(&self, resolver: impl ImportResolver) {438		self.settings_mut().import_resolver = tb!(resolver);439	}440	pub fn context_initializer(&self) -> Ref<'_, dyn ContextInitializer> {441		Ref::map(self.settings(), |s| &*s.context_initializer)442	}443	pub fn set_context_initializer(&self, initializer: impl ContextInitializer) {444		self.settings_mut().context_initializer = tb!(initializer);445	}446}
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 {