git.delta.rocks / jrsonnet / refs/commits / 150f28648ad3

difftreelog

refactor always use prepared calls

wulxrvxzYaroslav Bolyukin2026-04-04parent: #0f5b614.patch.diff
in: master

17 files changed

modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/evaluate/mod.rs
1use std::rc::Rc;23use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_ir::{6	ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, ExprParams, FieldMember,7	FieldName, ForSpecData, IfSpecData, ImportKind, LiteralType, ObjBody, ObjMembers, Spanned,8	function::ParamName,9};10use jrsonnet_types::ValType;11use rustc_hash::FxHashMap;1213use self::destructure::destruct;14use crate::{15	Context, Error, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, ResultExt,16	SupThis, Unbound, Val,17	arr::ArrValue,18	bail,19	destructure::evaluate_dest,20	error::{ErrorKind::*, suggest_object_fields},21	evaluate::operator::{evaluate_binary_op_special, evaluate_unary_op},22	function::{CallLocation, FuncDesc, FuncVal},23	gc::WithCapacityExt as _,24	in_frame,25	typed::{FromUntyped, IntoUntyped as _, Typed},26	val::{CachedUnbound, IndexableVal, NumValue, StrValue, Thunk},27	with_state,28};29pub mod destructure;30pub mod operator;3132// This is the amount of bytes that need to be left on the stack before increasing the size.33// It must be at least as large as the stack required by any code that does not call34// `ensure_sufficient_stack`.35const RED_ZONE: usize = 100 * 1024; // 100k3637// Only the first stack that is pushed, grows exponentially (2^n * STACK_PER_RECURSION) from then38// on. This flag has performance relevant characteristics. Don't set it too high.39const STACK_PER_RECURSION: usize = 1024 * 1024; // 1MB4041/// Grows the stack on demand to prevent stack overflow. Call this in strategic locations42/// to "break up" recursive calls. E.g. almost any call to `visit_expr` or equivalent can benefit43/// from this.44///45/// Should not be sprinkled around carelessly, as it causes a little bit of overhead.46#[inline]47pub fn ensure_sufficient_stack<R>(f: impl FnOnce() -> R) -> R {48	stacker::maybe_grow(RED_ZONE, STACK_PER_RECURSION, f)49}5051pub fn evaluate_trivial(expr: &Expr) -> Option<Val> {52	fn is_trivial(expr: &Expr) -> bool {53		match expr {54			Expr::Str(_)55			| Expr::Num(_)56			| Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,57			Expr::Arr(a) => a.iter().all(is_trivial),58			_ => false,59		}60	}61	Some(match expr {62		Expr::Str(s) => Val::string(s.clone()),63		Expr::Num(n) => {64			Val::Num(NumValue::new(*n).expect("parser will not allow non-finite values"))65		}66		Expr::Literal(LiteralType::False) => Val::Bool(false),67		Expr::Literal(LiteralType::True) => Val::Bool(true),68		Expr::Literal(LiteralType::Null) => Val::Null,69		Expr::Arr(n) => {70			if n.iter().any(|e| !is_trivial(e)) {71				return None;72			}73			Val::Arr(74				n.iter()75					.map(evaluate_trivial)76					.map(|e| e.expect("checked trivial"))77					.collect(),78			)79		}80		_ => return None,81	})82}8384pub fn evaluate_method(ctx: Context, name: IStr, params: ExprParams, body: Rc<Expr>) -> Val {85	Val::Func(FuncVal::Normal(Cc::new(FuncDesc {86		name,87		ctx,88		params,89		body,90	})))91}9293pub fn evaluate_field_name(ctx: Context, field_name: &Spanned<FieldName>) -> Result<Option<IStr>> {94	Ok(match &field_name.value {95		FieldName::Fixed(n) => Some(n.clone()),96		FieldName::Dyn(expr) => in_frame(97			CallLocation::new(&field_name.span),98			|| "evaluating field name".to_string(),99			|| {100				let v = evaluate(ctx, expr)?;101				Ok(if matches!(v, Val::Null) {102					None103				} else {104					Some(IStr::from_untyped(v)?)105				})106			},107		)?,108	})109}110111pub fn evaluate_comp(112	ctx: Context,113	specs: &[CompSpec],114	mut guaranteed_reserve: usize,115	callback: &mut impl FnMut(Context, usize) -> Result<()>,116) -> Result<()> {117	match specs.first() {118		None => callback(ctx, guaranteed_reserve)?,119		Some(CompSpec::IfSpec(IfSpecData { cond, span: _ })) => {120			if bool::from_untyped(evaluate(ctx.clone(), cond)?)? {121				evaluate_comp(ctx, &specs[1..], 0, callback)?;122			}123		}124		Some(CompSpec::ForSpec(ForSpecData {125			destruct: into,126			over,127		})) => {128			match evaluate(ctx.clone(), over)? {129				Val::Arr(list) => {130					guaranteed_reserve = guaranteed_reserve.max(1) * list.len();131					for (i, item) in list.iter_lazy().enumerate() {132						let fctx = Pending::new();133						let mut new_bindings = FxHashMap::with_capacity(into.binds_len());134						destruct(into, item, fctx.clone(), &mut new_bindings)?;135						let ctx = ctx.clone().extend_bindings(new_bindings).into_future(fctx);136137						let specs = &specs[1..];138						evaluate_comp(139							ctx,140							specs,141							if i == 0 || !specs.is_empty() {142								guaranteed_reserve143							} else {144								0145							},146							callback,147						)?;148					}149				}150				#[cfg(feature = "exp-object-iteration")]151				Val::Obj(obj) => {152					let fields = obj.fields(153						// TODO: Should there be ability to preserve iteration order?154						#[cfg(feature = "exp-preserve-order")]155						false,156					);157					guaranteed_reserve = guaranteed_reserve.max(1) * fields.len();158					for field in fields {159						let fctx = Pending::new();160						let mut new_bindings = FxHashMap::with_capacity(into.binds_len());161						let obj = obj.clone();162						let value = Thunk::evaluated(Val::arr(vec![163							Thunk::evaluated(Val::string(field.clone())),164							obj.get_lazy(field).transpose().expect(165								"field exists, as field name was obtained from object.fields()",166							),167						]));168						destruct(into, value, fctx.clone(), &mut new_bindings)?;169						let ctx = ctx.clone().extend_bindings(new_bindings).into_future(fctx);170171						evaluate_comp(ctx, &specs[1..], callback)?;172					}173				}174				_ => bail!(InComprehensionCanOnlyIterateOverArray),175			}176		}177	}178	Ok(())179}180181fn evaluate_arr_comp(ctx: Context, expr: &Rc<Expr>, comp_specs: &[CompSpec]) -> Result<ArrValue> {182	'eager: {183		let mut out = Vec::new();184185		if evaluate_comp(ctx.clone(), comp_specs, 0, &mut |ctx, reserve| {186			if reserve != 0 {187				out.reserve(reserve);188			}189			out.push(evaluate(ctx, expr)?);190			Ok(())191		})192		.is_err()193		{194			break 'eager;195		}196197		return Ok(ArrValue::new(out));198	};199	let mut out = Vec::new();200	evaluate_comp(ctx, comp_specs, 0, &mut |ctx, reserve| {201		if reserve != 0 {202			out.reserve(reserve);203		}204		let expr = expr.clone();205		out.push(Thunk!(move || evaluate(ctx, &expr)));206		Ok(())207	})?;208	Ok(ArrValue::new(out))209}210211trait CloneableUnbound<T>: Unbound<Bound = T> + Clone {}212impl<V, T> CloneableUnbound<T> for V where V: Unbound<Bound = T> + Clone {}213214fn evaluate_object_locals(215	fctx: Context,216	locals: Rc<Vec<BindSpec>>,217) -> impl CloneableUnbound<Context> {218	#[derive(Trace, Clone)]219	struct UnboundLocals {220		fctx: Context,221		locals: Rc<Vec<BindSpec>>,222	}223	impl Unbound for UnboundLocals {224		type Bound = Context;225226		fn bind(&self, sup_this: SupThis) -> Result<Context> {227			let fctx = Context::new_future();228			let mut new_bindings =229				FxHashMap::with_capacity(self.locals.iter().map(BindSpec::binds_len).sum());230			for b in self.locals.iter() {231				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;232			}233234			let ctx = self.fctx.clone();235236			let ctx = ctx237				.extend_bindings_sup_this(new_bindings, sup_this)238				.into_future(fctx);239240			Ok(ctx)241		}242	}243244	UnboundLocals { fctx, locals }245}246247pub fn evaluate_field_member<B: Unbound<Bound = Context> + Clone>(248	builder: &mut ObjValueBuilder,249	ctx: Context,250	uctx: B,251	field: &FieldMember,252) -> Result<()> {253	let name = evaluate_field_name(ctx, &field.name)?;254	let Some(name) = name else {255		return Ok(());256	};257258	match field {259		FieldMember {260			plus,261			params: None,262			visibility,263			value,264			..265		} => {266			#[derive(Trace)]267			struct UnboundValue<B: Trace> {268				uctx: B,269				value: Rc<Expr>,270				name: IStr,271			}272			impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {273				type Bound = Val;274				fn bind(&self, sup_this: SupThis) -> Result<Val> {275					evaluate_named(self.uctx.bind(sup_this)?, &self.value, self.name.clone())276				}277			}278279			builder280				.field(name.clone())281				.with_add(*plus)282				.with_visibility(*visibility)283				.with_location(field.name.span.clone())284				.bindable(UnboundValue {285					uctx,286					value: value.clone(),287					name,288				})?;289		}290		FieldMember {291			params: Some(params),292			visibility,293			value,294			..295		} => {296			#[derive(Trace)]297			struct UnboundMethod<B: Trace> {298				uctx: B,299				value: Rc<Expr>,300				params: ExprParams,301				name: IStr,302			}303			impl<B: Unbound<Bound = Context>> Unbound for UnboundMethod<B> {304				type Bound = Val;305				fn bind(&self, sup_this: SupThis) -> Result<Val> {306					Ok(evaluate_method(307						self.uctx.bind(sup_this)?,308						self.name.clone(),309						self.params.clone(),310						self.value.clone(),311					))312				}313			}314315			builder316				.field(name.clone())317				.with_visibility(*visibility)318				// .with_location(value.span())319				.bindable(UnboundMethod {320					uctx,321					value: value.clone(),322					params: params.clone(),323					name,324				})?;325		}326	}327	Ok(())328}329330#[derive(Trace, Clone)]331struct DirectUnbound(Context);332impl Unbound for DirectUnbound {333	type Bound = Context;334	fn bind(&self, sup_this: SupThis) -> Result<Context> {335		Ok(self336			.0337			.clone()338			.extend_bindings_sup_this(FxHashMap::new(), sup_this))339	}340}341342#[allow(clippy::too_many_lines)]343pub fn evaluate_member_list_object(344	super_obj: Option<ObjValue>,345	ctx: Context,346	members: &ObjMembers,347) -> Result<ObjValue> {348	#[derive(Trace)]349	struct ObjectAssert<B: Trace> {350		uctx: B,351		asserts: Rc<Vec<AssertStmt>>,352	}353	impl<B: Unbound<Bound = Context>> ObjectAssertion for ObjectAssert<B> {354		fn run(&self, sup_this: SupThis) -> Result<()> {355			let ctx = self.uctx.bind(sup_this)?;356			for assert in &*self.asserts {357				evaluate_assert(ctx.clone(), assert)?;358			}359			Ok(())360		}361	}362363	let mut builder = ObjValueBuilder::new();364	if let Some(super_obj) = super_obj {365		builder.with_super(super_obj);366	}367368	if members.locals.is_empty() {369		// We can use the same context for all field evaluation, it doesn't depends on locals, only on this/super370		let uctx = DirectUnbound(ctx.clone());371		for field in &members.fields {372			evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;373		}374		if !members.asserts.is_empty() {375			builder.assert(ObjectAssert {376				uctx,377				asserts: members.asserts.clone(),378			});379		}380	} else {381		let locals = members.locals.clone();382		// We have single context for all fields, so we can cache them together383		let uctx = CachedUnbound::new(evaluate_object_locals(ctx.clone(), locals));384		for field in &members.fields {385			evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;386		}387		if !members.asserts.is_empty() {388			builder.assert(ObjectAssert {389				uctx,390				asserts: members.asserts.clone(),391			});392		}393	}394395	Ok(builder.build())396}397398pub fn evaluate_object(399	super_obj: Option<ObjValue>,400	ctx: Context,401	object: &ObjBody,402) -> Result<ObjValue> {403	Ok(match object {404		ObjBody::MemberList(members) => evaluate_member_list_object(super_obj, ctx, members)?,405		ObjBody::ObjComp(obj) => {406			let mut builder = ObjValueBuilder::new();407			if let Some(super_obj) = super_obj {408				builder.with_super(super_obj);409			}410			let locals = obj.locals.clone();411			evaluate_comp(ctx, &obj.compspecs, 0, &mut |ctx, reserve| {412				let uctx = evaluate_object_locals(ctx.clone(), locals.clone());413				builder.reserve_cores(reserve);414415				evaluate_field_member(&mut builder, ctx, uctx, &obj.field)416			})?;417418			builder.build()419		}420	})421}422423pub fn evaluate_apply(424	ctx: Context,425	value: &Expr,426	args: &ArgsDesc,427	loc: CallLocation<'_>,428	tailstrict: bool,429) -> Result<Val> {430	let value = evaluate(ctx.clone(), value)?;431	Ok(match value {432		Val::Func(f) => {433			let body = || f.evaluate(ctx, loc, args, tailstrict);434			if tailstrict {435				body()?436			} else {437				in_frame(loc, || format!("function <{}> call", f.name()), body)?438			}439		}440		v => bail!(OnlyFunctionsCanBeCalledGot(v.value_type())),441	})442}443444pub fn evaluate_assert(ctx: Context, assertion: &AssertStmt) -> Result<()> {445	let value = &assertion.0;446	let msg = &assertion.1;447	let assertion_result = in_frame(448		CallLocation::new(&value.span),449		|| "assertion condition".to_owned(),450		|| bool::from_untyped(evaluate(ctx.clone(), value)?),451	)?;452	if !assertion_result {453		in_frame(454			CallLocation::new(&value.span),455			|| "assertion failure".to_owned(),456			|| {457				if let Some(msg) = msg {458					bail!(AssertionFailed(evaluate(ctx, msg)?.to_string()?));459				}460				bail!(AssertionFailed(Val::Null.to_string()?));461			},462		)?;463	}464	Ok(())465}466467pub fn evaluate_named_param(ctx: Context, expr: &Expr, name: ParamName) -> Result<Val> {468	match name {469		ParamName::Named(name) => evaluate_named(ctx, expr, name),470		ParamName::Unnamed => evaluate(ctx, expr),471	}472}473474pub fn evaluate_named(ctx: Context, expr: &Expr, name: IStr) -> Result<Val> {475	use Expr::*;476	Ok(match expr {477		Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),478		_ => evaluate(ctx, expr)?,479	})480}481482#[allow(clippy::too_many_lines)]483pub fn evaluate(ctx: Context, expr: &Expr) -> Result<Val> {484	use Expr::*;485486	Ok(match expr {487		Literal(LiteralType::This) => Val::Obj(ctx.try_this()?),488		Literal(LiteralType::Super) => Val::Obj(ctx.try_sup_this()?.standalone_super()?),489		Literal(LiteralType::Dollar) => Val::Obj(ctx.try_dollar()?),490		Literal(LiteralType::True) => Val::Bool(true),491		Literal(LiteralType::False) => Val::Bool(false),492		Literal(LiteralType::Null) => Val::Null,493		Str(v) => Val::string(v.clone()),494		Num(v) => Val::try_num(*v)?,495		// I have tried to remove special behavior from super by implementing standalone-super496		// expresion, but looks like this case still needs special treatment.497		//498		// Note that other jsonnet implementations will fail on `if value in (super)` expression,499		// because the standalone super literal is not supported, that is because in other500		// implementations `in super` treated differently from `in smth_else`.501		BinaryOp(bin)502			if matches!(&bin.rhs, Expr::Literal(LiteralType::Super))503				&& bin.op == BinaryOpType::In =>504		{505			let sup_this = ctx.try_sup_this()?;506			// In jsonnet, "field" in e is eager, LHS expression is always executed regardless of super existence.507			// In jrsonnet, however, this wasn't true, this was kept here for compatibility.508			if !sup_this.has_super() {509				return Ok(Val::Bool(false));510			}511			let field = evaluate(ctx, &bin.lhs)?;512			Val::Bool(sup_this.field_in_super(field.to_string()?))513		}514		BinaryOp(bin) => evaluate_binary_op_special(ctx, &bin.lhs, bin.op, &bin.rhs)?,515		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,516		Var(name) => in_frame(517			CallLocation::new(&name.span),518			|| format!("local <{}> access", &**name),519			|| ctx.binding((**name).clone())?.evaluate(),520		)?,521		Index { indexable, parts } => ensure_sufficient_stack(|| {522			let mut parts = parts.iter();523			let mut indexable = if matches!(&**indexable, Expr::Literal(LiteralType::Super)) {524				let part = parts.next().expect("at least part should exist");525				// sup_this existence check might also be skipped here for null-coalesce...526				// But I believe this might cause errors.527				let sup_this = ctx.try_sup_this()?;528				if !sup_this.has_super() {529					#[cfg(feature = "exp-null-coaelse")]530					if part.null_coaelse {531						return Ok(Val::Null);532					}533					bail!(NoSuperFound)534				}535				let name = evaluate(ctx.clone(), &part.value)?;536537				let Val::Str(name) = name else {538					bail!(ValueIndexMustBeTypeGot(539						ValType::Obj,540						ValType::Str,541						name.value_type(),542					))543				};544545				let name = name.into_flat();546				match sup_this547					.get_super(name.clone())548					.with_description_src(&part.span, || format!("field <{name}> access"))?549				{550					Some(v) => v,551					#[cfg(feature = "exp-null-coaelse")]552					None if part.null_coaelse => return Ok(Val::Null),553					None => {554						let suggestions = suggest_object_fields(555							&sup_this.standalone_super().expect("super exists"),556							name.clone(),557						);558559						bail!(NoSuchField(name, suggestions))560					}561				}562			} else {563				evaluate(ctx.clone(), indexable)?564			};565566			for part in parts {567				indexable = match (indexable, evaluate(ctx.clone(), &part.value)?) {568					(Val::Obj(v), Val::Str(key)) => match v569						.get(key.clone().into_flat())570						.with_description_src(&part.span, || format!("field <{key}> access"))?571					{572						Some(v) => v,573						#[cfg(feature = "exp-null-coaelse")]574						None if part.null_coaelse => return Ok(Val::Null),575						None => {576							let suggestions = suggest_object_fields(&v, key.into_flat());577578							return Err(Error::from(NoSuchField(579								key.clone().into_flat(),580								suggestions,581							)))582							.with_description_src(&part.span, || format!("field <{key}> access"));583						}584					},585					(Val::Obj(_), n) => bail!(ValueIndexMustBeTypeGot(586						ValType::Obj,587						ValType::Str,588						n.value_type(),589					)),590					(Val::Arr(v), Val::Num(n)) => {591						let n = n.get();592						if n.fract() > f64::EPSILON {593							bail!(FractionalIndex)594						}595						if n < 0.0 {596							#[expect(597								clippy::cast_possible_truncation,598								reason = "it would be truncated anyway"599							)]600							let n = n as isize;601							bail!(ArrayBoundsError(n, v.len()));602						}603						#[expect(604							clippy::cast_possible_truncation,605							clippy::cast_sign_loss,606							reason = "n is checked postive"607						)]608						v.get(n as usize)?609							.ok_or_else(|| ArrayBoundsError(n as isize, v.len()))?610					}611					(Val::Arr(_), Val::Str(n)) => {612						bail!(AttemptedIndexAnArrayWithString(n.into_flat()))613					}614					(Val::Arr(_), n) => bail!(ValueIndexMustBeTypeGot(615						ValType::Arr,616						ValType::Num,617						n.value_type(),618					)),619620					(Val::Str(s), Val::Num(n)) => Val::Str({621						let n = n.get();622						if n.fract() > f64::EPSILON {623							bail!(FractionalIndex)624						}625						if n < 0.0 {626							#[expect(627								clippy::cast_possible_truncation,628								reason = "it would be truncated anyway"629							)]630							let n = n as isize;631							bail!(ArrayBoundsError(n, s.into_flat().chars().count()));632						}633						#[expect(634							clippy::cast_sign_loss,635							clippy::cast_possible_truncation,636							reason = "n is positive, overflow will truncate as expected"637						)]638						let n = n as usize;639						let v: IStr = s640							.clone()641							.into_flat()642							.chars()643							.skip(n)644							.take(1)645							.collect::<String>()646							.into();647						if v.is_empty() {648							bail!(StringBoundsError(n, s.into_flat().chars().count()))649						}650						StrValue::Flat(v)651					}),652					(Val::Str(_), n) => bail!(ValueIndexMustBeTypeGot(653						ValType::Str,654						ValType::Num,655						n.value_type(),656					)),657					#[cfg(feature = "exp-null-coaelse")]658					(Val::Null, _) if part.null_coaelse => return Ok(Val::Null),659					(v, _) => bail!(CantIndexInto(v.value_type())),660				};661			}662			Ok(indexable)663		})?,664		LocalExpr(bindings, returned) => {665			let mut new_bindings: FxHashMap<IStr, Thunk<Val>> =666				FxHashMap::with_capacity(bindings.iter().map(BindSpec::binds_len).sum());667			let fctx = Context::new_future();668			for b in bindings {669				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;670			}671			let ctx = ctx.extend_bindings(new_bindings).into_future(fctx);672			evaluate(ctx, returned)?673		}674		Arr(items) => {675			if items.is_empty() {676				Val::arr(())677			} else {678				Val::Arr(ArrValue::expr(ctx, items.clone()))679			}680		}681		ArrComp(expr, comp_specs) => Val::Arr(evaluate_arr_comp(ctx, expr, comp_specs)?),682		Obj(body) => Val::Obj(evaluate_object(None, ctx, body)?),683		ObjExtend(a, b) => {684			let base = evaluate(ctx.clone(), a)?;685			match base {686				Val::Obj(base_obj) => Val::Obj(evaluate_object(Some(base_obj), ctx, b)?),687				_ => bail!("ObjExtend lhs should be an object value"),688			}689		}690		Apply(value, args, tailstrict) => ensure_sufficient_stack(|| {691			evaluate_apply(ctx, value, args, CallLocation::new(&args.span), *tailstrict)692		})?,693		Function(params, body) => {694			evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())695		}696		AssertExpr(assert) => {697			evaluate_assert(ctx.clone(), &assert.assert)?;698			evaluate(ctx, &assert.rest)?699		}700		ErrorStmt(s, e) => in_frame(701			CallLocation::new(s),702			|| "error statement".to_owned(),703			|| bail!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),704		)?,705		IfElse(if_else) => {706			if in_frame(707				CallLocation::new(&if_else.cond.span),708				|| "if condition".to_owned(),709				|| bool::from_untyped(evaluate(ctx.clone(), &if_else.cond.cond)?),710			)? {711				evaluate(ctx, &if_else.cond_then)?712			} else {713				match &if_else.cond_else {714					Some(v) => evaluate(ctx, v)?,715					None => Val::Null,716				}717			}718		}719		Slice(slice) => {720			fn parse_idx<T: Typed + FromUntyped>(721				ctx: Context,722				expr: Option<&Spanned<Expr>>,723				desc: &'static str,724			) -> Result<Option<T>> {725				if let Some(value) = expr {726					Ok(in_frame(727						CallLocation::new(&value.span),728						|| format!("slice {desc}"),729						|| <Option<T>>::from_untyped(evaluate(ctx, value)?),730					)?)731				} else {732					Ok(None)733				}734			}735736			let indexable = evaluate(ctx.clone(), &slice.value)?;737738			let start = parse_idx(ctx.clone(), slice.slice.start.as_ref(), "start")?;739			let end = parse_idx(ctx.clone(), slice.slice.end.as_ref(), "end")?;740			let step = parse_idx(ctx, slice.slice.step.as_ref(), "step")?;741742			IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?743		}744		Import(kind, path) => {745			let Expr::Str(path) = &**path else {746				bail!("computed imports are not supported")747			};748			with_state(|s| {749				let span = &kind.span;750				let resolved_path = s.resolve_from(span.0.source_path(), path)?;751				Ok(match &**kind {752					ImportKind::Normal => in_frame(753						CallLocation::new(span),754						|| format!("import {:?}", path.clone()),755						|| s.import_resolved(resolved_path),756					)?,757					ImportKind::Str => Val::string(s.import_resolved_str(resolved_path)?),758					ImportKind::Bin => Val::arr(s.import_resolved_bin(resolved_path)?),759				}) as Result<Val>760			})?761		}762	})763}
after · crates/jrsonnet-evaluator/src/evaluate/mod.rs
1use std::rc::Rc;23use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_ir::{6	ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, ExprParams, FieldMember,7	FieldName, ForSpecData, IfSpecData, ImportKind, LiteralType, ObjBody, ObjMembers, Spanned,8	function::ParamName,9};10use jrsonnet_types::ValType;11use rustc_hash::FxHashMap;1213use self::destructure::destruct;14use crate::{15	Context, Error, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, ResultExt,16	SupThis, Unbound, Val,17	arr::ArrValue,18	bail,19	destructure::evaluate_dest,20	error::{ErrorKind::*, suggest_object_fields},21	evaluate::operator::{evaluate_binary_op_special, evaluate_unary_op},22	function::{CallLocation, FuncDesc, FuncVal, PreparedFuncVal},23	gc::WithCapacityExt as _,24	in_frame,25	typed::{FromUntyped, IntoUntyped as _, Typed},26	val::{CachedUnbound, IndexableVal, NumValue, StrValue, Thunk},27	with_state,28};29pub mod destructure;30pub mod operator;3132// This is the amount of bytes that need to be left on the stack before increasing the size.33// It must be at least as large as the stack required by any code that does not call34// `ensure_sufficient_stack`.35const RED_ZONE: usize = 100 * 1024; // 100k3637// Only the first stack that is pushed, grows exponentially (2^n * STACK_PER_RECURSION) from then38// on. This flag has performance relevant characteristics. Don't set it too high.39const STACK_PER_RECURSION: usize = 1024 * 1024; // 1MB4041/// Grows the stack on demand to prevent stack overflow. Call this in strategic locations42/// to "break up" recursive calls. E.g. almost any call to `visit_expr` or equivalent can benefit43/// from this.44///45/// Should not be sprinkled around carelessly, as it causes a little bit of overhead.46#[inline]47pub fn ensure_sufficient_stack<R>(f: impl FnOnce() -> R) -> R {48	stacker::maybe_grow(RED_ZONE, STACK_PER_RECURSION, f)49}5051pub fn evaluate_trivial(expr: &Expr) -> Option<Val> {52	fn is_trivial(expr: &Expr) -> bool {53		match expr {54			Expr::Str(_)55			| Expr::Num(_)56			| Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,57			Expr::Arr(a) => a.iter().all(is_trivial),58			_ => false,59		}60	}61	Some(match expr {62		Expr::Str(s) => Val::string(s.clone()),63		Expr::Num(n) => {64			Val::Num(NumValue::new(*n).expect("parser will not allow non-finite values"))65		}66		Expr::Literal(LiteralType::False) => Val::Bool(false),67		Expr::Literal(LiteralType::True) => Val::Bool(true),68		Expr::Literal(LiteralType::Null) => Val::Null,69		Expr::Arr(n) => {70			if n.iter().any(|e| !is_trivial(e)) {71				return None;72			}73			Val::Arr(74				n.iter()75					.map(evaluate_trivial)76					.map(|e| e.expect("checked trivial"))77					.collect(),78			)79		}80		_ => return None,81	})82}8384pub fn evaluate_method(ctx: Context, name: IStr, params: ExprParams, body: Rc<Expr>) -> Val {85	Val::Func(FuncVal::Normal(Cc::new(FuncDesc {86		name,87		ctx,88		params,89		body,90	})))91}9293pub fn evaluate_field_name(ctx: Context, field_name: &Spanned<FieldName>) -> Result<Option<IStr>> {94	Ok(match &field_name.value {95		FieldName::Fixed(n) => Some(n.clone()),96		FieldName::Dyn(expr) => in_frame(97			CallLocation::new(&field_name.span),98			|| "evaluating field name".to_string(),99			|| {100				let v = evaluate(ctx, expr)?;101				Ok(if matches!(v, Val::Null) {102					None103				} else {104					Some(IStr::from_untyped(v)?)105				})106			},107		)?,108	})109}110111pub fn evaluate_comp(112	ctx: Context,113	specs: &[CompSpec],114	mut guaranteed_reserve: usize,115	callback: &mut impl FnMut(Context, usize) -> Result<()>,116) -> Result<()> {117	match specs.first() {118		None => callback(ctx, guaranteed_reserve)?,119		Some(CompSpec::IfSpec(IfSpecData { cond, span: _ })) => {120			if bool::from_untyped(evaluate(ctx.clone(), cond)?)? {121				evaluate_comp(ctx, &specs[1..], 0, callback)?;122			}123		}124		Some(CompSpec::ForSpec(ForSpecData {125			destruct: into,126			over,127		})) => {128			match evaluate(ctx.clone(), over)? {129				Val::Arr(list) => {130					guaranteed_reserve = guaranteed_reserve.max(1) * list.len();131					for (i, item) in list.iter_lazy().enumerate() {132						let fctx = Pending::new();133						let mut new_bindings = FxHashMap::with_capacity(into.binds_len());134						destruct(into, item, fctx.clone(), &mut new_bindings)?;135						let ctx = ctx.clone().extend_bindings(new_bindings).into_future(fctx);136137						let specs = &specs[1..];138						evaluate_comp(139							ctx,140							specs,141							if i == 0 || !specs.is_empty() {142								guaranteed_reserve143							} else {144								0145							},146							callback,147						)?;148					}149				}150				#[cfg(feature = "exp-object-iteration")]151				Val::Obj(obj) => {152					let fields = obj.fields(153						// TODO: Should there be ability to preserve iteration order?154						#[cfg(feature = "exp-preserve-order")]155						false,156					);157					guaranteed_reserve = guaranteed_reserve.max(1) * fields.len();158					for field in fields {159						let fctx = Pending::new();160						let mut new_bindings = FxHashMap::with_capacity(into.binds_len());161						let obj = obj.clone();162						let value = Thunk::evaluated(Val::arr(vec![163							Thunk::evaluated(Val::string(field.clone())),164							obj.get_lazy(field).transpose().expect(165								"field exists, as field name was obtained from object.fields()",166							),167						]));168						destruct(into, value, fctx.clone(), &mut new_bindings)?;169						let ctx = ctx.clone().extend_bindings(new_bindings).into_future(fctx);170171						evaluate_comp(ctx, &specs[1..], callback)?;172					}173				}174				_ => bail!(InComprehensionCanOnlyIterateOverArray),175			}176		}177	}178	Ok(())179}180181fn evaluate_arr_comp(ctx: Context, expr: &Rc<Expr>, comp_specs: &[CompSpec]) -> Result<ArrValue> {182	'eager: {183		let mut out = Vec::new();184185		if evaluate_comp(ctx.clone(), comp_specs, 0, &mut |ctx, reserve| {186			if reserve != 0 {187				out.reserve(reserve);188			}189			out.push(evaluate(ctx, expr)?);190			Ok(())191		})192		.is_err()193		{194			break 'eager;195		}196197		return Ok(ArrValue::new(out));198	};199	let mut out = Vec::new();200	evaluate_comp(ctx, comp_specs, 0, &mut |ctx, reserve| {201		if reserve != 0 {202			out.reserve(reserve);203		}204		let expr = expr.clone();205		out.push(Thunk!(move || evaluate(ctx, &expr)));206		Ok(())207	})?;208	Ok(ArrValue::new(out))209}210211trait CloneableUnbound<T>: Unbound<Bound = T> + Clone {}212impl<V, T> CloneableUnbound<T> for V where V: Unbound<Bound = T> + Clone {}213214fn evaluate_object_locals(215	fctx: Context,216	locals: Rc<Vec<BindSpec>>,217) -> impl CloneableUnbound<Context> {218	#[derive(Trace, Clone)]219	struct UnboundLocals {220		fctx: Context,221		locals: Rc<Vec<BindSpec>>,222	}223	impl Unbound for UnboundLocals {224		type Bound = Context;225226		fn bind(&self, sup_this: SupThis) -> Result<Context> {227			let fctx = Context::new_future();228			let mut new_bindings =229				FxHashMap::with_capacity(self.locals.iter().map(BindSpec::binds_len).sum());230			for b in self.locals.iter() {231				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;232			}233234			let ctx = self.fctx.clone();235236			let ctx = ctx237				.extend_bindings_sup_this(new_bindings, sup_this)238				.into_future(fctx);239240			Ok(ctx)241		}242	}243244	UnboundLocals { fctx, locals }245}246247pub fn evaluate_field_member<B: Unbound<Bound = Context> + Clone>(248	builder: &mut ObjValueBuilder,249	ctx: Context,250	uctx: B,251	field: &FieldMember,252) -> Result<()> {253	let name = evaluate_field_name(ctx, &field.name)?;254	let Some(name) = name else {255		return Ok(());256	};257258	match field {259		FieldMember {260			plus,261			params: None,262			visibility,263			value,264			..265		} => {266			#[derive(Trace)]267			struct UnboundValue<B: Trace> {268				uctx: B,269				value: Rc<Expr>,270				name: IStr,271			}272			impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {273				type Bound = Val;274				fn bind(&self, sup_this: SupThis) -> Result<Val> {275					evaluate_named(self.uctx.bind(sup_this)?, &self.value, self.name.clone())276				}277			}278279			builder280				.field(name.clone())281				.with_add(*plus)282				.with_visibility(*visibility)283				.with_location(field.name.span.clone())284				.bindable(UnboundValue {285					uctx,286					value: value.clone(),287					name,288				})?;289		}290		FieldMember {291			params: Some(params),292			visibility,293			value,294			..295		} => {296			#[derive(Trace)]297			struct UnboundMethod<B: Trace> {298				uctx: B,299				value: Rc<Expr>,300				params: ExprParams,301				name: IStr,302			}303			impl<B: Unbound<Bound = Context>> Unbound for UnboundMethod<B> {304				type Bound = Val;305				fn bind(&self, sup_this: SupThis) -> Result<Val> {306					Ok(evaluate_method(307						self.uctx.bind(sup_this)?,308						self.name.clone(),309						self.params.clone(),310						self.value.clone(),311					))312				}313			}314315			builder316				.field(name.clone())317				.with_visibility(*visibility)318				// .with_location(value.span())319				.bindable(UnboundMethod {320					uctx,321					value: value.clone(),322					params: params.clone(),323					name,324				})?;325		}326	}327	Ok(())328}329330#[derive(Trace, Clone)]331struct DirectUnbound(Context);332impl Unbound for DirectUnbound {333	type Bound = Context;334	fn bind(&self, sup_this: SupThis) -> Result<Context> {335		Ok(self336			.0337			.clone()338			.extend_bindings_sup_this(FxHashMap::new(), sup_this))339	}340}341342#[allow(clippy::too_many_lines)]343pub fn evaluate_member_list_object(344	super_obj: Option<ObjValue>,345	ctx: Context,346	members: &ObjMembers,347) -> Result<ObjValue> {348	#[derive(Trace)]349	struct ObjectAssert<B: Trace> {350		uctx: B,351		asserts: Rc<Vec<AssertStmt>>,352	}353	impl<B: Unbound<Bound = Context>> ObjectAssertion for ObjectAssert<B> {354		fn run(&self, sup_this: SupThis) -> Result<()> {355			let ctx = self.uctx.bind(sup_this)?;356			for assert in &*self.asserts {357				evaluate_assert(ctx.clone(), assert)?;358			}359			Ok(())360		}361	}362363	let mut builder = ObjValueBuilder::new();364	if let Some(super_obj) = super_obj {365		builder.with_super(super_obj);366	}367368	if members.locals.is_empty() {369		// We can use the same context for all field evaluation, it doesn't depends on locals, only on this/super370		let uctx = DirectUnbound(ctx.clone());371		for field in &members.fields {372			evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;373		}374		if !members.asserts.is_empty() {375			builder.assert(ObjectAssert {376				uctx,377				asserts: members.asserts.clone(),378			});379		}380	} else {381		let locals = members.locals.clone();382		// We have single context for all fields, so we can cache them together383		let uctx = CachedUnbound::new(evaluate_object_locals(ctx.clone(), locals));384		for field in &members.fields {385			evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;386		}387		if !members.asserts.is_empty() {388			builder.assert(ObjectAssert {389				uctx,390				asserts: members.asserts.clone(),391			});392		}393	}394395	Ok(builder.build())396}397398pub fn evaluate_object(399	super_obj: Option<ObjValue>,400	ctx: Context,401	object: &ObjBody,402) -> Result<ObjValue> {403	Ok(match object {404		ObjBody::MemberList(members) => evaluate_member_list_object(super_obj, ctx, members)?,405		ObjBody::ObjComp(obj) => {406			let mut builder = ObjValueBuilder::new();407			if let Some(super_obj) = super_obj {408				builder.with_super(super_obj);409			}410			let locals = obj.locals.clone();411			evaluate_comp(ctx, &obj.compspecs, 0, &mut |ctx, reserve| {412				let uctx = evaluate_object_locals(ctx.clone(), locals.clone());413				builder.reserve_cores(reserve);414415				evaluate_field_member(&mut builder, ctx, uctx, &obj.field)416			})?;417418			builder.build()419		}420	})421}422423pub fn evaluate_apply(424	ctx: Context,425	value: &Expr,426	args: &ArgsDesc,427	loc: CallLocation<'_>,428	tailstrict: bool,429) -> Result<Val> {430	let value = evaluate(ctx.clone(), value)?;431	Ok(match value {432		Val::Func(f) => {433			let name = f.name();434			let prepare = PreparedFuncVal::new(f, args.unnamed.len(), &args.names)?;435			let unnamed = args436				.unnamed437				.iter()438				.cloned()439				.map(|un| evaluate_thunk(ctx.clone(), un, tailstrict))440				.collect::<Result<Vec<_>>>()?;441			let named = args442				.values443				.iter()444				.cloned()445				.map(|un| evaluate_thunk(ctx.clone(), un, tailstrict))446				.collect::<Result<Vec<_>>>()?;447			let body = || prepare.call(loc, &unnamed, &named);448			if tailstrict {449				body()?450			} else {451				in_frame(loc, || format!("function <{name}> call"), body)?452			}453		}454		v => bail!(OnlyFunctionsCanBeCalledGot(v.value_type())),455	})456}457458pub fn evaluate_assert(ctx: Context, assertion: &AssertStmt) -> Result<()> {459	let value = &assertion.0;460	let msg = &assertion.1;461	let assertion_result = in_frame(462		CallLocation::new(&value.span),463		|| "assertion condition".to_owned(),464		|| bool::from_untyped(evaluate(ctx.clone(), value)?),465	)?;466	if !assertion_result {467		in_frame(468			CallLocation::new(&value.span),469			|| "assertion failure".to_owned(),470			|| {471				if let Some(msg) = msg {472					bail!(AssertionFailed(evaluate(ctx, msg)?.to_string()?));473				}474				bail!(AssertionFailed(Val::Null.to_string()?));475			},476		)?;477	}478	Ok(())479}480481pub fn evaluate_named_param(ctx: Context, expr: &Expr, name: ParamName) -> Result<Val> {482	match name {483		ParamName::Named(name) => evaluate_named(ctx, expr, name),484		ParamName::Unnamed => evaluate(ctx, expr),485	}486}487488pub fn evaluate_named(ctx: Context, expr: &Expr, name: IStr) -> Result<Val> {489	use Expr::*;490	Ok(match expr {491		Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),492		_ => evaluate(ctx, expr)?,493	})494}495496pub fn evaluate_thunk(ctx: Context, expr: Rc<Expr>, tailstrict: bool) -> Result<Thunk<Val>> {497	Ok(if tailstrict {498		Thunk::evaluated(evaluate(ctx, &expr)?)499	} else {500		Thunk!(move || { evaluate(ctx, &expr) })501	})502}503#[allow(clippy::too_many_lines)]504pub fn evaluate(ctx: Context, expr: &Expr) -> Result<Val> {505	use Expr::*;506507	Ok(match expr {508		Literal(LiteralType::This) => Val::Obj(ctx.try_this()?),509		Literal(LiteralType::Super) => Val::Obj(ctx.try_sup_this()?.standalone_super()?),510		Literal(LiteralType::Dollar) => Val::Obj(ctx.try_dollar()?),511		Literal(LiteralType::True) => Val::Bool(true),512		Literal(LiteralType::False) => Val::Bool(false),513		Literal(LiteralType::Null) => Val::Null,514		Str(v) => Val::string(v.clone()),515		Num(v) => Val::try_num(*v)?,516		// I have tried to remove special behavior from super by implementing standalone-super517		// expresion, but looks like this case still needs special treatment.518		//519		// Note that other jsonnet implementations will fail on `if value in (super)` expression,520		// because the standalone super literal is not supported, that is because in other521		// implementations `in super` treated differently from `in smth_else`.522		BinaryOp(bin)523			if matches!(&bin.rhs, Expr::Literal(LiteralType::Super))524				&& bin.op == BinaryOpType::In =>525		{526			let sup_this = ctx.try_sup_this()?;527			// In jsonnet, "field" in e is eager, LHS expression is always executed regardless of super existence.528			// In jrsonnet, however, this wasn't true, this was kept here for compatibility.529			if !sup_this.has_super() {530				return Ok(Val::Bool(false));531			}532			let field = evaluate(ctx, &bin.lhs)?;533			Val::Bool(sup_this.field_in_super(field.to_string()?))534		}535		BinaryOp(bin) => evaluate_binary_op_special(ctx, &bin.lhs, bin.op, &bin.rhs)?,536		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,537		Var(name) => in_frame(538			CallLocation::new(&name.span),539			|| format!("local <{}> access", &**name),540			|| ctx.binding((**name).clone())?.evaluate(),541		)?,542		Index { indexable, parts } => ensure_sufficient_stack(|| {543			let mut parts = parts.iter();544			let mut indexable = if matches!(&**indexable, Expr::Literal(LiteralType::Super)) {545				let part = parts.next().expect("at least part should exist");546				// sup_this existence check might also be skipped here for null-coalesce...547				// But I believe this might cause errors.548				let sup_this = ctx.try_sup_this()?;549				if !sup_this.has_super() {550					#[cfg(feature = "exp-null-coaelse")]551					if part.null_coaelse {552						return Ok(Val::Null);553					}554					bail!(NoSuperFound)555				}556				let name = evaluate(ctx.clone(), &part.value)?;557558				let Val::Str(name) = name else {559					bail!(ValueIndexMustBeTypeGot(560						ValType::Obj,561						ValType::Str,562						name.value_type(),563					))564				};565566				let name = name.into_flat();567				match sup_this568					.get_super(name.clone())569					.with_description_src(&part.span, || format!("field <{name}> access"))?570				{571					Some(v) => v,572					#[cfg(feature = "exp-null-coaelse")]573					None if part.null_coaelse => return Ok(Val::Null),574					None => {575						let suggestions = suggest_object_fields(576							&sup_this.standalone_super().expect("super exists"),577							name.clone(),578						);579580						bail!(NoSuchField(name, suggestions))581					}582				}583			} else {584				evaluate(ctx.clone(), indexable)?585			};586587			for part in parts {588				indexable = match (indexable, evaluate(ctx.clone(), &part.value)?) {589					(Val::Obj(v), Val::Str(key)) => match v590						.get(key.clone().into_flat())591						.with_description_src(&part.span, || format!("field <{key}> access"))?592					{593						Some(v) => v,594						#[cfg(feature = "exp-null-coaelse")]595						None if part.null_coaelse => return Ok(Val::Null),596						None => {597							let suggestions = suggest_object_fields(&v, key.into_flat());598599							return Err(Error::from(NoSuchField(600								key.clone().into_flat(),601								suggestions,602							)))603							.with_description_src(&part.span, || format!("field <{key}> access"));604						}605					},606					(Val::Obj(_), n) => bail!(ValueIndexMustBeTypeGot(607						ValType::Obj,608						ValType::Str,609						n.value_type(),610					)),611					(Val::Arr(v), Val::Num(n)) => {612						let n = n.get();613						if n.fract() > f64::EPSILON {614							bail!(FractionalIndex)615						}616						if n < 0.0 {617							#[expect(618								clippy::cast_possible_truncation,619								reason = "it would be truncated anyway"620							)]621							let n = n as isize;622							bail!(ArrayBoundsError(n, v.len()));623						}624						#[expect(625							clippy::cast_possible_truncation,626							clippy::cast_sign_loss,627							reason = "n is checked postive"628						)]629						v.get(n as usize)?630							.ok_or_else(|| ArrayBoundsError(n as isize, v.len()))?631					}632					(Val::Arr(_), Val::Str(n)) => {633						bail!(AttemptedIndexAnArrayWithString(n.into_flat()))634					}635					(Val::Arr(_), n) => bail!(ValueIndexMustBeTypeGot(636						ValType::Arr,637						ValType::Num,638						n.value_type(),639					)),640641					(Val::Str(s), Val::Num(n)) => Val::Str({642						let n = n.get();643						if n.fract() > f64::EPSILON {644							bail!(FractionalIndex)645						}646						if n < 0.0 {647							#[expect(648								clippy::cast_possible_truncation,649								reason = "it would be truncated anyway"650							)]651							let n = n as isize;652							bail!(ArrayBoundsError(n, s.into_flat().chars().count()));653						}654						#[expect(655							clippy::cast_sign_loss,656							clippy::cast_possible_truncation,657							reason = "n is positive, overflow will truncate as expected"658						)]659						let n = n as usize;660						let v: IStr = s661							.clone()662							.into_flat()663							.chars()664							.skip(n)665							.take(1)666							.collect::<String>()667							.into();668						if v.is_empty() {669							bail!(StringBoundsError(n, s.into_flat().chars().count()))670						}671						StrValue::Flat(v)672					}),673					(Val::Str(_), n) => bail!(ValueIndexMustBeTypeGot(674						ValType::Str,675						ValType::Num,676						n.value_type(),677					)),678					#[cfg(feature = "exp-null-coaelse")]679					(Val::Null, _) if part.null_coaelse => return Ok(Val::Null),680					(v, _) => bail!(CantIndexInto(v.value_type())),681				};682			}683			Ok(indexable)684		})?,685		LocalExpr(bindings, returned) => {686			let mut new_bindings: FxHashMap<IStr, Thunk<Val>> =687				FxHashMap::with_capacity(bindings.iter().map(BindSpec::binds_len).sum());688			let fctx = Context::new_future();689			for b in bindings {690				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;691			}692			let ctx = ctx.extend_bindings(new_bindings).into_future(fctx);693			evaluate(ctx, returned)?694		}695		Arr(items) => {696			if items.is_empty() {697				Val::arr(())698			} else {699				Val::Arr(ArrValue::expr(ctx, items.clone()))700			}701		}702		ArrComp(expr, comp_specs) => Val::Arr(evaluate_arr_comp(ctx, expr, comp_specs)?),703		Obj(body) => Val::Obj(evaluate_object(None, ctx, body)?),704		ObjExtend(a, b) => {705			let base = evaluate(ctx.clone(), a)?;706			match base {707				Val::Obj(base_obj) => Val::Obj(evaluate_object(Some(base_obj), ctx, b)?),708				_ => bail!("ObjExtend lhs should be an object value"),709			}710		}711		Apply(value, args, tailstrict) => ensure_sufficient_stack(|| {712			evaluate_apply(ctx, value, args, CallLocation::new(&args.span), *tailstrict)713		})?,714		Function(params, body) => {715			evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())716		}717		AssertExpr(assert) => {718			evaluate_assert(ctx.clone(), &assert.assert)?;719			evaluate(ctx, &assert.rest)?720		}721		ErrorStmt(s, e) => in_frame(722			CallLocation::new(s),723			|| "error statement".to_owned(),724			|| bail!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),725		)?,726		IfElse(if_else) => {727			if in_frame(728				CallLocation::new(&if_else.cond.span),729				|| "if condition".to_owned(),730				|| bool::from_untyped(evaluate(ctx.clone(), &if_else.cond.cond)?),731			)? {732				evaluate(ctx, &if_else.cond_then)?733			} else {734				match &if_else.cond_else {735					Some(v) => evaluate(ctx, v)?,736					None => Val::Null,737				}738			}739		}740		Slice(slice) => {741			fn parse_idx<T: Typed + FromUntyped>(742				ctx: Context,743				expr: Option<&Spanned<Expr>>,744				desc: &'static str,745			) -> Result<Option<T>> {746				if let Some(value) = expr {747					Ok(in_frame(748						CallLocation::new(&value.span),749						|| format!("slice {desc}"),750						|| <Option<T>>::from_untyped(evaluate(ctx, value)?),751					)?)752				} else {753					Ok(None)754				}755			}756757			let indexable = evaluate(ctx.clone(), &slice.value)?;758759			let start = parse_idx(ctx.clone(), slice.slice.start.as_ref(), "start")?;760			let end = parse_idx(ctx.clone(), slice.slice.end.as_ref(), "end")?;761			let step = parse_idx(ctx, slice.slice.step.as_ref(), "step")?;762763			IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?764		}765		Import(kind, path) => {766			let Expr::Str(path) = &**path else {767				bail!("computed imports are not supported")768			};769			with_state(|s| {770				let span = &kind.span;771				let resolved_path = s.resolve_from(span.0.source_path(), path)?;772				Ok(match &**kind {773					ImportKind::Normal => in_frame(774						CallLocation::new(span),775						|| format!("import {:?}", path.clone()),776						|| s.import_resolved(resolved_path),777					)?,778					ImportKind::Str => Val::string(s.import_resolved_str(resolved_path)?),779					ImportKind::Bin => Val::arr(s.import_resolved_bin(resolved_path)?),780				}) as Result<Val>781			})?782		}783	})784}
modifiedcrates/jrsonnet-evaluator/src/function/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function/mod.rs
+++ b/crates/jrsonnet-evaluator/src/function/mod.rs
@@ -3,12 +3,12 @@
 use educe::Educe;
 use jrsonnet_gcmodule::{Cc, Trace};
 use jrsonnet_interner::IStr;
