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

difftreelog

perf move C++ compat to native

Yaroslav Bolyukin2024-05-19parent: #cb937ae.patch.diff
in: master

3 files changed

modifiedcrates/jrsonnet-stdlib/src/compat.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/compat.rs
+++ b/crates/jrsonnet-stdlib/src/compat.rs
@@ -1,6 +1,8 @@
 use std::cmp::Ordering;
 
-use jrsonnet_evaluator::{function::builtin, operator::evaluate_compare_op, Result, Val};
+use jrsonnet_evaluator::{
+	function::builtin, operator::evaluate_compare_op, val::ArrValue, Result, Val,
+};
 
 #[builtin]
 #[allow(non_snake_case)]
@@ -13,3 +15,34 @@
 		},
 	)
 }
+
+#[builtin]
+#[allow(non_snake_case)]
+pub fn builtin___compare_array(arr1: ArrValue, arr2: ArrValue) -> Result<i32> {
+	builtin___compare(Val::Arr(arr1), Val::Arr(arr2))
+}
+
+macro_rules! arr_comp {
+	($name:ident, $operator:expr) => {
+		#[builtin]
+		#[allow(non_snake_case)]
+		pub fn $name(arr1: ArrValue, arr2: ArrValue) -> Result<bool> {
+			let ordering = evaluate_compare_op(
+				&Val::Arr(arr1),
+				&Val::Arr(arr2),
+				jrsonnet_parser::BinaryOpType::Lt,
+			)?;
+			Ok($operator.contains(&ordering))
+		}
+	};
+}
+arr_comp!(builtin___array_less, [Ordering::Less]);
+arr_comp!(builtin___array_greater, [Ordering::Greater]);
+arr_comp!(
+	builtin___array_less_or_equal,
+	[Ordering::Less, Ordering::Equal]
+);
+arr_comp!(
+	builtin___array_greater_or_equal,
+	[Ordering::Greater, Ordering::Equal]
+);
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -219,6 +219,14 @@
 		("regexQuoteMeta", builtin_regex_quote_meta::INST),
 		// Compat
 		("__compare", builtin___compare::INST),
