git.delta.rocks / jrsonnet / refs/commits / 74199ce77317

difftreelog

Merge remote-tracking branch 'origin/feature/importbin' into gcmodule

Yaroslav Bolyukin2022-04-20parents: #4f4be44 #be790e9.patch.diff
in: master

11 files changed

modifiedbindings/jsonnet/src/import.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/import.rs
+++ b/bindings/jsonnet/src/import.rs
@@ -2,7 +2,7 @@
 
 use jrsonnet_evaluator::{
 	error::{Error::*, Result},
-	throw, EvaluationState, IStr, ImportResolver,
+	throw, EvaluationState, ImportResolver,
 };
 use std::{
 	any::Any,
@@ -29,8 +29,7 @@
 pub struct CallbackImportResolver {
 	cb: JsonnetImportCallback,
 	ctx: *mut c_void,
-
-	out: RefCell<HashMap<PathBuf, IStr>>,
+	out: RefCell<HashMap<PathBuf, Vec<u8>>>,
 }
 impl ImportResolver for CallbackImportResolver {
 	fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
@@ -75,9 +74,10 @@
 
 		Ok(found_here_buf.into())
 	}
-	fn load_file_contents(&self, resolved: &Path) -> Result<IStr> {
+	fn load_file_contents(&self, resolved: &Path) -> Result<Vec<u8>> {
 		Ok(self.out.borrow().get(resolved).unwrap().clone())
 	}
+
 	unsafe fn as_any(&self) -> &dyn Any {
 		self
 	}
@@ -124,12 +124,12 @@
 			throw!(ImportFileNotFound(from.to_owned(), path.to_owned()))
 		}
 	}
-	fn load_file_contents(&self, id: &Path) -> Result<IStr> {
+	fn load_file_contents(&self, id: &Path) -> Result<Vec<u8>> {
 		let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.to_owned()))?;
-		let mut out = String::new();
-		file.read_to_string(&mut out)
-			.map_err(|_e| ImportBadFileUtf8(id.to_owned()))?;
-		Ok(out.into())
+		let mut out = Vec::new();
+		file.read_to_end(&mut out)
+			.map_err(|e| ImportIo(e.to_string()))?;
+		Ok(out)
 	}
 	unsafe fn as_any(&self) -> &dyn Any {
 		self
modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -1,5 +1,5 @@
 use crate::function::{CallLocation, StaticBuiltin};
-use crate::typed::{Any, PositiveF64, VecVal, M1};
+use crate::typed::{Any, Bytes, PositiveF64, VecVal, M1};
 use crate::{
 	builtin::manifest::{manifest_yaml_ex, ManifestYamlOptions},
 	equals,
@@ -447,17 +447,15 @@
 }
 
 #[jrsonnet_macros::builtin]
-fn builtin_encode_utf8(str: IStr) -> Result<VecVal> {
-	Ok(VecVal(
-		str.bytes()
-			.map(|b| Val::Num(b as f64))
-			.collect::<Vec<Val>>(),
-	))
+fn builtin_encode_utf8(str: IStr) -> Result<Bytes> {
+	Ok(Bytes(str.bytes().map(|b| b).collect::<Vec<u8>>().into()))
 }
 
 #[jrsonnet_macros::builtin]
-fn builtin_decode_utf8(arr: Vec<u8>) -> Result<String> {
-	Ok(String::from_utf8(arr).map_err(|_| RuntimeError("bad utf8".into()))?)
+fn builtin_decode_utf8(arr: Bytes) -> Result<IStr> {
+	Ok(std::str::from_utf8(&arr.0)
+		.map_err(|_| RuntimeError("bad utf8".into()))?
+		.into())
 }
 
 #[jrsonnet_macros::builtin]
@@ -483,17 +481,21 @@
 }
 
 #[jrsonnet_macros::builtin]
-fn builtin_base64(input: Either![Vec<u8>, IStr]) -> Result<String> {
+fn builtin_base64(input: Either![Bytes, IStr]) -> Result<String> {
 	use Either2::*;
 	Ok(match input {
-		A(a) => base64::encode(a),
+		A(a) => base64::encode(a.0),
 		B(l) => base64::encode(l.bytes().collect::<Vec<_>>()),
 	})
 }
 
 #[jrsonnet_macros::builtin]
-fn builtin_base64_decode_bytes(input: IStr) -> Result<Vec<u8>> {
-	Ok(base64::decode(&input.as_bytes()).map_err(|_| RuntimeError("bad base64".into()))?)
+fn builtin_base64_decode_bytes(input: IStr) -> Result<Bytes> {
+	Ok(Bytes(
+		base64::decode(&input.as_bytes())
+			.map_err(|_| RuntimeError("bad base64".into()))?
+			.into(),
+	))
 }
 
 #[jrsonnet_macros::builtin]
modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -82,6 +82,8 @@
 	ResolvedFileNotFound(PathBuf),
 	#[error("imported file is not valid utf-8: {0:?}")]
 	ImportBadFileUtf8(PathBuf),
+	#[error("import io error: {0}")]
+	ImportIo(String),
 	#[error("tried to import {1} from {0}, but imports is not supported")]
 	ImportNotSupported(PathBuf, PathBuf),
 	#[error(
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -701,5 +701,12 @@
 			import_location.pop();
 			Val::Str(with_state(|s| s.import_file_str(&import_location, path))?)
 		}
+		ImportBin(path) => {
+			let tmp = loc.clone().0;
+			let mut import_location = tmp.to_path_buf();
+			import_location.pop();
+			let bytes = with_state(|s| s.import_file_bin(&import_location, path))?;
+			Val::Arr(ArrValue::Bytes(bytes))
+		}
 	})
 }
modifiedcrates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -5,14 +5,12 @@
 use fs::File;
 use jrsonnet_interner::IStr;
 use std::fs;
-use std::io::Read;
 use std::{
 	any::Any,
-	cell::RefCell,
-	collections::HashMap,
 	path::{Path, PathBuf},
 	rc::Rc,
 };