-use jrsonnet_ir::{ArgsDesc, Destruct, Expr, ExprParams, Span};
+use jrsonnet_ir::{Destruct, Expr, ExprParams, Span};
 pub use jrsonnet_macros::builtin;
 
 use self::{
 	builtin::Builtin,
-	parse::{parse_builtin_call, parse_default_function_call, parse_function_call},
+	parse::parse_default_function_call,
 	prepared::{PreparedCall, parse_prepared_builtin_call, parse_prepared_function_call},
 };
 use crate::{
@@ -22,7 +22,7 @@
 
 pub use jrsonnet_ir::function::*;
 pub use native::NativeFn;
-pub use prepared::PreparedFuncVal;
+pub(crate) use prepared::PreparedFuncVal;
 
 /// Function callsite location.
 /// Either from other jsonnet code, specified by expression location, or from native (without location).
@@ -77,16 +77,6 @@
 		parse_default_function_call(self.ctx.clone(), &self.params)
 	}
 
-	/// Create context, with which body code will run
-	pub(crate) fn call_body_context(
-		&self,
-		call_ctx: Context,
-		args: &ArgsDesc,
-		tailstrict: bool,
-	) -> Result<Context> {
-		parse_function_call(call_ctx, self.ctx.clone(), &self.params, args, tailstrict)
-	}
-
 	pub fn evaluate_trivial(&self) -> Option<Val> {
 		evaluate_trivial(&self.body)
 	}
