git.delta.rocks / jrsonnet / refs/commits / ab385dd4fde0

difftreelog

feat(jrsonnet-evaluator) implement gc

Yaroslav Bolyukin2021-06-05parent: #e2e1f7b.patch.diff
in: master
Some manual Trace/Finalize implementations can be replaced with derives
with https://github.com/Manishearth/rust-gc/pull/116 getting merged

17 files changed

modifiedcrates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -30,13 +30,12 @@
 jrsonnet-types = { path = "../jrsonnet-types", version = "0.3.7" }
 pathdiff = "0.2.0"
 
-closure = "0.3.0"
-
 md5 = "0.7.0"
 base64 = "0.13.0"
 rustc-hash = "1.1.0"
 
 thiserror = "1.0"
+gc = { version = "0.4.1", features = ["derive"] }
 
 [dependencies.anyhow]
 version = "1.0"
modifiedcrates/jrsonnet-evaluator/src/builtin/format.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/format.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/format.rs
@@ -2,11 +2,12 @@
 #![allow(clippy::too_many_arguments)]
 
 use crate::{error::Error::*, throw, LocError, ObjValue, Result, Val};
+use gc::{Finalize, Trace};
 use jrsonnet_interner::IStr;
 use jrsonnet_types::ValType;
 use thiserror::Error;
 
-#[derive(Debug, Clone, Error)]
+#[derive(Debug, Clone, Error, Trace, Finalize)]
 pub enum FormatError {
 	#[error("truncated format code")]
 	TruncatedFormatCode,
modifiedcrates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/manifest.rs
@@ -126,6 +126,7 @@
 			buf.push('}');
 		}
 		Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),
+		Val::DebugGcTraceValue(v) => manifest_json_ex_buf(&v.value, buf, cur_padding, options)?,
 	};
 	Ok(())
 }
modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -1,10 +1,11 @@
 use crate::{
 	equals,
 	error::{Error::*, Result},
-	parse_args, primitive_equals, push, throw, with_state, ArrValue, Context, EvaluationState,
-	FuncVal, LazyVal, Val,
+	parse_args, primitive_equals, push, throw, with_state, ArrValue, Context, DebugGcTraceValue,
+	EvaluationState, FuncVal, LazyVal, Val,
 };
 use format::{format_arr, format_obj};
+use gc::Gc;
 use jrsonnet_interner::IStr;
 use jrsonnet_parser::{ArgsDesc, BinaryOpType, ExprLocation};
 use jrsonnet_types::ty;
@@ -68,6 +69,8 @@
 			("md5".into(), builtin_md5),
 			("base64".into(), builtin_base64),
 			("trace".into(), builtin_trace),
+			("gc".into(), builtin_gc),
+			("gcTrace".into(), builtin_gc_trace),
 			("join".into(), builtin_join),
 			("escapeStringJson".into(), builtin_escape_string_json),
 			("manifestJsonEx".into(), builtin_manifest_json_ex),
@@ -301,7 +304,7 @@
 	parse_args!(context, "native", args, 1, [
 		0, x: ty!(string) => Val::Str;
 	], {
-		Ok(with_state(|s| s.settings().ext_natives.get(&x).cloned()).map(|v| Val::Func(Rc::new(FuncVal::NativeExt(x.clone(), v)))).ok_or(UndefinedExternalFunction(x))?)
+		Ok(with_state(|s| s.settings().ext_natives.get(&x).cloned()).map(|v| Val::Func(Gc::new(FuncVal::NativeExt(x.clone(), v)))).ok_or(UndefinedExternalFunction(x))?)
 	})
 }
 
@@ -446,6 +449,28 @@
 	})
 }
 
