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

difftreelog

refactor better rebinding handling

Yaroslav Bolyukin2022-04-24parent: #88a0ba1.patch.diff
in: master

15 files changed

modifiedcrates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -4,34 +4,15 @@
 use jrsonnet_interner::IStr;
 
 use crate::{
-	cc_ptr_eq, error::Error::*, gc::GcHashMap, map::LayeredHashMap, LazyBinding, ObjValue, Pending,
-	Result, State, Thunk, Val,
+	cc_ptr_eq, error::Error::*, gc::GcHashMap, map::LayeredHashMap, ObjValue, Pending, Result,
+	Thunk, Val,
 };
 
-#[derive(Clone, Trace)]
-pub struct ContextCreator(pub Context, pub Pending<GcHashMap<IStr, LazyBinding>>);
-impl ContextCreator {
-	pub fn create(
-		&self,
-		s: State,
-		this: Option<ObjValue>,
-		super_obj: Option<ObjValue>,
-	) -> Result<Context> {
-		self.0.clone().extend_unbound(
-			s,
-			self.1.clone().unwrap(),
-			self.0.dollar().clone().or_else(|| this.clone()),
-			this,
-			super_obj,
-		)
-	}
-}
-
 #[derive(Trace)]
 struct ContextInternals {
 	dollar: Option<ObjValue>,
+	sup: Option<ObjValue>,
 	this: Option<ObjValue>,
-	super_obj: Option<ObjValue>,
 	bindings: LayeredHashMap,
 }
 impl Debug for ContextInternals {
@@ -56,14 +37,14 @@
 	}
 
 	pub fn super_obj(&self) -> &Option<ObjValue> {
-		&self.0.super_obj
+		&self.0.sup
 	}
 
 	pub fn new() -> Self {
 		Self(Cc::new(ContextInternals {
 			dollar: None,
 			this: None,
-			super_obj: None,
+			sup: None,
 			bindings: LayeredHashMap::default(),
 		}))
 	}
@@ -92,11 +73,6 @@
 		let mut new_bindings = GcHashMap::with_capacity(1);
 		new_bindings.insert(name, Thunk::evaluated(value));
 		self.extend(new_bindings, None, None, None)
-	}
-
-	#[must_use]
-	pub fn with_this_super(self, new_this: ObjValue, new_super_obj: Option<ObjValue>) -> Self {
-		self.extend(GcHashMap::new(), None, Some(new_this), new_super_obj)
 	}
 
 	#[must_use]
@@ -104,13 +80,13 @@
 		self,
 		new_bindings: GcHashMap<IStr, Thunk<Val>>,
 		new_dollar: Option<ObjValue>,
+		new_sup: Option<ObjValue>,
 		new_this: Option<ObjValue>,
-		new_super_obj: Option<ObjValue>,
 	) -> Self {
 		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 sup = new_sup.or_else(|| ctx.sup.clone());
 		let bindings = if new_bindings.is_empty() {
 			ctx.bindings.clone()
 		} else {
@@ -118,32 +94,10 @@
 		};
 		Self(Cc::new(ContextInternals {
 			dollar,
+			sup,
 			this,
-			super_obj,
 			bindings,
 		}))
-	}
-	#[must_use]
-	pub fn extend_bound(self, new_bindings: GcHashMap<IStr, Thunk<Val>>) -> Self {
-		let new_this = self.0.this.clone();
-		let new_super_obj = self.0.super_obj.clone();
-		self.extend(new_bindings, None, new_this, new_super_obj)
-	}
-	pub fn extend_unbound(
-		self,
-		s: State,
-		new_bindings: GcHashMap<IStr, LazyBinding>,
-		new_dollar: Option<ObjValue>,
-		new_this: Option<ObjValue>,
-		new_super_obj: Option<ObjValue>,
-	) -> Result<Self> {
-		let this = new_this.or_else(|| self.0.this.clone());
-		let super_obj = new_super_obj.or_else(|| self.0.super_obj.clone());
-		let mut new = GcHashMap::with_capacity(new_bindings.len());
-		for (k, v) in new_bindings.0 {
-			new.insert(k, v.evaluate(s.clone(), this.clone(), super_obj.clone())?);
-		}
-		Ok(self.extend(new, new_dollar, this, super_obj))
 	}
 }
 
modifiedcrates/jrsonnet-evaluator/src/evaluate/destructure.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/destructure.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/destructure.rs
@@ -11,12 +11,13 @@
 	Context, Pending, State, Thunk, Val,
 };
 
+#[allow(clippy::too_many_lines)]
 fn destruct(
 	d: &Destruct,
 	parent: Thunk<Val>,
 	new_bindings: &mut GcHashMap<IStr, Thunk<Val>>,
 ) -> Result<()> {
-	Ok(match d {
+	match d {
 		Destruct::Full(v) => {
 			let old = new_bindings.insert(v.clone(), parent);
 			if old.is_some() {
@@ -157,8 +158,6 @@
 		}
 		#[cfg(feature = "exp-destruct")]
 		Destruct::Object { fields, rest } => {
-			use jrsonnet_parser::DestructRest;
-
 			use crate::{obj::ObjValue, throw_runtime};
 
 			#[derive(Trace)]
@@ -223,7 +222,8 @@
 				}
 			}
 		}
-	})
+	}
+	Ok(())
 }
 
 pub fn evaluate_dest(
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -1,7 +1,9 @@
+use std::rc::Rc;
+
 use gcmodule::{Cc, Trace};
 use jrsonnet_interner::IStr;
 use jrsonnet_parser::{
-	ArgsDesc, AssertStmt, BindSpec, CompSpec, Destruct, Expr, FieldMember, ForSpecData, IfSpecData,
+	ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, FieldMember, ForSpecData, IfSpecData,
 	LiteralType, LocExpr, Member, ObjBody, ParamsDesc,
 };
 use jrsonnet_types::ValType;
@@ -14,147 +16,13 @@
 	stdlib::{std_slice, BUILTINS},
 	tb, throw,
 	typed::Typed,
-	val::{ArrValue, Thunk, ThunkValue},
-	Bindable, Context, ContextCreator, GcHashMap, LazyBinding, ObjValue, ObjValueBuilder,
-	ObjectAssertion, Pending, Result, State, Val,
+	val::{ArrValue, CachedBindable, Thunk, ThunkValue},
+	Bindable, Context, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result,
+	State, Val,
 };
 pub mod destructure;
 pub mod operator;
