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

difftreelog

refactor(evaluator) extract shared function call parsing code

Лач2020-06-09parent: #f287f46.patch.diff
in: master

8 files changed

modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -90,7 +90,7 @@
 	let opts: Opts = Opts::parse();
 	let evaluator = jsonnet_evaluator::EvaluationState::default();
 	if !opts.no_stdlib {
-		evaluator.add_stdlib();
+		evaluator.with_stdlib();
 	}
 	let mut input = current_dir().unwrap();
 	input.push(opts.input.clone());
@@ -105,7 +105,7 @@
 			let v = match opts.format {
 				Format::Json => {
 					if opts.no_stdlib {
-						evaluator.add_stdlib();
+						evaluator.with_stdlib();
 					}
 					evaluator.add_global("__tmp__to_json__".to_owned(), v);
 					let v = evaluator.parse_evaluate_raw(&format!(
@@ -122,7 +122,7 @@
 				}
 				Format::Yaml => {
 					if opts.no_stdlib {
-						evaluator.add_stdlib();
+						evaluator.with_stdlib();
 					}
 					evaluator.add_global("__tmp__to_yaml__".to_owned(), v);
 					let v = evaluator
modifiedcrates/jsonnet-evaluator/src/ctx.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/ctx.rs
+++ b/crates/jsonnet-evaluator/src/ctx.rs
@@ -1,5 +1,6 @@
 use crate::{
-	future_wrapper, rc_fn_helper, resolved_lazy_val, LazyBinding, LazyVal, ObjValue, Result, Val,
+	future_wrapper, map::LayeredHashMap, rc_fn_helper, resolved_lazy_val, LazyBinding, LazyVal,
+	ObjValue, Result, Val,
 };
 use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};
 
@@ -11,12 +12,11 @@
 
 future_wrapper!(Context, FutureContext);
 
-#[derive(Debug)]
 struct ContextInternals {
 	dollar: Option<ObjValue>,
 	this: Option<ObjValue>,
 	super_obj: Option<ObjValue>,
-	bindings: Rc<HashMap<String, LazyVal>>,
+	bindings: LayeredHashMap<String, LazyVal>,
 }
 pub struct Context(Rc<ContextInternals>);
 impl Debug for Context {
@@ -48,7 +48,7 @@
 			dollar: None,
 			this: None,
 			super_obj: None,
-			bindings: Rc::new(HashMap::new()),
+			bindings: LayeredHashMap::default(),
 		}))
 	}
 
@@ -65,14 +65,14 @@
 	}
 
 	pub fn with_var(&self, name: String, value: Val) -> Result<Context> {
-		let mut new_bindings: HashMap<_, LazyBinding> = HashMap::new();
-		new_bindings.insert(name, LazyBinding::Bound(resolved_lazy_val!(value.clone())));
+		let mut new_bindings = HashMap::new();
+		new_bindings.insert(name, resolved_lazy_val!(value));
 		self.extend(new_bindings, None, None, None)
 	}
 
 	pub fn extend(
 		&self,
-		new_bindings: HashMap<String, LazyBinding>,
+		new_bindings: HashMap<String, LazyVal>,
 		new_dollar: Option<ObjValue>,
 		new_this: Option<ObjValue>,
 		new_super_obj: Option<ObjValue>,
@@ -83,14 +83,7 @@
 		let bindings = if new_bindings.is_empty() {
 			self.0.bindings.clone()
 		} else {
-			let mut new = HashMap::new(); // = self.0.bindings.clone();
-			for (k, v) in self.0.bindings.iter() {
-				new.insert(k.clone(), v.clone());
-			}
-			for (k, v) in new_bindings.into_iter() {
-				new.insert(k, v.evaluate(this.clone(), super_obj.clone())?);
-			}
-			Rc::new(new)
+			self.0.bindings.extend(new_bindings)
 		};
 		Ok(Context(Rc::new(ContextInternals {
 			dollar,
@@ -99,6 +92,21 @@
 			bindings,
 		})))
 	}
