git.delta.rocks / jrsonnet / refs/commits / 9f3d17001fc1

difftreelog

feat obj comp support

Лач2020-06-11parent: #abdb122.patch.diff
in: master

5 files changed

modifiedcrates/jsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/error.rs
+++ b/crates/jsonnet-evaluator/src/error.rs
@@ -14,6 +14,8 @@
 
 	UndefinedExternalVariable(String),
 
+	FieldMustBeStringGot(ValType),
+
 	RuntimeError(String),
 	StackOverflow,
 	FractionalIndex,
modifiedcrates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/evaluate.rs
+++ b/crates/jsonnet-evaluator/src/evaluate.rs
@@ -1,6 +1,6 @@
 use crate::{
 	context_creator, create_error, future_wrapper, lazy_val, push, with_state, Context,
-	ContextCreator, FuncDesc, LazyBinding, LazyVal, ObjMember, ObjValue, Result, Val,
+	ContextCreator, Error, FuncDesc, LazyBinding, LazyVal, ObjMember, ObjValue, Result, Val,
 };
 use closure::closure;
 use jsonnet_parser::{
@@ -167,13 +167,14 @@
 future_wrapper!(HashMap<String, LazyBinding>, FutureNewBindings);
 future_wrapper!(ObjValue, FutureObjValue);
 
-pub fn evaluate_comp(
+#[inline(always)]
+pub fn evaluate_comp<T>(
 	context: Context,
-	value: &LocExpr,
+	value: &impl Fn(Context) -> Result<T>,
 	specs: &[CompSpec],
-) -> Result<Option<Vec<Val>>> {
+) -> Result<Option<Vec<T>>> {
 	Ok(match specs.get(0) {
-		None => Some(vec![evaluate(context, &value)?]),
+		None => Some(vec![value(context)?]),
 		Some(CompSpec::IfSpec(IfSpecData(cond))) => {
 			if evaluate(context.clone(), &cond)?.try_cast_bool("if spec")? {
 				evaluate_comp(context, value, &specs[1..])?
@@ -193,7 +194,7 @@
 							&specs[1..],
 						)?);
 					}
-					Some(out.iter().flatten().flatten().cloned().collect())
+					Some(out.into_iter().flatten().flatten().collect())
 				}
 				_ => panic!("for expression evaluated to non-iterable value"),
 			}
@@ -208,7 +209,7 @@
 			let new_bindings = FutureNewBindings::new();
 			let future_this = FutureObjValue::new();
 			let context_creator = context_creator!(
-				closure!(clone context, clone new_bindings, clone future_this, |this: Option<ObjValue>, super_obj: Option<ObjValue>| {
+				closure!(clone context, clone new_bindings, |this: Option<ObjValue>, super_obj: Option<ObjValue>| {
 					Ok(context.clone().extend_unbound(
 						new_bindings.clone().unwrap(),
 						context.clone().dollar().clone().or_else(||this.clone()),
@@ -301,7 +302,70 @@
 			}
 			future_this.fill(ObjValue::new(None, Rc::new(new_members)))
 		}
-		_ => todo!(),
+		ObjBody::ObjComp {
+			pre_locals,
+			key,
+			value,
+			post_locals,
+			compspecs,
+		} => {
+			let future_this = FutureObjValue::new();
+			let mut new_members = BTreeMap::new();
+			for (k, v) in evaluate_comp(
+				context.clone(),
+				&|ctx| {
+					let new_bindings = FutureNewBindings::new();
+					let context_creator = context_creator!(
+						closure!(clone context, clone new_bindings, |this: Option<ObjValue>, super_obj: Option<ObjValue>| {
+							Ok(context.clone().extend_unbound(
+								new_bindings.clone().unwrap(),
+								context.clone().dollar().clone().or_else(||this.clone()),
+								None,
+								super_obj
+							)?)
+						})
+					);
+					let mut bindings: HashMap<String, LazyBinding> = HashMap::new();
+					for (n, b) in pre_locals
+						.iter()
+						.chain(post_locals.iter())
+						.map(|b| evaluate_binding(b, context_creator.clone()))
+					{
+						bindings.insert(n, b);
+					}
+					let bindings = new_bindings.fill(bindings);
+					let ctx = ctx.extend_unbound(bindings, None, None, None)?;
+					let key = evaluate(ctx.clone(), &key)?;
+					let value = LazyBinding::Bindable(Rc::new(
+						closure!(clone ctx, clone value, |this, _super_obj| {
+							Ok(LazyVal::new_resolved(evaluate(ctx.extend(HashMap::new(), None, this, None)?, &value)?))
+						}),
+					));
+
+					Ok((key, value))
+				},
+				&compspecs,
+			)?
+			.unwrap()
+			{
+				match k {
+					Val::Null => {}
+					Val::Str(n) => {
+						new_members.insert(
+							n,
+							ObjMember {
+								add: false,
+								visibility: Visibility::Normal,
+								invoke: v,
+							},
+						);
+					}
+					v => create_error(Error::FieldMustBeStringGot(v.value_type()?))?,
+				}
+			}
+
+			future_this.fill(ObjValue::new(None, Rc::new(new_members)))
+		}
 	})
 }
 
@@ -405,7 +469,7 @@
 		}
 		ArrComp(expr, compspecs) => Val::Arr(
 			// First compspec should be forspec, so no "None" possible here
-			evaluate_comp(context, expr, compspecs)?.unwrap(),
+			evaluate_comp(context, &|ctx| evaluate(ctx, expr), compspecs)?.unwrap(),
 		),
 		Obj(body) => Val::Obj(evaluate_object(context, body.clone())?),
 		ObjExtend(s, t) => evaluate_add_op(
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::*;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 direct_self() {497		println!(498			"{:#?}",499			eval!(500				r#"501					{502						local me = self,503						a: 3,504						b(): me.a,505					}506				"#507			)508		);509	}510511	#[test]512	fn indirect_self() {513		// `self` assigned to `me` was lost when being514		// referenced from field515		eval!(516			r#"{517				local me = self,518				a: 3,519				b: me.a,520			}.b"#521		);522	}523524	// We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly525	#[test]526	fn std_assert_ok() {527		eval!("std.assertEqual(4.5 << 2, 16)");528	}529530	#[test]531	#[should_panic]532	fn std_assert_failure() {533		eval!("std.assertEqual(4.5 << 2, 15)");534	}535536	#[test]537	fn string_is_string() {538		assert_eq!(539			eval!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),540			Val::Bool(false)541		);542	}543544	#[test]545	fn base64_works() {546		assert_json!(r#"std.base64("test")"#, r#""dGVzdA==""#);547	}548549	#[test]550	fn utf8_chars() {551		assert_json!(552			r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,553			r#"{"c": 128526,"l": 1}"#554		)555	}556557	#[test]558	fn json() {559		assert_json!(560			r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,561			r#""{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}""#562		);563	}564565	#[test]566	fn test() {567		assert_json!(568			r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,569			"[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"570		);571	}572573	#[test]574	fn sjsonnet() {575		eval!(576			r#"577			local x0 = {k: 1};578			local x1 = {k: x0.k + x0.k};579			local x2 = {k: x1.k + x1.k};580			local x3 = {k: x2.k + x2.k};581			local x4 = {k: x3.k + x3.k};582			local x5 = {k: x4.k + x4.k};583			local x6 = {k: x5.k + x5.k};584			local x7 = {k: x6.k + x6.k};585			local x8 = {k: x7.k + x7.k};586			local x9 = {k: x8.k + x8.k};587			local x10 = {k: x9.k + x9.k};588			local x11 = {k: x10.k + x10.k};589			local x12 = {k: x11.k + x11.k};590			local x13 = {k: x12.k + x12.k};591			local x14 = {k: x13.k + x13.k};592			local x15 = {k: x14.k + x14.k};593			local x16 = {k: x15.k + x15.k};594			local x17 = {k: x16.k + x16.k};595			local x18 = {k: x17.k + x17.k};596			local x19 = {k: x18.k + x18.k};597			local x20 = {k: x19.k + x19.k};598			local x21 = {k: x20.k + x20.k};599			x21.k600		"#601		);602	}603}
modifiedcrates/jsonnet-parser/src/expr.rsdiffbeforeafterboth
--- a/crates/jsonnet-parser/src/expr.rs
+++ b/crates/jsonnet-parser/src/expr.rs
@@ -120,7 +120,7 @@
 		key: LocExpr,
 		value: LocExpr,
 		post_locals: Vec<BindSpec>,
-		rest: Vec<CompSpec>,
+		compspecs: Vec<CompSpec>,
 	},
 }
 
modifiedcrates/jsonnet-parser/src/lib.rsdiffbeforeafterboth
--- a/crates/jsonnet-parser/src/lib.rs
+++ b/crates/jsonnet-parser/src/lib.rs
@@ -138,7 +138,7 @@
 					key,
 					value,
 					post_locals,
-					rest: [vec![CompSpec::ForSpec(forspec)], others.unwrap_or_default()].concat(),
+					compspecs: [vec![CompSpec::ForSpec(forspec)], others.unwrap_or_default()].concat(),
 				}
 			}
 			/ members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}