git.delta.rocks / jrsonnet / refs/commits / ab6ba9935b92

difftreelog

refactor(evaluator)! remove standard library

Yaroslav Bolyukin2022-07-23parent: #34a152f.patch.diff
in: master
Implementation will be moved to jrsonnet-stdlib crate

BREAKING CHANGE: `State::with_stdlib` was removed

9 files changed

modifiedcrates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -7,9 +7,7 @@
 edition = "2021"
 
 [features]
-default = ["serialized-stdlib", "explaining-traces", "friendly-errors"]
-# Serializes standard library AST instead of parsing them every run
-serialized-stdlib = ["bincode", "jrsonnet-parser/serde"]
+default = ["explaining-traces", "friendly-errors"]
 # Rustc-like trace visualization
 explaining-traces = ["annotate-snippets"]
 # Allows library authors to throw custom errors
@@ -23,11 +21,12 @@
 exp-serde-preserve-order = ["serde_json/preserve_order"]
 # Implements field destructuring
 exp-destruct = ["jrsonnet-parser/exp-destruct"]
+# Provide Typed for conversions to/from serde_json::Value type
+serde_json = ["dep:serde_json"]
 
 [dependencies]
 jrsonnet-interner = { path = "../jrsonnet-interner", version = "0.4.2" }
 jrsonnet-parser = { path = "../jrsonnet-parser", version = "0.4.2" }
-jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "0.4.2" }
 jrsonnet-types = { path = "../jrsonnet-types", version = "0.4.2" }
 jrsonnet-macros = { path = "../jrsonnet-macros", version = "0.4.2" }
 jrsonnet-gcmodule = { version = "0.3.4" }
@@ -36,15 +35,13 @@
 hashbrown = "0.12.1"
 static_assertions = "1.1"
 
-md5 = "0.7.0"
-base64 = "0.13.0"
 rustc-hash = "1.1"
 
 thiserror = "1.0"
 
 serde = "1.0"
-serde_json = "1.0"
-serde_yaml_with_quirks = "0.8.24"
+# Optional integration
+serde_json = { version = "1.0.82", optional = true }
 
 anyhow = { version = "1.0", optional = true }
 # Friendly errors
@@ -53,9 +50,3 @@
 bincode = { version = "1.3", optional = true }
 # Explaining traces
 annotate-snippets = { version = "0.9.1", features = ["color"], optional = true }
