git.delta.rocks / jrsonnet / refs/commits / 89a650875ae4

difftreelog

perf move more stdlib functions to native

Yaroslav Bolyukin2023-08-13parent: #218d8cc.patch.diff
in: master

7 files changed

modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
before · crates/jrsonnet-stdlib/src/arrays.rs
1#![allow(non_snake_case)]23use jrsonnet_evaluator::{4	bail,5	function::{builtin, FuncVal},6	runtime_error,7	typed::{BoundedI32, BoundedUsize, Either2, NativeFn, Typed},8	val::{equals, ArrValue, IndexableVal},9	Either, IStr, Result, Thunk, Val,10};1112pub(crate) fn eval_on_empty(on_empty: Option<Thunk<Val>>) -> Result<Val> {13	if let Some(on_empty) = on_empty {14		on_empty.evaluate()15	} else {16		bail!("expected non-empty array")17	}18}1920#[builtin]21pub fn builtin_make_array(sz: BoundedI32<0, { i32::MAX }>, func: FuncVal) -> Result<ArrValue> {22	if *sz == 0 {23		return Ok(ArrValue::empty());24	}25	if let Some(trivial) = func.evaluate_trivial() {26		let mut out = Vec::with_capacity(*sz as usize);27		for _ in 0..*sz {28			out.push(trivial.clone())29		}30		Ok(ArrValue::eager(out))31	} else {32		Ok(ArrValue::range_exclusive(0, *sz).map(func))33	}34}3536#[builtin]37pub fn builtin_repeat(what: Either![IStr, ArrValue], count: usize) -> Result<Val> {38	Ok(match what {39		Either2::A(s) => Val::string(s.repeat(count)),40		Either2::B(arr) => Val::Arr(41			ArrValue::repeated(arr, count)42				.ok_or_else(|| runtime_error!("repeated length overflow"))?,43		),44	})45}4647#[builtin]48pub fn builtin_slice(49	indexable: IndexableVal,50	index: Option<BoundedUsize<0, { i32::MAX as usize }>>,51	end: Option<BoundedUsize<0, { i32::MAX as usize }>>,52	step: Option<BoundedUsize<1, { i32::MAX as usize }>>,53) -> Result<Val> {54	indexable.slice(index, end, step).map(Val::from)55}5657#[builtin]58pub fn builtin_map(func: FuncVal, arr: IndexableVal) -> ArrValue {59	let arr = arr.to_array();60	arr.map(func)61}6263#[builtin]64pub fn builtin_flatmap(65	func: NativeFn<((Either![String, Val],), Val)>,66	arr: IndexableVal,67) -> Result<IndexableVal> {68	use std::fmt::Write;69	match arr {70		IndexableVal::Str(str) => {71			let mut out = String::new();72			for c in str.chars() {73				match func(Either2::A(c.to_string()))? {74					Val::Str(o) => write!(out, "{o}").unwrap(),75					Val::Null => continue,76					_ => bail!("in std.join all items should be strings"),77				};78			}79			Ok(IndexableVal::Str(out.into()))80		}81		IndexableVal::Arr(a) => {82			let mut out = Vec::new();83			for el in a.iter() {84				let el = el?;85				match func(Either2::B(el))? {86					Val::Arr(o) => {87						for oe in o.iter() {88							out.push(oe?);89						}90					}91					Val::Null => continue,92					_ => bail!("in std.join all items should be arrays"),93				};94			}95			Ok(IndexableVal::Arr(out.into()))96		}97	}98}99100#[builtin]101pub fn builtin_filter(func: FuncVal, arr: ArrValue) -> Result<ArrValue> {102	arr.filter(|val| bool::from_untyped(func.evaluate_simple(&(val.clone(),), false)?))103}104105#[builtin]106pub fn builtin_foldl(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {107	let mut acc = init;108	for i in arr.iter() {109		acc = func.evaluate_simple(&(acc, i?), false)?;110	}111	Ok(acc)112}113114#[builtin]115pub fn builtin_foldr(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {116	let mut acc = init;117	for i in arr.iter().rev() {118		acc = func.evaluate_simple(&(i?, acc), false)?;119	}120	Ok(acc)121}122123#[builtin]124pub fn builtin_range(from: i32, to: i32) -> Result<ArrValue> {125	if to < from {126		return Ok(ArrValue::empty());127	}128	Ok(ArrValue::range_inclusive(from, to))129}130131#[builtin]132pub fn builtin_join(sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {133	use std::fmt::Write;134	Ok(match sep {135		IndexableVal::Arr(joiner_items) => {136			let mut out = Vec::new();137138			let mut first = true;139			for item in arr.iter() {140				let item = item?.clone();141				if let Val::Arr(items) = item {142					if !first {143						out.reserve(joiner_items.len());144						// TODO: extend145						for item in joiner_items.iter() {146							out.push(item?);147						}148					}149					first = false;150					out.reserve(items.len());151					for item in items.iter() {152						out.push(item?);153					}154				} else if matches!(item, Val::Null) {155					continue;156				} else {157					bail!("in std.join all items should be arrays");158				}159			}160161			IndexableVal::Arr(out.into())162		}163		IndexableVal::Str(sep) => {164			let mut out = String::new();165166			let mut first = true;167			for item in arr.iter() {168				let item = item?.clone();169				if let Val::Str(item) = item {170					if !first {171						out += &sep;172					}173					first = false;174					write!(out, "{item}").unwrap()175				} else if matches!(item, Val::Null) {176					continue;177				} else {178					bail!("in std.join all items should be strings");179				}180			}181182			IndexableVal::Str(out.into())183		}184	})185}186187#[builtin]188pub fn builtin_reverse(arr: ArrValue) -> ArrValue {189	arr.reversed()190}191192#[builtin]193pub fn builtin_any(arr: ArrValue) -> Result<bool> {194	for v in arr.iter() {195		let v = bool::from_untyped(v?)?;196		if v {197			return Ok(true);198		}199	}200	Ok(false)201}202203#[builtin]204pub fn builtin_all(arr: ArrValue) -> Result<bool> {205	for v in arr.iter() {206		let v = bool::from_untyped(v?)?;207		if !v {208			return Ok(false);209		}210	}211	Ok(true)212}213214#[builtin]215pub fn builtin_member(arr: IndexableVal, x: Val) -> Result<bool> {216	match arr {217		IndexableVal::Str(str) => {218			let x: IStr = IStr::from_untyped(x)?;219			Ok(!x.is_empty() && str.contains(&*x))220		}221		IndexableVal::Arr(a) => {222			for item in a.iter() {223				let item = item?;224				if equals(&item, &x)? {225					return Ok(true);226				}227			}228			Ok(false)229		}230	}231}232233#[builtin]234pub fn builtin_contains(arr: IndexableVal, elem: Val) -> Result<bool> {235	builtin_member(arr, elem)236}237238#[builtin]239pub fn builtin_count(arr: ArrValue, x: Val) -> Result<usize> {240	let mut count = 0;241	for item in arr.iter() {242		if equals(&item?, &x)? {243			count += 1;244		}245	}246	Ok(count)247}248249#[builtin]250pub fn builtin_avg(arr: Vec<f64>, onEmpty: Option<Thunk<Val>>) -> Result<Val> {251	if arr.is_empty() {252		return eval_on_empty(onEmpty);253	}254	Ok(Val::Num(arr.iter().sum::<f64>() / (arr.len() as f64)))255}256257#[builtin]258pub fn builtin_remove_at(arr: ArrValue, at: usize) -> Result<ArrValue> {259	let newArrLeft = arr.clone().slice(None, Some(at), None);260	let newArrRight = arr.slice(Some(at + 1), None, None);261262	Ok(ArrValue::extended(263		newArrLeft.unwrap_or(ArrValue::empty()),264		newArrRight.unwrap_or(ArrValue::empty()),265	))266}267268#[builtin]269pub fn builtin_remove(arr: ArrValue, elem: Val) -> Result<ArrValue> {270	for (index, item) in arr.iter().enumerate() {271		if equals(&item?, &elem)? {272			return builtin_remove_at(arr.clone(), index);273		}274	}275	Ok(arr)276}
after · crates/jrsonnet-stdlib/src/arrays.rs
1#![allow(non_snake_case)]23use jrsonnet_evaluator::{4	bail,5	function::{builtin, FuncVal},6	runtime_error,7	typed::{BoundedI32, BoundedUsize, Either2, NativeFn, Typed},8	val::{equals, ArrValue, IndexableVal},9	Either, IStr, Result, Thunk, Val,10};1112pub(crate) fn eval_on_empty(on_empty: Option<Thunk<Val>>) -> Result<Val> {13	if let Some(on_empty) = on_empty {14		on_empty.evaluate()15	} else {16		bail!("expected non-empty array")17	}18}1920#[builtin]21pub fn builtin_make_array(sz: BoundedI32<0, { i32::MAX }>, func: FuncVal) -> Result<ArrValue> {22	if *sz == 0 {23		return Ok(ArrValue::empty());24	}25	if let Some(trivial) = func.evaluate_trivial() {26		let mut out = Vec::with_capacity(*sz as usize);27		for _ in 0..*sz {28			out.push(trivial.clone())29		}30		Ok(ArrValue::eager(out))31	} else {32		Ok(ArrValue::range_exclusive(0, *sz).map(func))33	}34}3536#[builtin]37pub fn builtin_repeat(what: Either![IStr, ArrValue], count: usize) -> Result<Val> {38	Ok(match what {39		Either2::A(s) => Val::string(s.repeat(count)),40		Either2::B(arr) => Val::Arr(41			ArrValue::repeated(arr, count)42				.ok_or_else(|| runtime_error!("repeated length overflow"))?,43		),44	})45}4647#[builtin]48pub fn builtin_slice(49	indexable: IndexableVal,50	index: Option<BoundedUsize<0, { i32::MAX as usize }>>,51	end: Option<BoundedUsize<0, { i32::MAX as usize }>>,52	step: Option<BoundedUsize<1, { i32::MAX as usize }>>,53) -> Result<Val> {54	indexable.slice(index, end, step).map(Val::from)55}5657#[builtin]58pub fn builtin_map(func: FuncVal, arr: IndexableVal) -> ArrValue {59	let arr = arr.to_array();60	arr.map(func)61}6263#[builtin]64pub fn builtin_flatmap(65	func: NativeFn<((Either![String, Val],), Val)>,66	arr: IndexableVal,67) -> Result<IndexableVal> {68	use std::fmt::Write;69	match arr {70		IndexableVal::Str(str) => {71			let mut out = String::new();72			for c in str.chars() {73				match func(Either2::A(c.to_string()))? {74					Val::Str(o) => write!(out, "{o}").unwrap(),75					Val::Null => continue,76					_ => bail!("in std.join all items should be strings"),77				};78			}79			Ok(IndexableVal::Str(out.into()))80		}81		IndexableVal::Arr(a) => {82			let mut out = Vec::new();83			for el in a.iter() {84				let el = el?;85				match func(Either2::B(el))? {86					Val::Arr(o) => {87						for oe in o.iter() {88							out.push(oe?);89						}90					}91					Val::Null => continue,92					_ => bail!("in std.join all items should be arrays"),93				};94			}95			Ok(IndexableVal::Arr(out.into()))96		}97	}98}99100#[builtin]101pub fn builtin_filter(func: FuncVal, arr: ArrValue) -> Result<ArrValue> {102	arr.filter(|val| bool::from_untyped(func.evaluate_simple(&(val.clone(),), false)?))103}104105#[builtin]106pub fn builtin_filter_map(107	filter_func: FuncVal,108	map_func: FuncVal,109	arr: ArrValue,110) -> Result<ArrValue> {111	Ok(builtin_filter(filter_func, arr)?.map(map_func))112}113114#[builtin]115pub fn builtin_foldl(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {116	let mut acc = init;117	for i in arr.iter() {118		acc = func.evaluate_simple(&(acc, i?), false)?;119	}120	Ok(acc)121}122123#[builtin]124pub fn builtin_foldr(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {125	let mut acc = init;126	for i in arr.iter().rev() {127		acc = func.evaluate_simple(&(i?, acc), false)?;128	}129	Ok(acc)130}131132#[builtin]133pub fn builtin_range(from: i32, to: i32) -> Result<ArrValue> {134	if to < from {135		return Ok(ArrValue::empty());136	}137	Ok(ArrValue::range_inclusive(from, to))138}139140#[builtin]141pub fn builtin_join(sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {142	use std::fmt::Write;143	Ok(match sep {144		IndexableVal::Arr(joiner_items) => {145			let mut out = Vec::new();146147			let mut first = true;148			for item in arr.iter() {149				let item = item?.clone();150				if let Val::Arr(items) = item {151					if !first {152						out.reserve(joiner_items.len());153						// TODO: extend154						for item in joiner_items.iter() {155							out.push(item?);156						}157					}158					first = false;159					out.reserve(items.len());160					for item in items.iter() {161						out.push(item?);162					}163				} else if matches!(item, Val::Null) {164					continue;165				} else {166					bail!("in std.join all items should be arrays");167				}168			}169170			IndexableVal::Arr(out.into())171		}172		IndexableVal::Str(sep) => {173			let mut out = String::new();174175			let mut first = true;176			for item in arr.iter() {177				let item = item?.clone();178				if let Val::Str(item) = item {179					if !first {180						out += &sep;181					}182					first = false;183					write!(out, "{item}").unwrap()184				} else if matches!(item, Val::Null) {185					continue;186				} else {187					bail!("in std.join all items should be strings");188				}189			}190191			IndexableVal::Str(out.into())192		}193	})194}195196#[builtin]197pub fn builtin_reverse(arr: ArrValue) -> ArrValue {198	arr.reversed()199}200201#[builtin]202pub fn builtin_any(arr: ArrValue) -> Result<bool> {203	for v in arr.iter() {204		let v = bool::from_untyped(v?)?;205		if v {206			return Ok(true);207		}208	}209	Ok(false)210}211212#[builtin]213pub fn builtin_all(arr: ArrValue) -> Result<bool> {214	for v in arr.iter() {215		let v = bool::from_untyped(v?)?;216		if !v {217			return Ok(false);218		}219	}220	Ok(true)221}222223#[builtin]224pub fn builtin_member(arr: IndexableVal, x: Val) -> Result<bool> {225	match arr {226		IndexableVal::Str(str) => {227			let x: IStr = IStr::from_untyped(x)?;228			Ok(!x.is_empty() && str.contains(&*x))229		}230		IndexableVal::Arr(a) => {231			for item in a.iter() {232				let item = item?;233				if equals(&item, &x)? {234					return Ok(true);235				}236			}237			Ok(false)238		}239	}240}241242#[builtin]243pub fn builtin_contains(arr: IndexableVal, elem: Val) -> Result<bool> {244	builtin_member(arr, elem)245}246247#[builtin]248pub fn builtin_count(arr: ArrValue, x: Val) -> Result<usize> {249	let mut count = 0;250	for item in arr.iter() {251		if equals(&item?, &x)? {252			count += 1;253		}254	}255	Ok(count)256}257258#[builtin]259pub fn builtin_avg(arr: Vec<f64>, onEmpty: Option<Thunk<Val>>) -> Result<Val> {260	if arr.is_empty() {261		return eval_on_empty(onEmpty);262	}263	Ok(Val::Num(arr.iter().sum::<f64>() / (arr.len() as f64)))264}265266#[builtin]267pub fn builtin_remove_at(arr: ArrValue, at: usize) -> Result<ArrValue> {268	let newArrLeft = arr.clone().slice(None, Some(at), None);269	let newArrRight = arr.slice(Some(at + 1), None, None);270271	Ok(ArrValue::extended(272		newArrLeft.unwrap_or(ArrValue::empty()),273		newArrRight.unwrap_or(ArrValue::empty()),274	))275}276277#[builtin]278pub fn builtin_remove(arr: ArrValue, elem: Val) -> Result<ArrValue> {279	for (index, item) in arr.iter().enumerate() {280		if equals(&item?, &elem)? {281			return builtin_remove_at(arr.clone(), index);282		}283	}284	Ok(arr)285}286287#[builtin]288pub fn builtin_flatten_arrays(arrs: Vec<ArrValue>) -> ArrValue {289	pub fn flatten_inner(values: &[ArrValue]) -> ArrValue {290		if values.len() == 1 {291			return values[0].clone();292		} else if values.len() == 2 {293			return ArrValue::extended(values[0].clone(), values[1].clone());294		}295		let (a, b) = values.split_at(values.len() / 2);296		ArrValue::extended(flatten_inner(a), flatten_inner(b))297	}298	if arrs.is_empty() {299		return ArrValue::empty();300	} else if arrs.len() == 1 {301		return arrs.into_iter().next().expect("single");302	}303	flatten_inner(&arrs)304}
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -84,6 +84,8 @@
 		("avg", builtin_avg::INST),
 		("removeAt", builtin_remove_at::INST),
 		("remove", builtin_remove::INST),