+	pub fn extend_unbound(
+		&self,
+		new_bindings: HashMap<String, LazyBinding>,
+		new_dollar: Option<ObjValue>,
+		new_this: Option<ObjValue>,
+		new_super_obj: Option<ObjValue>,
+	) -> Result<Context> {
+		let this = new_this.or_else(|| self.0.this.clone());
+		let super_obj = new_super_obj.or_else(|| self.0.super_obj.clone());
+		let mut new = HashMap::new();
+		for (k, v) in new_bindings.into_iter() {
+			new.insert(k, v.evaluate(this.clone(), super_obj.clone())?);
+		}
+		self.extend(new, new_dollar, this, super_obj)
+	}
 }
 
 impl Default for Context {
modifiedcrates/jsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/error.rs
+++ b/crates/jsonnet-evaluator/src/error.rs
@@ -7,6 +7,11 @@
 	TypeMismatch(&'static str, Vec<ValType>, ValType),
 	NoSuchField(String),
 
+	UnknownFunctionParameter(String),
+	BindingParameterASecondTime(String),
+	TooManyArgsFunctionHas(usize),
+	FunctionParameterNotBoundInCall(String),
+
 	RuntimeError(String),
 	StackOverflow,
 	FractionalIndex,
modifiedcrates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/evaluate.rs
+++ b/crates/jsonnet-evaluator/src/evaluate.rs
@@ -1,12 +1,12 @@
 use crate::{
-	binding, context_creator, create_error, function_default, function_rhs, future_wrapper,
-	lazy_val, push, Context, ContextCreator, FuncDesc, LazyBinding, LazyVal, ObjMember, ObjValue,
-	Result, Val,
+	binding, context_creator, create_error, future_wrapper, lazy_val, push, Context,
+	ContextCreator, FuncDesc, LazyBinding, ObjMember, ObjValue, Result, Val,
 };
 use closure::closure;
 use jsonnet_parser::{
-	ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, FieldMember, ForSpecData,
-	IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc, UnaryOpType, Visibility,
+	el, Arg, ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, FieldMember,
+	ForSpecData, IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc, UnaryOpType,
+	Visibility,
 };
 use std::{
 	collections::{BTreeMap, HashMap},
@@ -15,16 +15,16 @@
 
 pub fn evaluate_binding(b: &BindSpec, context_creator: ContextCreator) -> (String, LazyBinding) {
 	let b = b.clone();
-	if let Some(args) = &b.params {
-		let args = args.clone();
+	if let Some(params) = &b.params {
+		let params = params.clone();
 		(
 			b.name.clone(),
 			LazyBinding::Bindable(Rc::new(move |this, super_obj| {
 				Ok(lazy_val!(
-					closure!(clone b, clone args, clone context_creator, || Ok(evaluate_method(
+					closure!(clone b, clone params, clone context_creator, || Ok(evaluate_method(
 						context_creator.0(this.clone(), super_obj.clone())?,
-						&b.value,
-						args.clone()
+						params.clone(),
+						b.value.clone(),
 					)))
 				))
 			})),
@@ -46,13 +46,8 @@
 	}
 }
 
-pub fn evaluate_method(ctx: Context, expr: &LocExpr, arg_spec: ParamsDesc) -> Val {
-	Val::Func(FuncDesc {
-		ctx,
-		params: arg_spec,
-		eval_rhs: function_rhs!(closure!(clone expr, |ctx| evaluate(ctx, &expr))),
-		eval_default: function_default!(closure!(|ctx, default| evaluate(ctx, &default))),
-	})
+pub fn evaluate_method(ctx: Context, params: ParamsDesc, body: LocExpr) -> Val {
+	Val::Func(FuncDesc { ctx, params, body })
 }
 
 pub fn evaluate_field_name(
@@ -214,7 +209,7 @@
 			let future_this = FutureObjValue::new();
 			let context_creator = context_creator!(
 				closure!(clone context, clone new_bindings, clone future_this, |this: Option<ObjValue>, super_obj: Option<ObjValue>| {
-					Ok(context.clone().extend(
+					Ok(context.clone().extend_unbound(
 						new_bindings.clone().unwrap(),
 						context.clone().dollar().clone().or_else(||this.clone()),
 						Some(this.unwrap()),
@@ -292,8 +287,8 @@
 										// TODO: Assert
 										Ok(evaluate_method(
 											context_creator.0(this, super_obj)?,
-											&value.clone(),
 											params.clone(),
+											value.clone(),
 										))
 									})
 								),
@@ -393,7 +388,7 @@
 			}
 
 			let context = context
-				.extend(new_bindings, None, None, None)?
+				.extend_unbound(new_bindings, None, None, None)?
 				.into_future(future_context);
 			evaluate(context, &returned.clone())?
 		}
@@ -417,7 +412,7 @@
 			&evaluate(context.clone(), s)?,
 			&Val::Obj(evaluate_object(context, t.clone())?),
 		)?,
-		Apply(value, ArgsDesc(args), tailstrict) => {
+		Apply(value, args, tailstrict) => {
 			let value = evaluate(context.clone(), value)?.unwrap_if_lazy()?;
 			match value {
 				Val::Intristic(ns, name) => match (&ns as &str, &name as &str) {
@@ -448,7 +443,13 @@
 							assert!(v >= 0.0);
 							let mut out = Vec::with_capacity(v as usize);
 							for i in 0..v as usize {
-								out.push(d.evaluate(vec![(None, Val::Num(i as f64))])?)
+								let call_ctx =
+									Context::new().with_var("v".to_owned(), Val::Num(i as f64))?;
+								out.push(d.evaluate(
+									call_ctx,
+									&ArgsDesc(vec![Arg(None, el!(Expr::Var("v".to_owned())))]),
+									true,
+								)?)
 							}
 							Val::Arr(out)
 						} else {
@@ -536,31 +537,7 @@
 				},
 				Val::Func(f) => {
 					let body = #[inline(always)]
-					|| {
-						f.evaluate(
-							args.clone()
-								.into_iter()
-								.map(
-									#[inline(always)]
-									move |a| {
-										Ok((
-											a.clone().0,
-											if *tailstrict {
-												Val::Lazy(LazyVal::new_resolved(evaluate(
-													context.clone(),
-													&a.1,
-												)?))
-											} else {
-												Val::Lazy(lazy_val!(
-													closure!(clone context, clone a, || evaluate(context.clone(), &a.clone().1))
-												))
-											},
-										))
-									},
-								)
-								.collect::<Result<Vec<_>>>()?,
-						)
-					};
+					|| f.evaluate(context, args, *tailstrict);
 					if *tailstrict {
 						body()?
 					} else {
@@ -570,7 +547,7 @@
 				_ => panic!("{:?} is not a function", value),
 			}
 		}
-		Function(params, body) => evaluate_method(context, body, params.clone()),
+		Function(params, body) => evaluate_method(context, params.clone(), body.clone()),
 		AssertExpr(AssertStmt(value, msg), returned) => {
 			let assertion_result = push(value.clone(), "assertion condition".to_owned(), || {
 				evaluate(context.clone(), &value)?
modifiedcrates/jsonnet-evaluator/src/function.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/function.rs
+++ b/crates/jsonnet-evaluator/src/function.rs
@@ -0,0 +1,80 @@
+use crate::{create_error, evaluate, lazy_val, resolved_lazy_val, Context, Error, Result};
+use closure::closure;
+use jsonnet_parser::{ArgsDesc, ParamsDesc};
+use std::collections::HashMap;
+
+/// Creates correct [context](Context) for function body evaluation, returning error on invalid call
+///
+/// * `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
+/// * `tailstruct` if true - function arguments is eager executed, otherwise - lazy
+pub fn parse_function_call(
+	ctx: Context,
+	body_ctx: Option<Context>,
+	params: &ParamsDesc,
+	args: &ArgsDesc,
+	tailstrict: bool,
+) -> Result<Context> {
+	inline_parse_function_call(ctx, body_ctx, params, args, tailstrict)
+}
+
+/// See [parse_function_call](parse_function_call)
+///
+/// ## Notes
+/// This function is always inlined for tailstrict
+#[inline(always)]
+pub(crate) fn inline_parse_function_call(
+	ctx: Context,
+	body_ctx: Option<Context>,
+	params: &ParamsDesc,
+	args: &ArgsDesc,
+	tailstrict: bool,
+) -> Result<Context> {
+	let mut out = HashMap::new();
+	let mut positioned_args = vec![None; params.0.len()];
+	for (id, arg) in args.iter().enumerate() {
+		let idx = if let Some(name) = &arg.0 {
+			params.iter().position(|p| &p.0 == name).ok_or_else(|| {
+				create_error::<()>(Error::UnknownFunctionParameter(name.clone()))
+					.err()
+					.unwrap()
+			})?
+		} else {
+			id
+		};
+
+		if idx >= params.len() {
+			create_error(Error::TooManyArgsFunctionHas(params.len()))?;
+		}
+		if positioned_args[idx].is_some() {
+			create_error(Error::BindingParameterASecondTime(params[idx].0.clone()))?;
+		}
+		positioned_args[idx] = Some(arg.1.clone());
+	}
+	// Fill defaults
+	for (id, p) in params.iter().enumerate() {
+		let (ctx, expr) = if let Some(arg) = &positioned_args[id] {
+			(ctx.clone(), arg)
+		} else if let Some(default) = &p.1 {
+			(
+				body_ctx
+					.clone()
+					.expect("no default context set for call with defined default parameter value"),
+				default,
+			)
+		} else {
+			create_error(Error::FunctionParameterNotBoundInCall(p.0.clone()))?;
+			unreachable!()
+		};
+		let val = if tailstrict {
+			resolved_lazy_val!(evaluate(ctx.clone(), expr)?)
+		} else {
+			lazy_val!(closure!(clone ctx, clone expr, ||evaluate(ctx.clone(), &expr)))
+		};
+		out.insert(p.0.clone(), val);
+	}
+
+	Ok(body_ctx.unwrap_or(ctx).extend(out, None, None, None)?)
+}
modifiedcrates/jsonnet-evaluator/src/lib.rsdiffbeforeafterboth
before · crates/jsonnet-evaluator/src/lib.rs
1#![feature(box_syntax, box_patterns)]2#![feature(type_alias_impl_trait)]3#![feature(debug_non_exhaustive)]4#![allow(macro_expanded_macro_exports_accessed_by_absolute_paths)]5#![feature(stmt_expr_attributes)]6mod ctx;7mod dynamic;8mod error;9mod evaluate;10mod function;11mod obj;12mod val;1314pub use ctx::*;15pub use dynamic::*;16pub use error::*;17pub use evaluate::*;18use jsonnet_parser::*;19pub use obj::*;20use std::{cell::RefCell, collections::HashMap, fmt::Debug, path::PathBuf, rc::Rc};21pub use val::*;2223rc_fn_helper!(24	Binding,25	binding,26	dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<Val>27);28rc_fn_helper!(FunctionRhs, function_rhs, dyn Fn(Context) -> Result<Val>);29rc_fn_helper!(30	FunctionDefault,31	function_default,32	dyn Fn(Context, LocExpr) -> Result<Val>33);3435#[derive(Clone)]36pub enum LazyBinding {37	Bindable(Rc<dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<LazyVal>>),38	Bound(LazyVal),39}4041impl Debug for LazyBinding {42	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {43		write!(f, "LazyBinding")44	}45}46impl LazyBinding {47	pub fn evaluate(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {48		match self {49			LazyBinding::Bindable(v) => v(this, super_obj),50			LazyBinding::Bound(v) => Ok(v.clone()),51		}52	}53}5455pub struct EvaluationSettings {56	max_stack_frames: usize,57	max_stack_trace_size: usize,58}59impl Default for EvaluationSettings {60	fn default() -> Self {61		EvaluationSettings {62			max_stack_frames: 500,63			max_stack_trace_size: 20,64		}65	}66}6768pub struct FileData(String, LocExpr, Option<Val>);69#[derive(Default)]70pub struct EvaluationStateInternals {71	/// Used for stack-overflows and stacktraces72	stack: RefCell<Vec<StackTraceElement>>,73	/// Contains file source codes and evaluated results for imports and pretty74	/// printing stacktraces75	files: RefCell<HashMap<PathBuf, FileData>>,76	globals: RefCell<HashMap<String, Val>>,7778	settings: EvaluationSettings,79}8081thread_local! {82	/// Contains state for currently executing file83	/// Global state is fine there84	pub(crate) static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)85}86#[inline(always)]87pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {88	EVAL_STATE.with(89		#[inline(always)]90		|s| f(s.borrow().as_ref().unwrap()),91	)92}93pub(crate) fn create_error<T>(err: Error) -> Result<T> {94	with_state(|s| s.error(err))95}96#[inline(always)]97pub(crate) fn push<T>(e: LocExpr, comment: String, f: impl FnOnce() -> Result<T>) -> Result<T> {98	with_state(|s| s.push(e, comment, f))99}100101/// Maintains stack trace and import resolution102#[derive(Default, Clone)]103pub struct EvaluationState(Rc<EvaluationStateInternals>);104impl EvaluationState {105	pub fn add_file(&self, name: PathBuf, code: String) -> std::result::Result<(), ParseError> {106		self.0.files.borrow_mut().insert(107			name.clone(),108			FileData(109				code.clone(),110				parse(111					&code,112					&ParserSettings {113						file_name: name,114						loc_data: true,115					},116				)?,117				None,118			),119		);120121		Ok(())122	}123	pub fn add_parsed_file(124		&self,125		name: PathBuf,126		code: String,127		parsed: LocExpr,128	) -> std::result::Result<(), ()> {129		self.0130			.files131			.borrow_mut()132			.insert(name, FileData(code, parsed, None));133134		Ok(())135	}136	pub fn get_source(&self, name: &PathBuf) -> Option<String> {137		let ro_map = self.0.files.borrow();138		ro_map.get(name).map(|value| value.0.clone())139	}140	pub fn evaluate_file(&self, name: &PathBuf) -> Result<Val> {141		self.begin_state();142		let expr: LocExpr = {143			let ro_map = self.0.files.borrow();144			let value = ro_map145				.get(name)146				.unwrap_or_else(|| panic!("file not added: {:?}", name));147			if value.2.is_some() {148				return Ok(value.2.clone().unwrap());149			}150			value.1.clone()151		};152		let value = evaluate(self.create_default_context()?, &expr)?;153		{154			self.0155				.files156				.borrow_mut()157				.get_mut(name)158				.unwrap()159				.2160				.replace(value.clone());161		}162		self.end_state();163		Ok(value)164	}165166	pub fn parse_evaluate_raw(&self, code: &str) -> Result<Val> {167		let parsed = parse(168			&code,169			&ParserSettings {170				file_name: PathBuf::from("raw.jsonnet"),171				loc_data: true,172			},173		);174		self.begin_state();175		let value = evaluate(self.create_default_context()?, &parsed.unwrap());176		self.end_state();177		value178	}179180	pub fn add_global(&self, name: String, value: Val) {181		self.0.globals.borrow_mut().insert(name, value);182	}183184	pub fn add_stdlib(&self) {185		self.begin_state();186		use jsonnet_stdlib::STDLIB_STR;187		if cfg!(feature = "serialized-stdlib") {188			self.add_parsed_file(189				PathBuf::from("std.jsonnet"),190				STDLIB_STR.to_owned(),191				bincode::deserialize(include_bytes!(concat!(env!("OUT_DIR"), "/stdlib.bincode")))192					.expect("deserialize stdlib"),193			)194			.unwrap();195		} else {196			self.add_file(PathBuf::from("std.jsonnet"), STDLIB_STR.to_owned())197				.unwrap();198		}199		let val = self.evaluate_file(&PathBuf::from("std.jsonnet")).unwrap();200		self.add_global("std".to_owned(), val);201		self.end_state();202	}203204	pub fn create_default_context(&self) -> Result<Context> {205		let globals = self.0.globals.borrow();206		let mut new_bindings: HashMap<String, LazyBinding> = HashMap::new();207		for (name, value) in globals.iter() {208			new_bindings.insert(209				name.clone(),210				LazyBinding::Bound(resolved_lazy_val!(value.clone())),211			);212		}213		Context::new().extend(new_bindings, None, None, None)214	}215216	#[inline(always)]217	pub fn push<T>(&self, e: LocExpr, comment: String, f: impl FnOnce() -> Result<T>) -> Result<T> {218		{219			let mut stack = self.0.stack.borrow_mut();220			if stack.len() > self.0.settings.max_stack_frames {221				drop(stack);222				return self.error(Error::StackOverflow);223			} else {224				stack.push(StackTraceElement(e, comment));225			}226		}227		let result = f();228		self.0.stack.borrow_mut().pop();229		result230	}231	pub fn print_stack_trace(&self) {232		for e in self.stack_trace().0 {233			println!("{:?} - {:?}", e.0, e.1)234		}235	}236	pub fn stack_trace(&self) -> StackTrace {237		StackTrace(238			self.0239				.stack240				.borrow()241				.iter()242				.rev()243				.take(self.0.settings.max_stack_trace_size)244				.cloned()245				.collect(),246		)247	}248	pub fn error<T>(&self, err: Error) -> Result<T> {249		Err(LocError(err, self.stack_trace()))250	}251252	fn begin_state(&self) {253		EVAL_STATE.with(|v| v.borrow_mut().replace(self.clone()));254	}255	fn end_state(&self) {256		EVAL_STATE.with(|v| v.borrow_mut().take());257	}258}259260#[cfg(test)]261pub mod tests {262	use super::Val;263	use crate::EvaluationState;264	use jsonnet_parser::*;265	use std::path::PathBuf;266267	#[test]268	fn eval_state_stacktrace() {269		let state = EvaluationState::default();270		state271			.push(272				loc_expr!(273					Expr::Num(0.0),274					true,275					(PathBuf::from("test1.jsonnet"), 10, 20)276				),277				"outer".to_owned(),278				|| {279					state.push(280						loc_expr!(281							Expr::Num(0.0),282							true,283							(PathBuf::from("test2.jsonnet"), 30, 40)284						),285						"inner".to_owned(),286						|| {287							state.print_stack_trace();288							Ok(())289						},290					)?;291					Ok(())292				},293			)294			.unwrap();295	}296297	#[test]298	fn eval_state_standard() {299		let state = EvaluationState::default();300		state.add_stdlib();301		assert_eq!(302			state303				.parse_evaluate_raw(r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#)304				.unwrap(),305			Val::Bool(true)306		);307	}308309	macro_rules! eval {310		($str: expr) => {311			evaluate(312				Context::new(),313				EvaluationState::default(),314				&parse(315					$str,316					&ParserSettings {317						loc_data: true,318						file_name: "test.jsonnet".to_owned(),319					},320					)321				.unwrap(),322				)323		};324	}325326	macro_rules! eval_stdlib {327		($str: expr) => {{328			let std = "local std = ".to_owned() + jsonnet_stdlib::STDLIB_STR + ";";329			evaluate(330				Context::new(),331				EvaluationState::default(),332				&parse(333					&(std + $str),334					&ParserSettings {335						loc_data: true,336						file_name: "test.jsonnet".to_owned(),337					},338					)339				.unwrap(),340				)341			}};342	}343344	macro_rules! assert_eval {345		($str: expr) => {346			assert_eq!(347				evaluate(348					Context::new(),349					EvaluationState::default(),350					&parse(351						$str,352						&ParserSettings {353							loc_data: true,354							file_name: "test.jsonnet".to_owned(),355						}356						)357					.unwrap()358					),359				Val::Bool(true)360				)361		};362	}363	macro_rules! assert_json {364		($str: expr, $out: expr) => {365			assert_eq!(366				format!(367					"{}",368					evaluate(369						Context::new(),370						EvaluationState::default(),371						&parse(372							$str,373							&ParserSettings {374								loc_data: true,375								file_name: "test.jsonnet".to_owned(),376							}377						)378						.unwrap()379						)380					),381				$out382				)383		};384	}385	macro_rules! assert_json_stdlib {386		($str: expr, $out: expr) => {387			assert_eq!(format!("{}", eval_stdlib!($str)), $out)388		};389	}390	macro_rules! assert_eval_neg {391		($str: expr) => {392			assert_eq!(393				evaluate(394					Context::new(),395					EvaluationState::default(),396					&parse(397						$str,398						&ParserSettings {399							loc_data: true,400							file_name: "test.jsonnet".to_owned(),401						}402						)403					.unwrap()404					),405				Val::Bool(false)406				)407		};408	}409410	/*411	/// Sanity checking, before trusting to another tests412	#[test]413	fn equality_operator() {414		assert_eval!("2 == 2");415		assert_eval_neg!("2 != 2");416		assert_eval!("2 != 3");417		assert_eval_neg!("2 == 3");418		assert_eval!("'Hello' == 'Hello'");419		assert_eval_neg!("'Hello' != 'Hello'");420		assert_eval!("'Hello' != 'World'");421		assert_eval_neg!("'Hello' == 'World'");422	}423424	#[test]425	fn math_evaluation() {426		assert_eval!("2 + 2 * 2 == 6");427		assert_eval!("3 + (2 + 2 * 2) == 9");428	}429430	#[test]431	fn string_concat() {432		assert_eval!("'Hello' + 'World' == 'HelloWorld'");433		assert_eval!("'Hello' * 3 == 'HelloHelloHello'");434		assert_eval!("'Hello' + 'World' * 3 == 'HelloWorldWorldWorld'");435	}436437	#[test]438	fn local() {439		assert_eval!("local a = 2; local b = 3; a + b == 5");440		assert_eval!("local a = 1, b = a + 1; a + b == 3");441		assert_eval!("local a = 1; local a = 2; a == 2");442	}443444	#[test]445	fn object_lazyness() {446		assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);447	}448449	#[test]450	fn object_inheritance() {451		assert_json!("{a: self.b} + {b:3}", r#"{"a":3,"b":3}"#);452	}453454	#[test]455	fn test_object() {456		assert_json!("{a:2}", r#"{"a":2}"#);457		assert_json!("{a:2+2}", r#"{"a":4}"#);458		assert_json!("{a:2}+{b:2}", r#"{"a":2,"b":2}"#);459		assert_json!("{b:3}+{b:2}", r#"{"b":2}"#);460		assert_json!("{b:3}+{b+:2}", r#"{"b":5}"#);461		assert_json!("local test='a'; {[test]:2}", r#"{"a":2}"#);462		assert_json!(463			r#"464				{465					name: "Alice",466					welcome: "Hello " + self.name + "!",467				}468			"#,469			r#"{"name":"Alice","welcome":"Hello Alice!"}"#470		);471		assert_json!(472			r#"473				{474					name: "Alice",475					welcome: "Hello " + self.name + "!",476				} + {477					name: "Bob"478				}479			"#,480			r#"{"name":"Bob","welcome":"Hello Bob!"}"#481		);482	}483484	#[test]485	fn functions() {486		assert_json!(r#"local a = function(b, c = 2) b + c; a(2)"#, "4");487		assert_json!(488			r#"local a = function(b, c = "Dear") b + c + d, d = "World"; a("Hello")"#,489			r#""HelloDearWorld""#490		);491	}492493	#[test]494	fn local_methods() {495		assert_json!(r#"local a(b, c = 2) = b + c; a(2)"#, "4");496		assert_json!(497			r#"local a(b, c = "Dear") = b + c + d, d = "World"; a("Hello")"#,498			r#""HelloDearWorld""#499		);500	}501502	#[test]503	fn object_locals() {504		assert_json!(r#"{local a = 3, b: a}"#, r#"{"b":3}"#);505		assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b":3}"#);506		assert_json!(507			r#"{local a = function (b) {[b]:4}, test: a("test")}"#,508			r#"{"test":{"test":4}}"#509		);510	}511512	#[test]513	fn direct_self() {514		println!(515			"{:#?}",516			eval!(517				r#"518					{519						local me = self,520						a: 3,521						b(): me.a,522					}523				"#524			)525		);526	}527528	#[test]529	fn indirect_self() {530		// `self` assigned to `me` was lost when being531		// referenced from field532		eval_stdlib!(533			r#"{534				local me = self,535				a: 3,536				b: me.a,537			}.b"#538		);539	}540541	// We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly542	#[test]543	fn std_assert_ok() {544		eval_stdlib!("std.assertEqual(4.5 << 2, 16)");545	}546547	#[test]548	#[should_panic]549	fn std_assert_failure() {550		eval_stdlib!("std.assertEqual(4.5 << 2, 15)");551	}552553	#[test]554	fn string_is_string() {555		assert_eq!(556			eval_stdlib!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),557			Val::Bool(false)558		);559	}560561	#[test]562	fn base64_works() {563		assert_json_stdlib!(r#"std.base64("test")"#, r#""dGVzdA==""#);564	}565566	#[test]567	fn utf8_chars() {568		assert_json_stdlib!(569			r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,570			r#"{"c":128526,"l":1}"#571		)572	}573574	#[test]575	fn json() {576		assert_json_stdlib!(577			r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,578			r#""{\n"a": 3,\n"b": 4,\n"c": 6\n}""#579		);580	}581582	#[test]583	fn test() {584		assert_json_stdlib!(585			r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,586			"[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"587		);588	}589590	#[test]591	fn sjsonnet() {592		eval!(593			r#"594			local x0 = {k: 1};595			local x1 = {k: x0.k + x0.k};596			local x2 = {k: x1.k + x1.k};597			local x3 = {k: x2.k + x2.k};598			local x4 = {k: x3.k + x3.k};599			local x5 = {k: x4.k + x4.k};600			local x6 = {k: x5.k + x5.k};601			local x7 = {k: x6.k + x6.k};602			local x8 = {k: x7.k + x7.k};603			local x9 = {k: x8.k + x8.k};604			local x10 = {k: x9.k + x9.k};605			local x11 = {k: x10.k + x10.k};606			local x12 = {k: x11.k + x11.k};607			local x13 = {k: x12.k + x12.k};608			local x14 = {k: x13.k + x13.k};609			local x15 = {k: x14.k + x14.k};610			local x16 = {k: x15.k + x15.k};611			local x17 = {k: x16.k + x16.k};612			local x18 = {k: x17.k + x17.k};613			local x19 = {k: x18.k + x18.k};614			local x20 = {k: x19.k + x19.k};615			local x21 = {k: x20.k + x20.k};616			x21.k617		"#618		);619	}620	*/621}
addedcrates/jsonnet-evaluator/src/map.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jsonnet-evaluator/src/map.rs
@@ -0,0 +1,46 @@
+use std::{borrow::Borrow, collections::HashMap, hash::Hash, rc::Rc};
+
+#[derive(Default, Debug)]
+struct LayeredHashMapInternals<K: Hash, V> {
+	parent: Option<LayeredHashMap<K, V>>,
+	current: HashMap<K, V>,
+}
+
+#[derive(Debug)]
+pub struct LayeredHashMap<K: Hash, V>(Rc<LayeredHashMapInternals<K, V>>);
+
+impl<K: Hash + Eq, V> LayeredHashMap<K, V> {
+	pub fn extend(&self, new_layer: HashMap<K, V>) -> Self {
+		let super_map = self.clone();
+		LayeredHashMap(Rc::new(LayeredHashMapInternals {
+			parent: Some(super_map),
+			current: new_layer,
+		}))
+	}
+
+	pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V>
+	where
+		K: Borrow<Q>,
+		Q: Hash + Eq,
+	{
+		(self.0)
+			.current
+			.get(&key)
+			.or_else(|| self.0.parent.as_ref().and_then(|p| p.get(key)))
+	}
+}
+
+impl<K: Hash, V> Clone for LayeredHashMap<K, V> {
+	fn clone(&self) -> Self {
+		LayeredHashMap(self.0.clone())
+	}
+}
+
+impl<K: Hash + Eq, V> Default for LayeredHashMap<K, V> {
+	fn default() -> Self {
+		LayeredHashMap(Rc::new(LayeredHashMapInternals {
+			parent: None,
+			current: HashMap::new(),
+		}))
+	}
+}
modifiedcrates/jsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/val.rs
+++ b/crates/jsonnet-evaluator/src/val.rs
@@ -1,11 +1,9 @@
 use crate::{
-	create_error, Context, Error, FunctionDefault, FunctionRhs, LazyBinding, ObjValue, Result,
+	create_error, evaluate, function::inline_parse_function_call, Context, Error, ObjValue, Result,
 };
-use closure::closure;
-use jsonnet_parser::{Param, ParamsDesc};
+use jsonnet_parser::{ArgsDesc, LocExpr, ParamsDesc};
 use std::{
 	cell::RefCell,
-	collections::HashMap,
 	fmt::{Debug, Display},
 	rc::Rc,
 };
@@ -73,47 +71,21 @@
 pub struct FuncDesc {
 	pub ctx: Context,
 	pub params: ParamsDesc,
-	pub eval_rhs: FunctionRhs,
-	pub eval_default: FunctionDefault,
+	pub body: LocExpr,
 }
 impl FuncDesc {
 	// TODO: Check for unset variables
 	/// This function is always inlined to make tailstrict work
 	#[inline(always)]
-	pub fn evaluate(&self, args: Vec<(Option<String>, Val)>) -> Result<Val> {
-		let mut new_bindings: HashMap<String, LazyBinding> = HashMap::new();
-		let future_ctx = Context::new_future();
-
-		for Param(name, default) in self.params.with_defaults() {
-			let default = default.unwrap();
-			let eval_default = self.eval_default.clone();
-			new_bindings.insert(
-				name,
-				LazyBinding::Bound(lazy_val!(
-					closure!(clone future_ctx, clone eval_default, clone default, || (eval_default.clone()).0
-					(future_ctx.clone().unwrap(), default.clone()))
-				)),
-			);
-		}
-		for (name, val) in args.clone().into_iter().filter(|e| e.0.is_some()) {
-			new_bindings.insert(
-				name.as_ref().unwrap().clone(),
-				LazyBinding::Bound(resolved_lazy_val!(val.clone())),
-			);
-		}
-		for (i, param) in self.params.0.iter().enumerate() {
-			if let Some((None, val)) = args.get(i) {
-				new_bindings.insert(
-					param.0.clone(),
-					LazyBinding::Bound(resolved_lazy_val!(val.clone())),
-				);
-			}
-		}
-		let ctx = self
-			.ctx
-			.extend(new_bindings, None, None, None)?
-			.into_future(future_ctx);
-		self.eval_rhs.0(ctx)
+	pub fn evaluate(&self, call_ctx: Context, args: &ArgsDesc, tailstrict: bool) -> Result<Val> {
+		let ctx = inline_parse_function_call(
+			call_ctx,
+			Some(self.ctx.clone()),
+			&self.params,
+			args,
+			tailstrict,
+		)?;
+		evaluate(ctx, &self.body)
 	}
 }