git.delta.rocks / jrsonnet / refs/commits / 8f3ed48f89ef

difftreelog

refactor simplify and unify builtins

Yaroslav Bolyukin2021-12-27parent: #393dcbd.patch.diff
in: master

14 files changed

modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -83,17 +83,16 @@
 		std::process::exit(0);
 	};
 
-	let success;
-	if let Some(size) = opts.debug.os_stack {
-		success = std::thread::Builder::new()
+	let success = if let Some(size) = opts.debug.os_stack {
+		std::thread::Builder::new()
 			.stack_size(size * 1024 * 1024)
 			.spawn(|| main_catch(opts))
 			.expect("new thread spawned")
 			.join()
-			.expect("thread finished successfully");
+			.expect("thread finished successfully")
 	} else {
-		success = main_catch(opts)
-	}
+		main_catch(opts)
+	};
 	if !success {
 		std::process::exit(1);
 	}
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};5use gcmodule::Trace;6use jrsonnet_interner::IStr;7use jrsonnet_types::ValType;8use std::convert::TryFrom;9use thiserror::Error;1011#[derive(Debug, Clone, Error, Trace)]12pub enum FormatError {13	#[error("truncated format code")]14	TruncatedFormatCode,15	#[error("unrecognized conversion type: {0}")]16	UnrecognizedConversionType(char),1718	#[error("not enough values")]19	NotEnoughValues,2021	#[error("cannot use * width with object")]22	CannotUseStarWidthWithObject,23	#[error("mapping keys required")]24	MappingKeysRequired,25	#[error("no such format field: {0}")]26	NoSuchFormatField(IStr),27}2829impl From<FormatError> for LocError {30	fn from(e: FormatError) -> Self {31		Self::new(Format(e))32	}33}3435use FormatError::*;3637type ParseResult<'t, T> = std::result::Result<(T, &'t str), FormatError>;3839pub fn try_parse_mapping_key(str: &str) -> ParseResult<&str> {40	if str.is_empty() {41		return Err(TruncatedFormatCode);42	}43	let bytes = str.as_bytes();44	if bytes[0] == b'(' {45		let mut i = 1;46		while i < bytes.len() {47			if bytes[i] == b')' {48				return Ok((&str[1..i as usize], &str[i as usize + 1..]));49			}50			i += 1;51		}52		Err(TruncatedFormatCode)53	} else {54		Ok(("", str))55	}56}5758#[cfg(test)]59pub mod tests_key {60	use super::*;6162	#[test]63	fn parse_key() {64		assert_eq!(65			try_parse_mapping_key("(hello ) world").unwrap(),66			("hello ", " world")67		);68		assert_eq!(try_parse_mapping_key("() world").unwrap(), ("", " world"));69		assert_eq!(try_parse_mapping_key(" world").unwrap(), ("", " world"));70		assert_eq!(71			try_parse_mapping_key(" () world").unwrap(),72			("", " () world")73		);74	}7576	#[test]77	#[should_panic]78	fn parse_key_missing_start() {79		try_parse_mapping_key("").unwrap();80	}8182	#[test]83	#[should_panic]84	fn parse_key_missing_end() {85		try_parse_mapping_key("(   ").unwrap();86	}87}8889#[derive(Default, Debug)]90pub struct CFlags {91	pub alt: bool,92	pub zero: bool,93	pub left: bool,94	pub blank: bool,95	pub sign: bool,96}9798pub fn try_parse_cflags(str: &str) -> ParseResult<CFlags> {99	if str.is_empty() {100		return Err(TruncatedFormatCode);101	}102	let bytes = str.as_bytes();103	let mut i = 0;104	let mut out = CFlags::default();105	loop {106		if bytes.len() == i {107			return Err(TruncatedFormatCode);108		}109		match bytes[i] {110			b'#' => out.alt = true,111			b'0' => out.zero = true,112			b'-' => out.left = true,113			b' ' => out.blank = true,114			b'+' => out.sign = true,115			_ => break,116		}117		i += 1;118	}119	Ok((out, &str[i..]))120}121122#[derive(Debug, PartialEq)]123pub enum Width {124	Star,125	Fixed(usize),126}127pub fn try_parse_field_width(str: &str) -> ParseResult<Width> {128	if str.is_empty() {129		return Err(TruncatedFormatCode);130	}131	let bytes = str.as_bytes();132	if bytes[0] == b'*' {133		return Ok((Width::Star, &str[1..]));134	}135	let mut out: usize = 0;136	let mut digits = 0;137	while let Some(digit) = (bytes[digits] as char).to_digit(10) {138		out *= 10;139		out += digit as usize;140		digits += 1;141		if digits == bytes.len() {142			return Err(TruncatedFormatCode);143		}144	}145	Ok((Width::Fixed(out), &str[digits..]))146}147148pub fn try_parse_precision(str: &str) -> ParseResult<Option<Width>> {149	if str.is_empty() {150		return Err(TruncatedFormatCode);151	}152	let bytes = str.as_bytes();153	if bytes[0] == b'.' {154		try_parse_field_width(&str[1..]).map(|(r, s)| (Some(r), s))155	} else {156		Ok((None, str))157	}158}159160// Only skips161pub fn try_parse_length_modifier(str: &str) -> ParseResult<()> {162	if str.is_empty() {163		return Err(TruncatedFormatCode);164	}165	let bytes = str.as_bytes();166	let mut idx = 0;167	while bytes[idx] == b'h' || bytes[idx] == b'l' || bytes[idx] == b'L' {168		idx += 1;169		if bytes.len() == idx {170			return Err(TruncatedFormatCode);171		}172	}173	Ok(((), &str[idx..]))174}175176#[derive(Debug, PartialEq)]177pub enum ConvTypeV {178	Decimal,179	Octal,180	Hexadecimal,181	Scientific,182	Float,183	Shorter,184	Char,185	String,186	Percent,187}188pub struct ConvType {189	v: ConvTypeV,190	caps: bool,191}192193pub fn parse_conversion_type(str: &str) -> ParseResult<ConvType> {194	if str.is_empty() {195		return Err(TruncatedFormatCode);196	}197198	let code = str.as_bytes()[0];199	let v: (ConvTypeV, bool) = match code {200		b'd' | b'i' | b'u' => (ConvTypeV::Decimal, false),201		b'o' => (ConvTypeV::Octal, false),202		b'x' => (ConvTypeV::Hexadecimal, false),203		b'X' => (ConvTypeV::Hexadecimal, true),204		b'e' => (ConvTypeV::Scientific, false),205		b'E' => (ConvTypeV::Scientific, true),206		b'f' => (ConvTypeV::Float, false),207		b'F' => (ConvTypeV::Float, true),208		b'g' => (ConvTypeV::Shorter, false),209		b'G' => (ConvTypeV::Shorter, true),210		b'c' => (ConvTypeV::Char, false),211		b's' => (ConvTypeV::String, false),212		b'%' => (ConvTypeV::Percent, false),213		c => return Err(UnrecognizedConversionType(c as char)),214	};215216	Ok((ConvType { v: v.0, caps: v.1 }, &str[1..]))217}218219#[derive(Debug)]220pub struct Code<'s> {221	mkey: &'s str,222	cflags: CFlags,223	width: Width,224	precision: Option<Width>,225	convtype: ConvTypeV,226	caps: bool,227}228pub fn parse_code(str: &str) -> ParseResult<Code> {229	if str.is_empty() {230		return Err(TruncatedFormatCode);231	}232	let (mkey, str) = try_parse_mapping_key(str)?;233	let (cflags, str) = try_parse_cflags(str)?;234	let (width, str) = try_parse_field_width(str)?;235	let (precision, str) = try_parse_precision(str)?;236	let (_, str) = try_parse_length_modifier(str)?;237	let (convtype, str) = parse_conversion_type(str)?;238239	Ok((240		Code {241			mkey,242			cflags,243			width,244			precision,245			convtype: convtype.v,246			caps: convtype.caps,247		},248		str,249	))250}251252#[derive(Debug)]253pub enum Element<'s> {254	String(&'s str),255	Code(Code<'s>),256}257pub fn parse_codes(mut str: &str) -> Result<Vec<Element>> {258	let mut bytes = str.as_bytes();259	let mut out = vec![];260	let mut offset = 0;261262	loop {263		while offset != bytes.len() && bytes[offset] != b'%' {264			offset += 1;265		}266		if offset != 0 {267			out.push(Element::String(&str[0..offset]));268		}269		if offset == bytes.len() {270			return Ok(out);271		}272		str = &str[offset + 1..];273		let (code, nstr) = parse_code(str)?;274		str = nstr;275		bytes = str.as_bytes();276		offset = 0;277278		out.push(Element::Code(code))279	}280}281282const NUMBERS: &[u8] = b"0123456789abcdefghijklmnopqrstuvwxyz";283284#[inline]285pub fn render_integer(286	out: &mut String,287	iv: i64,288	padding: usize,289	precision: usize,290	blank: bool,291	sign: bool,292	radix: i64,293	prefix: &str,294	caps: bool,295) {296	// Digit char indexes in reverse order, i.e297	// for radix = 16 and n = 12f: [15, 2, 1]298	let digits = if iv == 0 {299		vec![0u8]300	} else {301		let mut v = iv.abs();302		let mut nums = Vec::with_capacity(1);303		while v > 0 {304			nums.push((v % radix) as u8);305			v /= radix;306		}307		nums308	};309	let neg = iv < 0;310	let zp = padding.saturating_sub(if neg || blank || sign { 1 } else { 0 });311	let zp2 = zp312		.max(precision)313		.saturating_sub(prefix.len() + digits.len());314315	if neg {316		out.push('-')317	} else if sign {318		out.push('+');319	} else if blank {320		out.push(' ');321	}322323	out.reserve(zp2);324	for _ in 0..zp2 {325		out.push('0');326	}327	out.push_str(prefix);328329	for digit in digits.into_iter().rev() {330		let ch = NUMBERS[digit as usize] as char;331		out.push(if caps { ch.to_ascii_uppercase() } else { ch });332	}333}334335pub fn render_decimal(336	out: &mut String,337	iv: i64,338	padding: usize,339	precision: usize,340	blank: bool,341	sign: bool,342) {343	render_integer(out, iv, padding, precision, blank, sign, 10, "", false)344}345pub fn render_octal(346	out: &mut String,347	iv: i64,348	padding: usize,349	precision: usize,350	alt: bool,351	blank: bool,352	sign: bool,353) {354	render_integer(355		out,356		iv,357		padding,358		precision,359		blank,360		sign,361		8,362		if alt && iv != 0 { "0" } else { "" },363		false,364	)365}366pub fn render_hexadecimal(367	out: &mut String,368	iv: i64,369	padding: usize,370	precision: usize,371	alt: bool,372	blank: bool,373	sign: bool,374	caps: bool,375) {376	render_integer(377		out,378		iv,379		padding,380		precision,381		blank,382		sign,383		16,384		match (alt, caps) {385			(true, true) => "0X",386			(true, false) => "0x",387			(false, _) => "",388		},389		caps,390	)391}392393pub fn render_float(394	out: &mut String,395	n: f64,396	mut padding: usize,397	precision: usize,398	blank: bool,399	sign: bool,400	ensure_pt: bool,401	trailing: bool,402) {403	let dot_size = if precision == 0 && !ensure_pt { 0 } else { 1 };404	padding = padding.saturating_sub(dot_size + precision);405	render_decimal(out, n.floor() as i64, padding, 0, blank, sign);406	if precision == 0 {407		if ensure_pt {408			out.push('.');409		}410		return;411	}412	let frac = n413		.fract()414		.mul_add(10.0_f64.powf(precision as f64), 0.5)415		.floor();416	if trailing || frac > 0.0 {417		out.push('.');418		let mut frac_str = String::new();419		render_decimal(&mut frac_str, frac as i64, precision, 0, false, false);420		let mut trim = frac_str.len();421		if !trailing {422			for b in frac_str.as_bytes().iter().rev() {423				if *b == b'0' {424					trim -= 1;425				}426			}427		}428		out.push_str(&frac_str[..trim]);429	} else if ensure_pt {430		out.push('.');431	}432}433434pub fn render_float_sci(435	out: &mut String,436	n: f64,437	mut padding: usize,438	precision: usize,439	blank: bool,440	sign: bool,441	ensure_pt: bool,442	trailing: bool,443	caps: bool,444) {445	let exponent = n.log10().floor();446	let mantissa = if exponent as i16 == -324 {447		n * 10.0 / 10.0_f64.powf(exponent + 1.0)448	} else {449		n / 10.0_f64.powf(exponent)450	};451	let mut exponent_str = String::new();452	render_decimal(&mut exponent_str, exponent as i64, 3, 0, false, true);453454	// +1 for e455	padding = padding.saturating_sub(exponent_str.len() + 1);456457	render_float(458		out, mantissa, padding, precision, blank, sign, ensure_pt, trailing,459	);460	out.push(if caps { 'E' } else { 'e' });461	out.push_str(&exponent_str);462}463464pub fn format_code(465	out: &mut String,466	value: &Val,467	code: &Code,468	width: usize,469	precision: Option<usize>,470) -> Result<()> {471	let clfags = &code.cflags;472	let (fpprec, iprec) = match precision {473		Some(v) => (v, v),474		None => (6, 0),475	};476	let padding = if clfags.zero && !clfags.left {477		width478	} else {479		0480	};481482	// TODO: If left padded, can optimize by writing directly to out483	let mut tmp_out = String::new();484485	match code.convtype {486		ConvTypeV::String => tmp_out.push_str(&value.clone().to_string()?),487		ConvTypeV::Decimal => {488			let value = f64::try_from(value.clone())?;489			render_decimal(490				&mut tmp_out,491				value as i64,492				padding,493				iprec,494				clfags.blank,495				clfags.sign,496			);497		}498		ConvTypeV::Octal => {499			let value = f64::try_from(value.clone())?;500			render_octal(501				&mut tmp_out,502				value as i64,503				padding,504				iprec,505				clfags.alt,506				clfags.blank,507				clfags.sign,508			);509		}510		ConvTypeV::Hexadecimal => {511			let value = f64::try_from(value.clone())?;512			render_hexadecimal(513				&mut tmp_out,514				value as i64,515				padding,516				iprec,517				clfags.alt,518				clfags.blank,519				clfags.sign,520				code.caps,521			);522		}523		ConvTypeV::Scientific => {524			let value = f64::try_from(value.clone())?;525			render_float_sci(526				&mut tmp_out,527				value,528				padding,529				fpprec,530				clfags.blank,531				clfags.sign,532				clfags.alt,533				true,534				code.caps,535			);536		}537		ConvTypeV::Float => {538			let value = f64::try_from(value.clone())?;539			render_float(540				&mut tmp_out,541				value,542				padding,543				fpprec,544				clfags.blank,545				clfags.sign,546				clfags.alt,547				true,548			);549		}550		ConvTypeV::Shorter => {551			let value = f64::try_from(value.clone())?;552			let exponent = value.log10().floor();553			if exponent < -4.0 || exponent >= fpprec as f64 {554				render_float_sci(555					&mut tmp_out,556					value,557					padding,558					fpprec - 1,559					clfags.blank,560					clfags.sign,561					clfags.alt,562					clfags.alt,563					code.caps,564				);565			} else {566				let digits_before_pt = 1.max(exponent as usize + 1);567				render_float(568					&mut tmp_out,569					value,570					padding,571					fpprec - digits_before_pt,572					clfags.blank,573					clfags.sign,574					clfags.alt,575					clfags.alt,576				);577			}578		}579		ConvTypeV::Char => match value.clone() {580			Val::Num(n) => tmp_out.push(581				std::char::from_u32(n as u32)582					.ok_or_else(|| InvalidUnicodeCodepointGot(n as u32))?,583			),584			Val::Str(s) => {585				if s.chars().count() != 1 {586					throw!(RuntimeError(587						format!("%c expected 1 char string, got {}", s.chars().count()).into(),588					));589				}590				tmp_out.push_str(&s);591			}592			_ => {593				throw!(TypeMismatch(594					"%c requires number/string",595					vec![ValType::Num, ValType::Str],596					value.value_type(),597				));598			}599		},600		ConvTypeV::Percent => tmp_out.push('%'),601	};602603	let padding = width.saturating_sub(tmp_out.len());604605	if !clfags.left {606		for _ in 0..padding {607			out.push(' ');608		}609	}610	out.push_str(&tmp_out);611	if clfags.left {612		for _ in 0..padding {613			out.push(' ');614		}615	}616617	Ok(())618}619620pub fn format_arr(str: &str, mut values: &[Val]) -> Result<String> {621	let codes = parse_codes(str)?;622	let mut out = String::new();623624	for code in codes {625		match code {626			Element::String(s) => {627				out.push_str(s);628			}629			Element::Code(c) => {630				let width = match c.width {631					Width::Star => {632						if values.is_empty() {633							throw!(NotEnoughValues);634						}635						let value = &values[0];636						values = &values[1..];637						usize::try_from(value.clone())?638					}639					Width::Fixed(n) => n,640				};641				let precision = match c.precision {642					Some(Width::Star) => {643						if values.is_empty() {644							throw!(NotEnoughValues);645						}646						let value = &values[0];647						values = &values[1..];648						Some(usize::try_from(value.clone())?)649					}650					Some(Width::Fixed(n)) => Some(n),651					None => None,652				};653654				// %% should not consume a value655				let value = if c.convtype == ConvTypeV::Percent {656					&Val::Null657				} else {658					if values.is_empty() {659						throw!(NotEnoughValues);660					}661					let value = &values[0];662					values = &values[1..];663					value664				};665666				format_code(&mut out, value, &c, width, precision)?;667			}668		}669	}670671	Ok(out)672}673674pub fn format_obj(str: &str, values: &ObjValue) -> Result<String> {675	let codes = parse_codes(str)?;676	let mut out = String::new();677678	for code in codes {679		match code {680			Element::String(s) => {681				out.push_str(s);682			}683			Element::Code(c) => {684				// TODO: Operate on ref685				let f: IStr = c.mkey.into();686				let width = match c.width {687					Width::Star => {688						throw!(CannotUseStarWidthWithObject);689					}690					Width::Fixed(n) => n,691				};692				let precision = match c.precision {693					Some(Width::Star) => {694						throw!(CannotUseStarWidthWithObject);695					}696					Some(Width::Fixed(n)) => Some(n),697					None => None,698				};699700				let value = if c.convtype == ConvTypeV::Percent {701					Val::Null702				} else {703					if f.is_empty() {704						throw!(MappingKeysRequired);705					}706					if let Some(v) = values.get(f.clone())? {707						v708					} else {709						throw!(NoSuchFormatField(f));710					}711				};712713				format_code(&mut out, &value, &c, width, precision)?;714			}715		}716	}717718	Ok(out)719}720721#[cfg(test)]722pub mod test_format {723	use super::*;724725	#[test]726	fn parse() {727		assert_eq!(728			parse_codes(729				"How much error budget is left looking at our %.3f%% availability gurantees?"730			)731			.unwrap()732			.len(),733			4734		);735	}736737	#[test]738	fn octals() {739		assert_eq!(format_arr("%#o", &[Val::Num(8.0)]).unwrap(), "010");740		assert_eq!(format_arr("%#4o", &[Val::Num(8.0)]).unwrap(), " 010");741		assert_eq!(format_arr("%4o", &[Val::Num(8.0)]).unwrap(), "  10");742		assert_eq!(format_arr("%04o", &[Val::Num(8.0)]).unwrap(), "0010");743		assert_eq!(format_arr("%+4o", &[Val::Num(8.0)]).unwrap(), " +10");744		assert_eq!(format_arr("%+04o", &[Val::Num(8.0)]).unwrap(), "+010");745		assert_eq!(format_arr("%-4o", &[Val::Num(8.0)]).unwrap(), "10  ");746		assert_eq!(format_arr("%+-4o", &[Val::Num(8.0)]).unwrap(), "+10 ");747		assert_eq!(format_arr("%+-04o", &[Val::Num(8.0)]).unwrap(), "+10 ");748	}749750	#[test]751	fn percent_doesnt_consumes_values() {752		assert_eq!(753			format_arr(754				"How much error budget is left looking at our %.3f%% availability gurantees?",755				&[Val::Num(4.0)]756			)757			.unwrap(),758			"How much error budget is left looking at our 4.000% availability gurantees?"759		);760	}761}
after · 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};5use gcmodule::Trace;6use jrsonnet_interner::IStr;7use jrsonnet_types::ValType;8use std::convert::TryFrom;9use thiserror::Error;1011#[derive(Debug, Clone, Error, Trace)]12pub enum FormatError {13	#[error("truncated format code")]14	TruncatedFormatCode,15	#[error("unrecognized conversion type: {0}")]16	UnrecognizedConversionType(char),1718	#[error("not enough values")]19	NotEnoughValues,2021	#[error("cannot use * width with object")]22	CannotUseStarWidthWithObject,23	#[error("mapping keys required")]24	MappingKeysRequired,25	#[error("no such format field: {0}")]26	NoSuchFormatField(IStr),27}2829impl From<FormatError> for LocError {30	fn from(e: FormatError) -> Self {31		Self::new(Format(e))32	}33}3435use FormatError::*;3637type ParseResult<'t, T> = std::result::Result<(T, &'t str), FormatError>;3839pub fn try_parse_mapping_key(str: &str) -> ParseResult<&str> {40	if str.is_empty() {41		return Err(TruncatedFormatCode);42	}43	let bytes = str.as_bytes();44	if bytes[0] == b'(' {45		let mut i = 1;46		while i < bytes.len() {47			if bytes[i] == b')' {48				return Ok((&str[1..i as usize], &str[i as usize + 1..]));49			}50			i += 1;51		}52		Err(TruncatedFormatCode)53	} else {54		Ok(("", str))55	}56}5758#[cfg(test)]59pub mod tests_key {60	use super::*;6162	#[test]63	fn parse_key() {64		assert_eq!(65			try_parse_mapping_key("(hello ) world").unwrap(),66			("hello ", " world")67		);68		assert_eq!(try_parse_mapping_key("() world").unwrap(), ("", " world"));69		assert_eq!(try_parse_mapping_key(" world").unwrap(), ("", " world"));70		assert_eq!(71			try_parse_mapping_key(" () world").unwrap(),72			("", " () world")73		);74	}7576	#[test]77	#[should_panic]78	fn parse_key_missing_start() {79		try_parse_mapping_key("").unwrap();80	}8182	#[test]83	#[should_panic]84	fn parse_key_missing_end() {85		try_parse_mapping_key("(   ").unwrap();86	}87}8889#[derive(Default, Debug)]90pub struct CFlags {91	pub alt: bool,92	pub zero: bool,93	pub left: bool,94	pub blank: bool,95	pub sign: bool,96}9798pub fn try_parse_cflags(str: &str) -> ParseResult<CFlags> {99	if str.is_empty() {100		return Err(TruncatedFormatCode);101	}102	let bytes = str.as_bytes();103	let mut i = 0;104	let mut out = CFlags::default();105	loop {106		if bytes.len() == i {107			return Err(TruncatedFormatCode);108		}109		match bytes[i] {110			b'#' => out.alt = true,111			b'0' => out.zero = true,112			b'-' => out.left = true,113			b' ' => out.blank = true,114			b'+' => out.sign = true,115			_ => break,116		}117		i += 1;118	}119	Ok((out, &str[i..]))120}121122#[derive(Debug, PartialEq)]123pub enum Width {124	Star,125	Fixed(usize),126}127pub fn try_parse_field_width(str: &str) -> ParseResult<Width> {128	if str.is_empty() {129		return Err(TruncatedFormatCode);130	}131	let bytes = str.as_bytes();132	if bytes[0] == b'*' {133		return Ok((Width::Star, &str[1..]));134	}135	let mut out: usize = 0;136	let mut digits = 0;137	while let Some(digit) = (bytes[digits] as char).to_digit(10) {138		out *= 10;139		out += digit as usize;140		digits += 1;141		if digits == bytes.len() {142			return Err(TruncatedFormatCode);143		}144	}145	Ok((Width::Fixed(out), &str[digits..]))146}147148pub fn try_parse_precision(str: &str) -> ParseResult<Option<Width>> {149	if str.is_empty() {150		return Err(TruncatedFormatCode);151	}152	let bytes = str.as_bytes();153	if bytes[0] == b'.' {154		try_parse_field_width(&str[1..]).map(|(r, s)| (Some(r), s))155	} else {156		Ok((None, str))157	}158}159160// Only skips161pub fn try_parse_length_modifier(str: &str) -> ParseResult<()> {162	if str.is_empty() {163		return Err(TruncatedFormatCode);164	}165	let bytes = str.as_bytes();166	let mut idx = 0;167	while bytes[idx] == b'h' || bytes[idx] == b'l' || bytes[idx] == b'L' {168		idx += 1;169		if bytes.len() == idx {170			return Err(TruncatedFormatCode);171		}172	}173	Ok(((), &str[idx..]))174}175176#[derive(Debug, PartialEq)]177pub enum ConvTypeV {178	Decimal,179	Octal,180	Hexadecimal,181	Scientific,182	Float,183	Shorter,184	Char,185	String,186	Percent,187}188pub struct ConvType {189	v: ConvTypeV,190	caps: bool,191}192193pub fn parse_conversion_type(str: &str) -> ParseResult<ConvType> {194	if str.is_empty() {195		return Err(TruncatedFormatCode);196	}197198	let code = str.as_bytes()[0];199	let v: (ConvTypeV, bool) = match code {200		b'd' | b'i' | b'u' => (ConvTypeV::Decimal, false),201		b'o' => (ConvTypeV::Octal, false),202		b'x' => (ConvTypeV::Hexadecimal, false),203		b'X' => (ConvTypeV::Hexadecimal, true),204		b'e' => (ConvTypeV::Scientific, false),205		b'E' => (ConvTypeV::Scientific, true),206		b'f' => (ConvTypeV::Float, false),207		b'F' => (ConvTypeV::Float, true),208		b'g' => (ConvTypeV::Shorter, false),209		b'G' => (ConvTypeV::Shorter, true),210		b'c' => (ConvTypeV::Char, false),211		b's' => (ConvTypeV::String, false),212		b'%' => (ConvTypeV::Percent, false),213		c => return Err(UnrecognizedConversionType(c as char)),214	};215216	Ok((ConvType { v: v.0, caps: v.1 }, &str[1..]))217}218219#[derive(Debug)]220pub struct Code<'s> {221	mkey: &'s str,222	cflags: CFlags,223	width: Width,224	precision: Option<Width>,225	convtype: ConvTypeV,226	caps: bool,227}228pub fn parse_code(str: &str) -> ParseResult<Code> {229	if str.is_empty() {230		return Err(TruncatedFormatCode);231	}232	let (mkey, str) = try_parse_mapping_key(str)?;233	let (cflags, str) = try_parse_cflags(str)?;234	let (width, str) = try_parse_field_width(str)?;235	let (precision, str) = try_parse_precision(str)?;236	let (_, str) = try_parse_length_modifier(str)?;237	let (convtype, str) = parse_conversion_type(str)?;238239	Ok((240		Code {241			mkey,242			cflags,243			width,244			precision,245			convtype: convtype.v,246			caps: convtype.caps,247		},248		str,249	))250}251252#[derive(Debug)]253pub enum Element<'s> {254	String(&'s str),255	Code(Code<'s>),256}257pub fn parse_codes(mut str: &str) -> Result<Vec<Element>> {258	let mut bytes = str.as_bytes();259	let mut out = vec![];260	let mut offset = 0;261262	loop {263		while offset != bytes.len() && bytes[offset] != b'%' {264			offset += 1;265		}266		if offset != 0 {267			out.push(Element::String(&str[0..offset]));268		}269		if offset == bytes.len() {270			return Ok(out);271		}272		str = &str[offset + 1..];273		let (code, nstr) = parse_code(str)?;274		str = nstr;275		bytes = str.as_bytes();276		offset = 0;277278		out.push(Element::Code(code))279	}280}281282const NUMBERS: &[u8] = b"0123456789abcdefghijklmnopqrstuvwxyz";283284#[inline]285pub fn render_integer(286	out: &mut String,287	iv: i64,288	padding: usize,289	precision: usize,290	blank: bool,291	sign: bool,292	radix: i64,293	prefix: &str,294	caps: bool,295) {296	// Digit char indexes in reverse order, i.e297	// for radix = 16 and n = 12f: [15, 2, 1]298	let digits = if iv == 0 {299		vec![0u8]300	} else {301		let mut v = iv.abs();302		let mut nums = Vec::with_capacity(1);303		while v > 0 {304			nums.push((v % radix) as u8);305			v /= radix;306		}307		nums308	};309	let neg = iv < 0;310	let zp = padding.saturating_sub(if neg || blank || sign { 1 } else { 0 });311	let zp2 = zp312		.max(precision)313		.saturating_sub(prefix.len() + digits.len());314315	if neg {316		out.push('-')317	} else if sign {318		out.push('+');319	} else if blank {320		out.push(' ');321	}322323	out.reserve(zp2);324	for _ in 0..zp2 {325		out.push('0');326	}327	out.push_str(prefix);328329	for digit in digits.into_iter().rev() {330		let ch = NUMBERS[digit as usize] as char;331		out.push(if caps { ch.to_ascii_uppercase() } else { ch });332	}333}334335pub fn render_decimal(336	out: &mut String,337	iv: i64,338	padding: usize,339	precision: usize,340	blank: bool,341	sign: bool,342) {343	render_integer(out, iv, padding, precision, blank, sign, 10, "", false)344}345pub fn render_octal(346	out: &mut String,347	iv: i64,348	padding: usize,349	precision: usize,350	alt: bool,351	blank: bool,352	sign: bool,353) {354	render_integer(355		out,356		iv,357		padding,358		precision,359		blank,360		sign,361		8,362		if alt && iv != 0 { "0" } else { "" },363		false,364	)365}366pub fn render_hexadecimal(367	out: &mut String,368	iv: i64,369	padding: usize,370	precision: usize,371	alt: bool,372	blank: bool,373	sign: bool,374	caps: bool,375) {376	render_integer(377		out,378		iv,379		padding,380		precision,381		blank,382		sign,383		16,384		match (alt, caps) {385			(true, true) => "0X",386			(true, false) => "0x",387			(false, _) => "",388		},389		caps,390	)391}392393pub fn render_float(394	out: &mut String,395	n: f64,396	mut padding: usize,397	precision: usize,398	blank: bool,399	sign: bool,400	ensure_pt: bool,401	trailing: bool,402) {403	let dot_size = if precision == 0 && !ensure_pt { 0 } else { 1 };404	padding = padding.saturating_sub(dot_size + precision);405	render_decimal(out, n.floor() as i64, padding, 0, blank, sign);406	if precision == 0 {407		if ensure_pt {408			out.push('.');409		}410		return;411	}412	let frac = n413		.fract()414		.mul_add(10.0_f64.powf(precision as f64), 0.5)415		.floor();416	if trailing || frac > 0.0 {417		out.push('.');418		let mut frac_str = String::new();419		render_decimal(&mut frac_str, frac as i64, precision, 0, false, false);420		let mut trim = frac_str.len();421		if !trailing {422			for b in frac_str.as_bytes().iter().rev() {423				if *b == b'0' {424					trim -= 1;425				}426			}427		}428		out.push_str(&frac_str[..trim]);429	} else if ensure_pt {430		out.push('.');431	}432}433434pub fn render_float_sci(435	out: &mut String,436	n: f64,437	mut padding: usize,438	precision: usize,439	blank: bool,440	sign: bool,441	ensure_pt: bool,442	trailing: bool,443	caps: bool,444) {445	let exponent = n.log10().floor();446	let mantissa = if exponent as i16 == -324 {447		n * 10.0 / 10.0_f64.powf(exponent + 1.0)448	} else {449		n / 10.0_f64.powf(exponent)450	};451	let mut exponent_str = String::new();452	render_decimal(&mut exponent_str, exponent as i64, 3, 0, false, true);453454	// +1 for e455	padding = padding.saturating_sub(exponent_str.len() + 1);456457	render_float(458		out, mantissa, padding, precision, blank, sign, ensure_pt, trailing,459	);460	out.push(if caps { 'E' } else { 'e' });461	out.push_str(&exponent_str);462}463464pub fn format_code(465	out: &mut String,466	value: &Val,467	code: &Code,468	width: usize,469	precision: Option<usize>,470) -> Result<()> {471	let clfags = &code.cflags;472	let (fpprec, iprec) = match precision {473		Some(v) => (v, v),474		None => (6, 0),475	};476	let padding = if clfags.zero && !clfags.left {477		width478	} else {479		0480	};481482	// TODO: If left padded, can optimize by writing directly to out483	let mut tmp_out = String::new();484485	match code.convtype {486		ConvTypeV::String => tmp_out.push_str(&value.clone().to_string()?),487		ConvTypeV::Decimal => {488			let value = f64::try_from(value.clone())?;489			render_decimal(490				&mut tmp_out,491				value as i64,492				padding,493				iprec,494				clfags.blank,495				clfags.sign,496			);497		}498		ConvTypeV::Octal => {499			let value = f64::try_from(value.clone())?;500			render_octal(501				&mut tmp_out,502				value as i64,503				padding,504				iprec,505				clfags.alt,506				clfags.blank,507				clfags.sign,508			);509		}510		ConvTypeV::Hexadecimal => {511			let value = f64::try_from(value.clone())?;512			render_hexadecimal(513				&mut tmp_out,514				value as i64,515				padding,516				iprec,517				clfags.alt,518				clfags.blank,519				clfags.sign,520				code.caps,521			);522		}523		ConvTypeV::Scientific => {524			let value = f64::try_from(value.clone())?;525			render_float_sci(526				&mut tmp_out,527				value,528				padding,529				fpprec,530				clfags.blank,531				clfags.sign,532				clfags.alt,533				true,534				code.caps,535			);536		}537		ConvTypeV::Float => {538			let value = f64::try_from(value.clone())?;539			render_float(540				&mut tmp_out,541				value,542				padding,543				fpprec,544				clfags.blank,545				clfags.sign,546				clfags.alt,547				true,548			);549		}550		ConvTypeV::Shorter => {551			let value = f64::try_from(value.clone())?;552			let exponent = value.log10().floor();553			if exponent < -4.0 || exponent >= fpprec as f64 {554				render_float_sci(555					&mut tmp_out,556					value,557					padding,558					fpprec - 1,559					clfags.blank,560					clfags.sign,561					clfags.alt,562					clfags.alt,563					code.caps,564				);565			} else {566				let digits_before_pt = 1.max(exponent as usize + 1);567				render_float(568					&mut tmp_out,569					value,570					padding,571					fpprec - digits_before_pt,572					clfags.blank,573					clfags.sign,574					clfags.alt,575					clfags.alt,576				);577			}578		}579		ConvTypeV::Char => match value.clone() {580			Val::Num(n) => tmp_out581				.push(std::char::from_u32(n as u32).ok_or(InvalidUnicodeCodepointGot(n as u32))?),582			Val::Str(s) => {583				if s.chars().count() != 1 {584					throw!(RuntimeError(585						format!("%c expected 1 char string, got {}", s.chars().count()).into(),586					));587				}588				tmp_out.push_str(&s);589			}590			_ => {591				throw!(TypeMismatch(592					"%c requires number/string",593					vec![ValType::Num, ValType::Str],594					value.value_type(),595				));596			}597		},598		ConvTypeV::Percent => tmp_out.push('%'),599	};600601	let padding = width.saturating_sub(tmp_out.len());602603	if !clfags.left {604		for _ in 0..padding {605			out.push(' ');606		}607	}608	out.push_str(&tmp_out);609	if clfags.left {610		for _ in 0..padding {611			out.push(' ');612		}613	}614615	Ok(())616}617618pub fn format_arr(str: &str, mut values: &[Val]) -> Result<String> {619	let codes = parse_codes(str)?;620	let mut out = String::new();621622	for code in codes {623		match code {624			Element::String(s) => {625				out.push_str(s);626			}627			Element::Code(c) => {628				let width = match c.width {629					Width::Star => {630						if values.is_empty() {631							throw!(NotEnoughValues);632						}633						let value = &values[0];634						values = &values[1..];635						usize::try_from(value.clone())?636					}637					Width::Fixed(n) => n,638				};639				let precision = match c.precision {640					Some(Width::Star) => {641						if values.is_empty() {642							throw!(NotEnoughValues);643						}644						let value = &values[0];645						values = &values[1..];646						Some(usize::try_from(value.clone())?)647					}648					Some(Width::Fixed(n)) => Some(n),649					None => None,650				};651652				// %% should not consume a value653				let value = if c.convtype == ConvTypeV::Percent {654					&Val::Null655				} else {656					if values.is_empty() {657						throw!(NotEnoughValues);658					}659					let value = &values[0];660					values = &values[1..];661					value662				};663664				format_code(&mut out, value, &c, width, precision)?;665			}666		}667	}668669	Ok(out)670}671672pub fn format_obj(str: &str, values: &ObjValue) -> Result<String> {673	let codes = parse_codes(str)?;674	let mut out = String::new();675676	for code in codes {677		match code {678			Element::String(s) => {679				out.push_str(s);680			}681			Element::Code(c) => {682				// TODO: Operate on ref683				let f: IStr = c.mkey.into();684				let width = match c.width {685					Width::Star => {686						throw!(CannotUseStarWidthWithObject);687					}688					Width::Fixed(n) => n,689				};690				let precision = match c.precision {691					Some(Width::Star) => {692						throw!(CannotUseStarWidthWithObject);693					}694					Some(Width::Fixed(n)) => Some(n),695					None => None,696				};697698				let value = if c.convtype == ConvTypeV::Percent {699					Val::Null700				} else {701					if f.is_empty() {702						throw!(MappingKeysRequired);703					}704					if let Some(v) = values.get(f.clone())? {705						v706					} else {707						throw!(NoSuchFormatField(f));708					}709				};710711				format_code(&mut out, &value, &c, width, precision)?;712			}713		}714	}715716	Ok(out)717}718719#[cfg(test)]720pub mod test_format {721	use super::*;722723	#[test]724	fn parse() {725		assert_eq!(726			parse_codes(727				"How much error budget is left looking at our %.3f%% availability gurantees?"728			)729			.unwrap()730			.len(),731			4732		);733	}734735	#[test]736	fn octals() {737		assert_eq!(format_arr("%#o", &[Val::Num(8.0)]).unwrap(), "010");738		assert_eq!(format_arr("%#4o", &[Val::Num(8.0)]).unwrap(), " 010");739		assert_eq!(format_arr("%4o", &[Val::Num(8.0)]).unwrap(), "  10");740		assert_eq!(format_arr("%04o", &[Val::Num(8.0)]).unwrap(), "0010");741		assert_eq!(format_arr("%+4o", &[Val::Num(8.0)]).unwrap(), " +10");742		assert_eq!(format_arr("%+04o", &[Val::Num(8.0)]).unwrap(), "+010");743		assert_eq!(format_arr("%-4o", &[Val::Num(8.0)]).unwrap(), "10  ");744		assert_eq!(format_arr("%+-4o", &[Val::Num(8.0)]).unwrap(), "+10 ");745		assert_eq!(format_arr("%+-04o", &[Val::Num(8.0)]).unwrap(), "+10 ");746	}747748	#[test]749	fn percent_doesnt_consumes_values() {750		assert_eq!(751			format_arr(752				"How much error budget is left looking at our %.3f%% availability gurantees?",753				&[Val::Num(4.0)]754			)755			.unwrap(),756			"How much error budget is left looking at our 4.000% availability gurantees?"757		);758	}759}
modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -1,3 +1,4 @@
+use crate::function::StaticBuiltin;
 use crate::typed::{Any, Either, Null, PositiveF64, VecVal, M1};
 use crate::{self as jrsonnet_evaluator, ObjValue};
 use crate::{
@@ -5,21 +6,16 @@
 	equals,
 	error::{Error::*, Result},
 	operator::evaluate_mod_op,
-	primitive_equals, push_frame, throw, with_state, ArrValue, Context, FuncVal,
-	IndexableVal, Val,
+	primitive_equals, push_frame, throw, with_state, ArrValue, Context, FuncVal, IndexableVal, Val,
 };
 use format::{format_arr, format_obj};
 use gcmodule::Cc;
 use jrsonnet_interner::IStr;
