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

difftreelog

feat exp-bigint

Yaroslav Bolyukin2023-04-17parent: #205090d.patch.diff
in: master

16 files changed

modifiedCargo.lockdiffbeforeafterboth
298 "jrsonnet-macros",298 "jrsonnet-macros",
299 "jrsonnet-parser",299 "jrsonnet-parser",
300 "jrsonnet-types",300 "jrsonnet-types",
301 "num-bigint",
301 "pathdiff",302 "pathdiff",
302 "rustc-hash",303 "rustc-hash",
303 "serde",304 "serde",
370 "jrsonnet-macros",371 "jrsonnet-macros",
371 "jrsonnet-parser",372 "jrsonnet-parser",
372 "md5",373 "md5",
374 "num-bigint",
373 "serde",375 "serde",
374 "serde_json",376 "serde_json",
375 "serde_yaml_with_quirks",377 "serde_yaml_with_quirks",
448 "mimalloc-sys",450 "mimalloc-sys",
449]451]
452
453[[package]]
454name = "num-bigint"
455version = "0.4.3"
456source = "registry+https://github.com/rust-lang/crates.io-index"
457checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f"
458dependencies = [
459 "autocfg",
460 "num-integer",
461 "num-traits",
462 "serde",
463]
464
465[[package]]
466name = "num-integer"
467version = "0.1.45"
468source = "registry+https://github.com/rust-lang/crates.io-index"
469checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
470dependencies = [
471 "autocfg",
472 "num-traits",
473]
474
475[[package]]
476name = "num-traits"
477version = "0.2.15"
478source = "registry+https://github.com/rust-lang/crates.io-index"
479checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
480dependencies = [
481 "autocfg",
482]
450483
451[[package]]484[[package]]
452name = "once_cell"485name = "once_cell"
modifiedCargo.tomldiffbeforeafterboth
1[workspace]1[workspace]
2package.version = "0.5.0"2package.version = "0.5.0-pre7"
3members = ["crates/*", "bindings/jsonnet", "cmds/jrsonnet", "tests"]3members = ["crates/*", "bindings/jsonnet", "cmds/jrsonnet", "tests"]
4default-members = ["cmds/jrsonnet"]4default-members = ["cmds/jrsonnet"]
55
modifiedcmds/jrsonnet/Cargo.tomldiffbeforeafterboth
19exp-destruct = ["jrsonnet-evaluator/exp-destruct"]19exp-destruct = ["jrsonnet-evaluator/exp-destruct"]
20# Iteration over objects yields [key, value] elements20# Iteration over objects yields [key, value] elements
21exp-object-iteration = ["jrsonnet-evaluator/exp-object-iteration"]21exp-object-iteration = ["jrsonnet-evaluator/exp-object-iteration"]
22# Bigint type
23exp-bigint = ["jrsonnet-evaluator/exp-bigint", "jrsonnet-cli/exp-bigint"]
2224
23# std.thisFile support25# std.thisFile support
24legacy-this-file = ["jrsonnet-cli/legacy-this-file"]26legacy-this-file = ["jrsonnet-cli/legacy-this-file"]
modifiedcrates/jrsonnet-cli/Cargo.tomldiffbeforeafterboth
11 "jrsonnet-evaluator/exp-preserve-order",11 "jrsonnet-evaluator/exp-preserve-order",
12 "jrsonnet-stdlib/exp-preserve-order",12 "jrsonnet-stdlib/exp-preserve-order",
13]13]
14exp-bigint = [
15 "jrsonnet-evaluator/exp-bigint",
16 "jrsonnet-stdlib/exp-bigint",
17]
14legacy-this-file = ["jrsonnet-stdlib/legacy-this-file"]18legacy-this-file = ["jrsonnet-stdlib/legacy-this-file"]
1519
16[dependencies]20[dependencies]
modifiedcrates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth
24exp-destruct = ["jrsonnet-parser/exp-destruct"]24exp-destruct = ["jrsonnet-parser/exp-destruct"]
25# Iteration over objects yields [key, value] elements25# Iteration over objects yields [key, value] elements
26exp-object-iteration = []26exp-object-iteration = []
27# Bigint type
28exp-bigint = ["num-bigint"]
2729
28# Improves performance, and implements some useful things using nightly-only features30# Improves performance, and implements some useful things using nightly-only features
29nightly = ["hashbrown/nightly"]31nightly = ["hashbrown/nightly"]
54annotate-snippets = { version = "0.9.1", features = ["color"], optional = true }56annotate-snippets = { version = "0.9.1", features = ["color"], optional = true }
55# Async imports57# Async imports
56async-trait = { version = "0.1.60", optional = true }58async-trait = { version = "0.1.60", optional = true }
59# Bigint
60num-bigint = { version = "0.4.3", features = ["serde"], optional = true }
5761
modifiedcrates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth
47 (Arr(a), Arr(b)) => Val::Arr(ArrValue::extended(a.clone(), b.clone())),47 (Arr(a), Arr(b)) => Val::Arr(ArrValue::extended(a.clone(), b.clone())),
4848
49 (Num(v1), Num(v2)) => Val::new_checked_num(v1 + v2)?,49 (Num(v1), Num(v2)) => Val::new_checked_num(v1 + v2)?,
50 #[cfg(feature = "exp-bigint")]
51 (BigInt(a), BigInt(b)) => BigInt(Box::new((&**a).clone() + (&**b).clone())),
50 _ => throw!(BinaryOperatorDoesNotOperateOnValues(52 _ => throw!(BinaryOperatorDoesNotOperateOnValues(
51 BinaryOpType::Add,53 BinaryOpType::Add,
52 a.value_type(),54 a.value_type(),
95 Ok(match (a, b) {97 Ok(match (a, b) {
96 (Str(a), Str(b)) => a.cmp(b),98 (Str(a), Str(b)) => a.cmp(b),
97 (Num(a), Num(b)) => a.partial_cmp(b).expect("jsonnet numbers are non NaN"),99 (Num(a), Num(b)) => a.partial_cmp(b).expect("jsonnet numbers are non NaN"),
100 #[cfg(feature = "exp-bigint")]
101 (BigInt(a), BigInt(b)) => a.cmp(b),
98 (Arr(a), Arr(b)) => {102 (Arr(a), Arr(b)) => {
99 if let (Some(ai), Some(bi)) = (a.iter_cheap(), b.iter_cheap()) {103 if let (Some(ai), Some(bi)) = (a.iter_cheap(), b.iter_cheap()) {
100 for (a, b) in ai.zip(bi) {104 for (a, b) in ai.zip(bi) {
174 Num(f64::from((*v1 as i32) >> (*v2 as i32)))178 Num(f64::from((*v1 as i32) >> (*v2 as i32)))
175 }179 }
180
181 // Bigint X Bigint
182 #[cfg(feature = "exp-bigint")]
183 (BigInt(a), Mul, BigInt(b)) => BigInt(Box::new((&**a).clone() * (&**b).clone())),
184 #[cfg(feature = "exp-bigint")]
185 (BigInt(a), Sub, BigInt(b)) => BigInt(Box::new((&**a).clone() - (&**b).clone())),
176186
177 _ => throw!(BinaryOperatorDoesNotOperateOnValues(187 _ => throw!(BinaryOperatorDoesNotOperateOnValues(
178 op,188 op,
modifiedcrates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth
162 Val::Null => serializer.serialize_none(),162 Val::Null => serializer.serialize_none(),
163 Val::Str(s) => serializer.serialize_str(&s.clone().into_flat()),163 Val::Str(s) => serializer.serialize_str(&s.clone().into_flat()),
164 Val::Num(n) => serializer.serialize_f64(*n),164 Val::Num(n) => serializer.serialize_f64(*n),
165 #[cfg(feature = "exp-bigint")]
166 Val::BigInt(b) => b.serialize(serializer),
165 Val::Arr(arr) => {167 Val::Arr(arr) => {
166 let mut seq = serializer.serialize_seq(Some(arr.len()))?;168 let mut seq = serializer.serialize_seq(Some(arr.len()))?;
167 for (i, element) in arr.iter().enumerate() {169 for (i, element) in arr.iter().enumerate() {
modifiedcrates/jrsonnet-evaluator/src/manifest.rsdiffbeforeafterboth
149 Val::Null => buf.push_str("null"),149 Val::Null => buf.push_str("null"),
150 Val::Str(s) => escape_string_json_buf(&s.clone().into_flat(), buf),150 Val::Str(s) => escape_string_json_buf(&s.clone().into_flat(), buf),
151 Val::Num(n) => write!(buf, "{n}").unwrap(),151 Val::Num(n) => write!(buf, "{n}").unwrap(),
152 #[cfg(feature = "exp-bigint")]
153 Val::BigInt(n) => write!(buf, "{n}").unwrap(),
152 Val::Arr(items) => {154 Val::Arr(items) => {
153 buf.push('[');155 buf.push('[');
154 if !items.is_empty() {156 if !items.is_empty() {
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
311 /// Should be finite, and not NaN311 /// Should be finite, and not NaN
312 /// This restriction isn't enforced by enum, as enum field can't be marked as private312 /// This restriction isn't enforced by enum, as enum field can't be marked as private
313 Num(f64),313 Num(f64),
314 /// Experimental bigint
315 #[cfg(feature = "exp-bigint")]
316 BigInt(#[trace(skip)] Box<num_bigint::BigInt>),
314 /// Represents a Jsonnet array.317 /// Represents a Jsonnet array.
315 Arr(ArrValue),318 Arr(ArrValue),
316 /// Represents a Jsonnet object.319 /// Represents a Jsonnet object.
389 match self {392 match self {
390 Self::Str(..) => ValType::Str,393 Self::Str(..) => ValType::Str,
391 Self::Num(..) => ValType::Num,394 Self::Num(..) => ValType::Num,
395 #[cfg(feature = "exp-bigint")]
396 Self::BigInt(..) => ValType::BigInt,
392 Self::Arr(..) => ValType::Arr,397 Self::Arr(..) => ValType::Arr,
393 Self::Obj(..) => ValType::Obj,398 Self::Obj(..) => ValType::Obj,
394 Self::Bool(_) => ValType::Bool,399 Self::Bool(_) => ValType::Bool,
modifiedcrates/jrsonnet-stdlib/Cargo.tomldiffbeforeafterboth
17exp-preserve-order = ["jrsonnet-evaluator/exp-preserve-order"]17exp-preserve-order = ["jrsonnet-evaluator/exp-preserve-order"]
18# Add nonstandard `std.sha256` function18# Add nonstandard `std.sha256` function
19exp-more-hashes = ["dep:sha2"]19exp-more-hashes = ["dep:sha2"]
20# Bigint type
21exp-bigint = ["num-bigint", "jrsonnet-evaluator/exp-bigint"]
2022
21[dependencies]23[dependencies]
22jrsonnet-evaluator.workspace = true24jrsonnet-evaluator.workspace = true
39serde_yaml_with_quirks = "0.8.24"41serde_yaml_with_quirks = "0.8.24"
4042
41sha2 = { version = "0.10.6", optional = true }43sha2 = { version = "0.10.6", optional = true }
44num-bigint = { version = "0.4.3", optional = true }
4245
43[build-dependencies]46[build-dependencies]
44jrsonnet-parser.workspace = true47jrsonnet-parser.workspace = true
modifiedcrates/jrsonnet-stdlib/src/expr.rsdiffbeforeafterboth
83 pub(super) use std::{option::Option, rc::Rc, vec};83 pub(super) use std::{option::Option, rc::Rc, vec};
8484
85 pub(super) use jrsonnet_parser::*;85 pub(super) use jrsonnet_parser::*;
86 };86 }
8787
88 include!(concat!(env!("OUT_DIR"), "/stdlib.rs"))88 include!(concat!(env!("OUT_DIR"), "/stdlib.rs"))
89 }89 }
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
143 ("asciiLower", builtin_ascii_lower::INST),143 ("asciiLower", builtin_ascii_lower::INST),
144 ("findSubstr", builtin_find_substr::INST),144 ("findSubstr", builtin_find_substr::INST),
145 ("parseInt", builtin_parse_int::INST),145 ("parseInt", builtin_parse_int::INST),
146 #[cfg(feature = "exp-bigint")]
147 ("bigint", builtin_bigint::INST),
146 ("parseOctal", builtin_parse_octal::INST),148 ("parseOctal", builtin_parse_octal::INST),
147 ("parseHex", builtin_parse_hex::INST),149 ("parseHex", builtin_parse_hex::INST),
148 // Misc150 // Misc
modifiedcrates/jrsonnet-stdlib/src/manifest/toml.rsdiffbeforeafterboth
103 escape_string_json_buf(&s.clone().into_flat(), buf);103 escape_string_json_buf(&s.clone().into_flat(), buf);
104 }104 }
105 Val::Num(n) => write!(buf, "{n}").unwrap(),105 Val::Num(n) => write!(buf, "{n}").unwrap(),
106 #[cfg(feature = "exp-bigint")]
107 Val::BigInt(n) => write!(buf, "{n}").unwrap(),
106 Val::Arr(a) => {108 Val::Arr(a) => {
107 if a.is_empty() {109 if a.is_empty() {
108 buf.push_str("[]");110 buf.push_str("[]");
modifiedcrates/jrsonnet-stdlib/src/manifest/yaml.rsdiffbeforeafterboth
140 }140 }
141 }141 }
142 Val::Num(n) => write!(buf, "{}", *n).unwrap(),142 Val::Num(n) => write!(buf, "{}", *n).unwrap(),
143 #[cfg(feature = "exp-bigint")]
144 Val::BigInt(n) => write!(buf, "{}", *n).unwrap(),
143 Val::Arr(a) => {145 Val::Arr(a) => {
144 if a.is_empty() {146 if a.is_empty() {
145 buf.push_str("[]");147 buf.push_str("[]");
modifiedcrates/jrsonnet-stdlib/src/strings.rsdiffbeforeafterboth
151 })151 })
152}152}
153
154#[cfg(feature = "exp-bigint")]
155#[builtin]
156pub fn builtin_bigint(v: Either![f64, IStr]) -> Result<Val> {
157 use Either2::*;
158 Ok(match v {
159 A(a) => Val::BigInt(Box::new((a as i64).into())),
160 B(b) => Val::BigInt(Box::new(
161 b.as_str()
162 .parse()
163 .map_err(|e| RuntimeError(format!("bad bigint: {e}").into()))?,
164 )),
165 })
166}
153167
154#[cfg(test)]168#[cfg(test)]
155mod tests {169mod tests {
modifiedcrates/jrsonnet-types/src/lib.rsdiffbeforeafterboth
88 Null,88 Null,
89 Str,89 Str,
90 Num,90 Num,
91 #[cfg(feature = "exp-bigint")]
92 BigInt,
91 Arr,93 Arr,
92 Obj,94 Obj,
93 Func,95 Func,
101 Null => "null",103 Null => "null",
102 Str => "string",104 Str => "string",
103 Num => "number",105 Num => "number",
106 #[cfg(feature = "exp-bigint")]
107 BigInt => "bigint",
104 Arr => "array",108 Arr => "array",
105 Obj => "object",109 Obj => "object",
106 Func => "function",110 Func => "function",