@@ -137,27 +127,6 @@
 		match self {
 			Self::Normal(normal) => normal.name.clone(),
 			Self::Builtin(builtin) => builtin.name().into(),
-		}
-	}
-	/// Call function using arguments evaluated in specified `call_ctx` [`Context`].
-	///
-	/// If `tailstrict` is specified - then arguments will be evaluated before being passed to function body.
-	pub fn evaluate(
-		&self,
-		call_ctx: Context,
-		loc: CallLocation<'_>,
-		args: &ArgsDesc,
-		tailstrict: bool,
-	) -> Result<Val> {
-		match self {
-			Self::Normal(func) => {
-				let body_ctx = func.call_body_context(call_ctx, args, tailstrict)?;
-				evaluate(body_ctx, &func.body)
-			}
-			Self::Builtin(b) => {
-				let args = parse_builtin_call(call_ctx, b.params(), args, tailstrict)?;
-				b.call(loc, &args)
-			}
 		}
 	}
 
modifiedcrates/jrsonnet-evaluator/src/function/native.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function/native.rs
+++ b/crates/jrsonnet-evaluator/src/function/native.rs
@@ -39,12 +39,19 @@
 		impl<$($gen,)* O> FromUntyped for NativeFn<($($gen,)* O,)> {
 			fn from_untyped(untyped: Val) -> Result<Self> {
 				let func = FuncVal::from_untyped(untyped)?;
+				Self::try_from(func)
+			}
+		}
+		impl<$($gen,)* O> TryFrom<FuncVal> for NativeFn<($($gen,)* O,)> {
+			type Error = crate::Error;
+			fn try_from(v: FuncVal) -> Result<Self> {
 				Ok(Self(
-					PreparedFuncVal::new(func, $i, &[])?,
+					PreparedFuncVal::new(v, $i, &[])?,
 					PhantomData,
 				))
 			}
 		}
