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

difftreelog

doc: move lints to Crates.io

Yaroslav Bolyukin2024-03-17parent: #32dd70e.patch.diff
in: master

1 file 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	// too many false-positives with .expect() calls43	clippy::missing_panics_doc,44	// false positive for IStr type. There is an configuration option for45	// such cases, but it doesn't work:46	// https://github.com/rust-lang/rust-clippy/issues/980147	clippy::mutable_key_type,48	// false positives49	clippy::redundant_pub_crate,50	// Sometimes code is fancier without that51	clippy::manual_let_else,52)]5354// For jrsonnet-macros55extern crate self as jrsonnet_evaluator;5657mod arr;58#[cfg(feature = "async-import")]59pub mod async_import;60mod ctx;61mod dynamic;62pub mod error;63mod evaluate;64pub mod function;65pub mod gc;66mod import;67mod integrations;68pub mod manifest;69mod map;70mod obj;71pub mod stack;72pub mod stdlib;73mod tla;74pub mod trace;75pub mod typed;76pub mod val;7778use std::{79	any::Any,80	cell::{Ref, RefCell, RefMut},81	fmt::{self, Debug},82	path::Path,83};8485pub use ctx::*;86pub use dynamic::*;87pub use error::{Error, ErrorKind::*, Result, ResultExt};88pub use evaluate::*;89use function::CallLocation;90use gc::{GcHashMap, TraceBox};91use hashbrown::hash_map::RawEntryMut;92pub use import::*;93use jrsonnet_gcmodule::{Cc, Trace};94pub use jrsonnet_interner::{IBytes, IStr};95#[doc(hidden)]96pub use jrsonnet_macros;97pub use jrsonnet_parser as parser;98use jrsonnet_parser::*;99pub use obj::*;100use stack::check_depth;101pub use tla::apply_tla;102pub use val::{Thunk, Val};103104/// Thunk without bound `super`/`this`105/// object inheritance may be overriden multiple times, and will be fixed only on field read106pub trait Unbound: Trace {107	/// Type of value after object context is bound108	type Bound;109	/// Create value bound to specified object context110	fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Self::Bound>;111}112113/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code114/// Standard jsonnet fields are always unbound115#[derive(Clone, Trace)]116pub enum MaybeUnbound {117	/// Value needs to be bound to `this`/`super`118	Unbound(Cc<TraceBox<dyn Unbound<Bound = Val>>>),119	/// Value is object-independent120	Bound(Thunk<Val>),121}122123impl Debug for MaybeUnbound {124	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {125		write!(f, "MaybeUnbound")126	}127}128impl MaybeUnbound {129	/// Attach object context to value, if required130	pub fn evaluate(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {131		match self {132			Self::Unbound(v) => v.bind(sup, this),133			Self::Bound(v) => Ok(v.evaluate()?),134		}135	}136}137138/// During import, this trait will be called to create initial context for file.139/// It may initialize global variables, stdlib for example.140pub trait ContextInitializer: Trace {141	/// For which size the builder should be preallocated142	fn reserve_vars(&self) -> usize {143		0144	}145	/// Initialize default file context.146	/// Has default implementation, which calls `populate`.147	/// Prefer to always implement `populate` instead.148	fn initialize(&self, state: State, for_file: Source) -> Context {149		let mut builder = ContextBuilder::with_capacity(state, self.reserve_vars());150		self.populate(for_file, &mut builder);151		builder.build()152	}153	/// For composability: extend builder. May panic if this initialization is not supported,154	/// and the context may only be created via `initialize`.155	fn populate(&self, for_file: Source, builder: &mut ContextBuilder);156	/// Allows upcasting from abstract to concrete context initializer.157	/// jrsonnet by itself doesn't use this method, it is allowed for it to panic.158	fn as_any(&self) -> &dyn Any;159}160161/// Context initializer which adds nothing.162impl ContextInitializer for () {163	fn populate(&self, _for_file: Source, _builder: &mut ContextBuilder) {}164	fn as_any(&self) -> &dyn Any {165		self166	}167}168169macro_rules! impl_context_initializer {170	($($gen:ident)*) => {171		#[allow(non_snake_case)]172		impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {173			fn reserve_vars(&self) -> usize {174				let mut out = 0;175				let ($($gen,)*) = self;176				$(out += $gen.reserve_vars();)*177				out178			}179			fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {180				let ($($gen,)*) = self;181				$($gen.populate(for_file.clone(), builder);)*182			}183			fn as_any(&self) -> &dyn Any {184				self185			}186		}187	};188	($($cur:ident)* @ $c:ident $($rest:ident)*) => {189		impl_context_initializer!($($cur)*);190		impl_context_initializer!($($cur)* $c @ $($rest)*);191	};192	($($cur:ident)* @) => {193		impl_context_initializer!($($cur)*);194	}195}196impl_context_initializer! {197	A @ B C D E F G198}199200/// Dynamically reconfigurable evaluation settings201#[derive(Trace)]202pub struct EvaluationSettings {203	/// Context initializer, which will be used for imports and everything204	/// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`205	pub context_initializer: TraceBox<dyn ContextInitializer>,206	/// Used to resolve file locations/contents207	pub import_resolver: TraceBox<dyn ImportResolver>,208}209impl Default for EvaluationSettings {210	fn default() -> Self {211		Self {212			context_initializer: tb!(()),213			import_resolver: tb!(DummyImportResolver),214		}215	}216}217218#[derive(Trace)]219struct FileData {220	string: Option<IStr>,221	bytes: Option<IBytes>,222	parsed: Option<LocExpr>,223	evaluated: Option<Val>,224225	evaluating: bool,226}227impl FileData {228	fn new_string(data: IStr) -> Self {229		Self {230			string: Some(data),231			bytes: None,232			parsed: None,233			evaluated: None,234			evaluating: false,235		}236	}237	fn new_bytes(data: IBytes) -> Self {238		Self {239			string: None,240			bytes: Some(data),241			parsed: None,242			evaluated: None,243			evaluating: false,244		}245	}246	pub(crate) fn get_string(&mut self) -> Option<IStr> {247		if self.string.is_none() {248			self.string = Some(249				self.bytes250					.as_ref()251					.expect("either string or bytes should be set")252					.clone()253					.cast_str()?,254			);255		}256		Some(self.string.clone().expect("just set"))257	}258}259260#[derive(Default, Trace)]261pub struct EvaluationStateInternals {262	/// Internal state263	file_cache: RefCell<GcHashMap<SourcePath, FileData>>,264	/// Settings, safe to change at runtime265	settings: RefCell<EvaluationSettings>,266}267268/// Maintains stack trace and import resolution269#[derive(Default, Clone, Trace)]270pub struct State(Cc<EvaluationStateInternals>);271272impl State {273	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise274	pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {275		let mut file_cache = self.file_cache();276		let mut file = file_cache.raw_entry_mut().from_key(&path);277278		let file = match file {279			RawEntryMut::Occupied(ref mut d) => d.get_mut(),280			RawEntryMut::Vacant(v) => {281				let data = self.settings().import_resolver.load_file_contents(&path)?;282				v.insert(283					path.clone(),284					FileData::new_string(285						std::str::from_utf8(&data)286							.map_err(|_| ImportBadFileUtf8(path.clone()))?287							.into(),288					),289				)290				.1291			}292		};293		Ok(file294			.get_string()295			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?)296	}297	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise298	pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {299		let mut file_cache = self.file_cache();300		let mut file = file_cache.raw_entry_mut().from_key(&path);301302		let file = match file {303			RawEntryMut::Occupied(ref mut d) => d.get_mut(),304			RawEntryMut::Vacant(v) => {305				let data = self.settings().import_resolver.load_file_contents(&path)?;306				v.insert(path.clone(), FileData::new_bytes(data.as_slice().into()))307					.1308			}309		};310		if let Some(str) = &file.bytes {311			return Ok(str.clone());312		}313		if file.bytes.is_none() {314			file.bytes = Some(315				file.string316					.as_ref()317					.expect("either string or bytes should be set")318					.clone()319					.cast_bytes(),320			);321		}322		Ok(file.bytes.as_ref().expect("just set").clone())323	}324	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise325	pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {326		let mut file_cache = self.file_cache();327		let mut file = file_cache.raw_entry_mut().from_key(&path);328329		let file = match file {330			RawEntryMut::Occupied(ref mut d) => d.get_mut(),331			RawEntryMut::Vacant(v) => {332				let data = self.settings().import_resolver.load_file_contents(&path)?;333				v.insert(334					path.clone(),335					FileData::new_string(336						std::str::from_utf8(&data)337							.map_err(|_| ImportBadFileUtf8(path.clone()))?338							.into(),339					),340				)341				.1342			}343		};344		if let Some(val) = &file.evaluated {345			return Ok(val.clone());346		}347		let code = file348			.get_string()349			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?;350		let file_name = Source::new(path.clone(), code.clone());351		if file.parsed.is_none() {352			file.parsed = Some(353				jrsonnet_parser::parse(354					&code,355					&ParserSettings {356						source: file_name.clone(),357					},358				)359				.map_err(|e| ImportSyntaxError {360					path: file_name.clone(),361					error: Box::new(e),362				})?,363			);364		}365		let parsed = file.parsed.as_ref().expect("just set").clone();366		if file.evaluating {367			bail!(InfiniteRecursionDetected)368		}369		file.evaluating = true;370		// Dropping file cache guard here, as evaluation may use this map too371		drop(file_cache);372		let res = evaluate(self.create_default_context(file_name), &parsed);373374		let mut file_cache = self.file_cache();375		let mut file = file_cache.raw_entry_mut().from_key(&path);376377		let RawEntryMut::Occupied(file) = &mut file else {378			unreachable!("this file was just here!")379		};380		let file = file.get_mut();381		file.evaluating = false;382		match res {383			Ok(v) => {384				file.evaluated = Some(v.clone());385				Ok(v)386			}387			Err(e) => Err(e),388		}389	}390391	/// Has same semantics as `import 'path'` called from `from` file392	pub fn import_from(&self, from: &SourcePath, path: &str) -> Result<Val> {393		let resolved = self.resolve_from(from, path)?;394		self.import_resolved(resolved)395	}396	pub fn import(&self, path: impl AsRef<Path>) -> Result<Val> {397		let resolved = self.resolve(path)?;398		self.import_resolved(resolved)399	}400401	/// Creates context with all passed global variables402	pub fn create_default_context(&self, source: Source) -> Context {403		let context_initializer = &self.settings().context_initializer;404		context_initializer.initialize(self.clone(), source)405	}406407	/// Creates context with all passed global variables, calling custom modifier408	pub fn create_default_context_with(409		&self,410		source: Source,411		context_initializer: impl ContextInitializer,412	) -> Context {413		let default_initializer = &self.settings().context_initializer;414		let mut builder = ContextBuilder::with_capacity(415			self.clone(),416			default_initializer.reserve_vars() + context_initializer.reserve_vars(),417		);418		default_initializer.populate(source.clone(), &mut builder);419		context_initializer.populate(source, &mut builder);420421		builder.build()422	}423424	/// Executes code creating a new stack frame425	pub fn push<T>(426		e: CallLocation<'_>,427		frame_desc: impl FnOnce() -> String,428		f: impl FnOnce() -> Result<T>,429	) -> Result<T> {430		let _guard = check_depth()?;431432		f().with_description_src(e, frame_desc)433	}434435	/// Executes code creating a new stack frame436	pub fn push_val(437		&self,438		e: &ExprLocation,439		frame_desc: impl FnOnce() -> String,440		f: impl FnOnce() -> Result<Val>,441	) -> Result<Val> {442		let _guard = check_depth()?;443444		f().with_description_src(e, frame_desc)445	}446	/// Executes code creating a new stack frame447	pub fn push_description<T>(448		frame_desc: impl FnOnce() -> String,449		f: impl FnOnce() -> Result<T>,450	) -> Result<T> {451		let _guard = check_depth()?;452453		f().with_description(frame_desc)454	}455}456457/// Internals458impl State {459	fn file_cache(&self) -> RefMut<'_, GcHashMap<SourcePath, FileData>> {460		self.0.file_cache.borrow_mut()461	}462	pub fn settings(&self) -> Ref<'_, EvaluationSettings> {463		self.0.settings.borrow()464	}465	pub fn settings_mut(&self) -> RefMut<'_, EvaluationSettings> {466		self.0.settings.borrow_mut()467	}468	pub fn add_global(&self, name: IStr, value: Thunk<Val>) {469		#[derive(Trace)]470		struct GlobalsCtx {471			globals: RefCell<GcHashMap<IStr, Thunk<Val>>>,472			inner: TraceBox<dyn ContextInitializer>,473		}474		impl ContextInitializer for GlobalsCtx {475			fn reserve_vars(&self) -> usize {476				self.inner.reserve_vars() + self.globals.borrow().len()477			}478			fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {479				self.inner.populate(for_file, builder);480				for (name, val) in self.globals.borrow().iter() {481					builder.bind(name.clone(), val.clone());482				}483			}484485			fn as_any(&self) -> &dyn Any {486				self487			}488		}489		let mut settings = self.settings_mut();490		let initializer = &mut settings.context_initializer;491		if let Some(global) = initializer.as_any().downcast_ref::<GlobalsCtx>() {492			global.globals.borrow_mut().insert(name, value);493		} else {494			let inner = std::mem::replace(&mut settings.context_initializer, tb!(()));495			settings.context_initializer = tb!(GlobalsCtx {496				globals: {497					let mut out = GcHashMap::with_capacity(1);498					out.insert(name, value);499					RefCell::new(out)500				},501				inner502			});503		}504	}505}506507#[derive(Trace)]508pub struct InitialUnderscore(pub Thunk<Val>);509impl ContextInitializer for InitialUnderscore {510	fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) {511		builder.bind("_", self.0.clone());512	}513514	fn as_any(&self) -> &dyn Any {515		self516	}517}518519/// Raw methods evaluate passed values but don't perform TLA execution520impl State {521	/// Parses and evaluates the given snippet522	pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {523		let code = code.into();524		let source = Source::new_virtual(name.into(), code.clone());525		let parsed = jrsonnet_parser::parse(526			&code,527			&ParserSettings {528				source: source.clone(),529			},530		)531		.map_err(|e| ImportSyntaxError {532			path: source.clone(),533			error: Box::new(e),534		})?;535		evaluate(self.create_default_context(source), &parsed)536	}537	/// Parses and evaluates the given snippet with custom context modifier538	pub fn evaluate_snippet_with(539		&self,540		name: impl Into<IStr>,541		code: impl Into<IStr>,542		context_initializer: impl ContextInitializer,543	) -> Result<Val> {544		let code = code.into();545		let source = Source::new_virtual(name.into(), code.clone());546		let parsed = jrsonnet_parser::parse(547			&code,548			&ParserSettings {549				source: source.clone(),550			},551		)552		.map_err(|e| ImportSyntaxError {553			path: source.clone(),554			error: Box::new(e),555		})?;556		evaluate(557			self.create_default_context_with(source, context_initializer),558			&parsed,559		)560	}561}562563/// Settings utilities564impl State {565	// Only panics in case of [`ImportResolver`] contract violation566	#[allow(clippy::missing_panics_doc)]567	pub fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {568		self.import_resolver().resolve_from(from, path.as_ref())569	}570571	// Only panics in case of [`ImportResolver`] contract violation572	#[allow(clippy::missing_panics_doc)]573	pub fn resolve(&self, path: impl AsRef<Path>) -> Result<SourcePath> {574		self.import_resolver().resolve(path.as_ref())575	}576	pub fn import_resolver(&self) -> Ref<'_, dyn ImportResolver> {577		Ref::map(self.settings(), |s| &*s.import_resolver)578	}579	pub fn set_import_resolver(&self, resolver: impl ImportResolver) {580		self.settings_mut().import_resolver = tb!(resolver);581	}582	pub fn context_initializer(&self) -> Ref<'_, dyn ContextInitializer> {583		Ref::map(self.settings(), |s| &*s.context_initializer)584	}585	pub fn set_context_initializer(&self, initializer: impl ContextInitializer) {586		self.settings_mut().context_initializer = tb!(initializer);587	}588}
after · crates/jrsonnet-evaluator/src/lib.rs
1//! jsonnet interpreter implementation2#![cfg_attr(feature = "nightly", feature(thread_local, type_alias_impl_trait))]34// For jrsonnet-macros5extern crate self as jrsonnet_evaluator;67mod arr;8#[cfg(feature = "async-import")]9pub mod async_import;10mod ctx;11mod dynamic;12pub mod error;13mod evaluate;14pub mod function;15pub mod gc;16mod import;17mod integrations;18pub mod manifest;19mod map;20mod obj;21pub mod stack;22pub mod stdlib;23mod tla;24pub mod trace;25pub mod typed;26pub mod val;2728use std::{29	any::Any,30	cell::{Ref, RefCell, RefMut},31	fmt::{self, Debug},32	path::Path,33};3435pub use ctx::*;36pub use dynamic::*;37pub use error::{Error, ErrorKind::*, Result, ResultExt};38pub use evaluate::*;39use function::CallLocation;40use gc::{GcHashMap, TraceBox};41use hashbrown::hash_map::RawEntryMut;42pub use import::*;43use jrsonnet_gcmodule::{Cc, Trace};44pub use jrsonnet_interner::{IBytes, IStr};45#[doc(hidden)]46pub use jrsonnet_macros;47pub use jrsonnet_parser as parser;48use jrsonnet_parser::*;49pub use obj::*;50use stack::check_depth;51pub use tla::apply_tla;52pub use val::{Thunk, Val};5354/// Thunk without bound `super`/`this`55/// object inheritance may be overriden multiple times, and will be fixed only on field read56pub trait Unbound: Trace {57	/// Type of value after object context is bound58	type Bound;59	/// Create value bound to specified object context60	fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Self::Bound>;61}6263/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code64/// Standard jsonnet fields are always unbound65#[derive(Clone, Trace)]66pub enum MaybeUnbound {67	/// Value needs to be bound to `this`/`super`68	Unbound(Cc<TraceBox<dyn Unbound<Bound = Val>>>),69	/// Value is object-independent70	Bound(Thunk<Val>),71}7273impl Debug for MaybeUnbound {74	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {75		write!(f, "MaybeUnbound")76	}77}78impl MaybeUnbound {79	/// Attach object context to value, if required80	pub fn evaluate(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {81		match self {82			Self::Unbound(v) => v.bind(sup, this),83			Self::Bound(v) => Ok(v.evaluate()?),84		}85	}86}8788/// During import, this trait will be called to create initial context for file.89/// It may initialize global variables, stdlib for example.90pub trait ContextInitializer: Trace {91	/// For which size the builder should be preallocated92	fn reserve_vars(&self) -> usize {93		094	}95	/// Initialize default file context.96	/// Has default implementation, which calls `populate`.97	/// Prefer to always implement `populate` instead.98	fn initialize(&self, state: State, for_file: Source) -> Context {99		let mut builder = ContextBuilder::with_capacity(state, self.reserve_vars());100		self.populate(for_file, &mut builder);101		builder.build()102	}103	/// For composability: extend builder. May panic if this initialization is not supported,104	/// and the context may only be created via `initialize`.105	fn populate(&self, for_file: Source, builder: &mut ContextBuilder);106	/// Allows upcasting from abstract to concrete context initializer.107	/// jrsonnet by itself doesn't use this method, it is allowed for it to panic.108	fn as_any(&self) -> &dyn Any;109}110111/// Context initializer which adds nothing.112impl ContextInitializer for () {113	fn populate(&self, _for_file: Source, _builder: &mut ContextBuilder) {}114	fn as_any(&self) -> &dyn Any {115		self116	}117}118119macro_rules! impl_context_initializer {120	($($gen:ident)*) => {121		#[allow(non_snake_case)]122		impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {123			fn reserve_vars(&self) -> usize {124				let mut out = 0;125				let ($($gen,)*) = self;126				$(out += $gen.reserve_vars();)*127				out128			}129			fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {130				let ($($gen,)*) = self;131				$($gen.populate(for_file.clone(), builder);)*132			}133			fn as_any(&self) -> &dyn Any {134				self135			}136		}137	};138	($($cur:ident)* @ $c:ident $($rest:ident)*) => {139		impl_context_initializer!($($cur)*);140		impl_context_initializer!($($cur)* $c @ $($rest)*);141	};142	($($cur:ident)* @) => {143		impl_context_initializer!($($cur)*);144	}145}146impl_context_initializer! {147	A @ B C D E F G148}149150/// Dynamically reconfigurable evaluation settings151#[derive(Trace)]152pub struct EvaluationSettings {153	/// Context initializer, which will be used for imports and everything154	/// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`155	pub context_initializer: TraceBox<dyn ContextInitializer>,156	/// Used to resolve file locations/contents157	pub import_resolver: TraceBox<dyn ImportResolver>,158}159impl Default for EvaluationSettings {160	fn default() -> Self {161		Self {162			context_initializer: tb!(()),163			import_resolver: tb!(DummyImportResolver),164		}165	}166}167168#[derive(Trace)]169struct FileData {170	string: Option<IStr>,171	bytes: Option<IBytes>,172	parsed: Option<LocExpr>,173	evaluated: Option<Val>,174175	evaluating: bool,176}177impl FileData {178	fn new_string(data: IStr) -> Self {179		Self {180			string: Some(data),181			bytes: None,182			parsed: None,183			evaluated: None,184			evaluating: false,185		}186	}187	fn new_bytes(data: IBytes) -> Self {188		Self {189			string: None,190			bytes: Some(data),191			parsed: None,192			evaluated: None,193			evaluating: false,194		}195	}196	pub(crate) fn get_string(&mut self) -> Option<IStr> {197		if self.string.is_none() {198			self.string = Some(199				self.bytes200					.as_ref()201					.expect("either string or bytes should be set")202					.clone()203					.cast_str()?,204			);205		}206		Some(self.string.clone().expect("just set"))207	}208}209210#[derive(Default, Trace)]211pub struct EvaluationStateInternals {212	/// Internal state213	file_cache: RefCell<GcHashMap<SourcePath, FileData>>,214	/// Settings, safe to change at runtime215	settings: RefCell<EvaluationSettings>,216}217218/// Maintains stack trace and import resolution219#[derive(Default, Clone, Trace)]220pub struct State(Cc<EvaluationStateInternals>);221222impl State {223	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise224	pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {225		let mut file_cache = self.file_cache();226		let mut file = file_cache.raw_entry_mut().from_key(&path);227228		let file = match file {229			RawEntryMut::Occupied(ref mut d) => d.get_mut(),230			RawEntryMut::Vacant(v) => {231				let data = self.settings().import_resolver.load_file_contents(&path)?;232				v.insert(233					path.clone(),234					FileData::new_string(235						std::str::from_utf8(&data)236							.map_err(|_| ImportBadFileUtf8(path.clone()))?237							.into(),238					),239				)240				.1241			}242		};243		Ok(file244			.get_string()245			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?)246	}247	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise248	pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {249		let mut file_cache = self.file_cache();250		let mut file = file_cache.raw_entry_mut().from_key(&path);251252		let file = match file {253			RawEntryMut::Occupied(ref mut d) => d.get_mut(),254			RawEntryMut::Vacant(v) => {255				let data = self.settings().import_resolver.load_file_contents(&path)?;256				v.insert(path.clone(), FileData::new_bytes(data.as_slice().into()))257					.1258			}259		};260		if let Some(str) = &file.bytes {261			return Ok(str.clone());262		}263		if file.bytes.is_none() {264			file.bytes = Some(265				file.string266					.as_ref()267					.expect("either string or bytes should be set")268					.clone()269					.cast_bytes(),270			);271		}272		Ok(file.bytes.as_ref().expect("just set").clone())273	}274	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise275	pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {276		let mut file_cache = self.file_cache();277		let mut file = file_cache.raw_entry_mut().from_key(&path);278279		let file = match file {280			RawEntryMut::Occupied(ref mut d) => d.get_mut(),281			RawEntryMut::Vacant(v) => {282				let data = self.settings().import_resolver.load_file_contents(&path)?;283				v.insert(284					path.clone(),285					FileData::new_string(286						std::str::from_utf8(&data)287							.map_err(|_| ImportBadFileUtf8(path.clone()))?288							.into(),289					),290				)291				.1292			}293		};294		if let Some(val) = &file.evaluated {295			return Ok(val.clone());296		}297		let code = file298			.get_string()299			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?;300		let file_name = Source::new(path.clone(), code.clone());301		if file.parsed.is_none() {302			file.parsed = Some(303				jrsonnet_parser::parse(304					&code,305					&ParserSettings {306						source: file_name.clone(),307					},308				)309				.map_err(|e| ImportSyntaxError {310					path: file_name.clone(),311					error: Box::new(e),312				})?,313			);314		}315		let parsed = file.parsed.as_ref().expect("just set").clone();316		if file.evaluating {317			bail!(InfiniteRecursionDetected)318		}319		file.evaluating = true;320		// Dropping file cache guard here, as evaluation may use this map too321		drop(file_cache);322		let res = evaluate(self.create_default_context(file_name), &parsed);323324		let mut file_cache = self.file_cache();325		let mut file = file_cache.raw_entry_mut().from_key(&path);326327		let RawEntryMut::Occupied(file) = &mut file else {328			unreachable!("this file was just here!")329		};330		let file = file.get_mut();331		file.evaluating = false;332		match res {333			Ok(v) => {334				file.evaluated = Some(v.clone());335				Ok(v)336			}337			Err(e) => Err(e),338		}339	}340341	/// Has same semantics as `import 'path'` called from `from` file342	pub fn import_from(&self, from: &SourcePath, path: &str) -> Result<Val> {343		let resolved = self.resolve_from(from, path)?;344		self.import_resolved(resolved)345	}346	pub fn import(&self, path: impl AsRef<Path>) -> Result<Val> {347		let resolved = self.resolve(path)?;348		self.import_resolved(resolved)349	}350351	/// Creates context with all passed global variables352	pub fn create_default_context(&self, source: Source) -> Context {353		let context_initializer = &self.settings().context_initializer;354		context_initializer.initialize(self.clone(), source)355	}356357	/// Creates context with all passed global variables, calling custom modifier358	pub fn create_default_context_with(359		&self,360		source: Source,361		context_initializer: impl ContextInitializer,362	) -> Context {363		let default_initializer = &self.settings().context_initializer;364		let mut builder = ContextBuilder::with_capacity(365			self.clone(),366			default_initializer.reserve_vars() + context_initializer.reserve_vars(),367		);368		default_initializer.populate(source.clone(), &mut builder);369		context_initializer.populate(source, &mut builder);370371		builder.build()372	}373374	/// Executes code creating a new stack frame375	pub fn push<T>(376		e: CallLocation<'_>,377		frame_desc: impl FnOnce() -> String,378		f: impl FnOnce() -> Result<T>,379	) -> Result<T> {380		let _guard = check_depth()?;381382		f().with_description_src(e, frame_desc)383	}384385	/// Executes code creating a new stack frame386	pub fn push_val(387		&self,388		e: &ExprLocation,389		frame_desc: impl FnOnce() -> String,390		f: impl FnOnce() -> Result<Val>,391	) -> Result<Val> {392		let _guard = check_depth()?;393394		f().with_description_src(e, frame_desc)395	}396	/// Executes code creating a new stack frame397	pub fn push_description<T>(398		frame_desc: impl FnOnce() -> String,399		f: impl FnOnce() -> Result<T>,400	) -> Result<T> {401		let _guard = check_depth()?;402403		f().with_description(frame_desc)404	}405}406407/// Internals408impl State {409	fn file_cache(&self) -> RefMut<'_, GcHashMap<SourcePath, FileData>> {410		self.0.file_cache.borrow_mut()411	}412	pub fn settings(&self) -> Ref<'_, EvaluationSettings> {413		self.0.settings.borrow()414	}415	pub fn settings_mut(&self) -> RefMut<'_, EvaluationSettings> {416		self.0.settings.borrow_mut()417	}418	pub fn add_global(&self, name: IStr, value: Thunk<Val>) {419		#[derive(Trace)]420		struct GlobalsCtx {421			globals: RefCell<GcHashMap<IStr, Thunk<Val>>>,422			inner: TraceBox<dyn ContextInitializer>,423		}424		impl ContextInitializer for GlobalsCtx {425			fn reserve_vars(&self) -> usize {426				self.inner.reserve_vars() + self.globals.borrow().len()427			}428			fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {429				self.inner.populate(for_file, builder);430				for (name, val) in self.globals.borrow().iter() {431					builder.bind(name.clone(), val.clone());432				}433			}434435			fn as_any(&self) -> &dyn Any {436				self437			}438		}439		let mut settings = self.settings_mut();440		let initializer = &mut settings.context_initializer;441		if let Some(global) = initializer.as_any().downcast_ref::<GlobalsCtx>() {442			global.globals.borrow_mut().insert(name, value);443		} else {444			let inner = std::mem::replace(&mut settings.context_initializer, tb!(()));445			settings.context_initializer = tb!(GlobalsCtx {446				globals: {447					let mut out = GcHashMap::with_capacity(1);448					out.insert(name, value);449					RefCell::new(out)450				},451				inner452			});453		}454	}455}456457#[derive(Trace)]458pub struct InitialUnderscore(pub Thunk<Val>);459impl ContextInitializer for InitialUnderscore {460	fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) {461		builder.bind("_", self.0.clone());462	}463464	fn as_any(&self) -> &dyn Any {465		self466	}467}468469/// Raw methods evaluate passed values but don't perform TLA execution470impl State {471	/// Parses and evaluates the given snippet472	pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {473		let code = code.into();474		let source = Source::new_virtual(name.into(), code.clone());475		let parsed = jrsonnet_parser::parse(476			&code,477			&ParserSettings {478				source: source.clone(),479			},480		)481		.map_err(|e| ImportSyntaxError {482			path: source.clone(),483			error: Box::new(e),484		})?;485		evaluate(self.create_default_context(source), &parsed)486	}487	/// Parses and evaluates the given snippet with custom context modifier488	pub fn evaluate_snippet_with(489		&self,490		name: impl Into<IStr>,491		code: impl Into<IStr>,492		context_initializer: impl ContextInitializer,493	) -> Result<Val> {494		let code = code.into();495		let source = Source::new_virtual(name.into(), code.clone());496		let parsed = jrsonnet_parser::parse(497			&code,498			&ParserSettings {499				source: source.clone(),500			},501		)502		.map_err(|e| ImportSyntaxError {503			path: source.clone(),504			error: Box::new(e),505		})?;506		evaluate(507			self.create_default_context_with(source, context_initializer),508			&parsed,509		)510	}511}512513/// Settings utilities514impl State {515	// Only panics in case of [`ImportResolver`] contract violation516	#[allow(clippy::missing_panics_doc)]517	pub fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {518		self.import_resolver().resolve_from(from, path.as_ref())519	}520521	// Only panics in case of [`ImportResolver`] contract violation522	#[allow(clippy::missing_panics_doc)]523	pub fn resolve(&self, path: impl AsRef<Path>) -> Result<SourcePath> {524		self.import_resolver().resolve(path.as_ref())525	}526	pub fn import_resolver(&self) -> Ref<'_, dyn ImportResolver> {527		Ref::map(self.settings(), |s| &*s.import_resolver)528	}529	pub fn set_import_resolver(&self, resolver: impl ImportResolver) {530		self.settings_mut().import_resolver = tb!(resolver);531	}532	pub fn context_initializer(&self) -> Ref<'_, dyn ContextInitializer> {533		Ref::map(self.settings(), |s| &*s.context_initializer)534	}535	pub fn set_context_initializer(&self, initializer: impl ContextInitializer) {536		self.settings_mut().context_initializer = tb!(initializer);537	}538}