-use jrsonnet_parser::{ArgsDesc, ExprLocation};
+use jrsonnet_parser::ExprLocation;
 use serde::Deserialize;
 use serde_yaml::DeserializingQuirks;
-use std::{
-	collections::HashMap,
-	convert::{TryFrom, TryInto},
-	path::PathBuf,
-	rc::Rc,
-};
+use std::collections::HashMap;
+use std::convert::{TryFrom, TryInto};
 
 pub mod stdlib;
 pub use stdlib::*;
@@ -32,7 +28,7 @@
 
 pub fn std_format(str: IStr, vals: Val) -> Result<String> {
 	push_frame(
-		&ExprLocation(Rc::from(PathBuf::from("std.jsonnet")), 0, 0),
+		None,
 		|| format!("std.format of {}", str),
 		|| {
 			Ok(match vals {
@@ -75,86 +71,85 @@
 		)),
 	}
 }
-
-type Builtin = fn(context: Context, loc: &ExprLocation, args: &ArgsDesc) -> Result<Val>;
 
-type BuiltinsType = HashMap<Box<str>, Builtin>;
+type BuiltinsType = HashMap<IStr, &'static dyn StaticBuiltin>;
 
 thread_local! {
-	static BUILTINS: BuiltinsType = {
+	pub static BUILTINS: BuiltinsType = {
 		[
-			("length".into(), builtin_length as Builtin),
-			("type".into(), builtin_type),
-			("makeArray".into(), builtin_make_array),
-			("codepoint".into(), builtin_codepoint),
-			("objectFieldsEx".into(), builtin_object_fields_ex),
-			("objectHasEx".into(), builtin_object_has_ex),
-			("slice".into(), builtin_slice),
-			("substr".into(), builtin_substr),
-			("primitiveEquals".into(), builtin_primitive_equals),
-			("equals".into(), builtin_equals),
-			("modulo".into(), builtin_modulo),
-			("mod".into(), builtin_mod),
-			("floor".into(), builtin_floor),
-			("ceil".into(), builtin_ceil),
-			("log".into(), builtin_log),
-			("pow".into(), builtin_pow),
-			("sqrt".into(), builtin_sqrt),
-			("sin".into(), builtin_sin),
-			("cos".into(), builtin_cos),
-			("tan".into(), builtin_tan),
-			("asin".into(), builtin_asin),
-			("acos".into(), builtin_acos),
-			("atan".into(), builtin_atan),
-			("exp".into(), builtin_exp),
-			("mantissa".into(), builtin_mantissa),
-			("exponent".into(), builtin_exponent),
-			("extVar".into(), builtin_ext_var),
-			("native".into(), builtin_native),
-			("filter".into(), builtin_filter),
-			("map".into(), builtin_map),
-			("flatMap".into(), builtin_flatmap),
-			("foldl".into(), builtin_foldl),
-			("foldr".into(), builtin_foldr),
-			("sortImpl".into(), builtin_sort_impl),
-			("format".into(), builtin_format),
-			("range".into(), builtin_range),
-			("char".into(), builtin_char),
-			("encodeUTF8".into(), builtin_encode_utf8),
-			("decodeUTF8".into(), builtin_decode_utf8),
-			("md5".into(), builtin_md5),
-			("base64".into(), builtin_base64),
-			("base64DecodeBytes".into(), builtin_base64_decode_bytes),
-			("base64Decode".into(), builtin_base64_decode),
-			("trace".into(), builtin_trace),
-			("join".into(), builtin_join),
-			("escapeStringJson".into(), builtin_escape_string_json),
-			("manifestJsonEx".into(), builtin_manifest_json_ex),
-			("manifestYamlDocImpl".into(), builtin_manifest_yaml_doc),
-			("reverse".into(), builtin_reverse),
-			("id".into(), builtin_id),
-			("strReplace".into(), builtin_str_replace),
-			("splitLimit".into(), builtin_splitlimit),
-			("parseJson".into(), builtin_parse_json),
-			("parseYaml".into(), builtin_parse_yaml),
-			("asciiUpper".into(), builtin_ascii_upper),
-			("asciiLower".into(), builtin_ascii_lower),
-			("member".into(), builtin_member),
-			("count".into(), builtin_count),
+			("length".into(), builtin_length::INST),
+			("type".into(), builtin_type::INST),
+			("makeArray".into(), builtin_make_array::INST),
+			("codepoint".into(), builtin_codepoint::INST),
+			("objectFieldsEx".into(), builtin_object_fields_ex::INST),
+			("objectHasEx".into(), builtin_object_has_ex::INST),
+			("slice".into(), builtin_slice::INST),
+			("substr".into(), builtin_substr::INST),
+			("primitiveEquals".into(), builtin_primitive_equals::INST),
+			("equals".into(), builtin_equals::INST),
+			("modulo".into(), builtin_modulo::INST),
+			("mod".into(), builtin_mod::INST),
+			("floor".into(), builtin_floor::INST),
+			("ceil".into(), builtin_ceil::INST),
+			("log".into(), builtin_log::INST),
+			("pow".into(), builtin_pow::INST),
+			("sqrt".into(), builtin_sqrt::INST),
+			("sin".into(), builtin_sin::INST),
+			("cos".into(), builtin_cos::INST),
+			("tan".into(), builtin_tan::INST),
+			("asin".into(), builtin_asin::INST),
+			("acos".into(), builtin_acos::INST),
+			("atan".into(), builtin_atan::INST),
+			("exp".into(), builtin_exp::INST),
+			("mantissa".into(), builtin_mantissa::INST),
+			("exponent".into(), builtin_exponent::INST),
+			("extVar".into(), builtin_ext_var::INST),
+			("native".into(), builtin_native::INST),
+			("filter".into(), builtin_filter::INST),
+			("map".into(), builtin_map::INST),
+			("flatMap".into(), builtin_flatmap::INST),
+			("foldl".into(), builtin_foldl::INST),
+			("foldr".into(), builtin_foldr::INST),
+			("sort".into(), builtin_sort::INST),
+			("format".into(), builtin_format::INST),
+			("range".into(), builtin_range::INST),
+			("char".into(), builtin_char::INST),
+			("encodeUTF8".into(), builtin_encode_utf8::INST),
+			("decodeUTF8".into(), builtin_decode_utf8::INST),
+			("md5".into(), builtin_md5::INST),
+			("base64".into(), builtin_base64::INST),
+			("base64DecodeBytes".into(), builtin_base64_decode_bytes::INST),
+			("base64Decode".into(), builtin_base64_decode::INST),
+			("trace".into(), builtin_trace::INST),
+			("join".into(), builtin_join::INST),
+			("escapeStringJson".into(), builtin_escape_string_json::INST),
+			("manifestJsonEx".into(), builtin_manifest_json_ex::INST),
+			("manifestYamlDoc".into(), builtin_manifest_yaml_doc::INST),
+			("reverse".into(), builtin_reverse::INST),
+			("id".into(), builtin_id::INST),
+			("strReplace".into(), builtin_str_replace::INST),
+			("splitLimit".into(), builtin_splitlimit::INST),
+			("parseJson".into(), builtin_parse_json::INST),
+			("parseYaml".into(), builtin_parse_yaml::INST),
+			("asciiUpper".into(), builtin_ascii_upper::INST),
+			("asciiLower".into(), builtin_ascii_lower::INST),
+			("member".into(), builtin_member::INST),
+			("count".into(), builtin_count::INST),
 		].iter().cloned().collect()
 	};
 }
 
 #[jrsonnet_macros::builtin]
-fn builtin_length(x: Either<IStr, Either<VecVal, ObjValue>>) -> Result<usize> {
+fn builtin_length(x: Either<IStr, Either<VecVal, Either<ObjValue, Cc<FuncVal>>>>) -> Result<usize> {
 	Ok(match x {
 		Either::Left(x) => x.len(),
 		Either::Right(Either::Left(x)) => x.0.len(),
-		Either::Right(Either::Right(x)) => x
+		Either::Right(Either::Right(Either::Left(x))) => x
 			.fields_visibility()
 			.into_iter()
 			.filter(|(_k, v)| *v)
 			.count(),
+		Either::Right(Either::Right(Either::Right(f))) => f.args_len(),
 	})
 }
 
@@ -167,7 +162,7 @@
 fn builtin_make_array(sz: usize, func: Cc<FuncVal>) -> Result<VecVal> {
 	let mut out = Vec::with_capacity(sz);
 	for i in 0..sz {
-		out.push(func.evaluate_values(&[Val::Num(i as f64)])?)
+		out.push(func.evaluate_simple(&[i as f64].as_slice())?)
 	}
 	Ok(VecVal(out))
 }
@@ -354,12 +349,12 @@
 
 #[jrsonnet_macros::builtin]
 fn builtin_filter(func: Cc<FuncVal>, arr: ArrValue) -> Result<ArrValue> {
-	arr.filter(|val| bool::try_from(func.evaluate_values(&[val.clone()])?))
+	arr.filter(|val| bool::try_from(func.evaluate_simple(&[Any(val.clone())].as_slice())?))
 }
 
 #[jrsonnet_macros::builtin]
 fn builtin_map(func: Cc<FuncVal>, arr: ArrValue) -> Result<ArrValue> {
-	arr.map(|val| func.evaluate_values(&[val]))
+	arr.map(|val| func.evaluate_simple(&[Any(val)].as_slice()))
 }
 
 #[jrsonnet_macros::builtin]
@@ -368,7 +363,7 @@
 		IndexableVal::Str(s) => {
 			let mut out = String::new();
 			for c in s.chars() {
-				match func.evaluate_values(&[Val::Str(c.to_string().into())])? {
+				match func.evaluate_simple(&[c.to_string()].as_slice())? {
 					Val::Str(o) => out.push_str(&o),
 					_ => throw!(RuntimeError(
 						"in std.join all items should be strings".into()
@@ -381,7 +376,7 @@
 			let mut out = Vec::new();
 			for el in a.iter() {
 				let el = el?;
-				match func.evaluate_values(&[el])? {
+				match func.evaluate_simple(&[Any(el)].as_slice())? {
 					Val::Arr(o) => {
 						for oe in o.iter() {
 							out.push(oe?)
@@ -401,7 +396,7 @@
 fn builtin_foldl(func: Cc<FuncVal>, arr: ArrValue, init: Any) -> Result<Any> {
 	let mut acc = init.0;
 	for i in arr.iter() {
-		acc = func.evaluate_values(&[acc, i?])?;
+		acc = func.evaluate_simple(&[Any(acc), Any(i?)].as_slice())?;
 	}
 	Ok(Any(acc))
 }
@@ -410,18 +405,21 @@
 fn builtin_foldr(func: Cc<FuncVal>, arr: ArrValue, init: Any) -> Result<Any> {
 	let mut acc = init.0;
 	for i in arr.iter().rev() {
-		acc = func.evaluate_values(&[i?, acc])?;
+		acc = func.evaluate_simple(&[Any(i?), Any(acc)].as_slice())?;
 	}
 	Ok(Any(acc))
 }
 
 #[jrsonnet_macros::builtin]
 #[allow(non_snake_case)]
-fn builtin_sort_impl(arr: ArrValue, keyF: Cc<FuncVal>) -> Result<ArrValue> {
+fn builtin_sort(arr: ArrValue, keyF: Option<Cc<FuncVal>>) -> Result<ArrValue> {
 	if arr.len() <= 1 {
 		return Ok(arr);
 	}
-	Ok(ArrValue::Eager(sort::sort(arr.evaluated()?, &keyF)?))
+	Ok(ArrValue::Eager(sort::sort(
+		arr.evaluated()?,
+		keyF.as_deref(),
+	)?))
 }
 
 #[jrsonnet_macros::builtin]
@@ -443,7 +441,7 @@
 
 #[jrsonnet_macros::builtin]
 fn builtin_char(n: u32) -> Result<char> {
-	Ok(std::char::from_u32(n as u32).ok_or_else(|| InvalidUnicodeCodepointGot(n as u32))?)
+	Ok(std::char::from_u32(n as u32).ok_or(InvalidUnicodeCodepointGot(n as u32))?)
 }
 
 #[jrsonnet_macros::builtin]
@@ -466,16 +464,18 @@
 }
 
 #[jrsonnet_macros::builtin]
-fn builtin_trace(#[location] loc: &ExprLocation, str: IStr, rest: Any) -> Result<Any> {
+fn builtin_trace(#[location] loc: Option<&ExprLocation>, str: IStr, rest: Any) -> Result<Any> {
 	eprint!("TRACE:");
-	with_state(|s| {
-		let locs = s.map_source_locations(&loc.0, &[loc.1]);
-		eprint!(
-			" {}:{}",
-			loc.0.file_name().unwrap().to_str().unwrap(),
-			locs[0].line
-		);
-	});
+	if let Some(loc) = loc {
+		with_state(|s| {
+			let locs = s.map_source_locations(&loc.0, &[loc.1]);
+			eprint!(
+				" {}:{}",
+				loc.0.file_name().unwrap().to_str().unwrap(),
+				locs[0].line
+			);
+		});
+	}
 	eprintln!(" {}", str);
 	Ok(rest) as Result<Any>
 }
@@ -574,15 +574,19 @@
 #[jrsonnet_macros::builtin]
 fn builtin_manifest_yaml_doc(
 	value: Any,
-	indent_array_in_object: bool,
-	quote_keys: bool,
+	indent_array_in_object: Option<bool>,
+	quote_keys: Option<bool>,
 ) -> Result<String> {
 	manifest_yaml_ex(
 		&value.0,
 		&ManifestYamlOptions {
 			padding: "  ",
-			arr_element_padding: if indent_array_in_object { "  " } else { "" },
-			quote_keys,
+			arr_element_padding: if indent_array_in_object.unwrap_or(false) {
+				"  "
+			} else {
+				""
+			},
+			quote_keys: quote_keys.unwrap_or(true),
 		},
 	)
 }
@@ -648,15 +652,4 @@
 		}
 	}
 	Ok(count)
-}
-
-pub fn call_builtin(
-	context: Context,
-	loc: &ExprLocation,
-	name: &str,
-	args: &ArgsDesc,
-) -> Result<Val> {
-	BUILTINS
-		.with(|builtins| builtins.get(name).copied())
-		.ok_or_else(|| IntrinsicNotFound(name.into()))?(context, loc, args)
 }
modifiedcrates/jrsonnet-evaluator/src/builtin/sort.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/sort.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/sort.rs
@@ -1,6 +1,8 @@
 use crate::{
 	error::{Error, LocError, Result},
-	throw, FuncVal, Val,
+	throw,
+	typed::Any,
+	FuncVal, Val,
 };
 use gcmodule::{Cc, Trace};
 
@@ -59,42 +61,47 @@
 	Ok(sort_type)
 }
 
-pub fn sort(values: Cc<Vec<Val>>, key_getter: &FuncVal) -> Result<Cc<Vec<Val>>> {
+pub fn sort(values: Cc<Vec<Val>>, key_getter: Option<&FuncVal>) -> Result<Cc<Vec<Val>>> {
 	if values.len() <= 1 {
 		return Ok(values);
 	}
-	if key_getter.is_ident() {
-		let mut mvalues = (*values).clone();
-		let sort_type = get_sort_type(&mut mvalues, |k| k)?;
+	if let Some(key_getter) = key_getter {
+		// Slow path, user provided key getter
+		let mut vk = Vec::with_capacity(values.len());
+		for value in values.iter() {
+			vk.push((
+				value.clone(),
+				key_getter.evaluate_simple(&[Any(value.clone())].as_slice())?,
+			));
+		}
+		let sort_type = get_sort_type(&mut vk, |v| &mut v.1)?;
 		match sort_type {
-			SortKeyType::Number => mvalues.sort_by_key(|v| match v {
-				Val::Num(n) => NonNaNf64(*n),
+			SortKeyType::Number => vk.sort_by_key(|v| match v.1 {
+				Val::Num(n) => NonNaNf64(n),
 				_ => unreachable!(),
 			}),
-			SortKeyType::String => mvalues.sort_by_key(|v| match v {
+			SortKeyType::String => vk.sort_by_key(|v| match &v.1 {
 				Val::Str(s) => s.clone(),
 				_ => unreachable!(),
 			}),
 			SortKeyType::Unknown => unreachable!(),
 		};
-		Ok(Cc::new(mvalues))
+		Ok(Cc::new(vk.into_iter().map(|v| v.0).collect()))
 	} else {
-		let mut vk = Vec::with_capacity(values.len());
-		for value in values.iter() {
-			vk.push((value.clone(), key_getter.evaluate_values(&[value.clone()])?));
-		}
-		let sort_type = get_sort_type(&mut vk, |v| &mut v.1)?;
+		// Fast path, identity key getter
+		let mut mvalues = (*values).clone();
+		let sort_type = get_sort_type(&mut mvalues, |k| k)?;
 		match sort_type {
-			SortKeyType::Number => vk.sort_by_key(|v| match v.1 {
-				Val::Num(n) => NonNaNf64(n),
+			SortKeyType::Number => mvalues.sort_unstable_by_key(|v| match v {
+				Val::Num(n) => NonNaNf64(*n),
 				_ => unreachable!(),
 			}),
-			SortKeyType::String => vk.sort_by_key(|v| match &v.1 {
+			SortKeyType::String => mvalues.sort_unstable_by_key(|v| match v {
 				Val::Str(s) => s.clone(),
 				_ => unreachable!(),
 			}),
 			SortKeyType::Unknown => unreachable!(),
 		};
-		Ok(Cc::new(vk.into_iter().map(|v| v.0).collect()))
+		Ok(Cc::new(mvalues))
 	}
 }
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -1,7 +1,7 @@
 use std::convert::TryFrom;
 
 use crate::{
-	builtin::std_slice,
+	builtin::{std_slice, BUILTINS},
 	error::Error::*,
 	evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},
 	gc::TraceBox,
@@ -192,7 +192,7 @@
 	Ok(match field_name {
 		jrsonnet_parser::FieldName::Fixed(n) => Some(n.clone()),
 		jrsonnet_parser::FieldName::Dyn(expr) => push_frame(
-			&expr.1,
+			Some(&expr.1),
 			|| "evaluating field name".to_string(),
 			|| {
 				let value = evaluate(context, expr)?;
@@ -442,7 +442,7 @@
 	context: Context,
 	value: &LocExpr,
 	args: &ArgsDesc,
-	loc: &ExprLocation,
+	loc: Option<&ExprLocation>,
 	tailstrict: bool,
 ) -> Result<Val> {
 	let value = evaluate(context.clone(), value)?;
@@ -463,13 +463,13 @@
 	let value = &assertion.0;
 	let msg = &assertion.1;
 	let assertion_result = push_frame(
-		&value.1,
+		Some(&value.1),
 		|| "assertion condition".to_owned(),
 		|| bool::try_from(evaluate(context.clone(), value)?),
 	)?;
 	if !assertion_result {
 		push_frame(
-			&value.1,
+			Some(&value.1),
 			|| "assertion failure".to_owned(),
 			|| {
 				if let Some(msg) = msg {
@@ -519,7 +519,7 @@
 		BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, v1, *o, v2)?,
 		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,
 		Var(name) => push_frame(
-			loc,
+			Some(loc),
 			|| format!("variable <{}> access", name),
 			|| context.binding(name.clone())?.evaluate(),
 		)?,
@@ -528,7 +528,7 @@
 				(Val::Obj(v), Val::Str(s)) => {
 					let sn = s.clone();
 					push_frame(
-						loc,
+						Some(loc),
 						|| format!("field <{}> access", sn),
 						|| {
 							if let Some(v) = v.get(s.clone())? {
@@ -624,17 +624,23 @@
 			&evaluate(context.clone(), s)?,
 			&Val::Obj(evaluate_object(context, t)?),
 		)?,
-		Apply(value, args, tailstrict) => evaluate_apply(context, value, args, loc, *tailstrict)?,
+		Apply(value, args, tailstrict) => {
+			evaluate_apply(context, value, args, Some(loc), *tailstrict)?
+		}
 		Function(params, body) => {
 			evaluate_method(context, "anonymous".into(), params.clone(), body.clone())
 		}
-		Intrinsic(name) => Val::Func(Cc::new(FuncVal::Intrinsic(name.clone()))),
+		Intrinsic(name) => Val::Func(Cc::new(FuncVal::StaticBuiltin(
+			BUILTINS
+				.with(|b| b.get(name).copied())
+				.ok_or_else(|| IntrinsicNotFound(name.clone()))?,
+		))),
 		AssertExpr(assert, returned) => {
 			evaluate_assert(context.clone(), assert)?;
 			evaluate(context, returned)?
 		}
 		ErrorStmt(e) => push_frame(
-			loc,
+			Some(loc),
 			|| "error statement".to_owned(),
 			|| throw!(RuntimeError(IStr::try_from(evaluate(context, e)?)?,)),
 		)?,
@@ -644,7 +650,7 @@
 			cond_else,
 		} => {
 			if push_frame(
-				loc,
+				Some(loc),
 				|| "if condition".to_owned(),
 				|| bool::try_from(evaluate(context.clone(), &cond.0)?),
 			)? {
@@ -683,7 +689,7 @@
 			let mut import_location = tmp.to_path_buf();
 			import_location.pop();
 			push_frame(
-				loc,
+				Some(loc),
 				|| format!("import {:?}", path),
 				|| with_state(|s| s.import_file(&import_location, path)),
 			)?
modifiedcrates/jrsonnet-evaluator/src/function.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function.rs
+++ b/crates/jrsonnet-evaluator/src/function.rs
@@ -1,15 +1,16 @@
 use crate::{
-	error::Error::*, evaluate, evaluate_named, gc::TraceBox, throw, Context, FutureWrapper,
-	GcHashMap, LazyVal, LazyValValue, Result, Val,
+	error::{Error::*, LocError},
+	evaluate, evaluate_named,
+	gc::TraceBox,
+	throw,
+	typed::Typed,
+	Context, FutureWrapper, GcHashMap, LazyVal, LazyValValue, Result, Val,
 };
 use gcmodule::Trace;
 use jrsonnet_interner::IStr;
-use jrsonnet_parser::{ArgsDesc, LocExpr, ParamsDesc};
-use std::collections::HashMap;
+use jrsonnet_parser::{ArgsDesc, ExprLocation, LocExpr, ParamsDesc};
+use std::{borrow::Cow, collections::HashMap, convert::TryFrom};
 
-const NO_DEFAULT_CONTEXT: &str =
-	"no default context set for call with defined default parameter value";
-
 #[derive(Trace)]
 struct EvaluateLazyVal {
 	context: Context,
@@ -21,6 +22,248 @@
 	}
 }
 
+pub trait ArgLike {
+	fn evaluate_arg(&self, ctx: Context, tailstrict: bool) -> Result<LazyVal>;
+}
+impl ArgLike for &LocExpr {
+	fn evaluate_arg(&self, ctx: Context, tailstrict: bool) -> Result<LazyVal> {
+		Ok(if tailstrict {
+			LazyVal::new_resolved(evaluate(ctx, self)?)
+		} else {
+			LazyVal::new(TraceBox(Box::new(EvaluateLazyVal {
+				context: ctx,
+				expr: (*self).clone(),
+			})))
+		})
+	}
+}
+impl<T> ArgLike for T
+where
+	T: Typed + Clone,
+	Val: TryFrom<T, Error = LocError>,
+{
+	fn evaluate_arg(&self, _ctx: Context, _tailstrict: bool) -> Result<LazyVal> {
+		let val: Val = Val::try_from(self.clone())?;
+		Ok(LazyVal::new_resolved(val))
+	}
+}
+pub enum TlaArg {
+	String(IStr),
+	Code(LocExpr),
+	Val(Val),
+}
+impl ArgLike for TlaArg {
+	fn evaluate_arg(&self, ctx: Context, tailstrict: bool) -> Result<LazyVal> {
+		match self {
+			TlaArg::String(s) => Ok(LazyVal::new_resolved(Val::Str(s.clone()))),
+			TlaArg::Code(code) => Ok(if tailstrict {
+				LazyVal::new_resolved(evaluate(ctx, code)?)
+			} else {
+				LazyVal::new(TraceBox(Box::new(EvaluateLazyVal {
+					context: ctx,
+					expr: code.clone(),
+				})))
+			}),
+			TlaArg::Val(val) => Ok(LazyVal::new_resolved(val.clone())),
+		}
+	}
+}
+
+pub trait ArgsLike {
+	fn unnamed_len(&self) -> usize;
+	fn unnamed_iter(
+		&self,
+		ctx: Context,
+		tailstrict: bool,
+		handler: &mut dyn FnMut(usize, LazyVal) -> Result<()>,
+	) -> Result<()>;
+	fn named_iter(
+		&self,
+		ctx: Context,
+		tailstrict: bool,
+		handler: &mut dyn FnMut(&IStr, LazyVal) -> Result<()>,
+	) -> Result<()>;
+	fn named_names(&self, handler: &mut dyn FnMut(&IStr));
+}
+
+impl ArgsLike for ArgsDesc {
+	fn unnamed_len(&self) -> usize {
+		self.unnamed.len()
+	}
+
+	fn unnamed_iter(
+		&self,
+		ctx: Context,
+		tailstrict: bool,
+		handler: &mut dyn FnMut(usize, LazyVal) -> Result<()>,
+	) -> Result<()> {
+		for (id, arg) in self.unnamed.iter().enumerate() {
+			handler(
+				id,
+				if tailstrict {
+					LazyVal::new_resolved(evaluate(ctx.clone(), arg)?)
+				} else {
+					LazyVal::new(TraceBox(Box::new(EvaluateLazyVal {
+						context: ctx.clone(),
+						expr: arg.clone(),
+					})))
+				},
+			)?;
+		}
+		Ok(())
+	}
+
+	fn named_iter(
+		&self,
+		ctx: Context,
+		tailstrict: bool,
+		handler: &mut dyn FnMut(&IStr, LazyVal) -> Result<()>,
+	) -> Result<()> {
+		for (name, arg) in self.named.iter() {
+			handler(
+				name,
+				if tailstrict {
+					LazyVal::new_resolved(evaluate(ctx.clone(), arg)?)
+				} else {
+					LazyVal::new(TraceBox(Box::new(EvaluateLazyVal {
+						context: ctx.clone(),
+						expr: arg.clone(),
+					})))
+				},
+			)?;
+		}
+		Ok(())
+	}
+
+	fn named_names(&self, handler: &mut dyn FnMut(&IStr)) {
+		for (name, _) in self.named.iter() {
+			handler(name)
+		}
+	}
+}
+
+impl<A: ArgLike> ArgsLike for [(IStr, A)] {
+	fn unnamed_len(&self) -> usize {
+		0
+	}
+
+	fn unnamed_iter(
+		&self,
+		_ctx: Context,
+		_tailstrict: bool,
+		_handler: &mut dyn FnMut(usize, LazyVal) -> Result<()>,
+	) -> Result<()> {
+		Ok(())
+	}
+
+	fn named_iter(
+		&self,
+		ctx: Context,
+		tailstrict: bool,
+		handler: &mut dyn FnMut(&IStr, LazyVal) -> Result<()>,
+	) -> Result<()> {
+		for (name, val) in self.iter() {
+			handler(name, val.evaluate_arg(ctx.clone(), tailstrict)?)?;
+		}
+		Ok(())
+	}
+
+	fn named_names(&self, handler: &mut dyn FnMut(&IStr)) {
+		for (name, _) in self.iter() {
+			handler(name);
+		}
+	}
+}
+
+impl<A: ArgLike> ArgsLike for HashMap<IStr, A> {
+	fn unnamed_len(&self) -> usize {
+		0
+	}
+
+	fn unnamed_iter(
+		&self,
+		_ctx: Context,
+		_tailstrict: bool,
+		_handler: &mut dyn FnMut(usize, LazyVal) -> Result<()>,
+	) -> Result<()> {
+		Ok(())
+	}
+
+	fn named_iter(
+		&self,
+		ctx: Context,
+		tailstrict: bool,
+		handler: &mut dyn FnMut(&IStr, LazyVal) -> Result<()>,
+	) -> Result<()> {
+		for (name, value) in self.iter() {
+			handler(name, value.evaluate_arg(ctx.clone(), tailstrict)?)?;
+		}
+		Ok(())
+	}
+
+	fn named_names(&self, handler: &mut dyn FnMut(&IStr)) {
+		for (name, _) in self.iter() {
+			handler(name);
+		}
+	}
+}
+
+impl<A: ArgLike> ArgsLike for [A] {
+	fn unnamed_len(&self) -> usize {
+		self.len()
+	}
+
+	fn unnamed_iter(
+		&self,
+		ctx: Context,
+		tailstrict: bool,
+		handler: &mut dyn FnMut(usize, LazyVal) -> Result<()>,
+	) -> Result<()> {
+		for (i, arg) in self.iter().enumerate() {
+			handler(i, arg.evaluate_arg(ctx.clone(), tailstrict)?)?;
+		}
+		Ok(())
+	}
+
+	fn named_iter(
+		&self,
+		_ctx: Context,
+		_tailstrict: bool,
+		_handler: &mut dyn FnMut(&IStr, LazyVal) -> Result<()>,
+	) -> Result<()> {
+		Ok(())
+	}
+
+	fn named_names(&self, _handler: &mut dyn FnMut(&IStr)) {}
+}
+impl<A: ArgLike> ArgsLike for &[A] {
+	fn unnamed_len(&self) -> usize {
+		(*self).unnamed_len()
+	}
+
+	fn unnamed_iter(
+		&self,
+		ctx: Context,
+		tailstrict: bool,
+		handler: &mut dyn FnMut(usize, LazyVal) -> Result<()>,
+	) -> Result<()> {
+		(*self).unnamed_iter(ctx, tailstrict, handler)
+	}
+
+	fn named_iter(
+		&self,
+		ctx: Context,
+		tailstrict: bool,
+		handler: &mut dyn FnMut(&IStr, LazyVal) -> Result<()>,
+	) -> Result<()> {
+		(*self).named_iter(ctx, tailstrict, handler)
+	}
+
+	fn named_names(&self, handler: &mut dyn FnMut(&IStr)) {
+		(*self).named_names(handler)
+	}
+}
+
 /// Creates correct [context](Context) for function body evaluation returning error on invalid call.
 ///
 /// ## Parameters
@@ -33,55 +276,34 @@
 	ctx: Context,
 	body_ctx: Context,
 	params: &ParamsDesc,
-	args: &ArgsDesc,
+	args: &dyn ArgsLike,
 	tailstrict: bool,
 ) -> Result<Context> {
 	let mut passed_args = GcHashMap::with_capacity(params.len());
-	if args.unnamed.len() > params.len() {
+	if args.unnamed_len() > params.len() {
 		throw!(TooManyArgsFunctionHas(params.len()))
 	}
 
 	let mut filled_args = 0;
 
-	for (id, arg) in args.unnamed.iter().enumerate() {
+	args.unnamed_iter(ctx.clone(), tailstrict, &mut |id, arg| {
 		let name = params[id].0.clone();
-		passed_args.insert(
-			name,
-			if tailstrict {
-				LazyVal::new_resolved(evaluate(ctx.clone(), arg)?)
-			} else {
-				LazyVal::new(TraceBox(Box::new(EvaluateLazyVal {
-					context: ctx.clone(),
-					expr: arg.clone(),
-				})))
-			},
-		);
+		passed_args.insert(name, arg);
 		filled_args += 1;
-	}
+		Ok(())
+	})?;
 
-	for (name, value) in args.named.iter() {
+	args.named_iter(ctx, tailstrict, &mut |name, value| {
 		// FIXME: O(n) for arg existence check
 		if !params.iter().any(|p| &p.0 == name) {
 			throw!(UnknownFunctionParameter((name as &str).to_owned()));
 		}
-		if passed_args
-			.insert(
-				name.clone(),
-				if tailstrict {
-					LazyVal::new_resolved(evaluate(ctx.clone(), value)?)
-				} else {
-					LazyVal::new(TraceBox(Box::new(EvaluateLazyVal {
-						context: ctx.clone(),
-						expr: value.clone(),
-					})))
-				},
-			)
-			.is_some()
-		{
+		if passed_args.insert(name.clone(), value).is_some() {
 			throw!(BindingParameterASecondTime(name.clone()));
 		}
 		filled_args += 1;
-	}
+		Ok(())
+	})?;
 
 	if filled_args < params.len() {
 		// Some args are unset, but maybe we have defaults for them
@@ -123,8 +345,14 @@
 
 		// Some args still wasn't filled
 		if filled_args != params.len() {
-			for param in params.iter().skip(args.unnamed.len()) {
-				if !args.named.iter().any(|a| a.0 == param.0) {
+			for param in params.iter().skip(args.unnamed_len()) {
+				let mut found = false;
+				args.named_names(&mut |name| {
+					if name == &param.0 {
+						found = true;
+					}
+				});
+				if !found {
 					throw!(FunctionParameterNotBoundInCall(param.0.clone()));
 				}
 			}
@@ -141,12 +369,33 @@
 	}
 }
 
-#[derive(Clone, Copy)]
+type BuiltinParamName = Cow<'static, str>;
+
+#[derive(Clone)]
 pub struct BuiltinParam {
-	pub name: &'static str,
+	pub name: BuiltinParamName,
 	pub has_default: bool,
 }
 
+pub trait Builtin: Trace {
+	fn name(&self) -> &str;
+	fn params(&self) -> &[BuiltinParam];
+	fn call(
+		&self,
+		context: Context,
+		loc: Option<&ExprLocation>,
+		args: &dyn ArgsLike,
+	) -> Result<Val>;
+}
+
+pub trait StaticBuiltin: Builtin + Send + Sync
+where
+	Self: 'static,
+{
+	// In impl, to make it object safe:
+	// const INST: &'static Self;
+}
+
 /// You shouldn't probally use this function, use jrsonnet_macros::builtin instead
 ///
 /// ## Parameters
@@ -154,58 +403,38 @@
 /// * `params`: function parameters' definition
 /// * `args`: passed function arguments
 /// * `tailstrict`: if set to `true` function arguments are eagerly executed, otherwise - lazily
-pub fn parse_builtin_call<'k>(
+pub fn parse_builtin_call(
 	ctx: Context,
-	params: &'static [BuiltinParam],
-	args: &'k ArgsDesc,
+	params: &[BuiltinParam],
+	args: &dyn ArgsLike,
 	tailstrict: bool,
-) -> Result<GcHashMap<&'k str, LazyVal>> {
+) -> Result<GcHashMap<BuiltinParamName, LazyVal>> {
 	let mut passed_args = GcHashMap::with_capacity(params.len());
-	if args.unnamed.len() > params.len() {
+	if args.unnamed_len() > params.len() {
 		throw!(TooManyArgsFunctionHas(params.len()))
 	}
 
 	let mut filled_args = 0;
 
-	for (id, arg) in args.unnamed.iter().enumerate() {
-		let name = params[id].name;
-		passed_args.insert(
-			name,
-			if tailstrict {
-				LazyVal::new_resolved(evaluate(ctx.clone(), arg)?)
-			} else {
-				LazyVal::new(TraceBox(Box::new(EvaluateLazyVal {
-					context: ctx.clone(),
-					expr: arg.clone(),
-				})))
-			},
-		);
+	args.unnamed_iter(ctx.clone(), tailstrict, &mut |id, arg| {
+		let name = params[id].name.clone();
+		passed_args.insert(name, arg);
 		filled_args += 1;
-	}
+		Ok(())
+	})?;
 
-	for (name, value) in args.named.iter() {
+	args.named_iter(ctx, tailstrict, &mut |name, arg| {
 		// FIXME: O(n) for arg existence check
-		if !params.iter().any(|p| p.name == name as &str) {
-			throw!(UnknownFunctionParameter((name as &str).to_owned()));
-		}
-		if passed_args
-			.insert(
-				name,
-				if tailstrict {
-					LazyVal::new_resolved(evaluate(ctx.clone(), value)?)
-				} else {
-					LazyVal::new(TraceBox(Box::new(EvaluateLazyVal {
-						context: ctx.clone(),
-						expr: value.clone(),
-					})))
-				},
-			)
-			.is_some()
-		{
+		let p = params
+			.iter()
+			.find(|p| p.name == name as &str)
+			.ok_or_else(|| UnknownFunctionParameter((name as &str).to_owned()))?;
+		if passed_args.insert(p.name.clone(), arg).is_some() {
 			throw!(BindingParameterASecondTime(name.clone()));
 		}
 		filled_args += 1;
-	}
+		Ok(())
+	})?;
 
 	if filled_args < params.len() {
 		for param in params.iter().filter(|p| p.has_default) {
@@ -217,97 +446,19 @@
 
 		// Some args still wasn't filled
 		if filled_args != params.len() {
-			for param in params.iter().skip(args.unnamed.len()) {
-				if !args.named.iter().any(|a| &a.0 as &str == param.name) {
-					throw!(FunctionParameterNotBoundInCall(param.name.into()));
+			for param in params.iter().skip(args.unnamed_len()) {
+				let mut found = false;
+				args.named_names(&mut |name| {
+					if name as &str == &param.name as &str {
+						found = true;
+					}
+				});
+				if !found {
+					throw!(FunctionParameterNotBoundInCall(param.name.clone().into()));
 				}
 			}
 			unreachable!();
 		}
 	}
 	Ok(passed_args)
-}
-
-pub fn parse_function_call_map(
-	ctx: Context,
-	body_ctx: Option<Context>,
-	params: &ParamsDesc,
-	args: &HashMap<IStr, Val>,
-	tailstrict: bool,
-) -> Result<Context> {
-	let mut out = GcHashMap::with_capacity(params.len());
-	let mut positioned_args = vec![None; params.0.len()];
-	for (name, val) in args.iter() {
-		let idx = params
-			.iter()
-			.position(|p| *p.0 == **name)
-			.ok_or_else(|| UnknownFunctionParameter((name as &str).to_owned()))?;
-
-		if idx >= params.len() {
-			throw!(TooManyArgsFunctionHas(params.len()));
-		}
-		if positioned_args[idx].is_some() {
-			throw!(BindingParameterASecondTime(params[idx].0.clone()));
-		}
-		positioned_args[idx] = Some(val.clone());
-	}
-	// Fill defaults
-	for (id, p) in params.iter().enumerate() {
-		let val = if let Some(arg) = positioned_args[id].take() {
-			LazyVal::new_resolved(arg)
-		} else if let Some(default) = &p.1 {
-			if tailstrict {
-				LazyVal::new_resolved(evaluate(
-					body_ctx.clone().expect(NO_DEFAULT_CONTEXT),
-					default,
-				)?)
-			} else {
-				let body_ctx = body_ctx.clone();
-				let default = default.clone();
-				#[derive(Trace)]
-				struct EvaluateLazyVal {
-					body_ctx: Option<Context>,
-					default: LocExpr,
-				}
-				impl LazyValValue for EvaluateLazyVal {
-					fn get(self: Box<Self>) -> Result<Val> {
-						evaluate(
-							self.body_ctx.clone().expect(NO_DEFAULT_CONTEXT),
-							&self.default,
-						)
-					}
-				}
-				LazyVal::new(TraceBox(Box::new(EvaluateLazyVal { body_ctx, default })))
-			}
-		} else {
-			throw!(FunctionParameterNotBoundInCall(p.0.clone()));
-		};
-		out.insert(p.0.clone(), val);
-	}
-
-	Ok(body_ctx.unwrap_or(ctx).extend(out, None, None, None))
-}
-
-pub fn place_args(body_ctx: Context, params: &ParamsDesc, args: &[Val]) -> Result<Context> {
-	let mut out = GcHashMap::with_capacity(params.len());
-	let mut positioned_args = vec![None; params.0.len()];
-	for (id, arg) in args.iter().enumerate() {
-		if id >= params.len() {
-			throw!(TooManyArgsFunctionHas(params.len()));
-		}
-		positioned_args[id] = Some(arg);
-	}
-	// Fill defaults
-	for (id, p) in params.iter().enumerate() {
-		let val = if let Some(arg) = &positioned_args[id] {
-			(*arg).clone()
-		} else if let Some(default) = &p.1 {
-			evaluate(body_ctx.clone(), default)?
-		} else {
-			throw!(FunctionParameterNotBoundInCall(p.0.clone()));
-		};
-		out.insert(p.0.clone(), LazyVal::new_resolved(val));
-	}
-
-	Ok(body_ctx.extend(out, None, None, None))
 }
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -25,6 +25,7 @@
 use error::{Error::*, LocError, Result, StackTraceElement};
 pub use evaluate::*;
 pub use function::parse_function_call;
+use function::TlaArg;
 use gc::{GcHashMap, TraceBox};
 use gcmodule::{Cc, Trace};
 pub use import::*;
@@ -77,7 +78,7 @@
 	/// Used for ext.native
 	pub ext_natives: HashMap<IStr, Cc<NativeCallback>>,
 	/// TLA vars
-	pub tla_vars: HashMap<IStr, Val>,
+	pub tla_vars: HashMap<IStr, TlaArg>,
 	/// Global variables are inserted in default context
 	pub globals: HashMap<IStr, Val>,
 	/// Used to resolve file locations/contents
@@ -174,7 +175,7 @@
 	EVAL_STATE.with(|s| f(s.borrow().as_ref().unwrap()))
 }
 pub(crate) fn push_frame<T>(
-	e: &ExprLocation,
+	e: Option<&ExprLocation>,
 	frame_desc: impl FnOnce() -> String,
 	f: impl FnOnce() -> Result<T>,
 ) -> Result<T> {
@@ -203,24 +204,21 @@
 
 impl EvaluationState {
 	/// Parses and adds file as loaded
-	pub fn add_file(&self, path: Rc<Path>, source_code: IStr) -> Result<()> {
-		self.add_parsed_file(
-			path.clone(),
-			source_code.clone(),
-			parse(
-				&source_code,
-				&ParserSettings {
-					file_name: path.clone(),
-				},
-			)
-			.map_err(|error| ImportSyntaxError {
-				error: Box::new(error),
-				path: path.to_owned(),
-				source_code,
-			})?,
-		)?;
+	pub fn add_file(&self, path: Rc<Path>, source_code: IStr) -> Result<LocExpr> {
+		let parsed = parse(
+			&source_code,
+			&ParserSettings {
+				file_name: path.clone(),
+			},
+		)
+		.map_err(|error| ImportSyntaxError {
+			error: Box::new(error),
+			path: path.to_owned(),
+			source_code: source_code.clone(),
+		})?;
+		self.add_parsed_file(path, source_code, parsed.clone())?;
 
-		Ok(())
+		Ok(parsed)
 	}
 
 	pub fn reset_evaluation_state(&self, name: &Path) {
@@ -341,7 +339,7 @@
 	/// Executes code creating a new stack frame
 	pub fn push<T>(
 		&self,
-		e: &ExprLocation,
+		e: Option<&ExprLocation>,
 		frame_desc: impl FnOnce() -> String,
 		f: impl FnOnce() -> Result<T>,
 	) -> Result<T> {
@@ -364,7 +362,7 @@
 		}
 		if let Err(mut err) = result {
 			err.trace_mut().0.push(StackTraceElement {
-				location: Some(e.clone()),
+				location: e.cloned(),
 				desc: frame_desc(),
 			});
 			return Err(err);
@@ -506,8 +504,9 @@
 				Val::Func(func) => push_description_frame(
 					|| "during TLA call".to_owned(),
 					|| {
-						func.evaluate_map(
+						func.evaluate(
 							self.create_default_context(),
+							None,
 							&self.settings().tla_vars,
 							true,
 						)
@@ -581,15 +580,20 @@
 	}
 
 	pub fn add_tla(&self, name: IStr, value: Val) {
-		self.settings_mut().tla_vars.insert(name, value);
+		self.settings_mut()
+			.tla_vars
+			.insert(name, TlaArg::Val(value));
 	}
 	pub fn add_tla_str(&self, name: IStr, value: IStr) {
-		self.add_tla(name, Val::Str(value));
+		self.settings_mut()
+			.tla_vars
+			.insert(name, TlaArg::String(value));
 	}
 	pub fn add_tla_code(&self, name: IStr, code: IStr) -> Result<()> {
-		let value =
-			self.evaluate_snippet_raw(PathBuf::from(format!("tla_code {}", name)).into(), code)?;
-		self.add_tla(name, value);
+		let parsed = self.add_file(PathBuf::from(format!("tla_code {}", name)).into(), code)?;
+		self.settings_mut()
+			.tla_vars
+			.insert(name, TlaArg::Code(parsed));
 		Ok(())
 	}
 
@@ -668,11 +672,11 @@
 		state.run_in_state(|| {
 			state
 				.push(
-					&ExprLocation(PathBuf::from("test1.jsonnet").into(), 10, 20),
+					Some(&ExprLocation(PathBuf::from("test1.jsonnet").into(), 10, 20)),
 					|| "outer".to_owned(),
 					|| {
 						state.push(
-							&ExprLocation(PathBuf::from("test2.jsonnet").into(), 30, 40),
+							Some(&ExprLocation(PathBuf::from("test2.jsonnet").into(), 30, 40)),
 							|| "inner".to_owned(),
 							|| Err(RuntimeError("".into()).into()),
 						)?;
@@ -975,15 +979,8 @@
 	fn parse_json() {
 		assert_json!(
 			r#"std.parseJson('{"a": -1,"b": 1,"c": 3.141,"d": []}')"#,
-			r#"{"a": -1,"b": 1,"c": 3.141,"d": []}"#
-		);
-		// TODO: this should in fact fail as is no proper JSON syntax
-		assert_json!(
-			r#"std.parseJson("{a:-1, b:1, c:3.141, d:[]}")"#,
 			r#"{"a": -1,"b": 1,"c": 3.141,"d": []}"#
 		);
-		// TODO: this is also no valid JSON
-		assert_json!(r#"std.parseJson('local x = 2; x * x')"#, r#"4"#);
 	}
 
 	#[test]
modifiedcrates/jrsonnet-evaluator/src/native.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/native.rs
+++ b/crates/jrsonnet-evaluator/src/native.rs
@@ -8,6 +8,7 @@
 use std::path::Path;
 use std::rc::Rc;
 
+#[deprecated(note = "Use builtins instead")]
 pub trait NativeCallbackHandler: Trace {
 	fn call(&self, from: Rc<Path>, args: &[Val]) -> Result<Val>;
 }
modifiedcrates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -37,7 +37,7 @@
 								.into()
 							))
 						}
-						Ok(n as $ty)
+						Ok(n as Self)
 					}
 					_ => unreachable!(),
 				}
@@ -249,6 +249,7 @@
 
 /// To be used in Vec<Any>
 /// Regular Val can't be used here, because it has wrong TryFrom::Error type
+#[derive(Clone)]
 pub struct Any(pub Val);
 
 impl Typed for Any {
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -1,24 +1,20 @@
 use crate::{
-	builtin::{
-		call_builtin,
-		manifest::{
-			manifest_json_ex, manifest_yaml_ex, ManifestJsonOptions, ManifestType,
-			ManifestYamlOptions,
-		},
+	builtin::manifest::{
+		manifest_json_ex, manifest_yaml_ex, ManifestJsonOptions, ManifestType, ManifestYamlOptions,
 	},
 	cc_ptr_eq,
 	error::{Error::*, LocError},
 	evaluate,
-	function::{parse_function_call, parse_function_call_map, place_args},
+	function::{parse_function_call, ArgsLike, Builtin, StaticBuiltin},
 	gc::TraceBox,
 	native::NativeCallback,
 	throw, Context, ObjValue, Result,
 };
 use gcmodule::{Cc, Trace};
 use jrsonnet_interner::IStr;
-use jrsonnet_parser::{ArgsDesc, ExprLocation, LocExpr, ParamsDesc};
+use jrsonnet_parser::{ExprLocation, LocExpr, ParamsDesc};
 use jrsonnet_types::ValType;
-use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};
+use std::{cell::RefCell, fmt::Debug, rc::Rc};
 
 pub trait LazyValValue: Trace {
 	fn get(self: Box<Self>) -> Result<Val>;
@@ -41,6 +37,10 @@
 	pub fn new_resolved(val: Val) -> Self {
 		Self(Cc::new(RefCell::new(LazyValInternals::Computed(val))))
 	}
+	pub fn force(&self) -> Result<()> {
+		self.evaluate()?;
+		Ok(())
+	}
 	pub fn evaluate(&self) -> Result<Val> {
 		match &*self.0.borrow() {
 			LazyValInternals::Computed(v) => return Ok(v.clone()),
@@ -86,42 +86,63 @@
 	pub body: LocExpr,
 }
 
-#[derive(Debug, Trace)]
+#[derive(Trace)]
 pub enum FuncVal {
 	/// Plain function implemented in jsonnet
 	Normal(FuncDesc),
 	/// Standard library function
-	Intrinsic(IStr),
+	StaticBuiltin(#[skip_trace] &'static dyn StaticBuiltin),
+
+	Builtin(TraceBox<dyn Builtin>),
 	/// Library functions implemented in native
 	NativeExt(IStr, Cc<NativeCallback>),
 }
 
+impl Debug for FuncVal {
+	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::NativeExt(arg0, arg1) => {
+				f.debug_tuple("NativeExt").field(arg0).field(arg1).finish()
+			}
+		}
+	}
+}
+
 impl PartialEq for FuncVal {
 	fn eq(&self, other: &Self) -> bool {
 		match (self, other) {
 			(Self::Normal(a), Self::Normal(b)) => a == b,
-			(Self::Intrinsic(an), Self::Intrinsic(bn)) => an == bn,
+			(Self::StaticBuiltin(an), Self::StaticBuiltin(bn)) => std::ptr::eq(*an, *bn),
 			(Self::NativeExt(an, _), Self::NativeExt(bn, _)) => an == bn,
 			(..) => false,
 		}
 	}
 }
 impl FuncVal {
-	pub fn is_ident(&self) -> bool {
-		matches!(&self, Self::Intrinsic(n) if n as &str == "id")
+	pub fn args_len(&self) -> usize {
+		match self {
+			Self::Normal(n) => n.params.iter().filter(|p| p.1.is_none()).count(),
+			Self::StaticBuiltin(i) => i.params().iter().filter(|p| !p.has_default).count(),
+			Self::Builtin(i) => i.params().iter().filter(|p| !p.has_default).count(),
+			Self::NativeExt(_, n) => n.params.iter().filter(|p| p.1.is_none()).count(),
+		}
 	}
 	pub fn name(&self) -> IStr {
 		match self {
 			Self::Normal(normal) => normal.name.clone(),
-			Self::Intrinsic(name) => format!("std.{}", name).into(),
+			Self::StaticBuiltin(builtin) => builtin.name().into(),
+			Self::Builtin(builtin) => builtin.name().into(),
 			Self::NativeExt(n, _) => format!("native.{}", n).into(),
 		}
 	}
 	pub fn evaluate(
 		&self,
 		call_ctx: Context,
-		loc: &ExprLocation,
-		args: &ArgsDesc,
+		loc: Option<&ExprLocation>,
+		args: &dyn ArgsLike,
 		tailstrict: bool,
 	) -> Result<Val> {
 		match self {
@@ -135,7 +156,8 @@
 				)?;
 				evaluate(ctx, &func.body)
 			}
-			Self::Intrinsic(name) => call_builtin(call_ctx, loc, name, args),
+			Self::StaticBuiltin(name) => name.call(call_ctx, loc, args),
+			Self::Builtin(b) => b.call(call_ctx, loc, args),
 			Self::NativeExt(_name, handler) => {
 				let args =
 					parse_function_call(call_ctx, Context::new(), &handler.params, args, true)?;
@@ -143,42 +165,12 @@
 				for p in handler.params.0.iter() {
 					out_args.push(args.binding(p.0.clone())?.evaluate()?);
 				}
-				Ok(handler.call(loc.0.clone(), &out_args)?)
-			}
-		}
-	}
-
-	pub fn evaluate_map(
-		&self,
-		call_ctx: Context,
-		args: &HashMap<IStr, Val>,
-		tailstrict: bool,
-	) -> Result<Val> {
-		match self {
-			Self::Normal(func) => {
-				let ctx = parse_function_call_map(
-					call_ctx,
-					Some(func.ctx.clone()),
-					&func.params,
-					args,
-					tailstrict,
-				)?;
-				evaluate(ctx, &func.body)
+				Ok(handler.call(loc.expect("todo").0.clone(), &out_args)?)
 			}
-			Self::Intrinsic(_) => todo!(),
-			Self::NativeExt(_, _) => todo!(),
 		}
 	}
-
-	pub fn evaluate_values(&self, args: &[Val]) -> Result<Val> {
-		match self {
-			Self::Normal(func) => {
-				let ctx = place_args(func.ctx.clone(), &func.params, args)?;
-				evaluate(ctx, &func.body)
-			}
-			Self::Intrinsic(_) => todo!(),
-			Self::NativeExt(_, _) => todo!(),
-		}
+	pub fn evaluate_simple(&self, args: &dyn ArgsLike) -> Result<Val> {
+		self.evaluate(Context::default(), None, args, true)
 	}
 }
 
modifiedcrates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-interner/src/lib.rs
+++ b/crates/jrsonnet-interner/src/lib.rs
@@ -2,6 +2,7 @@
 use rustc_hash::FxHashMap;
 use serde::{Deserialize, Serialize};
 use std::{
+	borrow::Cow,
 	cell::RefCell,
 	fmt::{self, Display},
 	hash::{BuildHasherDefault, Hash, Hasher},
@@ -90,6 +91,12 @@
 	}
 }
 
+impl<'i> From<Cow<'i, str>> for IStr {
+	fn from(c: Cow<'i, str>) -> Self {
+		(&c as &str).into()
+	}
+}
+
 impl Serialize for IStr {
 	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
 	where
modifiedcrates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -1,11 +1,50 @@
-use proc_macro2::Span;
 use quote::quote;
-use syn::{parse_macro_input, FnArg, Ident, ItemFn, Pat, PatType};
+use syn::{
+	parse_macro_input, FnArg, GenericArgument, ItemFn, Pat, PatType, Path, PathArguments, Type,
+};
 
 fn is_location_arg(t: &PatType) -> bool {
 	t.attrs.iter().any(|a| a.path.is_ident("location"))
 }
 
+trait RetainHad<T> {
+	fn retain_had(&mut self, h: impl FnMut(&T) -> bool) -> bool;
+}
+impl<T> RetainHad<T> for Vec<T> {
+	fn retain_had(&mut self, h: impl FnMut(&T) -> bool) -> bool {
+		let before = self.len();
+		self.retain(h);
+		let after = self.len();
+		before != after
+	}
+}
+
+fn extract_type_from_option(ty: &Type) -> Option<&Type> {
+	fn path_is_option(path: &Path) -> bool {
+		path.leading_colon.is_none()
+			&& path.segments.len() == 1
+			&& path.segments.iter().next().unwrap().ident == "Option"
+	}
+
+	match ty {
+		Type::Path(typepath) if typepath.qself.is_none() && path_is_option(&typepath.path) => {
+			// Get the first segment of the path (there is only one, in fact: "Option"):
+			let type_params = &typepath.path.segments.iter().next().unwrap().arguments;
+			// It should have only on angle-bracketed param ("<String>"):
+			let generic_arg = match type_params {
+				PathArguments::AngleBracketed(params) => params.args.iter().next().unwrap(),
+				_ => panic!("missing option generic"),
+			};
+			// This argument must be a type:
+			match generic_arg {
+				GenericArgument::Type(ty) => Some(ty),
+				_ => panic!("option generic should be a type"),
+			}
+		}
+		_ => None,
+	}
+}
+
 #[proc_macro_attribute]
 pub fn builtin(
 	_attr: proc_macro::TokenStream,
@@ -33,11 +72,10 @@
 				Pat::Ident(i) => i.ident.to_string(),
 				_ => panic!("only idents supported yet"),
 			};
-			// TODO: Check if ty == Option<_>
-			let optional = false;
+			let optional = extract_type_from_option(&t.ty).is_some();
 			quote! {
 				BuiltinParam {
-					name: #ident,
+					name: std::borrow::Cow::Borrowed(#ident),
 					has_default: #optional,
 				}
 			}
@@ -53,10 +91,7 @@
 			FnArg::Typed(t) => t,
 		})
 		.map(|t| {
-			let count_before = t.attrs.len();
-			t.attrs.retain(|a| !a.path.is_ident("location"));
-			let count_after = t.attrs.len();
-			let is_location = count_before != count_after;
+			let is_location = t.attrs.retain_had(|a| !a.path.is_ident("location"));
 			if is_location {
 				quote! {{
 					loc
@@ -67,38 +102,68 @@
 					_ => panic!("only idents supported yet"),
 				};
 				let ty = &t.ty;
-				quote! {{
-					let value = parsed.get(#ident).unwrap();
+				if let Some(opt_ty) = extract_type_from_option(&t.ty) {
+					quote! {{
+						if let Some(value) = parsed.get(#ident) {
+							Some(jrsonnet_evaluator::push_description_frame(
+								|| format!("argument <{}> evaluation", #ident),
+								|| <#opt_ty>::try_from(value.evaluate()?),
+							)?)
+						} else {
+							None
+						}
+					}}
+				} else {
+					quote! {{
+						let value = parsed.get(#ident).unwrap();
 
-					jrsonnet_evaluator::push_description_frame(
-						|| format!("argument <{}> evaluation", #ident),
-						|| <#ty>::try_from(value.evaluate()?),
-					)?
-				}}
+						jrsonnet_evaluator::push_description_frame(
+							|| format!("argument <{}> evaluation", #ident),
+							|| <#ty>::try_from(value.evaluate()?),
+						)?
+					}}
+				}
 			}
-		}).collect::<Vec<_>>();
-	
-	let inner_name = Ident::new("inner", Span::call_site());
-	let mut inner_fun = fun.clone();
-	inner_fun.sig.ident = inner_name.clone();
+		})
+		.collect::<Vec<_>>();
 
-	let attrs = &fun.attrs;
+	let name = &fun.sig.ident;
 	let vis = &fun.vis;
-	let name = &fun.sig.ident;
 	(quote! {
-		#(#attrs)*
-		#vis fn #name(context: Context, loc: &ExprLocation, args: &ArgsDesc) -> Result<Val> {
-			#inner_fun
-			use jrsonnet_evaluator::function::BuiltinParam;
+		#fun
+		#[doc(hidden)]
+		#[allow(non_camel_case_types)]
+		#[derive(Clone, Copy, gcmodule::Trace)]
+		#vis struct #name {}
+		const _: () = {
+			use jrsonnet_evaluator::function::{Builtin, StaticBuiltin, BuiltinParam, ArgsLike};
 			const PARAMS: &'static [BuiltinParam] = &[
 				#(#params),*
 			];
-			let parsed = jrsonnet_evaluator::function::parse_builtin_call(context, &PARAMS, args, false)?;
 
-			let result: #result = #inner_name(#(#args),*);
-			let result = result?;
-			result.try_into()
-		}
+			impl #name {
+				pub const INST: &'static dyn StaticBuiltin = &#name {};
+			}
+			impl StaticBuiltin for #name {}
+			impl Builtin for #name
+			where
+				Self: 'static
+			{
+				fn name(&self) -> &str {
+					stringify!(#name)
+				}
+				fn params(&self) -> &[BuiltinParam] {
+					PARAMS
+				}
+				fn call(&self, context: Context, loc: Option<&ExprLocation>, args: &dyn ArgsLike) -> Result<Val> {
+					let parsed = jrsonnet_evaluator::function::parse_builtin_call(context, &PARAMS, args, false)?;
+
+					let result: #result = #name(#(#args),*);
+					let result = result?;
+					result.try_into()
+				}
+			}
+		};
 	})
 	.into()
 }
modifiedcrates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -40,7 +40,7 @@
 			/ "#" (!eol()[_])* eol()
 
 		rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")
-		rule _() = single_whitespace()*
+		rule _() = quiet!{([' ' | '\r' | '\n' | '\t']+) / comment()}* / expected!("<whitespace>")
 
 		/// For comma-delimited elements
 		rule comma() = quiet!{_ "," _} / expected!("<comma>")
@@ -305,6 +305,14 @@
 pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {
 	jsonnet_parser::jsonnet(str, settings)
 }
+/// Used for importstr values
+pub fn string_to_expr(str: IStr, settings: &ParserSettings) -> LocExpr {
+	let len = str.len();
+	LocExpr(
+		Rc::new(Expr::Str(str)),
+		ExprLocation(settings.file_name.clone(), 0, len),
+	)
+}
 
 #[cfg(test)]
 pub mod tests {
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -375,9 +375,7 @@
 
   manifestJsonEx:: $intrinsic(manifestJsonEx),
 
-  manifestYamlDocImpl:: $intrinsic(manifestYamlDocImpl),
-
-  manifestYamlDoc(value, indent_array_in_object=false, quote_keys=true):: std.manifestYamlDocImpl(value, indent_array_in_object, quote_keys),
+  manifestYamlDoc:: $intrinsic(manifestYamlDoc),
 
   manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::
     if !std.isArray(value) then
@@ -443,10 +441,7 @@
 
   reverse:: $intrinsic(reverse),
 
-  sortImpl:: $intrinsic(sortImpl),
-
-  sort(arr, keyF=id)::
-    std.sortImpl(arr, keyF),
+  sort:: $intrinsic(sort),
 
   uniq(arr, keyF=id)::
     local f(a, b) =