+		("flattenArrays", builtin_flatten_arrays::INST),
+		("filterMap", builtin_filter_map::INST),
 		// Math
 		("abs", builtin_abs::INST),
 		("sign", builtin_sign::INST),
@@ -137,17 +139,22 @@
 		("base64DecodeBytes", builtin_base64_decode_bytes::INST),
 		// Objects
 		("objectFieldsEx", builtin_object_fields_ex::INST),
+		("objectFields", builtin_object_fields::INST),
+		("objectFieldsAll", builtin_object_fields_all::INST),
 		("objectValues", builtin_object_values::INST),
 		("objectValuesAll", builtin_object_values_all::INST),
 		("objectKeysValues", builtin_object_keys_values::INST),
 		("objectKeysValuesAll", builtin_object_keys_values_all::INST),
 		("objectHasEx", builtin_object_has_ex::INST),
+		("objectHas", builtin_object_has::INST),
+		("objectHasAll", builtin_object_has_all::INST),
 		("objectRemoveKey", builtin_object_remove_key::INST),
 		// Manifest
 		("escapeStringJson", builtin_escape_string_json::INST),
 		("manifestJsonEx", builtin_manifest_json_ex::INST),
 		("manifestYamlDoc", builtin_manifest_yaml_doc::INST),
 		("manifestTomlEx", builtin_manifest_toml_ex::INST),
