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

difftreelog

source

crates/jsonnet-evaluator/src/lib.rs13.6 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::*;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}