+		("__compare_array", builtin___compare_array::INST),
+		("__array_less", builtin___array_less::INST),
+		("__array_greater", builtin___array_greater::INST),
+		("__array_less_or_equal", builtin___array_less_or_equal::INST),
+		(
+			"__array_greater_or_equal",
+			builtin___array_greater_or_equal::INST,
+		),
 	]
 	.iter()
 	.copied()
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
before · crates/jrsonnet-stdlib/src/std.jsonnet
1{2  local std = self,34  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',56  mapWithIndex(func, arr)::7    if !std.isFunction(func) then8      error ('std.mapWithIndex first param must be function, got ' + std.type(func))9    else if !std.isArray(arr) && !std.isString(arr) then10      error ('std.mapWithIndex second param must be array, got ' + std.type(arr))11    else12      std.makeArray(std.length(arr), function(i) func(i, arr[i])),1314  mapWithKey(func, obj)::15    if !std.isFunction(func) then16      error ('std.mapWithKey first param must be function, got ' + std.type(func))17    else if !std.isObject(obj) then18      error ('std.mapWithKey second param must be object, got ' + std.type(obj))19    else20      { [k]: func(k, obj[k]) for k in std.objectFields(obj) },2122  lines(arr)::23    std.join('\n', arr + ['']),2425  deepJoin(arr)::26    if std.isString(arr) then27      arr28    else if std.isArray(arr) then29      std.join('', [std.deepJoin(x) for x in arr])30    else31      error 'Expected string or array, got %s' % std.type(arr),3233  assertEqual(a, b)::34    if a == b then35      true36    else37      error 'Assertion failed. ' + a + ' != ' + b,3839  manifestIni(ini)::40    local body_lines(body) =41      std.join([], [42        local value_or_values = body[k];43        if std.isArray(value_or_values) then44          ['%s = %s' % [k, value] for value in value_or_values]45        else46          ['%s = %s' % [k, value_or_values]]4748        for k in std.objectFields(body)49      ]);5051    local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),52          main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],53          all_sections = [54      section_lines(k, ini.sections[k])55      for k in std.objectFields(ini.sections)56    ];57    std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),5859  mergePatch(target, patch)::60    if std.isObject(patch) then61      local target_object =62        if std.isObject(target) then target else {};6364      local target_fields =65        if std.isObject(target_object) then std.objectFields(target_object) else [];6667      local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];68      local both_fields = std.setUnion(target_fields, std.objectFields(patch));6970      {71        [k]:72          if !std.objectHas(patch, k) then73            target_object[k]74          else if !std.objectHas(target_object, k) then75            std.mergePatch(null, patch[k]) tailstrict76          else77            std.mergePatch(target_object[k], patch[k]) tailstrict78        for k in std.setDiff(both_fields, null_fields)79      }80    else81      patch,8283  resolvePath(f, r)::84    local arr = std.split(f, '/');85    std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),8687  find(value, arr)::88    if !std.isArray(arr) then89      error 'find second parameter should be an array, got ' + std.type(arr)90    else91      std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),9293  // Compat94  __compare_array(arr1, arr2)::95    assert std.isArray(arr1) && std.isArray(arr2);96    std.__compare(arr1, arr2),97  __array_less(arr1, arr2):: std.__compare_array(arr1, arr2) == -1,98  __array_greater(arr1, arr2):: std.__compare_array(arr1, arr2) == 1,99  __array_less_or_equal(arr1, arr2):: std.__compare_array(arr1, arr2) <= 0,100  __array_greater_or_equal(arr1, arr2):: std.__compare_array(arr1, arr2) >= 0,101}
after · crates/jrsonnet-stdlib/src/std.jsonnet
1{2  local std = self,34  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',56  mapWithIndex(func, arr)::7    if !std.isFunction(func) then8      error ('std.mapWithIndex first param must be function, got ' + std.type(func))9    else if !std.isArray(arr) && !std.isString(arr) then10      error ('std.mapWithIndex second param must be array, got ' + std.type(arr))11    else12      std.makeArray(std.length(arr), function(i) func(i, arr[i])),1314  mapWithKey(func, obj)::15    if !std.isFunction(func) then16      error ('std.mapWithKey first param must be function, got ' + std.type(func))17    else if !std.isObject(obj) then18      error ('std.mapWithKey second param must be object, got ' + std.type(obj))19    else20      { [k]: func(k, obj[k]) for k in std.objectFields(obj) },2122  lines(arr)::23    std.join('\n', arr + ['']),2425  deepJoin(arr)::26    if std.isString(arr) then27      arr28    else if std.isArray(arr) then29      std.join('', [std.deepJoin(x) for x in arr])30    else31      error 'Expected string or array, got %s' % std.type(arr),3233  assertEqual(a, b)::34    if a == b then35      true36    else37      error 'Assertion failed. ' + a + ' != ' + b,3839  manifestIni(ini)::40    local body_lines(body) =41      std.join([], [42        local value_or_values = body[k];43        if std.isArray(value_or_values) then44          ['%s = %s' % [k, value] for value in value_or_values]45        else46          ['%s = %s' % [k, value_or_values]]4748        for k in std.objectFields(body)49      ]);5051    local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),52          main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],53          all_sections = [54      section_lines(k, ini.sections[k])55      for k in std.objectFields(ini.sections)56    ];57    std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),5859  mergePatch(target, patch)::60    if std.isObject(patch) then61      local target_object =62        if std.isObject(target) then target else {};6364      local target_fields =65        if std.isObject(target_object) then std.objectFields(target_object) else [];6667      local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];68      local both_fields = std.setUnion(target_fields, std.objectFields(patch));6970      {71        [k]:72          if !std.objectHas(patch, k) then73            target_object[k]74          else if !std.objectHas(target_object, k) then75            std.mergePatch(null, patch[k]) tailstrict76          else77            std.mergePatch(target_object[k], patch[k]) tailstrict78        for k in std.setDiff(both_fields, null_fields)79      }80    else81      patch,8283  resolvePath(f, r)::84    local arr = std.split(f, '/');85    std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),8687  find(value, arr)::88    if !std.isArray(arr) then89      error 'find second parameter should be an array, got ' + std.type(arr)90    else91      std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),92}