+		("toString", builtin_to_string::INST),
 		// Parsing
 		("parseJson", builtin_parse_json::INST),
 		("parseYaml", builtin_parse_yaml::INST),
@@ -167,6 +174,7 @@
 		("bigint", builtin_bigint::INST),
 		("parseOctal", builtin_parse_octal::INST),
 		("parseHex", builtin_parse_hex::INST),
+		("stringChars", builtin_string_chars::INST),
 		// Misc
 		("length", builtin_length::INST),
 		("startsWith", builtin_starts_with::INST),
@@ -175,6 +183,7 @@
 		("setMember", builtin_set_member::INST),
 		("setInter", builtin_set_inter::INST),
 		("setDiff", builtin_set_diff::INST),
+		("setUnion", builtin_set_union::INST),
 		// Compat
 		("__compare", builtin___compare::INST),
 	]
@@ -347,19 +356,15 @@
 
 		let mut std = ObjValueBuilder::new();
 		std.with_super(self.stdlib_obj.clone());
-		std.field("thisFile".into())
+		std.field("thisFile")
 			.hide()
-			.value(Val::string(match source.source_path().path() {
-				Some(p) => self.settings().path_resolver.resolve(p).into(),
-				None => source.source_path().to_string().into(),
-			}))
-			.expect("this object builder is empty");
+			.value(match source.source_path().path() {
+				Some(p) => self.settings().path_resolver.resolve(p),
+				None => source.source_path().to_string(),
+			});
 		let stdlib_with_this_file = std.build();
 