+use std::{convert::TryFrom, io::Read};
 
 /// Implements file resolution logic for `import` and `importStr`
 pub trait ImportResolver {
@@ -21,9 +19,19 @@
 	/// where `${vendor}` is a library path.
 	fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>>;
 
+	fn load_file_contents(&self, resolved: &Path) -> Result<Vec<u8>>;
+
 	/// Reads file from filesystem, should be used only with path received from `resolve_file`
-	fn load_file_contents(&self, resolved: &Path) -> Result<IStr>;
+	fn load_file_str(&self, resolved: &Path) -> Result<IStr> {
+		Ok(IStr::try_from(&self.load_file_contents(resolved)? as &[u8])
+			.map_err(|_| ImportBadFileUtf8(resolved.to_path_buf()))?)
+	}
 
+	/// Reads file from filesystem, should be used only with path received from `resolve_file`
+	fn load_file_bin(&self, resolved: &Path) -> Result<Rc<[u8]>> {
+		Ok(self.load_file_contents(resolved)?.into())
+	}
+
 	/// # Safety
 	///
 	/// For use only in bindings, should not be used elsewhere.
@@ -39,8 +47,7 @@
 		throw!(ImportNotSupported(from.into(), path.into()))
 	}
 
-	fn load_file_contents(&self, _resolved: &Path) -> Result<IStr> {
-		// Can be only caused by library direct consumer, not by supplied jsonnet
+	fn load_file_contents(&self, _resolved: &Path) -> Result<Vec<u8>> {
 		panic!("dummy resolver can't load any file")
 	}
 
@@ -79,41 +86,12 @@
 			throw!(ImportFileNotFound(from.to_owned(), path.to_owned()))
 		}
 	}
-	fn load_file_contents(&self, id: &Path) -> Result<IStr> {
+	fn load_file_contents(&self, id: &Path) -> Result<Vec<u8>> {
 		let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.to_owned()))?;
-		let mut out = String::new();
-		file.read_to_string(&mut out)
-			.map_err(|_e| ImportBadFileUtf8(id.to_owned()))?;
-		Ok(out.into())
-	}
-	unsafe fn as_any(&self) -> &dyn Any {
-		panic!("this resolver can't be used as any")
-	}
-}
-
-type ResolutionData = (PathBuf, PathBuf);
-
-/// Caches results of the underlying resolver
-pub struct CachingImportResolver {
-	resolution_cache: RefCell<HashMap<ResolutionData, Result<Rc<Path>>>>,
-	loading_cache: RefCell<HashMap<PathBuf, Result<IStr>>>,
-	inner: Box<dyn ImportResolver>,
-}
-impl ImportResolver for CachingImportResolver {
-	fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
-		self.resolution_cache
-			.borrow_mut()
-			.entry((from.to_owned(), path.to_owned()))
-			.or_insert_with(|| self.inner.resolve_file(from, path))
-			.clone()
-	}
-
-	fn load_file_contents(&self, resolved: &Path) -> Result<IStr> {
-		self.loading_cache
-			.borrow_mut()
-			.entry(resolved.to_owned())
-			.or_insert_with(|| self.inner.load_file_contents(resolved))
-			.clone()
+		let mut out = Vec::new();
+		file.read_to_end(&mut out)
+			.map_err(|e| ImportIo(e.to_string()))?;
+		Ok(out)
 	}
 	unsafe fn as_any(&self) -> &dyn Any {
 		panic!("this resolver can't be used as any")
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -118,8 +118,9 @@
 
 	breakpoints: Breakpoints,
 	/// Contains file source codes and evaluation results for imports and pretty-printed stacktraces
-	files: HashMap<Rc<Path>, FileData>,
-	str_files: HashMap<Rc<Path>, IStr>,
+	files: GcHashMap<Rc<Path>, FileData>,
+	str_files: GcHashMap<Rc<Path>, IStr>,
+	bin_files: GcHashMap<Rc<Path>, Rc<[u8]>>,
 }
 
 pub struct FileData {
@@ -280,18 +281,26 @@
 				return self.evaluate_loaded_file_raw(&file_path);
 			}
 		}
-		let contents = self.load_file_contents(&file_path)?;
+		let contents = self.load_file_str(&file_path)?;
 		self.add_file(file_path.clone(), contents)?;
 		self.evaluate_loaded_file_raw(&file_path)
 	}
 	pub(crate) fn import_file_str(&self, from: &Path, path: &Path) -> Result<IStr> {
 		let path = self.resolve_file(from, path)?;
 		if !self.data().str_files.contains_key(&path) {
-			let file_str = self.load_file_contents(&path)?;
+			let file_str = self.load_file_str(&path)?;
 			self.data_mut().str_files.insert(path.clone(), file_str);
 		}
 		Ok(self.data().str_files.get(&path).cloned().unwrap())
 	}
