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
before · cmds/jrsonnet/src/main.rs
1pub mod location;23use clap::Clap;4use jsonnet_evaluator::{EvaluationState, LocError, StackTrace, Val};5use location::{offset_to_location, CodeLocation};6use std::env::current_dir;7use std::{path::PathBuf, str::FromStr};89enum Format {10	None,11	Json,12	Yaml,13}1415impl FromStr for Format {16	type Err = &'static str;17	fn from_str(s: &str) -> Result<Self, Self::Err> {18		Ok(match s {19			"none" => Format::None,20			"json" => Format::Json,21			"yaml" => Format::Yaml,22			_ => return Err("no such format"),23		})24	}25}2627#[derive(PartialEq)]28enum TraceFormat {29	CppJsonnet,30	GoJsonnet,31	Custom,32}33impl FromStr for TraceFormat {34	type Err = &'static str;35	fn from_str(s: &str) -> Result<Self, Self::Err> {36		Ok(match s {37			"cpp" => TraceFormat::CppJsonnet,38			"go" => TraceFormat::GoJsonnet,39			"default" => TraceFormat::Custom,40			_ => return Err("no such format"),41		})42	}43}4445#[derive(Clap)]46#[clap(version = "0.1.0", author = "Lach <iam@lach.pw>")]47struct Opts {48	#[clap(long, about = "Disable global std variable")]49	no_stdlib: bool,50	#[clap(long, about = "Add external string")]51	ext_str: Option<Vec<String>>,52	#[clap(long, about = "Add external string from code")]53	ext_code: Option<Vec<String>>,54	#[clap(long, about = "Add TLA")]55	tla_str: Option<Vec<String>>,56	#[clap(long, about = "Add TLA from code")]57	tla_code: Option<Vec<String>>,58	#[clap(long, short = "f", default_value = "json", possible_values = &["none", "json", "yaml"], about = "Output format, wraps resulting value to corresponding std.manifest call")]59	format: Format,60	#[clap(long, default_value = "default", possible_values = &["cpp", "go", "default"], about = "Emulated needed stacktrace display")]61	trace_format: TraceFormat,6263	#[clap(64		long,65		short = "s",66		default_value = "200",67		about = "Number of allowed stack frames"68	)]69	max_stack: usize,70	#[clap(71		long,72		short = "t",73		default_value = "20",74		about = "Max length of stack trace before cropping"75	)]76	max_trace: usize,7778	#[clap(79		long,80		default_value = "3",81		about = "When using --format, this option specifies string to pad output with"82	)]83	line_padding: usize,8485	#[clap(about = "File to compile", index = 1)]86	input: String,87}8889fn main() {90	let opts: Opts = Opts::parse();91	let evaluator = jsonnet_evaluator::EvaluationState::default();92	if !opts.no_stdlib {93		evaluator.add_stdlib();94	}95	let mut input = current_dir().unwrap();96	input.push(opts.input.clone());97	let code_string = String::from_utf8(std::fs::read(opts.input.clone()).unwrap()).unwrap();98	if let Err(e) = evaluator.add_file(input.clone(), code_string.clone()) {99		print_syntax_error(e, &input, &code_string);100		std::process::exit(1);101	}102	let result = evaluator.evaluate_file(&input);103	match result {104		Ok(v) => {105			let v = match opts.format {106				Format::Json => {107					if opts.no_stdlib {108						evaluator.add_stdlib();109					}110					evaluator.add_global("__tmp__to_json__".to_owned(), v);111					let v = evaluator.parse_evaluate_raw(&format!(112						"std.manifestJsonEx(__tmp__to_json__, \"{}\")",113						" ".repeat(opts.line_padding),114					));115					match v {116						Ok(v) => v,117						Err(err) => {118							print_error(&err, evaluator, &opts);119							std::process::exit(1);120						}121					}122				}123				Format::Yaml => {124					if opts.no_stdlib {125						evaluator.add_stdlib();126					}127					evaluator.add_global("__tmp__to_yaml__".to_owned(), v);128					let v = evaluator129						.parse_evaluate_raw("std.manifestYamlDoc(__tmp__to_yaml__, \"  \")");130					match v {131						Ok(v) => v,132						Err(err) => {133							print_error(&err, evaluator, &opts);134							std::process::exit(1);135						}136					}137				}138				_ => v,139			};140			match v {141				Val::Str(s) => println!("{}", s),142				Val::Num(n) => println!("{}", n),143				_v => eprintln!(144					"jsonnet output is not a string.\nDid you forgot to set --format, or wrap your data with std.manifestJson?"145				),146			}147		}148		Err(err) => {149			print_error(&err, evaluator, &opts);150			std::process::exit(1);151		}152	}153}154155fn print_error(err: &LocError, evaluator: EvaluationState, opts: &Opts) {156	println!("Error: {:?}", err.0);157	print_trace(&(err.1), evaluator, &opts);158}159160fn print_syntax_error(error: jsonnet_parser::ParseError, file: &PathBuf, code: &str) {161	use annotate_snippets::{162		display_list::{DisplayList, FormatOptions},163		snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},164	};165	//&("Expected: ".to_owned() + error.expected)166	let origin = file.to_str().unwrap();167	let error_message = format!("Expected: {}", error.expected);168	let snippet = Snippet {169		opt: FormatOptions {170			color: true,171			..Default::default()172		},173		title: Some(Annotation {174			label: Some(&error_message),175			id: None,176			annotation_type: AnnotationType::Error,177		}),178		footer: vec![],179		slices: vec![Slice {180			source: &code,181			line_start: 1,182			origin: Some(origin),183			fold: false,184			annotations: vec![SourceAnnotation {185				label: "At this position",186				annotation_type: AnnotationType::Error,187				range: (error.location.offset, error.location.offset + 1),188			}],189		}],190	};191192	let dl = DisplayList::from(snippet);193	println!("{}", dl);194}195196fn print_trace(trace: &StackTrace, evaluator: EvaluationState, opts: &Opts) {197	use annotate_snippets::{198		display_list::{DisplayList, FormatOptions},199		snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},200	};201	for item in trace.0.iter() {202		let desc = &item.1;203		if (item.0).1.is_none() {204			continue;205		}206		let source = (item.0).1.clone().unwrap();207		let code = evaluator.get_source(&source.0);208		if code.is_none() {209			continue;210		}211		let code = code.unwrap();212		let start_end = offset_to_location(&code, &[source.1, source.2]);213		if opts.trace_format == TraceFormat::Custom {214			let source_fragment: String = code215				.chars()216				.skip(start_end[0].line_start_offset)217				.take(start_end[1].line_end_offset - start_end[0].line_start_offset)218				.collect();219			let snippet = Snippet {220				opt: FormatOptions {221					color: true,222					..Default::default()223				},224				title: Some(Annotation {225					label: Some(&item.1),226					id: None,227					annotation_type: AnnotationType::Error,228				}),229				footer: vec![],230				slices: vec![Slice {231					source: &source_fragment,232					line_start: start_end[0].line,233					origin: Some(&source.0.to_str().unwrap()),234					fold: false,235					annotations: vec![SourceAnnotation {236						label: desc,237						annotation_type: AnnotationType::Error,238						range: (239							source.1 - start_end[0].line_start_offset,240							source.2 - start_end[0].line_start_offset,241						),242					}],243				}],244			};245246			let dl = DisplayList::from(snippet);247			println!("{}", dl);248		} else {249			print_jsonnet_pair(250				source.0.to_str().unwrap(),251				&start_end[0],252				&start_end[1],253				opts.trace_format == TraceFormat::GoJsonnet,254			);255		}256	}257}258259fn print_jsonnet_pair(file: &str, start: &CodeLocation, end: &CodeLocation, is_go: bool) {260	if is_go {261		print!("        ");262	} else {263		print!("  ");264	}265	print!("{}:", file);266	if start.line == end.line {267		// IDK why, but this is the behavior original jsonnet cpp impl shows268		if start.column == end.column || !is_go && start.column + 1 == end.column {269			println!("{}:{}", start.line, end.column)270		} else {271			println!("{}:{}-{}", start.line, start.column, end.column);272		}273	} else {274		println!(275			"({}:{})-({}:{})",276			start.line, end.column, start.line, end.column277		);278	}279}
after · cmds/jrsonnet/src/main.rs
1pub mod location;23use clap::Clap;4use jsonnet_evaluator::{EvaluationState, LocError, StackTrace, Val};5use location::{offset_to_location, CodeLocation};6use std::env::current_dir;7use std::{path::PathBuf, str::FromStr};89enum Format {10	None,11	Json,12	Yaml,13}1415impl FromStr for Format {16	type Err = &'static str;17	fn from_str(s: &str) -> Result<Self, Self::Err> {18		Ok(match s {19			"none" => Format::None,20			"json" => Format::Json,21			"yaml" => Format::Yaml,22			_ => return Err("no such format"),23		})24	}25}2627#[derive(PartialEq)]28enum TraceFormat {29	CppJsonnet,30	GoJsonnet,31	Custom,32}33impl FromStr for TraceFormat {34	type Err = &'static str;35	fn from_str(s: &str) -> Result<Self, Self::Err> {36		Ok(match s {37			"cpp" => TraceFormat::CppJsonnet,38			"go" => TraceFormat::GoJsonnet,39			"default" => TraceFormat::Custom,40			_ => return Err("no such format"),41		})42	}43}4445#[derive(Clap)]46#[clap(version = "0.1.0", author = "Lach <iam@lach.pw>")]47struct Opts {48	#[clap(long, about = "Disable global std variable")]49	no_stdlib: bool,50	#[clap(long, about = "Add external string")]51	ext_str: Option<Vec<String>>,52	#[clap(long, about = "Add external string from code")]53	ext_code: Option<Vec<String>>,54	#[clap(long, about = "Add TLA")]55	tla_str: Option<Vec<String>>,56	#[clap(long, about = "Add TLA from code")]57	tla_code: Option<Vec<String>>,58	#[clap(long, short = "f", default_value = "json", possible_values = &["none", "json", "yaml"], about = "Output format, wraps resulting value to corresponding std.manifest call")]59	format: Format,60	#[clap(long, default_value = "default", possible_values = &["cpp", "go", "default"], about = "Emulated needed stacktrace display")]61	trace_format: TraceFormat,6263	#[clap(64		long,65		short = "s",66		default_value = "200",67		about = "Number of allowed stack frames"68	)]69	max_stack: usize,70	#[clap(71		long,72		short = "t",73		default_value = "20",74		about = "Max length of stack trace before cropping"75	)]76	max_trace: usize,7778	#[clap(79		long,80		default_value = "3",81		about = "When using --format, this option specifies string to pad output with"82	)]83	line_padding: usize,8485	#[clap(about = "File to compile", index = 1)]86	input: String,87}8889fn main() {90	let opts: Opts = Opts::parse();91	let evaluator = jsonnet_evaluator::EvaluationState::default();92	if !opts.no_stdlib {93		evaluator.with_stdlib();94	}95	let mut input = current_dir().unwrap();96	input.push(opts.input.clone());97	let code_string = String::from_utf8(std::fs::read(opts.input.clone()).unwrap()).unwrap();98	if let Err(e) = evaluator.add_file(input.clone(), code_string.clone()) {99		print_syntax_error(e, &input, &code_string);100		std::process::exit(1);101	}102	let result = evaluator.evaluate_file(&input);103	match result {104		Ok(v) => {105			let v = match opts.format {106				Format::Json => {107					if opts.no_stdlib {108						evaluator.with_stdlib();109					}110					evaluator.add_global("__tmp__to_json__".to_owned(), v);111					let v = evaluator.parse_evaluate_raw(&format!(112						"std.manifestJsonEx(__tmp__to_json__, \"{}\")",113						" ".repeat(opts.line_padding),114					));115					match v {116						Ok(v) => v,117						Err(err) => {118							print_error(&err, evaluator, &opts);119							std::process::exit(1);120						}121					}122				}123				Format::Yaml => {124					if opts.no_stdlib {125						evaluator.with_stdlib();126					}127					evaluator.add_global("__tmp__to_yaml__".to_owned(), v);128					let v = evaluator129						.parse_evaluate_raw("std.manifestYamlDoc(__tmp__to_yaml__, \"  \")");130					match v {131						Ok(v) => v,132						Err(err) => {133							print_error(&err, evaluator, &opts);134							std::process::exit(1);135						}136					}137				}138				_ => v,139			};140			match v {141				Val::Str(s) => println!("{}", s),142				Val::Num(n) => println!("{}", n),143				_v => eprintln!(144					"jsonnet output is not a string.\nDid you forgot to set --format, or wrap your data with std.manifestJson?"145				),146			}147		}148		Err(err) => {149			print_error(&err, evaluator, &opts);150			std::process::exit(1);151		}152	}153}154155fn print_error(err: &LocError, evaluator: EvaluationState, opts: &Opts) {156	println!("Error: {:?}", err.0);157	print_trace(&(err.1), evaluator, &opts);158}159160fn print_syntax_error(error: jsonnet_parser::ParseError, file: &PathBuf, code: &str) {161	use annotate_snippets::{162		display_list::{DisplayList, FormatOptions},163		snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},164	};165	//&("Expected: ".to_owned() + error.expected)166	let origin = file.to_str().unwrap();167	let error_message = format!("Expected: {}", error.expected);168	let snippet = Snippet {169		opt: FormatOptions {170			color: true,171			..Default::default()172		},173		title: Some(Annotation {174			label: Some(&error_message),175			id: None,176			annotation_type: AnnotationType::Error,177		}),178		footer: vec![],179		slices: vec![Slice {180			source: &code,181			line_start: 1,182			origin: Some(origin),183			fold: false,184			annotations: vec![SourceAnnotation {185				label: "At this position",186				annotation_type: AnnotationType::Error,187				range: (error.location.offset, error.location.offset + 1),188			}],189		}],190	};191192	let dl = DisplayList::from(snippet);193	println!("{}", dl);194}195196fn print_trace(trace: &StackTrace, evaluator: EvaluationState, opts: &Opts) {197	use annotate_snippets::{198		display_list::{DisplayList, FormatOptions},199		snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},200	};201	for item in trace.0.iter() {202		let desc = &item.1;203		if (item.0).1.is_none() {204			continue;205		}206		let source = (item.0).1.clone().unwrap();207		let code = evaluator.get_source(&source.0);208		if code.is_none() {209			continue;210		}211		let code = code.unwrap();212		let start_end = offset_to_location(&code, &[source.1, source.2]);213		if opts.trace_format == TraceFormat::Custom {214			let source_fragment: String = code215				.chars()216				.skip(start_end[0].line_start_offset)217				.take(start_end[1].line_end_offset - start_end[0].line_start_offset)218				.collect();219			let snippet = Snippet {220				opt: FormatOptions {221					color: true,222					..Default::default()223				},224				title: Some(Annotation {225					label: Some(&item.1),226					id: None,227					annotation_type: AnnotationType::Error,228				}),229				footer: vec![],230				slices: vec![Slice {231					source: &source_fragment,232					line_start: start_end[0].line,233					origin: Some(&source.0.to_str().unwrap()),234					fold: false,235					annotations: vec![SourceAnnotation {236						label: desc,237						annotation_type: AnnotationType::Error,238						range: (239							source.1 - start_end[0].line_start_offset,240							source.2 - start_end[0].line_start_offset,241						),242					}],243				}],244			};245246			let dl = DisplayList::from(snippet);247			println!("{}", dl);248		} else {249			print_jsonnet_pair(250				source.0.to_str().unwrap(),251				&start_end[0],252				&start_end[1],253				opts.trace_format == TraceFormat::GoJsonnet,254			);255		}256	}257}258259fn print_jsonnet_pair(file: &str, start: &CodeLocation, end: &CodeLocation, is_go: bool) {260	if is_go {261		print!("        ");262	} else {263		print!("  ");264	}265	print!("{}:", file);266	if start.line == end.line {267		// IDK why, but this is the behavior original jsonnet cpp impl shows268		if start.column == end.column || !is_go && start.column + 1 == end.column {269			println!("{}:{}", start.line, end.column)270		} else {271			println!("{}:{}-{}", start.line, start.column, end.column);272		}273	} else {274		println!(275			"({}:{})-({}:{})",276			start.line, end.column, start.line, end.column277		);278	}279}
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
--- a/crates/jsonnet-evaluator/src/lib.rs
+++ b/crates/jsonnet-evaluator/src/lib.rs
@@ -8,6 +8,7 @@
 mod error;
 mod evaluate;
 mod function;