-		builder.bind(
-			"std".into(),
-			Thunk::evaluated(Val::Obj(stdlib_with_this_file)),
-		);
+		builder.bind("std", Thunk::evaluated(Val::Obj(stdlib_with_this_file)));
 	}
 	fn as_any(&self) -> &dyn std::any::Any {
 		self
modifiedcrates/jrsonnet-stdlib/src/manifest/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/manifest/mod.rs
+++ b/crates/jrsonnet-stdlib/src/manifest/mod.rs
@@ -60,3 +60,8 @@
 		preserve_order.unwrap_or(false),
 	))
 }
+
+#[builtin]
+pub fn builtin_to_string(a: Val) -> Result<IStr> {
+	a.to_string()
+}
modifiedcrates/jrsonnet-stdlib/src/objects.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/objects.rs
+++ b/crates/jrsonnet-stdlib/src/objects.rs
@@ -1,6 +1,6 @@
 use jrsonnet_evaluator::{
 	function::builtin,
-	val::{ArrValue, StrValue, Val},
+	val::{ArrValue, Val},
 	IStr, ObjValue, ObjValueBuilder,
 };
 
@@ -17,12 +17,35 @@
 		#[cfg(feature = "exp-preserve-order")]
 		preserve_order,
 	);
-	out.into_iter()
-		.map(StrValue::Flat)
-		.map(Val::Str)
-		.collect::<Vec<_>>()
+	out.into_iter().map(Val::string).collect::<Vec<_>>()
+}
+
+#[builtin]
+pub fn builtin_object_fields(
+	o: ObjValue,
+	#[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,
+) -> Vec<Val> {
+	builtin_object_fields_ex(
+		o,
+		false,
+		#[cfg(feature = "exp-preserve-order")]
+		preserve_order,
+	)
 }
 