-
-[build-dependencies]
-jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "0.4.2" }
-jrsonnet-parser = { path = "../jrsonnet-parser", version = "0.4.2" }
-serde = "1.0"
-bincode = "1.3"
deletedcrates/jrsonnet-evaluator/build.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/build.rs
+++ /dev/null
@@ -1,22 +0,0 @@
-use std::{borrow::Cow, env, fs::File, io::Write, path::Path};
-
-use bincode::serialize;
-use jrsonnet_parser::{parse, ParserSettings, Source};
-use jrsonnet_stdlib::STDLIB_STR;
-
-fn main() {
-	let parsed = parse(
-		STDLIB_STR,
-		&ParserSettings {
-			file_name: Source::new_virtual(Cow::Borrowed("<std>")),
-		},
-	)
-	.expect("parse");
-
-	{
-		let out_dir = env::var("OUT_DIR").unwrap();
-		let dest_path = Path::new(&out_dir).join("stdlib.bincode");
-		let mut f = File::create(&dest_path).unwrap();
-		f.write_all(&serialize(&parsed).unwrap()).unwrap();
-	}
-}
modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/error.rs
1use std::{fmt::Debug, path::PathBuf};23use jrsonnet_gcmodule::Trace;4use jrsonnet_interner::IStr;5use jrsonnet_parser::{BinaryOpType, ExprLocation, Source, UnaryOpType};6use jrsonnet_types::ValType;7use thiserror::Error;89use crate::{10	stdlib::{format::FormatError, sort::SortError},11	typed::TypeLocError,12};1314fn format_found(list: &[IStr], what: &str) -> String {15	if list.is_empty() {16		return String::new();17	}18	let mut out = String::new();19	out.push_str("\nThere is ");20	out.push_str(what);21	if list.len() > 1 {22		out.push('s');23	}24	out.push_str(" with similar name");25	if list.len() > 1 {26		out.push('s');27	}28	out.push_str(" present: ");29	for (i, v) in list.iter().enumerate() {30		if i != 0 {31			out.push_str(", ");32		}33		out.push_str(v as &str);34	}35	out36}3738const fn format_empty_str(str: &str) -> &str {39	if str.is_empty() {40		"\"\" (empty string)"41	} else {42		str43	}44}4546#[derive(Error, Debug, Clone, Trace)]47pub enum Error {48	#[error("intrinsic not found: {0}")]49	IntrinsicNotFound(IStr),5051	#[error("operator {0} does not operate on type {1}")]52	UnaryOperatorDoesNotOperateOnType(UnaryOpType, ValType),53	#[error("binary operation {1} {0} {2} is not implemented")]54	BinaryOperatorDoesNotOperateOnValues(BinaryOpType, ValType, ValType),5556	#[error("no top level object in this context")]57	NoTopLevelObjectFound,58	#[error("self is only usable inside objects")]59	CantUseSelfOutsideOfObject,60	#[error("no super found")]61	NoSuperFound,6263	#[error("for loop can only iterate over arrays")]64	InComprehensionCanOnlyIterateOverArray,6566	#[error("array out of bounds: {0} is not within [0,{1})")]67	ArrayBoundsError(usize, usize),68	#[error("string out of bounds: {0} is not within [0,{1})")]69	StringBoundsError(usize, usize),7071	#[error("assert failed: {}", format_empty_str(.0))]72	AssertionFailed(IStr),7374	#[error("variable is not defined: {0}{}", format_found(.1, "variable"))]75	VariableIsNotDefined(IStr, Vec<IStr>),76	#[error("duplicate local var: {0}")]77	DuplicateLocalVar(IStr),7879	#[error("type mismatch: expected {}, got {2} {0}", .1.iter().map(|e| format!("{}", e)).collect::<Vec<_>>().join(", "))]80	TypeMismatch(&'static str, Vec<ValType>, ValType),81	#[error("no such field: {}{}", format_empty_str(.0), format_found(.1, "field"))]82	NoSuchField(IStr, Vec<IStr>),8384	#[error("only functions can be called, got {0}")]85	OnlyFunctionsCanBeCalledGot(ValType),86	#[error("parameter {0} is not defined")]87	UnknownFunctionParameter(String),88	#[error("argument {0} is already bound")]89	BindingParameterASecondTime(IStr),90	#[error("too many args, function has {0}")]91	TooManyArgsFunctionHas(usize),92	#[error("function argument is not passed: {0}")]93	FunctionParameterNotBoundInCall(IStr),9495	#[error("external variable is not defined: {0}")]96	UndefinedExternalVariable(IStr),9798	#[error("field name should be string, got {0}")]99	FieldMustBeStringGot(ValType),100	#[error("duplicate field name: {}", format_empty_str(.0))]101	DuplicateFieldName(IStr),102103	#[error("attempted to index array with string {}", format_empty_str(.0))]104	AttemptedIndexAnArrayWithString(IStr),105	#[error("{0} index type should be {1}, got {2}")]106	ValueIndexMustBeTypeGot(ValType, ValType, ValType),107	#[error("cant index into {0}")]108	CantIndexInto(ValType),109	#[error("{0} is not indexable")]110	ValueIsNotIndexable(ValType),111112	#[error("super can't be used standalone")]113	StandaloneSuper,114115	#[error("can't resolve {1} from {0}")]116	ImportFileNotFound(PathBuf, String),117	#[error("resolved file not found: {0}")]118	ResolvedFileNotFound(PathBuf),119	#[error("imported file is not valid utf-8: {0:?}")]120	ImportBadFileUtf8(PathBuf),121	#[error("import io error: {0}")]122	ImportIo(String),123	#[error("tried to import {1} from {0}, but imports is not supported")]124	ImportNotSupported(PathBuf, PathBuf),125	#[error("can't import from virtual file")]126	CantImportFromVirtualFile,127	#[error(128		"syntax error: expected {}, got {:?}",129		.error.expected,130		.source_code.chars().nth(error.location.offset)131		.map_or_else(|| "EOF".into(), |c| c.to_string())132	)]133	ImportSyntaxError {134		path: Source,135		source_code: IStr,136		#[trace(skip)]137		error: Box<jrsonnet_parser::ParseError>,138	},139140	#[error("runtime error: {}", format_empty_str(.0))]141	RuntimeError(IStr),142	#[error("stack overflow, try to reduce recursion, or set --max-stack to bigger value")]143	StackOverflow,144	#[error("infinite recursion detected")]145	InfiniteRecursionDetected,146	#[error("tried to index by fractional value")]147	FractionalIndex,148	#[error("attempted to divide by zero")]149	DivisionByZero,150151	#[error("string manifest output is not an string")]152	StringManifestOutputIsNotAString,153	#[error("stream manifest output is not an array")]154	StreamManifestOutputIsNotAArray,155	#[error("multi manifest output is not an object")]156	MultiManifestOutputIsNotAObject,157158	#[error("cant recurse stream manifest")]159	StreamManifestOutputCannotBeRecursed,160	#[error("stream manifest output cannot consist of raw strings")]161	StreamManifestCannotNestString,162163	#[error("{}", format_empty_str(.0))]164	ImportCallbackError(String),165	#[error("invalid unicode codepoint: {0}")]166	InvalidUnicodeCodepointGot(u32),167168	#[error("format error: {0}")]169	Format(#[from] FormatError),170	#[error("type error: {0}")]171	TypeError(TypeLocError),172	#[error("sort error: {0}")]173	Sort(#[from] SortError),174175	/// Thrown as error, as this is legacy feature, and error here176	/// is acceptable for defeating object field cache177	#[error("should not reach outside: std.thisFile")]178	MagicThisFileUsed,179180	#[cfg(feature = "anyhow-error")]181	#[error(transparent)]182	Other(Rc<anyhow::Error>),183}184185#[cfg(feature = "anyhow-error")]186impl From<anyhow::Error> for LocError {187	fn from(e: anyhow::Error) -> Self {188		Self::new(Error::Other(Rc::new(e)))189	}190}191192impl From<Error> for LocError {193	fn from(e: Error) -> Self {194		Self::new(e)195	}196}197198#[derive(Clone, Debug, Trace)]199pub struct StackTraceElement {200	pub location: Option<ExprLocation>,201	pub desc: String,202}203#[derive(Debug, Clone, Trace)]204pub struct StackTrace(pub Vec<StackTraceElement>);205206#[derive(Clone, Trace)]207pub struct LocError(Box<(Error, StackTrace)>);208impl LocError {209	pub fn new(e: Error) -> Self {210		Self(Box::new((e, StackTrace(vec![]))))211	}212213	pub const fn error(&self) -> &Error {214		&(self.0).0215	}216	pub fn error_mut(&mut self) -> &mut Error {217		&mut (self.0).0218	}219	pub const fn trace(&self) -> &StackTrace {220		&(self.0).1221	}222	pub fn trace_mut(&mut self) -> &mut StackTrace {223		&mut (self.0).1224	}225}226impl Debug for LocError {227	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {228		writeln!(f, "{}", self.0 .0)?;229		for el in &self.0 .1 .0 {230			writeln!(f, "\t{:?}", el)?;231		}232		Ok(())233	}234}235236pub type Result<V, E = LocError> = std::result::Result<V, E>;237238#[macro_export]239macro_rules! throw {240	($e: expr) => {241		return Err($e.into())242	};243}244245#[macro_export]246macro_rules! throw_runtime {247	($($tt:tt)*) => {248		return Err($crate::error::Error::RuntimeError(format!($($tt)*).into()).into())249	};250}
after · crates/jrsonnet-evaluator/src/error.rs
1use std::{fmt::Debug, path::PathBuf};23use jrsonnet_gcmodule::Trace;4use jrsonnet_interner::IStr;5use jrsonnet_parser::{BinaryOpType, ExprLocation, Source, UnaryOpType};6use jrsonnet_types::ValType;7use thiserror::Error;89use crate::{stdlib::format::FormatError, typed::TypeLocError};1011fn format_found(list: &[IStr], what: &str) -> String {12	if list.is_empty() {13		return String::new();14	}15	let mut out = String::new();16	out.push_str("\nThere is ");17	out.push_str(what);18	if list.len() > 1 {19		out.push('s');20	}21	out.push_str(" with similar name");22	if list.len() > 1 {23		out.push('s');24	}25	out.push_str(" present: ");26	for (i, v) in list.iter().enumerate() {27		if i != 0 {28			out.push_str(", ");29		}30		out.push_str(v as &str);31	}32	out33}3435const fn format_empty_str(str: &str) -> &str {36	if str.is_empty() {37		"\"\" (empty string)"38	} else {39		str40	}41}4243#[derive(Error, Debug, Clone, Trace)]44pub enum Error {45	#[error("intrinsic not found: {0}")]46	IntrinsicNotFound(IStr),4748	#[error("operator {0} does not operate on type {1}")]49	UnaryOperatorDoesNotOperateOnType(UnaryOpType, ValType),50	#[error("binary operation {1} {0} {2} is not implemented")]51	BinaryOperatorDoesNotOperateOnValues(BinaryOpType, ValType, ValType),5253	#[error("no top level object in this context")]54	NoTopLevelObjectFound,55	#[error("self is only usable inside objects")]56	CantUseSelfOutsideOfObject,57	#[error("no super found")]58	NoSuperFound,5960	#[error("for loop can only iterate over arrays")]61	InComprehensionCanOnlyIterateOverArray,6263	#[error("array out of bounds: {0} is not within [0,{1})")]64	ArrayBoundsError(usize, usize),65	#[error("string out of bounds: {0} is not within [0,{1})")]66	StringBoundsError(usize, usize),6768	#[error("assert failed: {}", format_empty_str(.0))]69	AssertionFailed(IStr),7071	#[error("variable is not defined: {0}{}", format_found(.1, "variable"))]72	VariableIsNotDefined(IStr, Vec<IStr>),73	#[error("duplicate local var: {0}")]74	DuplicateLocalVar(IStr),7576	#[error("type mismatch: expected {}, got {2} {0}", .1.iter().map(|e| format!("{}", e)).collect::<Vec<_>>().join(", "))]77	TypeMismatch(&'static str, Vec<ValType>, ValType),78	#[error("no such field: {}{}", format_empty_str(.0), format_found(.1, "field"))]79	NoSuchField(IStr, Vec<IStr>),8081	#[error("only functions can be called, got {0}")]82	OnlyFunctionsCanBeCalledGot(ValType),83	#[error("parameter {0} is not defined")]84	UnknownFunctionParameter(String),85	#[error("argument {0} is already bound")]86	BindingParameterASecondTime(IStr),87	#[error("too many args, function has {0}")]88	TooManyArgsFunctionHas(usize),89	#[error("function argument is not passed: {0}")]90	FunctionParameterNotBoundInCall(IStr),9192	#[error("external variable is not defined: {0}")]93	UndefinedExternalVariable(IStr),9495	#[error("field name should be string, got {0}")]96	FieldMustBeStringGot(ValType),97	#[error("duplicate field name: {}", format_empty_str(.0))]98	DuplicateFieldName(IStr),99100	#[error("attempted to index array with string {}", format_empty_str(.0))]101	AttemptedIndexAnArrayWithString(IStr),102	#[error("{0} index type should be {1}, got {2}")]103	ValueIndexMustBeTypeGot(ValType, ValType, ValType),104	#[error("cant index into {0}")]105	CantIndexInto(ValType),106	#[error("{0} is not indexable")]107	ValueIsNotIndexable(ValType),108109	#[error("super can't be used standalone")]110	StandaloneSuper,111112	#[error("can't resolve {1} from {0}")]113	ImportFileNotFound(PathBuf, String),114	#[error("resolved file not found: {0}")]115	ResolvedFileNotFound(PathBuf),116	#[error("imported file is not valid utf-8: {0:?}")]117	ImportBadFileUtf8(PathBuf),118	#[error("import io error: {0}")]119	ImportIo(String),120	#[error("tried to import {1} from {0}, but imports is not supported")]121	ImportNotSupported(PathBuf, PathBuf),122	#[error("can't import from virtual file")]123	CantImportFromVirtualFile,124	#[error(125		"syntax error: expected {}, got {:?}",126		.error.expected,127		.source_code.chars().nth(error.location.offset)128		.map_or_else(|| "EOF".into(), |c| c.to_string())129	)]130	ImportSyntaxError {131		path: Source,132		source_code: IStr,133		#[trace(skip)]134		error: Box<jrsonnet_parser::ParseError>,135	},136137	#[error("runtime error: {}", format_empty_str(.0))]138	RuntimeError(IStr),139	#[error("stack overflow, try to reduce recursion, or set --max-stack to bigger value")]140	StackOverflow,141	#[error("infinite recursion detected")]142	InfiniteRecursionDetected,143	#[error("tried to index by fractional value")]144	FractionalIndex,145	#[error("attempted to divide by zero")]146	DivisionByZero,147148	#[error("string manifest output is not an string")]149	StringManifestOutputIsNotAString,150	#[error("stream manifest output is not an array")]151	StreamManifestOutputIsNotAArray,152	#[error("multi manifest output is not an object")]153	MultiManifestOutputIsNotAObject,154155	#[error("cant recurse stream manifest")]156	StreamManifestOutputCannotBeRecursed,157	#[error("stream manifest output cannot consist of raw strings")]158	StreamManifestCannotNestString,159160	#[error("{}", format_empty_str(.0))]161	ImportCallbackError(String),162	#[error("invalid unicode codepoint: {0}")]163	InvalidUnicodeCodepointGot(u32),164165	#[error("format error: {0}")]166	Format(#[from] FormatError),167	#[error("type error: {0}")]168	TypeError(TypeLocError),169170	#[cfg(feature = "anyhow-error")]171	#[error(transparent)]172	Other(Rc<anyhow::Error>),173}174175#[cfg(feature = "anyhow-error")]176impl From<anyhow::Error> for LocError {177	fn from(e: anyhow::Error) -> Self {178		Self::new(Error::Other(Rc::new(e)))179	}180}181182impl From<Error> for LocError {183	fn from(e: Error) -> Self {184		Self::new(e)185	}186}187188#[derive(Clone, Debug, Trace)]189pub struct StackTraceElement {190	pub location: Option<ExprLocation>,191	pub desc: String,192}193#[derive(Debug, Clone, Trace)]194pub struct StackTrace(pub Vec<StackTraceElement>);195196#[derive(Clone, Trace)]197pub struct LocError(Box<(Error, StackTrace)>);198impl LocError {199	pub fn new(e: Error) -> Self {200		Self(Box::new((e, StackTrace(vec![]))))201	}202203	pub const fn error(&self) -> &Error {204		&(self.0).0205	}206	pub fn error_mut(&mut self) -> &mut Error {207		&mut (self.0).0208	}209	pub const fn trace(&self) -> &StackTrace {210		&(self.0).1211	}212	pub fn trace_mut(&mut self) -> &mut StackTrace {213		&mut (self.0).1214	}215}216impl Debug for LocError {217	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {218		writeln!(f, "{}", self.0 .0)?;219		for el in &self.0 .1 .0 {220			writeln!(f, "\t{:?}", el)?;221		}222		Ok(())223	}224}225226pub type Result<V, E = LocError> = std::result::Result<V, E>;227228#[macro_export]229macro_rules! throw {230	($e: expr) => {231		return Err($e.into())232	};233}234235#[macro_export]236macro_rules! throw_runtime {237	($($tt:tt)*) => {238		return Err($crate::error::Error::RuntimeError(format!($($tt)*).into()).into())239	};240}
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -13,10 +13,9 @@
 	error::Error::*,
 	evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},
 	function::{CallLocation, FuncDesc, FuncVal},