-
-#[allow(clippy::too_many_lines)]
-pub fn evaluate_binding(b: BindSpec, cctx: ContextCreator) -> Result<(IStr, LazyBinding)> {
-	match b {
-		BindSpec::Field {
-			into: Destruct::Full(name),
-			value,
-		} => {
-			#[derive(Trace)]
-			struct BindableNamedThunk {
-				this: Option<ObjValue>,
-				super_obj: Option<ObjValue>,
-
-				cctx: ContextCreator,
-				name: IStr,
-				value: LocExpr,
-			}
-			impl ThunkValue for BindableNamedThunk {
-				type Output = Val;
-				fn get(self: Box<Self>, s: State) -> Result<Val> {
-					evaluate_named(
-						s.clone(),
-						self.cctx.create(s, self.this, self.super_obj)?,
-						&self.value,
-						self.name,
-					)
-				}
-			}
-
-			#[derive(Trace)]
-			struct BindableNamed {
-				cctx: ContextCreator,
-				name: IStr,
-				value: LocExpr,
-			}
-			impl Bindable for BindableNamed {
-				fn bind(
-					&self,
-					_: State,
-					this: Option<ObjValue>,
-					super_obj: Option<ObjValue>,
-				) -> Result<Thunk<Val>> {
-					Ok(Thunk::new(tb!(BindableNamedThunk {
-						this,
-						super_obj,
 
-						cctx: self.cctx.clone(),
-						name: self.name.clone(),
-						value: self.value.clone(),
-					})))
-				}
-			}
-
-			Ok((
-				name.clone(),
-				LazyBinding::Bindable(Cc::new(tb!(BindableNamed {
-					cctx,
-					name: name.clone(),
-					value: value.clone(),
-				}))),
-			))
-		}
-		#[cfg(feature = "exp-destruct")]
-		BindSpec::Field { into: _, .. } => {
-			use crate::throw_runtime;
-			throw_runtime!("destructuring is not yet supported here")
-		}
-		BindSpec::Function {
-			name,
-			params,
-			value,
-		} => {
-			#[derive(Trace)]
-			struct BindableMethodThunk {
-				this: Option<ObjValue>,
-				super_obj: Option<ObjValue>,
-
-				cctx: ContextCreator,
-				name: IStr,
-				params: ParamsDesc,
-				value: LocExpr,
-			}
-			impl ThunkValue for BindableMethodThunk {
-				type Output = Val;
-				fn get(self: Box<Self>, s: State) -> Result<Val> {
-					Ok(evaluate_method(
-						self.cctx.create(s, self.this, self.super_obj)?,
-						self.name,
-						self.params,
-						self.value,
-					))
-				}
-			}
-
-			#[derive(Trace)]
-			struct BindableMethod {
-				cctx: ContextCreator,
-				name: IStr,
-				params: ParamsDesc,
-				value: LocExpr,
-			}
-			impl Bindable for BindableMethod {
-				fn bind(
-					&self,
-					_: State,
-					this: Option<ObjValue>,
-					super_obj: Option<ObjValue>,
-				) -> Result<Thunk<Val>> {
-					Ok(Thunk::<Val>::new(tb!(BindableMethodThunk {
-						this,
-						super_obj,
-
-						cctx: self.cctx.clone(),
-						name: self.name.clone(),
-						params: self.params.clone(),
-						value: self.value.clone(),
-					})))
-				}
-			}
-
-			let params = params.clone();
-
-			Ok((
-				name.clone(),
-				LazyBinding::Bindable(Cc::new(tb!(BindableMethod {
-					cctx,
-					name: name.clone(),
-					params,
-					value,
-				}))),
-			))
-		}
-	}
-}
-
 pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {
 	Val::Func(FuncVal::Normal(Cc::new(FuncDesc {
 		name,
@@ -218,28 +86,65 @@
 	Ok(())
 }
 
+trait CloneableBindable<T>: Bindable<Bound = T> + Clone {}
+
+fn evaluate_object_locals(
+	fctx: Pending<Context>,
+	locals: Rc<Vec<BindSpec>>,
+) -> impl CloneableBindable<Context> {
+	#[derive(Trace, Clone)]
+	struct WithObjectLocals {
+		fctx: Pending<Context>,
+		locals: Rc<Vec<BindSpec>>,
+	}
+	impl CloneableBindable<Context> for WithObjectLocals {}
+	impl Bindable for WithObjectLocals {
+		type Bound = Context;
+
+		fn bind(
+			&self,
+			_s: State,
+			sup: Option<ObjValue>,
+			this: Option<ObjValue>,
+		) -> Result<Context> {
+			let fctx = Context::new_future();
+			let mut new_bindings = GcHashMap::new();
+			for b in self.locals.iter() {
+				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;
+			}
+
+			let ctx = self.fctx.unwrap();
+			let new_dollar = ctx.dollar().clone().or_else(|| this.clone());
+
+			let ctx = ctx
+				.extend(new_bindings, new_dollar, sup, this)
+				.into_future(fctx);
+
+			Ok(ctx)
+		}
+	}
+
+	WithObjectLocals { fctx, locals }
+}
+
 #[allow(clippy::too_many_lines)]
 pub fn evaluate_member_list_object(s: State, ctx: Context, members: &[Member]) -> Result<ObjValue> {
-	let new_bindings = Pending::new();
-	let future_this = Pending::new();
-	let cctx = ContextCreator(ctx.clone(), new_bindings.clone());
-	{
-		let mut bindings: GcHashMap<IStr, LazyBinding> = GcHashMap::with_capacity(members.len());
-		for r in members
+	let mut builder = ObjValueBuilder::new();
+	let locals = Rc::new(
+		members
 			.iter()
 			.filter_map(|m| match m {
-				Member::BindStmt(b) => Some(b.clone()),
+				Member::BindStmt(bind) => Some(bind.clone()),
 				_ => None,
 			})
-			.map(|b| evaluate_binding(b.clone(), cctx.clone()))
-		{
-			let (n, b) = r?;
-			bindings.insert(n, b);
-		}
-		new_bindings.fill(bindings);
-	}
+			.collect::<Vec<_>>(),
+	);
+
+	let fctx = Context::new_future();
+
+	// We have single context for all fields, so we can cache binds
+	let uctx = CachedBindable::new(evaluate_object_locals(fctx.clone(), locals));
 
-	let mut builder = ObjValueBuilder::new();
 	for member in members.iter() {
 		match member {
 			Member::Field(FieldMember {
@@ -250,21 +155,22 @@
 				value,
 			}) => {
 				#[derive(Trace)]
-				struct ObjMemberBinding {
-					cctx: ContextCreator,
+				struct ObjMemberBinding<B> {
+					uctx: B,
 					value: LocExpr,
 					name: IStr,
 				}
-				impl Bindable for ObjMemberBinding {
+				impl<B: Bindable<Bound = Context>> Bindable for ObjMemberBinding<B> {
+					type Bound = Thunk<Val>;
 					fn bind(
 						&self,
 						s: State,
+						sup: Option<ObjValue>,
 						this: Option<ObjValue>,
-						super_obj: Option<ObjValue>,
 					) -> Result<Thunk<Val>> {
 						Ok(Thunk::evaluated(evaluate_named(
 							s.clone(),
-							self.cctx.create(s, this, super_obj)?,
+							self.uctx.bind(s, sup, this)?,
 							&self.value,
 							self.name.clone(),
 						)?))
@@ -286,9 +192,9 @@
 					.bindable(
 						s.clone(),
 						tb!(ObjMemberBinding {
-							cctx: cctx.clone(),
+							uctx: uctx.clone(),
 							value: value.clone(),
-							name,
+							name: name.clone()
 						}),
 					)?;
 			}
@@ -299,21 +205,22 @@
 				..
 			}) => {
 				#[derive(Trace)]
-				struct ObjMemberBinding {
-					cctx: ContextCreator,
+				struct ObjMemberBinding<B> {
+					uctx: B,
 					value: LocExpr,
 					params: ParamsDesc,
 					name: IStr,
 				}
-				impl Bindable for ObjMemberBinding {
+				impl<B: Bindable<Bound = Context>> Bindable for ObjMemberBinding<B> {
+					type Bound = Thunk<Val>;
 					fn bind(
 						&self,
 						s: State,
+						sup: Option<ObjValue>,
 						this: Option<ObjValue>,
-						super_obj: Option<ObjValue>,
 					) -> Result<Thunk<Val>> {
 						Ok(Thunk::evaluated(evaluate_method(
-							self.cctx.create(s, this, super_obj)?,
+							self.uctx.bind(s, sup, this)?,
 							self.name.clone(),
 							self.params.clone(),
 							self.value.clone(),
@@ -334,40 +241,42 @@
 					.bindable(
 						s.clone(),
 						tb!(ObjMemberBinding {
-							cctx: cctx.clone(),
+							uctx: uctx.clone(),
 							value: value.clone(),
 							params: params.clone(),
-							name,
+							name: name.clone()
 						}),
 					)?;
 			}
 			Member::BindStmt(_) => {}
 			Member::AssertStmt(stmt) => {
 				#[derive(Trace)]
-				struct ObjectAssert {
-					cctx: ContextCreator,
+				struct ObjectAssert<B> {
+					uctx: B,
 					assert: AssertStmt,
 				}
-				impl ObjectAssertion for ObjectAssert {
+				impl<B: Bindable<Bound = Context>> ObjectAssertion for ObjectAssert<B> {
 					fn run(
 						&self,
 						s: State,
+						sup: Option<ObjValue>,
 						this: Option<ObjValue>,
-						super_obj: Option<ObjValue>,
 					) -> Result<()> {
-						let ctx = self.cctx.create(s.clone(), this, super_obj)?;
+						let ctx = self.uctx.bind(s.clone(), sup, this)?;
 						evaluate_assert(s, ctx, &self.assert)
 					}
 				}
 				builder.assert(tb!(ObjectAssert {
-					cctx: cctx.clone(),
+					uctx: uctx.clone(),
 					assert: stmt.clone(),
 				}));
 			}
 		}
 	}
 	let this = builder.build();
-	future_this.fill(this.clone());
+	let _ctx = ctx
+		.extend(GcHashMap::new(), None, None, Some(this.clone()))
+		.into_future(fctx);
 	Ok(this)
 }
 
@@ -375,44 +284,45 @@
 	Ok(match object {
 		ObjBody::MemberList(members) => evaluate_member_list_object(s, ctx, members)?,
 		ObjBody::ObjComp(obj) => {
-			let future_this = Pending::new();
 			let mut builder = ObjValueBuilder::new();
-			evaluate_comp(s.clone(), ctx, &obj.compspecs, &mut |ctx| {
-				let new_bindings = Pending::new();
-				let cctx = ContextCreator(ctx.clone(), new_bindings.clone());
-				let mut bindings: GcHashMap<IStr, LazyBinding> =
-					GcHashMap::with_capacity(obj.pre_locals.len() + obj.post_locals.len());
-				for r in obj
-					.pre_locals
+			let locals = Rc::new(
+				obj.pre_locals
 					.iter()
 					.chain(obj.post_locals.iter())
-					.map(|b| evaluate_binding(b.clone(), cctx.clone()))
-				{
-					let (n, b) = r?;
-					bindings.insert(n, b);
-				}
-				new_bindings.fill(bindings.clone());
-				let ctx = ctx.extend_unbound(s.clone(), bindings, None, None, None)?;
+					.cloned()
+					.collect::<Vec<_>>(),
+			);
+			let mut ctxs = vec![];
+			evaluate_comp(s.clone(), ctx, &obj.compspecs, &mut |ctx| {
 				let key = evaluate(s.clone(), ctx.clone(), &obj.key)?;
+				let fctx = Context::new_future();
+				ctxs.push((ctx, fctx.clone()));
+				let uctx = evaluate_object_locals(fctx, locals.clone());
 
 				match key {
 					Val::Null => {}
 					Val::Str(n) => {
 						#[derive(Trace)]
-						struct ObjCompBinding {
-							ctx: Context,
+						struct ObjCompBinding<B> {
+							uctx: B,
 							value: LocExpr,
 						}
-						impl Bindable for ObjCompBinding {
+						impl<B: Bindable<Bound = Context>> Bindable for ObjCompBinding<B> {
+							type Bound = Thunk<Val>;
 							fn bind(
 								&self,
 								s: State,
+								sup: Option<ObjValue>,
 								this: Option<ObjValue>,
-								_super_obj: Option<ObjValue>,
 							) -> Result<Thunk<Val>> {
 								Ok(Thunk::evaluated(evaluate(
-									s,
-									self.ctx.clone().extend(GcHashMap::new(), None, this, None),
+									s.clone(),
+									self.uctx.bind(s, sup, this.clone())?.extend(
+										GcHashMap::new(),
+										None,
+										None,
+										this,
+									),
 									&self.value,
 								)?))
 							}
@@ -424,7 +334,7 @@
 							.bindable(
 								s.clone(),
 								tb!(ObjCompBinding {
-									ctx,
+									uctx,
 									value: obj.value.clone(),
 								}),
 							)?;
@@ -436,7 +346,11 @@
 			})?;
 
 			let this = builder.build();
-			future_this.fill(this.clone());
+			for (ctx, fctx) in ctxs {
+				let _ctx = ctx
+					.extend(GcHashMap::new(), None, None, Some(this.clone()))
+					.into_future(fctx);
+			}
 			this
 		}
 	})
@@ -596,7 +510,7 @@
 			for b in bindings {
 				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;
 			}
-			let ctx = ctx.extend_bound(new_bindings).into_future(fctx);
+			let ctx = ctx.extend(new_bindings, None, None, None).into_future(fctx);
 			evaluate(s, ctx, &returned.clone())?
 		}
 		Arr(items) => {
modifiedcrates/jrsonnet-evaluator/src/function/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function/mod.rs
+++ b/crates/jrsonnet-evaluator/src/function/mod.rs
@@ -139,11 +139,12 @@
 	) -> Result<Val> {
 		match self {
 			Self::Id => {
+				#[allow(clippy::unnecessary_wraps)]
 				#[builtin]
-				fn builtin_id(v: Any) -> Result<Any> {
+				const fn builtin_id(v: Any) -> Result<Any> {
 					Ok(v)
 				}
-				static ID: &'static builtin_id = &builtin_id {};
+				static ID: &builtin_id = &builtin_id {};
 
 				ID.call(s, call_ctx, loc, args)
 			}
@@ -159,10 +160,10 @@
 		self.evaluate(s, Context::default(), CallLocation::native(), args, true)
 	}
 
-	pub fn is_identity(&self) -> bool {
+	pub const fn is_identity(&self) -> bool {
 		matches!(self, Self::Id)
 	}
-	pub fn identity() -> Self {
+	pub const fn identity() -> Self {
 		Self::Id
 	}
 }
modifiedcrates/jrsonnet-evaluator/src/function/parse.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function/parse.rs
+++ b/crates/jrsonnet-evaluator/src/function/parse.rs
@@ -111,7 +111,7 @@
 
 		Ok(body_ctx
 			.extend(passed_args, None, None, None)
-			.extend_bound(defaults)
+			.extend(defaults, None, None, None)
 			.into_future(fctx))
 	} else {
 		let body_ctx = body_ctx.extend(passed_args, None, None, None);
modifiedcrates/jrsonnet-evaluator/src/gc.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/gc.rs
+++ b/crates/jrsonnet-evaluator/src/gc.rs
@@ -10,7 +10,7 @@
 use rustc_hash::{FxHashMap, FxHashSet};
 
 /// Replacement for box, which assumes that the underlying type is [`Trace`]
-/// Used in places, where Cc<dyn Trait> should be used instead, but it can't, because CoerceUnsiced is not stable
+/// Used in places, where `Cc<dyn Trait>` should be used instead, but it can't, because `CoerceUnsiced` is not stable
 #[derive(Debug, Clone)]
 pub struct TraceBox<T: ?Sized>(pub Box<T>);
 #[macro_export]
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -61,17 +61,13 @@
 pub use val::{ManifestFormat, Thunk, Val};
 
 pub trait Bindable: Trace + 'static {
-	fn bind(
-		&self,
-		s: State,
-		this: Option<ObjValue>,
-		super_obj: Option<ObjValue>,
-	) -> Result<Thunk<Val>>;
+	type Bound;
+	fn bind(&self, s: State, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Self::Bound>;
 }
 
 #[derive(Clone, Trace)]
 pub enum LazyBinding {
-	Bindable(Cc<TraceBox<dyn Bindable>>),
+	Bindable(Cc<TraceBox<dyn Bindable<Bound = Thunk<Val>>>>),
 	Bound(Thunk<Val>),
 }
 
@@ -84,11 +80,11 @@
 	pub fn evaluate(
 		&self,
 		s: State,
+		sup: Option<ObjValue>,
 		this: Option<ObjValue>,
-		super_obj: Option<ObjValue>,
 	) -> Result<Thunk<Val>> {
 		match self {
-			Self::Bindable(v) => v.bind(s, this, super_obj),
+			Self::Bindable(v) => v.bind(s, sup, this),
 			Self::Bound(v) => Ok(v.clone()),
 		}
 	}
@@ -345,7 +341,7 @@
 		for (name, value) in globals.iter() {
 			new_bindings.insert(name.clone(), Thunk::evaluated(value.clone()));
 		}
-		Context::new().extend_bound(new_bindings)
+		Context::new().extend(new_bindings, None, None, None)
 	}
 
 	/// Executes code creating a new stack frame
modifiedcrates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -106,7 +106,7 @@
 }
 
 pub trait ObjectAssertion: Trace {
-	fn run(&self, s: State, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<()>;
+	fn run(&self, s: State, super_obj: Option<ObjValue>, this: Option<ObjValue>) -> Result<()>;
 }
 
 // Field => This
@@ -124,10 +124,11 @@
 #[derive(Trace)]
 #[force_tracking]
 pub struct ObjValueInternals {
-	super_obj: Option<ObjValue>,
+	sup: Option<ObjValue>,
+	this: Option<ObjValue>,
+
 	assertions: Cc<Vec<TraceBox<dyn ObjectAssertion>>>,
 	assertions_ran: RefCell<GcHashSet<ObjValue>>,
-	this_obj: Option<ObjValue>,
 	this_entries: Cc<GcHashMap<IStr, ObjMember>>,
 	value_cache: RefCell<GcHashMap<CacheKey, CacheValue>>,
 }
@@ -153,7 +154,7 @@
 pub struct ObjValue(pub(crate) Cc<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() {
+		if let Some(super_obj) = self.0.sup.as_ref() {
 			if f.alternate() {
 				write!(f, "{:#?}", super_obj)?;
 			} else {
@@ -171,15 +172,15 @@
 
 impl ObjValue {
 	pub fn new(
-		super_obj: Option<Self>,
+		sup: Option<Self>,
 		this_entries: Cc<GcHashMap<IStr, ObjMember>>,
 		assertions: Cc<Vec<TraceBox<dyn ObjectAssertion>>>,
 	) -> Self {
 		Self(Cc::new(ObjValueInternals {
-			super_obj,
+			sup,
+			this: None,
 			assertions,
 			assertions_ran: RefCell::new(GcHashSet::new()),
-			this_obj: None,
 			this_entries,
 			value_cache: RefCell::new(GcHashMap::new()),
 		}))
@@ -188,15 +189,15 @@
 		Self::new(None, Cc::new(GcHashMap::new()), Cc::new(Vec::new()))
 	}
 	#[must_use]
-	pub fn extend_from(&self, super_obj: Self) -> Self {
-		match &self.0.super_obj {
+	pub fn extend_from(&self, sup: Self) -> Self {
+		match &self.0.sup {
 			None => Self::new(
-				Some(super_obj),
+				Some(sup),
 				self.0.this_entries.clone(),
 				self.0.assertions.clone(),
 			),
 			Some(v) => Self::new(
-				Some(v.extend_from(super_obj)),
+				Some(v.extend_from(sup)),
 				self.0.this_entries.clone(),
 				self.0.assertions.clone(),
 			),
@@ -212,12 +213,12 @@
 	}
 
 	#[must_use]
-	pub fn with_this(&self, this_obj: Self) -> Self {
+	pub fn with_this(&self, this: Self) -> Self {
 		Self(Cc::new(ObjValueInternals {
-			super_obj: self.0.super_obj.clone(),
+			sup: self.0.sup.clone(),
 			assertions: self.0.assertions.clone(),
 			assertions_ran: RefCell::new(GcHashSet::new()),
-			this_obj: Some(this_obj),
+			this: Some(this),
 			this_entries: self.0.this_entries.clone(),
 			value_cache: RefCell::new(GcHashMap::new()),
 		}))
@@ -234,7 +235,7 @@
 		if !self.0.this_entries.is_empty() {
 			return false;
 		}
-		self.0.super_obj.as_ref().map_or(true, Self::is_empty)
+		self.0.sup.as_ref().map_or(true, Self::is_empty)
 	}
 
 	/// Run callback for every field found in object
@@ -243,7 +244,7 @@
 		depth: SuperDepth,
 		handler: &mut impl FnMut(SuperDepth, &IStr, &ObjMember) -> bool,
 	) -> bool {
-		if let Some(s) = &self.0.super_obj {
+		if let Some(s) = &self.0.sup {
 			if s.enum_fields(depth.deeper(), handler) {
 				return true;
 			}
@@ -332,13 +333,13 @@
 			Some(match &m.visibility {
 				Visibility::Normal => self
 					.0
-					.super_obj
+					.sup
 					.as_ref()
 					.and_then(|super_obj| super_obj.field_visibility(name))
 					.unwrap_or(Visibility::Normal),
 				v => *v,
 			})
-		} else if let Some(super_obj) = &self.0.super_obj {
+		} else if let Some(super_obj) = &self.0.sup {
 			super_obj.field_visibility(name)
 		} else {
 			None
@@ -348,7 +349,7 @@
 	fn has_field_include_hidden(&self, name: IStr) -> bool {
 		if self.0.this_entries.contains_key(&name) {
 			true
-		} else if let Some(super_obj) = &self.0.super_obj {
+		} else if let Some(super_obj) = &self.0.sup {
 			super_obj.has_field_include_hidden(name)
 		} else {
 			false
@@ -369,13 +370,12 @@
 
 	pub fn get(&self, s: State, key: IStr) -> Result<Option<Val>> {
 		self.run_assertions(s.clone())?;
-		self.get_raw(s, key, self.0.this_obj.as_ref())
+		self.get_raw(s, key, self.0.this.clone().unwrap_or_else(|| self.clone()))
 	}
 
 	// pub fn extend_with(self, key: )
 
-	fn get_raw(&self, s: State, key: IStr, real_this: Option<&Self>) -> Result<Option<Val>> {
-		let real_this = real_this.unwrap_or(self);
+	fn get_raw(&self, s: State, key: IStr, real_this: Self) -> Result<Option<Val>> {
 		let cache_key = (key.clone(), WeakObjValue(real_this.0.downgrade()));
 
 		if let Some(v) = self.0.value_cache.borrow().get(&cache_key) {
@@ -397,17 +397,17 @@
 				.insert(cache_key.clone(), CacheValue::Errored(e.clone()));
 			e
 		};
-		let value = match (self.0.this_entries.get(&key), &self.0.super_obj) {
+		let value = match (self.0.this_entries.get(&key), &self.0.sup) {
 			(Some(k), None) => Ok(Some(
 				self.evaluate_this(s, k, real_this).map_err(fill_error)?,
 			)),
 			(Some(k), Some(super_obj)) => {
 				let our = self
-					.evaluate_this(s.clone(), k, real_this)
+					.evaluate_this(s.clone(), k, real_this.clone())
 					.map_err(fill_error)?;
 				if k.add {
 					super_obj
-						.get_raw(s.clone(), key, Some(real_this))
+						.get_raw(s.clone(), key, real_this)
 						.map_err(fill_error)?
 						.map_or(Ok(Some(our.clone())), |v| {
 							Ok(Some(evaluate_add_op(s.clone(), &v, &our)?))
@@ -416,7 +416,7 @@
 					Ok(Some(our))
 				}
 			}
-			(None, Some(super_obj)) => super_obj.get_raw(s, key, Some(real_this)),
+			(None, Some(super_obj)) => super_obj.get_raw(s, key, real_this),
 			(None, None) => Ok(None),
 		}
 		.map_err(fill_error)?;
@@ -429,9 +429,9 @@
 		);
 		Ok(value)
 	}
-	fn evaluate_this(&self, s: State, v: &ObjMember, real_this: &Self) -> Result<Val> {
+	fn evaluate_this(&self, s: State, v: &ObjMember, real_this: Self) -> Result<Val> {
 		v.invoke
-			.evaluate(s.clone(), Some(real_this.clone()), self.0.super_obj.clone())?
+			.evaluate(s.clone(), self.0.sup.clone(), Some(real_this))?
 			.evaluate(s)
 	}
 
@@ -439,13 +439,13 @@
 		if self.0.assertions_ran.borrow_mut().insert(real_this.clone()) {
 			for assertion in self.0.assertions.iter() {
 				if let Err(e) =
-					assertion.run(s.clone(), Some(real_this.clone()), self.0.super_obj.clone())
+					assertion.run(s.clone(), self.0.sup.clone(), Some(real_this.clone()))
 				{
 					self.0.assertions_ran.borrow_mut().remove(real_this);
 					return Err(e);
 				}
 			}
-			if let Some(super_obj) = &self.0.super_obj {
+			if let Some(super_obj) = &self.0.sup {
 				super_obj.run_assertions_raw(s, real_this)?;
 			}
 		}
@@ -458,6 +458,9 @@
 	pub fn ptr_eq(a: &Self, b: &Self) -> bool {
 		cc_ptr_eq(&a.0, &b.0)
 	}
+	pub fn downgrade(self) -> WeakObjValue {
+		WeakObjValue(self.0.downgrade())
+	}
 }
 
 impl PartialEq for ObjValue {
@@ -475,7 +478,7 @@
 
 #[allow(clippy::module_name_repetitions)]
 pub struct ObjValueBuilder {
-	super_obj: Option<ObjValue>,
+	sup: Option<ObjValue>,
 	map: GcHashMap<IStr, ObjMember>,
 	assertions: Vec<TraceBox<dyn ObjectAssertion>>,
 	next_field_index: FieldIndex,
@@ -486,7 +489,7 @@
 	}
 	pub fn with_capacity(capacity: usize) -> Self {
 		Self {
-			super_obj: None,
+			sup: None,
 			map: GcHashMap::with_capacity(capacity),
 			assertions: Vec::new(),
 			next_field_index: FieldIndex::default(),
@@ -497,7 +500,7 @@
 		self
 	}
 	pub fn with_super(&mut self, super_obj: ObjValue) -> &mut Self {
-		self.super_obj = Some(super_obj);
+		self.sup = Some(super_obj);
 		self
 	}
 
@@ -512,7 +515,7 @@
 	}
 
 	pub fn build(self) -> ObjValue {
-		ObjValue::new(self.super_obj, Cc::new(self.map), Cc::new(self.assertions))
+		ObjValue::new(self.sup, Cc::new(self.map), Cc::new(self.assertions))
 	}
 }
 impl Default for ObjValueBuilder {
@@ -583,7 +586,11 @@
 	pub fn value(self, s: State, value: Val) -> Result<()> {
 		self.binding(s, LazyBinding::Bound(Thunk::evaluated(value)))
 	}
-	pub fn bindable(self, s: State, bindable: TraceBox<dyn Bindable>) -> Result<()> {
+	pub fn bindable(
+		self,
+		s: State,
+		bindable: TraceBox<dyn Bindable<Bound = Thunk<Val>>>,
+	) -> Result<()> {
 		self.binding(s, LazyBinding::Bindable(Cc::new(bindable)))
 	}
 	pub fn binding(self, s: State, binding: LazyBinding) -> Result<()> {
@@ -606,7 +613,7 @@
 	pub fn value(self, value: Val) {
 		self.binding(LazyBinding::Bound(Thunk::evaluated(value)));
 	}
-	pub fn bindable(self, bindable: TraceBox<dyn Bindable>) {
+	pub fn bindable(self, bindable: TraceBox<dyn Bindable<Bound = Thunk<Val>>>) {
 		self.binding(LazyBinding::Bindable(Cc::new(bindable)));
 	}
 	pub fn binding(self, binding: LazyBinding) {
modifiedcrates/jrsonnet-evaluator/src/stdlib/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/stdlib/mod.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/mod.rs
@@ -466,7 +466,7 @@
 	Ok(ArrValue::Eager(sort::sort(
 		s.clone(),
 		arr.evaluated(s)?,
-		keyF.unwrap_or(FuncVal::identity()),
+		keyF.unwrap_or_else(FuncVal::identity),
 	)?))
 }
 
modifiedcrates/jrsonnet-evaluator/src/stdlib/sort.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/stdlib/sort.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/sort.rs
@@ -68,7 +68,23 @@
 	if values.len() <= 1 {
 		return Ok(values);
 	}
-	if !key_getter.is_identity() {
+	if key_getter.is_identity() {
+		// Fast path, identity key getter
+		let mut values = (*values).clone();
+		let sort_type = get_sort_type(&mut values, |k| k)?;
+		match sort_type {
+			SortKeyType::Number => values.sort_unstable_by_key(|v| match v {
+				Val::Num(n) => NonNaNf64(*n),
+				_ => unreachable!(),
+			}),
+			SortKeyType::String => values.sort_unstable_by_key(|v| match v {
+				Val::Str(s) => s.clone(),
+				_ => unreachable!(),
+			}),
+			SortKeyType::Unknown => unreachable!(),
+		};
+		Ok(Cc::new(values))
+	} else {
 		// Slow path, user provided key getter
 		let mut vk = Vec::with_capacity(values.len());
 		for value in values.iter() {
@@ -90,21 +106,5 @@
 			SortKeyType::Unknown => unreachable!(),
 		};
 		Ok(Cc::new(vk.into_iter().map(|v| v.0).collect()))
-	} else {
-		// Fast path, identity key getter
-		let mut values = (*values).clone();
-		let sort_type = get_sort_type(&mut values, |k| k)?;
-		match sort_type {
-			SortKeyType::Number => values.sort_unstable_by_key(|v| match v {
-				Val::Num(n) => NonNaNf64(*n),
-				_ => unreachable!(),
-			}),
-			SortKeyType::String => values.sort_unstable_by_key(|v| match v {
-				Val::Str(s) => s.clone(),
-				_ => unreachable!(),
-			}),
-			SortKeyType::Unknown => unreachable!(),
-		};
-		Ok(Cc::new(values))
 	}
 }
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -8,11 +8,11 @@
 	cc_ptr_eq,
 	error::{Error::*, LocError},
 	function::FuncVal,
-	gc::TraceBox,
+	gc::{GcHashMap, TraceBox},
 	stdlib::manifest::{
 		manifest_json_ex, manifest_yaml_ex, ManifestJsonOptions, ManifestType, ManifestYamlOptions,
 	},
-	throw, ObjValue, Result, State,
+	throw, Bindable, ObjValue, Result, State, WeakObjValue,
 };
 
 pub trait ThunkValue: Trace {
@@ -71,6 +71,47 @@
 	}
 }
 
+type CacheKey = (Option<WeakObjValue>, Option<WeakObjValue>);
+
+#[derive(Trace, Clone)]
+pub struct CachedBindable<I, T>
+where
+	I: Bindable<Bound = T>,
+{
+	cache: Cc<RefCell<GcHashMap<CacheKey, T>>>,
+	value: I,
+}
+impl<I: Bindable<Bound = T>, T: Trace> CachedBindable<I, T> {
+	pub fn new(value: I) -> Self {
+		Self {
+			cache: Cc::new(RefCell::new(GcHashMap::new())),
+			value,
+		}
+	}
+}
+impl<I: Bindable<Bound = T>, T: Clone + Trace> Bindable for CachedBindable<I, T> {
+	type Bound = T;
+	fn bind(&self, s: State, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<T> {
+		let cache_key = (
+			sup.as_ref().map(|s| s.clone().downgrade()),
+			this.as_ref().map(|t| t.clone().downgrade()),
+		);
+		{
+			if let Some(t) = self.cache.borrow().get(&cache_key) {
+				return Ok(t.clone());
+			}
+		}
+		let bound = self.value.bind(s, sup, this)?;
+
+		{
+			let mut cache = self.cache.borrow_mut();
+			cache.insert(cache_key, bound.clone());
+		}
+
+		Ok(bound)
+	}
+}
+
 impl<T: Debug> Debug for Thunk<T> {
 	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 		write!(f, "Lazy")
modifiedcrates/jrsonnet-evaluator/tests/builtin.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/tests/builtin.rs
+++ b/crates/jrsonnet-evaluator/tests/builtin.rs
@@ -6,7 +6,6 @@
 use jrsonnet_evaluator::{
 	error::Result,
 	function::{builtin, builtin::Builtin, CallLocation, FuncVal},
-	gc::TraceBox,
 	tb,
 	typed::Typed,
 	State, Val,
modifiedcrates/jrsonnet-evaluator/tests/common.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/tests/common.rs
+++ b/crates/jrsonnet-evaluator/tests/common.rs
@@ -30,8 +30,18 @@
 		if !::jrsonnet_evaluator::val::equals($s.clone(), &$a.clone(), &$b.clone())? {
 			::jrsonnet_evaluator::throw_runtime!(
 				"assertion failed: a != b\na={:#?}\nb={:#?}",
-				$a.to_json($s.clone(), 2)?,
-				$b.to_json($s.clone(), 2)?,
+				$a.to_json(
+					$s.clone(),
+					2,
+					#[cfg(feature = "exp-preserve-order")]
+					false
+				)?,
+				$b.to_json(
+					$s.clone(),
+					2,
+					#[cfg(feature = "exp-preserve-order")]
+					false
+				)?,
 			)
 		}
 	}};
modifiedcrates/jrsonnet-evaluator/tests/golden.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/tests/golden.rs
+++ b/crates/jrsonnet-evaluator/tests/golden.rs
@@ -24,7 +24,12 @@
 		Ok(v) => v,
 		Err(e) => return s.stringify_err(&e),
 	};
-	match v.to_json(s.clone(), 3) {
+	match v.to_json(
+		s.clone(),
+		3,
+		#[cfg(feature = "exp-preserve-order")]
+		false,
+	) {
 		Ok(v) => v.to_string(),
 		Err(e) => s.stringify_err(&e),
 	}
modifiedcrates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-parser/src/lib.rs
1#![allow(clippy::redundant_closure_call)]23use std::{4	path::{Path, PathBuf},5	rc::Rc,6};78use peg::parser;9mod expr;10pub use expr::*;11pub use jrsonnet_interner::IStr;12pub use peg;13mod unescape;1415pub struct ParserSettings {16	pub file_name: Rc<Path>,17}1819macro_rules! expr_bin {20	($a:ident $op:ident $b:ident) => {21		Expr::BinaryOp($a, $op, $b)22	};23}24macro_rules! expr_un {25	($op:ident $a:ident) => {26		Expr::UnaryOp($op, $a)27	};28}2930parser! {31	grammar jsonnet_parser() for str {32		use peg::ParseLiteral;3334		rule eof() = quiet!{![_]} / expected!("<eof>")35		rule eol() = "\n" / eof()3637		/// Standard C-like comments38		rule comment()39			= "//" (!eol()[_])* eol()40			/ "/*" ("\\*/" / "\\\\" / (!("*/")[_]))* "*/"41			/ "#" (!eol()[_])* eol()4243		rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")44		rule _() = quiet!{([' ' | '\r' | '\n' | '\t']+) / comment()}* / expected!("<whitespace>")4546		/// For comma-delimited elements47		rule comma() = quiet!{_ "," _} / expected!("<comma>")48		rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}49		rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}50		rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']51		/// Sequence of digits52		rule uint_str() -> &'input str = a:$(digit()+) { a }53		/// Number in scientific notation format54		rule number() -> f64 = quiet!{a:$(uint_str() ("." uint_str())? (['e'|'E'] (s:['+'|'-'])? uint_str())?) {? a.parse().map_err(|_| "<number>") }} / expected!("<number>")5556		/// Reserved word followed by any non-alphanumberic57		rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "importbin" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()58		rule id() -> IStr = v:$(quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")) { v.into() }5960		rule keyword(id: &'static str) -> ()61			= ##parse_string_literal(id) end_of_ident()6263		pub rule param(s: &ParserSettings) -> expr::Param = name:id() expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name, expr) }64		pub rule params(s: &ParserSettings) -> expr::ParamsDesc65			= params:param(s) ** comma() comma()? { expr::ParamsDesc(Rc::new(params)) }66			/ { expr::ParamsDesc(Rc::new(Vec::new())) }6768		pub rule arg(s: &ParserSettings) -> (Option<IStr>, LocExpr)69			= quiet! { name:(s:id() _ "=" _ {s})? expr:expr(s) {(name, expr)} }70			/ expected!("<argument>")7172		pub rule args(s: &ParserSettings) -> expr::ArgsDesc73			= args:arg(s)**comma() comma()? {?74				let unnamed_count = args.iter().take_while(|(n, _)| n.is_none()).count();75				let mut unnamed = Vec::with_capacity(unnamed_count);76				let mut named = Vec::with_capacity(args.len() - unnamed_count);77				let mut named_started = false;78				for (name, value) in args {79					if let Some(name) = name {80						named_started = true;81						named.push((name, value));82					} else {83						if named_started {84							return Err("<named argument>")85						}86						unnamed.push(value);87					}88				}89				Ok(expr::ArgsDesc::new(unnamed, named))90			}9192		pub rule destruct_rest() -> expr::DestructRest93			= "..." into:(_ into:id() {into})? {if let Some(into) = into {94				expr::DestructRest::Keep(into)95			} else {expr::DestructRest::Drop}}96		pub rule destruct_array(s: &ParserSettings) -> expr::Destruct97			= "[" _ start:destruct(s)**comma() rest:(98				comma() _ rest:destruct_rest()? end:(99					comma() end:destruct(s)**comma() (_ comma())? {end}100					/ comma()? {Vec::new()}101				) {(rest, end)}102				/ comma()? {(None, Vec::new())}103			) _ "]" {?104				#[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Array {105					start,106					rest: rest.0,107					end: rest.1,108				});109				#[cfg(not(feature = "exp-destruct"))] Err("experimental destructuring was not enabled")110			}111		pub rule destruct_object(s: &ParserSettings) -> expr::Destruct112			= "{" _113				fields:(name:id() _ into:(":" _ into:destruct(s) {into})? {(name, into)})**comma()114				rest:(115					comma() rest:destruct_rest()? {rest}116					/ comma()? {None}117				)118			_ "}" {?119				#[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Object {120					fields,121					rest,122				});123				#[cfg(not(feature = "exp-destruct"))] Err("experimental destructuring was not enabled")124			}125		pub rule destruct(s: &ParserSettings) -> expr::Destruct126			= v:id() {expr::Destruct::Full(v)}127			/ "?" {?128				#[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Skip);129				#[cfg(not(feature = "exp-destruct"))] Err("experimental destructuring was not enabled")130			}131			/ arr:destruct_array(s) {arr}132			/ obj:destruct_object(s) {obj}133134		pub rule bind(s: &ParserSettings) -> expr::BindSpec135			= into:destruct(s) _ "=" _ expr:expr(s) {expr::BindSpec::Field{into, value: expr}}136			/ name:id() _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec::Function{name, params, value: expr}}137138		pub rule assertion(s: &ParserSettings) -> expr::AssertStmt139			= keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }140141		pub rule whole_line() -> &'input str142			= str:$((!['\n'][_])* "\n") {str}143		pub rule string_block() -> String144			= "|||" (!['\n']single_whitespace())* "\n"145			  empty_lines:$(['\n']*)146			  prefix:[' ' | '\t']+ first_line:whole_line()147			  lines:("\n" {"\n"} / [' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*148			  [' ' | '\t']*<, {prefix.len() - 1}> "|||"149			  {let mut l = empty_lines.to_owned(); l.push_str(first_line); l.extend(lines); l}150151		rule hex_char()152			= quiet! { ['0'..='9' | 'a'..='f' | 'A'..='F'] } / expected!("<hex char>")153154		rule string_char(c: rule<()>)155			= (!['\\']!c()[_])+156			/ "\\\\"157			/ "\\u" hex_char() hex_char() hex_char() hex_char()158			/ "\\x" hex_char() hex_char()159			/ ['\\'] (quiet! { ['b' | 'f' | 'n' | 'r' | 't' | '"' | '\''] } / expected!("<escape character>"))160		pub rule string() -> String161			= ['"'] str:$(string_char(<"\"">)*) ['"'] {? unescape::unescape(str).ok_or("<escaped string>")}162			/ ['\''] str:$(string_char(<"\'">)*) ['\''] {? unescape::unescape(str).ok_or("<escaped string>")}163			/ quiet!{ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}164			/ "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}165			/ string_block() } / expected!("<string>")166167		pub rule field_name(s: &ParserSettings) -> expr::FieldName168			= name:id() {expr::FieldName::Fixed(name.into())}169			/ name:string() {expr::FieldName::Fixed(name.into())}170			/ "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}171		pub rule visibility() -> expr::Visibility172			= ":::" {expr::Visibility::Unhide}173			/ "::" {expr::Visibility::Hidden}174			/ ":" {expr::Visibility::Normal}175		pub rule field(s: &ParserSettings) -> expr::FieldMember176			= name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{177				name,178				plus: plus.is_some(),179				params: None,180				visibility,181				value,182			}}183			/ name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{184				name,185				plus: false,186				params: Some(params),187				visibility,188				value,189			}}190		pub rule obj_local(s: &ParserSettings) -> BindSpec191			= keyword("local") _ bind:bind(s) {bind}192		pub rule member(s: &ParserSettings) -> expr::Member193			= bind:obj_local(s) {expr::Member::BindStmt(bind)}194			/ assertion:assertion(s) {expr::Member::AssertStmt(assertion)}195			/ field:field(s) {expr::Member::Field(field)}196		pub rule objinside(s: &ParserSettings) -> expr::ObjBody197			= pre_locals:(b: obj_local(s) comma() {b})* "[" _ key:expr(s) _ "]" _ plus:"+"? _ ":" _ value:expr(s) post_locals:(comma() b:obj_local(s) {b})* _ forspec:forspec(s) others:(_ rest:compspec(s) {rest})? {198				let mut compspecs = vec![CompSpec::ForSpec(forspec)];199				compspecs.extend(others.unwrap_or_default());200				expr::ObjBody::ObjComp(expr::ObjComp{201					pre_locals,202					key,203					plus: plus.is_some(),204					value,205					post_locals,206					compspecs,207				})208			}209			/ members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}210		pub rule ifspec(s: &ParserSettings) -> IfSpecData211			= keyword("if") _ expr:expr(s) {IfSpecData(expr)}212		pub rule forspec(s: &ParserSettings) -> ForSpecData213			= keyword("for") _ id:id() _ keyword("in") _ cond:expr(s) {ForSpecData(id, cond)}214		pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>215			= s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}216		pub rule local_expr(s: &ParserSettings) -> Expr217			= keyword("local") _ binds:bind(s) ** comma() _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }218		pub rule string_expr(s: &ParserSettings) -> Expr219			= s:string() {Expr::Str(s.into())}220		pub rule obj_expr(s: &ParserSettings) -> Expr221			= "{" _ body:objinside(s) _ "}" {Expr::Obj(body)}222		pub rule array_expr(s: &ParserSettings) -> Expr223			= "[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}224		pub rule array_comp_expr(s: &ParserSettings) -> Expr225			= "[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {226				let mut specs = vec![CompSpec::ForSpec(forspec)];227				specs.extend(others.unwrap_or_default());228				Expr::ArrComp(expr, specs)229			}230		pub rule number_expr(s: &ParserSettings) -> Expr231			= n:number() { expr::Expr::Num(n) }232		pub rule var_expr(s: &ParserSettings) -> Expr233			= n:id() { expr::Expr::Var(n) }234		pub rule id_loc(s: &ParserSettings) -> LocExpr235			= a:position!() n:id() b:position!() { LocExpr(Rc::new(expr::Expr::Str(n)), ExprLocation(s.file_name.clone(), a,b)) }236		pub rule if_then_else_expr(s: &ParserSettings) -> Expr237			= cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{238				cond,239				cond_then,240				cond_else,241			}}242243		pub rule literal(s: &ParserSettings) -> Expr244			= v:(245				keyword("null") {LiteralType::Null}246				/ keyword("true") {LiteralType::True}247				/ keyword("false") {LiteralType::False}248				/ keyword("self") {LiteralType::This}249				/ keyword("$") {LiteralType::Dollar}250				/ keyword("super") {LiteralType::Super}251			) {Expr::Literal(v)}252253		pub rule expr_basic(s: &ParserSettings) -> Expr254			= literal(s)255256			/ quiet!{"$intrinsicThisFile" {Expr::IntrinsicThisFile}}257			/ quiet!{"$intrinsicId" {Expr::IntrinsicId}}258			/ quiet!{"$intrinsic(" name:id() ")" {Expr::Intrinsic(name)}}259260			/ string_expr(s) / number_expr(s)261			/ array_expr(s)262			/ obj_expr(s)263			/ array_expr(s)264			/ array_comp_expr(s)265266			/ keyword("importstr") _ path:string() {Expr::ImportStr(PathBuf::from(path))}267			/ keyword("importbin") _ path:string() {Expr::ImportBin(PathBuf::from(path))}268			/ keyword("import") _ path:string() {Expr::Import(PathBuf::from(path))}269270			/ var_expr(s)271			/ local_expr(s)272			/ if_then_else_expr(s)273274			/ keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}275			/ assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }276277			/ keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }278279		rule slice_part(s: &ParserSettings) -> Option<LocExpr>280			= _ e:(e:expr(s) _{e})? {e}281		pub rule slice_desc(s: &ParserSettings) -> SliceDesc282			= start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {283				let (end, step) = if let Some((end, step)) = pair {284					(end, step)285				}else{286					(None, None)287				};288289				SliceDesc { start, end, step }290			}291292		rule binop(x: rule<()>) -> ()293			= quiet!{ x() } / expected!("<binary op>")294		rule unaryop(x: rule<()>) -> ()295			= quiet!{ x() } / expected!("<unary op>")296297298		use BinaryOpType::*;299		use UnaryOpType::*;300		rule expr(s: &ParserSettings) -> LocExpr301			= precedence! {302				start:position!() v:@ end:position!() { LocExpr(Rc::new(v), ExprLocation(s.file_name.clone(), start, end)) }303				--304				a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}305				--306				a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}307				--308				a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}309				--310				a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}311				--312				a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}313				--314				a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}315				a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}316				--317				a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}318				a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}319				a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}320				a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}321				a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}322				--323				a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}324				a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}325				--326				a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}327				a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}328				--329				a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}330				a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}331				a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}332				--333						unaryop(<"-">) _ b:@ {expr_un!(Minus b)}334						unaryop(<"!">) _ b:@ {expr_un!(Not b)}335						unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}336				--337				a:(@) _ "[" _ e:slice_desc(s) _ "]" {Expr::Slice(a, e)}338				a:(@) _ "." _ a:position!() e:id_loc(s) b:position!() {Expr::Index(a, e)}339				a:(@) _ "[" _ e:expr(s) _ "]" {Expr::Index(a, e)}340				a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {Expr::Apply(a, args, ts.is_some())}341				a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(a, body)}342				--343				e:expr_basic(s) {e}344				"(" _ e:expr(s) _ ")" {Expr::Parened(e)}345			}346347		pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}348	}349}350351pub type ParseError = peg::error::ParseError<peg::str::LineCol>;352pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {353	jsonnet_parser::jsonnet(str, settings)354}355/// Used for importstr values356pub fn string_to_expr(str: IStr, settings: &ParserSettings) -> LocExpr {357	let len = str.len();358	LocExpr(359		Rc::new(Expr::Str(str)),360		ExprLocation(settings.file_name.clone(), 0, len),361	)362}363364#[cfg(test)]365pub mod tests {366	use std::path::PathBuf;367368	use BinaryOpType::*;369370	use super::{expr::*, parse};371	use crate::ParserSettings;372373	macro_rules! parse {374		($s:expr) => {375			parse(376				$s,377				&ParserSettings {378					file_name: PathBuf::from("test.jsonnet").into(),379				},380			)381			.unwrap()382		};383	}384385	macro_rules! el {386		($expr:expr, $from:expr, $to:expr$(,)?) => {387			LocExpr(388				std::rc::Rc::new($expr),389				ExprLocation(PathBuf::from("test.jsonnet").into(), $from, $to),390			)391		};392	}393394	#[test]395	fn multiline_string() {396		assert_eq!(397			parse!("|||\n    Hello world!\n     a\n|||"),398			el!(Expr::Str("Hello world!\n a\n".into()), 0, 31),399		);400		assert_eq!(401			parse!("|||\n  Hello world!\n   a\n|||"),402			el!(Expr::Str("Hello world!\n a\n".into()), 0, 27),403		);404		assert_eq!(405			parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),406			el!(Expr::Str("Hello world!\n\ta\n".into()), 0, 27),407		);408		assert_eq!(409			parse!("|||\n   Hello world!\n    a\n |||"),410			el!(Expr::Str("Hello world!\n a\n".into()), 0, 30),411		);412	}413414	#[test]415	fn slice() {416		parse!("a[1:]");417		parse!("a[1::]");418		parse!("a[:1:]");419		parse!("a[::1]");420		parse!("str[:len - 1]");421	}422423	#[test]424	fn string_escaping() {425		assert_eq!(426			parse!(r#""Hello, \"world\"!""#),427			el!(Expr::Str(r#"Hello, "world"!"#.into()), 0, 19),428		);429		assert_eq!(430			parse!(r#"'Hello \'world\'!'"#),431			el!(Expr::Str("Hello 'world'!".into()), 0, 18),432		);433		assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into()), 0, 6));434	}435436	#[test]437	fn string_unescaping() {438		assert_eq!(439			parse!(r#""Hello\nWorld""#),440			el!(Expr::Str("Hello\nWorld".into()), 0, 14),441		);442	}443444	#[test]445	fn string_verbantim() {446		assert_eq!(447			parse!(r#"@"Hello\n""World""""#),448			el!(Expr::Str("Hello\\n\"World\"".into()), 0, 19),449		);450	}451452	#[test]453	fn imports() {454		assert_eq!(455			parse!("import \"hello\""),456			el!(Expr::Import(PathBuf::from("hello")), 0, 14),457		);458		assert_eq!(459			parse!("importstr \"garnish.txt\""),460			el!(Expr::ImportStr(PathBuf::from("garnish.txt")), 0, 23)461		);462	}463464	#[test]465	fn empty_object() {466		assert_eq!(467			parse!("{}"),468			el!(Expr::Obj(ObjBody::MemberList(vec![])), 0, 2)469		);470	}471472	#[test]473	fn basic_math() {474		assert_eq!(475			parse!("2+2*2"),476			el!(477				Expr::BinaryOp(478					el!(Expr::Num(2.0), 0, 1),479					Add,480					el!(481						Expr::BinaryOp(el!(Expr::Num(2.0), 2, 3), Mul, el!(Expr::Num(2.0), 4, 5)),482						2,483						5484					)485				),486				0,487				5488			)489		);490	}491492	#[test]493	fn basic_math_with_indents() {494		assert_eq!(495			parse!("2	+ 	  2	  *	2   	"),496			el!(497				Expr::BinaryOp(498					el!(Expr::Num(2.0), 0, 1),499					Add,500					el!(501						Expr::BinaryOp(el!(Expr::Num(2.0), 7, 8), Mul, el!(Expr::Num(2.0), 13, 14),),502						7,503						14504					),505				),506				0,507				14508			)509		);510	}511512	#[test]513	fn basic_math_parened() {514		assert_eq!(515			parse!("2+(2+2*2)"),516			el!(517				Expr::BinaryOp(518					el!(Expr::Num(2.0), 0, 1),519					Add,520					el!(521						Expr::Parened(el!(522							Expr::BinaryOp(523								el!(Expr::Num(2.0), 3, 4),524								Add,525								el!(526									Expr::BinaryOp(527										el!(Expr::Num(2.0), 5, 6),528										Mul,529										el!(Expr::Num(2.0), 7, 8),530									),531									5,532									8533								),534							),535							3,536							8537						)),538						2,539						9540					),541				),542				0,543				9544			)545		);546	}547548	/// Comments should not affect parsing549	#[test]550	fn comments() {551		assert_eq!(552			parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),553			el!(554				Expr::BinaryOp(555					el!(Expr::Num(2.0), 0, 1),556					Add,557					el!(558						Expr::BinaryOp(559							el!(Expr::Num(3.0), 22, 23),560							Mul,561							el!(Expr::Num(4.0), 40, 41)562						),563						22,564						41565					)566				),567				0,568				41569			)570		);571	}572573	/// Comments should be able to be escaped574	#[test]575	fn comment_escaping() {576		assert_eq!(577			parse!("2/*\\*/+*/ - 22"),578			el!(579				Expr::BinaryOp(el!(Expr::Num(2.0), 0, 1), Sub, el!(Expr::Num(22.0), 12, 14)),580				0,581				14582			)583		);584	}585586	#[test]587	fn suffix() {588		// assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));589		// assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));590		// assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));591		// assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))592	}593594	#[test]595	fn array_comp() {596		use Expr::*;597		/*598		`ArrComp(Apply(Index(Var("std") from "test.jsonnet":1-4, Var("deepJoin") from "test.jsonnet":5-13) from "test.jsonnet":1-13, ArgsDesc { unnamed: [Var("x") from "test.jsonnet":14-15], named: [] }, false) from "test.jsonnet":1-16, [ForSpec(ForSpecData("x", Var("arr") from "test.jsonnet":26-29))]) from "test.jsonnet":0-30`,599		`ArrComp(Apply(Index(Var("std") from "test.jsonnet":1-4, Str("deepJoin") from "test.jsonnet":5-13) from "test.jsonnet":1-13, ArgsDesc { unnamed: [Var("x") from "test.jsonnet":14-15], named: [] }, false) from "test.jsonnet":1-16, [ForSpec(ForSpecData("x", Var("arr") from "test.jsonnet":26-29))]) from "test.jsonnet":0-30`600				*/601		assert_eq!(602			parse!("[std.deepJoin(x) for x in arr]"),603			el!(604				ArrComp(605					el!(606						Apply(607							el!(608								Index(609									el!(Var("std".into()), 1, 4),610									el!(Str("deepJoin".into()), 5, 13)611								),612								1,613								13614							),615							ArgsDesc::new(vec![el!(Var("x".into()), 14, 15)], vec![]),616							false,617						),618						1,619						16620					),621					vec![CompSpec::ForSpec(ForSpecData(622						"x".into(),623						el!(Var("arr".into()), 26, 29)624					))]625				),626				0,627				30628			),629		)630	}631632	#[test]633	fn reserved() {634		use Expr::*;635		assert_eq!(parse!("null"), el!(Literal(LiteralType::Null), 0, 4));636		assert_eq!(parse!("nulla"), el!(Var("nulla".into()), 0, 5));637	}638639	#[test]640	fn multiple_args_buf() {641		parse!("a(b, null_fields)");642	}643644	#[test]645	fn infix_precedence() {646		use Expr::*;647		assert_eq!(648			parse!("!a && !b"),649			el!(650				BinaryOp(651					el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),652					And,653					el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 7, 8)), 6, 8)654				),655				0,656				8657			)658		);659	}660661	#[test]662	fn infix_precedence_division() {663		use Expr::*;664		assert_eq!(665			parse!("!a / !b"),666			el!(667				BinaryOp(668					el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),669					Div,670					el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 6, 7)), 5, 7)671				),672				0,673				7674			)675		);676	}677678	#[test]679	fn double_negation() {680		use Expr::*;681		assert_eq!(682			parse!("!!a"),683			el!(684				UnaryOp(685					UnaryOpType::Not,686					el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 2, 3)), 1, 3)687				),688				0,689				3690			)691		)692	}693694	#[test]695	fn array_test_error() {696		parse!("[a for a in b if c for e in f]");697		//                    ^^^^ failed code698	}699700	#[test]701	fn missing_newline_between_comment_and_eof() {702		parse!(703			"{a:1}704705			//+213"706		);707	}708709	#[test]710	fn default_param_before_nondefault() {711		parse!("local x(foo = 'foo', bar) = null; null");712	}713714	#[test]715	fn can_parse_stdlib() {716		parse!(jrsonnet_stdlib::STDLIB_STR);717	}718719	#[test]720	fn add_location_info_to_all_sub_expressions() {721		use Expr::*;722723		let file_name: std::rc::Rc<std::path::Path> = PathBuf::from("test.jsonnet").into();724		let expr = parse(725			"{} { local x = 1, x: x } + {}",726			&ParserSettings {727				file_name: file_name.clone(),728			},729		)730		.unwrap();731		assert_eq!(732			expr,733			el!(734				BinaryOp(735					el!(736						ObjExtend(737							el!(Obj(ObjBody::MemberList(vec![])), 0, 2),738							ObjBody::MemberList(vec![739								Member::BindStmt(BindSpec::Field {740									into: Destruct::Full("x".into()),741									value: el!(Num(1.0), 15, 16)742								}),743								Member::Field(FieldMember {744									name: FieldName::Fixed("x".into()),745									plus: false,746									params: None,747									visibility: Visibility::Normal,748									value: el!(Var("x".into()), 21, 22),749								})750							])751						),752						0,753						24754					),755					BinaryOpType::Add,756					el!(Obj(ObjBody::MemberList(vec![])), 27, 29),757				),758				0,759				29760			),761		);762	}763	// From source code764	/*765	#[bench]766	fn bench_parse_peg(b: &mut Bencher) {767		b.iter(|| parse!(jrsonnet_stdlib::STDLIB_STR))768	}769	*/770}