git.delta.rocks / jrsonnet / refs/commits / 64f3674f18c4

difftreelog

source

crates/jrsonnet-evaluator/src/lib.rs12.2 KiBsourcehistory
1//! jsonnet interpreter implementation2#![cfg_attr(feature = "nightly", feature(thread_local))]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	// ci is being run with nightly, but library should work on stable40	clippy::missing_const_for_fn,41)]4243// For jrsonnet-macros44extern crate self as jrsonnet_evaluator;4546mod ctx;47mod dynamic;48pub mod error;49mod evaluate;50pub mod function;51pub mod gc;52mod import;53mod integrations;54pub mod manifest;55mod map;56mod obj;57pub mod stack;58pub mod stdlib;59mod tla;60pub mod trace;61pub mod typed;62pub mod val;6364use std::{65	any::Any,66	cell::{Ref, RefCell, RefMut},67	fmt::{self, Debug},68	path::Path,69};7071pub use ctx::*;72pub use dynamic::*;73pub use error::{Error, ErrorKind::*, Result, ResultExt};74pub use evaluate::*;75use function::CallLocation;76use gc::{GcHashMap, TraceBox};77use hashbrown::hash_map::RawEntryMut;78pub use import::*;79use jrsonnet_gcmodule::{Cc, Trace};80pub use jrsonnet_interner::{IBytes, IStr};81pub use jrsonnet_parser as parser;82use jrsonnet_parser::*;83pub use obj::*;84use stack::check_depth;85pub use tla::apply_tla;86pub use val::{Thunk, Val};8788/// Thunk without bound `super`/`this`89/// object inheritance may be overriden multiple times, and will be fixed only on field read90pub trait Unbound: Trace {91	/// Type of value after object context is bound92	type Bound;93	/// Create value bound to specified object context94	fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Self::Bound>;95}9697/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code98/// Standard jsonnet fields are always unbound99#[derive(Clone, Trace)]100pub enum MaybeUnbound {101	/// Value needs to be bound to `this`/`super`102	Unbound(Cc<TraceBox<dyn Unbound<Bound = Val>>>),103	/// Value is object-independent104	Bound(Thunk<Val>),105}106107impl Debug for MaybeUnbound {108	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {109		write!(f, "MaybeUnbound")110	}111}112impl MaybeUnbound {113	/// Attach object context to value, if required114	pub fn evaluate(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {115		match self {116			Self::Unbound(v) => v.bind(sup, this),117			Self::Bound(v) => Ok(v.evaluate()?),118		}119	}120}121122/// During import, this trait will be called to create initial context for file.123/// It may initialize global variables, stdlib for example.124pub trait ContextInitializer: Trace {125	/// Initialize default file context.126	fn initialize(&self, state: State, for_file: Source) -> Context;127	/// Allows upcasting from abstract to concrete context initializer.128	/// jrsonnet by itself doesn't use this method, it is allowed for it to panic.129	fn as_any(&self) -> &dyn Any;130}131132/// Context initializer which adds nothing.133#[derive(Trace)]134pub struct DummyContextInitializer;135impl ContextInitializer for DummyContextInitializer {136	fn initialize(&self, state: State, _for_file: Source) -> Context {137		ContextBuilder::new(state).build()138	}139	fn as_any(&self) -> &dyn Any {140		self141	}142}143144/// Dynamically reconfigurable evaluation settings145#[derive(Trace)]146pub struct EvaluationSettings {147	/// Context initializer, which will be used for imports and everything148	/// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`149	pub context_initializer: TraceBox<dyn ContextInitializer>,150	/// Used to resolve file locations/contents151	pub import_resolver: TraceBox<dyn ImportResolver>,152}153impl Default for EvaluationSettings {154	fn default() -> Self {155		Self {156			context_initializer: tb!(DummyContextInitializer),157			import_resolver: tb!(DummyImportResolver),158		}159	}160}161162#[derive(Trace)]163struct FileData {164	string: Option<IStr>,165	bytes: Option<IBytes>,166	parsed: Option<LocExpr>,167	evaluated: Option<Val>,168169	evaluating: bool,170}171impl FileData {172	fn new_string(data: IStr) -> Self {173		Self {174			string: Some(data),175			bytes: None,176			parsed: None,177			evaluated: None,178			evaluating: false,179		}180	}181	fn new_bytes(data: IBytes) -> Self {182		Self {183			string: None,184			bytes: Some(data),185			parsed: None,186			evaluated: None,187			evaluating: false,188		}189	}190}191192#[derive(Default, Trace)]193pub struct EvaluationStateInternals {194	/// Internal state195	file_cache: RefCell<GcHashMap<SourcePath, FileData>>,196	/// Settings, safe to change at runtime197	settings: RefCell<EvaluationSettings>,198}199200/// Maintains stack trace and import resolution201#[derive(Default, Clone, Trace)]202pub struct State(Cc<EvaluationStateInternals>);203204impl State {205	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise206	pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {207		let mut file_cache = self.file_cache();208		let mut file = file_cache.raw_entry_mut().from_key(&path);209210		let file = match file {211			RawEntryMut::Occupied(ref mut d) => d.get_mut(),212			RawEntryMut::Vacant(v) => {213				let data = self.settings().import_resolver.load_file_contents(&path)?;214				v.insert(215					path.clone(),216					FileData::new_string(217						std::str::from_utf8(&data)218							.map_err(|_| ImportBadFileUtf8(path.clone()))?219							.into(),220					),221				)222				.1223			}224		};225		if let Some(str) = &file.string {226			return Ok(str.clone());227		}228		if file.string.is_none() {229			file.string = Some(230				file.bytes231					.as_ref()232					.expect("either string or bytes should be set")233					.clone()234					.cast_str()235					.ok_or_else(|| ImportBadFileUtf8(path.clone()))?,236			);237		}238		Ok(file.string.as_ref().expect("just set").clone())239	}240	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise241	pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {242		let mut file_cache = self.file_cache();243		let mut file = file_cache.raw_entry_mut().from_key(&path);244245		let file = match file {246			RawEntryMut::Occupied(ref mut d) => d.get_mut(),247			RawEntryMut::Vacant(v) => {248				let data = self.settings().import_resolver.load_file_contents(&path)?;249				v.insert(path.clone(), FileData::new_bytes(data.as_slice().into()))250					.1251			}252		};253		if let Some(str) = &file.bytes {254			return Ok(str.clone());255		}256		if file.bytes.is_none() {257			file.bytes = Some(258				file.string259					.as_ref()260					.expect("either string or bytes should be set")261					.clone()262					.cast_bytes(),263			);264		}265		Ok(file.bytes.as_ref().expect("just set").clone())266	}267	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise268	pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {269		let mut file_cache = self.file_cache();270		let mut file = file_cache.raw_entry_mut().from_key(&path);271272		let file = match file {273			RawEntryMut::Occupied(ref mut d) => d.get_mut(),274			RawEntryMut::Vacant(v) => {275				let data = self.settings().import_resolver.load_file_contents(&path)?;276				v.insert(277					path.clone(),278					FileData::new_string(279						std::str::from_utf8(&data)280							.map_err(|_| ImportBadFileUtf8(path.clone()))?281							.into(),282					),283				)284				.1285			}286		};287		if let Some(val) = &file.evaluated {288			return Ok(val.clone());289		}290		if file.string.is_none() {291			file.string = Some(292				std::str::from_utf8(293					file.bytes294						.as_ref()295						.expect("either string or bytes should be set"),296				)297				.map_err(|_| ImportBadFileUtf8(path.clone()))?298				.into(),299			);300		}301		let code = file.string.as_ref().expect("just set");302		let file_name = Source::new(path.clone(), code.clone());303		if file.parsed.is_none() {304			file.parsed = Some(305				jrsonnet_parser::parse(306					code,307					&ParserSettings {308						source: file_name.clone(),309					},310				)311				.map_err(|e| ImportSyntaxError {312					path: file_name.clone(),313					error: Box::new(e),314				})?,315			);316		}317		let parsed = file.parsed.as_ref().expect("just set").clone();318		if file.evaluating {319			throw!(InfiniteRecursionDetected)320		}321		file.evaluating = true;322		// Dropping file cache guard here, as evaluation may use this map too323		drop(file_cache);324		let res = evaluate(self.create_default_context(file_name), &parsed);325326		let mut file_cache = self.file_cache();327		let mut file = file_cache.raw_entry_mut().from_key(&path);328329		let RawEntryMut::Occupied(file) = &mut file else {330			unreachable!("this file was just here!")331		};332		let file = file.get_mut();333		file.evaluating = false;334		match res {335			Ok(v) => {336				file.evaluated = Some(v.clone());337				Ok(v)338			}339			Err(e) => Err(e),340		}341	}342343	/// Has same semantics as `import 'path'` called from `from` file344	pub fn import_from(&self, from: &SourcePath, path: &str) -> Result<Val> {345		let resolved = self.resolve_from(from, path)?;346		self.import_resolved(resolved)347	}348	pub fn import(&self, path: impl AsRef<Path>) -> Result<Val> {349		let resolved = self.resolve(path)?;350		self.import_resolved(resolved)351	}352353	/// Creates context with all passed global variables354	pub fn create_default_context(&self, source: Source) -> Context {355		let context_initializer = &self.settings().context_initializer;356		context_initializer.initialize(self.clone(), source)357	}358359	/// Executes code creating a new stack frame360	pub fn push<T>(361		e: CallLocation<'_>,362		frame_desc: impl FnOnce() -> String,363		f: impl FnOnce() -> Result<T>,364	) -> Result<T> {365		let _guard = check_depth()?;366367		f().with_description_src(e, frame_desc)368	}369370	/// Executes code creating a new stack frame371	pub fn push_val(372		&self,373		e: &ExprLocation,374		frame_desc: impl FnOnce() -> String,375		f: impl FnOnce() -> Result<Val>,376	) -> Result<Val> {377		let _guard = check_depth()?;378379		f().with_description_src(e, frame_desc)380	}381	/// Executes code creating a new stack frame382	pub fn push_description<T>(383		frame_desc: impl FnOnce() -> String,384		f: impl FnOnce() -> Result<T>,385	) -> Result<T> {386		let _guard = check_depth()?;387388		f().with_description(frame_desc)389	}390}391392/// Internals393impl State {394	fn file_cache(&self) -> RefMut<'_, GcHashMap<SourcePath, FileData>> {395		self.0.file_cache.borrow_mut()396	}397	pub fn settings(&self) -> Ref<'_, EvaluationSettings> {398		self.0.settings.borrow()399	}400	pub fn settings_mut(&self) -> RefMut<'_, EvaluationSettings> {401		self.0.settings.borrow_mut()402	}403}404405/// Raw methods evaluate passed values but don't perform TLA execution406impl State {407	/// Parses and evaluates the given snippet408	pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {409		let code = code.into();410		let source = Source::new_virtual(name.into(), code.clone());411		let parsed = jrsonnet_parser::parse(412			&code,413			&ParserSettings {414				source: source.clone(),415			},416		)417		.map_err(|e| ImportSyntaxError {418			path: source.clone(),419			error: Box::new(e),420		})?;421		evaluate(self.create_default_context(source), &parsed)422	}423}424425/// Settings utilities426impl State {427	// Only panics in case of [`ImportResolver`] contract violation428	#[allow(clippy::missing_panics_doc)]429	pub fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {430		self.import_resolver().resolve_from(from, path.as_ref())431	}432433	// Only panics in case of [`ImportResolver`] contract violation434	#[allow(clippy::missing_panics_doc)]435	pub fn resolve(&self, path: impl AsRef<Path>) -> Result<SourcePath> {436		self.import_resolver().resolve(path.as_ref())437	}438	pub fn import_resolver(&self) -> Ref<'_, dyn ImportResolver> {439		Ref::map(self.settings(), |s| &*s.import_resolver)440	}441	pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {442		self.settings_mut().import_resolver = TraceBox(resolver);443	}444	pub fn context_initializer(&self) -> Ref<'_, dyn ContextInitializer> {445		Ref::map(self.settings(), |s| &*s.context_initializer)446	}447}