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

difftreelog

source

crates/jrsonnet-evaluator/src/lib.rs16.8 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::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::{NumValue, Source, SourcePath, Span};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};5960pub mod analyze;61use self::analyze::{LExpr, analyze_root};62use crate::gc::WithCapacityExt as _;6364#[allow(clippy::needless_return)]65pub(crate) fn parse_jsonnet(code: &str, source: Source) -> Result<Expr, SyntaxError> {66	#[cfg(feature = "peg-parser")]67	{68		use std::sync::LazyLock;69		static USE_LEGACY_PARSER: LazyLock<bool> =70			LazyLock::new(|| std::env::var_os("JRSONNET_LEGACY_PARSER").is_some());7172		if *USE_LEGACY_PARSER {73			return parse_peg(code, source);74		}75	}76	#[cfg(feature = "ir-parser")]77	{78		return parse_ir(code, source);79	}80	#[cfg(feature = "peg-parser")]81	{82		return parse_peg(code, source);83	}84}8586#[cfg(feature = "ir-parser")]87fn parse_ir(code: &str, source: Source) -> Result<Expr, SyntaxError> {88	jrsonnet_ir_parser::parse(code, &jrsonnet_ir_parser::ParserSettings { source }).map_err(|e| {89		SyntaxError {90			message: e.message,91			location: e.location,92		}93	})94}9596#[cfg(feature = "peg-parser")]97fn parse_peg(code: &str, source: Source) -> Result<Expr, SyntaxError> {98	jrsonnet_peg_parser::parse(code, &jrsonnet_peg_parser::ParserSettings { source }).map_err(|e| {99		let message = e100			.expected101			.tokens()102			.find(|t| t.starts_with("!!!"))103			.map_or_else(104				|| {105					format!(106						"expected {}, got {:?}",107						e.expected,108						code.chars()109							.nth(e.location.0)110							.map_or_else(|| "EOF".into(), |c: char| c.to_string())111					)112				},113				|v| v[3..].into(),114			);115		SyntaxError {116			message,117			location: e.location,118		}119	})120}121122cc_dyn!(123	#[derive(Clone)]124	CcUnbound<V>,125	Unbound<Bound = V>126);127128/// Thunk without bound `super`/`this`129/// object inheritance may be overriden multiple times, and will be fixed only on field read130pub trait Unbound: Trace {131	/// Type of value after object context is bound132	type Bound;133	/// Create value bound to specified object context134	fn bind(&self, sup_this: SupThis) -> Result<Self::Bound>;135}136137/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code138/// Standard jsonnet fields are always unbound139#[derive(Clone, Trace)]140pub enum MaybeUnbound {141	/// Value needs to be bound to `this`/`super`142	Unbound(CcUnbound<Val>),143	/// Value is object-independent144	Bound(Thunk<Val>),145}146147impl Debug for MaybeUnbound {148	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {149		write!(f, "MaybeUnbound")150	}151}152impl MaybeUnbound {153	/// Attach object context to value, if required154	pub fn evaluate(&self, sup_this: SupThis) -> Result<Val> {155		match self {156			Self::Unbound(v) => v.0.bind(sup_this),157			Self::Bound(v) => Ok(v.evaluate()?),158		}159	}160}161162cc_dyn!(CcContextInitializer, ContextInitializer);163164/// During import, this trait will be called to create initial context for file.165/// It may initialize global variables, stdlib for example.166pub trait ContextInitializer {167	/// For composability: extend builder. May panic if this initialization is not supported,168	/// and the context may only be created via `initialize`.169	fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder);170	/// Allows upcasting from abstract to concrete context initializer.171	/// jrsonnet by itself doesn't use this method, it is allowed for it to panic.172	fn as_any(&self) -> &dyn Any;173}174impl<T> ContextInitializer for &T175where176	T: ContextInitializer,177{178	fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder) {179		(*self).populate(for_file, builder);180	}181182	fn as_any(&self) -> &dyn Any {183		(*self).as_any()184	}185}186187/// Context initializer which adds nothing.188impl ContextInitializer for () {189	fn populate(&self, _for_file: Source, _builder: &mut InitialContextBuilder) {}190	fn as_any(&self) -> &dyn Any {191		self192	}193}194195impl<T> ContextInitializer for Option<T>196where197	T: ContextInitializer + 'static,198{199	fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder) {200		if let Some(ctx) = self {201			ctx.populate(for_file, builder);202		}203	}204205	fn as_any(&self) -> &dyn Any {206		self207	}208}209210macro_rules! impl_context_initializer {211	($($gen:ident)*) => {212		#[allow(non_snake_case)]213		impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {214			fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder) {215				let ($($gen,)*) = self;216				$($gen.populate(for_file.clone(), builder);)*217			}218			fn as_any(&self) -> &dyn Any {219				self220			}221		}222	};223	($($cur:ident)* @ $c:ident $($rest:ident)*) => {224		impl_context_initializer!($($cur)*);225		impl_context_initializer!($($cur)* $c @ $($rest)*);226	};227	($($cur:ident)* @) => {228		impl_context_initializer!($($cur)*);229	}230}231impl_context_initializer! {232	A @ B C D E F G233}234235#[derive(Trace)]236struct FileData {237	string: Option<IStr>,238	bytes: Option<IBytes>,239	parsed: Option<Rc<Expr>>,240	evaluated: Option<Val>,241242	evaluating: bool,243}244impl FileData {245	fn new_string(data: IStr) -> Self {246		Self {247			string: Some(data),248			bytes: None,249			parsed: None,250			evaluated: None,251			evaluating: false,252		}253	}254	fn new_bytes(data: IBytes) -> Self {255		Self {256			string: None,257			bytes: Some(data),258			parsed: None,259			evaluated: None,260			evaluating: false,261		}262	}263	pub(crate) fn get_string(&mut self) -> Option<IStr> {264		if self.string.is_none() {265			self.string = Some(266				self.bytes267					.as_ref()268					.expect("either string or bytes should be set")269					.clone()270					.cast_str()?,271			);272		}273		Some(self.string.clone().expect("just set"))274	}275}276277#[derive(Trace)]278pub struct EvaluationStateInternals {279	/// Internal state280	file_cache: RefCell<FxHashMap<SourcePath, FileData>>,281	/// Context initializer, which will be used for imports and everything282	/// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`283	context_initializer: CcContextInitializer,284	/// Used to resolve file locations/contents285	import_resolver: Rc<dyn ImportResolver>,286}287288/// Maintains stack trace and import resolution289#[derive(Clone, Trace)]290pub struct State(Cc<EvaluationStateInternals>);291292thread_local! {293	pub static DEFAULT_STATE: State = State::builder().build();294	pub static STATE: RefCell<Option<State>> = const {RefCell::new(None)};295}296pub struct StateEnterGuard(PhantomData<()>);297impl Drop for StateEnterGuard {298	fn drop(&mut self) {299		STATE.with_borrow_mut(|v| *v = None);300	}301}302303pub fn with_state<V>(v: impl FnOnce(State) -> V) -> V {304	if let Some(state) = STATE.with_borrow(Clone::clone) {305		v(state)306	} else {307		let s = DEFAULT_STATE.with(Clone::clone);308		v(s)309	}310}311312impl State {313	pub fn enter(&self) -> StateEnterGuard {314		self.try_enter().expect("entered state already exists")315	}316	pub fn try_enter(&self) -> Option<StateEnterGuard> {317		STATE.with_borrow_mut(|v| {318			if v.is_none() {319				*v = Some(self.clone());320				Some(StateEnterGuard(PhantomData))321			} else {322				None323			}324		})325	}326	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise327	pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {328		let mut file_cache = self.file_cache();329		let mut file = file_cache.entry(path.clone());330331		let file = match file {332			Entry::Occupied(ref mut d) => d.get_mut(),333			Entry::Vacant(v) => {334				let data = self.import_resolver().load_file_contents(&path)?;335				v.insert(FileData::new_string(336					std::str::from_utf8(&data)337						.map_err(|_| ImportBadFileUtf8(path.clone()))?338						.into(),339				))340			}341		};342		Ok(file343			.get_string()344			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?)345	}346	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise347	pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {348		let mut file_cache = self.file_cache();349		let mut file = file_cache.entry(path.clone());350351		let file = match file {352			Entry::Occupied(ref mut d) => d.get_mut(),353			Entry::Vacant(v) => {354				let data = self.import_resolver().load_file_contents(&path)?;355				v.insert(FileData::new_bytes(data.as_slice().into()))356			}357		};358		if let Some(str) = &file.bytes {359			return Ok(str.clone());360		}361		if file.bytes.is_none() {362			file.bytes = Some(363				file.string364					.as_ref()365					.expect("either string or bytes should be set")366					.clone()367					.cast_bytes(),368			);369		}370		Ok(file.bytes.as_ref().expect("just set").clone())371	}372	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise373	pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {374		let mut file_cache = self.file_cache();375		let mut file = file_cache.entry(path.clone());376377		let file = match file {378			Entry::Occupied(ref mut d) => d.get_mut(),379			Entry::Vacant(v) => {380				let data = self.import_resolver().load_file_contents(&path)?;381				v.insert(FileData::new_string(382					std::str::from_utf8(&data)383						.map_err(|_| ImportBadFileUtf8(path.clone()))?384						.into(),385				))386			}387		};388		if let Some(val) = &file.evaluated {389			return Ok(val.clone());390		}391		let code = file392			.get_string()393			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?;394		let file_name = Source::new(path.clone(), code.clone());395		if file.parsed.is_none() {396			file.parsed = Some(397				parse_jsonnet(&code, file_name.clone())398					.map(Rc::new)399					.map_err(|e| ImportSyntaxError {400						path: file_name.clone(),401						error: Box::new(e),402					})?,403			);404		}405		let parsed = file.parsed.as_ref().expect("just set").clone();406		if file.evaluating {407			bail!(InfiniteRecursionDetected)408		}409		file.evaluating = true;410		// Dropping file cache guard here, as evaluation may use this map too411		drop(file_cache);412		let (externals, thunks) = self.create_default_context(file_name).build();413		let report = analyze_root(&parsed, externals);414		if report.errored {415			return Err(StaticAnalysisError(report.diagnostics_list).into());416		}417		debug_assert_eq!(report.root_shape.n_locals as usize, thunks.len());418		debug_assert!(report.root_shape.captures.is_empty());419		let ctx = Context::root(thunks);420		let res = evaluate::evaluate(ctx, &report.lir);421422		let mut file_cache = self.file_cache();423		let mut file = file_cache.entry(path);424425		let Entry::Occupied(file) = &mut file else {426			unreachable!("this file was just here")427		};428		let file = file.get_mut();429		file.evaluating = false;430		match res {431			Ok(v) => {432				file.evaluated = Some(v.clone());433				Ok(v)434			}435			Err(e) => Err(e),436		}437	}438439	/// Has same semantics as `import 'path'` called from `from` file440	pub fn import_from(&self, from: &SourcePath, path: impl AsPathLike) -> Result<Val> {441		let resolved = self.resolve_from(from, &path)?;442		self.import_resolved(resolved)443	}444	pub fn import(&self, path: impl AsPathLike) -> Result<Val> {445		let resolved = self.resolve_from_default(&path)?;446		self.import_resolved(resolved)447	}448449	/// Creates context with all passed global variables450	pub fn create_default_context(&self, source: Source) -> InitialContextBuilder {451		self.create_default_context_with(source, &())452	}453454	/// Creates context with all passed global variables, calling custom modifier455	pub fn create_default_context_with(456		&self,457		source: Source,458		context_initializer: &dyn ContextInitializer,459	) -> InitialContextBuilder {460		let default_initializer = self.context_initializer();461		let mut builder = InitialContextBuilder::new();462		default_initializer.populate(source.clone(), &mut builder);463		context_initializer.populate(source, &mut builder);464465		builder466	}467}468469/// Internals470impl State {471	fn file_cache(&self) -> RefMut<'_, FxHashMap<SourcePath, FileData>> {472		self.0.file_cache.borrow_mut()473	}474}475/// Executes code creating a new stack frame, to be replaced with try{}476pub fn in_frame<T>(477	e: CallLocation<'_>,478	frame_desc: impl FnOnce() -> String,479	f: impl FnOnce() -> Result<T>,480) -> Result<T> {481	let _guard = check_depth()?;482483	f().with_description_src(e, frame_desc)484}485486/// Executes code creating a new stack frame, to be replaced with try{}487pub fn in_description_frame<T>(488	frame_desc: impl FnOnce() -> String,489	f: impl FnOnce() -> Result<T>,490) -> Result<T> {491	let _guard = check_depth()?;492493	f().with_description(frame_desc)494}495496#[derive(Trace)]497pub struct InitialUnderscore(pub Thunk<Val>);498impl ContextInitializer for InitialUnderscore {499	fn populate(&self, _for_file: Source, builder: &mut InitialContextBuilder) {500		builder.bind("_", self.0.clone());501	}502503	fn as_any(&self) -> &dyn Any {504		self505	}506}507508pub struct PreparedSnippet {509	lir: LExpr,510	thunks: Vec<Thunk<Val>>,511}512513/// Raw methods evaluate passed values but don't perform TLA execution514impl State {515	/// Parses and analyses the given snippet with a custom context516	/// modifier.517	pub fn prepare_snippet_with(518		&self,519		name: impl Into<IStr>,520		code: impl Into<IStr>,521		context_initializer: &dyn ContextInitializer,522	) -> Result<PreparedSnippet> {523		let code = code.into();524		let source = Source::new_virtual(name.into(), code.clone());525		let parsed = parse_jsonnet(&code, source.clone()).map_err(|e| ImportSyntaxError {526			path: source.clone(),527			error: Box::new(e),528		})?;529		let (externals, thunks) = self530			.create_default_context_with(source, context_initializer)531			.build();532		let report = analyze_root(&parsed, externals);533		if report.errored {534			return Err(StaticAnalysisError(report.diagnostics_list).into());535		}536		debug_assert_eq!(report.root_shape.n_locals as usize, thunks.len());537		debug_assert!(report.root_shape.captures.is_empty());538		Ok(PreparedSnippet {539			lir: report.lir,540			thunks,541		})542	}543	/// Parses and analyses the given snippet544	pub fn prepare_snippet(545		&self,546		name: impl Into<IStr>,547		code: impl Into<IStr>,548	) -> Result<PreparedSnippet> {549		self.prepare_snippet_with(name, code, &())550	}551	pub fn evaluate_prepared_snippet(&self, prepared: &PreparedSnippet) -> Result<Val> {552		let ctx = Context::root(prepared.thunks.clone());553		evaluate::evaluate(ctx, &prepared.lir)554	}555	/// Parses and evaluates the given snippet with custom context modifier556	pub fn evaluate_snippet_with(557		&self,558		name: impl Into<IStr>,559		code: impl Into<IStr>,560		context_initializer: &dyn ContextInitializer,561	) -> Result<Val> {562		let prepared = self.prepare_snippet_with(name, code, context_initializer)?;563		self.evaluate_prepared_snippet(&prepared)564	}565	/// Parses and evaluates the given snippet566	pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {567		self.evaluate_snippet_with(name, code, &())568	}569}570571/// Settings utilities572impl State {573	// Only panics in case of [`ImportResolver`] contract violation574	#[allow(clippy::missing_panics_doc)]575	pub fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {576		self.import_resolver().resolve_from(from, path)577	}578	#[allow(clippy::missing_panics_doc)]579	pub fn resolve_from_default(&self, path: &dyn AsPathLike) -> Result<SourcePath> {580		self.import_resolver().resolve_from_default(path)581	}582	pub fn import_resolver(&self) -> &dyn ImportResolver {583		&*self.0.import_resolver584	}585	pub fn context_initializer(&self) -> &dyn ContextInitializer {586		&*self.0.context_initializer.0587	}588}589590impl State {591	pub fn builder() -> StateBuilder {592		StateBuilder::default()593	}594}595596impl Default for State {597	fn default() -> Self {598		Self::builder().build()599	}600}601602#[derive(Default)]603pub struct StateBuilder {604	import_resolver: Option<Rc<dyn ImportResolver>>,605	context_initializer: Option<CcContextInitializer>,606}607impl StateBuilder {608	pub fn import_resolver(&mut self, import_resolver: impl ImportResolver) -> &mut Self {609		let _ = self.import_resolver.insert(Rc::new(import_resolver));610		self611	}612	pub fn context_initializer(613		&mut self,614		context_initializer: impl ContextInitializer + Trace,615	) -> &mut Self {616		let _ = self617			.context_initializer618			.insert(CcContextInitializer::new(context_initializer));619		self620	}621	pub fn build(mut self) -> State {622		State(Cc::new(EvaluationStateInternals {623			file_cache: RefCell::new(FxHashMap::new()),624			context_initializer: self625				.context_initializer626				.take()627				.unwrap_or_else(|| CcContextInitializer::new(())),628			import_resolver: self629				.import_resolver630				.take()631				.unwrap_or_else(|| Rc::new(DummyImportResolver)),632		}))633	}634}