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

difftreelog

refactor receive Path in public api arguments

Yaroslav Bolyukin2021-06-14parent: #ad64d8a.patch.diff
in: master
Fixes #39
Fixes #47

14 files changed

modifiedbindings/jsonnet/src/import.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/import.rs
+++ b/bindings/jsonnet/src/import.rs
@@ -12,7 +12,7 @@
 	fs::File,
 	io::Read,
 	os::raw::{c_char, c_int},
-	path::PathBuf,
+	path::{Path, PathBuf},
 	ptr::null_mut,
 	rc::Rc,
 };
@@ -33,7 +33,7 @@
 	out: RefCell<HashMap<PathBuf, IStr>>,
 }
 impl ImportResolver for CallbackImportResolver {
-	fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {
+	fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
 		let base = CString::new(from.to_str().unwrap()).unwrap().into_raw();
 		let rel = CString::new(path.to_str().unwrap()).unwrap().into_raw();
 		let found_here: *mut c_char = null_mut();
@@ -73,9 +73,9 @@
 			unsafe { CString::from_raw(result_ptr) };
 		}
 
-		Ok(Rc::new(found_here_buf))
+		Ok(found_here_buf.into())
 	}
-	fn load_file_contents(&self, resolved: &PathBuf) -> Result<IStr> {
+	fn load_file_contents(&self, resolved: &Path) -> Result<IStr> {
 		Ok(self.out.borrow().get(resolved).unwrap().clone())
 	}
 	unsafe fn as_any(&self) -> &dyn Any {
@@ -108,27 +108,27 @@
 	}
 }
 impl ImportResolver for NativeImportResolver {
-	fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {
-		let mut new_path = from.clone();
+	fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
+		let mut new_path = from.to_owned();
 		new_path.push(path);
 		if new_path.exists() {
-			Ok(Rc::new(new_path))
+			Ok(new_path.into())
 		} else {
 			for library_path in self.library_paths.borrow().iter() {
 				let mut cloned = library_path.clone();
 				cloned.push(path);
 				if cloned.exists() {
-					return Ok(Rc::new(cloned));
+					return Ok(cloned.into());
 				}
 			}
-			throw!(ImportFileNotFound(from.clone(), path.clone()))
+			throw!(ImportFileNotFound(from.to_owned(), path.to_owned()))
 		}
 	}
-	fn load_file_contents(&self, id: &PathBuf) -> Result<IStr> {
-		let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.clone()))?;
+	fn load_file_contents(&self, id: &Path) -> Result<IStr> {
+		let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.to_owned()))?;
 		let mut out = String::new();
 		file.read_to_string(&mut out)
-			.map_err(|_e| ImportBadFileUtf8(id.clone()))?;
+			.map_err(|_e| ImportBadFileUtf8(id.to_owned()))?;
 		Ok(out.into())
 	}
 	unsafe fn as_any(&self) -> &dyn Any {
modifiedbindings/jsonnet/src/lib.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -15,7 +15,6 @@
 	ffi::{CStr, CString},
 	os::raw::{c_char, c_double, c_int, c_uint},
 	path::PathBuf,
-	rc::Rc,
 };
 
 /// WASM stub
@@ -144,7 +143,7 @@
 		let snippet = CStr::from_ptr(snippet);
 		match vm
 			.evaluate_snippet_raw(
-				Rc::new(PathBuf::from(filename.to_str().unwrap())),
+				PathBuf::from(filename.to_str().unwrap()).into(),
 				snippet.to_str().unwrap().into(),
 			)
 			.and_then(|v| vm.with_tla(v))
@@ -220,7 +219,7 @@
 		let snippet = CStr::from_ptr(snippet);
 		match vm
 			.evaluate_snippet_raw(
-				Rc::new(PathBuf::from(filename.to_str().unwrap())),
+				PathBuf::from(filename.to_str().unwrap()).into(),
 				snippet.to_str().unwrap().into(),
 			)
 			.and_then(|v| vm.with_tla(v))
@@ -294,7 +293,7 @@
 		let snippet = CStr::from_ptr(snippet);
 		match vm
 			.evaluate_snippet_raw(
-				Rc::new(PathBuf::from(filename.to_str().unwrap())),
+				PathBuf::from(filename.to_str().unwrap()).into(),
 				snippet.to_str().unwrap().into(),
 			)
 			.and_then(|v| vm.with_tla(v))
modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -6,7 +6,6 @@
 	io::Read,
 	io::Write,
 	path::PathBuf,
-	rc::Rc,
 	str::FromStr,
 };
 
@@ -134,14 +133,14 @@
 	let val = if opts.input.exec {
 		state.set_manifest_format(ManifestFormat::ToString);
 		state.evaluate_snippet_raw(
-			Rc::new(PathBuf::from("args")),
+			PathBuf::from("args").into(),
 			(&opts.input.input as &str).into(),
 		)?
 	} else if opts.input.input == "-" {
 		let mut input = Vec::new();
 		std::io::stdin().read_to_end(&mut input)?;
 		let input_str = std::str::from_utf8(&input)?.into();
-		state.evaluate_snippet_raw(Rc::new(PathBuf::from("<stdin>")), input_str)?
+		state.evaluate_snippet_raw(PathBuf::from("<stdin>").into(), input_str)?
 	} else {
 		state.evaluate_file_raw(&PathBuf::from(opts.input.input))?
 	};
modifiedcrates/jrsonnet-evaluator/build.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/build.rs
+++ b/crates/jrsonnet-evaluator/build.rs
@@ -15,7 +15,7 @@
 	let parsed = parse(
 		STDLIB_STR,
 		&ParserSettings {
-			file_name: Rc::new(PathBuf::from("std.jsonnet")),
+			file_name: PathBuf::from("std.jsonnet").into(),
 			loc_data: true,
 		},
 	)
modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -174,7 +174,7 @@
 		0, s: ty!(string) => Val::Str;
 	], {
 		let state = EvaluationState::default();
-		let path = Rc::new(PathBuf::from("std.parseJson"));
+		let path = PathBuf::from("std.parseJson").into();
 		state.evaluate_snippet_raw(path ,s)
 	})
 }
modifiedcrates/jrsonnet-evaluator/src/builtin/stdlib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/stdlib.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/stdlib.rs
@@ -1,5 +1,5 @@
 use jrsonnet_parser::{LocExpr, ParserSettings};
-use std::{path::PathBuf, rc::Rc};
+use std::path::PathBuf;
 
 thread_local! {
 	/// To avoid parsing again when issued from the same thread
@@ -25,7 +25,7 @@
 			jrsonnet_stdlib::STDLIB_STR,
 			&ParserSettings {
 				loc_data: true,
-				file_name: Rc::new(PathBuf::from("std.jsonnet")),
+				file_name: PathBuf::from("std.jsonnet").into(),
 			},
 		)
 		.unwrap()
modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -5,7 +5,10 @@
 use jrsonnet_interner::IStr;
 use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};
 use jrsonnet_types::ValType;
-use std::{path::PathBuf, rc::Rc};
+use std::{
+	path::{Path, PathBuf},
+	rc::Rc,
+};
 use thiserror::Error;
 
 #[derive(Error, Debug, Clone)]
@@ -86,7 +89,7 @@
 		.source_code.chars().nth(error.location.offset).map(|c| c.to_string()).unwrap_or_else(|| "EOF".into())
 	)]
 	ImportSyntaxError {
-		path: Rc<PathBuf>,
+		path: Rc<Path>,
 		source_code: IStr,
 		error: Box<jrsonnet_parser::ParseError>,
 	},
modifiedcrates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate.rs
@@ -609,26 +609,26 @@
 			}
 		}
 		Import(path) => {
-			let mut tmp = loc
+			let tmp = loc
 				.clone()
 				.expect("imports cannot be used without loc_data")
 				.0;
-			let import_location = Rc::make_mut(&mut tmp);
+			let mut import_location = tmp.to_path_buf();
 			import_location.pop();
 			push(
 				loc.as_ref(),
 				|| format!("import {:?}", path),
-				|| with_state(|s| s.import_file(import_location, path)),
+				|| with_state(|s| s.import_file(&import_location, path)),
 			)?
 		}
 		ImportStr(path) => {
-			let mut tmp = loc
+			let tmp = loc
 				.clone()
 				.expect("imports cannot be used without loc_data")
 				.0;
-			let import_location = Rc::make_mut(&mut tmp);
+			let mut import_location = tmp.to_path_buf();
 			import_location.pop();
-			Val::Str(with_state(|s| s.import_file_str(import_location, path))?)
+			Val::Str(with_state(|s| s.import_file_str(&import_location, path))?)
 		}
 	})
 }
modifiedcrates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -6,17 +6,23 @@
 use jrsonnet_interner::IStr;
 use std::fs;
 use std::io::Read;