+mod map;
 mod obj;
 mod val;
 
@@ -15,6 +16,7 @@
 pub use dynamic::*;
 pub use error::*;
 pub use evaluate::*;
+pub use function::parse_function_call;
 use jsonnet_parser::*;
 pub use obj::*;
 use std::{cell::RefCell, collections::HashMap, fmt::Debug, path::PathBuf, rc::Rc};
@@ -24,17 +26,12 @@
 	Binding,
 	binding,
 	dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<Val>
-);
-rc_fn_helper!(FunctionRhs, function_rhs, dyn Fn(Context) -> Result<Val>);
-rc_fn_helper!(
-	FunctionDefault,
-	function_default,
-	dyn Fn(Context, LocExpr) -> Result<Val>
 );
 
+type BindableFn = dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<LazyVal>;
 #[derive(Clone)]
 pub enum LazyBinding {
-	Bindable(Rc<dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<LazyVal>>),
+	Bindable(Rc<BindableFn>),
 	Bound(LazyVal),
 }
 
@@ -59,7 +56,7 @@
 impl Default for EvaluationSettings {
 	fn default() -> Self {
 		EvaluationSettings {
-			max_stack_frames: 500,
+			max_stack_frames: 200,
 			max_stack_trace_size: 20,
 		}
 	}
@@ -181,7 +178,7 @@
 		self.0.globals.borrow_mut().insert(name, value);
 	}
 