+fn builtin_gc(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
+	parse_args!(context, "gc", args, 1, [
+		0, rest: ty!(any);
+	], {
+		println!("GC start");
+		gc::force_collect();
+		println!("GC done");
+
+		Ok(rest)
+	})
+}
+
+fn builtin_gc_trace(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
+	parse_args!(context, "gcTrace", args, 2, [
+		0, name: ty!(string) => Val::Str;
+		1, rest: ty!(any);
+	], {
+
+		Ok(DebugGcTraceValue::new(name, rest))
+	})
+}
+
 fn builtin_base64(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
 	parse_args!(context, "base64", args, 1, [
 		0, input: ty!((string | (Array<number>)));
modifiedcrates/jrsonnet-evaluator/src/builtin/sort.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/sort.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/sort.rs
@@ -2,9 +2,9 @@
 	error::{Error, LocError, Result},
 	throw, Context, FuncVal, Val,
 };
-use std::rc::Rc;
+use gc::{Finalize, Gc, Trace};
 
-#[derive(Debug, Clone, thiserror::Error)]
+#[derive(Debug, Clone, thiserror::Error, Trace, Finalize)]
 pub enum SortError {
 	#[error("sort key should be string or number")]
 	SortKeyShouldBeStringOrNumber,
@@ -59,13 +59,13 @@
 	Ok(sort_type)
 }
 
-pub fn sort(ctx: Context, mut values: Rc<Vec<Val>>, key_getter: &FuncVal) -> Result<Rc<Vec<Val>>> {
+pub fn sort(ctx: Context, values: Gc<Vec<Val>>, key_getter: &FuncVal) -> Result<Gc<Vec<Val>>> {
 	if values.len() <= 1 {
 		return Ok(values);
 	}
 	if key_getter.is_ident() {
-		let mvalues = Rc::make_mut(&mut values);
-		let sort_type = get_sort_type(mvalues, |k| k)?;
+		let mut mvalues = (*values).clone();
+		let sort_type = get_sort_type(&mut mvalues, |k| k)?;
 		match sort_type {
 			SortKeyType::Number => mvalues.sort_by_key(|v| match v {
 				Val::Num(n) => NonNaNf64(*n),
@@ -77,7 +77,7 @@
 			}),
 			SortKeyType::Unknown => unreachable!(),
 		};
-		Ok(values)
+		Ok(Gc::new(mvalues))
 	} else {
 		let mut vk = Vec::with_capacity(values.len());
 		for value in values.iter() {
@@ -98,6 +98,6 @@
 			}),
 			SortKeyType::Unknown => unreachable!(),
 		};
-		Ok(Rc::new(vk.into_iter().map(|v| v.0).collect()))
+		Ok(Gc::new(vk.into_iter().map(|v| v.0).collect()))
 	}
 }
modifiedcrates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -1,13 +1,14 @@
 use crate::{
-	error::Error::*, map::LayeredHashMap, resolved_lazy_val, FutureWrapper, LazyBinding, LazyVal,
-	ObjValue, Result, Val,
+	error::Error::*, map::LayeredHashMap, FutureWrapper, LazyBinding, LazyVal, ObjValue, Result,
+	Val,
 };
+use gc::{Finalize, Gc, Trace};
 use jrsonnet_interner::IStr;
 use rustc_hash::FxHashMap;
+use std::fmt::Debug;
 use std::hash::BuildHasherDefault;
-use std::{fmt::Debug, rc::Rc};
 
-#[derive(Clone)]
+#[derive(Clone, Trace, Finalize)]
 pub struct ContextCreator(pub Context, pub FutureWrapper<FxHashMap<IStr, LazyBinding>>);
 impl ContextCreator {
 	pub fn create(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<Context> {
@@ -20,6 +21,7 @@
 	}
 }
 
+#[derive(Trace, Finalize)]
 struct ContextInternals {
 	dollar: Option<ObjValue>,
 	this: Option<ObjValue>,
@@ -28,15 +30,12 @@
 }
 impl Debug for ContextInternals {
 	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-		f.debug_struct("Context")
-			.field("this", &self.this.as_ref().map(|e| Rc::as_ptr(&e.0)))
-			.field("bindings", &self.bindings)
-			.finish()
+		f.debug_struct("Context").finish()
 	}
 }
 
-#[derive(Debug, Clone)]
-pub struct Context(Rc<ContextInternals>);
+#[derive(Debug, Clone, Trace, Finalize)]
+pub struct Context(Gc<ContextInternals>);
 impl Context {
 	pub fn new_future() -> FutureWrapper<Self> {
 		FutureWrapper::new()
@@ -55,7 +54,7 @@
 	}
 
 	pub fn new() -> Self {
-		Self(Rc::new(ContextInternals {
+		Self(Gc::new(ContextInternals {
 			dollar: None,
 			this: None,
 			super_obj: None,
@@ -81,7 +80,7 @@
 	pub fn with_var(self, name: IStr, value: Val) -> Self {
 		let mut new_bindings =
 			FxHashMap::with_capacity_and_hasher(1, BuildHasherDefault::default());
-		new_bindings.insert(name, resolved_lazy_val!(value));
+		new_bindings.insert(name, LazyVal::new_resolved(value));
 		self.extend(new_bindings, None, None, None)
 	}
 
@@ -96,40 +95,21 @@
 		new_this: Option<ObjValue>,
 		new_super_obj: Option<ObjValue>,
 	) -> Self {
-		match Rc::try_unwrap(self.0) {
-			Ok(mut ctx) => {
-				// Extended context aren't used by anything else, we can freely mutate it without cloning
-				if let Some(dollar) = new_dollar {
-					ctx.dollar = Some(dollar);
-				}
-				if let Some(this) = new_this {
-					ctx.this = Some(this);
-				}
-				if let Some(super_obj) = new_super_obj {
-					ctx.super_obj = Some(super_obj);
-				}
-				if !new_bindings.is_empty() {
-					ctx.bindings = ctx.bindings.extend(new_bindings);
-				}
-				Self(Rc::new(ctx))
-			}
-			Err(ctx) => {
-				let dollar = new_dollar.or_else(|| ctx.dollar.clone());
-				let this = new_this.or_else(|| ctx.this.clone());
-				let super_obj = new_super_obj.or_else(|| ctx.super_obj.clone());
-				let bindings = if new_bindings.is_empty() {
-					ctx.bindings.clone()
-				} else {
-					ctx.bindings.clone().extend(new_bindings)
-				};
-				Self(Rc::new(ContextInternals {
-					dollar,
-					this,
-					super_obj,
-					bindings,
-				}))
-			}
-		}
+		let ctx = &self.0;
+		let dollar = new_dollar.or_else(|| ctx.dollar.clone());
+		let this = new_this.or_else(|| ctx.this.clone());
+		let super_obj = new_super_obj.or_else(|| ctx.super_obj.clone());
+		let bindings = if new_bindings.is_empty() {
+			ctx.bindings.clone()
+		} else {
+			ctx.bindings.clone().extend(new_bindings)
+		};
+		Self(Gc::new(ContextInternals {
+			dollar,
+			this,
+			super_obj,
+			bindings,
+		}))
 	}
 	pub fn extend_bound(self, new_bindings: FxHashMap<IStr, LazyVal>) -> Self {
 		let new_this = self.0.this.clone();
@@ -166,22 +146,6 @@
 
 impl PartialEq for Context {
 	fn eq(&self, other: &Self) -> bool {
-		Rc::ptr_eq(&self.0, &other.0)
-	}
-}
-
-#[cfg(feature = "unstable")]
-#[derive(Debug, Clone)]
-pub struct WeakContext(std::rc::Weak<ContextInternals>);
-#[cfg(feature = "unstable")]
-impl WeakContext {
-	pub fn upgrade(&self) -> Context {
-		Context(self.0.upgrade().expect("context is removed"))
-	}
-}
-#[cfg(feature = "unstable")]
-impl PartialEq for WeakContext {
-	fn eq(&self, other: &Self) -> bool {
-		self.0.ptr_eq(&other.0)
+		Gc::ptr_eq(&self.0, &other.0)
 	}
 }
modifiedcrates/jrsonnet-evaluator/src/dynamic.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/dynamic.rs
+++ b/crates/jrsonnet-evaluator/src/dynamic.rs
@@ -1,23 +1,23 @@
-use std::{cell::RefCell, rc::Rc};
+use gc::{Finalize, Gc, GcCell, Trace};
 
-#[derive(Clone)]
-pub struct FutureWrapper<V>(pub Rc<RefCell<Option<V>>>);
-impl<T> FutureWrapper<T> {
+#[derive(Clone, Trace, Finalize)]
+pub struct FutureWrapper<V: Trace + 'static>(pub Gc<GcCell<Option<V>>>);
+impl<T: Trace + 'static> FutureWrapper<T> {
 	pub fn new() -> Self {
-		Self(Rc::new(RefCell::new(None)))
+		Self(Gc::new(GcCell::new(None)))
 	}
 	pub fn fill(self, value: T) {
 		assert!(self.0.borrow().is_none(), "wrapper is filled already");
 		self.0.borrow_mut().replace(value);
 	}
 }
-impl<T: Clone> FutureWrapper<T> {
+impl<T: Clone + Trace + 'static> FutureWrapper<T> {
 	pub fn unwrap(&self) -> T {
 		self.0.borrow().as_ref().cloned().unwrap()
 	}
 }
 
-impl<T> Default for FutureWrapper<T> {
+impl<T: Trace + 'static> Default for FutureWrapper<T> {
 	fn default() -> Self {
 		Self::new()
 	}
modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -2,13 +2,14 @@
 	builtin::{format::FormatError, sort::SortError},
 	typed::TypeLocError,
 };
+use gc::{Finalize, Trace};
 use jrsonnet_interner::IStr;
 use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};
 use jrsonnet_types::ValType;
 use std::{path::PathBuf, rc::Rc};
 use thiserror::Error;
 
-#[derive(Error, Debug, Clone)]
+#[derive(Error, Debug, Clone, Trace, Finalize)]
 pub enum Error {
 	#[error("intrinsic not found: {0}")]
 	IntrinsicNotFound(IStr),
@@ -88,6 +89,7 @@
 	ImportSyntaxError {
 		path: Rc<PathBuf>,
 		source_code: IStr,
+		#[unsafe_ignore_trace]
 		error: Box<jrsonnet_parser::ParseError>,
 	},
 
@@ -95,6 +97,8 @@
 	RuntimeError(IStr),
 	#[error("stack overflow, try to reduce recursion, or set --max-stack to bigger value")]
 	StackOverflow,
+	#[error("infinite recursion detected")]
+	RecursiveLazyValueEvaluation,
 	#[error("tried to index by fractional value")]
 	FractionalIndex,
 	#[error("attempted to divide by zero")]
@@ -142,15 +146,15 @@
 	}
 }
 
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, Trace, Finalize)]
 pub struct StackTraceElement {
 	pub location: Option<ExprLocation>,
 	pub desc: String,
 }
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Trace, Finalize)]
 pub struct StackTrace(pub Vec<StackTraceElement>);
 
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Trace, Finalize)]
 pub struct LocError(Box<(Error, StackTrace)>);
 impl LocError {
 	pub fn new(e: Error) -> Self {
modifiedcrates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate.rs
@@ -1,8 +1,9 @@
 use crate::{
-	equals, error::Error::*, lazy_val, push, throw, with_state, ArrValue, Context, ContextCreator,
-	FuncDesc, FuncVal, FutureWrapper, LazyBinding, LazyVal, ObjMember, ObjValue, Result, Val,
+	equals, error::Error::*, push, throw, with_state, ArrValue, Bindable, Context, ContextCreator,
+	FuncDesc, FuncVal, FutureWrapper, LazyBinding, LazyVal, LazyValValue, ObjMember, ObjValue,
+	ObjectAssertion, Result, Val,
 };
-use closure::closure;
+use gc::{custom_trace, Finalize, Gc, Trace};
 use jrsonnet_interner::IStr;
 use jrsonnet_parser::{
 	ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, ExprLocation, FieldMember,
@@ -20,17 +21,62 @@
 	let b = b.clone();
 	if let Some(params) = &b.params {
 		let params = params.clone();
-		LazyVal::new(Box::new(move || {
-			Ok(evaluate_method(
-				context_creator.unwrap(),
-				b.name.clone(),
-				params.clone(),
-				b.value.clone(),
-			))
+
+		struct LazyMethodBinding {
+			context_creator: FutureWrapper<Context>,
+			name: IStr,
+			params: ParamsDesc,
+			value: LocExpr,
+		}
+		impl Finalize for LazyMethodBinding {}
+		unsafe impl Trace for LazyMethodBinding {
+			custom_trace!(this, {
+				mark(&this.context_creator);
+				mark(&this.name);
+				mark(&this.params);
+				mark(&this.value);
+			});
+		}
+		impl LazyValValue for LazyMethodBinding {
+			fn get(self: Box<Self>) -> Result<Val> {
+				Ok(evaluate_method(
+					self.context_creator.unwrap(),
+					self.name,
+					self.params,
+					self.value,
+				))
+			}
+		}
+
+		LazyVal::new(Box::new(LazyMethodBinding {
+			context_creator,
+			name: b.name.clone(),
+			params,
+			value: b.value.clone(),
 		}))
 	} else {
-		LazyVal::new(Box::new(move || {
-			evaluate_named(context_creator.unwrap(), &b.value, b.name.clone())
+		struct LazyNamedBinding {
+			context_creator: FutureWrapper<Context>,
+			name: IStr,
+			value: LocExpr,
+		}
+		impl Finalize for LazyNamedBinding {}
+		unsafe impl Trace for LazyNamedBinding {
+			custom_trace!(this, {
+				mark(&this.context_creator);
+				mark(&this.name);
+				mark(&this.value);
+			});
+		}
+		impl LazyValValue for LazyNamedBinding {
+			fn get(self: Box<Self>) -> Result<Val> {
+				evaluate_named(self.context_creator.unwrap(), &self.value, self.name)
+			}
+		}
+		LazyVal::new(Box::new(LazyNamedBinding {
+			context_creator,
+			name: b.name.clone(),
+			value: b.value,
 		}))
 	}
 }
@@ -39,37 +85,129 @@
 	let b = b.clone();
 	if let Some(params) = &b.params {
 		let params = params.clone();
+
+		struct BindableMethodLazyVal {
+			this: Option<ObjValue>,
+			super_obj: Option<ObjValue>,
+
+			context_creator: ContextCreator,
+			name: IStr,
+			params: ParamsDesc,
+			value: LocExpr,
+		}
+		impl Finalize for BindableMethodLazyVal {}
+		unsafe impl Trace for BindableMethodLazyVal {
+			custom_trace!(this, {
+				mark(&this.this);
+				mark(&this.super_obj);
+				mark(&this.context_creator);
+				mark(&this.name);
+				mark(&this.params);
+				mark(&this.value);
+			});
+		}
+		impl LazyValValue for BindableMethodLazyVal {
+			fn get(self: Box<Self>) -> Result<Val> {
+				Ok(evaluate_method(
+					self.context_creator.create(self.this, self.super_obj)?,
+					self.name,
+					self.params,
+					self.value,
+				))
+			}
+		}
+
+		#[derive(Trace, Finalize)]
+		struct BindableMethod {
+			context_creator: ContextCreator,
+			name: IStr,
+			params: ParamsDesc,
+			value: LocExpr,
+		}
+		impl Bindable for BindableMethod {
+			fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {
+				Ok(LazyVal::new(Box::new(BindableMethodLazyVal {
+					this: this.clone(),
+					super_obj: super_obj.clone(),
+
+					context_creator: self.context_creator.clone(),
+					name: self.name.clone(),
+					params: self.params.clone(),
+					value: self.value.clone(),
+				})))
+			}
+		}
+
 		(
 			b.name.clone(),
-			LazyBinding::Bindable(Rc::new(move |this, super_obj| {
-				Ok(lazy_val!(
-					closure!(clone b, clone params, clone context_creator, || Ok(evaluate_method(
-						context_creator.create(this.clone(), super_obj.clone())?,
-						b.name.clone(),
-						params.clone(),
-						b.value.clone(),
-					)))
-				))
-			})),
+			LazyBinding::Bindable(Gc::new(Box::new(BindableMethod {
+				context_creator,
+				name: b.name.clone(),
+				params,
+				value: b.value.clone(),
+			}))),
 		)
 	} else {
+		struct BindableNamedLazyVal {
+			this: Option<ObjValue>,
+			super_obj: Option<ObjValue>,
+
+			context_creator: ContextCreator,
+			name: IStr,
+			value: LocExpr,
+		}
+		impl Finalize for BindableNamedLazyVal {}
+		unsafe impl Trace for BindableNamedLazyVal {
+			custom_trace!(this, {
+				mark(&this.this);
+				mark(&this.super_obj);
+				mark(&this.context_creator);
+				mark(&this.name);
+				mark(&this.value);
+			});
+		}
+		impl LazyValValue for BindableNamedLazyVal {
+			fn get(self: Box<Self>) -> Result<Val> {
+				evaluate_named(
+					self.context_creator.create(self.this, self.super_obj)?,
+					&self.value,
+					self.name,
+				)
+			}
+		}
+
+		#[derive(Trace, Finalize)]
+		struct BindableNamed {
+			context_creator: ContextCreator,
+			name: IStr,
+			value: LocExpr,
+		}
+		impl Bindable for BindableNamed {
+			fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {
+				Ok(LazyVal::new(Box::new(BindableNamedLazyVal {
+					this,
+					super_obj,
+
+					context_creator: self.context_creator.clone(),
+					name: self.name.clone(),
+					value: self.value.clone(),
+				})))
+			}
+		}
+
 		(
 			b.name.clone(),
-			LazyBinding::Bindable(Rc::new(move |this, super_obj| {
-				Ok(lazy_val!(closure!(clone context_creator, clone b, ||
-					evaluate_named(
-						context_creator.create(this.clone(), super_obj.clone())?,
-						&b.value,
-						b.name.clone()
-					)
-				)))
-			})),
+			LazyBinding::Bindable(Gc::new(Box::new(BindableNamed {
+				context_creator,
+				name: b.name.clone(),
+				value: b.value.clone(),
+			}))),
 		)
 	}
 }
 
 pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {
-	Val::Func(Rc::new(FuncVal::Normal(FuncDesc {
+	Val::Func(Gc::new(FuncVal::Normal(FuncDesc {
 		name,
 		ctx,
 		params,
@@ -105,6 +243,9 @@
 
 pub fn evaluate_add_op(a: &Val, b: &Val) -> Result<Val> {
 	Ok(match (a, b) {
+		(Val::DebugGcTraceValue(v1), Val::DebugGcTraceValue(v2)) => {
+			evaluate_add_op(&v1.value, &v2.value)?
+		}
 		(Val::Str(v1), Val::Str(v2)) => Val::Str(((**v1).to_owned() + v2).into()),
 
 		// Can't use generic json serialization way, because it depends on number to string concatenation (std.jsonnet:890)
@@ -257,7 +398,7 @@
 	}
 
 	let mut new_members = FxHashMap::default();
-	let mut assertions = Vec::new();
+	let mut assertions: Vec<Box<dyn ObjectAssertion>> = Vec::new();
 	for member in members.iter() {
 		match member {
 			Member::Field(FieldMember {
@@ -272,20 +413,36 @@
 					continue;
 				}
 				let name = name.unwrap();
+
+				#[derive(Trace, Finalize)]
+				struct ObjMemberBinding {
+					context_creator: ContextCreator,
+					value: LocExpr,
+					name: IStr,
+				}
+				impl Bindable for ObjMemberBinding {
+					fn bind(
+						&self,
+						this: Option<ObjValue>,
+						super_obj: Option<ObjValue>,
+					) -> Result<LazyVal> {
+						Ok(LazyVal::new_resolved(evaluate_named(
+							self.context_creator.create(this, super_obj)?,
+							&self.value,
+							self.name.clone(),
+						)?))
+					}
+				}
 				new_members.insert(
 					name.clone(),
 					ObjMember {
 						add: *plus,
 						visibility: *visibility,
-						invoke: LazyBinding::Bindable(Rc::new(
-							closure!(clone name, clone value, clone context_creator, |this, super_obj| {
-								Ok(LazyVal::new_resolved(evaluate_named(
-									context_creator.create(this, super_obj)?,
-									&value,
-									name.clone(),
-								)?))
-							}),
-						)),
+						invoke: LazyBinding::Bindable(Gc::new(Box::new(ObjMemberBinding {
+							context_creator: context_creator.clone(),
+							value: value.clone(),
+							name,
+						}))),
 						location: value.1.clone(),
 					},
 				);
@@ -301,33 +458,73 @@
 					continue;
 				}
 				let name = name.unwrap();
+				#[derive(Trace, Finalize)]
+				struct ObjMemberBinding {
+					context_creator: ContextCreator,
+					value: LocExpr,
+					params: ParamsDesc,
+					name: IStr,
+				}
+				impl Bindable for ObjMemberBinding {
+					fn bind(
+						&self,
+						this: Option<ObjValue>,
+						super_obj: Option<ObjValue>,
+					) -> Result<LazyVal> {
+						Ok(LazyVal::new_resolved(evaluate_method(
+							self.context_creator.create(this, super_obj)?,
+							self.name.clone(),
+							self.params.clone(),
+							self.value.clone(),
+						)))
+					}
+				}
 				new_members.insert(
 					name.clone(),
 					ObjMember {
 						add: false,
 						visibility: Visibility::Hidden,
-						invoke: LazyBinding::Bindable(Rc::new(
-							closure!(clone value, clone context_creator, clone params, clone name, |this, super_obj| {
-								// TODO: Assert
-								Ok(LazyVal::new_resolved(evaluate_method(
-									context_creator.create(this, super_obj)?,
-									name.clone(),
-									params.clone(),
-									value.clone(),
-								)))
-							}),
-						)),
+						invoke: LazyBinding::Bindable(Gc::new(Box::new(ObjMemberBinding {
+							context_creator: context_creator.clone(),
+							value: value.clone(),
+							params: params.clone(),
+							name,
+						}))),
 						location: value.1.clone(),
 					},
 				);
 			}
 			Member::BindStmt(_) => {}
 			Member::AssertStmt(stmt) => {
-				assertions.push(stmt.clone());
+				struct ObjectAssert {
+					context_creator: ContextCreator,
+					assert: AssertStmt,
+				}
+				impl Finalize for ObjectAssert {}
+				unsafe impl Trace for ObjectAssert {
+					custom_trace!(this, {
+						mark(&this.context_creator);
+						mark(&this.assert);
+					});
+				}
+				impl ObjectAssertion for ObjectAssert {
+					fn run(
+						&self,
+						this: Option<ObjValue>,
+						super_obj: Option<ObjValue>,
+					) -> Result<()> {
+						let ctx = self.context_creator.create(this, super_obj)?;
+						evaluate_assert(ctx, &self.assert)
+					}
+				}
+				assertions.push(Box::new(ObjectAssert {
+					context_creator: context_creator.clone(),
+					assert: stmt.clone(),
+				}));
 			}
 		}
 	}
-	let this = ObjValue::new(context, None, Rc::new(new_members), Rc::new(assertions));
+	let this = ObjValue::new(None, Gc::new(new_members), Gc::new(assertions));
 	future_this.fill(this.clone());
 	Ok(this)
 }
@@ -361,16 +558,37 @@
 				match key {
 					Val::Null => {}
 					Val::Str(n) => {
+						#[derive(Trace, Finalize)]
+						struct ObjCompBinding {
+							context: Context,
+							value: LocExpr,
+						}
+						impl Bindable for ObjCompBinding {
+							fn bind(
+								&self,
+								this: Option<ObjValue>,
+								_super_obj: Option<ObjValue>,
+							) -> Result<LazyVal> {
+								Ok(LazyVal::new_resolved(evaluate(
+									self.context.clone().extend(
+										FxHashMap::default(),
+										None,
+										this,
+										None,
+									),
+									&self.value,
+								)?))
+							}
+						}
 						new_members.insert(
 							n,
 							ObjMember {
 								add: false,
 								visibility: Visibility::Normal,
-								invoke: LazyBinding::Bindable(Rc::new(
-									closure!(clone ctx, clone obj.value, |this, _super_obj| {
-										Ok(LazyVal::new_resolved(evaluate(ctx.clone().extend(FxHashMap::default(), None, this, None), &value)?))
-									}),
-								)),
+								invoke: LazyBinding::Bindable(Gc::new(Box::new(ObjCompBinding {
+									context: ctx.clone(),
+									value: obj.value.clone(),
+								}))),
 								location: obj.value.1.clone(),
 							},
 						);
@@ -381,7 +599,7 @@
 				Ok(())
 			})?;
 
-			let this = ObjValue::new(context, None, Rc::new(new_members), Rc::new(Vec::new()));
+			let this = ObjValue::new(None, Gc::new(new_members), Gc::new(Vec::new()));
 			future_this.fill(this.clone());
 			this
 		}
@@ -486,7 +704,7 @@
 							if let Some(v) = v.get(s.clone())? {
 								Ok(v)
 							} else if v.get("__intrinsic_namespace__".into())?.is_some() {
-								Ok(Val::Func(Rc::new(FuncVal::Intrinsic(s))))
+								Ok(Val::Func(Gc::new(FuncVal::Intrinsic(s))))
 							} else {
 								throw!(NoSuchField(s))
 							}
@@ -549,11 +767,27 @@
 		Arr(items) => {
 			let mut out = Vec::with_capacity(items.len());
 			for item in items {
-				out.push(LazyVal::new(Box::new(
-					closure!(clone context, clone item, || {
-						evaluate(context.clone(), &item)
-					}),
-				)));
+				// TODO: Implement ArrValue::Lazy with same context for every element?
+				struct ArrayElement {
+					context: Context,
+					item: LocExpr,
+				}
+				impl Finalize for ArrayElement {}
+				unsafe impl Trace for ArrayElement {
+					custom_trace!(this, {
+						mark(&this.context);
+						mark(&this.item);
+					});
+				}
+				impl LazyValValue for ArrayElement {
+					fn get(self: Box<Self>) -> Result<Val> {
+						evaluate(self.context, &self.item)
+					}
+				}
+				out.push(LazyVal::new(Box::new(ArrayElement {
+					context: context.clone(),
+					item: item.clone(),
+				})));
 			}
 			Val::Arr(out.into())
 		}
@@ -563,7 +797,7 @@
 				out.push(evaluate(ctx, expr)?);
 				Ok(())
 			})?;
-			Val::Arr(ArrValue::Eager(Rc::new(out)))
+			Val::Arr(ArrValue::Eager(Gc::new(out)))
 		}
 		Obj(body) => Val::Obj(evaluate_object(context, body)?),
 		ObjExtend(s, t) => evaluate_add_op(
@@ -576,7 +810,7 @@
 		Function(params, body) => {
 			evaluate_method(context, "anonymous".into(), params.clone(), body.clone())
 		}
-		Intrinsic(name) => Val::Func(Rc::new(FuncVal::Intrinsic(name.clone()))),
+		Intrinsic(name) => Val::Func(Gc::new(FuncVal::Intrinsic(name.clone()))),
 		AssertExpr(assert, returned) => {
 			evaluate_assert(context.clone(), assert)?;
 			evaluate(context, returned)?
modifiedcrates/jrsonnet-evaluator/src/function.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function.rs
+++ b/crates/jrsonnet-evaluator/src/function.rs
@@ -1,7 +1,7 @@
-use crate::{error::Error::*, evaluate, lazy_val, resolved_lazy_val, throw, Context, Result, Val};
-use closure::closure;
+use crate::{error::Error::*, evaluate, throw, Context, LazyVal, LazyValValue, Result, Val};
+use gc::{custom_trace, Finalize, Trace};
 use jrsonnet_interner::IStr;
-use jrsonnet_parser::{ArgsDesc, ParamsDesc};
+use jrsonnet_parser::{ArgsDesc, LocExpr, ParamsDesc};
 use rustc_hash::FxHashMap;
 use std::{collections::HashMap, hash::BuildHasherDefault};
 
@@ -53,9 +53,29 @@
 			throw!(FunctionParameterNotBoundInCall(p.0.clone()));
 		};
 		let val = if tailstrict {
-			resolved_lazy_val!(evaluate(ctx, expr)?)
+			LazyVal::new_resolved(evaluate(ctx, expr)?)
 		} else {
-			lazy_val!(closure!(clone ctx, clone expr, ||evaluate(ctx.clone(), &expr)))
+			struct EvaluateLazyVal {
+				context: Context,
+				expr: LocExpr,
+			}
+			impl Finalize for EvaluateLazyVal {}
+			unsafe impl Trace for EvaluateLazyVal {
+				custom_trace!(this, {
+					mark(&this.context);
+					mark(&this.expr);
+				});
+			}
+			impl LazyValValue for EvaluateLazyVal {
+				fn get(self: Box<Self>) -> Result<Val> {
+					evaluate(self.context, &self.expr)
+				}
+			}
+
+			LazyVal::new(Box::new(EvaluateLazyVal {
+				context: ctx.clone(),
+				expr: expr.clone(),
+			}))
 		};
 		out.insert(p.0.clone(), val);
 	}
@@ -89,19 +109,30 @@
 	// Fill defaults
 	for (id, p) in params.iter().enumerate() {
 		let val = if let Some(arg) = positioned_args[id].take() {
-			resolved_lazy_val!(arg)
+			LazyVal::new_resolved(arg)
 		} else if let Some(default) = &p.1 {
 			if tailstrict {
-				resolved_lazy_val!(evaluate(
+				LazyVal::new_resolved(evaluate(
 					body_ctx.clone().expect(NO_DEFAULT_CONTEXT),
-					default
+					default,
 				)?)
 			} else {
 				let body_ctx = body_ctx.clone();
 				let default = default.clone();
-				lazy_val!(move || {
-					evaluate(body_ctx.clone().expect(NO_DEFAULT_CONTEXT), &default)
-				})
+				#[derive(Trace, Finalize)]
+				struct EvaluateLazyVal {
+					body_ctx: Option<Context>,
+					default: LocExpr,
+				}
+				impl LazyValValue for EvaluateLazyVal {
+					fn get(self: Box<Self>) -> Result<Val> {
+						evaluate(
+							self.body_ctx.clone().expect(NO_DEFAULT_CONTEXT),
+							&self.default,
+						)
+					}
+				}
+				LazyVal::new(Box::new(EvaluateLazyVal { body_ctx, default }))
 			}
 		} else {
 			throw!(FunctionParameterNotBoundInCall(p.0.clone()));
@@ -135,7 +166,7 @@
 		} else {
 			throw!(FunctionParameterNotBoundInCall(p.0.clone()));
 		};
-		out.insert(p.0.clone(), resolved_lazy_val!(val));
+		out.insert(p.0.clone(), LazyVal::new_resolved(val));
 	}
 
 	Ok(body_ctx.unwrap_or(ctx).extend(out, None, None, None))
modifiedcrates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -2,6 +2,7 @@
 	error::{Error::*, LocError, Result},
 	throw, Context, LazyBinding, LazyVal, ObjMember, ObjValue, Val,
 };
+use gc::Gc;
 use jrsonnet_parser::Visibility;
 use rustc_hash::FxHasher;
 use serde_json::{Map, Number, Value};
@@ -9,7 +10,6 @@
 	collections::HashMap,
 	convert::{TryFrom, TryInto},
 	hash::BuildHasherDefault,
-	rc::Rc,
 };
 
 impl TryFrom<&Val> for Value {
@@ -42,6 +42,7 @@
 				Self::Object(out)
 			}
 			Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),
+			Val::DebugGcTraceValue(v) => Value::try_from(&*v.value as &Val)?,
 		})
 	}
 }
@@ -76,12 +77,7 @@
 						},
 					);
 				}
-				Self::Obj(ObjValue::new(
-					Context::new(),
-					None,
-					Rc::new(entries),
-					Rc::new(Vec::new()),
-				))
+				Self::Obj(ObjValue::new(None, Gc::new(entries), Gc::new(Vec::new())))
 			}
 		}
 	}
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/lib.rs
1#![cfg_attr(feature = "unstable", feature(stmt_expr_attributes))]2#![warn(clippy::all, clippy::nursery)]3#![allow(4	macro_expanded_macro_exports_accessed_by_absolute_paths,5	clippy::ptr_arg6)]78mod builtin;9mod ctx;10mod dynamic;11pub mod error;12mod evaluate;13mod function;14mod import;15mod integrations;16mod map;17pub mod native;18mod obj;19pub mod trace;20pub mod typed;21mod val;2223pub use ctx::*;24pub use dynamic::*;25use error::{Error::*, LocError, Result, StackTraceElement};26pub use evaluate::*;27pub use function::parse_function_call;28pub use import::*;29use jrsonnet_interner::IStr;30use jrsonnet_parser::*;31use native::NativeCallback;32pub use obj::*;33use rustc_hash::FxHashMap;34use std::{35	cell::{Ref, RefCell, RefMut},36	collections::HashMap,37	fmt::Debug,38	hash::BuildHasherDefault,39	path::PathBuf,40	rc::Rc,41};42use trace::{offset_to_location, CodeLocation, CompactFormat, TraceFormat};43pub use val::*;4445type BindableFn = dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<LazyVal>;46#[derive(Clone)]47pub enum LazyBinding {48	Bindable(Rc<BindableFn>),49	Bound(LazyVal),50}5152impl Debug for LazyBinding {53	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {54		write!(f, "LazyBinding")55	}56}57impl LazyBinding {58	pub fn evaluate(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {59		match self {60			Self::Bindable(v) => v(this, super_obj),61			Self::Bound(v) => Ok(v.clone()),62		}63	}64}6566pub struct EvaluationSettings {67	/// Limits recursion by limiting the number of stack frames68	pub max_stack: usize,69	/// Limits amount of stack trace items preserved70	pub max_trace: usize,71	/// Used for s`td.extVar`72	pub ext_vars: HashMap<IStr, Val>,73	/// Used for ext.native74	pub ext_natives: HashMap<IStr, Rc<NativeCallback>>,75	/// TLA vars76	pub tla_vars: HashMap<IStr, Val>,77	/// Global variables are inserted in default context78	pub globals: HashMap<IStr, Val>,79	/// Used to resolve file locations/contents80	pub import_resolver: Box<dyn ImportResolver>,81	/// Used in manifestification functions82	pub manifest_format: ManifestFormat,83	/// Used for bindings84	pub trace_format: Box<dyn TraceFormat>,85}86impl Default for EvaluationSettings {87	fn default() -> Self {88		Self {89			max_stack: 200,90			max_trace: 20,91			globals: Default::default(),92			ext_vars: Default::default(),93			ext_natives: Default::default(),94			tla_vars: Default::default(),95			import_resolver: Box::new(DummyImportResolver),96			manifest_format: ManifestFormat::Json(4),97			trace_format: Box::new(CompactFormat {98				padding: 4,99				resolver: trace::PathResolver::Absolute,100			}),101		}102	}103}104105#[derive(Default)]106struct EvaluationData {107	/// Used for stack overflow detection, stacktrace is populated on unwind108	stack_depth: usize,109	/// Contains file source codes and evaluation results for imports and pretty-printed stacktraces110	files: HashMap<Rc<PathBuf>, FileData>,111	str_files: HashMap<Rc<PathBuf>, IStr>,112}113114pub struct FileData {115	source_code: IStr,116	parsed: LocExpr,117	evaluated: Option<Val>,118}119#[derive(Default)]120pub struct EvaluationStateInternals {121	/// Internal state122	data: RefCell<EvaluationData>,123	/// Settings, safe to change at runtime124	settings: RefCell<EvaluationSettings>,125}126127thread_local! {128	/// Contains the state for a currently executed file.129	/// Global state is fine here.130	pub(crate) static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)131}132pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {133	EVAL_STATE.with(|s| f(s.borrow().as_ref().unwrap()))134}135pub(crate) fn push<T>(136	e: Option<&ExprLocation>,137	frame_desc: impl FnOnce() -> String,138	f: impl FnOnce() -> Result<T>,139) -> Result<T> {140	with_state(|s| s.push(e, frame_desc, f))141}142143pub fn push_stack_frame<T>(144	e: Option<&ExprLocation>,145	frame_desc: impl FnOnce() -> String,146	f: impl FnOnce() -> Result<T>,147) -> Result<T> {148	push(e, frame_desc, f)149}150151/// Maintains stack trace and import resolution152#[derive(Default, Clone)]153pub struct EvaluationState(Rc<EvaluationStateInternals>);154155impl EvaluationState {156	/// Parses and adds file as loaded157	pub fn add_file(&self, path: Rc<PathBuf>, source_code: IStr) -> Result<()> {158		self.add_parsed_file(159			path.clone(),160			source_code.clone(),161			parse(162				&source_code,163				&ParserSettings {164					file_name: path.clone(),165					loc_data: true,166				},167			)168			.map_err(|error| ImportSyntaxError {169				error: Box::new(error),170				path,171				source_code,172			})?,173		)?;174175		Ok(())176	}177178	/// Adds file by source code and parsed expr179	pub fn add_parsed_file(180		&self,181		name: Rc<PathBuf>,182		source_code: IStr,183		parsed: LocExpr,184	) -> Result<()> {185		self.data_mut().files.insert(186			name,187			FileData {188				source_code,189				parsed,190				evaluated: None,191			},192		);193194		Ok(())195	}196	pub fn get_source(&self, name: &PathBuf) -> Option<IStr> {197		let ro_map = &self.data().files;198		ro_map.get(name).map(|value| value.source_code.clone())199	}200	pub fn map_source_locations(&self, file: &PathBuf, locs: &[usize]) -> Vec<CodeLocation> {201		offset_to_location(&self.get_source(file).unwrap(), locs)202	}203204	pub(crate) fn import_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Val> {205		let file_path = self.resolve_file(from, path)?;206		{207			let data = self.data();208			let files = &data.files;209			if files.contains_key(&file_path) {210				drop(data);211				return self.evaluate_loaded_file_raw(&file_path);212			}213		}214		let contents = self.load_file_contents(&file_path)?;215		self.add_file(file_path.clone(), contents)?;216		self.evaluate_loaded_file_raw(&file_path)217	}218	pub(crate) fn import_file_str(&self, from: &PathBuf, path: &PathBuf) -> Result<IStr> {219		let path = self.resolve_file(from, path)?;220		if !self.data().str_files.contains_key(&path) {221			let file_str = self.load_file_contents(&path)?;222			self.data_mut().str_files.insert(path.clone(), file_str);223		}224		Ok(self.data().str_files.get(&path).cloned().unwrap())225	}226227	fn evaluate_loaded_file_raw(&self, name: &PathBuf) -> Result<Val> {228		let expr: LocExpr = {229			let ro_map = &self.data().files;230			let value = ro_map231				.get(name)232				.unwrap_or_else(|| panic!("file not added: {:?}", name));233			if let Some(ref evaluated) = value.evaluated {234				return Ok(evaluated.clone());235			}236			value.parsed.clone()237		};238		let value = evaluate(self.create_default_context(), &expr)?;239		{240			self.data_mut()241				.files242				.get_mut(name)243				.unwrap()244				.evaluated245				.replace(value.clone());246		}247		Ok(value)248	}249250	/// Adds standard library global variable (std) to this evaluator251	pub fn with_stdlib(&self) -> &Self {252		use jrsonnet_stdlib::STDLIB_STR;253		let std_path = Rc::new(PathBuf::from("std.jsonnet"));254		self.run_in_state(|| {255			self.add_parsed_file(256				std_path.clone(),257				STDLIB_STR.to_owned().into(),258				builtin::get_parsed_stdlib(),259			)260			.unwrap();261			let val = self.evaluate_loaded_file_raw(&std_path).unwrap();262			self.settings_mut().globals.insert("std".into(), val);263		});264		self265	}266267	/// Creates context with all passed global variables268	pub fn create_default_context(&self) -> Context {269		let globals = &self.settings().globals;270		let mut new_bindings: FxHashMap<IStr, LazyVal> =271			FxHashMap::with_capacity_and_hasher(globals.len(), BuildHasherDefault::default());272		for (name, value) in globals.iter() {273			new_bindings.insert(name.clone(), resolved_lazy_val!(value.clone()));274		}275		Context::new().extend_bound(new_bindings)276	}277278	/// Executes code creating a new stack frame279	pub fn push<T>(280		&self,281		e: Option<&ExprLocation>,282		frame_desc: impl FnOnce() -> String,283		f: impl FnOnce() -> Result<T>,284	) -> Result<T> {285		{286			let mut data = self.data_mut();287			let stack_depth = &mut data.stack_depth;288			if *stack_depth > self.max_stack() {289				// Error creation uses data, so i drop guard here290				drop(data);291				throw!(StackOverflow);292			} else {293				*stack_depth += 1;294			}295		}296		let result = f();297		self.data_mut().stack_depth -= 1;298		if let Err(mut err) = result {299			err.trace_mut().0.push(StackTraceElement {300				location: e.cloned(),301				desc: frame_desc(),302			});303			return Err(err);304		}305		result306	}307308	/// Runs passed function in state (required if function needs to modify stack trace)309	pub fn run_in_state<T>(&self, f: impl FnOnce() -> T) -> T {310		EVAL_STATE.with(|v| {311			let has_state = v.borrow().is_some();312			if !has_state {313				v.borrow_mut().replace(self.clone());314			}315			let result = f();316			if !has_state {317				v.borrow_mut().take();318			}319			result320		})321	}322323	pub fn stringify_err(&self, e: &LocError) -> String {324		let mut out = String::new();325		self.settings()326			.trace_format327			.write_trace(&mut out, self, e)328			.unwrap();329		out330	}331332	pub fn manifest(&self, val: Val) -> Result<IStr> {333		self.run_in_state(|| val.manifest(&self.manifest_format()))334	}335	pub fn manifest_multi(&self, val: Val) -> Result<Vec<(IStr, IStr)>> {336		self.run_in_state(|| val.manifest_multi(&self.manifest_format()))337	}338	pub fn manifest_stream(&self, val: Val) -> Result<Vec<IStr>> {339		self.run_in_state(|| val.manifest_stream(&self.manifest_format()))340	}341342	/// If passed value is function then call with set TLA343	pub fn with_tla(&self, val: Val) -> Result<Val> {344		self.run_in_state(|| {345			Ok(match val {346				Val::Func(func) => push(347					None,348					|| "during TLA call".to_owned(),349					|| {350						func.evaluate_map(351							self.create_default_context(),352							&self.settings().tla_vars,353							true,354						)355					},356				)?,357				v => v,358			})359		})360	}361}362363/// Internals364impl EvaluationState {365	fn data(&self) -> Ref<EvaluationData> {366		self.0.data.borrow()367	}368	fn data_mut(&self) -> RefMut<EvaluationData> {369		self.0.data.borrow_mut()370	}371	pub fn settings(&self) -> Ref<EvaluationSettings> {372		self.0.settings.borrow()373	}374	pub fn settings_mut(&self) -> RefMut<EvaluationSettings> {375		self.0.settings.borrow_mut()376	}377}378379/// Raw methods evaluate passed values but don't perform TLA execution380impl EvaluationState {381	pub fn evaluate_file_raw(&self, name: &PathBuf) -> Result<Val> {382		self.run_in_state(|| self.import_file(&std::env::current_dir().expect("cwd"), name))383	}384	pub fn evaluate_file_raw_nocwd(&self, name: &PathBuf) -> Result<Val> {385		self.run_in_state(|| self.import_file(&PathBuf::from("."), name))386	}387	/// Parses and evaluates the given snippet388	pub fn evaluate_snippet_raw(&self, source: Rc<PathBuf>, code: IStr) -> Result<Val> {389		let parsed = parse(390			&code,391			&ParserSettings {392				file_name: source.clone(),393				loc_data: true,394			},395		)396		.map_err(|e| ImportSyntaxError {397			path: source.clone(),398			source_code: code.clone(),399			error: Box::new(e),400		})?;401		self.add_parsed_file(source, code, parsed.clone())?;402		self.evaluate_expr_raw(parsed)403	}404	/// Evaluates the parsed expression405	pub fn evaluate_expr_raw(&self, code: LocExpr) -> Result<Val> {406		self.run_in_state(|| evaluate(self.create_default_context(), &code))407	}408}409410/// Settings utilities411impl EvaluationState {412	pub fn add_ext_var(&self, name: IStr, value: Val) {413		self.settings_mut().ext_vars.insert(name, value);414	}415	pub fn add_ext_str(&self, name: IStr, value: IStr) {416		self.add_ext_var(name, Val::Str(value));417	}418	pub fn add_ext_code(&self, name: IStr, code: IStr) -> Result<()> {419		let value =420			self.evaluate_snippet_raw(Rc::new(PathBuf::from(format!("ext_code {}", name))), code)?;421		self.add_ext_var(name, value);422		Ok(())423	}424425	pub fn add_tla(&self, name: IStr, value: Val) {426		self.settings_mut().tla_vars.insert(name, value);427	}428	pub fn add_tla_str(&self, name: IStr, value: IStr) {429		self.add_tla(name, Val::Str(value));430	}431	pub fn add_tla_code(&self, name: IStr, code: IStr) -> Result<()> {432		let value =433			self.evaluate_snippet_raw(Rc::new(PathBuf::from(format!("tla_code {}", name))), code)?;434		self.add_tla(name, value);435		Ok(())436	}437438	pub fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {439		self.settings().import_resolver.resolve_file(from, path)440	}441	pub fn load_file_contents(&self, path: &PathBuf) -> Result<IStr> {442		self.settings().import_resolver.load_file_contents(path)443	}444445	pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {446		Ref::map(self.settings(), |s| &*s.import_resolver)447	}448	pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {449		self.settings_mut().import_resolver = resolver;450	}451452	pub fn add_native(&self, name: IStr, cb: Rc<NativeCallback>) {453		self.settings_mut().ext_natives.insert(name, cb);454	}455456	pub fn manifest_format(&self) -> ManifestFormat {457		self.settings().manifest_format.clone()458	}459	pub fn set_manifest_format(&self, format: ManifestFormat) {460		self.settings_mut().manifest_format = format;461	}462463	pub fn trace_format(&self) -> Ref<dyn TraceFormat> {464		Ref::map(self.settings(), |s| &*s.trace_format)465	}466	pub fn set_trace_format(&self, format: Box<dyn TraceFormat>) {467		self.settings_mut().trace_format = format;468	}469470	pub fn max_trace(&self) -> usize {471		self.settings().max_trace472	}473	pub fn set_max_trace(&self, trace: usize) {474		self.settings_mut().max_trace = trace;475	}476477	pub fn max_stack(&self) -> usize {478		self.settings().max_stack479	}480	pub fn set_max_stack(&self, trace: usize) {481		self.settings_mut().max_stack = trace;482	}483}484485#[cfg(test)]486pub mod tests {487	use super::Val;488	use crate::{error::Error::*, primitive_equals, EvaluationState};489	use jrsonnet_interner::IStr;490	use jrsonnet_parser::*;491	use std::{path::PathBuf, rc::Rc};492493	#[test]494	#[should_panic]495	fn eval_state_stacktrace() {496		let state = EvaluationState::default();497		state.run_in_state(|| {498			state499				.push(500					Some(&ExprLocation(501						Rc::new(PathBuf::from("test1.jsonnet")),502						10,503						20,504					)),505					|| "outer".to_owned(),506					|| {507						state.push(508							Some(&ExprLocation(509								Rc::new(PathBuf::from("test2.jsonnet")),510								30,511								40,512							)),513							|| "inner".to_owned(),514							|| Err(RuntimeError("".into()).into()),515						)?;516						Ok(())517					},518				)519				.unwrap();520		});521	}522523	#[test]524	fn eval_state_standard() {525		let state = EvaluationState::default();526		state.with_stdlib();527		assert!(primitive_equals(528			&state529				.evaluate_snippet_raw(530					Rc::new(PathBuf::from("raw.jsonnet")),531					r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#.into()532				)533				.unwrap(),534			&Val::Bool(true),535		)536		.unwrap());537	}538539	macro_rules! eval {540		($str: expr) => {541			EvaluationState::default()542				.with_stdlib()543				.evaluate_snippet_raw(Rc::new(PathBuf::from("raw.jsonnet")), $str.into())544				.unwrap()545		};546	}547	macro_rules! eval_json {548		($str: expr) => {{549			let evaluator = EvaluationState::default();550			evaluator.with_stdlib();551			evaluator.run_in_state(|| {552				evaluator553					.evaluate_snippet_raw(Rc::new(PathBuf::from("raw.jsonnet")), $str.into())554					.unwrap()555					.to_json(0)556					.unwrap()557					.replace("\n", "")558			})559		}};560	}561562	/// Asserts given code returns `true`563	macro_rules! assert_eval {564		($str: expr) => {565			assert!(primitive_equals(&eval!($str), &Val::Bool(true)).unwrap())566		};567	}568569	/// Asserts given code returns `false`570	macro_rules! assert_eval_neg {571		($str: expr) => {572			assert!(primitive_equals(&eval!($str), &Val::Bool(false)).unwrap())573		};574	}575	macro_rules! assert_json {576		($str: expr, $out: expr) => {577			assert_eq!(eval_json!($str), $out.replace("\t", ""))578		};579	}580581	/// Sanity checking, before trusting to another tests582	#[test]583	fn equality_operator() {584		assert_eval!("2 == 2");585		assert_eval_neg!("2 != 2");586		assert_eval!("2 != 3");587		assert_eval_neg!("2 == 3");588		assert_eval!("'Hello' == 'Hello'");589		assert_eval_neg!("'Hello' != 'Hello'");590		assert_eval!("'Hello' != 'World'");591		assert_eval_neg!("'Hello' == 'World'");592	}593594	#[test]595	fn math_evaluation() {596		assert_eval!("2 + 2 * 2 == 6");597		assert_eval!("3 + (2 + 2 * 2) == 9");598	}599600	#[test]601	fn string_concat() {602		assert_eval!("'Hello' + 'World' == 'HelloWorld'");603		assert_eval!("'Hello' * 3 == 'HelloHelloHello'");604		assert_eval!("'Hello' + 'World' * 3 == 'HelloWorldWorldWorld'");605	}606607	#[test]608	fn faster_join() {609		assert_eval!("std.join([0,0], [[1,2],[3,4],[5,6]]) == [1,2,0,0,3,4,0,0,5,6]");610		assert_eval!("std.join(',', ['1','2','3','4']) == '1,2,3,4'");611	}612613	#[test]614	fn function_contexts() {615		assert_eval!(616			r#"617				local k = {618					t(name = self.h): [self.h, name],619					h: 3,620				};621				local f = {622					t: k.t(),623					h: 4,624				};625				f.t[0] == f.t[1]626			"#627		);628	}629630	#[test]631	fn local() {632		assert_eval!("local a = 2; local b = 3; a + b == 5");633		assert_eval!("local a = 1, b = a + 1; a + b == 3");634		assert_eval!("local a = 1; local a = 2; a == 2");635	}636637	#[test]638	fn object_lazyness() {639		assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);640	}641642	#[test]643	fn object_inheritance() {644		assert_json!("{a: self.b} + {b:3}", r#"{"a": 3,"b": 3}"#);645	}646647	#[test]648	fn object_assertion_success() {649		eval!("{assert \"a\" in self} + {a:2}");650	}651652	#[test]653	fn object_assertion_error() {654		eval!("{assert \"a\" in self}");655	}656657	#[test]658	fn lazy_args() {659		eval!("local test(a) = 2; test(error '3')");660	}661662	#[test]663	#[should_panic]664	fn tailstrict_args() {665		eval!("local test(a) = 2; test(error '3') tailstrict");666	}667668	#[test]669	#[should_panic]670	fn no_binding_error() {671		eval!("a");672	}673674	#[test]675	fn test_object() {676		assert_json!("{a:2}", r#"{"a": 2}"#);677		assert_json!("{a:2+2}", r#"{"a": 4}"#);678		assert_json!("{a:2}+{b:2}", r#"{"a": 2,"b": 2}"#);679		assert_json!("{b:3}+{b:2}", r#"{"b": 2}"#);680		assert_json!("{b:3}+{b+:2}", r#"{"b": 5}"#);681		assert_json!("local test='a'; {[test]:2}", r#"{"a": 2}"#);682		assert_json!(683			r#"684				{685					name: "Alice",686					welcome: "Hello " + self.name + "!",687				}688			"#,689			r#"{"name": "Alice","welcome": "Hello Alice!"}"#690		);691		assert_json!(692			r#"693				{694					name: "Alice",695					welcome: "Hello " + self.name + "!",696				} + {697					name: "Bob"698				}699			"#,700			r#"{"name": "Bob","welcome": "Hello Bob!"}"#701		);702	}703704	#[test]705	fn functions() {706		assert_json!(r#"local a = function(b, c = 2) b + c; a(2)"#, "4");707		assert_json!(708			r#"local a = function(b, c = "Dear") b + c + d, d = "World"; a("Hello")"#,709			r#""HelloDearWorld""#710		);711	}712713	#[test]714	fn local_methods() {715		assert_json!(r#"local a(b, c = 2) = b + c; a(2)"#, "4");716		assert_json!(717			r#"local a(b, c = "Dear") = b + c + d, d = "World"; a("Hello")"#,718			r#""HelloDearWorld""#719		);720	}721722	#[test]723	fn object_locals() {724		assert_json!(r#"{local a = 3, b: a}"#, r#"{"b": 3}"#);725		assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b": 3}"#);726		assert_json!(727			r#"{local a = function (b) {[b]:4}, test: a("test")}"#,728			r#"{"test": {"test": 4}}"#729		);730	}731732	#[test]733	fn object_comp() {734		assert_json!(735			r#"{local t = "a", ["h"+i+"_"+z]: if "h"+(i-1)+"_"+z in self then t+1 else 0+t for i in [1,2,3] for z in [2,3,4] if z != i}"#,736			"{\"h1_2\": \"0a\",\"h1_3\": \"0a\",\"h1_4\": \"0a\",\"h2_3\": \"a1\",\"h2_4\": \"a1\",\"h3_2\": \"0a\",\"h3_4\": \"a1\"}"737		)738	}739740	#[test]741	fn direct_self() {742		println!(743			"{:#?}",744			eval!(745				r#"746					{747						local me = self,748						a: 3,749						b(): me.a,750					}751				"#752			)753		);754	}755756	#[test]757	fn indirect_self() {758		// `self` assigned to `me` was lost when being759		// referenced from field760		eval!(761			r#"{762				local me = self,763				a: 3,764				b: me.a,765			}.b"#766		);767	}768769	// We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly770	#[test]771	fn std_assert_ok() {772		eval!("std.assertEqual(4.5 << 2, 16)");773	}774775	#[test]776	#[should_panic]777	fn std_assert_failure() {778		eval!("std.assertEqual(4.5 << 2, 15)");779	}780781	#[test]782	fn string_is_string() {783		assert!(primitive_equals(784			&eval!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),785			&Val::Bool(false),786		)787		.unwrap());788	}789790	#[test]791	fn base64_works() {792		assert_json!(r#"std.base64("test")"#, r#""dGVzdA==""#);793	}794795	#[test]796	fn utf8_chars() {797		assert_json!(798			r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,799			r#"{"c": 128526,"l": 1}"#800		)801	}802803	#[test]804	fn json() {805		assert_json!(806			r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,807			r#""{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}""#808		);809	}810811	#[test]812	fn parse_json() {813		assert_json!(814			r#"std.parseJson('{"a": -1,"b": 1,"c": 3.141,"d": []}')"#,815			r#"{"a": -1,"b": 1,"c": 3.141,"d": []}"#816		);817		// TODO: this should in fact fail as is no proper JSON syntax818		assert_json!(819			r#"std.parseJson("{a:-1, b:1, c:3.141, d:[]}")"#,820			r#"{"a": -1,"b": 1,"c": 3.141,"d": []}"#821		);822		// TODO: this is also no valid JSON823		assert_json!(r#"std.parseJson('local x = 2; x * x')"#, r#"4"#);824	}825826	#[test]827	fn test() {828		assert_json!(829			r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,830			"[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"831		);832	}833834	#[test]835	fn sjsonnet() {836		eval!(837			r#"838			local x0 = {k: 1};839			local x1 = {k: x0.k + x0.k};840			local x2 = {k: x1.k + x1.k};841			local x3 = {k: x2.k + x2.k};842			local x4 = {k: x3.k + x3.k};843			local x5 = {k: x4.k + x4.k};844			local x6 = {k: x5.k + x5.k};845			local x7 = {k: x6.k + x6.k};846			local x8 = {k: x7.k + x7.k};847			local x9 = {k: x8.k + x8.k};848			local x10 = {k: x9.k + x9.k};849			local x11 = {k: x10.k + x10.k};850			local x12 = {k: x11.k + x11.k};851			local x13 = {k: x12.k + x12.k};852			local x14 = {k: x13.k + x13.k};853			local x15 = {k: x14.k + x14.k};854			local x16 = {k: x15.k + x15.k};855			local x17 = {k: x16.k + x16.k};856			local x18 = {k: x17.k + x17.k};857			local x19 = {k: x18.k + x18.k};858			local x20 = {k: x19.k + x19.k};859			local x21 = {k: x20.k + x20.k};860			x21.k861		"#862		);863	}864865	// This test is commented out by default, because of huge compilation slowdown866	// #[bench]867	// fn bench_codegen(b: &mut Bencher) {868	// 	b.iter(|| {869	// 		#[allow(clippy::all)]870	// 		let stdlib = {871	// 			use jrsonnet_parser::*;872	// 			include!(concat!(env!("OUT_DIR"), "/stdlib.rs"))873	// 		};874	// 		stdlib875	// 	})876	// }877878	/*879	#[bench]880	fn bench_serialize(b: &mut Bencher) {881		b.iter(|| {882			bincode::deserialize::<jrsonnet_parser::LocExpr>(include_bytes!(concat!(883				env!("OUT_DIR"),884				"/stdlib.bincode"885			)))886			.expect("deserialize stdlib")887		})888	}889890	#[bench]891	fn bench_parse(b: &mut Bencher) {892		b.iter(|| {893			jrsonnet_parser::parse(894				jrsonnet_stdlib::STDLIB_STR,895				&jrsonnet_parser::ParserSettings {896					loc_data: true,897					file_name: Rc::new(PathBuf::from("std.jsonnet")),898				},899			)900		})901	}902	*/903904	#[test]905	fn equality() {906		println!(907			"{:?}",908			jrsonnet_parser::parse(909				"{ x: 1, y: 2 } == { x: 1, y: 2 }",910				&ParserSettings::default()911			)912		);913		assert_eval!("{ x: 1, y: 2 } == { x: 1, y: 2 }")914	}915916	#[test]917	fn native_ext() -> crate::error::Result<()> {918		use super::native::NativeCallback;919		let evaluator = EvaluationState::default();920921		evaluator.with_stdlib();922		evaluator.settings_mut().ext_natives.insert(923			"native_add".into(),924			Rc::new(NativeCallback::new(925				ParamsDesc(Rc::new(vec![926					Param("a".into(), None),927					Param("b".into(), None),928				])),929				|caller, args| {930					assert_eq!(931						caller.unwrap(),932						Rc::new(PathBuf::from("native_caller.jsonnet"))933					);934					match (&args[0], &args[1]) {935						(Val::Num(a), Val::Num(b)) => Ok(Val::Num(a + b)),936						(_, _) => unreachable!(),937					}938				},939			)),940		);941		evaluator.evaluate_snippet_raw(942			Rc::new(PathBuf::from("native_caller.jsonnet")),943			"std.assertEqual(std.native(\"native_add\")(1, 2), 3)".into(),944		)?;945		Ok(())946	}947948	#[test]949	fn constant_intrinsic() -> crate::error::Result<()> {950		assert_eval!(951			"local std2 = std; local std = std2 { primitiveEquals(a, b):: false }; 1 == 1"952		);953		Ok(())954	}955956	#[test]957	fn standalone_super() -> crate::error::Result<()> {958		assert_eval!(959			r#"960			local obj = {961				a: 1,962				b: 2,963				c: 3,964			};965			local test = obj + {966				fields: std.objectFields(super),967				d: 5,968			};969			test.fields == ['a', 'b', 'c']970		"#971		);972		Ok(())973	}974975	#[test]976	fn comp_self() -> crate::error::Result<()> {977		assert_eval!(978			r#"979			std.objectFields({980				a:{981					[name]: name for name in std.objectFields(self)982				},983				b: 2,984				c: 3,985			}.a) == ['a', 'b', 'c']986			"#987		);988989		Ok(())990	}991992	struct TestImportResolver(IStr);993	impl crate::import::ImportResolver for TestImportResolver {994		fn resolve_file(&self, _: &PathBuf, _: &PathBuf) -> crate::error::Result<Rc<PathBuf>> {995			Ok(Rc::new(PathBuf::from("/test")))996		}997998		fn load_file_contents(&self, _: &PathBuf) -> crate::error::Result<IStr> {999			Ok(self.0.clone())1000		}10011002		unsafe fn as_any(&self) -> &dyn std::any::Any {1003			panic!()1004		}1005	}10061007	#[test]1008	fn issue_23() {1009		let state = EvaluationState::default();1010		state.set_import_resolver(Box::new(TestImportResolver(r#"import "/test""#.into())));1011		let _ = state.evaluate_file_raw(&PathBuf::from("/test"));1012	}10131014	#[test]1015	fn issue_40() {1016		let state = EvaluationState::default();1017		state.with_stdlib();10181019		let error = state1020			.evaluate_snippet_raw(1021				Rc::new(PathBuf::from("issue40.jsonnet")),1022				r#"1023				local conf = {1024					n: ""1025				};1026				1027				local result = conf + {1028					assert std.isNumber(self.n): "is number"1029				};10301031				std.manifestJsonEx(result, "")1032			"#1033				.into(),1034			)1035			.unwrap_err();1036		assert_eq!(error.error().to_string(), "assert failed: is number");1037	}1038}
modifiedcrates/jrsonnet-evaluator/src/map.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/map.rs
+++ b/crates/jrsonnet-evaluator/src/map.rs
@@ -1,28 +1,29 @@
+use gc::{Finalize, Gc, Trace};
 use jrsonnet_interner::IStr;
 use rustc_hash::FxHashMap;
-use std::rc::Rc;
 
-#[derive(Default, Debug)]
-struct LayeredHashMapInternals<V> {
+pub struct LayeredHashMapInternals<V: Trace + Finalize + 'static> {
 	parent: Option<LayeredHashMap<V>>,
 	current: FxHashMap<IStr, V>,
 }
 
-#[derive(Debug)]
-pub struct LayeredHashMap<V>(Rc<LayeredHashMapInternals<V>>);
+unsafe impl<V: Trace + Finalize + 'static> Trace for LayeredHashMapInternals<V> {
+	gc::custom_trace!(this, {
+		mark(&this.parent);
+		mark(&this.current);
+	});
+}
+impl<V: Trace + Finalize + 'static> Finalize for LayeredHashMapInternals<V> {}
+
+#[derive(Trace, Finalize)]
+pub struct LayeredHashMap<V: Trace + Finalize + 'static>(Gc<LayeredHashMapInternals<V>>);
 
-impl<V> LayeredHashMap<V> {
+impl<V: Trace + 'static> LayeredHashMap<V> {
 	pub fn extend(self, new_layer: FxHashMap<IStr, V>) -> Self {
-		match Rc::try_unwrap(self.0) {
-			Ok(mut map) => {
-				map.current.extend(new_layer);
-				Self(Rc::new(map))
-			}
-			Err(this) => Self(Rc::new(LayeredHashMapInternals {
-				parent: Some(Self(this)),
-				current: new_layer,
-			})),
-		}
+		Self(Gc::new(LayeredHashMapInternals {
+			parent: Some(self),
+			current: new_layer,
+		}))
 	}
 
 	pub fn get(&self, key: &IStr) -> Option<&V> {
@@ -33,15 +34,15 @@
 	}
 }
 
-impl<V> Clone for LayeredHashMap<V> {
+impl<V: Trace + 'static> Clone for LayeredHashMap<V> {
 	fn clone(&self) -> Self {
 		Self(self.0.clone())
 	}
 }
 
-impl<V> Default for LayeredHashMap<V> {
+impl<V: Trace + 'static> Default for LayeredHashMap<V> {
 	fn default() -> Self {
-		Self(Rc::new(LayeredHashMapInternals {
+		Self(Gc::new(LayeredHashMapInternals {
 			parent: None,
 			current: FxHashMap::default(),
 		}))
modifiedcrates/jrsonnet-evaluator/src/native.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/native.rs
+++ b/crates/jrsonnet-evaluator/src/native.rs
@@ -1,27 +1,27 @@
 #![allow(clippy::type_complexity)]
 
 use crate::{error::Result, Val};
+use gc::{Finalize, Trace};
 use jrsonnet_parser::ParamsDesc;
 use std::fmt::Debug;
 use std::path::PathBuf;
 use std::rc::Rc;
 
+pub trait NativeCallbackHandler: Trace {
+	fn call(&self, from: Option<Rc<PathBuf>>, args: &[Val]) -> Result<Val>;
+}
+
+#[derive(Trace, Finalize)]
 pub struct NativeCallback {
 	pub params: ParamsDesc,
-	handler: Box<dyn Fn(Option<Rc<PathBuf>>, &[Val]) -> Result<Val>>,
+	handler: Box<dyn NativeCallbackHandler>,
 }
 impl NativeCallback {
-	pub fn new(
-		params: ParamsDesc,
-		handler: impl Fn(Option<Rc<PathBuf>>, &[Val]) -> Result<Val> + 'static,
-	) -> Self {
-		Self {
-			params,
-			handler: Box::new(handler),
-		}
+	pub fn new(params: ParamsDesc, handler: Box<dyn NativeCallbackHandler>) -> Self {
+		Self { params, handler }
 	}
 	pub fn call(&self, caller: Option<Rc<PathBuf>>, args: &[Val]) -> Result<Val> {
-		(self.handler)(caller, args)
+		self.handler.call(caller, args)
 	}
 }
 impl Debug for NativeCallback {
modifiedcrates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -1,11 +1,12 @@
 use crate::{evaluate_add_op, evaluate_assert, Context, LazyBinding, Result, Val};
+use gc::{Finalize, Gc, GcCell, Trace};
 use jrsonnet_interner::IStr;
 use jrsonnet_parser::{AssertStmt, ExprLocation, Visibility};
 use rustc_hash::{FxHashMap, FxHashSet};
 use std::hash::{Hash, Hasher};
-use std::{cell::RefCell, fmt::Debug, hash::BuildHasherDefault, rc::Rc};
+use std::{fmt::Debug, hash::BuildHasherDefault};
 
-#[derive(Debug)]
+#[derive(Debug, Trace, Finalize)]
 pub struct ObjMember {
 	pub add: bool,
 	pub visibility: Visibility,
@@ -13,21 +14,24 @@
 	pub location: Option<ExprLocation>,
 }
 
+pub trait ObjectAssertion: Trace {
+	fn run(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<()>;
+}
+
 // Field => This
 type CacheKey = (IStr, ObjValue);
-#[derive(Debug)]
+#[derive(Trace, Finalize)]
 pub struct ObjValueInternals {
-	context: Context,
 	super_obj: Option<ObjValue>,
-	assertions: Rc<Vec<AssertStmt>>,
-	assertions_ran: RefCell<FxHashSet<ObjValue>>,
+	assertions: Gc<Vec<Box<dyn ObjectAssertion>>>,
+	assertions_ran: GcCell<FxHashSet<ObjValue>>,
 	this_obj: Option<ObjValue>,
-	this_entries: Rc<FxHashMap<IStr, ObjMember>>,
-	value_cache: RefCell<FxHashMap<CacheKey, Option<Val>>>,
+	this_entries: Gc<FxHashMap<IStr, ObjMember>>,
+	value_cache: GcCell<FxHashMap<CacheKey, Option<Val>>>,
 }
 
-#[derive(Clone)]
-pub struct ObjValue(pub(crate) Rc<ObjValueInternals>);
+#[derive(Clone, Trace, Finalize)]
+pub struct ObjValue(pub(crate) Gc<ObjValueInternals>);
 impl Debug for ObjValue {
 	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 		if let Some(super_obj) = self.0.super_obj.as_ref() {
@@ -55,39 +59,30 @@
 
 impl ObjValue {
 	pub fn new(
-		context: Context,
 		super_obj: Option<Self>,
-		this_entries: Rc<FxHashMap<IStr, ObjMember>>,
-		assertions: Rc<Vec<AssertStmt>>,
+		this_entries: Gc<FxHashMap<IStr, ObjMember>>,
+		assertions: Gc<Vec<Box<dyn ObjectAssertion>>>,
 	) -> Self {
-		Self(Rc::new(ObjValueInternals {
-			context,
+		Self(Gc::new(ObjValueInternals {
 			super_obj,
 			assertions,
-			assertions_ran: RefCell::new(FxHashSet::default()),
+			assertions_ran: GcCell::new(FxHashSet::default()),
 			this_obj: None,
 			this_entries,
-			value_cache: RefCell::new(FxHashMap::default()),
+			value_cache: GcCell::new(FxHashMap::default()),
 		}))
 	}
 	pub fn new_empty() -> Self {
-		Self::new(
-			Context::new(),
-			None,
-			Rc::new(FxHashMap::default()),
-			Rc::new(Vec::new()),
-		)
+		Self::new(None, Gc::new(FxHashMap::default()), Gc::new(Vec::new()))
 	}
 	pub fn extend_from(&self, super_obj: Self) -> Self {
 		match &self.0.super_obj {
 			None => Self::new(
-				self.0.context.clone(),
 				Some(super_obj),
 				self.0.this_entries.clone(),
 				self.0.assertions.clone(),
 			),
 			Some(v) => Self::new(
-				self.0.context.clone(),
 				Some(v.extend_from(super_obj)),
 				self.0.this_entries.clone(),
 				self.0.assertions.clone(),
@@ -95,14 +90,13 @@
 		}
 	}
 	pub fn with_this(&self, this_obj: Self) -> Self {
-		Self(Rc::new(ObjValueInternals {
-			context: self.0.context.clone(),
+		Self(Gc::new(ObjValueInternals {
 			super_obj: self.0.super_obj.clone(),
 			assertions: self.0.assertions.clone(),
-			assertions_ran: RefCell::new(FxHashSet::default()),
+			assertions_ran: GcCell::new(FxHashSet::default()),
 			this_obj: Some(this_obj),
 			this_entries: self.0.this_entries.clone(),
-			value_cache: RefCell::new(FxHashMap::default()),
+			value_cache: GcCell::new(FxHashMap::default()),
 		}))
 	}
 
@@ -203,12 +197,7 @@
 	pub fn extend_with_field(self, key: IStr, value: ObjMember) -> Self {
 		let mut new = FxHashMap::with_capacity_and_hasher(1, BuildHasherDefault::default());
 		new.insert(key, value);
-		Self::new(
-			Context::new(),
-			Some(self),
-			Rc::new(new),
-			Rc::new(Vec::new()),
-		)
+		Self::new(Some(self), Gc::new(new), Gc::new(Vec::new()))
 	}
 
 	fn get_raw(&self, key: IStr, real_this: Option<&Self>) -> Result<Option<Val>> {
@@ -249,13 +238,7 @@
 	fn run_assertions_raw(&self, real_this: &Self) -> Result<()> {
 		if self.0.assertions_ran.borrow_mut().insert(real_this.clone()) {
 			for assertion in self.0.assertions.iter() {
-				if let Err(e) = evaluate_assert(
-					self.0
-						.context
-						.clone()
-						.with_this_super(real_this.clone(), self.0.super_obj.clone()),
-					assertion,
-				) {
+				if let Err(e) = assertion.run(Some(real_this.clone()), self.0.super_obj.clone()) {
 					self.0.assertions_ran.borrow_mut().remove(real_this);
 					return Err(e);
 				}
@@ -271,19 +254,19 @@
 	}
 
 	pub fn ptr_eq(a: &Self, b: &Self) -> bool {
-		Rc::ptr_eq(&a.0, &b.0)
+		Gc::ptr_eq(&a.0, &b.0)
 	}
 }
 
 impl PartialEq for ObjValue {
 	fn eq(&self, other: &Self) -> bool {
-		Rc::ptr_eq(&self.0, &other.0)
+		Gc::ptr_eq(&self.0, &other.0)
 	}
 }
 
 impl Eq for ObjValue {}
 impl Hash for ObjValue {
-	fn hash<H: Hasher>(&self, state: &mut H) {
-		state.write_usize(Rc::as_ptr(&self.0) as usize)
+	fn hash<H: Hasher>(&self, hasher: &mut H) {
+		hasher.write_usize(&*self.0 as *const _ as usize)
 	}
 }
modifiedcrates/jrsonnet-evaluator/src/typed.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/typed.rs
+++ b/crates/jrsonnet-evaluator/src/typed.rs
@@ -4,6 +4,7 @@
 	error::{Error, LocError, Result},
 	push, Val,
 };
+use gc::{Finalize, Trace};
 use jrsonnet_parser::ExprLocation;
 use jrsonnet_types::{ComplexValType, ValType};
 use thiserror::Error;
@@ -20,7 +21,7 @@
 	}};
 }
 
-#[derive(Debug, Error, Clone)]
+#[derive(Debug, Error, Clone, Trace, Finalize)]
 pub enum TypeError {
 	#[error("expected {0}, got {1}")]
 	ExpectedGot(ComplexValType, ValType),
@@ -37,7 +38,7 @@
 	}
 }
 
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Trace, Finalize)]
 pub struct TypeLocError(Box<TypeError>, ValuePathStack);
 impl From<TypeError> for TypeLocError {
 	fn from(e: TypeError) -> Self {
@@ -59,7 +60,7 @@
 	}
 }
 
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Trace, Finalize)]
 pub struct TypeLocErrorList(Vec<TypeLocError>);
 impl Display for TypeLocErrorList {
 	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -122,7 +123,7 @@
 	}
 }
 
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, Trace, Finalize)]
 enum ValuePathItem {
 	Field(Rc<str>),
 	Index(u64),
@@ -137,7 +138,7 @@
 	}
 }
 
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, Trace, Finalize)]
 struct ValuePathStack(Vec<ValuePathItem>);
 impl Display for ValuePathStack {
 	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -3,52 +3,75 @@
 		call_builtin,
 		manifest::{manifest_json_ex, ManifestJsonOptions, ManifestType},
 	},
-	error::Error::*,
+	error::{Error::*, LocError},
 	evaluate,
 	function::{parse_function_call, parse_function_call_map, place_args},
 	native::NativeCallback,
 	throw, with_state, Context, ObjValue, Result,
 };
+use gc::{custom_trace, Finalize, Gc, GcCell, Trace};
 use jrsonnet_interner::IStr;
 use jrsonnet_parser::{el, Arg, ArgsDesc, Expr, ExprLocation, LiteralType, LocExpr, ParamsDesc};
 use jrsonnet_types::ValType;
-use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};
+use std::{collections::HashMap, fmt::Debug, rc::Rc};
 
+pub trait LazyValValue: Trace {
+	fn get(self: Box<Self>) -> Result<Val>;
+}
+
 enum LazyValInternals {
 	Computed(Val),
-	Waiting(Box<dyn Fn() -> Result<Val>>),
+	Errored(LocError),
+	Waiting(Box<dyn LazyValValue>),
+	Pending,
 }
-#[derive(Clone)]
-pub struct LazyVal(Rc<RefCell<LazyValInternals>>);
+impl Finalize for LazyValInternals {}
+unsafe impl Trace for LazyValInternals {
+	custom_trace!(this, {
+		match &this {
+			LazyValInternals::Computed(v) => mark(v),
+			LazyValInternals::Errored(e) => mark(e),
+			LazyValInternals::Waiting(w) => mark(w),
+			LazyValInternals::Pending => {}
+		}
+	});
+}
+
+#[derive(Clone, Trace, Finalize)]
+pub struct LazyVal(Gc<GcCell<LazyValInternals>>);
 impl LazyVal {
-	pub fn new(f: Box<dyn Fn() -> Result<Val>>) -> Self {
-		Self(Rc::new(RefCell::new(LazyValInternals::Waiting(f))))
+	pub fn new(f: Box<dyn LazyValValue>) -> Self {
+		Self(Gc::new(GcCell::new(LazyValInternals::Waiting(f))))
 	}
 	pub fn new_resolved(val: Val) -> Self {
-		Self(Rc::new(RefCell::new(LazyValInternals::Computed(val))))
+		Self(Gc::new(GcCell::new(LazyValInternals::Computed(val))))
 	}
 	pub fn evaluate(&self) -> Result<Val> {
-		let new_value = match &*self.0.borrow() {
+		match &*self.0.borrow() {
 			LazyValInternals::Computed(v) => return Ok(v.clone()),
-			LazyValInternals::Waiting(f) => f()?,
+			LazyValInternals::Errored(e) => return Err(e.clone().into()),
+			LazyValInternals::Pending => return Err(RecursiveLazyValueEvaluation.into()),
+			_ => (),
+		};
+		let value = if let LazyValInternals::Waiting(value) =
+			std::mem::replace(&mut *self.0.borrow_mut(), LazyValInternals::Pending)
+		{
+			value
+		} else {
+			unreachable!()
 		};
+		let new_value = match value.get() {
+			Ok(v) => v,
+			Err(e) => {
+				*self.0.borrow_mut() = LazyValInternals::Errored(e.clone());
+				return Err(e);
+			}
+		};
 		*self.0.borrow_mut() = LazyValInternals::Computed(new_value.clone());
 		Ok(new_value)
 	}
 }
 
-#[macro_export]
-macro_rules! lazy_val {
-	($f: expr) => {
-		$crate::LazyVal::new(Box::new($f))
-	};
-}
-#[macro_export]
-macro_rules! resolved_lazy_val {
-	($f: expr) => {
-		$crate::LazyVal::new_resolved($f)
-	};
-}
 impl Debug for LazyVal {
 	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 		write!(f, "Lazy")
@@ -56,11 +79,11 @@
 }
 impl PartialEq for LazyVal {
 	fn eq(&self, other: &Self) -> bool {
-		Rc::ptr_eq(&self.0, &other.0)
+		Gc::ptr_eq(&self.0, &other.0)
 	}
 }
 
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Trace, Finalize)]
 pub struct FuncDesc {
 	pub name: IStr,
 	pub ctx: Context,
@@ -68,14 +91,14 @@
 	pub body: LocExpr,
 }
 
-#[derive(Debug)]
+#[derive(Debug, Trace, Finalize)]
 pub enum FuncVal {
 	/// Plain function implemented in jsonnet
 	Normal(FuncDesc),
 	/// Standard library function
 	Intrinsic(IStr),
 	/// Library functions implemented in native
-	NativeExt(IStr, Rc<NativeCallback>),
+	NativeExt(IStr, Gc<NativeCallback>),
 }
 
 impl PartialEq for FuncVal {
@@ -174,13 +197,23 @@
 
 #[derive(Debug, Clone)]
 pub enum ArrValue {
-	Lazy(Rc<Vec<LazyVal>>),
-	Eager(Rc<Vec<Val>>),
+	Lazy(Gc<Vec<LazyVal>>),
+	Eager(Gc<Vec<Val>>),
 	Extended(Box<(Self, Self)>),
 }
+impl Finalize for ArrValue {}
+unsafe impl Trace for ArrValue {
+	custom_trace!(this, {
+		match &this {
+			ArrValue::Lazy(l) => mark(l),
+			ArrValue::Eager(e) => mark(e),
+			ArrValue::Extended(x) => mark(x),
+		}
+	});
+}
 impl ArrValue {
 	pub fn new_eager() -> Self {
-		Self::Eager(Rc::new(Vec::new()))
+		Self::Eager(Gc::new(Vec::new()))
 	}
 
 	pub fn len(&self) -> usize {
@@ -231,14 +264,14 @@
 		}
 	}
 
-	pub fn evaluated(&self) -> Result<Rc<Vec<Val>>> {
+	pub fn evaluated(&self) -> Result<Gc<Vec<Val>>> {
 		Ok(match self {
 			Self::Lazy(vec) => {
 				let mut out = Vec::with_capacity(vec.len());
 				for item in vec.iter() {
 					out.push(item.evaluate()?);
 				}
-				Rc::new(out)
+				Gc::new(out)
 			}
 			Self::Eager(vec) => vec.clone(),
 			Self::Extended(_v) => {
@@ -246,7 +279,7 @@
 				for item in self.iter() {
 					out.push(item?);
 				}
-				Rc::new(out)
+				Gc::new(out)
 			}
 		})
 	}
@@ -272,12 +305,12 @@
 			Self::Lazy(vec) => {
 				let mut out = (&vec as &Vec<_>).clone();
 				out.reverse();
-				Self::Lazy(Rc::new(out))
+				Self::Lazy(Gc::new(out))
 			}
 			Self::Eager(vec) => {
 				let mut out = (&vec as &Vec<_>).clone();
 				out.reverse();
-				Self::Eager(Rc::new(out))
+				Self::Eager(Gc::new(out))
 			}
 			Self::Extended(b) => Self::Extended(Box::new((b.1.reversed(), b.0.reversed()))),
 		}
@@ -290,7 +323,7 @@
 			out.push(mapper(value?)?);
 		}
 
-		Ok(Self::Eager(Rc::new(out)))
+		Ok(Self::Eager(Gc::new(out)))
 	}
 
 	pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {
@@ -303,13 +336,13 @@
 			}
 		}
 
-		Ok(Self::Eager(Rc::new(out)))
+		Ok(Self::Eager(Gc::new(out)))
 	}
 
 	pub fn ptr_eq(a: &Self, b: &Self) -> bool {
 		match (a, b) {
-			(Self::Lazy(a), Self::Lazy(b)) => Rc::ptr_eq(a, b),
-			(Self::Eager(a), Self::Eager(b)) => Rc::ptr_eq(a, b),
+			(Self::Lazy(a), Self::Lazy(b)) => Gc::ptr_eq(a, b),
+			(Self::Eager(a), Self::Eager(b)) => Gc::ptr_eq(a, b),
 			_ => false,
 		}
 	}
@@ -317,13 +350,72 @@
 
 impl From<Vec<LazyVal>> for ArrValue {
 	fn from(v: Vec<LazyVal>) -> Self {
-		Self::Lazy(Rc::new(v))
+		Self::Lazy(Gc::new(v))
 	}
 }
 
 impl From<Vec<Val>> for ArrValue {
 	fn from(v: Vec<Val>) -> Self {
-		Self::Eager(Rc::new(v))
+		Self::Eager(Gc::new(v))
+	}
+}
+
+#[derive(Debug)]
+pub struct DebugGcTraceValue {
+	name: IStr,
+	pub value: Box<Val>,
+}
+impl DebugGcTraceValue {
+	fn print(&self, action: &str) {
+		println!("{} {}#{:?}", action, self.name, &*self.value as *const _)
+	}
+}
+impl Finalize for DebugGcTraceValue {
+	fn finalize(&self) {
+		self.print("Garbage-collecting")
+	}
+}
+impl Drop for DebugGcTraceValue {
+	fn drop(&mut self) {
+		self.print("Garbage-collected")
+	}
+}
+unsafe impl Trace for DebugGcTraceValue {
+	unsafe fn trace(&self) {
+		self.print("Traced");
+		self.value.trace()
+	}
+	unsafe fn root(&self) {
+		self.print("Rooted");
+		self.value.root()
+	}
+	unsafe fn unroot(&self) {
+		self.print("Unrooted");
+		self.value.unroot()
+	}
+	fn finalize_glue(&self) {
+		Finalize::finalize(self)
+	}
+}
+impl Clone for DebugGcTraceValue {
+	fn clone(&self) -> Self {
+		self.print("Cloned");
+		let value = DebugGcTraceValue {
+			name: self.name.clone(),
+			value: self.value.clone(),
+		};
+		value.print("I'm clone");
+		value
+	}
+}
+impl DebugGcTraceValue {
+	pub fn new(name: IStr, value: Val) -> Val {
+		let value = Self {
+			name,
+			value: Box::new(value),
+		};
+		value.print("Constructed");
+		Val::DebugGcTraceValue(value)
 	}
 }
 
@@ -335,7 +427,23 @@
 	Num(f64),
 	Arr(ArrValue),
 	Obj(ObjValue),
-	Func(Rc<FuncVal>),
+	Func(Gc<FuncVal>),
+	DebugGcTraceValue(DebugGcTraceValue),
+}
+impl Finalize for Val {}
+unsafe impl Trace for Val {
+	custom_trace!(this, {
+		match &this {
+			Val::Bool(_) => {}
+			Val::Null => {}
+			Val::Str(_) => {}
+			Val::Num(_) => {}
+			Val::Arr(a) => mark(a),
+			Val::Obj(o) => mark(o),
+			Val::Func(f) => mark(f),
+			Val::DebugGcTraceValue(v) => mark(v),
+		}
+	});
 }
 
 macro_rules! matches_unwrap {
@@ -368,7 +476,7 @@
 	pub fn unwrap_num(self) -> Result<f64> {
 		Ok(matches_unwrap!(self, Self::Num(v), v))
 	}
-	pub fn unwrap_func(self) -> Result<Rc<FuncVal>> {
+	pub fn unwrap_func(self) -> Result<Gc<FuncVal>> {
 		Ok(matches_unwrap!(self, Self::Func(v), v))
 	}
 	pub fn try_cast_bool(self, context: &'static str) -> Result<bool> {
@@ -392,6 +500,7 @@
 			Self::Bool(_) => ValType::Bool,
 			Self::Null => ValType::Null,
 			Self::Func(..) => ValType::Func,
+			Self::DebugGcTraceValue(v) => v.value.value_type(),
 		}
 	}