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

difftreelog

Merge pull request #64 from CertainLach/feat/manifest-yaml-doc-builtin

Yaroslav Bolyukin2021-10-29parents: #27b30fb #c0cb444.patch.diff
in: master
Make manifestYamlDoc builtin

6 files changed

modifiedcrates/jrsonnet-cli/src/manifest.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/manifest.rs
+++ b/crates/jrsonnet-cli/src/manifest.rs
@@ -38,9 +38,9 @@
 	#[clap(long, short = 'y')]
 	yaml_stream: bool,
 	/// Number of spaces to pad output manifest with.
-	/// `0` for hard tabs, `-1` for single line output
-	#[clap(long, default_value = "3")]
-	line_padding: usize,
+	/// `0` for hard tabs, `-1` for single line output [default: 3 for json, 2 for yaml]
+	#[clap(long)]
+	line_padding: Option<usize>,
 }
 impl ConfigureState for ManifestOpts {
 	fn configure(&self, state: &EvaluationState) -> Result<()> {
@@ -50,10 +50,10 @@
 			match self.format {
 				ManifestFormatName::String => state.set_manifest_format(ManifestFormat::String),
 				ManifestFormatName::Json => {
-					state.set_manifest_format(ManifestFormat::Json(self.line_padding))
+					state.set_manifest_format(ManifestFormat::Json(self.line_padding.unwrap_or(3)))
 				}
 				ManifestFormatName::Yaml => {
-					state.set_manifest_format(ManifestFormat::Yaml(self.line_padding))
+					state.set_manifest_format(ManifestFormat::Yaml(self.line_padding.unwrap_or(2)))
 				}
 			}
 		}
modifiedcrates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/manifest.rs
@@ -156,3 +156,127 @@
 	}
 	buf.push('"');
 }
