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

difftreelog

feat std.extVar support

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

4 files changed

modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -42,19 +42,42 @@
 	}
 }
 
+#[derive(Clone)]
+struct ExtStr {
+	name: String,
+	value: String,
+}
+impl FromStr for ExtStr {
+	type Err = &'static str;
+	fn from_str(s: &str) -> Result<Self, Self::Err> {
+		let out: Vec<_> = s.split('=').collect();
+		match out.len() {
+			1 => Ok(ExtStr {
+				name: out[0].to_owned(),
+				value: std::env::var(out[0]).or(Err("missing env var"))?,
+			}),
+			2 => Ok(ExtStr {
+				name: out[0].to_owned(),
+				value: out[1].to_owned(),
+			}),
+			_ => Err("bad ext-str syntax"),
+		}
+	}
+}
+
 #[derive(Clap)]
 #[clap(version = "0.1.0", author = "Lach <iam@lach.pw>")]
 struct Opts {
 	#[clap(long, about = "Disable global std variable")]
 	no_stdlib: bool,
 	#[clap(long, about = "Add external string")]
-	ext_str: Option<Vec<String>>,
+	ext_str: Vec<ExtStr>,
 	#[clap(long, about = "Add external string from code")]
-	ext_code: Option<Vec<String>>,
+	ext_code: Vec<ExtStr>,
 	#[clap(long, about = "Add TLA")]
-	tla_str: Option<Vec<String>>,
+	tla_str: Vec<ExtStr>,
 	#[clap(long, about = "Add TLA from code")]
-	tla_code: Option<Vec<String>>,
+	tla_code: Vec<ExtStr>,
 	#[clap(long, short = "f", default_value = "json", possible_values = &["none", "json", "yaml"], about = "Output format, wraps resulting value to corresponding std.manifest call")]
 	format: Format,
 	#[clap(long, default_value = "default", possible_values = &["cpp", "go", "default"], about = "Emulated needed stacktrace display")]
@@ -92,6 +115,12 @@
 	if !opts.no_stdlib {
 		evaluator.with_stdlib();
 	}
+	for ExtStr { name, value } in opts.ext_str.iter().cloned() {
+		evaluator.add_ext_var(name, Val::Str(value));
+	}
+	for ExtStr { name, value } in opts.ext_code.iter().cloned() {
+		evaluator.add_ext_var(name, evaluator.parse_evaluate_raw(&value).unwrap());
+	}
 	let mut input = current_dir().unwrap();
 	input.push(opts.input.clone());
 	let code_string = String::from_utf8(std::fs::read(opts.input.clone()).unwrap()).unwrap();
modifiedcrates/jsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/error.rs
+++ b/crates/jsonnet-evaluator/src/error.rs
@@ -12,6 +12,8 @@
 	TooManyArgsFunctionHas(usize),
 	FunctionParameterNotBoundInCall(String),
 
+	UndefinedExternalVariable(String),
+
 	RuntimeError(String),
 	StackOverflow,
 	FractionalIndex,
modifiedcrates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/evaluate.rs
+++ b/crates/jsonnet-evaluator/src/evaluate.rs
@@ -533,6 +533,20 @@
 							panic!("bad pow call");
 						}
 					}
