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
102 arr.filter(|val| bool::from_untyped(func.evaluate_simple(&(val.clone(),), false)?))102 arr.filter(|val| bool::from_untyped(func.evaluate_simple(&(val.clone(),), false)?))
103}103}
104
105#[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}
104113
105#[builtin]114#[builtin]
106pub fn builtin_foldl(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {115pub fn builtin_foldl(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {
275 Ok(arr)284 Ok(arr)
276}285}
286
287#[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}
277305
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
84 ("avg", builtin_avg::INST),84 ("avg", builtin_avg::INST),
85 ("removeAt", builtin_remove_at::INST),85 ("removeAt", builtin_remove_at::INST),
86 ("remove", builtin_remove::INST),86 ("remove", builtin_remove::INST),
87 ("flattenArrays", builtin_flatten_arrays::INST),
88 ("filterMap", builtin_filter_map::INST),
87 // Math89 // Math
88 ("abs", builtin_abs::INST),90 ("abs", builtin_abs::INST),
89 ("sign", builtin_sign::INST),91 ("sign", builtin_sign::INST),
137 ("base64DecodeBytes", builtin_base64_decode_bytes::INST),139 ("base64DecodeBytes", builtin_base64_decode_bytes::INST),
138 // Objects140 // Objects
139 ("objectFieldsEx", builtin_object_fields_ex::INST),141 ("objectFieldsEx", builtin_object_fields_ex::INST),
142 ("objectFields", builtin_object_fields::INST),
143 ("objectFieldsAll", builtin_object_fields_all::INST),
140 ("objectValues", builtin_object_values::INST),144 ("objectValues", builtin_object_values::INST),
141 ("objectValuesAll", builtin_object_values_all::INST),145 ("objectValuesAll", builtin_object_values_all::INST),
142 ("objectKeysValues", builtin_object_keys_values::INST),146 ("objectKeysValues", builtin_object_keys_values::INST),
143 ("objectKeysValuesAll", builtin_object_keys_values_all::INST),147 ("objectKeysValuesAll", builtin_object_keys_values_all::INST),
144 ("objectHasEx", builtin_object_has_ex::INST),148 ("objectHasEx", builtin_object_has_ex::INST),
149 ("objectHas", builtin_object_has::INST),
150 ("objectHasAll", builtin_object_has_all::INST),
145 ("objectRemoveKey", builtin_object_remove_key::INST),151 ("objectRemoveKey", builtin_object_remove_key::INST),
146 // Manifest152 // Manifest
147 ("escapeStringJson", builtin_escape_string_json::INST),153 ("escapeStringJson", builtin_escape_string_json::INST),
148 ("manifestJsonEx", builtin_manifest_json_ex::INST),154 ("manifestJsonEx", builtin_manifest_json_ex::INST),
149 ("manifestYamlDoc", builtin_manifest_yaml_doc::INST),155 ("manifestYamlDoc", builtin_manifest_yaml_doc::INST),
150 ("manifestTomlEx", builtin_manifest_toml_ex::INST),156 ("manifestTomlEx", builtin_manifest_toml_ex::INST),
157 ("toString", builtin_to_string::INST),
151 // Parsing158 // Parsing
152 ("parseJson", builtin_parse_json::INST),159 ("parseJson", builtin_parse_json::INST),
153 ("parseYaml", builtin_parse_yaml::INST),160 ("parseYaml", builtin_parse_yaml::INST),
167 ("bigint", builtin_bigint::INST),174 ("bigint", builtin_bigint::INST),
168 ("parseOctal", builtin_parse_octal::INST),175 ("parseOctal", builtin_parse_octal::INST),
169 ("parseHex", builtin_parse_hex::INST),176 ("parseHex", builtin_parse_hex::INST),
177 ("stringChars", builtin_string_chars::INST),
170 // Misc178 // Misc
171 ("length", builtin_length::INST),179 ("length", builtin_length::INST),
172 ("startsWith", builtin_starts_with::INST),180 ("startsWith", builtin_starts_with::INST),
175 ("setMember", builtin_set_member::INST),183 ("setMember", builtin_set_member::INST),
176 ("setInter", builtin_set_inter::INST),184 ("setInter", builtin_set_inter::INST),
177 ("setDiff", builtin_set_diff::INST),185 ("setDiff", builtin_set_diff::INST),
186 ("setUnion", builtin_set_union::INST),
178 // Compat187 // Compat
179 ("__compare", builtin___compare::INST),188 ("__compare", builtin___compare::INST),
180 ]189 ]
347356
348 let mut std = ObjValueBuilder::new();357 let mut std = ObjValueBuilder::new();
349 std.with_super(self.stdlib_obj.clone());358 std.with_super(self.stdlib_obj.clone());
350 std.field("thisFile".into())359 std.field("thisFile")
351 .hide()360 .hide()
352 .value(Val::string(match source.source_path().path() {361 .value(match source.source_path().path() {
353 Some(p) => self.settings().path_resolver.resolve(p).into(),362 Some(p) => self.settings().path_resolver.resolve(p),
354 None => source.source_path().to_string().into(),363 None => source.source_path().to_string(),
355 }))364 });
356 .expect("this object builder is empty");
357 let stdlib_with_this_file = std.build();365 let stdlib_with_this_file = std.build();
358366
359 builder.bind(367 builder.bind("std", Thunk::evaluated(Val::Obj(stdlib_with_this_file)));
360 "std".into(),
361 Thunk::evaluated(Val::Obj(stdlib_with_this_file)),
362 );
363 }368 }
364 fn as_any(&self) -> &dyn std::any::Any {369 fn as_any(&self) -> &dyn std::any::Any {
modifiedcrates/jrsonnet-stdlib/src/manifest/mod.rsdiffbeforeafterboth
61 ))61 ))
62}62}
63
64#[builtin]
65pub fn builtin_to_string(a: Val) -> Result<IStr> {
66 a.to_string()
67}
6368
modifiedcrates/jrsonnet-stdlib/src/objects.rsdiffbeforeafterboth
1use jrsonnet_evaluator::{1use jrsonnet_evaluator::{
2 function::builtin,2 function::builtin,
3 val::{ArrValue, StrValue, Val},3 val::{ArrValue, Val},
4 IStr, ObjValue, ObjValueBuilder,4 IStr, ObjValue, ObjValueBuilder,
5};5};
66
17 #[cfg(feature = "exp-preserve-order")]17 #[cfg(feature = "exp-preserve-order")]
18 preserve_order,18 preserve_order,
19 );19 );
20 out.into_iter()20 out.into_iter().map(Val::string).collect::<Vec<_>>()
21 .map(StrValue::Flat)
22 .map(Val::Str)
23 .collect::<Vec<_>>()
24}21}
22
23#[builtin]
24pub fn builtin_object_fields(
25 o: ObjValue,
26 #[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,
27) -> Vec<Val> {
28 builtin_object_fields_ex(
29 o,
30 false,
31 #[cfg(feature = "exp-preserve-order")]
32 preserve_order,
33 )
34}
35
36#[builtin]
37pub fn builtin_object_fields_all(
38 o: ObjValue,
39 #[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,
40) -> Vec<Val> {
41 builtin_object_fields_ex(
42 o,
43 true,
44 #[cfg(feature = "exp-preserve-order")]
45 preserve_order,
46 )
47}
2548
26pub fn builtin_object_values_ex(49pub fn builtin_object_values_ex(
27 o: ObjValue,50 o: ObjValue,
104 obj.has_field_ex(fname, hidden)127 obj.has_field_ex(fname, hidden)
105}128}
129
130#[builtin]
131pub fn builtin_object_has(o: ObjValue, f: IStr) -> bool {
132 o.has_field(f)
133}
134
135#[builtin]
136pub fn builtin_object_has_all(o: ObjValue, f: IStr) -> bool {
137 o.has_field_include_hidden(f)
138}
106139
107#[builtin]140#[builtin]
108pub fn builtin_object_remove_key(141pub fn builtin_object_remove_key(
modifiedcrates/jrsonnet-stdlib/src/sets.rsdiffbeforeafterboth
116 Ok(ArrValue::lazy(out))117 Ok(ArrValue::lazy(out))
117}118}
119
120#[builtin]
121#[allow(non_snake_case, clippy::redundant_closure)]
122pub fn builtin_set_union(a: ArrValue, b: ArrValue, keyF: Option<FuncVal>) -> Result<ArrValue> {
123 let mut a = a.iter_lazy();
124 let mut b = b.iter_lazy();
125
126 let keyF = keyF
127 .unwrap_or(FuncVal::identity())
128 .into_native::<((Thunk<Val>,), Val)>();
129 let keyF = |v| keyF(v);
130
131 let mut av = a.next();
132 let mut bv = b.next();
133 let mut ak = av.clone().map(keyF).transpose()?;
134 let mut bk = bv.clone().map(keyF).transpose()?;
135
136 let mut out = Vec::new();
137 while let (Some(ac), Some(bc)) = (&ak, &bk) {
138 match evaluate_compare_op(ac, bc, BinaryOpType::Lt)? {
139 Ordering::Less => {
140 out.push(av.clone().expect("ak != None"));
141 av = a.next();
142 ak = av.clone().map(keyF).transpose()?;
143 }
144 Ordering::Greater => {
145 out.push(bv.clone().expect("bk != None"));
146 bv = b.next();
147 bk = bv.clone().map(keyF).transpose()?;
148 }
149 Ordering::Equal => {
150 // NOTE: order matters, values in `a` win
151 out.push(av.clone().expect("ak != None"));
152 av = a.next();
153 ak = av.clone().map(keyF).transpose()?;
154 bv = b.next();
155 bk = bv.clone().map(keyF).transpose()?;
156 }
157 };
158 }
159 // a.len() > b.len()
160 while let Some(_ac) = &ak {
161 out.push(av.clone().expect("ak != None"));
162 av = a.next();
163 ak = av.clone().map(keyF).transpose()?;
164 }
165 // b.len() > a.len()
166 while let Some(_bc) = &bk {
167 out.push(bv.clone().expect("ak != None"));
168 bv = b.next();
169 bk = bv.clone().map(keyF).transpose()?;
170 }
171 Ok(ArrValue::lazy(out))
172}
118173
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
44
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',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',
66
7 toString(a):: '' + a,
8
9 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]) then
11 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),
2422
25 stringChars(str)::
26 std.makeArray(std.length(str), function(i) str[i]),
27
28 splitLimitR(str, c, maxsplits)::23 splitLimitR(str, c, maxsplits)::
29 if maxsplits == -1 then24 if maxsplits == -1 then
30 std.splitLimit(str, c, -1)25 std.splitLimit(str, c, -1)
61 else56 else
62 error 'Expected string or array, got %s' % std.type(arr),57 error 'Expected string or array, got %s' % std.type(arr),
6358
64 filterMap(filter_func, map_func, arr)::
65 if !std.isFunction(filter_func) then
66 error ('std.filterMap first param must be function, got ' + std.type(filter_func))
67 else if !std.isFunction(map_func) then
68 error ('std.filterMap second param must be function, got ' + std.type(map_func))
69 else if !std.isArray(arr) then
70 error ('std.filterMap third param must be array, got ' + std.type(arr))
71 else
72 std.map(map_func, std.filter(filter_func, arr)),
73
74 assertEqual(a, b)::59 assertEqual(a, b)::
75 if a == b then60 if a == b then
76 true61 true
82 else if x > maxVal then maxVal67 else if x > maxVal then maxVal
83 else x,68 else x,
8469
85 flattenArrays(arrs)::
86 std.foldl(function(a, b) a + b, arrs, []),
87
88 manifestIni(ini)::70 manifestIni(ini)::
89 local body_lines(body) =71 local body_lines(body) =
90 std.join([], [72 std.join([], [
196178
197 aux(value),179 aux(value),
198180
199 setUnion(a, b, keyF=id)::
200 // NOTE: order matters, values in `a` win
201 local aux(a, b, i, j, acc) =
202 if i >= std.length(a) then
203 acc + b[j:]
204 else if j >= std.length(b) then
205 acc + a[i:]
206 else
207 local ak = keyF(a[i]);
208 local bk = keyF(b[j]);
209 if ak == bk then
210 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict
211 else if ak < bk then
212 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict
213 else
214 aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;
215 aux(a, b, 0, 0, []),
216
217 mergePatch(target, patch)::181 mergePatch(target, patch)::
218 if std.isObject(patch) then182 if std.isObject(patch) then
219 local target_object =183 local target_object =
240204
241 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,
243
244 objectFields(o)::
245 std.objectFieldsEx(o, false),
246
247 objectFieldsAll(o)::
248 std.objectFieldsEx(o, true),
249
250 objectHas(o, f)::
251 std.objectHasEx(o, f, false),
252
253 objectHasAll(o, f)::
254 std.objectHasEx(o, f, true),
255207
256 resolvePath(f, r)::208 resolvePath(f, r)::
257 local arr = std.split(f, '/');209 local arr = std.split(f, '/');
modifiedcrates/jrsonnet-stdlib/src/strings.rsdiffbeforeafterboth
199 }199 }
200}200}
201
202#[builtin]
203pub fn builtin_string_chars(str: IStr) -> ArrValue {
204 ArrValue::chars(str.chars())
205}
201206