+
+pub struct ManifestYamlOptions<'s> {
+	/// Padding before fields, i.e
+	/// ```yaml
+	/// a:
+	///   b:
+	/// ## <- this
+	/// ```
+	pub padding: &'s str,
+	/// Padding before array elements in objects
+	/// ```yaml
+	/// a:
+	///   - 1
+	/// ## <- this
+	/// ```
+	pub arr_element_padding: &'s str,
+}
+
+pub fn manifest_yaml_ex(val: &Val, options: &ManifestYamlOptions<'_>) -> Result<String> {
+	let mut out = String::new();
+	manifest_yaml_ex_buf(val, &mut out, &mut String::new(), options)?;
+	Ok(out)
+}
+fn manifest_yaml_ex_buf(
+	val: &Val,
+	buf: &mut String,
+	cur_padding: &mut String,
+	options: &ManifestYamlOptions<'_>,
+) -> Result<()> {
+	use std::fmt::Write;
+	match val {
+		Val::Bool(v) => {
+			if *v {
+				buf.push_str("true")
+			} else {
+				buf.push_str("false")
+			}
+		}
+		Val::Null => buf.push_str("null"),
+		Val::Str(s) => {
+			if s.is_empty() {
+				buf.push_str("\"\"");
+			} else if let Some(s) = s.strip_suffix('\n') {
+				buf.push('|');
+				for line in s.split('\n') {
+					buf.push('\n');
+					buf.push_str(options.padding);
+					buf.push_str(line);
+				}
+			} else {
+				escape_string_json_buf(s, buf)
+			}
+		}
+		Val::Num(n) => write!(buf, "{}", *n).unwrap(),
+		Val::Arr(a) => {
+			if a.is_empty() {
+				buf.push_str("[]");
+			} else {
+				for (i, item) in a.iter().enumerate() {
+					if i != 0 {
+						buf.push('\n');
+						buf.push_str(cur_padding);
+					}
+					let item = item?;
+					buf.push('-');
+					match &item {
+						Val::Arr(a) if !a.is_empty() => {
+							buf.push('\n');
+							buf.push_str(cur_padding);
+							buf.push_str(options.padding);
+						}
+						_ => buf.push(' '),
+					}
+					let extra_padding = match &item {
+						Val::Arr(a) => !a.is_empty(),
+						Val::Obj(o) => !o.is_empty(),
+						_ => false,
+					};
+					let prev_len = cur_padding.len();
+					if extra_padding {
+						cur_padding.push_str(options.padding);
+					}
+					manifest_yaml_ex_buf(&item, buf, cur_padding, options)?;
+					cur_padding.truncate(prev_len);
+				}
+			}
+		}
+		Val::Obj(o) => {
+			if o.is_empty() {
+				buf.push_str("{}");
+			} else {
+				for (i, key) in o.fields().iter().enumerate() {
+					if i != 0 {
+						buf.push('\n');
+						buf.push_str(cur_padding);
+					}
+					escape_string_json_buf(key, buf);
+					buf.push(':');
+					let prev_len = cur_padding.len();
+					let item = o.get(key.clone())?.expect("field exists");
+					match &item {
+						Val::Arr(a) if !a.is_empty() => {
+							buf.push('\n');
+							buf.push_str(cur_padding);
+							buf.push_str(options.arr_element_padding);
+							cur_padding.push_str(options.arr_element_padding);
+						}
+						Val::Obj(o) if !o.is_empty() => {
+							buf.push('\n');
+							buf.push_str(cur_padding);
+							buf.push_str(options.padding);
+							cur_padding.push_str(options.padding);
+						}
+						_ => buf.push(' '),
+					}
+					manifest_yaml_ex_buf(&item, buf, cur_padding, options)?;
+					cur_padding.truncate(prev_len);
+				}
+			}
+		}
+		Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),
+	}
+	Ok(())
+}
modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
after · crates/jrsonnet-evaluator/src/builtin/mod.rs
1use crate::{2	builtin::manifest::{manifest_yaml_ex, ManifestYamlOptions},3	equals,4	error::{Error::*, Result},5	operator::evaluate_mod_op,6	parse_args, primitive_equals, push, throw, with_state, ArrValue, Context, EvaluationState,7	FuncVal, IndexableVal, LazyVal, Val,8};9use format::{format_arr, format_obj};10use jrsonnet_gc::Gc;11use jrsonnet_interner::IStr;12use jrsonnet_parser::{ArgsDesc, ExprLocation};13use jrsonnet_types::ty;14use std::{collections::HashMap, path::PathBuf, rc::Rc};1516pub mod stdlib;17pub use stdlib::*;1819use self::manifest::{escape_string_json, manifest_json_ex, ManifestJsonOptions, ManifestType};2021pub mod format;22pub mod manifest;23pub mod sort;2425pub fn std_format(str: IStr, vals: Val) -> Result<Val> {26	push(27		Some(&ExprLocation(Rc::from(PathBuf::from("std.jsonnet")), 0, 0)),28		|| format!("std.format of {}", str),29		|| {30			Ok(match vals {31				Val::Arr(vals) => Val::Str(format_arr(&str, &vals.evaluated()?)?.into()),32				Val::Obj(obj) => Val::Str(format_obj(&str, &obj)?.into()),33				o => Val::Str(format_arr(&str, &[o])?.into()),34			})35		},36	)37}3839pub fn std_slice(40	indexable: IndexableVal,41	index: Option<usize>,42	end: Option<usize>,43	step: Option<usize>,44) -> Result<Val> {45	let index = index.unwrap_or(0);46	let end = end.unwrap_or_else(|| match &indexable {47		IndexableVal::Str(_) => usize::MAX,48		IndexableVal::Arr(v) => v.len(),49	});50	let step = step.unwrap_or(1);51	match &indexable {52		IndexableVal::Str(s) => Ok(Val::Str(53			(s.chars()54				.skip(index)55				.take(end - index)56				.step_by(step)57				.collect::<String>())58			.into(),59		)),60		IndexableVal::Arr(arr) => Ok(Val::Arr(61			(arr.iter()62				.skip(index)63				.take(end - index)64				.step_by(step)65				.collect::<Result<Vec<Val>>>()?)66			.into(),67		)),68	}69}7071type Builtin = fn(context: Context, loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val>;7273type BuiltinsType = HashMap<Box<str>, Builtin>;7475thread_local! {76	static BUILTINS: BuiltinsType = {77		[78			("length".into(), builtin_length as Builtin),79			("type".into(), builtin_type),80			("makeArray".into(), builtin_make_array),81			("codepoint".into(), builtin_codepoint),82			("objectFieldsEx".into(), builtin_object_fields_ex),83			("objectHasEx".into(), builtin_object_has_ex),84			("slice".into(), builtin_slice),85			("substr".into(), builtin_substr),86			("primitiveEquals".into(), builtin_primitive_equals),87			("equals".into(), builtin_equals),88			("modulo".into(), builtin_modulo),89			("mod".into(), builtin_mod),90			("floor".into(), builtin_floor),91			("ceil".into(), builtin_ceil),92			("log".into(), builtin_log),93			("pow".into(), builtin_pow),94			("sqrt".into(), builtin_sqrt),95			("sin".into(), builtin_sin),96			("cos".into(), builtin_cos),97			("tan".into(), builtin_tan),98			("asin".into(), builtin_asin),99			("acos".into(), builtin_acos),100			("atan".into(), builtin_atan),101			("exp".into(), builtin_exp),102			("mantissa".into(), builtin_mantissa),103			("exponent".into(), builtin_exponent),104			("extVar".into(), builtin_ext_var),105			("native".into(), builtin_native),106			("filter".into(), builtin_filter),107			("map".into(), builtin_map),108			("flatMap".into(), builtin_flatmap),109			("foldl".into(), builtin_foldl),110			("foldr".into(), builtin_foldr),111			("sortImpl".into(), builtin_sort_impl),112			("format".into(), builtin_format),113			("range".into(), builtin_range),114			("char".into(), builtin_char),115			("encodeUTF8".into(), builtin_encode_utf8),116			("decodeUTF8".into(), builtin_decode_utf8),117			("md5".into(), builtin_md5),118			("base64".into(), builtin_base64),119			("base64DecodeBytes".into(), builtin_base64_decode_bytes),120			("base64Decode".into(), builtin_base64_decode),121			("trace".into(), builtin_trace),122			("join".into(), builtin_join),123			("escapeStringJson".into(), builtin_escape_string_json),124			("manifestJsonEx".into(), builtin_manifest_json_ex),125			("manifestYamlDocImpl".into(), builtin_manifest_yaml_doc),126			("reverse".into(), builtin_reverse),127			("id".into(), builtin_id),128			("strReplace".into(), builtin_str_replace),129			("splitLimit".into(), builtin_splitlimit),130			("parseJson".into(), builtin_parse_json),131			("asciiUpper".into(), builtin_ascii_upper),132			("asciiLower".into(), builtin_ascii_lower),133			("member".into(), builtin_member),134			("count".into(), builtin_count),135		].iter().cloned().collect()136	};137}138139fn builtin_length(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {140	parse_args!(context, "length", args, 1, [141		0, x: ty!((string | object | array));142	], {143		Ok(match x {144			Val::Str(n) => Val::Num(n.chars().count() as f64),145			Val::Arr(a) => Val::Num(a.len() as f64),146			Val::Obj(o) => Val::Num(147				o.fields_visibility()148					.into_iter()149					.filter(|(_k, v)| *v)150					.count() as f64,151			),152			_ => unreachable!(),153		})154	})155}156157fn builtin_type(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {158	parse_args!(context, "type", args, 1, [159		0, x: ty!(any);160	], {161		Ok(Val::Str(x.value_type().name().into()))162	})163}164165fn builtin_make_array(166	context: Context,167	_loc: Option<&ExprLocation>,168	args: &ArgsDesc,169) -> Result<Val> {170	parse_args!(context, "makeArray", args, 2, [171		0, sz: ty!(BoundedNumber<(Some(0.0)), (None)>) => Val::Num;172		1, func: ty!(function) => Val::Func;173	], {174		let mut out = Vec::with_capacity(sz as usize);175		for i in 0..sz as usize {176			out.push(LazyVal::new_resolved(func.evaluate_values(177				context.clone(),178				&[Val::Num(i as f64)]179			)?))180		}181		Ok(Val::Arr(out.into()))182	})183}184185fn builtin_codepoint(186	context: Context,187	_loc: Option<&ExprLocation>,188	args: &ArgsDesc,189) -> Result<Val> {190	parse_args!(context, "codepoint", args, 1, [191		0, str: ty!(char) => Val::Str;192	], {193		Ok(Val::Num(str.chars().next().unwrap() as u32 as f64))194	})195}196197fn builtin_object_fields_ex(198	context: Context,199	_loc: Option<&ExprLocation>,200	args: &ArgsDesc,201) -> Result<Val> {202	parse_args!(context, "objectFieldsEx", args, 2, [203		0, obj: ty!(object) => Val::Obj;204		1, inc_hidden: ty!(boolean) => Val::Bool;205	], {206		let out = obj.fields_ex(inc_hidden);207		Ok(Val::Arr(out.into_iter().map(Val::Str).collect::<Vec<_>>().into()))208	})209}210211fn builtin_object_has_ex(212	context: Context,213	_loc: Option<&ExprLocation>,214	args: &ArgsDesc,215) -> Result<Val> {216	parse_args!(context, "objectHasEx", args, 3, [217		0, obj: ty!(object) => Val::Obj;218		1, f: ty!(string) => Val::Str;219		2, inc_hidden: ty!(boolean) => Val::Bool;220	], {221		Ok(Val::Bool(obj.has_field_ex(f, inc_hidden)))222	})223}224225fn builtin_parse_json(226	context: Context,227	_loc: Option<&ExprLocation>,228	args: &ArgsDesc,229) -> Result<Val> {230	parse_args!(context, "parseJson", args, 1, [231		0, s: ty!(string) => Val::Str;232	], {233		let state = EvaluationState::default();234		let path = PathBuf::from("std.parseJson").into();235		state.evaluate_snippet_raw(path ,s)236	})237}238239fn builtin_slice(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {240	parse_args!(context, "slice", args, 4, [241		0, indexable: ty!((string | array));242		1, index: ty!((number | null));243		2, end: ty!((number | null));244		3, step: ty!((number | null));245	], {246		std_slice(247			indexable.into_indexable()?,248			index.try_cast_nullable_num("index")?.map(|v| v as usize),249			end.try_cast_nullable_num("end")?.map(|v| v as usize),250			step.try_cast_nullable_num("step")?.map(|v| v as usize),251		)252	})253}254255fn builtin_substr(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {256	parse_args!(context, "substr", args, 3, [257		0, str: ty!(string) => Val::Str;258		1, from: ty!(BoundedNumber<(Some(0.0)), (None)>) => Val::Num;259		2, len: ty!(BoundedNumber<(Some(0.0)), (None)>) => Val::Num;260	], {261		let out: String = str.chars().skip(from as usize).take(len as usize).collect();262		Ok(Val::Str(out.into()))263	})264}265266fn builtin_primitive_equals(267	context: Context,268	_loc: Option<&ExprLocation>,269	args: &ArgsDesc,270) -> Result<Val> {271	parse_args!(context, "primitiveEquals", args, 2, [272		0, a: ty!(any);273		1, b: ty!(any);274	], {275		Ok(Val::Bool(primitive_equals(&a, &b)?))276	})277}278279fn builtin_equals(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {280	parse_args!(context, "equals", args, 2, [281		0, a: ty!(any);282		1, b: ty!(any);283	], {284		Ok(Val::Bool(equals(&a, &b)?))285	})286}287288fn builtin_modulo(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {289	parse_args!(context, "modulo", args, 2, [290		0, a: ty!(number) => Val::Num;291		1, b: ty!(number) => Val::Num;292	], {293		Ok(Val::Num(a % b))294	})295}296297fn builtin_mod(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {298	parse_args!(context, "mod", args, 2, [299		0, a: ty!((number | string));300		1, b: ty!(any);301	], {302		evaluate_mod_op(&a, &b)303	})304}305306fn builtin_floor(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {307	parse_args!(context, "floor", args, 1, [308		0, x: ty!(number) => Val::Num;309	], {310		Ok(Val::Num(x.floor()))311	})312}313314fn builtin_ceil(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {315	parse_args!(context, "ceil", args, 1, [316		0, x: ty!(number) => Val::Num;317	], {318		Ok(Val::Num(x.ceil()))319	})320}321322fn builtin_log(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {323	parse_args!(context, "log", args, 1, [324		0, n: ty!(number) => Val::Num;325	], {326		Ok(Val::Num(n.ln()))327	})328}329330fn builtin_pow(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {331	parse_args!(context, "pow", args, 2, [332		0, x: ty!(number) => Val::Num;333		1, n: ty!(number) => Val::Num;334	], {335		Ok(Val::Num(x.powf(n)))336	})337}338339fn builtin_sqrt(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {340	parse_args!(context, "sqrt", args, 1, [341		0, x: ty!(BoundedNumber<(Some(0.0)), (None)>) => Val::Num;342	], {343		Ok(Val::Num(x.sqrt()))344	})345}346347fn builtin_sin(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {348	parse_args!(context, "sin", args, 1, [349		0, x: ty!(number) => Val::Num;350	], {351		Ok(Val::Num(x.sin()))352	})353}354355fn builtin_cos(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {356	parse_args!(context, "cos", args, 1, [357		0, x: ty!(number) => Val::Num;358	], {359		Ok(Val::Num(x.cos()))360	})361}362363fn builtin_tan(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {364	parse_args!(context, "tan", args, 1, [365		0, x: ty!(number) => Val::Num;366	], {367		Ok(Val::Num(x.tan()))368	})369}370371fn builtin_asin(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {372	parse_args!(context, "asin", args, 1, [373		0, x: ty!(number) => Val::Num;374	], {375		Ok(Val::Num(x.asin()))376	})377}378379fn builtin_acos(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {380	parse_args!(context, "acos", args, 1, [381		0, x: ty!(number) => Val::Num;382	], {383		Ok(Val::Num(x.acos()))384	})385}386387fn builtin_atan(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {388	parse_args!(context, "atan", args, 1, [389		0, x: ty!(number) => Val::Num;390	], {391		Ok(Val::Num(x.atan()))392	})393}394395fn builtin_exp(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {396	parse_args!(context, "exp", args, 1, [397		0, x: ty!(number) => Val::Num;398	], {399		Ok(Val::Num(x.exp()))400	})401}402403fn frexp(s: f64) -> (f64, i16) {404	if 0.0 == s {405		(s, 0)406	} else {407		let lg = s.abs().log2();408		let x = (lg - lg.floor() - 1.0).exp2();409		let exp = lg.floor() + 1.0;410		(s.signum() * x, exp as i16)411	}412}413414fn builtin_mantissa(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {415	parse_args!(context, "mantissa", args, 1, [416		0, x: ty!(number) => Val::Num;417	], {418		Ok(Val::Num(frexp(x).0))419	})420}421422fn builtin_exponent(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {423	parse_args!(context, "exponent", args, 1, [424		0, x: ty!(number) => Val::Num;425	], {426		Ok(Val::Num(frexp(x).1.into()))427	})428}429430fn builtin_ext_var(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {431	parse_args!(context, "extVar", args, 1, [432		0, x: ty!(string) => Val::Str;433	], {434		Ok(with_state(|s| s.settings().ext_vars.get(&x).cloned()).ok_or(UndefinedExternalVariable(x))?)435	})436}437438fn builtin_native(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {439	parse_args!(context, "native", args, 1, [440		0, x: ty!(string) => Val::Str;441	], {442		Ok(with_state(|s| s.settings().ext_natives.get(&x).cloned()).map(|v| Val::Func(Gc::new(FuncVal::NativeExt(x.clone(), v)))).ok_or(UndefinedExternalFunction(x))?)443	})444}445446fn builtin_filter(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {447	parse_args!(context, "filter", args, 2, [448		0, func: ty!(function) => Val::Func;449		1, arr: ty!(array) => Val::Arr;450	], {451		Ok(Val::Arr(arr.filter(|val| func452			.evaluate_values(context.clone(), &[val.clone()])?453			.try_cast_bool("filter predicate"))?))454	})455}456457fn builtin_map(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {458	parse_args!(context, "map", args, 2, [459		0, func: ty!(function) => Val::Func;460		1, arr: ty!(array) => Val::Arr;461	], {462		Ok(Val::Arr(arr.map(|val| func463			.evaluate_values(context.clone(), &[val]))?))464	})465}466467fn builtin_flatmap(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {468	parse_args!(context, "flatMap", args, 2, [469		0, func: ty!(function) => Val::Func;470		1, arr: ty!((array | string));471	], {472		match arr {473			Val::Str(s) => {474				let mut out = String::new();475				for c in s.chars() {476					match func.evaluate_values(context.clone(), &[Val::Str(c.to_string().into())])? {477						Val::Str(o) => out.push_str(&o),478						_ => throw!(RuntimeError("in std.join all items should be strings".into())),479					};480				}481				Ok(Val::Str(out.into()))482			},483			Val::Arr(a) => {484				let mut out = Vec::new();485				for el in a.iter() {486					let el = el?;487					match func.evaluate_values(context.clone(), &[el])? {488						Val::Arr(o) => for oe in o.iter() {489							out.push(oe?)490						},491						_ => throw!(RuntimeError("in std.join all items should be arrays".into())),492					};493				}494				Ok(Val::Arr(out.into()))495			},496			_ => unreachable!(),497		}498	})499}500501fn builtin_foldl(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {502	parse_args!(context, "foldl", args, 3, [503		0, func: ty!(function) => Val::Func;504		1, arr: ty!(array) => Val::Arr;505		2, init: ty!(any);506	], {507		let mut acc = init;508		for i in arr.iter() {509			acc = func.evaluate_values(context.clone(), &[acc, i?])?;510		}511		Ok(acc)512	})513}514515fn builtin_foldr(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {516	parse_args!(context, "foldr", args, 3, [517		0, func: ty!(function) => Val::Func;518		1, arr: ty!(array) => Val::Arr;519		2, init: ty!(any);520	], {521		let mut acc = init;522		for i in arr.iter().rev() {523			acc = func.evaluate_values(context.clone(), &[i?, acc])?;524		}525		Ok(acc)526	})527}528529#[allow(non_snake_case)]530fn builtin_sort_impl(531	context: Context,532	_loc: Option<&ExprLocation>,533	args: &ArgsDesc,534) -> Result<Val> {535	parse_args!(context, "sort", args, 2, [536		0, arr: ty!(array) => Val::Arr;537		1, keyF: ty!(function) => Val::Func;538	], {539		if arr.len() <= 1 {540			return Ok(Val::Arr(arr))541		}542		Ok(Val::Arr(ArrValue::Eager(sort::sort(context, arr.evaluated()?, &keyF)?)))543	})544}545546fn builtin_format(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {547	parse_args!(context, "format", args, 2, [548		0, str: ty!(string) => Val::Str;549		1, vals: ty!(any)550	], {551		std_format(str, vals)552	})553}554555fn builtin_range(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {556	parse_args!(context, "range", args, 2, [557		0, from: ty!(number) => Val::Num;558		1, to: ty!(number) => Val::Num;559	], {560		if to < from {561			return Ok(Val::Arr(ArrValue::new_eager()))562		}563		let mut out = Vec::with_capacity((1+to as usize-from as usize).max(0));564		for i in from as usize..=to as usize {565			out.push(Val::Num(i as f64));566		}567		Ok(Val::Arr(out.into()))568	})569}570571fn builtin_char(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {572	parse_args!(context, "char", args, 1, [573		0, n: ty!(number) => Val::Num;574	], {575		let mut out = String::new();576		out.push(std::char::from_u32(n as u32).ok_or_else(||577			InvalidUnicodeCodepointGot(n as u32)578		)?);579		Ok(Val::Str(out.into()))580	})581}582583fn builtin_encode_utf8(584	context: Context,585	_loc: Option<&ExprLocation>,586	args: &ArgsDesc,587) -> Result<Val> {588	parse_args!(context, "encodeUTF8", args, 1, [589		0, str: ty!(string) => Val::Str;590	], {591		Ok(Val::Arr((str.bytes().map(|b| Val::Num(b as f64)).collect::<Vec<Val>>()).into()))592	})593}594595fn builtin_decode_utf8(596	context: Context,597	_loc: Option<&ExprLocation>,598	args: &ArgsDesc,599) -> Result<Val> {600	parse_args!(context, "decodeUTF8", args, 1, [601		0, arr: ty!((Array<ubyte>)) => Val::Arr;602	], {603		let data: Result<Vec<u8>> = arr.iter().map(|v| v.map(|v| match v{604			Val::Num(n) => n as u8,605			_ => unreachable!(),606		})).collect();607		let data = data?;608		Ok(Val::Str(String::from_utf8(data).map_err(|_| RuntimeError("bad utf8".into()))?.into()))609	})610}611612fn builtin_md5(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {613	parse_args!(context, "md5", args, 1, [614		0, str: ty!(string) => Val::Str;615	], {616		Ok(Val::Str(format!("{:x}", md5::compute(&str.as_bytes())).into()))617	})618}619620fn builtin_trace(context: Context, loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {621	parse_args!(context, "trace", args, 2, [622		0, str: ty!(string) => Val::Str;623		1, rest: ty!(any);624	], {625		eprint!("TRACE:");626		if let Some(loc) = loc {627			with_state(|s|{628				let locs = s.map_source_locations(&loc.0, &[loc.1]);629				eprint!(" {}:{}", loc.0.file_name().unwrap().to_str().unwrap(), locs[0].line);630			});631		}632		eprintln!(" {}", str);633		Ok(rest)634	})635}636637fn builtin_base64(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {638	parse_args!(context, "base64", args, 1, [639		0, input: ty!((string | (Array<number>)));640	], {641		Ok(Val::Str(match input {642			Val::Str(s) => {643				base64::encode(s.bytes().collect::<Vec<_>>()).into()644			},645			Val::Arr(a) => {646				base64::encode(a.iter().map(|v| {647					Ok(v?.unwrap_num()? as u8)648				}).collect::<Result<Vec<_>>>()?).into()649			},650			_ => unreachable!()651		}))652	})653}654655fn builtin_base64_decode_bytes(656	context: Context,657	_loc: Option<&ExprLocation>,658	args: &ArgsDesc,659) -> Result<Val> {660	parse_args!(context, "base64DecodeBytes", args, 1, [661		0, input: ty!(string) => Val::Str;662	], {663		Ok(Val::Arr(664			base64::decode(&input.as_bytes())665				.map_err(|_| RuntimeError("bad base64".into()))?666				.iter()667				.map(|v| Val::Num(*v as f64)).collect::<Vec<_>>().into()668		))669	})670}671672fn builtin_base64_decode(673	context: Context,674	_loc: Option<&ExprLocation>,675	args: &ArgsDesc,676) -> Result<Val> {677	parse_args!(context, "base64Decode", args, 1, [678		0, input: ty!(string) => Val::Str;679	], {680		Ok(Val::Str(681			String::from_utf8(base64::decode(&input.as_bytes())682				.map_err(|_| RuntimeError("bad base64".into()))?)683				.map_err(|_| RuntimeError("bad utf8".into()))?.into()684		))685	})686}687688fn builtin_join(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {689	parse_args!(context, "join", args, 2, [690		0, sep: ty!((string | array));691		1, arr: ty!(array) => Val::Arr;692	], {693		Ok(match sep {694			Val::Arr(joiner_items) => {695				let mut out = Vec::new();696697				let mut first = true;698				for item in arr.iter() {699					let item = item?.clone();700					if let Val::Arr(items) = item {701						if !first {702							out.reserve(joiner_items.len());703							// TODO: extend704							for item in joiner_items.iter() {705								out.push(item?);706							}707						}708						first = false;709						out.reserve(items.len());710						// TODO: extend711						for item in items.iter() {712							out.push(item?);713						}714					} else {715						throw!(RuntimeError("in std.join all items should be arrays".into()));716					}717				}718719				Val::Arr(out.into())720			},721			Val::Str(sep) => {722				let mut out = String::new();723724				let mut first = true;725				for item in arr.iter() {726					let item = item?.clone();727					if let Val::Str(item) = item {728						if !first {729							out += &sep;730						}731						first = false;732						out += &item;733					} else {734						throw!(RuntimeError("in std.join all items should be strings".into()));735					}736				}737738				Val::Str(out.into())739			},740			_ => unreachable!()741		})742	})743}744745fn builtin_escape_string_json(746	context: Context,747	_loc: Option<&ExprLocation>,748	args: &ArgsDesc,749) -> Result<Val> {750	parse_args!(context, "escapeStringJson", args, 1, [751		0, str_: ty!(string) => Val::Str;752	], {753		Ok(Val::Str(escape_string_json(&str_).into()))754	})755}756757fn builtin_manifest_json_ex(758	context: Context,759	_loc: Option<&ExprLocation>,760	args: &ArgsDesc,761) -> Result<Val> {762	parse_args!(context, "manifestJsonEx", args, 2, [763		0, value: ty!(any);764		1, indent: ty!(string) => Val::Str;765	], {766		Ok(Val::Str(manifest_json_ex(&value, &ManifestJsonOptions {767			padding: &indent,768			mtype: ManifestType::Std,769		})?.into()))770	})771}772773fn builtin_manifest_yaml_doc(774	context: Context,775	_loc: Option<&ExprLocation>,776	args: &ArgsDesc,777) -> Result<Val> {778	parse_args!(context, "manifestYamlDoc", args, 2, [779		0, value: ty!(any);780		1, indent_array_in_object: ty!(boolean) => Val::Bool;781	], {782		Ok(Val::Str(manifest_yaml_ex(&value, &ManifestYamlOptions {783			padding: "  ",784			arr_element_padding: if indent_array_in_object { "  " } else { "" },785		})?.into()))786	})787}788789fn builtin_reverse(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {790	parse_args!(context, "reverse", args, 1, [791		0, value: ty!(array) => Val::Arr;792	], {793		Ok(Val::Arr(value.reversed()))794	})795}796797fn builtin_id(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {798	parse_args!(context, "id", args, 1, [799		0, v: ty!(any);800	], {801		Ok(v)802	})803}804805fn builtin_str_replace(806	context: Context,807	_loc: Option<&ExprLocation>,808	args: &ArgsDesc,809) -> Result<Val> {810	parse_args!(context, "strReplace", args, 3, [811		0, str: ty!(string) => Val::Str;812		1, from: ty!(string) => Val::Str;813		2, to: ty!(string) => Val::Str;814	], {815		Ok(Val::Str(str.replace(&from as &str, &to as &str).into()))816	})817}818819fn builtin_splitlimit(820	context: Context,821	_loc: Option<&ExprLocation>,822	args: &ArgsDesc,823) -> Result<Val> {824	parse_args!(context, "splitLimit", args, 3, [825		0, str: ty!(string) => Val::Str;826		1, c: ty!(char) => Val::Str;827		2, maxsplits: ty!(number) => Val::Num;828	], {829		let maxsplits = maxsplits as isize;830		let c = c.chars().next().unwrap();831832		let out: Vec<Val> = if maxsplits == -1 {833			str.split(c).map(|s| Val::Str(s.into())).collect()834		} else {835			str.splitn(maxsplits as usize + 1, c).map(|s| Val::Str(s.into())).collect()836		};837838		Ok(Val::Arr(out.into()))839	})840}841842fn builtin_ascii_upper(843	context: Context,844	_loc: Option<&ExprLocation>,845	args: &ArgsDesc,846) -> Result<Val> {847	parse_args!(context, "asciiUpper", args, 1, [848		0, str: ty!(string) => Val::Str;849	], {850		Ok(Val::Str(str.to_ascii_uppercase().into()))851	})852}853854fn builtin_ascii_lower(855	context: Context,856	_loc: Option<&ExprLocation>,857	args: &ArgsDesc,858) -> Result<Val> {859	parse_args!(context, "asciiLower", args, 1, [860		0, str: ty!(string) => Val::Str;861	], {862		Ok(Val::Str(str.to_ascii_lowercase().into()))863	})864}865866fn builtin_member(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {867	parse_args!(context, "member", args, 2, [868		0, arr: ty!((array | string));869		1, x: ty!(any);870	], {871		match arr {872			Val::Str(s) => {873				let x = x.try_cast_str("x should be string")?;874				Ok(Val::Bool(!x.is_empty() && s.contains(&*x)))875			}876			Val::Arr(a) => {877				for item in a.iter() {878					let item = item?;879					if equals(&item, &x)? {880						return Ok(Val::Bool(true));881					}882				}883				Ok(Val::Bool(false))884			}885			_ => unreachable!(),886		}887	})888}889890fn builtin_count(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {891	parse_args!(context, "count", args, 2, [892		0, arr: ty!(array) => Val::Arr;893		1, x: ty!(any);894	], {895		let mut count = 0;896		for item in arr.iter() {897			let item = item?;898			if equals(&item, &x)? {899				count += 1;900			}901		}902		Ok(Val::Num(count as f64))903	})904}905906pub fn call_builtin(907	context: Context,908	loc: Option<&ExprLocation>,909	name: &str,910	args: &ArgsDesc,911) -> Result<Val> {912	BUILTINS913		.with(|builtins| builtins.get(name).copied())914		.ok_or_else(|| IntrinsicNotFound(name.into()))?(context, loc, args)915}
modifiedcrates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -105,6 +105,17 @@
 		}))
 	}
 
+	pub fn is_empty(&self) -> bool {
+		if !self.0.this_entries.is_empty() {
+			return false;
+		}
+		self.0
+			.super_obj
+			.as_ref()
+			.map(|s| s.is_empty())
+			.unwrap_or(true)
+	}
+
 	/// Run callback for every field found in object
 	pub(crate) fn enum_fields(&self, handler: &mut impl FnMut(&IStr, &Visibility) -> bool) -> bool {
 		if let Some(s) = &self.0.super_obj {
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -1,17 +1,20 @@
 use crate::{
 	builtin::{
 		call_builtin,
-		manifest::{manifest_json_ex, ManifestJsonOptions, ManifestType},
+		manifest::{
+			manifest_json_ex, manifest_yaml_ex, ManifestJsonOptions, ManifestType,
+			ManifestYamlOptions,
+		},
 	},
 	error::{Error::*, LocError},
 	evaluate,
 	function::{parse_function_call, parse_function_call_map, place_args},
 	native::NativeCallback,
-	throw, with_state, Context, ObjValue, Result,
+	throw, Context, ObjValue, Result,
 };
 use jrsonnet_gc::{Gc, GcCell, Trace};
 use jrsonnet_interner::IStr;
-use jrsonnet_parser::{el, ArgsDesc, Expr, ExprLocation, LiteralType, LocExpr, ParamsDesc};
+use jrsonnet_parser::{ArgsDesc, ExprLocation, LocExpr, ParamsDesc};
 use jrsonnet_types::ValType;
 use std::{collections::HashMap, fmt::Debug, rc::Rc};
 
@@ -393,6 +396,12 @@
 	pub fn unwrap_num(self) -> Result<f64> {
 		Ok(matches_unwrap!(self, Self::Num(v), v))
 	}
+	pub fn unwrap_str(self) -> Result<IStr> {
+		Ok(matches_unwrap!(self, Self::Str(v), v))
+	}
+	pub fn unwrap_arr(self) -> Result<ArrValue> {
+		Ok(matches_unwrap!(self, Self::Arr(v), v))
+	}
 	pub fn unwrap_func(self) -> Result<Gc<FuncVal>> {
 		Ok(matches_unwrap!(self, Self::Func(v), v))
 	}
@@ -544,33 +553,15 @@
 	}
 
 	pub fn to_yaml(&self, padding: usize) -> Result<IStr> {
-		with_state(|s| {
-			let ctx = s
-				.create_default_context()
-				.with_var("__tmp__to_json__".into(), self.clone());
-			evaluate(
-				ctx,
-				&el!(Expr::Apply(
-					el!(Expr::Index(
-						el!(Expr::Var("std".into())),
-						el!(Expr::Str("manifestYamlDoc".into()))
-					)),
-					ArgsDesc::new(
-						vec![
-							el!(Expr::Var("__tmp__to_json__".into())),
-							el!(Expr::Literal(if padding != 0 {
-								LiteralType::True
-							} else {
-								LiteralType::False
-							})),
-						],
-						vec![]
-					),
-					false
-				)),
-			)?
-			.try_cast_str("to json")
-		})
+		let padding = &" ".repeat(padding);
+		manifest_yaml_ex(
+			self,
+			&ManifestYamlOptions {
+				padding,
+				arr_element_padding: padding,
+			},
+		)
+		.map(|s| s.into())
 	}
 	pub fn into_indexable(self) -> Result<IndexableVal> {
 		Ok(match self {
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -374,88 +374,9 @@
 
   manifestJsonEx:: $intrinsic(manifestJsonEx),
 
-  manifestYamlDoc(value, indent_array_in_object=false)::
-    local aux(v, path, cindent) =
-      if v == true then
-        'true'
-      else if v == false then
-        'false'
-      else if v == null then
-        'null'
-      else if std.isNumber(v) then
-        '' + v
-      else if std.isString(v) then
-        local len = std.length(v);
-        if len == 0 then
-          '""'
-        else if v[len - 1] == '\n' then
-          local split = std.split(v, '\n');
-          std.join('\n' + cindent + '  ', ['|'] + split[0:std.length(split) - 1])
-        else
-          std.escapeStringJson(v)
-      else if std.isFunction(v) then
-        error 'Tried to manifest function at ' + path
-      else if std.isArray(v) then
-        if std.length(v) == 0 then
-          '[]'
-        else
-          local params(value) =
-            if std.isArray(value) && std.length(value) > 0 then {
-              // While we could avoid the new line, it yields YAML that is
-              // hard to read, e.g.:
-              // - - - 1
-              //     - 2
-              //   - - 3
-              //     - 4
-              new_indent: cindent + '  ',
-              space: '\n' + self.new_indent,
-            } else if std.isObject(value) && std.length(value) > 0 then {
-              new_indent: cindent + '  ',
-              // In this case we can start on the same line as the - because the indentation
-              // matches up then.  The converse is not true, because fields are not always
-              // 1 character long.
-              space: ' ',
-            } else {
-              // In this case, new_indent is only used in the case of multi-line strings.
-              new_indent: cindent,
-              space: ' ',
-            };
-          local range = std.range(0, std.length(v) - 1);
-          local parts = [
-            '-' + param.space + aux(v[i], path + [i], param.new_indent)
-            for i in range
-            for param in [params(v[i])]
-          ];
-          std.join('\n' + cindent, parts)
-      else if std.isObject(v) then
-        if std.length(v) == 0 then
-          '{}'
-        else
-          local params(value) =
-            if std.isArray(value) && std.length(value) > 0 then {
-              // Not indenting allows e.g.
-              // ports:
-              // - 80
-              // instead of
-              // ports:
-              //   - 80
-              new_indent: if indent_array_in_object then cindent + '  ' else cindent,
-              space: '\n' + self.new_indent,
-            } else if std.isObject(value) && std.length(value) > 0 then {
-              new_indent: cindent + '  ',
-              space: '\n' + self.new_indent,
-            } else {
-              // In this case, new_indent is only used in the case of multi-line strings.
-              new_indent: cindent,
-              space: ' ',
-            };
-          local lines = [
-            std.escapeStringJson(k) + ':' + param.space + aux(v[k], path + [k], param.new_indent)
-            for k in std.objectFields(v)
-            for param in [params(v[k])]
-          ];
-          std.join('\n' + cindent, lines);
-    aux(value, [], ''),
+  manifestYamlDocImpl:: $intrinsic(manifestYamlDocImpl),
+
+  manifestYamlDoc(value, indent_array_in_object=false):: std.manifestYamlDocImpl(value, indent_array_in_object),
 
   manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::
     if !std.isArray(value) then