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
--- a/crates/jrsonnet-evaluator/src/builtin/format.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/format.rs
@@ -577,10 +577,8 @@
 			}
 		}
 		ConvTypeV::Char => match value.clone() {
-			Val::Num(n) => tmp_out.push(
-				std::char::from_u32(n as u32)
-					.ok_or_else(|| InvalidUnicodeCodepointGot(n as u32))?,
-			),
+			Val::Num(n) => tmp_out
+				.push(std::char::from_u32(n as u32).ok_or(InvalidUnicodeCodepointGot(n as u32))?),
 			Val::Str(s) => {
 				if s.chars().count() != 1 {
 					throw!(RuntimeError(
modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/builtin/mod.rs
1use crate::typed::{Any, Either, Null, PositiveF64, VecVal, M1};2use crate::{self as jrsonnet_evaluator, ObjValue};3use crate::{4	builtin::manifest::{manifest_yaml_ex, ManifestYamlOptions},5	equals,6	error::{Error::*, Result},7	operator::evaluate_mod_op,8	primitive_equals, push_frame, throw, with_state, ArrValue, Context, FuncVal,9	IndexableVal, Val,10};11use format::{format_arr, format_obj};12use gcmodule::Cc;13use jrsonnet_interner::IStr;14use jrsonnet_parser::{ArgsDesc, ExprLocation};15use serde::Deserialize;16use serde_yaml::DeserializingQuirks;17use std::{18	collections::HashMap,19	convert::{TryFrom, TryInto},20	path::PathBuf,21	rc::Rc,22};2324pub mod stdlib;25pub use stdlib::*;2627use self::manifest::{escape_string_json, manifest_json_ex, ManifestJsonOptions, ManifestType};2829pub mod format;30pub mod manifest;31pub mod sort;3233pub fn std_format(str: IStr, vals: Val) -> Result<String> {34	push_frame(35		&ExprLocation(Rc::from(PathBuf::from("std.jsonnet")), 0, 0),36		|| format!("std.format of {}", str),37		|| {38			Ok(match vals {39				Val::Arr(vals) => format_arr(&str, &vals.evaluated()?)?,40				Val::Obj(obj) => format_obj(&str, &obj)?,41				o => format_arr(&str, &[o])?,42			})43		},44	)45}4647pub fn std_slice(48	indexable: IndexableVal,49	index: Option<usize>,50	end: Option<usize>,51	step: Option<usize>,52) -> Result<Val> {53	let index = index.unwrap_or(0);54	let end = end.unwrap_or_else(|| match &indexable {55		IndexableVal::Str(_) => usize::MAX,56		IndexableVal::Arr(v) => v.len(),57	});58	let step = step.unwrap_or(1);59	match &indexable {60		IndexableVal::Str(s) => Ok(Val::Str(61			(s.chars()62				.skip(index)63				.take(end - index)64				.step_by(step)65				.collect::<String>())66			.into(),67		)),68		IndexableVal::Arr(arr) => Ok(Val::Arr(69			(arr.iter()70				.skip(index)71				.take(end - index)72				.step_by(step)73				.collect::<Result<Vec<Val>>>()?)74			.into(),75		)),76	}77}7879type Builtin = fn(context: Context, loc: &ExprLocation, args: &ArgsDesc) -> Result<Val>;8081type BuiltinsType = HashMap<Box<str>, Builtin>;8283thread_local! {84	static BUILTINS: BuiltinsType = {85		[86			("length".into(), builtin_length as Builtin),87			("type".into(), builtin_type),88			("makeArray".into(), builtin_make_array),89			("codepoint".into(), builtin_codepoint),90			("objectFieldsEx".into(), builtin_object_fields_ex),91			("objectHasEx".into(), builtin_object_has_ex),92			("slice".into(), builtin_slice),93			("substr".into(), builtin_substr),94			("primitiveEquals".into(), builtin_primitive_equals),95			("equals".into(), builtin_equals),96			("modulo".into(), builtin_modulo),97			("mod".into(), builtin_mod),98			("floor".into(), builtin_floor),99			("ceil".into(), builtin_ceil),100			("log".into(), builtin_log),101			("pow".into(), builtin_pow),102			("sqrt".into(), builtin_sqrt),103			("sin".into(), builtin_sin),104			("cos".into(), builtin_cos),105			("tan".into(), builtin_tan),106			("asin".into(), builtin_asin),107			("acos".into(), builtin_acos),108			("atan".into(), builtin_atan),109			("exp".into(), builtin_exp),110			("mantissa".into(), builtin_mantissa),111			("exponent".into(), builtin_exponent),112			("extVar".into(), builtin_ext_var),113			("native".into(), builtin_native),114			("filter".into(), builtin_filter),115			("map".into(), builtin_map),116			("flatMap".into(), builtin_flatmap),117			("foldl".into(), builtin_foldl),118			("foldr".into(), builtin_foldr),119			("sortImpl".into(), builtin_sort_impl),120			("format".into(), builtin_format),121			("range".into(), builtin_range),122			("char".into(), builtin_char),123			("encodeUTF8".into(), builtin_encode_utf8),124			("decodeUTF8".into(), builtin_decode_utf8),125			("md5".into(), builtin_md5),126			("base64".into(), builtin_base64),127			("base64DecodeBytes".into(), builtin_base64_decode_bytes),128			("base64Decode".into(), builtin_base64_decode),129			("trace".into(), builtin_trace),130			("join".into(), builtin_join),131			("escapeStringJson".into(), builtin_escape_string_json),132			("manifestJsonEx".into(), builtin_manifest_json_ex),133			("manifestYamlDocImpl".into(), builtin_manifest_yaml_doc),134			("reverse".into(), builtin_reverse),135			("id".into(), builtin_id),136			("strReplace".into(), builtin_str_replace),137			("splitLimit".into(), builtin_splitlimit),138			("parseJson".into(), builtin_parse_json),139			("parseYaml".into(), builtin_parse_yaml),140			("asciiUpper".into(), builtin_ascii_upper),141			("asciiLower".into(), builtin_ascii_lower),142			("member".into(), builtin_member),143			("count".into(), builtin_count),144		].iter().cloned().collect()145	};146}147148#[jrsonnet_macros::builtin]149fn builtin_length(x: Either<IStr, Either<VecVal, ObjValue>>) -> Result<usize> {150	Ok(match x {151		Either::Left(x) => x.len(),152		Either::Right(Either::Left(x)) => x.0.len(),153		Either::Right(Either::Right(x)) => x154			.fields_visibility()155			.into_iter()156			.filter(|(_k, v)| *v)157			.count(),158	})159}160161#[jrsonnet_macros::builtin]162fn builtin_type(x: Any) -> Result<IStr> {163	Ok(x.0.value_type().name().into())164}165166#[jrsonnet_macros::builtin]167fn builtin_make_array(sz: usize, func: Cc<FuncVal>) -> Result<VecVal> {168	let mut out = Vec::with_capacity(sz);169	for i in 0..sz {170		out.push(func.evaluate_values(&[Val::Num(i as f64)])?)171	}172	Ok(VecVal(out))173}174175#[jrsonnet_macros::builtin]176const fn builtin_codepoint(str: char) -> Result<u32> {177	Ok(str as u32)178}179180#[jrsonnet_macros::builtin]181fn builtin_object_fields_ex(obj: ObjValue, inc_hidden: bool) -> Result<VecVal> {182	let out = obj.fields_ex(inc_hidden);183	Ok(VecVal(out.into_iter().map(Val::Str).collect::<Vec<_>>()))184}185186#[jrsonnet_macros::builtin]187fn builtin_object_has_ex(obj: ObjValue, f: IStr, inc_hidden: bool) -> Result<bool> {188	Ok(obj.has_field_ex(f, inc_hidden))189}190191#[jrsonnet_macros::builtin]192fn builtin_parse_json(s: IStr) -> Result<Any> {193	let value: serde_json::Value = serde_json::from_str(&s)194		.map_err(|e| RuntimeError(format!("failed to parse json: {}", e).into()))?;195	Ok(Any(Val::try_from(&value)?))196}197198#[jrsonnet_macros::builtin]199fn builtin_parse_yaml(s: IStr) -> Result<Any> {200	let value = serde_yaml::Deserializer::from_str_with_quirks(201		&s,202		DeserializingQuirks { old_octals: true },203	);204	let mut out = vec![];205	for item in value {206		let value = serde_json::Value::deserialize(item)207			.map_err(|e| RuntimeError(format!("failed to parse yaml: {}", e).into()))?;208		let val = Val::try_from(&value)?;209		out.push(val);210	}211	Ok(Any(if out.is_empty() {212		Val::Null213	} else if out.len() == 1 {214		out.into_iter().next().unwrap()215	} else {216		Val::Arr(out.into())217	}))218}219220#[jrsonnet_macros::builtin]221fn builtin_slice(222	indexable: IndexableVal,223	index: Either<usize, Null>,224	end: Either<usize, Null>,225	step: Either<usize, Null>,226) -> Result<Any> {227	std_slice(indexable, index.left(), end.left(), step.left()).map(Any)228}229230#[jrsonnet_macros::builtin]231fn builtin_substr(str: IStr, from: usize, len: usize) -> Result<String> {232	Ok(str.chars().skip(from as usize).take(len as usize).collect())233}234235#[jrsonnet_macros::builtin]236fn builtin_primitive_equals(a: Any, b: Any) -> Result<bool> {237	primitive_equals(&a.0, &b.0)238}239240#[jrsonnet_macros::builtin]241fn builtin_equals(a: Any, b: Any) -> Result<bool> {242	equals(&a.0, &b.0)243}244245#[jrsonnet_macros::builtin]246fn builtin_modulo(a: f64, b: f64) -> Result<f64> {247	Ok(a % b)248}249250#[jrsonnet_macros::builtin]251fn builtin_mod(a: Either<f64, IStr>, b: Any) -> Result<Any> {252	Ok(Any(evaluate_mod_op(253		&match a {254			Either::Left(v) => Val::Num(v),255			Either::Right(s) => Val::Str(s),256		},257		&b.0,258	)?))259}260261#[jrsonnet_macros::builtin]262fn builtin_floor(x: f64) -> Result<f64> {263	Ok(x.floor())264}265266#[jrsonnet_macros::builtin]267fn builtin_ceil(x: f64) -> Result<f64> {268	Ok(x.ceil())269}270271#[jrsonnet_macros::builtin]272fn builtin_log(n: f64) -> Result<f64> {273	Ok(n.ln())274}275276#[jrsonnet_macros::builtin]277fn builtin_pow(x: f64, n: f64) -> Result<f64> {278	Ok(x.powf(n))279}280281#[jrsonnet_macros::builtin]282fn builtin_sqrt(x: PositiveF64) -> Result<f64> {283	Ok(x.0.sqrt())284}285286#[jrsonnet_macros::builtin]287fn builtin_sin(x: f64) -> Result<f64> {288	Ok(x.sin())289}290291#[jrsonnet_macros::builtin]292fn builtin_cos(x: f64) -> Result<f64> {293	Ok(x.cos())294}295296#[jrsonnet_macros::builtin]297fn builtin_tan(x: f64) -> Result<f64> {298	Ok(x.tan())299}300301#[jrsonnet_macros::builtin]302fn builtin_asin(x: f64) -> Result<f64> {303	Ok(x.asin())304}305306#[jrsonnet_macros::builtin]307fn builtin_acos(x: f64) -> Result<f64> {308	Ok(x.acos())309}310311#[jrsonnet_macros::builtin]312fn builtin_atan(x: f64) -> Result<f64> {313	Ok(x.atan())314}315316#[jrsonnet_macros::builtin]317fn builtin_exp(x: f64) -> Result<f64> {318	Ok(x.exp())319}320321fn frexp(s: f64) -> (f64, i16) {322	if 0.0 == s {323		(s, 0)324	} else {325		let lg = s.abs().log2();326		let x = (lg - lg.floor() - 1.0).exp2();327		let exp = lg.floor() + 1.0;328		(s.signum() * x, exp as i16)329	}330}331332#[jrsonnet_macros::builtin]333fn builtin_mantissa(x: f64) -> Result<f64> {334	Ok(frexp(x).0)335}336337#[jrsonnet_macros::builtin]338fn builtin_exponent(x: f64) -> Result<i16> {339	Ok(frexp(x).1)340}341342#[jrsonnet_macros::builtin]343fn builtin_ext_var(x: IStr) -> Result<Any> {344	Ok(Any(with_state(|s| s.settings().ext_vars.get(&x).cloned())345		.ok_or(UndefinedExternalVariable(x))?))346}347348#[jrsonnet_macros::builtin]349fn builtin_native(name: IStr) -> Result<Cc<FuncVal>> {350	Ok(with_state(|s| s.settings().ext_natives.get(&name).cloned())351		.map(|v| Cc::new(FuncVal::NativeExt(name.clone(), v)))352		.ok_or(UndefinedExternalFunction(name))?)353}354355#[jrsonnet_macros::builtin]356fn builtin_filter(func: Cc<FuncVal>, arr: ArrValue) -> Result<ArrValue> {357	arr.filter(|val| bool::try_from(func.evaluate_values(&[val.clone()])?))358}359360#[jrsonnet_macros::builtin]361fn builtin_map(func: Cc<FuncVal>, arr: ArrValue) -> Result<ArrValue> {362	arr.map(|val| func.evaluate_values(&[val]))363}364365#[jrsonnet_macros::builtin]366fn builtin_flatmap(func: Cc<FuncVal>, arr: IndexableVal) -> Result<IndexableVal> {367	match arr {368		IndexableVal::Str(s) => {369			let mut out = String::new();370			for c in s.chars() {371				match func.evaluate_values(&[Val::Str(c.to_string().into())])? {372					Val::Str(o) => out.push_str(&o),373					_ => throw!(RuntimeError(374						"in std.join all items should be strings".into()375					)),376				};377			}378			Ok(IndexableVal::Str(out.into()))379		}380		IndexableVal::Arr(a) => {381			let mut out = Vec::new();382			for el in a.iter() {383				let el = el?;384				match func.evaluate_values(&[el])? {385					Val::Arr(o) => {386						for oe in o.iter() {387							out.push(oe?)388						}389					}390					_ => throw!(RuntimeError(391						"in std.join all items should be arrays".into()392					)),393				};394			}395			Ok(IndexableVal::Arr(out.into()))396		}397	}398}399400#[jrsonnet_macros::builtin]401fn builtin_foldl(func: Cc<FuncVal>, arr: ArrValue, init: Any) -> Result<Any> {402	let mut acc = init.0;403	for i in arr.iter() {404		acc = func.evaluate_values(&[acc, i?])?;405	}406	Ok(Any(acc))407}408409#[jrsonnet_macros::builtin]410fn builtin_foldr(func: Cc<FuncVal>, arr: ArrValue, init: Any) -> Result<Any> {411	let mut acc = init.0;412	for i in arr.iter().rev() {413		acc = func.evaluate_values(&[i?, acc])?;414	}415	Ok(Any(acc))416}417418#[jrsonnet_macros::builtin]419#[allow(non_snake_case)]420fn builtin_sort_impl(arr: ArrValue, keyF: Cc<FuncVal>) -> Result<ArrValue> {421	if arr.len() <= 1 {422		return Ok(arr);423	}424	Ok(ArrValue::Eager(sort::sort(arr.evaluated()?, &keyF)?))425}426427#[jrsonnet_macros::builtin]428fn builtin_format(str: IStr, vals: Any) -> Result<String> {429	std_format(str, vals.0)430}431432#[jrsonnet_macros::builtin]433fn builtin_range(from: i32, to: i32) -> Result<VecVal> {434	if to < from {435		return Ok(VecVal(Vec::new()));436	}437	let mut out = Vec::with_capacity((1 + to as usize - from as usize).max(0));438	for i in from as usize..=to as usize {439		out.push(Val::Num(i as f64));440	}441	Ok(VecVal(out))442}443444#[jrsonnet_macros::builtin]445fn builtin_char(n: u32) -> Result<char> {446	Ok(std::char::from_u32(n as u32).ok_or_else(|| InvalidUnicodeCodepointGot(n as u32))?)447}448449#[jrsonnet_macros::builtin]450fn builtin_encode_utf8(str: IStr) -> Result<VecVal> {451	Ok(VecVal(452		str.bytes()453			.map(|b| Val::Num(b as f64))454			.collect::<Vec<Val>>(),455	))456}457458#[jrsonnet_macros::builtin]459fn builtin_decode_utf8(arr: Vec<u8>) -> Result<String> {460	Ok(String::from_utf8(arr).map_err(|_| RuntimeError("bad utf8".into()))?)461}462463#[jrsonnet_macros::builtin]464fn builtin_md5(str: IStr) -> Result<String> {465	Ok(format!("{:x}", md5::compute(&str.as_bytes())))466}467468#[jrsonnet_macros::builtin]469fn builtin_trace(#[location] loc: &ExprLocation, str: IStr, rest: Any) -> Result<Any> {470	eprint!("TRACE:");471	with_state(|s| {472		let locs = s.map_source_locations(&loc.0, &[loc.1]);473		eprint!(474			" {}:{}",475			loc.0.file_name().unwrap().to_str().unwrap(),476			locs[0].line477		);478	});479	eprintln!(" {}", str);480	Ok(rest) as Result<Any>481}482483#[jrsonnet_macros::builtin]484fn builtin_base64(input: Either<Vec<u8>, IStr>) -> Result<String> {485	Ok(match input {486		Either::Left(a) => base64::encode(a),487		Either::Right(l) => base64::encode(l.bytes().collect::<Vec<_>>()),488	})489}490491#[jrsonnet_macros::builtin]492fn builtin_base64_decode_bytes(input: IStr) -> Result<Vec<u8>> {493	Ok(base64::decode(&input.as_bytes()).map_err(|_| RuntimeError("bad base64".into()))?)494}495496#[jrsonnet_macros::builtin]497fn builtin_base64_decode(input: IStr) -> Result<String> {498	let bytes = base64::decode(&input.as_bytes()).map_err(|_| RuntimeError("bad base64".into()))?;499	Ok(String::from_utf8(bytes).map_err(|_| RuntimeError("bad utf8".into()))?)500}501502#[jrsonnet_macros::builtin]503fn builtin_join(sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {504	Ok(match sep {505		IndexableVal::Arr(joiner_items) => {506			let mut out = Vec::new();507508			let mut first = true;509			for item in arr.iter() {510				let item = item?.clone();511				if let Val::Arr(items) = item {512					if !first {513						out.reserve(joiner_items.len());514						// TODO: extend515						for item in joiner_items.iter() {516							out.push(item?);517						}518					}519					first = false;520					out.reserve(items.len());521					// TODO: extend522					for item in items.iter() {523						out.push(item?);524					}525				} else {526					throw!(RuntimeError(527						"in std.join all items should be arrays".into()528					));529				}530			}531532			IndexableVal::Arr(out.into())533		}534		IndexableVal::Str(sep) => {535			let mut out = String::new();536537			let mut first = true;538			for item in arr.iter() {539				let item = item?.clone();540				if let Val::Str(item) = item {541					if !first {542						out += &sep;543					}544					first = false;545					out += &item;546				} else {547					throw!(RuntimeError(548						"in std.join all items should be strings".into()549					));550				}551			}552553			IndexableVal::Str(out.into())554		}555	})556}557558#[jrsonnet_macros::builtin]559fn builtin_escape_string_json(str_: IStr) -> Result<String> {560	Ok(escape_string_json(&str_))561}562563#[jrsonnet_macros::builtin]564fn builtin_manifest_json_ex(value: Any, indent: IStr) -> Result<String> {565	manifest_json_ex(566		&value.0,567		&ManifestJsonOptions {568			padding: &indent,569			mtype: ManifestType::Std,570		},571	)572}573574#[jrsonnet_macros::builtin]575fn builtin_manifest_yaml_doc(576	value: Any,577	indent_array_in_object: bool,578	quote_keys: bool,579) -> Result<String> {580	manifest_yaml_ex(581		&value.0,582		&ManifestYamlOptions {583			padding: "  ",584			arr_element_padding: if indent_array_in_object { "  " } else { "" },585			quote_keys,586		},587	)588}589590#[jrsonnet_macros::builtin]591fn builtin_reverse(value: ArrValue) -> Result<ArrValue> {592	Ok(value.reversed())593}594595#[jrsonnet_macros::builtin]596const fn builtin_id(v: Any) -> Result<Any> {597	Ok(v)598}599600#[jrsonnet_macros::builtin]601fn builtin_str_replace(str: String, from: IStr, to: IStr) -> Result<String> {602	Ok(str.replace(&from as &str, &to as &str))603}604605#[jrsonnet_macros::builtin]606fn builtin_splitlimit(str: IStr, c: char, maxsplits: Either<usize, M1>) -> Result<VecVal> {607	Ok(VecVal(match maxsplits {608		Either::Left(n) => str.splitn(n + 1, c).map(|s| Val::Str(s.into())).collect(),609		Either::Right(_) => str.split(c).map(|s| Val::Str(s.into())).collect(),610	}))611}612613#[jrsonnet_macros::builtin]614fn builtin_ascii_upper(str: IStr) -> Result<String> {615	Ok(str.to_ascii_uppercase())616}617618#[jrsonnet_macros::builtin]619fn builtin_ascii_lower(str: IStr) -> Result<String> {620	Ok(str.to_ascii_lowercase())621}622623#[jrsonnet_macros::builtin]624fn builtin_member(arr: IndexableVal, x: Any) -> Result<bool> {625	match arr {626		IndexableVal::Str(s) => {627			let x: IStr = IStr::try_from(x.0)?;628			Ok(!x.is_empty() && s.contains(&*x))629		}630		IndexableVal::Arr(a) => {631			for item in a.iter() {632				let item = item?;633				if equals(&item, &x.0)? {634					return Ok(true);635				}636			}637			Ok(false)638		}639	}640}641642#[jrsonnet_macros::builtin]643fn builtin_count(arr: Vec<Any>, v: Any) -> Result<usize> {644	let mut count = 0;645	for item in arr.iter() {646		if equals(&item.0, &v.0)? {647			count += 1;648		}649	}650	Ok(count)651}652653pub fn call_builtin(654	context: Context,655	loc: &ExprLocation,656	name: &str,657	args: &ArgsDesc,658) -> Result<Val> {659	BUILTINS660		.with(|builtins| builtins.get(name).copied())661		.ok_or_else(|| IntrinsicNotFound(name.into()))?(context, loc, args)662}
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) =