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.rsdiffbeforeafterboth1//! Some jsonnet operations are desugared to stdlib functions...2//! However, in our case we instead implement them in native, and implement native functions on top of core for backwards compatibility34use jrsonnet_evaluator::{5 IStr, NumValue, Result, Val,6 function::builtin,7 operator::evaluate_mod_op,8 stdlib::std_format,9 typed::{Either, Either2},10 val::{equals, primitive_equals},11};1213#[builtin]14pub fn builtin_mod(a: Either![NumValue, IStr], b: Val) -> Result<Val> {15 use Either2::*;16 evaluate_mod_op(17 &match a {18 A(v) => Val::Num(v),19 B(s) => Val::string(s),20 },21 &b,22 )23}2425#[builtin]26pub fn builtin_primitive_equals(x: Val, y: Val) -> Result<bool> {27 primitive_equals(&x, &y)28}2930#[builtin]31pub fn builtin_equals(a: Val, b: Val) -> Result<bool> {32 equals(&a, &b)33}3435#[builtin]36pub fn builtin_xor(x: bool, y: bool) -> bool {37 x ^ y38}3940#[builtin]41pub fn builtin_xnor(x: bool, y: bool) -> bool {42 x == y43}4445#[builtin]46pub fn builtin_format(str: IStr, vals: Val) -> Result<String> {47 std_format(&str, vals)48}1//! Some jsonnet operations are desugared to stdlib functions...2//! However, in our case we instead implement them in native, and implement native functions on top of core for backwards compatibility34use jrsonnet_evaluator::{5 function::builtin,6 stdlib::std_format,7 typed::{Either, Either2},8 val::{equals, primitive_equals},9 IStr, NumValue, Result, Val,10};1112#[builtin]13pub fn builtin_mod(a: Either![NumValue, IStr], b: Val) -> Result<Val> {14 use Either2::*;15 Val::try_mod(16 &match a {17 A(v) => Val::Num(v),18 B(s) => Val::string(s),19 },20 &b,21 )22}2324#[builtin]25pub fn builtin_primitive_equals(x: Val, y: Val) -> Result<bool> {26 primitive_equals(&x, &y)27}2829#[builtin]30pub fn builtin_equals(a: Val, b: Val) -> Result<bool> {31 equals(&a, &b)32}3334#[builtin]35pub fn builtin_xor(x: bool, y: bool) -> bool {36 x ^ y37}3839#[builtin]40pub fn builtin_xnor(x: bool, y: bool) -> bool {41 x == y42}4344#[builtin]45pub fn builtin_format(str: IStr, vals: Val) -> Result<String> {46 std_format(&str, vals)47}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.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/sort.rs
+++ b/crates/jrsonnet-stdlib/src/sort.rs
@@ -3,12 +3,11 @@
use std::cmp::Ordering;
use jrsonnet_evaluator::{
- Result, Thunk, Val, bail,
+ bail,
function::builtin,
- operator::evaluate_compare_op,
- val::{ArrValue, equals},
+ val::{equals, ArrValue},
+ Result, Thunk, Val,
};
-use jrsonnet_ir::BinaryOpType;
use crate::{eval_on_empty, keyf::KeyF};
@@ -53,7 +52,7 @@
let mut err = None;
// evaluate_compare_op will never return equal on types, which are different from
// jsonnet perspective
- values.sort_unstable_by(|a, b| match evaluate_compare_op(a, b, BinaryOpType::Lt) {
+ values.sort_unstable_by(|a, b| match Val::try_cmp(a, b) {
Ok(ord) => ord,
Err(e) if err.is_none() => {
let _ = err.insert(e);
@@ -71,7 +70,7 @@
fn sort_keyf(values: ArrValue, keyf: KeyF) -> Result<Vec<Thunk<Val>>> {
// Slow path, user provided key getter
- let mut vk = Vec::with_capacity(values.len());
+ let mut vk = Vec::with_capacity(values.len() as usize);
for value in values.iter_lazy() {
vk.push((value.clone(), keyf.eval(value)?));
}
@@ -89,16 +88,14 @@
let mut err = None;
// evaluate_compare_op will never return equal on types, which are different from
// jsonnet perspective
- vk.sort_by(
- |(_a, ak), (_b, bk)| match evaluate_compare_op(ak, bk, BinaryOpType::Lt) {
- Ok(ord) => ord,
- Err(e) if err.is_none() => {
- let _ = err.insert(e);
- Ordering::Equal
- }
- Err(_) => Ordering::Equal,
- },
- );
+ vk.sort_by(|(_a, ak), (_b, bk)| match Val::try_cmp(ak, bk) {
+ Ok(ord) => ord,
+ Err(e) if err.is_none() => {
+ let _ = err.insert(e);
+ Ordering::Equal
+ }
+ Err(_) => Ordering::Equal,
+ });
if let Some(err) = err {
return Err(err);
}
@@ -195,7 +192,7 @@
for item in iter {
let cur = item?;
let cur_key = keyf.eval(Thunk::evaluated(cur.clone()))?;
- if evaluate_compare_op(&cur_key, &min_key, BinaryOpType::Lt)? == ordering {
+ if Val::try_cmp(&cur_key, &min_key)? == ordering {
min = cur;
min_key = cur_key;
}
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))
}