git.delta.rocks / jrsonnet / refs/commits / 1de48b14c4dd

difftreelog

refactor simplify intrinsic handling

Yaroslav Bolyukin2021-07-04parent: #0b0d703.patch.diff
in: master

7 files changed

modifiedcrates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -7,14 +7,11 @@
 edition = "2018"
 
 [features]
-default = ["serialized-stdlib", "faster", "explaining-traces", "serde-json"]
+default = ["serialized-stdlib", "explaining-traces", "serde-json"]
 # Serializes standard library AST instead of parsing them every run
 serialized-stdlib = ["serde", "bincode", "jrsonnet-parser/deserialize"]
 # Allow to convert Val into serde_json::Value and backwards
 serde-json = ["serde", "serde_json"]
-# Replace some standard library functions with faster implementations (I.e manifestJsonEx)
-# Library works fine without this feature, but requires more memory and time for std function calls
-faster = []
 # Rustc-like trace visualization
 explaining-traces = ["annotate-snippets"]
 # Allows library authors to throw custom errors
@@ -24,10 +21,10 @@
 unstable = []
 
 [dependencies]
-jrsonnet-interner = { path = "../jrsonnet-interner", version = "0.4.0" }
-jrsonnet-parser = { path = "../jrsonnet-parser", version = "0.4.0" }
-jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "0.4.0" }
-jrsonnet-types = { path = "../jrsonnet-types", version = "0.4.0" }
+jrsonnet-interner = { path="../jrsonnet-interner", version="0.4.0" }
+jrsonnet-parser = { path="../jrsonnet-parser", version="0.4.0" }
+jrsonnet-stdlib = { path="../jrsonnet-stdlib", version="0.4.0" }
+jrsonnet-types = { path="../jrsonnet-types", version="0.4.0" }
 pathdiff = "0.2.0"
 
 md5 = "0.7.0"
@@ -35,7 +32,7 @@
 rustc-hash = "1.1.0"
 
 thiserror = "1.0"
-jrsonnet-gc = { version = "0.4.2", features = ["derive"] }
+jrsonnet-gc = { version="0.4.2", features=["derive"] }
 
 [dependencies.anyhow]
 version = "1.0"
@@ -61,7 +58,7 @@
 optional = true
 
 [build-dependencies]
-jrsonnet-parser = { path = "../jrsonnet-parser", features = ["serialize", "deserialize"], version = "0.4.0" }
-jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "0.4.0" }
+jrsonnet-parser = { path="../jrsonnet-parser", features=["serialize", "deserialize"], version="0.4.0" }
+jrsonnet-stdlib = { path="../jrsonnet-stdlib", version="0.4.0" }
 serde = "1.0"
 bincode = "1.3.1"
modifiedcrates/jrsonnet-evaluator/build.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/build.rs
+++ b/crates/jrsonnet-evaluator/build.rs
@@ -1,14 +1,11 @@
 use bincode::serialize;
-use jrsonnet_parser::{
-	parse, Expr, FieldMember, FieldName, LocExpr, Member, ObjBody, ParserSettings,
-};
+use jrsonnet_parser::{parse, ParserSettings};
 use jrsonnet_stdlib::STDLIB_STR;
 use std::{
 	env,
 	fs::File,
 	io::Write,
 	path::{Path, PathBuf},
-	rc::Rc,
 };
 
 fn main() {
@@ -21,37 +18,6 @@
 	)
 	.expect("parse");
 
-	let parsed = if cfg!(feature = "faster") {
-		let LocExpr(expr, location) = parsed;
-		LocExpr(
-			Rc::new(match Rc::try_unwrap(expr).unwrap() {
-				Expr::Obj(ObjBody::MemberList(members)) => Expr::Obj(ObjBody::MemberList(
-					members
-						.into_iter()
-						.filter(|p| {
-							!matches!(
-								p,
-								Member::Field(FieldMember {
-									name: FieldName::Fixed(name),
-									..
-								})
-								if name == "join" || name == "manifestJsonEx" ||
-								name == "escapeStringJson" || name == "equals" ||
-								name == "base64" || name == "foldl" || name == "foldr" ||
-								name == "sortImpl" || name == "format" || name == "range" ||
-								name == "reverse" || name == "slice" || name == "mod" ||
-								name == "strReplace" || name == "map"
-							)
-						})
-						.collect(),
-				)),
-				_ => panic!("std value should be object"),
-			}),
-			location,
-		)
-	} else {
-		parsed
-	};
 	{
 		let out_dir = env::var("OUT_DIR").unwrap();
 		let dest_path = Path::new(&out_dir).join("stdlib.bincode");
modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -213,7 +213,6 @@
 	})
 }
 
-// faster
 fn builtin_slice(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
 	parse_args!(context, "slice", args, 4, [
 		0, indexable: ty!((string | array));
@@ -230,7 +229,6 @@
 	})
 }
 
-// faster
 fn builtin_primitive_equals(
 	context: Context,
 	_loc: Option<&ExprLocation>,
@@ -244,7 +242,6 @@
 	})
 }
 
-// faster
 fn builtin_equals(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
 	parse_args!(context, "equals", args, 2, [
 		0, a: ty!(any);
@@ -379,7 +376,6 @@
 	})
 }
 
-// faster
 fn builtin_format(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
 	parse_args!(context, "format", args, 2, [
 		0, str: ty!(string) => Val::Str;
@@ -529,7 +525,6 @@
 	})
 }
 
-// faster
 fn builtin_escape_string_json(
 	context: Context,
 	_loc: Option<&ExprLocation>,
@@ -542,7 +537,6 @@
 	})
 }
 
-// faster
 fn builtin_manifest_json_ex(
 	context: Context,
 	_loc: Option<&ExprLocation>,
@@ -559,7 +553,6 @@
 	})
 }
 
-// faster
 fn builtin_reverse(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
 	parse_args!(context, "reverse", args, 1, [
 		0, value: ty!(array) => Val::Arr;
@@ -576,7 +569,6 @@
 	})
 }
 