+					("std", "extVar") => {
+						assert_eq!(args.len(), 1);
+						if let Val::Str(a) = evaluate(context, &args[0].1)? {
+							with_state(|s| s.0.ext_vars.borrow().get(&a).cloned()).ok_or_else(
+								|| {
+									create_error::<()>(crate::Error::UndefinedExternalVariable(a))
+										.err()
+										.unwrap()
+								},
+							)?
+						} else {
+							panic!("bad extVar call");
+						}
+					}
 					(ns, name) => panic!("Intristic not found: {}.{}", ns, name),
 				},
 				Val::Func(f) => {
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	settings: EvaluationSettings,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}83#[inline(always)]84pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {85	EVAL_STATE.with(86		#[inline(always)]87		|s| f(s.borrow().as_ref().unwrap()),88	)89}90pub(crate) fn create_error<T>(err: Error) -> Result<T> {91	with_state(|s| s.error(err))92}93#[inline(always)]94pub(crate) fn push<T>(e: LocExpr, comment: String, f: impl FnOnce() -> Result<T>) -> Result<T> {95	with_state(|s| s.push(e, comment, f))96}9798/// Maintains stack trace and import resolution99#[derive(Default, Clone)]100pub struct EvaluationState(Rc<EvaluationStateInternals>);101impl EvaluationState {102	pub fn add_file(&self, name: PathBuf, code: String) -> std::result::Result<(), ParseError> {103		self.0.files.borrow_mut().insert(104			name.clone(),105			FileData(106				code.clone(),107				parse(108					&code,109					&ParserSettings {110						file_name: name,111						loc_data: true,112					},113				)?,114				None,115			),116		);117118		Ok(())119	}120	pub fn add_parsed_file(121		&self,122		name: PathBuf,123		code: String,124		parsed: LocExpr,125	) -> std::result::Result<(), ()> {126		self.0127			.files128			.borrow_mut()129			.insert(name, FileData(code, parsed, None));130131		Ok(())132	}133	pub fn get_source(&self, name: &PathBuf) -> Option<String> {134		let ro_map = self.0.files.borrow();135		ro_map.get(name).map(|value| value.0.clone())136	}137	pub fn evaluate_file(&self, name: &PathBuf) -> Result<Val> {138		self.begin_state();139		let expr: LocExpr = {140			let ro_map = self.0.files.borrow();141			let value = ro_map142				.get(name)143				.unwrap_or_else(|| panic!("file not added: {:?}", name));144			if value.2.is_some() {145				return Ok(value.2.clone().unwrap());146			}147			value.1.clone()148		};149		let value = evaluate(self.create_default_context()?, &expr)?;150		{151			self.0152				.files153				.borrow_mut()154				.get_mut(name)155				.unwrap()156				.2157				.replace(value.clone());158		}159		self.end_state();160		Ok(value)161	}162163	pub fn parse_evaluate_raw(&self, code: &str) -> Result<Val> {164		let parsed = parse(165			&code,166			&ParserSettings {167				file_name: PathBuf::from("raw.jsonnet"),168				loc_data: true,169			},170		);171		self.begin_state();172		let value = evaluate(self.create_default_context()?, &parsed.unwrap());173		self.end_state();174		value175	}176177	pub fn add_global(&self, name: String, value: Val) {178		self.0.globals.borrow_mut().insert(name, value);179	}180181	pub fn with_stdlib(&self) -> &Self {182		self.begin_state();183		use jsonnet_stdlib::STDLIB_STR;184		if cfg!(feature = "serialized-stdlib") {185			self.add_parsed_file(186				PathBuf::from("std.jsonnet"),187				STDLIB_STR.to_owned(),188				bincode::deserialize(include_bytes!(concat!(env!("OUT_DIR"), "/stdlib.bincode")))189					.expect("deserialize stdlib"),190			)191			.unwrap();192		} else {193			self.add_file(PathBuf::from("std.jsonnet"), STDLIB_STR.to_owned())194				.unwrap();195		}196		let val = self.evaluate_file(&PathBuf::from("std.jsonnet")).unwrap();197		self.add_global("std".to_owned(), val);198		self.end_state();199		self200	}201202	pub fn create_default_context(&self) -> Result<Context> {203		let globals = self.0.globals.borrow();204		let mut new_bindings: HashMap<String, LazyBinding> = HashMap::new();205		for (name, value) in globals.iter() {206			new_bindings.insert(207				name.clone(),208				LazyBinding::Bound(resolved_lazy_val!(value.clone())),209			);210		}211		Context::new().extend_unbound(new_bindings, None, None, None)212	}213214	#[inline(always)]215	pub fn push<T>(&self, e: LocExpr, comment: String, f: impl FnOnce() -> Result<T>) -> Result<T> {216		{217			let mut stack = self.0.stack.borrow_mut();218			if stack.len() > self.0.settings.max_stack_frames {219				drop(stack);220				return self.error(Error::StackOverflow);221			} else {222				stack.push(StackTraceElement(e, comment));223			}224		}225		let result = f();226		self.0.stack.borrow_mut().pop();227		result228	}229	pub fn print_stack_trace(&self) {230		for e in self.stack_trace().0 {231			println!("{:?} - {:?}", e.0, e.1)232		}233	}234	pub fn stack_trace(&self) -> StackTrace {235		StackTrace(236			self.0237				.stack238				.borrow()239				.iter()240				.rev()241				.take(self.0.settings.max_stack_trace_size)242				.cloned()243				.collect(),244		)245	}246	pub fn error<T>(&self, err: Error) -> Result<T> {247		Err(LocError(err, self.stack_trace()))248	}249250	fn begin_state(&self) {251		EVAL_STATE.with(|v| v.borrow_mut().replace(self.clone()));252	}253	fn end_state(&self) {254		EVAL_STATE.with(|v| v.borrow_mut().take());255	}256}257258#[cfg(test)]259pub mod tests {260	use super::Val;261	use crate::EvaluationState;262	use jsonnet_parser::*;263	use std::path::PathBuf;264265	#[test]266	fn eval_state_stacktrace() {267		let state = EvaluationState::default();268		state269			.push(270				loc_expr!(271					Expr::Num(0.0),272					true,273					(PathBuf::from("test1.jsonnet"), 10, 20)274				),275				"outer".to_owned(),276				|| {277					state.push(278						loc_expr!(279							Expr::Num(0.0),280							true,281							(PathBuf::from("test2.jsonnet"), 30, 40)282						),283						"inner".to_owned(),284						|| {285							state.print_stack_trace();286							Ok(())287						},288					)?;289					Ok(())290				},291			)292			.unwrap();293	}294295	#[test]296	fn eval_state_standard() {297		let state = EvaluationState::default();298		state.with_stdlib();299		assert_eq!(300			state301				.parse_evaluate_raw(r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#)302				.unwrap(),303			Val::Bool(true)304		);305	}306307	macro_rules! eval {308		($str: expr) => {309			EvaluationState::default()310				.with_stdlib()311				.parse_evaluate_raw($str)312				.unwrap()313		};314	}315	macro_rules! eval_json {316		($str: expr) => {{317			let evaluator = EvaluationState::default();318			evaluator.with_stdlib();319			let val = evaluator.parse_evaluate_raw($str).unwrap();320			evaluator.add_global("__tmp__to_yaml__".to_owned(), val);321			evaluator322				.parse_evaluate_raw("std.manifestJsonEx(__tmp__to_yaml__, \"\")")323				.unwrap()324				.try_cast_str("there should be json string")325				.unwrap()326				.clone()327				.replace("\n", "")328			}};329	}330331	/// Asserts given code returns `true`332	macro_rules! assert_eval {333		($str: expr) => {334			assert_eq!(eval!($str), Val::Bool(true))335		};336	}337338	/// Asserts given code returns `false`339	macro_rules! assert_eval_neg {340		($str: expr) => {341			assert_eq!(eval!($str), Val::Bool(false))342		};343	}344	macro_rules! assert_json {345		($str: expr, $out: expr) => {346			assert_eq!(eval_json!($str), $out.replace("\t", ""))347		};348	}349350	/// Sanity checking, before trusting to another tests351	#[test]352	fn equality_operator() {353		assert_eval!("2 == 2");354		assert_eval_neg!("2 != 2");355		assert_eval!("2 != 3");356		assert_eval_neg!("2 == 3");357		assert_eval!("'Hello' == 'Hello'");358		assert_eval_neg!("'Hello' != 'Hello'");359		assert_eval!("'Hello' != 'World'");360		assert_eval_neg!("'Hello' == 'World'");361	}362363	#[test]364	fn math_evaluation() {365		assert_eval!("2 + 2 * 2 == 6");366		assert_eval!("3 + (2 + 2 * 2) == 9");367	}368369	#[test]370	fn string_concat() {371		assert_eval!("'Hello' + 'World' == 'HelloWorld'");372		assert_eval!("'Hello' * 3 == 'HelloHelloHello'");373		assert_eval!("'Hello' + 'World' * 3 == 'HelloWorldWorldWorld'");374	}375376	#[test]377	fn function_contexts() {378		assert_eval!(379			r#"380				local k = {381					t(name = self.h): [self.h, name],382					h: 3,383				};384				local f = {385					t: k.t(),386					h: 4,387				};388				f.t[0] == f.t[1]389			"#390		);391	}392393	#[test]394	fn local() {395		assert_eval!("local a = 2; local b = 3; a + b == 5");396		assert_eval!("local a = 1, b = a + 1; a + b == 3");397		assert_eval!("local a = 1; local a = 2; a == 2");398	}399400	#[test]401	fn object_lazyness() {402		assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);403	}404405	/// FIXME: This test gets stackoverflow in debug build406	#[test]407	fn object_inheritance() {408		assert_json!("{a: self.b} + {b:3}", r#"{"a": 3,"b": 3}"#);409	}410411	#[test]412	fn test_object() {413		assert_json!("{a:2}", r#"{"a": 2}"#);414		assert_json!("{a:2+2}", r#"{"a": 4}"#);415		assert_json!("{a:2}+{b:2}", r#"{"a": 2,"b": 2}"#);416		assert_json!("{b:3}+{b:2}", r#"{"b": 2}"#);417		assert_json!("{b:3}+{b+:2}", r#"{"b": 5}"#);418		assert_json!("local test='a'; {[test]:2}", r#"{"a": 2}"#);419		assert_json!(420			r#"421				{422					name: "Alice",423					welcome: "Hello " + self.name + "!",424				}425			"#,426			r#"{"name": "Alice","welcome": "Hello Alice!"}"#427		);428		assert_json!(429			r#"430				{431					name: "Alice",432					welcome: "Hello " + self.name + "!",433				} + {434					name: "Bob"435				}436			"#,437			r#"{"name": "Bob","welcome": "Hello Bob!"}"#438		);439	}440441	#[test]442	fn functions() {443		assert_json!(r#"local a = function(b, c = 2) b + c; a(2)"#, "4");444		assert_json!(445			r#"local a = function(b, c = "Dear") b + c + d, d = "World"; a("Hello")"#,446			r#""HelloDearWorld""#447		);448	}449450	#[test]451	fn local_methods() {452		assert_json!(r#"local a(b, c = 2) = b + c; a(2)"#, "4");453		assert_json!(454			r#"local a(b, c = "Dear") = b + c + d, d = "World"; a("Hello")"#,455			r#""HelloDearWorld""#456		);457	}458459	#[test]460	fn object_locals() {461		assert_json!(r#"{local a = 3, b: a}"#, r#"{"b": 3}"#);462		assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b": 3}"#);463		assert_json!(464			r#"{local a = function (b) {[b]:4}, test: a("test")}"#,465			r#"{"test": {"test": 4}}"#466		);467	}468469	#[test]470	fn direct_self() {471		println!(472			"{:#?}",473			eval!(474				r#"475					{476						local me = self,477						a: 3,478						b(): me.a,479					}480				"#481			)482		);483	}484485	#[test]486	fn indirect_self() {487		// `self` assigned to `me` was lost when being488		// referenced from field489		eval!(490			r#"{491				local me = self,492				a: 3,493				b: me.a,494			}.b"#495		);496	}497498	// We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly499	#[test]500	fn std_assert_ok() {501		eval!("std.assertEqual(4.5 << 2, 16)");502	}503504	#[test]505	#[should_panic]506	fn std_assert_failure() {507		eval!("std.assertEqual(4.5 << 2, 15)");508	}509510	#[test]511	fn string_is_string() {512		assert_eq!(513			eval!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),514			Val::Bool(false)515		);516	}517518	#[test]519	fn base64_works() {520		assert_json!(r#"std.base64("test")"#, r#""dGVzdA==""#);521	}522523	#[test]524	fn utf8_chars() {525		assert_json!(526			r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,527			r#"{"c": 128526,"l": 1}"#528		)529	}530531	#[test]532	fn json() {533		assert_json!(534			r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,535			r#""{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}""#536		);537	}538539	#[test]540	fn test() {541		assert_json!(542			r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,543			"[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"544		);545	}546547	#[test]548	fn sjsonnet() {549		eval!(550			r#"551			local x0 = {k: 1};552			local x1 = {k: x0.k + x0.k};553			local x2 = {k: x1.k + x1.k};554			local x3 = {k: x2.k + x2.k};555			local x4 = {k: x3.k + x3.k};556			local x5 = {k: x4.k + x4.k};557			local x6 = {k: x5.k + x5.k};558			local x7 = {k: x6.k + x6.k};559			local x8 = {k: x7.k + x7.k};560			local x9 = {k: x8.k + x8.k};561			local x10 = {k: x9.k + x9.k};562			local x11 = {k: x10.k + x10.k};563			local x12 = {k: x11.k + x11.k};564			local x13 = {k: x12.k + x12.k};565			local x14 = {k: x13.k + x13.k};566			local x15 = {k: x14.k + x14.k};567			local x16 = {k: x15.k + x15.k};568			local x17 = {k: x16.k + x16.k};569			local x18 = {k: x17.k + x17.k};570			local x19 = {k: x18.k + x18.k};571			local x20 = {k: x19.k + x19.k};572			local x21 = {k: x20.k + x20.k};573			x21.k574		"#575		);576	}577}