git.delta.rocks / jrsonnet / refs/commits / 028057c85dc5

difftreelog

refactor! remove Lazy from Val

Yaroslav Bolyukin2020-12-01parent: #194642f.patch.diff
in: master
Now lazy is used only in objects/arrays
BREAKING CHANGE: value_type() is never fails

9 files changed

modifiedbindings/jsonnet/src/val_extract.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/val_extract.rs
+++ b/bindings/jsonnet/src/val_extract.rs
@@ -9,7 +9,7 @@
 
 #[no_mangle]
 pub extern "C" fn jsonnet_json_extract_string(_vm: &EvaluationState, v: &Val) -> *mut c_char {
-	match v.unwrap_if_lazy().unwrap() {
+	match v {
 		Val::Str(s) => CString::new(&*s as &str).unwrap().into_raw(),
 		_ => std::ptr::null_mut(),
 	}
@@ -20,9 +20,9 @@
 	v: &Val,
 	out: &mut c_double,
 ) -> c_int {
-	match v.unwrap_if_lazy().unwrap() {
+	match v {
 		Val::Num(n) => {
-			*out = n;
+			*out = *n;
 			1
 		}
 		_ => 0,
@@ -30,7 +30,7 @@
 }
 #[no_mangle]
 pub extern "C" fn jsonnet_json_extract_bool(_vm: &EvaluationState, v: &Val) -> c_int {
-	match v.unwrap_if_lazy().unwrap() {
+	match v {
 		Val::Bool(false) => 0,
 		Val::Bool(true) => 1,
 		_ => 2,
@@ -38,7 +38,7 @@
 }
 #[no_mangle]
 pub extern "C" fn jsonnet_json_extract_null(_vm: &EvaluationState, v: &Val) -> c_int {
-	match v.unwrap_if_lazy().unwrap() {
+	match v {
 		Val::Null => 1,
 		_ => 0,
 	}
modifiedcrates/jrsonnet-evaluator/src/builtin/format.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/builtin/format.rs
1//! faster std.format impl2#![allow(clippy::too_many_arguments)]34use crate::{error::Error::*, throw, LocError, ObjValue, Result, Val, ValType};5use thiserror::Error;67#[derive(Debug, Clone, Error)]8pub enum FormatError {9	#[error("truncated format code")]10	TruncatedFormatCode,11	#[error("unrecognized conversion type: {0}")]12	UnrecognizedConversionType(char),1314	#[error("not enough values")]15	NotEnoughValues,1617	#[error("cannot use * width with object")]18	CannotUseStarWidthWithObject,19	#[error("mapping keys required")]20	MappingKeysRequired,21	#[error("no such format field: {0}")]22	NoSuchFormatField(Rc<str>),23}2425impl From<FormatError> for LocError {26	fn from(e: FormatError) -> Self {27		Self::new(Format(e))28	}29}3031use std::rc::Rc;32use FormatError::*;3334type ParseResult<'t, T> = std::result::Result<(T, &'t str), FormatError>;3536pub fn try_parse_mapping_key(str: &str) -> ParseResult<&str> {37	if str.is_empty() {38		return Err(TruncatedFormatCode);39	}40	let bytes = str.as_bytes();41	if bytes[0] == b'(' {42		let mut i = 1;43		while i < bytes.len() {44			if bytes[i] == b')' {45				return Ok((&str[1..i as usize], &str[i as usize + 1..]));46			}47			i += 1;48		}49		Err(TruncatedFormatCode)50	} else {51		Ok(("", str))52	}53}5455#[cfg(test)]56pub mod tests_key {57	use super::*;5859	#[test]60	fn parse_key() {61		assert_eq!(62			try_parse_mapping_key("(hello ) world").unwrap(),63			("hello ", " world")64		);65		assert_eq!(try_parse_mapping_key("() world").unwrap(), ("", " world"));66		assert_eq!(try_parse_mapping_key(" world").unwrap(), ("", " world"));67		assert_eq!(68			try_parse_mapping_key(" () world").unwrap(),69			("", " () world")70		);71	}7273	#[test]74	#[should_panic]75	fn parse_key_missing_start() {76		try_parse_mapping_key("").unwrap();77	}7879	#[test]80	#[should_panic]81	fn parse_key_missing_end() {82		try_parse_mapping_key("(   ").unwrap();83	}84}8586#[derive(Default, Debug)]87pub struct CFlags {88	pub alt: bool,89	pub zero: bool,90	pub left: bool,91	pub blank: bool,92	pub sign: bool,93}9495pub fn try_parse_cflags(str: &str) -> ParseResult<CFlags> {96	if str.is_empty() {97		return Err(TruncatedFormatCode);98	}99	let bytes = str.as_bytes();100	let mut i = 0;101	let mut out = CFlags::default();102	loop {103		if bytes.len() == i {104			return Err(TruncatedFormatCode);105		}106		match bytes[i] {107			b'#' => out.alt = true,108			b'0' => out.zero = true,109			b'-' => out.left = true,110			b' ' => out.blank = true,111			b'+' => out.sign = true,112			_ => break,113		}114		i += 1;115	}116	Ok((out, &str[i..]))117}118119#[derive(Debug, PartialEq)]120pub enum Width {121	Star,122	Fixed(usize),123}124pub fn try_parse_field_width(str: &str) -> ParseResult<Width> {125	if str.is_empty() {126		return Err(TruncatedFormatCode);127	}128	let bytes = str.as_bytes();129	if bytes[0] == b'*' {130		return Ok((Width::Star, &str[1..]));131	}132	let mut out: usize = 0;133	let mut digits = 0;134	while let Some(digit) = (bytes[digits] as char).to_digit(10) {135		out *= 10;136		out += digit as usize;137		digits += 1;138		if digits == bytes.len() {139			return Err(TruncatedFormatCode);140		}141	}142	Ok((Width::Fixed(out), &str[digits..]))143}144145pub fn try_parse_precision(str: &str) -> ParseResult<Option<Width>> {146	if str.is_empty() {147		return Err(TruncatedFormatCode);148	}149	let bytes = str.as_bytes();150	if bytes[0] == b'.' {151		try_parse_field_width(&str[1..]).map(|(r, s)| (Some(r), s))152	} else {153		Ok((None, str))154	}155}156157// Only skips158pub fn try_parse_length_modifier(str: &str) -> ParseResult<()> {159	if str.is_empty() {160		return Err(TruncatedFormatCode);161	}162	let bytes = str.as_bytes();163	let mut idx = 0;164	while bytes[idx] == b'h' || bytes[idx] == b'l' || bytes[idx] == b'L' {165		idx += 1;166		if bytes.len() == idx {167			return Err(TruncatedFormatCode);168		}169	}170	Ok(((), &str[idx..]))171}172173#[derive(Debug, PartialEq)]174pub enum ConvTypeV {175	Decimal,176	Octal,177	Hexadecimal,178	Scientific,179	Float,180	Shorter,181	Char,182	String,183	Percent,184}185pub struct ConvType {186	v: ConvTypeV,187	caps: bool,188}189190pub fn parse_conversion_type(str: &str) -> ParseResult<ConvType> {191	if str.is_empty() {192		return Err(TruncatedFormatCode);193	}194195	let code = str.as_bytes()[0];196	let v: (ConvTypeV, bool) = match code {197		b'd' | b'i' | b'u' => (ConvTypeV::Decimal, false),198		b'o' => (ConvTypeV::Octal, false),199		b'x' => (ConvTypeV::Hexadecimal, false),200		b'X' => (ConvTypeV::Hexadecimal, true),201		b'e' => (ConvTypeV::Scientific, false),202		b'E' => (ConvTypeV::Scientific, true),203		b'f' => (ConvTypeV::Float, false),204		b'F' => (ConvTypeV::Float, true),205		b'g' => (ConvTypeV::Shorter, false),206		b'G' => (ConvTypeV::Shorter, true),207		b'c' => (ConvTypeV::Char, false),208		b's' => (ConvTypeV::String, false),209		b'%' => (ConvTypeV::Percent, false),210		c => return Err(UnrecognizedConversionType(c as char)),211	};212213	Ok((ConvType { v: v.0, caps: v.1 }, &str[1..]))214}215216#[derive(Debug)]217pub struct Code<'s> {218	mkey: &'s str,219	cflags: CFlags,220	width: Width,221	precision: Option<Width>,222	convtype: ConvTypeV,223	caps: bool,224}225pub fn parse_code(str: &str) -> ParseResult<Code> {226	if str.is_empty() {227		return Err(TruncatedFormatCode);228	}229	let (mkey, str) = try_parse_mapping_key(str)?;230	let (cflags, str) = try_parse_cflags(str)?;231	let (width, str) = try_parse_field_width(str)?;232	let (precision, str) = try_parse_precision(str)?;233	let (_, str) = try_parse_length_modifier(str)?;234	let (convtype, str) = parse_conversion_type(str)?;235236	Ok((237		Code {238			mkey,239			cflags,240			width,241			precision,242			convtype: convtype.v,243			caps: convtype.caps,244		},245		str,246	))247}248249#[derive(Debug)]250pub enum Element<'s> {251	String(&'s str),252	Code(Code<'s>),253}254pub fn parse_codes(mut str: &str) -> Result<Vec<Element>> {255	let mut bytes = str.as_bytes();256	let mut out = vec![];257	let mut offset = 0;258259	loop {260		while offset != bytes.len() && bytes[offset] != b'%' {261			offset += 1;262		}263		if offset != 0 {264			out.push(Element::String(&str[0..offset]));265		}266		if offset == bytes.len() {267			return Ok(out);268		}269		str = &str[offset + 1..];270		let (code, nstr) = parse_code(str)?;271		str = nstr;272		bytes = str.as_bytes();273		offset = 0;274275		out.push(Element::Code(code))276	}277}278279const NUMBERS: &[u8] = b"0123456789abcdefghijklmnopqrstuvwxyz";280281#[inline]282pub fn render_integer(283	out: &mut String,284	iv: i64,285	padding: usize,286	precision: usize,287	blank: bool,288	sign: bool,289	radix: i64,290	prefix: &str,291	caps: bool,292) {293	// Digit char indexes in reverse order, i.e294	// for radix = 16 and n = 12f: [15, 2, 1]295	let digits = if iv == 0 {296		vec![0u8]297	} else {298		let mut v = iv.abs();299		let mut nums = Vec::with_capacity(1);300		while v > 0 {301			nums.push((v % radix) as u8);302			v /= radix;303		}304		nums305	};306	let neg = iv < 0;307	let zp = padding.saturating_sub(if neg || blank || sign { 1 } else { 0 });308	let zp2 = zp309		.max(precision)310		.saturating_sub(prefix.len() + digits.len());311312	if neg {313		out.push('-')314	} else if sign {315		out.push('+');316	} else if blank {317		out.push(' ');318	}319320	out.reserve(zp2);321	for _ in 0..zp2 {322		out.push('0');323	}324	out.push_str(prefix);325326	for digit in digits.into_iter().rev() {327		let ch = NUMBERS[digit as usize] as char;328		out.push(if caps { ch.to_ascii_uppercase() } else { ch });329	}330}331332pub fn render_decimal(333	out: &mut String,334	iv: i64,335	padding: usize,336	precision: usize,337	blank: bool,338	sign: bool,339) {340	render_integer(out, iv, padding, precision, blank, sign, 10, "", false)341}342pub fn render_octal(343	out: &mut String,344	iv: i64,345	padding: usize,346	precision: usize,347	alt: bool,348	blank: bool,349	sign: bool,350) {351	render_integer(352		out,353		iv,354		padding,355		precision,356		blank,357		sign,358		8,359		if alt && iv != 0 { "0" } else { "" },360		false,361	)362}363pub fn render_hexadecimal(364	out: &mut String,365	iv: i64,366	padding: usize,367	precision: usize,368	alt: bool,369	blank: bool,370	sign: bool,371	caps: bool,372) {373	render_integer(374		out,375		iv,376		padding,377		precision,378		blank,379		sign,380		16,381		match (alt, caps) {382			(true, true) => "0X",383			(true, false) => "0x",384			(false, _) => "",385		},386		caps,387	)388}389390pub fn render_float(391	out: &mut String,392	n: f64,393	mut padding: usize,394	precision: usize,395	blank: bool,396	sign: bool,397	ensure_pt: bool,398	trailing: bool,399) {400	let dot_size = if precision == 0 && !ensure_pt { 0 } else { 1 };401	padding = padding.saturating_sub(dot_size + precision);402	render_decimal(out, n.floor() as i64, padding, 0, blank, sign);403	if precision == 0 {404		if ensure_pt {405			out.push('.');406		}407		return;408	}409	let frac = n410		.fract()411		.mul_add(10.0_f64.powf(precision as f64), 0.5)412		.floor();413	if trailing || frac > 0.0 {414		out.push('.');415		let mut frac_str = String::new();416		render_decimal(&mut frac_str, frac as i64, precision, 0, false, false);417		let mut trim = frac_str.len();418		if !trailing {419			for b in frac_str.as_bytes().iter().rev() {420				if *b == b'0' {421					trim -= 1;422				}423			}424		}425		out.push_str(&frac_str[..trim]);426	} else if ensure_pt {427		out.push('.');428	}429}430431pub fn render_float_sci(432	out: &mut String,433	n: f64,434	mut padding: usize,435	precision: usize,436	blank: bool,437	sign: bool,438	ensure_pt: bool,439	trailing: bool,440	caps: bool,441) {442	let exponent = n.log10().floor();443	let mantissa = if exponent as i16 == -324 {444		n * 10.0 / 10.0_f64.powf(exponent + 1.0)445	} else {446		n / 10.0_f64.powf(exponent)447	};448	let mut exponent_str = String::new();449	render_decimal(&mut exponent_str, exponent as i64, 3, 0, false, true);450451	// +1 for e452	padding = padding.saturating_sub(exponent_str.len() + 1);453454	render_float(455		out, mantissa, padding, precision, blank, sign, ensure_pt, trailing,456	);457	out.push(if caps { 'E' } else { 'e' });458	out.push_str(&exponent_str);459}460461pub fn format_code(462	out: &mut String,463	value: &Val,464	code: &Code,465	width: usize,466	precision: Option<usize>,467) -> Result<()> {468	let clfags = &code.cflags;469	let (fpprec, iprec) = match precision {470		Some(v) => (v, v),471		None => (6, 0),472	};473	let padding = if clfags.zero && !clfags.left {474		width475	} else {476		0477	};478479	// TODO: If left padded, can optimize by writing directly to out480	let mut tmp_out = String::new();481482	match code.convtype {483		ConvTypeV::String => tmp_out.push_str(&value.clone().to_string()?),484		ConvTypeV::Decimal => {485			let value = value.clone().try_cast_num("%d/%u/%i requires number")?;486			render_decimal(487				&mut tmp_out,488				value as i64,489				padding,490				iprec,491				clfags.blank,492				clfags.sign,493			);494		}495		ConvTypeV::Octal => {496			let value = value.clone().try_cast_num("%o requires number")?;497			render_octal(498				&mut tmp_out,499				value as i64,500				padding,501				iprec,502				clfags.alt,503				clfags.blank,504				clfags.sign,505			);506		}507		ConvTypeV::Hexadecimal => {508			let value = value.clone().try_cast_num("%x/%X requires number")?;509			render_hexadecimal(510				&mut tmp_out,511				value as i64,512				padding,513				iprec,514				clfags.alt,515				clfags.blank,516				clfags.sign,517				code.caps,518			);519		}520		ConvTypeV::Scientific => {521			let value = value.clone().try_cast_num("%e/%E requires number")?;522			render_float_sci(523				&mut tmp_out,524				value,525				padding,526				fpprec,527				clfags.blank,528				clfags.sign,529				clfags.alt,530				true,531				code.caps,532			);533		}534		ConvTypeV::Float => {535			let value = value.clone().try_cast_num("%e/%E requires number")?;536			render_float(537				&mut tmp_out,538				value,539				padding,540				fpprec,541				clfags.blank,542				clfags.sign,543				clfags.alt,544				true,545			);546		}547		ConvTypeV::Shorter => {548			let value = value.clone().try_cast_num("%g/%G requires number")?;549			let exponent = value.log10().floor();550			if exponent < -4.0 || exponent >= fpprec as f64 {551				render_float_sci(552					&mut tmp_out,553					value,554					padding,555					fpprec - 1,556					clfags.blank,557					clfags.sign,558					clfags.alt,559					clfags.alt,560					code.caps,561				);562			} else {563				let digits_before_pt = 1.max(exponent as usize + 1);564				render_float(565					&mut tmp_out,566					value,567					padding,568					fpprec - digits_before_pt,569					clfags.blank,570					clfags.sign,571					clfags.alt,572					clfags.alt,573				);574			}575		}576		ConvTypeV::Char => match value.clone().unwrap_if_lazy()? {577			Val::Num(n) => tmp_out.push(578				std::char::from_u32(n as u32)579					.ok_or_else(|| InvalidUnicodeCodepointGot(n as u32))?,580			),581			Val::Str(s) => {582				if s.chars().count() != 1 {583					throw!(RuntimeError(584						format!("%c expected 1 char string, got {}", s.chars().count()).into(),585					));586				}587				tmp_out.push_str(&s);588			}589			_ => {590				throw!(TypeMismatch(591					"%c requires number/string",592					vec![ValType::Num, ValType::Str],593					value.value_type()?,594				));595			}596		},597		ConvTypeV::Percent => tmp_out.push('%'),598	};599600	let padding = width.saturating_sub(tmp_out.len());601602	if !clfags.left {603		for _ in 0..padding {604			out.push(' ');605		}606	}607	out.push_str(&tmp_out);608	if clfags.left {609		for _ in 0..padding {610			out.push(' ');611		}612	}613614	Ok(())615}616617pub fn format_arr(str: &str, mut values: &[Val]) -> Result<String> {618	let codes = parse_codes(str)?;619	let mut out = String::new();620621	for code in codes {622		match code {623			Element::String(s) => {624				out.push_str(s);625			}626			Element::Code(c) => {627				let width = match c.width {628					Width::Star => {629						if values.is_empty() {630							throw!(NotEnoughValues);631						}632						let value = &values[0];633						values = &values[1..];634						value.clone().try_cast_num("field width")? as usize635					}636					Width::Fixed(n) => n,637				};638				let precision = match c.precision {639					Some(Width::Star) => {640						if values.is_empty() {641							throw!(NotEnoughValues);642						}643						let value = &values[0];644						values = &values[1..];645						Some(value.clone().try_cast_num("field precision")? as usize)646					}647					Some(Width::Fixed(n)) => Some(n),648					None => None,649				};650651				// %% should not consume a value652				let value = if c.convtype == ConvTypeV::Percent {653					&Val::Null654				} else {655					if values.is_empty() {656						throw!(NotEnoughValues);657					}658					let value = &values[0];659					values = &values[1..];660					value661				};662663				format_code(&mut out, value, &c, width, precision)?;664			}665		}666	}667668	Ok(out)669}670671pub fn format_obj(str: &str, values: &ObjValue) -> Result<String> {672	let codes = parse_codes(str)?;673	let mut out = String::new();674675	for code in codes {676		match code {677			Element::String(s) => {678				out.push_str(s);679			}680			Element::Code(c) => {681				// TODO: Operate on ref682				let f: Rc<str> = c.mkey.into();683				let width = match c.width {684					Width::Star => {685						throw!(CannotUseStarWidthWithObject);686					}687					Width::Fixed(n) => n,688				};689				let precision = match c.precision {690					Some(Width::Star) => {691						throw!(CannotUseStarWidthWithObject);692					}693					Some(Width::Fixed(n)) => Some(n),694					None => None,695				};696697				let value = if c.convtype == ConvTypeV::Percent {698					Val::Null699				} else {700					if f.is_empty() {701						throw!(MappingKeysRequired);702					}703					if let Some(v) = values.get(f.clone())? {704						v705					} else {706						throw!(NoSuchFormatField(f));707					}708				};709710				format_code(&mut out, &value, &c, width, precision)?;711			}712		}713	}714715	Ok(out)716}717718#[cfg(test)]719pub mod test_format {720	use super::*;721722	#[test]723	fn parse() {724		assert_eq!(725			parse_codes(726				"How much error budget is left looking at our %.3f%% availability gurantees?"727			)728			.unwrap()729			.len(),730			4731		);732	}733734	#[test]735	fn octals() {736		assert_eq!(format_arr("%#o", &[Val::Num(8.0)]).unwrap(), "010");737		assert_eq!(format_arr("%#4o", &[Val::Num(8.0)]).unwrap(), " 010");738		assert_eq!(format_arr("%4o", &[Val::Num(8.0)]).unwrap(), "  10");739		assert_eq!(format_arr("%04o", &[Val::Num(8.0)]).unwrap(), "0010");740		assert_eq!(format_arr("%+4o", &[Val::Num(8.0)]).unwrap(), " +10");741		assert_eq!(format_arr("%+04o", &[Val::Num(8.0)]).unwrap(), "+010");742		assert_eq!(format_arr("%-4o", &[Val::Num(8.0)]).unwrap(), "10  ");743		assert_eq!(format_arr("%+-4o", &[Val::Num(8.0)]).unwrap(), "+10 ");744		assert_eq!(format_arr("%+-04o", &[Val::Num(8.0)]).unwrap(), "+10 ");745	}746747	#[test]748	fn percent_doesnt_consumes_values() {749		assert_eq!(750			format_arr(751				"How much error budget is left looking at our %.3f%% availability gurantees?",752				&[Val::Num(4.0)]753			)754			.unwrap(),755			"How much error budget is left looking at our 4.000% availability gurantees?"756		);757	}758}
modifiedcrates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/manifest.rs
@@ -33,9 +33,9 @@
 ) -> Result<()> {
 	use std::fmt::Write;
 	let mtype = options.mtype;
-	match val.unwrap_if_lazy()? {
+	match val {
 		Val::Bool(v) => {
-			if v {
+			if *v {
 				buf.push_str("true");
 			} else {
 				buf.push_str("false");
@@ -63,7 +63,7 @@
 						}
 					}
 					buf.push_str(cur_padding);
-					manifest_json_ex_buf(item, buf, cur_padding, options)?;
+					manifest_json_ex_buf(&item?, buf, cur_padding, options)?;
 				}
 				cur_padding.truncate(old_len);
 
@@ -118,7 +118,6 @@
 			buf.push('}');
 		}
 		Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),
-		Val::Lazy(_) => unreachable!(),
 	};
 	Ok(())
 }
modifiedcrates/jrsonnet-evaluator/src/builtin/sort.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/sort.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/sort.rs
@@ -46,7 +46,6 @@
 	let mut sort_type = SortKeyType::Unknown;
 	for i in values.iter_mut() {
 		let i = key_getter(i);
-		i.inplace_unwrap()?;
 		match (i, sort_type) {
 			(Val::Str(_), SortKeyType::Unknown) => sort_type = SortKeyType::String,
 			(Val::Num(_), SortKeyType::Unknown) => sort_type = SortKeyType::Number,
modifiedcrates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate.rs
@@ -1,7 +1,6 @@
 use crate::{
 	context_creator, error::Error::*, future_wrapper, lazy_val, push, throw, with_state, Context,
 	ContextCreator, FuncDesc, FuncVal, LazyBinding, LazyVal, ObjMember, ObjValue, Result, Val,
-	ValType,
 };
 use closure::closure;
 use jrsonnet_parser::{
@@ -9,6 +8,7 @@
 	ForSpecData, IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc, UnaryOpType,
 	Visibility,
 };
+use jrsonnet_types::ValType;
 use rustc_hash::FxHashMap;
 use std::{collections::HashMap, rc::Rc};
 
@@ -61,8 +61,7 @@
 	Ok(match field_name {
 		jrsonnet_parser::FieldName::Fixed(n) => Some(n.clone()),
 		jrsonnet_parser::FieldName::Dyn(expr) => {
-			let lazy = evaluate(context, expr)?;
-			let value = lazy.unwrap_if_lazy()?;
+			let value = evaluate(context, expr)?;
 			if matches!(value, Val::Null) {
 				None
 			} else {
@@ -74,11 +73,10 @@
 
 pub fn evaluate_unary_op(op: UnaryOpType, b: &Val) -> Result<Val> {
 	Ok(match (op, b) {
-		(o, Val::Lazy(l)) => evaluate_unary_op(o, &l.evaluate()?)?,
 		(UnaryOpType::Not, Val::Bool(v)) => Val::Bool(!v),
 		(UnaryOpType::Minus, Val::Num(n)) => Val::Num(-*n),
 		(UnaryOpType::BitNot, Val::Num(n)) => Val::Num(!(*n as i32) as f64),
-		(op, o) => throw!(UnaryOperatorDoesNotOperateOnType(op, o.value_type()?)),
+		(op, o) => throw!(UnaryOperatorDoesNotOperateOnType(op, o.value_type())),
 	})
 }
 
@@ -94,12 +92,17 @@
 		(o, Val::Str(s)) => Val::Str(format!("{}{}", o.clone().to_string()?, s).into()),
 
 		(Val::Obj(v1), Val::Obj(v2)) => Val::Obj(v2.with_super(v1.clone())),
-		(Val::Arr(a), Val::Arr(b)) => Val::Arr(Rc::new([&a[..], &b[..]].concat())),
+		(Val::Arr(a), Val::Arr(b)) => {
+			let mut out = Vec::with_capacity(a.len() + b.len());
+			out.extend(a.iter_lazy());
+			out.extend(b.iter_lazy());
+			Val::Arr(out.into())
+		}
 		(Val::Num(v1), Val::Num(v2)) => Val::new_checked_num(v1 + v2)?,
 		_ => throw!(BinaryOperatorDoesNotOperateOnValues(
 			BinaryOpType::Add,
-			a.value_type()?,
-			b.value_type()?,
+			a.value_type(),
+			b.value_type(),
 		)),
 	})
 }
@@ -110,15 +113,11 @@
 	op: BinaryOpType,
 	b: &LocExpr,
 ) -> Result<Val> {
-	Ok(
-		match (evaluate(context.clone(), a)?.unwrap_if_lazy()?, op, b) {
-			(Val::Bool(true), BinaryOpType::Or, _o) => Val::Bool(true),
-			(Val::Bool(false), BinaryOpType::And, _o) => Val::Bool(false),
-			(a, op, eb) => {
-				evaluate_binary_op_normal(&a, op, &evaluate(context, eb)?.unwrap_if_lazy()?)?
-			}
-		},
-	)
+	Ok(match (evaluate(context.clone(), a)?, op, b) {
+		(Val::Bool(true), BinaryOpType::Or, _o) => Val::Bool(true),
+		(Val::Bool(false), BinaryOpType::And, _o) => Val::Bool(false),
+		(a, op, eb) => evaluate_binary_op_normal(&a, op, &evaluate(context, eb)?)?,
+	})
 }
 
 pub fn evaluate_binary_op_normal(a: &Val, op: BinaryOpType, b: &Val) -> Result<Val> {
@@ -177,8 +176,8 @@
 
 		_ => throw!(BinaryOperatorDoesNotOperateOnValues(
 			op,
-			a.value_type()?,
-			b.value_type()?,
+			a.value_type(),
+			b.value_type(),
 		)),
 	})
 }
@@ -200,23 +199,20 @@
 				None
 			}
 		}
-		Some(CompSpec::ForSpec(ForSpecData(var, expr))) => {
-			match evaluate(context.clone(), expr)?.unwrap_if_lazy()? {
-				Val::Arr(list) => {
-					let mut out = Vec::new();
-					for item in list.iter() {
-						let item = item.unwrap_if_lazy()?;
-						out.push(evaluate_comp(
-							context.clone().with_var(var.clone(), item.clone()),
-							value,
-							&specs[1..],
-						)?);
-					}
-					Some(out.into_iter().flatten().flatten().collect())
+		Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(context.clone(), expr)? {
+			Val::Arr(list) => {
+				let mut out = Vec::new();
+				for item in list.iter() {
+					out.push(evaluate_comp(
+						context.clone().with_var(var.clone(), item?.clone()),
+						value,
+						&specs[1..],
+					)?);
 				}
-				_ => throw!(InComprehensionCanOnlyIterateOverArray),
+				Some(out.into_iter().flatten().flatten().collect())
 			}
-		}
+			_ => throw!(InComprehensionCanOnlyIterateOverArray),
+		},
 	})
 }
 
@@ -375,7 +371,7 @@
 							},
 						);
 					}
