git.delta.rocks / jrsonnet / refs/commits / 2fdeec2fa073

difftreelog

source

crates/jrsonnet-evaluator/src/val.rs17.5 KiBsourcehistory
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: Clone + 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 + Clone,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) => ArrValue::chars(s.chars()),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						Some(v) if v < 0 => get_len().saturating_sub((-v) as usize),299						// No need to clamp, as iterator interface is used300						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				step.map(|v| NonZeroU32::new(v.value() as u32).expect("bounded != 0")),326			))),327		}328	}329}330331#[derive(Debug, Clone, Acyclic)]332pub enum StrValue {333	Flat(IStr),334	Tree(Rc<(StrValue, StrValue, usize)>),335}336impl StrValue {337	pub fn concat(a: Self, b: Self) -> Self {338		// TODO: benchmark for an optimal value, currently just a arbitrary choice339		const STRING_EXTEND_THRESHOLD: usize = 100;340341		if a.is_empty() {342			b343		} else if b.is_empty() {344			a345		} else if a.len() + b.len() < STRING_EXTEND_THRESHOLD {346			Self::Flat(format!("{a}{b}").into())347		} else {348			let len = a.len() + b.len();349			Self::Tree(Rc::new((a, b, len)))350		}351	}352	pub fn into_flat(self) -> IStr {353		#[cold]354		fn write_buf(s: &StrValue, out: &mut String) {355			match s {356				StrValue::Flat(f) => out.push_str(f),357				StrValue::Tree(t) => {358					write_buf(&t.0, out);359					write_buf(&t.1, out);360				}361			}362		}363		match self {364			Self::Flat(f) => f,365			Self::Tree(_) => {366				let mut buf = String::with_capacity(self.len());367				write_buf(&self, &mut buf);368				buf.into()369			}370		}371	}372	pub fn len(&self) -> usize {373		match self {374			Self::Flat(v) => v.len(),375			Self::Tree(t) => t.2,376		}377	}378	pub fn is_empty(&self) -> bool {379		match self {380			Self::Flat(v) => v.is_empty(),381			// Can't create non-flat empty string382			Self::Tree(_) => false,383		}384	}385}386impl<T> From<T> for StrValue387where388	IStr: From<T>,389{390	fn from(value: T) -> Self {391		Self::Flat(IStr::from(value))392	}393}394impl Display for StrValue {395	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {396		match self {397			Self::Flat(v) => write!(f, "{v}"),398			Self::Tree(t) => {399				write!(f, "{}", t.0)?;400				write!(f, "{}", t.1)401			}402		}403	}404}405impl PartialEq for StrValue {406	// False positive, into_flat returns not StrValue, but IStr, thus no infinite recursion here.407	#[allow(clippy::unconditional_recursion)]408	fn eq(&self, other: &Self) -> bool {409		let a = self.clone().into_flat();410		let b = other.clone().into_flat();411		a == b412	}413}414impl Eq for StrValue {}415impl PartialOrd for StrValue {416	fn partial_cmp(&self, other: &Self) -> Option<Ordering> {417		Some(self.cmp(other))418	}419}420impl Ord for StrValue {421	fn cmp(&self, other: &Self) -> Ordering {422		let a = self.clone().into_flat();423		let b = other.clone().into_flat();424		a.cmp(&b)425	}426}427428/// Represents jsonnet number429/// Jsonnet numbers are finite f64, with NaNs disallowed430#[derive(Trace, Clone, Copy)]431#[repr(transparent)]432pub struct NumValue(f64);433impl NumValue {434	/// Creates a [`NumValue`], if value is finite and not NaN435	pub fn new(v: f64) -> Option<Self> {436		if !v.is_finite() {437			return None;438		}439		Some(Self(v))440	}441	#[inline]442	pub const fn get(&self) -> f64 {443		self.0444	}445	pub(crate) fn truncate_for_bitwise(self) -> Result<i64> {446		if self.0 < MIN_SAFE_INTEGER || self.0 > MAX_SAFE_INTEGER {447			bail!("numberic value outside of safe integer range for bitwise operation");448		}449		Ok(self.0 as i64)450	}451}452impl PartialEq for NumValue {453	fn eq(&self, other: &Self) -> bool {454		self.0 == other.0455	}456}457impl Eq for NumValue {}458impl Ord for NumValue {459	#[inline]460	fn cmp(&self, other: &Self) -> Ordering {461		// Can't use `total_cmp`: its behavior for `-0` and `0`462		// is not following wanted.463		unsafe { self.0.partial_cmp(&other.0).unwrap_unchecked() }464	}465}466impl PartialOrd for NumValue {467	#[inline]468	fn partial_cmp(&self, other: &Self) -> Option<Ordering> {469		Some(self.cmp(other))470	}471}472impl Debug for NumValue {473	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {474		Debug::fmt(&self.0, f)475	}476}477impl Display for NumValue {478	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {479		Display::fmt(&self.0, f)480	}481}482impl Deref for NumValue {483	type Target = f64;484485	#[inline]486	fn deref(&self) -> &Self::Target {487		&self.0488	}489}490macro_rules! impl_num {491	($($ty:ty),+) => {$(492		impl From<$ty> for NumValue {493			#[inline]494			fn from(value: $ty) -> Self {495				Self(value.into())496			}497		}498	)+};499}500impl_num!(i8, u8, i16, u16, i32, u32);501502#[derive(Clone, Copy, Debug, Error, Trace)]503pub enum ConvertNumValueError {504	#[error("overflow")]505	Overflow,506	#[error("underflow")]507	Underflow,508	#[error("non-finite")]509	NonFinite,510}511impl From<ConvertNumValueError> for Error {512	fn from(e: ConvertNumValueError) -> Self {513		Self::new(e.into())514	}515}516517macro_rules! impl_try_num {518	($($ty:ty),+) => {$(519		impl TryFrom<$ty> for NumValue {520			type Error = ConvertNumValueError;521			#[inline]522			fn try_from(value: $ty) -> Result<Self, ConvertNumValueError> {523				let value = value as f64;524				if value < MIN_SAFE_INTEGER {525					return Err(ConvertNumValueError::Underflow)526				} else if value > MAX_SAFE_INTEGER {527					return Err(ConvertNumValueError::Overflow)528				}529				// Number is finite.530				Ok(Self(value))531			}532		}533	)+};534}535impl_try_num!(usize, isize, i64, u64);536537impl TryFrom<f64> for NumValue {538	type Error = ConvertNumValueError;539540	#[inline]541	fn try_from(value: f64) -> Result<Self, Self::Error> {542		Self::new(value).ok_or(ConvertNumValueError::NonFinite)543	}544}545impl TryFrom<f32> for NumValue {546	type Error = ConvertNumValueError;547548	#[inline]549	fn try_from(value: f32) -> Result<Self, Self::Error> {550		Self::new(f64::from(value)).ok_or(ConvertNumValueError::NonFinite)551	}552}553554/// Represents any valid Jsonnet value.555#[derive(Debug, Clone, Trace, Default)]556pub enum Val {557	/// Represents a Jsonnet boolean.558	Bool(bool),559	/// Represents a Jsonnet null value.560	#[default]561	Null,562	/// Represents a Jsonnet string.563	Str(StrValue),564	/// Represents a Jsonnet number.565	/// Should be finite, and not NaN566	/// This restriction isn't enforced by enum, as enum field can't be marked as private567	Num(NumValue),568	/// Experimental bigint569	#[cfg(feature = "exp-bigint")]570	BigInt(#[trace(skip)] Box<num_bigint::BigInt>),571	/// Represents a Jsonnet array.572	Arr(ArrValue),573	/// Represents a Jsonnet object.574	Obj(ObjValue),575	/// Represents a Jsonnet function.576	Func(FuncVal),577}578579#[cfg(target_pointer_width = "64")]580static_assertions::assert_eq_size!(Val, [u8; 24]);581582impl From<IndexableVal> for Val {583	fn from(v: IndexableVal) -> Self {584		match v {585			IndexableVal::Str(s) => Self::string(s),586			IndexableVal::Arr(a) => Self::Arr(a),587		}588	}589}590591impl Val {592	pub const fn as_bool(&self) -> Option<bool> {593		match self {594			Self::Bool(v) => Some(*v),595			_ => None,596		}597	}598	pub const fn as_null(&self) -> Option<()> {599		match self {600			Self::Null => Some(()),601			_ => None,602		}603	}604	pub fn as_str(&self) -> Option<IStr> {605		match self {606			Self::Str(s) => Some(s.clone().into_flat()),607			_ => None,608		}609	}610	pub const fn as_num(&self) -> Option<f64> {611		match self {612			Self::Num(n) => Some(n.get()),613			_ => None,614		}615	}616	#[cfg(feature = "exp-bigint")]617	pub fn as_bigint(&self) -> Option<num_bigint::BigInt> {618		match self {619			Self::BigInt(n) => Some(*n.clone()),620			_ => None,621		}622	}623	pub fn as_arr(&self) -> Option<ArrValue> {624		match self {625			Self::Arr(a) => Some(a.clone()),626			_ => None,627		}628	}629	pub fn as_obj(&self) -> Option<ObjValue> {630		match self {631			Self::Obj(o) => Some(o.clone()),632			_ => None,633		}634	}635	pub fn as_func(&self) -> Option<FuncVal> {636		match self {637			Self::Func(f) => Some(f.clone()),638			_ => None,639		}640	}641642	pub const fn value_type(&self) -> ValType {643		match self {644			Self::Str(..) => ValType::Str,645			Self::Num(..) => ValType::Num,646			#[cfg(feature = "exp-bigint")]647			Self::BigInt(..) => ValType::BigInt,648			Self::Arr(..) => ValType::Arr,649			Self::Obj(..) => ValType::Obj,650			Self::Bool(_) => ValType::Bool,651			Self::Null => ValType::Null,652			Self::Func(..) => ValType::Func,653		}654	}655656	pub fn manifest(&self, format: impl ManifestFormat) -> Result<String> {657		fn manifest_dyn(val: &Val, manifest: &dyn ManifestFormat) -> Result<String> {658			manifest.manifest(val.clone())659		}660		manifest_dyn(self, &format)661	}662663	pub fn to_string(&self) -> Result<IStr> {664		Ok(match self {665			Self::Bool(true) => "true".into(),666			Self::Bool(false) => "false".into(),667			Self::Null => "null".into(),668			Self::Str(s) => s.clone().into_flat(),669			_ => self.manifest(ToStringFormat).map(IStr::from)?,670		})671	}672673	pub fn into_indexable(self) -> Result<IndexableVal> {674		Ok(match self {675			Self::Str(s) => IndexableVal::Str(s.into_flat()),676			Self::Arr(arr) => IndexableVal::Arr(arr),677			_ => bail!(ValueIsNotIndexable(self.value_type())),678		})679	}680681	pub fn function(function: impl Into<FuncVal>) -> Self {682		Self::Func(function.into())683	}684	pub fn string(string: impl Into<StrValue>) -> Self {685		Self::Str(string.into())686	}687	pub fn num(num: impl Into<NumValue>) -> Self {688		Self::Num(num.into())689	}690	pub fn try_num<V, E>(num: V) -> Result<Self, E>691	where692		NumValue: TryFrom<V, Error = E>,693	{694		Ok(Self::Num(num.try_into()?))695	}696}697698impl From<IStr> for Val {699	fn from(value: IStr) -> Self {700		Self::string(value)701	}702}703impl From<String> for Val {704	fn from(value: String) -> Self {705		Self::string(value)706	}707}708impl From<&str> for Val {709	fn from(value: &str) -> Self {710		Self::string(value)711	}712}713impl From<ObjValue> for Val {714	fn from(value: ObjValue) -> Self {715		Self::Obj(value)716	}717}718719const fn is_function_like(val: &Val) -> bool {720	matches!(val, Val::Func(_))721}722723/// Native implementation of `std.primitiveEquals`724pub fn primitive_equals(val_a: &Val, val_b: &Val) -> Result<bool> {725	Ok(match (val_a, val_b) {726		(Val::Bool(a), Val::Bool(b)) => a == b,727		(Val::Null, Val::Null) => true,728		(Val::Str(a), Val::Str(b)) => a == b,729		(Val::Num(a), Val::Num(b)) => (a.get() - b.get()).abs() <= f64::EPSILON,730		#[cfg(feature = "exp-bigint")]731		(Val::BigInt(a), Val::BigInt(b)) => a == b,732		(Val::Arr(_), Val::Arr(_)) => {733			bail!("primitiveEquals operates on primitive types, got array")734		}735		(Val::Obj(_), Val::Obj(_)) => {736			bail!("primitiveEquals operates on primitive types, got object")737		}738		(a, b) if is_function_like(a) && is_function_like(b) => {739			bail!("cannot test equality of functions")740		}741		(_, _) => false,742	})743}744745/// Native implementation of `std.equals`746pub fn equals(val_a: &Val, val_b: &Val) -> Result<bool> {747	if val_a.value_type() != val_b.value_type() {748		return Ok(false);749	}750	match (val_a, val_b) {751		(Val::Arr(a), Val::Arr(b)) => {752			if ArrValue::ptr_eq(a, b) {753				return Ok(true);754			}755			if a.len() != b.len() {756				return Ok(false);757			}758			for (a, b) in a.iter().zip(b.iter()) {759				if !equals(&a?, &b?)? {760					return Ok(false);761				}762			}763			Ok(true)764		}765		(Val::Obj(a), Val::Obj(b)) => {766			if ObjValue::ptr_eq(a, b) {767				return Ok(true);768			}769			let fields = a.fields(770				#[cfg(feature = "exp-preserve-order")]771				false,772			);773			if fields774				!= b.fields(775					#[cfg(feature = "exp-preserve-order")]776					false,777				) {778				return Ok(false);779			}780			for field in fields {781				if !equals(782					&a.get(field.clone())?.expect("field exists"),783					&b.get(field)?.expect("field exists"),784				)? {785					return Ok(false);786				}787			}788			Ok(true)789		}790		(a, b) => Ok(primitive_equals(a, b)?),791	}792}