git.delta.rocks / jrsonnet / refs/commits / 1dbd15d5102e

difftreelog

feat import support

Лач2020-06-10parent: #ff03a2a.patch.diff
in: master

3 files changed

modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -1,7 +1,7 @@
 pub mod location;
 
 use clap::Clap;
-use jsonnet_evaluator::{EvaluationState, LocError, StackTrace, Val};
+use jsonnet_evaluator::{EvaluationSettings, EvaluationState, LocError, StackTrace, Val};
 use location::{offset_to_location, CodeLocation};
 use std::env::current_dir;
 use std::{path::PathBuf, str::FromStr};
@@ -111,7 +111,10 @@
 
 fn main() {
 	let opts: Opts = Opts::parse();
-	let evaluator = jsonnet_evaluator::EvaluationState::default();
+	let evaluator = jsonnet_evaluator::EvaluationState::new(EvaluationSettings {
+		import_resolver: Box::new(|path| String::from_utf8(std::fs::read(path).unwrap()).unwrap()),
+		..Default::default()
+	});
 	if !opts.no_stdlib {
 		evaluator.with_stdlib();
 	}
modifiedcrates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/evaluate.rs
+++ b/crates/jsonnet-evaluator/src/evaluate.rs
@@ -1,5 +1,5 @@
 use crate::{
-	binding, context_creator, create_error, future_wrapper, lazy_val, push, Context,
+	binding, context_creator, create_error, future_wrapper, lazy_val, push, with_state, Context,
 	ContextCreator, FuncDesc, LazyBinding, ObjMember, ObjValue, Result, Val,
 };
 use closure::closure;
@@ -607,6 +607,16 @@
 				}
 			}
 		}