-					v => throw!(FieldMustBeStringGot(v.value_type()?)),
+					v => throw!(FieldMustBeStringGot(v.value_type())),
 				}
 			}
 
@@ -391,8 +387,7 @@
 	loc: &Option<ExprLocation>,
 	tailstrict: bool,
 ) -> Result<Val> {
-	let lazy = evaluate(context.clone(), value)?;
-	let value = lazy.unwrap_if_lazy()?;
+	let value = evaluate(context.clone(), value)?;
 	Ok(match value {
 		Val::Func(f) => {
 			let body = || f.evaluate(context, loc, args, tailstrict);
@@ -402,7 +397,7 @@
 				push(loc, || format!("function <{}> call", f.name()), body)?
 			}
 		}
-		v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type()?)),
+		v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),
 	})
 }
 
@@ -436,7 +431,7 @@
 		Var(name) => push(
 			loc,
 			|| format!("variable <{}>", name),
-			|| Ok(Val::Lazy(context.binding(name.clone())?).unwrap_if_lazy()?),
+			|| Ok(context.binding(name.clone())?.evaluate()?),
 		)?,
 		Index(LocExpr(v, _), index) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {
 			let name = evaluate(context.clone(), index)?.try_cast_str("object index")?;
@@ -444,14 +439,11 @@
 				.super_obj()
 				.clone()
 				.expect("no super found")
-				.get_raw(name, &context.this().clone().expect("no this found"))?
+				.get_raw(name, Some(&context.this().clone().expect("no this found")))?
 				.expect("value not found")
 		}
 		Index(value, index) => {
-			match (
-				evaluate(context.clone(), value)?.unwrap_if_lazy()?,
-				evaluate(context, index)?,
-			) {
+			match (evaluate(context.clone(), value)?, evaluate(context, index)?) {
 				(Val::Obj(v), Val::Str(s)) => {
 					let sn = s.clone();
 					push(
@@ -459,7 +451,7 @@
 						|| format!("field <{}> access", sn),
 						|| {
 							if let Some(v) = v.get(s.clone())? {
-								Ok(v.unwrap_if_lazy()?)
+								Ok(v)
 							} else if v.get("__intrinsic_namespace__".into())?.is_some() {
 								Ok(Val::Func(Rc::new(FuncVal::Intrinsic(s))))
 							} else {
@@ -471,23 +463,22 @@
 				(Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(
 					ValType::Obj,
 					ValType::Str,
-					n.value_type()?,
+					n.value_type(),
 				)),
 
 				(Val::Arr(v), Val::Num(n)) => {
 					if n.fract() > f64::EPSILON {
 						throw!(FractionalIndex)
 					}
-					v.get(n as usize)
+					v.get(n as usize)?
 						.ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?
 						.clone()
-						.unwrap_if_lazy()?
 				}
 				(Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),
 				(Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(
 					ValType::Arr,
 					ValType::Num,
-					n.value_type()?,
+					n.value_type(),
 				)),
 
 				(Val::Str(s), Val::Num(n)) => Val::Str(
@@ -500,10 +491,10 @@
 				(Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(
 					ValType::Str,
 					ValType::Num,
-					n.value_type()?,
+					n.value_type(),
 				)),
 
-				(v, _) => throw!(CantIndexInto(v.value_type()?)),
+				(v, _) => throw!(CantIndexInto(v.value_type())),
 			}
 		}
 		LocalExpr(bindings, returned) => {
@@ -529,17 +520,19 @@
 		Arr(items) => {
 			let mut out = Vec::with_capacity(items.len());
 			for item in items {
-				out.push(Val::Lazy(lazy_val!(
+				out.push(LazyVal::new(Box::new(
 					closure!(clone context, clone item, || {
 						evaluate(context.clone(), &item)
-					})
+					}),
 				)));
 			}
-			Val::Arr(Rc::new(out))
+			Val::Arr(out.into())
 		}
 		ArrComp(expr, comp_specs) => Val::Arr(
 			// First comp_spec should be for_spec, so no "None" possible here
-			Rc::new(evaluate_comp(context, &|ctx| evaluate(ctx, expr), comp_specs)?.unwrap()),
+			evaluate_comp(context, &|ctx| evaluate(ctx, expr), comp_specs)?
+				.unwrap()
+				.into(),
 		),
 		Obj(body) => Val::Obj(evaluate_object(context, body)?),
 		ObjExtend(s, t) => evaluate_add_op(
modifiedcrates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -22,11 +22,10 @@
 			} else {
 				Number::from_f64(*n).expect("to json number")
 			}),
-			Val::Lazy(v) => (&v.evaluate()?).try_into()?,
 			Val::Arr(a) => {
 				let mut out = Vec::with_capacity(a.len());
 				for item in a.iter() {
-					out.push(item.try_into()?);
+					out.push((&item?).try_into()?);
 				}
 				Self::Array(out)
 			}
@@ -55,9 +54,9 @@
 			Value::Array(a) => {
 				let mut out = Vec::with_capacity(a.len());
 				for v in a {
-					out.push(v.into());
+					out.push(LazyVal::new_resolved(v.into()));
 				}
-				Self::Arr(Rc::new(out))
+				Self::Arr(out.into())
 			}
 			Value::Object(o) => {
 				let mut entries = HashMap::with_capacity(o.len());
modifiedcrates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -102,9 +102,10 @@
 		visible_fields
 	}
 	pub fn get(&self, key: Rc<str>) -> Result<Option<Val>> {
-		Ok(self.get_raw(key, self)?)
+		Ok(self.get_raw(key, None)?)
 	}
-	pub(crate) fn get_raw(&self, key: Rc<str>, real_this: &Self) -> Result<Option<Val>> {
+	pub(crate) fn get_raw(&self, key: Rc<str>, real_this: Option<&Self>) -> Result<Option<Val>> {
+		let real_this = real_this.unwrap_or(self);
 		let cache_key = (key.clone(), Rc::as_ptr(&real_this.0) as usize);
 
 		if let Some(v) = self.0.value_cache.borrow().get(&cache_key) {
@@ -115,7 +116,7 @@
 			(Some(k), Some(s)) => {
 				let our = self.evaluate_this(k, real_this)?;
 				if k.add {
-					s.get_raw(key, real_this)?
+					s.get_raw(key, Some(real_this))?
 						.map_or(Ok(Some(our.clone())), |v| {
 							Ok(Some(evaluate_add_op(&v, &our)?))
 						})
@@ -123,7 +124,7 @@
 					Ok(Some(our))
 				}
 			}
-			(None, Some(s)) => s.get_raw(key, real_this),
+			(None, Some(s)) => s.get_raw(key, Some(real_this)),
 			(None, None) => Ok(None),
 		}?;
 		self.0
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -10,12 +10,8 @@
 	throw, with_state, Context, ObjValue, Result,
 };
 use jrsonnet_parser::{el, Arg, ArgsDesc, Expr, ExprLocation, LiteralType, LocExpr, ParamsDesc};
-use std::{
-	cell::RefCell,
-	collections::HashMap,
-	fmt::{Debug, Display},
-	rc::Rc,
-};
+use jrsonnet_types::ValType;
+use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};
 
 enum LazyValInternals {
 	Computed(Val),
@@ -166,43 +162,108 @@
 	}
 }
 
-#[derive(Debug, Clone, Copy, PartialEq)]
-pub enum ValType {
-	Bool,
-	Null,
-	Str,
-	Num,
-	Arr,
-	Obj,
-	Func,
+#[derive(Clone)]
+pub enum ManifestFormat {
+	YamlStream(Box<ManifestFormat>),
+	Yaml(usize),
+	Json(usize),
+	ToString,
+	String,
+}
+
+#[derive(Debug, Clone)]
+pub enum ArrValue {
+	Lazy(Rc<Vec<LazyVal>>),
+	Eager(Rc<Vec<Val>>),
 }
-impl ValType {
-	pub const fn name(&self) -> &'static str {
-		use ValType::*;
+impl ArrValue {
+	pub fn len(&self) -> usize {
 		match self {
-			Bool => "boolean",
-			Null => "null",
-			Str => "string",
-			Num => "number",
-			Arr => "array",
-			Obj => "object",
-			Func => "function",
+			ArrValue::Lazy(l) => l.len(),
+			ArrValue::Eager(e) => e.len(),
 		}
 	}
+
+	pub fn is_empty(&self) -> bool {
+		self.len() == 0
+	}
+
+	pub fn get(&self, index: usize) -> Result<Option<Val>> {
+		match self {
+			ArrValue::Lazy(vec) => {
+				if let Some(v) = vec.get(index) {
+					Ok(Some(v.evaluate()?))
+				} else {
+					Ok(None)
+				}
+			}
+			ArrValue::Eager(vec) => Ok(vec.get(index).cloned()),
+		}
+	}
+
+	pub fn get_lazy(&self, index: usize) -> Option<LazyVal> {
+		match self {
+			ArrValue::Lazy(vec) => vec.get(index).cloned(),
+			ArrValue::Eager(vec) => vec
+				.get(index)
+				.cloned()
+				.map(|val| LazyVal::new_resolved(val)),
+		}
+	}
+
+	pub fn evaluated(&self) -> Result<Rc<Vec<Val>>> {
+		Ok(match self {
+			ArrValue::Lazy(vec) => {
+				let mut out = Vec::with_capacity(vec.len());
+				for item in vec.iter() {
+					out.push(item.evaluate()?);
+				}
+				Rc::new(out)
+			}
+			ArrValue::Eager(vec) => vec.clone(),
+		})
+	}
+
+	pub fn iter(&self) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {
+		(0..self.len()).map(move |idx| match self {
+			ArrValue::Lazy(l) => l[idx].evaluate(),
+			ArrValue::Eager(e) => Ok(e[idx].clone()),
+		})
+	}
+
+	pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = LazyVal> + '_ {
+		(0..self.len()).map(move |idx| match self {
+			ArrValue::Lazy(l) => l[idx].clone(),
+			ArrValue::Eager(e) => LazyVal::new_resolved(e[idx].clone()),
+		})
+	}
+
+	pub fn reversed(self) -> Self {
+		match self {
+			ArrValue::Lazy(vec) => {
+				let mut out = (&vec as &Vec<_>).clone();
+				out.reverse();
+				Self::Lazy(Rc::new(out))
+			}
+			ArrValue::Eager(vec) => {
+				let mut out = (&vec as &Vec<_>).clone();
+				out.reverse();
+				Self::Eager(Rc::new(out))
+			}
+		}
+	}
 }
-impl Display for ValType {
-	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-		write!(f, "{}", self.name())
+
+impl From<Vec<LazyVal>> for ArrValue {
+	fn from(v: Vec<LazyVal>) -> Self {
+		Self::Lazy(Rc::new(v))
 	}
 }
 
-#[derive(Clone)]
-pub enum ManifestFormat {
-	YamlStream(Box<ManifestFormat>),
-	Yaml(usize),
-	Json(usize),
-	ToString,
-	String,
+impl From<Vec<Val>> for ArrValue {
+	fn from(v: Vec<Val>) -> Self {
+		Self::Eager(Rc::new(v))
+	}
 }
 
 #[derive(Debug, Clone)]
@@ -211,8 +272,7 @@
 	Null,
 	Str(Rc<str>),
 	Num(f64),
-	Lazy(LazyVal),
-	Arr(Rc<Vec<Val>>),
+	Arr(ArrValue),
 	Obj(ObjValue),
 	Func(Rc<FuncVal>),
 }
@@ -237,40 +297,33 @@
 	}
 
 	pub fn assert_type(&self, context: &'static str, val_type: ValType) -> Result<()> {
-		let this_type = self.value_type()?;
+		let this_type = self.value_type();
 		if this_type != val_type {
 			throw!(TypeMismatch(context, vec![val_type], this_type))
 		} else {
 			Ok(())
 		}
 	}
+	pub fn unwrap_num(self) -> Result<f64> {
+		Ok(matches_unwrap!(self, Self::Num(v), v))
+	}
+	pub fn unwrap_func(self) -> Result<Rc<FuncVal>> {
+		Ok(matches_unwrap!(self, Self::Func(v), v))
+	}
 	pub fn try_cast_bool(self, context: &'static str) -> Result<bool> {
 		self.assert_type(context, ValType::Bool)?;
-		Ok(matches_unwrap!(self.unwrap_if_lazy()?, Self::Bool(v), v))
+		Ok(matches_unwrap!(self, Self::Bool(v), v))
 	}
 	pub fn try_cast_str(self, context: &'static str) -> Result<Rc<str>> {
 		self.assert_type(context, ValType::Str)?;
-		Ok(matches_unwrap!(self.unwrap_if_lazy()?, Self::Str(v), v))
+		Ok(matches_unwrap!(self, Self::Str(v), v))
 	}
 	pub fn try_cast_num(self, context: &'static str) -> Result<f64> {
 		self.assert_type(context, ValType::Num)?;
-		Ok(matches_unwrap!(self.unwrap_if_lazy()?, Self::Num(v), v))
-	}
-	pub fn inplace_unwrap(&mut self) -> Result<()> {
-		while let Self::Lazy(lazy) = self {
-			*self = lazy.evaluate()?;
-		}
-		Ok(())
+		self.unwrap_num()
 	}
-	pub fn unwrap_if_lazy(&self) -> Result<Self> {
-		Ok(if let Self::Lazy(v) = self {
-			v.evaluate()?.unwrap_if_lazy()?
-		} else {
-			self.clone()
-		})
-	}
-	pub fn value_type(&self) -> Result<ValType> {
-		Ok(match self {
+	pub fn value_type(&self) -> ValType {
+		match self {
 			Self::Str(..) => ValType::Str,
 			Self::Num(..) => ValType::Num,
 			Self::Arr(..) => ValType::Arr,
@@ -278,16 +331,15 @@
 			Self::Bool(_) => ValType::Bool,
 			Self::Null => ValType::Null,
 			Self::Func(..) => ValType::Func,
-			Self::Lazy(_) => self.clone().unwrap_if_lazy()?.value_type()?,
-		})
+		}
 	}
 
 	pub fn to_string(&self) -> Result<Rc<str>> {
-		Ok(match self.unwrap_if_lazy()? {
+		Ok(match self {
 			Self::Bool(true) => "true".into(),
 			Self::Bool(false) => "false".into(),
 			Self::Null => "null".into(),
-			Self::Str(s) => s,
+			Self::Str(s) => s.clone(),
 			v => manifest_json_ex(
 				&v,
 				&ManifestJsonOptions {
@@ -325,7 +377,7 @@
 		};
 		let mut out = Vec::with_capacity(arr.len());
 		for i in arr.iter() {
-			out.push(i.manifest(ty)?);
+			out.push(i?.manifest(ty)?);
 		}
 		Ok(out)
 	}
@@ -348,7 +400,7 @@
 				if !arr.is_empty() {
 					for v in arr.iter() {
 						out.push_str("---\n");
-						out.push_str(&v.manifest(format)?);
+						out.push_str(&v?.manifest(format)?);
 						out.push('\n');
 					}
 					out.push_str("...");
@@ -456,7 +508,7 @@
 
 /// Native implementation of `std.primitiveEquals`
 pub fn primitive_equals(val_a: &Val, val_b: &Val) -> Result<bool> {
-	Ok(match (val_a.unwrap_if_lazy()?, val_b.unwrap_if_lazy()?) {
+	Ok(match (val_a, val_b) {
 		(Val::Bool(a), Val::Bool(b)) => a == b,
 		(Val::Null, Val::Null) => true,
 		(Val::Str(a), Val::Str(b)) => a == b,
@@ -476,10 +528,7 @@
 
 /// Native implementation of `std.equals`
 pub fn equals(val_a: &Val, val_b: &Val) -> Result<bool> {
-	let val_a = val_a.unwrap_if_lazy()?;
-	let val_b = val_b.unwrap_if_lazy()?;
-
-	if val_a.value_type()? != val_b.value_type()? {
+	if val_a.value_type() != val_b.value_type() {
 		return Ok(false);
 	}
 	match (val_a, val_b) {
@@ -489,7 +538,7 @@
 				return Ok(false);
 			}
 			for (a, b) in a.iter().zip(b.iter()) {
-				if !equals(&a.unwrap_if_lazy()?, &b.unwrap_if_lazy()?)? {
+				if !equals(&a?, &b?)? {
 					return Ok(false);
 				}
 			}
modifiedcrates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -97,6 +97,7 @@
 	Mul,
 	Div,
 
+	/// Implemented as intrinsic, put here for completeness
 	Mod,
 
 	Add,