-use std::{any::Any, cell::RefCell, collections::HashMap, path::PathBuf, rc::Rc};
+use std::{
+	any::Any,
+	cell::RefCell,
+	collections::HashMap,
+	path::{Path, PathBuf},
+	rc::Rc,
+};
 
 /// Implements file resolution logic for `import` and `importStr`
 pub trait ImportResolver {
 	/// Resolves real file path, e.g. `(/home/user/manifests, b.libjsonnet)` can correspond
 	/// both to `/home/user/manifests/b.libjsonnet` and to `/home/user/${vendor}/b.libjsonnet`
 	/// where `${vendor}` is a library path.
-	fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>>;
+	fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>>;
 
 	/// Reads file from filesystem, should be used only with path received from `resolve_file`
-	fn load_file_contents(&self, resolved: &PathBuf) -> Result<IStr>;
+	fn load_file_contents(&self, resolved: &Path) -> Result<IStr>;
 
 	/// # Safety
 	///
@@ -29,11 +35,11 @@
 /// Dummy resolver, can't resolve/load any file
 pub struct DummyImportResolver;
 impl ImportResolver for DummyImportResolver {
-	fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {
-		throw!(ImportNotSupported(from.clone(), path.clone()))
+	fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
+		throw!(ImportNotSupported(from.into(), path.into()))
 	}
 
-	fn load_file_contents(&self, _resolved: &PathBuf) -> Result<IStr> {
+	fn load_file_contents(&self, _resolved: &Path) -> Result<IStr> {
 		// Can be only caused by library direct consumer, not by supplied jsonnet
 		panic!("dummy resolver can't load any file")
 	}
@@ -57,27 +63,27 @@
 	pub library_paths: Vec<PathBuf>,
 }
 impl ImportResolver for FileImportResolver {
-	fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {
-		let mut new_path = from.clone();
-		new_path.push(path);
-		if new_path.exists() {
-			Ok(Rc::new(new_path))
+	fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
+		let mut direct = from.to_path_buf();
+		direct.push(path);
+		if direct.exists() {
+			Ok(direct.into())
 		} else {
 			for library_path in self.library_paths.iter() {
 				let mut cloned = library_path.clone();
 				cloned.push(path);
 				if cloned.exists() {
-					return Ok(Rc::new(cloned));
+					return Ok(cloned.into());
 				}
 			}
-			throw!(ImportFileNotFound(from.clone(), path.clone()))
+			throw!(ImportFileNotFound(from.to_owned(), path.to_owned()))
 		}
 	}
-	fn load_file_contents(&self, id: &PathBuf) -> Result<IStr> {
-		let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.clone()))?;
+	fn load_file_contents(&self, id: &Path) -> Result<IStr> {
+		let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.to_owned()))?;
 		let mut out = String::new();
 		file.read_to_string(&mut out)
-			.map_err(|_e| ImportBadFileUtf8(id.clone()))?;
+			.map_err(|_e| ImportBadFileUtf8(id.to_owned()))?;
 		Ok(out.into())
 	}
 	unsafe fn as_any(&self) -> &dyn Any {
@@ -89,23 +95,23 @@
 
 /// Caches results of the underlying resolver
 pub struct CachingImportResolver {
-	resolution_cache: RefCell<HashMap<ResolutionData, Result<Rc<PathBuf>>>>,
+	resolution_cache: RefCell<HashMap<ResolutionData, Result<Rc<Path>>>>,
 	loading_cache: RefCell<HashMap<PathBuf, Result<IStr>>>,
 	inner: Box<dyn ImportResolver>,
 }
 impl ImportResolver for CachingImportResolver {
-	fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {
+	fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
 		self.resolution_cache
 			.borrow_mut()
-			.entry((from.clone(), path.clone()))
+			.entry((from.to_owned(), path.to_owned()))
 			.or_insert_with(|| self.inner.resolve_file(from, path))
 			.clone()
 	}
 
-	fn load_file_contents(&self, resolved: &PathBuf) -> Result<IStr> {
+	fn load_file_contents(&self, resolved: &Path) -> Result<IStr> {
 		self.loading_cache
 			.borrow_mut()
-			.entry(resolved.clone())
+			.entry(resolved.to_owned())
 			.or_insert_with(|| self.inner.load_file_contents(resolved))
 			.clone()
 	}
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/lib.rs
1#![cfg_attr(feature = "unstable", feature(stmt_expr_attributes))]2#![warn(clippy::all, clippy::nursery)]3#![allow(4	macro_expanded_macro_exports_accessed_by_absolute_paths,5	clippy::ptr_arg6)]78mod builtin;9mod ctx;10mod dynamic;11pub mod error;12mod evaluate;13mod function;14mod import;15mod integrations;16mod map;17pub mod native;18mod obj;19pub mod trace;20pub mod typed;21mod val;2223pub use ctx::*;24pub use dynamic::*;25use error::{Error::*, LocError, Result, StackTraceElement};26pub use evaluate::*;27pub use function::parse_function_call;28pub use import::*;29use jrsonnet_parser::*;30use native::NativeCallback;31pub use obj::*;32use rustc_hash::FxHashMap;33use std::{34	cell::{Ref, RefCell, RefMut},35	collections::HashMap,36	fmt::Debug,37	hash::BuildHasherDefault,38	path::PathBuf,39	rc::Rc,40};41use trace::{offset_to_location, CodeLocation, CompactFormat, TraceFormat};42pub use val::*;4344// Re-exports45pub use jrsonnet_interner::IStr;4647type BindableFn = dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<LazyVal>;48#[derive(Clone)]49pub enum LazyBinding {50	Bindable(Rc<BindableFn>),51	Bound(LazyVal),52}5354impl Debug for LazyBinding {55	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {56		write!(f, "LazyBinding")57	}58}59impl LazyBinding {60	pub fn evaluate(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {61		match self {62			Self::Bindable(v) => v(this, super_obj),63			Self::Bound(v) => Ok(v.clone()),64		}65	}66}6768pub struct EvaluationSettings {69	/// Limits recursion by limiting the number of stack frames70	pub max_stack: usize,71	/// Limits amount of stack trace items preserved72	pub max_trace: usize,73	/// Used for s`td.extVar`74	pub ext_vars: HashMap<IStr, Val>,75	/// Used for ext.native76	pub ext_natives: HashMap<IStr, Rc<NativeCallback>>,77	/// TLA vars78	pub tla_vars: HashMap<IStr, Val>,79	/// Global variables are inserted in default context80	pub globals: HashMap<IStr, Val>,81	/// Used to resolve file locations/contents82	pub import_resolver: Box<dyn ImportResolver>,83	/// Used in manifestification functions84	pub manifest_format: ManifestFormat,85	/// Used for bindings86	pub trace_format: Box<dyn TraceFormat>,87}88impl Default for EvaluationSettings {89	fn default() -> Self {90		Self {91			max_stack: 200,92			max_trace: 20,93			globals: Default::default(),94			ext_vars: Default::default(),95			ext_natives: Default::default(),96			tla_vars: Default::default(),97			import_resolver: Box::new(DummyImportResolver),98			manifest_format: ManifestFormat::Json(4),99			trace_format: Box::new(CompactFormat {100				padding: 4,101				resolver: trace::PathResolver::Absolute,102			}),103		}104	}105}106107#[derive(Default)]108struct EvaluationData {109	/// Used for stack overflow detection, stacktrace is populated on unwind110	stack_depth: usize,111	/// Contains file source codes and evaluation results for imports and pretty-printed stacktraces112	files: HashMap<Rc<PathBuf>, FileData>,113	str_files: HashMap<Rc<PathBuf>, IStr>,114}115116pub struct FileData {117	source_code: IStr,118	parsed: LocExpr,119	evaluated: Option<Val>,120}121#[derive(Default)]122pub struct EvaluationStateInternals {123	/// Internal state124	data: RefCell<EvaluationData>,125	/// Settings, safe to change at runtime126	settings: RefCell<EvaluationSettings>,127}128129thread_local! {130	/// Contains the state for a currently executed file.131	/// Global state is fine here.132	pub(crate) static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)133}134pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {135	EVAL_STATE.with(|s| f(s.borrow().as_ref().unwrap()))136}137pub(crate) fn push<T>(138	e: Option<&ExprLocation>,139	frame_desc: impl FnOnce() -> String,140	f: impl FnOnce() -> Result<T>,141) -> Result<T> {142	with_state(|s| s.push(e, frame_desc, f))143}144145pub fn push_stack_frame<T>(146	e: Option<&ExprLocation>,147	frame_desc: impl FnOnce() -> String,148	f: impl FnOnce() -> Result<T>,149) -> Result<T> {150	push(e, frame_desc, f)151}152153/// Maintains stack trace and import resolution154#[derive(Default, Clone)]155pub struct EvaluationState(Rc<EvaluationStateInternals>);156157impl EvaluationState {158	/// Parses and adds file as loaded159	pub fn add_file(&self, path: Rc<PathBuf>, source_code: IStr) -> Result<()> {160		self.add_parsed_file(161			path.clone(),162			source_code.clone(),163			parse(164				&source_code,165				&ParserSettings {166					file_name: path.clone(),167					loc_data: true,168				},169			)170			.map_err(|error| ImportSyntaxError {171				error: Box::new(error),172				path,173				source_code,174			})?,175		)?;176177		Ok(())178	}179180	/// Adds file by source code and parsed expr181	pub fn add_parsed_file(182		&self,183		name: Rc<PathBuf>,184		source_code: IStr,185		parsed: LocExpr,186	) -> Result<()> {187		self.data_mut().files.insert(188			name,189			FileData {190				source_code,191				parsed,192				evaluated: None,193			},194		);195196		Ok(())197	}198	pub fn get_source(&self, name: &PathBuf) -> Option<IStr> {199		let ro_map = &self.data().files;200		ro_map.get(name).map(|value| value.source_code.clone())201	}202	pub fn map_source_locations(&self, file: &PathBuf, locs: &[usize]) -> Vec<CodeLocation> {203		offset_to_location(&self.get_source(file).unwrap(), locs)204	}205206	pub(crate) fn import_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Val> {207		let file_path = self.resolve_file(from, path)?;208		{209			let data = self.data();210			let files = &data.files;211			if files.contains_key(&file_path) {212				drop(data);213				return self.evaluate_loaded_file_raw(&file_path);214			}215		}216		let contents = self.load_file_contents(&file_path)?;217		self.add_file(file_path.clone(), contents)?;218		self.evaluate_loaded_file_raw(&file_path)219	}220	pub(crate) fn import_file_str(&self, from: &PathBuf, path: &PathBuf) -> Result<IStr> {221		let path = self.resolve_file(from, path)?;222		if !self.data().str_files.contains_key(&path) {223			let file_str = self.load_file_contents(&path)?;224			self.data_mut().str_files.insert(path.clone(), file_str);225		}226		Ok(self.data().str_files.get(&path).cloned().unwrap())227	}228229	fn evaluate_loaded_file_raw(&self, name: &PathBuf) -> Result<Val> {230		let expr: LocExpr = {231			let ro_map = &self.data().files;232			let value = ro_map233				.get(name)234				.unwrap_or_else(|| panic!("file not added: {:?}", name));235			if let Some(ref evaluated) = value.evaluated {236				return Ok(evaluated.clone());237			}238			value.parsed.clone()239		};240		let value = evaluate(self.create_default_context(), &expr)?;241		{242			self.data_mut()243				.files244				.get_mut(name)245				.unwrap()246				.evaluated247				.replace(value.clone());248		}249		Ok(value)250	}251252	/// Adds standard library global variable (std) to this evaluator253	pub fn with_stdlib(&self) -> &Self {254		use jrsonnet_stdlib::STDLIB_STR;255		let std_path = Rc::new(PathBuf::from("std.jsonnet"));256		self.run_in_state(|| {257			self.add_parsed_file(258				std_path.clone(),259				STDLIB_STR.to_owned().into(),260				builtin::get_parsed_stdlib(),261			)262			.unwrap();263			let val = self.evaluate_loaded_file_raw(&std_path).unwrap();264			self.settings_mut().globals.insert("std".into(), val);265		});266		self267	}268269	/// Creates context with all passed global variables270	pub fn create_default_context(&self) -> Context {271		let globals = &self.settings().globals;272		let mut new_bindings: FxHashMap<IStr, LazyVal> =273			FxHashMap::with_capacity_and_hasher(globals.len(), BuildHasherDefault::default());274		for (name, value) in globals.iter() {275			new_bindings.insert(name.clone(), resolved_lazy_val!(value.clone()));276		}277		Context::new().extend_bound(new_bindings)278	}279280	/// Executes code creating a new stack frame281	pub fn push<T>(282		&self,283		e: Option<&ExprLocation>,284		frame_desc: impl FnOnce() -> String,285		f: impl FnOnce() -> Result<T>,286	) -> Result<T> {287		{288			let mut data = self.data_mut();289			let stack_depth = &mut data.stack_depth;290			if *stack_depth > self.max_stack() {291				// Error creation uses data, so i drop guard here292				drop(data);293				throw!(StackOverflow);294			} else {295				*stack_depth += 1;296			}297		}298		let result = f();299		self.data_mut().stack_depth -= 1;300		if let Err(mut err) = result {301			err.trace_mut().0.push(StackTraceElement {302				location: e.cloned(),303				desc: frame_desc(),304			});305			return Err(err);306		}307		result308	}309310	/// Runs passed function in state (required if function needs to modify stack trace)311	pub fn run_in_state<T>(&self, f: impl FnOnce() -> T) -> T {312		EVAL_STATE.with(|v| {313			let has_state = v.borrow().is_some();314			if !has_state {315				v.borrow_mut().replace(self.clone());316			}317			let result = f();318			if !has_state {319				v.borrow_mut().take();320			}321			result322		})323	}324325	pub fn stringify_err(&self, e: &LocError) -> String {326		let mut out = String::new();327		self.settings()328			.trace_format329			.write_trace(&mut out, self, e)330			.unwrap();331		out332	}333334	pub fn manifest(&self, val: Val) -> Result<IStr> {335		self.run_in_state(|| val.manifest(&self.manifest_format()))336	}337	pub fn manifest_multi(&self, val: Val) -> Result<Vec<(IStr, IStr)>> {338		self.run_in_state(|| val.manifest_multi(&self.manifest_format()))339	}340	pub fn manifest_stream(&self, val: Val) -> Result<Vec<IStr>> {341		self.run_in_state(|| val.manifest_stream(&self.manifest_format()))342	}343344	/// If passed value is function then call with set TLA345	pub fn with_tla(&self, val: Val) -> Result<Val> {346		self.run_in_state(|| {347			Ok(match val {348				Val::Func(func) => push(349					None,350					|| "during TLA call".to_owned(),351					|| {352						func.evaluate_map(353							self.create_default_context(),354							&self.settings().tla_vars,355							true,356						)357					},358				)?,359				v => v,360			})361		})362	}363}364365/// Internals366impl EvaluationState {367	fn data(&self) -> Ref<EvaluationData> {368		self.0.data.borrow()369	}370	fn data_mut(&self) -> RefMut<EvaluationData> {371		self.0.data.borrow_mut()372	}373	pub fn settings(&self) -> Ref<EvaluationSettings> {374		self.0.settings.borrow()375	}376	pub fn settings_mut(&self) -> RefMut<EvaluationSettings> {377		self.0.settings.borrow_mut()378	}379}380381/// Raw methods evaluate passed values but don't perform TLA execution382impl EvaluationState {383	pub fn evaluate_file_raw(&self, name: &PathBuf) -> Result<Val> {384		self.run_in_state(|| self.import_file(&std::env::current_dir().expect("cwd"), name))385	}386	pub fn evaluate_file_raw_nocwd(&self, name: &PathBuf) -> Result<Val> {387		self.run_in_state(|| self.import_file(&PathBuf::from("."), name))388	}389	/// Parses and evaluates the given snippet390	pub fn evaluate_snippet_raw(&self, source: Rc<PathBuf>, code: IStr) -> Result<Val> {391		let parsed = parse(392			&code,393			&ParserSettings {394				file_name: source.clone(),395				loc_data: true,396			},397		)398		.map_err(|e| ImportSyntaxError {399			path: source.clone(),400			source_code: code.clone(),401			error: Box::new(e),402		})?;403		self.add_parsed_file(source, code, parsed.clone())?;404		self.evaluate_expr_raw(parsed)405	}406	/// Evaluates the parsed expression407	pub fn evaluate_expr_raw(&self, code: LocExpr) -> Result<Val> {408		self.run_in_state(|| evaluate(self.create_default_context(), &code))409	}410}411412/// Settings utilities413impl EvaluationState {414	pub fn add_ext_var(&self, name: IStr, value: Val) {415		self.settings_mut().ext_vars.insert(name, value);416	}417	pub fn add_ext_str(&self, name: IStr, value: IStr) {418		self.add_ext_var(name, Val::Str(value));419	}420	pub fn add_ext_code(&self, name: IStr, code: IStr) -> Result<()> {421		let value =422			self.evaluate_snippet_raw(Rc::new(PathBuf::from(format!("ext_code {}", name))), code)?;423		self.add_ext_var(name, value);424		Ok(())425	}426427	pub fn add_tla(&self, name: IStr, value: Val) {428		self.settings_mut().tla_vars.insert(name, value);429	}430	pub fn add_tla_str(&self, name: IStr, value: IStr) {431		self.add_tla(name, Val::Str(value));432	}433	pub fn add_tla_code(&self, name: IStr, code: IStr) -> Result<()> {434		let value =435			self.evaluate_snippet_raw(Rc::new(PathBuf::from(format!("tla_code {}", name))), code)?;436		self.add_tla(name, value);437		Ok(())438	}439440	pub fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {441		self.settings().import_resolver.resolve_file(from, path)442	}443	pub fn load_file_contents(&self, path: &PathBuf) -> Result<IStr> {444		self.settings().import_resolver.load_file_contents(path)445	}446447	pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {448		Ref::map(self.settings(), |s| &*s.import_resolver)449	}450	pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {451		self.settings_mut().import_resolver = resolver;452	}453454	pub fn add_native(&self, name: IStr, cb: Rc<NativeCallback>) {455		self.settings_mut().ext_natives.insert(name, cb);456	}457458	pub fn manifest_format(&self) -> ManifestFormat {459		self.settings().manifest_format.clone()460	}461	pub fn set_manifest_format(&self, format: ManifestFormat) {462		self.settings_mut().manifest_format = format;463	}464465	pub fn trace_format(&self) -> Ref<dyn TraceFormat> {466		Ref::map(self.settings(), |s| &*s.trace_format)467	}468	pub fn set_trace_format(&self, format: Box<dyn TraceFormat>) {469		self.settings_mut().trace_format = format;470	}471472	pub fn max_trace(&self) -> usize {473		self.settings().max_trace474	}475	pub fn set_max_trace(&self, trace: usize) {476		self.settings_mut().max_trace = trace;477	}478479	pub fn max_stack(&self) -> usize {480		self.settings().max_stack481	}482	pub fn set_max_stack(&self, trace: usize) {483		self.settings_mut().max_stack = trace;484	}485}486487#[cfg(test)]488pub mod tests {489	use super::Val;490	use crate::{error::Error::*, primitive_equals, EvaluationState};491	use jrsonnet_interner::IStr;492	use jrsonnet_parser::*;493	use std::{path::PathBuf, rc::Rc};494495	#[test]496	#[should_panic]497	fn eval_state_stacktrace() {498		let state = EvaluationState::default();499		state.run_in_state(|| {500			state501				.push(502					Some(&ExprLocation(503						Rc::new(PathBuf::from("test1.jsonnet")),504						10,505						20,506					)),507					|| "outer".to_owned(),508					|| {509						state.push(510							Some(&ExprLocation(511								Rc::new(PathBuf::from("test2.jsonnet")),512								30,513								40,514							)),515							|| "inner".to_owned(),516							|| Err(RuntimeError("".into()).into()),517						)?;518						Ok(())519					},520				)521				.unwrap();522		});523	}524525	#[test]526	fn eval_state_standard() {527		let state = EvaluationState::default();528		state.with_stdlib();529		assert!(primitive_equals(530			&state531				.evaluate_snippet_raw(532					Rc::new(PathBuf::from("raw.jsonnet")),533					r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#.into()534				)535				.unwrap(),536			&Val::Bool(true),537		)538		.unwrap());539	}540541	macro_rules! eval {542		($str: expr) => {543			EvaluationState::default()544				.with_stdlib()545				.evaluate_snippet_raw(Rc::new(PathBuf::from("raw.jsonnet")), $str.into())546				.unwrap()547		};548	}549	macro_rules! eval_json {550		($str: expr) => {{551			let evaluator = EvaluationState::default();552			evaluator.with_stdlib();553			evaluator.run_in_state(|| {554				evaluator555					.evaluate_snippet_raw(Rc::new(PathBuf::from("raw.jsonnet")), $str.into())556					.unwrap()557					.to_json(0)558					.unwrap()559					.replace("\n", "")560			})561		}};562	}563564	/// Asserts given code returns `true`565	macro_rules! assert_eval {566		($str: expr) => {567			assert!(primitive_equals(&eval!($str), &Val::Bool(true)).unwrap())568		};569	}570571	/// Asserts given code returns `false`572	macro_rules! assert_eval_neg {573		($str: expr) => {574			assert!(primitive_equals(&eval!($str), &Val::Bool(false)).unwrap())575		};576	}577	macro_rules! assert_json {578		($str: expr, $out: expr) => {579			assert_eq!(eval_json!($str), $out.replace("\t", ""))580		};581	}582583	/// Sanity checking, before trusting to another tests584	#[test]585	fn equality_operator() {586		assert_eval!("2 == 2");587		assert_eval_neg!("2 != 2");588		assert_eval!("2 != 3");589		assert_eval_neg!("2 == 3");590		assert_eval!("'Hello' == 'Hello'");591		assert_eval_neg!("'Hello' != 'Hello'");592		assert_eval!("'Hello' != 'World'");593		assert_eval_neg!("'Hello' == 'World'");594	}595596	#[test]597	fn math_evaluation() {598		assert_eval!("2 + 2 * 2 == 6");599		assert_eval!("3 + (2 + 2 * 2) == 9");600	}601602	#[test]603	fn string_concat() {604		assert_eval!("'Hello' + 'World' == 'HelloWorld'");605		assert_eval!("'Hello' * 3 == 'HelloHelloHello'");606		assert_eval!("'Hello' + 'World' * 3 == 'HelloWorldWorldWorld'");607	}608609	#[test]610	fn faster_join() {611		assert_eval!("std.join([0,0], [[1,2],[3,4],[5,6]]) == [1,2,0,0,3,4,0,0,5,6]");612		assert_eval!("std.join(',', ['1','2','3','4']) == '1,2,3,4'");613	}614615	#[test]616	fn function_contexts() {617		assert_eval!(618			r#"619				local k = {620					t(name = self.h): [self.h, name],621					h: 3,622				};623				local f = {624					t: k.t(),625					h: 4,626				};627				f.t[0] == f.t[1]628			"#629		);630	}631632	#[test]633	fn local() {634		assert_eval!("local a = 2; local b = 3; a + b == 5");635		assert_eval!("local a = 1, b = a + 1; a + b == 3");636		assert_eval!("local a = 1; local a = 2; a == 2");637	}638639	#[test]640	fn object_lazyness() {641		assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);642	}643644	#[test]645	fn object_inheritance() {646		assert_json!("{a: self.b} + {b:3}", r#"{"a": 3,"b": 3}"#);647	}648649	#[test]650	fn object_assertion_success() {651		eval!("{assert \"a\" in self} + {a:2}");652	}653654	#[test]655	fn object_assertion_error() {656		eval!("{assert \"a\" in self}");657	}658659	#[test]660	fn lazy_args() {661		eval!("local test(a) = 2; test(error '3')");662	}663664	#[test]665	#[should_panic]666	fn tailstrict_args() {667		eval!("local test(a) = 2; test(error '3') tailstrict");668	}669670	#[test]671	#[should_panic]672	fn no_binding_error() {673		eval!("a");674	}675676	#[test]677	fn test_object() {678		assert_json!("{a:2}", r#"{"a": 2}"#);679		assert_json!("{a:2+2}", r#"{"a": 4}"#);680		assert_json!("{a:2}+{b:2}", r#"{"a": 2,"b": 2}"#);681		assert_json!("{b:3}+{b:2}", r#"{"b": 2}"#);682		assert_json!("{b:3}+{b+:2}", r#"{"b": 5}"#);683		assert_json!("local test='a'; {[test]:2}", r#"{"a": 2}"#);684		assert_json!(685			r#"686				{687					name: "Alice",688					welcome: "Hello " + self.name + "!",689				}690			"#,691			r#"{"name": "Alice","welcome": "Hello Alice!"}"#692		);693		assert_json!(694			r#"695				{696					name: "Alice",697					welcome: "Hello " + self.name + "!",698				} + {699					name: "Bob"700				}701			"#,702			r#"{"name": "Bob","welcome": "Hello Bob!"}"#703		);704	}705706	#[test]707	fn functions() {708		assert_json!(r#"local a = function(b, c = 2) b + c; a(2)"#, "4");709		assert_json!(710			r#"local a = function(b, c = "Dear") b + c + d, d = "World"; a("Hello")"#,711			r#""HelloDearWorld""#712		);713	}714715	#[test]716	fn local_methods() {717		assert_json!(r#"local a(b, c = 2) = b + c; a(2)"#, "4");718		assert_json!(719			r#"local a(b, c = "Dear") = b + c + d, d = "World"; a("Hello")"#,720			r#""HelloDearWorld""#721		);722	}723724	#[test]725	fn object_locals() {726		assert_json!(r#"{local a = 3, b: a}"#, r#"{"b": 3}"#);727		assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b": 3}"#);728		assert_json!(729			r#"{local a = function (b) {[b]:4}, test: a("test")}"#,730			r#"{"test": {"test": 4}}"#731		);732	}733734	#[test]735	fn object_comp() {736		assert_json!(737			r#"{local t = "a", ["h"+i+"_"+z]: if "h"+(i-1)+"_"+z in self then t+1 else 0+t for i in [1,2,3] for z in [2,3,4] if z != i}"#,738			"{\"h1_2\": \"0a\",\"h1_3\": \"0a\",\"h1_4\": \"0a\",\"h2_3\": \"a1\",\"h2_4\": \"a1\",\"h3_2\": \"0a\",\"h3_4\": \"a1\"}"739		)740	}741742	#[test]743	fn direct_self() {744		println!(745			"{:#?}",746			eval!(747				r#"748					{749						local me = self,750						a: 3,751						b(): me.a,752					}753				"#754			)755		);756	}757758	#[test]759	fn indirect_self() {760		// `self` assigned to `me` was lost when being761		// referenced from field762		eval!(763			r#"{764				local me = self,765				a: 3,766				b: me.a,767			}.b"#768		);769	}770771	// We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly772	#[test]773	fn std_assert_ok() {774		eval!("std.assertEqual(4.5 << 2, 16)");775	}776777	#[test]778	#[should_panic]779	fn std_assert_failure() {780		eval!("std.assertEqual(4.5 << 2, 15)");781	}782783	#[test]784	fn string_is_string() {785		assert!(primitive_equals(786			&eval!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),787			&Val::Bool(false),788		)789		.unwrap());790	}791792	#[test]793	fn base64_works() {794		assert_json!(r#"std.base64("test")"#, r#""dGVzdA==""#);795	}796797	#[test]798	fn utf8_chars() {799		assert_json!(800			r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,801			r#"{"c": 128526,"l": 1}"#802		)803	}804805	#[test]806	fn json() {807		assert_json!(808			r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,809			r#""{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}""#810		);811	}812813	#[test]814	fn parse_json() {815		assert_json!(816			r#"std.parseJson('{"a": -1,"b": 1,"c": 3.141,"d": []}')"#,817			r#"{"a": -1,"b": 1,"c": 3.141,"d": []}"#818		);819		// TODO: this should in fact fail as is no proper JSON syntax820		assert_json!(821			r#"std.parseJson("{a:-1, b:1, c:3.141, d:[]}")"#,822			r#"{"a": -1,"b": 1,"c": 3.141,"d": []}"#823		);824		// TODO: this is also no valid JSON825		assert_json!(r#"std.parseJson('local x = 2; x * x')"#, r#"4"#);826	}827828	#[test]829	fn test() {830		assert_json!(831			r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,832			"[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"833		);834	}835836	#[test]837	fn sjsonnet() {838		eval!(839			r#"840			local x0 = {k: 1};841			local x1 = {k: x0.k + x0.k};842			local x2 = {k: x1.k + x1.k};843			local x3 = {k: x2.k + x2.k};844			local x4 = {k: x3.k + x3.k};845			local x5 = {k: x4.k + x4.k};846			local x6 = {k: x5.k + x5.k};847			local x7 = {k: x6.k + x6.k};848			local x8 = {k: x7.k + x7.k};849			local x9 = {k: x8.k + x8.k};850			local x10 = {k: x9.k + x9.k};851			local x11 = {k: x10.k + x10.k};852			local x12 = {k: x11.k + x11.k};853			local x13 = {k: x12.k + x12.k};854			local x14 = {k: x13.k + x13.k};855			local x15 = {k: x14.k + x14.k};856			local x16 = {k: x15.k + x15.k};857			local x17 = {k: x16.k + x16.k};858			local x18 = {k: x17.k + x17.k};859			local x19 = {k: x18.k + x18.k};860			local x20 = {k: x19.k + x19.k};861			local x21 = {k: x20.k + x20.k};862			x21.k863		"#864		);865	}866867	// This test is commented out by default, because of huge compilation slowdown868	// #[bench]869	// fn bench_codegen(b: &mut Bencher) {870	// 	b.iter(|| {871	// 		#[allow(clippy::all)]872	// 		let stdlib = {873	// 			use jrsonnet_parser::*;874	// 			include!(concat!(env!("OUT_DIR"), "/stdlib.rs"))875	// 		};876	// 		stdlib877	// 	})878	// }879880	/*881	#[bench]882	fn bench_serialize(b: &mut Bencher) {883		b.iter(|| {884			bincode::deserialize::<jrsonnet_parser::LocExpr>(include_bytes!(concat!(885				env!("OUT_DIR"),886				"/stdlib.bincode"887			)))888			.expect("deserialize stdlib")889		})890	}891892	#[bench]893	fn bench_parse(b: &mut Bencher) {894		b.iter(|| {895			jrsonnet_parser::parse(896				jrsonnet_stdlib::STDLIB_STR,897				&jrsonnet_parser::ParserSettings {898					loc_data: true,899					file_name: Rc::new(PathBuf::from("std.jsonnet")),900				},901			)902		})903	}904	*/905906	#[test]907	fn equality() {908		println!(909			"{:?}",910			jrsonnet_parser::parse(911				"{ x: 1, y: 2 } == { x: 1, y: 2 }",912				&ParserSettings::default()913			)914		);915		assert_eval!("{ x: 1, y: 2 } == { x: 1, y: 2 }")916	}917918	#[test]919	fn native_ext() -> crate::error::Result<()> {920		use super::native::NativeCallback;921		let evaluator = EvaluationState::default();922923		evaluator.with_stdlib();924		evaluator.settings_mut().ext_natives.insert(925			"native_add".into(),926			Rc::new(NativeCallback::new(927				ParamsDesc(Rc::new(vec![928					Param("a".into(), None),929					Param("b".into(), None),930				])),931				|caller, args| {932					assert_eq!(933						caller.unwrap(),934						Rc::new(PathBuf::from("native_caller.jsonnet"))935					);936					match (&args[0], &args[1]) {937						(Val::Num(a), Val::Num(b)) => Ok(Val::Num(a + b)),938						(_, _) => unreachable!(),939					}940				},941			)),942		);943		evaluator.evaluate_snippet_raw(944			Rc::new(PathBuf::from("native_caller.jsonnet")),945			"std.assertEqual(std.native(\"native_add\")(1, 2), 3)".into(),946		)?;947		Ok(())948	}949950	#[test]951	fn constant_intrinsic() -> crate::error::Result<()> {952		assert_eval!(953			"local std2 = std; local std = std2 { primitiveEquals(a, b):: false }; 1 == 1"954		);955		Ok(())956	}957958	#[test]959	fn standalone_super() -> crate::error::Result<()> {960		assert_eval!(961			r#"962			local obj = {963				a: 1,964				b: 2,965				c: 3,966			};967			local test = obj + {968				fields: std.objectFields(super),969				d: 5,970			};971			test.fields == ['a', 'b', 'c']972		"#973		);974		Ok(())975	}976977	#[test]978	fn comp_self() -> crate::error::Result<()> {979		assert_eval!(980			r#"981			std.objectFields({982				a:{983					[name]: name for name in std.objectFields(self)984				},985				b: 2,986				c: 3,987			}.a) == ['a', 'b', 'c']988			"#989		);990991		Ok(())992	}993994	struct TestImportResolver(IStr);995	impl crate::import::ImportResolver for TestImportResolver {996		fn resolve_file(&self, _: &PathBuf, _: &PathBuf) -> crate::error::Result<Rc<PathBuf>> {997			Ok(Rc::new(PathBuf::from("/test")))998		}9991000		fn load_file_contents(&self, _: &PathBuf) -> crate::error::Result<IStr> {1001			Ok(self.0.clone())1002		}10031004		unsafe fn as_any(&self) -> &dyn std::any::Any {1005			panic!()1006		}1007	}10081009	#[test]1010	fn issue_23() {1011		let state = EvaluationState::default();1012		state.set_import_resolver(Box::new(TestImportResolver(r#"import "/test""#.into())));1013		let _ = state.evaluate_file_raw(&PathBuf::from("/test"));1014	}10151016	#[test]1017	fn issue_40() {1018		let state = EvaluationState::default();1019		state.with_stdlib();10201021		let error = state1022			.evaluate_snippet_raw(1023				Rc::new(PathBuf::from("issue40.jsonnet")),1024				r#"1025				local conf = {1026					n: ""1027				};1028				1029				local result = conf + {1030					assert std.isNumber(self.n): "is number"1031				};10321033				std.manifestJsonEx(result, "")1034			"#1035				.into(),1036			)1037			.unwrap_err();1038		assert_eq!(error.error().to_string(), "assert failed: is number");1039	}1040}
after · crates/jrsonnet-evaluator/src/lib.rs
1#![cfg_attr(feature = "unstable", feature(stmt_expr_attributes))]2#![warn(clippy::all, clippy::nursery)]3#![allow(4	macro_expanded_macro_exports_accessed_by_absolute_paths,5	clippy::ptr_arg6)]78mod builtin;9mod ctx;10mod dynamic;11pub mod error;12mod evaluate;13mod function;14mod import;15mod integrations;16mod map;17pub mod native;18mod obj;19pub mod trace;20pub mod typed;21mod val;2223pub use ctx::*;24pub use dynamic::*;25use error::{Error::*, LocError, Result, StackTraceElement};26pub use evaluate::*;27pub use function::parse_function_call;28pub use import::*;29use jrsonnet_parser::*;30use native::NativeCallback;31pub use obj::*;32use rustc_hash::FxHashMap;33use std::{34	cell::{Ref, RefCell, RefMut},35	collections::HashMap,36	fmt::Debug,37	hash::BuildHasherDefault,38	path::{Path, PathBuf},39	rc::Rc,40};41use trace::{offset_to_location, CodeLocation, CompactFormat, TraceFormat};42pub use val::*;4344// Re-exports45pub use jrsonnet_interner::IStr;4647type BindableFn = dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<LazyVal>;48#[derive(Clone)]49pub enum LazyBinding {50	Bindable(Rc<BindableFn>),51	Bound(LazyVal),52}5354impl Debug for LazyBinding {55	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {56		write!(f, "LazyBinding")57	}58}59impl LazyBinding {60	pub fn evaluate(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {61		match self {62			Self::Bindable(v) => v(this, super_obj),63			Self::Bound(v) => Ok(v.clone()),64		}65	}66}6768pub struct EvaluationSettings {69	/// Limits recursion by limiting the number of stack frames70	pub max_stack: usize,71	/// Limits amount of stack trace items preserved72	pub max_trace: usize,73	/// Used for s`td.extVar`74	pub ext_vars: HashMap<IStr, Val>,75	/// Used for ext.native76	pub ext_natives: HashMap<IStr, Rc<NativeCallback>>,77	/// TLA vars78	pub tla_vars: HashMap<IStr, Val>,79	/// Global variables are inserted in default context80	pub globals: HashMap<IStr, Val>,81	/// Used to resolve file locations/contents82	pub import_resolver: Box<dyn ImportResolver>,83	/// Used in manifestification functions84	pub manifest_format: ManifestFormat,85	/// Used for bindings86	pub trace_format: Box<dyn TraceFormat>,87}88impl Default for EvaluationSettings {89	fn default() -> Self {90		Self {91			max_stack: 200,92			max_trace: 20,93			globals: Default::default(),94			ext_vars: Default::default(),95			ext_natives: Default::default(),96			tla_vars: Default::default(),97			import_resolver: Box::new(DummyImportResolver),98			manifest_format: ManifestFormat::Json(4),99			trace_format: Box::new(CompactFormat {100				padding: 4,101				resolver: trace::PathResolver::Absolute,102			}),103		}104	}105}106107#[derive(Default)]108struct EvaluationData {109	/// Used for stack overflow detection, stacktrace is populated on unwind110	stack_depth: usize,111	/// Contains file source codes and evaluation results for imports and pretty-printed stacktraces112	files: HashMap<Rc<Path>, FileData>,113	str_files: HashMap<Rc<Path>, IStr>,114}115116pub struct FileData {117	source_code: IStr,118	parsed: LocExpr,119	evaluated: Option<Val>,120}121#[derive(Default)]122pub struct EvaluationStateInternals {123	/// Internal state124	data: RefCell<EvaluationData>,125	/// Settings, safe to change at runtime126	settings: RefCell<EvaluationSettings>,127}128129thread_local! {130	/// Contains the state for a currently executed file.131	/// Global state is fine here.132	pub(crate) static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)133}134pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {135	EVAL_STATE.with(|s| f(s.borrow().as_ref().unwrap()))136}137pub(crate) fn push<T>(138	e: Option<&ExprLocation>,139	frame_desc: impl FnOnce() -> String,140	f: impl FnOnce() -> Result<T>,141) -> Result<T> {142	with_state(|s| s.push(e, frame_desc, f))143}144145pub fn push_stack_frame<T>(146	e: Option<&ExprLocation>,147	frame_desc: impl FnOnce() -> String,148	f: impl FnOnce() -> Result<T>,149) -> Result<T> {150	push(e, frame_desc, f)151}152153/// Maintains stack trace and import resolution154#[derive(Default, Clone)]155pub struct EvaluationState(Rc<EvaluationStateInternals>);156157impl EvaluationState {158	/// Parses and adds file as loaded159	pub fn add_file(&self, path: Rc<Path>, source_code: IStr) -> Result<()> {160		self.add_parsed_file(161			path.clone(),162			source_code.clone(),163			parse(164				&source_code,165				&ParserSettings {166					file_name: path.clone(),167					loc_data: true,168				},169			)170			.map_err(|error| ImportSyntaxError {171				error: Box::new(error),172				path: path.to_owned(),173				source_code,174			})?,175		)?;176177		Ok(())178	}179180	/// Adds file by source code and parsed expr181	pub fn add_parsed_file(182		&self,183		name: Rc<Path>,184		source_code: IStr,185		parsed: LocExpr,186	) -> Result<()> {187		self.data_mut().files.insert(188			name,189			FileData {190				source_code,191				parsed,192				evaluated: None,193			},194		);195196		Ok(())197	}198	pub fn get_source(&self, name: &Path) -> Option<IStr> {199		let ro_map = &self.data().files;200		ro_map.get(name).map(|value| value.source_code.clone())201	}202	pub fn map_source_locations(&self, file: &Path, locs: &[usize]) -> Vec<CodeLocation> {203		offset_to_location(&self.get_source(file).unwrap(), locs)204	}205206	pub(crate) fn import_file(&self, from: &Path, path: &Path) -> Result<Val> {207		let file_path = self.resolve_file(from, path)?;208		{209			let data = self.data();210			let files = &data.files;211			if files.contains_key(&file_path as &Path) {212				drop(data);213				return self.evaluate_loaded_file_raw(&file_path);214			}215		}216		let contents = self.load_file_contents(&file_path)?;217		self.add_file(file_path.clone(), contents)?;218		self.evaluate_loaded_file_raw(&file_path)219	}220	pub(crate) fn import_file_str(&self, from: &Path, path: &Path) -> Result<IStr> {221		let path = self.resolve_file(from, path)?;222		if !self.data().str_files.contains_key(&path) {223			let file_str = self.load_file_contents(&path)?;224			self.data_mut().str_files.insert(path.clone(), file_str);225		}226		Ok(self.data().str_files.get(&path).cloned().unwrap())227	}228229	fn evaluate_loaded_file_raw(&self, name: &Path) -> Result<Val> {230		let expr: LocExpr = {231			let ro_map = &self.data().files;232			let value = ro_map233				.get(name)234				.unwrap_or_else(|| panic!("file not added: {:?}", name));235			if let Some(ref evaluated) = value.evaluated {236				return Ok(evaluated.clone());237			}238			value.parsed.clone()239		};240		let value = evaluate(self.create_default_context(), &expr)?;241		{242			self.data_mut()243				.files244				.get_mut(name)245				.unwrap()246				.evaluated247				.replace(value.clone());248		}249		Ok(value)250	}251252	/// Adds standard library global variable (std) to this evaluator253	pub fn with_stdlib(&self) -> &Self {254		use jrsonnet_stdlib::STDLIB_STR;255		let std_path: Rc<Path> = PathBuf::from("std.jsonnet").into();256		self.run_in_state(|| {257			self.add_parsed_file(258				std_path.clone(),259				STDLIB_STR.to_owned().into(),260				builtin::get_parsed_stdlib(),261			)262			.unwrap();263			let val = self.evaluate_loaded_file_raw(&std_path).unwrap();264			self.settings_mut().globals.insert("std".into(), val);265		});266		self267	}268269	/// Creates context with all passed global variables270	pub fn create_default_context(&self) -> Context {271		let globals = &self.settings().globals;272		let mut new_bindings: FxHashMap<IStr, LazyVal> =273			FxHashMap::with_capacity_and_hasher(globals.len(), BuildHasherDefault::default());274		for (name, value) in globals.iter() {275			new_bindings.insert(name.clone(), resolved_lazy_val!(value.clone()));276		}277		Context::new().extend_bound(new_bindings)278	}279280	/// Executes code creating a new stack frame281	pub fn push<T>(282		&self,283		e: Option<&ExprLocation>,284		frame_desc: impl FnOnce() -> String,285		f: impl FnOnce() -> Result<T>,286	) -> Result<T> {287		{288			let mut data = self.data_mut();289			let stack_depth = &mut data.stack_depth;290			if *stack_depth > self.max_stack() {291				// Error creation uses data, so i drop guard here292				drop(data);293				throw!(StackOverflow);294			} else {295				*stack_depth += 1;296			}297		}298		let result = f();299		self.data_mut().stack_depth -= 1;300		if let Err(mut err) = result {301			err.trace_mut().0.push(StackTraceElement {302				location: e.cloned(),303				desc: frame_desc(),304			});305			return Err(err);306		}307		result308	}309310	/// Runs passed function in state (required if function needs to modify stack trace)311	pub fn run_in_state<T>(&self, f: impl FnOnce() -> T) -> T {312		EVAL_STATE.with(|v| {313			let has_state = v.borrow().is_some();314			if !has_state {315				v.borrow_mut().replace(self.clone());316			}317			let result = f();318			if !has_state {319				v.borrow_mut().take();320			}321			result322		})323	}324325	pub fn stringify_err(&self, e: &LocError) -> String {326		let mut out = String::new();327		self.settings()328			.trace_format329			.write_trace(&mut out, self, e)330			.unwrap();331		out332	}333334	pub fn manifest(&self, val: Val) -> Result<IStr> {335		self.run_in_state(|| val.manifest(&self.manifest_format()))336	}337	pub fn manifest_multi(&self, val: Val) -> Result<Vec<(IStr, IStr)>> {338		self.run_in_state(|| val.manifest_multi(&self.manifest_format()))339	}340	pub fn manifest_stream(&self, val: Val) -> Result<Vec<IStr>> {341		self.run_in_state(|| val.manifest_stream(&self.manifest_format()))342	}343344	/// If passed value is function then call with set TLA345	pub fn with_tla(&self, val: Val) -> Result<Val> {346		self.run_in_state(|| {347			Ok(match val {348				Val::Func(func) => push(349					None,350					|| "during TLA call".to_owned(),351					|| {352						func.evaluate_map(353							self.create_default_context(),354							&self.settings().tla_vars,355							true,356						)357					},358				)?,359				v => v,360			})361		})362	}363}364365/// Internals366impl EvaluationState {367	fn data(&self) -> Ref<EvaluationData> {368		self.0.data.borrow()369	}370	fn data_mut(&self) -> RefMut<EvaluationData> {371		self.0.data.borrow_mut()372	}373	pub fn settings(&self) -> Ref<EvaluationSettings> {374		self.0.settings.borrow()375	}376	pub fn settings_mut(&self) -> RefMut<EvaluationSettings> {377		self.0.settings.borrow_mut()378	}379}380381/// Raw methods evaluate passed values but don't perform TLA execution382impl EvaluationState {383	pub fn evaluate_file_raw(&self, name: &Path) -> Result<Val> {384		self.run_in_state(|| self.import_file(&std::env::current_dir().expect("cwd"), name))385	}386	pub fn evaluate_file_raw_nocwd(&self, name: &Path) -> Result<Val> {387		self.run_in_state(|| self.import_file(&PathBuf::from("."), name))388	}389	/// Parses and evaluates the given snippet390	pub fn evaluate_snippet_raw(&self, source: Rc<Path>, code: IStr) -> Result<Val> {391		let parsed = parse(392			&code,393			&ParserSettings {394				file_name: source.clone(),395				loc_data: true,396			},397		)398		.map_err(|e| ImportSyntaxError {399			path: source.clone(),400			source_code: code.clone(),401			error: Box::new(e),402		})?;403		self.add_parsed_file(source, code, parsed.clone())?;404		self.evaluate_expr_raw(parsed)405	}406	/// Evaluates the parsed expression407	pub fn evaluate_expr_raw(&self, code: LocExpr) -> Result<Val> {408		self.run_in_state(|| evaluate(self.create_default_context(), &code))409	}410}411412/// Settings utilities413impl EvaluationState {414	pub fn add_ext_var(&self, name: IStr, value: Val) {415		self.settings_mut().ext_vars.insert(name, value);416	}417	pub fn add_ext_str(&self, name: IStr, value: IStr) {418		self.add_ext_var(name, Val::Str(value));419	}420	pub fn add_ext_code(&self, name: IStr, code: IStr) -> Result<()> {421		let value =422			self.evaluate_snippet_raw(PathBuf::from(format!("ext_code {}", name)).into(), code)?;423		self.add_ext_var(name, value);424		Ok(())425	}426427	pub fn add_tla(&self, name: IStr, value: Val) {428		self.settings_mut().tla_vars.insert(name, value);429	}430	pub fn add_tla_str(&self, name: IStr, value: IStr) {431		self.add_tla(name, Val::Str(value));432	}433	pub fn add_tla_code(&self, name: IStr, code: IStr) -> Result<()> {434		let value =435			self.evaluate_snippet_raw(PathBuf::from(format!("tla_code {}", name)).into(), code)?;436		self.add_tla(name, value);437		Ok(())438	}439440	pub fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {441		self.settings().import_resolver.resolve_file(from, path)442	}443	pub fn load_file_contents(&self, path: &Path) -> Result<IStr> {444		self.settings().import_resolver.load_file_contents(path)445	}446447	pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {448		Ref::map(self.settings(), |s| &*s.import_resolver)449	}450	pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {451		self.settings_mut().import_resolver = resolver;452	}453454	pub fn add_native(&self, name: IStr, cb: Rc<NativeCallback>) {455		self.settings_mut().ext_natives.insert(name, cb);456	}457458	pub fn manifest_format(&self) -> ManifestFormat {459		self.settings().manifest_format.clone()460	}461	pub fn set_manifest_format(&self, format: ManifestFormat) {462		self.settings_mut().manifest_format = format;463	}464465	pub fn trace_format(&self) -> Ref<dyn TraceFormat> {466		Ref::map(self.settings(), |s| &*s.trace_format)467	}468	pub fn set_trace_format(&self, format: Box<dyn TraceFormat>) {469		self.settings_mut().trace_format = format;470	}471472	pub fn max_trace(&self) -> usize {473		self.settings().max_trace474	}475	pub fn set_max_trace(&self, trace: usize) {476		self.settings_mut().max_trace = trace;477	}478479	pub fn max_stack(&self) -> usize {480		self.settings().max_stack481	}482	pub fn set_max_stack(&self, trace: usize) {483		self.settings_mut().max_stack = trace;484	}485}486487#[cfg(test)]488pub mod tests {489	use super::Val;490	use crate::{error::Error::*, primitive_equals, EvaluationState};491	use jrsonnet_interner::IStr;492	use jrsonnet_parser::*;493	use std::{494		path::{Path, PathBuf},495		rc::Rc,496	};497498	#[test]499	#[should_panic]500	fn eval_state_stacktrace() {501		let state = EvaluationState::default();502		state.run_in_state(|| {503			state504				.push(505					Some(&ExprLocation(PathBuf::from("test1.jsonnet").into(), 10, 20)),506					|| "outer".to_owned(),507					|| {508						state.push(509							Some(&ExprLocation(PathBuf::from("test2.jsonnet").into(), 30, 40)),510							|| "inner".to_owned(),511							|| Err(RuntimeError("".into()).into()),512						)?;513						Ok(())514					},515				)516				.unwrap();517		});518	}519520	#[test]521	fn eval_state_standard() {522		let state = EvaluationState::default();523		state.with_stdlib();524		assert!(primitive_equals(525			&state526				.evaluate_snippet_raw(527					PathBuf::from("raw.jsonnet").into(),528					r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#.into()529				)530				.unwrap(),531			&Val::Bool(true),532		)533		.unwrap());534	}535536	macro_rules! eval {537		($str: expr) => {538			EvaluationState::default()539				.with_stdlib()540				.evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())541				.unwrap()542		};543	}544	macro_rules! eval_json {545		($str: expr) => {{546			let evaluator = EvaluationState::default();547			evaluator.with_stdlib();548			evaluator.run_in_state(|| {549				evaluator550					.evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())551					.unwrap()552					.to_json(0)553					.unwrap()554					.replace("\n", "")555			})556		}};557	}558559	/// Asserts given code returns `true`560	macro_rules! assert_eval {561		($str: expr) => {562			assert!(primitive_equals(&eval!($str), &Val::Bool(true)).unwrap())563		};564	}565566	/// Asserts given code returns `false`567	macro_rules! assert_eval_neg {568		($str: expr) => {569			assert!(primitive_equals(&eval!($str), &Val::Bool(false)).unwrap())570		};571	}572	macro_rules! assert_json {573		($str: expr, $out: expr) => {574			assert_eq!(eval_json!($str), $out.replace("\t", ""))575		};576	}577578	/// Sanity checking, before trusting to another tests579	#[test]580	fn equality_operator() {581		assert_eval!("2 == 2");582		assert_eval_neg!("2 != 2");583		assert_eval!("2 != 3");584		assert_eval_neg!("2 == 3");585		assert_eval!("'Hello' == 'Hello'");586		assert_eval_neg!("'Hello' != 'Hello'");587		assert_eval!("'Hello' != 'World'");588		assert_eval_neg!("'Hello' == 'World'");589	}590591	#[test]592	fn math_evaluation() {593		assert_eval!("2 + 2 * 2 == 6");594		assert_eval!("3 + (2 + 2 * 2) == 9");595	}596597	#[test]598	fn string_concat() {599		assert_eval!("'Hello' + 'World' == 'HelloWorld'");600		assert_eval!("'Hello' * 3 == 'HelloHelloHello'");601		assert_eval!("'Hello' + 'World' * 3 == 'HelloWorldWorldWorld'");602	}603604	#[test]605	fn faster_join() {606		assert_eval!("std.join([0,0], [[1,2],[3,4],[5,6]]) == [1,2,0,0,3,4,0,0,5,6]");607		assert_eval!("std.join(',', ['1','2','3','4']) == '1,2,3,4'");608	}609610	#[test]611	fn function_contexts() {612		assert_eval!(613			r#"614				local k = {615					t(name = self.h): [self.h, name],616					h: 3,617				};618				local f = {619					t: k.t(),620					h: 4,621				};622				f.t[0] == f.t[1]623			"#624		);625	}626627	#[test]628	fn local() {629		assert_eval!("local a = 2; local b = 3; a + b == 5");630		assert_eval!("local a = 1, b = a + 1; a + b == 3");631		assert_eval!("local a = 1; local a = 2; a == 2");632	}633634	#[test]635	fn object_lazyness() {636		assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);637	}638639	#[test]640	fn object_inheritance() {641		assert_json!("{a: self.b} + {b:3}", r#"{"a": 3,"b": 3}"#);642	}643644	#[test]645	fn object_assertion_success() {646		eval!("{assert \"a\" in self} + {a:2}");647	}648649	#[test]650	fn object_assertion_error() {651		eval!("{assert \"a\" in self}");652	}653654	#[test]655	fn lazy_args() {656		eval!("local test(a) = 2; test(error '3')");657	}658659	#[test]660	#[should_panic]661	fn tailstrict_args() {662		eval!("local test(a) = 2; test(error '3') tailstrict");663	}664665	#[test]666	#[should_panic]667	fn no_binding_error() {668		eval!("a");669	}670671	#[test]672	fn test_object() {673		assert_json!("{a:2}", r#"{"a": 2}"#);674		assert_json!("{a:2+2}", r#"{"a": 4}"#);675		assert_json!("{a:2}+{b:2}", r#"{"a": 2,"b": 2}"#);676		assert_json!("{b:3}+{b:2}", r#"{"b": 2}"#);677		assert_json!("{b:3}+{b+:2}", r#"{"b": 5}"#);678		assert_json!("local test='a'; {[test]:2}", r#"{"a": 2}"#);679		assert_json!(680			r#"681				{682					name: "Alice",683					welcome: "Hello " + self.name + "!",684				}685			"#,686			r#"{"name": "Alice","welcome": "Hello Alice!"}"#687		);688		assert_json!(689			r#"690				{691					name: "Alice",692					welcome: "Hello " + self.name + "!",693				} + {694					name: "Bob"695				}696			"#,697			r#"{"name": "Bob","welcome": "Hello Bob!"}"#698		);699	}700701	#[test]702	fn functions() {703		assert_json!(r#"local a = function(b, c = 2) b + c; a(2)"#, "4");704		assert_json!(705			r#"local a = function(b, c = "Dear") b + c + d, d = "World"; a("Hello")"#,706			r#""HelloDearWorld""#707		);708	}709710	#[test]711	fn local_methods() {712		assert_json!(r#"local a(b, c = 2) = b + c; a(2)"#, "4");713		assert_json!(714			r#"local a(b, c = "Dear") = b + c + d, d = "World"; a("Hello")"#,715			r#""HelloDearWorld""#716		);717	}718719	#[test]720	fn object_locals() {721		assert_json!(r#"{local a = 3, b: a}"#, r#"{"b": 3}"#);722		assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b": 3}"#);723		assert_json!(724			r#"{local a = function (b) {[b]:4}, test: a("test")}"#,725			r#"{"test": {"test": 4}}"#726		);727	}728729	#[test]730	fn object_comp() {731		assert_json!(732			r#"{local t = "a", ["h"+i+"_"+z]: if "h"+(i-1)+"_"+z in self then t+1 else 0+t for i in [1,2,3] for z in [2,3,4] if z != i}"#,733			"{\"h1_2\": \"0a\",\"h1_3\": \"0a\",\"h1_4\": \"0a\",\"h2_3\": \"a1\",\"h2_4\": \"a1\",\"h3_2\": \"0a\",\"h3_4\": \"a1\"}"734		)735	}736737	#[test]738	fn direct_self() {739		println!(740			"{:#?}",741			eval!(742				r#"743					{744						local me = self,745						a: 3,746						b(): me.a,747					}748				"#749			)750		);751	}752753	#[test]754	fn indirect_self() {755		// `self` assigned to `me` was lost when being756		// referenced from field757		eval!(758			r#"{759				local me = self,760				a: 3,761				b: me.a,762			}.b"#763		);764	}765766	// We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly767	#[test]768	fn std_assert_ok() {769		eval!("std.assertEqual(4.5 << 2, 16)");770	}771772	#[test]773	#[should_panic]774	fn std_assert_failure() {775		eval!("std.assertEqual(4.5 << 2, 15)");776	}777778	#[test]779	fn string_is_string() {780		assert!(primitive_equals(781			&eval!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),782			&Val::Bool(false),783		)784		.unwrap());785	}786787	#[test]788	fn base64_works() {789		assert_json!(r#"std.base64("test")"#, r#""dGVzdA==""#);790	}791792	#[test]793	fn utf8_chars() {794		assert_json!(795			r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,796			r#"{"c": 128526,"l": 1}"#797		)798	}799800	#[test]801	fn json() {802		assert_json!(803			r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,804			r#""{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}""#805		);806	}807808	#[test]809	fn parse_json() {810		assert_json!(811			r#"std.parseJson('{"a": -1,"b": 1,"c": 3.141,"d": []}')"#,812			r#"{"a": -1,"b": 1,"c": 3.141,"d": []}"#813		);814		// TODO: this should in fact fail as is no proper JSON syntax815		assert_json!(816			r#"std.parseJson("{a:-1, b:1, c:3.141, d:[]}")"#,817			r#"{"a": -1,"b": 1,"c": 3.141,"d": []}"#818		);819		// TODO: this is also no valid JSON820		assert_json!(r#"std.parseJson('local x = 2; x * x')"#, r#"4"#);821	}822823	#[test]824	fn test() {825		assert_json!(826			r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,827			"[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"828		);829	}830831	#[test]832	fn sjsonnet() {833		eval!(834			r#"835			local x0 = {k: 1};836			local x1 = {k: x0.k + x0.k};837			local x2 = {k: x1.k + x1.k};838			local x3 = {k: x2.k + x2.k};839			local x4 = {k: x3.k + x3.k};840			local x5 = {k: x4.k + x4.k};841			local x6 = {k: x5.k + x5.k};842			local x7 = {k: x6.k + x6.k};843			local x8 = {k: x7.k + x7.k};844			local x9 = {k: x8.k + x8.k};845			local x10 = {k: x9.k + x9.k};846			local x11 = {k: x10.k + x10.k};847			local x12 = {k: x11.k + x11.k};848			local x13 = {k: x12.k + x12.k};849			local x14 = {k: x13.k + x13.k};850			local x15 = {k: x14.k + x14.k};851			local x16 = {k: x15.k + x15.k};852			local x17 = {k: x16.k + x16.k};853			local x18 = {k: x17.k + x17.k};854			local x19 = {k: x18.k + x18.k};855			local x20 = {k: x19.k + x19.k};856			local x21 = {k: x20.k + x20.k};857			x21.k858		"#859		);860	}861862	// This test is commented out by default, because of huge compilation slowdown863	// #[bench]864	// fn bench_codegen(b: &mut Bencher) {865	// 	b.iter(|| {866	// 		#[allow(clippy::all)]867	// 		let stdlib = {868	// 			use jrsonnet_parser::*;869	// 			include!(concat!(env!("OUT_DIR"), "/stdlib.rs"))870	// 		};871	// 		stdlib872	// 	})873	// }874875	/*876	#[bench]877	fn bench_serialize(b: &mut Bencher) {878		b.iter(|| {879			bincode::deserialize::<jrsonnet_parser::LocExpr>(include_bytes!(concat!(880				env!("OUT_DIR"),881				"/stdlib.bincode"882			)))883			.expect("deserialize stdlib")884		})885	}886887	#[bench]888	fn bench_parse(b: &mut Bencher) {889		b.iter(|| {890			jrsonnet_parser::parse(891				jrsonnet_stdlib::STDLIB_STR,892				&jrsonnet_parser::ParserSettings {893					loc_data: true,894					file_name: Rc::new(PathBuf::from("std.jsonnet")),895				},896			)897		})898	}899	*/900901	#[test]902	fn equality() {903		println!(904			"{:?}",905			jrsonnet_parser::parse(906				"{ x: 1, y: 2 } == { x: 1, y: 2 }",907				&ParserSettings {908					file_name: PathBuf::from("equality").into(),909					loc_data: true,910				}911			)912		);913		assert_eval!("{ x: 1, y: 2 } == { x: 1, y: 2 }")914	}915916	#[test]917	fn native_ext() -> crate::error::Result<()> {918		use super::native::NativeCallback;919		let evaluator = EvaluationState::default();920921		evaluator.with_stdlib();922		evaluator.settings_mut().ext_natives.insert(923			"native_add".into(),924			Rc::new(NativeCallback::new(925				ParamsDesc(Rc::new(vec![926					Param("a".into(), None),927					Param("b".into(), None),928				])),929				|caller, args| {930					assert_eq!(931						&caller.unwrap() as &Path,932						&PathBuf::from("native_caller.jsonnet")933					);934					match (&args[0], &args[1]) {935						(Val::Num(a), Val::Num(b)) => Ok(Val::Num(a + b)),936						(_, _) => unreachable!(),937					}938				},939			)),940		);941		evaluator.evaluate_snippet_raw(942			PathBuf::from("native_caller.jsonnet").into(),943			"std.assertEqual(std.native(\"native_add\")(1, 2), 3)".into(),944		)?;945		Ok(())946	}947948	#[test]949	fn constant_intrinsic() -> crate::error::Result<()> {950		assert_eval!(951			"local std2 = std; local std = std2 { primitiveEquals(a, b):: false }; 1 == 1"952		);953		Ok(())954	}955956	#[test]957	fn standalone_super() -> crate::error::Result<()> {958		assert_eval!(959			r#"960			local obj = {961				a: 1,962				b: 2,963				c: 3,964			};965			local test = obj + {966				fields: std.objectFields(super),967				d: 5,968			};969			test.fields == ['a', 'b', 'c']970		"#971		);972		Ok(())973	}974975	#[test]976	fn comp_self() -> crate::error::Result<()> {977		assert_eval!(978			r#"979			std.objectFields({980				a:{981					[name]: name for name in std.objectFields(self)982				},983				b: 2,984				c: 3,985			}.a) == ['a', 'b', 'c']986			"#987		);988989		Ok(())990	}991992	struct TestImportResolver(IStr);993	impl crate::import::ImportResolver for TestImportResolver {994		fn resolve_file(&self, _: &Path, _: &Path) -> crate::error::Result<Rc<Path>> {995			Ok(PathBuf::from("/test").into())996		}997998		fn load_file_contents(&self, _: &Path) -> crate::error::Result<IStr> {999			Ok(self.0.clone())1000		}10011002		unsafe fn as_any(&self) -> &dyn std::any::Any {1003			panic!()1004		}1005	}10061007	#[test]1008	fn issue_23() {1009		let state = EvaluationState::default();1010		state.set_import_resolver(Box::new(TestImportResolver(r#"import "/test""#.into())));1011		let _ = state.evaluate_file_raw(&PathBuf::from("/test"));1012	}10131014	#[test]1015	fn issue_40() {1016		let state = EvaluationState::default();1017		state.with_stdlib();10181019		let error = state1020			.evaluate_snippet_raw(1021				PathBuf::from("issue40.jsonnet").into(),1022				r#"1023				local conf = {1024					n: ""1025				};1026				1027				local result = conf + {1028					assert std.isNumber(self.n): "is number"1029				};10301031				std.manifestJsonEx(result, "")1032			"#1033				.into(),1034			)1035			.unwrap_err();1036		assert_eq!(error.error().to_string(), "assert failed: is number");1037	}1038}
modifiedcrates/jrsonnet-evaluator/src/native.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/native.rs
+++ b/crates/jrsonnet-evaluator/src/native.rs
@@ -3,24 +3,24 @@
 use crate::{error::Result, Val};
 use jrsonnet_parser::ParamsDesc;
 use std::fmt::Debug;
-use std::path::PathBuf;
+use std::path::Path;
 use std::rc::Rc;
 
 pub struct NativeCallback {
 	pub params: ParamsDesc,
-	handler: Box<dyn Fn(Option<Rc<PathBuf>>, &[Val]) -> Result<Val>>,
+	handler: Box<dyn Fn(Option<Rc<Path>>, &[Val]) -> Result<Val>>,
 }
 impl NativeCallback {
 	pub fn new(
 		params: ParamsDesc,
-		handler: impl Fn(Option<Rc<PathBuf>>, &[Val]) -> Result<Val> + 'static,
+		handler: impl Fn(Option<Rc<Path>>, &[Val]) -> Result<Val> + 'static,
 	) -> Self {
 		Self {
 			params,
 			handler: Box::new(handler),
 		}
 	}
-	pub fn call(&self, caller: Option<Rc<PathBuf>>, args: &[Val]) -> Result<Val> {
+	pub fn call(&self, caller: Option<Rc<Path>>, args: &[Val]) -> Result<Val> {
 		(self.handler)(caller, args)
 	}
 }
modifiedcrates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/trace/mod.rs
+++ b/crates/jrsonnet-evaluator/src/trace/mod.rs
@@ -2,7 +2,7 @@
 
 use crate::{error::Error, EvaluationState, LocError};
 pub use location::*;
-use std::path::PathBuf;
+use std::path::{Path, PathBuf};
 
 /// The way paths should be displayed
 pub enum PathResolver {
@@ -15,7 +15,7 @@
 }
 
 impl PathResolver {
-	pub fn resolve(&self, from: &PathBuf) -> String {
+	pub fn resolve(&self, from: &Path) -> String {
 		match self {
 			Self::FileName => from.file_name().unwrap().to_string_lossy().into_owned(),
 			Self::Absolute => from.to_string_lossy().into_owned(),
@@ -255,7 +255,7 @@
 		&self,
 		out: &mut dyn std::fmt::Write,
 		source: &str,
-		origin: &PathBuf,
+		origin: &Path,
 		start: &CodeLocation,
 		end: &CodeLocation,
 		desc: &str,
modifiedcrates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -6,7 +6,7 @@
 use std::{
 	fmt::{Debug, Display},
 	ops::Deref,
-	path::PathBuf,
+	path::{Path, PathBuf},
 	rc::Rc,
 };
 
@@ -320,7 +320,7 @@
 #[cfg_attr(feature = "serialize", derive(Serialize))]
 #[cfg_attr(feature = "deserialize", derive(Deserialize))]
 #[derive(Clone, PartialEq)]
-pub struct ExprLocation(pub Rc<PathBuf>, pub usize, pub usize);
+pub struct ExprLocation(pub Rc<Path>, pub usize, pub usize);
 impl Debug for ExprLocation {
 	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 		write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)
modifiedcrates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -1,15 +1,17 @@
 #![allow(clippy::redundant_closure_call)]
 
 use peg::parser;
-use std::{path::PathBuf, rc::Rc};
+use std::{
+	path::{Path, PathBuf},
+	rc::Rc,
+};
 mod expr;
 pub use expr::*;
 pub use peg;
 
-#[derive(Default)]
 pub struct ParserSettings {
 	pub loc_data: bool,
-	pub file_name: Rc<PathBuf>,
+	pub file_name: Rc<Path>,
 }
 
 parser! {
@@ -304,7 +306,6 @@
 	use super::{expr::*, parse};
 	use crate::ParserSettings;
 	use std::path::PathBuf;
-	use std::rc::Rc;
 
 	macro_rules! parse {
 		($s:expr) => {
@@ -312,7 +313,7 @@
 				$s,
 				&ParserSettings {
 					loc_data: false,
-					file_name: Rc::new(PathBuf::from("/test.jsonnet")),
+					file_name: PathBuf::from("/test.jsonnet").into(),
 				},
 			)
 			.unwrap()