+		Import(path) => {
+			let mut lib_path = loc
+				.clone()
+				.expect("imports can't be used without loc_data")
+				.0
+				.clone();
+			lib_path.pop();
+			lib_path.push(path);
+			with_state(|s| s.import_file(&lib_path))?
+		}
 		_ => panic!(
 			"evaluation not implemented: {:?}",
 			LocExpr(expr.clone(), loc.clone())
modifiedcrates/jsonnet-evaluator/src/lib.rsdiffbeforeafterboth
before · crates/jsonnet-evaluator/src/lib.rs
1#![feature(box_syntax, box_patterns)]2#![feature(type_alias_impl_trait)]3#![feature(debug_non_exhaustive)]4#![allow(macro_expanded_macro_exports_accessed_by_absolute_paths)]5#![feature(stmt_expr_attributes)]6mod ctx;7mod dynamic;8mod error;9mod evaluate;10mod function;11mod map;12mod obj;13mod val;1415pub use ctx::*;16pub use dynamic::*;17pub use error::*;18pub use evaluate::*;19pub use function::parse_function_call;20use jsonnet_parser::*;21pub use obj::*;22use std::{cell::RefCell, collections::HashMap, fmt::Debug, path::PathBuf, rc::Rc};23pub use val::*;2425rc_fn_helper!(26	Binding,27	binding,28	dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<Val>29);3031type BindableFn = dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<LazyVal>;32#[derive(Clone)]33pub enum LazyBinding {34	Bindable(Rc<BindableFn>),35	Bound(LazyVal),36}3738impl Debug for LazyBinding {39	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {40		write!(f, "LazyBinding")41	}42}43impl LazyBinding {44	pub fn evaluate(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {45		match self {46			LazyBinding::Bindable(v) => v(this, super_obj),47			LazyBinding::Bound(v) => Ok(v.clone()),48		}49	}50}5152pub struct EvaluationSettings {53	max_stack_frames: usize,54	max_stack_trace_size: usize,55}56impl Default for EvaluationSettings {57	fn default() -> Self {58		EvaluationSettings {59			max_stack_frames: 200,60			max_stack_trace_size: 20,61		}62	}63}6465pub struct FileData(String, LocExpr, Option<Val>);66#[derive(Default)]67pub struct EvaluationStateInternals {68	/// Used for stack-overflows and stacktraces69	stack: RefCell<Vec<StackTraceElement>>,70	/// Contains file source codes and evaluated results for imports and pretty71	/// printing stacktraces72	files: RefCell<HashMap<PathBuf, FileData>>,73	globals: RefCell<HashMap<String, Val>>,7475	/// Values to use with std.extVar76	ext_vars: RefCell<HashMap<String, Val>>,7778	settings: EvaluationSettings,79}8081thread_local! {82	/// Contains state for currently executing file83	/// Global state is fine there84	pub(crate) static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)85}86#[inline(always)]87pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {88	EVAL_STATE.with(89		#[inline(always)]90		|s| f(s.borrow().as_ref().unwrap()),91	)92}93pub(crate) fn create_error<T>(err: Error) -> Result<T> {94	with_state(|s| s.error(err))95}96#[inline(always)]97pub(crate) fn push<T>(e: LocExpr, comment: String, f: impl FnOnce() -> Result<T>) -> Result<T> {98	with_state(|s| s.push(e, comment, f))99}100101/// Maintains stack trace and import resolution102#[derive(Default, Clone)]103pub struct EvaluationState(Rc<EvaluationStateInternals>);104impl EvaluationState {105	pub fn add_file(&self, name: PathBuf, code: String) -> std::result::Result<(), ParseError> {106		self.0.files.borrow_mut().insert(107			name.clone(),108			FileData(109				code.clone(),110				parse(111					&code,112					&ParserSettings {113						file_name: name,114						loc_data: true,115					},116				)?,117				None,118			),119		);120121		Ok(())122	}123	pub fn add_parsed_file(124		&self,125		name: PathBuf,126		code: String,127		parsed: LocExpr,128	) -> std::result::Result<(), ()> {129		self.0130			.files131			.borrow_mut()132			.insert(name, FileData(code, parsed, None));133134		Ok(())135	}136	pub fn get_source(&self, name: &PathBuf) -> Option<String> {137		let ro_map = self.0.files.borrow();138		ro_map.get(name).map(|value| value.0.clone())139	}140	pub fn evaluate_file(&self, name: &PathBuf) -> Result<Val> {141		self.begin_state();142		let expr: LocExpr = {143			let ro_map = self.0.files.borrow();144			let value = ro_map145				.get(name)146				.unwrap_or_else(|| panic!("file not added: {:?}", name));147			if value.2.is_some() {148				return Ok(value.2.clone().unwrap());149			}150			value.1.clone()151		};152		let value = evaluate(self.create_default_context()?, &expr)?;153		{154			self.0155				.files156				.borrow_mut()157				.get_mut(name)158				.unwrap()159				.2160				.replace(value.clone());161		}162		self.end_state();163		Ok(value)164	}165166	pub fn parse_evaluate_raw(&self, code: &str) -> Result<Val> {167		let parsed = parse(168			&code,169			&ParserSettings {170				file_name: PathBuf::from("raw.jsonnet"),171				loc_data: true,172			},173		);174		self.begin_state();175		let value = evaluate(self.create_default_context()?, &parsed.unwrap());176		self.end_state();177		value178	}179180	pub fn add_global(&self, name: String, value: Val) {181		self.0.globals.borrow_mut().insert(name, value);182	}183	pub fn add_ext_var(&self, name: String, value: Val) {184		self.0.ext_vars.borrow_mut().insert(name, value);185	}186187	pub fn with_stdlib(&self) -> &Self {188		self.begin_state();189		use jsonnet_stdlib::STDLIB_STR;190		if cfg!(feature = "serialized-stdlib") {191			self.add_parsed_file(192				PathBuf::from("std.jsonnet"),193				STDLIB_STR.to_owned(),194				bincode::deserialize(include_bytes!(concat!(env!("OUT_DIR"), "/stdlib.bincode")))195					.expect("deserialize stdlib"),196			)197			.unwrap();198		} else {199			self.add_file(PathBuf::from("std.jsonnet"), STDLIB_STR.to_owned())200				.unwrap();201		}202		let val = self.evaluate_file(&PathBuf::from("std.jsonnet")).unwrap();203		self.add_global("std".to_owned(), val);204		self.end_state();205		self206	}207208	pub fn create_default_context(&self) -> Result<Context> {209		let globals = self.0.globals.borrow();210		let mut new_bindings: HashMap<String, LazyBinding> = HashMap::new();211		for (name, value) in globals.iter() {212			new_bindings.insert(213				name.clone(),214				LazyBinding::Bound(resolved_lazy_val!(value.clone())),215			);216		}217		Context::new().extend_unbound(new_bindings, None, None, None)218	}219220	#[inline(always)]221	pub fn push<T>(&self, e: LocExpr, comment: String, f: impl FnOnce() -> Result<T>) -> Result<T> {222		{223			let mut stack = self.0.stack.borrow_mut();224			if stack.len() > self.0.settings.max_stack_frames {225				drop(stack);226				return self.error(Error::StackOverflow);227			} else {228				stack.push(StackTraceElement(e, comment));229			}230		}231		let result = f();232		self.0.stack.borrow_mut().pop();233		result234	}235	pub fn print_stack_trace(&self) {236		for e in self.stack_trace().0 {237			println!("{:?} - {:?}", e.0, e.1)238		}239	}240	pub fn stack_trace(&self) -> StackTrace {241		StackTrace(242			self.0243				.stack244				.borrow()245				.iter()246				.rev()247				.take(self.0.settings.max_stack_trace_size)248				.cloned()249				.collect(),250		)251	}252	pub fn error<T>(&self, err: Error) -> Result<T> {253		Err(LocError(err, self.stack_trace()))254	}255256	fn begin_state(&self) {257		EVAL_STATE.with(|v| v.borrow_mut().replace(self.clone()));258	}259	fn end_state(&self) {260		EVAL_STATE.with(|v| v.borrow_mut().take());261	}262}263264#[cfg(test)]265pub mod tests {266	use super::Val;267	use crate::EvaluationState;268	use jsonnet_parser::*;269	use std::path::PathBuf;270271	#[test]272	fn eval_state_stacktrace() {273		let state = EvaluationState::default();274		state275			.push(276				loc_expr!(277					Expr::Num(0.0),278					true,279					(PathBuf::from("test1.jsonnet"), 10, 20)280				),281				"outer".to_owned(),282				|| {283					state.push(284						loc_expr!(285							Expr::Num(0.0),286							true,287							(PathBuf::from("test2.jsonnet"), 30, 40)288						),289						"inner".to_owned(),290						|| {291							state.print_stack_trace();292							Ok(())293						},294					)?;295					Ok(())296				},297			)298			.unwrap();299	}300301	#[test]302	fn eval_state_standard() {303		let state = EvaluationState::default();304		state.with_stdlib();305		assert_eq!(306			state307				.parse_evaluate_raw(r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#)308				.unwrap(),309			Val::Bool(true)310		);311	}312313	macro_rules! eval {314		($str: expr) => {315			EvaluationState::default()316				.with_stdlib()317				.parse_evaluate_raw($str)318				.unwrap()319		};320	}321	macro_rules! eval_json {322		($str: expr) => {{323			let evaluator = EvaluationState::default();324			evaluator.with_stdlib();325			let val = evaluator.parse_evaluate_raw($str).unwrap();326			evaluator.add_global("__tmp__to_yaml__".to_owned(), val);327			evaluator328				.parse_evaluate_raw("std.manifestJsonEx(__tmp__to_yaml__, \"\")")329				.unwrap()330				.try_cast_str("there should be json string")331				.unwrap()332				.clone()333				.replace("\n", "")334			}};335	}336337	/// Asserts given code returns `true`338	macro_rules! assert_eval {339		($str: expr) => {340			assert_eq!(eval!($str), Val::Bool(true))341		};342	}343344	/// Asserts given code returns `false`345	macro_rules! assert_eval_neg {346		($str: expr) => {347			assert_eq!(eval!($str), Val::Bool(false))348		};349	}350	macro_rules! assert_json {351		($str: expr, $out: expr) => {352			assert_eq!(eval_json!($str), $out.replace("\t", ""))353		};354	}355356	/// Sanity checking, before trusting to another tests357	#[test]358	fn equality_operator() {359		assert_eval!("2 == 2");360		assert_eval_neg!("2 != 2");361		assert_eval!("2 != 3");362		assert_eval_neg!("2 == 3");363		assert_eval!("'Hello' == 'Hello'");364		assert_eval_neg!("'Hello' != 'Hello'");365		assert_eval!("'Hello' != 'World'");366		assert_eval_neg!("'Hello' == 'World'");367	}368369	#[test]370	fn math_evaluation() {371		assert_eval!("2 + 2 * 2 == 6");372		assert_eval!("3 + (2 + 2 * 2) == 9");373	}374375	#[test]376	fn string_concat() {377		assert_eval!("'Hello' + 'World' == 'HelloWorld'");378		assert_eval!("'Hello' * 3 == 'HelloHelloHello'");379		assert_eval!("'Hello' + 'World' * 3 == 'HelloWorldWorldWorld'");380	}381382	#[test]383	fn function_contexts() {384		assert_eval!(385			r#"386				local k = {387					t(name = self.h): [self.h, name],388					h: 3,389				};390				local f = {391					t: k.t(),392					h: 4,393				};394				f.t[0] == f.t[1]395			"#396		);397	}398399	#[test]400	fn local() {401		assert_eval!("local a = 2; local b = 3; a + b == 5");402		assert_eval!("local a = 1, b = a + 1; a + b == 3");403		assert_eval!("local a = 1; local a = 2; a == 2");404	}405406	#[test]407	fn object_lazyness() {408		assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);409	}410411	/// FIXME: This test gets stackoverflow in debug build412	#[test]413	fn object_inheritance() {414		assert_json!("{a: self.b} + {b:3}", r#"{"a": 3,"b": 3}"#);415	}416417	#[test]418	fn test_object() {419		assert_json!("{a:2}", r#"{"a": 2}"#);420		assert_json!("{a:2+2}", r#"{"a": 4}"#);421		assert_json!("{a:2}+{b:2}", r#"{"a": 2,"b": 2}"#);422		assert_json!("{b:3}+{b:2}", r#"{"b": 2}"#);423		assert_json!("{b:3}+{b+:2}", r#"{"b": 5}"#);424		assert_json!("local test='a'; {[test]:2}", r#"{"a": 2}"#);425		assert_json!(426			r#"427				{428					name: "Alice",429					welcome: "Hello " + self.name + "!",430				}431			"#,432			r#"{"name": "Alice","welcome": "Hello Alice!"}"#433		);434		assert_json!(435			r#"436				{437					name: "Alice",438					welcome: "Hello " + self.name + "!",439				} + {440					name: "Bob"441				}442			"#,443			r#"{"name": "Bob","welcome": "Hello Bob!"}"#444		);445	}446447	#[test]448	fn functions() {449		assert_json!(r#"local a = function(b, c = 2) b + c; a(2)"#, "4");450		assert_json!(451			r#"local a = function(b, c = "Dear") b + c + d, d = "World"; a("Hello")"#,452			r#""HelloDearWorld""#453		);454	}455456	#[test]457	fn local_methods() {458		assert_json!(r#"local a(b, c = 2) = b + c; a(2)"#, "4");459		assert_json!(460			r#"local a(b, c = "Dear") = b + c + d, d = "World"; a("Hello")"#,461			r#""HelloDearWorld""#462		);463	}464465	#[test]466	fn object_locals() {467		assert_json!(r#"{local a = 3, b: a}"#, r#"{"b": 3}"#);468		assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b": 3}"#);469		assert_json!(470			r#"{local a = function (b) {[b]:4}, test: a("test")}"#,471			r#"{"test": {"test": 4}}"#472		);473	}474475	#[test]476	fn direct_self() {477		println!(478			"{:#?}",479			eval!(480				r#"481					{482						local me = self,483						a: 3,484						b(): me.a,485					}486				"#487			)488		);489	}490491	#[test]492	fn indirect_self() {493		// `self` assigned to `me` was lost when being494		// referenced from field495		eval!(496			r#"{497				local me = self,498				a: 3,499				b: me.a,500			}.b"#501		);502	}503504	// We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly505	#[test]506	fn std_assert_ok() {507		eval!("std.assertEqual(4.5 << 2, 16)");508	}509510	#[test]511	#[should_panic]512	fn std_assert_failure() {513		eval!("std.assertEqual(4.5 << 2, 15)");514	}515516	#[test]517	fn string_is_string() {518		assert_eq!(519			eval!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),520			Val::Bool(false)521		);522	}523524	#[test]525	fn base64_works() {526		assert_json!(r#"std.base64("test")"#, r#""dGVzdA==""#);527	}528529	#[test]530	fn utf8_chars() {531		assert_json!(532			r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,533			r#"{"c": 128526,"l": 1}"#534		)535	}536537	#[test]538	fn json() {539		assert_json!(540			r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,541			r#""{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}""#542		);543	}544545	#[test]546	fn test() {547		assert_json!(548			r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,549			"[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"550		);551	}552553	#[test]554	fn sjsonnet() {555		eval!(556			r#"557			local x0 = {k: 1};558			local x1 = {k: x0.k + x0.k};559			local x2 = {k: x1.k + x1.k};560			local x3 = {k: x2.k + x2.k};561			local x4 = {k: x3.k + x3.k};562			local x5 = {k: x4.k + x4.k};563			local x6 = {k: x5.k + x5.k};564			local x7 = {k: x6.k + x6.k};565			local x8 = {k: x7.k + x7.k};566			local x9 = {k: x8.k + x8.k};567			local x10 = {k: x9.k + x9.k};568			local x11 = {k: x10.k + x10.k};569			local x12 = {k: x11.k + x11.k};570			local x13 = {k: x12.k + x12.k};571			local x14 = {k: x13.k + x13.k};572			local x15 = {k: x14.k + x14.k};573			local x16 = {k: x15.k + x15.k};574			local x17 = {k: x16.k + x16.k};575			local x18 = {k: x17.k + x17.k};576			local x19 = {k: x18.k + x18.k};577			local x20 = {k: x19.k + x19.k};578			local x21 = {k: x20.k + x20.k};579			x21.k580		"#581		);582	}583}
after · crates/jsonnet-evaluator/src/lib.rs
1#![feature(box_syntax, box_patterns)]2#![feature(type_alias_impl_trait)]3#![feature(debug_non_exhaustive)]4#![allow(macro_expanded_macro_exports_accessed_by_absolute_paths)]5#![feature(stmt_expr_attributes)]6mod ctx;7mod dynamic;8mod error;9mod evaluate;10mod function;11mod map;12mod obj;13mod val;1415pub use ctx::*;16pub use dynamic::*;17pub use error::*;18pub use evaluate::*;19pub use function::parse_function_call;20use jsonnet_parser::*;21pub use obj::*;22use std::{cell::RefCell, collections::HashMap, fmt::Debug, path::PathBuf, rc::Rc};23pub use val::*;2425rc_fn_helper!(26	Binding,27	binding,28	dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<Val>29);3031type BindableFn = dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<LazyVal>;32#[derive(Clone)]33pub enum LazyBinding {34	Bindable(Rc<BindableFn>),35	Bound(LazyVal),36}3738impl Debug for LazyBinding {39	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {40		write!(f, "LazyBinding")41	}42}43impl LazyBinding {44	pub fn evaluate(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {45		match self {46			LazyBinding::Bindable(v) => v(this, super_obj),47			LazyBinding::Bound(v) => Ok(v.clone()),48		}49	}50}5152pub struct EvaluationSettings {53	pub max_stack_frames: usize,54	pub max_stack_trace_size: usize,55	pub import_resolver: Box<dyn Fn(&PathBuf) -> String>,56}57impl Default for EvaluationSettings {58	fn default() -> Self {59		EvaluationSettings {60			max_stack_frames: 200,61			max_stack_trace_size: 20,62			import_resolver: Box::new(|path| {63				panic!("default EvaluationSettings have no support for import resolution, can't import {:?}", path)64			}),65		}66	}67}6869pub struct FileData(String, LocExpr, Option<Val>);70#[derive(Default)]71pub struct EvaluationStateInternals {72	/// Used for stack-overflows and stacktraces73	stack: RefCell<Vec<StackTraceElement>>,74	/// Contains file source codes and evaluated results for imports and pretty75	/// printing stacktraces76	files: RefCell<HashMap<PathBuf, FileData>>,77	globals: RefCell<HashMap<String, Val>>,7879	/// Values to use with std.extVar80	ext_vars: RefCell<HashMap<String, Val>>,8182	settings: EvaluationSettings,83}8485thread_local! {86	/// Contains state for currently executing file87	/// Global state is fine there88	pub(crate) static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)89}90#[inline(always)]91pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {92	EVAL_STATE.with(93		#[inline(always)]94		|s| f(s.borrow().as_ref().unwrap()),95	)96}97pub(crate) fn create_error<T>(err: Error) -> Result<T> {98	with_state(|s| s.error(err))99}100#[inline(always)]101pub(crate) fn push<T>(e: LocExpr, comment: String, f: impl FnOnce() -> Result<T>) -> Result<T> {102	with_state(|s| s.push(e, comment, f))103}104105/// Maintains stack trace and import resolution106#[derive(Default, Clone)]107pub struct EvaluationState(Rc<EvaluationStateInternals>);108impl EvaluationState {109	pub fn new(settings: EvaluationSettings) -> Self {110		EvaluationState(Rc::new(EvaluationStateInternals {111			settings,112			..Default::default()113		}))114	}115	pub fn add_file(&self, name: PathBuf, code: String) -> std::result::Result<(), ParseError> {116		self.0.files.borrow_mut().insert(117			name.clone(),118			FileData(119				code.clone(),120				parse(121					&code,122					&ParserSettings {123						file_name: name,124						loc_data: true,125					},126				)?,127				None,128			),129		);130131		Ok(())132	}133	pub fn add_parsed_file(134		&self,135		name: PathBuf,136		code: String,137		parsed: LocExpr,138	) -> std::result::Result<(), ()> {139		self.0140			.files141			.borrow_mut()142			.insert(name, FileData(code, parsed, None));143144		Ok(())145	}146	pub fn get_source(&self, name: &PathBuf) -> Option<String> {147		let ro_map = self.0.files.borrow();148		ro_map.get(name).map(|value| value.0.clone())149	}150	pub fn evaluate_file(&self, name: &PathBuf) -> Result<Val> {151		self.begin_state();152		let value = self.evaluate_file_in_current_state(name)?;153		self.end_state();154		Ok(value)155	}156	pub(crate) fn evaluate_file_in_current_state(&self, name: &PathBuf) -> Result<Val> {157		let expr: LocExpr = {158			let ro_map = self.0.files.borrow();159			let value = ro_map160				.get(name)161				.unwrap_or_else(|| panic!("file not added: {:?}", name));162			if value.2.is_some() {163				return Ok(value.2.clone().unwrap());164			}165			value.1.clone()166		};167		let value = evaluate(self.create_default_context()?, &expr)?;168		{169			self.0170				.files171				.borrow_mut()172				.get_mut(name)173				.unwrap()174				.2175				.replace(value.clone());176		}177		Ok(value)178	}179	pub(crate) fn import_file(&self, path: &PathBuf) -> Result<Val> {180		if !self.0.files.borrow().contains_key(path) {181			let file_str = (self.0.settings.import_resolver)(path);182			self.add_file(path.clone(), file_str).unwrap();183		}184		self.evaluate_file_in_current_state(path)185	}186187	pub fn parse_evaluate_raw(&self, code: &str) -> Result<Val> {188		let parsed = parse(189			&code,190			&ParserSettings {191				file_name: PathBuf::from("raw.jsonnet"),192				loc_data: true,193			},194		);195		self.begin_state();196		let value = evaluate(self.create_default_context()?, &parsed.unwrap());197		self.end_state();198		value199	}200201	pub fn add_global(&self, name: String, value: Val) {202		self.0.globals.borrow_mut().insert(name, value);203	}204	pub fn add_ext_var(&self, name: String, value: Val) {205		self.0.ext_vars.borrow_mut().insert(name, value);206	}207208	pub fn with_stdlib(&self) -> &Self {209		self.begin_state();210		use jsonnet_stdlib::STDLIB_STR;211		if cfg!(feature = "serialized-stdlib") {212			self.add_parsed_file(213				PathBuf::from("std.jsonnet"),214				STDLIB_STR.to_owned(),215				bincode::deserialize(include_bytes!(concat!(env!("OUT_DIR"), "/stdlib.bincode")))216					.expect("deserialize stdlib"),217			)218			.unwrap();219		} else {220			self.add_file(PathBuf::from("std.jsonnet"), STDLIB_STR.to_owned())221				.unwrap();222		}223		let val = self.evaluate_file(&PathBuf::from("std.jsonnet")).unwrap();224		self.add_global("std".to_owned(), val);225		self.end_state();226		self227	}228229	pub fn create_default_context(&self) -> Result<Context> {230		let globals = self.0.globals.borrow();231		let mut new_bindings: HashMap<String, LazyBinding> = HashMap::new();232		for (name, value) in globals.iter() {233			new_bindings.insert(234				name.clone(),235				LazyBinding::Bound(resolved_lazy_val!(value.clone())),236			);237		}238		Context::new().extend_unbound(new_bindings, None, None, None)239	}240241	#[inline(always)]242	pub fn push<T>(&self, e: LocExpr, comment: String, f: impl FnOnce() -> Result<T>) -> Result<T> {243		{244			let mut stack = self.0.stack.borrow_mut();245			if stack.len() > self.0.settings.max_stack_frames {246				drop(stack);247				return self.error(Error::StackOverflow);248			} else {249				stack.push(StackTraceElement(e, comment));250			}251		}252		let result = f();253		self.0.stack.borrow_mut().pop();254		result255	}256	pub fn print_stack_trace(&self) {257		for e in self.stack_trace().0 {258			println!("{:?} - {:?}", e.0, e.1)259		}260	}261	pub fn stack_trace(&self) -> StackTrace {262		StackTrace(263			self.0264				.stack265				.borrow()266				.iter()267				.rev()268				.take(self.0.settings.max_stack_trace_size)269				.cloned()270				.collect(),271		)272	}273	pub fn error<T>(&self, err: Error) -> Result<T> {274		Err(LocError(err, self.stack_trace()))275	}276277	fn begin_state(&self) {278		EVAL_STATE.with(|v| v.borrow_mut().replace(self.clone()));279	}280	fn end_state(&self) {281		EVAL_STATE.with(|v| v.borrow_mut().take());282	}283}284285#[cfg(test)]286pub mod tests {287	use super::Val;288	use crate::EvaluationState;289	use jsonnet_parser::*;290	use std::path::PathBuf;291292	#[test]293	fn eval_state_stacktrace() {294		let state = EvaluationState::default();295		state296			.push(297				loc_expr!(298					Expr::Num(0.0),299					true,300					(PathBuf::from("test1.jsonnet"), 10, 20)301				),302				"outer".to_owned(),303				|| {304					state.push(305						loc_expr!(306							Expr::Num(0.0),307							true,308							(PathBuf::from("test2.jsonnet"), 30, 40)309						),310						"inner".to_owned(),311						|| {312							state.print_stack_trace();313							Ok(())314						},315					)?;316					Ok(())317				},318			)319			.unwrap();320	}321322	#[test]323	fn eval_state_standard() {324		let state = EvaluationState::default();325		state.with_stdlib();326		assert_eq!(327			state328				.parse_evaluate_raw(r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#)329				.unwrap(),330			Val::Bool(true)331		);332	}333334	macro_rules! eval {335		($str: expr) => {336			EvaluationState::default()337				.with_stdlib()338				.parse_evaluate_raw($str)339				.unwrap()340		};341	}342	macro_rules! eval_json {343		($str: expr) => {{344			let evaluator = EvaluationState::default();345			evaluator.with_stdlib();346			let val = evaluator.parse_evaluate_raw($str).unwrap();347			evaluator.add_global("__tmp__to_yaml__".to_owned(), val);348			evaluator349				.parse_evaluate_raw("std.manifestJsonEx(__tmp__to_yaml__, \"\")")350				.unwrap()351				.try_cast_str("there should be json string")352				.unwrap()353				.clone()354				.replace("\n", "")355			}};356	}357358	/// Asserts given code returns `true`359	macro_rules! assert_eval {360		($str: expr) => {361			assert_eq!(eval!($str), Val::Bool(true))362		};363	}364365	/// Asserts given code returns `false`366	macro_rules! assert_eval_neg {367		($str: expr) => {368			assert_eq!(eval!($str), Val::Bool(false))369		};370	}371	macro_rules! assert_json {372		($str: expr, $out: expr) => {373			assert_eq!(eval_json!($str), $out.replace("\t", ""))374		};375	}376377	/// Sanity checking, before trusting to another tests378	#[test]379	fn equality_operator() {380		assert_eval!("2 == 2");381		assert_eval_neg!("2 != 2");382		assert_eval!("2 != 3");383		assert_eval_neg!("2 == 3");384		assert_eval!("'Hello' == 'Hello'");385		assert_eval_neg!("'Hello' != 'Hello'");386		assert_eval!("'Hello' != 'World'");387		assert_eval_neg!("'Hello' == 'World'");388	}389390	#[test]391	fn math_evaluation() {392		assert_eval!("2 + 2 * 2 == 6");393		assert_eval!("3 + (2 + 2 * 2) == 9");394	}395396	#[test]397	fn string_concat() {398		assert_eval!("'Hello' + 'World' == 'HelloWorld'");399		assert_eval!("'Hello' * 3 == 'HelloHelloHello'");400		assert_eval!("'Hello' + 'World' * 3 == 'HelloWorldWorldWorld'");401	}402403	#[test]404	fn function_contexts() {405		assert_eval!(406			r#"407				local k = {408					t(name = self.h): [self.h, name],409					h: 3,410				};411				local f = {412					t: k.t(),413					h: 4,414				};415				f.t[0] == f.t[1]416			"#417		);418	}419420	#[test]421	fn local() {422		assert_eval!("local a = 2; local b = 3; a + b == 5");423		assert_eval!("local a = 1, b = a + 1; a + b == 3");424		assert_eval!("local a = 1; local a = 2; a == 2");425	}426427	#[test]428	fn object_lazyness() {429		assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);430	}431432	/// FIXME: This test gets stackoverflow in debug build433	#[test]434	fn object_inheritance() {435		assert_json!("{a: self.b} + {b:3}", r#"{"a": 3,"b": 3}"#);436	}437438	#[test]439	fn test_object() {440		assert_json!("{a:2}", r#"{"a": 2}"#);441		assert_json!("{a:2+2}", r#"{"a": 4}"#);442		assert_json!("{a:2}+{b:2}", r#"{"a": 2,"b": 2}"#);443		assert_json!("{b:3}+{b:2}", r#"{"b": 2}"#);444		assert_json!("{b:3}+{b+:2}", r#"{"b": 5}"#);445		assert_json!("local test='a'; {[test]:2}", r#"{"a": 2}"#);446		assert_json!(447			r#"448				{449					name: "Alice",450					welcome: "Hello " + self.name + "!",451				}452			"#,453			r#"{"name": "Alice","welcome": "Hello Alice!"}"#454		);455		assert_json!(456			r#"457				{458					name: "Alice",459					welcome: "Hello " + self.name + "!",460				} + {461					name: "Bob"462				}463			"#,464			r#"{"name": "Bob","welcome": "Hello Bob!"}"#465		);466	}467468	#[test]469	fn functions() {470		assert_json!(r#"local a = function(b, c = 2) b + c; a(2)"#, "4");471		assert_json!(472			r#"local a = function(b, c = "Dear") b + c + d, d = "World"; a("Hello")"#,473			r#""HelloDearWorld""#474		);475	}476477	#[test]478	fn local_methods() {479		assert_json!(r#"local a(b, c = 2) = b + c; a(2)"#, "4");480		assert_json!(481			r#"local a(b, c = "Dear") = b + c + d, d = "World"; a("Hello")"#,482			r#""HelloDearWorld""#483		);484	}485486	#[test]487	fn object_locals() {488		assert_json!(r#"{local a = 3, b: a}"#, r#"{"b": 3}"#);489		assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b": 3}"#);490		assert_json!(491			r#"{local a = function (b) {[b]:4}, test: a("test")}"#,492			r#"{"test": {"test": 4}}"#493		);494	}495496	#[test]497	fn direct_self() {498		println!(499			"{:#?}",500			eval!(501				r#"502					{503						local me = self,504						a: 3,505						b(): me.a,506					}507				"#508			)509		);510	}511512	#[test]513	fn indirect_self() {514		// `self` assigned to `me` was lost when being515		// referenced from field516		eval!(517			r#"{518				local me = self,519				a: 3,520				b: me.a,521			}.b"#522		);523	}524525	// We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly526	#[test]527	fn std_assert_ok() {528		eval!("std.assertEqual(4.5 << 2, 16)");529	}530531	#[test]532	#[should_panic]533	fn std_assert_failure() {534		eval!("std.assertEqual(4.5 << 2, 15)");535	}536537	#[test]538	fn string_is_string() {539		assert_eq!(540			eval!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),541			Val::Bool(false)542		);543	}544545	#[test]546	fn base64_works() {547		assert_json!(r#"std.base64("test")"#, r#""dGVzdA==""#);548	}549550	#[test]551	fn utf8_chars() {552		assert_json!(553			r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,554			r#"{"c": 128526,"l": 1}"#555		)556	}557558	#[test]559	fn json() {560		assert_json!(561			r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,562			r#""{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}""#563		);564	}565566	#[test]567	fn test() {568		assert_json!(569			r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,570			"[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"571		);572	}573574	#[test]575	fn sjsonnet() {576		eval!(577			r#"578			local x0 = {k: 1};579			local x1 = {k: x0.k + x0.k};580			local x2 = {k: x1.k + x1.k};581			local x3 = {k: x2.k + x2.k};582			local x4 = {k: x3.k + x3.k};583			local x5 = {k: x4.k + x4.k};584			local x6 = {k: x5.k + x5.k};585			local x7 = {k: x6.k + x6.k};586			local x8 = {k: x7.k + x7.k};587			local x9 = {k: x8.k + x8.k};588			local x10 = {k: x9.k + x9.k};589			local x11 = {k: x10.k + x10.k};590			local x12 = {k: x11.k + x11.k};591			local x13 = {k: x12.k + x12.k};592			local x14 = {k: x13.k + x13.k};593			local x15 = {k: x14.k + x14.k};594			local x16 = {k: x15.k + x15.k};595			local x17 = {k: x16.k + x16.k};596			local x18 = {k: x17.k + x17.k};597			local x19 = {k: x18.k + x18.k};598			local x20 = {k: x19.k + x19.k};599			local x21 = {k: x20.k + x20.k};600			x21.k601		"#602		);603	}604}