+#[builtin]
+pub fn builtin_object_fields_all(
+	o: ObjValue,
+	#[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,
+) -> Vec<Val> {
+	builtin_object_fields_ex(
+		o,
+		true,
+		#[cfg(feature = "exp-preserve-order")]
+		preserve_order,
+	)
+}
+
 pub fn builtin_object_values_ex(
 	o: ObjValue,
 	include_hidden: bool,
@@ -105,6 +128,16 @@
 }
 
 #[builtin]
+pub fn builtin_object_has(o: ObjValue, f: IStr) -> bool {
+	o.has_field(f)
+}
+
+#[builtin]
+pub fn builtin_object_has_all(o: ObjValue, f: IStr) -> bool {
+	o.has_field_include_hidden(f)
+}
+
+#[builtin]
 pub fn builtin_object_remove_key(
 	obj: ObjValue,
 	key: IStr,
modifiedcrates/jrsonnet-stdlib/src/sets.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/sets.rs
+++ b/crates/jrsonnet-stdlib/src/sets.rs
@@ -70,6 +70,7 @@
 	}
 	Ok(ArrValue::lazy(out))
 }
+
 #[builtin]
 #[allow(non_snake_case, clippy::redundant_closure)]
 pub fn builtin_set_diff(a: ArrValue, b: ArrValue, keyF: Option<FuncVal>) -> Result<ArrValue> {
@@ -115,3 +116,57 @@
 	}
 	Ok(ArrValue::lazy(out))
 }
