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

difftreelog

source

crates/jrsonnet-evaluator/src/lib.rs16.2 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    // 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)]4950// For jrsonnet-macros51extern crate self as jrsonnet_evaluator;5253mod arr;54#[cfg(feature = "async-import")]55pub mod async_import;56mod ctx;57mod dynamic;58pub mod error;59mod evaluate;60pub mod function;61pub mod gc;62mod import;63mod integrations;64pub mod manifest;65mod map;66mod obj;67pub mod stack;68pub mod stdlib;69mod tla;70pub mod trace;71pub mod typed;72pub mod val;7374use std::{75	any::Any,76	cell::{Ref, RefCell, RefMut},77	fmt::{self, Debug},78	path::Path,79};8081pub use ctx::*;82pub use dynamic::*;83pub use error::{Error, ErrorKind::*, Result, ResultExt};84pub use evaluate::*;85use function::CallLocation;86use gc::{GcHashMap, TraceBox};87use hashbrown::hash_map::RawEntryMut;88pub use import::*;89use jrsonnet_gcmodule::{Cc, Trace};90pub use jrsonnet_interner::{IBytes, IStr};91#[doc(hidden)]92pub use jrsonnet_macros;93pub use jrsonnet_parser as parser;94use jrsonnet_parser::*;95pub use obj::*;96use stack::check_depth;97pub use tla::apply_tla;98pub use val::{Thunk, Val};99100/// Thunk without bound `super`/`this`101/// object inheritance may be overriden multiple times, and will be fixed only on field read102pub trait Unbound: Trace {103	/// Type of value after object context is bound104	type Bound;105	/// Create value bound to specified object context106	fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Self::Bound>;107}108109/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code110/// Standard jsonnet fields are always unbound111#[derive(Clone, Trace)]112pub enum MaybeUnbound {113	/// Value needs to be bound to `this`/`super`114	Unbound(Cc<TraceBox<dyn Unbound<Bound = Val>>>),115	/// Value is object-independent116	Bound(Thunk<Val>),117}118119impl Debug for MaybeUnbound {120	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {121		write!(f, "MaybeUnbound")122	}123}124impl MaybeUnbound {125	/// Attach object context to value, if required126	pub fn evaluate(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {127		match self {128			Self::Unbound(v) => v.bind(sup, this),129			Self::Bound(v) => Ok(v.evaluate()?),130		}131	}132}133134/// During import, this trait will be called to create initial context for file.135/// It may initialize global variables, stdlib for example.136pub trait ContextInitializer: Trace {137	/// For which size the builder should be preallocated138	fn reserve_vars(&self) -> usize {139		0140	}141	/// Initialize default file context.142	/// Has default implementation, which calls `populate`.143	/// Prefer to always implement `populate` instead.144	fn initialize(&self, state: State, for_file: Source) -> Context {145		let mut builder = ContextBuilder::with_capacity(state, self.reserve_vars());146		self.populate(for_file, &mut builder);147		builder.build()148	}149	/// For composability: extend builder. May panic if this initialization is not supported,150	/// and the context may only be created via `initialize`.151	fn populate(&self, for_file: Source, builder: &mut ContextBuilder);152	/// Allows upcasting from abstract to concrete context initializer.153	/// jrsonnet by itself doesn't use this method, it is allowed for it to panic.154	fn as_any(&self) -> &dyn Any;155}156157/// Context initializer which adds nothing.158impl ContextInitializer for () {159	fn populate(&self, _for_file: Source, _builder: &mut ContextBuilder) {}160	fn as_any(&self) -> &dyn Any {161		self162	}163}164165macro_rules! impl_context_initializer {166	($($gen:ident)*) => {167		#[allow(non_snake_case)]168		impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {169			fn reserve_vars(&self) -> usize {170				let mut out = 0;171				let ($($gen,)*) = self;172				$(out += $gen.reserve_vars();)*173				out174			}175			fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {176				let ($($gen,)*) = self;177				$($gen.populate(for_file.clone(), builder);)*178			}179			fn as_any(&self) -> &dyn Any {180				self181			}182		}183	};184	($($cur:ident)* @ $c:ident $($rest:ident)*) => {185		impl_context_initializer!($($cur)*);186		impl_context_initializer!($($cur)* $c @ $($rest)*);187	};188	($($cur:ident)* @) => {189		impl_context_initializer!($($cur)*);190	}191}192impl_context_initializer! {193	A @ B C D E F G194}195196/// Dynamically reconfigurable evaluation settings197#[derive(Trace)]198pub struct EvaluationSettings {199	/// Context initializer, which will be used for imports and everything200	/// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`201	pub context_initializer: TraceBox<dyn ContextInitializer>,202	/// Used to resolve file locations/contents203	pub import_resolver: TraceBox<dyn ImportResolver>,204}205impl Default for EvaluationSettings {206	fn default() -> Self {207		Self {208			context_initializer: tb!(()),209			import_resolver: tb!(DummyImportResolver),210		}211	}212}213214#[derive(Trace)]215struct FileData {216	string: Option<IStr>,217	bytes: Option<IBytes>,218	parsed: Option<LocExpr>,219	evaluated: Option<Val>,220221	evaluating: bool,222}223impl FileData {224	fn new_string(data: IStr) -> Self {225		Self {226			string: Some(data),227			bytes: None,228			parsed: None,229			evaluated: None,230			evaluating: false,231		}232	}233	fn new_bytes(data: IBytes) -> Self {234		Self {235			string: None,236			bytes: Some(data),237			parsed: None,238			evaluated: None,239			evaluating: false,240		}241	}242	pub(crate) fn get_string(&mut self) -> Option<IStr> {243		if self.string.is_none() {244			self.string = Some(245				self.bytes246					.as_ref()247					.expect("either string or bytes should be set")248					.clone()249					.cast_str()?,250			);251		}252		Some(self.string.clone().expect("just set"))253	}254}255256#[derive(Default, Trace)]257pub struct EvaluationStateInternals {258	/// Internal state259	file_cache: RefCell<GcHashMap<SourcePath, FileData>>,260	/// Settings, safe to change at runtime261	settings: RefCell<EvaluationSettings>,262}263264/// Maintains stack trace and import resolution265#[derive(Default, Clone, Trace)]266pub struct State(Cc<EvaluationStateInternals>);267268impl State {269	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise270	pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {271		let mut file_cache = self.file_cache();272		let mut file = file_cache.raw_entry_mut().from_key(&path);273274		let file = match file {275			RawEntryMut::Occupied(ref mut d) => d.get_mut(),276			RawEntryMut::Vacant(v) => {277				let data = self.settings().import_resolver.load_file_contents(&path)?;278				v.insert(279					path.clone(),280					FileData::new_string(281						std::str::from_utf8(&data)282							.map_err(|_| ImportBadFileUtf8(path.clone()))?283							.into(),284					),285				)286				.1287			}288		};289		Ok(file290			.get_string()291			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?)292	}293	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise294	pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {295		let mut file_cache = self.file_cache();296		let mut file = file_cache.raw_entry_mut().from_key(&path);297298		let file = match file {299			RawEntryMut::Occupied(ref mut d) => d.get_mut(),300			RawEntryMut::Vacant(v) => {301				let data = self.settings().import_resolver.load_file_contents(&path)?;302				v.insert(path.clone(), FileData::new_bytes(data.as_slice().into()))303					.1304			}305		};306		if let Some(str) = &file.bytes {307			return Ok(str.clone());308		}309		if file.bytes.is_none() {310			file.bytes = Some(311				file.string312					.as_ref()313					.expect("either string or bytes should be set")314					.clone()315					.cast_bytes(),316			);317		}318		Ok(file.bytes.as_ref().expect("just set").clone())319	}320	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise321	pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {322		let mut file_cache = self.file_cache();323		let mut file = file_cache.raw_entry_mut().from_key(&path);324325		let file = match file {326			RawEntryMut::Occupied(ref mut d) => d.get_mut(),327			RawEntryMut::Vacant(v) => {328				let data = self.settings().import_resolver.load_file_contents(&path)?;329				v.insert(330					path.clone(),331					FileData::new_string(332						std::str::from_utf8(&data)333							.map_err(|_| ImportBadFileUtf8(path.clone()))?334							.into(),335					),336				)337				.1338			}339		};340		if let Some(val) = &file.evaluated {341			return Ok(val.clone());342		}343		let code = file344			.get_string()345			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?;346		let file_name = Source::new(path.clone(), code.clone());347		if file.parsed.is_none() {348			file.parsed = Some(349				jrsonnet_parser::parse(350					&code,351					&ParserSettings {352						source: file_name.clone(),353					},354				)355				.map_err(|e| ImportSyntaxError {356					path: file_name.clone(),357					error: Box::new(e),358				})?,359			);360		}361		let parsed = file.parsed.as_ref().expect("just set").clone();362		if file.evaluating {363			bail!(InfiniteRecursionDetected)364		}365		file.evaluating = true;366		// Dropping file cache guard here, as evaluation may use this map too367		drop(file_cache);368		let res = evaluate(self.create_default_context(file_name), &parsed);369370		let mut file_cache = self.file_cache();371		let mut file = file_cache.raw_entry_mut().from_key(&path);372373		let RawEntryMut::Occupied(file) = &mut file else {374			unreachable!("this file was just here!")375		};376		let file = file.get_mut();377		file.evaluating = false;378		match res {379			Ok(v) => {380				file.evaluated = Some(v.clone());381				Ok(v)382			}383			Err(e) => Err(e),384		}385	}386387	/// Has same semantics as `import 'path'` called from `from` file388	pub fn import_from(&self, from: &SourcePath, path: &str) -> Result<Val> {389		let resolved = self.resolve_from(from, path)?;390		self.import_resolved(resolved)391	}392	pub fn import(&self, path: impl AsRef<Path>) -> Result<Val> {393		let resolved = self.resolve(path)?;394		self.import_resolved(resolved)395	}396397	/// Creates context with all passed global variables398	pub fn create_default_context(&self, source: Source) -> Context {399		let context_initializer = &self.settings().context_initializer;400		context_initializer.initialize(self.clone(), source)401	}402403	/// Creates context with all passed global variables, calling custom modifier404	pub fn create_default_context_with(405		&self,406		source: Source,407		context_initializer: impl ContextInitializer,408	) -> Context {409		let default_initializer = &self.settings().context_initializer;410		let mut builder = ContextBuilder::with_capacity(411			self.clone(),412			default_initializer.reserve_vars() + context_initializer.reserve_vars(),413		);414		default_initializer.populate(source.clone(), &mut builder);415		context_initializer.populate(source, &mut builder);416417		builder.build()418	}419420	/// Executes code creating a new stack frame421	pub fn push<T>(422		e: CallLocation<'_>,423		frame_desc: impl FnOnce() -> String,424		f: impl FnOnce() -> Result<T>,425	) -> Result<T> {426		let _guard = check_depth()?;427428		f().with_description_src(e, frame_desc)429	}430431	/// Executes code creating a new stack frame432	pub fn push_val(433		&self,434		e: &ExprLocation,435		frame_desc: impl FnOnce() -> String,436		f: impl FnOnce() -> Result<Val>,437	) -> Result<Val> {438		let _guard = check_depth()?;439440		f().with_description_src(e, frame_desc)441	}442	/// Executes code creating a new stack frame443	pub fn push_description<T>(444		frame_desc: impl FnOnce() -> String,445		f: impl FnOnce() -> Result<T>,446	) -> Result<T> {447		let _guard = check_depth()?;448449		f().with_description(frame_desc)450	}451}452453/// Internals454impl State {455	fn file_cache(&self) -> RefMut<'_, GcHashMap<SourcePath, FileData>> {456		self.0.file_cache.borrow_mut()457	}458	pub fn settings(&self) -> Ref<'_, EvaluationSettings> {459		self.0.settings.borrow()460	}461	pub fn settings_mut(&self) -> RefMut<'_, EvaluationSettings> {462		self.0.settings.borrow_mut()463	}464	pub fn add_global(&self, name: IStr, value: Thunk<Val>) {465		#[derive(Trace)]466		struct GlobalsCtx {467			globals: RefCell<GcHashMap<IStr, Thunk<Val>>>,468			inner: TraceBox<dyn ContextInitializer>,469		}470		impl ContextInitializer for GlobalsCtx {471			fn reserve_vars(&self) -> usize {472				self.inner.reserve_vars() + self.globals.borrow().len()473			}474			fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {475				self.inner.populate(for_file, builder);476				for (name, val) in self.globals.borrow().iter() {477					builder.bind(name.clone(), val.clone());478				}479			}480481			fn as_any(&self) -> &dyn Any {482				self483			}484		}485		let mut settings = self.settings_mut();486		let initializer = &mut settings.context_initializer;487		if let Some(global) = initializer.as_any().downcast_ref::<GlobalsCtx>() {488			global.globals.borrow_mut().insert(name, value);489		} else {490			let inner = std::mem::replace(&mut settings.context_initializer, tb!(()));491			settings.context_initializer = tb!(GlobalsCtx {492				globals: {493					let mut out = GcHashMap::with_capacity(1);494					out.insert(name, value);495					RefCell::new(out)496				},497				inner498			});499		}500	}501}502503#[derive(Trace)]504pub struct InitialUnderscore(pub Thunk<Val>);505impl ContextInitializer for InitialUnderscore {506	fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) {507		builder.bind("_", self.0.clone());508	}509510	fn as_any(&self) -> &dyn Any {511		self512	}513}514515/// Raw methods evaluate passed values but don't perform TLA execution516impl State {517	/// Parses and evaluates the given snippet518	pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {519		let code = code.into();520		let source = Source::new_virtual(name.into(), code.clone());521		let parsed = jrsonnet_parser::parse(522			&code,523			&ParserSettings {524				source: source.clone(),525			},526		)527		.map_err(|e| ImportSyntaxError {528			path: source.clone(),529			error: Box::new(e),530		})?;531		evaluate(self.create_default_context(source), &parsed)532	}533	/// Parses and evaluates the given snippet with custom context modifier534	pub fn evaluate_snippet_with(535		&self,536		name: impl Into<IStr>,537		code: impl Into<IStr>,538		context_initializer: impl ContextInitializer,539	) -> Result<Val> {540		let code = code.into();541		let source = Source::new_virtual(name.into(), code.clone());542		let parsed = jrsonnet_parser::parse(543			&code,544			&ParserSettings {545				source: source.clone(),546			},547		)548		.map_err(|e| ImportSyntaxError {549			path: source.clone(),550			error: Box::new(e),551		})?;552		evaluate(553			self.create_default_context_with(source, context_initializer),554			&parsed,555		)556	}557}558559/// Settings utilities560impl State {561	// Only panics in case of [`ImportResolver`] contract violation562	#[allow(clippy::missing_panics_doc)]563	pub fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {564		self.import_resolver().resolve_from(from, path.as_ref())565	}566567	// Only panics in case of [`ImportResolver`] contract violation568	#[allow(clippy::missing_panics_doc)]569	pub fn resolve(&self, path: impl AsRef<Path>) -> Result<SourcePath> {570		self.import_resolver().resolve(path.as_ref())571	}572	pub fn import_resolver(&self) -> Ref<'_, dyn ImportResolver> {573		Ref::map(self.settings(), |s| &*s.import_resolver)574	}575	pub fn set_import_resolver(&self, resolver: impl ImportResolver) {576		self.settings_mut().import_resolver = tb!(resolver);577	}578	pub fn context_initializer(&self) -> Ref<'_, dyn ContextInitializer> {579		Ref::map(self.settings(), |s| &*s.context_initializer)580	}581	pub fn set_context_initializer(&self, initializer: impl ContextInitializer) {582		self.settings_mut().context_initializer = tb!(initializer);583	}584}