git.delta.rocks / jrsonnet / refs/commits / 78d82dfdf2a1

difftreelog

feat derive(Typed) for struct

Yaroslav Bolyukin2022-04-04parent: #d710b4f.patch.diff
in: master

9 files changed

modifiedCargo.lockdiffbeforeafterboth
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -70,8 +70,9 @@
 
 [[package]]
 name = "clap"
-version = "3.0.0-beta.2"
-source = "git+https://github.com/clap-rs/clap?rev=f0c5ea5e1503de5c8e74d8c047a799cf51498e83#f0c5ea5e1503de5c8e74d8c047a799cf51498e83"
+version = "3.1.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "71c47df61d9e16dc010b55dba1952a57d8c215dbb533fd13cdd13369aac73b1c"
 dependencies = [
  "atty",
  "bitflags",
@@ -82,27 +83,28 @@
  "strsim",
  "termcolor",
  "textwrap",
- "vec_map",
 ]
 
 [[package]]
+name = "clap_complete"
+version = "3.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "df6f3613c0a3cddfd78b41b10203eb322cb29b600cbdf808a7d3db95691b8e25"
+dependencies = [
+ "clap",
+]
+
+[[package]]
 name = "clap_derive"
-version = "3.0.0-beta.2"
-source = "git+https://github.com/clap-rs/clap?rev=f0c5ea5e1503de5c8e74d8c047a799cf51498e83#f0c5ea5e1503de5c8e74d8c047a799cf51498e83"
+version = "3.1.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a3aab4734e083b809aaf5794e14e756d1c798d2c69c7f7de7a09a2f5214993c1"
 dependencies = [
  "heck",
  "proc-macro-error",
  "proc-macro2",
  "quote",
  "syn",
-]
-
-[[package]]
-name = "clap_generate"
-version = "3.0.0-beta.2"
-source = "git+https://github.com/clap-rs/clap?rev=f0c5ea5e1503de5c8e74d8c047a799cf51498e83#f0c5ea5e1503de5c8e74d8c047a799cf51498e83"
-dependencies = [
- "clap",
 ]
 
 [[package]]
@@ -148,12 +150,9 @@
 
 [[package]]
 name = "heck"
-version = "0.3.3"
+version = "0.4.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
-dependencies = [
- "unicode-segmentation",
-]
+checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
 
 [[package]]
 name = "hermit-abi"
@@ -185,7 +184,7 @@
 version = "0.4.2"
 dependencies = [
  "clap",
- "clap_generate",
+ "clap_complete",
  "gcmodule",
  "jrsonnet-cli",
  "jrsonnet-evaluator",
@@ -214,6 +213,7 @@
  "bincode",
  "gcmodule",
  "jrsonnet-interner",
+ "jrsonnet-macros",
  "jrsonnet-parser",
  "jrsonnet-stdlib",
  "jrsonnet-types",
@@ -236,6 +236,15 @@
 ]
 
 [[package]]
+name = "jrsonnet-macros"
+version = "0.4.2"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
 name = "jrsonnet-parser"
 version = "0.4.2"
 dependencies = [
@@ -301,6 +310,12 @@
 checksum = "490cc448043f947bae3cbee9c203358d62dbee0db12107a74be5c30ccfd09771"
 
 [[package]]
+name = "memchr"
+version = "2.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
+
+[[package]]
 name = "mimalloc-sys"
 version = "0.1.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -321,9 +336,12 @@
 
 [[package]]
 name = "os_str_bytes"
-version = "3.1.0"
+version = "6.0.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6acbef58a60fe69ab50510a55bc8cdd4d6cf2283d27ad338f54cb52747a9cf2d"
+checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64"
+dependencies = [
+ "memchr",
+]
 
 [[package]]
 name = "parking_lot"
@@ -357,9 +375,9 @@
 
 [[package]]
 name = "peg"
-version = "0.7.0"
+version = "0.8.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "07c0b841ea54f523f7aa556956fbd293bcbe06f2e67d2eb732b7278aaf1d166a"
+checksum = "af728fe826811af3b38c37e93de6d104485953ea373d656eebae53d6987fcd2c"
 dependencies = [
  "peg-macros",
  "peg-runtime",
@@ -367,9 +385,9 @@
 
 [[package]]
 name = "peg-macros"
-version = "0.7.0"
+version = "0.8.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b5aa52829b8decbef693af90202711348ab001456803ba2a98eb4ec8fb70844c"
+checksum = "4536be147b770b824895cbad934fccce8e49f14b4c4946eaa46a6e4a12fcdc16"
 dependencies = [
  "peg-runtime",
  "proc-macro2",
@@ -378,9 +396,9 @@
 
 [[package]]
 name = "peg-runtime"
-version = "0.7.0"
+version = "0.8.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c719dcf55f09a3a7e764c6649ab594c18a177e3599c467983cdf644bfc0a4088"
+checksum = "f9b0efd3ba03c3a409d44d60425f279ec442bcf0b9e63ff4e410da31c8b0f69f"
 
 [[package]]
 name = "proc-macro-error"
@@ -536,12 +554,9 @@
 
 [[package]]
 name = "textwrap"
-version = "0.14.2"
+version = "0.15.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80"
-dependencies = [
- "unicode-width",
-]
+checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"
 
 [[package]]
 name = "thiserror"
@@ -562,12 +577,6 @@
  "quote",
  "syn",
 ]
-
-[[package]]
-name = "unicode-segmentation"
-version = "1.8.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b"
 
 [[package]]
 name = "unicode-width"
@@ -580,12 +589,6 @@
 version = "0.2.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
-
-[[package]]
-name = "vec_map"
-version = "0.8.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
 
 [[package]]
 name = "version_check"
modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -179,7 +179,7 @@
 	}
 }
 
-pub type Result<V> = std::result::Result<V, LocError>;
+pub type Result<V, E = LocError> = std::result::Result<V, E>;
 
 #[macro_export]
 macro_rules! throw {
modifiedcrates/jrsonnet-evaluator/src/function.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function.rs
+++ b/crates/jrsonnet-evaluator/src/function.rs
@@ -8,6 +8,7 @@
 };
 use gcmodule::Trace;
 use jrsonnet_interner::IStr;
+pub use jrsonnet_macros::builtin;
 use jrsonnet_parser::{ArgsDesc, ExprLocation, LocExpr, ParamsDesc};
 use std::{borrow::Cow, collections::HashMap, convert::TryFrom};
 
@@ -377,6 +378,7 @@
 	pub has_default: bool,
 }
 