+
+#[builtin]
+#[allow(non_snake_case, clippy::redundant_closure)]
+pub fn builtin_set_union(a: ArrValue, b: ArrValue, keyF: Option<FuncVal>) -> Result<ArrValue> {
+	let mut a = a.iter_lazy();
+	let mut b = b.iter_lazy();
+
+	let keyF = keyF
+		.unwrap_or(FuncVal::identity())
+		.into_native::<((Thunk<Val>,), Val)>();
+	let keyF = |v| keyF(v);
+
+	let mut av = a.next();
+	let mut bv = b.next();
+	let mut ak = av.clone().map(keyF).transpose()?;
+	let mut bk = bv.clone().map(keyF).transpose()?;
+
+	let mut out = Vec::new();
+	while let (Some(ac), Some(bc)) = (&ak, &bk) {
+		match evaluate_compare_op(ac, bc, BinaryOpType::Lt)? {
+			Ordering::Less => {
+				out.push(av.clone().expect("ak != None"));
+				av = a.next();
+				ak = av.clone().map(keyF).transpose()?;
+			}
+			Ordering::Greater => {
+				out.push(bv.clone().expect("bk != None"));
+				bv = b.next();
+				bk = bv.clone().map(keyF).transpose()?;
+			}
+			Ordering::Equal => {
+				// NOTE: order matters, values in `a` win
+				out.push(av.clone().expect("ak != None"));
+				av = a.next();
+				ak = av.clone().map(keyF).transpose()?;
+				bv = b.next();
+				bk = bv.clone().map(keyF).transpose()?;
+			}
+		};
+	}
+	// a.len() > b.len()
+	while let Some(_ac) = &ak {
+		out.push(av.clone().expect("ak != None"));
+		av = a.next();
+		ak = av.clone().map(keyF).transpose()?;
+	}
+	// b.len() > a.len()
+	while let Some(_bc) = &bk {
+		out.push(bv.clone().expect("ak != None"));
+		bv = b.next();
+		bk = bv.clone().map(keyF).transpose()?;
+	}
+	Ok(ArrValue::lazy(out))
+}
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -4,8 +4,6 @@
 
   thisFile:: error 'std.thisFile is deprecated, to enable its support in jrsonnet - recompile it with "legacy-this-file" support.\nThis will slow down stdlib caching a bit, though',
 
