git.delta.rocks / jrsonnet / refs/commits / 7af406eaa740

difftreelog

feat return NumValue directly from parser

rlsuqplzYaroslav Bolyukin2026-04-25parent: #eee6c62.patch.diff
in: master

15 files changed

modifiedbindings/jsonnet/src/val_make.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/val_make.rs
+++ b/bindings/jsonnet/src/val_make.rs
@@ -5,10 +5,7 @@
 	os::raw::{c_char, c_double, c_int},
 };
 
-use jrsonnet_evaluator::{
-	ObjValue, Val,
-	val::{ArrValue, NumValue},
-};
+use jrsonnet_evaluator::{NumValue, ObjValue, Val};
 
 use crate::VM;
 
modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -2,7 +2,9 @@
 
 use jrsonnet_gcmodule::{Acyclic, Trace};
 use jrsonnet_interner::IStr;
-use jrsonnet_ir::{BinaryOpType, Source, SourcePath, Span, Spanned, UnaryOpType};
+use jrsonnet_ir::{
+	BinaryOpType, ConvertNumValueError, Source, SourcePath, Span, Spanned, UnaryOpType,
+};
 use jrsonnet_types::ValType;
 use thiserror::Error;
 
@@ -11,7 +13,6 @@
 	function::{CallLocation, FunctionSignature, ParamName},
 	stdlib::format::FormatError,
 	typed::TypeLocError,
-	val::ConvertNumValueError,
 };
 
 #[derive(Debug, Clone)]
@@ -228,6 +229,11 @@
 		Self::new(e)
 	}
 }