+
 	};
 	($i:expr; $($cur:ident)* @ $c:ident $($rest:ident)*) => {
 		impl_native_desc!($i; $($cur)*);
modifiedcrates/jrsonnet-evaluator/src/function/parse.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function/parse.rs
+++ b/crates/jrsonnet-evaluator/src/function/parse.rs
@@ -1,215 +1,13 @@
-use std::rc::Rc;
-
-use jrsonnet_ir::{
-	ArgsDesc, Expr, ExprParams,
-	function::{FunctionSignature, ParamName},
-};
+use jrsonnet_ir::ExprParams;
 use rustc_hash::FxHashMap;
 
 use crate::{
-	Context, Pending, Thunk, Val, bail,
+	Context, Thunk,
 	destructure::destruct,
 	error::{ErrorKind::*, Result},
-	evaluate, evaluate_named_param,
+	evaluate_named_param,
 	gc::WithCapacityExt as _,
 };
-
-fn eval_arg(ctx: Context, arg: &Rc<Expr>, tailstrict: bool) -> Result<Thunk<Val>> {
-	if tailstrict {
-		Ok(Thunk::evaluated(evaluate(ctx, arg)?))
-	} else {
-		let arg = arg.clone();
-		Ok(Thunk!(move || evaluate(ctx, &arg)))
-	}
-}
-
-/// Creates correct [context](Context) for function body evaluation returning error on invalid call.
-///
-/// ## Parameters
-/// * `ctx`: used for passed argument expressions' execution and for body execution (if `body_ctx` is not set)
-/// * `body_ctx`: used for default parameter values' execution and for body execution (if set)
-/// * `params`: function parameters' definition
-/// * `args`: passed function arguments
-/// * `tailstrict`: if set to `true` function arguments are eagerly executed, otherwise - lazily
-pub(crate) fn parse_function_call(
-	ctx: Context,
-	body_ctx: Context,
-	params: &ExprParams,
-	args: &ArgsDesc,
-	tailstrict: bool,
-) -> Result<Context> {
-	let mut passed_args = FxHashMap::with_capacity(params.binds_len());
-	if args.unnamed.len() > params.signature.len() {
-		bail!(TooManyArgsFunctionHas(
-			params.signature.len(),
-			params.signature.clone(),
-		))
-	}
-
-	let mut filled_named = 0;
-	let mut filled_positionals = 0;
-
-	for (id, arg) in args.unnamed.iter().enumerate() {
-		destruct(
-			&params.exprs[id].destruct,
-			eval_arg(ctx.clone(), arg, tailstrict)?,
-			Pending::new_filled(ctx.clone()),
-			&mut passed_args,
-		)?;
-		filled_positionals += 1;
-	}
-
-	for (name, value) in &args.named {
-		// FIXME: O(n) for arg existence check
-		if !params.exprs.iter().any(|p| &p.destruct.name() == name) {
-			bail!(UnknownFunctionParameter(name.clone()));
-		}
-		if passed_args
-			.insert(name.clone(), eval_arg(ctx.clone(), value, tailstrict)?)
-			.is_some()
-		{
-			bail!(BindingParameterASecondTime(name.clone()));
-		}
-		filled_named += 1;
-	}
-
-	if filled_named + filled_positionals < params.len() {
-		// Some args are unset, but maybe we have defaults for them
-		// Default values should be created in newly created context
-		let fctx = Context::new_future();
-		let mut defaults =
-			FxHashMap::with_capacity(params.binds_len() - filled_named - filled_positionals);
-
-		for (idx, into, default) in params
-			.exprs
-			.iter()
-			.enumerate()
-			.filter_map(|(i, p)| Some((i, &p.destruct, p.default.as_ref()?)))
-		{
-			if let ParamName::Named(name) = into.name() {
-				if passed_args.contains_key(&name) {
-					continue;
-				}
-			} else if idx < filled_positionals {
-				continue;
-			}
-
-			destruct(
-				into,
-				{
-					let ctx = fctx.clone();
-					let name = into.name();
-					let value = default.clone();
-					Thunk!(move || evaluate_named_param(ctx.unwrap(), &value, name))
-				},
-				fctx.clone(),
-				&mut defaults,
-			)?;
-			if into.name().is_named() {
-				filled_named += 1;
-			} else {
-				filled_positionals += 1;
-			}
-		}
-
-		// Some args still weren't filled
-		if filled_named + filled_positionals != params.len() {
-			for param in params.exprs.iter().skip(args.unnamed.len()) {
-				let mut found = false;
-				for (name, _) in &args.named {
-					if &param.destruct.name() == name {
-						found = true;
-					}
-				}
-				if !found {
-					bail!(FunctionParameterNotBoundInCall(
-						param.destruct.name(),
-						params.signature.clone()
-					));
-				}
-			}
-			unreachable!();
-		}
-
-		Ok(body_ctx
-			.extend_bindings(passed_args)
-			.extend_bindings(defaults)
-			.into_future(fctx))
-	} else {
-		let body_ctx = body_ctx.extend_bindings(passed_args);
-		Ok(body_ctx)
-	}
-}
-
-/// You shouldn't probally use this function, use `jrsonnet_macros::builtin` instead
-///
-/// ## Parameters
-/// * `ctx`: used for passed argument expressions' execution and for body execution (if `body_ctx` is not set)
-/// * `params`: function parameters' definition
-/// * `args`: passed function arguments
-/// * `tailstrict`: if set to `true` function arguments are eagerly executed, otherwise - lazily
-pub fn parse_builtin_call(
-	ctx: Context,
-	params: FunctionSignature,
-	args: &ArgsDesc,
-	tailstrict: bool,
-) -> Result<Vec<Option<Thunk<Val>>>> {
-	let mut passed_args: Vec<Option<Thunk<Val>>> = vec![None; params.len()];
-	if args.unnamed.len() > params.len() {
-		bail!(TooManyArgsFunctionHas(params.len(), params,))
-	}
-
-	let mut filled_args = 0;
-
-	for (id, arg) in args.unnamed.iter().enumerate() {
-		passed_args[id] = Some(eval_arg(ctx.clone(), arg, tailstrict)?);
-		filled_args += 1;
-	}
-
-	for (name, arg) in &args.named {
-		// FIXME: O(n) for arg existence check
-		let id = params
-			.iter()
-			.position(|p| p.name() == name)
-			.ok_or_else(|| UnknownFunctionParameter(name.clone()))?;
-		if passed_args[id]
-			.replace(eval_arg(ctx.clone(), arg, tailstrict)?)
-			.is_some()
-		{
-			bail!(BindingParameterASecondTime(name.clone()));
-		}
-		filled_args += 1;
-	}
-
-	if filled_args < params.len() {
-		for (id, _) in params.iter().enumerate().filter(|(_, p)| p.has_default()) {
-			if passed_args[id].is_some() {
-				continue;
-			}
-			filled_args += 1;
-		}
-
-		// Some args still wasn't filled
-		if filled_args != params.len() {
-			for param in params.iter().skip(args.unnamed.len()) {
-				let mut found = false;
-				for (name, _) in &args.named {
-					if param.name() == name {
-						found = true;
-					}
-				}
-				if !found {
-					bail!(FunctionParameterNotBoundInCall(
-						param.name().clone(),
-						params,
-					));
-				}
-			}
-			unreachable!();
-		}
-	}
-	Ok(passed_args)
-}
 
 /// Creates Context, which has all argument default values applied
 /// and with unbound values causing error to be returned
modifiedcrates/jrsonnet-ir-parser/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-ir-parser/src/lib.rs
+++ b/crates/jrsonnet-ir-parser/src/lib.rs
@@ -416,10 +416,11 @@
 
 fn args(p: &mut Parser<'_>) -> Result<ArgsDesc> {
 	if p.at(T![')']) {
-		return Ok(ArgsDesc::new(Vec::new(), Vec::new()));
+		return Ok(ArgsDesc::new(Vec::new(), Vec::new(), Vec::new()));
 	}
 	let mut unnamed = Vec::new();
-	let mut named = Vec::new();
+	let mut names = Vec::new();
+	let mut values = Vec::new();
 	let mut named_started = false;
 	loop {
 		let is_named = p.at(SyntaxKind::IDENT) && {
@@ -430,7 +431,9 @@
 			let name: IStr = ident(p)?;
 			p.eat(T![=])?;
 			let value = Rc::new(expr(p)?);
-			named.push((name, value));
+
+			names.push(name);
+			values.push(value);
 			named_started = true;
 		} else {
 			if named_started {
@@ -445,7 +448,7 @@
 			break;
 		}
 	}
-	Ok(ArgsDesc::new(unnamed, named))
+	Ok(ArgsDesc::new(unnamed, names, values))
 }
 
 fn bind(p: &mut Parser<'_>) -> Result<BindSpec> {
modifiedcrates/jrsonnet-ir-parser/src/snapshots/jrsonnet_ir_parser__tests__function_and_call.snapdiffbeforeafterboth
--- a/crates/jrsonnet-ir-parser/src/snapshots/jrsonnet_ir_parser__tests__function_and_call.snap
+++ b/crates/jrsonnet-ir-parser/src/snapshots/jrsonnet_ir_parser__tests__function_and_call.snap
@@ -66,12 +66,12 @@
                     2.0,
                 ),
             ],
-            named: [
-                (
-                    "y",
-                    Num(
-                        3.0,
-                    ),
+            names: [
+                "y",
+            ],
+            values: [
+                Num(
+                    3.0,
                 ),
             ],
         } from virtual:<test>:26-34,
modifiedcrates/jrsonnet-ir-parser/src/snapshots/jrsonnet_ir_parser__tests__index_and_suffix.snapdiffbeforeafterboth
--- a/crates/jrsonnet-ir-parser/src/snapshots/jrsonnet_ir_parser__tests__index_and_suffix.snap
+++ b/crates/jrsonnet-ir-parser/src/snapshots/jrsonnet_ir_parser__tests__index_and_suffix.snap
@@ -23,7 +23,8 @@
                     2.0,
                 ),
             ],
