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

difftreelog

feat yaml stream output

Lach2020-08-22parent: #ce6eeac.patch.diff
in: master

5 files changed

modifiedbindings/jsonnet/src/lib.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -54,7 +54,7 @@
 #[no_mangle]
 pub extern "C" fn jsonnet_string_output(vm: &EvaluationState, v: c_int) {
 	match v {
-		1 => vm.set_manifest_format(ManifestFormat::None),
+		1 => vm.set_manifest_format(ManifestFormat::String),
 		0 => vm.set_manifest_format(ManifestFormat::Json(4)),
 		_ => panic!("incorrect output format"),
 	}
modifiedcrates/jrsonnet-cli/src/manifest.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/manifest.rs
+++ b/crates/jrsonnet-cli/src/manifest.rs
@@ -5,7 +5,7 @@
 
 pub enum ManifestFormatName {
 	/// Expect string as output, and write them directly
-	None,
+	String,
 	Json,
 	Yaml,
 }
@@ -14,7 +14,7 @@
 	type Err = &'static str;
 	fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
 		Ok(match s {
-			"none" => ManifestFormatName::None,
+			"string" => ManifestFormatName::String,
 			"json" => ManifestFormatName::Json,
 			"yaml" => ManifestFormatName::Yaml,
 			_ => return Err("no such format"),
@@ -27,14 +27,17 @@
 // #[clap(group = clap::ArgGroup::new("output_format"), help_heading = "MANIFESTIFICATION OUTPUT")]
 pub struct ManifestOpts {
 	/// Output format, wraps resulting value to corresponding std.manifest call.
-	/// If none - then jsonnet file is expected to return plain string value, otherwise
+	/// If string - then jsonnet file is expected to return plain string value, otherwise
 	/// output will be serialized to specified format
-	#[clap(long, short = 'f', default_value = "json", possible_values = &["none", "json", "yaml"]/*, group = "output_format"*/)]
+	#[clap(long, short = 'f', default_value = "json", possible_values = &["string", "json", "yaml"]/*, group = "output_format"*/)]
 	format: ManifestFormatName,
 	/// Expect string as output, and write them directly.
-	/// Shortcut for --format=none, and can't be set with format together
+	/// Shortcut for --format=string, and can't be set with format together
 	#[clap(long, short = 'S'/*, group = "output_format"*/)]
 	string: bool,
+	/// Write output as YAML stream, can be used with --format json/yaml
+	#[clap(long, short = 'y')]
+	yaml_stream: bool,
 	/// Numbed of spaces to pad output manifest with.
 	/// 0 for hard tabs, -1 for single line output
 	#[clap(long, default_value = "3")]
@@ -43,10 +46,10 @@
 impl ConfigureState for ManifestOpts {
 	fn configure(&self, state: &EvaluationState) -> Result<()> {
 		if self.string {
-			state.set_manifest_format(ManifestFormat::None);
+			state.set_manifest_format(ManifestFormat::String);
 		} else {
 			match self.format {
-				ManifestFormatName::None => state.set_manifest_format(ManifestFormat::None),
+				ManifestFormatName::String => state.set_manifest_format(ManifestFormat::String),
 				ManifestFormatName::Json => {
 					state.set_manifest_format(ManifestFormat::Json(self.line_padding))
 				}
@@ -55,6 +58,11 @@
 				}
 			}
 		}
+		if self.yaml_stream {
+			state.set_manifest_format(ManifestFormat::YamlStream(Box::new(
+				state.manifest_format(),
+			)))
+		}
 		Ok(())
 	}
 }
modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -58,6 +58,11 @@
 	DivisionByZero,
 
 	StringManifestOutputIsNotAString,
+	StreamManifestOutputIsNotAArray,
+	MultiManifestOutputIsNotAObject,
+
+	StreamManifestOutputCannotBeRecursed,
+	StreamManifestCannotNestString,
 
 	ImportCallbackError(String),
 	InvalidUnicodeCodepointGot(u32),
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/lib.rs
1#![cfg_attr(feature = "unstable", feature(stmt_expr_attributes))]2#![allow(macro_expanded_macro_exports_accessed_by_absolute_paths)]34mod builtin;5mod ctx;6mod dynamic;7pub mod error;8mod evaluate;9mod function;10mod import;11mod integrations;12mod map;13mod obj;14pub mod trace;15mod val;1617pub use ctx::*;18pub use dynamic::*;19use error::{Error::*, LocError, Result, StackTraceElement};20pub use evaluate::*;21pub use function::parse_function_call;22pub use import::*;23use jrsonnet_parser::*;24pub use obj::*;25use std::{26	cell::{Ref, RefCell, RefMut},27	collections::HashMap,28	fmt::Debug,29	path::PathBuf,30	rc::Rc,31};32use trace::{offset_to_location, CodeLocation, CompactFormat, TraceFormat};33pub use val::*;3435type BindableFn = dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<LazyVal>;36#[derive(Clone)]37pub enum LazyBinding {38	Bindable(Rc<BindableFn>),39	Bound(LazyVal),40}4142impl Debug for LazyBinding {43	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {44		write!(f, "LazyBinding")45	}46}47impl LazyBinding {48	pub fn evaluate(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {49		match self {50			LazyBinding::Bindable(v) => v(this, super_obj),51			LazyBinding::Bound(v) => Ok(v.clone()),52		}53	}54}5556#[derive(Clone)]57pub enum ManifestFormat {58	Yaml(usize),59	Json(usize),60	None,61}6263pub struct EvaluationSettings {64	/// Limits recursion by limiting stack frames65	pub max_stack: usize,66	/// Limit amount of stack trace items preserved67	pub max_trace: usize,68	/// Used for std.extVar69	pub ext_vars: HashMap<Rc<str>, Val>,70	/// TLA vars71	pub tla_vars: HashMap<Rc<str>, Val>,72	/// Global variables are inserted in default context73	pub globals: HashMap<Rc<str>, Val>,74	/// Used to resolve file locations/contents75	pub import_resolver: Box<dyn ImportResolver>,76	/// Used in manifestification functions77	pub manifest_format: ManifestFormat,78	/// Used for bindings79	pub trace_format: Box<dyn TraceFormat>,80}81impl Default for EvaluationSettings {82	fn default() -> Self {83		EvaluationSettings {84			max_stack: 200,85			max_trace: 20,86			globals: Default::default(),87			ext_vars: Default::default(),88			tla_vars: Default::default(),89			import_resolver: Box::new(DummyImportResolver),90			manifest_format: ManifestFormat::Json(4),91			trace_format: Box::new(CompactFormat {92				padding: 4,93				resolver: trace::PathResolver::Absolute,94			}),95		}96	}97}9899#[derive(Default)]100struct EvaluationData {101	/// Used for stack overflow detection, stacktrace is now populated on unwind102	stack_depth: usize,103	/// Contains file source codes and evaluated results for imports and pretty104	/// printing stacktraces105	files: HashMap<Rc<PathBuf>, FileData>,106	str_files: HashMap<Rc<PathBuf>, Rc<str>>,107}108109pub struct FileData {110	source_code: Rc<str>,111	parsed: LocExpr,112	evaluated: Option<Val>,113}114#[derive(Default)]115pub struct EvaluationStateInternals {116	/// Internal state117	data: RefCell<EvaluationData>,118	/// Settings, safe to change at runtime119	settings: RefCell<EvaluationSettings>,120}121122thread_local! {123	/// Contains state for currently executing file124	/// Global state is fine there125	pub(crate) static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)126}127pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {128	EVAL_STATE.with(|s| f(s.borrow().as_ref().unwrap()))129}130pub(crate) fn push<T>(131	e: &Option<ExprLocation>,132	frame_desc: impl FnOnce() -> String,133	f: impl FnOnce() -> Result<T>,134) -> Result<T> {135	if let Some(v) = e {136		with_state(|s| s.push(&v, frame_desc, f))137	} else {138		f()139	}140}141142/// Maintains stack trace and import resolution143#[derive(Default, Clone)]144pub struct EvaluationState(Rc<EvaluationStateInternals>);145146impl EvaluationState {147	/// Parses and adds file to loaded148	pub fn add_file(&self, path: Rc<PathBuf>, source_code: Rc<str>) -> Result<()> {149		self.add_parsed_file(150			path.clone(),151			source_code.clone(),152			parse(153				&source_code,154				&ParserSettings {155					file_name: path.clone(),156					loc_data: true,157				},158			)159			.map_err(|error| ImportSyntaxError {160				error,161				path,162				source_code,163			})?,164		)?;165166		Ok(())167	}168169	/// Adds file by source code and parsed expr170	pub fn add_parsed_file(171		&self,172		name: Rc<PathBuf>,173		source_code: Rc<str>,174		parsed: LocExpr,175	) -> Result<()> {176		self.data_mut().files.insert(177			name,178			FileData {179				source_code,180				parsed,181				evaluated: None,182			},183		);184185		Ok(())186	}187	pub fn get_source(&self, name: &PathBuf) -> Option<Rc<str>> {188		let ro_map = &self.data().files;189		ro_map.get(name).map(|value| value.source_code.clone())190	}191	pub fn map_source_locations(&self, file: &PathBuf, locs: &[usize]) -> Vec<CodeLocation> {192		offset_to_location(&self.get_source(file).unwrap(), locs)193	}194195	pub(crate) fn import_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Val> {196		let file_path = self.resolve_file(from, path)?;197		{198			let files = &self.data().files;199			if files.contains_key(&file_path) {200				return self.evaluate_loaded_file_raw(&file_path);201			}202		}203		let contents = self.load_file_contents(&file_path)?;204		self.add_file(file_path.clone(), contents)?;205		self.evaluate_loaded_file_raw(&file_path)206	}207	pub(crate) fn import_file_str(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<str>> {208		let path = self.resolve_file(from, path)?;209		if !self.data().str_files.contains_key(&path) {210			let file_str = self.load_file_contents(&path)?;211			self.data_mut().str_files.insert(path.clone(), file_str);212		}213		Ok(self.data().str_files.get(&path).cloned().unwrap())214	}215216	fn evaluate_loaded_file_raw(&self, name: &PathBuf) -> Result<Val> {217		let expr: LocExpr = {218			let ro_map = &self.data().files;219			let value = ro_map220				.get(name)221				.unwrap_or_else(|| panic!("file not added: {:?}", name));222			if let Some(ref evaluated) = value.evaluated {223				return Ok(evaluated.clone());224			}225			value.parsed.clone()226		};227		let value = evaluate(self.create_default_context()?, &expr)?;228		{229			self.data_mut()230				.files231				.get_mut(name)232				.unwrap()233				.evaluated234				.replace(value.clone());235		}236		Ok(value)237	}238239	/// Adds standard library global variable (std) to this evaluator240	pub fn with_stdlib(&self) -> &Self {241		use jrsonnet_stdlib::STDLIB_STR;242		let std_path = Rc::new(PathBuf::from("std.jsonnet"));243		self.run_in_state(|| {244			self.add_parsed_file(245				std_path.clone(),246				STDLIB_STR.to_owned().into(),247				builtin::get_parsed_stdlib(),248			)249			.unwrap();250			let val = self.evaluate_loaded_file_raw(&std_path).unwrap();251			self.settings_mut().globals.insert("std".into(), val);252		});253		self254	}255256	/// Creates context with all passed global variables257	pub fn create_default_context(&self) -> Result<Context> {258		let globals = &self.settings().globals;259		let mut new_bindings: HashMap<Rc<str>, LazyBinding> = HashMap::new();260		for (name, value) in globals.iter() {261			new_bindings.insert(262				name.clone(),263				LazyBinding::Bound(resolved_lazy_val!(value.clone())),264			);265		}266		Context::new().extend_unbound(new_bindings, None, None, None)267	}268269	/// Executes code, creating new stack frame270	pub fn push<T>(271		&self,272		e: &ExprLocation,273		frame_desc: impl FnOnce() -> String,274		f: impl FnOnce() -> Result<T>,275	) -> Result<T> {276		{277			let mut data = self.data_mut();278			let stack_depth = &mut data.stack_depth;279			if *stack_depth > self.max_stack() {280				// Error creation uses data, so i drop guard here281				drop(data);282				throw!(StackOverflow);283			} else {284				*stack_depth += 1;285			}286		}287		let result = f();288		self.data_mut().stack_depth -= 1;289		if let Err(mut err) = result {290			(err.1).0.push(StackTraceElement {291				location: e.clone(),292				desc: frame_desc(),293			});294			return Err(err);295		}296		result297	}298299	/// Runs passed function in state (required, if function needs to modify stack trace)300	pub fn run_in_state<T>(&self, f: impl FnOnce() -> T) -> T {301		EVAL_STATE.with(|v| {302			let has_state = v.borrow().is_some();303			if !has_state {304				v.borrow_mut().replace(self.clone());305			}306			let result = f();307			if !has_state {308				v.borrow_mut().take();309			}310			result311		})312	}313314	pub fn stringify_err(&self, e: &LocError) -> String {315		let mut out = String::new();316		self.settings()317			.trace_format318			.write_trace(&mut out, self, e)319			.unwrap();320		out321	}322323	pub fn manifest(&self, val: Val) -> Result<Rc<str>> {324		self.run_in_state(|| {325			Ok(match self.manifest_format() {326				ManifestFormat::Yaml(padding) => val.into_yaml(padding)?,327				ManifestFormat::Json(padding) => val.into_json(padding)?,328				ManifestFormat::None => match val {329					Val::Str(s) => s,330					_ => throw!(StringManifestOutputIsNotAString),331				},332			})333		})334	}335336	/// If passed value is function - call with set TLA337	pub fn with_tla(&self, val: Val) -> Result<Val> {338		Ok(match val {339			Val::Func(func) => func.evaluate_map(340				self.create_default_context()?,341				&self.settings().tla_vars,342				true,343			)?,344			v => v,345		})346	}347}348349/// Internals350impl EvaluationState {351	fn data(&self) -> Ref<EvaluationData> {352		self.0.data.borrow()353	}354	fn data_mut(&self) -> RefMut<EvaluationData> {355		self.0.data.borrow_mut()356	}357	pub fn settings(&self) -> Ref<EvaluationSettings> {358		self.0.settings.borrow()359	}360	pub fn settings_mut(&self) -> RefMut<EvaluationSettings> {361		self.0.settings.borrow_mut()362	}363}364365/// Raw methods evaluates passed values, but not performs TLA execution366impl EvaluationState {367	pub fn evaluate_file_raw(&self, name: &PathBuf) -> Result<Val> {368		self.run_in_state(|| self.import_file(&std::env::current_dir().expect("cwd"), &name))369	}370	pub fn evaluate_file_raw_nocwd(&self, name: &PathBuf) -> Result<Val> {371		self.run_in_state(|| self.import_file(&PathBuf::from("."), &name))372	}373	/// Parses and evaluates snippet374	pub fn evaluate_snippet_raw(&self, source: Rc<PathBuf>, code: Rc<str>) -> Result<Val> {375		let parsed = parse(376			&code,377			&ParserSettings {378				file_name: source.clone(),379				loc_data: true,380			},381		)382		.unwrap();383		self.add_parsed_file(source, code, parsed.clone())?;384		self.evaluate_expr_raw(parsed)385	}386	/// Evaluates parsed expression387	pub fn evaluate_expr_raw(&self, code: LocExpr) -> Result<Val> {388		self.run_in_state(|| evaluate(self.create_default_context()?, &code))389	}390}391392/// Settings utilities393impl EvaluationState {394	pub fn add_ext_var(&self, name: Rc<str>, value: Val) {395		self.settings_mut().ext_vars.insert(name, value);396	}397	pub fn add_ext_str(&self, name: Rc<str>, value: Rc<str>) {398		self.add_ext_var(name, Val::Str(value));399	}400	pub fn add_ext_code(&self, name: Rc<str>, code: Rc<str>) -> Result<()> {401		let value =402			self.evaluate_snippet_raw(Rc::new(PathBuf::from(format!("ext_code {}", name))), code)?;403		self.add_ext_var(name, value);404		Ok(())405	}406407	pub fn add_tla(&self, name: Rc<str>, value: Val) {408		self.settings_mut().tla_vars.insert(name, value);409	}410	pub fn add_tla_str(&self, name: Rc<str>, value: Rc<str>) {411		self.add_tla(name, Val::Str(value));412	}413	pub fn add_tla_code(&self, name: Rc<str>, code: Rc<str>) -> Result<()> {414		let value =415			self.evaluate_snippet_raw(Rc::new(PathBuf::from(format!("tla_code {}", name))), code)?;416		self.add_ext_var(name, value);417		Ok(())418	}419420	pub fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {421		Ok(self.settings().import_resolver.resolve_file(from, path)?)422	}423	pub fn load_file_contents(&self, path: &PathBuf) -> Result<Rc<str>> {424		Ok(self.settings().import_resolver.load_file_contents(path)?)425	}426427	pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {428		Ref::map(self.settings(), |s| &*s.import_resolver)429	}430	pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {431		self.settings_mut().import_resolver = resolver;432	}433434	pub fn manifest_format(&self) -> ManifestFormat {435		self.settings().manifest_format.clone()436	}437	pub fn set_manifest_format(&self, format: ManifestFormat) {438		self.settings_mut().manifest_format = format;439	}440441	pub fn trace_format(&self) -> Ref<dyn TraceFormat> {442		Ref::map(self.settings(), |s| &*s.trace_format)443	}444	pub fn set_trace_format(&self, format: Box<dyn TraceFormat>) {445		self.settings_mut().trace_format = format;446	}447448	pub fn max_trace(&self) -> usize {449		self.settings().max_trace450	}451	pub fn set_max_trace(&self, trace: usize) {452		self.settings_mut().max_trace = trace;453	}454455	pub fn max_stack(&self) -> usize {456		self.settings().max_stack457	}458	pub fn set_max_stack(&self, trace: usize) {459		self.settings_mut().max_stack = trace;460	}461}462463#[cfg(test)]464pub mod tests {465	use super::Val;466	use crate::{error::Error::*, primitive_equals, EvaluationState};467	use jrsonnet_parser::*;468	use std::{path::PathBuf, rc::Rc};469470	#[test]471	#[should_panic]472	fn eval_state_stacktrace() {473		let state = EvaluationState::default();474		state.run_in_state(|| {475			state476				.push(477					&ExprLocation(Rc::new(PathBuf::from("test1.jsonnet")), 10, 20),478					|| "outer".to_owned(),479					|| {480						state.push(481							&ExprLocation(Rc::new(PathBuf::from("test2.jsonnet")), 30, 40),482							|| "inner".to_owned(),483							|| Err(RuntimeError("".into()).into()),484						)?;485						Ok(())486					},487				)488				.unwrap();489		});490	}491492	#[test]493	fn eval_state_standard() {494		let state = EvaluationState::default();495		state.with_stdlib();496		assert!(primitive_equals(497			&state498				.evaluate_snippet_raw(499					Rc::new(PathBuf::from("raw.jsonnet")),500					r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#.into()501				)502				.unwrap(),503			&Val::Bool(true),504		)505		.unwrap());506	}507508	macro_rules! eval {509		($str: expr) => {510			EvaluationState::default()511				.with_stdlib()512				.evaluate_snippet_raw(Rc::new(PathBuf::from("raw.jsonnet")), $str.into())513				.unwrap()514		};515	}516	macro_rules! eval_json {517		($str: expr) => {{518			let evaluator = EvaluationState::default();519			evaluator.with_stdlib();520			evaluator.run_in_state(|| {521				evaluator522					.evaluate_snippet_raw(Rc::new(PathBuf::from("raw.jsonnet")), $str.into())523					.unwrap()524					.into_json(0)525					.unwrap()526					.replace("\n", "")527				})528			}};529	}530531	/// Asserts given code returns `true`532	macro_rules! assert_eval {533		($str: expr) => {534			assert!(primitive_equals(&eval!($str), &Val::Bool(true)).unwrap())535		};536	}537538	/// Asserts given code returns `false`539	macro_rules! assert_eval_neg {540		($str: expr) => {541			assert!(primitive_equals(&eval!($str), &Val::Bool(false)).unwrap())542		};543	}544	macro_rules! assert_json {545		($str: expr, $out: expr) => {546			assert_eq!(eval_json!($str), $out.replace("\t", ""))547		};548	}549550	/// Sanity checking, before trusting to another tests551	#[test]552	fn equality_operator() {553		assert_eval!("2 == 2");554		assert_eval_neg!("2 != 2");555		assert_eval!("2 != 3");556		assert_eval_neg!("2 == 3");557		assert_eval!("'Hello' == 'Hello'");558		assert_eval_neg!("'Hello' != 'Hello'");559		assert_eval!("'Hello' != 'World'");560		assert_eval_neg!("'Hello' == 'World'");561	}562563	#[test]564	fn math_evaluation() {565		assert_eval!("2 + 2 * 2 == 6");566		assert_eval!("3 + (2 + 2 * 2) == 9");567	}568569	#[test]570	fn string_concat() {571		assert_eval!("'Hello' + 'World' == 'HelloWorld'");572		assert_eval!("'Hello' * 3 == 'HelloHelloHello'");573		assert_eval!("'Hello' + 'World' * 3 == 'HelloWorldWorldWorld'");574	}575576	#[test]577	fn faster_join() {578		assert_eval!("std.join([0,0], [[1,2],[3,4],[5,6]]) == [1,2,0,0,3,4,0,0,5,6]");579		assert_eval!("std.join(',', ['1','2','3','4']) == '1,2,3,4'");580	}581582	#[test]583	fn function_contexts() {584		assert_eval!(585			r#"586				local k = {587					t(name = self.h): [self.h, name],588					h: 3,589				};590				local f = {591					t: k.t(),592					h: 4,593				};594				f.t[0] == f.t[1]595			"#596		);597	}598599	#[test]600	fn local() {601		assert_eval!("local a = 2; local b = 3; a + b == 5");602		assert_eval!("local a = 1, b = a + 1; a + b == 3");603		assert_eval!("local a = 1; local a = 2; a == 2");604	}605606	#[test]607	fn object_lazyness() {608		assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);609	}610611	#[test]612	fn object_inheritance() {613		assert_json!("{a: self.b} + {b:3}", r#"{"a": 3,"b": 3}"#);614	}615616	#[test]617	fn object_assertion_success() {618		eval!("{assert \"a\" in self} + {a:2}");619	}620621	#[test]622	fn object_assertion_error() {623		eval!("{assert \"a\" in self}");624	}625626	#[test]627	fn lazy_args() {628		eval!("local test(a) = 2; test(error '3')");629	}630631	#[test]632	#[should_panic]633	fn tailstrict_args() {634		eval!("local test(a) = 2; test(error '3') tailstrict");635	}636637	#[test]638	#[should_panic]639	fn no_binding_error() {640		eval!("a");641	}642643	#[test]644	fn test_object() {645		assert_json!("{a:2}", r#"{"a": 2}"#);646		assert_json!("{a:2+2}", r#"{"a": 4}"#);647		assert_json!("{a:2}+{b:2}", r#"{"a": 2,"b": 2}"#);648		assert_json!("{b:3}+{b:2}", r#"{"b": 2}"#);649		assert_json!("{b:3}+{b+:2}", r#"{"b": 5}"#);650		assert_json!("local test='a'; {[test]:2}", r#"{"a": 2}"#);651		assert_json!(652			r#"653				{654					name: "Alice",655					welcome: "Hello " + self.name + "!",656				}657			"#,658			r#"{"name": "Alice","welcome": "Hello Alice!"}"#659		);660		assert_json!(661			r#"662				{663					name: "Alice",664					welcome: "Hello " + self.name + "!",665				} + {666					name: "Bob"667				}668			"#,669			r#"{"name": "Bob","welcome": "Hello Bob!"}"#670		);671	}672673	#[test]674	fn functions() {675		assert_json!(r#"local a = function(b, c = 2) b + c; a(2)"#, "4");676		assert_json!(677			r#"local a = function(b, c = "Dear") b + c + d, d = "World"; a("Hello")"#,678			r#""HelloDearWorld""#679		);680	}681682	#[test]683	fn local_methods() {684		assert_json!(r#"local a(b, c = 2) = b + c; a(2)"#, "4");685		assert_json!(686			r#"local a(b, c = "Dear") = b + c + d, d = "World"; a("Hello")"#,687			r#""HelloDearWorld""#688		);689	}690691	#[test]692	fn object_locals() {693		assert_json!(r#"{local a = 3, b: a}"#, r#"{"b": 3}"#);694		assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b": 3}"#);695		assert_json!(696			r#"{local a = function (b) {[b]:4}, test: a("test")}"#,697			r#"{"test": {"test": 4}}"#698		);699	}700701	#[test]702	fn object_comp() {703		assert_json!(704			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}"#,705			"{\"h1_2\": \"0a\",\"h1_3\": \"0a\",\"h1_4\": \"0a\",\"h2_3\": \"a1\",\"h2_4\": \"a1\",\"h3_2\": \"0a\",\"h3_4\": \"a1\"}"706		)707	}708709	#[test]710	fn direct_self() {711		println!(712			"{:#?}",713			eval!(714				r#"715					{716						local me = self,717						a: 3,718						b(): me.a,719					}720				"#721			)722		);723	}724725	#[test]726	fn indirect_self() {727		// `self` assigned to `me` was lost when being728		// referenced from field729		eval!(730			r#"{731				local me = self,732				a: 3,733				b: me.a,734			}.b"#735		);736	}737738	// We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly739	#[test]740	fn std_assert_ok() {741		eval!("std.assertEqual(4.5 << 2, 16)");742	}743744	#[test]745	#[should_panic]746	fn std_assert_failure() {747		eval!("std.assertEqual(4.5 << 2, 15)");748	}749750	#[test]751	fn string_is_string() {752		assert!(primitive_equals(753			&eval!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),754			&Val::Bool(false),755		)756		.unwrap());757	}758759	#[test]760	fn base64_works() {761		assert_json!(r#"std.base64("test")"#, r#""dGVzdA==""#);762	}763764	#[test]765	fn utf8_chars() {766		assert_json!(767			r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,768			r#"{"c": 128526,"l": 1}"#769		)770	}771772	#[test]773	fn json() {774		assert_json!(775			r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,776			r#""{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}""#777		);778	}779780	#[test]781	fn test() {782		assert_json!(783			r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,784			"[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"785		);786	}787788	#[test]789	fn sjsonnet() {790		eval!(791			r#"792			local x0 = {k: 1};793			local x1 = {k: x0.k + x0.k};794			local x2 = {k: x1.k + x1.k};795			local x3 = {k: x2.k + x2.k};796			local x4 = {k: x3.k + x3.k};797			local x5 = {k: x4.k + x4.k};798			local x6 = {k: x5.k + x5.k};799			local x7 = {k: x6.k + x6.k};800			local x8 = {k: x7.k + x7.k};801			local x9 = {k: x8.k + x8.k};802			local x10 = {k: x9.k + x9.k};803			local x11 = {k: x10.k + x10.k};804			local x12 = {k: x11.k + x11.k};805			local x13 = {k: x12.k + x12.k};806			local x14 = {k: x13.k + x13.k};807			local x15 = {k: x14.k + x14.k};808			local x16 = {k: x15.k + x15.k};809			local x17 = {k: x16.k + x16.k};810			local x18 = {k: x17.k + x17.k};811			local x19 = {k: x18.k + x18.k};812			local x20 = {k: x19.k + x19.k};813			local x21 = {k: x20.k + x20.k};814			x21.k815		"#816		);817	}818819	// This test is commented out by default, because of huge compilation slowdown820	// #[bench]821	// fn bench_codegen(b: &mut Bencher) {822	// 	b.iter(|| {823	// 		#[allow(clippy::all)]824	// 		let stdlib = {825	// 			use jrsonnet_parser::*;826	// 			include!(concat!(env!("OUT_DIR"), "/stdlib.rs"))827	// 		};828	// 		stdlib829	// 	})830	// }831832	/*833	#[bench]834	fn bench_serialize(b: &mut Bencher) {835		b.iter(|| {836			bincode::deserialize::<jrsonnet_parser::LocExpr>(include_bytes!(concat!(837				env!("OUT_DIR"),838				"/stdlib.bincode"839			)))840			.expect("deserialize stdlib")841		})842	}843844	#[bench]845	fn bench_parse(b: &mut Bencher) {846		b.iter(|| {847			jrsonnet_parser::parse(848				jrsonnet_stdlib::STDLIB_STR,849				&jrsonnet_parser::ParserSettings {850					loc_data: true,851					file_name: Rc::new(PathBuf::from("std.jsonnet")),852				},853			)854		})855	}856	*/857858	#[test]859	fn equality() {860		println!(861			"{:?}",862			jrsonnet_parser::parse(863				"{ x: 1, y: 2 } == { x: 1, y: 2 }",864				&ParserSettings::default()865			)866		);867		assert_eval!("{ x: 1, y: 2 } == { x: 1, y: 2 }")868	}869}
after · crates/jrsonnet-evaluator/src/lib.rs
1#![cfg_attr(feature = "unstable", feature(stmt_expr_attributes))]2#![allow(macro_expanded_macro_exports_accessed_by_absolute_paths)]34mod builtin;5mod ctx;6mod dynamic;7pub mod error;8mod evaluate;9mod function;10mod import;11mod integrations;12mod map;13mod obj;14pub mod trace;15mod val;1617pub use ctx::*;18pub use dynamic::*;19use error::{Error::*, LocError, Result, StackTraceElement};20pub use evaluate::*;21pub use function::parse_function_call;22pub use import::*;23use jrsonnet_parser::*;24pub use obj::*;25use std::{26	cell::{Ref, RefCell, RefMut},27	collections::HashMap,28	fmt::Debug,29	path::PathBuf,30	rc::Rc,31};32use trace::{offset_to_location, CodeLocation, CompactFormat, TraceFormat};33pub use val::*;3435type BindableFn = dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<LazyVal>;36#[derive(Clone)]37pub enum LazyBinding {38	Bindable(Rc<BindableFn>),39	Bound(LazyVal),40}4142impl Debug for LazyBinding {43	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {44		write!(f, "LazyBinding")45	}46}47impl LazyBinding {48	pub fn evaluate(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {49		match self {50			LazyBinding::Bindable(v) => v(this, super_obj),51			LazyBinding::Bound(v) => Ok(v.clone()),52		}53	}54}5556pub struct EvaluationSettings {57	/// Limits recursion by limiting stack frames58	pub max_stack: usize,59	/// Limit amount of stack trace items preserved60	pub max_trace: usize,61	/// Used for std.extVar62	pub ext_vars: HashMap<Rc<str>, Val>,63	/// TLA vars64	pub tla_vars: HashMap<Rc<str>, Val>,65	/// Global variables are inserted in default context66	pub globals: HashMap<Rc<str>, Val>,67	/// Used to resolve file locations/contents68	pub import_resolver: Box<dyn ImportResolver>,69	/// Used in manifestification functions70	pub manifest_format: ManifestFormat,71	/// Used for bindings72	pub trace_format: Box<dyn TraceFormat>,73}74impl Default for EvaluationSettings {75	fn default() -> Self {76		EvaluationSettings {77			max_stack: 200,78			max_trace: 20,79			globals: Default::default(),80			ext_vars: Default::default(),81			tla_vars: Default::default(),82			import_resolver: Box::new(DummyImportResolver),83			manifest_format: ManifestFormat::Json(4),84			trace_format: Box::new(CompactFormat {85				padding: 4,86				resolver: trace::PathResolver::Absolute,87			}),88		}89	}90}9192#[derive(Default)]93struct EvaluationData {94	/// Used for stack overflow detection, stacktrace is now populated on unwind95	stack_depth: usize,96	/// Contains file source codes and evaluated results for imports and pretty97	/// printing stacktraces98	files: HashMap<Rc<PathBuf>, FileData>,99	str_files: HashMap<Rc<PathBuf>, Rc<str>>,100}101102pub struct FileData {103	source_code: Rc<str>,104	parsed: LocExpr,105	evaluated: Option<Val>,106}107#[derive(Default)]108pub struct EvaluationStateInternals {109	/// Internal state110	data: RefCell<EvaluationData>,111	/// Settings, safe to change at runtime112	settings: RefCell<EvaluationSettings>,113}114115thread_local! {116	/// Contains state for currently executing file117	/// Global state is fine there118	pub(crate) static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)119}120pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {121	EVAL_STATE.with(|s| f(s.borrow().as_ref().unwrap()))122}123pub(crate) fn push<T>(124	e: &Option<ExprLocation>,125	frame_desc: impl FnOnce() -> String,126	f: impl FnOnce() -> Result<T>,127) -> Result<T> {128	if let Some(v) = e {129		with_state(|s| s.push(&v, frame_desc, f))130	} else {131		f()132	}133}134135/// Maintains stack trace and import resolution136#[derive(Default, Clone)]137pub struct EvaluationState(Rc<EvaluationStateInternals>);138139impl EvaluationState {140	/// Parses and adds file to loaded141	pub fn add_file(&self, path: Rc<PathBuf>, source_code: Rc<str>) -> Result<()> {142		self.add_parsed_file(143			path.clone(),144			source_code.clone(),145			parse(146				&source_code,147				&ParserSettings {148					file_name: path.clone(),149					loc_data: true,150				},151			)152			.map_err(|error| ImportSyntaxError {153				error,154				path,155				source_code,156			})?,157		)?;158159		Ok(())160	}161162	/// Adds file by source code and parsed expr163	pub fn add_parsed_file(164		&self,165		name: Rc<PathBuf>,166		source_code: Rc<str>,167		parsed: LocExpr,168	) -> Result<()> {169		self.data_mut().files.insert(170			name,171			FileData {172				source_code,173				parsed,174				evaluated: None,175			},176		);177178		Ok(())179	}180	pub fn get_source(&self, name: &PathBuf) -> Option<Rc<str>> {181		let ro_map = &self.data().files;182		ro_map.get(name).map(|value| value.source_code.clone())183	}184	pub fn map_source_locations(&self, file: &PathBuf, locs: &[usize]) -> Vec<CodeLocation> {185		offset_to_location(&self.get_source(file).unwrap(), locs)186	}187188	pub(crate) fn import_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Val> {189		let file_path = self.resolve_file(from, path)?;190		{191			let files = &self.data().files;192			if files.contains_key(&file_path) {193				return self.evaluate_loaded_file_raw(&file_path);194			}195		}196		let contents = self.load_file_contents(&file_path)?;197		self.add_file(file_path.clone(), contents)?;198		self.evaluate_loaded_file_raw(&file_path)199	}200	pub(crate) fn import_file_str(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<str>> {201		let path = self.resolve_file(from, path)?;202		if !self.data().str_files.contains_key(&path) {203			let file_str = self.load_file_contents(&path)?;204			self.data_mut().str_files.insert(path.clone(), file_str);205		}206		Ok(self.data().str_files.get(&path).cloned().unwrap())207	}208209	fn evaluate_loaded_file_raw(&self, name: &PathBuf) -> Result<Val> {210		let expr: LocExpr = {211			let ro_map = &self.data().files;212			let value = ro_map213				.get(name)214				.unwrap_or_else(|| panic!("file not added: {:?}", name));215			if let Some(ref evaluated) = value.evaluated {216				return Ok(evaluated.clone());217			}218			value.parsed.clone()219		};220		let value = evaluate(self.create_default_context()?, &expr)?;221		{222			self.data_mut()223				.files224				.get_mut(name)225				.unwrap()226				.evaluated227				.replace(value.clone());228		}229		Ok(value)230	}231232	/// Adds standard library global variable (std) to this evaluator233	pub fn with_stdlib(&self) -> &Self {234		use jrsonnet_stdlib::STDLIB_STR;235		let std_path = Rc::new(PathBuf::from("std.jsonnet"));236		self.run_in_state(|| {237			self.add_parsed_file(238				std_path.clone(),239				STDLIB_STR.to_owned().into(),240				builtin::get_parsed_stdlib(),241			)242			.unwrap();243			let val = self.evaluate_loaded_file_raw(&std_path).unwrap();244			self.settings_mut().globals.insert("std".into(), val);245		});246		self247	}248249	/// Creates context with all passed global variables250	pub fn create_default_context(&self) -> Result<Context> {251		let globals = &self.settings().globals;252		let mut new_bindings: HashMap<Rc<str>, LazyBinding> = HashMap::new();253		for (name, value) in globals.iter() {254			new_bindings.insert(255				name.clone(),256				LazyBinding::Bound(resolved_lazy_val!(value.clone())),257			);258		}259		Context::new().extend_unbound(new_bindings, None, None, None)260	}261262	/// Executes code, creating new stack frame263	pub fn push<T>(264		&self,265		e: &ExprLocation,266		frame_desc: impl FnOnce() -> String,267		f: impl FnOnce() -> Result<T>,268	) -> Result<T> {269		{270			let mut data = self.data_mut();271			let stack_depth = &mut data.stack_depth;272			if *stack_depth > self.max_stack() {273				// Error creation uses data, so i drop guard here274				drop(data);275				throw!(StackOverflow);276			} else {277				*stack_depth += 1;278			}279		}280		let result = f();281		self.data_mut().stack_depth -= 1;282		if let Err(mut err) = result {283			(err.1).0.push(StackTraceElement {284				location: e.clone(),285				desc: frame_desc(),286			});287			return Err(err);288		}289		result290	}291292	/// Runs passed function in state (required, if function needs to modify stack trace)293	pub fn run_in_state<T>(&self, f: impl FnOnce() -> T) -> T {294		EVAL_STATE.with(|v| {295			let has_state = v.borrow().is_some();296			if !has_state {297				v.borrow_mut().replace(self.clone());298			}299			let result = f();300			if !has_state {301				v.borrow_mut().take();302			}303			result304		})305	}306307	pub fn stringify_err(&self, e: &LocError) -> String {308		let mut out = String::new();309		self.settings()310			.trace_format311			.write_trace(&mut out, self, e)312			.unwrap();313		out314	}315316	pub fn manifest(&self, val: Val) -> Result<Rc<str>> {317		self.run_in_state(|| val.manifest(&self.manifest_format()))318	}319320	/// If passed value is function - call with set TLA321	pub fn with_tla(&self, val: Val) -> Result<Val> {322		Ok(match val {323			Val::Func(func) => func.evaluate_map(324				self.create_default_context()?,325				&self.settings().tla_vars,326				true,327			)?,328			v => v,329		})330	}331}332333/// Internals334impl EvaluationState {335	fn data(&self) -> Ref<EvaluationData> {336		self.0.data.borrow()337	}338	fn data_mut(&self) -> RefMut<EvaluationData> {339		self.0.data.borrow_mut()340	}341	pub fn settings(&self) -> Ref<EvaluationSettings> {342		self.0.settings.borrow()343	}344	pub fn settings_mut(&self) -> RefMut<EvaluationSettings> {345		self.0.settings.borrow_mut()346	}347}348349/// Raw methods evaluates passed values, but not performs TLA execution350impl EvaluationState {351	pub fn evaluate_file_raw(&self, name: &PathBuf) -> Result<Val> {352		self.run_in_state(|| self.import_file(&std::env::current_dir().expect("cwd"), &name))353	}354	pub fn evaluate_file_raw_nocwd(&self, name: &PathBuf) -> Result<Val> {355		self.run_in_state(|| self.import_file(&PathBuf::from("."), &name))356	}357	/// Parses and evaluates snippet358	pub fn evaluate_snippet_raw(&self, source: Rc<PathBuf>, code: Rc<str>) -> Result<Val> {359		let parsed = parse(360			&code,361			&ParserSettings {362				file_name: source.clone(),363				loc_data: true,364			},365		)366		.unwrap();367		self.add_parsed_file(source, code, parsed.clone())?;368		self.evaluate_expr_raw(parsed)369	}370	/// Evaluates parsed expression371	pub fn evaluate_expr_raw(&self, code: LocExpr) -> Result<Val> {372		self.run_in_state(|| evaluate(self.create_default_context()?, &code))373	}374}375376/// Settings utilities377impl EvaluationState {378	pub fn add_ext_var(&self, name: Rc<str>, value: Val) {379		self.settings_mut().ext_vars.insert(name, value);380	}381	pub fn add_ext_str(&self, name: Rc<str>, value: Rc<str>) {382		self.add_ext_var(name, Val::Str(value));383	}384	pub fn add_ext_code(&self, name: Rc<str>, code: Rc<str>) -> Result<()> {385		let value =386			self.evaluate_snippet_raw(Rc::new(PathBuf::from(format!("ext_code {}", name))), code)?;387		self.add_ext_var(name, value);388		Ok(())389	}390391	pub fn add_tla(&self, name: Rc<str>, value: Val) {392		self.settings_mut().tla_vars.insert(name, value);393	}394	pub fn add_tla_str(&self, name: Rc<str>, value: Rc<str>) {395		self.add_tla(name, Val::Str(value));396	}397	pub fn add_tla_code(&self, name: Rc<str>, code: Rc<str>) -> Result<()> {398		let value =399			self.evaluate_snippet_raw(Rc::new(PathBuf::from(format!("tla_code {}", name))), code)?;400		self.add_ext_var(name, value);401		Ok(())402	}403404	pub fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {405		Ok(self.settings().import_resolver.resolve_file(from, path)?)406	}407	pub fn load_file_contents(&self, path: &PathBuf) -> Result<Rc<str>> {408		Ok(self.settings().import_resolver.load_file_contents(path)?)409	}410411	pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {412		Ref::map(self.settings(), |s| &*s.import_resolver)413	}414	pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {415		self.settings_mut().import_resolver = resolver;416	}417418	pub fn manifest_format(&self) -> ManifestFormat {419		self.settings().manifest_format.clone()420	}421	pub fn set_manifest_format(&self, format: ManifestFormat) {422		self.settings_mut().manifest_format = format;423	}424425	pub fn trace_format(&self) -> Ref<dyn TraceFormat> {426		Ref::map(self.settings(), |s| &*s.trace_format)427	}428	pub fn set_trace_format(&self, format: Box<dyn TraceFormat>) {429		self.settings_mut().trace_format = format;430	}431432	pub fn max_trace(&self) -> usize {433		self.settings().max_trace434	}435	pub fn set_max_trace(&self, trace: usize) {436		self.settings_mut().max_trace = trace;437	}438439	pub fn max_stack(&self) -> usize {440		self.settings().max_stack441	}442	pub fn set_max_stack(&self, trace: usize) {443		self.settings_mut().max_stack = trace;444	}445}446447#[cfg(test)]448pub mod tests {449	use super::Val;450	use crate::{error::Error::*, primitive_equals, EvaluationState};451	use jrsonnet_parser::*;452	use std::{path::PathBuf, rc::Rc};453454	#[test]455	#[should_panic]456	fn eval_state_stacktrace() {457		let state = EvaluationState::default();458		state.run_in_state(|| {459			state460				.push(461					&ExprLocation(Rc::new(PathBuf::from("test1.jsonnet")), 10, 20),462					|| "outer".to_owned(),463					|| {464						state.push(465							&ExprLocation(Rc::new(PathBuf::from("test2.jsonnet")), 30, 40),466							|| "inner".to_owned(),467							|| Err(RuntimeError("".into()).into()),468						)?;469						Ok(())470					},471				)472				.unwrap();473		});474	}475476	#[test]477	fn eval_state_standard() {478		let state = EvaluationState::default();479		state.with_stdlib();480		assert!(primitive_equals(481			&state482				.evaluate_snippet_raw(483					Rc::new(PathBuf::from("raw.jsonnet")),484					r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#.into()485				)486				.unwrap(),487			&Val::Bool(true),488		)489		.unwrap());490	}491492	macro_rules! eval {493		($str: expr) => {494			EvaluationState::default()495				.with_stdlib()496				.evaluate_snippet_raw(Rc::new(PathBuf::from("raw.jsonnet")), $str.into())497				.unwrap()498		};499	}500	macro_rules! eval_json {501		($str: expr) => {{502			let evaluator = EvaluationState::default();503			evaluator.with_stdlib();504			evaluator.run_in_state(|| {505				evaluator506					.evaluate_snippet_raw(Rc::new(PathBuf::from("raw.jsonnet")), $str.into())507					.unwrap()508					.to_json(0)509					.unwrap()510					.replace("\n", "")511				})512			}};513	}514515	/// Asserts given code returns `true`516	macro_rules! assert_eval {517		($str: expr) => {518			assert!(primitive_equals(&eval!($str), &Val::Bool(true)).unwrap())519		};520	}521522	/// Asserts given code returns `false`523	macro_rules! assert_eval_neg {524		($str: expr) => {525			assert!(primitive_equals(&eval!($str), &Val::Bool(false)).unwrap())526		};527	}528	macro_rules! assert_json {529		($str: expr, $out: expr) => {530			assert_eq!(eval_json!($str), $out.replace("\t", ""))531		};532	}533534	/// Sanity checking, before trusting to another tests535	#[test]536	fn equality_operator() {537		assert_eval!("2 == 2");538		assert_eval_neg!("2 != 2");539		assert_eval!("2 != 3");540		assert_eval_neg!("2 == 3");541		assert_eval!("'Hello' == 'Hello'");542		assert_eval_neg!("'Hello' != 'Hello'");543		assert_eval!("'Hello' != 'World'");544		assert_eval_neg!("'Hello' == 'World'");545	}546547	#[test]548	fn math_evaluation() {549		assert_eval!("2 + 2 * 2 == 6");550		assert_eval!("3 + (2 + 2 * 2) == 9");551	}552553	#[test]554	fn string_concat() {555		assert_eval!("'Hello' + 'World' == 'HelloWorld'");556		assert_eval!("'Hello' * 3 == 'HelloHelloHello'");557		assert_eval!("'Hello' + 'World' * 3 == 'HelloWorldWorldWorld'");558	}559560	#[test]561	fn faster_join() {562		assert_eval!("std.join([0,0], [[1,2],[3,4],[5,6]]) == [1,2,0,0,3,4,0,0,5,6]");563		assert_eval!("std.join(',', ['1','2','3','4']) == '1,2,3,4'");564	}565566	#[test]567	fn function_contexts() {568		assert_eval!(569			r#"570				local k = {571					t(name = self.h): [self.h, name],572					h: 3,573				};574				local f = {575					t: k.t(),576					h: 4,577				};578				f.t[0] == f.t[1]579			"#580		);581	}582583	#[test]584	fn local() {585		assert_eval!("local a = 2; local b = 3; a + b == 5");586		assert_eval!("local a = 1, b = a + 1; a + b == 3");587		assert_eval!("local a = 1; local a = 2; a == 2");588	}589590	#[test]591	fn object_lazyness() {592		assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);593	}594595	#[test]596	fn object_inheritance() {597		assert_json!("{a: self.b} + {b:3}", r#"{"a": 3,"b": 3}"#);598	}599600	#[test]601	fn object_assertion_success() {602		eval!("{assert \"a\" in self} + {a:2}");603	}604605	#[test]606	fn object_assertion_error() {607		eval!("{assert \"a\" in self}");608	}609610	#[test]611	fn lazy_args() {612		eval!("local test(a) = 2; test(error '3')");613	}614615	#[test]616	#[should_panic]617	fn tailstrict_args() {618		eval!("local test(a) = 2; test(error '3') tailstrict");619	}620621	#[test]622	#[should_panic]623	fn no_binding_error() {624		eval!("a");625	}626627	#[test]628	fn test_object() {629		assert_json!("{a:2}", r#"{"a": 2}"#);630		assert_json!("{a:2+2}", r#"{"a": 4}"#);631		assert_json!("{a:2}+{b:2}", r#"{"a": 2,"b": 2}"#);632		assert_json!("{b:3}+{b:2}", r#"{"b": 2}"#);633		assert_json!("{b:3}+{b+:2}", r#"{"b": 5}"#);634		assert_json!("local test='a'; {[test]:2}", r#"{"a": 2}"#);635		assert_json!(636			r#"637				{638					name: "Alice",639					welcome: "Hello " + self.name + "!",640				}641			"#,642			r#"{"name": "Alice","welcome": "Hello Alice!"}"#643		);644		assert_json!(645			r#"646				{647					name: "Alice",648					welcome: "Hello " + self.name + "!",649				} + {650					name: "Bob"651				}652			"#,653			r#"{"name": "Bob","welcome": "Hello Bob!"}"#654		);655	}656657	#[test]658	fn functions() {659		assert_json!(r#"local a = function(b, c = 2) b + c; a(2)"#, "4");660		assert_json!(661			r#"local a = function(b, c = "Dear") b + c + d, d = "World"; a("Hello")"#,662			r#""HelloDearWorld""#663		);664	}665666	#[test]667	fn local_methods() {668		assert_json!(r#"local a(b, c = 2) = b + c; a(2)"#, "4");669		assert_json!(670			r#"local a(b, c = "Dear") = b + c + d, d = "World"; a("Hello")"#,671			r#""HelloDearWorld""#672		);673	}674675	#[test]676	fn object_locals() {677		assert_json!(r#"{local a = 3, b: a}"#, r#"{"b": 3}"#);678		assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b": 3}"#);679		assert_json!(680			r#"{local a = function (b) {[b]:4}, test: a("test")}"#,681			r#"{"test": {"test": 4}}"#682		);683	}684685	#[test]686	fn object_comp() {687		assert_json!(688			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}"#,689			"{\"h1_2\": \"0a\",\"h1_3\": \"0a\",\"h1_4\": \"0a\",\"h2_3\": \"a1\",\"h2_4\": \"a1\",\"h3_2\": \"0a\",\"h3_4\": \"a1\"}"690		)691	}692693	#[test]694	fn direct_self() {695		println!(696			"{:#?}",697			eval!(698				r#"699					{700						local me = self,701						a: 3,702						b(): me.a,703					}704				"#705			)706		);707	}708709	#[test]710	fn indirect_self() {711		// `self` assigned to `me` was lost when being712		// referenced from field713		eval!(714			r#"{715				local me = self,716				a: 3,717				b: me.a,718			}.b"#719		);720	}721722	// We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly723	#[test]724	fn std_assert_ok() {725		eval!("std.assertEqual(4.5 << 2, 16)");726	}727728	#[test]729	#[should_panic]730	fn std_assert_failure() {731		eval!("std.assertEqual(4.5 << 2, 15)");732	}733734	#[test]735	fn string_is_string() {736		assert!(primitive_equals(737			&eval!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),738			&Val::Bool(false),739		)740		.unwrap());741	}742743	#[test]744	fn base64_works() {745		assert_json!(r#"std.base64("test")"#, r#""dGVzdA==""#);746	}747748	#[test]749	fn utf8_chars() {750		assert_json!(751			r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,752			r#"{"c": 128526,"l": 1}"#753		)754	}755756	#[test]757	fn json() {758		assert_json!(759			r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,760			r#""{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}""#761		);762	}763764	#[test]765	fn test() {766		assert_json!(767			r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,768			"[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"769		);770	}771772	#[test]773	fn sjsonnet() {774		eval!(775			r#"776			local x0 = {k: 1};777			local x1 = {k: x0.k + x0.k};778			local x2 = {k: x1.k + x1.k};779			local x3 = {k: x2.k + x2.k};780			local x4 = {k: x3.k + x3.k};781			local x5 = {k: x4.k + x4.k};782			local x6 = {k: x5.k + x5.k};783			local x7 = {k: x6.k + x6.k};784			local x8 = {k: x7.k + x7.k};785			local x9 = {k: x8.k + x8.k};786			local x10 = {k: x9.k + x9.k};787			local x11 = {k: x10.k + x10.k};788			local x12 = {k: x11.k + x11.k};789			local x13 = {k: x12.k + x12.k};790			local x14 = {k: x13.k + x13.k};791			local x15 = {k: x14.k + x14.k};792			local x16 = {k: x15.k + x15.k};793			local x17 = {k: x16.k + x16.k};794			local x18 = {k: x17.k + x17.k};795			local x19 = {k: x18.k + x18.k};796			local x20 = {k: x19.k + x19.k};797			local x21 = {k: x20.k + x20.k};798			x21.k799		"#800		);801	}802803	// This test is commented out by default, because of huge compilation slowdown804	// #[bench]805	// fn bench_codegen(b: &mut Bencher) {806	// 	b.iter(|| {807	// 		#[allow(clippy::all)]808	// 		let stdlib = {809	// 			use jrsonnet_parser::*;810	// 			include!(concat!(env!("OUT_DIR"), "/stdlib.rs"))811	// 		};812	// 		stdlib813	// 	})814	// }815816	/*817	#[bench]818	fn bench_serialize(b: &mut Bencher) {819		b.iter(|| {820			bincode::deserialize::<jrsonnet_parser::LocExpr>(include_bytes!(concat!(821				env!("OUT_DIR"),822				"/stdlib.bincode"823			)))824			.expect("deserialize stdlib")825		})826	}827828	#[bench]829	fn bench_parse(b: &mut Bencher) {830		b.iter(|| {831			jrsonnet_parser::parse(832				jrsonnet_stdlib::STDLIB_STR,833				&jrsonnet_parser::ParserSettings {834					loc_data: true,835					file_name: Rc::new(PathBuf::from("std.jsonnet")),836				},837			)838		})839	}840	*/841842	#[test]843	fn equality() {844		println!(845			"{:?}",846			jrsonnet_parser::parse(847				"{ x: 1, y: 2 } == { x: 1, y: 2 }",848				&ParserSettings::default()849			)850		);851		assert_eval!("{ x: 1, y: 2 } == { x: 1, y: 2 }")852	}853}
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -131,6 +131,14 @@
 	}
 }
 
+#[derive(Clone)]
+pub enum ManifestFormat {
+	YamlStream(Box<ManifestFormat>),
+	Yaml(usize),
+	Json(usize),
+	String,
+}
+
 #[derive(Debug, Clone)]
 pub enum Val {
 	Bool(bool),
@@ -221,10 +229,46 @@
 		})
 	}
 
