git.delta.rocks / jrsonnet / refs/commits / 23d571a0df03

difftreelog

refactor move arrays to use dyn ArrayLike

Yaroslav Bolyukin2023-08-06parent: #09dae32.patch.diff
in: master

8 files changed

modifiedcrates/jrsonnet-evaluator/src/arr/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/arr/mod.rs
+++ b/crates/jrsonnet-evaluator/src/arr/mod.rs
@@ -1,44 +1,20 @@
-use std::rc::Rc;
+use std::any::Any;
 
 use jrsonnet_gcmodule::{Cc, Trace};
 use jrsonnet_interner::IBytes;
 use jrsonnet_parser::LocExpr;
 
-use crate::{function::FuncVal, Context, Result, Thunk, Val};
+use crate::{function::FuncVal, gc::TraceBox, tb, Context, Result, Thunk, Val};
 
 mod spec;
-use spec::*;
+pub use spec::ArrayLike;
+pub(crate) use spec::*;
 
 /// Represents a Jsonnet array value.
 #[derive(Debug, Clone, Trace)]
 // may contrain other ArrValue
 #[trace(tracking(force))]
-pub enum ArrValue {
-	/// Layout optimized byte array.
-	Bytes(BytesArray),
-	/// Layout optimized char array.
-	Chars(CharArray),
-	/// Every element is lazy evaluated.
-	Lazy(LazyArray),
-	/// Every element is defined somewhere in source code
-	Expr(ExprArray),
-	/// Every field is already evaluated.
-	Eager(EagerArray),
-	/// Concatenation of two arrays of any kind.
-	Extended(Cc<ExtendedArray>),
-	/// Represents a integer array in form `[start, start + 1, ... end - 1, end]`.
-	/// This kind of arrays is generated by `std.range(start, end)` call, and used for loops.
-	Range(RangeArray),
-	/// Sliced array view.
-	Slice(Cc<SliceArray>),
-	/// Reversed array view.
-	/// Returned by `std.reverse(other)` call
-	Reverse(Cc<ReverseArray>),
-	/// Returned by `std.map` call
-	Mapped(MappedArray),
-	/// Returned by `std.repeat` call
-	Repeated(RepeatedArray),
-}
+pub struct ArrValue(Cc<TraceBox<dyn ArrayLike>>);
 
 pub trait ArrayLikeIter<T>: Iterator<Item = T> + DoubleEndedIterator + ExactSizeIterator {}
 impl<I, T> ArrayLikeIter<T> for I where
@@ -47,36 +23,39 @@
 }
 
 impl ArrValue {
+	pub fn new(v: impl ArrayLike) -> Self {
+		Self(Cc::new(tb!(v)))
+	}
 	pub fn empty() -> Self {
-		Self::Range(RangeArray::empty())
+		Self::new(RangeArray::empty())
 	}
 
 	pub fn expr(ctx: Context, exprs: impl IntoIterator<Item = LocExpr>) -> Self {
-		Self::Expr(ExprArray::new(ctx, exprs))
+		Self::new(ExprArray::new(ctx, exprs))
 	}
 
-	pub fn lazy(thunks: Cc<Vec<Thunk<Val>>>) -> Self {
-		Self::Lazy(LazyArray(thunks))
+	pub fn lazy(thunks: Vec<Thunk<Val>>) -> Self {
+		Self::new(LazyArray(thunks))
 	}
 
 	pub fn eager(values: Vec<Val>) -> Self {
-		Self::Eager(EagerArray(Cc::new(values)))
+		Self::new(EagerArray(values))
 	}
 
 	pub fn repeated(data: ArrValue, repeats: usize) -> Option<Self> {
-		Some(Self::Repeated(RepeatedArray::new(data, repeats)?))
+		Some(Self::new(RepeatedArray::new(data, repeats)?))
 	}
 
 	pub fn bytes(bytes: IBytes) -> Self {
-		Self::Bytes(BytesArray(bytes))
+		Self::new(BytesArray(bytes))
 	}
 	pub fn chars(chars: impl Iterator<Item = char>) -> Self {
-		Self::Chars(CharArray(Rc::new(chars.collect())))
+		Self::new(CharArray(chars.collect()))
 	}
 
 	#[must_use]
 	pub fn map(self, mapper: FuncVal) -> Self {
-		Self::Mapped(MappedArray::new(self, mapper))
+		Self::new(MappedArray::new(self, mapper))
 	}
 
 	pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {
@@ -100,7 +79,7 @@
 		} else if b.is_empty() {
 			a
 		} else if a.len() + b.len() > ARR_EXTEND_THRESHOLD {
-			Self::Extended(Cc::new(ExtendedArray::new(a, b)))
+			Self::new(ExtendedArray::new(a, b))
 		} else if let (Some(a), Some(b)) = (a.iter_cheap(), b.iter_cheap()) {
 			let mut out = Vec::with_capacity(a.len() + b.len());
 			out.extend(a);
@@ -110,15 +89,15 @@
 			let mut out = Vec::with_capacity(a.len() + b.len());
 			out.extend(a.iter_lazy());
 			out.extend(b.iter_lazy());
-			Self::lazy(Cc::new(out))
+			Self::lazy(out)
 		}
 	}
 
 	pub fn range_exclusive(a: i32, b: i32) -> Self {
-		Self::Range(RangeArray::new_exclusive(a, b))
+		Self::new(RangeArray::new_exclusive(a, b))
 	}
 	pub fn range_inclusive(a: i32, b: i32) -> Self {
-		Self::Range(RangeArray::new_inclusive(a, b))
+		Self::new(RangeArray::new_inclusive(a, b))
 	}
 
 	#[must_use]
@@ -136,53 +115,42 @@
 		if from >= to || step == 0 {
 			return None;
 		}
-		// match self {
-		// 	ArrValue::Slice(slice) => {
-		// 		return Some(Self::Slice(Cc::new(SliceArray {
-		// 			inner: slice.inner.clone(),
-		// 			from: slice.from + slice.step * (from as u32),
-		// 			to: slice.from + (to as u32) * slice.step,
-		// 			step: slice.step * step as u32,
-		// 		})))
-		// 	}
-		// 	_ => {}
-		// }
 
-		Some(Self::Slice(Cc::new(SliceArray {
+		Some(Self::new(SliceArray {
 			inner: self,
 			from: from as u32,
 			to: to as u32,
 			step: step as u32,
-		})))
+		}))
 	}
 
 	/// Array length.
 	pub fn len(&self) -> usize {
-		pass!(self.len())
+		self.0.len()
 	}
 
 	/// Is array contains no elements?
 	pub fn is_empty(&self) -> bool {
-		pass!(self.is_empty())
+		self.0.is_empty()
 	}
 
 	/// Get array element by index, evaluating it, if it is lazy.
 	///
 	/// Returns `None` on out-of-bounds condition.
 	pub fn get(&self, index: usize) -> Result<Option<Val>> {
-		pass!(self.get(index))
+		self.0.get(index)
 	}
 
 	/// Returns None if get is either non cheap, or out of bounds
 	fn get_cheap(&self, index: usize) -> Option<Val> {
-		pass!(self.get_cheap(index))
+		self.0.get_cheap(index)
 	}
 
 	/// Get array element by index, without evaluation.
 	///
 	/// Returns `None` on out-of-bounds condition.
 	pub fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
