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
before · crates/jrsonnet-evaluator/src/evaluate/mod.rs
1use std::convert::TryFrom;23use crate::{4	builtin::{std_slice, BUILTINS},5	error::Error::*,6	evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},7	function::CallLocation,8	gc::TraceBox,9	push_frame, throw, with_state, ArrValue, Bindable, Context, ContextCreator, FuncDesc, FuncVal,10	FutureWrapper, GcHashMap, LazyBinding, LazyVal, LazyValValue, ObjValue, ObjValueBuilder,11	ObjectAssertion, Result, Val,12};13use gcmodule::{Cc, Trace};14use jrsonnet_interner::IStr;15use jrsonnet_parser::{16	ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, FieldMember, ForSpecData, IfSpecData,17	LiteralType, LocExpr, Member, ObjBody, ParamsDesc,18};19use jrsonnet_types::ValType;20pub mod operator;2122pub fn evaluate_binding_in_future(23	b: &BindSpec,24	context_creator: FutureWrapper<Context>,25) -> LazyVal {26	let b = b.clone();27	if let Some(params) = &b.params {28		let params = params.clone();2930		#[derive(Trace)]31		struct LazyMethodBinding {32			context_creator: FutureWrapper<Context>,33			name: IStr,34			params: ParamsDesc,35			value: LocExpr,36		}37		impl LazyValValue for LazyMethodBinding {38			fn get(self: Box<Self>) -> Result<Val> {39				Ok(evaluate_method(40					self.context_creator.unwrap(),41					self.name,42					self.params,43					self.value,44				))45			}46		}4748		LazyVal::new(TraceBox(Box::new(LazyMethodBinding {49			context_creator,50			name: b.name.clone(),51			params,52			value: b.value.clone(),53		})))54	} else {55		#[derive(Trace)]56		struct LazyNamedBinding {57			context_creator: FutureWrapper<Context>,58			name: IStr,59			value: LocExpr,60		}61		impl LazyValValue for LazyNamedBinding {62			fn get(self: Box<Self>) -> Result<Val> {63				evaluate_named(self.context_creator.unwrap(), &self.value, self.name)64			}65		}66		LazyVal::new(TraceBox(Box::new(LazyNamedBinding {67			context_creator,68			name: b.name.clone(),69			value: b.value,70		})))71	}72}7374pub fn evaluate_binding(b: &BindSpec, context_creator: ContextCreator) -> (IStr, LazyBinding) {75	let b = b.clone();76	if let Some(params) = &b.params {77		let params = params.clone();7879		#[derive(Trace)]80		struct BindableMethodLazyVal {81			this: Option<ObjValue>,82			super_obj: Option<ObjValue>,8384			context_creator: ContextCreator,85			name: IStr,86			params: ParamsDesc,87			value: LocExpr,88		}89		impl LazyValValue for BindableMethodLazyVal {90			fn get(self: Box<Self>) -> Result<Val> {91				Ok(evaluate_method(92					self.context_creator.create(self.this, self.super_obj)?,93					self.name,94					self.params,95					self.value,96				))97			}98		}99100		#[derive(Trace)]101		struct BindableMethod {102			context_creator: ContextCreator,103			name: IStr,104			params: ParamsDesc,105			value: LocExpr,106		}107		impl Bindable for BindableMethod {108			fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {109				Ok(LazyVal::new(TraceBox(Box::new(BindableMethodLazyVal {110					this,111					super_obj,112113					context_creator: self.context_creator.clone(),114					name: self.name.clone(),115					params: self.params.clone(),116					value: self.value.clone(),117				}))))118			}119		}120121		(122			b.name.clone(),123			LazyBinding::Bindable(Cc::new(TraceBox(Box::new(BindableMethod {124				context_creator,125				name: b.name.clone(),126				params,127				value: b.value.clone(),128			})))),129		)130	} else {131		#[derive(Trace)]132		struct BindableNamedLazyVal {133			this: Option<ObjValue>,134			super_obj: Option<ObjValue>,135136			context_creator: ContextCreator,137			name: IStr,138			value: LocExpr,139		}140		impl LazyValValue for BindableNamedLazyVal {141			fn get(self: Box<Self>) -> Result<Val> {142				evaluate_named(143					self.context_creator.create(self.this, self.super_obj)?,144					&self.value,145					self.name,146				)147			}148		}149150		#[derive(Trace)]151		struct BindableNamed {152			context_creator: ContextCreator,153			name: IStr,154			value: LocExpr,155		}156		impl Bindable for BindableNamed {157			fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {158				Ok(LazyVal::new(TraceBox(Box::new(BindableNamedLazyVal {159					this,160					super_obj,161162					context_creator: self.context_creator.clone(),163					name: self.name.clone(),164					value: self.value.clone(),165				}))))166			}167		}168169		(170			b.name.clone(),171			LazyBinding::Bindable(Cc::new(TraceBox(Box::new(BindableNamed {172				context_creator,173				name: b.name.clone(),174				value: b.value.clone(),175			})))),176		)177	}178}179180pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {181	Val::Func(FuncVal::Normal(Cc::new(FuncDesc {182		name,183		ctx,184		params,185		body,186	})))187}188189pub fn evaluate_field_name(190	context: Context,191	field_name: &jrsonnet_parser::FieldName,192) -> Result<Option<IStr>> {193	Ok(match field_name {194		jrsonnet_parser::FieldName::Fixed(n) => Some(n.clone()),195		jrsonnet_parser::FieldName::Dyn(expr) => push_frame(196			CallLocation::new(&expr.1),197			|| "evaluating field name".to_string(),198			|| {199				let value = evaluate(context, expr)?;200				if matches!(value, Val::Null) {201					Ok(None)202				} else {203					Ok(Some(IStr::try_from(value)?))204				}205			},206		)?,207	})208}209210pub fn evaluate_comp(211	context: Context,212	specs: &[CompSpec],213	callback: &mut impl FnMut(Context) -> Result<()>,214) -> Result<()> {215	match specs.get(0) {216		None => callback(context)?,217		Some(CompSpec::IfSpec(IfSpecData(cond))) => {218			if bool::try_from(evaluate(context.clone(), cond)?)? {219				evaluate_comp(context, &specs[1..], callback)?220			}221		}222		Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(context.clone(), expr)? {223			Val::Arr(list) => {224				for item in list.iter() {225					evaluate_comp(226						context.clone().with_var(var.clone(), item?.clone()),227						&specs[1..],228						callback,229					)?230				}231			}232			_ => throw!(InComprehensionCanOnlyIterateOverArray),233		},234	}235	Ok(())236}237238pub fn evaluate_member_list_object(context: Context, members: &[Member]) -> Result<ObjValue> {239	let new_bindings = FutureWrapper::new();240	let future_this = FutureWrapper::new();241	let context_creator = ContextCreator(context.clone(), new_bindings.clone());242	{243		let mut bindings: GcHashMap<IStr, LazyBinding> = GcHashMap::with_capacity(members.len());244		for (n, b) in members245			.iter()246			.filter_map(|m| match m {247				Member::BindStmt(b) => Some(b.clone()),248				_ => None,249			})250			.map(|b| evaluate_binding(&b, context_creator.clone()))251		{252			bindings.insert(n, b);253		}254		new_bindings.fill(bindings);255	}256257	let mut builder = ObjValueBuilder::new();258	for member in members.iter() {259		match member {260			Member::Field(FieldMember {261				name,262				plus,263				params: None,264				visibility,265				value,266			}) => {267				let name = evaluate_field_name(context.clone(), name)?;268				if name.is_none() {269					continue;270				}271				let name = name.unwrap();272273				#[derive(Trace)]274				struct ObjMemberBinding {275					context_creator: ContextCreator,276					value: LocExpr,277					name: IStr,278				}279				impl Bindable for ObjMemberBinding {280					fn bind(281						&self,282						this: Option<ObjValue>,283						super_obj: Option<ObjValue>,284					) -> Result<LazyVal> {285						Ok(LazyVal::new_resolved(evaluate_named(286							self.context_creator.create(this, super_obj)?,287							&self.value,288							self.name.clone(),289						)?))290					}291				}292				builder293					.member(name.clone())294					.with_add(*plus)295					.with_visibility(*visibility)296					.with_location(value.1.clone())297					.bindable(TraceBox(Box::new(ObjMemberBinding {298						context_creator: context_creator.clone(),299						value: value.clone(),300						name,301					})));302			}303			Member::Field(FieldMember {304				name,305				params: Some(params),306				value,307				..308			}) => {309				let name = evaluate_field_name(context.clone(), name)?;310				if name.is_none() {311					continue;312				}313				let name = name.unwrap();314				#[derive(Trace)]315				struct ObjMemberBinding {316					context_creator: ContextCreator,317					value: LocExpr,318					params: ParamsDesc,319					name: IStr,320				}321				impl Bindable for ObjMemberBinding {322					fn bind(323						&self,324						this: Option<ObjValue>,325						super_obj: Option<ObjValue>,326					) -> Result<LazyVal> {327						Ok(LazyVal::new_resolved(evaluate_method(328							self.context_creator.create(this, super_obj)?,329							self.name.clone(),330							self.params.clone(),331							self.value.clone(),332						)))333					}334				}335				builder336					.member(name.clone())337					.hide()338					.with_location(value.1.clone())339					.bindable(TraceBox(Box::new(ObjMemberBinding {340						context_creator: context_creator.clone(),341						value: value.clone(),342						params: params.clone(),343						name,344					})));345			}346			Member::BindStmt(_) => {}347			Member::AssertStmt(stmt) => {348				#[derive(Trace)]349				struct ObjectAssert {350					context_creator: ContextCreator,351					assert: AssertStmt,352				}353				impl ObjectAssertion for ObjectAssert {354					fn run(355						&self,356						this: Option<ObjValue>,357						super_obj: Option<ObjValue>,358					) -> Result<()> {359						let ctx = self.context_creator.create(this, super_obj)?;360						evaluate_assert(ctx, &self.assert)361					}362				}363				builder.assert(TraceBox(Box::new(ObjectAssert {364					context_creator: context_creator.clone(),365					assert: stmt.clone(),366				})));367			}368		}369	}370	let this = builder.build();371	future_this.fill(this.clone());372	Ok(this)373}374375pub fn evaluate_object(context: Context, object: &ObjBody) -> Result<ObjValue> {376	Ok(match object {377		ObjBody::MemberList(members) => evaluate_member_list_object(context, members)?,378		ObjBody::ObjComp(obj) => {379			let future_this = FutureWrapper::new();380			let mut builder = ObjValueBuilder::new();381			evaluate_comp(context.clone(), &obj.compspecs, &mut |ctx| {382				let new_bindings = FutureWrapper::new();383				let context_creator = ContextCreator(context.clone(), new_bindings.clone());384				let mut bindings: GcHashMap<IStr, LazyBinding> =385					GcHashMap::with_capacity(obj.pre_locals.len() + obj.post_locals.len());386				for (n, b) in obj387					.pre_locals388					.iter()389					.chain(obj.post_locals.iter())390					.map(|b| evaluate_binding(b, context_creator.clone()))391				{392					bindings.insert(n, b);393				}394				new_bindings.fill(bindings.clone());395				let ctx = ctx.extend_unbound(bindings, None, None, None)?;396				let key = evaluate(ctx.clone(), &obj.key)?;397398				match key {399					Val::Null => {}400					Val::Str(n) => {401						#[derive(Trace)]402						struct ObjCompBinding {403							context: Context,404							value: LocExpr,405						}406						impl Bindable for ObjCompBinding {407							fn bind(408								&self,409								this: Option<ObjValue>,410								_super_obj: Option<ObjValue>,411							) -> Result<LazyVal> {412								Ok(LazyVal::new_resolved(evaluate(413									self.context414										.clone()415										.extend(GcHashMap::new(), None, this, None),416									&self.value,417								)?))418							}419						}420						builder421							.member(n)422							.with_location(obj.value.1.clone())423							.with_add(obj.plus)424							.bindable(TraceBox(Box::new(ObjCompBinding {425								context: ctx,426								value: obj.value.clone(),427							})));428					}429					v => throw!(FieldMustBeStringGot(v.value_type())),430				}431432				Ok(())433			})?;434435			let this = builder.build();436			future_this.fill(this.clone());437			this438		}439	})440}441442pub fn evaluate_apply(443	context: Context,444	value: &LocExpr,445	args: &ArgsDesc,446	loc: CallLocation,447	tailstrict: bool,448) -> Result<Val> {449	let value = evaluate(context.clone(), value)?;450	Ok(match value {451		Val::Func(f) => {452			let body = || f.evaluate(context, loc, args, tailstrict);453			if tailstrict {454				body()?455			} else {456				push_frame(loc, || format!("function <{}> call", f.name()), body)?457			}458		}459		v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),460	})461}462463pub fn evaluate_assert(context: Context, assertion: &AssertStmt) -> Result<()> {464	let value = &assertion.0;465	let msg = &assertion.1;466	let assertion_result = push_frame(467		CallLocation::new(&value.1),468		|| "assertion condition".to_owned(),469		|| bool::try_from(evaluate(context.clone(), value)?),470	)?;471	if !assertion_result {472		push_frame(473			CallLocation::new(&value.1),474			|| "assertion failure".to_owned(),475			|| {476				if let Some(msg) = msg {477					throw!(AssertionFailed(evaluate(context, msg)?.to_string()?));478				} else {479					throw!(AssertionFailed(Val::Null.to_string()?));480				}481			},482		)?483	}484	Ok(())485}486487pub fn evaluate_named(context: Context, lexpr: &LocExpr, name: IStr) -> Result<Val> {488	use Expr::*;489	let LocExpr(expr, _loc) = lexpr;490	Ok(match &**expr {491		Function(params, body) => evaluate_method(context, name, params.clone(), body.clone()),492		_ => evaluate(context, lexpr)?,493	})494}495496pub fn evaluate(context: Context, expr: &LocExpr) -> Result<Val> {497	use Expr::*;498	let LocExpr(expr, loc) = expr;499	// let bp = with_state(|s| s.0.stop_at.borrow().clone());500	Ok(match &**expr {501		Literal(LiteralType::This) => {502			Val::Obj(context.this().clone().ok_or(CantUseSelfOutsideOfObject)?)503		}504		Literal(LiteralType::Super) => Val::Obj(505			context506				.super_obj()507				.clone()508				.ok_or(NoSuperFound)?509				.with_this(context.this().clone().unwrap()),510		),511		Literal(LiteralType::Dollar) => {512			Val::Obj(context.dollar().clone().ok_or(NoTopLevelObjectFound)?)513		}514		Literal(LiteralType::True) => Val::Bool(true),515		Literal(LiteralType::False) => Val::Bool(false),516		Literal(LiteralType::Null) => Val::Null,517		Parened(e) => evaluate(context, e)?,518		Str(v) => Val::Str(v.clone()),519		Num(v) => Val::new_checked_num(*v)?,520		BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, v1, *o, v2)?,521		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,522		Var(name) => push_frame(523			CallLocation::new(loc),524			|| format!("variable <{}> access", name),525			|| context.binding(name.clone())?.evaluate(),526		)?,527		Index(value, index) => {528			match (evaluate(context.clone(), value)?, evaluate(context, index)?) {529				(Val::Obj(v), Val::Str(s)) => {530					let sn = s.clone();531					push_frame(532						CallLocation::new(loc),533						|| format!("field <{}> access", sn),534						|| {535							if let Some(v) = v.get(s.clone())? {536								Ok(v)537							} else {538								throw!(NoSuchField(s))539							}540						},541					)?542				}543				(Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(544					ValType::Obj,545					ValType::Str,546					n.value_type(),547				)),548549				(Val::Arr(v), Val::Num(n)) => {550					if n.fract() > f64::EPSILON {551						throw!(FractionalIndex)552					}553					v.get(n as usize)?554						.ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?555				}556				(Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),557				(Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(558					ValType::Arr,559					ValType::Num,560					n.value_type(),561				)),562563				(Val::Str(s), Val::Num(n)) => Val::Str(564					s.chars()565						.skip(n as usize)566						.take(1)567						.collect::<String>()568						.into(),569				),570				(Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(571					ValType::Str,572					ValType::Num,573					n.value_type(),574				)),575576				(v, _) => throw!(CantIndexInto(v.value_type())),577			}578		}579		LocalExpr(bindings, returned) => {580			let mut new_bindings: GcHashMap<IStr, LazyVal> =581				GcHashMap::with_capacity(bindings.len());582			let future_context = Context::new_future();583			for b in bindings {584				new_bindings.insert(585					b.name.clone(),586					evaluate_binding_in_future(b, future_context.clone()),587				);588			}589			let context = context590				.extend_bound(new_bindings)591				.into_future(future_context);592			evaluate(context, &returned.clone())?593		}594		Arr(items) => {595			let mut out = Vec::with_capacity(items.len());596			for item in items {597				// TODO: Implement ArrValue::Lazy with same context for every element?598				#[derive(Trace)]599				struct ArrayElement {600					context: Context,601					item: LocExpr,602				}603				impl LazyValValue for ArrayElement {604					fn get(self: Box<Self>) -> Result<Val> {605						evaluate(self.context, &self.item)606					}607				}608				out.push(LazyVal::new(TraceBox(Box::new(ArrayElement {609					context: context.clone(),610					item: item.clone(),611				}))));612			}613			Val::Arr(out.into())614		}615		ArrComp(expr, comp_specs) => {616			let mut out = Vec::new();617			evaluate_comp(context, comp_specs, &mut |ctx| {618				out.push(evaluate(ctx, expr)?);619				Ok(())620			})?;621			Val::Arr(ArrValue::Eager(Cc::new(out)))622		}623		Obj(body) => Val::Obj(evaluate_object(context, body)?),624		ObjExtend(s, t) => evaluate_add_op(625			&evaluate(context.clone(), s)?,626			&Val::Obj(evaluate_object(context, t)?),627		)?,628		Apply(value, args, tailstrict) => {629			evaluate_apply(context, value, args, CallLocation::new(loc), *tailstrict)?630		}631		Function(params, body) => {632			evaluate_method(context, "anonymous".into(), params.clone(), body.clone())633		}634		Intrinsic(name) => Val::Func(FuncVal::StaticBuiltin(635			BUILTINS636				.with(|b| b.get(name).copied())637				.ok_or_else(|| IntrinsicNotFound(name.clone()))?,638		)),639		AssertExpr(assert, returned) => {640			evaluate_assert(context.clone(), assert)?;641			evaluate(context, returned)?642		}643		ErrorStmt(e) => push_frame(644			CallLocation::new(loc),645			|| "error statement".to_owned(),646			|| throw!(RuntimeError(IStr::try_from(evaluate(context, e)?)?,)),647		)?,648		IfElse {649			cond,650			cond_then,651			cond_else,652		} => {653			if push_frame(654				CallLocation::new(loc),655				|| "if condition".to_owned(),656				|| bool::try_from(evaluate(context.clone(), &cond.0)?),657			)? {658				evaluate(context, cond_then)?659			} else {660				match cond_else {661					Some(v) => evaluate(context, v)?,662					None => Val::Null,663				}664			}665		}666		Slice(value, desc) => {667			let indexable = evaluate(context.clone(), value)?;668669			fn parse_num(670				context: &Context,671				expr: Option<&LocExpr>,672				desc: &'static str,673			) -> Result<Option<usize>> {674				Ok(match expr {675					Some(s) => evaluate(context.clone(), s)?676						.try_cast_nullable_num(desc)?677						.map(|v| v as usize),678					None => None,679				})680			}681682			let start = parse_num(&context, desc.start.as_ref(), "start")?;683			let end = parse_num(&context, desc.end.as_ref(), "end")?;684			let step = parse_num(&context, desc.step.as_ref(), "step")?;685686			std_slice(indexable.into_indexable()?, start, end, step)?687		}688		Import(path) => {689			let tmp = loc.clone().0;690			let mut import_location = tmp.to_path_buf();691			import_location.pop();692			push_frame(693				CallLocation::new(loc),694				|| format!("import {:?}", path),695				|| with_state(|s| s.import_file(&import_location, path)),696			)?697		}698		ImportStr(path) => {699			let tmp = loc.clone().0;700			let mut import_location = tmp.to_path_buf();701			import_location.pop();702			Val::Str(with_state(|s| s.import_file_str(&import_location, path))?)703		}704	})705}
after · crates/jrsonnet-evaluator/src/evaluate/mod.rs
1use std::convert::TryFrom;23use crate::{4	builtin::{std_slice, BUILTINS},5	error::Error::*,6	evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},7	function::CallLocation,8	gc::TraceBox,9	push_frame, throw, with_state, ArrValue, Bindable, Context, ContextCreator, FuncDesc, FuncVal,10	FutureWrapper, GcHashMap, LazyBinding, LazyVal, LazyValValue, ObjValue, ObjValueBuilder,11	ObjectAssertion, Result, Val,12};13use gcmodule::{Cc, Trace};14use jrsonnet_interner::IStr;15use jrsonnet_parser::{16	ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, FieldMember, ForSpecData, IfSpecData,17	LiteralType, LocExpr, Member, ObjBody, ParamsDesc,18};19use jrsonnet_types::ValType;20pub mod operator;2122pub fn evaluate_binding_in_future(23	b: &BindSpec,24	context_creator: FutureWrapper<Context>,25) -> LazyVal {26	let b = b.clone();27	if let Some(params) = &b.params {28		let params = params.clone();2930		#[derive(Trace)]31		struct LazyMethodBinding {32			context_creator: FutureWrapper<Context>,33			name: IStr,34			params: ParamsDesc,35			value: LocExpr,36		}37		impl LazyValValue for LazyMethodBinding {38			fn get(self: Box<Self>) -> Result<Val> {39				Ok(evaluate_method(40					self.context_creator.unwrap(),41					self.name,42					self.params,43					self.value,44				))45			}46		}4748		LazyVal::new(TraceBox(Box::new(LazyMethodBinding {49			context_creator,50			name: b.name.clone(),51			params,52			value: b.value.clone(),53		})))54	} else {55		#[derive(Trace)]56		struct LazyNamedBinding {57			context_creator: FutureWrapper<Context>,58			name: IStr,59			value: LocExpr,60		}61		impl LazyValValue for LazyNamedBinding {62			fn get(self: Box<Self>) -> Result<Val> {63				evaluate_named(self.context_creator.unwrap(), &self.value, self.name)64			}65		}66		LazyVal::new(TraceBox(Box::new(LazyNamedBinding {67			context_creator,68			name: b.name.clone(),69			value: b.value,70		})))71	}72}7374pub fn evaluate_binding(b: &BindSpec, context_creator: ContextCreator) -> (IStr, LazyBinding) {75	let b = b.clone();76	if let Some(params) = &b.params {77		let params = params.clone();7879		#[derive(Trace)]80		struct BindableMethodLazyVal {81			this: Option<ObjValue>,82			super_obj: Option<ObjValue>,8384			context_creator: ContextCreator,85			name: IStr,86			params: ParamsDesc,87			value: LocExpr,88		}89		impl LazyValValue for BindableMethodLazyVal {90			fn get(self: Box<Self>) -> Result<Val> {91				Ok(evaluate_method(92					self.context_creator.create(self.this, self.super_obj)?,93					self.name,94					self.params,95					self.value,96				))97			}98		}99100		#[derive(Trace)]101		struct BindableMethod {102			context_creator: ContextCreator,103			name: IStr,104			params: ParamsDesc,105			value: LocExpr,106		}107		impl Bindable for BindableMethod {108			fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {109				Ok(LazyVal::new(TraceBox(Box::new(BindableMethodLazyVal {110					this,111					super_obj,112113					context_creator: self.context_creator.clone(),114					name: self.name.clone(),115					params: self.params.clone(),116					value: self.value.clone(),117				}))))118			}119		}120121		(122			b.name.clone(),123			LazyBinding::Bindable(Cc::new(TraceBox(Box::new(BindableMethod {124				context_creator,125				name: b.name.clone(),126				params,127				value: b.value.clone(),128			})))),129		)130	} else {131		#[derive(Trace)]132		struct BindableNamedLazyVal {133			this: Option<ObjValue>,134			super_obj: Option<ObjValue>,135136			context_creator: ContextCreator,137			name: IStr,138			value: LocExpr,139		}140		impl LazyValValue for BindableNamedLazyVal {141			fn get(self: Box<Self>) -> Result<Val> {142				evaluate_named(143					self.context_creator.create(self.this, self.super_obj)?,144					&self.value,145					self.name,146				)147			}148		}149150		#[derive(Trace)]151		struct BindableNamed {152			context_creator: ContextCreator,153			name: IStr,154			value: LocExpr,155		}156		impl Bindable for BindableNamed {157			fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {158				Ok(LazyVal::new(TraceBox(Box::new(BindableNamedLazyVal {159					this,160					super_obj,161162					context_creator: self.context_creator.clone(),163					name: self.name.clone(),164					value: self.value.clone(),165				}))))166			}167		}168169		(170			b.name.clone(),171			LazyBinding::Bindable(Cc::new(TraceBox(Box::new(BindableNamed {172				context_creator,173				name: b.name.clone(),174				value: b.value.clone(),175			})))),176		)177	}178}179180pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {181	Val::Func(FuncVal::Normal(Cc::new(FuncDesc {182		name,183		ctx,184		params,185		body,186	})))187}188189pub fn evaluate_field_name(190	context: Context,191	field_name: &jrsonnet_parser::FieldName,192) -> Result<Option<IStr>> {193	Ok(match field_name {194		jrsonnet_parser::FieldName::Fixed(n) => Some(n.clone()),195		jrsonnet_parser::FieldName::Dyn(expr) => push_frame(196			CallLocation::new(&expr.1),197			|| "evaluating field name".to_string(),198			|| {199				let value = evaluate(context, expr)?;200				if matches!(value, Val::Null) {201					Ok(None)202				} else {203					Ok(Some(IStr::try_from(value)?))204				}205			},206		)?,207	})208}209210pub fn evaluate_comp(211	context: Context,212	specs: &[CompSpec],213	callback: &mut impl FnMut(Context) -> Result<()>,214) -> Result<()> {215	match specs.get(0) {216		None => callback(context)?,217		Some(CompSpec::IfSpec(IfSpecData(cond))) => {218			if bool::try_from(evaluate(context.clone(), cond)?)? {219				evaluate_comp(context, &specs[1..], callback)?220			}221		}222		Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(context.clone(), expr)? {223			Val::Arr(list) => {224				for item in list.iter() {225					evaluate_comp(226						context.clone().with_var(var.clone(), item?.clone()),227						&specs[1..],228						callback,229					)?230				}231			}232			_ => throw!(InComprehensionCanOnlyIterateOverArray),233		},234	}235	Ok(())236}237238pub fn evaluate_member_list_object(context: Context, members: &[Member]) -> Result<ObjValue> {239	let new_bindings = FutureWrapper::new();240	let future_this = FutureWrapper::new();241	let context_creator = ContextCreator(context.clone(), new_bindings.clone());242	{243		let mut bindings: GcHashMap<IStr, LazyBinding> = GcHashMap::with_capacity(members.len());244		for (n, b) in members245			.iter()246			.filter_map(|m| match m {247				Member::BindStmt(b) => Some(b.clone()),248				_ => None,249			})250			.map(|b| evaluate_binding(&b, context_creator.clone()))251		{252			bindings.insert(n, b);253		}254		new_bindings.fill(bindings);255	}256257	let mut builder = ObjValueBuilder::new();258	for member in members.iter() {259		match member {260			Member::Field(FieldMember {261				name,262				plus,263				params: None,264				visibility,265				value,266			}) => {267				let name = evaluate_field_name(context.clone(), name)?;268				if name.is_none() {269					continue;270				}271				let name = name.unwrap();272273				#[derive(Trace)]274				struct ObjMemberBinding {275					context_creator: ContextCreator,276					value: LocExpr,277					name: IStr,278				}279				impl Bindable for ObjMemberBinding {280					fn bind(281						&self,282						this: Option<ObjValue>,283						super_obj: Option<ObjValue>,284					) -> Result<LazyVal> {285						Ok(LazyVal::new_resolved(evaluate_named(286							self.context_creator.create(this, super_obj)?,287							&self.value,288							self.name.clone(),289						)?))290					}291				}292				builder293					.member(name.clone())294					.with_add(*plus)295					.with_visibility(*visibility)296					.with_location(value.1.clone())297					.bindable(TraceBox(Box::new(ObjMemberBinding {298						context_creator: context_creator.clone(),299						value: value.clone(),300						name,301					})));302			}303			Member::Field(FieldMember {304				name,305				params: Some(params),306				value,307				..308			}) => {309				let name = evaluate_field_name(context.clone(), name)?;310				if name.is_none() {311					continue;312				}313				let name = name.unwrap();314				#[derive(Trace)]315				struct ObjMemberBinding {316					context_creator: ContextCreator,317					value: LocExpr,318					params: ParamsDesc,319					name: IStr,320				}321				impl Bindable for ObjMemberBinding {322					fn bind(323						&self,324						this: Option<ObjValue>,325						super_obj: Option<ObjValue>,326					) -> Result<LazyVal> {327						Ok(LazyVal::new_resolved(evaluate_method(328							self.context_creator.create(this, super_obj)?,329							self.name.clone(),330							self.params.clone(),331							self.value.clone(),332						)))333					}334				}335				builder336					.member(name.clone())337					.hide()338					.with_location(value.1.clone())339					.bindable(TraceBox(Box::new(ObjMemberBinding {340						context_creator: context_creator.clone(),341						value: value.clone(),342						params: params.clone(),343						name,344					})));345			}346			Member::BindStmt(_) => {}347			Member::AssertStmt(stmt) => {348				#[derive(Trace)]349				struct ObjectAssert {350					context_creator: ContextCreator,351					assert: AssertStmt,352				}353				impl ObjectAssertion for ObjectAssert {354					fn run(355						&self,356						this: Option<ObjValue>,357						super_obj: Option<ObjValue>,358					) -> Result<()> {359						let ctx = self.context_creator.create(this, super_obj)?;360						evaluate_assert(ctx, &self.assert)361					}362				}363				builder.assert(TraceBox(Box::new(ObjectAssert {364					context_creator: context_creator.clone(),365					assert: stmt.clone(),366				})));367			}368		}369	}370	let this = builder.build();371	future_this.fill(this.clone());372	Ok(this)373}374375pub fn evaluate_object(context: Context, object: &ObjBody) -> Result<ObjValue> {376	Ok(match object {377		ObjBody::MemberList(members) => evaluate_member_list_object(context, members)?,378		ObjBody::ObjComp(obj) => {379			let future_this = FutureWrapper::new();380			let mut builder = ObjValueBuilder::new();381			evaluate_comp(context.clone(), &obj.compspecs, &mut |ctx| {382				let new_bindings = FutureWrapper::new();383				let context_creator = ContextCreator(context.clone(), new_bindings.clone());384				let mut bindings: GcHashMap<IStr, LazyBinding> =385					GcHashMap::with_capacity(obj.pre_locals.len() + obj.post_locals.len());386				for (n, b) in obj387					.pre_locals388					.iter()389					.chain(obj.post_locals.iter())390					.map(|b| evaluate_binding(b, context_creator.clone()))391				{392					bindings.insert(n, b);393				}394				new_bindings.fill(bindings.clone());395				let ctx = ctx.extend_unbound(bindings, None, None, None)?;396				let key = evaluate(ctx.clone(), &obj.key)?;397398				match key {399					Val::Null => {}400					Val::Str(n) => {401						#[derive(Trace)]402						struct ObjCompBinding {403							context: Context,404							value: LocExpr,405						}406						impl Bindable for ObjCompBinding {407							fn bind(408								&self,409								this: Option<ObjValue>,410								_super_obj: Option<ObjValue>,411							) -> Result<LazyVal> {412								Ok(LazyVal::new_resolved(evaluate(413									self.context414										.clone()415										.extend(GcHashMap::new(), None, this, None),416									&self.value,417								)?))418							}419						}420						builder421							.member(n)422							.with_location(obj.value.1.clone())423							.with_add(obj.plus)424							.bindable(TraceBox(Box::new(ObjCompBinding {425								context: ctx,426								value: obj.value.clone(),427							})));428					}429					v => throw!(FieldMustBeStringGot(v.value_type())),430				}431432				Ok(())433			})?;434435			let this = builder.build();436			future_this.fill(this.clone());437			this438		}439	})440}441442pub fn evaluate_apply(443	context: Context,444	value: &LocExpr,445	args: &ArgsDesc,446	loc: CallLocation,447	tailstrict: bool,448) -> Result<Val> {449	let value = evaluate(context.clone(), value)?;450	Ok(match value {451		Val::Func(f) => {452			let body = || f.evaluate(context, loc, args, tailstrict);453			if tailstrict {454				body()?455			} else {456				push_frame(loc, || format!("function <{}> call", f.name()), body)?457			}458		}459		v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),460	})461}462463pub fn evaluate_assert(context: Context, assertion: &AssertStmt) -> Result<()> {464	let value = &assertion.0;465	let msg = &assertion.1;466	let assertion_result = push_frame(467		CallLocation::new(&value.1),468		|| "assertion condition".to_owned(),469		|| bool::try_from(evaluate(context.clone(), value)?),470	)?;471	if !assertion_result {472		push_frame(473			CallLocation::new(&value.1),474			|| "assertion failure".to_owned(),475			|| {476				if let Some(msg) = msg {477					throw!(AssertionFailed(evaluate(context, msg)?.to_string()?));478				} else {479					throw!(AssertionFailed(Val::Null.to_string()?));480				}481			},482		)?483	}484	Ok(())485}486487pub fn evaluate_named(context: Context, lexpr: &LocExpr, name: IStr) -> Result<Val> {488	use Expr::*;489	let LocExpr(expr, _loc) = lexpr;490	Ok(match &**expr {491		Function(params, body) => evaluate_method(context, name, params.clone(), body.clone()),492		_ => evaluate(context, lexpr)?,493	})494}495496pub fn evaluate(context: Context, expr: &LocExpr) -> Result<Val> {497	use Expr::*;498	let LocExpr(expr, loc) = expr;499	// let bp = with_state(|s| s.0.stop_at.borrow().clone());500	Ok(match &**expr {501		Literal(LiteralType::This) => {502			Val::Obj(context.this().clone().ok_or(CantUseSelfOutsideOfObject)?)503		}504		Literal(LiteralType::Super) => Val::Obj(505			context506				.super_obj()507				.clone()508				.ok_or(NoSuperFound)?509				.with_this(context.this().clone().unwrap()),510		),511		Literal(LiteralType::Dollar) => {512			Val::Obj(context.dollar().clone().ok_or(NoTopLevelObjectFound)?)513		}514		Literal(LiteralType::True) => Val::Bool(true),515		Literal(LiteralType::False) => Val::Bool(false),516		Literal(LiteralType::Null) => Val::Null,517		Parened(e) => evaluate(context, e)?,518		Str(v) => Val::Str(v.clone()),519		Num(v) => Val::new_checked_num(*v)?,520		BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, v1, *o, v2)?,521		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,522		Var(name) => push_frame(523			CallLocation::new(loc),524			|| format!("variable <{}> access", name),525			|| context.binding(name.clone())?.evaluate(),526		)?,527		Index(value, index) => {528			match (evaluate(context.clone(), value)?, evaluate(context, index)?) {529				(Val::Obj(v), Val::Str(s)) => {530					let sn = s.clone();531					push_frame(532						CallLocation::new(loc),533						|| format!("field <{}> access", sn),534						|| {535							if let Some(v) = v.get(s.clone())? {536								Ok(v)537							} else {538								throw!(NoSuchField(s))539							}540						},541					)?542				}543				(Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(544					ValType::Obj,545					ValType::Str,546					n.value_type(),547				)),548549				(Val::Arr(v), Val::Num(n)) => {550					if n.fract() > f64::EPSILON {551						throw!(FractionalIndex)552					}553					v.get(n as usize)?554						.ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?555				}556				(Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),557				(Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(558					ValType::Arr,559					ValType::Num,560					n.value_type(),561				)),562563				(Val::Str(s), Val::Num(n)) => Val::Str(564					s.chars()565						.skip(n as usize)566						.take(1)567						.collect::<String>()568						.into(),569				),570				(Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(571					ValType::Str,572					ValType::Num,573					n.value_type(),574				)),575576				(v, _) => throw!(CantIndexInto(v.value_type())),577			}578		}579		LocalExpr(bindings, returned) => {580			let mut new_bindings: GcHashMap<IStr, LazyVal> =581				GcHashMap::with_capacity(bindings.len());582			let future_context = Context::new_future();583			for b in bindings {584				new_bindings.insert(585					b.name.clone(),586					evaluate_binding_in_future(b, future_context.clone()),587				);588			}589			let context = context590				.extend_bound(new_bindings)591				.into_future(future_context);592			evaluate(context, &returned.clone())?593		}594		Arr(items) => {595			let mut out = Vec::with_capacity(items.len());596			for item in items {597				// TODO: Implement ArrValue::Lazy with same context for every element?598				#[derive(Trace)]599				struct ArrayElement {600					context: Context,601					item: LocExpr,602				}603				impl LazyValValue for ArrayElement {604					fn get(self: Box<Self>) -> Result<Val> {605						evaluate(self.context, &self.item)606					}607				}608				out.push(LazyVal::new(TraceBox(Box::new(ArrayElement {609					context: context.clone(),610					item: item.clone(),611				}))));612			}613			Val::Arr(out.into())614		}615		ArrComp(expr, comp_specs) => {616			let mut out = Vec::new();617			evaluate_comp(context, comp_specs, &mut |ctx| {618				out.push(evaluate(ctx, expr)?);619				Ok(())620			})?;621			Val::Arr(ArrValue::Eager(Cc::new(out)))622		}623		Obj(body) => Val::Obj(evaluate_object(context, body)?),624		ObjExtend(s, t) => evaluate_add_op(625			&evaluate(context.clone(), s)?,626			&Val::Obj(evaluate_object(context, t)?),627		)?,628		Apply(value, args, tailstrict) => {629			evaluate_apply(context, value, args, CallLocation::new(loc), *tailstrict)?630		}631		Function(params, body) => {632			evaluate_method(context, "anonymous".into(), params.clone(), body.clone())633		}634		Intrinsic(name) => Val::Func(FuncVal::StaticBuiltin(635			BUILTINS636				.with(|b| b.get(name).copied())637				.ok_or_else(|| IntrinsicNotFound(name.clone()))?,638		)),639		AssertExpr(assert, returned) => {640			evaluate_assert(context.clone(), assert)?;641			evaluate(context, returned)?642		}643		ErrorStmt(e) => push_frame(644			CallLocation::new(loc),645			|| "error statement".to_owned(),646			|| throw!(RuntimeError(IStr::try_from(evaluate(context, e)?)?,)),647		)?,648		IfElse {649			cond,650			cond_then,651			cond_else,652		} => {653			if push_frame(654				CallLocation::new(loc),655				|| "if condition".to_owned(),656				|| bool::try_from(evaluate(context.clone(), &cond.0)?),657			)? {658				evaluate(context, cond_then)?659			} else {660				match cond_else {661					Some(v) => evaluate(context, v)?,662					None => Val::Null,663				}664			}665		}666		Slice(value, desc) => {667			let indexable = evaluate(context.clone(), value)?;668669			fn parse_num(670				context: &Context,671				expr: Option<&LocExpr>,672				desc: &'static str,673			) -> Result<Option<usize>> {674				Ok(match expr {675					Some(s) => evaluate(context.clone(), s)?676						.try_cast_nullable_num(desc)?677						.map(|v| v as usize),678					None => None,679				})680			}681682			let start = parse_num(&context, desc.start.as_ref(), "start")?;683			let end = parse_num(&context, desc.end.as_ref(), "end")?;684			let step = parse_num(&context, desc.step.as_ref(), "step")?;685686			std_slice(indexable.into_indexable()?, start, end, step)?687		}688		Import(path) => {689			let tmp = loc.clone().0;690			let mut import_location = tmp.to_path_buf();691			import_location.pop();692			push_frame(693				CallLocation::new(loc),694				|| format!("import {:?}", path),695				|| with_state(|s| s.import_file(&import_location, path)),696			)?697		}698		ImportStr(path) => {699			let tmp = loc.clone().0;700			let mut import_location = tmp.to_path_buf();701			import_location.pop();702			Val::Str(with_state(|s| s.import_file_str(&import_location, path))?)703		}704		ImportBin(path) => {705			let tmp = loc.clone().0;706			let mut import_location = tmp.to_path_buf();707			import_location.pop();708			let bytes = with_state(|s| s.import_file_bin(&import_location, path))?;709			Val::Arr(ArrValue::Bytes(bytes))710		}711	})712}
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
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -174,6 +174,7 @@
 #[derive(Debug, Clone, Trace)]
 #[force_tracking]
 pub enum ArrValue {
+	Bytes(#[skip_trace] Rc<[u8]>),
 	Lazy(Cc<Vec<LazyVal>>),
 	Eager(Cc<Vec<Val>>),
 	Extended(Box<(Self, Self)>),
@@ -185,6 +186,7 @@
 
 	pub fn len(&self) -> usize {
 		match self {
+			Self::Bytes(i) => i.len(),
 			Self::Lazy(l) => l.len(),
 			Self::Eager(e) => e.len(),
 			Self::Extended(v) => v.0.len() + v.1.len(),
@@ -197,6 +199,9 @@
 
 	pub fn get(&self, index: usize) -> Result<Option<Val>> {
 		match self {
+			Self::Bytes(i) => i
+				.get(index)
+				.map_or(Ok(None), |v| Ok(Some(Val::Num(*v as f64)))),
 			Self::Lazy(vec) => {
 				if let Some(v) = vec.get(index) {
 					Ok(Some(v.evaluate()?))
@@ -218,6 +223,9 @@
 
 	pub fn get_lazy(&self, index: usize) -> Option<LazyVal> {
 		match self {
+			Self::Bytes(i) => i
+				.get(index)
+				.map(|b| LazyVal::new_resolved(Val::Num(*b as f64))),
 			Self::Lazy(vec) => vec.get(index).cloned(),
 			Self::Eager(vec) => vec.get(index).cloned().map(LazyVal::new_resolved),
 			Self::Extended(v) => {
@@ -233,6 +241,13 @@
 
 	pub fn evaluated(&self) -> Result<Cc<Vec<Val>>> {
 		Ok(match self {
+			Self::Bytes(i) => {
+				let mut out = Vec::with_capacity(i.len());
+				for v in i.iter() {
+					out.push(Val::Num(*v as f64));
+				}
+				Cc::new(out)
+			}
 			Self::Lazy(vec) => {
 				let mut out = Vec::with_capacity(vec.len());
 				for item in vec.iter() {
@@ -253,6 +268,7 @@
 
 	pub fn iter(&self) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {
 		(0..self.len()).map(move |idx| match self {
+			Self::Bytes(b) => Ok(Val::Num(b[idx] as f64)),
 			Self::Lazy(l) => l[idx].evaluate(),
 			Self::Eager(e) => Ok(e[idx].clone()),
 			Self::Extended(_) => self.get(idx).map(|e| e.unwrap()),
@@ -261,6 +277,7 @@
 
 	pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = LazyVal> + '_ {
 		(0..self.len()).map(move |idx| match self {
+			Self::Bytes(b) => LazyVal::new_resolved(Val::Num(b[idx] as f64)),
 			Self::Lazy(l) => l[idx].clone(),
 			Self::Eager(e) => LazyVal::new_resolved(e[idx].clone()),
 			Self::Extended(_) => self.get_lazy(idx).unwrap(),
@@ -269,6 +286,11 @@
 
 	pub fn reversed(self) -> Self {
 		match self {
+			Self::Bytes(b) => {
+				let mut out = b.to_vec();
+				out.reverse();
+				Self::Bytes(out.into())
+			}
 			Self::Lazy(vec) => {
 				let mut out = (&vec as &Vec<_>).clone();
 				out.reverse();
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)