git.delta.rocks / jrsonnet / refs/commits / 609e5ef85d49

difftreelog

source

crates/jrsonnet-evaluator/src/lib.rs16.0 KiBsourcehistory
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)]4546// For jrsonnet-macros47extern crate self as jrsonnet_evaluator;4849mod arr;50#[cfg(feature = "async-import")]51pub mod async_import;52mod ctx;53mod dynamic;54pub mod error;55mod evaluate;56pub mod function;57pub mod gc;58mod import;59mod integrations;60pub mod manifest;61mod map;62mod obj;63pub mod stack;64pub mod stdlib;65mod tla;66pub mod trace;67pub mod typed;68pub mod val;6970use std::{71	any::Any,72	cell::{Ref, RefCell, RefMut},73	fmt::{self, Debug},74	path::Path,75};7677pub use ctx::*;78pub use dynamic::*;79pub use error::{Error, ErrorKind::*, Result, ResultExt};80pub use evaluate::*;81use function::CallLocation;82use gc::{GcHashMap, TraceBox};83use hashbrown::hash_map::RawEntryMut;84pub use import::*;85use jrsonnet_gcmodule::{Cc, Trace};86pub use jrsonnet_interner::{IBytes, IStr};87#[doc(hidden)]88pub use jrsonnet_macros;89pub use jrsonnet_parser as parser;90use jrsonnet_parser::*;91pub use obj::*;92use stack::check_depth;93pub use tla::apply_tla;94pub use val::{Thunk, Val};9596/// Thunk without bound `super`/`this`97/// object inheritance may be overriden multiple times, and will be fixed only on field read98pub trait Unbound: Trace {99	/// Type of value after object context is bound100	type Bound;101	/// Create value bound to specified object context102	fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Self::Bound>;103}104105/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code106/// Standard jsonnet fields are always unbound107#[derive(Clone, Trace)]108pub enum MaybeUnbound {109	/// Value needs to be bound to `this`/`super`110	Unbound(Cc<TraceBox<dyn Unbound<Bound = Val>>>),111	/// Value is object-independent112	Bound(Thunk<Val>),113}114115impl Debug for MaybeUnbound {116	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {117		write!(f, "MaybeUnbound")118	}119}120impl MaybeUnbound {121	/// Attach object context to value, if required122	pub fn evaluate(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {123		match self {124			Self::Unbound(v) => v.bind(sup, this),125			Self::Bound(v) => Ok(v.evaluate()?),126		}127	}128}129130/// During import, this trait will be called to create initial context for file.131/// It may initialize global variables, stdlib for example.132pub trait ContextInitializer: Trace {133	/// For which size the builder should be preallocated134	fn reserve_vars(&self) -> usize {135		0136	}137	/// Initialize default file context.138	/// Has default implementation, which calls `populate`.139	/// Prefer to always implement `populate` instead.140	fn initialize(&self, state: State, for_file: Source) -> Context {141		let mut builder = ContextBuilder::with_capacity(state, self.reserve_vars());142		self.populate(for_file, &mut builder);143		builder.build()144	}145	/// For composability: extend builder. May panic if this initialization is not supported,146	/// and the context may only be created via `initialize`.147	fn populate(&self, for_file: Source, builder: &mut ContextBuilder);148	/// Allows upcasting from abstract to concrete context initializer.149	/// jrsonnet by itself doesn't use this method, it is allowed for it to panic.150	fn as_any(&self) -> &dyn Any;151}152153/// Context initializer which adds nothing.154impl ContextInitializer for () {155	fn populate(&self, _for_file: Source, _builder: &mut ContextBuilder) {}156	fn as_any(&self) -> &dyn Any {157		self158	}159}160161macro_rules! impl_context_initializer {162	($($gen:ident)*) => {163		#[allow(non_snake_case)]164		impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {165			fn reserve_vars(&self) -> usize {166				let mut out = 0;167				let ($($gen,)*) = self;168				$(out += $gen.reserve_vars();)*169				out170			}171			fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {172				let ($($gen,)*) = self;173				$($gen.populate(for_file.clone(), builder);)*174			}175			fn as_any(&self) -> &dyn Any {176				self177			}178		}179	};180	($($cur:ident)* @ $c:ident $($rest:ident)*) => {181		impl_context_initializer!($($cur)*);182		impl_context_initializer!($($cur)* $c @ $($rest)*);183	};184	($($cur:ident)* @) => {185		impl_context_initializer!($($cur)*);186	}187}188impl_context_initializer! {189	A @ B C D E F G190}191192/// Dynamically reconfigurable evaluation settings193#[derive(Trace)]194pub struct EvaluationSettings {195	/// Context initializer, which will be used for imports and everything196	/// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`197	pub context_initializer: TraceBox<dyn ContextInitializer>,198	/// Used to resolve file locations/contents199	pub import_resolver: TraceBox<dyn ImportResolver>,200}201impl Default for EvaluationSettings {202	fn default() -> Self {203		Self {204			context_initializer: tb!(()),205			import_resolver: tb!(DummyImportResolver),206		}207	}208}209210#[derive(Trace)]211struct FileData {212	string: Option<IStr>,213	bytes: Option<IBytes>,214	parsed: Option<LocExpr>,215	evaluated: Option<Val>,216217	evaluating: bool,218}219impl FileData {220	fn new_string(data: IStr) -> Self {221		Self {222			string: Some(data),223			bytes: None,224			parsed: None,225			evaluated: None,226			evaluating: false,227		}228	}229	fn new_bytes(data: IBytes) -> Self {230		Self {231			string: None,232			bytes: Some(data),233			parsed: None,234			evaluated: None,235			evaluating: false,236		}237	}238	pub(crate) fn get_string(&mut self) -> Option<IStr> {239		if self.string.is_none() {240			self.string = Some(241				self.bytes242					.as_ref()243					.expect("either string or bytes should be set")244					.clone()245					.cast_str()?,246			);247		}248		Some(self.string.clone().expect("just set"))249	}250}251252#[derive(Default, Trace)]253pub struct EvaluationStateInternals {254	/// Internal state255	file_cache: RefCell<GcHashMap<SourcePath, FileData>>,256	/// Settings, safe to change at runtime257	settings: RefCell<EvaluationSettings>,258}259260/// Maintains stack trace and import resolution261#[derive(Default, Clone, Trace)]262pub struct State(Cc<EvaluationStateInternals>);263264impl State {265	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise266	pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {267		let mut file_cache = self.file_cache();268		let mut file = file_cache.raw_entry_mut().from_key(&path);269270		let file = match file {271			RawEntryMut::Occupied(ref mut d) => d.get_mut(),272			RawEntryMut::Vacant(v) => {273				let data = self.settings().import_resolver.load_file_contents(&path)?;274				v.insert(275					path.clone(),276					FileData::new_string(277						std::str::from_utf8(&data)278							.map_err(|_| ImportBadFileUtf8(path.clone()))?279							.into(),280					),281				)282				.1283			}284		};285		Ok(file286			.get_string()287			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?)288	}289	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise290	pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {291		let mut file_cache = self.file_cache();292		let mut file = file_cache.raw_entry_mut().from_key(&path);293294		let file = match file {295			RawEntryMut::Occupied(ref mut d) => d.get_mut(),296			RawEntryMut::Vacant(v) => {297				let data = self.settings().import_resolver.load_file_contents(&path)?;298				v.insert(path.clone(), FileData::new_bytes(data.as_slice().into()))299					.1300			}301		};302		if let Some(str) = &file.bytes {303			return Ok(str.clone());304		}305		if file.bytes.is_none() {306			file.bytes = Some(307				file.string308					.as_ref()309					.expect("either string or bytes should be set")310					.clone()311					.cast_bytes(),312			);313		}314		Ok(file.bytes.as_ref().expect("just set").clone())315	}316	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise317	pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {318		let mut file_cache = self.file_cache();319		let mut file = file_cache.raw_entry_mut().from_key(&path);320321		let file = match file {322			RawEntryMut::Occupied(ref mut d) => d.get_mut(),323			RawEntryMut::Vacant(v) => {324				let data = self.settings().import_resolver.load_file_contents(&path)?;325				v.insert(326					path.clone(),327					FileData::new_string(328						std::str::from_utf8(&data)329							.map_err(|_| ImportBadFileUtf8(path.clone()))?330							.into(),331					),332				)333				.1334			}335		};336		if let Some(val) = &file.evaluated {337			return Ok(val.clone());338		}339		let code = file340			.get_string()341			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?;342		let file_name = Source::new(path.clone(), code.clone());343		if file.parsed.is_none() {344			file.parsed = Some(345				jrsonnet_parser::parse(346					&code,347					&ParserSettings {348						source: file_name.clone(),349					},350				)351				.map_err(|e| ImportSyntaxError {352					path: file_name.clone(),353					error: Box::new(e),354				})?,355			);356		}357		let parsed = file.parsed.as_ref().expect("just set").clone();358		if file.evaluating {359			bail!(InfiniteRecursionDetected)360		}361		file.evaluating = true;362		// Dropping file cache guard here, as evaluation may use this map too363		drop(file_cache);364		let res = evaluate(self.create_default_context(file_name), &parsed);365366		let mut file_cache = self.file_cache();367		let mut file = file_cache.raw_entry_mut().from_key(&path);368369		let RawEntryMut::Occupied(file) = &mut file else {370			unreachable!("this file was just here!")371		};372		let file = file.get_mut();373		file.evaluating = false;374		match res {375			Ok(v) => {376				file.evaluated = Some(v.clone());377				Ok(v)378			}379			Err(e) => Err(e),380		}381	}382383	/// Has same semantics as `import 'path'` called from `from` file384	pub fn import_from(&self, from: &SourcePath, path: &str) -> Result<Val> {385		let resolved = self.resolve_from(from, path)?;386		self.import_resolved(resolved)387	}388	pub fn import(&self, path: impl AsRef<Path>) -> Result<Val> {389		let resolved = self.resolve(path)?;390		self.import_resolved(resolved)391	}392393	/// Creates context with all passed global variables394	pub fn create_default_context(&self, source: Source) -> Context {395		let context_initializer = &self.settings().context_initializer;396		context_initializer.initialize(self.clone(), source)397	}398399	/// Creates context with all passed global variables, calling custom modifier400	pub fn create_default_context_with(401		&self,402		source: Source,403		context_initializer: impl ContextInitializer,404	) -> Context {405		let default_initializer = &self.settings().context_initializer;406		let mut builder = ContextBuilder::with_capacity(407			self.clone(),408			default_initializer.reserve_vars() + context_initializer.reserve_vars(),409		);410		default_initializer.populate(source.clone(), &mut builder);411		context_initializer.populate(source, &mut builder);412413		builder.build()414	}415416	/// Executes code creating a new stack frame417	pub fn push<T>(418		e: CallLocation<'_>,419		frame_desc: impl FnOnce() -> String,420		f: impl FnOnce() -> Result<T>,421	) -> Result<T> {422		let _guard = check_depth()?;423424		f().with_description_src(e, frame_desc)425	}426427	/// Executes code creating a new stack frame428	pub fn push_val(429		&self,430		e: &ExprLocation,431		frame_desc: impl FnOnce() -> String,432		f: impl FnOnce() -> Result<Val>,433	) -> Result<Val> {434		let _guard = check_depth()?;435436		f().with_description_src(e, frame_desc)437	}438	/// Executes code creating a new stack frame439	pub fn push_description<T>(440		frame_desc: impl FnOnce() -> String,441		f: impl FnOnce() -> Result<T>,442	) -> Result<T> {443		let _guard = check_depth()?;444445		f().with_description(frame_desc)446	}447}448449/// Internals450impl State {451	fn file_cache(&self) -> RefMut<'_, GcHashMap<SourcePath, FileData>> {452		self.0.file_cache.borrow_mut()453	}454	pub fn settings(&self) -> Ref<'_, EvaluationSettings> {455		self.0.settings.borrow()456	}457	pub fn settings_mut(&self) -> RefMut<'_, EvaluationSettings> {458		self.0.settings.borrow_mut()459	}460	pub fn add_global(&self, name: IStr, value: Thunk<Val>) {461		#[derive(Trace)]462		struct GlobalsCtx {463			globals: RefCell<GcHashMap<IStr, Thunk<Val>>>,464			inner: TraceBox<dyn ContextInitializer>,465		}466		impl ContextInitializer for GlobalsCtx {467			fn reserve_vars(&self) -> usize {468				self.inner.reserve_vars() + self.globals.borrow().len()469			}470			fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {471				self.inner.populate(for_file, builder);472				for (name, val) in self.globals.borrow().iter() {473					builder.bind(name.clone(), val.clone());474				}475			}476477			fn as_any(&self) -> &dyn Any {478				self479			}480		}481		let mut settings = self.settings_mut();482		let initializer = &mut settings.context_initializer;483		if let Some(global) = initializer.as_any().downcast_ref::<GlobalsCtx>() {484			global.globals.borrow_mut().insert(name, value);485		} else {486			let inner = std::mem::replace(&mut settings.context_initializer, tb!(()));487			settings.context_initializer = tb!(GlobalsCtx {488				globals: {489					let mut out = GcHashMap::with_capacity(1);490					out.insert(name, value);491					RefCell::new(out)492				},493				inner494			});495		}496	}497}498499#[derive(Trace)]500pub struct InitialUnderscore(pub Thunk<Val>);501impl ContextInitializer for InitialUnderscore {502	fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) {503		builder.bind("_", self.0.clone());504	}505506	fn as_any(&self) -> &dyn Any {507		self508	}509}510511/// Raw methods evaluate passed values but don't perform TLA execution512impl State {513	/// Parses and evaluates the given snippet514	pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {515		let code = code.into();516		let source = Source::new_virtual(name.into(), code.clone());517		let parsed = jrsonnet_parser::parse(518			&code,519			&ParserSettings {520				source: source.clone(),521			},522		)523		.map_err(|e| ImportSyntaxError {524			path: source.clone(),525			error: Box::new(e),526		})?;527		evaluate(self.create_default_context(source), &parsed)528	}529	/// Parses and evaluates the given snippet with custom context modifier530	pub fn evaluate_snippet_with(531		&self,532		name: impl Into<IStr>,533		code: impl Into<IStr>,534		context_initializer: impl ContextInitializer,535	) -> Result<Val> {536		let code = code.into();537		let source = Source::new_virtual(name.into(), code.clone());538		let parsed = jrsonnet_parser::parse(539			&code,540			&ParserSettings {541				source: source.clone(),542			},543		)544		.map_err(|e| ImportSyntaxError {545			path: source.clone(),546			error: Box::new(e),547		})?;548		evaluate(549			self.create_default_context_with(source, context_initializer),550			&parsed,551		)552	}553}554555/// Settings utilities556impl State {557	// Only panics in case of [`ImportResolver`] contract violation558	#[allow(clippy::missing_panics_doc)]559	pub fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {560		self.import_resolver().resolve_from(from, path.as_ref())561	}562563	// Only panics in case of [`ImportResolver`] contract violation564	#[allow(clippy::missing_panics_doc)]565	pub fn resolve(&self, path: impl AsRef<Path>) -> Result<SourcePath> {566		self.import_resolver().resolve(path.as_ref())567	}568	pub fn import_resolver(&self) -> Ref<'_, dyn ImportResolver> {569		Ref::map(self.settings(), |s| &*s.import_resolver)570	}571	pub fn set_import_resolver(&self, resolver: impl ImportResolver) {572		self.settings_mut().import_resolver = tb!(resolver);573	}574	pub fn context_initializer(&self) -> Ref<'_, dyn ContextInitializer> {575		Ref::map(self.settings(), |s| &*s.context_initializer)576	}577	pub fn set_context_initializer(&self, initializer: impl ContextInitializer) {578		self.settings_mut().context_initializer = tb!(initializer);579	}580}