-            named: [],
+            names: [],
+            values: [],
         } from virtual:<test>:8-11,
         false,
     ),
modifiedcrates/jrsonnet-ir-parser/src/snapshots/jrsonnet_ir_parser__tests__peg_snapshots@array_comp.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-ir-parser/src/snapshots/jrsonnet_ir_parser__tests__peg_snapshots@array_comp.jsonnet.snap
+++ b/crates/jrsonnet-ir-parser/src/snapshots/jrsonnet_ir_parser__tests__peg_snapshots@array_comp.jsonnet.snap
@@ -26,7 +26,8 @@
                             "x" from virtual:<test>:16-17,
                         ),
                     ],
-                    named: [],
+                    names: [],
+                    values: [],
                 } from virtual:<test>:15-18,
                 false,
             ),
modifiedcrates/jrsonnet-ir-parser/src/snapshots/jrsonnet_ir_parser__tests__peg_snapshots@reserved.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-ir-parser/src/snapshots/jrsonnet_ir_parser__tests__peg_snapshots@reserved.jsonnet.snap
+++ b/crates/jrsonnet-ir-parser/src/snapshots/jrsonnet_ir_parser__tests__peg_snapshots@reserved.jsonnet.snap
@@ -24,7 +24,8 @@
                         "null_fields" from virtual:<test>:20-31,
                     ),
                 ],
