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

difftreelog

refactor get rid of LazyBinding variants

Yaroslav Bolyukin2021-02-22parent: #3154bc9.patch.diff
in: master

5 files changed

modifiedcrates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -1,17 +1,24 @@
 use crate::{
-	error::Error::*, map::LayeredHashMap, rc_fn_helper, resolved_lazy_val, FutureWrapper,
-	LazyBinding, LazyVal, ObjValue, Result, Val,
+	error::Error::*, map::LayeredHashMap, resolved_lazy_val, FutureWrapper, LazyBinding, LazyVal,
+	ObjValue, Result, Val,
 };
 use jrsonnet_interner::IStr;
 use rustc_hash::FxHashMap;
 use std::hash::BuildHasherDefault;
-use std::{collections::HashMap, fmt::Debug, rc::Rc};
+use std::{fmt::Debug, rc::Rc};
 
-rc_fn_helper!(
-	ContextCreator,
-	context_creator,
-	dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<Context>
-);
+#[derive(Clone)]
+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> {
+		self.0.clone().extend_unbound(
+			self.1.clone().unwrap(),
+			self.0.dollar().clone().or_else(|| this.clone()),
+			this,
+			super_obj,
+		)
+	}
+}
 
 struct ContextInternals {
 	dollar: Option<ObjValue>,
@@ -120,9 +127,14 @@
 			}
 		}
 	}
+	pub fn extend_bound(self, new_bindings: FxHashMap<IStr, LazyVal>) -> 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,
-		new_bindings: HashMap<IStr, LazyBinding>,
+		new_bindings: FxHashMap<IStr, LazyBinding>,
 		new_dollar: Option<ObjValue>,
 		new_this: Option<ObjValue>,
 		new_super_obj: Option<ObjValue>,
modifiedcrates/jrsonnet-evaluator/src/dynamic.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/dynamic.rs
+++ b/crates/jrsonnet-evaluator/src/dynamic.rs
@@ -12,7 +12,7 @@
 	}
 }
 impl<T: Clone> FutureWrapper<T> {
-	pub fn unwrap(self) -> T {
+	pub fn unwrap(&self) -> T {
 		self.0.borrow().as_ref().cloned().unwrap()
 	}
 }
@@ -21,30 +21,4 @@
 	fn default() -> Self {
 		Self::new()
 	}
-}
-
-#[macro_export]
-macro_rules! rc_fn_helper {
-	($name: ident, $macro_name: ident, $fn: ty) => {
-		#[derive(Clone)]
-		#[doc = "Function wrapper"]
-		pub struct $name(pub std::rc::Rc<$fn>);
-		impl std::fmt::Debug for $name {
-			fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-				f.debug_struct(std::stringify!($name)).finish()
-			}
-		}
-		impl std::cmp::PartialEq for $name {
-			fn eq(&self, other: &$name) -> bool {
-				std::ptr::eq(&self.0, &other.0)
-			}
-		}
-		#[doc = "Macro to ease wrapper creation"]
-		#[macro_export]
-		macro_rules! $macro_name {
-			($val: expr) => {
-				$crate::$name(std::rc::Rc::new($val))
-			};
-		}
-	};
 }
modifiedcrates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate.rs
@@ -1,6 +1,6 @@
 use crate::{
-	context_creator, error::Error::*, lazy_val, push, throw, with_state, Context, ContextCreator,
-	FuncDesc, FuncVal, FutureWrapper, LazyBinding, LazyVal, ObjMember, ObjValue, Result, Val,
+	error::Error::*, lazy_val, push, throw, with_state, Context, ContextCreator, FuncDesc, FuncVal,
+	FutureWrapper, LazyBinding, LazyVal, ObjMember, ObjValue, Result, Val,
 };
 use closure::closure;
 use jrsonnet_interner::IStr;
@@ -10,8 +10,30 @@
 	Visibility,
 };
 use jrsonnet_types::ValType;
