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.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/operator.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/operator.rs
@@ -47,6 +47,8 @@
(Arr(a), Arr(b)) => Val::Arr(ArrValue::extended(a.clone(), b.clone())),
(Num(v1), Num(v2)) => Val::new_checked_num(v1 + v2)?,
+ #[cfg(feature = "exp-bigint")]
+ (BigInt(a), BigInt(b)) => BigInt(Box::new((&**a).clone() + (&**b).clone())),
_ => throw!(BinaryOperatorDoesNotOperateOnValues(
BinaryOpType::Add,
a.value_type(),
@@ -95,6 +97,8 @@
Ok(match (a, b) {
(Str(a), Str(b)) => a.cmp(b),
(Num(a), Num(b)) => a.partial_cmp(b).expect("jsonnet numbers are non NaN"),
+ #[cfg(feature = "exp-bigint")]
+ (BigInt(a), BigInt(b)) => a.cmp(b),
(Arr(a), Arr(b)) => {
if let (Some(ai), Some(bi)) = (a.iter_cheap(), b.iter_cheap()) {
for (a, b) in ai.zip(bi) {
@@ -174,6 +178,12 @@
Num(f64::from((*v1 as i32) >> (*v2 as i32)))
}
+ // Bigint X Bigint
+ #[cfg(feature = "exp-bigint")]
+ (BigInt(a), Mul, BigInt(b)) => BigInt(Box::new((&**a).clone() * (&**b).clone())),
+ #[cfg(feature = "exp-bigint")]
+ (BigInt(a), Sub, BigInt(b)) => BigInt(Box::new((&**a).clone() - (&**b).clone())),
+
_ => throw!(BinaryOperatorDoesNotOperateOnValues(
op,
a.value_type(),
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.rsdiffbeforeafterboth1use std::{borrow::Cow, fmt::Write};23use crate::{4 error::{ErrorKind::*, Result},5 throw, State, Val,6};78pub trait ManifestFormat {9 fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()>;10 fn manifest(&self, val: Val) -> Result<String> {11 let mut out = String::new();12 self.manifest_buf(val, &mut out)?;13 Ok(out)14 }15}16impl<T> ManifestFormat for Box<T>17where18 T: ManifestFormat + ?Sized,19{20 fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {21 let inner = &**self;22 inner.manifest_buf(val, buf)23 }24}25impl<T> ManifestFormat for &'_ T26where27 T: ManifestFormat + ?Sized,28{29 fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {30 let inner = &**self;31 inner.manifest_buf(val, buf)32 }33}3435#[derive(PartialEq, Eq, Clone, Copy)]36enum JsonFormatting {37 // Applied in manifestification38 Manifest,39 /// Used for std.manifestJson40 /// Empty array/objects extends to "[\n\n]" instead of "[ ]" as in manifest41 Std,42 /// No line breaks, used in `obj+''`43 ToString,44 /// Minified json45 Minify,46}4748pub struct JsonFormat<'s> {49 padding: Cow<'s, str>,50 mtype: JsonFormatting,51 newline: &'s str,52 key_val_sep: &'s str,53 #[cfg(feature = "exp-preserve-order")]54 preserve_order: bool,55}5657impl<'s> JsonFormat<'s> {58 // Minifying format59 pub fn minify(#[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> Self {60 Self {61 padding: Cow::Borrowed(""),62 mtype: JsonFormatting::Minify,63 newline: "\n",64 key_val_sep: ":",65 #[cfg(feature = "exp-preserve-order")]66 preserve_order,67 }68 }69 // Same format as std.toString70 pub fn std_to_string() -> Self {71 Self {72 padding: Cow::Borrowed(""),73 mtype: JsonFormatting::ToString,74 newline: "\n",75 key_val_sep: ": ",76 #[cfg(feature = "exp-preserve-order")]77 preserve_order: false,78 }79 }80 pub fn std_to_json(81 padding: String,82 newline: &'s str,83 key_val_sep: &'s str,84 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,85 ) -> Self {86 Self {87 padding: Cow::Owned(padding),88 mtype: JsonFormatting::Std,89 newline,90 key_val_sep,91 #[cfg(feature = "exp-preserve-order")]92 preserve_order,93 }94 }95 // Same format as CLI manifestification96 pub fn cli(97 padding: usize,98 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,99 ) -> Self {100 if padding == 0 {101 return Self::minify(102 #[cfg(feature = "exp-preserve-order")]103 preserve_order,104 );105 }106 Self {107 padding: Cow::Owned(" ".repeat(padding)),108 mtype: JsonFormatting::Manifest,109 newline: "\n",110 key_val_sep: ": ",111 #[cfg(feature = "exp-preserve-order")]112 preserve_order,113 }114 }115}116impl Default for JsonFormat<'static> {117 fn default() -> Self {118 Self {119 padding: Cow::Borrowed(" "),120 mtype: JsonFormatting::Manifest,121 newline: "\n",122 key_val_sep: ": ",123 #[cfg(feature = "exp-preserve-order")]124 preserve_order: false,125 }126 }127}128129pub fn manifest_json_ex(val: &Val, options: &JsonFormat<'_>) -> Result<String> {130 let mut out = String::new();131 manifest_json_ex_buf(val, &mut out, &mut String::new(), options)?;132 Ok(out)133}134fn manifest_json_ex_buf(135 val: &Val,136 buf: &mut String,137 cur_padding: &mut String,138 options: &JsonFormat<'_>,139) -> Result<()> {140 let mtype = options.mtype;141 match val {142 Val::Bool(v) => {143 if *v {144 buf.push_str("true");145 } else {146 buf.push_str("false");147 }148 }149 Val::Null => buf.push_str("null"),150 Val::Str(s) => escape_string_json_buf(&s.clone().into_flat(), buf),151 Val::Num(n) => write!(buf, "{n}").unwrap(),152 Val::Arr(items) => {153 buf.push('[');154 if !items.is_empty() {155 if mtype != JsonFormatting::ToString && mtype != JsonFormatting::Minify {156 buf.push_str(options.newline);157 }158159 let old_len = cur_padding.len();160 cur_padding.push_str(&options.padding);161 for (i, item) in items.iter().enumerate() {162 if i != 0 {163 buf.push(',');164 if mtype == JsonFormatting::ToString {165 buf.push(' ');166 } else if mtype != JsonFormatting::Minify {167 buf.push_str(options.newline);168 }169 }170 buf.push_str(cur_padding);171 manifest_json_ex_buf(&item?, buf, cur_padding, options)?;172 }173 cur_padding.truncate(old_len);174175 if mtype != JsonFormatting::ToString && mtype != JsonFormatting::Minify {176 buf.push_str(options.newline);177 buf.push_str(cur_padding);178 }179 } else if mtype == JsonFormatting::Std {180 buf.push_str(options.newline);181 buf.push_str(options.newline);182 buf.push_str(cur_padding);183 } else if mtype == JsonFormatting::ToString || mtype == JsonFormatting::Manifest {184 buf.push(' ');185 }186 buf.push(']');187 }188 Val::Obj(obj) => {189 obj.run_assertions()?;190 buf.push('{');191 let fields = obj.fields(192 #[cfg(feature = "exp-preserve-order")]193 options.preserve_order,194 );195 if !fields.is_empty() {196 if mtype != JsonFormatting::ToString && mtype != JsonFormatting::Minify {197 buf.push_str(options.newline);198 }199200 let old_len = cur_padding.len();201 cur_padding.push_str(&options.padding);202 for (i, field) in fields.into_iter().enumerate() {203 if i != 0 {204 buf.push(',');205 if mtype == JsonFormatting::ToString {206 buf.push(' ');207 } else if mtype != JsonFormatting::Minify {208 buf.push_str(options.newline);209 }210 }211 buf.push_str(cur_padding);212 escape_string_json_buf(&field, buf);213 buf.push_str(options.key_val_sep);214 State::push_description(215 || format!("field <{}> manifestification", field.clone()),216 || {217 let value = obj.get(field.clone())?.unwrap();218 manifest_json_ex_buf(&value, buf, cur_padding, options)?;219 Ok(())220 },221 )?;222 }223 cur_padding.truncate(old_len);224225 if mtype != JsonFormatting::ToString && mtype != JsonFormatting::Minify {226 buf.push_str(options.newline);227 buf.push_str(cur_padding);228 }229 } else if mtype == JsonFormatting::Std {230 buf.push_str(options.newline);231 buf.push_str(options.newline);232 buf.push_str(cur_padding);233 } else if mtype == JsonFormatting::ToString || mtype == JsonFormatting::Manifest {234 buf.push(' ');235 }236 buf.push('}');237 }238 Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),239 };240 Ok(())241}242243impl ManifestFormat for JsonFormat<'_> {244 fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {245 manifest_json_ex_buf(&val, buf, &mut String::new(), self)246 }247}248249pub struct ToStringFormat;250impl ManifestFormat for ToStringFormat {251 fn manifest_buf(&self, val: Val, out: &mut String) -> Result<()> {252 JsonFormat::std_to_string().manifest_buf(val, out)253 }254}255pub struct StringFormat;256impl ManifestFormat for StringFormat {257 fn manifest_buf(&self, val: Val, out: &mut String) -> Result<()> {258 let Val::Str(s) = val else {259 throw!("output should be string for string manifest format, got {}", val.value_type())260 };261 write!(out, "{s}").unwrap();262 Ok(())263 }264}265266pub struct YamlStreamFormat<I>(pub I);267impl<I: ManifestFormat> ManifestFormat for YamlStreamFormat<I> {268 fn manifest_buf(&self, val: Val, out: &mut String) -> Result<()> {269 let Val::Arr(arr) = val else {270 throw!("output should be array for yaml stream format, got {}", val.value_type())271 };272 if !arr.is_empty() {273 for v in arr.iter() {274 let v = v?;275 out.push_str("---\n");276 self.0.manifest_buf(v, out)?;277 out.push('\n');278 }279 out.push_str("...");280 }281 Ok(())282 }283}284285pub fn escape_string_json(s: &str) -> String {286 let mut buf = String::new();287 escape_string_json_buf(s, &mut buf);288 buf289}290291// Json string encoding was borrowed from https://github.com/serde-rs/json292293const BB: u8 = b'b'; // \x08294const TT: u8 = b't'; // \x09295const NN: u8 = b'n'; // \x0A296const FF: u8 = b'f'; // \x0C297const RR: u8 = b'r'; // \x0D298const QU: u8 = b'"'; // \x22299const BS: u8 = b'\\'; // \x5C300const UU: u8 = b'u'; // \x00...\x1F except the ones above301const __: u8 = 0;302303// Lookup table of escape sequences. A value of b'x' at index i means that byte304// i is escaped as "\x" in JSON. A value of 0 means that byte i is not escaped.305static ESCAPE: [u8; 256] = [306 // 1 2 3 4 5 6 7 8 9 A B C D E F307 UU, UU, UU, UU, UU, UU, UU, UU, BB, TT, NN, UU, FF, RR, UU, UU, // 0308 UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, // 1309 __, __, QU, __, __, __, __, __, __, __, __, __, __, __, __, __, // 2310 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 3311 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 4312 __, __, __, __, __, __, __, __, __, __, __, __, BS, __, __, __, // 5313 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 6314 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 7315 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 8316 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 9317 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // A318 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // B319 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // C320 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // D321 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // E322 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // F323];324325pub fn escape_string_json_buf(value: &str, buf: &mut String) {326 // Safety: we only write correct utf-8 in this function327 let buf: &mut Vec<u8> = unsafe { &mut *(buf as *mut String).cast::<Vec<u8>>() };328 let bytes = value.as_bytes();329330 // Perfect for ascii strings, removes any reallocations331 buf.reserve(value.len() + 2);332333 buf.push(b'"');334335 let mut start = 0;336337 for (i, &byte) in bytes.iter().enumerate() {338 let escape = ESCAPE[byte as usize];339 if escape == __ {340 continue;341 }342343 if start < i {344 buf.extend_from_slice(&bytes[start..i]);345 }346 start = i + 1;347348 match escape {349 self::BB | self::TT | self::NN | self::FF | self::RR | self::QU | self::BS => {350 buf.extend_from_slice(&[b'\\', escape]);351 }352 self::UU => {353 static HEX_DIGITS: [u8; 16] = *b"0123456789abcdef";354 let bytes = &[355 b'\\',356 b'u',357 b'0',358 b'0',359 HEX_DIGITS[(byte >> 4) as usize],360 HEX_DIGITS[(byte & 0xF) as usize],361 ];362 buf.extend_from_slice(bytes);363 }364 _ => unreachable!(),365 }366 }367368 if start == bytes.len() {369 buf.push(b'"');370 return;371 }372373 buf.extend_from_slice(&bytes[start..]);374 buf.push(b'"');375}1use std::{borrow::Cow, fmt::Write};23use crate::{4 error::{ErrorKind::*, Result},5 throw, State, Val,6};78pub trait ManifestFormat {9 fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()>;10 fn manifest(&self, val: Val) -> Result<String> {11 let mut out = String::new();12 self.manifest_buf(val, &mut out)?;13 Ok(out)14 }15}16impl<T> ManifestFormat for Box<T>17where18 T: ManifestFormat + ?Sized,19{20 fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {21 let inner = &**self;22 inner.manifest_buf(val, buf)23 }24}25impl<T> ManifestFormat for &'_ T26where27 T: ManifestFormat + ?Sized,28{29 fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {30 let inner = &**self;31 inner.manifest_buf(val, buf)32 }33}3435#[derive(PartialEq, Eq, Clone, Copy)]36enum JsonFormatting {37 // Applied in manifestification38 Manifest,39 /// Used for std.manifestJson40 /// Empty array/objects extends to "[\n\n]" instead of "[ ]" as in manifest41 Std,42 /// No line breaks, used in `obj+''`43 ToString,44 /// Minified json45 Minify,46}4748pub struct JsonFormat<'s> {49 padding: Cow<'s, str>,50 mtype: JsonFormatting,51 newline: &'s str,52 key_val_sep: &'s str,53 #[cfg(feature = "exp-preserve-order")]54 preserve_order: bool,55}5657impl<'s> JsonFormat<'s> {58 // Minifying format59 pub fn minify(#[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> Self {60 Self {61 padding: Cow::Borrowed(""),62 mtype: JsonFormatting::Minify,63 newline: "\n",64 key_val_sep: ":",65 #[cfg(feature = "exp-preserve-order")]66 preserve_order,67 }68 }69 // Same format as std.toString70 pub fn std_to_string() -> Self {71 Self {72 padding: Cow::Borrowed(""),73 mtype: JsonFormatting::ToString,74 newline: "\n",75 key_val_sep: ": ",76 #[cfg(feature = "exp-preserve-order")]77 preserve_order: false,78 }79 }80 pub fn std_to_json(81 padding: String,82 newline: &'s str,83 key_val_sep: &'s str,84 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,85 ) -> Self {86 Self {87 padding: Cow::Owned(padding),88 mtype: JsonFormatting::Std,89 newline,90 key_val_sep,91 #[cfg(feature = "exp-preserve-order")]92 preserve_order,93 }94 }95 // Same format as CLI manifestification96 pub fn cli(97 padding: usize,98 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,99 ) -> Self {100 if padding == 0 {101 return Self::minify(102 #[cfg(feature = "exp-preserve-order")]103 preserve_order,104 );105 }106 Self {107 padding: Cow::Owned(" ".repeat(padding)),108 mtype: JsonFormatting::Manifest,109 newline: "\n",110 key_val_sep: ": ",111 #[cfg(feature = "exp-preserve-order")]112 preserve_order,113 }114 }115}116impl Default for JsonFormat<'static> {117 fn default() -> Self {118 Self {119 padding: Cow::Borrowed(" "),120 mtype: JsonFormatting::Manifest,121 newline: "\n",122 key_val_sep: ": ",123 #[cfg(feature = "exp-preserve-order")]124 preserve_order: false,125 }126 }127}128129pub fn manifest_json_ex(val: &Val, options: &JsonFormat<'_>) -> Result<String> {130 let mut out = String::new();131 manifest_json_ex_buf(val, &mut out, &mut String::new(), options)?;132 Ok(out)133}134fn manifest_json_ex_buf(135 val: &Val,136 buf: &mut String,137 cur_padding: &mut String,138 options: &JsonFormat<'_>,139) -> Result<()> {140 let mtype = options.mtype;141 match val {142 Val::Bool(v) => {143 if *v {144 buf.push_str("true");145 } else {146 buf.push_str("false");147 }148 }149 Val::Null => buf.push_str("null"),150 Val::Str(s) => escape_string_json_buf(&s.clone().into_flat(), buf),151 Val::Num(n) => write!(buf, "{n}").unwrap(),152 #[cfg(feature = "exp-bigint")]153 Val::BigInt(n) => write!(buf, "{n}").unwrap(),154 Val::Arr(items) => {155 buf.push('[');156 if !items.is_empty() {157 if mtype != JsonFormatting::ToString && mtype != JsonFormatting::Minify {158 buf.push_str(options.newline);159 }160161 let old_len = cur_padding.len();162 cur_padding.push_str(&options.padding);163 for (i, item) in items.iter().enumerate() {164 if i != 0 {165 buf.push(',');166 if mtype == JsonFormatting::ToString {167 buf.push(' ');168 } else if mtype != JsonFormatting::Minify {169 buf.push_str(options.newline);170 }171 }172 buf.push_str(cur_padding);173 manifest_json_ex_buf(&item?, buf, cur_padding, options)?;174 }175 cur_padding.truncate(old_len);176177 if mtype != JsonFormatting::ToString && mtype != JsonFormatting::Minify {178 buf.push_str(options.newline);179 buf.push_str(cur_padding);180 }181 } else if mtype == JsonFormatting::Std {182 buf.push_str(options.newline);183 buf.push_str(options.newline);184 buf.push_str(cur_padding);185 } else if mtype == JsonFormatting::ToString || mtype == JsonFormatting::Manifest {186 buf.push(' ');187 }188 buf.push(']');189 }190 Val::Obj(obj) => {191 obj.run_assertions()?;192 buf.push('{');193 let fields = obj.fields(194 #[cfg(feature = "exp-preserve-order")]195 options.preserve_order,196 );197 if !fields.is_empty() {198 if mtype != JsonFormatting::ToString && mtype != JsonFormatting::Minify {199 buf.push_str(options.newline);200 }201202 let old_len = cur_padding.len();203 cur_padding.push_str(&options.padding);204 for (i, field) in fields.into_iter().enumerate() {205 if i != 0 {206 buf.push(',');207 if mtype == JsonFormatting::ToString {208 buf.push(' ');209 } else if mtype != JsonFormatting::Minify {210 buf.push_str(options.newline);211 }212 }213 buf.push_str(cur_padding);214 escape_string_json_buf(&field, buf);215 buf.push_str(options.key_val_sep);216 State::push_description(217 || format!("field <{}> manifestification", field.clone()),218 || {219 let value = obj.get(field.clone())?.unwrap();220 manifest_json_ex_buf(&value, buf, cur_padding, options)?;221 Ok(())222 },223 )?;224 }225 cur_padding.truncate(old_len);226227 if mtype != JsonFormatting::ToString && mtype != JsonFormatting::Minify {228 buf.push_str(options.newline);229 buf.push_str(cur_padding);230 }231 } else if mtype == JsonFormatting::Std {232 buf.push_str(options.newline);233 buf.push_str(options.newline);234 buf.push_str(cur_padding);235 } else if mtype == JsonFormatting::ToString || mtype == JsonFormatting::Manifest {236 buf.push(' ');237 }238 buf.push('}');239 }240 Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),241 };242 Ok(())243}244245impl ManifestFormat for JsonFormat<'_> {246 fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {247 manifest_json_ex_buf(&val, buf, &mut String::new(), self)248 }249}250251pub struct ToStringFormat;252impl ManifestFormat for ToStringFormat {253 fn manifest_buf(&self, val: Val, out: &mut String) -> Result<()> {254 JsonFormat::std_to_string().manifest_buf(val, out)255 }256}257pub struct StringFormat;258impl ManifestFormat for StringFormat {259 fn manifest_buf(&self, val: Val, out: &mut String) -> Result<()> {260 let Val::Str(s) = val else {261 throw!("output should be string for string manifest format, got {}", val.value_type())262 };263 write!(out, "{s}").unwrap();264 Ok(())265 }266}267268pub struct YamlStreamFormat<I>(pub I);269impl<I: ManifestFormat> ManifestFormat for YamlStreamFormat<I> {270 fn manifest_buf(&self, val: Val, out: &mut String) -> Result<()> {271 let Val::Arr(arr) = val else {272 throw!("output should be array for yaml stream format, got {}", val.value_type())273 };274 if !arr.is_empty() {275 for v in arr.iter() {276 let v = v?;277 out.push_str("---\n");278 self.0.manifest_buf(v, out)?;279 out.push('\n');280 }281 out.push_str("...");282 }283 Ok(())284 }285}286287pub fn escape_string_json(s: &str) -> String {288 let mut buf = String::new();289 escape_string_json_buf(s, &mut buf);290 buf291}292293// Json string encoding was borrowed from https://github.com/serde-rs/json294295const BB: u8 = b'b'; // \x08296const TT: u8 = b't'; // \x09297const NN: u8 = b'n'; // \x0A298const FF: u8 = b'f'; // \x0C299const RR: u8 = b'r'; // \x0D300const QU: u8 = b'"'; // \x22301const BS: u8 = b'\\'; // \x5C302const UU: u8 = b'u'; // \x00...\x1F except the ones above303const __: u8 = 0;304305// Lookup table of escape sequences. A value of b'x' at index i means that byte306// i is escaped as "\x" in JSON. A value of 0 means that byte i is not escaped.307static ESCAPE: [u8; 256] = [308 // 1 2 3 4 5 6 7 8 9 A B C D E F309 UU, UU, UU, UU, UU, UU, UU, UU, BB, TT, NN, UU, FF, RR, UU, UU, // 0310 UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, // 1311 __, __, QU, __, __, __, __, __, __, __, __, __, __, __, __, __, // 2312 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 3313 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 4314 __, __, __, __, __, __, __, __, __, __, __, __, BS, __, __, __, // 5315 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 6316 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 7317 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 8318 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 9319 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // A320 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // B321 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // C322 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // D323 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // E324 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // F325];326327pub fn escape_string_json_buf(value: &str, buf: &mut String) {328 // Safety: we only write correct utf-8 in this function329 let buf: &mut Vec<u8> = unsafe { &mut *(buf as *mut String).cast::<Vec<u8>>() };330 let bytes = value.as_bytes();331332 // Perfect for ascii strings, removes any reallocations333 buf.reserve(value.len() + 2);334335 buf.push(b'"');336337 let mut start = 0;338339 for (i, &byte) in bytes.iter().enumerate() {340 let escape = ESCAPE[byte as usize];341 if escape == __ {342 continue;343 }344345 if start < i {346 buf.extend_from_slice(&bytes[start..i]);347 }348 start = i + 1;349350 match escape {351 self::BB | self::TT | self::NN | self::FF | self::RR | self::QU | self::BS => {352 buf.extend_from_slice(&[b'\\', escape]);353 }354 self::UU => {355 static HEX_DIGITS: [u8; 16] = *b"0123456789abcdef";356 let bytes = &[357 b'\\',358 b'u',359 b'0',360 b'0',361 HEX_DIGITS[(byte >> 4) as usize],362 HEX_DIGITS[(byte & 0xF) as usize],363 ];364 buf.extend_from_slice(bytes);365 }366 _ => unreachable!(),367 }368 }369370 if start == bytes.len() {371 buf.push(b'"');372 return;373 }374375 buf.extend_from_slice(&bytes[start..]);376 buf.push(b'"');377}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",