+/// Do not implement it directly, instead use #[builtin] macro
 pub trait Builtin: Trace {
 	fn name(&self) -> &str;
 	fn params(&self) -> &[BuiltinParam];
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/lib.rs
1#![warn(clippy::all, clippy::nursery)]2#![allow(3	macro_expanded_macro_exports_accessed_by_absolute_paths,4	clippy::ptr_arg5)]67// For jrsonnet-macros8extern crate self as jrsonnet_evaluator;910mod builtin;11mod ctx;12mod dynamic;13pub mod error;14mod evaluate;15pub mod function;16mod import;17mod integrations;18mod map;19pub mod native;20mod obj;21pub mod trace;22pub mod typed;23mod val;2425pub use jrsonnet_parser as parser;2627pub use ctx::*;28pub use dynamic::*;29use error::{Error::*, LocError, Result, StackTraceElement};30pub use evaluate::*;31use function::{Builtin, TlaArg};32use gc::{GcHashMap, TraceBox};33use gcmodule::{Cc, Trace, Weak};34pub use import::*;35pub use jrsonnet_interner::IStr;36use jrsonnet_parser::*;37pub use obj::*;38use std::{39	cell::{Ref, RefCell, RefMut},40	collections::HashMap,41	fmt::Debug,42	path::{Path, PathBuf},43	rc::Rc,44};45use trace::{location_to_offset, offset_to_location, CodeLocation, CompactFormat, TraceFormat};46pub use val::*;47pub mod gc;4849pub trait Bindable: Trace + 'static {50	fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal>;51}5253#[derive(Clone, Trace)]54pub enum LazyBinding {55	Bindable(Cc<TraceBox<dyn Bindable>>),56	Bound(LazyVal),57}5859impl Debug for LazyBinding {60	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {61		write!(f, "LazyBinding")62	}63}64impl LazyBinding {65	pub fn evaluate(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {66		match self {67			Self::Bindable(v) => v.bind(this, super_obj),68			Self::Bound(v) => Ok(v.clone()),69		}70	}71}7273pub struct EvaluationSettings {74	/// Limits recursion by limiting the number of stack frames75	pub max_stack: usize,76	/// Limits amount of stack trace items preserved77	pub max_trace: usize,78	/// Used for s`td.extVar`79	pub ext_vars: HashMap<IStr, Val>,80	/// Used for ext.native81	pub ext_natives: HashMap<IStr, Cc<TraceBox<dyn Builtin>>>,82	/// TLA vars83	pub tla_vars: HashMap<IStr, TlaArg>,84	/// Global variables are inserted in default context85	pub globals: HashMap<IStr, Val>,86	/// Used to resolve file locations/contents87	pub import_resolver: Box<dyn ImportResolver>,88	/// Used in manifestification functions89	pub manifest_format: ManifestFormat,90	/// Used for bindings91	pub trace_format: Box<dyn TraceFormat>,92}93impl Default for EvaluationSettings {94	fn default() -> Self {95		Self {96			max_stack: 200,97			max_trace: 20,98			globals: Default::default(),99			ext_vars: Default::default(),100			ext_natives: Default::default(),101			tla_vars: Default::default(),102			import_resolver: Box::new(DummyImportResolver),103			manifest_format: ManifestFormat::Json(4),104			trace_format: Box::new(CompactFormat {105				padding: 4,106				resolver: trace::PathResolver::Absolute,107			}),108		}109	}110}111112#[derive(Default)]113struct EvaluationData {114	/// Used for stack overflow detection, stacktrace is populated on unwind115	stack_depth: usize,116	/// Updated every time stack entry is popt117	stack_generation: usize,118119	breakpoints: Breakpoints,120	/// Contains file source codes and evaluation results for imports and pretty-printed stacktraces121	files: HashMap<Rc<Path>, FileData>,122	str_files: HashMap<Rc<Path>, IStr>,123}124125pub struct FileData {126	source_code: IStr,127	parsed: LocExpr,128	evaluated: Option<Val>,129}130131#[allow(clippy::type_complexity)]132pub struct Breakpoint {133	loc: ExprLocation,134	collected: RefCell<HashMap<usize, (usize, Vec<Result<Val>>)>>,135}136#[derive(Default)]137struct Breakpoints(Vec<Rc<Breakpoint>>);138impl Breakpoints {139	fn insert(140		&self,141		stack_depth: usize,142		stack_generation: usize,143		loc: &ExprLocation,144		result: Result<Val>,145	) -> Result<Val> {146		if self.0.is_empty() {147			return result;148		}149		for item in self.0.iter() {150			if item.loc.belongs_to(loc) {151				let mut collected = item.collected.borrow_mut();152				let (depth, vals) = collected.entry(stack_generation).or_default();153				if stack_depth > *depth {154					vals.clear();155				}156				vals.push(result.clone());157			}158		}159		result160	}161}162163#[derive(Default)]164pub struct EvaluationStateInternals {165	/// Internal state166	data: RefCell<EvaluationData>,167	/// Settings, safe to change at runtime168	settings: RefCell<EvaluationSettings>,169}170171thread_local! {172	/// Contains the state for a currently executed file.173	/// Global state is fine here.174	pub(crate) static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)175}176pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {177	EVAL_STATE.with(|s| f(s.borrow().as_ref().unwrap()))178}179pub fn push_frame<T>(180	e: Option<&ExprLocation>,181	frame_desc: impl FnOnce() -> String,182	f: impl FnOnce() -> Result<T>,183) -> Result<T> {184	with_state(|s| s.push(e, frame_desc, f))185}186187#[allow(dead_code)]188pub fn push_val_frame(189	e: &ExprLocation,190	frame_desc: impl FnOnce() -> String,191	f: impl FnOnce() -> Result<Val>,192) -> Result<Val> {193	with_state(|s| s.push_val(e, frame_desc, f))194}195#[allow(dead_code)]196pub fn push_description_frame<T>(197	frame_desc: impl FnOnce() -> String,198	f: impl FnOnce() -> Result<T>,199) -> Result<T> {200	with_state(|s| s.push_description(frame_desc, f))201}202203/// Maintains stack trace and import resolution204#[derive(Default, Clone)]205pub struct EvaluationState(Rc<EvaluationStateInternals>);206207impl EvaluationState {208	/// Parses and adds file as loaded209	pub fn add_file(&self, path: Rc<Path>, source_code: IStr) -> Result<LocExpr> {210		let parsed = parse(211			&source_code,212			&ParserSettings {213				file_name: path.clone(),214			},215		)216		.map_err(|error| ImportSyntaxError {217			error: Box::new(error),218			path: path.to_owned(),219			source_code: source_code.clone(),220		})?;221		self.add_parsed_file(path, source_code, parsed.clone())?;222223		Ok(parsed)224	}225226	pub fn reset_evaluation_state(&self, name: &Path) {227		self.data_mut()228			.files229			.get_mut(name)230			.unwrap()231			.evaluated232			.take();233	}234235	/// Adds file by source code and parsed expr236	pub fn add_parsed_file(237		&self,238		name: Rc<Path>,239		source_code: IStr,240		parsed: LocExpr,241	) -> Result<()> {242		self.data_mut().files.insert(243			name,244			FileData {245				source_code,246				parsed,247				evaluated: None,248			},249		);250251		Ok(())252	}253	pub fn get_source(&self, name: &Path) -> Option<IStr> {254		let ro_map = &self.data().files;255		ro_map.get(name).map(|value| value.source_code.clone())256	}257	pub fn map_source_locations(&self, file: &Path, locs: &[usize]) -> Vec<CodeLocation> {258		offset_to_location(&self.get_source(file).unwrap_or_else(|| "".into()), locs)259	}260	pub fn map_from_source_location(261		&self,262		file: &Path,263		line: usize,264		column: usize,265	) -> Option<usize> {266		location_to_offset(&self.get_source(file).unwrap(), line, column)267	}268	pub fn import_file(&self, from: &Path, path: &Path) -> Result<Val> {269		let file_path = self.resolve_file(from, path)?;270		{271			let data = self.data();272			let files = &data.files;273			if files.contains_key(&file_path as &Path) {274				drop(data);275				return self.evaluate_loaded_file_raw(&file_path);276			}277		}278		let contents = self.load_file_contents(&file_path)?;279		self.add_file(file_path.clone(), contents)?;280		self.evaluate_loaded_file_raw(&file_path)281	}282	pub(crate) fn import_file_str(&self, from: &Path, path: &Path) -> Result<IStr> {283		let path = self.resolve_file(from, path)?;284		if !self.data().str_files.contains_key(&path) {285			let file_str = self.load_file_contents(&path)?;286			self.data_mut().str_files.insert(path.clone(), file_str);287		}288		Ok(self.data().str_files.get(&path).cloned().unwrap())289	}290291	fn evaluate_loaded_file_raw(&self, name: &Path) -> Result<Val> {292		let expr: LocExpr = {293			let ro_map = &self.data().files;294			let value = ro_map295				.get(name)296				.unwrap_or_else(|| panic!("file not added: {:?}", name));297			if let Some(ref evaluated) = value.evaluated {298				return Ok(evaluated.clone());299			}300			value.parsed.clone()301		};302		let value = evaluate(self.create_default_context(), &expr)?;303		{304			self.data_mut()305				.files306				.get_mut(name)307				.unwrap()308				.evaluated309				.replace(value.clone());310		}311		Ok(value)312	}313314	/// Adds standard library global variable (std) to this evaluator315	pub fn with_stdlib(&self) -> &Self {316		use jrsonnet_stdlib::STDLIB_STR;317		let std_path: Rc<Path> = PathBuf::from("std.jsonnet").into();318		self.run_in_state(|| {319			self.add_parsed_file(320				std_path.clone(),321				STDLIB_STR.to_owned().into(),322				builtin::get_parsed_stdlib(),323			)324			.unwrap();325			let val = self.evaluate_loaded_file_raw(&std_path).unwrap();326			self.settings_mut().globals.insert("std".into(), val);327		});328		self329	}330331	/// Creates context with all passed global variables332	pub fn create_default_context(&self) -> Context {333		let globals = &self.settings().globals;334		let mut new_bindings = GcHashMap::with_capacity(globals.len());335		for (name, value) in globals.iter() {336			new_bindings.insert(name.clone(), LazyVal::new_resolved(value.clone()));337		}338		Context::new().extend_bound(new_bindings)339	}340341	/// Executes code creating a new stack frame342	pub fn push<T>(343		&self,344		e: Option<&ExprLocation>,345		frame_desc: impl FnOnce() -> String,346		f: impl FnOnce() -> Result<T>,347	) -> Result<T> {348		{349			let mut data = self.data_mut();350			let stack_depth = &mut data.stack_depth;351			if *stack_depth > self.max_stack() {352				// Error creation uses data, so i drop guard here353				drop(data);354				throw!(StackOverflow);355			} else {356				*stack_depth += 1;357			}358		}359		let result = f();360		{361			let mut data = self.data_mut();362			data.stack_depth -= 1;363			data.stack_generation += 1;364		}365		if let Err(mut err) = result {366			err.trace_mut().0.push(StackTraceElement {367				location: e.cloned(),368				desc: frame_desc(),369			});370			return Err(err);371		}372		result373	}374375	/// Executes code creating a new stack frame376	pub fn push_val(377		&self,378		e: &ExprLocation,379		frame_desc: impl FnOnce() -> String,380		f: impl FnOnce() -> Result<Val>,381	) -> Result<Val> {382		{383			let mut data = self.data_mut();384			let stack_depth = &mut data.stack_depth;385			if *stack_depth > self.max_stack() {386				// Error creation uses data, so i drop guard here387				drop(data);388				throw!(StackOverflow);389			} else {390				*stack_depth += 1;391			}392		}393		let mut result = f();394		{395			let mut data = self.data_mut();396			data.stack_depth -= 1;397			data.stack_generation += 1;398			result = data399				.breakpoints400				.insert(data.stack_depth, data.stack_generation, e, result);401		}402		if let Err(mut err) = result {403			err.trace_mut().0.push(StackTraceElement {404				location: Some(e.clone()),405				desc: frame_desc(),406			});407			return Err(err);408		}409		result410	}411	/// Executes code creating a new stack frame412	pub fn push_description<T>(413		&self,414		frame_desc: impl FnOnce() -> String,415		f: impl FnOnce() -> Result<T>,416	) -> Result<T> {417		{418			let mut data = self.data_mut();419			let stack_depth = &mut data.stack_depth;420			if *stack_depth > self.max_stack() {421				// Error creation uses data, so i drop guard here422				drop(data);423				throw!(StackOverflow);424			} else {425				*stack_depth += 1;426			}427		}428		let result = f();429		{430			let mut data = self.data_mut();431			data.stack_depth -= 1;432			data.stack_generation += 1;433		}434		if let Err(mut err) = result {435			err.trace_mut().0.push(StackTraceElement {436				location: None,437				desc: frame_desc(),438			});439			return Err(err);440		}441		result442	}443444	/// Runs passed function in state (required if function needs to modify stack trace)445	pub fn run_in_state<T>(&self, f: impl FnOnce() -> T) -> T {446		EVAL_STATE.with(|v| {447			let has_state = v.borrow().is_some();448			if !has_state {449				v.borrow_mut().replace(self.clone());450			}451			let result = f();452			if !has_state {453				v.borrow_mut().take();454			}455			result456		})457	}458	pub fn run_in_state_with_breakpoint(459		&self,460		bp: Rc<Breakpoint>,461		f: impl FnOnce() -> Result<()>,462	) -> Result<()> {463		{464			let mut data = self.data_mut();465			data.breakpoints.0.push(bp);466		}467468		let result = self.run_in_state(f);469470		{471			let mut data = self.data_mut();472			data.breakpoints.0.pop();473		}474475		result476	}477478	pub fn stringify_err(&self, e: &LocError) -> String {479		let mut out = String::new();480		self.settings()481			.trace_format482			.write_trace(&mut out, self, e)483			.unwrap();484		out485	}486487	pub fn manifest(&self, val: Val) -> Result<IStr> {488		self.run_in_state(|| {489			push_description_frame(490				|| "manifestification".to_string(),491				|| val.manifest(&self.manifest_format()),492			)493		})494	}495	pub fn manifest_multi(&self, val: Val) -> Result<Vec<(IStr, IStr)>> {496		self.run_in_state(|| val.manifest_multi(&self.manifest_format()))497	}498	pub fn manifest_stream(&self, val: Val) -> Result<Vec<IStr>> {499		self.run_in_state(|| val.manifest_stream(&self.manifest_format()))500	}501502	/// If passed value is function then call with set TLA503	pub fn with_tla(&self, val: Val) -> Result<Val> {504		self.run_in_state(|| {505			Ok(match val {506				Val::Func(func) => push_description_frame(507					|| "during TLA call".to_owned(),508					|| {509						func.evaluate(510							self.create_default_context(),511							None,512							&self.settings().tla_vars,513							true,514						)515					},516				)?,517				v => v,518			})519		})520	}521}522523/// Internals524impl EvaluationState {525	fn data(&self) -> Ref<EvaluationData> {526		self.0.data.borrow()527	}528	fn data_mut(&self) -> RefMut<EvaluationData> {529		self.0.data.borrow_mut()530	}531	pub fn settings(&self) -> Ref<EvaluationSettings> {532		self.0.settings.borrow()533	}534	pub fn settings_mut(&self) -> RefMut<EvaluationSettings> {535		self.0.settings.borrow_mut()536	}537}538539/// Raw methods evaluate passed values but don't perform TLA execution540impl EvaluationState {541	pub fn evaluate_file_raw(&self, name: &Path) -> Result<Val> {542		self.run_in_state(|| self.import_file(&std::env::current_dir().expect("cwd"), name))543	}544	pub fn evaluate_file_raw_nocwd(&self, name: &Path) -> Result<Val> {545		self.run_in_state(|| self.import_file(&PathBuf::from("."), name))546	}547	/// Parses and evaluates the given snippet548	pub fn evaluate_snippet_raw(&self, source: Rc<Path>, code: IStr) -> Result<Val> {549		let parsed = parse(550			&code,551			&ParserSettings {552				file_name: source.clone(),553			},554		)555		.map_err(|e| ImportSyntaxError {556			path: source.clone(),557			source_code: code.clone(),558			error: Box::new(e),559		})?;560		self.add_parsed_file(source, code, parsed.clone())?;561		self.evaluate_expr_raw(parsed)562	}563	/// Evaluates the parsed expression564	pub fn evaluate_expr_raw(&self, code: LocExpr) -> Result<Val> {565		self.run_in_state(|| evaluate(self.create_default_context(), &code))566	}567}568569/// Settings utilities570impl EvaluationState {571	pub fn add_ext_var(&self, name: IStr, value: Val) {572		self.settings_mut().ext_vars.insert(name, value);573	}574	pub fn add_ext_str(&self, name: IStr, value: IStr) {575		self.add_ext_var(name, Val::Str(value));576	}577	pub fn add_ext_code(&self, name: IStr, code: IStr) -> Result<()> {578		let value =579			self.evaluate_snippet_raw(PathBuf::from(format!("ext_code {}", name)).into(), code)?;580		self.add_ext_var(name, value);581		Ok(())582	}583584	pub fn add_tla(&self, name: IStr, value: Val) {585		self.settings_mut()586			.tla_vars587			.insert(name, TlaArg::Val(value));588	}589	pub fn add_tla_str(&self, name: IStr, value: IStr) {590		self.settings_mut()591			.tla_vars592			.insert(name, TlaArg::String(value));593	}594	pub fn add_tla_code(&self, name: IStr, code: IStr) -> Result<()> {595		let parsed = self.add_file(PathBuf::from(format!("tla_code {}", name)).into(), code)?;596		self.settings_mut()597			.tla_vars598			.insert(name, TlaArg::Code(parsed));599		Ok(())600	}601602	pub fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {603		self.settings().import_resolver.resolve_file(from, path)604	}605	pub fn load_file_contents(&self, path: &Path) -> Result<IStr> {606		self.settings().import_resolver.load_file_contents(path)607	}608609	pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {610		Ref::map(self.settings(), |s| &*s.import_resolver)611	}612	pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {613		self.settings_mut().import_resolver = resolver;614	}615616	pub fn add_native(&self, name: IStr, cb: Cc<TraceBox<dyn Builtin>>) {617		self.settings_mut().ext_natives.insert(name, cb);618	}619620	pub fn manifest_format(&self) -> ManifestFormat {621		self.settings().manifest_format.clone()622	}623	pub fn set_manifest_format(&self, format: ManifestFormat) {624		self.settings_mut().manifest_format = format;625	}626627	pub fn trace_format(&self) -> Ref<dyn TraceFormat> {628		Ref::map(self.settings(), |s| &*s.trace_format)629	}630	pub fn set_trace_format(&self, format: Box<dyn TraceFormat>) {631		self.settings_mut().trace_format = format;632	}633634	pub fn max_trace(&self) -> usize {635		self.settings().max_trace636	}637	pub fn set_max_trace(&self, trace: usize) {638		self.settings_mut().max_trace = trace;639	}640641	pub fn max_stack(&self) -> usize {642		self.settings().max_stack643	}644	pub fn set_max_stack(&self, trace: usize) {645		self.settings_mut().max_stack = trace;646	}647}648649pub fn cc_ptr_eq<T>(a: &Cc<T>, b: &Cc<T>) -> bool {650	let a = a as &T;651	let b = b as &T;652	std::ptr::eq(a, b)653}654655fn weak_raw<T>(a: Weak<T>) -> *const () {656	unsafe { std::mem::transmute(a) }657}658fn weak_ptr_eq<T>(a: Weak<T>, b: Weak<T>) -> bool {659	std::ptr::eq(weak_raw(a), weak_raw(b))660}661662#[test]663fn weak_unsafe() {664	let a = Cc::new(1);665	let b = Cc::new(2);666667	let aw1 = a.clone().downgrade();668	let aw2 = a.clone().downgrade();669	let aw3 = a.clone().downgrade();670671	let bw = b.clone().downgrade();672673	assert!(weak_ptr_eq(aw1, aw2));674	assert!(!weak_ptr_eq(aw3, bw));675}676677#[cfg(test)]678pub mod tests {679	use super::Val;680	use crate::{681		error::Error::*, function::BuiltinParam, gc::TraceBox, native::NativeCallbackHandler,682		primitive_equals, EvaluationState,683	};684	use gcmodule::{Cc, Trace};685	use jrsonnet_interner::IStr;686	use jrsonnet_parser::*;687	use std::{688		path::{Path, PathBuf},689		rc::Rc,690	};691692	#[test]693	#[should_panic]694	fn eval_state_stacktrace() {695		let state = EvaluationState::default();696		state.run_in_state(|| {697			state698				.push(699					Some(&ExprLocation(PathBuf::from("test1.jsonnet").into(), 10, 20)),700					|| "outer".to_owned(),701					|| {702						state.push(703							Some(&ExprLocation(PathBuf::from("test2.jsonnet").into(), 30, 40)),704							|| "inner".to_owned(),705							|| Err(RuntimeError("".into()).into()),706						)?;707						Ok(Val::Null)708					},709				)710				.unwrap();711		});712	}713714	#[test]715	fn eval_state_standard() {716		let state = EvaluationState::default();717		state.with_stdlib();718		assert!(primitive_equals(719			&state720				.evaluate_snippet_raw(721					PathBuf::from("raw.jsonnet").into(),722					r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#.into()723				)724				.unwrap(),725			&Val::Bool(true),726		)727		.unwrap());728	}729730	macro_rules! eval {731		($str: expr) => {732			EvaluationState::default()733				.with_stdlib()734				.evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())735				.unwrap()736		};737	}738	macro_rules! eval_json {739		($str: expr) => {{740			let evaluator = EvaluationState::default();741			evaluator.with_stdlib();742			evaluator.run_in_state(|| {743				evaluator744					.evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())745					.unwrap()746					.to_json(0)747					.unwrap()748					.replace("\n", "")749			})750		}};751	}752753	/// Asserts given code returns `true`754	macro_rules! assert_eval {755		($str: expr) => {756			assert!(primitive_equals(&eval!($str), &Val::Bool(true)).unwrap())757		};758	}759760	/// Asserts given code returns `false`761	macro_rules! assert_eval_neg {762		($str: expr) => {763			assert!(primitive_equals(&eval!($str), &Val::Bool(false)).unwrap())764		};765	}766	macro_rules! assert_json {767		($str: expr, $out: expr) => {768			assert_eq!(eval_json!($str), $out.replace("\t", ""))769		};770	}771772	/// Sanity checking, before trusting to another tests773	#[test]774	fn equality_operator() {775		assert_eval!("2 == 2");776		assert_eval_neg!("2 != 2");777		assert_eval!("2 != 3");778		assert_eval_neg!("2 == 3");779		assert_eval!("'Hello' == 'Hello'");780		assert_eval_neg!("'Hello' != 'Hello'");781		assert_eval!("'Hello' != 'World'");782		assert_eval_neg!("'Hello' == 'World'");783	}784785	#[test]786	fn math_evaluation() {787		assert_eval!("2 + 2 * 2 == 6");788		assert_eval!("3 + (2 + 2 * 2) == 9");789	}790791	#[test]792	fn string_concat() {793		assert_eval!("'Hello' + 'World' == 'HelloWorld'");794		assert_eval!("'Hello' * 3 == 'HelloHelloHello'");795		assert_eval!("'Hello' + 'World' * 3 == 'HelloWorldWorldWorld'");796	}797798	#[test]799	fn faster_join() {800		assert_eval!("std.join([0,0], [[1,2],[3,4],[5,6]]) == [1,2,0,0,3,4,0,0,5,6]");801		assert_eval!("std.join(',', ['1','2','3','4']) == '1,2,3,4'");802	}803804	#[test]805	fn function_contexts() {806		assert_eval!(807			r#"808				local k = {809					t(name = self.h): [self.h, name],810					h: 3,811				};812				local f = {813					t: k.t(),814					h: 4,815				};816				f.t[0] == f.t[1]817			"#818		);819	}820821	#[test]822	fn local() {823		assert_eval!("local a = 2; local b = 3; a + b == 5");824		assert_eval!("local a = 1, b = a + 1; a + b == 3");825		assert_eval!("local a = 1; local a = 2; a == 2");826	}827828	#[test]829	fn object_lazyness() {830		assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);831	}832833	#[test]834	fn object_inheritance() {835		assert_json!("{a: self.b} + {b:3}", r#"{"a": 3,"b": 3}"#);836	}837838	#[test]839	fn object_assertion_success() {840		eval!("{assert \"a\" in self} + {a:2}");841	}842843	#[test]844	fn object_assertion_error() {845		eval!("{assert \"a\" in self}");846	}847848	#[test]849	fn lazy_args() {850		eval!("local test(a) = 2; test(error '3')");851	}852853	#[test]854	#[should_panic]855	fn tailstrict_args() {856		eval!("local test(a) = 2; test(error '3') tailstrict");857	}858859	#[test]860	#[should_panic]861	fn no_binding_error() {862		eval!("a");863	}864865	#[test]866	fn test_object() {867		assert_json!("{a:2}", r#"{"a": 2}"#);868		assert_json!("{a:2+2}", r#"{"a": 4}"#);869		assert_json!("{a:2}+{b:2}", r#"{"a": 2,"b": 2}"#);870		assert_json!("{b:3}+{b:2}", r#"{"b": 2}"#);871		assert_json!("{b:3}+{b+:2}", r#"{"b": 5}"#);872		assert_json!("local test='a'; {[test]:2}", r#"{"a": 2}"#);873		assert_json!(874			r#"875				{876					name: "Alice",877					welcome: "Hello " + self.name + "!",878				}879			"#,880			r#"{"name": "Alice","welcome": "Hello Alice!"}"#881		);882		assert_json!(883			r#"884				{885					name: "Alice",886					welcome: "Hello " + self.name + "!",887				} + {888					name: "Bob"889				}890			"#,891			r#"{"name": "Bob","welcome": "Hello Bob!"}"#892		);893	}894895	#[test]896	fn functions() {897		assert_json!(r#"local a = function(b, c = 2) b + c; a(2)"#, "4");898		assert_json!(899			r#"local a = function(b, c = "Dear") b + c + d, d = "World"; a("Hello")"#,900			r#""HelloDearWorld""#901		);902	}903904	#[test]905	fn local_methods() {906		assert_json!(r#"local a(b, c = 2) = b + c; a(2)"#, "4");907		assert_json!(908			r#"local a(b, c = "Dear") = b + c + d, d = "World"; a("Hello")"#,909			r#""HelloDearWorld""#910		);911	}912913	#[test]914	fn object_locals() {915		assert_json!(r#"{local a = 3, b: a}"#, r#"{"b": 3}"#);916		assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b": 3}"#);917		assert_json!(918			r#"{local a = function (b) {[b]:4}, test: a("test")}"#,919			r#"{"test": {"test": 4}}"#920		);921	}922923	#[test]924	fn object_comp() {925		assert_json!(926			r#"{local t = "a", ["h"+i+"_"+z]: if "h"+(i-1)+"_"+z in self then t+1 else 0+t for i in [1,2,3] for z in [2,3,4] if z != i}"#,927			"{\"h1_2\": \"0a\",\"h1_3\": \"0a\",\"h1_4\": \"0a\",\"h2_3\": \"a1\",\"h2_4\": \"a1\",\"h3_2\": \"0a\",\"h3_4\": \"a1\"}"928		)929	}930931	#[test]932	fn direct_self() {933		println!(934			"{:#?}",935			eval!(936				r#"937					{938						local me = self,939						a: 3,940						b(): me.a,941					}942				"#943			)944		);945	}946947	#[test]948	fn indirect_self() {949		// `self` assigned to `me` was lost when being950		// referenced from field951		eval!(952			r#"{953				local me = self,954				a: 3,955				b: me.a,956			}.b"#957		);958	}959960	// We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly961	#[test]962	fn std_assert_ok() {963		eval!("std.assertEqual(4.5 << 2, 16)");964	}965966	#[test]967	#[should_panic]968	fn std_assert_failure() {969		eval!("std.assertEqual(4.5 << 2, 15)");970	}971972	#[test]973	fn string_is_string() {974		assert!(primitive_equals(975			&eval!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),976			&Val::Bool(false),977		)978		.unwrap());979	}980981	#[test]982	fn base64_works() {983		assert_json!(r#"std.base64("test")"#, r#""dGVzdA==""#);984	}985986	#[test]987	fn utf8_chars() {988		assert_json!(989			r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,990			r#"{"c": 128526,"l": 1}"#991		)992	}993994	#[test]995	fn json() {996		assert_json!(997			r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,998			r#""{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}""#999		);1000	}10011002	#[test]1003	fn json_minified() {1004		assert_json!(1005			r#"std.manifestJsonMinified({a:3, b:4, c:6})"#,1006			r#""{\"a\":3,\"b\":4,\"c\":6}""#1007		);1008	}10091010	#[test]1011	fn parse_json() {1012		assert_json!(1013			r#"std.parseJson('{"a": -1,"b": 1,"c": 3.141,"d": []}')"#,1014			r#"{"a": -1,"b": 1,"c": 3.141,"d": []}"#1015		);1016	}10171018	#[test]1019	fn test() {1020		assert_json!(1021			r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,1022			"[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"1023		);1024	}10251026	#[test]1027	fn sjsonnet() {1028		eval!(1029			r#"1030			local x0 = {k: 1};1031			local x1 = {k: x0.k + x0.k};1032			local x2 = {k: x1.k + x1.k};1033			local x3 = {k: x2.k + x2.k};1034			local x4 = {k: x3.k + x3.k};1035			local x5 = {k: x4.k + x4.k};1036			local x6 = {k: x5.k + x5.k};1037			local x7 = {k: x6.k + x6.k};1038			local x8 = {k: x7.k + x7.k};1039			local x9 = {k: x8.k + x8.k};1040			local x10 = {k: x9.k + x9.k};1041			local x11 = {k: x10.k + x10.k};1042			local x12 = {k: x11.k + x11.k};1043			local x13 = {k: x12.k + x12.k};1044			local x14 = {k: x13.k + x13.k};1045			local x15 = {k: x14.k + x14.k};1046			local x16 = {k: x15.k + x15.k};1047			local x17 = {k: x16.k + x16.k};1048			local x18 = {k: x17.k + x17.k};1049			local x19 = {k: x18.k + x18.k};1050			local x20 = {k: x19.k + x19.k};1051			local x21 = {k: x20.k + x20.k};1052			x21.k1053		"#1054		);1055	}10561057	// This test is commented out by default, because of huge compilation slowdown1058	// #[bench]1059	// fn bench_codegen(b: &mut Bencher) {1060	// 	b.iter(|| {1061	// 		#[allow(clippy::all)]1062	// 		let stdlib = {1063	// 			use jrsonnet_parser::*;1064	// 			include!(concat!(env!("OUT_DIR"), "/stdlib.rs"))1065	// 		};1066	// 		stdlib1067	// 	})1068	// }10691070	/*1071	#[bench]1072	fn bench_serialize(b: &mut Bencher) {1073		b.iter(|| {1074			bincode::deserialize::<jrsonnet_parser::LocExpr>(include_bytes!(concat!(1075				env!("OUT_DIR"),1076				"/stdlib.bincode"1077			)))1078			.expect("deserialize stdlib")1079		})1080	}10811082	#[bench]1083	fn bench_parse(b: &mut Bencher) {1084		b.iter(|| {1085			jrsonnet_parser::parse(1086				jrsonnet_stdlib::STDLIB_STR,1087				&jrsonnet_parser::ParserSettings {1088					loc_data: true,1089					file_name: Rc::new(PathBuf::from("std.jsonnet")),1090				},1091			)1092		})1093	}1094	*/10951096	#[test]1097	fn equality() {1098		println!(1099			"{:?}",1100			jrsonnet_parser::parse(1101				"{ x: 1, y: 2 } == { x: 1, y: 2 }",1102				&ParserSettings {1103					file_name: PathBuf::from("equality").into(),1104				}1105			)1106		);1107		assert_eval!("{ x: 1, y: 2 } == { x: 1, y: 2 }")1108	}11091110	#[test]1111	fn native_ext() -> crate::error::Result<()> {1112		use super::native::NativeCallback;1113		let evaluator = EvaluationState::default();11141115		evaluator.with_stdlib();11161117		#[derive(Trace)]1118		struct NativeAdd;1119		impl NativeCallbackHandler for NativeAdd {1120			fn call(&self, from: Option<Rc<Path>>, args: &[Val]) -> crate::error::Result<Val> {1121				assert_eq!(1122					&from.unwrap() as &Path,1123					&PathBuf::from("native_caller.jsonnet")1124				);1125				match (&args[0], &args[1]) {1126					(Val::Num(a), Val::Num(b)) => Ok(Val::Num(a + b)),1127					(_, _) => unreachable!(),1128				}1129			}1130		}1131		evaluator.settings_mut().ext_natives.insert(1132			"native_add".into(),1133			#[allow(deprecated)]1134			Cc::new(TraceBox(Box::new(NativeCallback::new(1135				vec![1136					BuiltinParam {1137						name: "a".into(),1138						has_default: false,1139					},1140					BuiltinParam {1141						name: "b".into(),1142						has_default: false,1143					},1144				],1145				TraceBox(Box::new(NativeAdd)),1146			)))),1147		);1148		evaluator.evaluate_snippet_raw(1149			PathBuf::from("native_caller.jsonnet").into(),1150			"std.assertEqual(std.native(\"native_add\")(1, 2), 3)".into(),1151		)?;1152		Ok(())1153	}11541155	#[test]1156	fn constant_intrinsic() -> crate::error::Result<()> {1157		assert_eval!(1158			"local std2 = std; local std = std2 { primitiveEquals(a, b):: false }; 1 == 1"1159		);1160		Ok(())1161	}11621163	#[test]1164	fn standalone_super() -> crate::error::Result<()> {1165		assert_eval!(1166			r#"1167			local obj = {1168				a: 1,1169				b: 2,1170				c: 3,1171			};1172			local test = obj + {1173				fields: std.objectFields(super),1174				d: 5,1175			};1176			test.fields == ['a', 'b', 'c']1177		"#1178		);1179		Ok(())1180	}11811182	#[test]1183	fn comp_self() -> crate::error::Result<()> {1184		assert_eval!(1185			r#"1186			std.objectFields({1187				a:{1188					[name]: name for name in std.objectFields(self)1189				},1190				b: 2,1191				c: 3,1192			}.a) == ['a', 'b', 'c']1193			"#1194		);11951196		Ok(())1197	}11981199	struct TestImportResolver(IStr);1200	impl crate::import::ImportResolver for TestImportResolver {1201		fn resolve_file(&self, _: &Path, _: &Path) -> crate::error::Result<Rc<Path>> {1202			Ok(PathBuf::from("/test").into())1203		}12041205		fn load_file_contents(&self, _: &Path) -> crate::error::Result<IStr> {1206			Ok(self.0.clone())1207		}12081209		unsafe fn as_any(&self) -> &dyn std::any::Any {1210			panic!()1211		}1212	}12131214	#[test]1215	fn issue_23() {1216		let state = EvaluationState::default();1217		state.set_import_resolver(Box::new(TestImportResolver(r#"import "/test""#.into())));1218		let _ = state.evaluate_file_raw(&PathBuf::from("/test"));1219	}12201221	#[test]1222	fn issue_40() {1223		let state = EvaluationState::default();1224		state.with_stdlib();12251226		let error = state1227			.evaluate_snippet_raw(1228				PathBuf::from("issue40.jsonnet").into(),1229				r#"1230				local conf = {1231					n: ""1232				};12331234				local result = conf + {1235					assert std.isNumber(self.n): "is number"1236				};12371238				std.manifestJsonEx(result, "")1239			"#1240				.into(),1241			)1242			.unwrap_err();1243		assert_eq!(error.error().to_string(), "assert failed: is number");1244	}12451246	#[test]1247	fn test_ascii_upper_lower() {1248		assert_eval!(r#"std.assertEqual(std.asciiUpper("aBc😀"), "ABC😀")"#);1249		assert_eval!(r#"std.assertEqual(std.asciiLower("aBc😀"), "abc😀")"#);1250	}12511252	#[test]1253	fn test_member() {1254		assert_eval!(r#"!std.member("", "")"#);1255		assert_eval!(r#"std.member("abc", "a")"#);1256		assert_eval!(r#"!std.member("abc", "d")"#);1257		assert_eval!(r#"!std.member([], "")"#);1258		assert_eval!(r#"std.member(["a", "b", "c"], "a")"#);1259		assert_eval!(r#"!std.member(["a", "b", "c"], "d")"#);1260	}12611262	#[test]1263	fn test_count() {1264		assert_eval!(r#"std.assertEqual(std.count([], ""), 0)"#);1265		assert_eval!(r#"std.assertEqual(std.count(["a", "b", "a"], "d"), 0)"#);1266		assert_eval!(r#"std.assertEqual(std.count(["a", "b", "a"], "a"), 2)"#);1267	}1268}
modifiedcrates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -1,6 +1,7 @@
 use std::convert::{TryFrom, TryInto};
 
 use jrsonnet_interner::IStr;
+pub use jrsonnet_macros::Typed;
 use jrsonnet_types::{ComplexValType, ValType};
 
 use crate::{
modifiedcrates/jrsonnet-evaluator/src/typed/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/typed/mod.rs
+++ b/crates/jrsonnet-evaluator/src/typed/mod.rs
@@ -8,20 +8,8 @@
 	push_description_frame, Val,
 };
 use gcmodule::Trace;
-use jrsonnet_types::{ComplexValType, ValType};
+pub use jrsonnet_types::{ComplexValType, ValType};
 use thiserror::Error;
-
-#[macro_export]
-macro_rules! unwrap_type {
-	($desc:expr, $value:expr, $typ:expr => $match:path) => {{
-		use $crate::{push_frame, typed::CheckType};
-		push_frame(None, $desc, || Ok($typ.check(&$value)?))?;
-		match $value {
-			$match(v) => v,
-			_ => unreachable!(),
-		}
-	}};
-}
 
 #[derive(Debug, Error, Clone, Trace)]
 pub enum TypeError {
@@ -136,7 +124,7 @@
 impl Display for ValuePathItem {
 	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 		match self {
-			Self::Field(name) => write!(f, ".{}", name)?,
+			Self::Field(name) => write!(f, ".{:?}", name)?,
 			Self::Index(idx) => write!(f, "[{}]", idx)?,
 		}
 		Ok(())
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -91,7 +91,7 @@
 	Normal(Cc<FuncDesc>),
 	/// Standard library function
 	StaticBuiltin(#[skip_trace] &'static dyn StaticBuiltin),
-
+	/// User-provided function
 	Builtin(Cc<TraceBox<dyn Builtin>>),
 }
 
@@ -99,8 +99,10 @@
 	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 		match self {
 			Self::Normal(arg0) => f.debug_tuple("Normal").field(arg0).finish(),
-			Self::StaticBuiltin(arg0) => f.debug_tuple("Intrinsic").field(&arg0.name()).finish(),
-			Self::Builtin(arg0) => f.debug_tuple("Intrinsic").field(&arg0.name()).finish(),
+			Self::StaticBuiltin(arg0) => {
+				f.debug_tuple("StaticBuiltin").field(&arg0.name()).finish()
+			}
+			Self::Builtin(arg0) => f.debug_tuple("Builtin").field(&arg0.name()).finish(),
 		}
 	}
 }
@@ -338,6 +340,49 @@
 }
 
 impl Val {
+	pub fn as_bool(&self) -> Option<bool> {
+		match self {
+			Val::Bool(v) => Some(*v),
+			_ => None,
+		}
+	}
+	pub fn as_null(&self) -> Option<()> {
+		match self {
+			Val::Null => Some(()),
+			_ => None,
+		}
+	}
+	pub fn as_str(&self) -> Option<IStr> {
+		match self {
+			Val::Str(s) => Some(s.clone()),
+			_ => None,
+		}
+	}
+	pub fn as_num(&self) -> Option<f64> {
+		match self {
+			Val::Num(n) => Some(*n),
+			_ => None,
+		}
+	}
+	pub fn as_arr(&self) -> Option<ArrValue> {
+		match self {
+			Val::Arr(a) => Some(a.clone()),
+			_ => None,
+		}
+	}
+	pub fn as_obj(&self) -> Option<ObjValue> {
+		match self {
+			Val::Obj(o) => Some(o.clone()),
+			_ => None,
+		}
+	}
+	pub fn as_func(&self) -> Option<FuncVal> {
+		match self {
+			Val::Func(f) => Some(f.clone()),
+			_ => None,
+		}
+	}
+
 	/// Creates `Val::Num` after checking for numeric overflow.
 	/// As numbers are `f64`, we can just check for their finity.
 	pub fn new_checked_num(num: f64) -> Result<Self> {
modifiedcrates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -1,8 +1,8 @@
 use quote::{quote, quote_spanned};
 use syn::{
 	parenthesized, parse::Parse, parse_macro_input, punctuated::Punctuated, spanned::Spanned,
-	token::Comma, FnArg, GenericArgument, Ident, ItemFn, Pat, PatType, Path, PathArguments, Token,
-	Type,
+	token::Comma, DeriveInput, FnArg, GenericArgument, Ident, ItemFn, Pat, PatType, Path,
+	PathArguments, Token, Type,
 };
 
 fn is_location_arg(t: &PatType) -> bool {
@@ -254,3 +254,86 @@
 	})
 	.into()
 }
