difftreelog
perf move more stdlib functions to native
in: master
7 files changed
crates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -103,6 +103,15 @@
}
#[builtin]
+pub fn builtin_filter_map(
+ filter_func: FuncVal,
+ map_func: FuncVal,
+ arr: ArrValue,
+) -> Result<ArrValue> {
+ Ok(builtin_filter(filter_func, arr)?.map(map_func))
+}
+
+#[builtin]
pub fn builtin_foldl(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {
let mut acc = init;
for i in arr.iter() {
@@ -274,3 +283,22 @@
}
Ok(arr)
}
+
+#[builtin]
+pub fn builtin_flatten_arrays(arrs: Vec<ArrValue>) -> ArrValue {
+ pub fn flatten_inner(values: &[ArrValue]) -> ArrValue {
+ if values.len() == 1 {
+ return values[0].clone();
+ } else if values.len() == 2 {
+ return ArrValue::extended(values[0].clone(), values[1].clone());
+ }
+ let (a, b) = values.split_at(values.len() / 2);
+ ArrValue::extended(flatten_inner(a), flatten_inner(b))
+ }
+ if arrs.is_empty() {
+ return ArrValue::empty();
+ } else if arrs.len() == 1 {
+ return arrs.into_iter().next().expect("single");
+ }
+ flatten_inner(&arrs)
+}
crates/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
crates/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()
+}
crates/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,
crates/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))
+}
crates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth445 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',5 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',667 toString(a):: '' + a,89 lstripChars(str, chars)::7 lstripChars(str, chars)::10 if std.length(str) > 0 && std.member(chars, str[0]) then8 if std.length(str) > 0 && std.member(chars, str[0]) then11 std.lstripChars(str[1:], chars)9 std.lstripChars(str[1:], chars)22 stripChars(str, chars)::20 stripChars(str, chars)::23 std.lstripChars(std.rstripChars(str, chars), chars),21 std.lstripChars(std.rstripChars(str, chars), chars),242225 stringChars(str)::26 std.makeArray(std.length(str), function(i) str[i]),2728 splitLimitR(str, c, maxsplits)::23 splitLimitR(str, c, maxsplits)::29 if maxsplits == -1 then24 if maxsplits == -1 then30 std.splitLimit(str, c, -1)25 std.splitLimit(str, c, -1)61 else56 else62 error 'Expected string or array, got %s' % std.type(arr),57 error 'Expected string or array, got %s' % std.type(arr),635864 filterMap(filter_func, map_func, arr)::65 if !std.isFunction(filter_func) then66 error ('std.filterMap first param must be function, got ' + std.type(filter_func))67 else if !std.isFunction(map_func) then68 error ('std.filterMap second param must be function, got ' + std.type(map_func))69 else if !std.isArray(arr) then70 error ('std.filterMap third param must be array, got ' + std.type(arr))71 else72 std.map(map_func, std.filter(filter_func, arr)),7374 assertEqual(a, b)::59 assertEqual(a, b)::75 if a == b then60 if a == b then76 true61 true82 else if x > maxVal then maxVal67 else if x > maxVal then maxVal83 else x,68 else x,846985 flattenArrays(arrs)::86 std.foldl(function(a, b) a + b, arrs, []),8788 manifestIni(ini)::70 manifestIni(ini)::89 local body_lines(body) =71 local body_lines(body) =90 std.join([], [72 std.join([], [196178197 aux(value),179 aux(value),198180199 setUnion(a, b, keyF=id)::200 // NOTE: order matters, values in `a` win201 local aux(a, b, i, j, acc) =202 if i >= std.length(a) then203 acc + b[j:]204 else if j >= std.length(b) then205 acc + a[i:]206 else207 local ak = keyF(a[i]);208 local bk = keyF(b[j]);209 if ak == bk then210 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict211 else if ak < bk then212 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict213 else214 aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;215 aux(a, b, 0, 0, []),216217 mergePatch(target, patch)::181 mergePatch(target, patch)::218 if std.isObject(patch) then182 if std.isObject(patch) then219 local target_object =183 local target_object =240204241 get(o, f, default=null, inc_hidden=true)::205 get(o, f, default=null, inc_hidden=true)::242 if std.objectHasEx(o, f, inc_hidden) then o[f] else default,206 if std.objectHasEx(o, f, inc_hidden) then o[f] else default,243244 objectFields(o)::245 std.objectFieldsEx(o, false),246247 objectFieldsAll(o)::248 std.objectFieldsEx(o, true),249250 objectHas(o, f)::251 std.objectHasEx(o, f, false),252253 objectHasAll(o, f)::254 std.objectHasEx(o, f, true),255207256 resolvePath(f, r)::208 resolvePath(f, r)::257 local arr = std.split(f, '/');209 local arr = std.split(f, '/');crates/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())
+}