+impl From<ConvertNumValueError> for Error {
+	fn from(e: ConvertNumValueError) -> Self {
+		Self::new(ErrorKind::ConvertNumValue(e))
+	}
+}
 
 impl From<Infallible> for Error {
 	fn from(_value: Infallible) -> Self {
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -21,7 +21,7 @@
 	function::{CallLocation, FuncDesc, FuncVal, PreparedFuncVal},
 	in_frame,
 	typed::{FromUntyped, IntoUntyped as _, Typed},
-	val::{CachedUnbound, IndexableVal, NumValue, StrValue, Thunk},
+	val::{CachedUnbound, IndexableVal, StrValue, Thunk},
 	with_state,
 };
 pub mod destructure;
@@ -58,9 +58,7 @@
 	}
 	Some(match expr {
 		Expr::Str(s) => Val::string(s.clone()),
-		Expr::Num(n) => {
-			Val::Num(NumValue::new(*n).expect("parser will not allow non-finite values"))
-		}
+		Expr::Num(n) => Val::Num(*n),
 		Expr::Literal(LiteralType::False) => Val::Bool(false),
 		Expr::Literal(LiteralType::True) => Val::Bool(true),
 		Expr::Literal(LiteralType::Null) => Val::Null,
modifiedcrates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -1,6 +1,7 @@
 use std::borrow::Cow;
 
 use jrsonnet_interner::{IBytes, IStr};
+use jrsonnet_ir::NumValue;
 use serde::{
 	Deserialize, Serialize, Serializer,
 	de::{self, Visitor},
@@ -12,7 +13,6 @@
 
 use crate::{
 	Error as JrError, ObjValue, ObjValueBuilder, Result, Val, in_description_frame, runtime_error,
-	val::NumValue,
 };
 
 impl<'de> Deserialize<'de> for Val {
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -42,6 +42,7 @@
 use jrsonnet_gcmodule::{Cc, Trace, cc_dyn};
 pub use jrsonnet_interner::{IBytes, IStr};
 pub use jrsonnet_ir as parser;
+pub use jrsonnet_ir::NumValue;
 use jrsonnet_ir::{Expr, Source, SourcePath};
 #[doc(hidden)]
 pub use jrsonnet_macros;
modifiedcrates/jrsonnet-evaluator/src/stdlib/format.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/stdlib/format.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/format.rs
@@ -829,7 +829,7 @@
 #[cfg(test)]
 pub mod test_format {
 	use super::*;
-	use crate::val::NumValue;
+	use crate::NumValue;
 
 	#[test]
 	fn parse() {
modifiedcrates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -2,6 +2,8 @@
 
 use jrsonnet_gcmodule::Trace;
 use jrsonnet_interner::{IBytes, IStr};
+use jrsonnet_ir::NumValue;
+pub use jrsonnet_ir::{MAX_SAFE_INTEGER, MIN_SAFE_INTEGER};
 use jrsonnet_types::{ComplexValType, ValType};
 
 use crate::{
@@ -10,7 +12,7 @@
 	bail,
 	function::FuncVal,
 	typed::CheckType,
-	val::{IndexableVal, NumValue, StrValue, ThunkMapper},
+	val::{IndexableVal, StrValue, ThunkMapper},
 };
 
 #[doc(hidden)]
@@ -219,11 +221,6 @@
 		Ok(inner.map(<ThunkFromUntyped<T>>::default()))
 	}
 }
-
-#[expect(clippy::cast_precision_loss, reason = "checked to not overflow")]
-pub const MAX_SAFE_INTEGER: f64 = ((1u64 << (f64::MANTISSA_DIGITS)) - 1) as f64;
-#[expect(clippy::cast_precision_loss, reason = "checked to not overflow")]
-pub const MIN_SAFE_INTEGER: f64 = (-((1i64 << (f64::MANTISSA_DIGITS)) - 1)) as f64;
 
 macro_rules! impl_int {
 	($($ty:ty)*) => {$(
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -5,7 +5,6 @@
 	marker::PhantomData,
 	mem::replace,
 	num::NonZeroU32,
-	ops::Deref,
 	rc::Rc,
 };
 
@@ -14,16 +13,15 @@
 pub use jrsonnet_macros::Thunk;
 use jrsonnet_types::ValType;
 use rustc_hash::FxHashMap;
-use thiserror::Error;
 
 pub use crate::arr::{ArrValue, ArrayLike};
 use crate::{
-	ObjValue, Result, SupThis, Unbound, WeakSupThis, bail,
+	NumValue, ObjValue, Result, SupThis, Unbound, WeakSupThis, bail,
 	error::{Error, ErrorKind::*},
 	function::FuncVal,
 	gc::WithCapacityExt as _,
 	manifest::{ManifestFormat, ToStringFormat},
-	typed::{BoundedUsize, MAX_SAFE_INTEGER, MIN_SAFE_INTEGER},
+	typed::BoundedUsize,
 };
 
 pub trait ThunkValue: Trace {
@@ -439,134 +437,6 @@
 		let a = self.clone().into_flat();
 		let b = other.clone().into_flat();
 		a.cmp(&b)
-	}
-}
-
-/// Represents jsonnet number
-/// Jsonnet numbers are finite f64, with NaNs disallowed
-#[derive(Trace, Clone, Copy)]
-#[repr(transparent)]
-pub struct NumValue(f64);
-impl NumValue {
-	/// Creates a [`NumValue`], if value is finite and not NaN
-	pub fn new(v: f64) -> Option<Self> {
-		if !v.is_finite() {
-			return None;
-		}
-		Some(Self(v))
-	}
-	#[inline]
-	pub const fn get(&self) -> f64 {
-		self.0
-	}
-	pub(crate) fn truncate_for_bitwise(self) -> Result<i64> {
-		if self.0 < MIN_SAFE_INTEGER || self.0 > MAX_SAFE_INTEGER {
-			bail!("numberic value outside of safe integer range for bitwise operation");
-		}
-		#[expect(clippy::cast_possible_truncation, reason = "intended")]
-		Ok(self.0 as i64)
-	}
-}
-impl PartialEq for NumValue {
-	fn eq(&self, other: &Self) -> bool {
-		self.0 == other.0
-	}
-}
-impl Eq for NumValue {}
-impl Ord for NumValue {
-	#[inline]
-	fn cmp(&self, other: &Self) -> Ordering {
-		// Can't use `total_cmp`: its behavior for `-0` and `0`
-		// is not following wanted.
-		unsafe { self.0.partial_cmp(&other.0).unwrap_unchecked() }
-	}
-}
-impl PartialOrd for NumValue {
-	#[inline]
-	fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
-		Some(self.cmp(other))
-	}
-}
-impl Debug for NumValue {
-	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-		Debug::fmt(&self.0, f)
-	}
-}
-impl Display for NumValue {
-	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-		Display::fmt(&self.0, f)
-	}
-}
-impl Deref for NumValue {
-	type Target = f64;
-
-	#[inline]
-	fn deref(&self) -> &Self::Target {
-		&self.0
-	}
-}
-macro_rules! impl_num {
-	($($ty:ty),+) => {$(
-		impl From<$ty> for NumValue {
-			#[inline]
-			fn from(value: $ty) -> Self {
-				Self(value.into())
-			}
-		}
-	)+};
-}
-impl_num!(i8, u8, i16, u16, i32, u32);
-
-#[derive(Clone, Copy, Debug, Error, Trace)]
-pub enum ConvertNumValueError {
-	#[error("overflow")]
-	Overflow,
-	#[error("underflow")]
-	Underflow,
-	#[error("non-finite")]
-	NonFinite,
-}
-impl From<ConvertNumValueError> for Error {
-	fn from(e: ConvertNumValueError) -> Self {
-		Self::new(e.into())
-	}
-}
-
-macro_rules! impl_try_num {
-	($($ty:ty),+) => {$(
-		impl TryFrom<$ty> for NumValue {
-			type Error = ConvertNumValueError;
-			#[inline]
-			fn try_from(value: $ty) -> Result<Self, ConvertNumValueError> {
-				#[expect(clippy::cast_precision_loss, reason = "precision loss is explicitly handled")]
-				let value = value as f64;
-				if value < MIN_SAFE_INTEGER {
-					return Err(ConvertNumValueError::Underflow)
-				} else if value > MAX_SAFE_INTEGER {
-					return Err(ConvertNumValueError::Overflow)
-				}
-				// Number is finite.
-				Ok(Self(value))
-			}
-		}
-	)+};
-}
-impl_try_num!(usize, isize, i64, u64);
-
-impl TryFrom<f64> for NumValue {
-	type Error = ConvertNumValueError;
-
-	#[inline]
-	fn try_from(value: f64) -> Result<Self, Self::Error> {
-		Self::new(value).ok_or(ConvertNumValueError::NonFinite)
-	}
-}
-impl TryFrom<f32> for NumValue {
-	type Error = ConvertNumValueError;
-
-	#[inline]
-	fn try_from(value: f32) -> Result<Self, Self::Error> {
-		Self::new(f64::from(value)).ok_or(ConvertNumValueError::NonFinite)
 	}
 }
 
modifiedcrates/jrsonnet-ir-parser/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-ir-parser/src/lib.rs
+++ b/crates/jrsonnet-ir-parser/src/lib.rs
@@ -4,8 +4,8 @@
 use jrsonnet_ir::{
 	ArgsDesc, AssertExpr, AssertStmt, BinaryOp, BinaryOpType, BindSpec, CompSpec, Destruct, Expr,
 	ExprParam, ExprParams, FieldMember, FieldName, ForSpecData, IStr, IfElse, IfSpecData,
-	ImportKind, IndexPart, LiteralType, Member, ObjBody, ObjComp, ObjMembers, Slice, SliceDesc,
-	Source, Span, Spanned, UnaryOpType, Visibility, unescape,
+	ImportKind, IndexPart, LiteralType, Member, NumValue, ObjBody, ObjComp, ObjMembers, Slice,
+	SliceDesc, Source, Span, Spanned, UnaryOpType, Visibility, unescape,
 };
 use jrsonnet_lexer::{Lexeme, Lexer, Span as LexSpan, SyntaxKind, T, collect_lexed_str_block};
 
@@ -202,17 +202,21 @@
 	)
 }
 
