git.delta.rocks / jrsonnet / refs/commits / 6db420cedf84

difftreelog

source

crates/jsonnet-evaluator/src/lib.rs14.8 KiBsourcehistory
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)]5mod ctx;6mod dynamic;7mod error;8mod evaluate;9mod function;10mod import;11mod map;12mod obj;13mod val;1415pub use ctx::*;16pub use dynamic::*;17pub use error::*;18pub use evaluate::*;19pub use function::parse_function_call;20pub use import::*;21use jsonnet_parser::*;22pub use obj::*;23use std::{cell::RefCell, collections::HashMap, fmt::Debug, path::PathBuf, rc::Rc};24pub use val::*;2526type BindableFn = dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<LazyVal>;27#[derive(Clone)]28pub enum LazyBinding {29	Bindable(Rc<BindableFn>),30	Bound(LazyVal),31}3233impl Debug for LazyBinding {34	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {35		write!(f, "LazyBinding")36	}37}38impl LazyBinding {39	pub fn evaluate(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {40		match self {41			LazyBinding::Bindable(v) => v(this, super_obj),42			LazyBinding::Bound(v) => Ok(v.clone()),43		}44	}45}4647pub struct EvaluationSettings {48	pub max_stack_frames: usize,49	pub max_stack_trace_size: usize,50}51impl Default for EvaluationSettings {52	fn default() -> Self {53		EvaluationSettings {54			max_stack_frames: 200,55			max_stack_trace_size: 20,56		}57	}58}5960pub struct FileData(Rc<str>, LocExpr, Option<Val>);61#[derive(Default)]62pub struct EvaluationStateInternals {63	/// Used for stack-overflows and stacktraces64	stack: RefCell<Vec<StackTraceElement>>,65	/// Contains file source codes and evaluated results for imports and pretty66	/// printing stacktraces67	files: RefCell<HashMap<Rc<PathBuf>, FileData>>,68	str_files: RefCell<HashMap<Rc<PathBuf>, Rc<str>>>,69	globals: RefCell<HashMap<Rc<str>, Val>>,7071	/// Values to use with std.extVar72	ext_vars: RefCell<HashMap<Rc<str>, Val>>,7374	settings: EvaluationSettings,75	import_resolver: Box<dyn ImportResolver>,76}7778thread_local! {79	/// Contains state for currently executing file80	/// Global state is fine there81	pub(crate) static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)82}83pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {84	EVAL_STATE.with(|s| f(s.borrow().as_ref().unwrap()))85}86pub(crate) fn create_error<T>(err: Error) -> Result<T> {87	with_state(|s| s.error(err))88}89pub(crate) fn push<T>(90	e: &Option<ExprLocation>,91	comment: &str,92	f: impl FnOnce() -> Result<T>,93) -> Result<T> {94	if e.is_some() {95		with_state(|s| s.push(e.clone().unwrap(), comment.to_owned(), f))96	} else {97		f()98	}99}100101/// Maintains stack trace and import resolution102#[derive(Default, Clone)]103pub struct EvaluationState(Rc<EvaluationStateInternals>);104impl EvaluationState {105	pub fn new(settings: EvaluationSettings, import_resolver: Box<dyn ImportResolver>) -> Self {106		EvaluationState(Rc::new(EvaluationStateInternals {107			settings,108			import_resolver,109			..Default::default()110		}))111	}112	pub fn add_file(113		&self,114		name: Rc<PathBuf>,115		code: Rc<str>,116	) -> std::result::Result<(), ParseError> {117		self.0.files.borrow_mut().insert(118			name.clone(),119			FileData(120				code.clone(),121				parse(122					&code,123					&ParserSettings {124						file_name: name,125						loc_data: true,126					},127				)?,128				None,129			),130		);131132		Ok(())133	}134	pub fn add_parsed_file(135		&self,136		name: Rc<PathBuf>,137		code: Rc<str>,138		parsed: LocExpr,139	) -> std::result::Result<(), ()> {140		self.0141			.files142			.borrow_mut()143			.insert(name, FileData(code, parsed, None));144145		Ok(())146	}147	pub fn get_source(&self, name: &PathBuf) -> Option<Rc<str>> {148		let ro_map = self.0.files.borrow();149		ro_map.get(name).map(|value| value.0.clone())150	}151	pub fn evaluate_file(&self, name: &PathBuf) -> Result<Val> {152		self.run_in_state(|| {153			let expr: LocExpr = {154				let ro_map = self.0.files.borrow();155				let value = ro_map156					.get(name)157					.unwrap_or_else(|| panic!("file not added: {:?}", name));158				if value.2.is_some() {159					return Ok(value.2.clone().unwrap());160				}161				value.1.clone()162			};163			let value = evaluate(self.create_default_context()?, &expr)?;164			{165				self.0166					.files167					.borrow_mut()168					.get_mut(name)169					.unwrap()170					.2171					.replace(value.clone());172			}173			Ok(value)174		})175	}176	pub(crate) fn import_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Val> {177		let file_path = self.0.import_resolver.resolve_file(from, path)?;178		{179			let files = self.0.files.borrow();180			if files.contains_key(&file_path) {181				return self.evaluate_file(&file_path);182			}183		}184		let contents = self.0.import_resolver.load_file_contents(&file_path)?;185		self.add_file(file_path.clone(), contents).map_err(|e| {186			create_error::<()>(Error::ImportSyntaxError(e))187				.err()188				.unwrap()189		})?;190		self.evaluate_file(&file_path)191	}192	pub(crate) fn import_file_str(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<str>> {193		let path = self.0.import_resolver.resolve_file(from, path)?;194		if !self.0.str_files.borrow().contains_key(&path) {195			let file_str = self.0.import_resolver.load_file_contents(&path)?;196			self.0197				.str_files198				.borrow_mut()199				.insert(path.clone(), file_str.into());200		}201		Ok(self.0.str_files.borrow().get(&path).cloned().unwrap())202	}203204	pub fn parse_evaluate_raw(&self, code: &str) -> Result<Val> {205		let parsed = parse(206			&code,207			&ParserSettings {208				file_name: Rc::new(PathBuf::from("raw.jsonnet")),209				loc_data: true,210			},211		)212		.unwrap();213		self.evaluate_raw(parsed)214	}215216	pub fn evaluate_raw(&self, code: LocExpr) -> Result<Val> {217		self.run_in_state(|| evaluate(self.create_default_context()?, &code))218	}219220	pub fn add_global(&self, name: Rc<str>, value: Val) {221		self.0.globals.borrow_mut().insert(name, value);222	}223	pub fn add_ext_var(&self, name: Rc<str>, value: Val) {224		self.0.ext_vars.borrow_mut().insert(name, value);225	}226227	pub fn with_stdlib(&self) -> &Self {228		let std_path = Rc::new(PathBuf::from("std.jsonnet"));229		self.run_in_state(|| {230			use jsonnet_stdlib::STDLIB_STR;231			if cfg!(feature = "serialized-stdlib") {232				self.add_parsed_file(233					std_path,234					STDLIB_STR.to_owned().into(),235					bincode::deserialize(include_bytes!(concat!(236						env!("OUT_DIR"),237						"/stdlib.bincode"238					)))239					.expect("deserialize stdlib"),240				)241				.unwrap();242			} else {243				self.add_file(std_path, STDLIB_STR.to_owned().into())244					.unwrap();245			}246			let val = self.evaluate_file(&PathBuf::from("std.jsonnet")).unwrap();247			self.add_global("std".into(), val);248		});249		self250	}251252	pub fn create_default_context(&self) -> Result<Context> {253		let globals = self.0.globals.borrow();254		let mut new_bindings: HashMap<Rc<str>, LazyBinding> = HashMap::new();255		for (name, value) in globals.iter() {256			new_bindings.insert(257				name.clone(),258				LazyBinding::Bound(resolved_lazy_val!(value.clone())),259			);260		}261		Context::new().extend_unbound(new_bindings, None, None, None)262	}263264	pub fn push<T>(265		&self,266		e: ExprLocation,267		comment: String,268		f: impl FnOnce() -> Result<T>,269	) -> Result<T> {270		{271			let mut stack = self.0.stack.borrow_mut();272			if stack.len() > self.0.settings.max_stack_frames {273				drop(stack);274				return self.error(Error::StackOverflow);275			} else {276				stack.push(StackTraceElement(e, comment));277			}278		}279		let result = f();280		self.0.stack.borrow_mut().pop();281		result282	}283	pub fn print_stack_trace(&self) {284		for e in self.stack_trace().0 {285			println!("{:?} - {:?}", e.0, e.1)286		}287	}288	pub fn stack_trace(&self) -> StackTrace {289		StackTrace(290			self.0291				.stack292				.borrow()293				.iter()294				.rev()295				.take(self.0.settings.max_stack_trace_size)296				.cloned()297				.collect(),298		)299	}300	pub fn error<T>(&self, err: Error) -> Result<T> {301		Err(LocError(err, self.stack_trace()))302	}303304	pub fn run_in_state<T>(&self, f: impl FnOnce() -> T) -> T {305		EVAL_STATE.with(|v| {306			let has_state = v.borrow().is_some();307			if !has_state {308				v.borrow_mut().replace(self.clone());309			}310			let result = f();311			if !has_state {312				v.borrow_mut().take();313			}314			result315		})316	}317}318319#[cfg(test)]320pub mod tests {321	use super::Val;322	use crate::EvaluationState;323	use jsonnet_parser::*;324	use std::{path::PathBuf, rc::Rc};325326	#[test]327	fn eval_state_stacktrace() {328		let state = EvaluationState::default();329		state330			.push(331				ExprLocation(Rc::new(PathBuf::from("test1.jsonnet")), 10, 20),332				"outer".to_owned(),333				|| {334					state.push(335						ExprLocation(Rc::new(PathBuf::from("test2.jsonnet")), 30, 40),336						"inner".to_owned(),337						|| {338							state.print_stack_trace();339							Ok(())340						},341					)?;342					Ok(())343				},344			)345			.unwrap();346	}347348	#[test]349	fn eval_state_standard() {350		let state = EvaluationState::default();351		state.with_stdlib();352		assert_eq!(353			state354				.parse_evaluate_raw(r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#)355				.unwrap(),356			Val::Bool(true)357		);358	}359360	macro_rules! eval {361		($str: expr) => {362			EvaluationState::default()363				.with_stdlib()364				.parse_evaluate_raw($str)365				.unwrap()366		};367	}368	macro_rules! eval_json {369		($str: expr) => {{370			let evaluator = EvaluationState::default();371			evaluator.with_stdlib();372			evaluator373				.parse_evaluate_raw($str)374				.unwrap()375				.into_json(0)376				.unwrap()377				.replace("\n", "")378			}};379	}380381	/// Asserts given code returns `true`382	macro_rules! assert_eval {383		($str: expr) => {384			assert_eq!(eval!($str), Val::Bool(true))385		};386	}387388	/// Asserts given code returns `false`389	macro_rules! assert_eval_neg {390		($str: expr) => {391			assert_eq!(eval!($str), Val::Bool(false))392		};393	}394	macro_rules! assert_json {395		($str: expr, $out: expr) => {396			assert_eq!(eval_json!($str), $out.replace("\t", ""))397		};398	}399400	/// Sanity checking, before trusting to another tests401	#[test]402	fn equality_operator() {403		assert_eval!("2 == 2");404		assert_eval_neg!("2 != 2");405		assert_eval!("2 != 3");406		assert_eval_neg!("2 == 3");407		assert_eval!("'Hello' == 'Hello'");408		assert_eval_neg!("'Hello' != 'Hello'");409		assert_eval!("'Hello' != 'World'");410		assert_eval_neg!("'Hello' == 'World'");411	}412413	#[test]414	fn math_evaluation() {415		assert_eval!("2 + 2 * 2 == 6");416		assert_eval!("3 + (2 + 2 * 2) == 9");417	}418419	#[test]420	fn string_concat() {421		assert_eval!("'Hello' + 'World' == 'HelloWorld'");422		assert_eval!("'Hello' * 3 == 'HelloHelloHello'");423		assert_eval!("'Hello' + 'World' * 3 == 'HelloWorldWorldWorld'");424	}425426	#[test]427	fn faster_join() {428		assert_eval!("std.join([0,0], [[1,2],[3,4],[5,6]]) == [1,2,0,0,3,4,0,0,5,6]");429		assert_eval!("std.join(',', ['1','2','3','4']) == '1,2,3,4'");430	}431432	#[test]433	fn function_contexts() {434		assert_eval!(435			r#"436				local k = {437					t(name = self.h): [self.h, name],438					h: 3,439				};440				local f = {441					t: k.t(),442					h: 4,443				};444				f.t[0] == f.t[1]445			"#446		);447	}448449	#[test]450	fn local() {451		assert_eval!("local a = 2; local b = 3; a + b == 5");452		assert_eval!("local a = 1, b = a + 1; a + b == 3");453		assert_eval!("local a = 1; local a = 2; a == 2");454	}455456	#[test]457	fn object_lazyness() {458		assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);459	}460461	#[test]462	fn object_inheritance() {463		assert_json!("{a: self.b} + {b:3}", r#"{"a": 3,"b": 3}"#);464	}465466	#[test]467	fn object_assertion_success() {468		eval!("{assert \"a\" in self} + {a:2}");469	}470471	#[test]472	fn object_assertion_error() {473		eval!("{assert \"a\" in self}");474	}475476	#[test]477	fn lazy_args() {478		eval!("local test(a) = 2; test(error '3')");479	}480481	#[test]482	fn tailstrict_args() {483		eval!("local test(a) = 2; test(error '3') tailstrict");484	}485486	#[test]487	fn no_binding_error() {488		eval!("a");489	}490491	#[test]492	fn test_object() {493		assert_json!("{a:2}", r#"{"a": 2}"#);494		assert_json!("{a:2+2}", r#"{"a": 4}"#);495		assert_json!("{a:2}+{b:2}", r#"{"a": 2,"b": 2}"#);496		assert_json!("{b:3}+{b:2}", r#"{"b": 2}"#);497		assert_json!("{b:3}+{b+:2}", r#"{"b": 5}"#);498		assert_json!("local test='a'; {[test]:2}", r#"{"a": 2}"#);499		assert_json!(500			r#"501				{502					name: "Alice",503					welcome: "Hello " + self.name + "!",504				}505			"#,506			r#"{"name": "Alice","welcome": "Hello Alice!"}"#507		);508		assert_json!(509			r#"510				{511					name: "Alice",512					welcome: "Hello " + self.name + "!",513				} + {514					name: "Bob"515				}516			"#,517			r#"{"name": "Bob","welcome": "Hello Bob!"}"#518		);519	}520521	#[test]522	fn functions() {523		assert_json!(r#"local a = function(b, c = 2) b + c; a(2)"#, "4");524		assert_json!(525			r#"local a = function(b, c = "Dear") b + c + d, d = "World"; a("Hello")"#,526			r#""HelloDearWorld""#527		);528	}529530	#[test]531	fn local_methods() {532		assert_json!(r#"local a(b, c = 2) = b + c; a(2)"#, "4");533		assert_json!(534			r#"local a(b, c = "Dear") = b + c + d, d = "World"; a("Hello")"#,535			r#""HelloDearWorld""#536		);537	}538539	#[test]540	fn object_locals() {541		assert_json!(r#"{local a = 3, b: a}"#, r#"{"b": 3}"#);542		assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b": 3}"#);543		assert_json!(544			r#"{local a = function (b) {[b]:4}, test: a("test")}"#,545			r#"{"test": {"test": 4}}"#546		);547	}548549	#[test]550	fn object_comp() {551		assert_json!(552			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}"#,553			"{\"h1_2\": \"0a\",\"h1_3\": \"0a\",\"h1_4\": \"0a\",\"h2_3\": \"a1\",\"h2_4\": \"a1\",\"h3_2\": \"0a\",\"h3_4\": \"a1\"}"554		)555	}556557	#[test]558	fn direct_self() {559		println!(560			"{:#?}",561			eval!(562				r#"563					{564						local me = self,565						a: 3,566						b(): me.a,567					}568				"#569			)570		);571	}572573	#[test]574	fn indirect_self() {575		// `self` assigned to `me` was lost when being576		// referenced from field577		eval!(578			r#"{579				local me = self,580				a: 3,581				b: me.a,582			}.b"#583		);584	}585586	// We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly587	#[test]588	fn std_assert_ok() {589		eval!("std.assertEqual(4.5 << 2, 16)");590	}591592	#[test]593	#[should_panic]594	fn std_assert_failure() {595		eval!("std.assertEqual(4.5 << 2, 15)");596	}597598	#[test]599	fn string_is_string() {600		assert_eq!(601			eval!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),602			Val::Bool(false)603		);604	}605606	#[test]607	fn base64_works() {608		assert_json!(r#"std.base64("test")"#, r#""dGVzdA==""#);609	}610611	#[test]612	fn utf8_chars() {613		assert_json!(614			r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,615			r#"{"c": 128526,"l": 1}"#616		)617	}618619	#[test]620	fn json() {621		assert_json!(622			r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,623			r#""{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}""#624		);625	}626627	#[test]628	fn test() {629		assert_json!(630			r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,631			"[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"632		);633	}634635	#[test]636	fn sjsonnet() {637		eval!(638			r#"639			local x0 = {k: 1};640			local x1 = {k: x0.k + x0.k};641			local x2 = {k: x1.k + x1.k};642			local x3 = {k: x2.k + x2.k};643			local x4 = {k: x3.k + x3.k};644			local x5 = {k: x4.k + x4.k};645			local x6 = {k: x5.k + x5.k};646			local x7 = {k: x6.k + x6.k};647			local x8 = {k: x7.k + x7.k};648			local x9 = {k: x8.k + x8.k};649			local x10 = {k: x9.k + x9.k};650			local x11 = {k: x10.k + x10.k};651			local x12 = {k: x11.k + x11.k};652			local x13 = {k: x12.k + x12.k};653			local x14 = {k: x13.k + x13.k};654			local x15 = {k: x14.k + x14.k};655			local x16 = {k: x15.k + x15.k};656			local x17 = {k: x16.k + x16.k};657			local x18 = {k: x17.k + x17.k};658			local x19 = {k: x18.k + x18.k};659			local x20 = {k: x19.k + x19.k};660			local x21 = {k: x20.k + x20.k};661			x21.k662		"#663		);664	}665}