-                named: [],
+                names: [],
+                values: [],
             } from virtual:<test>:16-32,
             false,
         ),
modifiedcrates/jrsonnet-ir-parser/src/snapshots/jrsonnet_ir_parser__tests__peg_snapshots@suffix.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-ir-parser/src/snapshots/jrsonnet_ir_parser__tests__peg_snapshots@suffix.jsonnet.snap
+++ b/crates/jrsonnet-ir-parser/src/snapshots/jrsonnet_ir_parser__tests__peg_snapshots@suffix.jsonnet.snap
@@ -28,7 +28,8 @@
                         2.0,
                     ),
                 ],
-                named: [],
+                names: [],
+                values: [],
             } from virtual:<test>:15-18,
             false,
         ),
@@ -52,7 +53,8 @@
                         2.0,
                     ),
                 ],
-                named: [],
+                names: [],
+                values: [],
             } from virtual:<test>:28-31,
             false,
         ),
modifiedcrates/jrsonnet-ir/src/expr.rsdiffbeforeafterboth
--- a/crates/jrsonnet-ir/src/expr.rs
+++ b/crates/jrsonnet-ir/src/expr.rs
@@ -195,11 +195,16 @@
 #[derive(Debug, PartialEq, Acyclic)]
 pub struct ArgsDesc {
 	pub unnamed: Vec<Rc<Expr>>,
-	pub named: Vec<(IStr, Rc<Expr>)>,
+	pub names: Vec<IStr>,
+	pub values: Vec<Rc<Expr>>,
 }
 impl ArgsDesc {
-	pub fn new(unnamed: Vec<Rc<Expr>>, named: Vec<(IStr, Rc<Expr>)>) -> Self {
-		Self { unnamed, named }
+	pub fn new(unnamed: Vec<Rc<Expr>>, names: Vec<IStr>, values: Vec<Rc<Expr>>) -> Self {
+		Self {
+			unnamed,
+			names,
+			values,
+		}
 	}
 }
 
modifiedcrates/jrsonnet-ir/src/visit.rsdiffbeforeafterboth
--- a/crates/jrsonnet-ir/src/visit.rs
+++ b/crates/jrsonnet-ir/src/visit.rs
@@ -216,11 +216,15 @@
 		}
 		Expr::Apply(expr, spanned, _) => {
 			v.visit_expr(expr);
-			let ArgsDesc { unnamed, named } = &**spanned;
+			let ArgsDesc {
+				unnamed,
+				names: _,
+				values,
+			} = &**spanned;
 			for unnamed in unnamed {
 				v.visit_expr(unnamed);
 			}
-			for (_name, named) in named {
+			for named in values {
 				v.visit_expr(named);
 			}
 		}
modifiedcrates/jrsonnet-peg-parser/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-peg-parser/src/lib.rs
+++ b/crates/jrsonnet-peg-parser/src/lib.rs
@@ -73,12 +73,14 @@
 			= args:arg(s)**comma() comma()? {?
 				let unnamed_count = args.iter().take_while(|(n, _)| n.is_none()).count();
 				let mut unnamed = Vec::with_capacity(unnamed_count);
-				let mut named = Vec::with_capacity(args.len() - unnamed_count);
+				let mut names = Vec::with_capacity(args.len() - unnamed_count);
+				let mut values = Vec::with_capacity(args.len() - unnamed_count);
 				let mut named_started = false;
 				for (name, value) in args {
 					if let Some(name) = name {
 						named_started = true;
-						named.push((name, value));
+						names.push(name);
+						values.push(value);
 					} else {
 						if named_started {
 							return Err("<named argument>")
@@ -86,7 +88,7 @@
 						unnamed.push(value);
 					}
 				}
-				Ok(ArgsDesc::new(unnamed, named))
+				Ok(ArgsDesc{unnamed, names, values})
 			}
 
 		pub rule destruct_rest() -> DestructRest
modifiedcrates/jrsonnet-peg-parser/src/snapshots/jrsonnet_peg_parser__tests__snapshots@array_comp.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-peg-parser/src/snapshots/jrsonnet_peg_parser__tests__snapshots@array_comp.jsonnet.snap
+++ b/crates/jrsonnet-peg-parser/src/snapshots/jrsonnet_peg_parser__tests__snapshots@array_comp.jsonnet.snap
@@ -26,7 +26,8 @@
                             "x" from virtual:<test>:16-17,
                         ),
                     ],
