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

difftreelog

source

crates/jrsonnet-evaluator/src/lib.rs16.3 KiBsourcehistory
1#![warn(clippy::all, clippy::nursery, clippy::pedantic)]2#![allow(3	macro_expanded_macro_exports_accessed_by_absolute_paths,4	clippy::ptr_arg,5	// Too verbose6	clippy::must_use_candidate,7	// A lot of functions pass around errors thrown by code8	clippy::missing_errors_doc,9	// A lot of pointers have interior Rc10	clippy::needless_pass_by_value,11	// Its fine12	clippy::wildcard_imports,13	clippy::enum_glob_use,14	clippy::module_name_repetitions,15	// TODO: fix individual issues, however this works as intended almost everywhere16	clippy::cast_precision_loss,17	clippy::cast_possible_wrap,18	clippy::cast_possible_truncation,19	clippy::cast_sign_loss,20)]2122// For jrsonnet-macros23extern crate self as jrsonnet_evaluator;2425mod builtin;26mod ctx;27mod dynamic;28pub mod error;29mod evaluate;30pub mod function;31pub mod gc;32mod import;33mod integrations;34mod map;35pub mod native;36mod obj;37pub mod trace;38pub mod typed;39pub mod val;4041use std::{42	cell::{Ref, RefCell, RefMut},43	collections::HashMap,44	fmt::Debug,45	path::{Path, PathBuf},46	rc::Rc,47};4849pub use ctx::*;50pub use dynamic::*;51use error::{Error::*, LocError, Result, StackTraceElement};52pub use evaluate::*;53use function::{Builtin, CallLocation, TlaArg};54use gc::{GcHashMap, TraceBox};55use gcmodule::{Cc, Trace, Weak};56pub use import::*;57pub use jrsonnet_interner::IStr;58pub use jrsonnet_parser as parser;59use jrsonnet_parser::*;60pub use obj::*;61use trace::{location_to_offset, offset_to_location, CodeLocation, CompactFormat, TraceFormat};62pub use val::{LazyVal, ManifestFormat, Val};6364pub trait Bindable: Trace + 'static {65	fn bind(66		&self,67		s: State,68		this: Option<ObjValue>,69		super_obj: Option<ObjValue>,70	) -> Result<LazyVal>;71}7273#[derive(Clone, Trace)]74pub enum LazyBinding {75	Bindable(Cc<TraceBox<dyn Bindable>>),76	Bound(LazyVal),77}7879impl Debug for LazyBinding {80	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {81		write!(f, "LazyBinding")82	}83}84impl LazyBinding {85	pub fn evaluate(86		&self,87		s: State,88		this: Option<ObjValue>,89		super_obj: Option<ObjValue>,90	) -> Result<LazyVal> {91		match self {92			Self::Bindable(v) => v.bind(s, this, super_obj),93			Self::Bound(v) => Ok(v.clone()),94		}95	}96}9798pub struct EvaluationSettings {99	/// Limits recursion by limiting the number of stack frames100	pub max_stack: usize,101	/// Limits amount of stack trace items preserved102	pub max_trace: usize,103	/// Used for s`td.extVar`104	pub ext_vars: HashMap<IStr, Val>,105	/// Used for ext.native106	pub ext_natives: HashMap<IStr, Cc<TraceBox<dyn Builtin>>>,107	/// TLA vars108	pub tla_vars: HashMap<IStr, TlaArg>,109	/// Global variables are inserted in default context110	pub globals: HashMap<IStr, Val>,111	/// Used to resolve file locations/contents112	pub import_resolver: Box<dyn ImportResolver>,113	/// Used in manifestification functions114	pub manifest_format: ManifestFormat,115	/// Used for bindings116	pub trace_format: Box<dyn TraceFormat>,117}118impl Default for EvaluationSettings {119	fn default() -> Self {120		Self {121			max_stack: 200,122			max_trace: 20,123			globals: HashMap::default(),124			ext_vars: HashMap::default(),125			ext_natives: HashMap::default(),126			tla_vars: HashMap::default(),127			import_resolver: Box::new(DummyImportResolver),128			manifest_format: ManifestFormat::Json {129				padding: 4,130				#[cfg(feature = "exp-preserve-order")]131				preserve_order: false,132			},133			trace_format: Box::new(CompactFormat {134				padding: 4,135				resolver: trace::PathResolver::Absolute,136			}),137		}138	}139}140141#[derive(Default)]142struct EvaluationData {143	/// Used for stack overflow detection, stacktrace is populated on unwind144	stack_depth: usize,145	/// Updated every time stack entry is popt146	stack_generation: usize,147148	breakpoints: Breakpoints,149	/// Contains file source codes and evaluation results for imports and pretty-printed stacktraces150	files: GcHashMap<Rc<Path>, FileData>,151	str_files: GcHashMap<Rc<Path>, IStr>,152	bin_files: GcHashMap<Rc<Path>, Rc<[u8]>>,153}154155pub struct FileData {156	source_code: IStr,157	parsed: LocExpr,158	evaluated: Option<Val>,159}160161#[allow(clippy::type_complexity)]162pub struct Breakpoint {163	loc: ExprLocation,164	collected: RefCell<HashMap<usize, (usize, Vec<Result<Val>>)>>,165}166#[derive(Default)]167struct Breakpoints(Vec<Rc<Breakpoint>>);168impl Breakpoints {169	fn insert(170		&self,171		stack_depth: usize,172		stack_generation: usize,173		loc: &ExprLocation,174		result: Result<Val>,175	) -> Result<Val> {176		if self.0.is_empty() {177			return result;178		}179		for item in &self.0 {180			if item.loc.belongs_to(loc) {181				let mut collected = item.collected.borrow_mut();182				let (depth, vals) = collected.entry(stack_generation).or_default();183				if stack_depth > *depth {184					vals.clear();185				}186				vals.push(result.clone());187			}188		}189		result190	}191}192193#[derive(Default)]194pub struct EvaluationStateInternals {195	/// Internal state196	data: RefCell<EvaluationData>,197	/// Settings, safe to change at runtime198	settings: RefCell<EvaluationSettings>,199}200201/// Maintains stack trace and import resolution202#[derive(Default, Clone)]203pub struct State(Rc<EvaluationStateInternals>);204205impl State {206	/// Parses and adds file as loaded207	pub fn add_file(&self, path: Rc<Path>, source_code: IStr) -> Result<LocExpr> {208		let parsed = parse(209			&source_code,210			&ParserSettings {211				file_name: path.clone(),212			},213		)214		.map_err(|error| ImportSyntaxError {215			error: Box::new(error),216			path: path.clone(),217			source_code: source_code.clone(),218		})?;219		self.add_parsed_file(path, source_code, parsed.clone())?;220221		Ok(parsed)222	}223224	pub fn reset_evaluation_state(&self, name: &Path) {225		self.data_mut()226			.files227			.get_mut(name)228			.expect("file not found")229			.evaluated230			.take();231	}232233	/// Adds file by source code and parsed expr234	pub fn add_parsed_file(235		&self,236		name: Rc<Path>,237		source_code: IStr,238		parsed: LocExpr,239	) -> Result<()> {240		self.data_mut().files.insert(241			name,242			FileData {243				source_code,244				parsed,245				evaluated: None,246			},247		);248249		Ok(())250	}251	pub fn get_source(&self, name: &Path) -> Option<IStr> {252		let ro_map = &self.data().files;253		ro_map.get(name).map(|value| value.source_code.clone())254	}255	pub fn map_source_locations(&self, file: &Path, locs: &[usize]) -> Vec<CodeLocation> {256		offset_to_location(&self.get_source(file).unwrap_or_else(|| "".into()), locs)257	}258	pub fn map_from_source_location(259		&self,260		file: &Path,261		line: usize,262		column: usize,263	) -> Option<usize> {264		location_to_offset(265			&self.get_source(file).expect("file not found"),266			line,267			column,268		)269	}270	pub fn import_file(&self, from: &Path, path: &Path) -> Result<Val> {271		let file_path = self.resolve_file(from, path)?;272		{273			let data = self.data();274			let files = &data.files;275			if files.contains_key(&file_path as &Path) {276				drop(data);277				return self.evaluate_loaded_file_raw(&file_path);278			}279		}280		let contents = self.load_file_str(&file_path)?;281		self.add_file(file_path.clone(), contents)?;282		self.evaluate_loaded_file_raw(&file_path)283	}284	pub(crate) fn import_file_str(&self, from: &Path, path: &Path) -> Result<IStr> {285		let path = self.resolve_file(from, path)?;286		if !self.data().str_files.contains_key(&path) {287			let file_str = self.load_file_str(&path)?;288			self.data_mut().str_files.insert(path.clone(), file_str);289		}290		Ok(self.data().str_files.get(&path).cloned().unwrap())291	}292	pub(crate) fn import_file_bin(&self, from: &Path, path: &Path) -> Result<Rc<[u8]>> {293		let path = self.resolve_file(from, path)?;294		if !self.data().bin_files.contains_key(&path) {295			let file_bin = self.load_file_bin(&path)?;296			self.data_mut().bin_files.insert(path.clone(), file_bin);297		}298		Ok(self.data().bin_files.get(&path).cloned().unwrap())299	}300301	fn evaluate_loaded_file_raw(&self, name: &Path) -> Result<Val> {302		let expr: LocExpr = {303			let ro_map = &self.data().files;304			let value = ro_map305				.get(name)306				.unwrap_or_else(|| panic!("file not added: {:?}", name));307			if let Some(ref evaluated) = value.evaluated {308				return Ok(evaluated.clone());309			}310			value.parsed.clone()311		};312		let value = evaluate(self.clone(), self.create_default_context(), &expr)?;313		{314			self.data_mut()315				.files316				.get_mut(name)317				.unwrap()318				.evaluated319				.replace(value.clone());320		}321		Ok(value)322	}323324	/// Adds standard library global variable (std) to this evaluator325	pub fn with_stdlib(&self) -> &Self {326		use jrsonnet_stdlib::STDLIB_STR;327		let std_path: Rc<Path> = PathBuf::from("std.jsonnet").into();328329		self.add_parsed_file(330			std_path.clone(),331			STDLIB_STR.to_owned().into(),332			builtin::get_parsed_stdlib(),333		)334		.expect("stdlib is correct");335		let val = self336			.evaluate_loaded_file_raw(&std_path)337			.expect("stdlib is correct");338		self.settings_mut().globals.insert("std".into(), val);339		self340	}341342	/// Creates context with all passed global variables343	pub fn create_default_context(&self) -> Context {344		let globals = &self.settings().globals;345		let mut new_bindings = GcHashMap::with_capacity(globals.len());346		for (name, value) in globals.iter() {347			new_bindings.insert(name.clone(), LazyVal::new_resolved(value.clone()));348		}349		Context::new().extend_bound(new_bindings)350	}351352	/// Executes code creating a new stack frame353	pub fn push<T>(354		&self,355		e: CallLocation,356		frame_desc: impl FnOnce() -> String,357		f: impl FnOnce() -> Result<T>,358	) -> Result<T> {359		{360			let mut data = self.data_mut();361			let stack_depth = &mut data.stack_depth;362			if *stack_depth > self.max_stack() {363				// Error creation uses data, so i drop guard here364				drop(data);365				throw!(StackOverflow);366			}367			*stack_depth += 1;368		}369		let result = f();370		{371			let mut data = self.data_mut();372			data.stack_depth -= 1;373			data.stack_generation += 1;374		}375		if let Err(mut err) = result {376			err.trace_mut().0.push(StackTraceElement {377				location: e.0.cloned(),378				desc: frame_desc(),379			});380			return Err(err);381		}382		result383	}384385	/// Executes code creating a new stack frame386	pub fn push_val(387		&self,388		e: &ExprLocation,389		frame_desc: impl FnOnce() -> String,390		f: impl FnOnce() -> Result<Val>,391	) -> Result<Val> {392		{393			let mut data = self.data_mut();394			let stack_depth = &mut data.stack_depth;395			if *stack_depth > self.max_stack() {396				// Error creation uses data, so i drop guard here397				drop(data);398				throw!(StackOverflow);399			}400			*stack_depth += 1;401		}402		let mut result = f();403		{404			let mut data = self.data_mut();405			data.stack_depth -= 1;406			data.stack_generation += 1;407			result = data408				.breakpoints409				.insert(data.stack_depth, data.stack_generation, e, result);410		}411		if let Err(mut err) = result {412			err.trace_mut().0.push(StackTraceElement {413				location: Some(e.clone()),414				desc: frame_desc(),415			});416			return Err(err);417		}418		result419	}420	/// Executes code creating a new stack frame421	pub fn push_description<T>(422		&self,423		frame_desc: impl FnOnce() -> String,424		f: impl FnOnce() -> Result<T>,425	) -> Result<T> {426		{427			let mut data = self.data_mut();428			let stack_depth = &mut data.stack_depth;429			if *stack_depth > self.max_stack() {430				// Error creation uses data, so i drop guard here431				drop(data);432				throw!(StackOverflow);433			}434			*stack_depth += 1;435		}436		let result = f();437		{438			let mut data = self.data_mut();439			data.stack_depth -= 1;440			data.stack_generation += 1;441		}442		if let Err(mut err) = result {443			err.trace_mut().0.push(StackTraceElement {444				location: None,445				desc: frame_desc(),446			});447			return Err(err);448		}449		result450	}451452	/// # Panics453	/// In case of formatting failure454	pub fn stringify_err(&self, e: &LocError) -> String {455		let mut out = String::new();456		self.settings()457			.trace_format458			.write_trace(&mut out, self, e)459			.unwrap();460		out461	}462463	pub fn manifest(&self, val: Val) -> Result<IStr> {464		self.push_description(465			|| "manifestification".to_string(),466			|| val.manifest(self.clone(), &self.manifest_format()),467		)468	}469	pub fn manifest_multi(&self, val: Val) -> Result<Vec<(IStr, IStr)>> {470		val.manifest_multi(self.clone(), &self.manifest_format())471	}472	pub fn manifest_stream(&self, val: Val) -> Result<Vec<IStr>> {473		val.manifest_stream(self.clone(), &self.manifest_format())474	}475476	/// If passed value is function then call with set TLA477	pub fn with_tla(&self, val: Val) -> Result<Val> {478		Ok(match val {479			Val::Func(func) => self.push_description(480				|| "during TLA call".to_owned(),481				|| {482					func.evaluate(483						self.clone(),484						self.create_default_context(),485						CallLocation::native(),486						&self.settings().tla_vars,487						true,488					)489				},490			)?,491			v => v,492		})493	}494}495496/// Internals497impl State {498	fn data(&self) -> Ref<EvaluationData> {499		self.0.data.borrow()500	}501	fn data_mut(&self) -> RefMut<EvaluationData> {502		self.0.data.borrow_mut()503	}504	pub fn settings(&self) -> Ref<EvaluationSettings> {505		self.0.settings.borrow()506	}507	pub fn settings_mut(&self) -> RefMut<EvaluationSettings> {508		self.0.settings.borrow_mut()509	}510}511512/// Raw methods evaluate passed values but don't perform TLA execution513impl State {514	pub fn evaluate_file_raw(&self, name: &Path) -> Result<Val> {515		self.import_file(&std::env::current_dir().expect("cwd"), name)516	}517	pub fn evaluate_file_raw_nocwd(&self, name: &Path) -> Result<Val> {518		self.import_file(&PathBuf::from("."), name)519	}520	/// Parses and evaluates the given snippet521	pub fn evaluate_snippet_raw(&self, source: Rc<Path>, code: IStr) -> Result<Val> {522		let parsed = parse(523			&code,524			&ParserSettings {525				file_name: source.clone(),526			},527		)528		.map_err(|e| ImportSyntaxError {529			path: source.clone(),530			source_code: code.clone(),531			error: Box::new(e),532		})?;533		self.add_parsed_file(source, code, parsed.clone())?;534		self.evaluate_expr_raw(parsed)535	}536	/// Evaluates the parsed expression537	pub fn evaluate_expr_raw(&self, code: LocExpr) -> Result<Val> {538		evaluate(self.clone(), self.create_default_context(), &code)539	}540}541542/// Settings utilities543impl State {544	pub fn add_ext_var(&self, name: IStr, value: Val) {545		self.settings_mut().ext_vars.insert(name, value);546	}547	pub fn add_ext_str(&self, name: IStr, value: IStr) {548		self.add_ext_var(name, Val::Str(value));549	}550	pub fn add_ext_code(&self, name: IStr, code: IStr) -> Result<()> {551		let value =552			self.evaluate_snippet_raw(PathBuf::from(format!("ext_code {}", name)).into(), code)?;553		self.add_ext_var(name, value);554		Ok(())555	}556557	pub fn add_tla(&self, name: IStr, value: Val) {558		self.settings_mut()559			.tla_vars560			.insert(name, TlaArg::Val(value));561	}562	pub fn add_tla_str(&self, name: IStr, value: IStr) {563		self.settings_mut()564			.tla_vars565			.insert(name, TlaArg::String(value));566	}567	pub fn add_tla_code(&self, name: IStr, code: IStr) -> Result<()> {568		let parsed = self.add_file(PathBuf::from(format!("tla_code {}", name)).into(), code)?;569		self.settings_mut()570			.tla_vars571			.insert(name, TlaArg::Code(parsed));572		Ok(())573	}574575	pub fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {576		self.settings().import_resolver.resolve_file(from, path)577	}578	pub fn load_file_str(&self, path: &Path) -> Result<IStr> {579		self.settings().import_resolver.load_file_str(path)580	}581	pub fn load_file_bin(&self, path: &Path) -> Result<Rc<[u8]>> {582		self.settings().import_resolver.load_file_bin(path)583	}584585	pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {586		Ref::map(self.settings(), |s| &*s.import_resolver)587	}588	pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {589		self.settings_mut().import_resolver = resolver;590	}591592	pub fn add_native(&self, name: IStr, cb: Cc<TraceBox<dyn Builtin>>) {593		self.settings_mut().ext_natives.insert(name, cb);594	}595596	pub fn manifest_format(&self) -> ManifestFormat {597		self.settings().manifest_format.clone()598	}599	pub fn set_manifest_format(&self, format: ManifestFormat) {600		self.settings_mut().manifest_format = format;601	}602603	pub fn trace_format(&self) -> Ref<dyn TraceFormat> {604		Ref::map(self.settings(), |s| &*s.trace_format)605	}606	pub fn set_trace_format(&self, format: Box<dyn TraceFormat>) {607		self.settings_mut().trace_format = format;608	}609610	pub fn max_trace(&self) -> usize {611		self.settings().max_trace612	}613	pub fn set_max_trace(&self, trace: usize) {614		self.settings_mut().max_trace = trace;615	}616617	pub fn max_stack(&self) -> usize {618		self.settings().max_stack619	}620	pub fn set_max_stack(&self, trace: usize) {621		self.settings_mut().max_stack = trace;622	}623}624625pub fn cc_ptr_eq<T>(a: &Cc<T>, b: &Cc<T>) -> bool {626	let a = a as &T;627	let b = b as &T;628	std::ptr::eq(a, b)629}630631fn weak_raw<T>(a: Weak<T>) -> *const () {632	unsafe { std::mem::transmute(a) }633}634fn weak_ptr_eq<T>(a: Weak<T>, b: Weak<T>) -> bool {635	std::ptr::eq(weak_raw(a), weak_raw(b))636}637638#[test]639fn weak_unsafe() {640	let a = Cc::new(1);641	let b = Cc::new(2);642643	let aw1 = a.clone().downgrade();644	let aw2 = a.clone().downgrade();645	let aw3 = a.clone().downgrade();646647	let bw = b.clone().downgrade();648649	assert!(weak_ptr_eq(aw1, aw2));650	assert!(!weak_ptr_eq(aw3, bw));651}