-// faster
 fn builtin_str_replace(
 	context: Context,
 	_loc: Option<&ExprLocation>,
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -546,8 +546,6 @@
 						|| {
 							if let Some(v) = v.get(s.clone())? {
 								Ok(v)
-							} else if v.get("__intrinsic_namespace__".into())?.is_some() {
-								Ok(Val::Func(Gc::new(FuncVal::Intrinsic(s))))
 							} else {
 								throw!(NoSuchField(s))
 							}
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/val.rs
1use crate::{2	builtin::{3		call_builtin,4		manifest::{manifest_json_ex, ManifestJsonOptions, ManifestType},5	},6	error::{Error::*, LocError},7	evaluate,8	function::{parse_function_call, parse_function_call_map, place_args},9	native::NativeCallback,10	throw, with_state, Context, ObjValue, Result,11};12use jrsonnet_gc::{Gc, GcCell, Trace};13use jrsonnet_interner::IStr;14use jrsonnet_parser::{el, Arg, ArgsDesc, Expr, ExprLocation, LiteralType, LocExpr, ParamsDesc};15use jrsonnet_types::ValType;16use std::{collections::HashMap, fmt::Debug, rc::Rc};1718pub trait LazyValValue: Trace {19	fn get(self: Box<Self>) -> Result<Val>;20}2122#[derive(Trace)]23#[trivially_drop]24enum LazyValInternals {25	Computed(Val),26	Errored(LocError),27	Waiting(Box<dyn LazyValValue>),28	Pending,29}3031#[derive(Clone, Trace)]32#[trivially_drop]33pub struct LazyVal(Gc<GcCell<LazyValInternals>>);34impl LazyVal {35	pub fn new(f: Box<dyn LazyValValue>) -> Self {36		Self(Gc::new(GcCell::new(LazyValInternals::Waiting(f))))37	}38	pub fn new_resolved(val: Val) -> Self {39		Self(Gc::new(GcCell::new(LazyValInternals::Computed(val))))40	}41	pub fn evaluate(&self) -> Result<Val> {42		match &*self.0.borrow() {43			LazyValInternals::Computed(v) => return Ok(v.clone()),44			LazyValInternals::Errored(e) => return Err(e.clone()),45			LazyValInternals::Pending => return Err(RecursiveLazyValueEvaluation.into()),46			_ => (),47		};48		let value = if let LazyValInternals::Waiting(value) =49			std::mem::replace(&mut *self.0.borrow_mut(), LazyValInternals::Pending)50		{51			value52		} else {53			unreachable!()54		};55		let new_value = match value.get() {56			Ok(v) => v,57			Err(e) => {58				*self.0.borrow_mut() = LazyValInternals::Errored(e.clone());59				return Err(e);60			}61		};62		*self.0.borrow_mut() = LazyValInternals::Computed(new_value.clone());63		Ok(new_value)64	}65}6667impl Debug for LazyVal {68	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {69		write!(f, "Lazy")70	}71}72impl PartialEq for LazyVal {73	fn eq(&self, other: &Self) -> bool {74		Gc::ptr_eq(&self.0, &other.0)75	}76}7778#[derive(Debug, PartialEq, Trace)]79#[trivially_drop]80pub struct FuncDesc {81	pub name: IStr,82	pub ctx: Context,83	pub params: ParamsDesc,84	pub body: LocExpr,85}8687#[derive(Debug, Trace)]88#[trivially_drop]89pub enum FuncVal {90	/// Plain function implemented in jsonnet91	Normal(FuncDesc),92	/// Standard library function93	Intrinsic(IStr),94	/// Library functions implemented in native95	NativeExt(IStr, Gc<NativeCallback>),96}9798impl PartialEq for FuncVal {99	fn eq(&self, other: &Self) -> bool {100		match (self, other) {101			(Self::Normal(a), Self::Normal(b)) => a == b,102			(Self::Intrinsic(an), Self::Intrinsic(bn)) => an == bn,103			(Self::NativeExt(an, _), Self::NativeExt(bn, _)) => an == bn,104			(..) => false,105		}106	}107}108impl FuncVal {109	pub fn is_ident(&self) -> bool {110		matches!(&self, Self::Intrinsic(n) if n as &str == "id")111	}112	pub fn name(&self) -> IStr {113		match self {114			Self::Normal(normal) => normal.name.clone(),115			Self::Intrinsic(name) => format!("std.{}", name).into(),116			Self::NativeExt(n, _) => format!("native.{}", n).into(),117		}118	}119	pub fn evaluate(120		&self,121		call_ctx: Context,122		loc: Option<&ExprLocation>,123		args: &ArgsDesc,124		tailstrict: bool,125	) -> Result<Val> {126		match self {127			Self::Normal(func) => {128				let ctx = parse_function_call(129					call_ctx,130					Some(func.ctx.clone()),131					&func.params,132					args,133					tailstrict,134				)?;135				evaluate(ctx, &func.body)136			}137			Self::Intrinsic(name) => call_builtin(call_ctx, loc, name, args),138			Self::NativeExt(_name, handler) => {139				let args = parse_function_call(call_ctx, None, &handler.params, args, true)?;140				let mut out_args = Vec::with_capacity(handler.params.len());141				for p in handler.params.0.iter() {142					out_args.push(args.binding(p.0.clone())?.evaluate()?);143				}144				Ok(handler.call(loc.map(|l| l.0.clone()), &out_args)?)145			}146		}147	}148149	pub fn evaluate_map(150		&self,151		call_ctx: Context,152		args: &HashMap<IStr, Val>,153		tailstrict: bool,154	) -> Result<Val> {155		match self {156			Self::Normal(func) => {157				let ctx = parse_function_call_map(158					call_ctx,159					Some(func.ctx.clone()),160					&func.params,161					args,162					tailstrict,163				)?;164				evaluate(ctx, &func.body)165			}166			Self::Intrinsic(_) => todo!(),167			Self::NativeExt(_, _) => todo!(),168		}169	}170171	pub fn evaluate_values(&self, call_ctx: Context, args: &[Val]) -> Result<Val> {172		match self {173			Self::Normal(func) => {174				let ctx = place_args(call_ctx, Some(func.ctx.clone()), &func.params, args)?;175				evaluate(ctx, &func.body)176			}177			Self::Intrinsic(_) => todo!(),178			Self::NativeExt(_, _) => todo!(),179		}180	}181}182183#[derive(Clone)]184pub enum ManifestFormat {185	YamlStream(Box<ManifestFormat>),186	Yaml(usize),187	Json(usize),188	ToString,189	String,190}191192#[derive(Debug, Clone, Trace)]193#[trivially_drop]194pub enum ArrValue {195	Lazy(Gc<Vec<LazyVal>>),196	Eager(Gc<Vec<Val>>),197	Extended(Box<(Self, Self)>),198}199impl ArrValue {200	pub fn new_eager() -> Self {201		Self::Eager(Gc::new(Vec::new()))202	}203204	pub fn len(&self) -> usize {205		match self {206			Self::Lazy(l) => l.len(),207			Self::Eager(e) => e.len(),208			Self::Extended(v) => v.0.len() + v.1.len(),209		}210	}211212	pub fn is_empty(&self) -> bool {213		self.len() == 0214	}215216	pub fn get(&self, index: usize) -> Result<Option<Val>> {217		match self {218			Self::Lazy(vec) => {219				if let Some(v) = vec.get(index) {220					Ok(Some(v.evaluate()?))221				} else {222					Ok(None)223				}224			}225			Self::Eager(vec) => Ok(vec.get(index).cloned()),226			Self::Extended(v) => {227				let a_len = v.0.len();228				if a_len > index {229					v.0.get(index)230				} else {231					v.1.get(index - a_len)232				}233			}234		}235	}236237	pub fn get_lazy(&self, index: usize) -> Option<LazyVal> {238		match self {239			Self::Lazy(vec) => vec.get(index).cloned(),240			Self::Eager(vec) => vec.get(index).cloned().map(LazyVal::new_resolved),241			Self::Extended(v) => {242				let a_len = v.0.len();243				if a_len > index {244					v.0.get_lazy(index)245				} else {246					v.1.get_lazy(index - a_len)247				}248			}249		}250	}251252	pub fn evaluated(&self) -> Result<Gc<Vec<Val>>> {253		Ok(match self {254			Self::Lazy(vec) => {255				let mut out = Vec::with_capacity(vec.len());256				for item in vec.iter() {257					out.push(item.evaluate()?);258				}259				Gc::new(out)260			}261			Self::Eager(vec) => vec.clone(),262			Self::Extended(_v) => {263				let mut out = Vec::with_capacity(self.len());264				for item in self.iter() {265					out.push(item?);266				}267				Gc::new(out)268			}269		})270	}271272	pub fn iter(&self) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {273		(0..self.len()).map(move |idx| match self {274			Self::Lazy(l) => l[idx].evaluate(),275			Self::Eager(e) => Ok(e[idx].clone()),276			Self::Extended(_) => self.get(idx).map(|e| e.unwrap()),277		})278	}279280	pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = LazyVal> + '_ {281		(0..self.len()).map(move |idx| match self {282			Self::Lazy(l) => l[idx].clone(),283			Self::Eager(e) => LazyVal::new_resolved(e[idx].clone()),284			Self::Extended(_) => self.get_lazy(idx).unwrap(),285		})286	}287288	pub fn reversed(self) -> Self {289		match self {290			Self::Lazy(vec) => {291				let mut out = (&vec as &Vec<_>).clone();292				out.reverse();293				Self::Lazy(Gc::new(out))294			}295			Self::Eager(vec) => {296				let mut out = (&vec as &Vec<_>).clone();297				out.reverse();298				Self::Eager(Gc::new(out))299			}300			Self::Extended(b) => Self::Extended(Box::new((b.1.reversed(), b.0.reversed()))),301		}302	}303304	pub fn map(self, mapper: impl Fn(Val) -> Result<Val>) -> Result<Self> {305		let mut out = Vec::with_capacity(self.len());306307		for value in self.iter() {308			out.push(mapper(value?)?);309		}310311		Ok(Self::Eager(Gc::new(out)))312	}313314	pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {315		let mut out = Vec::with_capacity(self.len());316317		for value in self.iter() {318			let value = value?;319			if filter(&value)? {320				out.push(value);321			}322		}323324		Ok(Self::Eager(Gc::new(out)))325	}326327	pub fn ptr_eq(a: &Self, b: &Self) -> bool {328		match (a, b) {329			(Self::Lazy(a), Self::Lazy(b)) => Gc::ptr_eq(a, b),330			(Self::Eager(a), Self::Eager(b)) => Gc::ptr_eq(a, b),331			_ => false,332		}333	}334}335336impl From<Vec<LazyVal>> for ArrValue {337	fn from(v: Vec<LazyVal>) -> Self {338		Self::Lazy(Gc::new(v))339	}340}341342impl From<Vec<Val>> for ArrValue {343	fn from(v: Vec<Val>) -> Self {344		Self::Eager(Gc::new(v))345	}346}347348pub enum IndexableVal {349	Str(IStr),350	Arr(ArrValue),351}352353#[derive(Debug, Clone, Trace)]354#[trivially_drop]355pub enum Val {356	Bool(bool),357	Null,358	Str(IStr),359	Num(f64),360	Arr(ArrValue),361	Obj(ObjValue),362	Func(Gc<FuncVal>),363}364365macro_rules! matches_unwrap {366	($e: expr, $p: pat, $r: expr) => {367		match $e {368			$p => $r,369			_ => panic!("no match"),370		}371	};372}373impl Val {374	/// Creates `Val::Num` after checking for numeric overflow.375	/// As numbers are `f64`, we can just check for their finity.376	pub fn new_checked_num(num: f64) -> Result<Self> {377		if num.is_finite() {378			Ok(Self::Num(num))379		} else {380			throw!(RuntimeError("overflow".into()))381		}382	}383384	pub fn assert_type(&self, context: &'static str, val_type: ValType) -> Result<()> {385		let this_type = self.value_type();386		if this_type != val_type {387			throw!(TypeMismatch(context, vec![val_type], this_type))388		} else {389			Ok(())390		}391	}392	pub fn unwrap_num(self) -> Result<f64> {393		Ok(matches_unwrap!(self, Self::Num(v), v))394	}395	pub fn unwrap_func(self) -> Result<Gc<FuncVal>> {396		Ok(matches_unwrap!(self, Self::Func(v), v))397	}398	pub fn try_cast_bool(self, context: &'static str) -> Result<bool> {399		self.assert_type(context, ValType::Bool)?;400		Ok(matches_unwrap!(self, Self::Bool(v), v))401	}402	pub fn try_cast_str(self, context: &'static str) -> Result<IStr> {403		self.assert_type(context, ValType::Str)?;404		Ok(matches_unwrap!(self, Self::Str(v), v))405	}406	pub fn try_cast_num(self, context: &'static str) -> Result<f64> {407		self.assert_type(context, ValType::Num)?;408		self.unwrap_num()409	}410	pub fn try_cast_nullable_num(self, context: &'static str) -> Result<Option<f64>> {411		Ok(match self {412			Val::Null => None,413			Val::Num(num) => Some(num),414			_ => throw!(TypeMismatch(415				context,416				vec![ValType::Null, ValType::Num],417				self.value_type()418			)),419		})420	}421	pub const fn value_type(&self) -> ValType {422		match self {423			Self::Str(..) => ValType::Str,424			Self::Num(..) => ValType::Num,425			Self::Arr(..) => ValType::Arr,426			Self::Obj(..) => ValType::Obj,427			Self::Bool(_) => ValType::Bool,428			Self::Null => ValType::Null,429			Self::Func(..) => ValType::Func,430		}431	}432433	pub fn to_string(&self) -> Result<IStr> {434		Ok(match self {435			Self::Bool(true) => "true".into(),436			Self::Bool(false) => "false".into(),437			Self::Null => "null".into(),438			Self::Str(s) => s.clone(),439			v => manifest_json_ex(440				v,441				&ManifestJsonOptions {442					padding: "",443					mtype: ManifestType::ToString,444				},445			)?446			.into(),447		})448	}449450	/// Expects value to be object, outputs (key, manifested value) pairs451	pub fn manifest_multi(&self, ty: &ManifestFormat) -> Result<Vec<(IStr, IStr)>> {452		let obj = match self {453			Self::Obj(obj) => obj,454			_ => throw!(MultiManifestOutputIsNotAObject),455		};456		let keys = obj.fields();457		let mut out = Vec::with_capacity(keys.len());458		for key in keys {459			let value = obj460				.get(key.clone())?461				.expect("item in object")462				.manifest(ty)?;463			out.push((key, value));464		}465		Ok(out)466	}467468	/// Expects value to be array, outputs manifested values469	pub fn manifest_stream(&self, ty: &ManifestFormat) -> Result<Vec<IStr>> {470		let arr = match self {471			Self::Arr(a) => a,472			_ => throw!(StreamManifestOutputIsNotAArray),473		};474		let mut out = Vec::with_capacity(arr.len());475		for i in arr.iter() {476			out.push(i?.manifest(ty)?);477		}478		Ok(out)479	}480481	pub fn manifest(&self, ty: &ManifestFormat) -> Result<IStr> {482		Ok(match ty {483			ManifestFormat::YamlStream(format) => {484				let arr = match self {485					Self::Arr(a) => a,486					_ => throw!(StreamManifestOutputIsNotAArray),487				};488				let mut out = String::new();489490				match format as &ManifestFormat {491					ManifestFormat::YamlStream(_) => throw!(StreamManifestOutputCannotBeRecursed),492					ManifestFormat::String => throw!(StreamManifestCannotNestString),493					_ => {}494				};495496				if !arr.is_empty() {497					for v in arr.iter() {498						out.push_str("---\n");499						out.push_str(&v?.manifest(format)?);500						out.push('\n');501					}502					out.push_str("...");503				}504505				out.into()506			}507			ManifestFormat::Yaml(padding) => self.to_yaml(*padding)?,508			ManifestFormat::Json(padding) => self.to_json(*padding)?,509			ManifestFormat::ToString => self.to_string()?,510			ManifestFormat::String => match self {511				Self::Str(s) => s.clone(),512				_ => throw!(StringManifestOutputIsNotAString),513			},514		})515	}516517	/// For manifestification518	pub fn to_json(&self, padding: usize) -> Result<IStr> {519		manifest_json_ex(520			self,521			&ManifestJsonOptions {522				padding: &" ".repeat(padding),523				mtype: if padding == 0 {524					ManifestType::Minify525				} else {526					ManifestType::Manifest527				},528			},529		)530		.map(|s| s.into())531	}532533	/// Calls `std.manifestJson`534	#[cfg(feature = "faster")]535	pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {536		manifest_json_ex(537			self,538			&ManifestJsonOptions {539				padding: &" ".repeat(padding),540				mtype: ManifestType::Std,541			},542		)543		.map(|s| s.into())544	}545546	/// Calls `std.manifestJson`547	#[cfg(not(feature = "faster"))]548	pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {549		with_state(|s| {550			let ctx = s551				.create_default_context()?552				.with_var("__tmp__to_json__".into(), self.clone())?;553			Ok(evaluate(554				ctx,555				&el!(Expr::Apply(556					el!(Expr::Index(557						el!(Expr::Var("std".into())),558						el!(Expr::Str("manifestJsonEx".into()))559					)),560					ArgsDesc(vec![561						Arg(None, el!(Expr::Var("__tmp__to_json__".into()))),562						Arg(None, el!(Expr::Str(" ".repeat(padding).into())))563					]),564					false565				)),566			)?567			.try_cast_str("to json")?)568		})569	}570	pub fn to_yaml(&self, padding: usize) -> Result<IStr> {571		with_state(|s| {572			let ctx = s573				.create_default_context()574				.with_var("__tmp__to_json__".into(), self.clone());575			evaluate(576				ctx,577				&el!(Expr::Apply(578					el!(Expr::Index(579						el!(Expr::Var("std".into())),580						el!(Expr::Str("manifestYamlDoc".into()))581					)),582					ArgsDesc(vec![583						Arg(None, el!(Expr::Var("__tmp__to_json__".into()))),584						Arg(585							None,586							el!(Expr::Literal(if padding != 0 {587								LiteralType::True588							} else {589								LiteralType::False590							}))591						)592					]),593					false594				)),595			)?596			.try_cast_str("to json")597		})598	}599	pub fn to_indexable(self) -> Result<IndexableVal> {600		Ok(match self {601			Val::Str(s) => IndexableVal::Str(s),602			Val::Arr(arr) => IndexableVal::Arr(arr),603			_ => throw!(ValueIsNotIndexable(self.value_type())),604		})605	}606}607608const fn is_function_like(val: &Val) -> bool {609	matches!(val, Val::Func(_))610}611612/// Native implementation of `std.primitiveEquals`613pub fn primitive_equals(val_a: &Val, val_b: &Val) -> Result<bool> {614	Ok(match (val_a, val_b) {615		(Val::Bool(a), Val::Bool(b)) => a == b,616		(Val::Null, Val::Null) => true,617		(Val::Str(a), Val::Str(b)) => a == b,618		(Val::Num(a), Val::Num(b)) => (a - b).abs() <= f64::EPSILON,619		(Val::Arr(_), Val::Arr(_)) => throw!(RuntimeError(620			"primitiveEquals operates on primitive types, got array".into(),621		)),622		(Val::Obj(_), Val::Obj(_)) => throw!(RuntimeError(623			"primitiveEquals operates on primitive types, got object".into(),624		)),625		(a, b) if is_function_like(a) && is_function_like(b) => {626			throw!(RuntimeError("cannot test equality of functions".into()))627		}628		(_, _) => false,629	})630}631632/// Native implementation of `std.equals`633pub fn equals(val_a: &Val, val_b: &Val) -> Result<bool> {634	if val_a.value_type() != val_b.value_type() {635		return Ok(false);636	}637	match (val_a, val_b) {638		(Val::Arr(a), Val::Arr(b)) => {639			if ArrValue::ptr_eq(a, b) {640				return Ok(true);641			}642			if a.len() != b.len() {643				return Ok(false);644			}645			for (a, b) in a.iter().zip(b.iter()) {646				if !equals(&a?, &b?)? {647					return Ok(false);648				}649			}650			Ok(true)651		}652		(Val::Obj(a), Val::Obj(b)) => {653			if ObjValue::ptr_eq(a, b) {654				return Ok(true);655			}656			let fields = a.fields();657			if fields != b.fields() {658				return Ok(false);659			}660			for field in fields {661				if !equals(&a.get(field.clone())?.unwrap(), &b.get(field)?.unwrap())? {662					return Ok(false);663				}664			}665			Ok(true)666		}667		(a, b) => Ok(primitive_equals(a, b)?),668	}669}
after · crates/jrsonnet-evaluator/src/val.rs
1use crate::{2	builtin::{3		call_builtin,4		manifest::{manifest_json_ex, ManifestJsonOptions, ManifestType},5	},6	error::{Error::*, LocError},7	evaluate,8	function::{parse_function_call, parse_function_call_map, place_args},9	native::NativeCallback,10	throw, with_state, Context, ObjValue, Result,11};12use jrsonnet_gc::{Gc, GcCell, Trace};13use jrsonnet_interner::IStr;14use jrsonnet_parser::{el, Arg, ArgsDesc, Expr, ExprLocation, LiteralType, LocExpr, ParamsDesc};15use jrsonnet_types::ValType;16use std::{collections::HashMap, fmt::Debug, rc::Rc};1718pub trait LazyValValue: Trace {19	fn get(self: Box<Self>) -> Result<Val>;20}2122#[derive(Trace)]23#[trivially_drop]24enum LazyValInternals {25	Computed(Val),26	Errored(LocError),27	Waiting(Box<dyn LazyValValue>),28	Pending,29}3031#[derive(Clone, Trace)]32#[trivially_drop]33pub struct LazyVal(Gc<GcCell<LazyValInternals>>);34impl LazyVal {35	pub fn new(f: Box<dyn LazyValValue>) -> Self {36		Self(Gc::new(GcCell::new(LazyValInternals::Waiting(f))))37	}38	pub fn new_resolved(val: Val) -> Self {39		Self(Gc::new(GcCell::new(LazyValInternals::Computed(val))))40	}41	pub fn evaluate(&self) -> Result<Val> {42		match &*self.0.borrow() {43			LazyValInternals::Computed(v) => return Ok(v.clone()),44			LazyValInternals::Errored(e) => return Err(e.clone()),45			LazyValInternals::Pending => return Err(RecursiveLazyValueEvaluation.into()),46			_ => (),47		};48		let value = if let LazyValInternals::Waiting(value) =49			std::mem::replace(&mut *self.0.borrow_mut(), LazyValInternals::Pending)50		{51			value52		} else {53			unreachable!()54		};55		let new_value = match value.get() {56			Ok(v) => v,57			Err(e) => {58				*self.0.borrow_mut() = LazyValInternals::Errored(e.clone());59				return Err(e);60			}61		};62		*self.0.borrow_mut() = LazyValInternals::Computed(new_value.clone());63		Ok(new_value)64	}65}6667impl Debug for LazyVal {68	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {69		write!(f, "Lazy")70	}71}72impl PartialEq for LazyVal {73	fn eq(&self, other: &Self) -> bool {74		Gc::ptr_eq(&self.0, &other.0)75	}76}7778#[derive(Debug, PartialEq, Trace)]79#[trivially_drop]80pub struct FuncDesc {81	pub name: IStr,82	pub ctx: Context,83	pub params: ParamsDesc,84	pub body: LocExpr,85}8687#[derive(Debug, Trace)]88#[trivially_drop]89pub enum FuncVal {90	/// Plain function implemented in jsonnet91	Normal(FuncDesc),92	/// Standard library function93	Intrinsic(IStr),94	/// Library functions implemented in native95	NativeExt(IStr, Gc<NativeCallback>),96}9798impl PartialEq for FuncVal {99	fn eq(&self, other: &Self) -> bool {100		match (self, other) {101			(Self::Normal(a), Self::Normal(b)) => a == b,102			(Self::Intrinsic(an), Self::Intrinsic(bn)) => an == bn,103			(Self::NativeExt(an, _), Self::NativeExt(bn, _)) => an == bn,104			(..) => false,105		}106	}107}108impl FuncVal {109	pub fn is_ident(&self) -> bool {110		matches!(&self, Self::Intrinsic(n) if n as &str == "id")111	}112	pub fn name(&self) -> IStr {113		match self {114			Self::Normal(normal) => normal.name.clone(),115			Self::Intrinsic(name) => format!("std.{}", name).into(),116			Self::NativeExt(n, _) => format!("native.{}", n).into(),117		}118	}119	pub fn evaluate(120		&self,121		call_ctx: Context,122		loc: Option<&ExprLocation>,123		args: &ArgsDesc,124		tailstrict: bool,125	) -> Result<Val> {126		match self {127			Self::Normal(func) => {128				let ctx = parse_function_call(129					call_ctx,130					Some(func.ctx.clone()),131					&func.params,132					args,133					tailstrict,134				)?;135				evaluate(ctx, &func.body)136			}137			Self::Intrinsic(name) => call_builtin(call_ctx, loc, name, args),138			Self::NativeExt(_name, handler) => {139				let args = parse_function_call(call_ctx, None, &handler.params, args, true)?;140				let mut out_args = Vec::with_capacity(handler.params.len());141				for p in handler.params.0.iter() {142					out_args.push(args.binding(p.0.clone())?.evaluate()?);143				}144				Ok(handler.call(loc.map(|l| l.0.clone()), &out_args)?)145			}146		}147	}148149	pub fn evaluate_map(150		&self,151		call_ctx: Context,152		args: &HashMap<IStr, Val>,153		tailstrict: bool,154	) -> Result<Val> {155		match self {156			Self::Normal(func) => {157				let ctx = parse_function_call_map(158					call_ctx,159					Some(func.ctx.clone()),160					&func.params,161					args,162					tailstrict,163				)?;164				evaluate(ctx, &func.body)165			}166			Self::Intrinsic(_) => todo!(),167			Self::NativeExt(_, _) => todo!(),168		}169	}170171	pub fn evaluate_values(&self, call_ctx: Context, args: &[Val]) -> Result<Val> {172		match self {173			Self::Normal(func) => {174				let ctx = place_args(call_ctx, Some(func.ctx.clone()), &func.params, args)?;175				evaluate(ctx, &func.body)176			}177			Self::Intrinsic(_) => todo!(),178			Self::NativeExt(_, _) => todo!(),179		}180	}181}182183#[derive(Clone)]184pub enum ManifestFormat {185	YamlStream(Box<ManifestFormat>),186	Yaml(usize),187	Json(usize),188	ToString,189	String,190}191192#[derive(Debug, Clone, Trace)]193#[trivially_drop]194pub enum ArrValue {195	Lazy(Gc<Vec<LazyVal>>),196	Eager(Gc<Vec<Val>>),197	Extended(Box<(Self, Self)>),198}199impl ArrValue {200	pub fn new_eager() -> Self {201		Self::Eager(Gc::new(Vec::new()))202	}203204	pub fn len(&self) -> usize {205		match self {206			Self::Lazy(l) => l.len(),207			Self::Eager(e) => e.len(),208			Self::Extended(v) => v.0.len() + v.1.len(),209		}210	}211212	pub fn is_empty(&self) -> bool {213		self.len() == 0214	}215216	pub fn get(&self, index: usize) -> Result<Option<Val>> {217		match self {218			Self::Lazy(vec) => {219				if let Some(v) = vec.get(index) {220					Ok(Some(v.evaluate()?))221				} else {222					Ok(None)223				}224			}225			Self::Eager(vec) => Ok(vec.get(index).cloned()),226			Self::Extended(v) => {227				let a_len = v.0.len();228				if a_len > index {229					v.0.get(index)230				} else {231					v.1.get(index - a_len)232				}233			}234		}235	}236237	pub fn get_lazy(&self, index: usize) -> Option<LazyVal> {238		match self {239			Self::Lazy(vec) => vec.get(index).cloned(),240			Self::Eager(vec) => vec.get(index).cloned().map(LazyVal::new_resolved),241			Self::Extended(v) => {242				let a_len = v.0.len();243				if a_len > index {244					v.0.get_lazy(index)245				} else {246					v.1.get_lazy(index - a_len)247				}248			}249		}250	}251252	pub fn evaluated(&self) -> Result<Gc<Vec<Val>>> {253		Ok(match self {254			Self::Lazy(vec) => {255				let mut out = Vec::with_capacity(vec.len());256				for item in vec.iter() {257					out.push(item.evaluate()?);258				}259				Gc::new(out)260			}261			Self::Eager(vec) => vec.clone(),262			Self::Extended(_v) => {263				let mut out = Vec::with_capacity(self.len());264				for item in self.iter() {265					out.push(item?);266				}267				Gc::new(out)268			}269		})270	}271272	pub fn iter(&self) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {273		(0..self.len()).map(move |idx| match self {274			Self::Lazy(l) => l[idx].evaluate(),275			Self::Eager(e) => Ok(e[idx].clone()),276			Self::Extended(_) => self.get(idx).map(|e| e.unwrap()),277		})278	}279280	pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = LazyVal> + '_ {281		(0..self.len()).map(move |idx| match self {282			Self::Lazy(l) => l[idx].clone(),283			Self::Eager(e) => LazyVal::new_resolved(e[idx].clone()),284			Self::Extended(_) => self.get_lazy(idx).unwrap(),285		})286	}287288	pub fn reversed(self) -> Self {289		match self {290			Self::Lazy(vec) => {291				let mut out = (&vec as &Vec<_>).clone();292				out.reverse();293				Self::Lazy(Gc::new(out))294			}295			Self::Eager(vec) => {296				let mut out = (&vec as &Vec<_>).clone();297				out.reverse();298				Self::Eager(Gc::new(out))299			}300			Self::Extended(b) => Self::Extended(Box::new((b.1.reversed(), b.0.reversed()))),301		}302	}303304	pub fn map(self, mapper: impl Fn(Val) -> Result<Val>) -> Result<Self> {305		let mut out = Vec::with_capacity(self.len());306307		for value in self.iter() {308			out.push(mapper(value?)?);309		}310311		Ok(Self::Eager(Gc::new(out)))312	}313314	pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {315		let mut out = Vec::with_capacity(self.len());316317		for value in self.iter() {318			let value = value?;319			if filter(&value)? {320				out.push(value);321			}322		}323324		Ok(Self::Eager(Gc::new(out)))325	}326327	pub fn ptr_eq(a: &Self, b: &Self) -> bool {328		match (a, b) {329			(Self::Lazy(a), Self::Lazy(b)) => Gc::ptr_eq(a, b),330			(Self::Eager(a), Self::Eager(b)) => Gc::ptr_eq(a, b),331			_ => false,332		}333	}334}335336impl From<Vec<LazyVal>> for ArrValue {337	fn from(v: Vec<LazyVal>) -> Self {338		Self::Lazy(Gc::new(v))339	}340}341342impl From<Vec<Val>> for ArrValue {343	fn from(v: Vec<Val>) -> Self {344		Self::Eager(Gc::new(v))345	}346}347348pub enum IndexableVal {349	Str(IStr),350	Arr(ArrValue),351}352353#[derive(Debug, Clone, Trace)]354#[trivially_drop]355pub enum Val {356	Bool(bool),357	Null,358	Str(IStr),359	Num(f64),360	Arr(ArrValue),361	Obj(ObjValue),362	Func(Gc<FuncVal>),363}364365macro_rules! matches_unwrap {366	($e: expr, $p: pat, $r: expr) => {367		match $e {368			$p => $r,369			_ => panic!("no match"),370		}371	};372}373impl Val {374	/// Creates `Val::Num` after checking for numeric overflow.375	/// As numbers are `f64`, we can just check for their finity.376	pub fn new_checked_num(num: f64) -> Result<Self> {377		if num.is_finite() {378			Ok(Self::Num(num))379		} else {380			throw!(RuntimeError("overflow".into()))381		}382	}383384	pub fn assert_type(&self, context: &'static str, val_type: ValType) -> Result<()> {385		let this_type = self.value_type();386		if this_type != val_type {387			throw!(TypeMismatch(context, vec![val_type], this_type))388		} else {389			Ok(())390		}391	}392	pub fn unwrap_num(self) -> Result<f64> {393		Ok(matches_unwrap!(self, Self::Num(v), v))394	}395	pub fn unwrap_func(self) -> Result<Gc<FuncVal>> {396		Ok(matches_unwrap!(self, Self::Func(v), v))397	}398	pub fn try_cast_bool(self, context: &'static str) -> Result<bool> {399		self.assert_type(context, ValType::Bool)?;400		Ok(matches_unwrap!(self, Self::Bool(v), v))401	}402	pub fn try_cast_str(self, context: &'static str) -> Result<IStr> {403		self.assert_type(context, ValType::Str)?;404		Ok(matches_unwrap!(self, Self::Str(v), v))405	}406	pub fn try_cast_num(self, context: &'static str) -> Result<f64> {407		self.assert_type(context, ValType::Num)?;408		self.unwrap_num()409	}410	pub fn try_cast_nullable_num(self, context: &'static str) -> Result<Option<f64>> {411		Ok(match self {412			Val::Null => None,413			Val::Num(num) => Some(num),414			_ => throw!(TypeMismatch(415				context,416				vec![ValType::Null, ValType::Num],417				self.value_type()418			)),419		})420	}421	pub const fn value_type(&self) -> ValType {422		match self {423			Self::Str(..) => ValType::Str,424			Self::Num(..) => ValType::Num,425			Self::Arr(..) => ValType::Arr,426			Self::Obj(..) => ValType::Obj,427			Self::Bool(_) => ValType::Bool,428			Self::Null => ValType::Null,429			Self::Func(..) => ValType::Func,430		}431	}432433	pub fn to_string(&self) -> Result<IStr> {434		Ok(match self {435			Self::Bool(true) => "true".into(),436			Self::Bool(false) => "false".into(),437			Self::Null => "null".into(),438			Self::Str(s) => s.clone(),439			v => manifest_json_ex(440				v,441				&ManifestJsonOptions {442					padding: "",443					mtype: ManifestType::ToString,444				},445			)?446			.into(),447		})448	}449450	/// Expects value to be object, outputs (key, manifested value) pairs451	pub fn manifest_multi(&self, ty: &ManifestFormat) -> Result<Vec<(IStr, IStr)>> {452		let obj = match self {453			Self::Obj(obj) => obj,454			_ => throw!(MultiManifestOutputIsNotAObject),455		};456		let keys = obj.fields();457		let mut out = Vec::with_capacity(keys.len());458		for key in keys {459			let value = obj460				.get(key.clone())?461				.expect("item in object")462				.manifest(ty)?;463			out.push((key, value));464		}465		Ok(out)466	}467468	/// Expects value to be array, outputs manifested values469	pub fn manifest_stream(&self, ty: &ManifestFormat) -> Result<Vec<IStr>> {470		let arr = match self {471			Self::Arr(a) => a,472			_ => throw!(StreamManifestOutputIsNotAArray),473		};474		let mut out = Vec::with_capacity(arr.len());475		for i in arr.iter() {476			out.push(i?.manifest(ty)?);477		}478		Ok(out)479	}480481	pub fn manifest(&self, ty: &ManifestFormat) -> Result<IStr> {482		Ok(match ty {483			ManifestFormat::YamlStream(format) => {484				let arr = match self {485					Self::Arr(a) => a,486					_ => throw!(StreamManifestOutputIsNotAArray),487				};488				let mut out = String::new();489490				match format as &ManifestFormat {491					ManifestFormat::YamlStream(_) => throw!(StreamManifestOutputCannotBeRecursed),492					ManifestFormat::String => throw!(StreamManifestCannotNestString),493					_ => {}494				};495496				if !arr.is_empty() {497					for v in arr.iter() {498						out.push_str("---\n");499						out.push_str(&v?.manifest(format)?);500						out.push('\n');501					}502					out.push_str("...");503				}504505				out.into()506			}507			ManifestFormat::Yaml(padding) => self.to_yaml(*padding)?,508			ManifestFormat::Json(padding) => self.to_json(*padding)?,509			ManifestFormat::ToString => self.to_string()?,510			ManifestFormat::String => match self {511				Self::Str(s) => s.clone(),512				_ => throw!(StringManifestOutputIsNotAString),513			},514		})515	}516517	/// For manifestification518	pub fn to_json(&self, padding: usize) -> Result<IStr> {519		manifest_json_ex(520			self,521			&ManifestJsonOptions {522				padding: &" ".repeat(padding),523				mtype: if padding == 0 {524					ManifestType::Minify525				} else {526					ManifestType::Manifest527				},528			},529		)530		.map(|s| s.into())531	}532533	/// Calls `std.manifestJson`534	pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {535		manifest_json_ex(536			self,537			&ManifestJsonOptions {538				padding: &" ".repeat(padding),539				mtype: ManifestType::Std,540			},541		)542		.map(|s| s.into())543	}544545	pub fn to_yaml(&self, padding: usize) -> Result<IStr> {546		with_state(|s| {547			let ctx = s548				.create_default_context()549				.with_var("__tmp__to_json__".into(), self.clone());550			evaluate(551				ctx,552				&el!(Expr::Apply(553					el!(Expr::Index(554						el!(Expr::Var("std".into())),555						el!(Expr::Str("manifestYamlDoc".into()))556					)),557					ArgsDesc(vec![558						Arg(None, el!(Expr::Var("__tmp__to_json__".into()))),559						Arg(560							None,561							el!(Expr::Literal(if padding != 0 {562								LiteralType::True563							} else {564								LiteralType::False565							}))566						)567					]),568					false569				)),570			)?571			.try_cast_str("to json")572		})573	}574	pub fn to_indexable(self) -> Result<IndexableVal> {575		Ok(match self {576			Val::Str(s) => IndexableVal::Str(s),577			Val::Arr(arr) => IndexableVal::Arr(arr),578			_ => throw!(ValueIsNotIndexable(self.value_type())),579		})580	}581}582583const fn is_function_like(val: &Val) -> bool {584	matches!(val, Val::Func(_))585}586587/// Native implementation of `std.primitiveEquals`588pub fn primitive_equals(val_a: &Val, val_b: &Val) -> Result<bool> {589	Ok(match (val_a, val_b) {590		(Val::Bool(a), Val::Bool(b)) => a == b,591		(Val::Null, Val::Null) => true,592		(Val::Str(a), Val::Str(b)) => a == b,593		(Val::Num(a), Val::Num(b)) => (a - b).abs() <= f64::EPSILON,594		(Val::Arr(_), Val::Arr(_)) => throw!(RuntimeError(595			"primitiveEquals operates on primitive types, got array".into(),596		)),597		(Val::Obj(_), Val::Obj(_)) => throw!(RuntimeError(598			"primitiveEquals operates on primitive types, got object".into(),599		)),600		(a, b) if is_function_like(a) && is_function_like(b) => {601			throw!(RuntimeError("cannot test equality of functions".into()))602		}603		(_, _) => false,604	})605}606607/// Native implementation of `std.equals`608pub fn equals(val_a: &Val, val_b: &Val) -> Result<bool> {609	if val_a.value_type() != val_b.value_type() {610		return Ok(false);611	}612	match (val_a, val_b) {613		(Val::Arr(a), Val::Arr(b)) => {614			if ArrValue::ptr_eq(a, b) {615				return Ok(true);616			}617			if a.len() != b.len() {618				return Ok(false);619			}620			for (a, b) in a.iter().zip(b.iter()) {621				if !equals(&a?, &b?)? {622					return Ok(false);623				}624			}625			Ok(true)626		}627		(Val::Obj(a), Val::Obj(b)) => {628			if ObjValue::ptr_eq(a, b) {629				return Ok(true);630			}631			let fields = a.fields();632			if fields != b.fields() {633				return Ok(false);634			}635			for field in fields {636				if !equals(&a.get(field.clone())?.unwrap(), &b.get(field)?.unwrap())? {637					return Ok(false);638				}639			}640			Ok(true)641		}642		(a, b) => Ok(primitive_equals(a, b)?),643	}644}
modifiedcrates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -192,6 +192,8 @@
 		pub rule expr_basic(s: &ParserSettings) -> LocExpr
 			= literal(s)
 
+			/ quiet!{l(s,<"$intrinsic(" name:$(id()) ")" {Expr::Intrinsic(name.into())}>)}
+
 			/ string_expr(s) / number_expr(s)
 			/ array_expr(s)
 			/ obj_expr(s)
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -1,9 +1,29 @@
 {
-  __intrinsic_namespace__:: 'std',
-
   local std = self,
   local id = std.id,
 
+  # Those functions aren't normally located in stdlib
+  length:: $intrinsic(length),
+  type:: $intrinsic(type),
+  makeArray:: $intrinsic(makeArray),
+  codepoint:: $intrinsic(codepoint),
+  objectFieldsEx:: $intrinsic(objectFieldsEx),
+  objectHasEx:: $intrinsic(objectHasEx),
+  primitiveEquals:: $intrinsic(primitiveEquals),
+  modulo:: $intrinsic(modulo),
+  floor:: $intrinsic(floor),
+  log:: $intrinsic(log),
+  pow:: $intrinsic(pow),
+  extVar:: $intrinsic(extVar),
+  native:: $intrinsic(native),
+  filter:: $intrinsic(filter),
+  char:: $intrinsic(char),
+  encodeUTF8:: $intrinsic(encodeUTF8),
+  md5:: $intrinsic(md5),
+  trace:: $intrinsic(trace),
+  id:: $intrinsic(id),
+  parseJson:: $intrinsic(parseJson),
+
   isString(v):: std.type(v) == 'string',
   isNumber(v):: std.type(v) == 'number',
   isBoolean(v):: std.type(v) == 'boolean',
@@ -109,37 +129,8 @@
       else
         aux(str, delim, i2, arr, v + c) tailstrict;
     aux(str, c, 0, [], ''),
-
-  strReplace(str, from, to)::
-    assert std.isString(str);
-    assert std.isString(from);
-    assert std.isString(to);
-    assert from != '' : "'from' string must not be zero length.";
 
-    // Cache for performance.
-    local str_len = std.length(str);
-    local from_len = std.length(from);
-
-    // True if from is at str[i].
-    local found_at(i) = str[i:i + from_len] == from;
-
-    // Return the remainder of 'str' starting with 'start_index' where
-    // all occurrences of 'from' after 'curr_index' are replaced with 'to'.
-    local replace_after(start_index, curr_index, acc) =
-      if curr_index > str_len then
-        acc + str[start_index:curr_index]
-      else if found_at(curr_index) then
-        local new_index = curr_index + std.length(from);
-        replace_after(new_index, new_index, acc + str[start_index:curr_index] + to) tailstrict
-      else
-        replace_after(start_index, curr_index + 1, acc) tailstrict;
-
-    // if from_len==1, then we replace by splitting and rejoining the
-    // string which is much faster than recursing on replace_after
-    if from_len == 1 then
-      std.join(to, std.split(str, from))
-    else
-      replace_after(0, 0, ''),
+  strReplace:: $intrinsic(strReplace),
 
   asciiUpper(str)::
     local cp = std.codepoint;
@@ -157,8 +148,7 @@
       c;
     std.join('', std.map(down_letter, std.stringChars(str))),
 
-  range(from, to)::
-    std.makeArray(to - from + 1, function(i) i + from),
+  range:: $intrinsic(range),
 
   repeat(what, count)::
     local joiner =
@@ -167,38 +157,7 @@
       else error 'std.repeat first argument must be an array or a string';
     std.join(joiner, std.makeArray(count, function(i) what)),
 
-  slice(indexable, index, end, step)::
-    local invar =
-      // loop invariant with defaults applied
-      {
-        indexable: indexable,
-        index:
-          if index == null then 0
-          else index,
-        end:
-          if end == null then std.length(indexable)
-          else end,
-        step:
-          if step == null then 1
-          else step,
-        length: std.length(indexable),
-        type: std.type(indexable),
-      };
-    assert invar.index >= 0 && invar.end >= 0 && invar.step >= 0 : 'got [%s:%s:%s] but negative index, end, and steps are not supported' % [invar.index, invar.end, invar.step];
-    assert step != 0 : 'got %s but step must be greater than 0' % step;
-    assert std.isString(indexable) || std.isArray(indexable) : 'std.slice accepts a string or an array, but got: %s' % std.type(indexable);
-    local build(slice, cur) =
-      if cur >= invar.end || cur >= invar.length then
-        slice
-      else
-        build(
-          if invar.type == 'string' then
-            slice + invar.indexable[cur]
-          else
-            slice + [invar.indexable[cur]],
-          cur + invar.step
-        ) tailstrict;
-    build(if invar.type == 'string' then '' else [], invar.index),
+  slice:: $intrinsic(slice),
 
   member(arr, x)::
     if std.isArray(arr) then
@@ -209,21 +168,9 @@
 
   count(arr, x):: std.length(std.filter(function(v) v == x, arr)),
 
-  mod(a, b)::
-    if std.isNumber(a) && std.isNumber(b) then
-      std.modulo(a, b)
-    else if std.isString(a) then
-      std.format(a, b)
-    else
-      error 'Operator % cannot be used on types ' + std.type(a) + ' and ' + std.type(b) + '.',
+  mod:: $intrinsic(mod),
 
-  map(func, arr)::
-    if !std.isFunction(func) then
-      error ('std.map first param must be function, got ' + std.type(func))
-    else if !std.isArray(arr) && !std.isString(arr) then
-      error ('std.map second param must be array / string, got ' + std.type(arr))
-    else
-      std.makeArray(std.length(arr), function(i) func(arr[i])),
+  map:: $intrinsic(map),
 
   mapWithIndex(func, arr)::
     if !std.isFunction(func) then
@@ -250,26 +197,7 @@
       std.join('', std.makeArray(std.length(arr), function(i) func(arr[i])))
     else error ('std.flatMap second param must be array / string, got ' + std.type(arr)),
 
-  join(sep, arr)::
-    local aux(arr, i, first, running) =
-      if i >= std.length(arr) then
-        running
-      else if arr[i] == null then
-        aux(arr, i + 1, first, running) tailstrict
-      else if std.type(arr[i]) != std.type(sep) then
-        error 'expected %s but arr[%d] was %s ' % [std.type(sep), i, std.type(arr[i])]
-      else if first then
-        aux(arr, i + 1, false, running + arr[i]) tailstrict
-      else
-        aux(arr, i + 1, false, running + sep + arr[i]) tailstrict;
-    if !std.isArray(arr) then
-      error 'join second parameter should be array, got ' + std.type(arr)
-    else if std.isString(sep) then
-      aux(arr, 0, true, '')
-    else if std.isArray(sep) then
-      aux(arr, 0, true, [])
-    else
-      error 'join first parameter should be string or array, got ' + std.type(sep),
+  join:: $intrinsic(join),
 
   lines(arr)::
     std.join('\n', arr + ['']),
@@ -281,479 +209,14 @@
       std.join('', [std.deepJoin(x) for x in arr])
     else
       error 'Expected string or array, got %s' % std.type(arr),
-
-
-  format(str, vals)::
-
-    /////////////////////////////
-    // Parse the mini-language //
-    /////////////////////////////
-
-    local try_parse_mapping_key(str, i) =
-      assert i < std.length(str) : 'Truncated format code.';
-      local c = str[i];
-      if c == '(' then
-        local consume(str, j, v) =
-          if j >= std.length(str) then
-            error 'Truncated format code.'
-          else
-            local c = str[j];
-            if c != ')' then
-              consume(str, j + 1, v + c)
-            else
-              { i: j + 1, v: v };
-        consume(str, i + 1, '')
-      else
-        { i: i, v: null };
 
-    local try_parse_cflags(str, i) =
-      local consume(str, j, v) =
-        assert j < std.length(str) : 'Truncated format code.';
-        local c = str[j];
-        if c == '#' then
-          consume(str, j + 1, v { alt: true })
-        else if c == '0' then
-          consume(str, j + 1, v { zero: true })
-        else if c == '-' then
-          consume(str, j + 1, v { left: true })
-        else if c == ' ' then
-          consume(str, j + 1, v { blank: true })
-        else if c == '+' then
-          consume(str, j + 1, v { sign: true })
-        else
-          { i: j, v: v };
-      consume(str, i, { alt: false, zero: false, left: false, blank: false, sign: false });
 
-    local try_parse_field_width(str, i) =
-      if i < std.length(str) && str[i] == '*' then
-        { i: i + 1, v: '*' }
-      else
-        local consume(str, j, v) =
-          assert j < std.length(str) : 'Truncated format code.';
-          local c = str[j];
-          if c == '0' then
-            consume(str, j + 1, v * 10 + 0)
-          else if c == '1' then
-            consume(str, j + 1, v * 10 + 1)
-          else if c == '2' then
-            consume(str, j + 1, v * 10 + 2)
-          else if c == '3' then
-            consume(str, j + 1, v * 10 + 3)
-          else if c == '4' then
-            consume(str, j + 1, v * 10 + 4)
-          else if c == '5' then
-            consume(str, j + 1, v * 10 + 5)
-          else if c == '6' then
-            consume(str, j + 1, v * 10 + 6)
-          else if c == '7' then
-            consume(str, j + 1, v * 10 + 7)
-          else if c == '8' then
-            consume(str, j + 1, v * 10 + 8)
-          else if c == '9' then
-            consume(str, j + 1, v * 10 + 9)
-          else
-            { i: j, v: v };
-        consume(str, i, 0);
-
-    local try_parse_precision(str, i) =
-      assert i < std.length(str) : 'Truncated format code.';
-      local c = str[i];
-      if c == '.' then
-        try_parse_field_width(str, i + 1)
-      else
-        { i: i, v: null };
-
-    // Ignored, if it exists.
-    local try_parse_length_modifier(str, i) =
-      assert i < std.length(str) : 'Truncated format code.';
-      local c = str[i];
-      if c == 'h' || c == 'l' || c == 'L' then
-        i + 1
-      else
-        i;
-
-    local parse_conv_type(str, i) =
-      assert i < std.length(str) : 'Truncated format code.';
-      local c = str[i];
-      if c == 'd' || c == 'i' || c == 'u' then
-        { i: i + 1, v: 'd', caps: false }
-      else if c == 'o' then
-        { i: i + 1, v: 'o', caps: false }
-      else if c == 'x' then
-        { i: i + 1, v: 'x', caps: false }
-      else if c == 'X' then
-        { i: i + 1, v: 'x', caps: true }
-      else if c == 'e' then
-        { i: i + 1, v: 'e', caps: false }
-      else if c == 'E' then
-        { i: i + 1, v: 'e', caps: true }
-      else if c == 'f' then
-        { i: i + 1, v: 'f', caps: false }
-      else if c == 'F' then
-        { i: i + 1, v: 'f', caps: true }
-      else if c == 'g' then
-        { i: i + 1, v: 'g', caps: false }
-      else if c == 'G' then
-        { i: i + 1, v: 'g', caps: true }
-      else if c == 'c' then
-        { i: i + 1, v: 'c', caps: false }
-      else if c == 's' then
-        { i: i + 1, v: 's', caps: false }
-      else if c == '%' then
-        { i: i + 1, v: '%', caps: false }
-      else
-        error 'Unrecognised conversion type: ' + c;
-
-
-    // Parsed initial %, now the rest.
-    local parse_code(str, i) =
-      assert i < std.length(str) : 'Truncated format code.';
-      local mkey = try_parse_mapping_key(str, i);
-      local cflags = try_parse_cflags(str, mkey.i);
-      local fw = try_parse_field_width(str, cflags.i);
-      local prec = try_parse_precision(str, fw.i);
-      local len_mod = try_parse_length_modifier(str, prec.i);
-      local ctype = parse_conv_type(str, len_mod);
-      {
-        i: ctype.i,
-        code: {
-          mkey: mkey.v,
-          cflags: cflags.v,
-          fw: fw.v,
-          prec: prec.v,
-          ctype: ctype.v,
-          caps: ctype.caps,
-        },
-      };
-
-    // Parse a format string (containing none or more % format tags).
-    local parse_codes(str, i, out, cur) =
-      if i >= std.length(str) then
-        out + [cur]
-      else
-        local c = str[i];
-        if c == '%' then
-          local r = parse_code(str, i + 1);
-          parse_codes(str, r.i, out + [cur, r.code], '') tailstrict
-        else
-          parse_codes(str, i + 1, out, cur + c) tailstrict;
-
-    local codes = parse_codes(str, 0, [], '');
-
-
-    ///////////////////////
-    // Format the values //
-    ///////////////////////
-
-    // Useful utilities
-    local padding(w, s) =
-      local aux(w, v) =
-        if w <= 0 then
-          v
-        else
-          aux(w - 1, v + s);
-      aux(w, '');
-
-    // Add s to the left of str so that its length is at least w.
-    local pad_left(str, w, s) =
-      padding(w - std.length(str), s) + str;
-
-    // Add s to the right of str so that its length is at least w.
-    local pad_right(str, w, s) =
-      str + padding(w - std.length(str), s);
-
-    // Render an integer (e.g., decimal or octal).
-    local render_int(n__, min_chars, min_digits, blank, sign, radix, zero_prefix) =
-      local n_ = std.abs(n__);
-      local aux(n) =
-        if n == 0 then
-          zero_prefix
-        else
-          aux(std.floor(n / radix)) + (n % radix);
-      local dec = if std.floor(n_) == 0 then '0' else aux(std.floor(n_));
-      local neg = n__ < 0;
-      local zp = min_chars - (if neg || blank || sign then 1 else 0);
-      local zp2 = std.max(zp, min_digits);
-      local dec2 = pad_left(dec, zp2, '0');
-      (if neg then '-' else if sign then '+' else if blank then ' ' else '') + dec2;
-
-    // Render an integer in hexadecimal.
-    local render_hex(n__, min_chars, min_digits, blank, sign, add_zerox, capitals) =
-      local numerals = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
-                       + if capitals then ['A', 'B', 'C', 'D', 'E', 'F']
-                       else ['a', 'b', 'c', 'd', 'e', 'f'];
-      local n_ = std.abs(n__);
-      local aux(n) =
-        if n == 0 then
-          ''
-        else
-          aux(std.floor(n / 16)) + numerals[n % 16];
-      local hex = if std.floor(n_) == 0 then '0' else aux(std.floor(n_));
-      local neg = n__ < 0;
-      local zp = min_chars - (if neg || blank || sign then 1 else 0)
-                 - (if add_zerox then 2 else 0);
-      local zp2 = std.max(zp, min_digits);
-      local hex2 = (if add_zerox then (if capitals then '0X' else '0x') else '')
-                   + pad_left(hex, zp2, '0');
-      (if neg then '-' else if sign then '+' else if blank then ' ' else '') + hex2;
-
-    local strip_trailing_zero(str) =
-      local aux(str, i) =
-        if i < 0 then
-          ''
-        else
-          if str[i] == '0' then
-            aux(str, i - 1)
-          else
-            std.substr(str, 0, i + 1);
-      aux(str, std.length(str) - 1);
-
-    // Render floating point in decimal form
-    local render_float_dec(n__, zero_pad, blank, sign, ensure_pt, trailing, prec) =
-      local n_ = std.abs(n__);
-      local whole = std.floor(n_);
-      local dot_size = if prec == 0 && !ensure_pt then 0 else 1;
-      local zp = zero_pad - prec - dot_size;
-      local str = render_int(std.sign(n__) * whole, zp, 0, blank, sign, 10, '');
-      if prec == 0 then
-        str + if ensure_pt then '.' else ''
-      else
-        local frac = std.floor((n_ - whole) * std.pow(10, prec) + 0.5);
-        if trailing || frac > 0 then
-          local frac_str = render_int(frac, prec, 0, false, false, 10, '');
-          str + '.' + if !trailing then strip_trailing_zero(frac_str) else frac_str
-        else
-          str;
-
-    // Render floating point in scientific form
-    local render_float_sci(n__, zero_pad, blank, sign, ensure_pt, trailing, caps, prec) =
-      local exponent = if n__ == 0 then 0 else std.floor(std.log(std.abs(n__)) / std.log(10));
-      local suff = (if caps then 'E' else 'e')
-                   + render_int(exponent, 3, 0, false, true, 10, '');
-      local mantissa = if exponent == -324 then
-        // Avoid a rounding error where std.pow(10, -324) is 0
-        // -324 is the smallest exponent possible.
-        n__ * 10 / std.pow(10, exponent + 1)
-      else
-        n__ / std.pow(10, exponent);
-      local zp2 = zero_pad - std.length(suff);
-      render_float_dec(mantissa, zp2, blank, sign, ensure_pt, trailing, prec) + suff;
-
-    // Render a value with an arbitrary format code.
-    local format_code(val, code, fw, prec_or_null, i) =
-      local cflags = code.cflags;
-      local fpprec = if prec_or_null != null then prec_or_null else 6;
-      local iprec = if prec_or_null != null then prec_or_null else 0;
-      local zp = if cflags.zero && !cflags.left then fw else 0;
-      if code.ctype == 's' then
-        std.toString(val)
-      else if code.ctype == 'd' then
-        if std.type(val) != 'number' then
-          error 'Format required number at '
-                + i + ', got ' + std.type(val)
-        else
-          render_int(val, zp, iprec, cflags.blank, cflags.sign, 10, '')
-      else if code.ctype == 'o' then
-        if std.type(val) != 'number' then
-          error 'Format required number at '
-                + i + ', got ' + std.type(val)
-        else
-          local zero_prefix = if cflags.alt then '0' else '';
-          render_int(val, zp, iprec, cflags.blank, cflags.sign, 8, zero_prefix)
-      else if code.ctype == 'x' then
-        if std.type(val) != 'number' then
-          error 'Format required number at '
-                + i + ', got ' + std.type(val)
-        else
-          render_hex(val,
-                     zp,
-                     iprec,
-                     cflags.blank,
-                     cflags.sign,
-                     cflags.alt,
-                     code.caps)
-      else if code.ctype == 'f' then
-        if std.type(val) != 'number' then
-          error 'Format required number at '
-                + i + ', got ' + std.type(val)
-        else
-          render_float_dec(val,
-                           zp,
-                           cflags.blank,
-                           cflags.sign,
-                           cflags.alt,
-                           true,
-                           fpprec)
-      else if code.ctype == 'e' then
-        if std.type(val) != 'number' then
-          error 'Format required number at '
-                + i + ', got ' + std.type(val)
-        else
-          render_float_sci(val,
-                           zp,
-                           cflags.blank,
-                           cflags.sign,
-                           cflags.alt,
-                           true,
-                           code.caps,
-                           fpprec)
-      else if code.ctype == 'g' then
-        if std.type(val) != 'number' then
-          error 'Format required number at '
-                + i + ', got ' + std.type(val)
-        else
-          local exponent = std.floor(std.log(std.abs(val)) / std.log(10));
-          if exponent < -4 || exponent >= fpprec then
-            render_float_sci(val,
-                             zp,
-                             cflags.blank,
-                             cflags.sign,
-                             cflags.alt,
-                             cflags.alt,
-                             code.caps,
-                             fpprec - 1)
-          else
-            local digits_before_pt = std.max(1, exponent + 1);
-            render_float_dec(val,
-                             zp,
-                             cflags.blank,
-                             cflags.sign,
-                             cflags.alt,
-                             cflags.alt,
-                             fpprec - digits_before_pt)
-      else if code.ctype == 'c' then
-        if std.type(val) == 'number' then
-          std.char(val)
-        else if std.type(val) == 'string' then
-          if std.length(val) == 1 then
-            val
-          else
-            error '%c expected 1-sized string got: ' + std.length(val)
-        else
-          error '%c expected number / string, got: ' + std.type(val)
-      else
-        error 'Unknown code: ' + code.ctype;
+  format:: $intrinsic(format),
 
-    // Render a parsed format string with an array of values.
-    local format_codes_arr(codes, arr, i, j, v) =
-      if i >= std.length(codes) then
-        if j < std.length(arr) then
-          error ('Too many values to format: ' + std.length(arr) + ', expected ' + j)
-        else
-          v
-      else
-        local code = codes[i];
-        if std.type(code) == 'string' then
-          format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict
-        else
-          local tmp = if code.fw == '*' then {
-            j: j + 1,
-            fw: if j >= std.length(arr) then
-              error ('Not enough values to format: ' + std.length(arr) + ', expected at least ' + j)
-            else
-              arr[j],
-          } else {
-            j: j,
-            fw: code.fw,
-          };
-          local tmp2 = if code.prec == '*' then {
-            j: tmp.j + 1,
-            prec: if tmp.j >= std.length(arr) then
-              error ('Not enough values to format: ' + std.length(arr) + ', expected at least ' + tmp.j)
-            else
-              arr[tmp.j],
-          } else {
-            j: tmp.j,
-            prec: code.prec,
-          };
-          local j2 = tmp2.j;
-          local val =
-            if j2 < std.length(arr) then
-              arr[j2]
-            else
-              error ('Not enough values to format: ' + std.length(arr) + ', expected more than ' + j2);
-          local s =
-            if code.ctype == '%' then
-              '%'
-            else
-              format_code(val, code, tmp.fw, tmp2.prec, j2);
-          local s_padded =
-            if code.cflags.left then
-              pad_right(s, tmp.fw, ' ')
-            else
-              pad_left(s, tmp.fw, ' ');
-          local j3 =
-            if code.ctype == '%' then
-              j2
-            else
-              j2 + 1;
-          format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;
+  foldr:: $intrinsic(foldr),
 
-    // Render a parsed format string with an object of values.
-    local format_codes_obj(codes, obj, i, v) =
-      if i >= std.length(codes) then
-        v
-      else
-        local code = codes[i];
-        if std.type(code) == 'string' then
-          format_codes_obj(codes, obj, i + 1, v + code) tailstrict
-        else
-          local f =
-            if code.mkey == null then
-              error 'Mapping keys required.'
-            else
-              code.mkey;
-          local fw =
-            if code.fw == '*' then
-              error 'Cannot use * field width with object.'
-            else
-              code.fw;
-          local prec =
-            if code.prec == '*' then
-              error 'Cannot use * precision with object.'
-            else
-              code.prec;
-          local val =
-            if std.objectHasAll(obj, f) then
-              obj[f]
-            else
-              error 'No such field: ' + f;
-          local s =
-            if code.ctype == '%' then
-              '%'
-            else
-              format_code(val, code, fw, prec, f);
-          local s_padded =
-            if code.cflags.left then
-              pad_right(s, fw, ' ')
-            else
-              pad_left(s, fw, ' ');
-          format_codes_obj(codes, obj, i + 1, v + s_padded) tailstrict;
-
-    if std.isArray(vals) then
-      format_codes_arr(codes, vals, 0, 0, '')
-    else if std.isObject(vals) then
-      format_codes_obj(codes, vals, 0, '')
-    else
-      format_codes_arr(codes, [vals], 0, 0, ''),
-
-  foldr(func, arr, init)::
-    local aux(func, arr, running, idx) =
-      if idx < 0 then
-        running
-      else
-        aux(func, arr, func(arr[idx], running), idx - 1) tailstrict;
-    aux(func, arr, init, std.length(arr) - 1),
+  foldl:: $intrinsic(foldl),
 
-  foldl(func, arr, init)::
-    local aux(func, arr, running, idx) =
-      if idx >= std.length(arr) then
-        running
-      else
-        aux(func, arr, func(running, arr[idx]), idx + 1) tailstrict;
-    aux(func, arr, init, 0),
-
-
   filterMap(filter_func, map_func, arr)::
     if !std.isFunction(filter_func) then
       error ('std.filterMap first param must be function, got ' + std.type(filter_func))
@@ -912,30 +375,7 @@
     else
       error 'TOML body must be an object. Got ' + std.type(value),
 
-  escapeStringJson(str_)::
-    local str = std.toString(str_);
-    local trans(ch) =
-      if ch == '"' then
-        '\\"'
-      else if ch == '\\' then
-        '\\\\'
-      else if ch == '\b' then
-        '\\b'
-      else if ch == '\f' then
-        '\\f'
-      else if ch == '\n' then
-        '\\n'
-      else if ch == '\r' then
-        '\\r'
-      else if ch == '\t' then
-        '\\t'
-      else
-        local cp = std.codepoint(ch);
-        if cp < 32 || (cp >= 127 && cp <= 159) then
-          '\\u%04x' % [cp]
-        else
-          ch;
-    '"%s"' % std.join('', [trans(ch) for ch in std.stringChars(str)]),
+  escapeStringJson:: $intrinsic(escapeStringJson),
 
   escapeStringPython(str)::
     std.escapeStringJson(str),
@@ -960,42 +400,7 @@
 
   manifestJson(value):: std.manifestJsonEx(value, '    '),
 
-  manifestJsonEx(value, indent)::
-    local aux(v, path, cindent) =
-      if v == true then
-        'true'
-      else if v == false then
-        'false'
-      else if v == null then
-        'null'
-      else if std.isNumber(v) then
-        '' + v
-      else if std.isString(v) then
-        std.escapeStringJson(v)
-      else if std.isFunction(v) then
-        error 'Tried to manifest function at ' + path
-      else if std.isArray(v) then
-        local range = std.range(0, std.length(v) - 1);
-        local new_indent = cindent + indent;
-        local lines = ['[\n']
-                      + std.join([',\n'],
-                                 [
-                                   [new_indent + aux(v[i], path + [i], new_indent)]
-                                   for i in range
-                                 ])
-                      + ['\n' + cindent + ']'];
-        std.join('', lines)
-      else if std.isObject(v) then
-        local lines = ['{\n']
-                      + std.join([',\n'],
-                                 [
-                                   [cindent + indent + std.escapeStringJson(k) + ': '
-                                    + aux(v[k], path + [k], cindent + indent)]
-                                   for k in std.objectFields(v)
-                                 ])
-                      + ['\n' + cindent + '}'];
-        std.join('', lines);
-    aux(value, [], ''),
+  manifestJsonEx:: $intrinsic(manifestJsonEx),
 
   manifestYamlDoc(value, indent_array_in_object=false)::
     local aux(v, path, cindent) =
@@ -1136,52 +541,7 @@
   local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
   local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },
 
-  base64(input)::
-    local bytes =
-      if std.isString(input) then
-        std.map(function(c) std.codepoint(c), input)
-      else
-        input;
-
-    local aux(arr, i, r) =
-      if i >= std.length(arr) then
-        r
-      else if i + 1 >= std.length(arr) then
-        local str =
-          // 6 MSB of i
-          base64_table[(arr[i] & 252) >> 2] +
-          // 2 LSB of i
-          base64_table[(arr[i] & 3) << 4] +
-          '==';
-        aux(arr, i + 3, r + str) tailstrict
-      else if i + 2 >= std.length(arr) then
-        local str =
-          // 6 MSB of i
-          base64_table[(arr[i] & 252) >> 2] +
-          // 2 LSB of i, 4 MSB of i+1
-          base64_table[(arr[i] & 3) << 4 | (arr[i + 1] & 240) >> 4] +
-          // 4 LSB of i+1
-          base64_table[(arr[i + 1] & 15) << 2] +
-          '=';
-        aux(arr, i + 3, r + str) tailstrict
-      else
-        local str =
-          // 6 MSB of i
-          base64_table[(arr[i] & 252) >> 2] +
-          // 2 LSB of i, 4 MSB of i+1
-          base64_table[(arr[i] & 3) << 4 | (arr[i + 1] & 240) >> 4] +
-          // 4 LSB of i+1, 2 MSB of i+2
-          base64_table[(arr[i + 1] & 15) << 2 | (arr[i + 2] & 192) >> 6] +
-          // 6 LSB of i+2
-          base64_table[(arr[i + 2] & 63)];
-        aux(arr, i + 3, r + str) tailstrict;
-
-    local sanity = std.foldl(function(r, a) r && (a < 256), bytes, true);
-    if !sanity then
-      error 'Can only base64 encode strings / arrays of single bytes.'
-    else
-      aux(bytes, 0, ''),
-
+  base64:: $intrinsic(base64),
 
   base64DecodeBytes(str)::
     if std.length(str) % 4 != 0 then
@@ -1207,47 +567,11 @@
   base64Decode(str)::
     local bytes = std.base64DecodeBytes(str);
     std.join('', std.map(function(b) std.char(b), bytes)),
-
-  reverse(arr)::
-    local l = std.length(arr);
-    std.makeArray(l, function(i) arr[l - i - 1]),
 
-  // Merge-sort for long arrays and naive quicksort for shorter ones
-  sortImpl(arr, keyF)::
-    local quickSort(arr, keyF=id) =
-      local l = std.length(arr);
-      if std.length(arr) <= 1 then
-        arr
-      else
-        local pos = 0;
-        local pivot = keyF(arr[pos]);
-        local rest = std.makeArray(l - 1, function(i) if i < pos then arr[i] else arr[i + 1]);
-        local left = std.filter(function(x) keyF(x) < pivot, rest);
-        local right = std.filter(function(x) keyF(x) >= pivot, rest);
-        quickSort(left, keyF) + [arr[pos]] + quickSort(right, keyF);
+  reverse:: $intrinsic(reverse),
 
-    local merge(a, b) =
-      local la = std.length(a), lb = std.length(b);
-      local aux(i, j, prefix) =
-        if i == la then
-          prefix + b[j:]
-        else if j == lb then
-          prefix + a[i:]
-        else
-          if keyF(a[i]) <= keyF(b[j]) then
-            aux(i + 1, j, prefix + [a[i]]) tailstrict
-          else
-            aux(i, j + 1, prefix + [b[j]]) tailstrict;
-      aux(0, 0, []);
+  sortImpl:: $intrinsic(sortImpl),
 
-    local l = std.length(arr);
-    if std.length(arr) <= 30 then
-      quickSort(arr, keyF=keyF)
-    else
-      local mid = std.floor(l / 2);
-      local left = arr[:mid], right = arr[mid:];
-      merge(std.sort(left, keyF=keyF), std.sort(right, keyF=keyF)),
-
   sort(arr, keyF=id)::
     std.sortImpl(arr, keyF),
 
@@ -1356,42 +680,7 @@
   objectValuesAll(o)::
     [o[k] for k in std.objectFieldsAll(o)],
 
-  equals(a, b)::
-    local ta = std.type(a);
-    local tb = std.type(b);
-    if !std.primitiveEquals(ta, tb) then
-      false
-    else
-      if std.primitiveEquals(ta, 'array') then
-        local la = std.length(a);
-        if !std.primitiveEquals(la, std.length(b)) then
-          false
-        else
-          local aux(a, b, i) =
-            if i >= la then
-              true
-            else if a[i] != b[i] then
-              false
-            else
-              aux(a, b, i + 1) tailstrict;
-          aux(a, b, 0)
-      else if std.primitiveEquals(ta, 'object') then
-        local fields = std.objectFields(a);
-        local lfields = std.length(fields);
-        if fields != std.objectFields(b) then
-          false
-        else
-          local aux(a, b, i) =
-            if i >= lfields then
-              true
-            else if local f = fields[i]; a[f] != b[f] then
-              false
-            else
-              aux(a, b, i + 1) tailstrict;
-          aux(a, b, 0)
-      else
-        std.primitiveEquals(a, b),
-
+  equals:: $intrinsic(equals),
 
   resolvePath(f, r)::
     local arr = std.split(f, '/');