-	stdlib::{std_slice, BUILTINS},
 	tb, throw,
 	typed::Typed,
-	val::{ArrValue, CachedUnbound, Thunk, ThunkValue},
+	val::{ArrValue, CachedUnbound, IndexableVal, Thunk, ThunkValue},
 	Context, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, State,
 	Unbound, Val,
 };
@@ -417,13 +416,13 @@
 		Literal(LiteralType::This) => {
 			Val::Obj(ctx.this().clone().ok_or(CantUseSelfOutsideOfObject)?)
 		}
-		Literal(LiteralType::Super) => Val::Obj(
-			ctx.super_obj().clone().ok_or(NoSuperFound)?.with_this(
+		Literal(LiteralType::Super) => {
+			Val::Obj(ctx.super_obj().clone().ok_or(NoSuperFound)?.with_this(
 				ctx.this()
 					.clone()
 					.expect("if super exists - then this should to"),
-			),
-		),
+			))
+		}
 		Literal(LiteralType::Dollar) => {
 			Val::Obj(ctx.dollar().clone().ok_or(NoTopLevelObjectFound)?)
 		}
@@ -473,9 +472,6 @@
 								heap.into_iter().map(|(_, v)| v).collect()
 							))
 						}
-						Err(e) if matches!(e.error(), MagicThisFileUsed) => {
-							Ok(Val::Str(loc.0.full_path().into()))
-						}
 						Err(e) => Err(e),
 					},
 				)?,
@@ -573,13 +569,6 @@
 		Function(params, body) => {
 			evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())
 		}