+	pub(crate) fn import_file_bin(&self, from: &Path, path: &Path) -> Result<Rc<[u8]>> {
+		let path = self.resolve_file(from, path)?;
+		if !self.data().bin_files.contains_key(&path) {
+			let file_bin = self.load_file_bin(&path)?;
+			self.data_mut().bin_files.insert(path.clone(), file_bin);
+		}
+		Ok(self.data().bin_files.get(&path).cloned().unwrap())
+	}
 
 	fn evaluate_loaded_file_raw(&self, name: &Path) -> Result<Val> {
 		let expr: LocExpr = {
@@ -607,8 +616,11 @@
 	pub fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
 		self.settings().import_resolver.resolve_file(from, path)
 	}
-	pub fn load_file_contents(&self, path: &Path) -> Result<IStr> {
-		self.settings().import_resolver.load_file_contents(path)
+	pub fn load_file_str(&self, path: &Path) -> Result<IStr> {
+		self.settings().import_resolver.load_file_str(path)
+	}
+	pub fn load_file_bin(&self, path: &Path) -> Result<Rc<[u8]>> {
+		self.settings().import_resolver.load_file_bin(path)
 	}
 
 	pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {
@@ -687,7 +699,6 @@
 		primitive_equals, EvaluationState,
 	};
 	use gcmodule::{Cc, Trace};
-	use jrsonnet_interner::IStr;
 	use jrsonnet_parser::*;
 	use std::{
 		path::{Path, PathBuf},
@@ -1204,19 +1215,23 @@
 		Ok(())
 	}
 
-	struct TestImportResolver(IStr);
+	struct TestImportResolver(Vec<u8>);
 	impl crate::import::ImportResolver for TestImportResolver {
 		fn resolve_file(&self, _: &Path, _: &Path) -> crate::error::Result<Rc<Path>> {
 			Ok(PathBuf::from("/test").into())
 		}
 
-		fn load_file_contents(&self, _: &Path) -> crate::error::Result<IStr> {
+		fn load_file_contents(&self, _: &Path) -> crate::error::Result<Vec<u8>> {
 			Ok(self.0.clone())
 		}
 
 		unsafe fn as_any(&self) -> &dyn std::any::Any {
 			panic!()
 		}
+
+		fn load_file_bin(&self, _resolved: &Path) -> crate::error::Result<Rc<[u8]>> {
+			panic!()
+		}
 	}
 
 	#[test]
modifiedcrates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -1,4 +1,7 @@
-use std::convert::{TryFrom, TryInto};
+use std::{
+	convert::{TryFrom, TryInto},
+	rc::Rc,
+};
 
 use gcmodule::Cc;
 use jrsonnet_interner::IStr;
@@ -306,6 +309,44 @@
 	}
 }
 
+/// Specialization
+pub struct Bytes(pub Rc<[u8]>);
+
+impl Typed for Bytes {
+	const TYPE: &'static ComplexValType =
+		&ComplexValType::ArrayRef(&ComplexValType::BoundedNumber(Some(0.0), Some(255.0)));
+}
+impl TryFrom<Val> for Bytes {
+	type Error = LocError;
+
+	fn try_from(value: Val) -> Result<Self> {
+		match value {
+			Val::Arr(ArrValue::Bytes(bytes)) => Ok(Self(bytes)),
+			_ => {
+				<Self as Typed>::TYPE.check(&value)?;
+				match value {
+					Val::Arr(a) => {
+						let mut out = Vec::with_capacity(a.len());
+						for e in a.iter() {
+							let r = e?;
+							out.push(u8::try_from(r)?);
+						}
+						Ok(Self(out.into()))
+					}
+					_ => unreachable!(),
+				}
+			}
+		}
+	}
+}
+impl TryFrom<Bytes> for Val {
+	type Error = LocError;
+
+	fn try_from(value: Bytes) -> Result<Self> {
+		Ok(Val::Arr(ArrValue::Bytes(value.0)))
+	}
+}
+
 pub struct M1;
 impl Typed for M1 {
 	const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(-1.0), Some(-1.0));
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/val.rs
1use crate::{2	builtin::manifest::{3		manifest_json_ex, manifest_yaml_ex, ManifestJsonOptions, ManifestType, ManifestYamlOptions,4	},5	cc_ptr_eq,6	error::{Error::*, LocError},7	evaluate,8	function::{9		parse_default_function_call, parse_function_call, ArgsLike, Builtin, CallLocation,10		StaticBuiltin,11	},12	gc::TraceBox,13	throw, Context, ObjValue, Result,14};15use gcmodule::{Cc, Trace};16use jrsonnet_interner::IStr;17use jrsonnet_parser::{LocExpr, ParamsDesc};18use jrsonnet_types::ValType;19use std::{cell::RefCell, fmt::Debug, rc::Rc};2021pub trait LazyValValue: Trace {22	fn get(self: Box<Self>) -> Result<Val>;23}2425#[derive(Trace)]26enum LazyValInternals {27	Computed(Val),28	Errored(LocError),29	Waiting(TraceBox<dyn LazyValValue>),30	Pending,31}3233#[derive(Clone, Trace)]34pub struct LazyVal(Cc<RefCell<LazyValInternals>>);35impl LazyVal {36	pub fn new(f: TraceBox<dyn LazyValValue>) -> Self {37		Self(Cc::new(RefCell::new(LazyValInternals::Waiting(f))))38	}39	pub fn new_resolved(val: Val) -> Self {40		Self(Cc::new(RefCell::new(LazyValInternals::Computed(val))))41	}42	pub fn force(&self) -> Result<()> {43		self.evaluate()?;44		Ok(())45	}46	pub fn evaluate(&self) -> Result<Val> {47		match &*self.0.borrow() {48			LazyValInternals::Computed(v) => return Ok(v.clone()),49			LazyValInternals::Errored(e) => return Err(e.clone()),50			LazyValInternals::Pending => return Err(RecursiveLazyValueEvaluation.into()),51			_ => (),52		};53		let value = if let LazyValInternals::Waiting(value) =54			std::mem::replace(&mut *self.0.borrow_mut(), LazyValInternals::Pending)55		{56			value57		} else {58			unreachable!()59		};60		let new_value = match value.0.get() {61			Ok(v) => v,62			Err(e) => {63				*self.0.borrow_mut() = LazyValInternals::Errored(e.clone());64				return Err(e);65			}66		};67		*self.0.borrow_mut() = LazyValInternals::Computed(new_value.clone());68		Ok(new_value)69	}70}7172impl Debug for LazyVal {73	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {74		write!(f, "Lazy")75	}76}77impl PartialEq for LazyVal {78	fn eq(&self, other: &Self) -> bool {79		cc_ptr_eq(&self.0, &other.0)80	}81}8283#[derive(Debug, PartialEq, Trace)]84pub struct FuncDesc {85	pub name: IStr,86	pub ctx: Context,87	pub params: ParamsDesc,88	pub body: LocExpr,89}90impl FuncDesc {91	/// Create body context, but fill arguments without defaults with lazy error92	pub fn default_body_context(&self) -> Context {93		parse_default_function_call(self.ctx.clone(), &self.params)94	}9596	/// Create context, with which body code will run97	pub fn call_body_context(98		&self,99		call_ctx: Context,100		args: &dyn ArgsLike,101		tailstrict: bool,102	) -> Result<Context> {103		parse_function_call(call_ctx, self.ctx.clone(), &self.params, args, tailstrict)104	}105}106107#[derive(Trace, Clone)]108pub enum FuncVal {109	/// Plain function implemented in jsonnet110	Normal(Cc<FuncDesc>),111	/// Standard library function112	StaticBuiltin(#[skip_trace] &'static dyn StaticBuiltin),113	/// User-provided function114	Builtin(Cc<TraceBox<dyn Builtin>>),115}116117impl Debug for FuncVal {118	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {119		match self {120			Self::Normal(arg0) => f.debug_tuple("Normal").field(arg0).finish(),121			Self::StaticBuiltin(arg0) => {122				f.debug_tuple("StaticBuiltin").field(&arg0.name()).finish()123			}124			Self::Builtin(arg0) => f.debug_tuple("Builtin").field(&arg0.name()).finish(),125		}126	}127}128129impl FuncVal {130	pub fn args_len(&self) -> usize {131		match self {132			Self::Normal(n) => n.params.iter().filter(|p| p.1.is_none()).count(),133			Self::StaticBuiltin(i) => i.params().iter().filter(|p| !p.has_default).count(),134			Self::Builtin(i) => i.params().iter().filter(|p| !p.has_default).count(),135		}136	}137	pub fn name(&self) -> IStr {138		match self {139			Self::Normal(normal) => normal.name.clone(),140			Self::StaticBuiltin(builtin) => builtin.name().into(),141			Self::Builtin(builtin) => builtin.name().into(),142		}143	}144	pub fn evaluate(145		&self,146		call_ctx: Context,147		loc: CallLocation,148		args: &dyn ArgsLike,149		tailstrict: bool,150	) -> Result<Val> {151		match self {152			Self::Normal(func) => {153				let body_ctx = func.call_body_context(call_ctx, args, tailstrict)?;154				evaluate(body_ctx, &func.body)155			}156			Self::StaticBuiltin(b) => b.call(call_ctx, loc, args),157			Self::Builtin(b) => b.call(call_ctx, loc, args),158		}159	}160	pub fn evaluate_simple(&self, args: &dyn ArgsLike) -> Result<Val> {161		self.evaluate(Context::default(), CallLocation::native(), args, true)162	}163}164165#[derive(Clone)]166pub enum ManifestFormat {167	YamlStream(Box<ManifestFormat>),168	Yaml(usize),169	Json(usize),170	ToString,171	String,172}173174#[derive(Debug, Clone, Trace)]175#[force_tracking]176pub enum ArrValue {177	Lazy(Cc<Vec<LazyVal>>),178	Eager(Cc<Vec<Val>>),179	Extended(Box<(Self, Self)>),180}181impl ArrValue {182	pub fn new_eager() -> Self {183		Self::Eager(Cc::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<Cc<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				Cc::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				Cc::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(Cc::new(out))276			}277			Self::Eager(vec) => {278				let mut out = (&vec as &Vec<_>).clone();279				out.reverse();280				Self::Eager(Cc::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(Cc::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(Cc::new(out)))307	}308309	pub fn ptr_eq(a: &Self, b: &Self) -> bool {310		match (a, b) {311			(Self::Lazy(a), Self::Lazy(b)) => cc_ptr_eq(a, b),312			(Self::Eager(a), Self::Eager(b)) => cc_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(Cc::new(v))321	}322}323324impl From<Vec<Val>> for ArrValue {325	fn from(v: Vec<Val>) -> Self {326		Self::Eager(Cc::new(v))327	}328}329330pub enum IndexableVal {331	Str(IStr),332	Arr(ArrValue),333}334335#[derive(Debug, Clone, Trace)]336pub enum Val {337	Bool(bool),338	Null,339	Str(IStr),340	Num(f64),341	Arr(ArrValue),342	Obj(ObjValue),343	Func(FuncVal),344}345346impl Val {347	pub const fn as_bool(&self) -> Option<bool> {348		match self {349			Val::Bool(v) => Some(*v),350			_ => None,351		}352	}353	pub const fn as_null(&self) -> Option<()> {354		match self {355			Val::Null => Some(()),356			_ => None,357		}358	}359	pub fn as_str(&self) -> Option<IStr> {360		match self {361			Val::Str(s) => Some(s.clone()),362			_ => None,363		}364	}365	pub const fn as_num(&self) -> Option<f64> {366		match self {367			Val::Num(n) => Some(*n),368			_ => None,369		}370	}371	pub fn as_arr(&self) -> Option<ArrValue> {372		match self {373			Val::Arr(a) => Some(a.clone()),374			_ => None,375		}376	}377	pub fn as_obj(&self) -> Option<ObjValue> {378		match self {379			Val::Obj(o) => Some(o.clone()),380			_ => None,381		}382	}383	pub fn as_func(&self) -> Option<FuncVal> {384		match self {385			Val::Func(f) => Some(f.clone()),386			_ => None,387		}388	}389390	/// Creates `Val::Num` after checking for numeric overflow.391	/// As numbers are `f64`, we can just check for their finity.392	pub fn new_checked_num(num: f64) -> Result<Self> {393		if num.is_finite() {394			Ok(Self::Num(num))395		} else {396			throw!(RuntimeError("overflow".into()))397		}398	}399400	pub fn try_cast_nullable_num(self, context: &'static str) -> Result<Option<f64>> {401		Ok(match self {402			Val::Null => None,403			Val::Num(num) => Some(num),404			_ => throw!(TypeMismatch(405				context,406				vec![ValType::Null, ValType::Num],407				self.value_type()408			)),409		})410	}411	pub const fn value_type(&self) -> ValType {412		match self {413			Self::Str(..) => ValType::Str,414			Self::Num(..) => ValType::Num,415			Self::Arr(..) => ValType::Arr,416			Self::Obj(..) => ValType::Obj,417			Self::Bool(_) => ValType::Bool,418			Self::Null => ValType::Null,419			Self::Func(..) => ValType::Func,420		}421	}422423	pub fn to_string(&self) -> Result<IStr> {424		Ok(match self {425			Self::Bool(true) => "true".into(),426			Self::Bool(false) => "false".into(),427			Self::Null => "null".into(),428			Self::Str(s) => s.clone(),429			v => manifest_json_ex(430				v,431				&ManifestJsonOptions {432					padding: "",433					mtype: ManifestType::ToString,434					newline: "\n",435					key_val_sep: ": ",436				},437			)?438			.into(),439		})440	}441442	/// Expects value to be object, outputs (key, manifested value) pairs443	pub fn manifest_multi(&self, ty: &ManifestFormat) -> Result<Vec<(IStr, IStr)>> {444		let obj = match self {445			Self::Obj(obj) => obj,446			_ => throw!(MultiManifestOutputIsNotAObject),447		};448		let keys = obj.fields();449		let mut out = Vec::with_capacity(keys.len());450		for key in keys {451			let value = obj452				.get(key.clone())?453				.expect("item in object")454				.manifest(ty)?;455			out.push((key, value));456		}457		Ok(out)458	}459460	/// Expects value to be array, outputs manifested values461	pub fn manifest_stream(&self, ty: &ManifestFormat) -> Result<Vec<IStr>> {462		let arr = match self {463			Self::Arr(a) => a,464			_ => throw!(StreamManifestOutputIsNotAArray),465		};466		let mut out = Vec::with_capacity(arr.len());467		for i in arr.iter() {468			out.push(i?.manifest(ty)?);469		}470		Ok(out)471	}472473	pub fn manifest(&self, ty: &ManifestFormat) -> Result<IStr> {474		Ok(match ty {475			ManifestFormat::YamlStream(format) => {476				let arr = match self {477					Self::Arr(a) => a,478					_ => throw!(StreamManifestOutputIsNotAArray),479				};480				let mut out = String::new();481482				match format as &ManifestFormat {483					ManifestFormat::YamlStream(_) => throw!(StreamManifestOutputCannotBeRecursed),484					ManifestFormat::String => throw!(StreamManifestCannotNestString),485					_ => {}486				};487488				if !arr.is_empty() {489					for v in arr.iter() {490						out.push_str("---\n");491						out.push_str(&v?.manifest(format)?);492						out.push('\n');493					}494					out.push_str("...");495				}496497				out.into()498			}499			ManifestFormat::Yaml(padding) => self.to_yaml(*padding)?,500			ManifestFormat::Json(padding) => self.to_json(*padding)?,501			ManifestFormat::ToString => self.to_string()?,502			ManifestFormat::String => match self {503				Self::Str(s) => s.clone(),504				_ => throw!(StringManifestOutputIsNotAString),505			},506		})507	}508509	/// For manifestification510	pub fn to_json(&self, padding: usize) -> Result<IStr> {511		manifest_json_ex(512			self,513			&ManifestJsonOptions {514				padding: &" ".repeat(padding),515				mtype: if padding == 0 {516					ManifestType::Minify517				} else {518					ManifestType::Manifest519				},520				newline: "\n",521				key_val_sep: ": ",522			},523		)524		.map(|s| s.into())525	}526527	/// Calls `std.manifestJson`528	pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {529		manifest_json_ex(530			self,531			&ManifestJsonOptions {532				padding: &" ".repeat(padding),533				mtype: ManifestType::Std,534				newline: "\n",535				key_val_sep: ": ",536			},537		)538		.map(|s| s.into())539	}540541	pub fn to_yaml(&self, padding: usize) -> Result<IStr> {542		let padding = &" ".repeat(padding);543		manifest_yaml_ex(544			self,545			&ManifestYamlOptions {546				padding,547				arr_element_padding: padding,548				quote_keys: false,549			},550		)551		.map(|s| s.into())552	}553	pub fn into_indexable(self) -> Result<IndexableVal> {554		Ok(match self {555			Val::Str(s) => IndexableVal::Str(s),556			Val::Arr(arr) => IndexableVal::Arr(arr),557			_ => throw!(ValueIsNotIndexable(self.value_type())),558		})559	}560}561562const fn is_function_like(val: &Val) -> bool {563	matches!(val, Val::Func(_))564}565566/// Native implementation of `std.primitiveEquals`567pub fn primitive_equals(val_a: &Val, val_b: &Val) -> Result<bool> {568	Ok(match (val_a, val_b) {569		(Val::Bool(a), Val::Bool(b)) => a == b,570		(Val::Null, Val::Null) => true,571		(Val::Str(a), Val::Str(b)) => a == b,572		(Val::Num(a), Val::Num(b)) => (a - b).abs() <= f64::EPSILON,573		(Val::Arr(_), Val::Arr(_)) => throw!(RuntimeError(574			"primitiveEquals operates on primitive types, got array".into(),575		)),576		(Val::Obj(_), Val::Obj(_)) => throw!(RuntimeError(577			"primitiveEquals operates on primitive types, got object".into(),578		)),579		(a, b) if is_function_like(a) && is_function_like(b) => {580			throw!(RuntimeError("cannot test equality of functions".into()))581		}582		(_, _) => false,583	})584}585586/// Native implementation of `std.equals`587pub fn equals(val_a: &Val, val_b: &Val) -> Result<bool> {588	if val_a.value_type() != val_b.value_type() {589		return Ok(false);590	}591	match (val_a, val_b) {592		(Val::Arr(a), Val::Arr(b)) => {593			if ArrValue::ptr_eq(a, b) {594				return Ok(true);595			}596			if a.len() != b.len() {597				return Ok(false);598			}599			for (a, b) in a.iter().zip(b.iter()) {600				if !equals(&a?, &b?)? {601					return Ok(false);602				}603			}604			Ok(true)605		}606		(Val::Obj(a), Val::Obj(b)) => {607			if ObjValue::ptr_eq(a, b) {608				return Ok(true);609			}610			let fields = a.fields();611			if fields != b.fields() {612				return Ok(false);613			}614			for field in fields {615				if !equals(&a.get(field.clone())?.unwrap(), &b.get(field)?.unwrap())? {616					return Ok(false);617				}618			}619			Ok(true)620		}621		(a, b) => Ok(primitive_equals(a, b)?),622	}623}
after · crates/jrsonnet-evaluator/src/val.rs
1use crate::{2	builtin::manifest::{3		manifest_json_ex, manifest_yaml_ex, ManifestJsonOptions, ManifestType, ManifestYamlOptions,4	},5	cc_ptr_eq,6	error::{Error::*, LocError},7	evaluate,8	function::{9		parse_default_function_call, parse_function_call, ArgsLike, Builtin, CallLocation,10		StaticBuiltin,11	},12	gc::TraceBox,13	throw, Context, ObjValue, Result,14};15use gcmodule::{Cc, Trace};16use jrsonnet_interner::IStr;17use jrsonnet_parser::{LocExpr, ParamsDesc};18use jrsonnet_types::ValType;19use std::{cell::RefCell, fmt::Debug, rc::Rc};2021pub trait LazyValValue: Trace {22	fn get(self: Box<Self>) -> Result<Val>;23}2425#[derive(Trace)]26enum LazyValInternals {27	Computed(Val),28	Errored(LocError),29	Waiting(TraceBox<dyn LazyValValue>),30	Pending,31}3233#[derive(Clone, Trace)]34pub struct LazyVal(Cc<RefCell<LazyValInternals>>);35impl LazyVal {36	pub fn new(f: TraceBox<dyn LazyValValue>) -> Self {37		Self(Cc::new(RefCell::new(LazyValInternals::Waiting(f))))38	}39	pub fn new_resolved(val: Val) -> Self {40		Self(Cc::new(RefCell::new(LazyValInternals::Computed(val))))41	}42	pub fn force(&self) -> Result<()> {43		self.evaluate()?;44		Ok(())45	}46	pub fn evaluate(&self) -> Result<Val> {47		match &*self.0.borrow() {48			LazyValInternals::Computed(v) => return Ok(v.clone()),49			LazyValInternals::Errored(e) => return Err(e.clone()),50			LazyValInternals::Pending => return Err(RecursiveLazyValueEvaluation.into()),51			_ => (),52		};53		let value = if let LazyValInternals::Waiting(value) =54			std::mem::replace(&mut *self.0.borrow_mut(), LazyValInternals::Pending)55		{56			value57		} else {58			unreachable!()59		};60		let new_value = match value.0.get() {61			Ok(v) => v,62			Err(e) => {63				*self.0.borrow_mut() = LazyValInternals::Errored(e.clone());64				return Err(e);65			}66		};67		*self.0.borrow_mut() = LazyValInternals::Computed(new_value.clone());68		Ok(new_value)69	}70}7172impl Debug for LazyVal {73	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {74		write!(f, "Lazy")75	}76}77impl PartialEq for LazyVal {78	fn eq(&self, other: &Self) -> bool {79		cc_ptr_eq(&self.0, &other.0)80	}81}8283#[derive(Debug, PartialEq, Trace)]84pub struct FuncDesc {85	pub name: IStr,86	pub ctx: Context,87	pub params: ParamsDesc,88	pub body: LocExpr,89}90impl FuncDesc {91	/// Create body context, but fill arguments without defaults with lazy error92	pub fn default_body_context(&self) -> Context {93		parse_default_function_call(self.ctx.clone(), &self.params)94	}9596	/// Create context, with which body code will run97	pub fn call_body_context(98		&self,99		call_ctx: Context,100		args: &dyn ArgsLike,101		tailstrict: bool,102	) -> Result<Context> {103		parse_function_call(call_ctx, self.ctx.clone(), &self.params, args, tailstrict)104	}105}106107#[derive(Trace, Clone)]108pub enum FuncVal {109	/// Plain function implemented in jsonnet110	Normal(Cc<FuncDesc>),111	/// Standard library function112	StaticBuiltin(#[skip_trace] &'static dyn StaticBuiltin),113	/// User-provided function114	Builtin(Cc<TraceBox<dyn Builtin>>),115}116117impl Debug for FuncVal {118	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {119		match self {120			Self::Normal(arg0) => f.debug_tuple("Normal").field(arg0).finish(),121			Self::StaticBuiltin(arg0) => {122				f.debug_tuple("StaticBuiltin").field(&arg0.name()).finish()123			}124			Self::Builtin(arg0) => f.debug_tuple("Builtin").field(&arg0.name()).finish(),125		}126	}127}128129impl FuncVal {130	pub fn args_len(&self) -> usize {131		match self {132			Self::Normal(n) => n.params.iter().filter(|p| p.1.is_none()).count(),133			Self::StaticBuiltin(i) => i.params().iter().filter(|p| !p.has_default).count(),134			Self::Builtin(i) => i.params().iter().filter(|p| !p.has_default).count(),135		}136	}137	pub fn name(&self) -> IStr {138		match self {139			Self::Normal(normal) => normal.name.clone(),140			Self::StaticBuiltin(builtin) => builtin.name().into(),141			Self::Builtin(builtin) => builtin.name().into(),142		}143	}144	pub fn evaluate(145		&self,146		call_ctx: Context,147		loc: CallLocation,148		args: &dyn ArgsLike,149		tailstrict: bool,150	) -> Result<Val> {151		match self {152			Self::Normal(func) => {153				let body_ctx = func.call_body_context(call_ctx, args, tailstrict)?;154				evaluate(body_ctx, &func.body)155			}156			Self::StaticBuiltin(b) => b.call(call_ctx, loc, args),157			Self::Builtin(b) => b.call(call_ctx, loc, args),158		}159	}160	pub fn evaluate_simple(&self, args: &dyn ArgsLike) -> Result<Val> {161		self.evaluate(Context::default(), CallLocation::native(), args, true)162	}163}164165#[derive(Clone)]166pub enum ManifestFormat {167	YamlStream(Box<ManifestFormat>),168	Yaml(usize),169	Json(usize),170	ToString,171	String,172}173174#[derive(Debug, Clone, Trace)]175#[force_tracking]176pub enum ArrValue {177	Bytes(#[skip_trace] Rc<[u8]>),178	Lazy(Cc<Vec<LazyVal>>),179	Eager(Cc<Vec<Val>>),180	Extended(Box<(Self, Self)>),181}182impl ArrValue {183	pub fn new_eager() -> Self {184		Self::Eager(Cc::new(Vec::new()))185	}186187	pub fn len(&self) -> usize {188		match self {189			Self::Bytes(i) => i.len(),190			Self::Lazy(l) => l.len(),191			Self::Eager(e) => e.len(),192			Self::Extended(v) => v.0.len() + v.1.len(),193		}194	}195196	pub fn is_empty(&self) -> bool {197		self.len() == 0198	}199200	pub fn get(&self, index: usize) -> Result<Option<Val>> {201		match self {202			Self::Bytes(i) => i203				.get(index)204				.map_or(Ok(None), |v| Ok(Some(Val::Num(*v as f64)))),205			Self::Lazy(vec) => {206				if let Some(v) = vec.get(index) {207					Ok(Some(v.evaluate()?))208				} else {209					Ok(None)210				}211			}212			Self::Eager(vec) => Ok(vec.get(index).cloned()),213			Self::Extended(v) => {214				let a_len = v.0.len();215				if a_len > index {216					v.0.get(index)217				} else {218					v.1.get(index - a_len)219				}220			}221		}222	}223224	pub fn get_lazy(&self, index: usize) -> Option<LazyVal> {225		match self {226			Self::Bytes(i) => i227				.get(index)228				.map(|b| LazyVal::new_resolved(Val::Num(*b as f64))),229			Self::Lazy(vec) => vec.get(index).cloned(),230			Self::Eager(vec) => vec.get(index).cloned().map(LazyVal::new_resolved),231			Self::Extended(v) => {232				let a_len = v.0.len();233				if a_len > index {234					v.0.get_lazy(index)235				} else {236					v.1.get_lazy(index - a_len)237				}238			}239		}240	}241242	pub fn evaluated(&self) -> Result<Cc<Vec<Val>>> {243		Ok(match self {244			Self::Bytes(i) => {245				let mut out = Vec::with_capacity(i.len());246				for v in i.iter() {247					out.push(Val::Num(*v as f64));248				}249				Cc::new(out)250			}251			Self::Lazy(vec) => {252				let mut out = Vec::with_capacity(vec.len());253				for item in vec.iter() {254					out.push(item.evaluate()?);255				}256				Cc::new(out)257			}258			Self::Eager(vec) => vec.clone(),259			Self::Extended(_v) => {260				let mut out = Vec::with_capacity(self.len());261				for item in self.iter() {262					out.push(item?);263				}264				Cc::new(out)265			}266		})267	}268269	pub fn iter(&self) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {270		(0..self.len()).map(move |idx| match self {271			Self::Bytes(b) => Ok(Val::Num(b[idx] as f64)),272			Self::Lazy(l) => l[idx].evaluate(),273			Self::Eager(e) => Ok(e[idx].clone()),274			Self::Extended(_) => self.get(idx).map(|e| e.unwrap()),275		})276	}277278	pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = LazyVal> + '_ {279		(0..self.len()).map(move |idx| match self {280			Self::Bytes(b) => LazyVal::new_resolved(Val::Num(b[idx] as f64)),281			Self::Lazy(l) => l[idx].clone(),282			Self::Eager(e) => LazyVal::new_resolved(e[idx].clone()),283			Self::Extended(_) => self.get_lazy(idx).unwrap(),284		})285	}286287	pub fn reversed(self) -> Self {288		match self {289			Self::Bytes(b) => {290				let mut out = b.to_vec();291				out.reverse();292				Self::Bytes(out.into())293			}294			Self::Lazy(vec) => {295				let mut out = (&vec as &Vec<_>).clone();296				out.reverse();297				Self::Lazy(Cc::new(out))298			}299			Self::Eager(vec) => {300				let mut out = (&vec as &Vec<_>).clone();301				out.reverse();302				Self::Eager(Cc::new(out))303			}304			Self::Extended(b) => Self::Extended(Box::new((b.1.reversed(), b.0.reversed()))),305		}306	}307308	pub fn map(self, mapper: impl Fn(Val) -> Result<Val>) -> Result<Self> {309		let mut out = Vec::with_capacity(self.len());310311		for value in self.iter() {312			out.push(mapper(value?)?);313		}314315		Ok(Self::Eager(Cc::new(out)))316	}317318	pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {319		let mut out = Vec::with_capacity(self.len());320321		for value in self.iter() {322			let value = value?;323			if filter(&value)? {324				out.push(value);325			}326		}327328		Ok(Self::Eager(Cc::new(out)))329	}330331	pub fn ptr_eq(a: &Self, b: &Self) -> bool {332		match (a, b) {333			(Self::Lazy(a), Self::Lazy(b)) => cc_ptr_eq(a, b),334			(Self::Eager(a), Self::Eager(b)) => cc_ptr_eq(a, b),335			_ => false,336		}337	}338}339340impl From<Vec<LazyVal>> for ArrValue {341	fn from(v: Vec<LazyVal>) -> Self {342		Self::Lazy(Cc::new(v))343	}344}345346impl From<Vec<Val>> for ArrValue {347	fn from(v: Vec<Val>) -> Self {348		Self::Eager(Cc::new(v))349	}350}351352pub enum IndexableVal {353	Str(IStr),354	Arr(ArrValue),355}356357#[derive(Debug, Clone, Trace)]358pub enum Val {359	Bool(bool),360	Null,361	Str(IStr),362	Num(f64),363	Arr(ArrValue),364	Obj(ObjValue),365	Func(FuncVal),366}367368impl Val {369	pub const fn as_bool(&self) -> Option<bool> {370		match self {371			Val::Bool(v) => Some(*v),372			_ => None,373		}374	}375	pub const fn as_null(&self) -> Option<()> {376		match self {377			Val::Null => Some(()),378			_ => None,379		}380	}381	pub fn as_str(&self) -> Option<IStr> {382		match self {383			Val::Str(s) => Some(s.clone()),384			_ => None,385		}386	}387	pub const fn as_num(&self) -> Option<f64> {388		match self {389			Val::Num(n) => Some(*n),390			_ => None,391		}392	}393	pub fn as_arr(&self) -> Option<ArrValue> {394		match self {395			Val::Arr(a) => Some(a.clone()),396			_ => None,397		}398	}399	pub fn as_obj(&self) -> Option<ObjValue> {400		match self {401			Val::Obj(o) => Some(o.clone()),402			_ => None,403		}404	}405	pub fn as_func(&self) -> Option<FuncVal> {406		match self {407			Val::Func(f) => Some(f.clone()),408			_ => None,409		}410	}411412	/// Creates `Val::Num` after checking for numeric overflow.413	/// As numbers are `f64`, we can just check for their finity.414	pub fn new_checked_num(num: f64) -> Result<Self> {415		if num.is_finite() {416			Ok(Self::Num(num))417		} else {418			throw!(RuntimeError("overflow".into()))419		}420	}421422	pub fn try_cast_nullable_num(self, context: &'static str) -> Result<Option<f64>> {423		Ok(match self {424			Val::Null => None,425			Val::Num(num) => Some(num),426			_ => throw!(TypeMismatch(427				context,428				vec![ValType::Null, ValType::Num],429				self.value_type()430			)),431		})432	}433	pub const fn value_type(&self) -> ValType {434		match self {435			Self::Str(..) => ValType::Str,436			Self::Num(..) => ValType::Num,437			Self::Arr(..) => ValType::Arr,438			Self::Obj(..) => ValType::Obj,439			Self::Bool(_) => ValType::Bool,440			Self::Null => ValType::Null,441			Self::Func(..) => ValType::Func,442		}443	}444445	pub fn to_string(&self) -> Result<IStr> {446		Ok(match self {447			Self::Bool(true) => "true".into(),448			Self::Bool(false) => "false".into(),449			Self::Null => "null".into(),450			Self::Str(s) => s.clone(),451			v => manifest_json_ex(452				v,453				&ManifestJsonOptions {454					padding: "",455					mtype: ManifestType::ToString,456					newline: "\n",457					key_val_sep: ": ",458				},459			)?460			.into(),461		})462	}463464	/// Expects value to be object, outputs (key, manifested value) pairs465	pub fn manifest_multi(&self, ty: &ManifestFormat) -> Result<Vec<(IStr, IStr)>> {466		let obj = match self {467			Self::Obj(obj) => obj,468			_ => throw!(MultiManifestOutputIsNotAObject),469		};470		let keys = obj.fields();471		let mut out = Vec::with_capacity(keys.len());472		for key in keys {473			let value = obj474				.get(key.clone())?475				.expect("item in object")476				.manifest(ty)?;477			out.push((key, value));478		}479		Ok(out)480	}481482	/// Expects value to be array, outputs manifested values483	pub fn manifest_stream(&self, ty: &ManifestFormat) -> Result<Vec<IStr>> {484		let arr = match self {485			Self::Arr(a) => a,486			_ => throw!(StreamManifestOutputIsNotAArray),487		};488		let mut out = Vec::with_capacity(arr.len());489		for i in arr.iter() {490			out.push(i?.manifest(ty)?);491		}492		Ok(out)493	}494495	pub fn manifest(&self, ty: &ManifestFormat) -> Result<IStr> {496		Ok(match ty {497			ManifestFormat::YamlStream(format) => {498				let arr = match self {499					Self::Arr(a) => a,500					_ => throw!(StreamManifestOutputIsNotAArray),501				};502				let mut out = String::new();503504				match format as &ManifestFormat {505					ManifestFormat::YamlStream(_) => throw!(StreamManifestOutputCannotBeRecursed),506					ManifestFormat::String => throw!(StreamManifestCannotNestString),507					_ => {}508				};509510				if !arr.is_empty() {511					for v in arr.iter() {512						out.push_str("---\n");513						out.push_str(&v?.manifest(format)?);514						out.push('\n');515					}516					out.push_str("...");517				}518519				out.into()520			}521			ManifestFormat::Yaml(padding) => self.to_yaml(*padding)?,522			ManifestFormat::Json(padding) => self.to_json(*padding)?,523			ManifestFormat::ToString => self.to_string()?,524			ManifestFormat::String => match self {525				Self::Str(s) => s.clone(),526				_ => throw!(StringManifestOutputIsNotAString),527			},528		})529	}530531	/// For manifestification532	pub fn to_json(&self, padding: usize) -> Result<IStr> {533		manifest_json_ex(534			self,535			&ManifestJsonOptions {536				padding: &" ".repeat(padding),537				mtype: if padding == 0 {538					ManifestType::Minify539				} else {540					ManifestType::Manifest541				},542				newline: "\n",543				key_val_sep: ": ",544			},545		)546		.map(|s| s.into())547	}548549	/// Calls `std.manifestJson`550	pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {551		manifest_json_ex(552			self,553			&ManifestJsonOptions {554				padding: &" ".repeat(padding),555				mtype: ManifestType::Std,556				newline: "\n",557				key_val_sep: ": ",558			},559		)560		.map(|s| s.into())561	}562563	pub fn to_yaml(&self, padding: usize) -> Result<IStr> {564		let padding = &" ".repeat(padding);565		manifest_yaml_ex(566			self,567			&ManifestYamlOptions {568				padding,569				arr_element_padding: padding,570				quote_keys: false,571			},572		)573		.map(|s| s.into())574	}575	pub fn into_indexable(self) -> Result<IndexableVal> {576		Ok(match self {577			Val::Str(s) => IndexableVal::Str(s),578			Val::Arr(arr) => IndexableVal::Arr(arr),579			_ => throw!(ValueIsNotIndexable(self.value_type())),580		})581	}582}583584const fn is_function_like(val: &Val) -> bool {585	matches!(val, Val::Func(_))586}587588/// Native implementation of `std.primitiveEquals`589pub fn primitive_equals(val_a: &Val, val_b: &Val) -> Result<bool> {590	Ok(match (val_a, val_b) {591		(Val::Bool(a), Val::Bool(b)) => a == b,592		(Val::Null, Val::Null) => true,593		(Val::Str(a), Val::Str(b)) => a == b,594		(Val::Num(a), Val::Num(b)) => (a - b).abs() <= f64::EPSILON,595		(Val::Arr(_), Val::Arr(_)) => throw!(RuntimeError(596			"primitiveEquals operates on primitive types, got array".into(),597		)),598		(Val::Obj(_), Val::Obj(_)) => throw!(RuntimeError(599			"primitiveEquals operates on primitive types, got object".into(),600		)),601		(a, b) if is_function_like(a) && is_function_like(b) => {602			throw!(RuntimeError("cannot test equality of functions".into()))603		}604		(_, _) => false,605	})606}607608/// Native implementation of `std.equals`609pub fn equals(val_a: &Val, val_b: &Val) -> Result<bool> {610	if val_a.value_type() != val_b.value_type() {611		return Ok(false);612	}613	match (val_a, val_b) {614		(Val::Arr(a), Val::Arr(b)) => {615			if ArrValue::ptr_eq(a, b) {616				return Ok(true);617			}618			if a.len() != b.len() {619				return Ok(false);620			}621			for (a, b) in a.iter().zip(b.iter()) {622				if !equals(&a?, &b?)? {623					return Ok(false);624				}625			}626			Ok(true)627		}628		(Val::Obj(a), Val::Obj(b)) => {629			if ObjValue::ptr_eq(a, b) {630				return Ok(true);631			}632			let fields = a.fields();633			if fields != b.fields() {634				return Ok(false);635			}636			for field in fields {637				if !equals(&a.get(field.clone())?.unwrap(), &b.get(field)?.unwrap())? {638					return Ok(false);639				}640			}641			Ok(true)642		}643		(a, b) => Ok(primitive_equals(a, b)?),644	}645}
modifiedcrates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-interner/src/lib.rs
+++ b/crates/jrsonnet-interner/src/lib.rs
@@ -4,10 +4,12 @@
 use std::{
 	borrow::Cow,
 	cell::RefCell,
+	convert::TryFrom,
 	fmt::{self, Display},
 	hash::{BuildHasherDefault, Hash, Hasher},
 	ops::Deref,
 	rc::Rc,
+	str::Utf8Error,
 };
 
 #[derive(Clone, PartialOrd, Ord, Eq)]
@@ -85,6 +87,15 @@
 	}
 }
 
