git.delta.rocks / jrsonnet / refs/commits / 449686f01d55

difftreelog

source

crates/jrsonnet-evaluator/src/lib.rs15.2 KiBsourcehistory
1//! jsonnet interpreter implementation2#![cfg_attr(nightly, feature(thread_local, type_alias_impl_trait))]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};39pub use evaluate::*;40use function::CallLocation;41pub use import::*;42use jrsonnet_gcmodule::{Cc, Trace, cc_dyn};43pub use jrsonnet_interner::{IBytes, IStr};44pub use jrsonnet_ir as parser;45use jrsonnet_ir::{Expr, Source, SourcePath};46#[doc(hidden)]47pub use jrsonnet_macros;4849#[cfg(not(any(feature = "ir-parser", feature = "peg-parser")))]50compile_error!("at least one of `ir-parser` or `peg-parser` features must be enabled");5152pub use error::SyntaxError;53pub use obj::*;54pub use rustc_hash;55use rustc_hash::FxHashMap;56use stack::check_depth;57pub use tla::apply_tla;58pub use val::{Thunk, Val};5960use crate::gc::WithCapacityExt as _;6162#[allow(clippy::needless_return)]63pub(crate) fn parse_jsonnet(code: &str, source: Source) -> Result<Expr, SyntaxError> {64	#[cfg(feature = "peg-parser")]65	{66		use std::sync::LazyLock;67		static USE_LEGACY_PARSER: LazyLock<bool> =68			LazyLock::new(|| std::env::var_os("JRSONNET_LEGACY_PARSER").is_some());6970		if *USE_LEGACY_PARSER {71			return parse_peg(code, source);72		}73	}74	#[cfg(feature = "ir-parser")]75	{76		return parse_ir(code, source);77	}78	#[cfg(feature = "peg-parser")]79	{80		return parse_peg(code, source);81	}82}8384#[cfg(feature = "ir-parser")]85fn parse_ir(code: &str, source: Source) -> Result<Expr, SyntaxError> {86	jrsonnet_ir_parser::parse(code, &jrsonnet_ir_parser::ParserSettings { source }).map_err(|e| {87		SyntaxError {88			message: e.message,89			location: (e.location.0, e.location.1),90		}91	})92}9394#[cfg(feature = "peg-parser")]95fn parse_peg(code: &str, source: Source) -> Result<Expr, SyntaxError> {96	jrsonnet_peg_parser::parse(code, &jrsonnet_peg_parser::ParserSettings { source }).map_err(|e| {97		let message = e98			.expected99			.tokens()100			.find(|t| t.starts_with("!!!"))101			.map_or_else(102				|| {103					format!(104						"expected {}, got {:?}",105						e.expected,106						code.chars()107							.nth(e.location.0)108							.map_or_else(|| "EOF".into(), |c: char| c.to_string())109					)110				},111				|v| v[3..].into(),112			);113		SyntaxError {114			message,115			location: e.location,116		}117	})118}119120cc_dyn!(121	#[derive(Clone)]122	CcUnbound<V>,123	Unbound<Bound = V>124);125126/// Thunk without bound `super`/`this`127/// object inheritance may be overriden multiple times, and will be fixed only on field read128pub trait Unbound: Trace {129	/// Type of value after object context is bound130	type Bound;131	/// Create value bound to specified object context132	fn bind(&self, sup_this: SupThis) -> Result<Self::Bound>;133}134135/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code136/// Standard jsonnet fields are always unbound137#[derive(Clone, Trace)]138pub enum MaybeUnbound {139	/// Value needs to be bound to `this`/`super`140	Unbound(CcUnbound<Val>),141	/// Value is object-independent142	Bound(Thunk<Val>),143}144145impl Debug for MaybeUnbound {146	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {147		write!(f, "MaybeUnbound")148	}149}150impl MaybeUnbound {151	/// Attach object context to value, if required152	pub fn evaluate(&self, sup_this: SupThis) -> Result<Val> {153		match self {154			Self::Unbound(v) => v.0.bind(sup_this),155			Self::Bound(v) => Ok(v.evaluate()?),156		}157	}158}159160cc_dyn!(CcContextInitializer, ContextInitializer);161162/// During import, this trait will be called to create initial context for file.163/// It may initialize global variables, stdlib for example.164pub trait ContextInitializer {165	/// For composability: extend builder. May panic if this initialization is not supported,166	/// and the context may only be created via `initialize`.167	fn populate(&self, for_file: Source, builder: &mut ContextBuilder);168	/// Allows upcasting from abstract to concrete context initializer.169	/// jrsonnet by itself doesn't use this method, it is allowed for it to panic.170	fn as_any(&self) -> &dyn Any;171}172impl<T> ContextInitializer for &T173where174	T: ContextInitializer,175{176	fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {177		(*self).populate(for_file, builder);178	}179180	fn as_any(&self) -> &dyn Any {181		(*self).as_any()182	}183}184185/// Context initializer which adds nothing.186impl ContextInitializer for () {187	fn populate(&self, _for_file: Source, _builder: &mut ContextBuilder) {}188	fn as_any(&self) -> &dyn Any {189		self190	}191}192193impl<T> ContextInitializer for Option<T>194where195	T: ContextInitializer + 'static,196{197	fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {198		if let Some(ctx) = self {199			ctx.populate(for_file, builder);200		}201	}202203	fn as_any(&self) -> &dyn Any {204		self205	}206}207208macro_rules! impl_context_initializer {209	($($gen:ident)*) => {210		#[allow(non_snake_case)]211		impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {212			fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {213				let ($($gen,)*) = self;214				$($gen.populate(for_file.clone(), builder);)*215			}216			fn as_any(&self) -> &dyn Any {217				self218			}219		}220	};221	($($cur:ident)* @ $c:ident $($rest:ident)*) => {222		impl_context_initializer!($($cur)*);223		impl_context_initializer!($($cur)* $c @ $($rest)*);224	};225	($($cur:ident)* @) => {226		impl_context_initializer!($($cur)*);227	}228}229impl_context_initializer! {230	A @ B C D E F G231}232233#[derive(Trace)]234struct FileData {235	string: Option<IStr>,236	bytes: Option<IBytes>,237	parsed: Option<Rc<Expr>>,238	evaluated: Option<Val>,239240	evaluating: bool,241}242impl FileData {243	fn new_string(data: IStr) -> Self {244		Self {245			string: Some(data),246			bytes: None,247			parsed: None,248			evaluated: None,249			evaluating: false,250		}251	}252	fn new_bytes(data: IBytes) -> Self {253		Self {254			string: None,255			bytes: Some(data),256			parsed: None,257			evaluated: None,258			evaluating: false,259		}260	}261	pub(crate) fn get_string(&mut self) -> Option<IStr> {262		if self.string.is_none() {263			self.string = Some(264				self.bytes265					.as_ref()266					.expect("either string or bytes should be set")267					.clone()268					.cast_str()?,269			);270		}271		Some(self.string.clone().expect("just set"))272	}273}274275#[derive(Trace)]276pub struct EvaluationStateInternals {277	/// Internal state278	file_cache: RefCell<FxHashMap<SourcePath, FileData>>,279	/// Context initializer, which will be used for imports and everything280	/// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`281	context_initializer: CcContextInitializer,282	/// Used to resolve file locations/contents283	import_resolver: Rc<dyn ImportResolver>,284}285286/// Maintains stack trace and import resolution287#[derive(Clone, Trace)]288pub struct State(Cc<EvaluationStateInternals>);289290thread_local! {291	pub static DEFAULT_STATE: State = State::builder().build();292	pub static STATE: RefCell<Option<State>> = const {RefCell::new(None)};293}294pub struct StateEnterGuard(PhantomData<()>);295impl Drop for StateEnterGuard {296	fn drop(&mut self) {297		STATE.with_borrow_mut(|v| *v = None);298	}299}300301pub fn with_state<V>(v: impl FnOnce(State) -> V) -> V {302	if let Some(state) = STATE.with_borrow(Clone::clone) {303		v(state)304	} else {305		let s = DEFAULT_STATE.with(Clone::clone);306		v(s)307	}308}309310impl State {311	pub fn enter(&self) -> StateEnterGuard {312		self.try_enter().expect("entered state already exists")313	}314	pub fn try_enter(&self) -> Option<StateEnterGuard> {315		STATE.with_borrow_mut(|v| {316			if v.is_none() {317				*v = Some(self.clone());318				Some(StateEnterGuard(PhantomData))319			} else {320				None321			}322		})323	}324	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise325	pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {326		let mut file_cache = self.file_cache();327		let mut file = file_cache.entry(path.clone());328329		let file = match file {330			Entry::Occupied(ref mut d) => d.get_mut(),331			Entry::Vacant(v) => {332				let data = self.import_resolver().load_file_contents(&path)?;333				v.insert(FileData::new_string(334					std::str::from_utf8(&data)335						.map_err(|_| ImportBadFileUtf8(path.clone()))?336						.into(),337				))338			}339		};340		Ok(file341			.get_string()342			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?)343	}344	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise345	pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {346		let mut file_cache = self.file_cache();347		let mut file = file_cache.entry(path.clone());348349		let file = match file {350			Entry::Occupied(ref mut d) => d.get_mut(),351			Entry::Vacant(v) => {352				let data = self.import_resolver().load_file_contents(&path)?;353				v.insert(FileData::new_bytes(data.as_slice().into()))354			}355		};356		if let Some(str) = &file.bytes {357			return Ok(str.clone());358		}359		if file.bytes.is_none() {360			file.bytes = Some(361				file.string362					.as_ref()363					.expect("either string or bytes should be set")364					.clone()365					.cast_bytes(),366			);367		}368		Ok(file.bytes.as_ref().expect("just set").clone())369	}370	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise371	pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {372		let mut file_cache = self.file_cache();373		let mut file = file_cache.entry(path.clone());374375		let file = match file {376			Entry::Occupied(ref mut d) => d.get_mut(),377			Entry::Vacant(v) => {378				let data = self.import_resolver().load_file_contents(&path)?;379				v.insert(FileData::new_string(380					std::str::from_utf8(&data)381						.map_err(|_| ImportBadFileUtf8(path.clone()))?382						.into(),383				))384			}385		};386		if let Some(val) = &file.evaluated {387			return Ok(val.clone());388		}389		let code = file390			.get_string()391			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?;392		let file_name = Source::new(path.clone(), code.clone());393		if file.parsed.is_none() {394			file.parsed = Some(395				parse_jsonnet(&code, file_name.clone())396					.map(Rc::new)397					.map_err(|e| ImportSyntaxError {398						path: file_name.clone(),399						error: Box::new(e),400					})?,401			);402		}403		let parsed = file.parsed.as_ref().expect("just set").clone();404		if file.evaluating {405			bail!(InfiniteRecursionDetected)406		}407		file.evaluating = true;408		// Dropping file cache guard here, as evaluation may use this map too409		drop(file_cache);410		let res = evaluate(self.create_default_context(file_name), &parsed);411412		let mut file_cache = self.file_cache();413		let mut file = file_cache.entry(path);414415		let Entry::Occupied(file) = &mut file else {416			unreachable!("this file was just here")417		};418		let file = file.get_mut();419		file.evaluating = false;420		match res {421			Ok(v) => {422				file.evaluated = Some(v.clone());423				Ok(v)424			}425			Err(e) => Err(e),426		}427	}428429	/// Has same semantics as `import 'path'` called from `from` file430	pub fn import_from(&self, from: &SourcePath, path: impl AsPathLike) -> Result<Val> {431		let resolved = self.resolve_from(from, &path)?;432		self.import_resolved(resolved)433	}434	pub fn import(&self, path: impl AsPathLike) -> Result<Val> {435		let resolved = self.resolve_from_default(&path)?;436		self.import_resolved(resolved)437	}438439	/// Creates context with all passed global variables440	pub fn create_default_context(&self, source: Source) -> Context {441		self.create_default_context_with(source, &())442	}443444	/// Creates context with all passed global variables, calling custom modifier445	pub fn create_default_context_with(446		&self,447		source: Source,448		context_initializer: &dyn ContextInitializer,449	) -> Context {450		let default_initializer = self.context_initializer();451		let mut builder = ContextBuilder::new();452		default_initializer.populate(source.clone(), &mut builder);453		context_initializer.populate(source, &mut builder);454455		builder.build()456	}457}458459/// Internals460impl State {461	fn file_cache(&self) -> RefMut<'_, FxHashMap<SourcePath, FileData>> {462		self.0.file_cache.borrow_mut()463	}464}465/// Executes code creating a new stack frame, to be replaced with try{}466pub fn in_frame<T>(467	e: CallLocation<'_>,468	frame_desc: impl FnOnce() -> String,469	f: impl FnOnce() -> Result<T>,470) -> Result<T> {471	let _guard = check_depth()?;472473	f().with_description_src(e, frame_desc)474}475476/// Executes code creating a new stack frame, to be replaced with try{}477pub fn in_description_frame<T>(478	frame_desc: impl FnOnce() -> String,479	f: impl FnOnce() -> Result<T>,480) -> Result<T> {481	let _guard = check_depth()?;482483	f().with_description(frame_desc)484}485486#[derive(Trace)]487pub struct InitialUnderscore(pub Thunk<Val>);488impl ContextInitializer for InitialUnderscore {489	fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) {490		builder.bind("_", self.0.clone());491	}492493	fn as_any(&self) -> &dyn Any {494		self495	}496}497498/// Raw methods evaluate passed values but don't perform TLA execution499impl State {500	/// Parses and evaluates the given snippet501	pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {502		self.evaluate_snippet_with(name, code, &())503	}504	/// Parses and evaluates the given snippet with custom context modifier505	pub fn evaluate_snippet_with(506		&self,507		name: impl Into<IStr>,508		code: impl Into<IStr>,509		context_initializer: &dyn ContextInitializer,510	) -> Result<Val> {511		let code = code.into();512		let source = Source::new_virtual(name.into(), code.clone());513		let parsed = parse_jsonnet(&code, source.clone()).map_err(|e| ImportSyntaxError {514			path: source.clone(),515			error: Box::new(e),516		})?;517		evaluate(518			self.create_default_context_with(source, context_initializer),519			&parsed,520		)521	}522}523524/// Settings utilities525impl State {526	// Only panics in case of [`ImportResolver`] contract violation527	#[allow(clippy::missing_panics_doc)]528	pub fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {529		self.import_resolver().resolve_from(from, path)530	}531	#[allow(clippy::missing_panics_doc)]532	pub fn resolve_from_default(&self, path: &dyn AsPathLike) -> Result<SourcePath> {533		self.import_resolver().resolve_from_default(path)534	}535	pub fn import_resolver(&self) -> &dyn ImportResolver {536		&*self.0.import_resolver537	}538	pub fn context_initializer(&self) -> &dyn ContextInitializer {539		&*self.0.context_initializer.0540	}541}542543impl State {544	pub fn builder() -> StateBuilder {545		StateBuilder::default()546	}547}548549impl Default for State {550	fn default() -> Self {551		Self::builder().build()552	}553}554555#[derive(Default)]556pub struct StateBuilder {557	import_resolver: Option<Rc<dyn ImportResolver>>,558	context_initializer: Option<CcContextInitializer>,559}560impl StateBuilder {561	pub fn import_resolver(&mut self, import_resolver: impl ImportResolver) -> &mut Self {562		let _ = self.import_resolver.insert(Rc::new(import_resolver));563		self564	}565	pub fn context_initializer(566		&mut self,567		context_initializer: impl ContextInitializer + Trace,568	) -> &mut Self {569		let _ = self570			.context_initializer571			.insert(CcContextInitializer::new(context_initializer));572		self573	}574	pub fn build(mut self) -> State {575		State(Cc::new(EvaluationStateInternals {576			file_cache: RefCell::new(FxHashMap::new()),577			context_initializer: self578				.context_initializer579				.take()580				.unwrap_or_else(|| CcContextInitializer::new(())),581			import_resolver: self582				.import_resolver583				.take()584				.unwrap_or_else(|| Rc::new(DummyImportResolver)),585		}))586	}587}