+
+	pub fn manifest(&self, ty: &ManifestFormat) -> Result<Rc<str>> {
+		Ok(match ty {
+			ManifestFormat::YamlStream(format) => {
+				let arr = match self {
+					Val::Arr(a) => a,
+					_ => throw!(StreamManifestOutputIsNotAArray),
+				};
+				let mut out = String::new();
+
+				match format as &ManifestFormat {
+					ManifestFormat::YamlStream(_) => throw!(StreamManifestOutputCannotBeRecursed),
+					ManifestFormat::String => throw!(StreamManifestCannotNestString),
+					_ => {}
+				};
+
+				if !arr.is_empty() {
+					for v in arr.iter() {
+						out.push_str("---\n");
+						out.push_str(&v.manifest(format)?);
+						out.push_str("\n");
+					}
+					out.push_str("...");
+				}
+
+				out.into()
+			}
+			ManifestFormat::Yaml(padding) => self.to_yaml(*padding)?,
+			ManifestFormat::Json(padding) => self.to_json(*padding)?,
+			ManifestFormat::String => match self {
+				Val::Str(s) => s.clone(),
+				_ => throw!(StringManifestOutputIsNotAString),
+			},
+		})
+	}
+
 	/// For manifestification
-	pub fn into_json(self, padding: usize) -> Result<Rc<str>> {
+	pub fn to_json(&self, padding: usize) -> Result<Rc<str>> {
 		manifest_json_ex(
-			&self,
+			self,
 			&ManifestJsonOptions {
 				padding: &" ".repeat(padding),
 				mtype: if padding == 0 {
@@ -239,7 +283,7 @@
 
 	/// Calls std.manifestJson
 	#[cfg(feature = "faster")]
-	pub fn into_std_json(self, padding: usize) -> Result<Rc<str>> {
+	pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {
 		manifest_json_ex(
 			&self,
 			&ManifestJsonOptions {
@@ -252,11 +296,11 @@
 
 	/// Calls std.manifestJson
 	#[cfg(not(feature = "faster"))]
-	pub fn into_std_json(self, padding: usize) -> Result<Rc<str>> {
+	pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {
 		with_state(|s| {
 			let ctx = s
 				.create_default_context()?
-				.with_var("__tmp__to_json__".into(), self)?;
+				.with_var("__tmp__to_json__".into(), self.clone())?;
 			Ok(evaluate(
 				ctx,
 				&el!(Expr::Apply(
@@ -274,11 +318,11 @@
 			.try_cast_str("to json")?)
 		})
 	}
-	pub fn into_yaml(self, padding: usize) -> Result<Rc<str>> {
+	pub fn to_yaml(&self, padding: usize) -> Result<Rc<str>> {
 		with_state(|s| {
 			let ctx = s
 				.create_default_context()?
-				.with_var("__tmp__to_json__".into(), self);
+				.with_var("__tmp__to_json__".into(), self.clone());
 			Ok(evaluate(
 				ctx,
 				&el!(Expr::Apply(