+
+#[proc_macro_derive(Typed)]
+pub fn derive_typed(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
+	let input = parse_macro_input!(item as DeriveInput);
+	let data = match &input.data {
+		syn::Data::Struct(s) => s,
+		_ => {
+			return syn::Error::new(input.span(), "only structs supported")
+				.to_compile_error()
+				.into()
+		}
+	};
+
+	let ident = &input.ident;
+
+	let fields_def = data.fields.iter().map(|f| {
+		let name = f
+			.ident
+			.as_ref()
+			.expect("only named fields supported")
+			.to_string();
+		let ty = &f.ty;
+		quote! {
+			(#name, #ty::TYPE),
+		}
+	});
+	let fields_parse = data.fields.iter().map(|f| {
+		let ident = f.ident.as_ref().unwrap();
+		let name = ident.to_string();
+		let ty = &f.ty;
+		quote! {
+			#ident: #ty::try_from(obj.get(#name.into())?.expect("shape is correct"))?,
+		}
+	});
+	let fields_serialize = data.fields.iter().map(|f| {
+		let ident = f.ident.as_ref().unwrap();
+		let name = ident.to_string();
+		quote! {
+			out.member(#name.into()).value(self.#ident.try_into()?);
+		}
+	});
+	let field_count = data.fields.len();
+
+	quote! {
+		const _: () = {
+			use ::jrsonnet_evaluator::{
+				typed::{ComplexValType, Typed, CheckType},
+				Val,
+				error::LocError,
+				obj::ObjValueBuilder,
+			};
+
+			const ITEMS: [(&'static str, &'static ComplexValType); #field_count] = [
+				#(#fields_def)*
+			];
+			impl Typed for #ident {
+				const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&ITEMS);
+			}
+
+			impl TryFrom<Val> for #ident {
+				type Error = LocError;
+				fn try_from(value: Val) -> Result<Self, Self::Error> {
+					<Self as Typed>::TYPE.check(&value)?;
+					let obj = value.as_obj().expect("shape is correct");
+
+					Ok(Self {
+						#(#fields_parse)*
+					})
+				}
+			}
+			impl TryInto<Val> for #ident {
+				type Error = LocError;
+				fn try_into(self) -> Result<Val, Self::Error> {
+					let mut out = ObjValueBuilder::new();
+					#(#fields_serialize)*
+					Ok(Val::Obj(out.build()))
+				}
+			}
+			()
+		};
+	}
+	.into()
+}
modifiedcrates/jrsonnet-types/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-types/src/lib.rs
+++ b/crates/jrsonnet-types/src/lib.rs
@@ -122,7 +122,7 @@
 	BoundedNumber(Option<f64>, Option<f64>),
 	Array(Box<ComplexValType>),
 	ArrayRef(&'static ComplexValType),
-	ObjectRef(&'static [(&'static str, ComplexValType)]),
+	ObjectRef(&'static [(&'static str, &'static ComplexValType)]),
 	Union(Vec<ComplexValType>),
 	UnionRef(&'static [&'static ComplexValType]),
 	Sum(Vec<ComplexValType>),