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

difftreelog

source

crates/jrsonnet-evaluator/src/lib.rs17.1 KiBsourcehistory
1//! jsonnet interpreter implementation2#![cfg_attr(nightly, feature(thread_local))]34// For jrsonnet-macros5extern crate self as jrsonnet_evaluator;67mod arr;8pub mod async_import;9mod ctx;10mod dynamic;11pub mod error;12mod evaluate;13pub mod function;14pub mod gc;15mod import;16mod integrations;17pub mod manifest;18mod obj;19pub mod stack;20pub mod stdlib;21pub mod tla;22pub mod trace;23pub mod typed;24pub mod val;2526use std::{27	any::Any,28	cell::{RefCell, RefMut},29	clone::Clone,30	collections::hash_map::Entry,31	fmt::{self, Debug},32	marker::PhantomData,33	rc::Rc,34};3536pub use ctx::*;37pub use dynamic::*;38pub use error::{Error, ErrorKind::*, Result, ResultExt, StackTraceElement};39pub use evaluate::ensure_sufficient_stack;40use function::CallLocation;41pub use import::*;42use jrsonnet_gcmodule::{Cc, Trace, cc_dyn};43pub use jrsonnet_interner::{IBytes, IStr};44use jrsonnet_ir::Expr;45pub use jrsonnet_ir::{46	NumValue, Source, SourceDefaultIgnoreJpath, SourcePath, SourceUrl, SourceVirtual, Span,47};48#[doc(hidden)]49pub use jrsonnet_macros;5051#[cfg(not(any(feature = "ir-parser", feature = "peg-parser")))]52compile_error!("at least one of `ir-parser` or `peg-parser` features must be enabled");5354pub use error::SyntaxError;55pub use obj::*;56pub use rustc_hash;57use rustc_hash::FxHashMap;58use stack::check_depth;59pub use tla::apply_tla;60pub use val::{Thunk, Val};6162pub mod analyze;63use self::analyze::{LExpr, analyze_root};64use crate::gc::WithCapacityExt as _;6566#[allow(clippy::needless_return)]67pub(crate) fn parse_jsonnet(code: &str, source: Source) -> Result<Expr, SyntaxError> {68	#[cfg(feature = "peg-parser")]69	{70		use std::sync::LazyLock;71		static USE_LEGACY_PARSER: LazyLock<bool> =72			LazyLock::new(|| std::env::var_os("JRSONNET_LEGACY_PARSER").is_some());7374		if *USE_LEGACY_PARSER {75			return parse_peg(code, source);76		}77	}78	#[cfg(feature = "ir-parser")]79	{80		return parse_ir(code, source);81	}82	#[cfg(feature = "peg-parser")]83	{84		return parse_peg(code, source);85	}86}8788#[cfg(feature = "ir-parser")]89fn parse_ir(code: &str, source: Source) -> Result<Expr, SyntaxError> {90	jrsonnet_ir_parser::parse(code, &jrsonnet_ir_parser::ParserSettings { source }).map_err(|e| {91		SyntaxError {92			message: e.message,93			location: e.location,94		}95	})96}9798#[cfg(feature = "peg-parser")]99fn parse_peg(code: &str, source: Source) -> Result<Expr, SyntaxError> {100	jrsonnet_peg_parser::parse(code, &jrsonnet_peg_parser::ParserSettings { source }).map_err(|e| {101		let message = e102			.expected103			.tokens()104			.find(|t| t.starts_with("!!!"))105			.map_or_else(106				|| {107					format!(108						"expected {}, got {:?}",109						e.expected,110						code.chars()111							.nth(e.location.0)112							.map_or_else(|| "EOF".into(), |c: char| c.to_string())113					)114				},115				|v| v[3..].into(),116			);117		SyntaxError {118			message,119			location: e.location,120		}121	})122}123124cc_dyn!(125	#[derive(Clone)]126	CcUnbound<V>,127	Unbound<Bound = V>128);129130/// Thunk without bound `super`/`this`131/// object inheritance may be overriden multiple times, and will be fixed only on field read132pub trait Unbound: Trace {133	/// Type of value after object context is bound134	type Bound;135	/// Create value bound to specified object context136	fn bind(&self, sup_this: SupThis) -> Result<Self::Bound>;137}138139/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code140/// Standard jsonnet fields are always unbound141#[derive(Clone, Trace)]142pub enum MaybeUnbound {143	/// Value needs to be bound to `this`/`super`144	Unbound(CcUnbound<Val>),145	/// Value is object-independent146	Bound(Thunk<Val>),147}148149impl Debug for MaybeUnbound {150	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {151		write!(f, "MaybeUnbound")152	}153}154impl MaybeUnbound {155	/// Attach object context to value, if required156	pub fn evaluate(&self, sup_this: SupThis) -> Result<Val> {157		match self {158			Self::Unbound(v) => v.0.bind(sup_this),159			Self::Bound(v) => Ok(v.evaluate()?),160		}161	}162}163164cc_dyn!(CcContextInitializer, ContextInitializer);165166/// During import, this trait will be called to create initial context for file.167/// It may initialize global variables, stdlib for example.168pub trait ContextInitializer {169	/// For composability: extend builder. May panic if this initialization is not supported,170	/// and the context may only be created via `initialize`.171	fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder);172	/// Allows upcasting from abstract to concrete context initializer.173	/// jrsonnet by itself doesn't use this method, it is allowed for it to panic.174	fn as_any(&self) -> &dyn Any;175}176impl<T> ContextInitializer for &T177where178	T: ContextInitializer,179{180	fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder) {181		(*self).populate(for_file, builder);182	}183184	fn as_any(&self) -> &dyn Any {185		(*self).as_any()186	}187}188189/// Context initializer which adds nothing.190impl ContextInitializer for () {191	fn populate(&self, _for_file: Source, _builder: &mut InitialContextBuilder) {}192	fn as_any(&self) -> &dyn Any {193		self194	}195}196197impl<T> ContextInitializer for Option<T>198where199	T: ContextInitializer + 'static,200{201	fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder) {202		if let Some(ctx) = self {203			ctx.populate(for_file, builder);204		}205	}206207	fn as_any(&self) -> &dyn Any {208		self209	}210}211212macro_rules! impl_context_initializer {213	($($gen:ident)*) => {214		#[allow(non_snake_case)]215		impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {216			fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder) {217				let ($($gen,)*) = self;218				$($gen.populate(for_file.clone(), builder);)*219			}220			fn as_any(&self) -> &dyn Any {221				self222			}223		}224	};225	($($cur:ident)* @ $c:ident $($rest:ident)*) => {226		impl_context_initializer!($($cur)*);227		impl_context_initializer!($($cur)* $c @ $($rest)*);228	};229	($($cur:ident)* @) => {230		impl_context_initializer!($($cur)*);231	}232}233impl_context_initializer! {234	A @ B C D E F G235}236237#[derive(Trace)]238struct FileData {239	string: Option<IStr>,240	bytes: Option<IBytes>,241	parsed: Option<Rc<Expr>>,242	evaluated: Option<Val>,243244	evaluating: bool,245}246impl FileData {247	fn new_string(data: IStr) -> Self {248		Self {249			string: Some(data),250			bytes: None,251			parsed: None,252			evaluated: None,253			evaluating: false,254		}255	}256	fn new_bytes(data: IBytes) -> Self {257		Self {258			string: None,259			bytes: Some(data),260			parsed: None,261			evaluated: None,262			evaluating: false,263		}264	}265	pub(crate) fn get_string(&mut self) -> Option<IStr> {266		if self.string.is_none() {267			self.string = Some(268				self.bytes269					.as_ref()270					.expect("either string or bytes should be set")271					.clone()272					.cast_str()?,273			);274		}275		Some(self.string.clone().expect("just set"))276	}277}278279#[derive(Trace)]280pub struct EvaluationStateInternals {281	/// Internal state282	file_cache: RefCell<FxHashMap<SourcePath, FileData>>,283	/// Context initializer, which will be used for imports and everything284	/// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`285	context_initializer: CcContextInitializer,286	/// Used to resolve file locations/contents287	import_resolver: Rc<dyn ImportResolver>,288}289290/// Maintains stack trace and import resolution291#[derive(Clone, Trace)]292pub struct State(Cc<EvaluationStateInternals>);293294thread_local! {295	pub static DEFAULT_STATE: State = State::builder().build();296	pub static STATE: RefCell<Option<State>> = const {RefCell::new(None)};297}298pub struct StateEnterGuard(PhantomData<()>);299impl Drop for StateEnterGuard {300	fn drop(&mut self) {301		STATE.with_borrow_mut(|v| *v = None);302	}303}304305pub fn with_state<V>(v: impl FnOnce(State) -> V) -> V {306	if let Some(state) = STATE.with_borrow(Clone::clone) {307		v(state)308	} else {309		let s = DEFAULT_STATE.with(Clone::clone);310		v(s)311	}312}313314impl State {315	pub fn enter(&self) -> StateEnterGuard {316		self.try_enter().expect("entered state already exists")317	}318	pub fn try_enter(&self) -> Option<StateEnterGuard> {319		STATE.with_borrow_mut(|v| {320			if v.is_none() {321				*v = Some(self.clone());322				Some(StateEnterGuard(PhantomData))323			} else {324				None325			}326		})327	}328	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise329	pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {330		let mut file_cache = self.file_cache();331		let mut file = file_cache.entry(path.clone());332333		let file = match file {334			Entry::Occupied(ref mut d) => d.get_mut(),335			Entry::Vacant(v) => {336				let data = self.import_resolver().load_file_contents(&path)?;337				v.insert(FileData::new_string(338					std::str::from_utf8(&data)339						.map_err(|_| ImportBadFileUtf8(path.clone()))?340						.into(),341				))342			}343		};344		Ok(file345			.get_string()346			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?)347	}348	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise349	pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {350		let mut file_cache = self.file_cache();351		let mut file = file_cache.entry(path.clone());352353		let file = match file {354			Entry::Occupied(ref mut d) => d.get_mut(),355			Entry::Vacant(v) => {356				let data = self.import_resolver().load_file_contents(&path)?;357				v.insert(FileData::new_bytes(data.as_slice().into()))358			}359		};360		if let Some(str) = &file.bytes {361			return Ok(str.clone());362		}363		if file.bytes.is_none() {364			file.bytes = Some(365				file.string366					.as_ref()367					.expect("either string or bytes should be set")368					.clone()369					.cast_bytes(),370			);371		}372		Ok(file.bytes.as_ref().expect("just set").clone())373	}374	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise375	pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {376		let mut file_cache = self.file_cache();377		let mut file = file_cache.entry(path.clone());378379		let file = match file {380			Entry::Occupied(ref mut d) => d.get_mut(),381			Entry::Vacant(v) => {382				let data = self.import_resolver().load_file_contents(&path)?;383				v.insert(FileData::new_string(384					std::str::from_utf8(&data)385						.map_err(|_| ImportBadFileUtf8(path.clone()))?386						.into(),387				))388			}389		};390		if let Some(val) = &file.evaluated {391			return Ok(val.clone());392		}393		let code = file394			.get_string()395			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?;396		let file_name = Source::new(path.clone(), code.clone());397		if file.parsed.is_none() {398			file.parsed = Some(399				parse_jsonnet(&code, file_name.clone())400					.map(Rc::new)401					.map_err(|e| {402						let span = e.location.clone();403						let mut err = Error::from(ImportSyntaxError {404							path: file_name.clone(),405							error: Box::new(e),406						});407						err.trace_mut().0.push(StackTraceElement {408							location: Some(span),409							desc: "parse imported".to_string(),410						});411						err412					})?,413			);414		}415		let parsed = file.parsed.as_ref().expect("just set").clone();416		if file.evaluating {417			bail!(InfiniteRecursionDetected)418		}419		file.evaluating = true;420		// Dropping file cache guard here, as evaluation may use this map too421		drop(file_cache);422		let (externals, thunks) = self.create_default_context(file_name).build();423		let report = analyze_root(&parsed, externals);424		if report.errored {425			return Err(StaticAnalysisError(report.diagnostics_list).into());426		}427		debug_assert_eq!(report.root_shape.n_locals as usize, thunks.len());428		debug_assert!(report.root_shape.captures.is_empty());429		let ctx = Context::root(thunks);430		let res = evaluate::evaluate(ctx, &report.lir);431432		let mut file_cache = self.file_cache();433		let mut file = file_cache.entry(path);434435		let Entry::Occupied(file) = &mut file else {436			unreachable!("this file was just here")437		};438		let file = file.get_mut();439		file.evaluating = false;440		match res {441			Ok(v) => {442				file.evaluated = Some(v.clone());443				Ok(v)444			}445			Err(e) => Err(e),446		}447	}448449	/// Has same semantics as `import 'path'` called from `from` file450	pub fn import_from(&self, from: &SourcePath, path: impl AsPathLike) -> Result<Val> {451		let resolved = self.resolve_from(from, &path)?;452		self.import_resolved(resolved)453	}454	pub fn import(&self, path: impl AsPathLike) -> Result<Val> {455		let resolved = self.resolve_from_default(&path)?;456		self.import_resolved(resolved)457	}458459	/// Creates context with all passed global variables460	pub fn create_default_context(&self, source: Source) -> InitialContextBuilder {461		self.create_default_context_with(source, &())462	}463464	/// Creates context with all passed global variables, calling custom modifier465	pub fn create_default_context_with(466		&self,467		source: Source,468		context_initializer: &dyn ContextInitializer,469	) -> InitialContextBuilder {470		let default_initializer = self.context_initializer();471		let mut builder = InitialContextBuilder::new();472		default_initializer.populate(source.clone(), &mut builder);473		context_initializer.populate(source, &mut builder);474475		builder476	}477}478479/// Internals480impl State {481	fn file_cache(&self) -> RefMut<'_, FxHashMap<SourcePath, FileData>> {482		self.0.file_cache.borrow_mut()483	}484}485/// Executes code creating a new stack frame, to be replaced with try{}486pub fn in_frame<T>(487	e: CallLocation<'_>,488	frame_desc: impl FnOnce() -> String,489	f: impl FnOnce() -> Result<T>,490) -> Result<T> {491	let _guard = check_depth()?;492493	f().with_description_src(e, frame_desc)494}495496/// Executes code creating a new stack frame, to be replaced with try{}497pub fn in_description_frame<T>(498	frame_desc: impl FnOnce() -> String,499	f: impl FnOnce() -> Result<T>,500) -> Result<T> {501	let _guard = check_depth()?;502503	f().with_description(frame_desc)504}505506#[derive(Trace)]507pub struct InitialUnderscore(pub Thunk<Val>);508impl ContextInitializer for InitialUnderscore {509	fn populate(&self, _for_file: Source, builder: &mut InitialContextBuilder) {510		builder.bind("_", self.0.clone());511	}512513	fn as_any(&self) -> &dyn Any {514		self515	}516}517518pub struct PreparedSnippet {519	lir: LExpr,520	thunks: Vec<Thunk<Val>>,521}522523/// Raw methods evaluate passed values but don't perform TLA execution524impl State {525	/// Parses and analyses the given snippet with a custom context526	/// modifier.527	pub fn prepare_snippet_with(528		&self,529		name: impl Into<IStr>,530		code: impl Into<IStr>,531		context_initializer: &dyn ContextInitializer,532	) -> Result<PreparedSnippet> {533		let code = code.into();534		let source = Source::new_virtual(name.into(), code.clone());535		let parsed = parse_jsonnet(&code, source.clone()).map_err(|e| ImportSyntaxError {536			path: source.clone(),537			error: Box::new(e),538		})?;539		let (externals, thunks) = self540			.create_default_context_with(source, context_initializer)541			.build();542		let report = analyze_root(&parsed, externals);543		if report.errored {544			return Err(StaticAnalysisError(report.diagnostics_list).into());545		}546		debug_assert_eq!(report.root_shape.n_locals as usize, thunks.len());547		debug_assert!(report.root_shape.captures.is_empty());548		Ok(PreparedSnippet {549			lir: report.lir,550			thunks,551		})552	}553	/// Parses and analyses the given snippet554	pub fn prepare_snippet(555		&self,556		name: impl Into<IStr>,557		code: impl Into<IStr>,558	) -> Result<PreparedSnippet> {559		self.prepare_snippet_with(name, code, &())560	}561	pub fn evaluate_prepared_snippet(&self, prepared: &PreparedSnippet) -> Result<Val> {562		let ctx = Context::root(prepared.thunks.clone());563		evaluate::evaluate(ctx, &prepared.lir)564	}565	/// Parses and evaluates the given snippet with custom context modifier566	pub fn evaluate_snippet_with(567		&self,568		name: impl Into<IStr>,569		code: impl Into<IStr>,570		context_initializer: &dyn ContextInitializer,571	) -> Result<Val> {572		let prepared = self.prepare_snippet_with(name, code, context_initializer)?;573		self.evaluate_prepared_snippet(&prepared)574	}575	/// Parses and evaluates the given snippet576	pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {577		self.evaluate_snippet_with(name, code, &())578	}579}580581/// Settings utilities582impl State {583	// Only panics in case of [`ImportResolver`] contract violation584	#[allow(clippy::missing_panics_doc)]585	pub fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {586		self.import_resolver().resolve_from(from, path)587	}588	#[allow(clippy::missing_panics_doc)]589	pub fn resolve_from_default(&self, path: &dyn AsPathLike) -> Result<SourcePath> {590		self.import_resolver().resolve_from_default(path)591	}592	pub fn import_resolver(&self) -> &dyn ImportResolver {593		&*self.0.import_resolver594	}595	pub fn context_initializer(&self) -> &dyn ContextInitializer {596		&*self.0.context_initializer.0597	}598}599600impl State {601	pub fn builder() -> StateBuilder {602		StateBuilder::default()603	}604}605606impl Default for State {607	fn default() -> Self {608		Self::builder().build()609	}610}611612#[derive(Default)]613pub struct StateBuilder {614	import_resolver: Option<Rc<dyn ImportResolver>>,615	context_initializer: Option<CcContextInitializer>,616}617impl StateBuilder {618	pub fn import_resolver(&mut self, import_resolver: impl ImportResolver) -> &mut Self {619		let _ = self.import_resolver.insert(Rc::new(import_resolver));620		self621	}622	pub fn context_initializer(623		&mut self,624		context_initializer: impl ContextInitializer + Trace,625	) -> &mut Self {626		let _ = self627			.context_initializer628			.insert(CcContextInitializer::new(context_initializer));629		self630	}631	pub fn build(mut self) -> State {632		State(Cc::new(EvaluationStateInternals {633			file_cache: RefCell::new(FxHashMap::new()),634			context_initializer: self635				.context_initializer636				.take()637				.unwrap_or_else(|| CcContextInitializer::new(())),638			import_resolver: self639				.import_resolver640				.take()641				.unwrap_or_else(|| Rc::new(DummyImportResolver)),642		}))643	}644}