git.delta.rocks / jrsonnet / refs/commits / 7bd5bbd0756a

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	Const(Val),148}149150impl Debug for MaybeUnbound {151	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {152		write!(f, "MaybeUnbound")153	}154}155impl MaybeUnbound {156	/// Attach object context to value, if required157	pub fn evaluate(&self, sup_this: SupThis) -> Result<Val> {158		match self {159			Self::Unbound(v) => v.0.bind(sup_this),160			Self::Bound(v) => Ok(v.evaluate()?),161			Self::Const(v) => Ok(v.clone()),162		}163	}164}165166cc_dyn!(CcContextInitializer, ContextInitializer);167168/// During import, this trait will be called to create initial context for file.169/// It may initialize global variables, stdlib for example.170pub trait ContextInitializer {171	/// For composability: extend builder. May panic if this initialization is not supported,172	/// and the context may only be created via `initialize`.173	fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder);174	/// Allows upcasting from abstract to concrete context initializer.175	/// jrsonnet by itself doesn't use this method, it is allowed for it to panic.176	fn as_any(&self) -> &dyn Any;177}178impl<T> ContextInitializer for &T179where180	T: ContextInitializer,181{182	fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder) {183		(*self).populate(for_file, builder);184	}185186	fn as_any(&self) -> &dyn Any {187		(*self).as_any()188	}189}190191/// Context initializer which adds nothing.192impl ContextInitializer for () {193	fn populate(&self, _for_file: Source, _builder: &mut InitialContextBuilder) {}194	fn as_any(&self) -> &dyn Any {195		self196	}197}198199impl<T> ContextInitializer for Option<T>200where201	T: ContextInitializer + 'static,202{203	fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder) {204		if let Some(ctx) = self {205			ctx.populate(for_file, builder);206		}207	}208209	fn as_any(&self) -> &dyn Any {210		self211	}212}213214macro_rules! impl_context_initializer {215	($($gen:ident)*) => {216		#[allow(non_snake_case)]217		impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {218			fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder) {219				let ($($gen,)*) = self;220				$($gen.populate(for_file.clone(), builder);)*221			}222			fn as_any(&self) -> &dyn Any {223				self224			}225		}226	};227	($($cur:ident)* @ $c:ident $($rest:ident)*) => {228		impl_context_initializer!($($cur)*);229		impl_context_initializer!($($cur)* $c @ $($rest)*);230	};231	($($cur:ident)* @) => {232		impl_context_initializer!($($cur)*);233	}234}235impl_context_initializer! {236	A @ B C D E F G237}238239#[derive(Trace)]240struct FileData {241	string: Option<IStr>,242	bytes: Option<IBytes>,243	parsed: Option<Rc<Expr>>,244	evaluated: Option<Val>,245246	evaluating: bool,247}248impl FileData {249	fn new_string(data: IStr) -> Self {250		Self {251			string: Some(data),252			bytes: None,253			parsed: None,254			evaluated: None,255			evaluating: false,256		}257	}258	fn new_bytes(data: IBytes) -> Self {259		Self {260			string: None,261			bytes: Some(data),262			parsed: None,263			evaluated: None,264			evaluating: false,265		}266	}267	pub(crate) fn get_string(&mut self) -> Option<IStr> {268		if self.string.is_none() {269			self.string = Some(270				self.bytes271					.as_ref()272					.expect("either string or bytes should be set")273					.clone()274					.cast_str()?,275			);276		}277		Some(self.string.clone().expect("just set"))278	}279}280281#[derive(Trace)]282pub struct EvaluationStateInternals {283	/// Internal state284	file_cache: RefCell<FxHashMap<SourcePath, FileData>>,285	/// Context initializer, which will be used for imports and everything286	/// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`287	context_initializer: CcContextInitializer,288	/// Used to resolve file locations/contents289	import_resolver: Rc<dyn ImportResolver>,290}291292/// Maintains stack trace and import resolution293#[derive(Clone, Trace)]294pub struct State(Cc<EvaluationStateInternals>);295296thread_local! {297	pub static DEFAULT_STATE: State = State::builder().build();298	pub static STATE: RefCell<Option<State>> = const {RefCell::new(None)};299}300pub struct StateEnterGuard(PhantomData<()>);301impl Drop for StateEnterGuard {302	fn drop(&mut self) {303		STATE.with_borrow_mut(|v| *v = None);304	}305}306307pub fn with_state<V>(v: impl FnOnce(State) -> V) -> V {308	if let Some(state) = STATE.with_borrow(Clone::clone) {309		v(state)310	} else {311		let s = DEFAULT_STATE.with(Clone::clone);312		v(s)313	}314}315316impl State {317	pub fn enter(&self) -> StateEnterGuard {318		self.try_enter().expect("entered state already exists")319	}320	pub fn try_enter(&self) -> Option<StateEnterGuard> {321		STATE.with_borrow_mut(|v| {322			if v.is_none() {323				*v = Some(self.clone());324				Some(StateEnterGuard(PhantomData))325			} else {326				None327			}328		})329	}330	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise331	pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {332		let mut file_cache = self.file_cache();333		let mut file = file_cache.entry(path.clone());334335		let file = match file {336			Entry::Occupied(ref mut d) => d.get_mut(),337			Entry::Vacant(v) => {338				let data = self.import_resolver().load_file_contents(&path)?;339				v.insert(FileData::new_string(340					std::str::from_utf8(&data)341						.map_err(|_| ImportBadFileUtf8(path.clone()))?342						.into(),343				))344			}345		};346		Ok(file347			.get_string()348			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?)349	}350	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise351	pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {352		let mut file_cache = self.file_cache();353		let mut file = file_cache.entry(path.clone());354355		let file = match file {356			Entry::Occupied(ref mut d) => d.get_mut(),357			Entry::Vacant(v) => {358				let data = self.import_resolver().load_file_contents(&path)?;359				v.insert(FileData::new_bytes(data.as_slice().into()))360			}361		};362		if let Some(str) = &file.bytes {363			return Ok(str.clone());364		}365		if file.bytes.is_none() {366			file.bytes = Some(367				file.string368					.as_ref()369					.expect("either string or bytes should be set")370					.clone()371					.cast_bytes(),372			);373		}374		Ok(file.bytes.as_ref().expect("just set").clone())375	}376	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise377	pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {378		let mut file_cache = self.file_cache();379		let mut file = file_cache.entry(path.clone());380381		let file = match file {382			Entry::Occupied(ref mut d) => d.get_mut(),383			Entry::Vacant(v) => {384				let data = self.import_resolver().load_file_contents(&path)?;385				v.insert(FileData::new_string(386					std::str::from_utf8(&data)387						.map_err(|_| ImportBadFileUtf8(path.clone()))?388						.into(),389				))390			}391		};392		if let Some(val) = &file.evaluated {393			return Ok(val.clone());394		}395		let code = file396			.get_string()397			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?;398		let file_name = Source::new(path.clone(), code.clone());399		if file.parsed.is_none() {400			file.parsed = Some(401				parse_jsonnet(&code, file_name.clone())402					.map(Rc::new)403					.map_err(|e| {404						let span = e.location.clone();405						let mut err = Error::from(ImportSyntaxError {406							path: file_name.clone(),407							error: Box::new(e),408						});409						err.trace_mut().0.push(StackTraceElement {410							location: Some(span),411							desc: "parse imported".to_string(),412						});413						err414					})?,415			);416		}417		let parsed = file.parsed.as_ref().expect("just set").clone();418		if file.evaluating {419			bail!(InfiniteRecursionDetected)420		}421		file.evaluating = true;422		// Dropping file cache guard here, as evaluation may use this map too423		drop(file_cache);424		let (externals, thunks) = self.create_default_context(file_name).build();425		let report = analyze_root(&parsed, externals);426		if report.errored {427			return Err(StaticAnalysisError(report.diagnostics_list).into());428		}429		debug_assert_eq!(report.root_shape.n_locals as usize, thunks.len());430		debug_assert!(report.root_shape.captures.is_empty());431		let ctx = Context::root(thunks);432		let res = evaluate::evaluate(ctx, &report.lir);433434		let mut file_cache = self.file_cache();435		let mut file = file_cache.entry(path);436437		let Entry::Occupied(file) = &mut file else {438			unreachable!("this file was just here")439		};440		let file = file.get_mut();441		file.evaluating = false;442		match res {443			Ok(v) => {444				file.evaluated = Some(v.clone());445				Ok(v)446			}447			Err(e) => Err(e),448		}449	}450451	/// Has same semantics as `import 'path'` called from `from` file452	pub fn import_from(&self, from: &SourcePath, path: impl AsPathLike) -> Result<Val> {453		let resolved = self.resolve_from(from, &path)?;454		self.import_resolved(resolved)455	}456	pub fn import(&self, path: impl AsPathLike) -> Result<Val> {457		let resolved = self.resolve_from_default(&path)?;458		self.import_resolved(resolved)459	}460461	/// Creates context with all passed global variables462	pub fn create_default_context(&self, source: Source) -> InitialContextBuilder {463		self.create_default_context_with(source, &())464	}465466	/// Creates context with all passed global variables, calling custom modifier467	pub fn create_default_context_with(468		&self,469		source: Source,470		context_initializer: &dyn ContextInitializer,471	) -> InitialContextBuilder {472		let default_initializer = self.context_initializer();473		let mut builder = InitialContextBuilder::new();474		default_initializer.populate(source.clone(), &mut builder);475		context_initializer.populate(source, &mut builder);476477		builder478	}479}480481/// Internals482impl State {483	fn file_cache(&self) -> RefMut<'_, FxHashMap<SourcePath, FileData>> {484		self.0.file_cache.borrow_mut()485	}486}487/// Executes code creating a new stack frame, to be replaced with try{}488pub fn in_frame<T>(489	e: CallLocation<'_>,490	frame_desc: impl FnOnce() -> String,491	f: impl FnOnce() -> Result<T>,492) -> Result<T> {493	let _guard = check_depth()?;494495	f().with_description_src(e, frame_desc)496}497498/// Executes code creating a new stack frame, to be replaced with try{}499pub fn in_description_frame<T>(500	frame_desc: impl FnOnce() -> String,501	f: impl FnOnce() -> Result<T>,502) -> Result<T> {503	let _guard = check_depth()?;504505	f().with_description(frame_desc)506}507508#[derive(Trace)]509pub struct InitialUnderscore(pub Thunk<Val>);510impl ContextInitializer for InitialUnderscore {511	fn populate(&self, _for_file: Source, builder: &mut InitialContextBuilder) {512		builder.bind("_", self.0.clone());513	}514515	fn as_any(&self) -> &dyn Any {516		self517	}518}519520pub struct PreparedSnippet {521	lir: LExpr,522	thunks: Vec<Thunk<Val>>,523}524525/// Raw methods evaluate passed values but don't perform TLA execution526impl State {527	/// Parses and analyses the given snippet with a custom context528	/// modifier.529	pub fn prepare_snippet_with(530		&self,531		name: impl Into<IStr>,532		code: impl Into<IStr>,533		context_initializer: &dyn ContextInitializer,534	) -> Result<PreparedSnippet> {535		let code = code.into();536		let source = Source::new_virtual(name.into(), code.clone());537		let parsed = parse_jsonnet(&code, source.clone()).map_err(|e| ImportSyntaxError {538			path: source.clone(),539			error: Box::new(e),540		})?;541		let (externals, thunks) = self542			.create_default_context_with(source, context_initializer)543			.build();544		let report = analyze_root(&parsed, externals);545		if report.errored {546			return Err(StaticAnalysisError(report.diagnostics_list).into());547		}548		debug_assert_eq!(report.root_shape.n_locals as usize, thunks.len());549		debug_assert!(report.root_shape.captures.is_empty());550		Ok(PreparedSnippet {551			lir: report.lir,552			thunks,553		})554	}555	/// Parses and analyses the given snippet556	pub fn prepare_snippet(557		&self,558		name: impl Into<IStr>,559		code: impl Into<IStr>,560	) -> Result<PreparedSnippet> {561		self.prepare_snippet_with(name, code, &())562	}563	pub fn evaluate_prepared_snippet(&self, prepared: &PreparedSnippet) -> Result<Val> {564		let ctx = Context::root(prepared.thunks.clone());565		evaluate::evaluate(ctx, &prepared.lir)566	}567	/// Parses and evaluates the given snippet with custom context modifier568	pub fn evaluate_snippet_with(569		&self,570		name: impl Into<IStr>,571		code: impl Into<IStr>,572		context_initializer: &dyn ContextInitializer,573	) -> Result<Val> {574		let prepared = self.prepare_snippet_with(name, code, context_initializer)?;575		self.evaluate_prepared_snippet(&prepared)576	}577	/// Parses and evaluates the given snippet578	pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {579		self.evaluate_snippet_with(name, code, &())580	}581}582583/// Settings utilities584impl State {585	// Only panics in case of [`ImportResolver`] contract violation586	#[allow(clippy::missing_panics_doc)]587	pub fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {588		self.import_resolver().resolve_from(from, path)589	}590	#[allow(clippy::missing_panics_doc)]591	pub fn resolve_from_default(&self, path: &dyn AsPathLike) -> Result<SourcePath> {592		self.import_resolver().resolve_from_default(path)593	}594	pub fn import_resolver(&self) -> &dyn ImportResolver {595		&*self.0.import_resolver596	}597	pub fn context_initializer(&self) -> &dyn ContextInitializer {598		&*self.0.context_initializer.0599	}600}601602impl State {603	pub fn builder() -> StateBuilder {604		StateBuilder::default()605	}606}607608impl Default for State {609	fn default() -> Self {610		Self::builder().build()611	}612}613614#[derive(Default)]615pub struct StateBuilder {616	import_resolver: Option<Rc<dyn ImportResolver>>,617	context_initializer: Option<CcContextInitializer>,618}619impl StateBuilder {620	pub fn import_resolver(&mut self, import_resolver: impl ImportResolver) -> &mut Self {621		let _ = self.import_resolver.insert(Rc::new(import_resolver));622		self623	}624	pub fn context_initializer(625		&mut self,626		context_initializer: impl ContextInitializer + Trace,627	) -> &mut Self {628		let _ = self629			.context_initializer630			.insert(CcContextInitializer::new(context_initializer));631		self632	}633	pub fn build(mut self) -> State {634		State(Cc::new(EvaluationStateInternals {635			file_cache: RefCell::new(FxHashMap::new()),636			context_initializer: self637				.context_initializer638				.take()639				.unwrap_or_else(|| CcContextInitializer::new(())),640			import_resolver: self641				.import_resolver642				.take()643				.unwrap_or_else(|| Rc::new(DummyImportResolver)),644		}))645	}646}