git.delta.rocks / jrsonnet / refs/commits / 4ad9956e5f9b

difftreelog

refactor keep source code alongside source path

Yaroslav Bolyukin2022-08-05parent: #68c8ac0.patch.diff
in: master

18 files changed

modifiedbindings/jsonnet/src/import.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/import.rs
+++ b/bindings/jsonnet/src/import.rs
@@ -16,6 +16,7 @@
 	error::{Error::*, Result},
 	throw, ImportResolver, State,
 };
+use jrsonnet_parser::SourcePath;
 
 pub type JsonnetImportCallback = unsafe extern "C" fn(
 	ctx: *mut c_void,
@@ -29,10 +30,10 @@
 pub struct CallbackImportResolver {
 	cb: JsonnetImportCallback,
 	ctx: *mut c_void,
-	out: RefCell<HashMap<PathBuf, Vec<u8>>>,
+	out: RefCell<HashMap<SourcePath, Vec<u8>>>,
 }
 impl ImportResolver for CallbackImportResolver {
-	fn resolve_file(&self, from: &Path, path: &str) -> Result<PathBuf> {
+	fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {
 		let base = CString::new(from.to_str().unwrap()).unwrap().into_raw();
 		let rel = CString::new(path).unwrap().into_raw();
 		let found_here: *mut c_char = null_mut();
@@ -61,7 +62,7 @@
 		}
 
 		let found_here_raw = unsafe { CStr::from_ptr(found_here) };
-		let found_here_buf = PathBuf::from(found_here_raw.to_str().unwrap());
+		let found_here_buf = SourcePath::Path(PathBuf::from(found_here_raw.to_str().unwrap()));
 		unsafe {
 			let _ = CString::from_raw(found_here);
 		}
@@ -74,7 +75,7 @@
 
 		Ok(found_here_buf)
 	}
-	fn load_file_contents(&self, resolved: &Path) -> Result<Vec<u8>> {
+	fn load_file_contents(&self, resolved: &SourcePath) -> Result<Vec<u8>> {
 		Ok(self.out.borrow().get(resolved).unwrap().clone())
 	}
 
@@ -108,24 +109,28 @@
 	}
 }
 impl ImportResolver for NativeImportResolver {
-	fn resolve_file(&self, from: &Path, path: &str) -> Result<PathBuf> {
+	fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {
 		let mut new_path = from.to_owned();
 		new_path.push(path);
 		if new_path.exists() {
-			Ok(new_path)
+			Ok(SourcePath::Path(new_path))
 		} else {
 			for library_path in self.library_paths.borrow().iter() {
 				let mut cloned = library_path.clone();
 				cloned.push(path);
 				if cloned.exists() {
-					return Ok(cloned);
+					return Ok(SourcePath::Path(cloned));
 				}
 			}
 			throw!(ImportFileNotFound(from.to_owned(), path.to_owned()))
 		}
 	}
-	fn load_file_contents(&self, id: &Path) -> Result<Vec<u8>> {
-		let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.to_owned()))?;
+	fn load_file_contents(&self, id: &SourcePath) -> Result<Vec<u8>> {
+		let path = match id {
+			SourcePath::Path(path) => path,
+			_ => unreachable!("NativeImportResolver::resolve_file may only return plain paths"),
+		};
+		let mut file = File::open(path).map_err(|_e| ResolvedFileNotFound(id.clone()))?;
 		let mut out = Vec::new();
 		file.read_to_end(&mut out)
 			.map_err(|e| ImportIo(e.to_string()))?;
modifiedbindings/jsonnet/src/lib.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -10,9 +10,9 @@
 
 use std::{
 	alloc::Layout,
+	env,
 	ffi::{CStr, CString},
 	os::raw::{c_char, c_double, c_int, c_uint},
-	path::PathBuf,
 };
 
 use import::NativeImportResolver;
@@ -112,7 +112,10 @@
 ) -> *const c_char {
 	let filename = CStr::from_ptr(filename);
 	match vm
-		.import(PathBuf::from(filename.to_str().unwrap()))
+		.import(
+			&env::current_dir().expect("cwd"),
+			filename.to_str().unwrap(),
+		)
 		.and_then(|v| vm.with_tla(v))
 		.and_then(|v| vm.manifest(v))
 	{
@@ -141,10 +144,7 @@
 	let filename = CStr::from_ptr(filename);
 	let snippet = CStr::from_ptr(snippet);
 	match vm
-		.evaluate_snippet(
-			filename.to_str().unwrap().into(),
-			snippet.to_str().unwrap().into(),
-		)
+		.evaluate_snippet(filename.to_str().unwrap().into(), snippet.to_str().unwrap())
 		.and_then(|v| vm.with_tla(v))
 		.and_then(|v| vm.manifest(v))
 	{
@@ -186,7 +186,10 @@
 ) -> *const c_char {
 	let filename = CStr::from_ptr(filename);
 	match vm
-		.import(PathBuf::from(filename.to_str().unwrap()))
+		.import(
+			&env::current_dir().expect("cwd"),
+			filename.to_str().unwrap(),
+		)
 		.and_then(|v| vm.with_tla(v))
 		.and_then(|v| vm.manifest_multi(v))
 	{
@@ -213,10 +216,7 @@
 	let filename = CStr::from_ptr(filename);
 	let snippet = CStr::from_ptr(snippet);
 	match vm
-		.evaluate_snippet(
-			filename.to_str().unwrap().into(),
-			snippet.to_str().unwrap().into(),
-		)
+		.evaluate_snippet(filename.to_str().unwrap().into(), snippet.to_str().unwrap())
 		.and_then(|v| vm.with_tla(v))
 		.and_then(|v| vm.manifest_multi(v))
 	{
@@ -256,7 +256,10 @@
 ) -> *const c_char {
 	let filename = CStr::from_ptr(filename);
 	match vm
-		.import(PathBuf::from(filename.to_str().unwrap()))
+		.import(
+			&env::current_dir().expect("cwd"),
+			filename.to_str().unwrap(),
+		)
 		.and_then(|v| vm.with_tla(v))
 		.and_then(|v| vm.manifest_stream(v))
 	{
@@ -283,10 +286,7 @@
 	let filename = CStr::from_ptr(filename);
 	let snippet = CStr::from_ptr(snippet);
 	match vm
-		.evaluate_snippet(
-			filename.to_str().unwrap().into(),
-			snippet.to_str().unwrap().into(),
-		)
+		.evaluate_snippet(filename.to_str().unwrap().into(), snippet.to_str().unwrap())
 		.and_then(|v| vm.with_tla(v))
 		.and_then(|v| vm.manifest_stream(v))
 	{
modifiedbindings/jsonnet/src/vars_tlas.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/vars_tlas.rs
+++ b/bindings/jsonnet/src/vars_tlas.rs
@@ -32,7 +32,7 @@
 		.as_any()
 		.downcast_ref::<jrsonnet_stdlib::ContextInitializer>()
 		.expect("only stdlib context initializer supported")
-		.add_ext_code(name.to_str().unwrap(), value.to_str().unwrap().into())
+		.add_ext_code(name.to_str().unwrap(), value.to_str().unwrap())
 		.unwrap()
 }
 /// # Safety
modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -133,14 +133,14 @@
 
 	let input = opts.input.input.ok_or(Error::MissingInputArgument)?;
 	let val = if opts.input.exec {
-		s.evaluate_snippet("<cmdline>".to_owned(), (&input as &str).into())?
+		s.evaluate_snippet("<cmdline>".to_owned(), &input as &str)?
 	} else if input == "-" {
 		let mut input = Vec::new();
 		std::io::stdin().read_to_end(&mut input)?;
-		let input_str = std::str::from_utf8(&input)?.into();
+		let input_str = std::str::from_utf8(&input)?;
 		s.evaluate_snippet("<stdin>".to_owned(), input_str)?
 	} else {
-		s.import(s.resolve_file(&current_dir().expect("cwd"), &input)?)?
+		s.import(&current_dir().expect("cwd"), &input)?
 	};
 
 	let val = s.with_tla(val)?;
modifiedcrates/jrsonnet-cli/src/stdlib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/stdlib.rs
+++ b/crates/jrsonnet-cli/src/stdlib.rs
@@ -118,10 +118,10 @@
 			ctx.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());
 		}
 		for ext in self.ext_code.iter() {
-			ctx.add_ext_code(&ext.name as &str, (&ext.value as &str).into())?;
+			ctx.add_ext_code(&ext.name as &str, &ext.value as &str)?;
 		}
 		for ext in self.ext_code_file.iter() {
-			ctx.add_ext_code(&ext.name as &str, (&ext.value as &str).into())?;
+			ctx.add_ext_code(&ext.name as &str, &ext.value as &str)?;
 		}
 		s.settings_mut().context_initializer = Box::new(ctx);
 		Ok(())
modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -2,7 +2,7 @@
 
 use jrsonnet_gcmodule::Trace;
 use jrsonnet_interner::IStr;
-use jrsonnet_parser::{BinaryOpType, ExprLocation, Source, UnaryOpType};
+use jrsonnet_parser::{BinaryOpType, ExprLocation, Source, SourcePath, UnaryOpType};
 use jrsonnet_types::ValType;
 use thiserror::Error;
 
@@ -138,10 +138,10 @@
 
 	#[error("can't resolve {1} from {0}")]
 	ImportFileNotFound(PathBuf, String),
-	#[error("resolved file not found: {0}")]
-	ResolvedFileNotFound(PathBuf),
+	#[error("resolved file not found: {:?}", .0)]
+	ResolvedFileNotFound(SourcePath),
 	#[error("imported file is not valid utf-8: {0:?}")]
-	ImportBadFileUtf8(PathBuf),
+	ImportBadFileUtf8(SourcePath),
 	#[error("import io error: {0}")]
 	ImportIo(String),
 	#[error("tried to import {1} from {0}, but imports is not supported")]
@@ -151,12 +151,11 @@
 	#[error(
 		"syntax error: expected {}, got {:?}",
 		.error.expected,
-		.source_code.chars().nth(error.location.offset)
+		.path.code().chars().nth(error.location.offset)
 		.map_or_else(|| "EOF".into(), |c| c.to_string())
 	)]
 	ImportSyntaxError {
 		path: Source,
-		source_code: IStr,
 		#[trace(skip)]
 		error: Box<jrsonnet_parser::ParseError>,
 	},
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -416,13 +416,13 @@
 		Literal(LiteralType::This) => {
 			Val::Obj(ctx.this().clone().ok_or(CantUseSelfOutsideOfObject)?)
 		}
-		Literal(LiteralType::Super) => {
-			Val::Obj(ctx.super_obj().clone().ok_or(NoSuperFound)?.with_this(
+		Literal(LiteralType::Super) => Val::Obj(
+			ctx.super_obj().clone().ok_or(NoSuperFound)?.with_this(
 				ctx.this()
 					.clone()
 					.expect("if super exists - then this should to"),
-			))
-		}
+			),
+		),
 		Literal(LiteralType::Dollar) => {
 			Val::Obj(ctx.dollar().clone().ok_or(NoTopLevelObjectFound)?)
 		}
@@ -643,10 +643,10 @@
 				Import(_) => s.push(
 					CallLocation::new(loc),
 					|| format!("import {:?}", path.clone()),
-					|| s.import(resolved_path.clone()),
+					|| s.import_resolved(resolved_path),
 				)?,
-				ImportStr(_) => Val::Str(s.import_str(resolved_path)?),
-				ImportBin(_) => Val::Arr(ArrValue::Bytes(s.import_bin(resolved_path)?)),
+				ImportStr(_) => Val::Str(s.import_resolved_str(resolved_path)?),
+				ImportBin(_) => Val::Arr(ArrValue::Bytes(s.import_resolved_bin(resolved_path)?)),
 				_ => unreachable!(),
 			}
 		}
modifiedcrates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -6,6 +6,7 @@
 };
 
 use fs::File;
+use jrsonnet_parser::SourcePath;
 
 use crate::{
 	error::{Error::*, Result},
@@ -17,9 +18,12 @@
 	/// Resolves real file path, e.g. `(/home/user/manifests, b.libjsonnet)` can correspond
 	/// both to `/home/user/manifests/b.libjsonnet` and to `/home/user/${vendor}/b.libjsonnet`
 	/// where `${vendor}` is a library path.
-	fn resolve_file(&self, from: &Path, path: &str) -> Result<PathBuf>;
+	fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath>;
 
-	fn load_file_contents(&self, resolved: &Path) -> Result<Vec<u8>>;
+	/// Load resolved file
+	/// This should only be called with value returned from `resolve_file`, this cannot be resolved using associated type,
+	/// as evaluator uses object instead of generic for [`ImportResolver`]
+	fn load_file_contents(&self, resolved: &SourcePath) -> Result<Vec<u8>>;
 
 	/// # Safety
 	///
@@ -32,11 +36,11 @@
 /// Dummy resolver, can't resolve/load any file
 pub struct DummyImportResolver;
 impl ImportResolver for DummyImportResolver {
-	fn resolve_file(&self, from: &Path, path: &str) -> Result<PathBuf> {
+	fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {
 		throw!(ImportNotSupported(from.into(), path.into()))
 	}
 
-	fn load_file_contents(&self, _resolved: &Path) -> Result<Vec<u8>> {
+	fn load_file_contents(&self, _resolved: &SourcePath) -> Result<Vec<u8>> {
 		panic!("dummy resolver can't load any file")
 	}
 
@@ -59,25 +63,35 @@
 	pub library_paths: Vec<PathBuf>,
 }
 impl ImportResolver for FileImportResolver {
-	fn resolve_file(&self, from: &Path, path: &str) -> Result<PathBuf> {
+	fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {
 		let mut direct = from.to_path_buf();
 		direct.push(path);
 		if direct.exists() {
-			Ok(direct.canonicalize().map_err(|e| ImportIo(e.to_string()))?)
+			Ok(SourcePath::Path(
+				direct.canonicalize().map_err(|e| ImportIo(e.to_string()))?,
+			))
 		} else {
 			for library_path in &self.library_paths {
 				let mut cloned = library_path.clone();
 				cloned.push(path);
 				if cloned.exists() {
-					return Ok(cloned.canonicalize().map_err(|e| ImportIo(e.to_string()))?);
+					return Ok(SourcePath::Path(
+						cloned.canonicalize().map_err(|e| ImportIo(e.to_string()))?,
+					));
 				}
 			}
 			throw!(ImportFileNotFound(from.to_owned(), path.to_owned()))
 		}
 	}
 
-	fn load_file_contents(&self, id: &Path) -> Result<Vec<u8>> {
-		let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.to_owned()))?;
+	fn load_file_contents(&self, id: &SourcePath) -> Result<Vec<u8>> {
+		let path = match id {
+			SourcePath::Path(path) => path,
+			_ => {
+				panic!("this resolver can only resolve to path")
+			}
+		};
+		let mut file = File::open(path).map_err(|_e| ResolvedFileNotFound(id.clone()))?;
 		let mut out = Vec::new();
 		file.read_to_end(&mut out)
 			.map_err(|e| ImportIo(e.to_string()))?;
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/lib.rs
1#![warn(clippy::all, clippy::nursery, clippy::pedantic)]2#![allow(3	macro_expanded_macro_exports_accessed_by_absolute_paths,4	clippy::ptr_arg,5	// Too verbose6	clippy::must_use_candidate,7	// A lot of functions pass around errors thrown by code8	clippy::missing_errors_doc,9	// A lot of pointers have interior Rc10	clippy::needless_pass_by_value,11	// Its fine12	clippy::wildcard_imports,13	clippy::enum_glob_use,14	clippy::module_name_repetitions,15	// TODO: fix individual issues, however this works as intended almost everywhere16	clippy::cast_precision_loss,17	clippy::cast_possible_wrap,18	clippy::cast_possible_truncation,19	clippy::cast_sign_loss,20	// False positives21	// https://github.com/rust-lang/rust-clippy/issues/690222	clippy::use_self,23	// https://github.com/rust-lang/rust-clippy/issues/853924	clippy::iter_with_drain,25)]2627// For jrsonnet-macros28extern crate self as jrsonnet_evaluator;2930mod ctx;31mod dynamic;32pub mod error;33mod evaluate;34pub mod function;35pub mod gc;36mod import;37mod integrations;38mod map;39mod obj;40pub mod stdlib;41pub mod trace;42pub mod typed;43pub mod val;4445use std::{46	any::Any,47	borrow::Cow,48	cell::{Ref, RefCell, RefMut},49	collections::HashMap,50	fmt::{self, Debug},51	path::{Path, PathBuf},52	rc::Rc,53};5455pub use ctx::*;56pub use dynamic::*;57use error::{Error::*, LocError, Result, StackTraceElement};58pub use evaluate::*;59use function::{CallLocation, TlaArg};60use gc::{GcHashMap, TraceBox};61use hashbrown::hash_map::RawEntryMut;62pub use import::*;63use jrsonnet_gcmodule::{Cc, Trace};64pub use jrsonnet_interner::{IBytes, IStr};65pub use jrsonnet_parser as parser;66use jrsonnet_parser::*;67pub use obj::*;68use trace::{location_to_offset, offset_to_location, CodeLocation, CompactFormat, TraceFormat};69pub use val::{ManifestFormat, Thunk, Val};7071pub trait Unbound: Trace {72	type Bound;73	fn bind(&self, s: State, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Self::Bound>;74}7576#[derive(Clone, Trace)]77pub enum LazyBinding {78	Bindable(Cc<TraceBox<dyn Unbound<Bound = Thunk<Val>>>>),79	Bound(Thunk<Val>),80}8182impl Debug for LazyBinding {83	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {84		write!(f, "LazyBinding")85	}86}87impl LazyBinding {88	pub fn evaluate(89		&self,90		s: State,91		sup: Option<ObjValue>,92		this: Option<ObjValue>,93	) -> Result<Thunk<Val>> {94		match self {95			Self::Bindable(v) => v.bind(s, sup, this),96			Self::Bound(v) => Ok(v.clone()),97		}98	}99}100101/// During import, this trait will be called to create initial context for file102/// It may initialize global variables, stdlib for example103pub trait ContextInitializer {104	fn initialize(&self, state: State, for_file: Source) -> Context;105106	/// # Safety107	///108	/// For use only in bindings, should not be used elsewhere.109	/// Implementations which are not intended to be used in bindings110	/// should panic on call to this method.111	unsafe fn as_any(&self) -> &dyn Any;112}113114/// Context initializer, which adds noth115pub struct DummyContextInitializer;116impl ContextInitializer for DummyContextInitializer {117	fn initialize(&self, _state: State, _for_file: Source) -> Context {118		Context::default()119	}120	unsafe fn as_any(&self) -> &dyn Any {121		panic!("`as_any(&self)` is not supported by dummy initializer")122	}123}124125pub struct EvaluationSettings {126	/// Limits recursion by limiting the number of stack frames127	pub max_stack: usize,128	/// Limits amount of stack trace items preserved129	pub max_trace: usize,130	/// TLA vars131	pub tla_vars: HashMap<IStr, TlaArg>,132	/// Context initializer, which will be used for imports and everything133	/// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`134	pub context_initializer: Box<dyn ContextInitializer>,135	/// Used to resolve file locations/contents136	pub import_resolver: Box<dyn ImportResolver>,137	/// Used in manifestification functions138	pub manifest_format: ManifestFormat,139	/// Used for bindings140	pub trace_format: Box<dyn TraceFormat>,141}142impl Default for EvaluationSettings {143	fn default() -> Self {144		Self {145			max_stack: 200,146			max_trace: 20,147			context_initializer: Box::new(DummyContextInitializer),148			tla_vars: HashMap::default(),149			import_resolver: Box::new(DummyImportResolver),150			manifest_format: ManifestFormat::Json {151				padding: 4,152				#[cfg(feature = "exp-preserve-order")]153				preserve_order: false,154			},155			trace_format: Box::new(CompactFormat {156				padding: 4,157				resolver: trace::PathResolver::Absolute,158			}),159		}160	}161}162163#[derive(Default)]164struct EvaluationData {165	/// Used for stack overflow detection, stacktrace is populated on unwind166	stack_depth: usize,167	/// Updated every time stack entry is popt168	stack_generation: usize,169170	breakpoints: Breakpoints,171172	/// Contains file source codes and evaluation results for imports and pretty-printed stacktraces173	files: GcHashMap<PathBuf, FileData>,174	/// Contains tla arguments and others, which aren't needed to be obtained by name, however may be used for receiving source175	/// TODO: look into nix approach, storing source code in `Source` object176	volatile_files: GcHashMap<String, String>,177}178struct FileData {179	string: Option<IStr>,180	bytes: Option<IBytes>,181	parsed: Option<LocExpr>,182	evaluated: Option<Val>,183184	evaluating: bool,185}186impl FileData {187	fn new_string(data: IStr) -> Self {188		Self {189			string: Some(data),190			bytes: None,191			parsed: None,192			evaluated: None,193			evaluating: false,194		}195	}196	fn new_bytes(data: IBytes) -> Self {197		Self {198			string: None,199			bytes: Some(data),200			parsed: None,201			evaluated: None,202			evaluating: false,203		}204	}205}206207#[allow(clippy::type_complexity)]208pub struct Breakpoint {209	loc: ExprLocation,210	collected: RefCell<HashMap<usize, (usize, Vec<Result<Val>>)>>,211}212#[derive(Default)]213struct Breakpoints(Vec<Rc<Breakpoint>>);214impl Breakpoints {215	fn insert(216		&self,217		stack_depth: usize,218		stack_generation: usize,219		loc: &ExprLocation,220		result: Result<Val>,221	) -> Result<Val> {222		if self.0.is_empty() {223			return result;224		}225		for item in &self.0 {226			if item.loc.belongs_to(loc) {227				let mut collected = item.collected.borrow_mut();228				let (depth, vals) = collected.entry(stack_generation).or_default();229				if stack_depth > *depth {230					vals.clear();231				}232				vals.push(result.clone());233			}234		}235		result236	}237}238239#[derive(Default)]240pub struct EvaluationStateInternals {241	/// Internal state242	data: RefCell<EvaluationData>,243	/// Settings, safe to change at runtime244	settings: RefCell<EvaluationSettings>,245}246247/// Maintains stack trace and import resolution248#[derive(Default, Clone)]249pub struct State(Rc<EvaluationStateInternals>);250251impl State {252	pub fn import_str(&self, path: PathBuf) -> Result<IStr> {253		let mut data = self.data_mut();254		let mut file = data.files.raw_entry_mut().from_key(&path);255256		let file = match file {257			RawEntryMut::Occupied(ref mut d) => d.get_mut(),258			RawEntryMut::Vacant(v) => {259				let data = self.settings().import_resolver.load_file_contents(&path)?;260				v.insert(261					path.clone(),262					FileData::new_string(263						std::str::from_utf8(&data)264							.map_err(|_| ImportBadFileUtf8(path.clone()))?265							.into(),266					),267				)268				.1269			}270		};271		if let Some(str) = &file.string {272			return Ok(str.clone());273		}274		if file.string.is_none() {275			file.string = Some(276				file.bytes277					.as_ref()278					.expect("either string or bytes should be set")279					.clone()280					.cast_str()281					.ok_or_else(|| ImportBadFileUtf8(path.clone()))?,282			);283		}284		Ok(file.string.as_ref().expect("just set").clone())285	}286	pub fn import_bin(&self, path: PathBuf) -> Result<IBytes> {287		let mut data = self.data_mut();288		let mut file = data.files.raw_entry_mut().from_key(&path);289290		let file = match file {291			RawEntryMut::Occupied(ref mut d) => d.get_mut(),292			RawEntryMut::Vacant(v) => {293				let data = self.settings().import_resolver.load_file_contents(&path)?;294				v.insert(path.clone(), FileData::new_bytes(data.as_slice().into()))295					.1296			}297		};298		if let Some(str) = &file.bytes {299			return Ok(str.clone());300		}301		if file.bytes.is_none() {302			file.bytes = Some(303				file.string304					.as_ref()305					.expect("either string or bytes should be set")306					.clone()307					.cast_bytes(),308			);309		}310		Ok(file.bytes.as_ref().expect("just set").clone())311	}312	pub fn import(&self, path: PathBuf) -> Result<Val> {313		let mut data = self.data_mut();314		let mut file = data.files.raw_entry_mut().from_key(&path);315316		let file = match file {317			RawEntryMut::Occupied(ref mut d) => d.get_mut(),318			RawEntryMut::Vacant(v) => {319				let data = self.settings().import_resolver.load_file_contents(&path)?;320				v.insert(321					path.clone(),322					FileData::new_string(323						std::str::from_utf8(&data)324							.map_err(|_| ImportBadFileUtf8(path.clone()))?325							.into(),326					),327				)328				.1329			}330		};331		if let Some(val) = &file.evaluated {332			return Ok(val.clone());333		}334		if file.string.is_none() {335			file.string = Some(336				std::str::from_utf8(337					file.bytes338						.as_ref()339						.expect("either string or bytes should be set"),340				)341				.map_err(|_| ImportBadFileUtf8(path.clone()))?342				.into(),343			);344		}345		let code = file.string.as_ref().expect("just set");346		let file_name = Source::new(path.clone()).expect("resolver should return correct name");347		if file.parsed.is_none() {348			file.parsed = Some(349				jrsonnet_parser::parse(350					code,351					&ParserSettings {352						file_name: file_name.clone(),353					},354				)355				.map_err(|e| ImportSyntaxError {356					path: file_name.clone(),357					source_code: code.clone(),358					error: Box::new(e),359				})?,360			);361		}362		let parsed = file.parsed.as_ref().expect("just set").clone();363		if file.evaluating {364			throw!(InfiniteRecursionDetected)365		}366		file.evaluating = true;367		// Dropping file here, as it borrows data, which may be used in evaluation368		drop(data);369		let res = evaluate(370			self.clone(),371			self.create_default_context(file_name),372			&parsed,373		);374375		let mut data = self.data_mut();376		let mut file = data.files.raw_entry_mut().from_key(&path);377378		let file = match file {379			RawEntryMut::Occupied(ref mut d) => d.get_mut(),380			RawEntryMut::Vacant(_) => unreachable!("this file was just here!"),381		};382		file.evaluating = false;383		match res {384			Ok(v) => {385				file.evaluated = Some(v.clone());386				Ok(v)387			}388			Err(e) => Err(e),389		}390	}391392	pub fn get_source(&self, name: Source) -> Option<String> {393		let data = self.data();394		match name.repr() {395			Ok(real) => data396				.files397				.get(real)398				.and_then(|f| f.string.as_ref())399				.map(ToString::to_string),400			Err(e) => data.volatile_files.get(e).map(ToOwned::to_owned),401		}402	}403	pub fn map_source_locations(&self, file: Source, locs: &[u32]) -> Vec<CodeLocation> {404		offset_to_location(&self.get_source(file).unwrap_or_else(|| "".into()), locs)405	}406	pub fn map_from_source_location(407		&self,408		file: Source,409		line: usize,410		column: usize,411	) -> Option<usize> {412		location_to_offset(413			&self.get_source(file).expect("file not found"),414			line,415			column,416		)417	}418419	/// Creates context with all passed global variables420	pub fn create_default_context(&self, source: Source) -> Context {421		let context_initializer = &self.settings().context_initializer;422		context_initializer.initialize(self.clone(), source)423	}424425	/// Executes code creating a new stack frame426	pub fn push<T>(427		&self,428		e: CallLocation,429		frame_desc: impl FnOnce() -> String,430		f: impl FnOnce() -> Result<T>,431	) -> Result<T> {432		{433			let mut data = self.data_mut();434			let stack_depth = &mut data.stack_depth;435			if *stack_depth > self.max_stack() {436				// Error creation uses data, so i drop guard here437				drop(data);438				throw!(StackOverflow);439			}440			*stack_depth += 1;441		}442		let result = f();443		{444			let mut data = self.data_mut();445			data.stack_depth -= 1;446			data.stack_generation += 1;447		}448		if let Err(mut err) = result {449			err.trace_mut().0.push(StackTraceElement {450				location: e.0.cloned(),451				desc: frame_desc(),452			});453			return Err(err);454		}455		result456	}457458	/// Executes code creating a new stack frame459	pub fn push_val(460		&self,461		e: &ExprLocation,462		frame_desc: impl FnOnce() -> String,463		f: impl FnOnce() -> Result<Val>,464	) -> Result<Val> {465		{466			let mut data = self.data_mut();467			let stack_depth = &mut data.stack_depth;468			if *stack_depth > self.max_stack() {469				// Error creation uses data, so i drop guard here470				drop(data);471				throw!(StackOverflow);472			}473			*stack_depth += 1;474		}475		let mut result = f();476		{477			let mut data = self.data_mut();478			data.stack_depth -= 1;479			data.stack_generation += 1;480			result = data481				.breakpoints482				.insert(data.stack_depth, data.stack_generation, e, result);483		}484		if let Err(mut err) = result {485			err.trace_mut().0.push(StackTraceElement {486				location: Some(e.clone()),487				desc: frame_desc(),488			});489			return Err(err);490		}491		result492	}493	/// Executes code creating a new stack frame494	pub fn push_description<T>(495		&self,496		frame_desc: impl FnOnce() -> String,497		f: impl FnOnce() -> Result<T>,498	) -> Result<T> {499		{500			let mut data = self.data_mut();501			let stack_depth = &mut data.stack_depth;502			if *stack_depth > self.max_stack() {503				// Error creation uses data, so i drop guard here504				drop(data);505				throw!(StackOverflow);506			}507			*stack_depth += 1;508		}509		let result = f();510		{511			let mut data = self.data_mut();512			data.stack_depth -= 1;513			data.stack_generation += 1;514		}515		if let Err(mut err) = result {516			err.trace_mut().0.push(StackTraceElement {517				location: None,518				desc: frame_desc(),519			});520			return Err(err);521		}522		result523	}524525	/// # Panics526	/// In case of formatting failure527	pub fn stringify_err(&self, e: &LocError) -> String {528		let mut out = String::new();529		self.settings()530			.trace_format531			.write_trace(&mut out, self, e)532			.unwrap();533		out534	}535536	pub fn manifest(&self, val: Val) -> Result<IStr> {537		self.push_description(538			|| "manifestification".to_string(),539			|| val.manifest(self.clone(), &self.manifest_format()),540		)541	}542	pub fn manifest_multi(&self, val: Val) -> Result<Vec<(IStr, IStr)>> {543		val.manifest_multi(self.clone(), &self.manifest_format())544	}545	pub fn manifest_stream(&self, val: Val) -> Result<Vec<IStr>> {546		val.manifest_stream(self.clone(), &self.manifest_format())547	}548549	/// If passed value is function then call with set TLA550	pub fn with_tla(&self, val: Val) -> Result<Val> {551		Ok(match val {552			Val::Func(func) => self.push_description(553				|| "during TLA call".to_owned(),554				|| {555					func.evaluate(556						self.clone(),557						self.create_default_context(Source::new_virtual(Cow::Borrowed("<tla>"))),558						CallLocation::native(),559						&self.settings().tla_vars,560						true,561					)562				},563			)?,564			v => v,565		})566	}567}568569/// Internals570impl State {571	fn data(&self) -> Ref<EvaluationData> {572		self.0.data.borrow()573	}574	fn data_mut(&self) -> RefMut<EvaluationData> {575		self.0.data.borrow_mut()576	}577	pub fn settings(&self) -> Ref<EvaluationSettings> {578		self.0.settings.borrow()579	}580	pub fn settings_mut(&self) -> RefMut<EvaluationSettings> {581		self.0.settings.borrow_mut()582	}583}584585/// Raw methods evaluate passed values but don't perform TLA execution586impl State {587	/// Parses and evaluates the given snippet588	pub fn evaluate_snippet(&self, name: String, code: String) -> Result<Val> {589		let source = Source::new_virtual(Cow::Owned(name.clone()));590		let parsed = jrsonnet_parser::parse(591			&code,592			&ParserSettings {593				file_name: source.clone(),594			},595		)596		.map_err(|e| ImportSyntaxError {597			path: source.clone(),598			source_code: code.clone().into(),599			error: Box::new(e),600		})?;601		self.data_mut().volatile_files.insert(name, code);602		evaluate(self.clone(), self.create_default_context(source), &parsed)603	}604}605606/// Settings utilities607impl State {608	pub fn add_tla(&self, name: IStr, value: Val) {609		self.settings_mut()610			.tla_vars611			.insert(name, TlaArg::Val(value));612	}613	pub fn add_tla_str(&self, name: IStr, value: IStr) {614		self.settings_mut()615			.tla_vars616			.insert(name, TlaArg::String(value));617	}618	pub fn add_tla_code(&self, name: IStr, code: &str) -> Result<()> {619		let source_name = format!("<top-level-arg:{}>", name);620		let source = Source::new_virtual(Cow::Owned(source_name.clone()));621		let parsed = jrsonnet_parser::parse(622			code,623			&ParserSettings {624				file_name: source.clone(),625			},626		)627		.map_err(|e| ImportSyntaxError {628			path: source,629			source_code: code.into(),630			error: Box::new(e),631		})?;632		self.data_mut()633			.volatile_files634			.insert(source_name, code.to_owned());635		self.settings_mut()636			.tla_vars637			.insert(name, TlaArg::Code(parsed));638		Ok(())639	}640641	pub fn resolve_file(&self, from: &Path, path: &str) -> Result<PathBuf> {642		self.settings()643			.import_resolver644			.resolve_file(from, path.as_ref())645	}646647	pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {648		Ref::map(self.settings(), |s| &*s.import_resolver)649	}650	pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {651		self.settings_mut().import_resolver = resolver;652	}653	pub fn context_initializer(&self) -> Ref<dyn ContextInitializer> {654		Ref::map(self.settings(), |s| &*s.context_initializer)655	}656657	pub fn manifest_format(&self) -> ManifestFormat {658		self.settings().manifest_format.clone()659	}660	pub fn set_manifest_format(&self, format: ManifestFormat) {661		self.settings_mut().manifest_format = format;662	}663664	pub fn trace_format(&self) -> Ref<dyn TraceFormat> {665		Ref::map(self.settings(), |s| &*s.trace_format)666	}667	pub fn set_trace_format(&self, format: Box<dyn TraceFormat>) {668		self.settings_mut().trace_format = format;669	}670671	pub fn max_trace(&self) -> usize {672		self.settings().max_trace673	}674	pub fn set_max_trace(&self, trace: usize) {675		self.settings_mut().max_trace = trace;676	}677678	pub fn max_stack(&self) -> usize {679		self.settings().max_stack680	}681	pub fn set_max_stack(&self, trace: usize) {682		self.settings_mut().max_stack = trace;683	}684}
after · crates/jrsonnet-evaluator/src/lib.rs
1#![warn(clippy::all, clippy::nursery, clippy::pedantic)]2#![allow(3	macro_expanded_macro_exports_accessed_by_absolute_paths,4	clippy::ptr_arg,5	// Too verbose6	clippy::must_use_candidate,7	// A lot of functions pass around errors thrown by code8	clippy::missing_errors_doc,9	// A lot of pointers have interior Rc10	clippy::needless_pass_by_value,11	// Its fine12	clippy::wildcard_imports,13	clippy::enum_glob_use,14	clippy::module_name_repetitions,15	// TODO: fix individual issues, however this works as intended almost everywhere16	clippy::cast_precision_loss,17	clippy::cast_possible_wrap,18	clippy::cast_possible_truncation,19	clippy::cast_sign_loss,20	// False positives21	// https://github.com/rust-lang/rust-clippy/issues/690222	clippy::use_self,23	// https://github.com/rust-lang/rust-clippy/issues/853924	clippy::iter_with_drain,25)]2627// For jrsonnet-macros28extern crate self as jrsonnet_evaluator;2930mod ctx;31mod dynamic;32pub mod error;33mod evaluate;34pub mod function;35pub mod gc;36mod import;37mod integrations;38mod map;39mod obj;40pub mod stdlib;41pub mod trace;42pub mod typed;43pub mod val;4445use std::{46	any::Any,47	borrow::Cow,48	cell::{Ref, RefCell, RefMut},49	collections::HashMap,50	fmt::{self, Debug},51	path::Path,52	rc::Rc,53};5455pub use ctx::*;56pub use dynamic::*;57use error::{Error::*, LocError, Result, StackTraceElement};58pub use evaluate::*;59use function::{CallLocation, TlaArg};60use gc::{GcHashMap, TraceBox};61use hashbrown::hash_map::RawEntryMut;62pub use import::*;63use jrsonnet_gcmodule::{Cc, Trace};64pub use jrsonnet_interner::{IBytes, IStr};65pub use jrsonnet_parser as parser;66use jrsonnet_parser::*;67pub use obj::*;68use trace::{CompactFormat, TraceFormat};69pub use val::{ManifestFormat, Thunk, Val};7071pub trait Unbound: Trace {72	type Bound;73	fn bind(&self, s: State, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Self::Bound>;74}7576#[derive(Clone, Trace)]77pub enum LazyBinding {78	Bindable(Cc<TraceBox<dyn Unbound<Bound = Thunk<Val>>>>),79	Bound(Thunk<Val>),80}8182impl Debug for LazyBinding {83	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {84		write!(f, "LazyBinding")85	}86}87impl LazyBinding {88	pub fn evaluate(89		&self,90		s: State,91		sup: Option<ObjValue>,92		this: Option<ObjValue>,93	) -> Result<Thunk<Val>> {94		match self {95			Self::Bindable(v) => v.bind(s, sup, this),96			Self::Bound(v) => Ok(v.clone()),97		}98	}99}100101/// During import, this trait will be called to create initial context for file102/// It may initialize global variables, stdlib for example103pub trait ContextInitializer {104	fn initialize(&self, state: State, for_file: Source) -> Context;105106	/// # Safety107	///108	/// For use only in bindings, should not be used elsewhere.109	/// Implementations which are not intended to be used in bindings110	/// should panic on call to this method.111	unsafe fn as_any(&self) -> &dyn Any;112}113114/// Context initializer, which adds noth115pub struct DummyContextInitializer;116impl ContextInitializer for DummyContextInitializer {117	fn initialize(&self, _state: State, _for_file: Source) -> Context {118		Context::default()119	}120	unsafe fn as_any(&self) -> &dyn Any {121		panic!("`as_any(&self)` is not supported by dummy initializer")122	}123}124125pub struct EvaluationSettings {126	/// Limits recursion by limiting the number of stack frames127	pub max_stack: usize,128	/// Limits amount of stack trace items preserved129	pub max_trace: usize,130	/// TLA vars131	pub tla_vars: HashMap<IStr, TlaArg>,132	/// Context initializer, which will be used for imports and everything133	/// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`134	pub context_initializer: Box<dyn ContextInitializer>,135	/// Used to resolve file locations/contents136	pub import_resolver: Box<dyn ImportResolver>,137	/// Used in manifestification functions138	pub manifest_format: ManifestFormat,139	/// Used for bindings140	pub trace_format: Box<dyn TraceFormat>,141}142impl Default for EvaluationSettings {143	fn default() -> Self {144		Self {145			max_stack: 200,146			max_trace: 20,147			context_initializer: Box::new(DummyContextInitializer),148			tla_vars: HashMap::default(),149			import_resolver: Box::new(DummyImportResolver),150			manifest_format: ManifestFormat::Json {151				padding: 4,152				#[cfg(feature = "exp-preserve-order")]153				preserve_order: false,154			},155			trace_format: Box::new(CompactFormat {156				padding: 4,157				resolver: trace::PathResolver::Absolute,158			}),159		}160	}161}162163#[derive(Default)]164struct EvaluationData {165	/// Used for stack overflow detection, stacktrace is populated on unwind166	stack_depth: usize,167	/// Updated every time stack entry is popt168	stack_generation: usize,169170	breakpoints: Breakpoints,171172	/// Contains file source codes and evaluation results for imports and pretty-printed stacktraces173	files: GcHashMap<SourcePath, FileData>,174}175struct FileData {176	string: Option<IStr>,177	bytes: Option<IBytes>,178	parsed: Option<LocExpr>,179	evaluated: Option<Val>,180181	evaluating: bool,182}183impl FileData {184	fn new_string(data: IStr) -> Self {185		Self {186			string: Some(data),187			bytes: None,188			parsed: None,189			evaluated: None,190			evaluating: false,191		}192	}193	fn new_bytes(data: IBytes) -> Self {194		Self {195			string: None,196			bytes: Some(data),197			parsed: None,198			evaluated: None,199			evaluating: false,200		}201	}202}203204#[allow(clippy::type_complexity)]205pub struct Breakpoint {206	loc: ExprLocation,207	collected: RefCell<HashMap<usize, (usize, Vec<Result<Val>>)>>,208}209#[derive(Default)]210struct Breakpoints(Vec<Rc<Breakpoint>>);211impl Breakpoints {212	fn insert(213		&self,214		stack_depth: usize,215		stack_generation: usize,216		loc: &ExprLocation,217		result: Result<Val>,218	) -> Result<Val> {219		if self.0.is_empty() {220			return result;221		}222		for item in &self.0 {223			if item.loc.belongs_to(loc) {224				let mut collected = item.collected.borrow_mut();225				let (depth, vals) = collected.entry(stack_generation).or_default();226				if stack_depth > *depth {227					vals.clear();228				}229				vals.push(result.clone());230			}231		}232		result233	}234}235236#[derive(Default)]237pub struct EvaluationStateInternals {238	/// Internal state239	data: RefCell<EvaluationData>,240	/// Settings, safe to change at runtime241	settings: RefCell<EvaluationSettings>,242}243244/// Maintains stack trace and import resolution245#[derive(Default, Clone)]246pub struct State(Rc<EvaluationStateInternals>);247248impl State {249	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise250	pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {251		let mut data = self.data_mut();252		let mut file = data.files.raw_entry_mut().from_key(&path);253254		let file = match file {255			RawEntryMut::Occupied(ref mut d) => d.get_mut(),256			RawEntryMut::Vacant(v) => {257				let data = self.settings().import_resolver.load_file_contents(&path)?;258				v.insert(259					path.clone(),260					FileData::new_string(261						std::str::from_utf8(&data)262							.map_err(|_| ImportBadFileUtf8(path.clone()))?263							.into(),264					),265				)266				.1267			}268		};269		if let Some(str) = &file.string {270			return Ok(str.clone());271		}272		if file.string.is_none() {273			file.string = Some(274				file.bytes275					.as_ref()276					.expect("either string or bytes should be set")277					.clone()278					.cast_str()279					.ok_or_else(|| ImportBadFileUtf8(path.clone()))?,280			);281		}282		Ok(file.string.as_ref().expect("just set").clone())283	}284	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise285	pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {286		let mut data = self.data_mut();287		let mut file = data.files.raw_entry_mut().from_key(&path);288289		let file = match file {290			RawEntryMut::Occupied(ref mut d) => d.get_mut(),291			RawEntryMut::Vacant(v) => {292				let data = self.settings().import_resolver.load_file_contents(&path)?;293				v.insert(path.clone(), FileData::new_bytes(data.as_slice().into()))294					.1295			}296		};297		if let Some(str) = &file.bytes {298			return Ok(str.clone());299		}300		if file.bytes.is_none() {301			file.bytes = Some(302				file.string303					.as_ref()304					.expect("either string or bytes should be set")305					.clone()306					.cast_bytes(),307			);308		}309		Ok(file.bytes.as_ref().expect("just set").clone())310	}311	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise312	pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {313		let mut data = self.data_mut();314		let mut file = data.files.raw_entry_mut().from_key(&path);315316		let file = match file {317			RawEntryMut::Occupied(ref mut d) => d.get_mut(),318			RawEntryMut::Vacant(v) => {319				let data = self.settings().import_resolver.load_file_contents(&path)?;320				v.insert(321					path.clone(),322					FileData::new_string(323						std::str::from_utf8(&data)324							.map_err(|_| ImportBadFileUtf8(path.clone()))?325							.into(),326					),327				)328				.1329			}330		};331		if let Some(val) = &file.evaluated {332			return Ok(val.clone());333		}334		if file.string.is_none() {335			file.string = Some(336				std::str::from_utf8(337					file.bytes338						.as_ref()339						.expect("either string or bytes should be set"),340				)341				.map_err(|_| ImportBadFileUtf8(path.clone()))?342				.into(),343			);344		}345		let code = file.string.as_ref().expect("just set");346		let file_name =347			Source::new(path.clone(), code.clone()).expect("resolver should return correct name");348		if file.parsed.is_none() {349			file.parsed = Some(350				jrsonnet_parser::parse(351					code,352					&ParserSettings {353						file_name: file_name.clone(),354					},355				)356				.map_err(|e| ImportSyntaxError {357					path: file_name.clone(),358					error: Box::new(e),359				})?,360			);361		}362		let parsed = file.parsed.as_ref().expect("just set").clone();363		if file.evaluating {364			throw!(InfiniteRecursionDetected)365		}366		file.evaluating = true;367		// Dropping file here, as it borrows data, which may be used in evaluation368		drop(data);369		let res = evaluate(370			self.clone(),371			self.create_default_context(file_name),372			&parsed,373		);374375		let mut data = self.data_mut();376		let mut file = data.files.raw_entry_mut().from_key(&path);377378		let file = match file {379			RawEntryMut::Occupied(ref mut d) => d.get_mut(),380			RawEntryMut::Vacant(_) => unreachable!("this file was just here!"),381		};382		file.evaluating = false;383		match res {384			Ok(v) => {385				file.evaluated = Some(v.clone());386				Ok(v)387			}388			Err(e) => Err(e),389		}390	}391	pub fn import(&self, from: &Path, path: &str) -> Result<Val> {392		let resolved = self.resolve_file(from, path)?;393		self.import_resolved(resolved)394	}395396	/// Creates context with all passed global variables397	pub fn create_default_context(&self, source: Source) -> Context {398		let context_initializer = &self.settings().context_initializer;399		context_initializer.initialize(self.clone(), source)400	}401402	/// Executes code creating a new stack frame403	pub fn push<T>(404		&self,405		e: CallLocation,406		frame_desc: impl FnOnce() -> String,407		f: impl FnOnce() -> Result<T>,408	) -> Result<T> {409		{410			let mut data = self.data_mut();411			let stack_depth = &mut data.stack_depth;412			if *stack_depth > self.max_stack() {413				// Error creation uses data, so i drop guard here414				drop(data);415				throw!(StackOverflow);416			}417			*stack_depth += 1;418		}419		let result = f();420		{421			let mut data = self.data_mut();422			data.stack_depth -= 1;423			data.stack_generation += 1;424		}425		if let Err(mut err) = result {426			err.trace_mut().0.push(StackTraceElement {427				location: e.0.cloned(),428				desc: frame_desc(),429			});430			return Err(err);431		}432		result433	}434435	/// Executes code creating a new stack frame436	pub fn push_val(437		&self,438		e: &ExprLocation,439		frame_desc: impl FnOnce() -> String,440		f: impl FnOnce() -> Result<Val>,441	) -> Result<Val> {442		{443			let mut data = self.data_mut();444			let stack_depth = &mut data.stack_depth;445			if *stack_depth > self.max_stack() {446				// Error creation uses data, so i drop guard here447				drop(data);448				throw!(StackOverflow);449			}450			*stack_depth += 1;451		}452		let mut result = f();453		{454			let mut data = self.data_mut();455			data.stack_depth -= 1;456			data.stack_generation += 1;457			result = data458				.breakpoints459				.insert(data.stack_depth, data.stack_generation, e, result);460		}461		if let Err(mut err) = result {462			err.trace_mut().0.push(StackTraceElement {463				location: Some(e.clone()),464				desc: frame_desc(),465			});466			return Err(err);467		}468		result469	}470	/// Executes code creating a new stack frame471	pub fn push_description<T>(472		&self,473		frame_desc: impl FnOnce() -> String,474		f: impl FnOnce() -> Result<T>,475	) -> Result<T> {476		{477			let mut data = self.data_mut();478			let stack_depth = &mut data.stack_depth;479			if *stack_depth > self.max_stack() {480				// Error creation uses data, so i drop guard here481				drop(data);482				throw!(StackOverflow);483			}484			*stack_depth += 1;485		}486		let result = f();487		{488			let mut data = self.data_mut();489			data.stack_depth -= 1;490			data.stack_generation += 1;491		}492		if let Err(mut err) = result {493			err.trace_mut().0.push(StackTraceElement {494				location: None,495				desc: frame_desc(),496			});497			return Err(err);498		}499		result500	}501502	/// # Panics503	/// In case of formatting failure504	pub fn stringify_err(&self, e: &LocError) -> String {505		let mut out = String::new();506		self.settings()507			.trace_format508			.write_trace(&mut out, self, e)509			.unwrap();510		out511	}512513	pub fn manifest(&self, val: Val) -> Result<IStr> {514		self.push_description(515			|| "manifestification".to_string(),516			|| val.manifest(self.clone(), &self.manifest_format()),517		)518	}519	pub fn manifest_multi(&self, val: Val) -> Result<Vec<(IStr, IStr)>> {520		val.manifest_multi(self.clone(), &self.manifest_format())521	}522	pub fn manifest_stream(&self, val: Val) -> Result<Vec<IStr>> {523		val.manifest_stream(self.clone(), &self.manifest_format())524	}525526	/// If passed value is function then call with set TLA527	pub fn with_tla(&self, val: Val) -> Result<Val> {528		Ok(match val {529			Val::Func(func) => self.push_description(530				|| "during TLA call".to_owned(),531				|| {532					func.evaluate(533						self.clone(),534						self.create_default_context(Source::new_virtual(535							Cow::Borrowed("<tla>"),536							IStr::empty(),537						)),538						CallLocation::native(),539						&self.settings().tla_vars,540						true,541					)542				},543			)?,544			v => v,545		})546	}547}548549/// Internals550impl State {551	// fn data(&self) -> Ref<EvaluationData> {552	// 	self.0.data.borrow()553	// }554	fn data_mut(&self) -> RefMut<EvaluationData> {555		self.0.data.borrow_mut()556	}557	pub fn settings(&self) -> Ref<EvaluationSettings> {558		self.0.settings.borrow()559	}560	pub fn settings_mut(&self) -> RefMut<EvaluationSettings> {561		self.0.settings.borrow_mut()562	}563}564565/// Raw methods evaluate passed values but don't perform TLA execution566impl State {567	/// Parses and evaluates the given snippet568	pub fn evaluate_snippet(&self, name: String, code: impl Into<IStr>) -> Result<Val> {569		let code = code.into();570		let source = Source::new_virtual(Cow::Owned(name), code.clone());571		let parsed = jrsonnet_parser::parse(572			&code,573			&ParserSettings {574				file_name: source.clone(),575			},576		)577		.map_err(|e| ImportSyntaxError {578			path: source.clone(),579			error: Box::new(e),580		})?;581		evaluate(self.clone(), self.create_default_context(source), &parsed)582	}583}584585/// Settings utilities586impl State {587	pub fn add_tla(&self, name: IStr, value: Val) {588		self.settings_mut()589			.tla_vars590			.insert(name, TlaArg::Val(value));591	}592	pub fn add_tla_str(&self, name: IStr, value: IStr) {593		self.settings_mut()594			.tla_vars595			.insert(name, TlaArg::String(value));596	}597	pub fn add_tla_code(&self, name: IStr, code: &str) -> Result<()> {598		let source_name = format!("<top-level-arg:{}>", name);599		let source = Source::new_virtual(Cow::Owned(source_name.clone()), code.into());600		let parsed = jrsonnet_parser::parse(601			code,602			&ParserSettings {603				file_name: source.clone(),604			},605		)606		.map_err(|e| ImportSyntaxError {607			path: source,608			error: Box::new(e),609		})?;610		self.settings_mut()611			.tla_vars612			.insert(name, TlaArg::Code(parsed));613		Ok(())614	}615616	pub fn resolve_file(&self, from: &Path, path: &str) -> Result<SourcePath> {617		self.settings()618			.import_resolver619			.resolve_file_relative(from, path.as_ref())620	}621622	pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {623		Ref::map(self.settings(), |s| &*s.import_resolver)624	}625	pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {626		self.settings_mut().import_resolver = resolver;627	}628	pub fn context_initializer(&self) -> Ref<dyn ContextInitializer> {629		Ref::map(self.settings(), |s| &*s.context_initializer)630	}631632	pub fn manifest_format(&self) -> ManifestFormat {633		self.settings().manifest_format.clone()634	}635	pub fn set_manifest_format(&self, format: ManifestFormat) {636		self.settings_mut().manifest_format = format;637	}638639	pub fn trace_format(&self) -> Ref<dyn TraceFormat> {640		Ref::map(self.settings(), |s| &*s.trace_format)641	}642	pub fn set_trace_format(&self, format: Box<dyn TraceFormat>) {643		self.settings_mut().trace_format = format;644	}645646	pub fn max_trace(&self) -> usize {647		self.settings().max_trace648	}649	pub fn set_max_trace(&self, trace: usize) {650		self.settings_mut().max_trace = trace;651	}652653	pub fn max_stack(&self) -> usize {654		self.settings().max_stack655	}656	pub fn set_max_stack(&self, trace: usize) {657		self.settings_mut().max_stack = trace;658	}659}
deletedcrates/jrsonnet-evaluator/src/trace/location.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/trace/location.rs
+++ /dev/null
@@ -1,124 +0,0 @@
-#[allow(clippy::module_name_repetitions)]
-#[derive(Clone, PartialEq, Eq, Debug)]
-pub struct CodeLocation {
-	pub offset: usize,
-
-	pub line: usize,
-	pub column: usize,
-
-	pub line_start_offset: usize,
-	pub line_end_offset: usize,
-}
-
-#[allow(clippy::module_name_repetitions)]
-pub fn location_to_offset(mut file: &str, mut line: usize, column: usize) -> Option<usize> {
-	let mut offset = 0;
-	while line > 1 {
-		let pos = file.find('\n')?;
-		offset += pos + 1;
-		file = &file[pos + 1..];
-		line -= 1;
-	}
-	offset += column - 1;
-	Some(offset)
-}
-
-#[allow(clippy::module_name_repetitions)]
-pub fn offset_to_location(file: &str, offsets: &[u32]) -> Vec<CodeLocation> {
-	if offsets.is_empty() {
-		return vec![];
-	}
-	let mut line = 1;
-	let mut column = 1;
-	let max_offset = *offsets.iter().max().expect("offsets is not empty");
-
-	let mut offset_map = offsets
-		.iter()
-		.enumerate()
-		.map(|(pos, offset)| (*offset, pos))
-		.collect::<Vec<_>>();
-	offset_map.sort_by_key(|v| v.0);
-	offset_map.reverse();
-
-	let mut out = vec![
-		CodeLocation {
-			offset: 0,
-			column: 0,
-			line: 0,
-			line_start_offset: 0,
-			line_end_offset: 0
-		};
-		offsets.len()
-	];
-	let mut with_no_known_line_ending = vec![];
-	let mut this_line_offset = 0;
-	for (pos, ch) in file
-		.chars()
-		.enumerate()
-		.chain(std::iter::once((file.len(), ' ')))
-	{
-		column += 1;
-		match offset_map.last() {
-			Some(x) if x.0 == pos as u32 => {
-				let out_idx = x.1;
-				with_no_known_line_ending.push(out_idx);
-				out[out_idx].offset = pos;
-				out[out_idx].line = line;
-				out[out_idx].column = column;
-				out[out_idx].line_start_offset = this_line_offset;
-				offset_map.pop();
-			}
-			_ => {}
-		}
-		if ch == '\n' {
-			line += 1;
-			column = 1;
-
-			for idx in with_no_known_line_ending.drain(..) {
-				out[idx].line_end_offset = pos;
-			}
-			this_line_offset = pos + 1;
-
-			if pos == max_offset as usize + 1 {
-				break;
-			}
-		}
-	}
-	let file_end = file.chars().count();
-	for idx in with_no_known_line_ending {
-		out[idx].line_end_offset = file_end;
-	}
-
-	out
-}
-
-#[cfg(test)]
-pub mod tests {
-	use super::{offset_to_location, CodeLocation};
-
-	#[test]
-	fn test() {
-		assert_eq!(
-			offset_to_location(
-				"hello world\n_______________________________________________________",
-				&[0, 14]
-			),
-			vec![
-				CodeLocation {
-					offset: 0,
-					line: 1,
-					column: 2,
-					line_start_offset: 0,
-					line_end_offset: 11,
-				},
-				CodeLocation {
-					offset: 14,
-					line: 2,
-					column: 4,
-					line_start_offset: 12,
-					line_end_offset: 67
-				}
-			]
-		)
-	}
-}
modifiedcrates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/trace/mod.rs
+++ b/crates/jrsonnet-evaluator/src/trace/mod.rs
@@ -1,9 +1,6 @@
-mod location;
-
 use std::path::{Path, PathBuf};
 
-use jrsonnet_parser::Source;
-pub use location::*;
+use jrsonnet_parser::{CodeLocation, Source};
 
 use crate::{error::Error, LocError, State};
 
@@ -84,31 +81,27 @@
 	fn write_trace(
 		&self,
 		out: &mut dyn std::fmt::Write,
-		s: &State,
+		_s: &State,
 		error: &LocError,
 	) -> Result<(), std::fmt::Error> {
 		write!(out, "{}", error.error())?;
-		if let Error::ImportSyntaxError {
-			path,
-			source_code,
-			error,
-		} = error.error()
-		{
+		if let Error::ImportSyntaxError { path, error } = error.error() {
 			use std::fmt::Write;
 
 			writeln!(out)?;
-			let mut n = match path.repr() {
-				Ok(r) => self.resolver.resolve(r),
-				Err(v) => v.to_string(),
+			let mut n = match path.path() {
+				Some(r) => self.resolver.resolve(r),
+				None => path.short_display().to_string(),
 			};
 			let mut offset = error.location.offset;
-			let is_eof = if offset >= source_code.len() {
-				offset = source_code.len().saturating_sub(1);
+			let is_eof = if offset >= path.code().len() {
+				offset = path.code().len().saturating_sub(1);
 				true
 			} else {
 				false
 			};
-			let mut location = offset_to_location(source_code, &[offset as u32])
+			let mut location = path
+				.map_source_locations(&[offset as u32])
 				.into_iter()
 				.next()
 				.unwrap();
@@ -129,13 +122,12 @@
 				use std::fmt::Write;
 				#[allow(clippy::option_if_let_else)]
 				if let Some(location) = location {
-					let mut resolved_path = match location.0.repr() {
-						Ok(r) => self.resolver.resolve(r),
-						Err(v) => v.to_string(),
+					let mut resolved_path = match location.0.path() {
+						Some(r) => self.resolver.resolve(r),
+						None => location.0.short_display().to_string(),
 					};
 					// TODO: Process all trace elements first
-					let location =
-						s.map_source_locations(location.0.clone(), &[location.1, location.2]);
+					let location = location.0.map_source_locations(&[location.1, location.2]);
 					write!(resolved_path, ":").unwrap();
 					print_code_location(&mut resolved_path, &location[0], &location[1]).unwrap();
 					write!(resolved_path, ":").unwrap();
@@ -176,7 +168,7 @@
 	fn write_trace(
 		&self,
 		out: &mut dyn std::fmt::Write,
-		s: &State,
+		_s: &State,
 		error: &LocError,
 	) -> Result<(), std::fmt::Error> {
 		write!(out, "{}", error.error())?;
@@ -184,10 +176,10 @@
 			writeln!(out)?;
 			let desc = &item.desc;
 			if let Some(source) = &item.location {
-				let start_end = s.map_source_locations(source.0.clone(), &[source.1, source.2]);
-				let resolved_path = match source.0.repr() {
-					Ok(r) => r.display().to_string(),
-					Err(v) => v.to_string(),
+				let start_end = source.0.map_source_locations(&[source.1, source.2]);
+				let resolved_path = match source.0.path() {
+					Some(r) => r.display().to_string(),
+					None => source.0.short_display().to_string(),
 				};
 
 				write!(
@@ -213,19 +205,15 @@
 	fn write_trace(
 		&self,
 		out: &mut dyn std::fmt::Write,
-		s: &State,
+		_s: &State,
 		error: &LocError,
 	) -> Result<(), std::fmt::Error> {
 		write!(out, "{}", error.error())?;
-		if let Error::ImportSyntaxError {
-			path,
-			source_code,
-			error,
-		} = error.error()
-		{
+		if let Error::ImportSyntaxError { path, error } = error.error() {
 			writeln!(out)?;
 			let offset = error.location.offset;
-			let location = offset_to_location(source_code, &[offset as u32])
+			let location = path
+				.map_source_locations(&[offset as u32])
 				.into_iter()
 				.next()
 				.unwrap();
@@ -234,7 +222,7 @@
 
 			self.print_snippet(
 				out,
-				source_code,
+				path.code(),
 				path,
 				&location,
 				&end_location,
@@ -246,10 +234,10 @@
 			writeln!(out)?;
 			let desc = &item.desc;
 			if let Some(source) = &item.location {
-				let start_end = s.map_source_locations(source.0.clone(), &[source.1, source.2]);
+				let start_end = source.0.map_source_locations(&[source.1, source.2]);
 				self.print_snippet(
 					out,
-					&s.get_source(source.0.clone()).unwrap(),
+					&source.0.code(),
 					&source.0,
 					&start_end[0],
 					&start_end[1],
@@ -284,9 +272,9 @@
 			.take(end.line_end_offset - end.line_start_offset)
 			.collect();
 
-		let origin = match origin.repr() {
-			Ok(r) => self.resolver.resolve(r),
-			Err(v) => v.to_string(),
+		let origin = match origin.path() {
+			Some(r) => self.resolver.resolve(r),
+			None => origin.short_display().to_string(),
 		};
 		let snippet = Snippet {
 			opt: FormatOptions {
modifiedcrates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-interner/src/lib.rs
+++ b/crates/jrsonnet-interner/src/lib.rs
@@ -33,6 +33,10 @@
 
 impl IStr {
 	#[must_use]
+	pub fn empty() -> Self {
+		"".into()
+	}
+	#[must_use]
 	pub fn as_str(&self) -> &str {
 		self as &str
 	}
modifiedcrates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -7,9 +7,11 @@
 pub use expr::*;
 pub use jrsonnet_interner::IStr;
 pub use peg;
+mod location;
 mod source;
 mod unescape;
-pub use source::Source;
+pub use location::CodeLocation;
+pub use source::{Source, SourcePath};
 
 pub struct ParserSettings {
 	pub file_name: Source,
addedcrates/jrsonnet-parser/src/location.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-parser/src/location.rs
@@ -0,0 +1,124 @@
+#[allow(clippy::module_name_repetitions)]
+#[derive(Clone, PartialEq, Eq, Debug)]
+pub struct CodeLocation {
+	pub offset: usize,
+
+	pub line: usize,
+	pub column: usize,
+
+	pub line_start_offset: usize,
+	pub line_end_offset: usize,
+}
+
+#[allow(clippy::module_name_repetitions)]
+pub fn location_to_offset(mut file: &str, mut line: usize, column: usize) -> Option<usize> {
+	let mut offset = 0;
+	while line > 1 {
+		let pos = file.find('\n')?;
+		offset += pos + 1;
+		file = &file[pos + 1..];
+		line -= 1;
+	}
+	offset += column - 1;
+	Some(offset)
+}
+
+#[allow(clippy::module_name_repetitions)]
+pub fn offset_to_location(file: &str, offsets: &[u32]) -> Vec<CodeLocation> {
+	if offsets.is_empty() {
+		return vec![];
+	}
+	let mut line = 1;
+	let mut column = 1;
+	let max_offset = *offsets.iter().max().expect("offsets is not empty");
+
+	let mut offset_map = offsets
+		.iter()
+		.enumerate()
+		.map(|(pos, offset)| (*offset, pos))
+		.collect::<Vec<_>>();
+	offset_map.sort_by_key(|v| v.0);
+	offset_map.reverse();
+
+	let mut out = vec![
+		CodeLocation {
+			offset: 0,
+			column: 0,
+			line: 0,
+			line_start_offset: 0,
+			line_end_offset: 0
+		};
+		offsets.len()
+	];
+	let mut with_no_known_line_ending = vec![];
+	let mut this_line_offset = 0;
+	for (pos, ch) in file
+		.chars()
+		.enumerate()
+		.chain(std::iter::once((file.len(), ' ')))
+	{
+		column += 1;
+		match offset_map.last() {
+			Some(x) if x.0 == pos as u32 => {
+				let out_idx = x.1;
+				with_no_known_line_ending.push(out_idx);
+				out[out_idx].offset = pos;
+				out[out_idx].line = line;
+				out[out_idx].column = column;
+				out[out_idx].line_start_offset = this_line_offset;
+				offset_map.pop();
+			}
+			_ => {}
+		}
+		if ch == '\n' {
+			line += 1;
+			column = 1;
+
+			for idx in with_no_known_line_ending.drain(..) {
+				out[idx].line_end_offset = pos;
+			}
+			this_line_offset = pos + 1;
+
+			if pos == max_offset as usize + 1 {
+				break;
+			}
+		}
+	}
+	let file_end = file.chars().count();
+	for idx in with_no_known_line_ending {
+		out[idx].line_end_offset = file_end;
+	}
+
+	out
+}
+
+#[cfg(test)]
+pub mod tests {
+	use super::{offset_to_location, CodeLocation};
+
+	#[test]
+	fn test() {
+		assert_eq!(
+			offset_to_location(
+				"hello world\n_______________________________________________________",
+				&[0, 14]
+			),
+			vec![
+				CodeLocation {
+					offset: 0,
+					line: 1,
+					column: 2,
+					line_start_offset: 0,
+					line_end_offset: 11,
+				},
+				CodeLocation {
+					offset: 14,
+					line: 2,
+					column: 4,
+					line_start_offset: 12,
+					line_end_offset: 67
+				}
+			]
+		)
+	}
+}
modifiedcrates/jrsonnet-parser/src/source.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/source.rs
+++ b/crates/jrsonnet-parser/src/source.rs
@@ -6,21 +6,42 @@
 };
 
 use jrsonnet_gcmodule::{Trace, Tracer};
+use jrsonnet_interner::IStr;
 #[cfg(feature = "serde")]
 use serde::{Deserialize, Serialize};
 
+use crate::location::{location_to_offset, offset_to_location, CodeLocation};
+
 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
-#[derive(PartialEq, Eq, Debug, Hash)]
-enum Inner {
-	Real(PathBuf),
+#[derive(PartialEq, Eq, Debug, Hash, Clone)]
+pub enum SourcePath {
+	/// This file is located on disk
+	Path(PathBuf),
+	/// This file is located somewhere else (I.e http), but it can refer to relative paths, and is egilible for caching
+	Custom(String),
+	/// This file is only located in memory, and can't be cached
 	Virtual(Cow<'static, str>),
 }
+impl Trace for SourcePath {
+	fn trace(&self, _tracer: &mut Tracer) {}
 
+	fn is_type_tracked() -> bool {
+		false
+	}
+}
+
+impl SourcePath {
+	/// Should import resolver be able to read file by this path?
+	pub fn can_load(&self) -> bool {
+		matches!(self, Self::Path(_) | Self::Custom(_))
+	}
+}
+
 /// Either real file, or virtual
 /// Hash of FileName always have same value as raw Path, to make it possible to use with raw_entry_mut
 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
 #[derive(Clone, PartialEq, Eq, Debug)]
-pub struct Source(Rc<Inner>);
+pub struct Source(Rc<(SourcePath, IStr)>);
 static_assertions::assert_eq_size!(Source, *const ());
 
 impl Trace for Source {
@@ -33,55 +54,63 @@
 
 impl Source {
 	/// Fails when path contains inner /../ or /./ references, or not absolute
-	pub fn new(path: PathBuf) -> Option<Self> {
-		if !path.is_absolute()
-			|| path
-				.components()
-				.any(|c| matches!(c, Component::CurDir | Component::ParentDir))
-		{
-			return None;
+	pub fn new(path: SourcePath, code: IStr) -> Option<Self> {
+		if let SourcePath::Path(path) = &path {
+			if !path.is_absolute()
+				|| path
+					.components()
+					.any(|c| matches!(c, Component::CurDir | Component::ParentDir))
+			{
+				return None;
+			}
 		}
-		Some(Self(Rc::new(Inner::Real(path))))
+		Some(Self(Rc::new((path, code))))
 	}
 
-	pub fn new_virtual(n: Cow<'static, str>) -> Self {
-		Self(Rc::new(Inner::Virtual(n)))
+	pub fn new_virtual(n: Cow<'static, str>, code: IStr) -> Self {
+		Self(Rc::new((SourcePath::Virtual(n), code)))
 	}
 
 	pub fn short_display(&self) -> ShortDisplay {
 		ShortDisplay(self.clone())
 	}
 
-	/// Returns None if file is virtual
+	/// Returns Some if this file is loaded from FS
 	pub fn path(&self) -> Option<&Path> {
-		match self.inner() {
-			Inner::Real(r) => Some(r),
-			Inner::Virtual(_) => None,
+		match self.source_path() {
+			SourcePath::Path(r) => Some(r),
+			SourcePath::Custom(_) => None,
+			SourcePath::Virtual(_) => None,
 		}
 	}
-	pub fn repr(&self) -> Result<&Path, &str> {
-		match self.inner() {
-			Inner::Real(r) => Ok(r),
-			Inner::Virtual(v) => Err(v.as_ref()),
-		}
+	pub fn code(&self) -> &str {
+		&self.0 .1
+	}
+
+	pub fn source_path(&self) -> &SourcePath {
+		&self.0 .0 as &SourcePath
 	}
 
-	fn inner(&self) -> &Inner {
-		&self.0 as &Inner
+	pub fn map_source_locations(&self, locs: &[u32]) -> Vec<CodeLocation> {
+		offset_to_location(&self.0 .1, locs)
+	}
+	pub fn map_from_source_location(&self, line: usize, column: usize) -> Option<usize> {
+		location_to_offset(&self.0 .1, line, column)
 	}
 }
 pub struct ShortDisplay(Source);
 impl fmt::Display for ShortDisplay {
 	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-		match &self.0 .0 as &Inner {
-			Inner::Real(r) => {
+		match &self.0 .0 .0 as &SourcePath {
+			SourcePath::Path(r) => {
 				write!(
 					f,
 					"{}",
 					r.file_name().expect("path is valid").to_string_lossy()
 				)
 			}
-			Inner::Virtual(n) => write!(f, "{}", n),
+			SourcePath::Custom(r) => write!(f, "{}", r),
+			SourcePath::Virtual(n) => write!(f, "{}", n),
 		}
 	}
 }
modifiedcrates/jrsonnet-stdlib/build.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/build.rs
+++ b/crates/jrsonnet-stdlib/build.rs
@@ -7,7 +7,10 @@
 	let parsed = parse(
 		include_str!("./src/std.jsonnet"),
 		&ParserSettings {
-			file_name: Source::new_virtual(Cow::Borrowed("<std>")),
+			file_name: Source::new_virtual(
+				Cow::Borrowed("<std>"),
+				include_str!("./src/std.jsonnet").into(),
+			),
 		},
 	)
 	.expect("parse");
modifiedcrates/jrsonnet-stdlib/src/expr.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/expr.rs
+++ b/crates/jrsonnet-stdlib/src/expr.rs
@@ -15,7 +15,7 @@
 	jrsonnet_parser::parse(
 		STDLIB_STR,
 		&ParserSettings {
-			file_name: Source::new_virtual(Cow::Borrowed("<std>")),
+			file_name: Source::new_virtual(Cow::Borrowed("<std>"), STDLIB_STR.into()),
 		},
 	)
 	.unwrap()
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -183,10 +183,10 @@
 
 pub struct StdTracePrinter;
 impl TracePrinter for StdTracePrinter {
-	fn print_trace(&self, s: State, loc: CallLocation, value: IStr) {
+	fn print_trace(&self, _s: State, loc: CallLocation, value: IStr) {
 		eprint!("TRACE:");
 		if let Some(loc) = loc.0 {
-			let locs = s.map_source_locations(loc.0.clone(), &[loc.1]);
+			let locs = loc.0.map_source_locations(&[loc.1]);
 			eprint!(" {}:{}", loc.0.short_display(), locs[0].line);
 		}
 		eprintln!(" {}", value);
@@ -212,9 +212,9 @@
 	}
 }
 
-pub fn extvar_source(name: &str) -> Source {
+pub fn extvar_source(name: &str, code: impl Into<IStr>) -> Source {
 	let source_name = format!("<extvar:{}>", name);
-	Source::new_virtual(Cow::Owned(source_name))
+	Source::new_virtual(Cow::Owned(source_name), code.into())
 }
 
 pub struct ContextInitializer {
@@ -260,8 +260,9 @@
 			.ext_vars
 			.insert(name, TlaArg::String(value));
 	}
-	pub fn add_ext_code(&self, name: &str, code: String) -> Result<()> {
-		let source = extvar_source(name);
+	pub fn add_ext_code(&self, name: &str, code: impl Into<IStr>) -> Result<()> {
+		let code = code.into();
+		let source = extvar_source(name, code.clone());
 		let parsed = jrsonnet_parser::parse(
 			&code,
 			&jrsonnet_parser::ParserSettings {
@@ -270,7 +271,6 @@
 		)
 		.map_err(|e| ImportSyntaxError {
 			path: source,
-			source_code: code.clone().into(),
 			error: Box::new(e),
 		})?;
 		// self.data_mut().volatile_files.insert(source_name, code);
@@ -297,11 +297,13 @@
 			.hide()
 			.value(
 				s,
-				Val::Str(match source.repr() {
-					Ok(p) => p.display().to_string().into(),
-					// Virtual files end up as empty strings in std.thisFile
-					Err(_e) => "".into(),
-				}),
+				Val::Str(
+					source
+						.path()
+						.map(|p| p.display().to_string())
+						.unwrap_or_else(String::new)
+						.into(),
+				),
 			)
 			.expect("this object builder is empty");
 		let stdlib_with_this_file = builder.build();
@@ -343,7 +345,7 @@
 	settings: Rc<RefCell<Settings>>,
 ))]
 fn builtin_ext_var(this: &builtin_ext_var, s: State, x: IStr) -> Result<Any> {
-	let ctx = s.create_default_context(extvar_source(&x));
+	let ctx = s.create_default_context(extvar_source(&x, ""));
 	Ok(Any(this
 		.settings
 		.borrow()