-		pass!(self.get_lazy(index))
+		self.0.get_lazy(index)
 	}
 
 	pub fn iter(&self) -> impl ArrayLikeIter<Result<Val>> + '_ {
@@ -205,33 +173,20 @@
 	/// Return a reversed view on current array.
 	#[must_use]
 	pub fn reversed(self) -> Self {
-		Self::Reverse(Cc::new(ReverseArray(self)))
+		Self::new(ReverseArray(self))
 	}
 
 	pub fn ptr_eq(a: &Self, b: &Self) -> bool {
-		match (a, b) {
-			(ArrValue::Bytes(a), ArrValue::Bytes(b)) => a.0 == b.0,
-			(ArrValue::Lazy(a), ArrValue::Lazy(b)) => Cc::ptr_eq(&a.0, &b.0),
-			(ArrValue::Expr(a), ArrValue::Expr(b)) => Cc::ptr_eq(&a.0, &b.0),
-			(ArrValue::Eager(a), ArrValue::Eager(b)) => Cc::ptr_eq(&a.0, &b.0),
-			(ArrValue::Extended(a), ArrValue::Extended(b)) => Cc::ptr_eq(a, b),
-			(ArrValue::Range(a), ArrValue::Range(b)) => a == b,
-			_ => false,
-		}
+		Cc::ptr_eq(&a.0, &b.0)
 	}
 
 	/// Is this vec supports `.get_cheap()?`
 	pub fn is_cheap(&self) -> bool {
-		match self {
-			ArrValue::Eager(_) | ArrValue::Range(..) | ArrValue::Bytes(_) | ArrValue::Chars(_) => {
-				true
-			}
-			ArrValue::Extended(v) => v.a.is_cheap() && v.b.is_cheap(),
-			ArrValue::Slice(r) => r.inner.is_cheap(),
-			ArrValue::Reverse(i) => i.0.is_cheap(),
-			ArrValue::Repeated(v) => v.is_cheap(),
-			ArrValue::Expr(_) | ArrValue::Lazy(_) | ArrValue::Mapped(_) => false,
-		}
+		self.0.is_cheap()
+	}
+
+	pub fn as_any(&self) -> &dyn Any {
+		&self.0
 	}
 }
 impl From<Vec<Val>> for ArrValue {
@@ -241,7 +196,7 @@
 }
 impl From<Vec<Thunk<Val>>> for ArrValue {
 	fn from(value: Vec<Thunk<Val>>) -> Self {
-		Self::lazy(Cc::new(value))
+		Self::lazy(value)
 	}
 }
 impl FromIterator<Val> for ArrValue {
@@ -249,6 +204,27 @@
 		Self::eager(iter.into_iter().collect())
 	}
 }
+impl ArrayLike for ArrValue {
+	fn len(&self) -> usize {
+		self.0.len()
+	}
 
+	fn get(&self, index: usize) -> Result<Option<Val>> {
+		self.0.get(index)
+	}
+
+	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
+		self.0.get_lazy(index)
+	}
+
+	fn get_cheap(&self, index: usize) -> Option<Val> {
+		self.0.get_cheap(index)
+	}
+
+	fn is_cheap(&self) -> bool {
+		self.0.is_cheap()
+	}
+}
+
 #[cfg(target_pointer_width = "64")]
-static_assertions::assert_eq_size!(ArrValue, [u8; 16]);
+static_assertions::assert_eq_size!(ArrValue, [u8; 8]);
modifiedcrates/jrsonnet-evaluator/src/arr/spec.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/arr/spec.rs
+++ b/crates/jrsonnet-evaluator/src/arr/spec.rs
@@ -1,4 +1,4 @@
-use std::{cell::RefCell, iter, mem::replace, rc::Rc};
+use std::{any::Any, cell::RefCell, fmt::Debug, iter, mem::replace};
 
 use jrsonnet_gcmodule::{Cc, Trace};
 use jrsonnet_interner::{IBytes, IStr};
@@ -13,7 +13,7 @@
 	Context, Error, Result, Thunk, Val,
 };
 
