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

difftreelog

perf(evaluator) remove inline attribute

Лач2020-06-25parent: #1a55b09.patch.diff
in: master

6 files changed

modifiedcrates/jsonnet-evaluator/Cargo.tomldiffbeforeafterboth
--- a/crates/jsonnet-evaluator/Cargo.toml
+++ b/crates/jsonnet-evaluator/Cargo.toml
@@ -10,6 +10,7 @@
 default = ["serialized-stdlib", "faster"]
 serialized-stdlib = ["serde", "bincode"]
 # Replace some standard library functions with faster implementations
+# Library works fine without this feature, but requires more memory and time for std function calls
 faster = []
 
 [dependencies]
modifiedcrates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
before · crates/jsonnet-evaluator/src/evaluate.rs
1use crate::{2	context_creator, create_error, future_wrapper, lazy_val, push, with_state, Context,3	ContextCreator, Error, FuncDesc, LazyBinding, LazyVal, ObjMember, ObjValue, Result, Val,4	ValType,5};6use closure::closure;7use jsonnet_parser::{8	el, Arg, ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, FieldMember,9	ForSpecData, IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc, UnaryOpType,10	Visibility,11};12use std::{13	collections::{BTreeMap, HashMap},14	rc::Rc,15};1617pub fn evaluate_binding(b: &BindSpec, context_creator: ContextCreator) -> (String, LazyBinding) {18	let b = b.clone();19	if let Some(params) = &b.params {20		let params = params.clone();21		(22			b.name.clone(),23			LazyBinding::Bindable(Rc::new(move |this, super_obj| {24				Ok(lazy_val!(25					closure!(clone b, clone params, clone context_creator, || Ok(evaluate_method(26						context_creator.0(this.clone(), super_obj.clone())?,27						params.clone(),28						b.value.clone(),29					)))30				))31			})),32		)33	} else {34		(35			b.name.clone(),36			LazyBinding::Bindable(Rc::new(move |this, super_obj| {37				Ok(lazy_val!(closure!(clone context_creator, clone b, ||38					push(b.value.clone(), "thunk".to_owned(), ||{39						evaluate(40							context_creator.0(this.clone(), super_obj.clone())?,41							&b.value42						)43					})44				)))45			})),46		)47	}48}4950pub fn evaluate_method(ctx: Context, params: ParamsDesc, body: LocExpr) -> Val {51	Val::Func(FuncDesc { ctx, params, body })52}5354pub fn evaluate_field_name(55	context: Context,56	field_name: &jsonnet_parser::FieldName,57) -> Result<Option<String>> {58	Ok(match field_name {59		jsonnet_parser::FieldName::Fixed(n) => Some(n.clone()),60		jsonnet_parser::FieldName::Dyn(expr) => {61			let value = evaluate(context, expr)?.unwrap_if_lazy()?;62			if matches!(value, Val::Null) {63				None64			} else {65				Some(value.try_cast_str("dynamic field name")?)66			}67		}68	})69}7071pub fn evaluate_unary_op(op: UnaryOpType, b: &Val) -> Result<Val> {72	Ok(match (op, b) {73		(o, Val::Lazy(l)) => evaluate_unary_op(o, &l.evaluate()?)?,74		(UnaryOpType::Not, Val::Bool(v)) => Val::Bool(!v),75		(UnaryOpType::Minus, Val::Num(n)) => Val::Num(-*n),76		(UnaryOpType::BitNot, Val::Num(n)) => Val::Num(!(*n as i32) as f64),77		(op, o) => panic!("unary op not implemented: {:?} {:?}", op, o),78	})79}8081pub(crate) fn evaluate_add_op(a: &Val, b: &Val) -> Result<Val> {82	Ok(match (a, b) {83		(Val::Str(v1), Val::Str(v2)) => Val::Str(v1.to_owned() + &v2),8485		// Can't use generic json serialization way, because it depends on number to string concatenation (std.jsonnet:890)86		(Val::Num(n), Val::Str(o)) => Val::Str(format!("{}{}", n, o)),87		(Val::Str(o), Val::Num(n)) => Val::Str(format!("{}{}", o, n)),8889		(Val::Str(s), o) => Val::Str(format!("{}{}", s, o.clone().into_json(0)?)),90		(o, Val::Str(s)) => Val::Str(format!("{}{}", o.clone().into_json(0)?, s)),9192		(Val::Obj(v1), Val::Obj(v2)) => Val::Obj(v2.with_super(v1.clone())),93		(Val::Arr(a), Val::Arr(b)) => Val::Arr([&a[..], &b[..]].concat()),94		(Val::Num(v1), Val::Num(v2)) => Val::Num(v1 + v2),95		_ => panic!("can't add: {:?} and {:?}", a, b),96	})97}9899pub fn evaluate_binary_op_special(100	context: Context,101	a: &LocExpr,102	op: BinaryOpType,103	b: &LocExpr,104) -> Result<Val> {105	Ok(106		match (evaluate(context.clone(), &a)?.unwrap_if_lazy()?, op, b) {107			(Val::Bool(true), BinaryOpType::Or, _o) => Val::Bool(true),108			(Val::Bool(false), BinaryOpType::And, _o) => Val::Bool(false),109			(a, op, eb) => {110				evaluate_binary_op_normal(&a, op, &evaluate(context, eb)?.unwrap_if_lazy()?)?111			}112		},113	)114}115116pub fn evaluate_binary_op_normal(a: &Val, op: BinaryOpType, b: &Val) -> Result<Val> {117	Ok(match (a, op, b) {118		(a, BinaryOpType::Add, b) => evaluate_add_op(a, b)?,119120		(Val::Str(v1), BinaryOpType::Mul, Val::Num(v2)) => Val::Str(v1.repeat(*v2 as usize)),121122		// Bool X Bool123		(Val::Bool(a), BinaryOpType::And, Val::Bool(b)) => Val::Bool(*a && *b),124		(Val::Bool(a), BinaryOpType::Or, Val::Bool(b)) => Val::Bool(*a || *b),125126		// Str X Str127		(Val::Str(v1), BinaryOpType::Lt, Val::Str(v2)) => Val::Bool(v1 < v2),128		(Val::Str(v1), BinaryOpType::Gt, Val::Str(v2)) => Val::Bool(v1 > v2),129		(Val::Str(v1), BinaryOpType::Lte, Val::Str(v2)) => Val::Bool(v1 <= v2),130		(Val::Str(v1), BinaryOpType::Gte, Val::Str(v2)) => Val::Bool(v1 >= v2),131132		// Num X Num133		(Val::Num(v1), BinaryOpType::Mul, Val::Num(v2)) => Val::Num(v1 * v2),134		(Val::Num(v1), BinaryOpType::Div, Val::Num(v2)) => {135			if *v2 <= f64::EPSILON {136				create_error(crate::Error::DivisionByZero)?137			}138			Val::Num(v1 / v2)139		}140141		(Val::Num(v1), BinaryOpType::Sub, Val::Num(v2)) => Val::Num(v1 - v2),142143		(Val::Num(v1), BinaryOpType::Lt, Val::Num(v2)) => Val::Bool(v1 < v2),144		(Val::Num(v1), BinaryOpType::Gt, Val::Num(v2)) => Val::Bool(v1 > v2),145		(Val::Num(v1), BinaryOpType::Lte, Val::Num(v2)) => Val::Bool(v1 <= v2),146		(Val::Num(v1), BinaryOpType::Gte, Val::Num(v2)) => Val::Bool(v1 >= v2),147148		(Val::Num(v1), BinaryOpType::BitAnd, Val::Num(v2)) => {149			Val::Num(((*v1 as i32) & (*v2 as i32)) as f64)150		}151		(Val::Num(v1), BinaryOpType::BitOr, Val::Num(v2)) => {152			Val::Num(((*v1 as i32) | (*v2 as i32)) as f64)153		}154		(Val::Num(v1), BinaryOpType::BitXor, Val::Num(v2)) => {155			Val::Num(((*v1 as i32) ^ (*v2 as i32)) as f64)156		}157		(Val::Num(v1), BinaryOpType::Lhs, Val::Num(v2)) => {158			Val::Num(((*v1 as i32) << (*v2 as i32)) as f64)159		}160		(Val::Num(v1), BinaryOpType::Rhs, Val::Num(v2)) => {161			Val::Num(((*v1 as i32) >> (*v2 as i32)) as f64)162		}163164		_ => panic!("no rules for binary operation: {:?} {:?} {:?}", a, op, b),165	})166}167168future_wrapper!(HashMap<String, LazyBinding>, FutureNewBindings);169future_wrapper!(ObjValue, FutureObjValue);170171#[inline(always)]172pub fn evaluate_comp<T>(173	context: Context,174	value: &impl Fn(Context) -> Result<T>,175	specs: &[CompSpec],176) -> Result<Option<Vec<T>>> {177	Ok(match specs.get(0) {178		None => Some(vec![value(context)?]),179		Some(CompSpec::IfSpec(IfSpecData(cond))) => {180			if evaluate(context.clone(), &cond)?.try_cast_bool("if spec")? {181				evaluate_comp(context, value, &specs[1..])?182			} else {183				None184			}185		}186		Some(CompSpec::ForSpec(ForSpecData(var, expr))) => {187			match evaluate(context.clone(), &expr)?.unwrap_if_lazy()? {188				Val::Arr(list) => {189					let mut out = Vec::new();190					for item in list {191						let item = item.clone().unwrap_if_lazy()?;192						out.push(evaluate_comp(193							context.with_var(var.clone(), item)?,194							value,195							&specs[1..],196						)?);197					}198					Some(out.into_iter().flatten().flatten().collect())199				}200				_ => panic!("for expression evaluated to non-iterable value"),201			}202		}203	})204}205206// TODO: Asserts207pub fn evaluate_object(context: Context, object: ObjBody) -> Result<ObjValue> {208	Ok(match object {209		ObjBody::MemberList(members) => {210			let new_bindings = FutureNewBindings::new();211			let future_this = FutureObjValue::new();212			let context_creator = context_creator!(213				closure!(clone context, clone new_bindings, |this: Option<ObjValue>, super_obj: Option<ObjValue>| {214					Ok(context.extend_unbound(215						new_bindings.clone().unwrap(),216						context.dollar().clone().or_else(||this.clone()),217						Some(this.unwrap()),218						super_obj219					)?)220				})221			);222			{223				let mut bindings: HashMap<String, LazyBinding> = HashMap::new();224				for (n, b) in members225					.iter()226					.filter_map(|m| match m {227						Member::BindStmt(b) => Some(b.clone()),228						_ => None,229					})230					.map(|b| evaluate_binding(&b, context_creator.clone()))231				{232					bindings.insert(n, b);233				}234				new_bindings.fill(bindings);235			}236237			let mut new_members = BTreeMap::new();238			for member in members.into_iter() {239				match member {240					Member::Field(FieldMember {241						name,242						plus,243						params: None,244						visibility,245						value,246					}) => {247						let name = evaluate_field_name(context.clone(), &name)?;248						if name.is_none() {249							continue;250						}251						let name = name.unwrap();252						new_members.insert(253							name.clone(),254							ObjMember {255								add: plus,256								visibility: visibility.clone(),257								invoke: LazyBinding::Bindable(Rc::new(258									closure!(clone name, clone value, clone context_creator, |this, super_obj| {259										Ok(LazyVal::new_resolved(push(value.clone(), "object ".to_owned()+&name+" field", ||{260											let context = context_creator.0(this, super_obj)?;261											evaluate(262												context,263												&value,264											)?.unwrap_if_lazy()265										})?))266									}),267								)),268							},269						);270					}271					Member::Field(FieldMember {272						name,273						params: Some(params),274						value,275						..276					}) => {277						let name = evaluate_field_name(context.clone(), &name)?;278						if name.is_none() {279							continue;280						}281						let name = name.unwrap();282						new_members.insert(283							name,284							ObjMember {285								add: false,286								visibility: Visibility::Hidden,287								invoke: LazyBinding::Bindable(Rc::new(288									closure!(clone value, clone context_creator, |this, super_obj| {289										// TODO: Assert290										Ok(LazyVal::new_resolved(evaluate_method(291											context_creator.0(this, super_obj)?,292											params.clone(),293											value.clone(),294										)))295									}),296								)),297							},298						);299					}300					Member::BindStmt(_) => {}301					Member::AssertStmt(_) => {}302				}303			}304			future_this.fill(ObjValue::new(None, Rc::new(new_members)))305		}306		ObjBody::ObjComp {307			pre_locals,308			key,309			value,310			post_locals,311			compspecs,312		} => {313			let future_this = FutureObjValue::new();314			let mut new_members = BTreeMap::new();315			for (k, v) in evaluate_comp(316				context.clone(),317				&|ctx| {318					let new_bindings = FutureNewBindings::new();319					let context_creator = context_creator!(320						closure!(clone context, clone new_bindings, |this: Option<ObjValue>, super_obj: Option<ObjValue>| {321							Ok(context.extend_unbound(322								new_bindings.clone().unwrap(),323								context.dollar().clone().or_else(||this.clone()),324								None,325								super_obj326							)?)327						})328					);329					let mut bindings: HashMap<String, LazyBinding> = HashMap::new();330					for (n, b) in pre_locals331						.iter()332						.chain(post_locals.iter())333						.map(|b| evaluate_binding(b, context_creator.clone()))334					{335						bindings.insert(n, b);336					}337					let bindings = new_bindings.fill(bindings);338					let ctx = ctx.extend_unbound(bindings, None, None, None)?;339					let key = evaluate(ctx.clone(), &key)?;340					let value = LazyBinding::Bindable(Rc::new(341						closure!(clone ctx, clone value, |this, _super_obj| {342							Ok(LazyVal::new_resolved(evaluate(ctx.extend(HashMap::new(), None, this, None)?, &value)?))343						}),344					));345346					Ok((key, value))347				},348				&compspecs,349			)?350			.unwrap()351			{352				match k {353					Val::Null => {}354					Val::Str(n) => {355						new_members.insert(356							n,357							ObjMember {358								add: false,359								visibility: Visibility::Normal,360								invoke: v,361							},362						);363					}364					v => create_error(Error::FieldMustBeStringGot(v.value_type()?))?,365				}366			}367368			future_this.fill(ObjValue::new(None, Rc::new(new_members)))369		}370	})371}372373#[inline(always)]374pub fn evaluate(context: Context, expr: &LocExpr) -> Result<Val> {375	use Expr::*;376	let locexpr = expr.clone();377	let LocExpr(expr, loc) = expr;378	Ok(match &**expr {379		Literal(LiteralType::This) => Val::Obj(380			context381				.this()382				.clone()383				.unwrap_or_else(|| panic!("this not found")),384		),385		Literal(LiteralType::Dollar) => Val::Obj(386			context387				.dollar()388				.clone()389				.unwrap_or_else(|| panic!("dollar not found")),390		),391		Literal(LiteralType::True) => Val::Bool(true),392		Literal(LiteralType::False) => Val::Bool(false),393		Literal(LiteralType::Null) => Val::Null,394		Parened(e) => evaluate(context, e)?,395		Str(v) => Val::Str(v.clone()),396		Num(v) => Val::Num(*v),397		BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, &v1, *o, &v2)?,398		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,399		Var(name) => push(locexpr, "var".to_owned(), || {400			Val::Lazy(context.binding(&name)?).unwrap_if_lazy()401		})?,402		Index(LocExpr(v, _), index) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {403			let name = evaluate(context.clone(), index)?.try_cast_str("object index")?;404			context405				.super_obj()406				.clone()407				.expect("no super found")408				.get_raw(&name, &context.this().clone().expect("no this found"))?409				.expect("value not found")410		}411		Index(value, index) => {412			match (413				evaluate(context.clone(), value)?.unwrap_if_lazy()?,414				evaluate(context, index)?,415			) {416				(Val::Obj(v), Val::Str(s)) => {417					if let Some(v) = v.get(&s)? {418						v.unwrap_if_lazy()?419					} else if let Some(Val::Str(n)) = v.get("__intristic_namespace__")? {420						Val::Intristic(n, s)421					} else {422						create_error(crate::Error::NoSuchField(s))?423					}424				}425				(Val::Obj(_), n) => create_error(crate::Error::ValueIndexMustBeTypeGot(426					ValType::Obj,427					ValType::Str,428					n.value_type()?,429				))?,430431				(Val::Arr(v), Val::Num(n)) => {432					if n.fract() > f64::EPSILON {433						create_error(crate::Error::FractionalIndex)?434					}435					v.get(n as usize)436						.unwrap_or_else(|| panic!("out of bounds"))437						.clone()438						.unwrap_if_lazy()?439				}440				(Val::Arr(_), Val::Str(n)) => {441					create_error(crate::Error::AttemptedIndexAnArrayWithString(n))?442				}443				(Val::Arr(_), n) => create_error(crate::Error::ValueIndexMustBeTypeGot(444					ValType::Arr,445					ValType::Num,446					n.value_type()?,447				))?,448449				(Val::Str(s), Val::Num(n)) => {450					Val::Str(s.chars().skip(n as usize).take(1).collect())451				}452				(Val::Str(_), n) => create_error(crate::Error::ValueIndexMustBeTypeGot(453					ValType::Str,454					ValType::Num,455					n.value_type()?,456				))?,457458				(v, _) => create_error(crate::Error::CantIndexInto(v.value_type()?))?,459			}460		}461		LocalExpr(bindings, returned) => {462			let mut new_bindings: HashMap<String, LazyBinding> = HashMap::new();463			let future_context = Context::new_future();464465			let context_creator = context_creator!(466				closure!(clone future_context, |_, _| Ok(future_context.clone().unwrap()))467			);468469			for (k, v) in bindings470				.iter()471				.map(|b| evaluate_binding(b, context_creator.clone()))472			{473				new_bindings.insert(k, v);474			}475476			let context = context477				.extend_unbound(new_bindings, None, None, None)?478				.into_future(future_context);479			evaluate(context, &returned.clone())?480		}481		Arr(items) => {482			let mut out = Vec::with_capacity(items.len());483			for item in items {484				out.push(Val::Lazy(lazy_val!(485					closure!(clone context, clone item, || {486						evaluate(context.clone(), &item)487					})488				)));489			}490			Val::Arr(out)491		}492		ArrComp(expr, compspecs) => Val::Arr(493			// First compspec should be forspec, so no "None" possible here494			evaluate_comp(context, &|ctx| evaluate(ctx, expr), compspecs)?.unwrap(),495		),496		Obj(body) => Val::Obj(evaluate_object(context, body.clone())?),497		ObjExtend(s, t) => evaluate_add_op(498			&evaluate(context.clone(), s)?,499			&Val::Obj(evaluate_object(context, t.clone())?),500		)?,501		Apply(value, args, tailstrict) => {502			let value = evaluate(context.clone(), value)?.unwrap_if_lazy()?;503			match value {504				Val::Intristic(ns, name) => match (&ns as &str, &name as &str) {505					// arr/string/function506					("std", "length") => {507						assert_eq!(args.len(), 1);508						let expr = &args.get(0).unwrap().1;509						match evaluate(context, expr)? {510							Val::Str(n) => Val::Num(n.chars().count() as f64),511							Val::Arr(i) => Val::Num(i.len() as f64),512							Val::Obj(o) => Val::Num(513								o.fields_visibility()514									.into_iter()515									.filter(|(_k, v)| *v)516									.count() as f64,517							),518							v => panic!("can't get length of {:?}", v),519						}520					}521					// any522					("std", "type") => {523						assert_eq!(args.len(), 1);524						let expr = &args.get(0).unwrap().1;525						Val::Str(evaluate(context, expr)?.value_type()?.name().to_owned())526					}527					// length, idx=>any528					("std", "makeArray") => {529						assert_eq!(args.len(), 2);530						if let (Val::Num(v), Val::Func(d)) = (531							evaluate(context.clone(), &args[0].1)?,532							evaluate(context, &args[1].1)?,533						) {534							assert!(v >= 0.0);535							let mut out = Vec::with_capacity(v as usize);536							for i in 0..v as usize {537								let call_ctx =538									Context::new().with_var("v".to_owned(), Val::Num(i as f64))?;539								out.push(d.evaluate(540									call_ctx,541									&ArgsDesc(vec![Arg(None, el!(Expr::Var("v".to_owned())))]),542									true,543								)?)544							}545							Val::Arr(out)546						} else {547							panic!("bad makeArray call");548						}549					}550					// string551					("std", "codepoint") => {552						assert_eq!(args.len(), 1);553						if let Val::Str(s) = evaluate(context, &args[0].1)? {554							assert!(555								s.chars().count() == 1,556								"std.codepoint should receive single char string"557							);558							Val::Num(s.chars().take(1).next().unwrap() as u32 as f64)559						} else {560							panic!("bad codepoint call");561						}562					}563					// object, includeHidden564					("std", "objectFieldsEx") => {565						assert_eq!(args.len(), 2);566						if let (Val::Obj(body), Val::Bool(include_hidden)) = (567							evaluate(context.clone(), &args[0].1)?,568							evaluate(context, &args[1].1)?,569						) {570							Val::Arr(571								body.fields_visibility()572									.into_iter()573									.filter(|(_k, v)| *v || include_hidden)574									.map(|(k, _v)| Val::Str(k))575									.collect(),576							)577						} else {578							panic!("bad objectFieldsEx call");579						}580					}581					// object, field, includeHidden582					("std", "objectHasEx") => {583						assert_eq!(args.len(), 3);584						if let (Val::Obj(body), Val::Str(name), Val::Bool(include_hidden)) = (585							evaluate(context.clone(), &args[0].1)?,586							evaluate(context.clone(), &args[1].1)?,587							evaluate(context, &args[2].1)?,588						) {589							Val::Bool(590								body.fields_visibility()591									.into_iter()592									.filter(|(_k, v)| *v || include_hidden)593									.any(|(k, _v)| k == name),594							)595						} else {596							panic!("bad objectHasEx call");597						}598					}599					("std", "primitiveEquals") => {600						assert_eq!(args.len(), 2);601						let (a, b) = (602							evaluate(context.clone(), &args[0].1)?,603							evaluate(context, &args[1].1)?,604						);605						Val::Bool(a == b)606					}607					("std", "modulo") => {608						assert_eq!(args.len(), 2);609						if let (Val::Num(a), Val::Num(b)) = (610							evaluate(context.clone(), &args[0].1)?,611							evaluate(context, &args[1].1)?,612						) {613							Val::Num(a % b)614						} else {615							panic!("bad modulo call");616						}617					}618					("std", "floor") => {619						assert_eq!(args.len(), 1);620						if let Val::Num(a) = evaluate(context, &args[0].1)? {621							Val::Num(a.floor())622						} else {623							panic!("bad floor call");624						}625					}626					("std", "trace") => {627						assert_eq!(args.len(), 2);628						if let (Val::Str(a), b) = (629							evaluate(context.clone(), &args[0].1)?,630							evaluate(context, &args[1].1)?,631						) {632							// TODO: Line numbers as in original jsonnet633							println!("TRACE: {}", a);634							b635						} else {636							panic!("bad trace call");637						}638					}639					("std", "pow") => {640						assert_eq!(args.len(), 2);641						if let (Val::Num(a), Val::Num(b)) = (642							evaluate(context.clone(), &args[0].1)?,643							evaluate(context, &args[1].1)?,644						) {645							Val::Num(a.powf(b))646						} else {647							panic!("bad pow call");648						}649					}650					("std", "extVar") => {651						assert_eq!(args.len(), 1);652						if let Val::Str(a) = evaluate(context, &args[0].1)? {653							with_state(|s| s.0.ext_vars.borrow().get(&a).cloned()).ok_or_else(654								|| {655									create_error::<()>(crate::Error::UndefinedExternalVariable(a))656										.err()657										.unwrap()658								},659							)?660						} else {661							panic!("bad extVar call");662						}663					}664					("std", "filter") => {665						assert_eq!(args.len(), 2);666						if let (Val::Func(predicate), Val::Arr(arr)) = (667							evaluate(context.clone(), &args[0].1)?,668							evaluate(context.clone(), &args[1].1)?,669						) {670							Val::Arr(671								arr.into_iter()672									.filter(|e| {673										predicate674											.evaluate_values(context.clone(), &[e.clone()])675											.unwrap()676											.try_cast_bool("filter predicate")677											.unwrap()678									})679									.collect(),680							)681						} else {682							panic!("bad filter call");683						}684					}685					// faster686					("std", "join") => {687						assert_eq!(args.len(), 2);688						let joiner = evaluate(context.clone(), &args[0].1)?.unwrap_if_lazy()?;689						let items = evaluate(context, &args[1].1)?.unwrap_if_lazy()?;690						match (joiner, items) {691							(Val::Arr(joiner_items), Val::Arr(items)) => {692								// TODO: Minimal size should be known693								let mut out = Vec::new();694695								let mut first = true;696								for item in items {697									if let Val::Arr(items) = item.unwrap_if_lazy()? {698										if !first {699											out.extend(joiner_items.iter().cloned());700										}701										first = false;702										out.extend(items);703									} else {704										panic!("all array items should be arrays")705									}706								}707708								Val::Arr(out)709							}710							(Val::Str(joiner), Val::Arr(items)) => {711								let mut out = String::new();712713								let mut first = true;714								for item in items {715									if let Val::Str(item) = item.unwrap_if_lazy()? {716										if !first {717											out += &joiner;718										}719										first = false;720										out += &item;721									} else {722										panic!("all array items should be strings")723									}724								}725726								Val::Str(out)727							}728							(joiner, items) => panic!("bad join call: {:?} {:?}", joiner, items),729						}730					}731					(ns, name) => panic!("Intristic not found: {}.{}", ns, name),732				},733				Val::Func(f) => {734					let body = #[inline(always)]735					|| f.evaluate(context, args, *tailstrict);736					if *tailstrict {737						body()?738					} else {739						push(locexpr, "function call".to_owned(), body)?740					}741				}742				_ => panic!("{:?} is not a function", value),743			}744		}745		Function(params, body) => evaluate_method(context, params.clone(), body.clone()),746		AssertExpr(AssertStmt(value, msg), returned) => {747			let assertion_result = push(value.clone(), "assertion condition".to_owned(), || {748				evaluate(context.clone(), &value)?749					.try_cast_bool("assertion condition should be boolean")750			})?;751			if assertion_result {752				push(753					returned.clone(),754					"assert 'return' branch".to_owned(),755					|| evaluate(context, returned),756				)?757			} else if let Some(msg) = msg {758				panic!(759					"assertion failed ({:?}): {}",760					value,761					evaluate(context, msg)?.try_cast_str("assertion message should be string")?762				);763			} else {764				panic!("assertion failed ({:?}): no message", value);765			}766		}767		Error(e) => create_error(crate::Error::RuntimeError(768			evaluate(context, e)?.try_cast_str("error text should be string")?,769		))?,770		IfElse {771			cond,772			cond_then,773			cond_else,774		} => {775			if evaluate(context.clone(), &cond.0)?776				.try_cast_bool("if condition should be boolean")?777			{778				evaluate(context, cond_then)?779			} else {780				match cond_else {781					Some(v) => evaluate(context, v)?,782					None => Val::Null,783				}784			}785		}786		Import(path) => {787			let mut import_location = loc788				.clone()789				.expect("imports can't be used without loc_data")790				.0791				.clone();792			import_location.pop();793			with_state(|s| s.import_file(&import_location, path))?794		}795		ImportStr(path) => {796			let mut import_location = loc797				.clone()798				.expect("imports can't be used without loc_data")799				.0800				.clone();801			import_location.pop();802			Val::Str(with_state(|s| s.import_file_str(&import_location, path))?)803		}804		Literal(LiteralType::Super) => return create_error(crate::error::Error::StandaloneSuper),805	})806}
after · crates/jsonnet-evaluator/src/evaluate.rs
1use crate::{2	context_creator, create_error, future_wrapper, lazy_val, push, with_state, Context,3	ContextCreator, Error, FuncDesc, LazyBinding, LazyVal, ObjMember, ObjValue, Result, Val,4	ValType,5};6use closure::closure;7use jsonnet_parser::{8	el, Arg, ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, FieldMember,9	ForSpecData, IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc, UnaryOpType,10	Visibility,11};12use std::{13	collections::{BTreeMap, HashMap},14	rc::Rc,15};1617pub fn evaluate_binding(b: &BindSpec, context_creator: ContextCreator) -> (String, LazyBinding) {18	let b = b.clone();19	if let Some(params) = &b.params {20		let params = params.clone();21		(22			b.name.clone(),23			LazyBinding::Bindable(Rc::new(move |this, super_obj| {24				Ok(lazy_val!(25					closure!(clone b, clone params, clone context_creator, || Ok(evaluate_method(26						context_creator.0(this.clone(), super_obj.clone())?,27						params.clone(),28						b.value.clone(),29					)))30				))31			})),32		)33	} else {34		(35			b.name.clone(),36			LazyBinding::Bindable(Rc::new(move |this, super_obj| {37				Ok(lazy_val!(closure!(clone context_creator, clone b, ||38					push(b.value.clone(), "thunk".to_owned(), ||{39						evaluate(40							context_creator.0(this.clone(), super_obj.clone())?,41							&b.value42						)43					})44				)))45			})),46		)47	}48}4950pub fn evaluate_method(ctx: Context, params: ParamsDesc, body: LocExpr) -> Val {51	Val::Func(FuncDesc { ctx, params, body })52}5354pub fn evaluate_field_name(55	context: Context,56	field_name: &jsonnet_parser::FieldName,57) -> Result<Option<String>> {58	Ok(match field_name {59		jsonnet_parser::FieldName::Fixed(n) => Some(n.clone()),60		jsonnet_parser::FieldName::Dyn(expr) => {61			let value = evaluate(context, expr)?.unwrap_if_lazy()?;62			if matches!(value, Val::Null) {63				None64			} else {65				Some(value.try_cast_str("dynamic field name")?)66			}67		}68	})69}7071pub fn evaluate_unary_op(op: UnaryOpType, b: &Val) -> Result<Val> {72	Ok(match (op, b) {73		(o, Val::Lazy(l)) => evaluate_unary_op(o, &l.evaluate()?)?,74		(UnaryOpType::Not, Val::Bool(v)) => Val::Bool(!v),75		(UnaryOpType::Minus, Val::Num(n)) => Val::Num(-*n),76		(UnaryOpType::BitNot, Val::Num(n)) => Val::Num(!(*n as i32) as f64),77		(op, o) => panic!("unary op not implemented: {:?} {:?}", op, o),78	})79}8081pub(crate) fn evaluate_add_op(a: &Val, b: &Val) -> Result<Val> {82	Ok(match (a, b) {83		(Val::Str(v1), Val::Str(v2)) => Val::Str(v1.to_owned() + &v2),8485		// Can't use generic json serialization way, because it depends on number to string concatenation (std.jsonnet:890)86		(Val::Num(n), Val::Str(o)) => Val::Str(format!("{}{}", n, o)),87		(Val::Str(o), Val::Num(n)) => Val::Str(format!("{}{}", o, n)),8889		(Val::Str(s), o) => Val::Str(format!("{}{}", s, o.clone().into_json(0)?)),90		(o, Val::Str(s)) => Val::Str(format!("{}{}", o.clone().into_json(0)?, s)),9192		(Val::Obj(v1), Val::Obj(v2)) => Val::Obj(v2.with_super(v1.clone())),93		(Val::Arr(a), Val::Arr(b)) => Val::Arr([&a[..], &b[..]].concat()),94		(Val::Num(v1), Val::Num(v2)) => Val::Num(v1 + v2),95		_ => panic!("can't add: {:?} and {:?}", a, b),96	})97}9899pub fn evaluate_binary_op_special(100	context: Context,101	a: &LocExpr,102	op: BinaryOpType,103	b: &LocExpr,104) -> Result<Val> {105	Ok(106		match (evaluate(context.clone(), &a)?.unwrap_if_lazy()?, op, b) {107			(Val::Bool(true), BinaryOpType::Or, _o) => Val::Bool(true),108			(Val::Bool(false), BinaryOpType::And, _o) => Val::Bool(false),109			(a, op, eb) => {110				evaluate_binary_op_normal(&a, op, &evaluate(context, eb)?.unwrap_if_lazy()?)?111			}112		},113	)114}115116pub fn evaluate_binary_op_normal(a: &Val, op: BinaryOpType, b: &Val) -> Result<Val> {117	Ok(match (a, op, b) {118		(a, BinaryOpType::Add, b) => evaluate_add_op(a, b)?,119120		(Val::Str(v1), BinaryOpType::Mul, Val::Num(v2)) => Val::Str(v1.repeat(*v2 as usize)),121122		// Bool X Bool123		(Val::Bool(a), BinaryOpType::And, Val::Bool(b)) => Val::Bool(*a && *b),124		(Val::Bool(a), BinaryOpType::Or, Val::Bool(b)) => Val::Bool(*a || *b),125126		// Str X Str127		(Val::Str(v1), BinaryOpType::Lt, Val::Str(v2)) => Val::Bool(v1 < v2),128		(Val::Str(v1), BinaryOpType::Gt, Val::Str(v2)) => Val::Bool(v1 > v2),129		(Val::Str(v1), BinaryOpType::Lte, Val::Str(v2)) => Val::Bool(v1 <= v2),130		(Val::Str(v1), BinaryOpType::Gte, Val::Str(v2)) => Val::Bool(v1 >= v2),131132		// Num X Num133		(Val::Num(v1), BinaryOpType::Mul, Val::Num(v2)) => Val::Num(v1 * v2),134		(Val::Num(v1), BinaryOpType::Div, Val::Num(v2)) => {135			if *v2 <= f64::EPSILON {136				create_error(crate::Error::DivisionByZero)?137			}138			Val::Num(v1 / v2)139		}140141		(Val::Num(v1), BinaryOpType::Sub, Val::Num(v2)) => Val::Num(v1 - v2),142143		(Val::Num(v1), BinaryOpType::Lt, Val::Num(v2)) => Val::Bool(v1 < v2),144		(Val::Num(v1), BinaryOpType::Gt, Val::Num(v2)) => Val::Bool(v1 > v2),145		(Val::Num(v1), BinaryOpType::Lte, Val::Num(v2)) => Val::Bool(v1 <= v2),146		(Val::Num(v1), BinaryOpType::Gte, Val::Num(v2)) => Val::Bool(v1 >= v2),147148		(Val::Num(v1), BinaryOpType::BitAnd, Val::Num(v2)) => {149			Val::Num(((*v1 as i32) & (*v2 as i32)) as f64)150		}151		(Val::Num(v1), BinaryOpType::BitOr, Val::Num(v2)) => {152			Val::Num(((*v1 as i32) | (*v2 as i32)) as f64)153		}154		(Val::Num(v1), BinaryOpType::BitXor, Val::Num(v2)) => {155			Val::Num(((*v1 as i32) ^ (*v2 as i32)) as f64)156		}157		(Val::Num(v1), BinaryOpType::Lhs, Val::Num(v2)) => {158			Val::Num(((*v1 as i32) << (*v2 as i32)) as f64)159		}160		(Val::Num(v1), BinaryOpType::Rhs, Val::Num(v2)) => {161			Val::Num(((*v1 as i32) >> (*v2 as i32)) as f64)162		}163164		_ => panic!("no rules for binary operation: {:?} {:?} {:?}", a, op, b),165	})166}167168future_wrapper!(HashMap<String, LazyBinding>, FutureNewBindings);169future_wrapper!(ObjValue, FutureObjValue);170171pub fn evaluate_comp<T>(172	context: Context,173	value: &impl Fn(Context) -> Result<T>,174	specs: &[CompSpec],175) -> Result<Option<Vec<T>>> {176	Ok(match specs.get(0) {177		None => Some(vec![value(context)?]),178		Some(CompSpec::IfSpec(IfSpecData(cond))) => {179			if evaluate(context.clone(), &cond)?.try_cast_bool("if spec")? {180				evaluate_comp(context, value, &specs[1..])?181			} else {182				None183			}184		}185		Some(CompSpec::ForSpec(ForSpecData(var, expr))) => {186			match evaluate(context.clone(), &expr)?.unwrap_if_lazy()? {187				Val::Arr(list) => {188					let mut out = Vec::new();189					for item in list {190						let item = item.clone().unwrap_if_lazy()?;191						out.push(evaluate_comp(192							context.with_var(var.clone(), item)?,193							value,194							&specs[1..],195						)?);196					}197					Some(out.into_iter().flatten().flatten().collect())198				}199				_ => panic!("for expression evaluated to non-iterable value"),200			}201		}202	})203}204205// TODO: Asserts206pub fn evaluate_object(context: Context, object: ObjBody) -> Result<ObjValue> {207	Ok(match object {208		ObjBody::MemberList(members) => {209			let new_bindings = FutureNewBindings::new();210			let future_this = FutureObjValue::new();211			let context_creator = context_creator!(212				closure!(clone context, clone new_bindings, |this: Option<ObjValue>, super_obj: Option<ObjValue>| {213					Ok(context.extend_unbound(214						new_bindings.clone().unwrap(),215						context.dollar().clone().or_else(||this.clone()),216						Some(this.unwrap()),217						super_obj218					)?)219				})220			);221			{222				let mut bindings: HashMap<String, LazyBinding> = HashMap::new();223				for (n, b) in members224					.iter()225					.filter_map(|m| match m {226						Member::BindStmt(b) => Some(b.clone()),227						_ => None,228					})229					.map(|b| evaluate_binding(&b, context_creator.clone()))230				{231					bindings.insert(n, b);232				}233				new_bindings.fill(bindings);234			}235236			let mut new_members = BTreeMap::new();237			for member in members.into_iter() {238				match member {239					Member::Field(FieldMember {240						name,241						plus,242						params: None,243						visibility,244						value,245					}) => {246						let name = evaluate_field_name(context.clone(), &name)?;247						if name.is_none() {248							continue;249						}250						let name = name.unwrap();251						new_members.insert(252							name.clone(),253							ObjMember {254								add: plus,255								visibility: visibility.clone(),256								invoke: LazyBinding::Bindable(Rc::new(257									closure!(clone name, clone value, clone context_creator, |this, super_obj| {258										Ok(LazyVal::new_resolved(push(value.clone(), "object ".to_owned()+&name+" field", ||{259											let context = context_creator.0(this, super_obj)?;260											evaluate(261												context,262												&value,263											)?.unwrap_if_lazy()264										})?))265									}),266								)),267							},268						);269					}270					Member::Field(FieldMember {271						name,272						params: Some(params),273						value,274						..275					}) => {276						let name = evaluate_field_name(context.clone(), &name)?;277						if name.is_none() {278							continue;279						}280						let name = name.unwrap();281						new_members.insert(282							name,283							ObjMember {284								add: false,285								visibility: Visibility::Hidden,286								invoke: LazyBinding::Bindable(Rc::new(287									closure!(clone value, clone context_creator, |this, super_obj| {288										// TODO: Assert289										Ok(LazyVal::new_resolved(evaluate_method(290											context_creator.0(this, super_obj)?,291											params.clone(),292											value.clone(),293										)))294									}),295								)),296							},297						);298					}299					Member::BindStmt(_) => {}300					Member::AssertStmt(_) => {}301				}302			}303			future_this.fill(ObjValue::new(None, Rc::new(new_members)))304		}305		ObjBody::ObjComp {306			pre_locals,307			key,308			value,309			post_locals,310			compspecs,311		} => {312			let future_this = FutureObjValue::new();313			let mut new_members = BTreeMap::new();314			for (k, v) in evaluate_comp(315				context.clone(),316				&|ctx| {317					let new_bindings = FutureNewBindings::new();318					let context_creator = context_creator!(319						closure!(clone context, clone new_bindings, |this: Option<ObjValue>, super_obj: Option<ObjValue>| {320							Ok(context.extend_unbound(321								new_bindings.clone().unwrap(),322								context.dollar().clone().or_else(||this.clone()),323								None,324								super_obj325							)?)326						})327					);328					let mut bindings: HashMap<String, LazyBinding> = HashMap::new();329					for (n, b) in pre_locals330						.iter()331						.chain(post_locals.iter())332						.map(|b| evaluate_binding(b, context_creator.clone()))333					{334						bindings.insert(n, b);335					}336					let bindings = new_bindings.fill(bindings);337					let ctx = ctx.extend_unbound(bindings, None, None, None)?;338					let key = evaluate(ctx.clone(), &key)?;339					let value = LazyBinding::Bindable(Rc::new(340						closure!(clone ctx, clone value, |this, _super_obj| {341							Ok(LazyVal::new_resolved(evaluate(ctx.extend(HashMap::new(), None, this, None)?, &value)?))342						}),343					));344345					Ok((key, value))346				},347				&compspecs,348			)?349			.unwrap()350			{351				match k {352					Val::Null => {}353					Val::Str(n) => {354						new_members.insert(355							n,356							ObjMember {357								add: false,358								visibility: Visibility::Normal,359								invoke: v,360							},361						);362					}363					v => create_error(Error::FieldMustBeStringGot(v.value_type()?))?,364				}365			}366367			future_this.fill(ObjValue::new(None, Rc::new(new_members)))368		}369	})370}371372pub fn evaluate(context: Context, expr: &LocExpr) -> Result<Val> {373	use Expr::*;374	let locexpr = expr.clone();375	let LocExpr(expr, loc) = expr;376	Ok(match &**expr {377		Literal(LiteralType::This) => Val::Obj(378			context379				.this()380				.clone()381				.unwrap_or_else(|| panic!("this not found")),382		),383		Literal(LiteralType::Dollar) => Val::Obj(384			context385				.dollar()386				.clone()387				.unwrap_or_else(|| panic!("dollar not found")),388		),389		Literal(LiteralType::True) => Val::Bool(true),390		Literal(LiteralType::False) => Val::Bool(false),391		Literal(LiteralType::Null) => Val::Null,392		Parened(e) => evaluate(context, e)?,393		Str(v) => Val::Str(v.clone()),394		Num(v) => Val::Num(*v),395		BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, &v1, *o, &v2)?,396		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,397		Var(name) => push(locexpr, "var".to_owned(), || {398			Val::Lazy(context.binding(&name)?).unwrap_if_lazy()399		})?,400		Index(LocExpr(v, _), index) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {401			let name = evaluate(context.clone(), index)?.try_cast_str("object index")?;402			context403				.super_obj()404				.clone()405				.expect("no super found")406				.get_raw(&name, &context.this().clone().expect("no this found"))?407				.expect("value not found")408		}409		Index(value, index) => {410			match (411				evaluate(context.clone(), value)?.unwrap_if_lazy()?,412				evaluate(context, index)?,413			) {414				(Val::Obj(v), Val::Str(s)) => {415					if let Some(v) = v.get(&s)? {416						v.unwrap_if_lazy()?417					} else if let Some(Val::Str(n)) = v.get("__intristic_namespace__")? {418						Val::Intristic(n, s)419					} else {420						create_error(crate::Error::NoSuchField(s))?421					}422				}423				(Val::Obj(_), n) => create_error(crate::Error::ValueIndexMustBeTypeGot(424					ValType::Obj,425					ValType::Str,426					n.value_type()?,427				))?,428429				(Val::Arr(v), Val::Num(n)) => {430					if n.fract() > f64::EPSILON {431						create_error(crate::Error::FractionalIndex)?432					}433					v.get(n as usize)434						.unwrap_or_else(|| panic!("out of bounds"))435						.clone()436						.unwrap_if_lazy()?437				}438				(Val::Arr(_), Val::Str(n)) => {439					create_error(crate::Error::AttemptedIndexAnArrayWithString(n))?440				}441				(Val::Arr(_), n) => create_error(crate::Error::ValueIndexMustBeTypeGot(442					ValType::Arr,443					ValType::Num,444					n.value_type()?,445				))?,446447				(Val::Str(s), Val::Num(n)) => {448					Val::Str(s.chars().skip(n as usize).take(1).collect())449				}450				(Val::Str(_), n) => create_error(crate::Error::ValueIndexMustBeTypeGot(451					ValType::Str,452					ValType::Num,453					n.value_type()?,454				))?,455456				(v, _) => create_error(crate::Error::CantIndexInto(v.value_type()?))?,457			}458		}459		LocalExpr(bindings, returned) => {460			let mut new_bindings: HashMap<String, LazyBinding> = HashMap::new();461			let future_context = Context::new_future();462463			let context_creator = context_creator!(464				closure!(clone future_context, |_, _| Ok(future_context.clone().unwrap()))465			);466467			for (k, v) in bindings468				.iter()469				.map(|b| evaluate_binding(b, context_creator.clone()))470			{471				new_bindings.insert(k, v);472			}473474			let context = context475				.extend_unbound(new_bindings, None, None, None)?476				.into_future(future_context);477			evaluate(context, &returned.clone())?478		}479		Arr(items) => {480			let mut out = Vec::with_capacity(items.len());481			for item in items {482				out.push(Val::Lazy(lazy_val!(483					closure!(clone context, clone item, || {484						evaluate(context.clone(), &item)485					})486				)));487			}488			Val::Arr(out)489		}490		ArrComp(expr, compspecs) => Val::Arr(491			// First compspec should be forspec, so no "None" possible here492			evaluate_comp(context, &|ctx| evaluate(ctx, expr), compspecs)?.unwrap(),493		),494		Obj(body) => Val::Obj(evaluate_object(context, body.clone())?),495		ObjExtend(s, t) => evaluate_add_op(496			&evaluate(context.clone(), s)?,497			&Val::Obj(evaluate_object(context, t.clone())?),498		)?,499		Apply(value, args, tailstrict) => {500			let value = evaluate(context.clone(), value)?.unwrap_if_lazy()?;501			match value {502				Val::Intristic(ns, name) => match (&ns as &str, &name as &str) {503					// arr/string/function504					("std", "length") => {505						assert_eq!(args.len(), 1);506						let expr = &args.get(0).unwrap().1;507						match evaluate(context, expr)? {508							Val::Str(n) => Val::Num(n.chars().count() as f64),509							Val::Arr(i) => Val::Num(i.len() as f64),510							Val::Obj(o) => Val::Num(511								o.fields_visibility()512									.into_iter()513									.filter(|(_k, v)| *v)514									.count() as f64,515							),516							v => panic!("can't get length of {:?}", v),517						}518					}519					// any520					("std", "type") => {521						assert_eq!(args.len(), 1);522						let expr = &args.get(0).unwrap().1;523						Val::Str(evaluate(context, expr)?.value_type()?.name().to_owned())524					}525					// length, idx=>any526					("std", "makeArray") => {527						assert_eq!(args.len(), 2);528						if let (Val::Num(v), Val::Func(d)) = (529							evaluate(context.clone(), &args[0].1)?,530							evaluate(context, &args[1].1)?,531						) {532							assert!(v >= 0.0);533							let mut out = Vec::with_capacity(v as usize);534							for i in 0..v as usize {535								let call_ctx =536									Context::new().with_var("v".to_owned(), Val::Num(i as f64))?;537								out.push(d.evaluate(538									call_ctx,539									&ArgsDesc(vec![Arg(None, el!(Expr::Var("v".to_owned())))]),540									true,541								)?)542							}543							Val::Arr(out)544						} else {545							panic!("bad makeArray call");546						}547					}548					// string549					("std", "codepoint") => {550						assert_eq!(args.len(), 1);551						if let Val::Str(s) = evaluate(context, &args[0].1)? {552							assert!(553								s.chars().count() == 1,554								"std.codepoint should receive single char string"555							);556							Val::Num(s.chars().take(1).next().unwrap() as u32 as f64)557						} else {558							panic!("bad codepoint call");559						}560					}561					// object, includeHidden562					("std", "objectFieldsEx") => {563						assert_eq!(args.len(), 2);564						if let (Val::Obj(body), Val::Bool(include_hidden)) = (565							evaluate(context.clone(), &args[0].1)?,566							evaluate(context, &args[1].1)?,567						) {568							Val::Arr(569								body.fields_visibility()570									.into_iter()571									.filter(|(_k, v)| *v || include_hidden)572									.map(|(k, _v)| Val::Str(k))573									.collect(),574							)575						} else {576							panic!("bad objectFieldsEx call");577						}578					}579					// object, field, includeHidden580					("std", "objectHasEx") => {581						assert_eq!(args.len(), 3);582						if let (Val::Obj(body), Val::Str(name), Val::Bool(include_hidden)) = (583							evaluate(context.clone(), &args[0].1)?,584							evaluate(context.clone(), &args[1].1)?,585							evaluate(context, &args[2].1)?,586						) {587							Val::Bool(588								body.fields_visibility()589									.into_iter()590									.filter(|(_k, v)| *v || include_hidden)591									.any(|(k, _v)| k == name),592							)593						} else {594							panic!("bad objectHasEx call");595						}596					}597					("std", "primitiveEquals") => {598						assert_eq!(args.len(), 2);599						let (a, b) = (600							evaluate(context.clone(), &args[0].1)?,601							evaluate(context, &args[1].1)?,602						);603						Val::Bool(a == b)604					}605					("std", "modulo") => {606						assert_eq!(args.len(), 2);607						if let (Val::Num(a), Val::Num(b)) = (608							evaluate(context.clone(), &args[0].1)?,609							evaluate(context, &args[1].1)?,610						) {611							Val::Num(a % b)612						} else {613							panic!("bad modulo call");614						}615					}616					("std", "floor") => {617						assert_eq!(args.len(), 1);618						if let Val::Num(a) = evaluate(context, &args[0].1)? {619							Val::Num(a.floor())620						} else {621							panic!("bad floor call");622						}623					}624					("std", "trace") => {625						assert_eq!(args.len(), 2);626						if let (Val::Str(a), b) = (627							evaluate(context.clone(), &args[0].1)?,628							evaluate(context, &args[1].1)?,629						) {630							// TODO: Line numbers as in original jsonnet631							println!("TRACE: {}", a);632							b633						} else {634							panic!("bad trace call");635						}636					}637					("std", "pow") => {638						assert_eq!(args.len(), 2);639						if let (Val::Num(a), Val::Num(b)) = (640							evaluate(context.clone(), &args[0].1)?,641							evaluate(context, &args[1].1)?,642						) {643							Val::Num(a.powf(b))644						} else {645							panic!("bad pow call");646						}647					}648					("std", "extVar") => {649						assert_eq!(args.len(), 1);650						if let Val::Str(a) = evaluate(context, &args[0].1)? {651							with_state(|s| s.0.ext_vars.borrow().get(&a).cloned()).ok_or_else(652								|| {653									create_error::<()>(crate::Error::UndefinedExternalVariable(a))654										.err()655										.unwrap()656								},657							)?658						} else {659							panic!("bad extVar call");660						}661					}662					("std", "filter") => {663						assert_eq!(args.len(), 2);664						if let (Val::Func(predicate), Val::Arr(arr)) = (665							evaluate(context.clone(), &args[0].1)?,666							evaluate(context.clone(), &args[1].1)?,667						) {668							Val::Arr(669								arr.into_iter()670									.filter(|e| {671										predicate672											.evaluate_values(context.clone(), &[e.clone()])673											.unwrap()674											.try_cast_bool("filter predicate")675											.unwrap()676									})677									.collect(),678							)679						} else {680							panic!("bad filter call");681						}682					}683					// faster684					("std", "join") => {685						assert_eq!(args.len(), 2);686						let joiner = evaluate(context.clone(), &args[0].1)?.unwrap_if_lazy()?;687						let items = evaluate(context, &args[1].1)?.unwrap_if_lazy()?;688						match (joiner, items) {689							(Val::Arr(joiner_items), Val::Arr(items)) => {690								// TODO: Minimal size should be known691								let mut out = Vec::new();692693								let mut first = true;694								for item in items {695									if let Val::Arr(items) = item.unwrap_if_lazy()? {696										if !first {697											out.extend(joiner_items.iter().cloned());698										}699										first = false;700										out.extend(items);701									} else {702										panic!("all array items should be arrays")703									}704								}705706								Val::Arr(out)707							}708							(Val::Str(joiner), Val::Arr(items)) => {709								let mut out = String::new();710711								let mut first = true;712								for item in items {713									if let Val::Str(item) = item.unwrap_if_lazy()? {714										if !first {715											out += &joiner;716										}717										first = false;718										out += &item;719									} else {720										panic!("all array items should be strings")721									}722								}723724								Val::Str(out)725							}726							(joiner, items) => panic!("bad join call: {:?} {:?}", joiner, items),727						}728					}729					(ns, name) => panic!("Intristic not found: {}.{}", ns, name),730				},731				Val::Func(f) => {732					let body = || f.evaluate(context, args, *tailstrict);733					if *tailstrict {734						body()?735					} else {736						push(locexpr, "function call".to_owned(), body)?737					}738				}739				_ => panic!("{:?} is not a function", value),740			}741		}742		Function(params, body) => evaluate_method(context, params.clone(), body.clone()),743		AssertExpr(AssertStmt(value, msg), returned) => {744			let assertion_result = push(value.clone(), "assertion condition".to_owned(), || {745				evaluate(context.clone(), &value)?746					.try_cast_bool("assertion condition should be boolean")747			})?;748			if assertion_result {749				push(750					returned.clone(),751					"assert 'return' branch".to_owned(),752					|| evaluate(context, returned),753				)?754			} else if let Some(msg) = msg {755				panic!(756					"assertion failed ({:?}): {}",757					value,758					evaluate(context, msg)?.try_cast_str("assertion message should be string")?759				);760			} else {761				panic!("assertion failed ({:?}): no message", value);762			}763		}764		Error(e) => create_error(crate::Error::RuntimeError(765			evaluate(context, e)?.try_cast_str("error text should be string")?,766		))?,767		IfElse {768			cond,769			cond_then,770			cond_else,771		} => {772			if evaluate(context.clone(), &cond.0)?773				.try_cast_bool("if condition should be boolean")?774			{775				evaluate(context, cond_then)?776			} else {777				match cond_else {778					Some(v) => evaluate(context, v)?,779					None => Val::Null,780				}781			}782		}783		Import(path) => {784			let mut import_location = loc785				.clone()786				.expect("imports can't be used without loc_data")787				.0788				.clone();789			import_location.pop();790			with_state(|s| s.import_file(&import_location, path))?791		}792		ImportStr(path) => {793			let mut import_location = loc794				.clone()795				.expect("imports can't be used without loc_data")796				.0797				.clone();798			import_location.pop();799			Val::Str(with_state(|s| s.import_file_str(&import_location, path))?)800		}801		Literal(LiteralType::Super) => return create_error(crate::error::Error::StandaloneSuper),802	})803}
modifiedcrates/jsonnet-evaluator/src/function.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/function.rs
+++ b/crates/jsonnet-evaluator/src/function.rs
@@ -24,7 +24,6 @@
 ///
 /// ## Notes
 /// This function is always inlined for tailstrict
-#[inline(always)]
 pub(crate) fn inline_parse_function_call(
 	ctx: Context,
 	body_ctx: Option<Context>,
@@ -79,7 +78,6 @@
 	Ok(body_ctx.unwrap_or(ctx).extend(out, None, None, None)?)
 }
 
-#[inline(always)]
 pub(crate) fn place_args(
 	ctx: Context,
 	body_ctx: Option<Context>,
modifiedcrates/jsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/lib.rs
+++ b/crates/jsonnet-evaluator/src/lib.rs
@@ -2,7 +2,6 @@
 #![feature(type_alias_impl_trait)]
 #![feature(debug_non_exhaustive)]
 #![allow(macro_expanded_macro_exports_accessed_by_absolute_paths)]
-#![feature(stmt_expr_attributes)]
 mod ctx;
 mod dynamic;
 mod error;
@@ -81,17 +80,14 @@
 	/// Global state is fine there
 	pub(crate) static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)
 }
-#[inline(always)]
 pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {
 	EVAL_STATE.with(
-		#[inline(always)]
 		|s| f(s.borrow().as_ref().unwrap()),
 	)
 }
 pub(crate) fn create_error<T>(err: Error) -> Result<T> {
 	with_state(|s| s.error(err))
 }
-#[inline(always)]
 pub(crate) fn push<T>(e: LocExpr, comment: String, f: impl FnOnce() -> Result<T>) -> Result<T> {
 	with_state(|s| s.push(e, comment, f))
 }
@@ -251,7 +247,6 @@
 		Context::new().extend_unbound(new_bindings, None, None, None)
 	}
 
-	#[inline(always)]
 	pub fn push<T>(&self, e: LocExpr, comment: String, f: impl FnOnce() -> Result<T>) -> Result<T> {
 		{
 			let mut stack = self.0.stack.borrow_mut();
@@ -287,7 +282,6 @@
 		Err(LocError(err, self.stack_trace()))
 	}
 
-	#[inline(always)]
 	fn run_in_state<T>(&self, f: impl FnOnce() -> T) -> T {
 		EVAL_STATE.with(|v| {
 			let has_state = v.borrow().is_some();
modifiedcrates/jsonnet-evaluator/src/map.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/map.rs
+++ b/crates/jsonnet-evaluator/src/map.rs
@@ -18,7 +18,6 @@
 		}))
 	}
 
-	#[inline(always)]
 	pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V>
 	where
 		K: Borrow<Q>,
modifiedcrates/jsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/val.rs
+++ b/crates/jsonnet-evaluator/src/val.rs
@@ -77,7 +77,6 @@
 }
 impl FuncDesc {
 	/// This function is always inlined to make tailstrict work
-	#[inline(always)]
 	pub fn evaluate(&self, call_ctx: Context, args: &ArgsDesc, tailstrict: bool) -> Result<Val> {
 		let ctx = inline_parse_function_call(
 			call_ctx,
@@ -89,7 +88,6 @@
 		evaluate(ctx, &self.body)
 	}
 
-	#[inline(always)]
 	pub fn evaluate_values(&self, call_ctx: Context, args: &[Val]) -> Result<Val> {
 		let ctx = place_args(call_ctx, Some(self.ctx.clone()), &self.params, args)?;
 		evaluate(ctx, &self.body)