-                    named: [],
+                    names: [],
+                    values: [],
                 } from virtual:<test>:15-18,
                 false,
             ),
modifiedcrates/jrsonnet-peg-parser/src/snapshots/jrsonnet_peg_parser__tests__snapshots@reserved.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-peg-parser/src/snapshots/jrsonnet_peg_parser__tests__snapshots@reserved.jsonnet.snap
+++ b/crates/jrsonnet-peg-parser/src/snapshots/jrsonnet_peg_parser__tests__snapshots@reserved.jsonnet.snap
@@ -24,7 +24,8 @@
                         "null_fields" from virtual:<test>:20-31,
                     ),
                 ],
-                named: [],
+                names: [],
+                values: [],
             } from virtual:<test>:16-32,
             false,
         ),
modifiedcrates/jrsonnet-peg-parser/src/snapshots/jrsonnet_peg_parser__tests__snapshots@suffix.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-peg-parser/src/snapshots/jrsonnet_peg_parser__tests__snapshots@suffix.jsonnet.snap
+++ b/crates/jrsonnet-peg-parser/src/snapshots/jrsonnet_peg_parser__tests__snapshots@suffix.jsonnet.snap
@@ -28,7 +28,8 @@
                         2.0,
                     ),
                 ],
-                named: [],
+                names: [],
+                values: [],
             } from virtual:<test>:15-18,
             false,
         ),