-  toString(a):: '' + a,
-
   lstripChars(str, chars)::
     if std.length(str) > 0 && std.member(chars, str[0]) then
       std.lstripChars(str[1:], chars)
@@ -21,9 +19,6 @@
 
   stripChars(str, chars)::
     std.lstripChars(std.rstripChars(str, chars), chars),
-
-  stringChars(str)::
-    std.makeArray(std.length(str), function(i) str[i]),
 
   splitLimitR(str, c, maxsplits)::
     if maxsplits == -1 then
@@ -60,16 +55,6 @@
       std.join('', [std.deepJoin(x) for x in arr])
     else
       error 'Expected string or array, got %s' % std.type(arr),
-
-  filterMap(filter_func, map_func, arr)::
-    if !std.isFunction(filter_func) then
-      error ('std.filterMap first param must be function, got ' + std.type(filter_func))
-    else if !std.isFunction(map_func) then
-      error ('std.filterMap second param must be function, got ' + std.type(map_func))
-    else if !std.isArray(arr) then
-      error ('std.filterMap third param must be array, got ' + std.type(arr))
-    else
-      std.map(map_func, std.filter(filter_func, arr)),
 
   assertEqual(a, b)::
     if a == b then
@@ -81,9 +66,6 @@
     if x < minVal then minVal
     else if x > maxVal then maxVal
     else x,
-
-  flattenArrays(arrs)::
-    std.foldl(function(a, b) a + b, arrs, []),
 
   manifestIni(ini)::
     local body_lines(body) =
@@ -195,24 +177,6 @@
           std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);
 
       aux(value),
-
-  setUnion(a, b, keyF=id)::
-    // NOTE: order matters, values in `a` win
-    local aux(a, b, i, j, acc) =
-      if i >= std.length(a) then
-        acc + b[j:]
-      else if j >= std.length(b) then
-        acc + a[i:]
-      else
-        local ak = keyF(a[i]);
-        local bk = keyF(b[j]);
-        if ak == bk then
-          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict
-        else if ak < bk then
-          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict
-        else
-          aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;
-    aux(a, b, 0, 0, []),
 
   mergePatch(target, patch)::
     if std.isObject(patch) then
@@ -240,18 +204,6 @@
 
   get(o, f, default=null, inc_hidden=true)::
     if std.objectHasEx(o, f, inc_hidden) then o[f] else default,
-
-  objectFields(o)::
-    std.objectFieldsEx(o, false),
-
-  objectFieldsAll(o)::
-    std.objectFieldsEx(o, true),
-
-  objectHas(o, f)::
-    std.objectHasEx(o, f, false),
-
-  objectHasAll(o, f)::
-    std.objectHasEx(o, f, true),
 
   resolvePath(f, r)::
     local arr = std.split(f, '/');
modifiedcrates/jrsonnet-stdlib/src/strings.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/strings.rs
+++ b/crates/jrsonnet-stdlib/src/strings.rs
@@ -198,3 +198,8 @@
 		assert_eq!(parse_nat::<16>("BbC").unwrap(), 0xBBC as f64);
 	}
 }
+
+#[builtin]
+pub fn builtin_string_chars(str: IStr) -> ArrValue {
+	ArrValue::chars(str.chars())
+}