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
--- 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"
modifiedCargo.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"]
 
modifiedcmds/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"]
modifiedcrates/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]
modifiedcrates/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 }
modifiedcrates/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(),
modifiedcrates/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() {
modifiedcrates/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() {
modifiedcrates/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,
modifiedcrates/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
modifiedcrates/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"))
 	}
modifiedcrates/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
modifiedcrates/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("[]");
modifiedcrates/jrsonnet-stdlib/src/manifest/yaml.rsdiffbeforeafterboth
before · crates/jrsonnet-stdlib/src/manifest/yaml.rs
1use std::{borrow::Cow, fmt::Write};23use jrsonnet_evaluator::{4	manifest::{escape_string_json_buf, ManifestFormat},5	throw, Result, Val,6};78pub struct YamlFormat<'s> {9	/// Padding before fields, i.e10	/// ```yaml11	/// a:12	///   b:13	/// ## <- this14	/// ```15	padding: Cow<'s, str>,16	/// Padding before array elements in objects17	/// ```yaml18	/// a:19	///   - 120	/// ## <- this21	/// ```22	arr_element_padding: Cow<'s, str>,23	/// Should yaml keys appear unescaped, when possible24	/// ```yaml25	/// "safe_key": 126	/// # vs27	/// safe_key: 128	/// ```29	quote_keys: bool,30	/// If true - then order of fields is preserved as written,31	/// instead of sorting alphabetically32	#[cfg(feature = "exp-preserve-order")]33	preserve_order: bool,34}35impl YamlFormat<'_> {36	pub fn cli(37		padding: usize,38		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,39	) -> Self {40		let padding = " ".repeat(padding);41		Self {42			padding: Cow::Owned(padding.clone()),43			arr_element_padding: Cow::Owned(padding),44			quote_keys: false,45			#[cfg(feature = "exp-preserve-order")]46			preserve_order,47		}48	}49	pub fn std_to_yaml(50		indent_array_in_object: bool,51		quote_keys: bool,52		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,53	) -> Self {54		Self {55			padding: Cow::Borrowed("  "),56			arr_element_padding: Cow::Borrowed(if indent_array_in_object { "  " } else { "" }),57			quote_keys,58			#[cfg(feature = "exp-preserve-order")]59			preserve_order,60		}61	}62}63impl ManifestFormat for YamlFormat<'_> {64	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {65		manifest_yaml_ex_buf(&val, buf, &mut String::new(), self)66	}67}6869/// From <https://github.com/chyh1990/yaml-rust/blob/da52a68615f2ecdd6b7e4567019f280c433c1521/src/emitter.rs#L289>70/// With added date check71fn yaml_needs_quotes(string: &str) -> bool {72	fn need_quotes_spaces(string: &str) -> bool {73		string.starts_with(' ') || string.ends_with(' ')74	}7576	string.is_empty()77		|| need_quotes_spaces(string)78		|| string.starts_with(|c| matches!(c, '&' | '*' | '?' | '|' | '-' | '<' | '>' | '=' | '!' | '%' | '@'))79		|| string.contains(|c| matches!(c, ':' | '{' | '}' | '[' | ']' | ',' | '#' | '`' | '\"' | '\'' | '\\' | '\0'..='\x06' | '\t' | '\n' | '\r' | '\x0e'..='\x1a' | '\x1c'..='\x1f'))80		|| [81			// http://yaml.org/type/bool.html82			"yes", "Yes", "YES", "no", "No", "NO", "True", "TRUE", "true", "False", "FALSE", "false",83			"on", "On", "ON", "off", "Off", "OFF", // http://yaml.org/type/null.html84			"null", "Null", "NULL", "~",85			// > Quoted in std.jsonnet, however, in serde_yaml they were quoted:86			// > Note: 'y', 'Y', 'n', 'N', is not quoted deliberately, as in libyaml. PyYAML also parse87			// > them as string, not booleans, although it is violating the YAML 1.1 specification.88			// > See https://github.com/dtolnay/serde-yaml/pull/83#discussion_r152628088.89			"y", "Y", "n", "N",90			"-.inf", "+.inf", ".inf",91			"-", "---", ""92		].contains(&string)93		|| (string.chars().all(|c| matches!(c, '0'..='9' | '-'))94			&& string.chars().filter(|c| *c == '-').count() == 2)95		|| string.starts_with('.')96		|| string.starts_with("0x")97		|| string.parse::<i64>().is_ok()98		|| string.parse::<f64>().is_ok()99}100101#[allow(dead_code)]102fn manifest_yaml_ex(val: &Val, options: &YamlFormat<'_>) -> Result<String> {103	let mut out = String::new();104	manifest_yaml_ex_buf(val, &mut out, &mut String::new(), options)?;105	Ok(out)106}107108#[allow(clippy::too_many_lines)]109fn manifest_yaml_ex_buf(110	val: &Val,111	buf: &mut String,112	cur_padding: &mut String,113	options: &YamlFormat<'_>,114) -> Result<()> {115	match val {116		Val::Bool(v) => {117			if *v {118				buf.push_str("true");119			} else {120				buf.push_str("false");121			}122		}123		Val::Null => buf.push_str("null"),124		Val::Str(s) => {125			let s = s.clone().into_flat();126			if s.is_empty() {127				buf.push_str("\"\"");128			} else if let Some(s) = s.strip_suffix('\n') {129				buf.push('|');130				for line in s.split('\n') {131					buf.push('\n');132					buf.push_str(cur_padding);133					buf.push_str(&options.padding);134					buf.push_str(line);135				}136			} else if !options.quote_keys && !yaml_needs_quotes(&s) {137				buf.push_str(&s);138			} else {139				escape_string_json_buf(&s, buf);140			}141		}142		Val::Num(n) => write!(buf, "{}", *n).unwrap(),143		Val::Arr(a) => {144			if a.is_empty() {145				buf.push_str("[]");146			} else {147				for (i, item) in a.iter().enumerate() {148					if i != 0 {149						buf.push('\n');150						buf.push_str(cur_padding);151					}152					let item = item?;153					buf.push('-');154					match &item {155						Val::Arr(a) if !a.is_empty() => {156							buf.push('\n');157							buf.push_str(cur_padding);158							buf.push_str(&options.padding);159						}160						_ => buf.push(' '),161					}162					let extra_padding = match &item {163						Val::Arr(a) => !a.is_empty(),164						Val::Obj(o) => !o.is_empty(),165						_ => false,166					};167					let prev_len = cur_padding.len();168					if extra_padding {169						cur_padding.push_str(&options.padding);170					}171					manifest_yaml_ex_buf(&item, buf, cur_padding, options)?;172					cur_padding.truncate(prev_len);173				}174			}175		}176		Val::Obj(o) => {177			if o.is_empty() {178				buf.push_str("{}");179			} else {180				for (i, key) in o181					.fields(182						#[cfg(feature = "exp-preserve-order")]183						options.preserve_order,184					)185					.iter()186					.enumerate()187				{188					if i != 0 {189						buf.push('\n');190						buf.push_str(cur_padding);191					}192					if !options.quote_keys && !yaml_needs_quotes(key) {193						buf.push_str(key);194					} else {195						escape_string_json_buf(key, buf);196					}197					buf.push(':');198					let prev_len = cur_padding.len();199					let item = o.get(key.clone())?.expect("field exists");200					match &item {201						Val::Arr(a) if !a.is_empty() => {202							buf.push('\n');203							buf.push_str(cur_padding);204							buf.push_str(&options.arr_element_padding);205							cur_padding.push_str(&options.arr_element_padding);206						}207						Val::Obj(o) if !o.is_empty() => {208							buf.push('\n');209							buf.push_str(cur_padding);210							buf.push_str(&options.padding);211							cur_padding.push_str(&options.padding);212						}213						_ => buf.push(' '),214					}215					manifest_yaml_ex_buf(&item, buf, cur_padding, options)?;216					cur_padding.truncate(prev_len);217				}218			}219		}220		Val::Func(_) => throw!("tried to manifest function"),221	}222	Ok(())223}
modifiedcrates/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::*;
modifiedcrates/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",