-use rustc_hash::FxHashMap;
-use std::{collections::HashMap, rc::Rc};
+use rustc_hash::{FxHashMap, FxHasher};
+use std::{collections::HashMap, hash::BuildHasherDefault, rc::Rc};
+
+pub fn evaluate_binding_in_future(
+	b: &BindSpec,
+	context_creator: FutureWrapper<Context>,
+) -> LazyVal {
+	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(),
+			))
+		}))
+	} else {
+		LazyVal::new(Box::new(move || {
+			evaluate_named(context_creator.unwrap(), &b.value, b.name.clone())
+		}))
+	}
+}
 
 pub fn evaluate_binding(b: &BindSpec, context_creator: ContextCreator) -> (IStr, LazyBinding) {
 	let b = b.clone();
@@ -22,7 +44,7 @@
 			LazyBinding::Bindable(Rc::new(move |this, super_obj| {
 				Ok(lazy_val!(
 					closure!(clone b, clone params, clone context_creator, || Ok(evaluate_method(
-						context_creator.0(this.clone(), super_obj.clone())?,
+						context_creator.create(this.clone(), super_obj.clone())?,
 						b.name.clone(),
 						params.clone(),
 						b.value.clone(),
@@ -35,11 +57,11 @@
 			b.name.clone(),
 			LazyBinding::Bindable(Rc::new(move |this, super_obj| {
 				Ok(lazy_val!(closure!(clone context_creator, clone b, ||
-						evaluate_named(
-							context_creator.0(this.clone(), super_obj.clone())?,
-							&b.value,
-							b.name.clone()
-						)
+					evaluate_named(
+						context_creator.create(this.clone(), super_obj.clone())?,
+						&b.value,
+						b.name.clone()
+					)
 				)))
 			})),
 		)
@@ -217,18 +239,10 @@
 pub fn evaluate_member_list_object(context: Context, members: &[Member]) -> Result<ObjValue> {
 	let new_bindings = FutureWrapper::new();
 	let future_this = FutureWrapper::new();
-	let context_creator = context_creator!(
-		closure!(clone context, clone new_bindings, |this: Option<ObjValue>, super_obj: Option<ObjValue>| {
-			context.clone().extend_unbound(
-				new_bindings.clone().unwrap(),
-				context.dollar().clone().or_else(||this.clone()),
-				Some(this.unwrap()),
-				super_obj
-			)
-		})
-	);
+	let context_creator = ContextCreator(context.clone(), new_bindings.clone());
 	{
-		let mut bindings: HashMap<IStr, LazyBinding> = HashMap::new();
+		let mut bindings: FxHashMap<IStr, LazyBinding> =
+			FxHashMap::with_capacity_and_hasher(members.len(), BuildHasherDefault::default());
 		for (n, b) in members
 			.iter()
 			.filter_map(|m| match m {
@@ -265,7 +279,7 @@
 						invoke: LazyBinding::Bindable(Rc::new(
 							closure!(clone name, clone value, clone context_creator, |this, super_obj| {
 								Ok(LazyVal::new_resolved(evaluate(
-									context_creator.0(this, super_obj)?,
+									context_creator.create(this, super_obj)?,
 									&value,
 								)?))
 							}),
@@ -294,7 +308,7 @@
 							closure!(clone value, clone context_creator, clone params, clone name, |this, super_obj| {
 								// TODO: Assert
 								Ok(LazyVal::new_resolved(evaluate_method(
-									context_creator.0(this, super_obj)?,
+									context_creator.create(this, super_obj)?,
 									name.clone(),
 									params.clone(),
 									value.clone(),
@@ -324,17 +338,8 @@
 				context.clone(),
 				&|ctx| {
 					let new_bindings = FutureWrapper::new();
-					let context_creator = context_creator!(
-						closure!(clone context, clone new_bindings, |this: Option<ObjValue>, super_obj: Option<ObjValue>| {
-							context.clone().extend_unbound(
-								new_bindings.clone().unwrap(),
-								context.dollar().clone().or_else(||this.clone()),
-								None,
-								super_obj
-							)
-						})
-					);
-					let mut bindings: HashMap<IStr, LazyBinding> = HashMap::new();
+					let context_creator = ContextCreator(context.clone(), new_bindings.clone());
+					let mut bindings: FxHashMap<IStr, LazyBinding> = FxHashMap::with_capacity_and_hasher(obj.pre_locals.len() + obj.post_locals.len(), BuildHasherDefault::default());
 					for (n, b) in obj
 						.pre_locals
 						.iter()
@@ -497,22 +502,19 @@
 			}
 		}
 		LocalExpr(bindings, returned) => {
-			let mut new_bindings: HashMap<IStr, LazyBinding> = HashMap::new();
+			let mut new_bindings: FxHashMap<IStr, LazyVal> = HashMap::with_capacity_and_hasher(
+				bindings.len(),
+				BuildHasherDefault::<FxHasher>::default(),
+			);
 			let future_context = Context::new_future();
-
-			let context_creator = context_creator!(
-				closure!(clone future_context, |_, _| Ok(future_context.clone().unwrap()))
-			);
-
-			for (k, v) in bindings
-				.iter()
-				.map(|b| evaluate_binding(b, context_creator.clone()))
-			{
-				new_bindings.insert(k, v);
+			for b in bindings {
+				new_bindings.insert(
+					b.name.clone(),
+					evaluate_binding_in_future(b, future_context.clone()),
+				);
 			}
-
 			let context = context
-				.extend_unbound(new_bindings, None, None, None)?
+				.extend_bound(new_bindings)
 				.into_future(future_context);
 			evaluate(context, &returned.clone())?
 		}
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -27,10 +27,12 @@
 use jrsonnet_parser::*;
 use native::NativeCallback;
 pub use obj::*;
+use rustc_hash::FxHashMap;
 use std::{
 	cell::{Ref, RefCell, RefMut},
 	collections::HashMap,
 	fmt::Debug,
+	hash::BuildHasherDefault,
 	path::PathBuf,
 	rc::Rc,
 };
@@ -230,7 +232,7 @@
 			}
 			value.parsed.clone()
 		};
-		let value = evaluate(self.create_default_context()?, &expr)?;
+		let value = evaluate(self.create_default_context(), &expr)?;
 		{
 			self.data_mut()
 				.files
@@ -260,16 +262,14 @@
 	}
 
 	/// Creates context with all passed global variables
-	pub fn create_default_context(&self) -> Result<Context> {
+	pub fn create_default_context(&self) -> Context {
 		let globals = &self.settings().globals;
-		let mut new_bindings: HashMap<IStr, LazyBinding> = HashMap::new();
+		let mut new_bindings: FxHashMap<IStr, LazyVal> =
+			FxHashMap::with_capacity_and_hasher(globals.len(), BuildHasherDefault::default());
 		for (name, value) in globals.iter() {
-			new_bindings.insert(
-				name.clone(),
-				LazyBinding::Bound(resolved_lazy_val!(value.clone())),
-			);
+			new_bindings.insert(name.clone(), resolved_lazy_val!(value.clone()));
 		}
-		Context::new().extend_unbound(new_bindings, None, None, None)
+		Context::new().extend_bound(new_bindings)
 	}
 
 	/// Executes code creating a new stack frame
@@ -345,7 +345,7 @@
 					|| "during TLA call".to_owned(),
 					|| {
 						func.evaluate_map(
-							self.create_default_context()?,
+							self.create_default_context(),
 							&self.settings().tla_vars,
 							true,
 						)
@@ -396,7 +396,7 @@
 	}
 	/// Evaluates the parsed expression
 	pub fn evaluate_expr_raw(&self, code: LocExpr) -> Result<Val> {
-		self.run_in_state(|| evaluate(self.create_default_context()?, &code))
+		self.run_in_state(|| evaluate(self.create_default_context(), &code))
 	}
 }
 
@@ -950,6 +950,23 @@
 		Ok(())
 	}
 
+	#[test]
+	fn comp_self() -> crate::error::Result<()> {
+		assert_eval!(
+			r#"
+			std.objectFields({
+				a:{
+					[name]: name for name in std.objectFields(self)
+				},
+				b: 2,
+				c: 3,
+			}.a) == ['a', 'b', 'c']
+			"#
+		);
+
+		Ok(())
+	}
+
 	struct TestImportResolver(IStr);
 	impl crate::import::ImportResolver for TestImportResolver {
 		fn resolve_file(&self, _: &PathBuf, _: &PathBuf) -> crate::error::Result<Rc<PathBuf>> {
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/val.rs
1use crate::{2	builtin::{3		call_builtin,4		manifest::{manifest_json_ex, ManifestJsonOptions, ManifestType},5	},6	error::Error::*,7	evaluate,8	function::{parse_function_call, parse_function_call_map, place_args},9	native::NativeCallback,10	throw, with_state, Context, ObjValue, Result,11};12use jrsonnet_interner::IStr;13use jrsonnet_parser::{el, Arg, ArgsDesc, Expr, ExprLocation, LiteralType, LocExpr, ParamsDesc};14use jrsonnet_types::ValType;15use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};1617enum LazyValInternals {18	Computed(Val),19	Waiting(Box<dyn Fn() -> Result<Val>>),20}21#[derive(Clone)]22pub struct LazyVal(Rc<RefCell<LazyValInternals>>);23impl LazyVal {24	pub fn new(f: Box<dyn Fn() -> Result<Val>>) -> Self {25		Self(Rc::new(RefCell::new(LazyValInternals::Waiting(f))))26	}27	pub fn new_resolved(val: Val) -> Self {28		Self(Rc::new(RefCell::new(LazyValInternals::Computed(val))))29	}30	pub fn evaluate(&self) -> Result<Val> {31		let new_value = match &*self.0.borrow() {32			LazyValInternals::Computed(v) => return Ok(v.clone()),33			LazyValInternals::Waiting(f) => f()?,34		};35		*self.0.borrow_mut() = LazyValInternals::Computed(new_value.clone());36		Ok(new_value)37	}38}3940#[macro_export]41macro_rules! lazy_val {42	($f: expr) => {43		$crate::LazyVal::new(Box::new($f))44	};45}46#[macro_export]47macro_rules! resolved_lazy_val {48	($f: expr) => {49		$crate::LazyVal::new_resolved($f)50	};51}52impl Debug for LazyVal {53	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {54		write!(f, "Lazy")55	}56}57impl PartialEq for LazyVal {58	fn eq(&self, other: &Self) -> bool {59		Rc::ptr_eq(&self.0, &other.0)60	}61}6263#[derive(Debug, PartialEq)]64pub struct FuncDesc {65	pub name: IStr,66	pub ctx: Context,67	pub params: ParamsDesc,68	pub body: LocExpr,69}7071#[derive(Debug)]72pub enum FuncVal {73	/// Plain function implemented in jsonnet74	Normal(FuncDesc),75	/// Standard library function76	Intrinsic(IStr),77	/// Library functions implemented in native78	NativeExt(IStr, Rc<NativeCallback>),79}8081impl PartialEq for FuncVal {82	fn eq(&self, other: &Self) -> bool {83		match (self, other) {84			(Self::Normal(a), Self::Normal(b)) => a == b,85			(Self::Intrinsic(an), Self::Intrinsic(bn)) => an == bn,86			(Self::NativeExt(an, _), Self::NativeExt(bn, _)) => an == bn,87			(..) => false,88		}89	}90}91impl FuncVal {92	pub fn is_ident(&self) -> bool {93		matches!(&self, Self::Intrinsic(n) if n as &str == "id")94	}95	pub fn name(&self) -> IStr {96		match self {97			Self::Normal(normal) => normal.name.clone(),98			Self::Intrinsic(name) => format!("std.{}", name).into(),99			Self::NativeExt(n, _) => format!("native.{}", n).into(),100		}101	}102	pub fn evaluate(103		&self,104		call_ctx: Context,105		loc: Option<&ExprLocation>,106		args: &ArgsDesc,107		tailstrict: bool,108	) -> Result<Val> {109		match self {110			Self::Normal(func) => {111				let ctx = parse_function_call(112					call_ctx,113					Some(func.ctx.clone()),114					&func.params,115					args,116					tailstrict,117				)?;118				evaluate(ctx, &func.body)119			}120			Self::Intrinsic(name) => call_builtin(call_ctx, loc, name, args),121			Self::NativeExt(_name, handler) => {122				let args = parse_function_call(call_ctx, None, &handler.params, args, true)?;123				let mut out_args = Vec::with_capacity(handler.params.len());124				for p in handler.params.0.iter() {125					out_args.push(args.binding(p.0.clone())?.evaluate()?);126				}127				Ok(handler.call(loc.clone().map(|l| l.0.clone()), &out_args)?)128			}129		}130	}131132	pub fn evaluate_map(133		&self,134		call_ctx: Context,135		args: &HashMap<IStr, Val>,136		tailstrict: bool,137	) -> Result<Val> {138		match self {139			Self::Normal(func) => {140				let ctx = parse_function_call_map(141					call_ctx,142					Some(func.ctx.clone()),143					&func.params,144					args,145					tailstrict,146				)?;147				evaluate(ctx, &func.body)148			}149			Self::Intrinsic(_) => todo!(),150			Self::NativeExt(_, _) => todo!(),151		}152	}153154	pub fn evaluate_values(&self, call_ctx: Context, args: &[Val]) -> Result<Val> {155		match self {156			Self::Normal(func) => {157				let ctx = place_args(call_ctx, Some(func.ctx.clone()), &func.params, args)?;158				evaluate(ctx, &func.body)159			}160			Self::Intrinsic(_) => todo!(),161			Self::NativeExt(_, _) => todo!(),162		}163	}164}165166#[derive(Clone)]167pub enum ManifestFormat {168	YamlStream(Box<ManifestFormat>),169	Yaml(usize),170	Json(usize),171	ToString,172	String,173}174175#[derive(Debug, Clone)]176pub enum ArrValue {177	Lazy(Rc<Vec<LazyVal>>),178	Eager(Rc<Vec<Val>>),179	Extended(Box<(Self, Self)>),180}181impl ArrValue {182	pub fn new_eager() -> Self {183		Self::Eager(Rc::new(Vec::new()))184	}185186	pub fn len(&self) -> usize {187		match self {188			Self::Lazy(l) => l.len(),189			Self::Eager(e) => e.len(),190			Self::Extended(v) => v.0.len() + v.1.len(),191		}192	}193194	pub fn is_empty(&self) -> bool {195		self.len() == 0196	}197198	pub fn get(&self, index: usize) -> Result<Option<Val>> {199		match self {200			Self::Lazy(vec) => {201				if let Some(v) = vec.get(index) {202					Ok(Some(v.evaluate()?))203				} else {204					Ok(None)205				}206			}207			Self::Eager(vec) => Ok(vec.get(index).cloned()),208			Self::Extended(v) => {209				let a_len = v.0.len();210				if a_len > index {211					v.0.get(index)212				} else {213					v.1.get(index - a_len)214				}215			}216		}217	}218219	pub fn get_lazy(&self, index: usize) -> Option<LazyVal> {220		match self {221			Self::Lazy(vec) => vec.get(index).cloned(),222			Self::Eager(vec) => vec.get(index).cloned().map(LazyVal::new_resolved),223			Self::Extended(v) => {224				let a_len = v.0.len();225				if a_len > index {226					v.0.get_lazy(index)227				} else {228					v.1.get_lazy(index - a_len)229				}230			}231		}232	}233234	pub fn evaluated(&self) -> Result<Rc<Vec<Val>>> {235		Ok(match self {236			Self::Lazy(vec) => {237				let mut out = Vec::with_capacity(vec.len());238				for item in vec.iter() {239					out.push(item.evaluate()?);240				}241				Rc::new(out)242			}243			Self::Eager(vec) => vec.clone(),244			Self::Extended(_v) => {245				let mut out = Vec::with_capacity(self.len());246				for item in self.iter() {247					out.push(item?);248				}249				Rc::new(out)250			}251		})252	}253254	pub fn iter(&self) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {255		(0..self.len()).map(move |idx| match self {256			Self::Lazy(l) => l[idx].evaluate(),257			Self::Eager(e) => Ok(e[idx].clone()),258			Self::Extended(_) => self.get(idx).map(|e| e.unwrap()),259		})260	}261262	pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = LazyVal> + '_ {263		(0..self.len()).map(move |idx| match self {264			Self::Lazy(l) => l[idx].clone(),265			Self::Eager(e) => LazyVal::new_resolved(e[idx].clone()),266			Self::Extended(_) => self.get_lazy(idx).unwrap(),267		})268	}269270	pub fn reversed(self) -> Self {271		match self {272			Self::Lazy(vec) => {273				let mut out = (&vec as &Vec<_>).clone();274				out.reverse();275				Self::Lazy(Rc::new(out))276			}277			Self::Eager(vec) => {278				let mut out = (&vec as &Vec<_>).clone();279				out.reverse();280				Self::Eager(Rc::new(out))281			}282			Self::Extended(b) => Self::Extended(Box::new((b.1.reversed(), b.0.reversed()))),283		}284	}285286	pub fn map(self, mapper: impl Fn(Val) -> Result<Val>) -> Result<Self> {287		let mut out = Vec::with_capacity(self.len());288289		for value in self.iter() {290			out.push(mapper(value?)?);291		}292293		Ok(Self::Eager(Rc::new(out)))294	}295296	pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {297		let mut out = Vec::with_capacity(self.len());298299		for value in self.iter() {300			let value = value?;301			if filter(&value)? {302				out.push(value);303			}304		}305306		Ok(Self::Eager(Rc::new(out)))307	}308309	pub fn ptr_eq(a: &Self, b: &Self) -> bool {310		match (a, b) {311			(Self::Lazy(a), Self::Lazy(b)) => Rc::ptr_eq(a, b),312			(Self::Eager(a), Self::Eager(b)) => Rc::ptr_eq(a, b),313			_ => false,314		}315	}316}317318impl From<Vec<LazyVal>> for ArrValue {319	fn from(v: Vec<LazyVal>) -> Self {320		Self::Lazy(Rc::new(v))321	}322}323324impl From<Vec<Val>> for ArrValue {325	fn from(v: Vec<Val>) -> Self {326		Self::Eager(Rc::new(v))327	}328}329330#[derive(Debug, Clone)]331pub enum Val {332	Bool(bool),333	Null,334	Str(IStr),335	Num(f64),336	Arr(ArrValue),337	Obj(ObjValue),338	Func(Rc<FuncVal>),339}340341macro_rules! matches_unwrap {342	($e: expr, $p: pat, $r: expr) => {343		match $e {344			$p => $r,345			_ => panic!("no match"),346		}347	};348}349impl Val {350	/// Creates `Val::Num` after checking for numeric overflow.351	/// As numbers are `f64`, we can just check for their finity.352	pub fn new_checked_num(num: f64) -> Result<Self> {353		if num.is_finite() {354			Ok(Self::Num(num))355		} else {356			throw!(RuntimeError("overflow".into()))357		}358	}359360	pub fn assert_type(&self, context: &'static str, val_type: ValType) -> Result<()> {361		let this_type = self.value_type();362		if this_type != val_type {363			throw!(TypeMismatch(context, vec![val_type], this_type))364		} else {365			Ok(())366		}367	}368	pub fn unwrap_num(self) -> Result<f64> {369		Ok(matches_unwrap!(self, Self::Num(v), v))370	}371	pub fn unwrap_func(self) -> Result<Rc<FuncVal>> {372		Ok(matches_unwrap!(self, Self::Func(v), v))373	}374	pub fn try_cast_bool(self, context: &'static str) -> Result<bool> {375		self.assert_type(context, ValType::Bool)?;376		Ok(matches_unwrap!(self, Self::Bool(v), v))377	}378	pub fn try_cast_str(self, context: &'static str) -> Result<IStr> {379		self.assert_type(context, ValType::Str)?;380		Ok(matches_unwrap!(self, Self::Str(v), v))381	}382	pub fn try_cast_num(self, context: &'static str) -> Result<f64> {383		self.assert_type(context, ValType::Num)?;384		self.unwrap_num()385	}386	pub const fn value_type(&self) -> ValType {387		match self {388			Self::Str(..) => ValType::Str,389			Self::Num(..) => ValType::Num,390			Self::Arr(..) => ValType::Arr,391			Self::Obj(..) => ValType::Obj,392			Self::Bool(_) => ValType::Bool,393			Self::Null => ValType::Null,394			Self::Func(..) => ValType::Func,395		}396	}397398	pub fn to_string(&self) -> Result<IStr> {399		Ok(match self {400			Self::Bool(true) => "true".into(),401			Self::Bool(false) => "false".into(),402			Self::Null => "null".into(),403			Self::Str(s) => s.clone(),404			v => manifest_json_ex(405				v,406				&ManifestJsonOptions {407					padding: "",408					mtype: ManifestType::ToString,409				},410			)?411			.into(),412		})413	}414415	/// Expects value to be object, outputs (key, manifested value) pairs416	pub fn manifest_multi(&self, ty: &ManifestFormat) -> Result<Vec<(IStr, IStr)>> {417		let obj = match self {418			Self::Obj(obj) => obj,419			_ => throw!(MultiManifestOutputIsNotAObject),420		};421		let keys = obj.fields();422		let mut out = Vec::with_capacity(keys.len());423		for key in keys {424			let value = obj425				.get(key.clone())?426				.expect("item in object")427				.manifest(ty)?;428			out.push((key, value));429		}430		Ok(out)431	}432433	/// Expects value to be array, outputs manifested values434	pub fn manifest_stream(&self, ty: &ManifestFormat) -> Result<Vec<IStr>> {435		let arr = match self {436			Self::Arr(a) => a,437			_ => throw!(StreamManifestOutputIsNotAArray),438		};439		let mut out = Vec::with_capacity(arr.len());440		for i in arr.iter() {441			out.push(i?.manifest(ty)?);442		}443		Ok(out)444	}445446	pub fn manifest(&self, ty: &ManifestFormat) -> Result<IStr> {447		Ok(match ty {448			ManifestFormat::YamlStream(format) => {449				let arr = match self {450					Self::Arr(a) => a,451					_ => throw!(StreamManifestOutputIsNotAArray),452				};453				let mut out = String::new();454455				match format as &ManifestFormat {456					ManifestFormat::YamlStream(_) => throw!(StreamManifestOutputCannotBeRecursed),457					ManifestFormat::String => throw!(StreamManifestCannotNestString),458					_ => {}459				};460461				if !arr.is_empty() {462					for v in arr.iter() {463						out.push_str("---\n");464						out.push_str(&v?.manifest(format)?);465						out.push('\n');466					}467					out.push_str("...");468				}469470				out.into()471			}472			ManifestFormat::Yaml(padding) => self.to_yaml(*padding)?,473			ManifestFormat::Json(padding) => self.to_json(*padding)?,474			ManifestFormat::ToString => self.to_string()?,475			ManifestFormat::String => match self {476				Self::Str(s) => s.clone(),477				_ => throw!(StringManifestOutputIsNotAString),478			},479		})480	}481482	/// For manifestification483	pub fn to_json(&self, padding: usize) -> Result<IStr> {484		manifest_json_ex(485			self,486			&ManifestJsonOptions {487				padding: &" ".repeat(padding),488				mtype: if padding == 0 {489					ManifestType::Minify490				} else {491					ManifestType::Manifest492				},493			},494		)495		.map(|s| s.into())496	}497498	/// Calls `std.manifestJson`499	#[cfg(feature = "faster")]500	pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {501		manifest_json_ex(502			self,503			&ManifestJsonOptions {504				padding: &" ".repeat(padding),505				mtype: ManifestType::Std,506			},507		)508		.map(|s| s.into())509	}510511	/// Calls `std.manifestJson`512	#[cfg(not(feature = "faster"))]513	pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {514		with_state(|s| {515			let ctx = s516				.create_default_context()?517				.with_var("__tmp__to_json__".into(), self.clone())?;518			Ok(evaluate(519				ctx,520				&el!(Expr::Apply(521					el!(Expr::Index(522						el!(Expr::Var("std".into())),523						el!(Expr::Str("manifestJsonEx".into()))524					)),525					ArgsDesc(vec![526						Arg(None, el!(Expr::Var("__tmp__to_json__".into()))),527						Arg(None, el!(Expr::Str(" ".repeat(padding).into())))528					]),529					false530				)),531			)?532			.try_cast_str("to json")?)533		})534	}535	pub fn to_yaml(&self, padding: usize) -> Result<IStr> {536		with_state(|s| {537			let ctx = s538				.create_default_context()?539				.with_var("__tmp__to_json__".into(), self.clone());540			evaluate(541				ctx,542				&el!(Expr::Apply(543					el!(Expr::Index(544						el!(Expr::Var("std".into())),545						el!(Expr::Str("manifestYamlDoc".into()))546					)),547					ArgsDesc(vec![548						Arg(None, el!(Expr::Var("__tmp__to_json__".into()))),549						Arg(550							None,551							el!(Expr::Literal(if padding != 0 {552								LiteralType::True553							} else {554								LiteralType::False555							}))556						)557					]),558					false559				)),560			)?561			.try_cast_str("to json")562		})563	}564}565566const fn is_function_like(val: &Val) -> bool {567	matches!(val, Val::Func(_))568}569570/// Native implementation of `std.primitiveEquals`571pub fn primitive_equals(val_a: &Val, val_b: &Val) -> Result<bool> {572	Ok(match (val_a, val_b) {573		(Val::Bool(a), Val::Bool(b)) => a == b,574		(Val::Null, Val::Null) => true,575		(Val::Str(a), Val::Str(b)) => a == b,576		(Val::Num(a), Val::Num(b)) => (a - b).abs() <= f64::EPSILON,577		(Val::Arr(_), Val::Arr(_)) => throw!(RuntimeError(578			"primitiveEquals operates on primitive types, got array".into(),579		)),580		(Val::Obj(_), Val::Obj(_)) => throw!(RuntimeError(581			"primitiveEquals operates on primitive types, got object".into(),582		)),583		(a, b) if is_function_like(a) && is_function_like(b) => {584			throw!(RuntimeError("cannot test equality of functions".into()))585		}586		(_, _) => false,587	})588}589590/// Native implementation of `std.equals`591pub fn equals(val_a: &Val, val_b: &Val) -> Result<bool> {592	if val_a.value_type() != val_b.value_type() {593		return Ok(false);594	}595	match (val_a, val_b) {596		(Val::Arr(a), Val::Arr(b)) => {597			if ArrValue::ptr_eq(a, b) {598				return Ok(true);599			}600			if a.len() != b.len() {601				return Ok(false);602			}603			for (a, b) in a.iter().zip(b.iter()) {604				if !equals(&a?, &b?)? {605					return Ok(false);606				}607			}608			Ok(true)609		}610		(Val::Obj(a), Val::Obj(b)) => {611			if ObjValue::ptr_eq(a, b) {612				return Ok(true);613			}614			let fields = a.fields();615			if fields != b.fields() {616				return Ok(false);617			}618			for field in fields {619				if !equals(&a.get(field.clone())?.unwrap(), &b.get(field)?.unwrap())? {620					return Ok(false);621				}622			}623			Ok(true)624		}625		(a, b) => Ok(primitive_equals(a, b)?),626	}627}
after · crates/jrsonnet-evaluator/src/val.rs
1use crate::{2	builtin::{3		call_builtin,4		manifest::{manifest_json_ex, ManifestJsonOptions, ManifestType},5	},6	error::Error::*,7	evaluate,8	function::{parse_function_call, parse_function_call_map, place_args},9	native::NativeCallback,10	throw, with_state, Context, ObjValue, Result,11};12use jrsonnet_interner::IStr;13use jrsonnet_parser::{el, Arg, ArgsDesc, Expr, ExprLocation, LiteralType, LocExpr, ParamsDesc};14use jrsonnet_types::ValType;15use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};1617enum LazyValInternals {18	Computed(Val),19	Waiting(Box<dyn Fn() -> Result<Val>>),20}21#[derive(Clone)]22pub struct LazyVal(Rc<RefCell<LazyValInternals>>);23impl LazyVal {24	pub fn new(f: Box<dyn Fn() -> Result<Val>>) -> Self {25		Self(Rc::new(RefCell::new(LazyValInternals::Waiting(f))))26	}27	pub fn new_resolved(val: Val) -> Self {28		Self(Rc::new(RefCell::new(LazyValInternals::Computed(val))))29	}30	pub fn evaluate(&self) -> Result<Val> {31		let new_value = match &*self.0.borrow() {32			LazyValInternals::Computed(v) => return Ok(v.clone()),33			LazyValInternals::Waiting(f) => f()?,34		};35		*self.0.borrow_mut() = LazyValInternals::Computed(new_value.clone());36		Ok(new_value)37	}38}3940#[macro_export]41macro_rules! lazy_val {42	($f: expr) => {43		$crate::LazyVal::new(Box::new($f))44	};45}46#[macro_export]47macro_rules! resolved_lazy_val {48	($f: expr) => {49		$crate::LazyVal::new_resolved($f)50	};51}52impl Debug for LazyVal {53	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {54		write!(f, "Lazy")55	}56}57impl PartialEq for LazyVal {58	fn eq(&self, other: &Self) -> bool {59		Rc::ptr_eq(&self.0, &other.0)60	}61}6263#[derive(Debug, PartialEq)]64pub struct FuncDesc {65	pub name: IStr,66	pub ctx: Context,67	pub params: ParamsDesc,68	pub body: LocExpr,69}7071#[derive(Debug)]72pub enum FuncVal {73	/// Plain function implemented in jsonnet74	Normal(FuncDesc),75	/// Standard library function76	Intrinsic(IStr),77	/// Library functions implemented in native78	NativeExt(IStr, Rc<NativeCallback>),79}8081impl PartialEq for FuncVal {82	fn eq(&self, other: &Self) -> bool {83		match (self, other) {84			(Self::Normal(a), Self::Normal(b)) => a == b,85			(Self::Intrinsic(an), Self::Intrinsic(bn)) => an == bn,86			(Self::NativeExt(an, _), Self::NativeExt(bn, _)) => an == bn,87			(..) => false,88		}89	}90}91impl FuncVal {92	pub fn is_ident(&self) -> bool {93		matches!(&self, Self::Intrinsic(n) if n as &str == "id")94	}95	pub fn name(&self) -> IStr {96		match self {97			Self::Normal(normal) => normal.name.clone(),98			Self::Intrinsic(name) => format!("std.{}", name).into(),99			Self::NativeExt(n, _) => format!("native.{}", n).into(),100		}101	}102	pub fn evaluate(103		&self,104		call_ctx: Context,105		loc: Option<&ExprLocation>,106		args: &ArgsDesc,107		tailstrict: bool,108	) -> Result<Val> {109		match self {110			Self::Normal(func) => {111				let ctx = parse_function_call(112					call_ctx,113					Some(func.ctx.clone()),114					&func.params,115					args,116					tailstrict,117				)?;118				evaluate(ctx, &func.body)119			}120			Self::Intrinsic(name) => call_builtin(call_ctx, loc, name, args),121			Self::NativeExt(_name, handler) => {122				let args = parse_function_call(call_ctx, None, &handler.params, args, true)?;123				let mut out_args = Vec::with_capacity(handler.params.len());124				for p in handler.params.0.iter() {125					out_args.push(args.binding(p.0.clone())?.evaluate()?);126				}127				Ok(handler.call(loc.clone().map(|l| l.0.clone()), &out_args)?)128			}129		}130	}131132	pub fn evaluate_map(133		&self,134		call_ctx: Context,135		args: &HashMap<IStr, Val>,136		tailstrict: bool,137	) -> Result<Val> {138		match self {139			Self::Normal(func) => {140				let ctx = parse_function_call_map(141					call_ctx,142					Some(func.ctx.clone()),143					&func.params,144					args,145					tailstrict,146				)?;147				evaluate(ctx, &func.body)148			}149			Self::Intrinsic(_) => todo!(),150			Self::NativeExt(_, _) => todo!(),151		}152	}153154	pub fn evaluate_values(&self, call_ctx: Context, args: &[Val]) -> Result<Val> {155		match self {156			Self::Normal(func) => {157				let ctx = place_args(call_ctx, Some(func.ctx.clone()), &func.params, args)?;158				evaluate(ctx, &func.body)159			}160			Self::Intrinsic(_) => todo!(),161			Self::NativeExt(_, _) => todo!(),162		}163	}164}165166#[derive(Clone)]167pub enum ManifestFormat {168	YamlStream(Box<ManifestFormat>),169	Yaml(usize),170	Json(usize),171	ToString,172	String,173}174175#[derive(Debug, Clone)]176pub enum ArrValue {177	Lazy(Rc<Vec<LazyVal>>),178	Eager(Rc<Vec<Val>>),179	Extended(Box<(Self, Self)>),180}181impl ArrValue {182	pub fn new_eager() -> Self {183		Self::Eager(Rc::new(Vec::new()))184	}185186	pub fn len(&self) -> usize {187		match self {188			Self::Lazy(l) => l.len(),189			Self::Eager(e) => e.len(),190			Self::Extended(v) => v.0.len() + v.1.len(),191		}192	}193194	pub fn is_empty(&self) -> bool {195		self.len() == 0196	}197198	pub fn get(&self, index: usize) -> Result<Option<Val>> {199		match self {200			Self::Lazy(vec) => {201				if let Some(v) = vec.get(index) {202					Ok(Some(v.evaluate()?))203				} else {204					Ok(None)205				}206			}207			Self::Eager(vec) => Ok(vec.get(index).cloned()),208			Self::Extended(v) => {209				let a_len = v.0.len();210				if a_len > index {211					v.0.get(index)212				} else {213					v.1.get(index - a_len)214				}215			}216		}217	}218219	pub fn get_lazy(&self, index: usize) -> Option<LazyVal> {220		match self {221			Self::Lazy(vec) => vec.get(index).cloned(),222			Self::Eager(vec) => vec.get(index).cloned().map(LazyVal::new_resolved),223			Self::Extended(v) => {224				let a_len = v.0.len();225				if a_len > index {226					v.0.get_lazy(index)227				} else {228					v.1.get_lazy(index - a_len)229				}230			}231		}232	}233234	pub fn evaluated(&self) -> Result<Rc<Vec<Val>>> {235		Ok(match self {236			Self::Lazy(vec) => {237				let mut out = Vec::with_capacity(vec.len());238				for item in vec.iter() {239					out.push(item.evaluate()?);240				}241				Rc::new(out)242			}243			Self::Eager(vec) => vec.clone(),244			Self::Extended(_v) => {245				let mut out = Vec::with_capacity(self.len());246				for item in self.iter() {247					out.push(item?);248				}249				Rc::new(out)250			}251		})252	}253254	pub fn iter(&self) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {255		(0..self.len()).map(move |idx| match self {256			Self::Lazy(l) => l[idx].evaluate(),257			Self::Eager(e) => Ok(e[idx].clone()),258			Self::Extended(_) => self.get(idx).map(|e| e.unwrap()),259		})260	}261262	pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = LazyVal> + '_ {263		(0..self.len()).map(move |idx| match self {264			Self::Lazy(l) => l[idx].clone(),265			Self::Eager(e) => LazyVal::new_resolved(e[idx].clone()),266			Self::Extended(_) => self.get_lazy(idx).unwrap(),267		})268	}269270	pub fn reversed(self) -> Self {271		match self {272			Self::Lazy(vec) => {273				let mut out = (&vec as &Vec<_>).clone();274				out.reverse();275				Self::Lazy(Rc::new(out))276			}277			Self::Eager(vec) => {278				let mut out = (&vec as &Vec<_>).clone();279				out.reverse();280				Self::Eager(Rc::new(out))281			}282			Self::Extended(b) => Self::Extended(Box::new((b.1.reversed(), b.0.reversed()))),283		}284	}285286	pub fn map(self, mapper: impl Fn(Val) -> Result<Val>) -> Result<Self> {287		let mut out = Vec::with_capacity(self.len());288289		for value in self.iter() {290			out.push(mapper(value?)?);291		}292293		Ok(Self::Eager(Rc::new(out)))294	}295296	pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {297		let mut out = Vec::with_capacity(self.len());298299		for value in self.iter() {300			let value = value?;301			if filter(&value)? {302				out.push(value);303			}304		}305306		Ok(Self::Eager(Rc::new(out)))307	}308309	pub fn ptr_eq(a: &Self, b: &Self) -> bool {310		match (a, b) {311			(Self::Lazy(a), Self::Lazy(b)) => Rc::ptr_eq(a, b),312			(Self::Eager(a), Self::Eager(b)) => Rc::ptr_eq(a, b),313			_ => false,314		}315	}316}317318impl From<Vec<LazyVal>> for ArrValue {319	fn from(v: Vec<LazyVal>) -> Self {320		Self::Lazy(Rc::new(v))321	}322}323324impl From<Vec<Val>> for ArrValue {325	fn from(v: Vec<Val>) -> Self {326		Self::Eager(Rc::new(v))327	}328}329330#[derive(Debug, Clone)]331pub enum Val {332	Bool(bool),333	Null,334	Str(IStr),335	Num(f64),336	Arr(ArrValue),337	Obj(ObjValue),338	Func(Rc<FuncVal>),339}340341macro_rules! matches_unwrap {342	($e: expr, $p: pat, $r: expr) => {343		match $e {344			$p => $r,345			_ => panic!("no match"),346		}347	};348}349impl Val {350	/// Creates `Val::Num` after checking for numeric overflow.351	/// As numbers are `f64`, we can just check for their finity.352	pub fn new_checked_num(num: f64) -> Result<Self> {353		if num.is_finite() {354			Ok(Self::Num(num))355		} else {356			throw!(RuntimeError("overflow".into()))357		}358	}359360	pub fn assert_type(&self, context: &'static str, val_type: ValType) -> Result<()> {361		let this_type = self.value_type();362		if this_type != val_type {363			throw!(TypeMismatch(context, vec![val_type], this_type))364		} else {365			Ok(())366		}367	}368	pub fn unwrap_num(self) -> Result<f64> {369		Ok(matches_unwrap!(self, Self::Num(v), v))370	}371	pub fn unwrap_func(self) -> Result<Rc<FuncVal>> {372		Ok(matches_unwrap!(self, Self::Func(v), v))373	}374	pub fn try_cast_bool(self, context: &'static str) -> Result<bool> {375		self.assert_type(context, ValType::Bool)?;376		Ok(matches_unwrap!(self, Self::Bool(v), v))377	}378	pub fn try_cast_str(self, context: &'static str) -> Result<IStr> {379		self.assert_type(context, ValType::Str)?;380		Ok(matches_unwrap!(self, Self::Str(v), v))381	}382	pub fn try_cast_num(self, context: &'static str) -> Result<f64> {383		self.assert_type(context, ValType::Num)?;384		self.unwrap_num()385	}386	pub const fn value_type(&self) -> ValType {387		match self {388			Self::Str(..) => ValType::Str,389			Self::Num(..) => ValType::Num,390			Self::Arr(..) => ValType::Arr,391			Self::Obj(..) => ValType::Obj,392			Self::Bool(_) => ValType::Bool,393			Self::Null => ValType::Null,394			Self::Func(..) => ValType::Func,395		}396	}397398	pub fn to_string(&self) -> Result<IStr> {399		Ok(match self {400			Self::Bool(true) => "true".into(),401			Self::Bool(false) => "false".into(),402			Self::Null => "null".into(),403			Self::Str(s) => s.clone(),404			v => manifest_json_ex(405				v,406				&ManifestJsonOptions {407					padding: "",408					mtype: ManifestType::ToString,409				},410			)?411			.into(),412		})413	}414415	/// Expects value to be object, outputs (key, manifested value) pairs416	pub fn manifest_multi(&self, ty: &ManifestFormat) -> Result<Vec<(IStr, IStr)>> {417		let obj = match self {418			Self::Obj(obj) => obj,419			_ => throw!(MultiManifestOutputIsNotAObject),420		};421		let keys = obj.fields();422		let mut out = Vec::with_capacity(keys.len());423		for key in keys {424			let value = obj425				.get(key.clone())?426				.expect("item in object")427				.manifest(ty)?;428			out.push((key, value));429		}430		Ok(out)431	}432433	/// Expects value to be array, outputs manifested values434	pub fn manifest_stream(&self, ty: &ManifestFormat) -> Result<Vec<IStr>> {435		let arr = match self {436			Self::Arr(a) => a,437			_ => throw!(StreamManifestOutputIsNotAArray),438		};439		let mut out = Vec::with_capacity(arr.len());440		for i in arr.iter() {441			out.push(i?.manifest(ty)?);442		}443		Ok(out)444	}445446	pub fn manifest(&self, ty: &ManifestFormat) -> Result<IStr> {447		Ok(match ty {448			ManifestFormat::YamlStream(format) => {449				let arr = match self {450					Self::Arr(a) => a,451					_ => throw!(StreamManifestOutputIsNotAArray),452				};453				let mut out = String::new();454455				match format as &ManifestFormat {456					ManifestFormat::YamlStream(_) => throw!(StreamManifestOutputCannotBeRecursed),457					ManifestFormat::String => throw!(StreamManifestCannotNestString),458					_ => {}459				};460461				if !arr.is_empty() {462					for v in arr.iter() {463						out.push_str("---\n");464						out.push_str(&v?.manifest(format)?);465						out.push('\n');466					}467					out.push_str("...");468				}469470				out.into()471			}472			ManifestFormat::Yaml(padding) => self.to_yaml(*padding)?,473			ManifestFormat::Json(padding) => self.to_json(*padding)?,474			ManifestFormat::ToString => self.to_string()?,475			ManifestFormat::String => match self {476				Self::Str(s) => s.clone(),477				_ => throw!(StringManifestOutputIsNotAString),478			},479		})480	}481482	/// For manifestification483	pub fn to_json(&self, padding: usize) -> Result<IStr> {484		manifest_json_ex(485			self,486			&ManifestJsonOptions {487				padding: &" ".repeat(padding),488				mtype: if padding == 0 {489					ManifestType::Minify490				} else {491					ManifestType::Manifest492				},493			},494		)495		.map(|s| s.into())496	}497498	/// Calls `std.manifestJson`499	#[cfg(feature = "faster")]500	pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {501		manifest_json_ex(502			self,503			&ManifestJsonOptions {504				padding: &" ".repeat(padding),505				mtype: ManifestType::Std,506			},507		)508		.map(|s| s.into())509	}510511	/// Calls `std.manifestJson`512	#[cfg(not(feature = "faster"))]513	pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {514		with_state(|s| {515			let ctx = s516				.create_default_context()?517				.with_var("__tmp__to_json__".into(), self.clone())?;518			Ok(evaluate(519				ctx,520				&el!(Expr::Apply(521					el!(Expr::Index(522						el!(Expr::Var("std".into())),523						el!(Expr::Str("manifestJsonEx".into()))524					)),525					ArgsDesc(vec![526						Arg(None, el!(Expr::Var("__tmp__to_json__".into()))),527						Arg(None, el!(Expr::Str(" ".repeat(padding).into())))528					]),529					false530				)),531			)?532			.try_cast_str("to json")?)533		})534	}535	pub fn to_yaml(&self, padding: usize) -> Result<IStr> {536		with_state(|s| {537			let ctx = s538				.create_default_context()539				.with_var("__tmp__to_json__".into(), self.clone());540			evaluate(541				ctx,542				&el!(Expr::Apply(543					el!(Expr::Index(544						el!(Expr::Var("std".into())),545						el!(Expr::Str("manifestYamlDoc".into()))546					)),547					ArgsDesc(vec![548						Arg(None, el!(Expr::Var("__tmp__to_json__".into()))),549						Arg(550							None,551							el!(Expr::Literal(if padding != 0 {552								LiteralType::True553							} else {554								LiteralType::False555							}))556						)557					]),558					false559				)),560			)?561			.try_cast_str("to json")562		})563	}564}565566const fn is_function_like(val: &Val) -> bool {567	matches!(val, Val::Func(_))568}569570/// Native implementation of `std.primitiveEquals`571pub fn primitive_equals(val_a: &Val, val_b: &Val) -> Result<bool> {572	Ok(match (val_a, val_b) {573		(Val::Bool(a), Val::Bool(b)) => a == b,574		(Val::Null, Val::Null) => true,575		(Val::Str(a), Val::Str(b)) => a == b,576		(Val::Num(a), Val::Num(b)) => (a - b).abs() <= f64::EPSILON,577		(Val::Arr(_), Val::Arr(_)) => throw!(RuntimeError(578			"primitiveEquals operates on primitive types, got array".into(),579		)),580		(Val::Obj(_), Val::Obj(_)) => throw!(RuntimeError(581			"primitiveEquals operates on primitive types, got object".into(),582		)),583		(a, b) if is_function_like(a) && is_function_like(b) => {584			throw!(RuntimeError("cannot test equality of functions".into()))585		}586		(_, _) => false,587	})588}589590/// Native implementation of `std.equals`591pub fn equals(val_a: &Val, val_b: &Val) -> Result<bool> {592	if val_a.value_type() != val_b.value_type() {593		return Ok(false);594	}595	match (val_a, val_b) {596		(Val::Arr(a), Val::Arr(b)) => {597			if ArrValue::ptr_eq(a, b) {598				return Ok(true);599			}600			if a.len() != b.len() {601				return Ok(false);602			}603			for (a, b) in a.iter().zip(b.iter()) {604				if !equals(&a?, &b?)? {605					return Ok(false);606				}607			}608			Ok(true)609		}610		(Val::Obj(a), Val::Obj(b)) => {611			if ObjValue::ptr_eq(a, b) {612				return Ok(true);613			}614			let fields = a.fields();615			if fields != b.fields() {616				return Ok(false);617			}618			for field in fields {619				if !equals(&a.get(field.clone())?.unwrap(), &b.get(field)?.unwrap())? {620					return Ok(false);621				}622			}623			Ok(true)624		}625		(a, b) => Ok(primitive_equals(a, b)?),626	}627}