+impl TryFrom<&[u8]> for IStr {
+	type Error = Utf8Error;
+
+	fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
+		let str = std::str::from_utf8(value)?;
+		Ok(str.into())
+	}
+}
+
 impl From<String> for IStr {
 	fn from(str: String) -> Self {
 		(&str as &str).into()
modifiedcrates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -285,6 +285,8 @@
 	Import(PathBuf),
 	/// importStr "file.txt"
 	ImportStr(PathBuf),
+	/// importBin "file.txt"
+	ImportBin(PathBuf),
 	/// error "I'm broken"
 	ErrorStmt(LocExpr),
 	/// a(b, c)
modifiedcrates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -53,7 +53,7 @@
 		rule number() -> f64 = quiet!{a:$(uint_str() ("." uint_str())? (['e'|'E'] (s:['+'|'-'])? uint_str())?) {? a.parse().map_err(|_| "<number>") }} / expected!("<number>")
 
 		/// Reserved word followed by any non-alphanumberic
-		rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()
+		rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "importbin" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()
 		rule id() = quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")
 
 		rule keyword(id: &'static str) -> ()
@@ -218,6 +218,7 @@
 			/ array_comp_expr(s)
 
 			/ keyword("importstr") _ path:string() {Expr::ImportStr(PathBuf::from(path))}
+			/ keyword("importbin") _ path:string() {Expr::ImportBin(PathBuf::from(path))}
 			/ keyword("import") _ path:string() {Expr::Import(PathBuf::from(path))}
 
 			/ var_expr(s)