-pub trait ArrayLike: Sized + Into<ArrValue> {
+pub trait ArrayLike: Any + Trace + Debug {
 	fn len(&self) -> usize;
 	fn is_empty(&self) -> bool {
 		self.len() == 0
@@ -22,12 +22,10 @@
 	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>>;
 	fn get_cheap(&self, index: usize) -> Option<Val>;
 
-	fn reverse(self) -> ArrValue {
-		ArrValue::Reverse(Cc::new(ReverseArray(self.into())))
-	}
+	fn is_cheap(&self) -> bool;
 }
 
-#[derive(Debug, Clone, Trace)]
+#[derive(Debug, Trace)]
 pub struct SliceArray {
 	pub(crate) inner: ArrValue,
 	pub(crate) from: u32,
@@ -81,15 +79,13 @@
 	fn get_cheap(&self, index: usize) -> Option<Val> {
 		self.iter_cheap()?.nth(index)
 	}
-}
-impl From<SliceArray> for ArrValue {
-	fn from(value: SliceArray) -> Self {
-		Self::Slice(Cc::new(value))
+	fn is_cheap(&self) -> bool {
+		self.inner.is_cheap()
 	}
 }
 
-#[derive(Trace, Debug, Clone)]
-pub struct CharArray(pub Rc<Vec<char>>);
+#[derive(Trace, Debug)]
+pub struct CharArray(pub Vec<char>);
 impl ArrayLike for CharArray {
 	fn len(&self) -> usize {
 		self.0.len()
@@ -108,14 +104,12 @@
 			.get(index)
 			.map(|v| Val::Str(StrValue::Flat(IStr::from(*v))))
 	}
-}
-impl From<CharArray> for ArrValue {
-	fn from(value: CharArray) -> Self {
-		ArrValue::Chars(value)
+	fn is_cheap(&self) -> bool {
+		true
 	}
 }
 
-#[derive(Trace, Debug, Clone)]
+#[derive(Trace, Debug)]
 pub struct BytesArray(pub IBytes);
 impl ArrayLike for BytesArray {
 	fn len(&self) -> usize {
@@ -133,10 +127,8 @@
 	fn get_cheap(&self, index: usize) -> Option<Val> {
 		self.0.get(index).map(|v| Val::Num(f64::from(*v)))
 	}
-}
-impl From<BytesArray> for ArrValue {
-	fn from(value: BytesArray) -> Self {
-		ArrValue::Bytes(value)
+	fn is_cheap(&self) -> bool {
+		true
 	}
 }
 
@@ -148,30 +140,30 @@
 	Pending,
 }
 
-#[derive(Debug, Trace)]
-pub struct ExprArrayInner {
+#[derive(Debug, Trace, Clone)]
+pub struct ExprArray {
 	ctx: Context,
-	cached: RefCell<Vec<ArrayThunk<LocExpr>>>,
+	cached: Cc<RefCell<Vec<ArrayThunk<LocExpr>>>>,
 }
-#[derive(Debug, Trace, Clone)]
-pub struct ExprArray(pub Cc<ExprArrayInner>);
 impl ExprArray {
 	pub fn new(ctx: Context, items: impl IntoIterator<Item = LocExpr>) -> Self {
-		Self(Cc::new(ExprArrayInner {
+		Self {
 			ctx,
-			cached: RefCell::new(items.into_iter().map(ArrayThunk::Waiting).collect()),
-		}))
+			cached: Cc::new(RefCell::new(
+				items.into_iter().map(ArrayThunk::Waiting).collect(),
+			)),
+		}
 	}
 }
 impl ArrayLike for ExprArray {
 	fn len(&self) -> usize {
-		self.0.cached.borrow().len()
+		self.cached.borrow().len()
 	}
 	fn get(&self, index: usize) -> Result<Option<Val>> {
 		if index >= self.len() {
 			return Ok(None);
 		}
-		match &self.0.cached.borrow()[index] {
+		match &self.cached.borrow()[index] {
 			ArrayThunk::Computed(c) => return Ok(Some(c.clone())),
 			ArrayThunk::Errored(e) => return Err(e.clone()),
 			ArrayThunk::Pending => return Err(InfiniteRecursionDetected.into()),
@@ -179,19 +171,19 @@
 		};
 
 		let ArrayThunk::Waiting(expr) =
-			replace(&mut self.0.cached.borrow_mut()[index], ArrayThunk::Pending)
+			replace(&mut self.cached.borrow_mut()[index], ArrayThunk::Pending)
 		else {
 			unreachable!()
 		};
 
-		let new_value = match evaluate(self.0.ctx.clone(), &expr) {
+		let new_value = match evaluate(self.ctx.clone(), &expr) {
 			Ok(v) => v,
 			Err(e) => {
-				self.0.cached.borrow_mut()[index] = ArrayThunk::Errored(e.clone());
+				self.cached.borrow_mut()[index] = ArrayThunk::Errored(e.clone());
 				return Err(e);
 			}
 		};
-		self.0.cached.borrow_mut()[index] = ArrayThunk::Computed(new_value.clone());
+		self.cached.borrow_mut()[index] = ArrayThunk::Computed(new_value.clone());
 		Ok(Some(new_value))
 	}
 	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
@@ -215,7 +207,7 @@
 		if index >= self.len() {
 			return None;
 		}
-		match &self.0.cached.borrow()[index] {
+		match &self.cached.borrow()[index] {
 			ArrayThunk::Computed(c) => return Some(Thunk::evaluated(c.clone())),
 			ArrayThunk::Errored(e) => return Some(Thunk::errored(e.clone())),
 			ArrayThunk::Waiting(_) | ArrayThunk::Pending => {}
@@ -229,14 +221,12 @@
 	fn get_cheap(&self, _index: usize) -> Option<Val> {
 		None
 	}
-}
-impl From<ExprArray> for ArrValue {
-	fn from(value: ExprArray) -> Self {
-		Self::Expr(value)
+	fn is_cheap(&self) -> bool {
+		false
 	}
 }
 
-#[derive(Trace, Debug, Clone)]
+#[derive(Trace, Debug)]
 pub struct ExtendedArray {
 	pub a: ArrValue,
 	pub b: ArrValue,
@@ -319,15 +309,13 @@
 			self.b.get_cheap(index - self.split)
 		}
 	}
-}
-impl From<ExtendedArray> for ArrValue {
-	fn from(value: ExtendedArray) -> Self {
-		Self::Extended(Cc::new(value))
+	fn is_cheap(&self) -> bool {
+		self.a.is_cheap() && self.b.is_cheap()
 	}
 }
 
-#[derive(Trace, Debug, Clone)]
-pub struct LazyArray(pub Cc<Vec<Thunk<Val>>>);
+#[derive(Trace, Debug)]
+pub struct LazyArray(pub Vec<Thunk<Val>>);
 impl ArrayLike for LazyArray {
 	fn len(&self) -> usize {
 		self.0.len()
@@ -344,15 +332,13 @@
 	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
 		self.0.get(index).cloned()
 	}
-}
-impl From<LazyArray> for ArrValue {
-	fn from(value: LazyArray) -> Self {
-		Self::Lazy(value)
+	fn is_cheap(&self) -> bool {
+		false
 	}
 }
 
-#[derive(Trace, Debug, Clone)]
-pub struct EagerArray(pub Cc<Vec<Val>>);
+#[derive(Trace, Debug)]
+pub struct EagerArray(pub Vec<Val>);
 impl ArrayLike for EagerArray {
 	fn len(&self) -> usize {
 		self.0.len()
@@ -369,15 +355,13 @@
 	fn get_cheap(&self, index: usize) -> Option<Val> {
 		self.0.get(index).cloned()
 	}
-}
-impl From<EagerArray> for ArrValue {
-	fn from(value: EagerArray) -> Self {
-		Self::Eager(value)
+	fn is_cheap(&self) -> bool {
+		true
 	}
 }
 
 /// Inclusive range type
-#[derive(Debug, Trace, Clone, PartialEq, Eq)]
+#[derive(Debug, Trace, PartialEq, Eq)]
 pub struct RangeArray {
 	start: i32,
 	end: i32,
@@ -422,14 +406,12 @@
 	fn get_cheap(&self, index: usize) -> Option<Val> {
 		self.range().nth(index).map(|i| Val::Num(f64::from(i)))
 	}
-}
-impl From<RangeArray> for ArrValue {
-	fn from(value: RangeArray) -> Self {
-		Self::Range(value)
+	fn is_cheap(&self) -> bool {
+		true
 	}
 }
 
-#[derive(Debug, Trace, Clone)]
+#[derive(Debug, Trace)]
 pub struct ReverseArray(pub ArrValue);
 impl ArrayLike for ReverseArray {
 	fn len(&self) -> usize {
@@ -447,44 +429,37 @@
 	fn get_cheap(&self, index: usize) -> Option<Val> {
 		self.0.get_cheap(self.0.len() - index - 1)
 	}
-	fn reverse(self) -> ArrValue {
-		self.0
-	}
-}
-impl From<ReverseArray> for ArrValue {
-	fn from(value: ReverseArray) -> Self {
-		Self::Reverse(Cc::new(value))
+	fn is_cheap(&self) -> bool {
+		self.0.is_cheap()
 	}
 }
 
-#[derive(Trace, Debug)]
-pub struct MappedArrayInner {
+#[derive(Trace, Debug, Clone)]
+pub struct MappedArray {
 	inner: ArrValue,
-	cached: RefCell<Vec<ArrayThunk<()>>>,
+	cached: Cc<RefCell<Vec<ArrayThunk<()>>>>,
 	mapper: FuncVal,
 }
-#[derive(Trace, Debug, Clone)]
-pub struct MappedArray(Cc<MappedArrayInner>);
 impl MappedArray {
 	pub fn new(inner: ArrValue, mapper: FuncVal) -> Self {
 		let len = inner.len();
-		Self(Cc::new(MappedArrayInner {
+		Self {
 			inner,
-			cached: RefCell::new(vec![ArrayThunk::Waiting(()); len]),
+			cached: Cc::new(RefCell::new(vec![ArrayThunk::Waiting(()); len])),
 			mapper,
-		}))
+		}
 	}
 }
 impl ArrayLike for MappedArray {
 	fn len(&self) -> usize {
-		self.0.cached.borrow().len()
+		self.cached.borrow().len()
 	}
 
 	fn get(&self, index: usize) -> Result<Option<Val>> {
 		if index >= self.len() {
 			return Ok(None);
 		}
-		match &self.0.cached.borrow()[index] {
+		match &self.cached.borrow()[index] {
 			ArrayThunk::Computed(c) => return Ok(Some(c.clone())),
 			ArrayThunk::Errored(e) => return Err(e.clone()),
 			ArrayThunk::Pending => return Err(InfiniteRecursionDetected.into()),
@@ -492,27 +467,26 @@
 		};
 
 		let ArrayThunk::Waiting(_) =
-			replace(&mut self.0.cached.borrow_mut()[index], ArrayThunk::Pending)
+			replace(&mut self.cached.borrow_mut()[index], ArrayThunk::Pending)
 		else {
 			unreachable!()
 		};
 
 		let val = self
-			.0
 			.inner
 			.get(index)
 			.transpose()
 			.expect("index checked")
-			.and_then(|r| self.0.mapper.evaluate_simple(&(r,), false));
+			.and_then(|r| self.mapper.evaluate_simple(&(r,), false));
 
 		let new_value = match val {
 			Ok(v) => v,
 			Err(e) => {
-				self.0.cached.borrow_mut()[index] = ArrayThunk::Errored(e.clone());
+				self.cached.borrow_mut()[index] = ArrayThunk::Errored(e.clone());
 				return Err(e);
 			}
 		};
-		self.0.cached.borrow_mut()[index] = ArrayThunk::Computed(new_value.clone());
+		self.cached.borrow_mut()[index] = ArrayThunk::Computed(new_value.clone());
 		Ok(Some(new_value))
 	}
 	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
@@ -536,7 +510,7 @@
 		if index >= self.len() {
 			return None;
 		}
-		match &self.0.cached.borrow()[index] {
+		match &self.cached.borrow()[index] {
 			ArrayThunk::Computed(c) => return Some(Thunk::evaluated(c.clone())),
 			ArrayThunk::Errored(e) => return Some(Thunk::errored(e.clone())),
 			ArrayThunk::Waiting(_) | ArrayThunk::Pending => {}
@@ -551,82 +525,54 @@
 	fn get_cheap(&self, _index: usize) -> Option<Val> {
 		None
 	}
-}
-impl From<MappedArray> for ArrValue {
-	fn from(value: MappedArray) -> Self {
-		Self::Mapped(value)
+	fn is_cheap(&self) -> bool {
+		false
 	}
 }
 
 #[derive(Trace, Debug)]
-pub struct RepeatedArrayInner {
+pub struct RepeatedArray {
 	data: ArrValue,
 	repeats: usize,
 	total_len: usize,
 }
-#[derive(Trace, Debug, Clone)]
-pub struct RepeatedArray(Cc<RepeatedArrayInner>);
 impl RepeatedArray {
 	pub fn new(data: ArrValue, repeats: usize) -> Option<Self> {
 		let total_len = data.len().checked_mul(repeats)?;
-		Some(Self(Cc::new(RepeatedArrayInner {
+		Some(Self {
 			data,
 			repeats,
 			total_len,
-		})))
-	}
-	pub fn is_cheap(&self) -> bool {
-		self.0.data.is_cheap()
+		})
 	}
 }
 
 impl ArrayLike for RepeatedArray {
 	fn len(&self) -> usize {
-		self.0.total_len
+		self.total_len
 	}
 
 	fn get(&self, index: usize) -> Result<Option<Val>> {
-		if index > self.0.total_len {
+		if index > self.total_len {
 			return Ok(None);
 		}
-		self.0.data.get(index % self.0.data.len())
+		self.data.get(index % self.data.len())
 	}
 
 	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
-		if index > self.0.total_len {
+		if index > self.total_len {
 			return None;
 		}
-		self.0.data.get_lazy(index % self.0.data.len())
+		self.data.get_lazy(index % self.data.len())
 	}
 
 	fn get_cheap(&self, index: usize) -> Option<Val> {
-		if index > self.0.total_len {
+		if index > self.total_len {
 			return None;
 		}
-		self.0.data.get_cheap(index % self.0.data.len())
+		self.data.get_cheap(index % self.data.len())
 	}
-}
-impl From<RepeatedArray> for ArrValue {
-	fn from(value: RepeatedArray) -> Self {
-		Self::Repeated(value)
+	fn is_cheap(&self) -> bool {
+		self.data.is_cheap()
 	}
 }
-
-macro_rules! pass {
-	($t:ident.$m:ident($($ident:ident),*)) => {
-		match $t {
-			Self::Bytes(e) => e.$m($($ident)*),
-			Self::Chars(e) => e.$m($($ident)*),
-			Self::Expr(e) => e.$m($($ident)*),
-			Self::Lazy(e) => e.$m($($ident)*),
-			Self::Eager(e) => e.$m($($ident)*),
-			Self::Range(e) => e.$m($($ident)*),
-			Self::Slice(e) => e.$m($($ident)*),
-			Self::Extended(e) => e.$m($($ident)*),
-			Self::Reverse(e) => e.$m($($ident)*),
-			Self::Mapped(e) => e.$m($($ident)*),
-			Self::Repeated(e) => e.$m($($ident)*),
-		}
-	};
-}
-pub(super) use pass;
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/evaluate/mod.rs
1use std::rc::Rc;23use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_parser::{6	ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, FieldMember, FieldName, ForSpecData,7	IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc,8};9use jrsonnet_types::ValType;1011use self::destructure::destruct;12use crate::{13	arr::ArrValue,14	destructure::evaluate_dest,15	error::{suggest_object_fields, ErrorKind::*},16	evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},17	function::{CallLocation, FuncDesc, FuncVal},18	throw,19	typed::Typed,20	val::{CachedUnbound, IndexableVal, StrValue, Thunk, ThunkValue},21	Context, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, State,22	Unbound, Val,23};24pub mod destructure;25pub mod operator;2627pub fn evaluate_trivial(expr: &LocExpr) -> Option<Val> {28	fn is_trivial(expr: &LocExpr) -> bool {29		match &*expr.0 {30			Expr::Str(_)31			| Expr::Num(_)32			| Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,33			Expr::Arr(a) => a.iter().all(is_trivial),34			Expr::Parened(e) => is_trivial(e),35			_ => false,36		}37	}38	Some(match &*expr.0 {39		Expr::Str(s) => Val::Str(StrValue::Flat(s.clone())),40		Expr::Num(n) => Val::Num(*n),41		Expr::Literal(LiteralType::False) => Val::Bool(false),42		Expr::Literal(LiteralType::True) => Val::Bool(true),43		Expr::Literal(LiteralType::Null) => Val::Null,44		Expr::Arr(n) => {45			if n.iter().any(|e| !is_trivial(e)) {46				return None;47			}48			Val::Arr(ArrValue::eager(49				n.iter()50					.map(evaluate_trivial)51					.map(|e| e.expect("checked trivial"))52					.collect(),53			))54		}55		Expr::Parened(e) => evaluate_trivial(e)?,56		_ => return None,57	})58}5960pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {61	Val::Func(FuncVal::Normal(Cc::new(FuncDesc {62		name,63		ctx,64		params,65		body,66	})))67}6869pub fn evaluate_field_name(ctx: Context, field_name: &FieldName) -> Result<Option<IStr>> {70	Ok(match field_name {71		FieldName::Fixed(n) => Some(n.clone()),72		FieldName::Dyn(expr) => State::push(73			CallLocation::new(&expr.1),74			|| "evaluating field name".to_string(),75			|| {76				let value = evaluate(ctx, expr)?;77				if matches!(value, Val::Null) {78					Ok(None)79				} else {80					Ok(Some(IStr::from_untyped(value)?))81				}82			},83		)?,84	})85}8687pub fn evaluate_comp(88	ctx: Context,89	specs: &[CompSpec],90	callback: &mut impl FnMut(Context) -> Result<()>,91) -> Result<()> {92	match specs.get(0) {93		None => callback(ctx)?,94		Some(CompSpec::IfSpec(IfSpecData(cond))) => {95			if bool::from_untyped(evaluate(ctx.clone(), cond)?)? {96				evaluate_comp(ctx, &specs[1..], callback)?;97			}98		}99		Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(ctx.clone(), expr)? {100			Val::Arr(list) => {101				for item in list.iter_lazy() {102					let fctx = Pending::new();103					let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());104					destruct(var, item, fctx.clone(), &mut new_bindings)?;105					let ctx = ctx106						.clone()107						.extend(new_bindings, None, None, None)108						.into_future(fctx);109110					evaluate_comp(ctx, &specs[1..], callback)?;111				}112			}113			#[cfg(feature = "exp-object-iteration")]114			Val::Obj(obj) => {115				for field in obj.fields(116					// TODO: Should there be ability to preserve iteration order?117					#[cfg(feature = "exp-preserve-order")]118					false,119				) {120					#[derive(Trace)]121					struct ObjectFieldThunk {122						obj: ObjValue,123						field: IStr,124					}125					impl ThunkValue for ObjectFieldThunk {126						type Output = Val;127128						fn get(self: Box<Self>) -> Result<Self::Output> {129							self.obj.get(self.field).transpose().expect(130								"field exists, as field name was obtained from object.fields()",131							)132						}133					}134135					let fctx = Pending::new();136					let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());137					let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(Cc::new(vec![138						Thunk::evaluated(Val::Str(StrValue::Flat(field.clone()))),139						Thunk::new(ObjectFieldThunk {140							field: field.clone(),141							obj: obj.clone(),142						}),143					]))));144					destruct(var, value, fctx.clone(), &mut new_bindings)?;145					let ctx = ctx146						.clone()147						.extend(new_bindings, None, None, None)148						.into_future(fctx);149150					evaluate_comp(ctx, &specs[1..], callback)?;151				}152			}153			_ => throw!(InComprehensionCanOnlyIterateOverArray),154		},155	}156	Ok(())157}158159trait CloneableUnbound<T>: Unbound<Bound = T> + Clone {}160impl<V, T> CloneableUnbound<T> for V where V: Unbound<Bound = T> + Clone {}161162fn evaluate_object_locals(163	fctx: Pending<Context>,164	locals: Rc<Vec<BindSpec>>,165) -> impl CloneableUnbound<Context> {166	#[derive(Trace, Clone)]167	struct UnboundLocals {168		fctx: Pending<Context>,169		locals: Rc<Vec<BindSpec>>,170	}171	impl Unbound for UnboundLocals {172		type Bound = Context;173174		fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Context> {175			let fctx = Context::new_future();176			let mut new_bindings =177				GcHashMap::with_capacity(self.locals.iter().map(BindSpec::capacity_hint).sum());178			for b in self.locals.iter() {179				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;180			}181182			let ctx = self.fctx.unwrap();183			let new_dollar = ctx.dollar().cloned().or_else(|| this.clone());184185			let ctx = ctx186				.extend(new_bindings, new_dollar, sup, this)187				.into_future(fctx);188189			Ok(ctx)190		}191	}192193	UnboundLocals { fctx, locals }194}195196pub fn evaluate_field_member<B: Unbound<Bound = Context> + Clone>(197	builder: &mut ObjValueBuilder,198	ctx: Context,199	uctx: B,200	field: &FieldMember,201) -> Result<()> {202	let name = evaluate_field_name(ctx, &field.name)?;203	let Some(name) = name else {204		return Ok(());205	};206207	match field {208		FieldMember {209			plus,210			params: None,211			visibility,212			value,213			..214		} => {215			#[derive(Trace)]216			struct UnboundValue<B: Trace> {217				uctx: B,218				value: LocExpr,219				name: IStr,220			}221			impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {222				type Bound = Val;223				fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {224					evaluate_named(self.uctx.bind(sup, this)?, &self.value, self.name.clone())225				}226			}227228			builder229				.member(name.clone())230				.with_add(*plus)231				.with_visibility(*visibility)232				.with_location(value.1.clone())233				.bindable(UnboundValue {234					uctx,235					value: value.clone(),236					name,237				})?;238		}239		FieldMember {240			params: Some(params),241			visibility,242			value,243			..244		} => {245			#[derive(Trace)]246			struct UnboundMethod<B: Trace> {247				uctx: B,248				value: LocExpr,249				params: ParamsDesc,250				name: IStr,251			}252			impl<B: Unbound<Bound = Context>> Unbound for UnboundMethod<B> {253				type Bound = Val;254				fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {255					Ok(evaluate_method(256						self.uctx.bind(sup, this)?,257						self.name.clone(),258						self.params.clone(),259						self.value.clone(),260					))261				}262			}263264			builder265				.member(name.clone())266				.with_visibility(*visibility)267				.with_location(value.1.clone())268				.bindable(UnboundMethod {269					uctx,270					value: value.clone(),271					params: params.clone(),272					name,273				})?;274		}275	}276	Ok(())277}278279#[allow(clippy::too_many_lines)]280pub fn evaluate_member_list_object(ctx: Context, members: &[Member]) -> Result<ObjValue> {281	let mut builder = ObjValueBuilder::new();282	let locals = Rc::new(283		members284			.iter()285			.filter_map(|m| match m {286				Member::BindStmt(bind) => Some(bind.clone()),287				_ => None,288			})289			.collect::<Vec<_>>(),290	);291292	let fctx = Context::new_future();293294	// We have single context for all fields, so we can cache binds295	let uctx = CachedUnbound::new(evaluate_object_locals(fctx.clone(), locals));296297	for member in members {298		match member {299			Member::Field(field) => {300				evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;301			}302			Member::AssertStmt(stmt) => {303				#[derive(Trace)]304				struct ObjectAssert<B: Trace> {305					uctx: B,306					assert: AssertStmt,307				}308				impl<B: Unbound<Bound = Context>> ObjectAssertion for ObjectAssert<B> {309					fn run(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<()> {310						let ctx = self.uctx.bind(sup, this)?;311						evaluate_assert(ctx, &self.assert)312					}313				}314				builder.assert(ObjectAssert {315					uctx: uctx.clone(),316					assert: stmt.clone(),317				});318			}319			Member::BindStmt(_) => {320				// Already handled321			}322		}323	}324	let this = builder.build();325	fctx.fill(ctx.extend(GcHashMap::new(), None, None, Some(this.clone())));326	Ok(this)327}328329pub fn evaluate_object(ctx: Context, object: &ObjBody) -> Result<ObjValue> {330	Ok(match object {331		ObjBody::MemberList(members) => evaluate_member_list_object(ctx, members)?,332		ObjBody::ObjComp(obj) => {333			let mut builder = ObjValueBuilder::new();334			let locals = Rc::new(335				obj.pre_locals336					.iter()337					.chain(obj.post_locals.iter())338					.cloned()339					.collect::<Vec<_>>(),340			);341			let mut ctxs = vec![];342			evaluate_comp(ctx, &obj.compspecs, &mut |ctx| {343				let fctx = Context::new_future();344				ctxs.push((ctx.clone(), fctx.clone()));345				let uctx = evaluate_object_locals(fctx, locals.clone());346347				evaluate_field_member(&mut builder, ctx, uctx, &obj.field)348			})?;349350			let this = builder.build();351			for (ctx, fctx) in ctxs {352				let _ctx = ctx353					.extend(GcHashMap::new(), None, None, Some(this.clone()))354					.into_future(fctx);355			}356			this357		}358	})359}360361pub fn evaluate_apply(362	ctx: Context,363	value: &LocExpr,364	args: &ArgsDesc,365	loc: CallLocation<'_>,366	tailstrict: bool,367) -> Result<Val> {368	let value = evaluate(ctx.clone(), value)?;369	Ok(match value {370		Val::Func(f) => {371			let body = || f.evaluate(ctx, loc, args, tailstrict);372			if tailstrict {373				body()?374			} else {375				State::push(loc, || format!("function <{}> call", f.name()), body)?376			}377		}378		v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),379	})380}381382pub fn evaluate_assert(ctx: Context, assertion: &AssertStmt) -> Result<()> {383	let value = &assertion.0;384	let msg = &assertion.1;385	let assertion_result = State::push(386		CallLocation::new(&value.1),387		|| "assertion condition".to_owned(),388		|| bool::from_untyped(evaluate(ctx.clone(), value)?),389	)?;390	if !assertion_result {391		State::push(392			CallLocation::new(&value.1),393			|| "assertion failure".to_owned(),394			|| {395				if let Some(msg) = msg {396					throw!(AssertionFailed(evaluate(ctx, msg)?.to_string()?));397				}398				throw!(AssertionFailed(Val::Null.to_string()?));399			},400		)?;401	}402	Ok(())403}404405pub fn evaluate_named(ctx: Context, expr: &LocExpr, name: IStr) -> Result<Val> {406	use Expr::*;407	let LocExpr(raw_expr, _loc) = expr;408	Ok(match &**raw_expr {409		Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),410		_ => evaluate(ctx, expr)?,411	})412}413414#[allow(clippy::too_many_lines)]415pub fn evaluate(ctx: Context, expr: &LocExpr) -> Result<Val> {416	use Expr::*;417418	if let Some(trivial) = evaluate_trivial(expr) {419		return Ok(trivial);420	}421	let LocExpr(expr, loc) = expr;422	Ok(match &**expr {423		Literal(LiteralType::This) => {424			Val::Obj(ctx.this().ok_or(CantUseSelfOutsideOfObject)?.clone())425		}426		Literal(LiteralType::Super) => Val::Obj(427			ctx.super_obj().ok_or(NoSuperFound)?.with_this(428				ctx.this()429					.expect("if super exists - then this should too")430					.clone(),431			),432		),433		Literal(LiteralType::Dollar) => {434			Val::Obj(ctx.dollar().ok_or(NoTopLevelObjectFound)?.clone())435		}436		Literal(LiteralType::True) => Val::Bool(true),437		Literal(LiteralType::False) => Val::Bool(false),438		Literal(LiteralType::Null) => Val::Null,439		Parened(e) => evaluate(ctx, e)?,440		Str(v) => Val::Str(StrValue::Flat(v.clone())),441		Num(v) => Val::new_checked_num(*v)?,442		BinaryOp(v1, o, v2) => evaluate_binary_op_special(ctx, v1, *o, v2)?,443		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,444		Var(name) => State::push(445			CallLocation::new(loc),446			|| format!("variable <{name}> access"),447			|| ctx.binding(name.clone())?.evaluate(),448		)?,449		Index {450			indexable: LocExpr(v, _),451			index,452			#[cfg(feature = "exp-null-coaelse")]453			null_coaelse,454		} if matches!(&**v, Expr::Literal(LiteralType::Super)) => {455			let name = evaluate(ctx.clone(), index)?;456			let Val::Str(name) = name else {457				throw!(ValueIndexMustBeTypeGot(458					ValType::Obj,459					ValType::Str,460					name.value_type(),461				))462			};463			let Some(super_obj) = ctx.super_obj() else {464				#[cfg(feature = "exp-null-coaelse")]465				if *null_coaelse {466					return Ok(Val::Null);467				}468				throw!(NoSuperFound)469			};470			let this = ctx471				.this()472				.expect("no this found, while super present, should not happen");473			let key = name.into_flat();474			match super_obj.get_for(key.clone(), this.clone())? {475				Some(v) => v,476				#[cfg(feature = "exp-null-coaelse")]477				None if *null_coaelse => Val::Null,478				None => {479					let suggestions = suggest_object_fields(super_obj, key.clone());480481					throw!(NoSuchField(key, suggestions))482				}483			}484		}485		Index {486			indexable,487			index,488			#[cfg(feature = "exp-null-coaelse")]489			null_coaelse,490		} => match (evaluate(ctx.clone(), indexable)?, evaluate(ctx, index)?) {491			(Val::Obj(v), Val::Str(key)) => State::push(492				CallLocation::new(loc),493				|| format!("field <{key}> access"),494				|| match v.get(key.clone().into_flat()) {495					Ok(Some(v)) => Ok(v),496					#[cfg(feature = "exp-null-coaelse")]497					Ok(None) if *null_coaelse => Ok(Val::Null),498					Ok(None) => {499						let suggestions = suggest_object_fields(&v, key.clone().into_flat());500501						throw!(NoSuchField(key.clone().into_flat(), suggestions))502					}503					Err(e) => Err(e),504				},505			)?,506			(Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(507				ValType::Obj,508				ValType::Str,509				n.value_type(),510			)),511512			(Val::Arr(v), Val::Num(n)) => {513				if n.fract() > f64::EPSILON {514					throw!(FractionalIndex)515				}516				v.get(n as usize)?517					.ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?518			}519			(Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n.into_flat())),520			(Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(521				ValType::Arr,522				ValType::Num,523				n.value_type(),524			)),525526			(Val::Str(s), Val::Num(n)) => Val::Str({527				let v: IStr = s528					.clone()529					.into_flat()530					.chars()531					.skip(n as usize)532					.take(1)533					.collect::<String>()534					.into();535				if v.is_empty() {536					let size = s.into_flat().chars().count();537					throw!(StringBoundsError(n as usize, size))538				}539				StrValue::Flat(v)540			}),541			(Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(542				ValType::Str,543				ValType::Num,544				n.value_type(),545			)),546			#[cfg(feature = "exp-null-coaelse")]547			(Val::Null, _) if *null_coaelse => Val::Null,548549			(v, _) => throw!(CantIndexInto(v.value_type())),550		},551		LocalExpr(bindings, returned) => {552			let mut new_bindings: GcHashMap<IStr, Thunk<Val>> =553				GcHashMap::with_capacity(bindings.iter().map(BindSpec::capacity_hint).sum());554			let fctx = Context::new_future();555			for b in bindings {556				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;557			}558			let ctx = ctx.extend(new_bindings, None, None, None).into_future(fctx);559			evaluate(ctx, &returned.clone())?560		}561		Arr(items) => {562			if items.is_empty() {563				Val::Arr(ArrValue::empty())564			} else if items.len() == 1 {565				#[derive(Trace)]566				struct ArrayElement {567					ctx: Context,568					item: LocExpr,569				}570				impl ThunkValue for ArrayElement {571					type Output = Val;572					fn get(self: Box<Self>) -> Result<Val> {573						evaluate(self.ctx, &self.item)574					}575				}576				Val::Arr(ArrValue::lazy(Cc::new(vec![Thunk::new(ArrayElement {577					ctx,578					item: items[0].clone(),579				})])))580			} else {581				Val::Arr(ArrValue::expr(ctx, items.iter().cloned()))582			}583		}584		ArrComp(expr, comp_specs) => {585			let mut out = Vec::new();586			evaluate_comp(ctx, comp_specs, &mut |ctx| {587				out.push(evaluate(ctx, expr)?);588				Ok(())589			})?;590			Val::Arr(ArrValue::eager(out))591		}592		Obj(body) => Val::Obj(evaluate_object(ctx, body)?),593		ObjExtend(a, b) => evaluate_add_op(594			&evaluate(ctx.clone(), a)?,595			&Val::Obj(evaluate_object(ctx, b)?),596		)?,597		Apply(value, args, tailstrict) => {598			evaluate_apply(ctx, value, args, CallLocation::new(loc), *tailstrict)?599		}600		Function(params, body) => {601			evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())602		}603		AssertExpr(assert, returned) => {604			evaluate_assert(ctx.clone(), assert)?;605			evaluate(ctx, returned)?606		}607		ErrorStmt(e) => State::push(608			CallLocation::new(loc),609			|| "error statement".to_owned(),610			|| throw!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),611		)?,612		IfElse {613			cond,614			cond_then,615			cond_else,616		} => {617			if State::push(618				CallLocation::new(loc),619				|| "if condition".to_owned(),620				|| bool::from_untyped(evaluate(ctx.clone(), &cond.0)?),621			)? {622				evaluate(ctx, cond_then)?623			} else {624				match cond_else {625					Some(v) => evaluate(ctx, v)?,626					None => Val::Null,627				}628			}629		}630		Slice(value, desc) => {631			fn parse_idx<T: Typed>(632				loc: CallLocation<'_>,633				ctx: &Context,634				expr: Option<&LocExpr>,635				desc: &'static str,636			) -> Result<Option<T>> {637				if let Some(value) = expr {638					Ok(Some(State::push(639						loc,640						|| format!("slice {desc}"),641						|| T::from_untyped(evaluate(ctx.clone(), value)?),642					)?))643				} else {644					Ok(None)645				}646			}647648			let indexable = evaluate(ctx.clone(), value)?;649			let loc = CallLocation::new(loc);650651			let start = parse_idx(loc, &ctx, desc.start.as_ref(), "start")?;652			let end = parse_idx(loc, &ctx, desc.end.as_ref(), "end")?;653			let step = parse_idx(loc, &ctx, desc.step.as_ref(), "step")?;654655			IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?656		}657		i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {658			let Expr::Str(path) = &*path.0 else {659				throw!("computed imports are not supported")660			};661			let tmp = loc.clone().0;662			let s = ctx.state();663			let resolved_path = s.resolve_from(tmp.source_path(), path as &str)?;664			match i {665				Import(_) => State::push(666					CallLocation::new(loc),667					|| format!("import {:?}", path.clone()),668					|| s.import_resolved(resolved_path),669				)?,670				ImportStr(_) => Val::Str(StrValue::Flat(s.import_resolved_str(resolved_path)?)),671				ImportBin(_) => Val::Arr(ArrValue::bytes(s.import_resolved_bin(resolved_path)?)),672				_ => unreachable!(),673			}674		}675	})676}
modifiedcrates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -17,7 +17,7 @@
 	operator::evaluate_add_op,
 	tb, throw,
 	val::ThunkValue,
-	MaybeUnbound, Result, ResultExt, State, Thunk, Unbound, Val,
+	MaybeUnbound, Result, State, Thunk, Unbound, Val,
 };
 
 #[cfg(not(feature = "exp-preserve-order"))]
modifiedcrates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -6,7 +6,7 @@
 use jrsonnet_types::{ComplexValType, ValType};
 
 use crate::{
-	arr::ArrValue,
+	arr::{ArrValue, BytesArray},
 	error::Result,
 	function::{native::NativeDesc, FuncDesc, FuncVal},
 	throw,
@@ -434,12 +434,13 @@
 	}
 
 	fn from_untyped(value: Val) -> Result<Self> {
-		if let Val::Arr(ArrValue::Bytes(bytes)) = value {
-			return Ok(bytes.0);
-		}
-		<Self as Typed>::TYPE.check(&value)?;
-		match value {
+		match &value {
 			Val::Arr(a) => {
+				if let Some(bytes) = a.as_any().downcast_ref::<BytesArray>() {
+					return Ok(bytes.0.as_slice().into());
+				};
+				<Self as Typed>::TYPE.check(&value)?;
+				// Any::downcast_ref::<ByteArray>(&a);
 				let mut out = Vec::with_capacity(a.len());
 				for e in a.iter() {
 					let r = e?;
@@ -447,7 +448,10 @@
 				}
 				Ok(out.as_slice().into())
 			}
-			_ => unreachable!(),
+			_ => {
+				<Self as Typed>::TYPE.check(&value)?;
+				unreachable!()
+			}
 		}
 	}
 }
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -9,7 +9,7 @@
 use jrsonnet_interner::IStr;
 use jrsonnet_types::ValType;
 
-pub use crate::arr::ArrValue;
+pub use crate::arr::{ArrValue, ArrayLike};
 use crate::{
 	error::{Error, ErrorKind::*},
 	function::FuncVal,
modifiedcrates/jrsonnet-stdlib/src/sets.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/sets.rs
+++ b/crates/jrsonnet-stdlib/src/sets.rs
@@ -7,7 +7,6 @@
 	val::ArrValue,
 	Thunk, Val,
 };
-use jrsonnet_gcmodule::Cc;
 use jrsonnet_parser::BinaryOpType;
 
 #[builtin]
@@ -70,5 +69,5 @@
 			}
 		};
 	}
-	Ok(ArrValue::lazy(Cc::new(out)))
+	Ok(ArrValue::lazy(out))
 }
modifiedcrates/jrsonnet-stdlib/src/sort.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/sort.rs
+++ b/crates/jrsonnet-stdlib/src/sort.rs
@@ -10,7 +10,6 @@
 	val::{equals, ArrValue},
 	Thunk, Val,
 };
-use jrsonnet_gcmodule::Cc;
 use jrsonnet_parser::BinaryOpType;
 
 use crate::eval_on_empty;
@@ -136,7 +135,7 @@
 			values.iter().collect::<Result<Vec<Val>>>()?,
 		)?))
 	} else {
-		Ok(ArrValue::lazy(Cc::new(sort_keyf(values, key_getter)?)))
+		Ok(ArrValue::lazy(sort_keyf(values, key_getter)?))
 	}
 }
 
@@ -186,7 +185,7 @@
 			arr.iter().collect::<Result<Vec<Val>>>()?,
 		)?))
 	} else {
-		Ok(ArrValue::lazy(Cc::new(uniq_keyf(arr, keyF)?)))
+		Ok(ArrValue::lazy(uniq_keyf(arr, keyF)?))
 	}
 }
 
@@ -204,8 +203,8 @@
 		Ok(ArrValue::eager(arr))
 	} else {
 		let arr = sort_keyf(arr, keyF.clone())?;
-		let arr = uniq_keyf(ArrValue::lazy(Cc::new(arr)), keyF)?;
-		Ok(ArrValue::lazy(Cc::new(arr)))
+		let arr = uniq_keyf(ArrValue::lazy(arr), keyF)?;
+		Ok(ArrValue::lazy(arr))
 	}
 }