-fn parse_number(p: &mut Parser<'_>) -> Result<f64> {
+fn parse_number(p: &mut Parser<'_>) -> Result<NumValue> {
 	let text = p.text();
 	let n: f64 = text
 		.replace('_', "")
 		.parse()
 		.map_err(|_| p.error(format!("invalid number literal: {text}")))?;
-	if !n.is_finite() {
-		return Err(p.error("numbers are finite".into()));
-	}
+
+	let v = match NumValue::try_from(n) {
+		Ok(v) => v,
+		Err(e) => return Err(p.error(format!("invalid number value: {e}"))),
+	};
+
 	p.eat_any();
-	Ok(n)
+
+	Ok(v)
 }
 
 fn ident(p: &mut Parser<'_>) -> Result<IStr> {
modifiedcrates/jrsonnet-ir/Cargo.tomldiffbeforeafterboth
--- a/crates/jrsonnet-ir/Cargo.toml
+++ b/crates/jrsonnet-ir/Cargo.toml
@@ -19,6 +19,7 @@
 static_assertions.workspace = true
 
 peg.workspace = true
+thiserror.workspace = true
 
 [dev-dependencies]
 insta.workspace = true
modifiedcrates/jrsonnet-ir/src/expr.rsdiffbeforeafterboth
--- a/crates/jrsonnet-ir/src/expr.rs
+++ b/crates/jrsonnet-ir/src/expr.rs
@@ -8,6 +8,7 @@
 use jrsonnet_interner::IStr;
 
 use crate::{
+	NumValue,
 	function::{FunctionSignature, ParamDefault, ParamName, ParamParse},
 	source::Source,
 };
@@ -398,7 +399,7 @@
 	/// String value: "hello"
 	Str(IStr),
 	/// Number: 1, 2.0, 2e+20
-	Num(f64),
+	Num(NumValue),
 	/// Variable name: test
 	Var(Spanned<IStr>),
 
modifiedcrates/jrsonnet-ir/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-ir/src/lib.rs
+++ b/crates/jrsonnet-ir/src/lib.rs
@@ -1,7 +1,10 @@
 #![allow(clippy::redundant_closure_call, clippy::derive_partial_eq_without_eq)]
 
 mod expr;
+use std::{cmp::Ordering, fmt, ops::Deref};
+
 pub use expr::*;
+use jrsonnet_gcmodule::Acyclic;
 pub use jrsonnet_interner::IStr;
 pub mod function;
 mod location;
@@ -14,3 +17,134 @@
 	Source, SourceDefaultIgnoreJpath, SourceDirectory, SourceFifo, SourceFile, SourcePath,
 	SourcePathT, SourceVirtual,
 };
+
+// It seels to be a wrong place for this kind of stuff, but as it would also be used for static analysis and
+// is already wanted for NumValue, I don't know a better place.
+#[expect(clippy::cast_precision_loss, reason = "checked to not overflow")]
+pub const MAX_SAFE_INTEGER: f64 = ((1u64 << (f64::MANTISSA_DIGITS)) - 1) as f64;
+#[expect(clippy::cast_precision_loss, reason = "checked to not overflow")]
+pub const MIN_SAFE_INTEGER: f64 = (-((1i64 << (f64::MANTISSA_DIGITS)) - 1)) as f64;
+
+/// Represents jsonnet number
+/// Jsonnet numbers are finite f64, with NaNs disallowed
+#[derive(Acyclic, Clone, Copy)]
+pub struct NumValue(f64);
+impl NumValue {
+	/// Creates a [`NumValue`], if value is finite and not NaN
+	pub fn new(v: f64) -> Option<Self> {
+		if !v.is_finite() {
+			return None;
+		}
+		Some(Self(v))
+	}
+	#[inline]
+	pub const fn get(&self) -> f64 {
+		self.0
+	}
+	pub fn truncate_for_bitwise(self) -> Result<i64, ConvertNumValueError> {
+		if self.0 < MIN_SAFE_INTEGER || self.0 > MAX_SAFE_INTEGER {
+			return Err(ConvertNumValueError::BitwiseSafeRange);
+		}
+		#[expect(clippy::cast_possible_truncation, reason = "intended")]
+		Ok(self.0 as i64)
+	}
+}
+impl PartialEq for NumValue {
+	fn eq(&self, other: &Self) -> bool {
+		self.0 == other.0
+	}
+}
+impl Eq for NumValue {}
+impl Ord for NumValue {
+	#[inline]
+	fn cmp(&self, other: &Self) -> Ordering {
+		// Can't use `total_cmp`: its behavior for `-0` and `0`
+		// is not following wanted.
+		unsafe { self.0.partial_cmp(&other.0).unwrap_unchecked() }
+	}
+}
+impl PartialOrd for NumValue {
+	#[inline]
+	fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
+		Some(self.cmp(other))
+	}
+}
+impl fmt::Debug for NumValue {
+	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+		fmt::Debug::fmt(&self.0, f)
+	}
+}
+impl fmt::Display for NumValue {
+	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+		fmt::Display::fmt(&self.0, f)
+	}
+}
+impl Deref for NumValue {
+	type Target = f64;
+
+	#[inline]
+	fn deref(&self) -> &Self::Target {
+		&self.0
+	}
+}
+macro_rules! impl_num {
+	($($ty:ty),+) => {$(
+		impl From<$ty> for NumValue {
+			#[inline]
+			fn from(value: $ty) -> Self {
+				Self(value.into())
+			}
+		}
+	)+};
+}
+impl_num!(i8, u8, i16, u16, i32, u32);
+
+#[derive(Clone, Copy, Debug, thiserror::Error, Acyclic)]
+pub enum ConvertNumValueError {
+	#[error("overflow")]
+	Overflow,
+	#[error("underflow")]
+	Underflow,
+	#[error("non-finite")]
+	NonFinite,
+	#[error("float out of safe int range")]
+	BitwiseSafeRange,
+}
+
+macro_rules! impl_try_num {
+	($($ty:ty),+) => {$(
+		impl TryFrom<$ty> for NumValue {
+			type Error = ConvertNumValueError;
+			#[inline]
+			fn try_from(value: $ty) -> Result<Self, ConvertNumValueError> {
+				#[expect(clippy::cast_precision_loss, reason = "precision loss is explicitly handled")]
+				let value = value as f64;
+				if value < MIN_SAFE_INTEGER {
+					return Err(ConvertNumValueError::Underflow)
+				} else if value > MAX_SAFE_INTEGER {
+					return Err(ConvertNumValueError::Overflow)
+				}
+				// Number is finite.
+				Ok(Self(value))
+			}
+		}
+	)+};
+}
+impl_try_num!(usize, isize, i64, u64);
+
+impl TryFrom<f64> for NumValue {
+	type Error = ConvertNumValueError;
+
+	#[inline]
+	fn try_from(value: f64) -> Result<Self, Self::Error> {
+		Self::new(value).ok_or(ConvertNumValueError::NonFinite)
+	}
+}
+impl TryFrom<f32> for NumValue {
+	type Error = ConvertNumValueError;
+
+	#[inline]
+	fn try_from(value: f32) -> Result<Self, Self::Error> {
+		Self::new(f64::from(value)).ok_or(ConvertNumValueError::NonFinite)
+	}
+}
modifiedcrates/jrsonnet-peg-parser/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-peg-parser/src/lib.rs
+++ b/crates/jrsonnet-peg-parser/src/lib.rs
@@ -4,8 +4,8 @@
 use jrsonnet_ir::{
 	ArgsDesc, AssertExpr, AssertStmt, BinaryOp, BindSpec, CompSpec, Destruct, DestructRest, Expr,
 	ExprParam, ExprParams, FieldMember, FieldName, ForSpecData, IStr, IfElse, IfSpecData,
-	ImportKind, IndexPart, LiteralType, Member, ObjBody, ObjComp, ObjMembers, Slice, SliceDesc,
-	Source, Span, Spanned, Visibility, unescape,
+	ImportKind, IndexPart, LiteralType, Member, NumValue, ObjBody, ObjComp, ObjMembers, Slice,
+	SliceDesc, Source, Span, Spanned, Visibility, unescape,
 };
 use peg::parser;
 
