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

difftreelog

source

crates/jsonnet-evaluator/src/lib.rs13.9 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)]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::*;2425type BindableFn = dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<LazyVal>;26#[derive(Clone)]27pub enum LazyBinding {28	Bindable(Rc<BindableFn>),29	Bound(LazyVal),30}3132impl Debug for LazyBinding {33	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {34		write!(f, "LazyBinding")35	}36}37impl LazyBinding {38	pub fn evaluate(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {39		match self {40			LazyBinding::Bindable(v) => v(this, super_obj),41			LazyBinding::Bound(v) => Ok(v.clone()),42		}43	}44}4546pub struct EvaluationSettings {47	pub max_stack_frames: usize,48	pub max_stack_trace_size: usize,49	pub import_resolver: Box<dyn Fn(&PathBuf) -> String>,50}51impl Default for EvaluationSettings {52	fn default() -> Self {53		EvaluationSettings {54			max_stack_frames: 200,55			max_stack_trace_size: 20,56			import_resolver: Box::new(|path| {57				panic!("default EvaluationSettings have no support for import resolution, can't import {:?}", path)58			}),59		}60	}61}6263pub struct FileData(String, LocExpr, Option<Val>);64#[derive(Default)]65pub struct EvaluationStateInternals {66	/// Used for stack-overflows and stacktraces67	stack: RefCell<Vec<StackTraceElement>>,68	/// Contains file source codes and evaluated results for imports and pretty69	/// printing stacktraces70	files: RefCell<HashMap<PathBuf, FileData>>,71	globals: RefCell<HashMap<String, Val>>,7273	/// Values to use with std.extVar74	ext_vars: RefCell<HashMap<String, Val>>,7576	settings: EvaluationSettings,77}7879thread_local! {80	/// Contains state for currently executing file81	/// Global state is fine there82	pub(crate) static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)83}84#[inline(always)]85pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {86	EVAL_STATE.with(87		#[inline(always)]88		|s| f(s.borrow().as_ref().unwrap()),89	)90}91pub(crate) fn create_error<T>(err: Error) -> Result<T> {92	with_state(|s| s.error(err))93}94#[inline(always)]95pub(crate) fn push<T>(e: LocExpr, comment: String, f: impl FnOnce() -> Result<T>) -> Result<T> {96	with_state(|s| s.push(e, comment, f))97}9899/// Maintains stack trace and import resolution100#[derive(Default, Clone)]101pub struct EvaluationState(Rc<EvaluationStateInternals>);102impl EvaluationState {103	pub fn new(settings: EvaluationSettings) -> Self {104		EvaluationState(Rc::new(EvaluationStateInternals {105			settings,106			..Default::default()107		}))108	}109	pub fn add_file(&self, name: PathBuf, code: String) -> std::result::Result<(), ParseError> {110		self.0.files.borrow_mut().insert(111			name.clone(),112			FileData(113				code.clone(),114				parse(115					&code,116					&ParserSettings {117						file_name: name,118						loc_data: true,119					},120				)?,121				None,122			),123		);124125		Ok(())126	}127	pub fn add_parsed_file(128		&self,129		name: PathBuf,130		code: String,131		parsed: LocExpr,132	) -> std::result::Result<(), ()> {133		self.0134			.files135			.borrow_mut()136			.insert(name, FileData(code, parsed, None));137138		Ok(())139	}140	pub fn get_source(&self, name: &PathBuf) -> Option<String> {141		let ro_map = self.0.files.borrow();142		ro_map.get(name).map(|value| value.0.clone())143	}144	pub fn evaluate_file(&self, name: &PathBuf) -> Result<Val> {145		self.begin_state();146		let value = self.evaluate_file_in_current_state(name)?;147		self.end_state();148		Ok(value)149	}150	pub(crate) fn evaluate_file_in_current_state(&self, name: &PathBuf) -> Result<Val> {151		let expr: LocExpr = {152			let ro_map = self.0.files.borrow();153			let value = ro_map154				.get(name)155				.unwrap_or_else(|| panic!("file not added: {:?}", name));156			if value.2.is_some() {157				return Ok(value.2.clone().unwrap());158			}159			value.1.clone()160		};161		let value = evaluate(self.create_default_context()?, &expr)?;162		{163			self.0164				.files165				.borrow_mut()166				.get_mut(name)167				.unwrap()168				.2169				.replace(value.clone());170		}171		Ok(value)172	}173	pub(crate) fn import_file(&self, path: &PathBuf) -> Result<Val> {174		if !self.0.files.borrow().contains_key(path) {175			let file_str = (self.0.settings.import_resolver)(path);176			self.add_file(path.clone(), file_str).unwrap();177		}178		self.evaluate_file_in_current_state(path)179	}180181	pub fn parse_evaluate_raw(&self, code: &str) -> Result<Val> {182		let parsed = parse(183			&code,184			&ParserSettings {185				file_name: PathBuf::from("raw.jsonnet"),186				loc_data: true,187			},188		)189		.unwrap();190		self.evaluate_raw(parsed)191	}192193	pub fn evaluate_raw(&self, code: LocExpr) -> Result<Val> {194		self.begin_state();195		let value = evaluate(self.create_default_context()?, &code);196		self.end_state();197		value198	}199200	pub fn add_global(&self, name: String, value: Val) {201		self.0.globals.borrow_mut().insert(name, value);202	}203	pub fn add_ext_var(&self, name: String, value: Val) {204		self.0.ext_vars.borrow_mut().insert(name, value);205	}206207	pub fn with_stdlib(&self) -> &Self {208		self.begin_state();209		use jsonnet_stdlib::STDLIB_STR;210		if cfg!(feature = "serialized-stdlib") {211			self.add_parsed_file(212				PathBuf::from("std.jsonnet"),213				STDLIB_STR.to_owned(),214				bincode::deserialize(include_bytes!(concat!(env!("OUT_DIR"), "/stdlib.bincode")))215					.expect("deserialize stdlib"),216			)217			.unwrap();218		} else {219			self.add_file(PathBuf::from("std.jsonnet"), STDLIB_STR.to_owned())220				.unwrap();221		}222		let val = self.evaluate_file(&PathBuf::from("std.jsonnet")).unwrap();223		self.add_global("std".to_owned(), val);224		self.end_state();225		self226	}227228	pub fn create_default_context(&self) -> Result<Context> {229		let globals = self.0.globals.borrow();230		let mut new_bindings: HashMap<String, LazyBinding> = HashMap::new();231		for (name, value) in globals.iter() {232			new_bindings.insert(233				name.clone(),234				LazyBinding::Bound(resolved_lazy_val!(value.clone())),235			);236		}237		Context::new().extend_unbound(new_bindings, None, None, None)238	}239240	#[inline(always)]241	pub fn push<T>(&self, e: LocExpr, comment: String, f: impl FnOnce() -> Result<T>) -> Result<T> {242		{243			let mut stack = self.0.stack.borrow_mut();244			if stack.len() > self.0.settings.max_stack_frames {245				drop(stack);246				return self.error(Error::StackOverflow);247			} else {248				stack.push(StackTraceElement(e, comment));249			}250		}251		let result = f();252		self.0.stack.borrow_mut().pop();253		result254	}255	pub fn print_stack_trace(&self) {256		for e in self.stack_trace().0 {257			println!("{:?} - {:?}", e.0, e.1)258		}259	}260	pub fn stack_trace(&self) -> StackTrace {261		StackTrace(262			self.0263				.stack264				.borrow()265				.iter()266				.rev()267				.take(self.0.settings.max_stack_trace_size)268				.cloned()269				.collect(),270		)271	}272	pub fn error<T>(&self, err: Error) -> Result<T> {273		Err(LocError(err, self.stack_trace()))274	}275276	fn begin_state(&self) {277		EVAL_STATE.with(|v| v.borrow_mut().replace(self.clone()));278	}279	fn end_state(&self) {280		EVAL_STATE.with(|v| v.borrow_mut().take());281	}282}283284#[cfg(test)]285pub mod tests {286	use super::Val;287	use crate::EvaluationState;288	use jsonnet_parser::*;289	use std::path::PathBuf;290291	#[test]292	fn eval_state_stacktrace() {293		let state = EvaluationState::default();294		state295			.push(296				loc_expr!(297					Expr::Num(0.0),298					true,299					(PathBuf::from("test1.jsonnet"), 10, 20)300				),301				"outer".to_owned(),302				|| {303					state.push(304						loc_expr!(305							Expr::Num(0.0),306							true,307							(PathBuf::from("test2.jsonnet"), 30, 40)308						),309						"inner".to_owned(),310						|| {311							state.print_stack_trace();312							Ok(())313						},314					)?;315					Ok(())316				},317			)318			.unwrap();319	}320321	#[test]322	fn eval_state_standard() {323		let state = EvaluationState::default();324		state.with_stdlib();325		assert_eq!(326			state327				.parse_evaluate_raw(r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#)328				.unwrap(),329			Val::Bool(true)330		);331	}332333	macro_rules! eval {334		($str: expr) => {335			EvaluationState::default()336				.with_stdlib()337				.parse_evaluate_raw($str)338				.unwrap()339		};340	}341	macro_rules! eval_json {342		($str: expr) => {{343			let evaluator = EvaluationState::default();344			evaluator.with_stdlib();345			let val = evaluator.parse_evaluate_raw($str).unwrap();346			evaluator.add_global("__tmp__to_yaml__".to_owned(), val);347			evaluator348				.parse_evaluate_raw("std.manifestJsonEx(__tmp__to_yaml__, \"\")")349				.unwrap()350				.try_cast_str("there should be json string")351				.unwrap()352				.clone()353				.replace("\n", "")354			}};355	}356357	/// Asserts given code returns `true`358	macro_rules! assert_eval {359		($str: expr) => {360			assert_eq!(eval!($str), Val::Bool(true))361		};362	}363364	/// Asserts given code returns `false`365	macro_rules! assert_eval_neg {366		($str: expr) => {367			assert_eq!(eval!($str), Val::Bool(false))368		};369	}370	macro_rules! assert_json {371		($str: expr, $out: expr) => {372			assert_eq!(eval_json!($str), $out.replace("\t", ""))373		};374	}375376	/// Sanity checking, before trusting to another tests377	#[test]378	fn equality_operator() {379		assert_eval!("2 == 2");380		assert_eval_neg!("2 != 2");381		assert_eval!("2 != 3");382		assert_eval_neg!("2 == 3");383		assert_eval!("'Hello' == 'Hello'");384		assert_eval_neg!("'Hello' != 'Hello'");385		assert_eval!("'Hello' != 'World'");386		assert_eval_neg!("'Hello' == 'World'");387	}388389	#[test]390	fn math_evaluation() {391		assert_eval!("2 + 2 * 2 == 6");392		assert_eval!("3 + (2 + 2 * 2) == 9");393	}394395	#[test]396	fn string_concat() {397		assert_eval!("'Hello' + 'World' == 'HelloWorld'");398		assert_eval!("'Hello' * 3 == 'HelloHelloHello'");399		assert_eval!("'Hello' + 'World' * 3 == 'HelloWorldWorldWorld'");400	}401402	#[test]403	fn function_contexts() {404		assert_eval!(405			r#"406				local k = {407					t(name = self.h): [self.h, name],408					h: 3,409				};410				local f = {411					t: k.t(),412					h: 4,413				};414				f.t[0] == f.t[1]415			"#416		);417	}418419	#[test]420	fn local() {421		assert_eval!("local a = 2; local b = 3; a + b == 5");422		assert_eval!("local a = 1, b = a + 1; a + b == 3");423		assert_eval!("local a = 1; local a = 2; a == 2");424	}425426	#[test]427	fn object_lazyness() {428		assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);429	}430431	/// FIXME: This test gets stackoverflow in debug build432	#[test]433	fn object_inheritance() {434		assert_json!("{a: self.b} + {b:3}", r#"{"a": 3,"b": 3}"#);435	}436437	#[test]438	fn test_object() {439		assert_json!("{a:2}", r#"{"a": 2}"#);440		assert_json!("{a:2+2}", r#"{"a": 4}"#);441		assert_json!("{a:2}+{b:2}", r#"{"a": 2,"b": 2}"#);442		assert_json!("{b:3}+{b:2}", r#"{"b": 2}"#);443		assert_json!("{b:3}+{b+:2}", r#"{"b": 5}"#);444		assert_json!("local test='a'; {[test]:2}", r#"{"a": 2}"#);445		assert_json!(446			r#"447				{448					name: "Alice",449					welcome: "Hello " + self.name + "!",450				}451			"#,452			r#"{"name": "Alice","welcome": "Hello Alice!"}"#453		);454		assert_json!(455			r#"456				{457					name: "Alice",458					welcome: "Hello " + self.name + "!",459				} + {460					name: "Bob"461				}462			"#,463			r#"{"name": "Bob","welcome": "Hello Bob!"}"#464		);465	}466467	#[test]468	fn functions() {469		assert_json!(r#"local a = function(b, c = 2) b + c; a(2)"#, "4");470		assert_json!(471			r#"local a = function(b, c = "Dear") b + c + d, d = "World"; a("Hello")"#,472			r#""HelloDearWorld""#473		);474	}475476	#[test]477	fn local_methods() {478		assert_json!(r#"local a(b, c = 2) = b + c; a(2)"#, "4");479		assert_json!(480			r#"local a(b, c = "Dear") = b + c + d, d = "World"; a("Hello")"#,481			r#""HelloDearWorld""#482		);483	}484485	#[test]486	fn object_locals() {487		assert_json!(r#"{local a = 3, b: a}"#, r#"{"b": 3}"#);488		assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b": 3}"#);489		assert_json!(490			r#"{local a = function (b) {[b]:4}, test: a("test")}"#,491			r#"{"test": {"test": 4}}"#492		);493	}494495	#[test]496	fn object_comp() {497		assert_json!(498			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}"#,499			"{\"h1_2\": \"0a\",\"h1_3\": \"0a\",\"h1_4\": \"0a\",\"h2_3\": \"a1\",\"h2_4\": \"a1\",\"h3_2\": \"0a\",\"h3_4\": \"a1\"}"500		)501	}502503	#[test]504	fn direct_self() {505		println!(506			"{:#?}",507			eval!(508				r#"509					{510						local me = self,511						a: 3,512						b(): me.a,513					}514				"#515			)516		);517	}518519	#[test]520	fn indirect_self() {521		// `self` assigned to `me` was lost when being522		// referenced from field523		eval!(524			r#"{525				local me = self,526				a: 3,527				b: me.a,528			}.b"#529		);530	}531532	// We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly533	#[test]534	fn std_assert_ok() {535		eval!("std.assertEqual(4.5 << 2, 16)");536	}537538	#[test]539	#[should_panic]540	fn std_assert_failure() {541		eval!("std.assertEqual(4.5 << 2, 15)");542	}543544	#[test]545	fn string_is_string() {546		assert_eq!(547			eval!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),548			Val::Bool(false)549		);550	}551552	#[test]553	fn base64_works() {554		assert_json!(r#"std.base64("test")"#, r#""dGVzdA==""#);555	}556557	#[test]558	fn utf8_chars() {559		assert_json!(560			r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,561			r#"{"c": 128526,"l": 1}"#562		)563	}564565	#[test]566	fn json() {567		assert_json!(568			r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,569			r#""{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}""#570		);571	}572573	#[test]574	fn test() {575		assert_json!(576			r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,577			"[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"578		);579	}580581	#[test]582	fn sjsonnet() {583		eval!(584			r#"585			local x0 = {k: 1};586			local x1 = {k: x0.k + x0.k};587			local x2 = {k: x1.k + x1.k};588			local x3 = {k: x2.k + x2.k};589			local x4 = {k: x3.k + x3.k};590			local x5 = {k: x4.k + x4.k};591			local x6 = {k: x5.k + x5.k};592			local x7 = {k: x6.k + x6.k};593			local x8 = {k: x7.k + x7.k};594			local x9 = {k: x8.k + x8.k};595			local x10 = {k: x9.k + x9.k};596			local x11 = {k: x10.k + x10.k};597			local x12 = {k: x11.k + x11.k};598			local x13 = {k: x12.k + x12.k};599			local x14 = {k: x13.k + x13.k};600			local x15 = {k: x14.k + x14.k};601			local x16 = {k: x15.k + x15.k};602			local x17 = {k: x16.k + x16.k};603			local x18 = {k: x17.k + x17.k};604			local x19 = {k: x18.k + x18.k};605			local x20 = {k: x19.k + x19.k};606			local x21 = {k: x20.k + x20.k};607			x21.k608		"#609		);610	}611}