difftreelog
feat exp-bigint
in: master
16 files changed
Cargo.lockdiffbeforeafterboth--- a/Cargo.lock
+++ b/Cargo.lock
@@ -298,6 +298,7 @@
"jrsonnet-macros",
"jrsonnet-parser",
"jrsonnet-types",
+ "num-bigint",
"pathdiff",
"rustc-hash",
"serde",
@@ -370,6 +371,7 @@
"jrsonnet-macros",
"jrsonnet-parser",
"md5",
+ "num-bigint",
"serde",
"serde_json",
"serde_yaml_with_quirks",
@@ -449,6 +451,37 @@
]
[[package]]
+name = "num-bigint"
+version = "0.4.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f"
+dependencies = [
+ "autocfg",
+ "num-integer",
+ "num-traits",
+ "serde",
+]
+
+[[package]]
+name = "num-integer"
+version = "0.1.45"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
+dependencies = [
+ "autocfg",
+ "num-traits",
+]
+
+[[package]]
+name = "num-traits"
+version = "0.2.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
+dependencies = [
+ "autocfg",
+]
+
+[[package]]
name = "once_cell"
version = "1.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
Cargo.tomldiffbeforeafterboth--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,5 +1,5 @@
[workspace]
-package.version = "0.5.0"
+package.version = "0.5.0-pre7"
members = ["crates/*", "bindings/jsonnet", "cmds/jrsonnet", "tests"]
default-members = ["cmds/jrsonnet"]
cmds/jrsonnet/Cargo.tomldiffbeforeafterboth--- a/cmds/jrsonnet/Cargo.toml
+++ b/cmds/jrsonnet/Cargo.toml
@@ -19,6 +19,8 @@
exp-destruct = ["jrsonnet-evaluator/exp-destruct"]
# Iteration over objects yields [key, value] elements
exp-object-iteration = ["jrsonnet-evaluator/exp-object-iteration"]
+# Bigint type
+exp-bigint = ["jrsonnet-evaluator/exp-bigint", "jrsonnet-cli/exp-bigint"]
# std.thisFile support
legacy-this-file = ["jrsonnet-cli/legacy-this-file"]
crates/jrsonnet-cli/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-cli/Cargo.toml
+++ b/crates/jrsonnet-cli/Cargo.toml
@@ -11,6 +11,10 @@
"jrsonnet-evaluator/exp-preserve-order",
"jrsonnet-stdlib/exp-preserve-order",
]
+exp-bigint = [
+ "jrsonnet-evaluator/exp-bigint",
+ "jrsonnet-stdlib/exp-bigint",
+]
legacy-this-file = ["jrsonnet-stdlib/legacy-this-file"]
[dependencies]
crates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -24,6 +24,8 @@
exp-destruct = ["jrsonnet-parser/exp-destruct"]
# Iteration over objects yields [key, value] elements
exp-object-iteration = []
+# Bigint type
+exp-bigint = ["num-bigint"]
# Improves performance, and implements some useful things using nightly-only features
nightly = ["hashbrown/nightly"]
@@ -54,3 +56,5 @@
annotate-snippets = { version = "0.9.1", features = ["color"], optional = true }
# Async imports
async-trait = { version = "0.1.60", optional = true }
+# Bigint
+num-bigint = { version = "0.4.3", features = ["serde"], optional = true }
crates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth1use std::cmp::Ordering;23use jrsonnet_parser::{BinaryOpType, LocExpr, UnaryOpType};45use crate::{6 arr::ArrValue,7 error::ErrorKind::*,8 evaluate,9 stdlib::std_format,10 throw,11 typed::Typed,12 val::{equals, StrValue},13 Context, Result, Val,14};1516pub fn evaluate_unary_op(op: UnaryOpType, b: &Val) -> Result<Val> {17 use UnaryOpType::*;18 use Val::*;19 Ok(match (op, b) {20 (Not, Bool(v)) => Bool(!v),21 (Minus, Num(n)) => Num(-*n),22 (BitNot, Num(n)) => Num(f64::from(!(*n as i32))),23 (op, o) => throw!(UnaryOperatorDoesNotOperateOnType(op, o.value_type())),24 })25}2627pub fn evaluate_add_op(a: &Val, b: &Val) -> Result<Val> {28 use Val::*;29 Ok(match (a, b) {30 (Str(v1), Str(v2)) => Str(StrValue::concat(v1.clone(), v2.clone())),3132 // Can't use generic json serialization way, because it depends on number to string concatenation (std.jsonnet:890)33 (Num(a), Str(b)) => Str(StrValue::Flat(format!("{a}{b}").into())),34 (Str(a), Num(b)) => Str(StrValue::Flat(format!("{a}{b}").into())),3536 (Str(a), o) | (o, Str(a)) if a.is_empty() => {37 Val::Str(StrValue::Flat(o.clone().to_string()?))38 }39 (Str(a), o) => Str(StrValue::Flat(40 format!("{a}{}", o.clone().to_string()?).into(),41 )),42 (o, Str(a)) => Str(StrValue::Flat(43 format!("{}{a}", o.clone().to_string()?).into(),44 )),4546 (Obj(v1), Obj(v2)) => Obj(v2.extend_from(v1.clone())),47 (Arr(a), Arr(b)) => Val::Arr(ArrValue::extended(a.clone(), b.clone())),4849 (Num(v1), Num(v2)) => Val::new_checked_num(v1 + v2)?,50 _ => throw!(BinaryOperatorDoesNotOperateOnValues(51 BinaryOpType::Add,52 a.value_type(),53 b.value_type(),54 )),55 })56}5758pub fn evaluate_mod_op(a: &Val, b: &Val) -> Result<Val> {59 use Val::*;60 match (a, b) {61 (Num(a), Num(b)) => {62 if *b == 0.0 {63 throw!(DivisionByZero)64 }65 Ok(Num(a % b))66 }67 (Str(str), vals) => {68 String::into_untyped(std_format(&str.clone().into_flat(), vals.clone())?)69 }70 (a, b) => throw!(BinaryOperatorDoesNotOperateOnValues(71 BinaryOpType::Mod,72 a.value_type(),73 b.value_type()74 )),75 }76}7778pub fn evaluate_binary_op_special(79 ctx: Context,80 a: &LocExpr,81 op: BinaryOpType,82 b: &LocExpr,83) -> Result<Val> {84 use BinaryOpType::*;85 use Val::*;86 Ok(match (evaluate(ctx.clone(), a)?, op, b) {87 (Bool(true), Or, _o) => Val::Bool(true),88 (Bool(false), And, _o) => Val::Bool(false),89 (a, op, eb) => evaluate_binary_op_normal(&a, op, &evaluate(ctx, eb)?)?,90 })91}9293pub fn evaluate_compare_op(a: &Val, b: &Val, op: BinaryOpType) -> Result<Ordering> {94 use Val::*;95 Ok(match (a, b) {96 (Str(a), Str(b)) => a.cmp(b),97 (Num(a), Num(b)) => a.partial_cmp(b).expect("jsonnet numbers are non NaN"),98 (Arr(a), Arr(b)) => {99 if let (Some(ai), Some(bi)) = (a.iter_cheap(), b.iter_cheap()) {100 for (a, b) in ai.zip(bi) {101 let ord = evaluate_compare_op(&a, &b, op)?;102 if !ord.is_eq() {103 return Ok(ord);104 }105 }106 } else {107 let ai = a.iter();108 let bi = b.iter();109110 for (a, b) in ai.zip(bi) {111 let ord = evaluate_compare_op(&a?, &b?, op)?;112 if !ord.is_eq() {113 return Ok(ord);114 }115 }116 }117 a.len().cmp(&b.len())118 }119 (_, _) => throw!(BinaryOperatorDoesNotOperateOnValues(120 op,121 a.value_type(),122 b.value_type()123 )),124 })125}126127pub fn evaluate_binary_op_normal(a: &Val, op: BinaryOpType, b: &Val) -> Result<Val> {128 use BinaryOpType::*;129 use Val::*;130 Ok(match (a, op, b) {131 (a, Add, b) => evaluate_add_op(a, b)?,132133 (a, Eq, b) => Bool(equals(a, b)?),134 (a, Neq, b) => Bool(!equals(a, b)?),135136 (a, Lt, b) => Bool(evaluate_compare_op(a, b, Lt)?.is_lt()),137 (a, Gt, b) => Bool(evaluate_compare_op(a, b, Gt)?.is_gt()),138 (a, Lte, b) => Bool(evaluate_compare_op(a, b, Lte)?.is_le()),139 (a, Gte, b) => Bool(evaluate_compare_op(a, b, Gte)?.is_ge()),140141 (Str(a), In, Obj(obj)) => Bool(obj.has_field_ex(a.clone().into_flat(), true)),142 (a, Mod, b) => evaluate_mod_op(a, b)?,143144 (Str(v1), Mul, Num(v2)) => Str(StrValue::Flat(v1.to_string().repeat(*v2 as usize).into())),145146 // Bool X Bool147 (Bool(a), And, Bool(b)) => Bool(*a && *b),148 (Bool(a), Or, Bool(b)) => Bool(*a || *b),149150 // Num X Num151 (Num(v1), Mul, Num(v2)) => Val::new_checked_num(v1 * v2)?,152 (Num(v1), Div, Num(v2)) => {153 if *v2 == 0.0 {154 throw!(DivisionByZero)155 }156 Val::new_checked_num(v1 / v2)?157 }158159 (Num(v1), Sub, Num(v2)) => Val::new_checked_num(v1 - v2)?,160161 (Num(v1), BitAnd, Num(v2)) => Num(f64::from((*v1 as i32) & (*v2 as i32))),162 (Num(v1), BitOr, Num(v2)) => Num(f64::from((*v1 as i32) | (*v2 as i32))),163 (Num(v1), BitXor, Num(v2)) => Num(f64::from((*v1 as i32) ^ (*v2 as i32))),164 (Num(v1), Lhs, Num(v2)) => {165 if *v2 < 0.0 {166 throw!("shift by negative exponent")167 }168 Num(f64::from((*v1 as i32) << (*v2 as i32)))169 }170 (Num(v1), Rhs, Num(v2)) => {171 if *v2 < 0.0 {172 throw!("shift by negative exponent")173 }174 Num(f64::from((*v1 as i32) >> (*v2 as i32)))175 }176177 _ => throw!(BinaryOperatorDoesNotOperateOnValues(178 op,179 a.value_type(),180 b.value_type(),181 )),182 })183}crates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -162,6 +162,8 @@
Val::Null => serializer.serialize_none(),
Val::Str(s) => serializer.serialize_str(&s.clone().into_flat()),
Val::Num(n) => serializer.serialize_f64(*n),
+ #[cfg(feature = "exp-bigint")]
+ Val::BigInt(b) => b.serialize(serializer),
Val::Arr(arr) => {
let mut seq = serializer.serialize_seq(Some(arr.len()))?;
for (i, element) in arr.iter().enumerate() {
crates/jrsonnet-evaluator/src/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/manifest.rs
@@ -149,6 +149,8 @@
Val::Null => buf.push_str("null"),
Val::Str(s) => escape_string_json_buf(&s.clone().into_flat(), buf),
Val::Num(n) => write!(buf, "{n}").unwrap(),
+ #[cfg(feature = "exp-bigint")]
+ Val::BigInt(n) => write!(buf, "{n}").unwrap(),
Val::Arr(items) => {
buf.push('[');
if !items.is_empty() {
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -311,6 +311,9 @@
/// Should be finite, and not NaN
/// This restriction isn't enforced by enum, as enum field can't be marked as private
Num(f64),
+ /// Experimental bigint
+ #[cfg(feature = "exp-bigint")]
+ BigInt(#[trace(skip)] Box<num_bigint::BigInt>),
/// Represents a Jsonnet array.
Arr(ArrValue),
/// Represents a Jsonnet object.
@@ -389,6 +392,8 @@
match self {
Self::Str(..) => ValType::Str,
Self::Num(..) => ValType::Num,
+ #[cfg(feature = "exp-bigint")]
+ Self::BigInt(..) => ValType::BigInt,
Self::Arr(..) => ValType::Arr,
Self::Obj(..) => ValType::Obj,
Self::Bool(_) => ValType::Bool,
crates/jrsonnet-stdlib/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-stdlib/Cargo.toml
+++ b/crates/jrsonnet-stdlib/Cargo.toml
@@ -17,6 +17,8 @@
exp-preserve-order = ["jrsonnet-evaluator/exp-preserve-order"]
# Add nonstandard `std.sha256` function
exp-more-hashes = ["dep:sha2"]
+# Bigint type
+exp-bigint = ["num-bigint", "jrsonnet-evaluator/exp-bigint"]
[dependencies]
jrsonnet-evaluator.workspace = true
@@ -39,6 +41,7 @@
serde_yaml_with_quirks = "0.8.24"
sha2 = { version = "0.10.6", optional = true }
+num-bigint = { version = "0.4.3", optional = true }
[build-dependencies]
jrsonnet-parser.workspace = true
crates/jrsonnet-stdlib/src/expr.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/expr.rs
+++ b/crates/jrsonnet-stdlib/src/expr.rs
@@ -83,7 +83,7 @@
pub(super) use std::{option::Option, rc::Rc, vec};
pub(super) use jrsonnet_parser::*;
- };
+ }
include!(concat!(env!("OUT_DIR"), "/stdlib.rs"))
}
crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -143,6 +143,8 @@
("asciiLower", builtin_ascii_lower::INST),
("findSubstr", builtin_find_substr::INST),
("parseInt", builtin_parse_int::INST),
+ #[cfg(feature = "exp-bigint")]
+ ("bigint", builtin_bigint::INST),
("parseOctal", builtin_parse_octal::INST),
("parseHex", builtin_parse_hex::INST),
// Misc
crates/jrsonnet-stdlib/src/manifest/toml.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/manifest/toml.rs
+++ b/crates/jrsonnet-stdlib/src/manifest/toml.rs
@@ -103,6 +103,8 @@
escape_string_json_buf(&s.clone().into_flat(), buf);
}
Val::Num(n) => write!(buf, "{n}").unwrap(),
+ #[cfg(feature = "exp-bigint")]
+ Val::BigInt(n) => write!(buf, "{n}").unwrap(),
Val::Arr(a) => {
if a.is_empty() {
buf.push_str("[]");
crates/jrsonnet-stdlib/src/manifest/yaml.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/manifest/yaml.rs
+++ b/crates/jrsonnet-stdlib/src/manifest/yaml.rs
@@ -140,6 +140,8 @@
}
}
Val::Num(n) => write!(buf, "{}", *n).unwrap(),
+ #[cfg(feature = "exp-bigint")]
+ Val::BigInt(n) => write!(buf, "{}", *n).unwrap(),
Val::Arr(a) => {
if a.is_empty() {
buf.push_str("[]");
crates/jrsonnet-stdlib/src/strings.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/strings.rs
+++ b/crates/jrsonnet-stdlib/src/strings.rs
@@ -151,6 +151,20 @@
})
}
+#[cfg(feature = "exp-bigint")]
+#[builtin]
+pub fn builtin_bigint(v: Either![f64, IStr]) -> Result<Val> {
+ use Either2::*;
+ Ok(match v {
+ A(a) => Val::BigInt(Box::new((a as i64).into())),
+ B(b) => Val::BigInt(Box::new(
+ b.as_str()
+ .parse()
+ .map_err(|e| RuntimeError(format!("bad bigint: {e}").into()))?,
+ )),
+ })
+}
+
#[cfg(test)]
mod tests {
use super::*;
crates/jrsonnet-types/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-types/src/lib.rs
+++ b/crates/jrsonnet-types/src/lib.rs
@@ -88,6 +88,8 @@
Null,
Str,
Num,
+ #[cfg(feature = "exp-bigint")]
+ BigInt,
Arr,
Obj,
Func,
@@ -101,6 +103,8 @@
Null => "null",
Str => "string",
Num => "number",
+ #[cfg(feature = "exp-bigint")]
+ BigInt => "bigint",
Arr => "array",
Obj => "object",
Func => "function",