difftreelog
refactor do not expose direct operator evaluator access
in: master
7 files changed
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -10,18 +10,14 @@
use jrsonnet_gcmodule::{Acyclic, Cc, Trace, cc_dyn};
use jrsonnet_interner::IStr;
+use jrsonnet_ir::BinaryOpType;
pub use jrsonnet_macros::Thunk;
use jrsonnet_types::ValType;
use rustc_hash::FxHashMap;
pub use crate::arr::{ArrValue, ArrayLike};
use crate::{
- NumValue, ObjValue, Result, SupThis, Unbound, WeakSupThis, bail,
- error::{Error, ErrorKind::*},
- function::FuncVal,
- gc::WithCapacityExt as _,
- manifest::{ManifestFormat, ToStringFormat},
- typed::BoundedUsize,
+ NumValue, ObjValue, Result, SupThis, Unbound, WeakSupThis, bail, error::{Error, ErrorKind::*}, evaluate::operator::{evaluate_compare_op, evaluate_mod_op}, function::FuncVal, gc::WithCapacityExt as _, manifest::{ManifestFormat, ToStringFormat}, typed::BoundedUsize
};
pub trait ThunkValue: Trace {
@@ -585,6 +581,13 @@
pub fn arr(a: impl ArrayLike) -> Self {
Self::Arr(ArrValue::new(a))
}
+
+ pub fn try_cmp(a: &Val, b: &Val) -> Result<Ordering> {
+ evaluate_compare_op(a, b, BinaryOpType::Lt)
+ }
+ pub fn try_mod(a: &Val, b: &Val) -> Result<Val> {
+ evaluate_mod_op(a, b)
+ }
}
impl From<IStr> for Val {
crates/jrsonnet-stdlib/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-stdlib/Cargo.toml
+++ b/crates/jrsonnet-stdlib/Cargo.toml
@@ -16,17 +16,13 @@
# Bigint type
exp-bigint = ["dep:num-bigint", "jrsonnet-evaluator/exp-bigint"]
-exp-null-coaelse = [
- "jrsonnet-ir/exp-null-coaelse",
- "jrsonnet-evaluator/exp-null-coaelse",
-]
+exp-null-coaelse = ["jrsonnet-evaluator/exp-null-coaelse"]
# std.regexMatch and other helpers
exp-regex = ["dep:regex", "dep:lru", "dep:rustc-hash"]
[dependencies]
jrsonnet-evaluator.workspace = true
jrsonnet-macros.workspace = true
-jrsonnet-ir.workspace = true
jrsonnet-gcmodule.workspace = true
# Used for std.parseJson/std.parseYaml
crates/jrsonnet-stdlib/src/compat.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/compat.rs
+++ b/crates/jrsonnet-stdlib/src/compat.rs
@@ -1,19 +1,15 @@
use std::cmp::Ordering;
-use jrsonnet_evaluator::{
- Result, Val, function::builtin, operator::evaluate_compare_op, val::ArrValue,
-};
+use jrsonnet_evaluator::{function::builtin, val::ArrValue, Result, Val};
#[builtin]
#[allow(non_snake_case)]
pub fn builtin___compare(v1: Val, v2: Val) -> Result<i32> {
- Ok(
- match evaluate_compare_op(&v1, &v2, jrsonnet_ir::BinaryOpType::Lt)? {
- Ordering::Less => -1,
- Ordering::Equal => 0,
- Ordering::Greater => 1,
- },
- )
+ Ok(match Val::try_cmp(&v1, &v2)? {
+ Ordering::Less => -1,
+ Ordering::Equal => 0,
+ Ordering::Greater => 1,
+ })
}
#[builtin]
@@ -27,11 +23,7 @@
#[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_ir::BinaryOpType::Lt,
- )?;
+ let ordering = Val::try_cmp(&Val::Arr(arr1), &Val::Arr(arr2))?;
Ok($operator.contains(&ordering))
}
};
crates/jrsonnet-stdlib/src/operator.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/operator.rs
+++ b/crates/jrsonnet-stdlib/src/operator.rs
@@ -2,18 +2,17 @@
//! However, in our case we instead implement them in native, and implement native functions on top of core for backwards compatibility
use jrsonnet_evaluator::{
- IStr, NumValue, Result, Val,
function::builtin,
- operator::evaluate_mod_op,
stdlib::std_format,
typed::{Either, Either2},
val::{equals, primitive_equals},
+ IStr, NumValue, Result, Val,
};
#[builtin]
pub fn builtin_mod(a: Either![NumValue, IStr], b: Val) -> Result<Val> {
use Either2::*;
- evaluate_mod_op(
+ Val::try_mod(
&match a {
A(v) => Val::Num(v),
B(s) => Val::string(s),
crates/jrsonnet-stdlib/src/sets.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/sets.rs
+++ b/crates/jrsonnet-stdlib/src/sets.rs
@@ -1,9 +1,6 @@
use std::cmp::Ordering;
-use jrsonnet_evaluator::{
- Result, Thunk, Val, function::builtin, operator::evaluate_compare_op, val::ArrValue,
-};
-use jrsonnet_ir::BinaryOpType;
+use jrsonnet_evaluator::{function::builtin, val::ArrValue, Result, Thunk, Val};
use crate::keyf::KeyF;
@@ -16,9 +13,9 @@
let x = keyF.eval(x)?;
while low < high {
- let middle = usize::midpoint(high, low);
+ let middle = u32::midpoint(high, low);
let comp = keyF.eval(arr.get_lazy(middle).expect("in bounds"))?;
- match evaluate_compare_op(&comp, &x, BinaryOpType::Lt)? {
+ match Val::try_cmp(&comp, &x)? {
Ordering::Less => low = middle + 1,
Ordering::Equal => return Ok(true),
Ordering::Greater => high = middle,
@@ -46,7 +43,7 @@
let mut out = Vec::new();
while let (Some(ac), Some(bc)) = (&ak, &bk) {
- match evaluate_compare_op(ac, bc, BinaryOpType::Lt)? {
+ match Val::try_cmp(ac, bc)? {
Ordering::Less => {
av = a.next();
ak = av.clone().map(keyF).transpose()?;
@@ -86,7 +83,7 @@
let mut out = Vec::new();
while let (Some(ac), Some(bc)) = (&ak, &bk) {
- match evaluate_compare_op(ac, bc, BinaryOpType::Lt)? {
+ match Val::try_cmp(ac, bc)? {
Ordering::Less => {
// In a, but not in b
out.push(av.clone().expect("ak != None"));
@@ -133,7 +130,7 @@
let mut out = Vec::new();
while let (Some(ac), Some(bc)) = (&ak, &bk) {
- match evaluate_compare_op(ac, bc, BinaryOpType::Lt)? {
+ match Val::try_cmp(ac, bc)? {
Ordering::Less => {
out.push(av.clone().expect("ak != None"));
av = a.next();
crates/jrsonnet-stdlib/src/sort.rsdiffbeforeafterboth1#![allow(non_snake_case)]23use std::cmp::Ordering;45use jrsonnet_evaluator::{6 bail,7 function::builtin,8 val::{equals, ArrValue},9 Result, Thunk, Val,10};1112use crate::{eval_on_empty, keyf::KeyF};1314#[derive(Copy, Clone)]15enum SortKeyType {16 Number,17 String,18 Unspecialized,19 Unknown,20}2122fn get_sort_type<T>(values: &[T], key_getter: impl Fn(&T) -> &Val) -> Result<SortKeyType> {23 let mut sort_type = SortKeyType::Unknown;24 for i in values {25 let i = key_getter(i);26 match (i, sort_type) {27 (Val::Str(_), SortKeyType::Unknown) => sort_type = SortKeyType::String,28 (Val::Num(_), SortKeyType::Unknown) => sort_type = SortKeyType::Number,29 (Val::Str(_), SortKeyType::String) | (Val::Num(_), SortKeyType::Number) => {}30 (Val::Str(_) | Val::Num(_), _) => {31 bail!("sort elements should have the same types")32 }33 (_, _) => return Ok(SortKeyType::Unspecialized),34 }35 }36 Ok(sort_type)37}3839fn sort_identity(mut values: Vec<Val>) -> Result<Vec<Val>> {40 // Fast path, identity key getter41 let sort_type = get_sort_type(&values, |k| k)?;42 match sort_type {43 SortKeyType::Number => values.sort_unstable_by_key(|v| match v {44 Val::Num(n) => *n,45 _ => unreachable!(),46 }),47 SortKeyType::String => values.sort_unstable_by_key(|v| match v {48 Val::Str(s) => s.clone(),49 _ => unreachable!(),50 }),51 SortKeyType::Unknown | SortKeyType::Unspecialized => {52 let mut err = None;53 // evaluate_compare_op will never return equal on types, which are different from54 // jsonnet perspective55 values.sort_unstable_by(|a, b| match Val::try_cmp(a, b) {56 Ok(ord) => ord,57 Err(e) if err.is_none() => {58 let _ = err.insert(e);59 Ordering::Equal60 }61 Err(_) => Ordering::Equal,62 });63 if let Some(err) = err {64 return Err(err);65 }66 }67 }68 Ok(values)69}7071fn sort_keyf(values: ArrValue, keyf: KeyF) -> Result<Vec<Thunk<Val>>> {72 // Slow path, user provided key getter73 let mut vk = Vec::with_capacity(values.len() as usize);74 for value in values.iter_lazy() {75 vk.push((value.clone(), keyf.eval(value)?));76 }77 let sort_type = get_sort_type(&vk, |v| &v.1)?;78 match sort_type {79 SortKeyType::Number => vk.sort_by_key(|v| match v.1 {80 Val::Num(n) => n,81 _ => unreachable!(),82 }),83 SortKeyType::String => vk.sort_by_key(|v| match &v.1 {84 Val::Str(s) => s.clone(),85 _ => unreachable!(),86 }),87 SortKeyType::Unknown | SortKeyType::Unspecialized => {88 let mut err = None;89 // evaluate_compare_op will never return equal on types, which are different from90 // jsonnet perspective91 vk.sort_by(|(_a, ak), (_b, bk)| match Val::try_cmp(ak, bk) {92 Ok(ord) => ord,93 Err(e) if err.is_none() => {94 let _ = err.insert(e);95 Ordering::Equal96 }97 Err(_) => Ordering::Equal,98 });99 if let Some(err) = err {100 return Err(err);101 }102 }103 }104 Ok(vk.into_iter().map(|v| v.0).collect())105}106107/// * `key_getter` - None, if identity sort required108pub fn sort(values: ArrValue, key_getter: KeyF) -> Result<ArrValue> {109 if values.len() <= 1 {110 return Ok(values);111 }112 if key_getter.is_identity() {113 Ok(ArrValue::new(sort_identity(114 values.iter().collect::<Result<Vec<Val>>>()?,115 )?))116 } else {117 Ok(ArrValue::new(sort_keyf(values, key_getter)?))118 }119}120121#[builtin]122pub fn builtin_sort(arr: ArrValue, #[default] keyF: KeyF) -> Result<ArrValue> {123 super::sort::sort(arr, keyF)124}125126fn uniq_identity(arr: Vec<Val>) -> Result<Vec<Val>> {127 let mut out = Vec::new();128 let mut last = arr[0].clone();129 out.push(last.clone());130 for next in arr.into_iter().skip(1) {131 if !equals(&last, &next)? {132 out.push(next.clone());133 }134 last = next;135 }136 Ok(out)137}138139fn uniq_keyf(arr: ArrValue, keyf: KeyF) -> Result<Vec<Thunk<Val>>> {140 let mut out = Vec::new();141 let last_value = arr.get_lazy(0).unwrap();142 let mut last_key = keyf.eval(last_value.clone())?;143 out.push(last_value);144145 for next in arr.iter_lazy().skip(1) {146 let next_key = keyf.eval(next.clone())?;147 if !equals(&last_key, &next_key)? {148 out.push(next.clone());149 }150 last_key = next_key;151 }152 Ok(out)153}154155#[builtin]156#[allow(non_snake_case)]157pub fn builtin_uniq(arr: ArrValue, #[default] keyF: KeyF) -> Result<ArrValue> {158 if arr.len() <= 1 {159 return Ok(arr);160 }161 if keyF.is_identity() {162 Ok(ArrValue::new(uniq_identity(163 arr.iter().collect::<Result<Vec<Val>>>()?,164 )?))165 } else {166 Ok(ArrValue::new(uniq_keyf(arr, keyF)?))167 }168}169170#[builtin]171#[allow(non_snake_case)]172pub fn builtin_set(arr: ArrValue, #[default] keyF: KeyF) -> Result<ArrValue> {173 if arr.len() <= 1 {174 return Ok(arr);175 }176 if keyF.is_identity() {177 let arr = arr.iter().collect::<Result<Vec<Val>>>()?;178 let arr = sort_identity(arr)?;179 let arr = uniq_identity(arr)?;180 Ok(ArrValue::new(arr))181 } else {182 let arr = sort_keyf(arr, keyF.clone())?;183 let arr = uniq_keyf(ArrValue::new(arr), keyF)?;184 Ok(ArrValue::new(arr))185 }186}187188fn array_top1(arr: ArrValue, keyf: KeyF, ordering: Ordering) -> Result<Val> {189 let mut iter = arr.iter();190 let mut min = iter.next().expect("not empty")?;191 let mut min_key = keyf.eval(Thunk::evaluated(min.clone()))?;192 for item in iter {193 let cur = item?;194 let cur_key = keyf.eval(Thunk::evaluated(cur.clone()))?;195 if Val::try_cmp(&cur_key, &min_key)? == ordering {196 min = cur;197 min_key = cur_key;198 }199 }200 Ok(min)201}202203#[builtin]204pub fn builtin_min_array(205 arr: ArrValue,206 #[default] keyF: KeyF,207 onEmpty: Option<Thunk<Val>>,208) -> Result<Val> {209 if arr.is_empty() {210 return eval_on_empty(onEmpty);211 }212 array_top1(arr, keyF, Ordering::Less)213}214#[builtin]215pub fn builtin_max_array(216 arr: ArrValue,217 #[default] keyF: KeyF,218 onEmpty: Option<Thunk<Val>>,219) -> Result<Val> {220 if arr.is_empty() {221 return eval_on_empty(onEmpty);222 }223 array_top1(arr, keyF, Ordering::Greater)224}crates/jrsonnet-stdlib/src/strings.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/strings.rs
+++ b/crates/jrsonnet-stdlib/src/strings.rs
@@ -26,7 +26,7 @@
#[builtin]
pub fn builtin_str_replace(str: String, from: IStr, to: IStr) -> Result<String> {
if from.is_empty() {
- bail!("'from' string must not be zero length");
+ bail!("`from` string must not be zero length");
}
Ok(str.replace(&from as &str, &to as &str))
}