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

difftreelog

source

crates/jrsonnet-evaluator/src/lib.rs15.7 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 crate::gc::WithCapacityExt as _;6263#[allow(clippy::needless_return)]64pub(crate) fn parse_jsonnet(code: &str, source: Source) -> Result<Expr, SyntaxError> {65	#[cfg(feature = "peg-parser")]66	{67		use std::sync::LazyLock;68		static USE_LEGACY_PARSER: LazyLock<bool> =69			LazyLock::new(|| std::env::var_os("JRSONNET_LEGACY_PARSER").is_some());7071		if *USE_LEGACY_PARSER {72			return parse_peg(code, source);73		}74	}75	#[cfg(feature = "ir-parser")]76	{77		return parse_ir(code, source);78	}79	#[cfg(feature = "peg-parser")]80	{81		return parse_peg(code, source);82	}83}8485#[cfg(feature = "ir-parser")]86fn parse_ir(code: &str, source: Source) -> Result<Expr, SyntaxError> {87	jrsonnet_ir_parser::parse(code, &jrsonnet_ir_parser::ParserSettings { source }).map_err(|e| {88		SyntaxError {89			message: e.message,90			location: e.location,91		}92	})93}9495#[cfg(feature = "peg-parser")]96fn parse_peg(code: &str, source: Source) -> Result<Expr, SyntaxError> {97	jrsonnet_peg_parser::parse(code, &jrsonnet_peg_parser::ParserSettings { source }).map_err(|e| {98		let message = e99			.expected100			.tokens()101			.find(|t| t.starts_with("!!!"))102			.map_or_else(103				|| {104					format!(105						"expected {}, got {:?}",106						e.expected,107						code.chars()108							.nth(e.location.0)109							.map_or_else(|| "EOF".into(), |c: char| c.to_string())110					)111				},112				|v| v[3..].into(),113			);114		SyntaxError {115			message,116			location: e.location,117		}118	})119}120121cc_dyn!(122	#[derive(Clone)]123	CcUnbound<V>,124	Unbound<Bound = V>125);126127/// Thunk without bound `super`/`this`128/// object inheritance may be overriden multiple times, and will be fixed only on field read129pub trait Unbound: Trace {130	/// Type of value after object context is bound131	type Bound;132	/// Create value bound to specified object context133	fn bind(&self, sup_this: SupThis) -> Result<Self::Bound>;134}135136/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code137/// Standard jsonnet fields are always unbound138#[derive(Clone, Trace)]139pub enum MaybeUnbound {140	/// Value needs to be bound to `this`/`super`141	Unbound(CcUnbound<Val>),142	/// Value is object-independent143	Bound(Thunk<Val>),144}145146impl Debug for MaybeUnbound {147	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {148		write!(f, "MaybeUnbound")149	}150}151impl MaybeUnbound {152	/// Attach object context to value, if required153	pub fn evaluate(&self, sup_this: SupThis) -> Result<Val> {154		match self {155			Self::Unbound(v) => v.0.bind(sup_this),156			Self::Bound(v) => Ok(v.evaluate()?),157		}158	}159}160161cc_dyn!(CcContextInitializer, ContextInitializer);162163/// During import, this trait will be called to create initial context for file.164/// It may initialize global variables, stdlib for example.165pub trait ContextInitializer {166	/// For composability: extend builder. May panic if this initialization is not supported,167	/// and the context may only be created via `initialize`.168	fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder);169	/// Allows upcasting from abstract to concrete context initializer.170	/// jrsonnet by itself doesn't use this method, it is allowed for it to panic.171	fn as_any(&self) -> &dyn Any;172}173impl<T> ContextInitializer for &T174where175	T: ContextInitializer,176{177	fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder) {178		(*self).populate(for_file, builder);179	}180181	fn as_any(&self) -> &dyn Any {182		(*self).as_any()183	}184}185186/// Context initializer which adds nothing.187impl ContextInitializer for () {188	fn populate(&self, _for_file: Source, _builder: &mut InitialContextBuilder) {}189	fn as_any(&self) -> &dyn Any {190		self191	}192}193194impl<T> ContextInitializer for Option<T>195where196	T: ContextInitializer + 'static,197{198	fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder) {199		if let Some(ctx) = self {200			ctx.populate(for_file, builder);201		}202	}203204	fn as_any(&self) -> &dyn Any {205		self206	}207}208209macro_rules! impl_context_initializer {210	($($gen:ident)*) => {211		#[allow(non_snake_case)]212		impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {213			fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder) {214				let ($($gen,)*) = self;215				$($gen.populate(for_file.clone(), builder);)*216			}217			fn as_any(&self) -> &dyn Any {218				self219			}220		}221	};222	($($cur:ident)* @ $c:ident $($rest:ident)*) => {223		impl_context_initializer!($($cur)*);224		impl_context_initializer!($($cur)* $c @ $($rest)*);225	};226	($($cur:ident)* @) => {227		impl_context_initializer!($($cur)*);228	}229}230impl_context_initializer! {231	A @ B C D E F G232}233234#[derive(Trace)]235struct FileData {236	string: Option<IStr>,237	bytes: Option<IBytes>,238	parsed: Option<Rc<Expr>>,239	evaluated: Option<Val>,240241	evaluating: bool,242}243impl FileData {244	fn new_string(data: IStr) -> Self {245		Self {246			string: Some(data),247			bytes: None,248			parsed: None,249			evaluated: None,250			evaluating: false,251		}252	}253	fn new_bytes(data: IBytes) -> Self {254		Self {255			string: None,256			bytes: Some(data),257			parsed: None,258			evaluated: None,259			evaluating: false,260		}261	}262	pub(crate) fn get_string(&mut self) -> Option<IStr> {263		if self.string.is_none() {264			self.string = Some(265				self.bytes266					.as_ref()267					.expect("either string or bytes should be set")268					.clone()269					.cast_str()?,270			);271		}272		Some(self.string.clone().expect("just set"))273	}274}275276#[derive(Trace)]277pub struct EvaluationStateInternals {278	/// Internal state279	file_cache: RefCell<FxHashMap<SourcePath, FileData>>,280	/// Context initializer, which will be used for imports and everything281	/// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`282	context_initializer: CcContextInitializer,283	/// Used to resolve file locations/contents284	import_resolver: Rc<dyn ImportResolver>,285}286287/// Maintains stack trace and import resolution288#[derive(Clone, Trace)]289pub struct State(Cc<EvaluationStateInternals>);290291thread_local! {292	pub static DEFAULT_STATE: State = State::builder().build();293	pub static STATE: RefCell<Option<State>> = const {RefCell::new(None)};294}295pub struct StateEnterGuard(PhantomData<()>);296impl Drop for StateEnterGuard {297	fn drop(&mut self) {298		STATE.with_borrow_mut(|v| *v = None);299	}300}301302pub fn with_state<V>(v: impl FnOnce(State) -> V) -> V {303	if let Some(state) = STATE.with_borrow(Clone::clone) {304		v(state)305	} else {306		let s = DEFAULT_STATE.with(Clone::clone);307		v(s)308	}309}310311impl State {312	pub fn enter(&self) -> StateEnterGuard {313		self.try_enter().expect("entered state already exists")314	}315	pub fn try_enter(&self) -> Option<StateEnterGuard> {316		STATE.with_borrow_mut(|v| {317			if v.is_none() {318				*v = Some(self.clone());319				Some(StateEnterGuard(PhantomData))320			} else {321				None322			}323		})324	}325	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise326	pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {327		let mut file_cache = self.file_cache();328		let mut file = file_cache.entry(path.clone());329330		let file = match file {331			Entry::Occupied(ref mut d) => d.get_mut(),332			Entry::Vacant(v) => {333				let data = self.import_resolver().load_file_contents(&path)?;334				v.insert(FileData::new_string(335					std::str::from_utf8(&data)336						.map_err(|_| ImportBadFileUtf8(path.clone()))?337						.into(),338				))339			}340		};341		Ok(file342			.get_string()343			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?)344	}345	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise346	pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {347		let mut file_cache = self.file_cache();348		let mut file = file_cache.entry(path.clone());349350		let file = match file {351			Entry::Occupied(ref mut d) => d.get_mut(),352			Entry::Vacant(v) => {353				let data = self.import_resolver().load_file_contents(&path)?;354				v.insert(FileData::new_bytes(data.as_slice().into()))355			}356		};357		if let Some(str) = &file.bytes {358			return Ok(str.clone());359		}360		if file.bytes.is_none() {361			file.bytes = Some(362				file.string363					.as_ref()364					.expect("either string or bytes should be set")365					.clone()366					.cast_bytes(),367			);368		}369		Ok(file.bytes.as_ref().expect("just set").clone())370	}371	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise372	pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {373		let mut file_cache = self.file_cache();374		let mut file = file_cache.entry(path.clone());375376		let file = match file {377			Entry::Occupied(ref mut d) => d.get_mut(),378			Entry::Vacant(v) => {379				let data = self.import_resolver().load_file_contents(&path)?;380				v.insert(FileData::new_string(381					std::str::from_utf8(&data)382						.map_err(|_| ImportBadFileUtf8(path.clone()))?383						.into(),384				))385			}386		};387		if let Some(val) = &file.evaluated {388			return Ok(val.clone());389		}390		let code = file391			.get_string()392			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?;393		let file_name = Source::new(path.clone(), code.clone());394		if file.parsed.is_none() {395			file.parsed = Some(396				parse_jsonnet(&code, file_name.clone())397					.map(Rc::new)398					.map_err(|e| ImportSyntaxError {399						path: file_name.clone(),400						error: Box::new(e),401					})?,402			);403		}404		let parsed = file.parsed.as_ref().expect("just set").clone();405		if file.evaluating {406			bail!(InfiniteRecursionDetected)407		}408		file.evaluating = true;409		// Dropping file cache guard here, as evaluation may use this map too410		drop(file_cache);411		let (ctx, externals) = self.create_default_context(file_name.clone()).build();412		let report = analyze::analyze_root(&parsed, externals);413		if report.errored {414			return Err(StaticAnalysisError(report.diagnostics_list).into());415		}416		let res = evaluate::evaluate(ctx.build(), &report.lir);417418		let mut file_cache = self.file_cache();419		let mut file = file_cache.entry(path);420421		let Entry::Occupied(file) = &mut file else {422			unreachable!("this file was just here")423		};424		let file = file.get_mut();425		file.evaluating = false;426		match res {427			Ok(v) => {428				file.evaluated = Some(v.clone());429				Ok(v)430			}431			Err(e) => Err(e),432		}433	}434435	/// Has same semantics as `import 'path'` called from `from` file436	pub fn import_from(&self, from: &SourcePath, path: impl AsPathLike) -> Result<Val> {437		let resolved = self.resolve_from(from, &path)?;438		self.import_resolved(resolved)439	}440	pub fn import(&self, path: impl AsPathLike) -> Result<Val> {441		let resolved = self.resolve_from_default(&path)?;442		self.import_resolved(resolved)443	}444445	/// Creates context with all passed global variables446	pub fn create_default_context(&self, source: Source) -> InitialContextBuilder {447		self.create_default_context_with(source, &())448	}449450	/// Creates context with all passed global variables, calling custom modifier451	pub fn create_default_context_with(452		&self,453		source: Source,454		context_initializer: &dyn ContextInitializer,455	) -> InitialContextBuilder {456		let default_initializer = self.context_initializer();457		let mut builder = InitialContextBuilder::new();458		default_initializer.populate(source.clone(), &mut builder);459		context_initializer.populate(source, &mut builder);460461		builder462	}463}464465/// Internals466impl State {467	fn file_cache(&self) -> RefMut<'_, FxHashMap<SourcePath, FileData>> {468		self.0.file_cache.borrow_mut()469	}470}471/// Executes code creating a new stack frame, to be replaced with try{}472pub fn in_frame<T>(473	e: CallLocation<'_>,474	frame_desc: impl FnOnce() -> String,475	f: impl FnOnce() -> Result<T>,476) -> Result<T> {477	let _guard = check_depth()?;478479	f().with_description_src(e, frame_desc)480}481482/// Executes code creating a new stack frame, to be replaced with try{}483pub fn in_description_frame<T>(484	frame_desc: impl FnOnce() -> String,485	f: impl FnOnce() -> Result<T>,486) -> Result<T> {487	let _guard = check_depth()?;488489	f().with_description(frame_desc)490}491492#[derive(Trace)]493pub struct InitialUnderscore(pub Thunk<Val>);494impl ContextInitializer for InitialUnderscore {495	fn populate(&self, _for_file: Source, builder: &mut InitialContextBuilder) {496		builder.bind("_", self.0.clone());497	}498499	fn as_any(&self) -> &dyn Any {500		self501	}502}503504/// Raw methods evaluate passed values but don't perform TLA execution505impl State {506	/// Parses and evaluates the given snippet507	pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {508		self.evaluate_snippet_with(name, code, &())509	}510	/// Parses and evaluates the given snippet with custom context modifier511	pub fn evaluate_snippet_with(512		&self,513		name: impl Into<IStr>,514		code: impl Into<IStr>,515		context_initializer: &dyn ContextInitializer,516	) -> Result<Val> {517		let code = code.into();518		let source = Source::new_virtual(name.into(), code.clone());519		let parsed = parse_jsonnet(&code, source.clone()).map_err(|e| ImportSyntaxError {520			path: source.clone(),521			error: Box::new(e),522		})?;523		let (ctx, externals) = self524			.create_default_context_with(source.clone(), context_initializer)525			.build();526		let report = analyze::analyze_root(&parsed, externals);527		if report.errored {528			return Err(StaticAnalysisError(report.diagnostics_list).into());529		}530		evaluate::evaluate(ctx.build(), &report.lir)531	}532}533534/// Settings utilities535impl State {536	// Only panics in case of [`ImportResolver`] contract violation537	#[allow(clippy::missing_panics_doc)]538	pub fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {539		self.import_resolver().resolve_from(from, path)540	}541	#[allow(clippy::missing_panics_doc)]542	pub fn resolve_from_default(&self, path: &dyn AsPathLike) -> Result<SourcePath> {543		self.import_resolver().resolve_from_default(path)544	}545	pub fn import_resolver(&self) -> &dyn ImportResolver {546		&*self.0.import_resolver547	}548	pub fn context_initializer(&self) -> &dyn ContextInitializer {549		&*self.0.context_initializer.0550	}551}552553impl State {554	pub fn builder() -> StateBuilder {555		StateBuilder::default()556	}557}558559impl Default for State {560	fn default() -> Self {561		Self::builder().build()562	}563}564565#[derive(Default)]566pub struct StateBuilder {567	import_resolver: Option<Rc<dyn ImportResolver>>,568	context_initializer: Option<CcContextInitializer>,569}570impl StateBuilder {571	pub fn import_resolver(&mut self, import_resolver: impl ImportResolver) -> &mut Self {572		let _ = self.import_resolver.insert(Rc::new(import_resolver));573		self574	}575	pub fn context_initializer(576		&mut self,577		context_initializer: impl ContextInitializer + Trace,578	) -> &mut Self {579		let _ = self580			.context_initializer581			.insert(CcContextInitializer::new(context_initializer));582		self583	}584	pub fn build(mut self) -> State {585		State(Cc::new(EvaluationStateInternals {586			file_cache: RefCell::new(FxHashMap::new()),587			context_initializer: self588				.context_initializer589				.take()590				.unwrap_or_else(|| CcContextInitializer::new(())),591			import_resolver: self592				.import_resolver593				.take()594				.unwrap_or_else(|| Rc::new(DummyImportResolver)),595		}))596	}597}