@@ -52,7 +53,8 @@
                         2.0,
                     ),
                 ],
-                named: [],
+                names: [],
+                values: [],
             } from virtual:<test>:28-31,
             false,
         ),
modifiedcrates/jrsonnet-stdlib/src/keyf.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/keyf.rs
+++ b/crates/jrsonnet-stdlib/src/keyf.rs
@@ -1,14 +1,16 @@
 use jrsonnet_evaluator::{
 	Error, Result, Thunk, Val,
-	function::{CallLocation, FuncVal, PreparedFuncVal},
+	function::{FuncVal, NativeFn},
 	typed::{ComplexValType, FromUntyped, Typed, ValType},
 };
 
+type PreparedKeyF = NativeFn!((Thunk<Val>) -> Val);
+
 #[derive(Default, Clone)]
 pub enum KeyF {
 	#[default]
 	Identity,
-	Prepared(PreparedFuncVal),
+	Prepared(PreparedKeyF),
 	PrepareFailure(Error),
 }
 impl KeyF {
@@ -19,13 +21,13 @@
 		if val.is_identity() {
 			Self::Identity
 		} else {
-			PreparedFuncVal::new(val, 1, &[]).map_or_else(Self::PrepareFailure, Self::Prepared)
+			PreparedKeyF::try_from(val).map_or_else(Self::PrepareFailure, Self::Prepared)
 		}
 	}
 	pub fn eval(&self, val: impl Into<Thunk<Val>>) -> Result<Val> {
 		match self {
 			KeyF::Identity => val.into().evaluate(),
-			KeyF::Prepared(p) => p.call(CallLocation::native(), &[val.into()], &[]),
+			KeyF::Prepared(p) => p.call(val.into()),
 			KeyF::PrepareFailure(e) => Err(e.clone()),
 		}
 	}