@@ -52,7 +52,7 @@
 		/// Sequence of digits
 		rule uint_str() -> &'input str = a:$(digit()+ ("_" digit()+)*) { a }
 		/// Number in scientific notation format
-		rule number() -> f64 = quiet!{a:$(uint_str() ("." uint_str())? (['e'|'E'] (s:['+'|'-'])? uint_str())?) {? a.replace("_","").parse().map_err(|_| "<number>") }} / expected!("<number>")
+		rule number() -> f64 = quiet!{a:$(uint_str() ("." uint_str())? (['e'|'E'] (s:['+'|'-'])? uint_str())?) {? a.replace('_',"").parse().map_err(|_| "<number>") }} / expected!("<number>")
 
 		/// Reserved word followed by any non-alphanumberic
 		rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "importbin" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()
@@ -267,7 +267,7 @@
 				Expr::ArrComp(Rc::new(expr), specs)
 			}
 		pub rule number_expr(s: &ParserSettings) -> Expr
-			= n:number() {? if n.is_finite() {
+			= n:number() {? if let Some(n) = NumValue::new(n) {
 				Ok(Expr::Num(n))
 			} else {
 				Err("!!!numbers are finite")
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
after · crates/jrsonnet-stdlib/src/lib.rs
1#![allow(clippy::similar_names)]23use std::{4	cell::{Ref, RefCell, RefMut},5	collections::HashMap,6	f64,7	rc::Rc,8};910pub use arrays::*;11pub use compat::*;12pub use encoding::*;13pub use hash::*;14use jrsonnet_evaluator::{15	ContextBuilder, IStr, NumValue, ObjValue, ObjValueBuilder, Thunk, Val,16	error::Result,17	function::{CallLocation, FuncVal, builtin_id},18	tla::TlaArg,19	trace::PathResolver,20	typed::SerializeTypedObj as _,21};22use jrsonnet_gcmodule::{Acyclic, Cc, Trace};23use jrsonnet_ir::Source;24use jrsonnet_macros::{IntoUntyped, Typed};25pub use manifest::*;26pub use math::*;27pub use misc::*;28pub use objects::*;29pub use operator::*;30pub use parse::*;31pub use sets::*;32pub use sort::*;33pub use strings::*;34pub use types::*;3536#[cfg(feature = "exp-regex")]37pub use crate::regex::*;3839mod arrays;40mod compat;41mod encoding;42mod hash;43mod keyf;44mod manifest;45mod math;46mod misc;47mod objects;48mod operator;49mod parse;50#[cfg(feature = "exp-regex")]51mod regex;52mod sets;53mod sort;54mod strings;55mod types;5657#[derive(Typed, IntoUntyped, Default)]58#[allow(non_snake_case)]59struct Builtins {60	#[typed(method)]61	id: builtin_id,62	// Types63	#[typed(method, rename = "type")]64	r#type: builtin_type,65	#[typed(method)]66	isString: builtin_is_string,67	#[typed(method)]68	isNumber: builtin_is_number,69	#[typed(method)]70	isBoolean: builtin_is_boolean,71	#[typed(method)]72	isObject: builtin_is_object,73	#[typed(method)]74	isArray: builtin_is_array,75	#[typed(method)]76	isFunction: builtin_is_function,77	#[typed(method)]78	isNull: builtin_is_null,79	// Arrays80	#[typed(method)]81	makeArray: builtin_make_array,82	#[typed(method)]83	repeat: builtin_repeat,84	#[typed(method)]85	slice: builtin_slice,86	#[typed(method)]87	map: builtin_map,88	#[typed(method)]89	mapWithIndex: builtin_map_with_index,90	#[typed(method)]91	mapWithKey: builtin_map_with_key,92	#[typed(method)]93	flatMap: builtin_flatmap,94	#[typed(method)]95	filter: builtin_filter,96	#[typed(method)]97	foldl: builtin_foldl,98	#[typed(method)]99	foldr: builtin_foldr,100	#[typed(method)]101	range: builtin_range,102	#[typed(method)]103	join: builtin_join,104	#[typed(method)]105	lines: builtin_lines,106	#[typed(method)]107	resolvePath: builtin_resolve_path,108	#[typed(method)]109	deepJoin: builtin_deep_join,110	#[typed(method)]111	reverse: builtin_reverse,112	#[typed(method)]113	any: builtin_any,114	#[typed(method)]115	all: builtin_all,116	#[typed(method)]117	member: builtin_member,118	#[typed(method)]119	find: builtin_find,120	#[typed(method)]121	contains: builtin_contains,122	#[typed(method)]123	count: builtin_count,124	#[typed(method)]125	avg: builtin_avg,126	#[typed(method)]127	removeAt: builtin_remove_at,128	#[typed(method)]129	remove: builtin_remove,130	#[typed(method)]131	flattenArrays: builtin_flatten_arrays,132	#[typed(method)]133	flattenDeepArray: builtin_flatten_deep_array,134	#[typed(method)]135	prune: builtin_prune,136	#[typed(method)]137	filterMap: builtin_filter_map,138	// Math139	#[typed(method)]140	abs: builtin_abs,141	#[typed(method)]142	sign: builtin_sign,143	#[typed(method)]144	max: builtin_max,145	#[typed(method)]146	min: builtin_min,147	#[typed(method)]148	clamp: builtin_clamp,149	#[typed(method)]150	sum: builtin_sum,151	#[typed(method)]152	modulo: builtin_modulo,153	#[typed(method)]154	floor: builtin_floor,155	#[typed(method)]156	ceil: builtin_ceil,157	#[typed(method)]158	log: builtin_log,159	#[typed(method)]160	log2: builtin_log2,161	#[typed(method)]162	log10: builtin_log10,163	#[typed(method)]164	pow: builtin_pow,165	#[typed(method)]166	sqrt: builtin_sqrt,167	#[typed(method)]168	sin: builtin_sin,169	#[typed(method)]170	cos: builtin_cos,171	#[typed(method)]172	tan: builtin_tan,173	#[typed(method)]174	asin: builtin_asin,175	#[typed(method)]176	acos: builtin_acos,177	#[typed(method)]178	atan: builtin_atan,179	#[typed(method)]180	atan2: builtin_atan2,181	#[typed(method)]182	exp: builtin_exp,183	#[typed(method)]184	mantissa: builtin_mantissa,185	#[typed(method)]186	exponent: builtin_exponent,187	#[typed(method)]188	round: builtin_round,189	#[typed(method)]190	isEven: builtin_is_even,191	#[typed(method)]192	isOdd: builtin_is_odd,193	#[typed(method)]194	isInteger: builtin_is_integer,195	#[typed(method)]196	isDecimal: builtin_is_decimal,197	#[typed(method)]198	deg2rad: builtin_deg2rad,199	#[typed(method)]200	rad2deg: builtin_rad2deg,201	#[typed(method)]202	hypot: builtin_hypot,203	// Operator204	#[typed(rename = "mod", method)]205	r#mod: builtin_mod,206	#[typed(method)]207	primitiveEquals: builtin_primitive_equals,208	#[typed(method)]209	equals: builtin_equals,210	#[typed(method)]211	xor: builtin_xor,212	#[typed(method)]213	xnor: builtin_xnor,214	#[typed(method)]215	format: builtin_format,216	// Sort217	#[typed(method)]218	sort: builtin_sort,219	#[typed(method)]220	uniq: builtin_uniq,221	#[typed(method)]222	set: builtin_set,223	#[typed(method)]224	minArray: builtin_min_array,225	#[typed(method)]226	maxArray: builtin_max_array,227	// Hash228	#[typed(method)]229	md5: builtin_md5,230	#[typed(method)]231	sha1: builtin_sha1,232	#[typed(method)]233	sha256: builtin_sha256,234	#[typed(method)]235	sha512: builtin_sha512,236	#[typed(method)]237	sha3: builtin_sha3,238	// Encoding239	#[typed(method)]240	encodeUTF8: builtin_encode_utf8,241	#[typed(method)]242	decodeUTF8: builtin_decode_utf8,243	#[typed(method)]244	base64: builtin_base64,245	#[typed(method)]246	base64Decode: builtin_base64_decode,247	#[typed(method)]248	base64DecodeBytes: builtin_base64_decode_bytes,249	// Objects250	#[typed(method)]251	objectFieldsEx: builtin_object_fields_ex,252	#[typed(method)]253	objectFields: builtin_object_fields,254	#[typed(method)]255	objectFieldsAll: builtin_object_fields_all,256	#[typed(method)]257	objectValues: builtin_object_values,258	#[typed(method)]259	objectValuesAll: builtin_object_values_all,260	#[typed(method)]261	objectKeysValues: builtin_object_keys_values,262	#[typed(method)]263	objectKeysValuesAll: builtin_object_keys_values_all,264	#[typed(method)]265	objectHasEx: builtin_object_has_ex,266	#[typed(method)]267	objectHas: builtin_object_has,268	#[typed(method)]269	objectHasAll: builtin_object_has_all,270	#[typed(method)]271	objectRemoveKey: builtin_object_remove_key,272	// Manifest273	#[typed(method)]274	escapeStringJson: builtin_escape_string_json,275	#[typed(method)]276	escapeStringPython: builtin_escape_string_python,277	#[typed(method)]278	escapeStringXML: builtin_escape_string_xml,279	#[typed(method)]280	manifestJsonEx: builtin_manifest_json_ex,281	#[typed(method)]282	manifestJson: builtin_manifest_json,283	#[typed(method)]284	manifestJsonMinified: builtin_manifest_json_minified,285	#[typed(method)]286	manifestYamlDoc: builtin_manifest_yaml_doc,287	#[typed(method)]288	manifestYamlStream: builtin_manifest_yaml_stream,289	#[typed(method)]290	manifestTomlEx: builtin_manifest_toml_ex,291	#[typed(method)]292	manifestToml: builtin_manifest_toml,293	#[typed(method)]294	toString: builtin_to_string,295	#[typed(method)]296	manifestPython: builtin_manifest_python,297	#[typed(method)]298	manifestPythonVars: builtin_manifest_python_vars,299	#[typed(method)]300	manifestXmlJsonml: builtin_manifest_xml_jsonml,301	#[typed(method)]302	manifestIni: builtin_manifest_ini,303	// Parse304	#[typed(method)]305	parseJson: builtin_parse_json,306	#[typed(method)]307	parseYaml: builtin_parse_yaml,308	// Strings309	#[typed(method)]310	codepoint: builtin_codepoint,311	#[typed(method)]312	substr: builtin_substr,313	#[typed(method)]314	char: builtin_char,315	#[typed(method)]316	strReplace: builtin_str_replace,317	#[typed(method)]318	escapeStringBash: builtin_escape_string_bash,319	#[typed(method)]320	escapeStringDollars: builtin_escape_string_dollars,321	#[typed(method)]322	isEmpty: builtin_is_empty,323	#[typed(method)]324	equalsIgnoreCase: builtin_equals_ignore_case,325	#[typed(method)]326	splitLimit: builtin_splitlimit,327	#[typed(method)]328	splitLimitR: builtin_splitlimitr,329	#[typed(method)]330	split: builtin_split,331	#[typed(method)]332	asciiUpper: builtin_ascii_upper,333	#[typed(method)]334	asciiLower: builtin_ascii_lower,335	#[typed(method)]336	findSubstr: builtin_find_substr,337	#[typed(method)]338	parseInt: builtin_parse_int,339	#[cfg(feature = "exp-bigint")]340	#[typed(method)]341	bigint: builtin_bigint,342	#[typed(method)]343	parseOctal: builtin_parse_octal,344	#[typed(method)]345	parseHex: builtin_parse_hex,346	#[typed(method)]347	stringChars: builtin_string_chars,348	#[typed(method)]349	lstripChars: builtin_lstrip_chars,350	#[typed(method)]351	rstripChars: builtin_rstrip_chars,352	#[typed(method)]353	stripChars: builtin_strip_chars,354	#[typed(method)]355	trim: builtin_trim,356	// Misc357	#[typed(method)]358	length: builtin_length,359	#[typed(method)]360	get: builtin_get,361	#[typed(method)]362	startsWith: builtin_starts_with,363	#[typed(method)]364	endsWith: builtin_ends_with,365	#[typed(method)]366	assertEqual: builtin_assert_equal,367	#[typed(method)]368	mergePatch: builtin_merge_patch,369	// Sets370	#[typed(method)]371	setMember: builtin_set_member,372	#[typed(method)]373	setInter: builtin_set_inter,374	#[typed(method)]375	setDiff: builtin_set_diff,376	#[typed(method)]377	setUnion: builtin_set_union,378	// Regex379	#[cfg(feature = "exp-regex")]380	#[typed(method)]381	regexQuoteMeta: builtin_regex_quote_meta,382	// Compat383	#[typed(method)]384	__compare: builtin___compare,385	#[typed(method)]386	__compare_array: builtin___compare_array,387	#[typed(method)]388	__array_less: builtin___array_less,389	#[typed(method)]390	__array_greater: builtin___array_greater,391	#[typed(method)]392	__array_less_or_equal: builtin___array_less_or_equal,393	#[typed(method)]394	__array_greater_or_equal: builtin___array_greater_or_equal,395}396397#[allow(clippy::too_many_lines)]398pub fn stdlib_uncached(settings: Cc<RefCell<Settings>>) -> ObjValue {399	let mut builder = ObjValueBuilder::new();400401	let builtins = Builtins::default();402	builtins.serialize(&mut builder).expect("no conflicts");403404	builder.method(405		"extVar",406		builtin_ext_var {407			settings: settings.clone(),408		},409	);410	builder.method(411		"native",412		builtin_native {413			settings: settings.clone(),414		},415	);416	builder.method("trace", builtin_trace { settings });417418	builder.field("pi").hide().value(Val::Num(419		NumValue::new(f64::consts::PI).expect("pi is finite"),420	));421422	#[cfg(feature = "exp-regex")]423	{424		// Regex425		let regex_cache = RegexCache::default();426		builder.method(427			"regexFullMatch",428			builtin_regex_full_match {429				cache: regex_cache.clone(),430			},431		);432		builder.method(433			"regexPartialMatch",434			builtin_regex_partial_match {435				cache: regex_cache.clone(),436			},437		);438		builder.method(439			"regexReplace",440			builtin_regex_replace {441				cache: regex_cache.clone(),442			},443		);444		builder.method(445			"regexGlobalReplace",446			builtin_regex_global_replace { cache: regex_cache },447		);448	};449450	builder.build()451}452453pub trait TracePrinter: Acyclic {454	fn print_trace(&self, loc: CallLocation, value: IStr);455}456457#[derive(Acyclic)]458pub struct StdTracePrinter {459	resolver: PathResolver,460}461impl StdTracePrinter {462	pub fn new(resolver: PathResolver) -> Self {463		Self { resolver }464	}465}466impl TracePrinter for StdTracePrinter {467	fn print_trace(&self, loc: CallLocation, value: IStr) {468		eprint!("TRACE:");469		if let Some(loc) = loc.0 {470			let locs = loc.0.map_source_locations(&[loc.1]);471			eprint!(472				" {}:{}",473				loc.0.source_path().path().map_or_else(474					|| loc.0.source_path().to_string(),475					|p| self.resolver.resolve(p)476				),477				locs[0].line478			);479		}480		eprintln!(" {value}");481	}482}483484#[derive(Clone, Trace)]485pub struct Settings {486	/// Used for `std.extVar`487	pub ext_vars: HashMap<IStr, TlaArg>,488	/// Used for `std.native`489	pub ext_natives: HashMap<IStr, FuncVal>,490	/// Used for `std.trace`491	pub trace_printer: Rc<dyn TracePrinter>,492	/// Used for `std.thisFile`493	pub path_resolver: PathResolver,494}495496#[derive(Trace, Clone)]497pub struct ContextInitializer {498	/// std without applied thisFile overlay499	stdlib_obj: ObjValue,500	settings: Cc<RefCell<Settings>>,501}502impl ContextInitializer {503	pub fn new(resolver: PathResolver) -> Self {504		let settings = Settings {505			ext_vars: HashMap::new(),506			ext_natives: HashMap::new(),507			trace_printer: Rc::new(StdTracePrinter::new(resolver.clone())),508			path_resolver: resolver,509		};510		let settings = Cc::new(RefCell::new(settings));511		let stdlib_obj = stdlib_uncached(settings.clone());512		Self {513			stdlib_obj,514			settings,515		}516	}517	pub fn settings(&self) -> Ref<'_, Settings> {518		self.settings.borrow()519	}520	pub fn settings_mut(&self) -> RefMut<'_, Settings> {521		self.settings.borrow_mut()522	}523	pub fn add_ext_var(&self, name: IStr, value: Val) {524		self.settings_mut()525			.ext_vars526			.insert(name, TlaArg::Val(value));527	}528	pub fn add_ext_str(&self, name: IStr, value: IStr) {529		self.settings_mut()530			.ext_vars531			.insert(name, TlaArg::String(value));532	}533	pub fn add_ext_code(&self, name: &str, code: impl AsRef<str>) -> Result<()> {534		// self.data_mut().volatile_files.insert(source_name, code);535		self.settings_mut()536			.ext_vars537			.insert(name.into(), TlaArg::InlineCode(code.as_ref().to_owned()));538		Ok(())539	}540	pub fn add_native(&self, name: impl Into<IStr>, cb: impl Into<FuncVal>) {541		self.settings_mut()542			.ext_natives543			.insert(name.into(), cb.into());544	}545}546impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {547	fn populate(&self, source: Source, builder: &mut ContextBuilder) {548		let mut std = ObjValueBuilder::new();549		std.with_super(self.stdlib_obj.clone());550		std.field("thisFile").hide().value({551			let source_path = source.source_path();552			source_path.path().map_or_else(553				|| source_path.to_string(),554				|p| self.settings().path_resolver.resolve(p),555			)556		});557		let stdlib_with_this_file = std.build();558559		builder.bind("std", Thunk::evaluated(Val::Obj(stdlib_with_this_file)));560	}561	fn as_any(&self) -> &dyn std::any::Any {562		self563	}564}
modifiedcrates/jrsonnet-stdlib/src/operator.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/operator.rs
+++ b/crates/jrsonnet-stdlib/src/operator.rs
@@ -2,12 +2,12 @@
 //! 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, Result, Val,
+	IStr, NumValue, Result, Val,
 	function::builtin,
 	operator::evaluate_mod_op,
 	stdlib::std_format,
 	typed::{Either, Either2},
-	val::{NumValue, equals, primitive_equals},
+	val::{equals, primitive_equals},
 };
 
 #[builtin]