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
before · crates/jrsonnet-evaluator/src/val.rs
1use std::{2	cell::RefCell,3	cmp::Ordering,4	fmt::{self, Debug, Display},5	marker::PhantomData,6	mem::replace,7	num::NonZeroU32,8	ops::Deref,9	rc::Rc,10};1112use jrsonnet_gcmodule::{Acyclic, Cc, Trace, cc_dyn};13use jrsonnet_interner::IStr;14pub use jrsonnet_macros::Thunk;15use jrsonnet_types::ValType;16use rustc_hash::FxHashMap;17use thiserror::Error;1819pub use crate::arr::{ArrValue, ArrayLike};20use crate::{21	ObjValue, Result, SupThis, Unbound, WeakSupThis, bail,22	error::{Error, ErrorKind::*},23	function::FuncVal,24	gc::WithCapacityExt as _,25	manifest::{ManifestFormat, ToStringFormat},26	typed::{BoundedUsize, MAX_SAFE_INTEGER, MIN_SAFE_INTEGER},27};2829pub trait ThunkValue: Trace {30	type Output;31	fn get(&self) -> Result<Self::Output>;32}3334#[derive(Trace)]35enum MemoizedClusureThunkInner<D: Trace, T: Trace> {36	Computed(T),37	Errored(Error),38	Waiting {39		env: D,40		// Carries no data, as it is not a real closure, all the41		// captured environment is stored in `env` field.42		#[trace(skip)]43		closure: fn(D) -> Result<T>,44	},45	Pending,46}47#[derive(Trace)]48pub struct MemoizedClosureThunk<D: Trace, T: Trace>(RefCell<MemoizedClusureThunkInner<D, T>>);49impl<D: Trace, T: Trace> MemoizedClosureThunk<D, T> {50	pub fn new(env: D, closure: fn(D) -> Result<T>) -> Self {51		Self(RefCell::new(MemoizedClusureThunkInner::Waiting {52			env,53			closure,54		}))55	}56}5758impl<D: Trace, T: Trace + Clone> ThunkValue for MemoizedClosureThunk<D, T> {59	type Output = T;6061	fn get(&self) -> Result<Self::Output> {62		match &*self.0.borrow() {63			MemoizedClusureThunkInner::Computed(v) => return Ok(v.clone()),64			MemoizedClusureThunkInner::Errored(e) => return Err(e.clone()),65			MemoizedClusureThunkInner::Pending => return Err(InfiniteRecursionDetected.into()),66			MemoizedClusureThunkInner::Waiting { .. } => (),67		}68		let MemoizedClusureThunkInner::Waiting { env, closure } = replace(69			&mut *self.0.borrow_mut(),70			MemoizedClusureThunkInner::Pending,71		) else {72			unreachable!();73		};74		let new_value = match closure(env) {75			Ok(v) => v,76			Err(e) => {77				*self.0.borrow_mut() = MemoizedClusureThunkInner::Errored(e.clone());78				return Err(e);79			}80		};81		*self.0.borrow_mut() = MemoizedClusureThunkInner::Computed(new_value.clone());82		Ok(new_value)83	}84}8586cc_dyn!(87	/// Lazily evaluated value88	#[derive(Clone)] Thunk<V: Trace>,89	ThunkValue<Output = V>,90	pub fn new() {...}91);9293impl<T: Trace> Thunk<T> {94	pub fn evaluated(val: T) -> Self95	where96		T: Clone,97	{98		#[derive(Trace)]99		struct EvaluatedThunk<T: Trace>(T);100		impl<T> ThunkValue for EvaluatedThunk<T>101		where102			T: Clone + Trace,103		{104			type Output = T;105106			fn get(&self) -> Result<Self::Output> {107				Ok(self.0.clone())108			}109		}110		Self::new(EvaluatedThunk(val))111	}112	pub fn errored(e: Error) -> Self {113		#[derive(Trace)]114		struct ErroredThunk<T: Trace>(Error, PhantomData<T>);115		impl<T> ThunkValue for ErroredThunk<T>116		where117			T: Trace,118		{119			type Output = T;120121			fn get(&self) -> Result<Self::Output> {122				Err(self.0.clone())123			}124		}125		Self::new(ErroredThunk(e, PhantomData))126	}127	pub fn result(res: Result<T, Error>) -> Self128	where129		T: Clone,130	{131		match res {132			Ok(o) => Self::evaluated(o),133			Err(e) => Self::errored(e),134		}135	}136}137138impl<T> Thunk<T>139where140	T: Trace,141{142	pub fn force(&self) -> Result<()> {143		self.evaluate()?;144		Ok(())145	}146147	/// Evaluate thunk, or return cached value148	///149	/// # Errors150	///151	/// - Lazy value evaluation returned error152	/// - This method was called during inner value evaluation153	pub fn evaluate(&self) -> Result<T> {154		self.0.get()155	}156}157158pub trait ThunkMapper<Input>: Trace {159	type Output;160	fn map(self, from: Input) -> Result<Self::Output>;161}162impl<Input> Thunk<Input>163where164	Input: Trace,165{166	pub fn map<M>(self, mapper: M) -> Thunk<M::Output>167	where168		M: ThunkMapper<Input>,169		M::Output: Trace + Clone,170	{171		let inner = self;172		Thunk!(move || {173			let value = inner.evaluate()?;174			let mapped = mapper.map(value)?;175			Ok(mapped)176		})177	}178}179180impl<T: Trace + Clone> From<Result<T>> for Thunk<T> {181	fn from(value: Result<T>) -> Self {182		match value {183			Ok(o) => Self::evaluated(o),184			Err(e) => Self::errored(e),185		}186	}187}188impl<T, V: Trace> From<T> for Thunk<V>189where190	T: ThunkValue<Output = V>,191{192	fn from(value: T) -> Self {193		Self::new(value)194	}195}196197impl<T: Trace + Default + Clone> Default for Thunk<T> {198	fn default() -> Self {199		Self::evaluated(T::default())200	}201}202203#[derive(Trace, Clone)]204pub struct CachedUnbound<I, T>205where206	I: Unbound<Bound = T>,207	T: Trace,208{209	cache: Cc<RefCell<FxHashMap<WeakSupThis, T>>>,210	value: I,211}212impl<I: Unbound<Bound = T>, T: Trace> CachedUnbound<I, T> {213	pub fn new(value: I) -> Self {214		Self {215			cache: Cc::new(RefCell::new(FxHashMap::new())),216			value,217		}218	}219}220impl<I: Unbound<Bound = T>, T: Clone + Trace> Unbound for CachedUnbound<I, T> {221	type Bound = T;222	fn bind(&self, sup_this: SupThis) -> Result<T> {223		let cache_key = sup_this.clone().downgrade();224		{225			if let Some(t) = self.cache.borrow().get(&cache_key) {226				return Ok(t.clone());227			}228		}229		let bound = self.value.bind(sup_this)?;230231		{232			let mut cache = self.cache.borrow_mut();233			cache.insert(cache_key, bound.clone());234		}235236		Ok(bound)237	}238}239240impl<T: Debug + Trace> Debug for Thunk<T> {241	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {242		write!(f, "Lazy")243	}244}245impl<T: Trace> PartialEq for Thunk<T> {246	fn eq(&self, other: &Self) -> bool {247		Cc::ptr_eq(&self.0, &other.0)248	}249}250251/// Represents a Jsonnet value, which can be sliced or indexed (string or array).252#[allow(clippy::module_name_repetitions)]253pub enum IndexableVal {254	/// String.255	Str(IStr),256	/// Array.257	Arr(ArrValue),258}259impl IndexableVal {260	pub fn is_empty(&self) -> bool {261		match self {262			Self::Str(s) => s.is_empty(),263			Self::Arr(s) => s.is_empty(),264		}265	}266267	pub fn to_array(self) -> ArrValue {268		match self {269			Self::Str(s) => s.chars().collect(),270			Self::Arr(arr) => arr,271		}272	}273	/// Slice the value.274	///275	/// # Implementation276	///277	/// For strings, will create a copy of specified interval.278	///279	/// For arrays, nothing will be copied on this call, instead [`ArrValue::Slice`] view will be returned.280	pub fn slice(281		self,282		index: Option<i32>,283		end: Option<i32>,284		step: Option<BoundedUsize<1, { i32::MAX as usize }>>,285	) -> Result<Self> {286		match &self {287			Self::Str(s) => {288				let mut computed_len = None;289				let mut get_len = || {290					computed_len.unwrap_or_else(|| {291						let len = s.chars().count();292						let _ = computed_len.insert(len);293						len294					})295				};296				let mut get_idx = |pos: Option<i32>, default| {297					match pos {298						#[expect(clippy::cast_sign_loss, reason = "abs value is used")]299						Some(v) if v < 0 => get_len().saturating_sub((-v as isize) as usize),300						// No need to clamp, as iterator interface is used301						#[expect(clippy::cast_sign_loss, reason = "abs value is used")]302						Some(v) => v as usize,303						None => default,304					}305				};306307				let index = get_idx(index, 0);308				let end = get_idx(end, usize::MAX);309				let step = step.as_deref().copied().unwrap_or(1);310311				if index >= end {312					return Ok(Self::Str("".into()));313				}314315				Ok(Self::Str(316					(s.chars()317						.skip(index)318						.take(end - index)319						.step_by(step)320						.collect::<String>())321					.into(),322				))323			}324			Self::Arr(arr) => Ok(Self::Arr(arr.clone().slice(325				index,326				end,327				#[expect(328					clippy::cast_possible_truncation,329					reason = "overflow will result with skip too large which would be equivalent"330				)]331				step.map(|v| NonZeroU32::new(v.value() as u32).expect("bounded != 0")),332			))),333		}334	}335}336337#[derive(Debug, Clone, Acyclic)]338pub enum StrValue {339	Flat(IStr),340	Tree(Rc<(StrValue, StrValue, usize)>),341}342impl StrValue {343	pub fn concat(a: Self, b: Self) -> Self {344		// TODO: benchmark for an optimal value, currently just a arbitrary choice345		const STRING_EXTEND_THRESHOLD: usize = 100;346347		if a.is_empty() {348			b349		} else if b.is_empty() {350			a351		} else if a.len() + b.len() < STRING_EXTEND_THRESHOLD {352			Self::Flat(format!("{a}{b}").into())353		} else {354			let len = a.len() + b.len();355			Self::Tree(Rc::new((a, b, len)))356		}357	}358	pub fn chunks(&self, c: &mut impl FnMut(&IStr)) {359		fn write_buf(s: &StrValue, c: &mut impl FnMut(&IStr)) {360			match s {361				StrValue::Flat(f) => c(f),362				StrValue::Tree(t) => {363					write_buf(&t.0, c);364					write_buf(&t.1, c);365				}366			}367		}368		write_buf(self, c);369	}370	pub fn into_flat(&self) -> IStr {371		fn write_buf(s: &StrValue, out: &mut String) {372			match s {373				StrValue::Flat(f) => out.push_str(f),374				StrValue::Tree(t) => {375					write_buf(&t.0, out);376					write_buf(&t.1, out);377				}378			}379		}380		match self {381			Self::Flat(f) => f.clone(),382			Self::Tree(_) => {383				let mut buf = String::with_capacity(self.len());384				write_buf(self, &mut buf);385				buf.into()386			}387		}388	}389	pub fn len(&self) -> usize {390		match self {391			Self::Flat(v) => v.len(),392			Self::Tree(t) => t.2,393		}394	}395	pub fn is_empty(&self) -> bool {396		match self {397			Self::Flat(v) => v.is_empty(),398			// Can't create non-flat empty string399			Self::Tree(_) => false,400		}401	}402}403impl<T> From<T> for StrValue404where405	IStr: From<T>,406{407	fn from(value: T) -> Self {408		Self::Flat(IStr::from(value))409	}410}411impl Display for StrValue {412	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {413		match self {414			Self::Flat(v) => write!(f, "{v}"),415			Self::Tree(t) => {416				write!(f, "{}", t.0)?;417				write!(f, "{}", t.1)418			}419		}420	}421}422impl PartialEq for StrValue {423	// False positive, into_flat returns not StrValue, but IStr, thus no infinite recursion here.424	#[allow(clippy::unconditional_recursion)]425	fn eq(&self, other: &Self) -> bool {426		let a = self.clone().into_flat();427		let b = other.clone().into_flat();428		a == b429	}430}431impl Eq for StrValue {}432impl PartialOrd for StrValue {433	fn partial_cmp(&self, other: &Self) -> Option<Ordering> {434		Some(self.cmp(other))435	}436}437impl Ord for StrValue {438	fn cmp(&self, other: &Self) -> Ordering {439		let a = self.clone().into_flat();440		let b = other.clone().into_flat();441		a.cmp(&b)442	}443}444445/// Represents jsonnet number446/// Jsonnet numbers are finite f64, with NaNs disallowed447#[derive(Trace, Clone, Copy)]448#[repr(transparent)]449pub struct NumValue(f64);450impl NumValue {451	/// Creates a [`NumValue`], if value is finite and not NaN452	pub fn new(v: f64) -> Option<Self> {453		if !v.is_finite() {454			return None;455		}456		Some(Self(v))457	}458	#[inline]459	pub const fn get(&self) -> f64 {460		self.0461	}462	pub(crate) fn truncate_for_bitwise(self) -> Result<i64> {463		if self.0 < MIN_SAFE_INTEGER || self.0 > MAX_SAFE_INTEGER {464			bail!("numberic value outside of safe integer range for bitwise operation");465		}466		#[expect(clippy::cast_possible_truncation, reason = "intended")]467		Ok(self.0 as i64)468	}469}470impl PartialEq for NumValue {471	fn eq(&self, other: &Self) -> bool {472		self.0 == other.0473	}474}475impl Eq for NumValue {}476impl Ord for NumValue {477	#[inline]478	fn cmp(&self, other: &Self) -> Ordering {479		// Can't use `total_cmp`: its behavior for `-0` and `0`480		// is not following wanted.481		unsafe { self.0.partial_cmp(&other.0).unwrap_unchecked() }482	}483}484impl PartialOrd for NumValue {485	#[inline]486	fn partial_cmp(&self, other: &Self) -> Option<Ordering> {487		Some(self.cmp(other))488	}489}490impl Debug for NumValue {491	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {492		Debug::fmt(&self.0, f)493	}494}495impl Display for NumValue {496	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {497		Display::fmt(&self.0, f)498	}499}500impl Deref for NumValue {501	type Target = f64;502503	#[inline]504	fn deref(&self) -> &Self::Target {505		&self.0506	}507}508macro_rules! impl_num {509	($($ty:ty),+) => {$(510		impl From<$ty> for NumValue {511			#[inline]512			fn from(value: $ty) -> Self {513				Self(value.into())514			}515		}516	)+};517}518impl_num!(i8, u8, i16, u16, i32, u32);519520#[derive(Clone, Copy, Debug, Error, Trace)]521pub enum ConvertNumValueError {522	#[error("overflow")]523	Overflow,524	#[error("underflow")]525	Underflow,526	#[error("non-finite")]527	NonFinite,528}529impl From<ConvertNumValueError> for Error {530	fn from(e: ConvertNumValueError) -> Self {531		Self::new(e.into())532	}533}534535macro_rules! impl_try_num {536	($($ty:ty),+) => {$(537		impl TryFrom<$ty> for NumValue {538			type Error = ConvertNumValueError;539			#[inline]540			fn try_from(value: $ty) -> Result<Self, ConvertNumValueError> {541				#[expect(clippy::cast_precision_loss, reason = "precision loss is explicitly handled")]542				let value = value as f64;543				if value < MIN_SAFE_INTEGER {544					return Err(ConvertNumValueError::Underflow)545				} else if value > MAX_SAFE_INTEGER {546					return Err(ConvertNumValueError::Overflow)547				}548				// Number is finite.549				Ok(Self(value))550			}551		}552	)+};553}554impl_try_num!(usize, isize, i64, u64);555556impl TryFrom<f64> for NumValue {557	type Error = ConvertNumValueError;558559	#[inline]560	fn try_from(value: f64) -> Result<Self, Self::Error> {561		Self::new(value).ok_or(ConvertNumValueError::NonFinite)562	}563}564impl TryFrom<f32> for NumValue {565	type Error = ConvertNumValueError;566567	#[inline]568	fn try_from(value: f32) -> Result<Self, Self::Error> {569		Self::new(f64::from(value)).ok_or(ConvertNumValueError::NonFinite)570	}571}572573/// Represents any valid Jsonnet value.574#[derive(Debug, Clone, Trace, Default)]575pub enum Val {576	/// Represents a Jsonnet boolean.577	Bool(bool),578	/// Represents a Jsonnet null value.579	#[default]580	Null,581	/// Represents a Jsonnet string.582	Str(StrValue),583	/// Represents a Jsonnet number.584	/// Should be finite, and not NaN585	/// This restriction isn't enforced by enum, as enum field can't be marked as private586	Num(NumValue),587	/// Experimental bigint588	#[cfg(feature = "exp-bigint")]589	BigInt(#[trace(skip)] Box<num_bigint::BigInt>),590	/// Represents a Jsonnet array.591	Arr(ArrValue),592	/// Represents a Jsonnet object.593	Obj(ObjValue),594	/// Represents a Jsonnet function.595	Func(FuncVal),596}597598#[cfg(target_pointer_width = "64")]599static_assertions::assert_eq_size!(Val, [u8; 24]);600601impl From<IndexableVal> for Val {602	fn from(v: IndexableVal) -> Self {603		match v {604			IndexableVal::Str(s) => Self::string(s),605			IndexableVal::Arr(a) => Self::Arr(a),606		}607	}608}609610impl Val {611	pub const fn as_bool(&self) -> Option<bool> {612		match self {613			Self::Bool(v) => Some(*v),614			_ => None,615		}616	}617	pub const fn as_null(&self) -> Option<()> {618		match self {619			Self::Null => Some(()),620			_ => None,621		}622	}623	pub fn as_str(&self) -> Option<IStr> {624		match self {625			Self::Str(s) => Some(s.clone().into_flat()),626			_ => None,627		}628	}629	pub const fn as_num(&self) -> Option<f64> {630		match self {631			Self::Num(n) => Some(n.get()),632			_ => None,633		}634	}635	#[cfg(feature = "exp-bigint")]636	pub fn as_bigint(&self) -> Option<num_bigint::BigInt> {637		match self {638			Self::BigInt(n) => Some(*n.clone()),639			_ => None,640		}641	}642	pub fn as_arr(&self) -> Option<ArrValue> {643		match self {644			Self::Arr(a) => Some(a.clone()),645			_ => None,646		}647	}648	pub fn as_obj(&self) -> Option<ObjValue> {649		match self {650			Self::Obj(o) => Some(o.clone()),651			_ => None,652		}653	}654	pub fn as_func(&self) -> Option<FuncVal> {655		match self {656			Self::Func(f) => Some(f.clone()),657			_ => None,658		}659	}660661	pub const fn value_type(&self) -> ValType {662		match self {663			Self::Str(..) => ValType::Str,664			Self::Num(..) => ValType::Num,665			#[cfg(feature = "exp-bigint")]666			Self::BigInt(..) => ValType::BigInt,667			Self::Arr(..) => ValType::Arr,668			Self::Obj(..) => ValType::Obj,669			Self::Bool(_) => ValType::Bool,670			Self::Null => ValType::Null,671			Self::Func(..) => ValType::Func,672		}673	}674675	pub fn manifest(&self, format: impl ManifestFormat) -> Result<String> {676		fn manifest_dyn(val: &Val, manifest: &dyn ManifestFormat) -> Result<String> {677			manifest.manifest(val.clone())678		}679		manifest_dyn(self, &format)680	}681682	pub fn to_string(&self) -> Result<IStr> {683		Ok(match self {684			Self::Bool(true) => "true".into(),685			Self::Bool(false) => "false".into(),686			Self::Null => "null".into(),687			Self::Str(s) => s.clone().into_flat(),688			_ => self.manifest(ToStringFormat).map(IStr::from)?,689		})690	}691692	pub fn into_indexable(self) -> Result<IndexableVal> {693		Ok(match self {694			Self::Str(s) => IndexableVal::Str(s.into_flat()),695			Self::Arr(arr) => IndexableVal::Arr(arr),696			_ => bail!(ValueIsNotIndexable(self.value_type())),697		})698	}699700	pub fn function(function: impl Into<FuncVal>) -> Self {701		Self::Func(function.into())702	}703	pub fn string(string: impl Into<StrValue>) -> Self {704		Self::Str(string.into())705	}706	pub fn num(num: impl Into<NumValue>) -> Self {707		Self::Num(num.into())708	}709	pub fn try_num<V, E>(num: V) -> Result<Self, E>710	where711		NumValue: TryFrom<V, Error = E>,712	{713		Ok(Self::Num(num.try_into()?))714	}715	pub fn arr(a: impl ArrayLike) -> Self {716		Self::Arr(ArrValue::new(a))717	}718}719720impl From<IStr> for Val {721	fn from(value: IStr) -> Self {722		Self::string(value)723	}724}725impl From<String> for Val {726	fn from(value: String) -> Self {727		Self::string(value)728	}729}730impl From<&str> for Val {731	fn from(value: &str) -> Self {732		Self::string(value)733	}734}735impl From<ObjValue> for Val {736	fn from(value: ObjValue) -> Self {737		Self::Obj(value)738	}739}740741const fn is_function_like(val: &Val) -> bool {742	matches!(val, Val::Func(_))743}744745/// Native implementation of `std.primitiveEquals`746pub fn primitive_equals(val_a: &Val, val_b: &Val) -> Result<bool> {747	Ok(match (val_a, val_b) {748		(Val::Bool(a), Val::Bool(b)) => a == b,749		(Val::Null, Val::Null) => true,750		(Val::Str(a), Val::Str(b)) => a == b,751		(Val::Num(a), Val::Num(b)) => (a.get() - b.get()).abs() <= f64::EPSILON,752		#[cfg(feature = "exp-bigint")]753		(Val::BigInt(a), Val::BigInt(b)) => a == b,754		(Val::Arr(_), Val::Arr(_)) => {755			bail!("primitiveEquals operates on primitive types, got array")756		}757		(Val::Obj(_), Val::Obj(_)) => {758			bail!("primitiveEquals operates on primitive types, got object")759		}760		(a, b) if is_function_like(a) && is_function_like(b) => {761			bail!("cannot test equality of functions")762		}763		(_, _) => false,764	})765}766767/// Native implementation of `std.equals`768pub fn equals(val_a: &Val, val_b: &Val) -> Result<bool> {769	if val_a.value_type() != val_b.value_type() {770		return Ok(false);771	}772	match (val_a, val_b) {773		(Val::Arr(a), Val::Arr(b)) => {774			if ArrValue::ptr_eq(a, b) {775				return Ok(true);776			}777			if a.len() != b.len() {778				return Ok(false);779			}780			for (a, b) in a.iter().zip(b.iter()) {781				if !equals(&a?, &b?)? {782					return Ok(false);783				}784			}785			Ok(true)786		}787		(Val::Obj(a), Val::Obj(b)) => {788			if ObjValue::ptr_eq(a, b) {789				return Ok(true);790			}791			let fields = a.fields(792				#[cfg(feature = "exp-preserve-order")]793				false,794			);795			if fields796				!= b.fields(797					#[cfg(feature = "exp-preserve-order")]798					false,799				) {800				return Ok(false);801			}802			for field in fields {803				if !equals(804					&a.get(field.clone())?.expect("field exists"),805					&b.get(field)?.expect("field exists"),806				)? {807					return Ok(false);808				}809			}810			Ok(true)811		}812		(a, b) => Ok(primitive_equals(a, b)?),813	}814}
after · crates/jrsonnet-evaluator/src/val.rs
1use std::{2	cell::RefCell,3	cmp::Ordering,4	fmt::{self, Debug, Display},5	marker::PhantomData,6	mem::replace,7	num::NonZeroU32,8	rc::Rc,9};1011use jrsonnet_gcmodule::{Acyclic, Cc, Trace, cc_dyn};12use jrsonnet_interner::IStr;13pub use jrsonnet_macros::Thunk;14use jrsonnet_types::ValType;15use rustc_hash::FxHashMap;1617pub use crate::arr::{ArrValue, ArrayLike};18use crate::{19	NumValue, ObjValue, Result, SupThis, Unbound, WeakSupThis, bail,20	error::{Error, ErrorKind::*},21	function::FuncVal,22	gc::WithCapacityExt as _,23	manifest::{ManifestFormat, ToStringFormat},24	typed::BoundedUsize,25};2627pub trait ThunkValue: Trace {28	type Output;29	fn get(&self) -> Result<Self::Output>;30}3132#[derive(Trace)]33enum MemoizedClusureThunkInner<D: Trace, T: Trace> {34	Computed(T),35	Errored(Error),36	Waiting {37		env: D,38		// Carries no data, as it is not a real closure, all the39		// captured environment is stored in `env` field.40		#[trace(skip)]41		closure: fn(D) -> Result<T>,42	},43	Pending,44}45#[derive(Trace)]46pub struct MemoizedClosureThunk<D: Trace, T: Trace>(RefCell<MemoizedClusureThunkInner<D, T>>);47impl<D: Trace, T: Trace> MemoizedClosureThunk<D, T> {48	pub fn new(env: D, closure: fn(D) -> Result<T>) -> Self {49		Self(RefCell::new(MemoizedClusureThunkInner::Waiting {50			env,51			closure,52		}))53	}54}5556impl<D: Trace, T: Trace + Clone> ThunkValue for MemoizedClosureThunk<D, T> {57	type Output = T;5859	fn get(&self) -> Result<Self::Output> {60		match &*self.0.borrow() {61			MemoizedClusureThunkInner::Computed(v) => return Ok(v.clone()),62			MemoizedClusureThunkInner::Errored(e) => return Err(e.clone()),63			MemoizedClusureThunkInner::Pending => return Err(InfiniteRecursionDetected.into()),64			MemoizedClusureThunkInner::Waiting { .. } => (),65		}66		let MemoizedClusureThunkInner::Waiting { env, closure } = replace(67			&mut *self.0.borrow_mut(),68			MemoizedClusureThunkInner::Pending,69		) else {70			unreachable!();71		};72		let new_value = match closure(env) {73			Ok(v) => v,74			Err(e) => {75				*self.0.borrow_mut() = MemoizedClusureThunkInner::Errored(e.clone());76				return Err(e);77			}78		};79		*self.0.borrow_mut() = MemoizedClusureThunkInner::Computed(new_value.clone());80		Ok(new_value)81	}82}8384cc_dyn!(85	/// Lazily evaluated value86	#[derive(Clone)] Thunk<V: Trace>,87	ThunkValue<Output = V>,88	pub fn new() {...}89);9091impl<T: Trace> Thunk<T> {92	pub fn evaluated(val: T) -> Self93	where94		T: Clone,95	{96		#[derive(Trace)]97		struct EvaluatedThunk<T: Trace>(T);98		impl<T> ThunkValue for EvaluatedThunk<T>99		where100			T: Clone + Trace,101		{102			type Output = T;103104			fn get(&self) -> Result<Self::Output> {105				Ok(self.0.clone())106			}107		}108		Self::new(EvaluatedThunk(val))109	}110	pub fn errored(e: Error) -> Self {111		#[derive(Trace)]112		struct ErroredThunk<T: Trace>(Error, PhantomData<T>);113		impl<T> ThunkValue for ErroredThunk<T>114		where115			T: Trace,116		{117			type Output = T;118119			fn get(&self) -> Result<Self::Output> {120				Err(self.0.clone())121			}122		}123		Self::new(ErroredThunk(e, PhantomData))124	}125	pub fn result(res: Result<T, Error>) -> Self126	where127		T: Clone,128	{129		match res {130			Ok(o) => Self::evaluated(o),131			Err(e) => Self::errored(e),132		}133	}134}135136impl<T> Thunk<T>137where138	T: Trace,139{140	pub fn force(&self) -> Result<()> {141		self.evaluate()?;142		Ok(())143	}144145	/// Evaluate thunk, or return cached value146	///147	/// # Errors148	///149	/// - Lazy value evaluation returned error150	/// - This method was called during inner value evaluation151	pub fn evaluate(&self) -> Result<T> {152		self.0.get()153	}154}155156pub trait ThunkMapper<Input>: Trace {157	type Output;158	fn map(self, from: Input) -> Result<Self::Output>;159}160impl<Input> Thunk<Input>161where162	Input: Trace,163{164	pub fn map<M>(self, mapper: M) -> Thunk<M::Output>165	where166		M: ThunkMapper<Input>,167		M::Output: Trace + Clone,168	{169		let inner = self;170		Thunk!(move || {171			let value = inner.evaluate()?;172			let mapped = mapper.map(value)?;173			Ok(mapped)174		})175	}176}177178impl<T: Trace + Clone> From<Result<T>> for Thunk<T> {179	fn from(value: Result<T>) -> Self {180		match value {181			Ok(o) => Self::evaluated(o),182			Err(e) => Self::errored(e),183		}184	}185}186impl<T, V: Trace> From<T> for Thunk<V>187where188	T: ThunkValue<Output = V>,189{190	fn from(value: T) -> Self {191		Self::new(value)192	}193}194195impl<T: Trace + Default + Clone> Default for Thunk<T> {196	fn default() -> Self {197		Self::evaluated(T::default())198	}199}200201#[derive(Trace, Clone)]202pub struct CachedUnbound<I, T>203where204	I: Unbound<Bound = T>,205	T: Trace,206{207	cache: Cc<RefCell<FxHashMap<WeakSupThis, T>>>,208	value: I,209}210impl<I: Unbound<Bound = T>, T: Trace> CachedUnbound<I, T> {211	pub fn new(value: I) -> Self {212		Self {213			cache: Cc::new(RefCell::new(FxHashMap::new())),214			value,215		}216	}217}218impl<I: Unbound<Bound = T>, T: Clone + Trace> Unbound for CachedUnbound<I, T> {219	type Bound = T;220	fn bind(&self, sup_this: SupThis) -> Result<T> {221		let cache_key = sup_this.clone().downgrade();222		{223			if let Some(t) = self.cache.borrow().get(&cache_key) {224				return Ok(t.clone());225			}226		}227		let bound = self.value.bind(sup_this)?;228229		{230			let mut cache = self.cache.borrow_mut();231			cache.insert(cache_key, bound.clone());232		}233234		Ok(bound)235	}236}237238impl<T: Debug + Trace> Debug for Thunk<T> {239	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {240		write!(f, "Lazy")241	}242}243impl<T: Trace> PartialEq for Thunk<T> {244	fn eq(&self, other: &Self) -> bool {245		Cc::ptr_eq(&self.0, &other.0)246	}247}248249/// Represents a Jsonnet value, which can be sliced or indexed (string or array).250#[allow(clippy::module_name_repetitions)]251pub enum IndexableVal {252	/// String.253	Str(IStr),254	/// Array.255	Arr(ArrValue),256}257impl IndexableVal {258	pub fn is_empty(&self) -> bool {259		match self {260			Self::Str(s) => s.is_empty(),261			Self::Arr(s) => s.is_empty(),262		}263	}264265	pub fn to_array(self) -> ArrValue {266		match self {267			Self::Str(s) => s.chars().collect(),268			Self::Arr(arr) => arr,269		}270	}271	/// Slice the value.272	///273	/// # Implementation274	///275	/// For strings, will create a copy of specified interval.276	///277	/// For arrays, nothing will be copied on this call, instead [`ArrValue::Slice`] view will be returned.278	pub fn slice(279		self,280		index: Option<i32>,281		end: Option<i32>,282		step: Option<BoundedUsize<1, { i32::MAX as usize }>>,283	) -> Result<Self> {284		match &self {285			Self::Str(s) => {286				let mut computed_len = None;287				let mut get_len = || {288					computed_len.unwrap_or_else(|| {289						let len = s.chars().count();290						let _ = computed_len.insert(len);291						len292					})293				};294				let mut get_idx = |pos: Option<i32>, default| {295					match pos {296						#[expect(clippy::cast_sign_loss, reason = "abs value is used")]297						Some(v) if v < 0 => get_len().saturating_sub((-v as isize) as usize),298						// No need to clamp, as iterator interface is used299						#[expect(clippy::cast_sign_loss, reason = "abs value is used")]300						Some(v) => v as usize,301						None => default,302					}303				};304305				let index = get_idx(index, 0);306				let end = get_idx(end, usize::MAX);307				let step = step.as_deref().copied().unwrap_or(1);308309				if index >= end {310					return Ok(Self::Str("".into()));311				}312313				Ok(Self::Str(314					(s.chars()315						.skip(index)316						.take(end - index)317						.step_by(step)318						.collect::<String>())319					.into(),320				))321			}322			Self::Arr(arr) => Ok(Self::Arr(arr.clone().slice(323				index,324				end,325				#[expect(326					clippy::cast_possible_truncation,327					reason = "overflow will result with skip too large which would be equivalent"328				)]329				step.map(|v| NonZeroU32::new(v.value() as u32).expect("bounded != 0")),330			))),331		}332	}333}334335#[derive(Debug, Clone, Acyclic)]336pub enum StrValue {337	Flat(IStr),338	Tree(Rc<(StrValue, StrValue, usize)>),339}340impl StrValue {341	pub fn concat(a: Self, b: Self) -> Self {342		// TODO: benchmark for an optimal value, currently just a arbitrary choice343		const STRING_EXTEND_THRESHOLD: usize = 100;344345		if a.is_empty() {346			b347		} else if b.is_empty() {348			a349		} else if a.len() + b.len() < STRING_EXTEND_THRESHOLD {350			Self::Flat(format!("{a}{b}").into())351		} else {352			let len = a.len() + b.len();353			Self::Tree(Rc::new((a, b, len)))354		}355	}356	pub fn chunks(&self, c: &mut impl FnMut(&IStr)) {357		fn write_buf(s: &StrValue, c: &mut impl FnMut(&IStr)) {358			match s {359				StrValue::Flat(f) => c(f),360				StrValue::Tree(t) => {361					write_buf(&t.0, c);362					write_buf(&t.1, c);363				}364			}365		}366		write_buf(self, c);367	}368	pub fn into_flat(&self) -> IStr {369		fn write_buf(s: &StrValue, out: &mut String) {370			match s {371				StrValue::Flat(f) => out.push_str(f),372				StrValue::Tree(t) => {373					write_buf(&t.0, out);374					write_buf(&t.1, out);375				}376			}377		}378		match self {379			Self::Flat(f) => f.clone(),380			Self::Tree(_) => {381				let mut buf = String::with_capacity(self.len());382				write_buf(self, &mut buf);383				buf.into()384			}385		}386	}387	pub fn len(&self) -> usize {388		match self {389			Self::Flat(v) => v.len(),390			Self::Tree(t) => t.2,391		}392	}393	pub fn is_empty(&self) -> bool {394		match self {395			Self::Flat(v) => v.is_empty(),396			// Can't create non-flat empty string397			Self::Tree(_) => false,398		}399	}400}401impl<T> From<T> for StrValue402where403	IStr: From<T>,404{405	fn from(value: T) -> Self {406		Self::Flat(IStr::from(value))407	}408}409impl Display for StrValue {410	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {411		match self {412			Self::Flat(v) => write!(f, "{v}"),413			Self::Tree(t) => {414				write!(f, "{}", t.0)?;415				write!(f, "{}", t.1)416			}417		}418	}419}420impl PartialEq for StrValue {421	// False positive, into_flat returns not StrValue, but IStr, thus no infinite recursion here.422	#[allow(clippy::unconditional_recursion)]423	fn eq(&self, other: &Self) -> bool {424		let a = self.clone().into_flat();425		let b = other.clone().into_flat();426		a == b427	}428}429impl Eq for StrValue {}430impl PartialOrd for StrValue {431	fn partial_cmp(&self, other: &Self) -> Option<Ordering> {432		Some(self.cmp(other))433	}434}435impl Ord for StrValue {436	fn cmp(&self, other: &Self) -> Ordering {437		let a = self.clone().into_flat();438		let b = other.clone().into_flat();439		a.cmp(&b)440	}441}442443/// Represents any valid Jsonnet value.444#[derive(Debug, Clone, Trace, Default)]445pub enum Val {446	/// Represents a Jsonnet boolean.447	Bool(bool),448	/// Represents a Jsonnet null value.449	#[default]450	Null,451	/// Represents a Jsonnet string.452	Str(StrValue),453	/// Represents a Jsonnet number.454	/// Should be finite, and not NaN455	/// This restriction isn't enforced by enum, as enum field can't be marked as private456	Num(NumValue),457	/// Experimental bigint458	#[cfg(feature = "exp-bigint")]459	BigInt(#[trace(skip)] Box<num_bigint::BigInt>),460	/// Represents a Jsonnet array.461	Arr(ArrValue),462	/// Represents a Jsonnet object.463	Obj(ObjValue),464	/// Represents a Jsonnet function.465	Func(FuncVal),466}467468#[cfg(target_pointer_width = "64")]469static_assertions::assert_eq_size!(Val, [u8; 24]);470471impl From<IndexableVal> for Val {472	fn from(v: IndexableVal) -> Self {473		match v {474			IndexableVal::Str(s) => Self::string(s),475			IndexableVal::Arr(a) => Self::Arr(a),476		}477	}478}479480impl Val {481	pub const fn as_bool(&self) -> Option<bool> {482		match self {483			Self::Bool(v) => Some(*v),484			_ => None,485		}486	}487	pub const fn as_null(&self) -> Option<()> {488		match self {489			Self::Null => Some(()),490			_ => None,491		}492	}493	pub fn as_str(&self) -> Option<IStr> {494		match self {495			Self::Str(s) => Some(s.clone().into_flat()),496			_ => None,497		}498	}499	pub const fn as_num(&self) -> Option<f64> {500		match self {501			Self::Num(n) => Some(n.get()),502			_ => None,503		}504	}505	#[cfg(feature = "exp-bigint")]506	pub fn as_bigint(&self) -> Option<num_bigint::BigInt> {507		match self {508			Self::BigInt(n) => Some(*n.clone()),509			_ => None,510		}511	}512	pub fn as_arr(&self) -> Option<ArrValue> {513		match self {514			Self::Arr(a) => Some(a.clone()),515			_ => None,516		}517	}518	pub fn as_obj(&self) -> Option<ObjValue> {519		match self {520			Self::Obj(o) => Some(o.clone()),521			_ => None,522		}523	}524	pub fn as_func(&self) -> Option<FuncVal> {525		match self {526			Self::Func(f) => Some(f.clone()),527			_ => None,528		}529	}530531	pub const fn value_type(&self) -> ValType {532		match self {533			Self::Str(..) => ValType::Str,534			Self::Num(..) => ValType::Num,535			#[cfg(feature = "exp-bigint")]536			Self::BigInt(..) => ValType::BigInt,537			Self::Arr(..) => ValType::Arr,538			Self::Obj(..) => ValType::Obj,539			Self::Bool(_) => ValType::Bool,540			Self::Null => ValType::Null,541			Self::Func(..) => ValType::Func,542		}543	}544545	pub fn manifest(&self, format: impl ManifestFormat) -> Result<String> {546		fn manifest_dyn(val: &Val, manifest: &dyn ManifestFormat) -> Result<String> {547			manifest.manifest(val.clone())548		}549		manifest_dyn(self, &format)550	}551552	pub fn to_string(&self) -> Result<IStr> {553		Ok(match self {554			Self::Bool(true) => "true".into(),555			Self::Bool(false) => "false".into(),556			Self::Null => "null".into(),557			Self::Str(s) => s.clone().into_flat(),558			_ => self.manifest(ToStringFormat).map(IStr::from)?,559		})560	}561562	pub fn into_indexable(self) -> Result<IndexableVal> {563		Ok(match self {564			Self::Str(s) => IndexableVal::Str(s.into_flat()),565			Self::Arr(arr) => IndexableVal::Arr(arr),566			_ => bail!(ValueIsNotIndexable(self.value_type())),567		})568	}569570	pub fn function(function: impl Into<FuncVal>) -> Self {571		Self::Func(function.into())572	}573	pub fn string(string: impl Into<StrValue>) -> Self {574		Self::Str(string.into())575	}576	pub fn num(num: impl Into<NumValue>) -> Self {577		Self::Num(num.into())578	}579	pub fn try_num<V, E>(num: V) -> Result<Self, E>580	where581		NumValue: TryFrom<V, Error = E>,582	{583		Ok(Self::Num(num.try_into()?))584	}585	pub fn arr(a: impl ArrayLike) -> Self {586		Self::Arr(ArrValue::new(a))587	}588}589590impl From<IStr> for Val {591	fn from(value: IStr) -> Self {592		Self::string(value)593	}594}595impl From<String> for Val {596	fn from(value: String) -> Self {597		Self::string(value)598	}599}600impl From<&str> for Val {601	fn from(value: &str) -> Self {602		Self::string(value)603	}604}605impl From<ObjValue> for Val {606	fn from(value: ObjValue) -> Self {607		Self::Obj(value)608	}609}610611const fn is_function_like(val: &Val) -> bool {612	matches!(val, Val::Func(_))613}614615/// Native implementation of `std.primitiveEquals`616pub fn primitive_equals(val_a: &Val, val_b: &Val) -> Result<bool> {617	Ok(match (val_a, val_b) {618		(Val::Bool(a), Val::Bool(b)) => a == b,619		(Val::Null, Val::Null) => true,620		(Val::Str(a), Val::Str(b)) => a == b,621		(Val::Num(a), Val::Num(b)) => (a.get() - b.get()).abs() <= f64::EPSILON,622		#[cfg(feature = "exp-bigint")]623		(Val::BigInt(a), Val::BigInt(b)) => a == b,624		(Val::Arr(_), Val::Arr(_)) => {625			bail!("primitiveEquals operates on primitive types, got array")626		}627		(Val::Obj(_), Val::Obj(_)) => {628			bail!("primitiveEquals operates on primitive types, got object")629		}630		(a, b) if is_function_like(a) && is_function_like(b) => {631			bail!("cannot test equality of functions")632		}633		(_, _) => false,634	})635}636637/// Native implementation of `std.equals`638pub fn equals(val_a: &Val, val_b: &Val) -> Result<bool> {639	if val_a.value_type() != val_b.value_type() {640		return Ok(false);641	}642	match (val_a, val_b) {643		(Val::Arr(a), Val::Arr(b)) => {644			if ArrValue::ptr_eq(a, b) {645				return Ok(true);646			}647			if a.len() != b.len() {648				return Ok(false);649			}650			for (a, b) in a.iter().zip(b.iter()) {651				if !equals(&a?, &b?)? {652					return Ok(false);653				}654			}655			Ok(true)656		}657		(Val::Obj(a), Val::Obj(b)) => {658			if ObjValue::ptr_eq(a, b) {659				return Ok(true);660			}661			let fields = a.fields(662				#[cfg(feature = "exp-preserve-order")]663				false,664			);665			if fields666				!= b.fields(667					#[cfg(feature = "exp-preserve-order")]668					false,669				) {670				return Ok(false);671			}672			for field in fields {673				if !equals(674					&a.get(field.clone())?.expect("field exists"),675					&b.get(field)?.expect("field exists"),676				)? {677					return Ok(false);678				}679			}680			Ok(true)681		}682		(a, b) => Ok(primitive_equals(a, b)?),683	}684}
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
--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -12,13 +12,12 @@
 pub use encoding::*;
 pub use hash::*;
 use jrsonnet_evaluator::{
-	ContextBuilder, IStr, ObjValue, ObjValueBuilder, Thunk, Val,
+	ContextBuilder, IStr, NumValue, ObjValue, ObjValueBuilder, Thunk, Val,
 	error::Result,
 	function::{CallLocation, FuncVal, builtin_id},
 	tla::TlaArg,
 	trace::PathResolver,
 	typed::SerializeTypedObj as _,
-	val::NumValue,
 };
 use jrsonnet_gcmodule::{Acyclic, Cc, Trace};
 use jrsonnet_ir::Source;
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]