-		Intrinsic(name) => Val::Func(FuncVal::StaticBuiltin(
-			BUILTINS
-				.with(|b| b.get(name).copied())
-				.ok_or_else(|| IntrinsicNotFound(name.clone()))?,
-		)),
-		IntrinsicThisFile => return Err(MagicThisFileUsed.into()),
-		IntrinsicId => Val::Func(FuncVal::identity()),
 		AssertExpr(assert, returned) => {
 			evaluate_assert(s.clone(), ctx.clone(), assert)?;
 			evaluate(s, ctx, returned)?
@@ -635,9 +624,9 @@
 
 			let start = parse_idx(loc, s.clone(), &ctx, &desc.start, "start")?;
 			let end = parse_idx(loc, s.clone(), &ctx, &desc.end, "end")?;
-			let step = parse_idx(loc, s, &ctx, &desc.step, "step")?;
+			let step = parse_idx(loc, s.clone(), &ctx, &desc.step, "step")?;
 
-			std_slice(indexable.into_indexable()?, start, end, step)?
+			IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?, s)?
 		}
 		i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {
 			let tmp = loc.clone().0;
modifiedcrates/jrsonnet-evaluator/src/function/native.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function/native.rs
+++ b/crates/jrsonnet-evaluator/src/function/native.rs
@@ -1,5 +1,5 @@
 use super::{arglike::ArgLike, CallLocation, FuncVal};
-use crate::{error::Result, typed::Typed, State};
+use crate::{error::Result, typed::Typed, Context, State};
 
 pub trait NativeDesc {
 	type Value;
@@ -19,7 +19,8 @@
 				Box::new(move |s: State, $($gen),*| {
 					let val = val.evaluate(
 						s.clone(),
-						s.create_default_context(),
+						// This isn't intended to be used with ArgsDesc
+						Context::default(),
 						CallLocation::native(),
 						&($($gen,)*),
 						true
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -37,12 +37,13 @@
 mod integrations;
 mod map;
 mod obj;
-mod stdlib;
+pub mod stdlib;
 pub mod trace;
 pub mod typed;
 pub mod val;
 
 use std::{
+	any::Any,
 	borrow::Cow,
 	cell::{Ref, RefCell, RefMut},
 	collections::HashMap,
@@ -55,13 +56,12 @@
 pub use dynamic::*;
 use error::{Error::*, LocError, Result, StackTraceElement};
 pub use evaluate::*;
-use function::{builtin::Builtin, CallLocation, TlaArg};
+use function::{CallLocation, TlaArg};
 use gc::{GcHashMap, TraceBox};
 use hashbrown::hash_map::RawEntryMut;
 pub use import::*;
 use jrsonnet_gcmodule::{Cc, Trace};
-use jrsonnet_interner::IBytes;
-pub use jrsonnet_interner::IStr;
+pub use jrsonnet_interner::{IBytes, IStr};
 pub use jrsonnet_parser as parser;
 use jrsonnet_parser::*;
 pub use obj::*;
@@ -98,19 +98,40 @@
 	}
 }
 
+/// During import, this trait will be called to create initial context for file
+/// It may initialize global variables, stdlib for example
+pub trait ContextInitializer {
+	fn initialize(&self, state: State, for_file: Source) -> Context;
+
+	/// # Safety
+	///
+	/// For use only in bindings, should not be used elsewhere.
+	/// Implementations which are not intended to be used in bindings
+	/// should panic on call to this method.
+	unsafe fn as_any(&self) -> &dyn Any;
+}
+
+/// Context initializer, which adds noth
+pub struct DummyContextInitializer;
+impl ContextInitializer for DummyContextInitializer {
+	fn initialize(&self, _state: State, _for_file: Source) -> Context {
+		Context::default()
+	}
+	unsafe fn as_any(&self) -> &dyn Any {
+		panic!("`as_any(&self)` is not supported by dummy initializer")
+	}
+}
+
 pub struct EvaluationSettings {
 	/// Limits recursion by limiting the number of stack frames
 	pub max_stack: usize,
 	/// Limits amount of stack trace items preserved
 	pub max_trace: usize,
-	/// Used for s`td.extVar`
-	pub ext_vars: HashMap<IStr, TlaArg>,
-	/// Used for ext.native
-	pub ext_natives: HashMap<IStr, Cc<TraceBox<dyn Builtin>>>,
 	/// TLA vars
 	pub tla_vars: HashMap<IStr, TlaArg>,
-	/// Global variables are inserted in default context
-	pub globals: HashMap<IStr, Val>,
+	/// Context initializer, which will be used for imports and everything
+	/// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`
+	pub context_initializer: Box<dyn ContextInitializer>,
 	/// Used to resolve file locations/contents
 	pub import_resolver: Box<dyn ImportResolver>,
 	/// Used in manifestification functions
@@ -123,9 +144,7 @@
 		Self {
 			max_stack: 200,
 			max_trace: 20,
-			globals: HashMap::default(),
-			ext_vars: HashMap::default(),
-			ext_natives: HashMap::default(),
+			context_initializer: Box::new(DummyContextInitializer),
 			tla_vars: HashMap::default(),
 			import_resolver: Box::new(DummyImportResolver),
 			manifest_format: ManifestFormat::Json {
@@ -152,7 +171,8 @@
 
 	/// Contains file source codes and evaluation results for imports and pretty-printed stacktraces
 	files: GcHashMap<PathBuf, FileData>,
-	/// Contains tla arguments and others, which aren't needed to be obtained by name
+	/// Contains tla arguments and others, which aren't needed to be obtained by name, however may be used for receiving source
+	/// TODO: look into nix approach, storing source code in `Source` object
 	volatile_files: GcHashMap<String, String>,
 }
 struct FileData {
@@ -333,7 +353,7 @@
 					},
 				)
 				.map_err(|e| ImportSyntaxError {
-					path: file_name,
+					path: file_name.clone(),
 					source_code: code.clone(),
 					error: Box::new(e),
 				})?,
@@ -346,7 +366,11 @@
 		file.evaluating = true;
 		// Dropping file here, as it borrows data, which may be used in evaluation
 		drop(data);
-		let res = evaluate(self.clone(), self.create_default_context(), &parsed);
+		let res = evaluate(
+			self.clone(),
+			self.create_default_context(file_name),
+			&parsed,
+		);
 
 		let mut data = self.data_mut();
 		let mut file = data.files.raw_entry_mut().from_key(&path);
@@ -391,26 +415,11 @@
 			column,
 		)
 	}
-	/// Adds standard library global variable (std) to this evaluator
-	pub fn with_stdlib(&self) -> &Self {
-		let val = evaluate(
-			self.clone(),
-			self.create_default_context(),
-			&stdlib::get_parsed_stdlib(),
-		)
-		.expect("std should not fail");
-		self.settings_mut().globals.insert("std".into(), val);
-		self
-	}
 
 	/// Creates context with all passed global variables
-	pub fn create_default_context(&self) -> Context {
-		let globals = &self.settings().globals;
-		let mut new_bindings = GcHashMap::with_capacity(globals.len());
-		for (name, value) in globals.iter() {
-			new_bindings.insert(name.clone(), Thunk::evaluated(value.clone()));
-		}
-		Context::new().extend(new_bindings, None, None, None)
+	pub fn create_default_context(&self, source: Source) -> Context {
+		let context_initializer = &self.settings().context_initializer;
+		context_initializer.initialize(self.clone(), source)
 	}
 
 	/// Executes code creating a new stack frame
@@ -545,7 +554,7 @@
 				|| {
 					func.evaluate(
 						self.clone(),
-						self.create_default_context(),
+						self.create_default_context(Source::new_virtual(Cow::Borrowed("<tla>"))),
 						CallLocation::native(),
 						&self.settings().tla_vars,
 						true,
@@ -585,48 +594,17 @@
 			},
 		)
 		.map_err(|e| ImportSyntaxError {
-			path: source,
+			path: source.clone(),
 			source_code: code.clone().into(),
 			error: Box::new(e),
 		})?;
 		self.data_mut().volatile_files.insert(name, code);
-		evaluate(self.clone(), self.create_default_context(), &parsed)
+		evaluate(self.clone(), self.create_default_context(source), &parsed)
 	}
 }
 
 /// Settings utilities
 impl State {
-	pub fn add_ext_var(&self, name: IStr, value: Val) {
-		self.settings_mut()
-			.ext_vars
-			.insert(name, TlaArg::Val(value));
-	}
-	pub fn add_ext_str(&self, name: IStr, value: IStr) {
-		self.settings_mut()
-			.ext_vars
-			.insert(name, TlaArg::String(value));
-	}
-	pub fn add_ext_code(&self, name: &str, code: String) -> Result<()> {
-		let source_name = format!("<extvar:{}>", name);
-		let source = Source::new_virtual(Cow::Owned(source_name.clone()));
-		let parsed = jrsonnet_parser::parse(
-			&code,
-			&ParserSettings {
-				file_name: source.clone(),
-			},
-		)
-		.map_err(|e| ImportSyntaxError {
-			path: source,
-			source_code: code.clone().into(),
-			error: Box::new(e),
-		})?;
-		self.data_mut().volatile_files.insert(source_name, code);
-		self.settings_mut()
-			.ext_vars
-			.insert(name.into(), TlaArg::Code(parsed));
-		Ok(())
-	}
-
 	pub fn add_tla(&self, name: IStr, value: Val) {
 		self.settings_mut()
 			.tla_vars
@@ -672,9 +650,8 @@
 	pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {
 		self.settings_mut().import_resolver = resolver;
 	}
-
-	pub fn add_native(&self, name: IStr, cb: Cc<TraceBox<dyn Builtin>>) {
-		self.settings_mut().ext_natives.insert(name, cb);
+	pub fn context_initializer(&self) -> Ref<dyn ContextInitializer> {
+		Ref::map(self.settings(), |s| &*s.context_initializer)
 	}
 
 	pub fn manifest_format(&self) -> ManifestFormat {
deletedcrates/jrsonnet-evaluator/src/stdlib/expr.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/stdlib/expr.rs
+++ /dev/null
@@ -1,28 +0,0 @@
-use std::borrow::Cow;
-
-use jrsonnet_parser::{LocExpr, ParserSettings, Source};
-
-thread_local! {
-	/// To avoid parsing again when issued from the same thread
-	#[allow(unreachable_code)]
-	static PARSED_STDLIB: LocExpr = {
-		#[cfg(feature = "serialized-stdlib")]
-		{
-			// Should not panic, stdlib.bincode is generated in build.rs
-			return bincode::deserialize(include_bytes!(concat!(env!("OUT_DIR"), "/stdlib.bincode")))
-				.unwrap();
-		}
-
-		jrsonnet_parser::parse(
-			jrsonnet_stdlib::STDLIB_STR,
-			&ParserSettings {
-				file_name: Source::new_virtual(Cow::Borrowed("<std>")),
-			},
-		)
-		.unwrap()
-	}
-}
-
-pub fn get_parsed_stdlib() -> LocExpr {
-	PARSED_STDLIB.with(Clone::clone)
-}
modifiedcrates/jrsonnet-evaluator/src/stdlib/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/stdlib/mod.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/mod.rs
@@ -1,33 +1,13 @@
 // All builtins should return results
 #![allow(clippy::unnecessary_wraps)]
 
-use std::collections::HashMap;
-
 use format::{format_arr, format_obj};
-use jrsonnet_gcmodule::Cc;
-use jrsonnet_interner::{IBytes, IStr};
-use serde::Deserialize;
-use serde_yaml_with_quirks::DeserializingQuirks;
-
-use crate::{
-	error::{Error::*, Result},
-	function::{builtin::StaticBuiltin, ArgLike, CallLocation, FuncVal},
-	operator::evaluate_mod_op,
-	stdlib::manifest::{manifest_yaml_ex, ManifestYamlOptions},
-	throw,
-	typed::{Any, BoundedUsize, Either2, Either4, PositiveF64, Typed, VecVal, M1},
-	val::{equals, primitive_equals, ArrValue, IndexableVal, Slice},
-	Either, ObjValue, State, Val,
-};
-
-pub mod expr;
-pub use expr::*;
+use jrsonnet_interner::IStr;
 
-use self::manifest::{escape_string_json, manifest_json_ex, ManifestJsonOptions, ManifestType};
+use crate::{error::Result, function::CallLocation, State, Val};
 
 pub mod format;
 pub mod manifest;
-pub mod sort;
 
 pub fn std_format(s: State, str: IStr, vals: Val) -> Result<String> {
 	s.push(
@@ -39,704 +19,6 @@
 				Val::Obj(obj) => format_obj(s.clone(), &str, &obj)?,
 				o => format_arr(s.clone(), &str, &[o])?,
 			})
-		},
-	)
-}
-
-pub fn std_slice(
-	indexable: IndexableVal,
-	index: Option<BoundedUsize<0, { i32::MAX as usize }>>,
-	end: Option<BoundedUsize<0, { i32::MAX as usize }>>,
-	step: Option<BoundedUsize<1, { i32::MAX as usize }>>,
-) -> Result<Val> {
-	match &indexable {
-		IndexableVal::Str(s) => {
-			let index = index.as_deref().copied().unwrap_or(0);
-			let end = end.as_deref().copied().unwrap_or(usize::MAX);
-			let step = step.as_deref().copied().unwrap_or(1);
-
-			if index >= end {
-				return Ok(Val::Str("".into()));
-			}
-
-			Ok(Val::Str(
-				(s.chars()
-					.skip(index)
-					.take(end - index)
-					.step_by(step)
-					.collect::<String>())
-				.into(),
-			))
-		}
-		IndexableVal::Arr(arr) => {
-			let index = index.as_deref().copied().unwrap_or(0);
-			let end = end.as_deref().copied().unwrap_or(usize::MAX).min(arr.len());
-			let step = step.as_deref().copied().unwrap_or(1);
-
-			if index >= end {
-				return Ok(Val::Arr(ArrValue::new_eager()));
-			}
-
-			Ok(Val::Arr(ArrValue::Slice(Box::new(Slice {
-				inner: arr.clone(),
-				from: index as u32,
-				to: end as u32,
-				step: step as u32,
-			}))))
-		}
-	}
-}
-
-type BuiltinsType = HashMap<IStr, &'static dyn StaticBuiltin>;
-
-thread_local! {
-	pub static BUILTINS: BuiltinsType = {
-		[
-			("length".into(), builtin_length::INST),
-			("type".into(), builtin_type::INST),
-			("makeArray".into(), builtin_make_array::INST),
-			("codepoint".into(), builtin_codepoint::INST),
-			("objectFieldsEx".into(), builtin_object_fields_ex::INST),
-			("objectHasEx".into(), builtin_object_has_ex::INST),
-			("slice".into(), builtin_slice::INST),
-			("substr".into(), builtin_substr::INST),
-			("primitiveEquals".into(), builtin_primitive_equals::INST),
-			("equals".into(), builtin_equals::INST),
-			("modulo".into(), builtin_modulo::INST),
-			("mod".into(), builtin_mod::INST),
-			("floor".into(), builtin_floor::INST),
-			("ceil".into(), builtin_ceil::INST),
-			("log".into(), builtin_log::INST),
-			("pow".into(), builtin_pow::INST),
-			("sqrt".into(), builtin_sqrt::INST),
-			("sin".into(), builtin_sin::INST),
-			("cos".into(), builtin_cos::INST),
-			("tan".into(), builtin_tan::INST),
-			("asin".into(), builtin_asin::INST),
-			("acos".into(), builtin_acos::INST),
-			("atan".into(), builtin_atan::INST),
-			("exp".into(), builtin_exp::INST),
-			("mantissa".into(), builtin_mantissa::INST),
-			("exponent".into(), builtin_exponent::INST),
-			("extVar".into(), builtin_ext_var::INST),
-			("native".into(), builtin_native::INST),
-			("filter".into(), builtin_filter::INST),
-			("map".into(), builtin_map::INST),
-			("flatMap".into(), builtin_flatmap::INST),
-			("foldl".into(), builtin_foldl::INST),
-			("foldr".into(), builtin_foldr::INST),
-			("sort".into(), builtin_sort::INST),
-			("format".into(), builtin_format::INST),
-			("range".into(), builtin_range::INST),
-			("char".into(), builtin_char::INST),
-			("encodeUTF8".into(), builtin_encode_utf8::INST),
-			("decodeUTF8".into(), builtin_decode_utf8::INST),
-			("md5".into(), builtin_md5::INST),
-			("base64".into(), builtin_base64::INST),
-			("base64DecodeBytes".into(), builtin_base64_decode_bytes::INST),
-			("base64Decode".into(), builtin_base64_decode::INST),
-			("trace".into(), builtin_trace::INST),
-			("join".into(), builtin_join::INST),
-			("escapeStringJson".into(), builtin_escape_string_json::INST),
-			("manifestJsonEx".into(), builtin_manifest_json_ex::INST),
-			("manifestYamlDoc".into(), builtin_manifest_yaml_doc::INST),
-			("reverse".into(), builtin_reverse::INST),
-			("strReplace".into(), builtin_str_replace::INST),
-			("splitLimit".into(), builtin_splitlimit::INST),
-			("parseJson".into(), builtin_parse_json::INST),
-			("parseYaml".into(), builtin_parse_yaml::INST),
-			("asciiUpper".into(), builtin_ascii_upper::INST),
-			("asciiLower".into(), builtin_ascii_lower::INST),
-			("member".into(), builtin_member::INST),
-			("count".into(), builtin_count::INST),
-			("any".into(), builtin_any::INST),
-			("all".into(), builtin_all::INST),
-		].iter().cloned().collect()
-	};
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_length(x: Either![IStr, ArrValue, ObjValue, FuncVal]) -> Result<usize> {
-	use Either4::*;
-	Ok(match x {
-		A(x) => x.chars().count(),
-		B(x) => x.len(),
-		C(x) => x.len(),
-		D(f) => f.params_len(),
-	})
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_type(x: Any) -> Result<IStr> {
-	Ok(x.0.value_type().name().into())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_make_array(s: State, sz: usize, func: FuncVal) -> Result<VecVal> {
-	let mut out = Vec::with_capacity(sz);
-	for i in 0..sz {
-		out.push(func.evaluate_simple(s.clone(), &(i as f64,))?);
-	}
-	Ok(VecVal(Cc::new(out)))
-}
-
-#[jrsonnet_macros::builtin]
-const fn builtin_codepoint(str: char) -> Result<u32> {
-	Ok(str as u32)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_object_fields_ex(
-	obj: ObjValue,
-	inc_hidden: bool,
-	#[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,
-) -> Result<VecVal> {
-	#[cfg(feature = "exp-preserve-order")]
-	let preserve_order = preserve_order.unwrap_or(false);
-	let out = obj.fields_ex(
-		inc_hidden,
-		#[cfg(feature = "exp-preserve-order")]
-		preserve_order,
-	);
-	Ok(VecVal(Cc::new(
-		out.into_iter().map(Val::Str).collect::<Vec<_>>(),
-	)))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_object_has_ex(obj: ObjValue, f: IStr, inc_hidden: bool) -> Result<bool> {
-	Ok(obj.has_field_ex(f, inc_hidden))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_parse_json(st: State, s: IStr) -> Result<Any> {
-	use serde_json::Value;
-	let value: Value = serde_json::from_str(&s)
-		.map_err(|e| RuntimeError(format!("failed to parse json: {}", e).into()))?;
-	Ok(Any(Value::into_untyped(value, st)?))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_parse_yaml(st: State, s: IStr) -> Result<Any> {
-	use serde_json::Value;
-	let value = serde_yaml_with_quirks::Deserializer::from_str_with_quirks(
-		&s,
-		DeserializingQuirks { old_octals: true },
-	);
-	let mut out = vec![];
-	for item in value {
-		let value = Value::deserialize(item)
-			.map_err(|e| RuntimeError(format!("failed to parse yaml: {}", e).into()))?;
-		let val = Value::into_untyped(value, st.clone())?;
-		out.push(val);
-	}
-	Ok(Any(if out.is_empty() {
-		Val::Null
-	} else if out.len() == 1 {
-		out.into_iter().next().unwrap()
-	} else {
-		Val::Arr(out.into())
-	}))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_slice(
-	indexable: IndexableVal,
-	index: Option<BoundedUsize<0, { i32::MAX as usize }>>,
-	end: Option<BoundedUsize<0, { i32::MAX as usize }>>,
-	step: Option<BoundedUsize<1, { i32::MAX as usize }>>,
-) -> Result<Any> {
-	std_slice(indexable, index, end, step).map(Any)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_substr(str: IStr, from: usize, len: usize) -> Result<String> {
-	Ok(str.chars().skip(from as usize).take(len as usize).collect())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_primitive_equals(a: Any, b: Any) -> Result<bool> {
-	primitive_equals(&a.0, &b.0)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_equals(s: State, a: Any, b: Any) -> Result<bool> {
-	equals(s, &a.0, &b.0)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_modulo(a: f64, b: f64) -> Result<f64> {
-	Ok(a % b)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_mod(s: State, a: Either![f64, IStr], b: Any) -> Result<Any> {
-	use Either2::*;
-	Ok(Any(evaluate_mod_op(
-		s,
-		&match a {
-			A(v) => Val::Num(v),
-			B(s) => Val::Str(s),
-		},
-		&b.0,
-	)?))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_floor(x: f64) -> Result<f64> {
-	Ok(x.floor())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_ceil(x: f64) -> Result<f64> {
-	Ok(x.ceil())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_log(n: f64) -> Result<f64> {
-	Ok(n.ln())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_pow(x: f64, n: f64) -> Result<f64> {
-	Ok(x.powf(n))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_sqrt(x: PositiveF64) -> Result<f64> {
-	Ok(x.0.sqrt())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_sin(x: f64) -> Result<f64> {
-	Ok(x.sin())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_cos(x: f64) -> Result<f64> {
-	Ok(x.cos())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_tan(x: f64) -> Result<f64> {
-	Ok(x.tan())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_asin(x: f64) -> Result<f64> {
-	Ok(x.asin())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_acos(x: f64) -> Result<f64> {
-	Ok(x.acos())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_atan(x: f64) -> Result<f64> {
-	Ok(x.atan())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_exp(x: f64) -> Result<f64> {
-	Ok(x.exp())
-}
-
-fn frexp(s: f64) -> (f64, i16) {
-	if 0.0 == s {
-		(s, 0)
-	} else {
-		let lg = s.abs().log2();
-		let x = (lg - lg.floor() - 1.0).exp2();
-		let exp = lg.floor() + 1.0;
-		(s.signum() * x, exp as i16)
-	}
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_mantissa(x: f64) -> Result<f64> {
-	Ok(frexp(x).0)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_exponent(x: f64) -> Result<i16> {
-	Ok(frexp(x).1)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_ext_var(s: State, x: IStr) -> Result<Any> {
-	let ctx = s.create_default_context();
-	Ok(Any(s
-		.clone()
-		.settings()
-		.ext_vars
-		.get(&x)
-		.cloned()
-		.ok_or(UndefinedExternalVariable(x))?
-		.evaluate_arg(s.clone(), ctx, true)?
-		.evaluate(s)?))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_native(s: State, name: IStr) -> Result<Any> {
-	Ok(Any(s
-		.settings()
-		.ext_natives
-		.get(&name)
-		.cloned()
-		.map_or(Val::Null, |v| {
-			Val::Func(FuncVal::Builtin(v.clone()))
-		})))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_filter(s: State, func: FuncVal, arr: ArrValue) -> Result<ArrValue> {
-	arr.filter(s.clone(), |val| {
-		bool::from_untyped(
-			func.evaluate_simple(s.clone(), &(Any(val.clone()),))?,
-			s.clone(),
-		)
-	})
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_map(s: State, func: FuncVal, arr: ArrValue) -> Result<ArrValue> {
-	arr.map(s.clone(), |val| {
-		func.evaluate_simple(s.clone(), &(Any(val),))
-	})
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_flatmap(s: State, func: FuncVal, arr: IndexableVal) -> Result<IndexableVal> {
-	match arr {
-		IndexableVal::Str(str) => {
-			let mut out = String::new();
-			for c in str.chars() {
-				match func.evaluate_simple(s.clone(), &(c.to_string(),))? {
-					Val::Str(o) => out.push_str(&o),
-					Val::Null => continue,
-					_ => throw!(RuntimeError(
-						"in std.join all items should be strings".into()
-					)),
-				};
-			}
-			Ok(IndexableVal::Str(out.into()))
-		}
-		IndexableVal::Arr(a) => {
-			let mut out = Vec::new();
-			for el in a.iter(s.clone()) {
-				let el = el?;
-				match func.evaluate_simple(s.clone(), &(Any(el),))? {
-					Val::Arr(o) => {
-						for oe in o.iter(s.clone()) {
-							out.push(oe?);
-						}
-					}
-					Val::Null => continue,
-					_ => throw!(RuntimeError(
-						"in std.join all items should be arrays".into()
-					)),
-				};
-			}
-			Ok(IndexableVal::Arr(out.into()))
-		}
-	}
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_foldl(s: State, func: FuncVal, arr: ArrValue, init: Any) -> Result<Any> {
-	let mut acc = init.0;
-	for i in arr.iter(s.clone()) {
-		acc = func.evaluate_simple(s.clone(), &(Any(acc), Any(i?)))?;
-	}
-	Ok(Any(acc))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_foldr(s: State, func: FuncVal, arr: ArrValue, init: Any) -> Result<Any> {
-	let mut acc = init.0;
-	for i in arr.iter(s.clone()).rev() {
-		acc = func.evaluate_simple(s.clone(), &(Any(i?), Any(acc)))?;
-	}
-	Ok(Any(acc))
-}
-
-#[jrsonnet_macros::builtin]
-#[allow(non_snake_case)]
-fn builtin_sort(s: State, arr: ArrValue, keyF: Option<FuncVal>) -> Result<ArrValue> {
-	if arr.len() <= 1 {
-		return Ok(arr);
-	}
-	Ok(ArrValue::Eager(sort::sort(
-		s.clone(),
-		arr.evaluated(s)?,
-		keyF.unwrap_or_else(FuncVal::identity),
-	)?))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_format(s: State, str: IStr, vals: Any) -> Result<String> {
-	std_format(s, str, vals.0)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_range(from: i32, to: i32) -> Result<ArrValue> {
-	if to < from {
-		return Ok(ArrValue::new_eager());
-	}
-	Ok(ArrValue::new_range(from, to))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_char(n: u32) -> Result<char> {
-	Ok(std::char::from_u32(n as u32).ok_or(InvalidUnicodeCodepointGot(n as u32))?)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_encode_utf8(str: IStr) -> Result<IBytes> {
-	Ok(str.cast_bytes())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_decode_utf8(arr: IBytes) -> Result<IStr> {
-	Ok(arr
-		.cast_str()
-		.ok_or_else(|| RuntimeError("bad utf8".into()))?)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_md5(str: IStr) -> Result<String> {
-	Ok(format!("{:x}", md5::compute(&str.as_bytes())))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_trace(s: State, loc: CallLocation, str: IStr, rest: Any) -> Result<Any> {
-	eprint!("TRACE:");
-	if let Some(loc) = loc.0 {
-		let locs = s.map_source_locations(loc.0.clone(), &[loc.1]);
-		eprint!(" {}:{}", loc.0.short_display(), locs[0].line);
-	}
-	eprintln!(" {}", str);
-	Ok(rest) as Result<Any>
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_base64(input: Either![IBytes, IStr]) -> Result<String> {
-	use Either2::*;
-	Ok(match input {
-		A(a) => base64::encode(a.as_slice()),
-		B(l) => base64::encode(l.bytes().collect::<Vec<_>>()),
-	})
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_base64_decode_bytes(input: IStr) -> Result<IBytes> {
-	Ok(base64::decode(&input.as_bytes())
-		.map_err(|_| RuntimeError("bad base64".into()))?
-		.as_slice()
-		.into())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_base64_decode(input: IStr) -> Result<String> {
-	let bytes = base64::decode(&input.as_bytes()).map_err(|_| RuntimeError("bad base64".into()))?;
-	Ok(String::from_utf8(bytes).map_err(|_| RuntimeError("bad utf8".into()))?)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_join(s: State, sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {
-	Ok(match sep {
-		IndexableVal::Arr(joiner_items) => {
-			let mut out = Vec::new();
-
-			let mut first = true;
-			for item in arr.iter(s.clone()) {
-				let item = item?.clone();
-				if let Val::Arr(items) = item {
-					if !first {
-						out.reserve(joiner_items.len());
-						// TODO: extend
-						for item in joiner_items.iter(s.clone()) {
-							out.push(item?);
-						}
-					}
-					first = false;
-					out.reserve(items.len());
-					for item in items.iter(s.clone()) {
-						out.push(item?);
-					}
-				} else if matches!(item, Val::Null) {
-					continue;
-				} else {
-					throw!(RuntimeError(
-						"in std.join all items should be arrays".into()
-					));
-				}
-			}
-
-			IndexableVal::Arr(out.into())
-		}
-		IndexableVal::Str(sep) => {
-			let mut out = String::new();
-
-			let mut first = true;
-			for item in arr.iter(s) {
-				let item = item?.clone();
-				if let Val::Str(item) = item {
-					if !first {
-						out += &sep;
-					}
-					first = false;
-					out += &item;
-				} else if matches!(item, Val::Null) {
-					continue;
-				} else {
-					throw!(RuntimeError(
-						"in std.join all items should be strings".into()
-					));
-				}
-			}
-
-			IndexableVal::Str(out.into())
-		}
-	})
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_escape_string_json(str_: IStr) -> Result<String> {
-	Ok(escape_string_json(&str_))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_manifest_json_ex(
-	s: State,
-	value: Any,
-	indent: IStr,
-	newline: Option<IStr>,
-	key_val_sep: Option<IStr>,
-	#[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,
-) -> Result<String> {
-	let newline = newline.as_deref().unwrap_or("\n");
-	let key_val_sep = key_val_sep.as_deref().unwrap_or(": ");
-	manifest_json_ex(
-		s,
-		&value.0,
-		&ManifestJsonOptions {
-			padding: &indent,
-			mtype: ManifestType::Std,
-			newline,
-			key_val_sep,
-			#[cfg(feature = "exp-preserve-order")]
-			preserve_order: preserve_order.unwrap_or(false),
-		},
-	)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_manifest_yaml_doc(
-	s: State,
-	value: Any,
-	indent_array_in_object: Option<bool>,
-	quote_keys: Option<bool>,
-	#[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,
-) -> Result<String> {
-	manifest_yaml_ex(
-		s,
-		&value.0,
-		&ManifestYamlOptions {
-			padding: "  ",
-			arr_element_padding: if indent_array_in_object.unwrap_or(false) {
-				"  "
-			} else {
-				""
-			},
-			quote_keys: quote_keys.unwrap_or(true),
-			#[cfg(feature = "exp-preserve-order")]
-			preserve_order: preserve_order.unwrap_or(false),
 		},
 	)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_reverse(value: ArrValue) -> Result<ArrValue> {
-	Ok(value.reversed())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_str_replace(str: String, from: IStr, to: IStr) -> Result<String> {
-	Ok(str.replace(&from as &str, &to as &str))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_splitlimit(str: IStr, c: IStr, maxsplits: Either![usize, M1]) -> Result<VecVal> {
-	use Either2::*;
-	Ok(VecVal(Cc::new(match maxsplits {
-		A(n) => str
-			.splitn(n + 1, &c as &str)
-			.map(|s| Val::Str(s.into()))
-			.collect(),
-		B(_) => str.split(&c as &str).map(|s| Val::Str(s.into())).collect(),
-	})))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_ascii_upper(str: IStr) -> Result<String> {
-	Ok(str.to_ascii_uppercase())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_ascii_lower(str: IStr) -> Result<String> {
-	Ok(str.to_ascii_lowercase())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_member(s: State, arr: IndexableVal, x: Any) -> Result<bool> {
-	match arr {
-		IndexableVal::Str(str) => {
-			let x: IStr = IStr::from_untyped(x.0, s)?;
-			Ok(!x.is_empty() && str.contains(&*x))
-		}
-		IndexableVal::Arr(a) => {
-			for item in a.iter(s.clone()) {
-				let item = item?;
-				if equals(s.clone(), &item, &x.0)? {
-					return Ok(true);
-				}
-			}
-			Ok(false)
-		}
-	}
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_count(s: State, arr: Vec<Any>, v: Any) -> Result<usize> {
-	let mut count = 0;
-	for item in &arr {
-		if equals(s.clone(), &item.0, &v.0)? {
-			count += 1;
-		}
-	}
-	Ok(count)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_any(s: State, arr: ArrValue) -> Result<bool> {
-	for v in arr.iter(s.clone()) {
-		let v = bool::from_untyped(v?, s.clone())?;
-		if v {
-			return Ok(true);
-		}
-	}
-	Ok(false)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_all(s: State, arr: ArrValue) -> Result<bool> {
-	for v in arr.iter(s.clone()) {
-		let v = bool::from_untyped(v?, s.clone())?;
-		if !v {
-			return Ok(false);
-		}
-	}
-	Ok(true)
 }
deletedcrates/jrsonnet-evaluator/src/stdlib/sort.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/stdlib/sort.rs
+++ /dev/null
@@ -1,110 +0,0 @@
-use jrsonnet_gcmodule::{Cc, Trace};
-
-use crate::{
-	error::{Error, LocError, Result},
-	function::FuncVal,
-	throw,
-	typed::Any,
-	State, Val,
-};
-
-#[derive(Debug, Clone, thiserror::Error, Trace)]
-pub enum SortError {
-	#[error("sort key should be string or number")]
-	SortKeyShouldBeStringOrNumber,
-	#[error("sort elements should have equal types")]
-	SortElementsShouldHaveEqualType,
-}
-
-impl From<SortError> for LocError {
-	fn from(s: SortError) -> Self {
-		Self::new(Error::Sort(s))
-	}
-}
-
-#[derive(Copy, Clone)]
-enum SortKeyType {
-	Number,
-	String,
-	Unknown,
-}
-
-#[derive(PartialEq)]
-struct NonNaNf64(f64);
-impl PartialOrd for NonNaNf64 {
-	fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
-		self.0.partial_cmp(&other.0)
-	}
-}
-impl Eq for NonNaNf64 {}
-impl Ord for NonNaNf64 {
-	fn cmp(&self, other: &Self) -> std::cmp::Ordering {
-		self.partial_cmp(other).expect("non nan")
-	}
-}
-
-fn get_sort_type<T>(
-	values: &mut Vec<T>,
-	key_getter: impl Fn(&mut T) -> &mut Val,
-) -> Result<SortKeyType> {
-	let mut sort_type = SortKeyType::Unknown;
-	for i in values.iter_mut() {
-		let i = key_getter(i);
-		match (i, sort_type) {
-			(Val::Str(_), SortKeyType::Unknown) => sort_type = SortKeyType::String,
-			(Val::Num(_), SortKeyType::Unknown) => sort_type = SortKeyType::Number,
-			(Val::Str(_), SortKeyType::String) | (Val::Num(_), SortKeyType::Number) => {}
-			(Val::Str(_) | Val::Num(_), _) => {
-				throw!(SortError::SortElementsShouldHaveEqualType)
-			}
-			_ => throw!(SortError::SortKeyShouldBeStringOrNumber),
-		}
-	}
-	Ok(sort_type)
-}
-
-/// * `key_getter` - None, if identity sort required
-pub fn sort(s: State, values: Cc<Vec<Val>>, key_getter: FuncVal) -> Result<Cc<Vec<Val>>> {
-	if values.len() <= 1 {
-		return Ok(values);
-	}
-	if key_getter.is_identity() {
-		// Fast path, identity key getter
-		let mut values = (*values).clone();
-		let sort_type = get_sort_type(&mut values, |k| k)?;
-		match sort_type {
-			SortKeyType::Number => values.sort_unstable_by_key(|v| match v {
-				Val::Num(n) => NonNaNf64(*n),
-				_ => unreachable!(),
-			}),
-			SortKeyType::String => values.sort_unstable_by_key(|v| match v {
-				Val::Str(s) => s.clone(),
-				_ => unreachable!(),
-			}),
-			SortKeyType::Unknown => unreachable!(),
-		};
-		Ok(Cc::new(values))
-	} else {
-		// 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(s.clone(), &(Any(value.clone()),))?,
-			));
-		}
-		let sort_type = get_sort_type(&mut vk, |v| &mut v.1)?;
-		match sort_type {
-			SortKeyType::Number => vk.sort_by_key(|v| match v.1 {
-				Val::Num(n) => NonNaNf64(n),
-				_ => unreachable!(),
-			}),
-			SortKeyType::String => vk.sort_by_key(|v| match &v.1 {
-				Val::Str(s) => s.clone(),
-				_ => unreachable!(),
-			}),
-			SortKeyType::Unknown => unreachable!(),
-		};
-		Ok(Cc::new(vk.into_iter().map(|v| v.0).collect()))
-	}
-}