-	pub fn add_stdlib(&self) {
+	pub fn with_stdlib(&self) -> &Self {
 		self.begin_state();
 		use jsonnet_stdlib::STDLIB_STR;
 		if cfg!(feature = "serialized-stdlib") {
@@ -199,6 +196,7 @@
 		let val = self.evaluate_file(&PathBuf::from("std.jsonnet")).unwrap();
 		self.add_global("std".to_owned(), val);
 		self.end_state();
+		self
 	}
 
 	pub fn create_default_context(&self) -> Result<Context> {
@@ -210,7 +208,7 @@
 				LazyBinding::Bound(resolved_lazy_val!(value.clone())),
 			);
 		}
-		Context::new().extend(new_bindings, None, None, None)
+		Context::new().extend_unbound(new_bindings, None, None, None)
 	}
 
 	#[inline(always)]
@@ -297,7 +295,7 @@
 	#[test]
 	fn eval_state_standard() {
 		let state = EvaluationState::default();
-		state.add_stdlib();
+		state.with_stdlib();
 		assert_eq!(
 			state
 				.parse_evaluate_raw(r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#)
@@ -308,106 +306,47 @@
 
 	macro_rules! eval {
 		($str: expr) => {
-			evaluate(
-				Context::new(),
-				EvaluationState::default(),
-				&parse(
-					$str,
-					&ParserSettings {
-						loc_data: true,
-						file_name: "test.jsonnet".to_owned(),
-					},
-					)
-				.unwrap(),
-				)
+			EvaluationState::default()
+				.with_stdlib()
+				.parse_evaluate_raw($str)
+				.unwrap()
 		};
 	}
-
-	macro_rules! eval_stdlib {
+	macro_rules! eval_json {
 		($str: expr) => {{
-			let std = "local std = ".to_owned() + jsonnet_stdlib::STDLIB_STR + ";";
-			evaluate(
-				Context::new(),
-				EvaluationState::default(),
-				&parse(
-					&(std + $str),
-					&ParserSettings {
-						loc_data: true,
-						file_name: "test.jsonnet".to_owned(),
-					},
-					)
-				.unwrap(),
-				)
+			let evaluator = EvaluationState::default();
+			evaluator.with_stdlib();
+			let val = evaluator.parse_evaluate_raw($str).unwrap();
+			evaluator.add_global("__tmp__to_yaml__".to_owned(), val);
+			evaluator
+				.parse_evaluate_raw("std.manifestJsonEx(__tmp__to_yaml__, \"\")")
+				.unwrap()
+				.try_cast_str("there should be json string")
+				.unwrap()
+				.clone()
+				.replace("\n", "")
 			}};
 	}
 
+	/// Asserts given code returns `true`
 	macro_rules! assert_eval {
 		($str: expr) => {
-			assert_eq!(
-				evaluate(
-					Context::new(),
-					EvaluationState::default(),
-					&parse(
-						$str,
-						&ParserSettings {
-							loc_data: true,
-							file_name: "test.jsonnet".to_owned(),
-						}
-						)
-					.unwrap()
-					),
-				Val::Bool(true)
-				)
+			assert_eq!(eval!($str), Val::Bool(true))
 		};
 	}
-	macro_rules! assert_json {
-		($str: expr, $out: expr) => {
-			assert_eq!(
-				format!(
-					"{}",
-					evaluate(
-						Context::new(),
-						EvaluationState::default(),
-						&parse(
-							$str,
-							&ParserSettings {
-								loc_data: true,
-								file_name: "test.jsonnet".to_owned(),
-							}
-						)
-						.unwrap()
-						)
-					),
-				$out
-				)
+
+	/// Asserts given code returns `false`
+	macro_rules! assert_eval_neg {
+		($str: expr) => {
+			assert_eq!(eval!($str), Val::Bool(false))
 		};
 	}
-	macro_rules! assert_json_stdlib {
+	macro_rules! assert_json {
 		($str: expr, $out: expr) => {
-			assert_eq!(format!("{}", eval_stdlib!($str)), $out)
-		};
-	}
-	macro_rules! assert_eval_neg {
-		($str: expr) => {
-			assert_eq!(
-				evaluate(
-					Context::new(),
-					EvaluationState::default(),
-					&parse(
-						$str,
-						&ParserSettings {
-							loc_data: true,
-							file_name: "test.jsonnet".to_owned(),
-						}
-						)
-					.unwrap()
-					),
-				Val::Bool(false)
-				)
+			assert_eq!(eval_json!($str), $out.replace("\t", ""))
 		};
 	}
 
-	/*
 	/// Sanity checking, before trusting to another tests
 	#[test]
 	fn equality_operator() {
@@ -435,6 +374,23 @@
 	}
 
 	#[test]
+	fn function_contexts() {
+		assert_eval!(
+			r#"
+				local k = {
+					t(name = self.h): [self.h, name],
+					h: 3,
+				};
+				local f = {
+					t: k.t(),
+					h: 4,
+				};
+				f.t[0] == f.t[1]
+			"#
+		);
+	}
+
+	#[test]
 	fn local() {
 		assert_eval!("local a = 2; local b = 3; a + b == 5");
 		assert_eval!("local a = 1, b = a + 1; a + b == 3");
@@ -446,19 +402,20 @@
 		assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);
 	}
 
+	/// FIXME: This test gets stackoverflow in debug build
 	#[test]
 	fn object_inheritance() {
-		assert_json!("{a: self.b} + {b:3}", r#"{"a":3,"b":3}"#);
+		assert_json!("{a: self.b} + {b:3}", r#"{"a": 3,"b": 3}"#);
 	}
 
 	#[test]
 	fn test_object() {
-		assert_json!("{a:2}", r#"{"a":2}"#);
-		assert_json!("{a:2+2}", r#"{"a":4}"#);
-		assert_json!("{a:2}+{b:2}", r#"{"a":2,"b":2}"#);
-		assert_json!("{b:3}+{b:2}", r#"{"b":2}"#);
-		assert_json!("{b:3}+{b+:2}", r#"{"b":5}"#);
-		assert_json!("local test='a'; {[test]:2}", r#"{"a":2}"#);
+		assert_json!("{a:2}", r#"{"a": 2}"#);
+		assert_json!("{a:2+2}", r#"{"a": 4}"#);
+		assert_json!("{a:2}+{b:2}", r#"{"a": 2,"b": 2}"#);
+		assert_json!("{b:3}+{b:2}", r#"{"b": 2}"#);
+		assert_json!("{b:3}+{b+:2}", r#"{"b": 5}"#);
+		assert_json!("local test='a'; {[test]:2}", r#"{"a": 2}"#);
 		assert_json!(
 			r#"
 				{
@@ -466,7 +423,7 @@
 					welcome: "Hello " + self.name + "!",
 				}
 			"#,
-			r#"{"name":"Alice","welcome":"Hello Alice!"}"#
+			r#"{"name": "Alice","welcome": "Hello Alice!"}"#
 		);
 		assert_json!(
 			r#"
@@ -477,7 +434,7 @@
 					name: "Bob"
 				}
 			"#,
-			r#"{"name":"Bob","welcome":"Hello Bob!"}"#
+			r#"{"name": "Bob","welcome": "Hello Bob!"}"#
 		);
 	}
 
@@ -501,11 +458,11 @@
 
 	#[test]
 	fn object_locals() {
-		assert_json!(r#"{local a = 3, b: a}"#, r#"{"b":3}"#);
-		assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b":3}"#);
+		assert_json!(r#"{local a = 3, b: a}"#, r#"{"b": 3}"#);
+		assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b": 3}"#);
 		assert_json!(
 			r#"{local a = function (b) {[b]:4}, test: a("test")}"#,
-			r#"{"test":{"test":4}}"#
+			r#"{"test": {"test": 4}}"#
 		);
 	}
 
@@ -529,7 +486,7 @@
 	fn indirect_self() {
 		// `self` assigned to `me` was lost when being
 		// referenced from field
-		eval_stdlib!(
+		eval!(
 			r#"{
 				local me = self,
 				a: 3,
@@ -541,47 +498,47 @@
 	// We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly
 	#[test]
 	fn std_assert_ok() {
-		eval_stdlib!("std.assertEqual(4.5 << 2, 16)");
+		eval!("std.assertEqual(4.5 << 2, 16)");
 	}
 
 	#[test]
 	#[should_panic]
 	fn std_assert_failure() {
-		eval_stdlib!("std.assertEqual(4.5 << 2, 15)");
+		eval!("std.assertEqual(4.5 << 2, 15)");
 	}
 
 	#[test]
 	fn string_is_string() {
 		assert_eq!(
-			eval_stdlib!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),
+			eval!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),
 			Val::Bool(false)
 		);
 	}
 
 	#[test]
 	fn base64_works() {
-		assert_json_stdlib!(r#"std.base64("test")"#, r#""dGVzdA==""#);
+		assert_json!(r#"std.base64("test")"#, r#""dGVzdA==""#);
 	}
 
 	#[test]
 	fn utf8_chars() {
-		assert_json_stdlib!(
+		assert_json!(
 			r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,
-			r#"{"c":128526,"l":1}"#
+			r#"{"c": 128526,"l": 1}"#
 		)
 	}
 
 	#[test]
 	fn json() {
-		assert_json_stdlib!(
+		assert_json!(
 			r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,
-			r#""{\n"a": 3,\n"b": 4,\n"c": 6\n}""#
+			r#""{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}""#
 		);
 	}
 
 	#[test]
 	fn test() {
-		assert_json_stdlib!(
+		assert_json!(
 			r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,
 			"[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"
 		);
@@ -617,5 +574,4 @@
 		"#
 		);
 	}
-	*/
 }
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)
 	}
 }