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.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.rsdiffbeforeafterboth1use std::collections::BTreeSet;23use jrsonnet_evaluator::{4 Either, IStr, Val, bail,5 error::{ErrorKind::*, Result},6 function::builtin,7 typed::{Either2, FromUntyped, M1},8 val::{ArrValue, IndexableVal},9};1011#[builtin]12pub const fn builtin_codepoint(str: char) -> u32 {13 str as u3214}1516#[builtin]17pub fn builtin_substr(str: IStr, from: usize, len: usize) -> String {18 str.chars().skip(from).take(len).collect()19}2021#[builtin]22pub fn builtin_char(n: u32) -> Result<char> {23 Ok(std::char::from_u32(n).ok_or_else(|| InvalidUnicodeCodepointGot(n))?)24}2526#[builtin]27pub fn builtin_str_replace(str: String, from: IStr, to: IStr) -> Result<String> {28 if from.is_empty() {29 bail!("`from` string must not be zero length");30 }31 Ok(str.replace(&from as &str, &to as &str))32}3334#[builtin]35pub fn builtin_escape_string_bash(str_: String) -> String {36 const QUOTE: char = '\'';37 let mut out = str_.replace(QUOTE, "'\"'\"'");38 out.insert(0, QUOTE);39 out.push(QUOTE);40 out41}4243#[builtin]44pub fn builtin_escape_string_dollars(str_: String) -> String {45 str_.replace('$', "$$")46}4748#[builtin]49pub fn builtin_is_empty(str: String) -> bool {50 str.is_empty()51}5253#[builtin]54pub fn builtin_equals_ignore_case(str1: String, str2: String) -> bool {55 str1.eq_ignore_ascii_case(&str2)56}5758#[builtin]59pub fn builtin_splitlimit(str: IStr, c: IStr, maxsplits: Either![usize, M1]) -> ArrValue {60 use Either2::*;61 match maxsplits {62 A(n) => str.splitn(n + 1, &c as &str).map(Val::string).collect(),63 B(_) => str.split(&c as &str).map(Val::string).collect(),64 }65}6667#[builtin]68pub fn builtin_splitlimitr(str: IStr, c: IStr, maxsplits: Either![usize, M1]) -> ArrValue {69 use Either2::*;70 match maxsplits {71 A(n) =>72 // rsplitn does not implement DoubleEndedIterator so collect into73 // a temporary vec74 {75 str.rsplitn(n + 1, &c as &str)76 .map(Val::string)77 .collect::<Vec<_>>()78 .into_iter()79 .rev()80 .collect()81 }82 B(_) => str.split(&c as &str).map(Val::string).collect(),83 }84}8586#[builtin]87pub fn builtin_split(str: IStr, c: IStr) -> ArrValue {88 use Either2::*;89 builtin_splitlimit(str, c, B(M1))90}9192#[builtin]93pub fn builtin_ascii_upper(str: IStr) -> String {94 str.to_ascii_uppercase()95}9697#[builtin]98pub fn builtin_ascii_lower(str: IStr) -> String {99 str.to_ascii_lowercase()100}101102#[builtin]103pub fn builtin_find_substr(pat: IStr, str: IStr) -> ArrValue {104 if pat.is_empty() || str.is_empty() || pat.len() > str.len() {105 return ArrValue::empty();106 }107108 let str = str.as_str();109 let pat = pat.as_bytes();110 let strb = str.as_bytes();111112 let max_pos = str.len() - pat.len();113114 let mut out: Vec<Val> = Vec::new();115 for (ch_idx, (i, _)) in str116 .char_indices()117 .take_while(|(i, _)| i <= &max_pos)118 .enumerate()119 {120 if &strb[i..i + pat.len()] == pat {121 out.push(Val::Num(122 ch_idx.try_into().expect("unrealisticly long string"),123 ));124 }125 }126 out.into()127}128129#[builtin]130pub fn builtin_parse_int(str: IStr) -> Result<f64> {131 if let Some(raw) = str.strip_prefix('-') {132 if raw.is_empty() {133 bail!("integer only consists of a minus")134 }135136 parse_nat::<10>(raw).map(|value| -value)137 } else {138 if str.is_empty() {139 bail!("empty integer")140 }141142 parse_nat::<10>(str.as_str())143 }144}145146#[builtin]147pub fn builtin_parse_octal(str: IStr) -> Result<f64> {148 if str.is_empty() {149 bail!("empty octal integer");150 }151152 parse_nat::<8>(str.as_str())153}154155#[builtin]156pub fn builtin_parse_hex(str: IStr) -> Result<f64> {157 if str.is_empty() {158 bail!("empty hexadecimal integer");159 }160161 parse_nat::<16>(str.as_str())162}163164fn parse_nat<const BASE: u32>(raw: &str) -> Result<f64> {165 const ZERO_CODE: u32 = '0' as u32;166 const UPPER_A_CODE: u32 = 'A' as u32;167 const LOWER_A_CODE: u32 = 'a' as u32;168169 #[inline]170 fn checked_sub_if(condition: bool, lhs: u32, rhs: u32) -> Option<u32> {171 if condition {172 lhs.checked_sub(rhs)173 } else {174 None175 }176 }177178 debug_assert!(179 1 <= BASE && BASE <= 16,180 "integer base should be between 1 and 16"181 );182183 let base = f64::from(BASE);184185 raw.chars().try_fold(0f64, |aggregate, digit| {186 let digit = digit as u32;187 // if-let-else looks better here than Option combinators188 #[allow(clippy::option_if_let_else)]189 let digit = if let Some(digit) = checked_sub_if(BASE > 10, digit, LOWER_A_CODE) {190 digit + 10191 } else if let Some(digit) = checked_sub_if(BASE > 10, digit, UPPER_A_CODE) {192 digit + 10193 } else {194 digit.checked_sub(ZERO_CODE).unwrap_or(BASE)195 };196197 if digit < BASE {198 Ok(base.mul_add(aggregate, f64::from(digit)))199 } else {200 bail!("{raw:?} is not a base {BASE} integer");201 }202 })203}204205#[cfg(feature = "exp-bigint")]206#[builtin]207pub fn builtin_bigint(v: Either![f64, IStr]) -> Result<Val> {208 use Either2::*;209 use jrsonnet_evaluator::runtime_error;210 Ok(match v {211 A(a) => {212 Val::BigInt(Box::new(a.to_string().parse().map_err(|e| {213 runtime_error!("number is not convertible to bigint: {e}")214 })?))215 }216 B(b) => Val::BigInt(Box::new(217 b.as_str()218 .parse()219 .map_err(|e| runtime_error!("bad bigint: {e}"))?,220 )),221 })222}223224#[builtin]225pub fn builtin_string_chars(str: IStr) -> ArrValue {226 str.chars().collect()227}228229#[builtin]230pub fn builtin_lstrip_chars(str: IStr, chars: IndexableVal) -> Result<IStr> {231 if str.is_empty() || chars.is_empty() {232 return Ok(str);233 }234235 let pattern = new_trim_pattern(chars)?;236 Ok(str.as_str().trim_start_matches(pattern).into())237}238239#[builtin]240pub fn builtin_rstrip_chars(str: IStr, chars: IndexableVal) -> Result<IStr> {241 if str.is_empty() || chars.is_empty() {242 return Ok(str);243 }244245 let pattern = new_trim_pattern(chars)?;246 Ok(str.as_str().trim_end_matches(pattern).into())247}248249#[builtin]250pub fn builtin_strip_chars(str: IStr, chars: IndexableVal) -> Result<IStr> {251 if str.is_empty() || chars.is_empty() {252 return Ok(str);253 }254255 let pattern = new_trim_pattern(chars)?;256 Ok(str.as_str().trim_matches(pattern).into())257}258259#[builtin]260pub fn builtin_trim(str: IStr) -> String {261 let filter =262 |v: char| {263 v == ' '264 || v == '\t' || v == '\n'265 || v == '\u{000c}'266 || v == '\r' || v == '\u{0085}'267 || v == '\u{00a0}'268 };269 str.as_str().trim_matches(filter).to_string()270}271272fn new_trim_pattern(chars: IndexableVal) -> Result<impl Fn(char) -> bool> {273 let chars: BTreeSet<char> = match chars {274 IndexableVal::Str(chars) => chars.chars().collect(),275 IndexableVal::Arr(chars) => chars276 .iter()277 .filter_map(|it| it.map(|it| char::from_untyped(it).ok()).transpose())278 .collect::<Result<_, _>>()?,279 };280281 Ok(move |char| chars.contains(&char))282}283284#[cfg(test)]285#[allow(clippy::float_cmp)]286mod tests {287 use super::*;288289 #[test]290 fn parse_nat_base_8() {291 assert_eq!(parse_nat::<8>("0").unwrap(), 0.);292 assert_eq!(parse_nat::<8>("5").unwrap(), 5.);293 assert_eq!(parse_nat::<8>("32").unwrap(), f64::from(0o32));294 assert_eq!(parse_nat::<8>("761").unwrap(), f64::from(0o761));295 }296297 #[test]298 fn parse_nat_base_10() {299 assert_eq!(parse_nat::<10>("0").unwrap(), 0.);300 assert_eq!(parse_nat::<10>("3").unwrap(), 3.);301 assert_eq!(parse_nat::<10>("27").unwrap(), 27.);302 assert_eq!(parse_nat::<10>("123").unwrap(), 123.);303 }304305 #[test]306 fn parse_nat_base_16() {307 assert_eq!(parse_nat::<16>("0").unwrap(), 0.);308 assert_eq!(parse_nat::<16>("A").unwrap(), 10.);309 assert_eq!(parse_nat::<16>("a9").unwrap(), f64::from(0xA9));310 assert_eq!(parse_nat::<16>("BbC").unwrap(), f64::from(0xBBC));311 }312}