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
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -6,10 +6,7 @@
 use jrsonnet_types::ValType;
 use thiserror::Error;
 
-use crate::{
-	stdlib::{format::FormatError, sort::SortError},
-	typed::TypeLocError,
-};
+use crate::{stdlib::format::FormatError, typed::TypeLocError};
 
 fn format_found(list: &[IStr], what: &str) -> String {
 	if list.is_empty() {
@@ -169,13 +166,6 @@
 	Format(#[from] FormatError),
 	#[error("type error: {0}")]
 	TypeError(TypeLocError),
-	#[error("sort error: {0}")]
-	Sort(#[from] SortError),
-
-	/// Thrown as error, as this is legacy feature, and error here
-	/// is acceptable for defeating object field cache
-	#[error("should not reach outside: std.thisFile")]
-	MagicThisFileUsed,
 
 	#[cfg(feature = "anyhow-error")]
 	#[error(transparent)]
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/evaluate/mod.rs
1use std::{cmp::Ordering, rc::Rc};23use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_parser::{6	ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, FieldMember, FieldName, ForSpecData,7	IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc,8};9use jrsonnet_types::ValType;1011use crate::{12	destructure::evaluate_dest,13	error::Error::*,14	evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},15	function::{CallLocation, FuncDesc, FuncVal},16	stdlib::{std_slice, BUILTINS},17	tb, throw,18	typed::Typed,19	val::{ArrValue, CachedUnbound, Thunk, ThunkValue},20	Context, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, State,21	Unbound, Val,22};23pub mod destructure;24pub mod operator;2526pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {27	Val::Func(FuncVal::Normal(Cc::new(FuncDesc {28		name,29		ctx,30		params,31		body,32	})))33}3435pub fn evaluate_field_name(s: State, ctx: Context, field_name: &FieldName) -> Result<Option<IStr>> {36	Ok(match field_name {37		FieldName::Fixed(n) => Some(n.clone()),38		FieldName::Dyn(expr) => s.push(39			CallLocation::new(&expr.1),40			|| "evaluating field name".to_string(),41			|| {42				let value = evaluate(s.clone(), ctx, expr)?;43				if matches!(value, Val::Null) {44					Ok(None)45				} else {46					Ok(Some(IStr::from_untyped(value, s.clone())?))47				}48			},49		)?,50	})51}5253pub fn evaluate_comp(54	s: State,55	ctx: Context,56	specs: &[CompSpec],57	callback: &mut impl FnMut(Context) -> Result<()>,58) -> Result<()> {59	match specs.get(0) {60		None => callback(ctx)?,61		Some(CompSpec::IfSpec(IfSpecData(cond))) => {62			if bool::from_untyped(evaluate(s.clone(), ctx.clone(), cond)?, s.clone())? {63				evaluate_comp(s, ctx, &specs[1..], callback)?;64			}65		}66		Some(CompSpec::ForSpec(ForSpecData(var, expr))) => {67			match evaluate(s.clone(), ctx.clone(), expr)? {68				Val::Arr(list) => {69					for item in list.iter(s.clone()) {70						evaluate_comp(71							s.clone(),72							ctx.clone().with_var(var.clone(), item?.clone()),73							&specs[1..],74							callback,75						)?;76					}77				}78				_ => throw!(InComprehensionCanOnlyIterateOverArray),79			}80		}81	}82	Ok(())83}8485trait CloneableUnbound<T>: Unbound<Bound = T> + Clone {}8687fn evaluate_object_locals(88	fctx: Pending<Context>,89	locals: Rc<Vec<BindSpec>>,90) -> impl CloneableUnbound<Context> {91	#[derive(Trace, Clone)]92	struct UnboundLocals {93		fctx: Pending<Context>,94		locals: Rc<Vec<BindSpec>>,95	}96	impl CloneableUnbound<Context> for UnboundLocals {}97	impl Unbound for UnboundLocals {98		type Bound = Context;99100		fn bind(101			&self,102			_s: State,103			sup: Option<ObjValue>,104			this: Option<ObjValue>,105		) -> Result<Context> {106			let fctx = Context::new_future();107			let mut new_bindings = GcHashMap::new();108			for b in self.locals.iter() {109				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;110			}111112			let ctx = self.fctx.unwrap();113			let new_dollar = ctx.dollar().clone().or_else(|| this.clone());114115			let ctx = ctx116				.extend(new_bindings, new_dollar, sup, this)117				.into_future(fctx);118119			Ok(ctx)120		}121	}122123	UnboundLocals { fctx, locals }124}125126#[allow(clippy::too_many_lines)]127pub fn evaluate_member_list_object(s: State, ctx: Context, members: &[Member]) -> Result<ObjValue> {128	let mut builder = ObjValueBuilder::new();129	let locals = Rc::new(130		members131			.iter()132			.filter_map(|m| match m {133				Member::BindStmt(bind) => Some(bind.clone()),134				_ => None,135			})136			.collect::<Vec<_>>(),137	);138139	let fctx = Context::new_future();140141	// We have single context for all fields, so we can cache binds142	let uctx = CachedUnbound::new(evaluate_object_locals(fctx.clone(), locals));143144	for member in members.iter() {145		match member {146			Member::Field(FieldMember {147				name,148				plus,149				params: None,150				visibility,151				value,152			}) => {153				#[derive(Trace)]154				struct UnboundValue<B: Trace> {155					uctx: B,156					value: LocExpr,157					name: IStr,158				}159				impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {160					type Bound = Thunk<Val>;161					fn bind(162						&self,163						s: State,164						sup: Option<ObjValue>,165						this: Option<ObjValue>,166					) -> Result<Thunk<Val>> {167						Ok(Thunk::evaluated(evaluate_named(168							s.clone(),169							self.uctx.bind(s, sup, this)?,170							&self.value,171							self.name.clone(),172						)?))173					}174				}175176				let name = evaluate_field_name(s.clone(), ctx.clone(), name)?;177				let name = if let Some(name) = name {178					name179				} else {180					continue;181				};182183				builder184					.member(name.clone())185					.with_add(*plus)186					.with_visibility(*visibility)187					.with_location(value.1.clone())188					.bindable(189						s.clone(),190						tb!(UnboundValue {191							uctx: uctx.clone(),192							value: value.clone(),193							name: name.clone()194						}),195					)?;196			}197			Member::Field(FieldMember {198				name,199				params: Some(params),200				value,201				..202			}) => {203				#[derive(Trace)]204				struct UnboundMethod<B: Trace> {205					uctx: B,206					value: LocExpr,207					params: ParamsDesc,208					name: IStr,209				}210				impl<B: Unbound<Bound = Context>> Unbound for UnboundMethod<B> {211					type Bound = Thunk<Val>;212					fn bind(213						&self,214						s: State,215						sup: Option<ObjValue>,216						this: Option<ObjValue>,217					) -> Result<Thunk<Val>> {218						Ok(Thunk::evaluated(evaluate_method(219							self.uctx.bind(s, sup, this)?,220							self.name.clone(),221							self.params.clone(),222							self.value.clone(),223						)))224					}225				}226227				let name = if let Some(name) = evaluate_field_name(s.clone(), ctx.clone(), name)? {228					name229				} else {230					continue;231				};232233				builder234					.member(name.clone())235					.hide()236					.with_location(value.1.clone())237					.bindable(238						s.clone(),239						tb!(UnboundMethod {240							uctx: uctx.clone(),241							value: value.clone(),242							params: params.clone(),243							name: name.clone()244						}),245					)?;246			}247			Member::BindStmt(_) => {}248			Member::AssertStmt(stmt) => {249				#[derive(Trace)]250				struct ObjectAssert<B: Trace> {251					uctx: B,252					assert: AssertStmt,253				}254				impl<B: Unbound<Bound = Context>> ObjectAssertion for ObjectAssert<B> {255					fn run(256						&self,257						s: State,258						sup: Option<ObjValue>,259						this: Option<ObjValue>,260					) -> Result<()> {261						let ctx = self.uctx.bind(s.clone(), sup, this)?;262						evaluate_assert(s, ctx, &self.assert)263					}264				}265				builder.assert(tb!(ObjectAssert {266					uctx: uctx.clone(),267					assert: stmt.clone(),268				}));269			}270		}271	}272	let this = builder.build();273	let _ctx = ctx274		.extend(GcHashMap::new(), None, None, Some(this.clone()))275		.into_future(fctx);276	Ok(this)277}278279pub fn evaluate_object(s: State, ctx: Context, object: &ObjBody) -> Result<ObjValue> {280	Ok(match object {281		ObjBody::MemberList(members) => evaluate_member_list_object(s, ctx, members)?,282		ObjBody::ObjComp(obj) => {283			let mut builder = ObjValueBuilder::new();284			let locals = Rc::new(285				obj.pre_locals286					.iter()287					.chain(obj.post_locals.iter())288					.cloned()289					.collect::<Vec<_>>(),290			);291			let mut ctxs = vec![];292			evaluate_comp(s.clone(), ctx, &obj.compspecs, &mut |ctx| {293				let key = evaluate(s.clone(), ctx.clone(), &obj.key)?;294				let fctx = Context::new_future();295				ctxs.push((ctx, fctx.clone()));296				let uctx = evaluate_object_locals(fctx, locals.clone());297298				match key {299					Val::Null => {}300					Val::Str(n) => {301						#[derive(Trace)]302						struct UnboundValue<B: Trace> {303							uctx: B,304							value: LocExpr,305						}306						impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {307							type Bound = Thunk<Val>;308							fn bind(309								&self,310								s: State,311								sup: Option<ObjValue>,312								this: Option<ObjValue>,313							) -> Result<Thunk<Val>> {314								Ok(Thunk::evaluated(evaluate(315									s.clone(),316									self.uctx.bind(s, sup, this.clone())?.extend(317										GcHashMap::new(),318										None,319										None,320										this,321									),322									&self.value,323								)?))324							}325						}326						builder327							.member(n)328							.with_location(obj.value.1.clone())329							.with_add(obj.plus)330							.bindable(331								s.clone(),332								tb!(UnboundValue {333									uctx,334									value: obj.value.clone(),335								}),336							)?;337					}338					v => throw!(FieldMustBeStringGot(v.value_type())),339				}340341				Ok(())342			})?;343344			let this = builder.build();345			for (ctx, fctx) in ctxs {346				let _ctx = ctx347					.extend(GcHashMap::new(), None, None, Some(this.clone()))348					.into_future(fctx);349			}350			this351		}352	})353}354355pub fn evaluate_apply(356	s: State,357	ctx: Context,358	value: &LocExpr,359	args: &ArgsDesc,360	loc: CallLocation,361	tailstrict: bool,362) -> Result<Val> {363	let value = evaluate(s.clone(), ctx.clone(), value)?;364	Ok(match value {365		Val::Func(f) => {366			let body = || f.evaluate(s.clone(), ctx, loc, args, tailstrict);367			if tailstrict {368				body()?369			} else {370				s.push(loc, || format!("function <{}> call", f.name()), body)?371			}372		}373		v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),374	})375}376377pub fn evaluate_assert(s: State, ctx: Context, assertion: &AssertStmt) -> Result<()> {378	let value = &assertion.0;379	let msg = &assertion.1;380	let assertion_result = s.push(381		CallLocation::new(&value.1),382		|| "assertion condition".to_owned(),383		|| bool::from_untyped(evaluate(s.clone(), ctx.clone(), value)?, s.clone()),384	)?;385	if !assertion_result {386		s.push(387			CallLocation::new(&value.1),388			|| "assertion failure".to_owned(),389			|| {390				if let Some(msg) = msg {391					throw!(AssertionFailed(392						evaluate(s.clone(), ctx, msg)?.to_string(s.clone())?393					));394				}395				throw!(AssertionFailed(Val::Null.to_string(s.clone())?));396			},397		)?;398	}399	Ok(())400}401402pub fn evaluate_named(s: State, ctx: Context, expr: &LocExpr, name: IStr) -> Result<Val> {403	use Expr::*;404	let LocExpr(raw_expr, _loc) = expr;405	Ok(match &**raw_expr {406		Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),407		_ => evaluate(s, ctx, expr)?,408	})409}410411#[allow(clippy::too_many_lines)]412pub fn evaluate(s: State, ctx: Context, expr: &LocExpr) -> Result<Val> {413	use Expr::*;414	let LocExpr(expr, loc) = expr;415	// let bp = with_state(|s| s.0.stop_at.borrow().clone());416	Ok(match &**expr {417		Literal(LiteralType::This) => {418			Val::Obj(ctx.this().clone().ok_or(CantUseSelfOutsideOfObject)?)419		}420		Literal(LiteralType::Super) => Val::Obj(421			ctx.super_obj().clone().ok_or(NoSuperFound)?.with_this(422				ctx.this()423					.clone()424					.expect("if super exists - then this should to"),425			),426		),427		Literal(LiteralType::Dollar) => {428			Val::Obj(ctx.dollar().clone().ok_or(NoTopLevelObjectFound)?)429		}430		Literal(LiteralType::True) => Val::Bool(true),431		Literal(LiteralType::False) => Val::Bool(false),432		Literal(LiteralType::Null) => Val::Null,433		Parened(e) => evaluate(s, ctx, e)?,434		Str(v) => Val::Str(v.clone()),435		Num(v) => Val::new_checked_num(*v)?,436		BinaryOp(v1, o, v2) => evaluate_binary_op_special(s, ctx, v1, *o, v2)?,437		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(s, ctx, v)?)?,438		Var(name) => s.push(439			CallLocation::new(loc),440			|| format!("variable <{}> access", name),441			|| ctx.binding(name.clone())?.evaluate(s.clone()),442		)?,443		Index(value, index) => {444			match (445				evaluate(s.clone(), ctx.clone(), value)?,446				evaluate(s.clone(), ctx, index)?,447			) {448				(Val::Obj(v), Val::Str(key)) => s.push(449					CallLocation::new(loc),450					|| format!("field <{}> access", key),451					|| match v.get(s.clone(), key.clone()) {452						Ok(Some(v)) => Ok(v),453						#[cfg(not(feature = "friendly-errors"))]454						Ok(None) => throw!(NoSuchField(key.clone(), vec![])),455						#[cfg(feature = "friendly-errors")]456						Ok(None) => {457							let mut heap = Vec::new();458							for field in v.fields_ex(459								true,460								#[cfg(feature = "exp-preserve-order")]461								false,462							) {463								let conf = strsim::jaro_winkler(&field as &str, &key as &str);464								if conf < 0.8 {465									continue;466								}467								heap.push((conf, field));468							}469							heap.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(Ordering::Equal));470471							throw!(NoSuchField(472								key.clone(),473								heap.into_iter().map(|(_, v)| v).collect()474							))475						}476						Err(e) if matches!(e.error(), MagicThisFileUsed) => {477							Ok(Val::Str(loc.0.full_path().into()))478						}479						Err(e) => Err(e),480					},481				)?,482				(Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(483					ValType::Obj,484					ValType::Str,485					n.value_type(),486				)),487488				(Val::Arr(v), Val::Num(n)) => {489					if n.fract() > f64::EPSILON {490						throw!(FractionalIndex)491					}492					v.get(s, n as usize)?493						.ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?494				}495				(Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),496				(Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(497					ValType::Arr,498					ValType::Num,499					n.value_type(),500				)),501502				(Val::Str(s), Val::Num(n)) => Val::Str({503					let v: IStr = s504						.chars()505						.skip(n as usize)506						.take(1)507						.collect::<String>()508						.into();509					if v.is_empty() {510						let size = s.chars().count();511						throw!(StringBoundsError(n as usize, size))512					}513					v514				}),515				(Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(516					ValType::Str,517					ValType::Num,518					n.value_type(),519				)),520521				(v, _) => throw!(CantIndexInto(v.value_type())),522			}523		}524		LocalExpr(bindings, returned) => {525			let mut new_bindings: GcHashMap<IStr, Thunk<Val>> =526				GcHashMap::with_capacity(bindings.len());527			let fctx = Context::new_future();528			for b in bindings {529				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;530			}531			let ctx = ctx.extend(new_bindings, None, None, None).into_future(fctx);532			evaluate(s, ctx, &returned.clone())?533		}534		Arr(items) => {535			let mut out = Vec::with_capacity(items.len());536			for item in items {537				// TODO: Implement ArrValue::Lazy with same context for every element?538				#[derive(Trace)]539				struct ArrayElement {540					ctx: Context,541					item: LocExpr,542				}543				impl ThunkValue for ArrayElement {544					type Output = Val;545					fn get(self: Box<Self>, s: State) -> Result<Val> {546						evaluate(s, self.ctx, &self.item)547					}548				}549				out.push(Thunk::new(tb!(ArrayElement {550					ctx: ctx.clone(),551					item: item.clone(),552				})));553			}554			Val::Arr(out.into())555		}556		ArrComp(expr, comp_specs) => {557			let mut out = Vec::new();558			evaluate_comp(s.clone(), ctx, comp_specs, &mut |ctx| {559				out.push(evaluate(s.clone(), ctx, expr)?);560				Ok(())561			})?;562			Val::Arr(ArrValue::Eager(Cc::new(out)))563		}564		Obj(body) => Val::Obj(evaluate_object(s, ctx, body)?),565		ObjExtend(a, b) => evaluate_add_op(566			s.clone(),567			&evaluate(s.clone(), ctx.clone(), a)?,568			&Val::Obj(evaluate_object(s, ctx, b)?),569		)?,570		Apply(value, args, tailstrict) => {571			evaluate_apply(s, ctx, value, args, CallLocation::new(loc), *tailstrict)?572		}573		Function(params, body) => {574			evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())575		}576		Intrinsic(name) => Val::Func(FuncVal::StaticBuiltin(577			BUILTINS578				.with(|b| b.get(name).copied())579				.ok_or_else(|| IntrinsicNotFound(name.clone()))?,580		)),581		IntrinsicThisFile => return Err(MagicThisFileUsed.into()),582		IntrinsicId => Val::Func(FuncVal::identity()),583		AssertExpr(assert, returned) => {584			evaluate_assert(s.clone(), ctx.clone(), assert)?;585			evaluate(s, ctx, returned)?586		}587		ErrorStmt(e) => s.push(588			CallLocation::new(loc),589			|| "error statement".to_owned(),590			|| {591				throw!(RuntimeError(592					evaluate(s.clone(), ctx, e)?.to_string(s.clone())?,593				))594			},595		)?,596		IfElse {597			cond,598			cond_then,599			cond_else,600		} => {601			if s.push(602				CallLocation::new(loc),603				|| "if condition".to_owned(),604				|| bool::from_untyped(evaluate(s.clone(), ctx.clone(), &cond.0)?, s.clone()),605			)? {606				evaluate(s, ctx, cond_then)?607			} else {608				match cond_else {609					Some(v) => evaluate(s, ctx, v)?,610					None => Val::Null,611				}612			}613		}614		Slice(value, desc) => {615			fn parse_idx<T: Typed>(616				loc: CallLocation,617				s: State,618				ctx: &Context,619				expr: &Option<LocExpr>,620				desc: &'static str,621			) -> Result<Option<T>> {622				if let Some(value) = expr {623					Ok(Some(s.push(624						loc,625						|| format!("slice {}", desc),626						|| T::from_untyped(evaluate(s.clone(), ctx.clone(), value)?, s.clone()),627					)?))628				} else {629					Ok(None)630				}631			}632633			let indexable = evaluate(s.clone(), ctx.clone(), value)?;634			let loc = CallLocation::new(loc);635636			let start = parse_idx(loc, s.clone(), &ctx, &desc.start, "start")?;637			let end = parse_idx(loc, s.clone(), &ctx, &desc.end, "end")?;638			let step = parse_idx(loc, s, &ctx, &desc.step, "step")?;639640			std_slice(indexable.into_indexable()?, start, end, step)?641		}642		i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {643			let tmp = loc.clone().0;644			let import_location = tmp645				.path()646				.map(|p| {647					let mut p = p.to_owned();648					p.pop();649					p650				})651				.unwrap_or_default();652			let resolved_path = s.resolve_file(&import_location, path as &str)?;653			match i {654				Import(_) => s.push(655					CallLocation::new(loc),656					|| format!("import {:?}", path.clone()),657					|| s.import(resolved_path.clone()),658				)?,659				ImportStr(_) => Val::Str(s.import_str(resolved_path)?),660				ImportBin(_) => Val::Arr(ArrValue::Bytes(s.import_bin(resolved_path)?)),661				_ => unreachable!(),662			}663		}664	})665}
after · crates/jrsonnet-evaluator/src/evaluate/mod.rs
1use std::{cmp::Ordering, rc::Rc};23use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_parser::{6	ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, FieldMember, FieldName, ForSpecData,7	IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc,8};9use jrsonnet_types::ValType;1011use crate::{12	destructure::evaluate_dest,13	error::Error::*,14	evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},15	function::{CallLocation, FuncDesc, FuncVal},16	tb, throw,17	typed::Typed,18	val::{ArrValue, CachedUnbound, IndexableVal, Thunk, ThunkValue},19	Context, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, State,20	Unbound, Val,21};22pub mod destructure;23pub mod operator;2425pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {26	Val::Func(FuncVal::Normal(Cc::new(FuncDesc {27		name,28		ctx,29		params,30		body,31	})))32}3334pub fn evaluate_field_name(s: State, ctx: Context, field_name: &FieldName) -> Result<Option<IStr>> {35	Ok(match field_name {36		FieldName::Fixed(n) => Some(n.clone()),37		FieldName::Dyn(expr) => s.push(38			CallLocation::new(&expr.1),39			|| "evaluating field name".to_string(),40			|| {41				let value = evaluate(s.clone(), ctx, expr)?;42				if matches!(value, Val::Null) {43					Ok(None)44				} else {45					Ok(Some(IStr::from_untyped(value, s.clone())?))46				}47			},48		)?,49	})50}5152pub fn evaluate_comp(53	s: State,54	ctx: Context,55	specs: &[CompSpec],56	callback: &mut impl FnMut(Context) -> Result<()>,57) -> Result<()> {58	match specs.get(0) {59		None => callback(ctx)?,60		Some(CompSpec::IfSpec(IfSpecData(cond))) => {61			if bool::from_untyped(evaluate(s.clone(), ctx.clone(), cond)?, s.clone())? {62				evaluate_comp(s, ctx, &specs[1..], callback)?;63			}64		}65		Some(CompSpec::ForSpec(ForSpecData(var, expr))) => {66			match evaluate(s.clone(), ctx.clone(), expr)? {67				Val::Arr(list) => {68					for item in list.iter(s.clone()) {69						evaluate_comp(70							s.clone(),71							ctx.clone().with_var(var.clone(), item?.clone()),72							&specs[1..],73							callback,74						)?;75					}76				}77				_ => throw!(InComprehensionCanOnlyIterateOverArray),78			}79		}80	}81	Ok(())82}8384trait CloneableUnbound<T>: Unbound<Bound = T> + Clone {}8586fn evaluate_object_locals(87	fctx: Pending<Context>,88	locals: Rc<Vec<BindSpec>>,89) -> impl CloneableUnbound<Context> {90	#[derive(Trace, Clone)]91	struct UnboundLocals {92		fctx: Pending<Context>,93		locals: Rc<Vec<BindSpec>>,94	}95	impl CloneableUnbound<Context> for UnboundLocals {}96	impl Unbound for UnboundLocals {97		type Bound = Context;9899		fn bind(100			&self,101			_s: State,102			sup: Option<ObjValue>,103			this: Option<ObjValue>,104		) -> Result<Context> {105			let fctx = Context::new_future();106			let mut new_bindings = GcHashMap::new();107			for b in self.locals.iter() {108				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;109			}110111			let ctx = self.fctx.unwrap();112			let new_dollar = ctx.dollar().clone().or_else(|| this.clone());113114			let ctx = ctx115				.extend(new_bindings, new_dollar, sup, this)116				.into_future(fctx);117118			Ok(ctx)119		}120	}121122	UnboundLocals { fctx, locals }123}124125#[allow(clippy::too_many_lines)]126pub fn evaluate_member_list_object(s: State, ctx: Context, members: &[Member]) -> Result<ObjValue> {127	let mut builder = ObjValueBuilder::new();128	let locals = Rc::new(129		members130			.iter()131			.filter_map(|m| match m {132				Member::BindStmt(bind) => Some(bind.clone()),133				_ => None,134			})135			.collect::<Vec<_>>(),136	);137138	let fctx = Context::new_future();139140	// We have single context for all fields, so we can cache binds141	let uctx = CachedUnbound::new(evaluate_object_locals(fctx.clone(), locals));142143	for member in members.iter() {144		match member {145			Member::Field(FieldMember {146				name,147				plus,148				params: None,149				visibility,150				value,151			}) => {152				#[derive(Trace)]153				struct UnboundValue<B: Trace> {154					uctx: B,155					value: LocExpr,156					name: IStr,157				}158				impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {159					type Bound = Thunk<Val>;160					fn bind(161						&self,162						s: State,163						sup: Option<ObjValue>,164						this: Option<ObjValue>,165					) -> Result<Thunk<Val>> {166						Ok(Thunk::evaluated(evaluate_named(167							s.clone(),168							self.uctx.bind(s, sup, this)?,169							&self.value,170							self.name.clone(),171						)?))172					}173				}174175				let name = evaluate_field_name(s.clone(), ctx.clone(), name)?;176				let name = if let Some(name) = name {177					name178				} else {179					continue;180				};181182				builder183					.member(name.clone())184					.with_add(*plus)185					.with_visibility(*visibility)186					.with_location(value.1.clone())187					.bindable(188						s.clone(),189						tb!(UnboundValue {190							uctx: uctx.clone(),191							value: value.clone(),192							name: name.clone()193						}),194					)?;195			}196			Member::Field(FieldMember {197				name,198				params: Some(params),199				value,200				..201			}) => {202				#[derive(Trace)]203				struct UnboundMethod<B: Trace> {204					uctx: B,205					value: LocExpr,206					params: ParamsDesc,207					name: IStr,208				}209				impl<B: Unbound<Bound = Context>> Unbound for UnboundMethod<B> {210					type Bound = Thunk<Val>;211					fn bind(212						&self,213						s: State,214						sup: Option<ObjValue>,215						this: Option<ObjValue>,216					) -> Result<Thunk<Val>> {217						Ok(Thunk::evaluated(evaluate_method(218							self.uctx.bind(s, sup, this)?,219							self.name.clone(),220							self.params.clone(),221							self.value.clone(),222						)))223					}224				}225226				let name = if let Some(name) = evaluate_field_name(s.clone(), ctx.clone(), name)? {227					name228				} else {229					continue;230				};231232				builder233					.member(name.clone())234					.hide()235					.with_location(value.1.clone())236					.bindable(237						s.clone(),238						tb!(UnboundMethod {239							uctx: uctx.clone(),240							value: value.clone(),241							params: params.clone(),242							name: name.clone()243						}),244					)?;245			}246			Member::BindStmt(_) => {}247			Member::AssertStmt(stmt) => {248				#[derive(Trace)]249				struct ObjectAssert<B: Trace> {250					uctx: B,251					assert: AssertStmt,252				}253				impl<B: Unbound<Bound = Context>> ObjectAssertion for ObjectAssert<B> {254					fn run(255						&self,256						s: State,257						sup: Option<ObjValue>,258						this: Option<ObjValue>,259					) -> Result<()> {260						let ctx = self.uctx.bind(s.clone(), sup, this)?;261						evaluate_assert(s, ctx, &self.assert)262					}263				}264				builder.assert(tb!(ObjectAssert {265					uctx: uctx.clone(),266					assert: stmt.clone(),267				}));268			}269		}270	}271	let this = builder.build();272	let _ctx = ctx273		.extend(GcHashMap::new(), None, None, Some(this.clone()))274		.into_future(fctx);275	Ok(this)276}277278pub fn evaluate_object(s: State, ctx: Context, object: &ObjBody) -> Result<ObjValue> {279	Ok(match object {280		ObjBody::MemberList(members) => evaluate_member_list_object(s, ctx, members)?,281		ObjBody::ObjComp(obj) => {282			let mut builder = ObjValueBuilder::new();283			let locals = Rc::new(284				obj.pre_locals285					.iter()286					.chain(obj.post_locals.iter())287					.cloned()288					.collect::<Vec<_>>(),289			);290			let mut ctxs = vec![];291			evaluate_comp(s.clone(), ctx, &obj.compspecs, &mut |ctx| {292				let key = evaluate(s.clone(), ctx.clone(), &obj.key)?;293				let fctx = Context::new_future();294				ctxs.push((ctx, fctx.clone()));295				let uctx = evaluate_object_locals(fctx, locals.clone());296297				match key {298					Val::Null => {}299					Val::Str(n) => {300						#[derive(Trace)]301						struct UnboundValue<B: Trace> {302							uctx: B,303							value: LocExpr,304						}305						impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {306							type Bound = Thunk<Val>;307							fn bind(308								&self,309								s: State,310								sup: Option<ObjValue>,311								this: Option<ObjValue>,312							) -> Result<Thunk<Val>> {313								Ok(Thunk::evaluated(evaluate(314									s.clone(),315									self.uctx.bind(s, sup, this.clone())?.extend(316										GcHashMap::new(),317										None,318										None,319										this,320									),321									&self.value,322								)?))323							}324						}325						builder326							.member(n)327							.with_location(obj.value.1.clone())328							.with_add(obj.plus)329							.bindable(330								s.clone(),331								tb!(UnboundValue {332									uctx,333									value: obj.value.clone(),334								}),335							)?;336					}337					v => throw!(FieldMustBeStringGot(v.value_type())),338				}339340				Ok(())341			})?;342343			let this = builder.build();344			for (ctx, fctx) in ctxs {345				let _ctx = ctx346					.extend(GcHashMap::new(), None, None, Some(this.clone()))347					.into_future(fctx);348			}349			this350		}351	})352}353354pub fn evaluate_apply(355	s: State,356	ctx: Context,357	value: &LocExpr,358	args: &ArgsDesc,359	loc: CallLocation,360	tailstrict: bool,361) -> Result<Val> {362	let value = evaluate(s.clone(), ctx.clone(), value)?;363	Ok(match value {364		Val::Func(f) => {365			let body = || f.evaluate(s.clone(), ctx, loc, args, tailstrict);366			if tailstrict {367				body()?368			} else {369				s.push(loc, || format!("function <{}> call", f.name()), body)?370			}371		}372		v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),373	})374}375376pub fn evaluate_assert(s: State, ctx: Context, assertion: &AssertStmt) -> Result<()> {377	let value = &assertion.0;378	let msg = &assertion.1;379	let assertion_result = s.push(380		CallLocation::new(&value.1),381		|| "assertion condition".to_owned(),382		|| bool::from_untyped(evaluate(s.clone(), ctx.clone(), value)?, s.clone()),383	)?;384	if !assertion_result {385		s.push(386			CallLocation::new(&value.1),387			|| "assertion failure".to_owned(),388			|| {389				if let Some(msg) = msg {390					throw!(AssertionFailed(391						evaluate(s.clone(), ctx, msg)?.to_string(s.clone())?392					));393				}394				throw!(AssertionFailed(Val::Null.to_string(s.clone())?));395			},396		)?;397	}398	Ok(())399}400401pub fn evaluate_named(s: State, ctx: Context, expr: &LocExpr, name: IStr) -> Result<Val> {402	use Expr::*;403	let LocExpr(raw_expr, _loc) = expr;404	Ok(match &**raw_expr {405		Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),406		_ => evaluate(s, ctx, expr)?,407	})408}409410#[allow(clippy::too_many_lines)]411pub fn evaluate(s: State, ctx: Context, expr: &LocExpr) -> Result<Val> {412	use Expr::*;413	let LocExpr(expr, loc) = expr;414	// let bp = with_state(|s| s.0.stop_at.borrow().clone());415	Ok(match &**expr {416		Literal(LiteralType::This) => {417			Val::Obj(ctx.this().clone().ok_or(CantUseSelfOutsideOfObject)?)418		}419		Literal(LiteralType::Super) => {420			Val::Obj(ctx.super_obj().clone().ok_or(NoSuperFound)?.with_this(421				ctx.this()422					.clone()423					.expect("if super exists - then this should to"),424			))425		}426		Literal(LiteralType::Dollar) => {427			Val::Obj(ctx.dollar().clone().ok_or(NoTopLevelObjectFound)?)428		}429		Literal(LiteralType::True) => Val::Bool(true),430		Literal(LiteralType::False) => Val::Bool(false),431		Literal(LiteralType::Null) => Val::Null,432		Parened(e) => evaluate(s, ctx, e)?,433		Str(v) => Val::Str(v.clone()),434		Num(v) => Val::new_checked_num(*v)?,435		BinaryOp(v1, o, v2) => evaluate_binary_op_special(s, ctx, v1, *o, v2)?,436		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(s, ctx, v)?)?,437		Var(name) => s.push(438			CallLocation::new(loc),439			|| format!("variable <{}> access", name),440			|| ctx.binding(name.clone())?.evaluate(s.clone()),441		)?,442		Index(value, index) => {443			match (444				evaluate(s.clone(), ctx.clone(), value)?,445				evaluate(s.clone(), ctx, index)?,446			) {447				(Val::Obj(v), Val::Str(key)) => s.push(448					CallLocation::new(loc),449					|| format!("field <{}> access", key),450					|| match v.get(s.clone(), key.clone()) {451						Ok(Some(v)) => Ok(v),452						#[cfg(not(feature = "friendly-errors"))]453						Ok(None) => throw!(NoSuchField(key.clone(), vec![])),454						#[cfg(feature = "friendly-errors")]455						Ok(None) => {456							let mut heap = Vec::new();457							for field in v.fields_ex(458								true,459								#[cfg(feature = "exp-preserve-order")]460								false,461							) {462								let conf = strsim::jaro_winkler(&field as &str, &key as &str);463								if conf < 0.8 {464									continue;465								}466								heap.push((conf, field));467							}468							heap.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(Ordering::Equal));469470							throw!(NoSuchField(471								key.clone(),472								heap.into_iter().map(|(_, v)| v).collect()473							))474						}475						Err(e) => Err(e),476					},477				)?,478				(Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(479					ValType::Obj,480					ValType::Str,481					n.value_type(),482				)),483484				(Val::Arr(v), Val::Num(n)) => {485					if n.fract() > f64::EPSILON {486						throw!(FractionalIndex)487					}488					v.get(s, n as usize)?489						.ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?490				}491				(Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),492				(Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(493					ValType::Arr,494					ValType::Num,495					n.value_type(),496				)),497498				(Val::Str(s), Val::Num(n)) => Val::Str({499					let v: IStr = s500						.chars()501						.skip(n as usize)502						.take(1)503						.collect::<String>()504						.into();505					if v.is_empty() {506						let size = s.chars().count();507						throw!(StringBoundsError(n as usize, size))508					}509					v510				}),511				(Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(512					ValType::Str,513					ValType::Num,514					n.value_type(),515				)),516517				(v, _) => throw!(CantIndexInto(v.value_type())),518			}519		}520		LocalExpr(bindings, returned) => {521			let mut new_bindings: GcHashMap<IStr, Thunk<Val>> =522				GcHashMap::with_capacity(bindings.len());523			let fctx = Context::new_future();524			for b in bindings {525				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;526			}527			let ctx = ctx.extend(new_bindings, None, None, None).into_future(fctx);528			evaluate(s, ctx, &returned.clone())?529		}530		Arr(items) => {531			let mut out = Vec::with_capacity(items.len());532			for item in items {533				// TODO: Implement ArrValue::Lazy with same context for every element?534				#[derive(Trace)]535				struct ArrayElement {536					ctx: Context,537					item: LocExpr,538				}539				impl ThunkValue for ArrayElement {540					type Output = Val;541					fn get(self: Box<Self>, s: State) -> Result<Val> {542						evaluate(s, self.ctx, &self.item)543					}544				}545				out.push(Thunk::new(tb!(ArrayElement {546					ctx: ctx.clone(),547					item: item.clone(),548				})));549			}550			Val::Arr(out.into())551		}552		ArrComp(expr, comp_specs) => {553			let mut out = Vec::new();554			evaluate_comp(s.clone(), ctx, comp_specs, &mut |ctx| {555				out.push(evaluate(s.clone(), ctx, expr)?);556				Ok(())557			})?;558			Val::Arr(ArrValue::Eager(Cc::new(out)))559		}560		Obj(body) => Val::Obj(evaluate_object(s, ctx, body)?),561		ObjExtend(a, b) => evaluate_add_op(562			s.clone(),563			&evaluate(s.clone(), ctx.clone(), a)?,564			&Val::Obj(evaluate_object(s, ctx, b)?),565		)?,566		Apply(value, args, tailstrict) => {567			evaluate_apply(s, ctx, value, args, CallLocation::new(loc), *tailstrict)?568		}569		Function(params, body) => {570			evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())571		}572		AssertExpr(assert, returned) => {573			evaluate_assert(s.clone(), ctx.clone(), assert)?;574			evaluate(s, ctx, returned)?575		}576		ErrorStmt(e) => s.push(577			CallLocation::new(loc),578			|| "error statement".to_owned(),579			|| {580				throw!(RuntimeError(581					evaluate(s.clone(), ctx, e)?.to_string(s.clone())?,582				))583			},584		)?,585		IfElse {586			cond,587			cond_then,588			cond_else,589		} => {590			if s.push(591				CallLocation::new(loc),592				|| "if condition".to_owned(),593				|| bool::from_untyped(evaluate(s.clone(), ctx.clone(), &cond.0)?, s.clone()),594			)? {595				evaluate(s, ctx, cond_then)?596			} else {597				match cond_else {598					Some(v) => evaluate(s, ctx, v)?,599					None => Val::Null,600				}601			}602		}603		Slice(value, desc) => {604			fn parse_idx<T: Typed>(605				loc: CallLocation,606				s: State,607				ctx: &Context,608				expr: &Option<LocExpr>,609				desc: &'static str,610			) -> Result<Option<T>> {611				if let Some(value) = expr {612					Ok(Some(s.push(613						loc,614						|| format!("slice {}", desc),615						|| T::from_untyped(evaluate(s.clone(), ctx.clone(), value)?, s.clone()),616					)?))617				} else {618					Ok(None)619				}620			}621622			let indexable = evaluate(s.clone(), ctx.clone(), value)?;623			let loc = CallLocation::new(loc);624625			let start = parse_idx(loc, s.clone(), &ctx, &desc.start, "start")?;626			let end = parse_idx(loc, s.clone(), &ctx, &desc.end, "end")?;627			let step = parse_idx(loc, s.clone(), &ctx, &desc.step, "step")?;628629			IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?, s)?630		}631		i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {632			let tmp = loc.clone().0;633			let import_location = tmp634				.path()635				.map(|p| {636					let mut p = p.to_owned();637					p.pop();638					p639				})640				.unwrap_or_default();641			let resolved_path = s.resolve_file(&import_location, path as &str)?;642			match i {643				Import(_) => s.push(644					CallLocation::new(loc),645					|| format!("import {:?}", path.clone()),646					|| s.import(resolved_path.clone()),647				)?,648				ImportStr(_) => Val::Str(s.import_str(resolved_path)?),649				ImportBin(_) => Val::Arr(ArrValue::Bytes(s.import_bin(resolved_path)?)),650				_ => unreachable!(),651			}652		}653	})654}
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()))
-	}
-}