git.delta.rocks / jrsonnet / refs/commits / 0ec1408323ca

difftreelog

perf do not use intrinsics for == operator

Yaroslav Bolyukin2021-05-23parent: #8e8bdf2.patch.diff
in: master

3 files changed

modifiedcrates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/evaluate.rs
1use crate::{2	error::Error::*, lazy_val, push, throw, with_state, Context, ContextCreator, FuncDesc, FuncVal,3	FutureWrapper, LazyBinding, LazyVal, ObjMember, ObjValue, Result, Val,4};5use closure::closure;6use jrsonnet_interner::IStr;7use jrsonnet_parser::{8	ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, ExprLocation, FieldMember,9	ForSpecData, IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc, UnaryOpType,10	Visibility,11};12use jrsonnet_types::ValType;13use rustc_hash::{FxHashMap, FxHasher};14use std::{collections::HashMap, hash::BuildHasherDefault, rc::Rc};1516pub fn evaluate_binding_in_future(17	b: &BindSpec,18	context_creator: FutureWrapper<Context>,19) -> LazyVal {20	let b = b.clone();21	if let Some(params) = &b.params {22		let params = params.clone();23		LazyVal::new(Box::new(move || {24			Ok(evaluate_method(25				context_creator.unwrap(),26				b.name.clone(),27				params.clone(),28				b.value.clone(),29			))30		}))31	} else {32		LazyVal::new(Box::new(move || {33			evaluate_named(context_creator.unwrap(), &b.value, b.name.clone())34		}))35	}36}3738pub fn evaluate_binding(b: &BindSpec, context_creator: ContextCreator) -> (IStr, LazyBinding) {39	let b = b.clone();40	if let Some(params) = &b.params {41		let params = params.clone();42		(43			b.name.clone(),44			LazyBinding::Bindable(Rc::new(move |this, super_obj| {45				Ok(lazy_val!(46					closure!(clone b, clone params, clone context_creator, || Ok(evaluate_method(47						context_creator.create(this.clone(), super_obj.clone())?,48						b.name.clone(),49						params.clone(),50						b.value.clone(),51					)))52				))53			})),54		)55	} else {56		(57			b.name.clone(),58			LazyBinding::Bindable(Rc::new(move |this, super_obj| {59				Ok(lazy_val!(closure!(clone context_creator, clone b, ||60					evaluate_named(61						context_creator.create(this.clone(), super_obj.clone())?,62						&b.value,63						b.name.clone()64					)65				)))66			})),67		)68	}69}7071pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {72	Val::Func(Rc::new(FuncVal::Normal(FuncDesc {73		name,74		ctx,75		params,76		body,77	})))78}7980pub fn evaluate_field_name(81	context: Context,82	field_name: &jrsonnet_parser::FieldName,83) -> Result<Option<IStr>> {84	Ok(match field_name {85		jrsonnet_parser::FieldName::Fixed(n) => Some(n.clone()),86		jrsonnet_parser::FieldName::Dyn(expr) => {87			let value = evaluate(context, expr)?;88			if matches!(value, Val::Null) {89				None90			} else {91				Some(value.try_cast_str("dynamic field name")?)92			}93		}94	})95}9697pub fn evaluate_unary_op(op: UnaryOpType, b: &Val) -> Result<Val> {98	Ok(match (op, b) {99		(UnaryOpType::Not, Val::Bool(v)) => Val::Bool(!v),100		(UnaryOpType::Minus, Val::Num(n)) => Val::Num(-*n),101		(UnaryOpType::BitNot, Val::Num(n)) => Val::Num(!(*n as i32) as f64),102		(op, o) => throw!(UnaryOperatorDoesNotOperateOnType(op, o.value_type())),103	})104}105106pub fn evaluate_add_op(a: &Val, b: &Val) -> Result<Val> {107	Ok(match (a, b) {108		(Val::Str(v1), Val::Str(v2)) => Val::Str(((**v1).to_owned() + v2).into()),109110		// Can't use generic json serialization way, because it depends on number to string concatenation (std.jsonnet:890)111		(Val::Num(n), Val::Str(o)) => Val::Str(format!("{}{}", n, o).into()),112		(Val::Str(o), Val::Num(n)) => Val::Str(format!("{}{}", o, n).into()),113114		(Val::Str(s), o) => Val::Str(format!("{}{}", s, o.clone().to_string()?).into()),115		(o, Val::Str(s)) => Val::Str(format!("{}{}", o.clone().to_string()?, s).into()),116117		(Val::Obj(v1), Val::Obj(v2)) => Val::Obj(v2.extend_from(v1.clone())),118		(Val::Arr(a), Val::Arr(b)) => {119			let mut out = Vec::with_capacity(a.len() + b.len());120			out.extend(a.iter_lazy());121			out.extend(b.iter_lazy());122			Val::Arr(out.into())123		}124		(Val::Num(v1), Val::Num(v2)) => Val::new_checked_num(v1 + v2)?,125		_ => throw!(BinaryOperatorDoesNotOperateOnValues(126			BinaryOpType::Add,127			a.value_type(),128			b.value_type(),129		)),130	})131}132133pub fn evaluate_binary_op_special(134	context: Context,135	a: &LocExpr,136	op: BinaryOpType,137	b: &LocExpr,138) -> Result<Val> {139	Ok(match (evaluate(context.clone(), a)?, op, b) {140		(Val::Bool(true), BinaryOpType::Or, _o) => Val::Bool(true),141		(Val::Bool(false), BinaryOpType::And, _o) => Val::Bool(false),142		(a, op, eb) => evaluate_binary_op_normal(&a, op, &evaluate(context, eb)?)?,143	})144}145146pub fn evaluate_binary_op_normal(a: &Val, op: BinaryOpType, b: &Val) -> Result<Val> {147	Ok(match (a, op, b) {148		(a, BinaryOpType::Add, b) => evaluate_add_op(a, b)?,149150		(Val::Str(v1), BinaryOpType::Mul, Val::Num(v2)) => Val::Str(v1.repeat(*v2 as usize).into()),151152		// Bool X Bool153		(Val::Bool(a), BinaryOpType::And, Val::Bool(b)) => Val::Bool(*a && *b),154		(Val::Bool(a), BinaryOpType::Or, Val::Bool(b)) => Val::Bool(*a || *b),155156		// Str X Str157		(Val::Str(v1), BinaryOpType::Lt, Val::Str(v2)) => Val::Bool(v1 < v2),158		(Val::Str(v1), BinaryOpType::Gt, Val::Str(v2)) => Val::Bool(v1 > v2),159		(Val::Str(v1), BinaryOpType::Lte, Val::Str(v2)) => Val::Bool(v1 <= v2),160		(Val::Str(v1), BinaryOpType::Gte, Val::Str(v2)) => Val::Bool(v1 >= v2),161162		// Num X Num163		(Val::Num(v1), BinaryOpType::Mul, Val::Num(v2)) => Val::new_checked_num(v1 * v2)?,164		(Val::Num(v1), BinaryOpType::Div, Val::Num(v2)) => {165			if *v2 <= f64::EPSILON {166				throw!(DivisionByZero)167			}168			Val::new_checked_num(v1 / v2)?169		}170171		(Val::Num(v1), BinaryOpType::Sub, Val::Num(v2)) => Val::new_checked_num(v1 - v2)?,172173		(Val::Num(v1), BinaryOpType::Lt, Val::Num(v2)) => Val::Bool(v1 < v2),174		(Val::Num(v1), BinaryOpType::Gt, Val::Num(v2)) => Val::Bool(v1 > v2),175		(Val::Num(v1), BinaryOpType::Lte, Val::Num(v2)) => Val::Bool(v1 <= v2),176		(Val::Num(v1), BinaryOpType::Gte, Val::Num(v2)) => Val::Bool(v1 >= v2),177178		(Val::Num(v1), BinaryOpType::BitAnd, Val::Num(v2)) => {179			Val::Num(((*v1 as i32) & (*v2 as i32)) as f64)180		}181		(Val::Num(v1), BinaryOpType::BitOr, Val::Num(v2)) => {182			Val::Num(((*v1 as i32) | (*v2 as i32)) as f64)183		}184		(Val::Num(v1), BinaryOpType::BitXor, Val::Num(v2)) => {185			Val::Num(((*v1 as i32) ^ (*v2 as i32)) as f64)186		}187		(Val::Num(v1), BinaryOpType::Lhs, Val::Num(v2)) => {188			if *v2 < 0.0 {189				throw!(RuntimeError("shift by negative exponent".into()))190			}191			Val::Num(((*v1 as i32) << (*v2 as i32)) as f64)192		}193		(Val::Num(v1), BinaryOpType::Rhs, Val::Num(v2)) => {194			if *v2 < 0.0 {195				throw!(RuntimeError("shift by negative exponent".into()))196			}197			Val::Num(((*v1 as i32) >> (*v2 as i32)) as f64)198		}199200		_ => throw!(BinaryOperatorDoesNotOperateOnValues(201			op,202			a.value_type(),203			b.value_type(),204		)),205	})206}207208pub fn evaluate_comp<T>(209	context: Context,210	value: &impl Fn(Context) -> Result<T>,211	specs: &[CompSpec],212) -> Result<Option<Vec<T>>> {213	Ok(match specs.get(0) {214		None => Some(vec![value(context)?]),215		Some(CompSpec::IfSpec(IfSpecData(cond))) => {216			if evaluate(context.clone(), cond)?.try_cast_bool("if spec")? {217				evaluate_comp(context, value, &specs[1..])?218			} else {219				None220			}221		}222		Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(context.clone(), expr)? {223			Val::Arr(list) => {224				let mut out = Vec::new();225				for item in list.iter() {226					out.push(evaluate_comp(227						context.clone().with_var(var.clone(), item?.clone()),228						value,229						&specs[1..],230					)?);231				}232				Some(out.into_iter().flatten().flatten().collect())233			}234			_ => throw!(InComprehensionCanOnlyIterateOverArray),235		},236	})237}238239pub fn evaluate_member_list_object(context: Context, members: &[Member]) -> Result<ObjValue> {240	let new_bindings = FutureWrapper::new();241	let future_this = FutureWrapper::new();242	let context_creator = ContextCreator(context.clone(), new_bindings.clone());243	{244		let mut bindings: FxHashMap<IStr, LazyBinding> =245			FxHashMap::with_capacity_and_hasher(members.len(), BuildHasherDefault::default());246		for (n, b) in members247			.iter()248			.filter_map(|m| match m {249				Member::BindStmt(b) => Some(b.clone()),250				_ => None,251			})252			.map(|b| evaluate_binding(&b, context_creator.clone()))253		{254			bindings.insert(n, b);255		}256		new_bindings.fill(bindings);257	}258259	let mut new_members = FxHashMap::default();260	for member in members.iter() {261		match member {262			Member::Field(FieldMember {263				name,264				plus,265				params: None,266				visibility,267				value,268			}) => {269				let name = evaluate_field_name(context.clone(), name)?;270				if name.is_none() {271					continue;272				}273				let name = name.unwrap();274				new_members.insert(275					name.clone(),276					ObjMember {277						add: *plus,278						visibility: *visibility,279						invoke: LazyBinding::Bindable(Rc::new(280							closure!(clone name, clone value, clone context_creator, |this, super_obj| {281								Ok(LazyVal::new_resolved(evaluate_named(282									context_creator.create(this, super_obj)?,283									&value,284									name.clone(),285								)?))286							}),287						)),288						location: value.1.clone(),289					},290				);291			}292			Member::Field(FieldMember {293				name,294				params: Some(params),295				value,296				..297			}) => {298				let name = evaluate_field_name(context.clone(), name)?;299				if name.is_none() {300					continue;301				}302				let name = name.unwrap();303				new_members.insert(304					name.clone(),305					ObjMember {306						add: false,307						visibility: Visibility::Hidden,308						invoke: LazyBinding::Bindable(Rc::new(309							closure!(clone value, clone context_creator, clone params, clone name, |this, super_obj| {310								// TODO: Assert311								Ok(LazyVal::new_resolved(evaluate_method(312									context_creator.create(this, super_obj)?,313									name.clone(),314									params.clone(),315									value.clone(),316								)))317							}),318						)),319						location: value.1.clone(),320					},321				);322			}323			Member::BindStmt(_) => {}324			Member::AssertStmt(_) => {}325		}326	}327	let this = ObjValue::new(None, Rc::new(new_members));328	future_this.fill(this.clone());329	Ok(this)330}331332pub fn evaluate_object(context: Context, object: &ObjBody) -> Result<ObjValue> {333	Ok(match object {334		ObjBody::MemberList(members) => evaluate_member_list_object(context, members)?,335		ObjBody::ObjComp(obj) => {336			let future_this = FutureWrapper::new();337			let mut new_members = FxHashMap::default();338			for (k, v) in evaluate_comp(339				context.clone(),340				&|ctx| {341					let new_bindings = FutureWrapper::new();342					let context_creator = ContextCreator(context.clone(), new_bindings.clone());343					let mut bindings: FxHashMap<IStr, LazyBinding> = FxHashMap::with_capacity_and_hasher(obj.pre_locals.len() + obj.post_locals.len(), BuildHasherDefault::default());344					for (n, b) in obj345						.pre_locals346						.iter()347						.chain(obj.post_locals.iter())348						.map(|b| evaluate_binding(b, context_creator.clone()))349					{350						bindings.insert(n, b);351					}352					new_bindings.fill(bindings.clone());353					let ctx = ctx.extend_unbound(bindings, None, None, None)?;354					let key = evaluate(ctx.clone(), &obj.key)?;355					let value = LazyBinding::Bindable(Rc::new(356						closure!(clone ctx, clone obj.value, |this, _super_obj| {357							Ok(LazyVal::new_resolved(evaluate(ctx.clone().extend(FxHashMap::default(), None, this, None), &value)?))358						}),359					));360361					Ok((key, value))362				},363				&obj.compspecs,364			)?365			.unwrap()366			{367				match k {368					Val::Null => {}369					Val::Str(n) => {370						new_members.insert(371							n,372							ObjMember {373								add: false,374								visibility: Visibility::Normal,375								invoke: v,376								location: obj.value.1.clone(),377							},378						);379					}380					v => throw!(FieldMustBeStringGot(v.value_type())),381				}382			}383384			let this = ObjValue::new(None, Rc::new(new_members));385			future_this.fill(this.clone());386			this387		}388	})389}390391pub fn evaluate_apply(392	context: Context,393	value: &LocExpr,394	args: &ArgsDesc,395	loc: Option<&ExprLocation>,396	tailstrict: bool,397) -> Result<Val> {398	let value = evaluate(context.clone(), value)?;399	Ok(match value {400		Val::Func(f) => {401			let body = || f.evaluate(context, loc, args, tailstrict);402			if tailstrict {403				body()?404			} else {405				push(loc, || format!("function <{}> call", f.name()), body)?406			}407		}408		v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),409	})410}411412pub fn evaluate_named(context: Context, lexpr: &LocExpr, name: IStr) -> Result<Val> {413	use Expr::*;414	let LocExpr(expr, _loc) = lexpr;415	Ok(match &**expr {416		Function(params, body) => evaluate_method(context, name, params.clone(), body.clone()),417		_ => evaluate(context, lexpr)?,418	})419}420421pub fn evaluate(context: Context, expr: &LocExpr) -> Result<Val> {422	use Expr::*;423	let LocExpr(expr, loc) = expr;424	Ok(match &**expr {425		Literal(LiteralType::This) => {426			Val::Obj(context.this().clone().ok_or(CantUseSelfOutsideOfObject)?)427		}428		Literal(LiteralType::Super) => Val::Obj(429			context430				.super_obj()431				.clone()432				.ok_or(NoSuperFound)?433				.with_this(context.this().clone().unwrap()),434		),435		Literal(LiteralType::Dollar) => {436			Val::Obj(context.dollar().clone().ok_or(NoTopLevelObjectFound)?)437		}438		Literal(LiteralType::True) => Val::Bool(true),439		Literal(LiteralType::False) => Val::Bool(false),440		Literal(LiteralType::Null) => Val::Null,441		Parened(e) => evaluate(context, e)?,442		Str(v) => Val::Str(v.clone()),443		Num(v) => Val::new_checked_num(*v)?,444		BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, v1, *o, v2)?,445		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,446		Var(name) => push(447			loc.as_ref(),448			|| format!("variable <{}>", name),449			|| context.binding(name.clone())?.evaluate(),450		)?,451		Index(value, index) => {452			match (evaluate(context.clone(), value)?, evaluate(context, index)?) {453				(Val::Obj(v), Val::Str(s)) => {454					let sn = s.clone();455					push(456						loc.as_ref(),457						|| format!("field <{}> access", sn),458						|| {459							if let Some(v) = v.get(s.clone())? {460								Ok(v)461							} else if v.get("__intrinsic_namespace__".into())?.is_some() {462								Ok(Val::Func(Rc::new(FuncVal::Intrinsic(s))))463							} else {464								throw!(NoSuchField(s))465							}466						},467					)?468				}469				(Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(470					ValType::Obj,471					ValType::Str,472					n.value_type(),473				)),474475				(Val::Arr(v), Val::Num(n)) => {476					if n.fract() > f64::EPSILON {477						throw!(FractionalIndex)478					}479					v.get(n as usize)?480						.ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?481				}482				(Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),483				(Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(484					ValType::Arr,485					ValType::Num,486					n.value_type(),487				)),488489				(Val::Str(s), Val::Num(n)) => Val::Str(490					s.chars()491						.skip(n as usize)492						.take(1)493						.collect::<String>()494						.into(),495				),496				(Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(497					ValType::Str,498					ValType::Num,499					n.value_type(),500				)),501502				(v, _) => throw!(CantIndexInto(v.value_type())),503			}504		}505		LocalExpr(bindings, returned) => {506			let mut new_bindings: FxHashMap<IStr, LazyVal> = HashMap::with_capacity_and_hasher(507				bindings.len(),508				BuildHasherDefault::<FxHasher>::default(),509			);510			let future_context = Context::new_future();511			for b in bindings {512				new_bindings.insert(513					b.name.clone(),514					evaluate_binding_in_future(b, future_context.clone()),515				);516			}517			let context = context518				.extend_bound(new_bindings)519				.into_future(future_context);520			evaluate(context, &returned.clone())?521		}522		Arr(items) => {523			let mut out = Vec::with_capacity(items.len());524			for item in items {525				out.push(LazyVal::new(Box::new(526					closure!(clone context, clone item, || {527						evaluate(context.clone(), &item)528					}),529				)));530			}531			Val::Arr(out.into())532		}533		ArrComp(expr, comp_specs) => Val::Arr(534			// First comp_spec should be for_spec, so no "None" possible here535			evaluate_comp(context, &|ctx| evaluate(ctx, expr), comp_specs)?536				.unwrap()537				.into(),538		),539		Obj(body) => Val::Obj(evaluate_object(context, body)?),540		ObjExtend(s, t) => evaluate_add_op(541			&evaluate(context.clone(), s)?,542			&Val::Obj(evaluate_object(context, t)?),543		)?,544		Apply(value, args, tailstrict) => {545			evaluate_apply(context, value, args, loc.as_ref(), *tailstrict)?546		}547		Function(params, body) => {548			evaluate_method(context, "anonymous".into(), params.clone(), body.clone())549		}550		Intrinsic(name) => Val::Func(Rc::new(FuncVal::Intrinsic(name.clone()))),551		AssertExpr(AssertStmt(value, msg), returned) => {552			let assertion_result = push(553				value.1.as_ref(),554				|| "assertion condition".to_owned(),555				|| {556					evaluate(context.clone(), value)?557						.try_cast_bool("assertion condition should be of type `boolean`")558				},559			)?;560			if assertion_result {561				evaluate(context, returned)?562			} else {563				push(564					value.1.as_ref(),565					|| "assertion failure".to_owned(),566					|| {567						if let Some(msg) = msg {568							throw!(AssertionFailed(evaluate(context, msg)?.to_string()?));569						} else {570							throw!(AssertionFailed(Val::Null.to_string()?));571						}572					},573				)?574			}575		}576		ErrorStmt(e) => push(577			loc.as_ref(),578			|| "error statement".to_owned(),579			|| {580				throw!(RuntimeError(581					evaluate(context, e)?.try_cast_str("error text should be of type `string`")?,582				))583			},584		)?,585		IfElse {586			cond,587			cond_then,588			cond_else,589		} => {590			if push(591				loc.as_ref(),592				|| "if condition".to_owned(),593				|| evaluate(context.clone(), &cond.0)?.try_cast_bool("in if condition"),594			)? {595				evaluate(context, cond_then)?596			} else {597				match cond_else {598					Some(v) => evaluate(context, v)?,599					None => Val::Null,600				}601			}602		}603		Import(path) => {604			let mut tmp = loc605				.clone()606				.expect("imports cannot be used without loc_data")607				.0;608			let import_location = Rc::make_mut(&mut tmp);609			import_location.pop();610			push(611				loc.as_ref(),612				|| format!("import {:?}", path),613				|| with_state(|s| s.import_file(import_location, path)),614			)?615		}616		ImportStr(path) => {617			let mut tmp = loc618				.clone()619				.expect("imports cannot be used without loc_data")620				.0;621			let import_location = Rc::make_mut(&mut tmp);622			import_location.pop();623			Val::Str(with_state(|s| s.import_file_str(import_location, path))?)624		}625	})626}
after · crates/jrsonnet-evaluator/src/evaluate.rs
1use crate::{2	error::Error::*, lazy_val, push, throw, with_state, Context, ContextCreator, FuncDesc, FuncVal,3	FutureWrapper, LazyBinding, LazyVal, ObjMember, ObjValue, Result, Val,4	equals,5};6use closure::closure;7use jrsonnet_interner::IStr;8use jrsonnet_parser::{9	ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, ExprLocation, FieldMember,10	ForSpecData, IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc, UnaryOpType,11	Visibility,12};13use jrsonnet_types::ValType;14use rustc_hash::{FxHashMap, FxHasher};15use std::{collections::HashMap, hash::BuildHasherDefault, rc::Rc};1617pub fn evaluate_binding_in_future(18	b: &BindSpec,19	context_creator: FutureWrapper<Context>,20) -> LazyVal {21	let b = b.clone();22	if let Some(params) = &b.params {23		let params = params.clone();24		LazyVal::new(Box::new(move || {25			Ok(evaluate_method(26				context_creator.unwrap(),27				b.name.clone(),28				params.clone(),29				b.value.clone(),30			))31		}))32	} else {33		LazyVal::new(Box::new(move || {34			evaluate_named(context_creator.unwrap(), &b.value, b.name.clone())35		}))36	}37}3839pub fn evaluate_binding(b: &BindSpec, context_creator: ContextCreator) -> (IStr, LazyBinding) {40	let b = b.clone();41	if let Some(params) = &b.params {42		let params = params.clone();43		(44			b.name.clone(),45			LazyBinding::Bindable(Rc::new(move |this, super_obj| {46				Ok(lazy_val!(47					closure!(clone b, clone params, clone context_creator, || Ok(evaluate_method(48						context_creator.create(this.clone(), super_obj.clone())?,49						b.name.clone(),50						params.clone(),51						b.value.clone(),52					)))53				))54			})),55		)56	} else {57		(58			b.name.clone(),59			LazyBinding::Bindable(Rc::new(move |this, super_obj| {60				Ok(lazy_val!(closure!(clone context_creator, clone b, ||61					evaluate_named(62						context_creator.create(this.clone(), super_obj.clone())?,63						&b.value,64						b.name.clone()65					)66				)))67			})),68		)69	}70}7172pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {73	Val::Func(Rc::new(FuncVal::Normal(FuncDesc {74		name,75		ctx,76		params,77		body,78	})))79}8081pub fn evaluate_field_name(82	context: Context,83	field_name: &jrsonnet_parser::FieldName,84) -> Result<Option<IStr>> {85	Ok(match field_name {86		jrsonnet_parser::FieldName::Fixed(n) => Some(n.clone()),87		jrsonnet_parser::FieldName::Dyn(expr) => {88			let value = evaluate(context, expr)?;89			if matches!(value, Val::Null) {90				None91			} else {92				Some(value.try_cast_str("dynamic field name")?)93			}94		}95	})96}9798pub fn evaluate_unary_op(op: UnaryOpType, b: &Val) -> Result<Val> {99	Ok(match (op, b) {100		(UnaryOpType::Not, Val::Bool(v)) => Val::Bool(!v),101		(UnaryOpType::Minus, Val::Num(n)) => Val::Num(-*n),102		(UnaryOpType::BitNot, Val::Num(n)) => Val::Num(!(*n as i32) as f64),103		(op, o) => throw!(UnaryOperatorDoesNotOperateOnType(op, o.value_type())),104	})105}106107pub fn evaluate_add_op(a: &Val, b: &Val) -> Result<Val> {108	Ok(match (a, b) {109		(Val::Str(v1), Val::Str(v2)) => Val::Str(((**v1).to_owned() + v2).into()),110111		// Can't use generic json serialization way, because it depends on number to string concatenation (std.jsonnet:890)112		(Val::Num(n), Val::Str(o)) => Val::Str(format!("{}{}", n, o).into()),113		(Val::Str(o), Val::Num(n)) => Val::Str(format!("{}{}", o, n).into()),114115		(Val::Str(s), o) => Val::Str(format!("{}{}", s, o.clone().to_string()?).into()),116		(o, Val::Str(s)) => Val::Str(format!("{}{}", o.clone().to_string()?, s).into()),117118		(Val::Obj(v1), Val::Obj(v2)) => Val::Obj(v2.extend_from(v1.clone())),119		(Val::Arr(a), Val::Arr(b)) => {120			let mut out = Vec::with_capacity(a.len() + b.len());121			out.extend(a.iter_lazy());122			out.extend(b.iter_lazy());123			Val::Arr(out.into())124		}125		(Val::Num(v1), Val::Num(v2)) => Val::new_checked_num(v1 + v2)?,126		_ => throw!(BinaryOperatorDoesNotOperateOnValues(127			BinaryOpType::Add,128			a.value_type(),129			b.value_type(),130		)),131	})132}133134pub fn evaluate_binary_op_special(135	context: Context,136	a: &LocExpr,137	op: BinaryOpType,138	b: &LocExpr,139) -> Result<Val> {140	Ok(match (evaluate(context.clone(), a)?, op, b) {141		(Val::Bool(true), BinaryOpType::Or, _o) => Val::Bool(true),142		(Val::Bool(false), BinaryOpType::And, _o) => Val::Bool(false),143		(a, op, eb) => evaluate_binary_op_normal(&a, op, &evaluate(context, eb)?)?,144	})145}146147pub fn evaluate_binary_op_normal(a: &Val, op: BinaryOpType, b: &Val) -> Result<Val> {148	Ok(match (a, op, b) {149		(a, BinaryOpType::Add, b) => evaluate_add_op(a, b)?,150151		(a, BinaryOpType::Eq, b) => Val::Bool(equals(&a, &b)?),152		(a, BinaryOpType::Neq, b) => Val::Bool(!equals(&a, &b)?),153154		(Val::Str(v1), BinaryOpType::Mul, Val::Num(v2)) => Val::Str(v1.repeat(*v2 as usize).into()),155156		// Bool X Bool157		(Val::Bool(a), BinaryOpType::And, Val::Bool(b)) => Val::Bool(*a && *b),158		(Val::Bool(a), BinaryOpType::Or, Val::Bool(b)) => Val::Bool(*a || *b),159160		// Str X Str161		(Val::Str(v1), BinaryOpType::Lt, Val::Str(v2)) => Val::Bool(v1 < v2),162		(Val::Str(v1), BinaryOpType::Gt, Val::Str(v2)) => Val::Bool(v1 > v2),163		(Val::Str(v1), BinaryOpType::Lte, Val::Str(v2)) => Val::Bool(v1 <= v2),164		(Val::Str(v1), BinaryOpType::Gte, Val::Str(v2)) => Val::Bool(v1 >= v2),165166		// Num X Num167		(Val::Num(v1), BinaryOpType::Mul, Val::Num(v2)) => Val::new_checked_num(v1 * v2)?,168		(Val::Num(v1), BinaryOpType::Div, Val::Num(v2)) => {169			if *v2 <= f64::EPSILON {170				throw!(DivisionByZero)171			}172			Val::new_checked_num(v1 / v2)?173		}174175		(Val::Num(v1), BinaryOpType::Sub, Val::Num(v2)) => Val::new_checked_num(v1 - v2)?,176177		(Val::Num(v1), BinaryOpType::Lt, Val::Num(v2)) => Val::Bool(v1 < v2),178		(Val::Num(v1), BinaryOpType::Gt, Val::Num(v2)) => Val::Bool(v1 > v2),179		(Val::Num(v1), BinaryOpType::Lte, Val::Num(v2)) => Val::Bool(v1 <= v2),180		(Val::Num(v1), BinaryOpType::Gte, Val::Num(v2)) => Val::Bool(v1 >= v2),181182		(Val::Num(v1), BinaryOpType::BitAnd, Val::Num(v2)) => {183			Val::Num(((*v1 as i32) & (*v2 as i32)) as f64)184		}185		(Val::Num(v1), BinaryOpType::BitOr, Val::Num(v2)) => {186			Val::Num(((*v1 as i32) | (*v2 as i32)) as f64)187		}188		(Val::Num(v1), BinaryOpType::BitXor, Val::Num(v2)) => {189			Val::Num(((*v1 as i32) ^ (*v2 as i32)) as f64)190		}191		(Val::Num(v1), BinaryOpType::Lhs, Val::Num(v2)) => {192			if *v2 < 0.0 {193				throw!(RuntimeError("shift by negative exponent".into()))194			}195			Val::Num(((*v1 as i32) << (*v2 as i32)) as f64)196		}197		(Val::Num(v1), BinaryOpType::Rhs, Val::Num(v2)) => {198			if *v2 < 0.0 {199				throw!(RuntimeError("shift by negative exponent".into()))200			}201			Val::Num(((*v1 as i32) >> (*v2 as i32)) as f64)202		}203204		_ => throw!(BinaryOperatorDoesNotOperateOnValues(205			op,206			a.value_type(),207			b.value_type(),208		)),209	})210}211212pub fn evaluate_comp<T>(213	context: Context,214	value: &impl Fn(Context) -> Result<T>,215	specs: &[CompSpec],216) -> Result<Option<Vec<T>>> {217	Ok(match specs.get(0) {218		None => Some(vec![value(context)?]),219		Some(CompSpec::IfSpec(IfSpecData(cond))) => {220			if evaluate(context.clone(), cond)?.try_cast_bool("if spec")? {221				evaluate_comp(context, value, &specs[1..])?222			} else {223				None224			}225		}226		Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(context.clone(), expr)? {227			Val::Arr(list) => {228				let mut out = Vec::new();229				for item in list.iter() {230					out.push(evaluate_comp(231						context.clone().with_var(var.clone(), item?.clone()),232						value,233						&specs[1..],234					)?);235				}236				Some(out.into_iter().flatten().flatten().collect())237			}238			_ => throw!(InComprehensionCanOnlyIterateOverArray),239		},240	})241}242243pub fn evaluate_member_list_object(context: Context, members: &[Member]) -> Result<ObjValue> {244	let new_bindings = FutureWrapper::new();245	let future_this = FutureWrapper::new();246	let context_creator = ContextCreator(context.clone(), new_bindings.clone());247	{248		let mut bindings: FxHashMap<IStr, LazyBinding> =249			FxHashMap::with_capacity_and_hasher(members.len(), BuildHasherDefault::default());250		for (n, b) in members251			.iter()252			.filter_map(|m| match m {253				Member::BindStmt(b) => Some(b.clone()),254				_ => None,255			})256			.map(|b| evaluate_binding(&b, context_creator.clone()))257		{258			bindings.insert(n, b);259		}260		new_bindings.fill(bindings);261	}262263	let mut new_members = FxHashMap::default();264	for member in members.iter() {265		match member {266			Member::Field(FieldMember {267				name,268				plus,269				params: None,270				visibility,271				value,272			}) => {273				let name = evaluate_field_name(context.clone(), name)?;274				if name.is_none() {275					continue;276				}277				let name = name.unwrap();278				new_members.insert(279					name.clone(),280					ObjMember {281						add: *plus,282						visibility: *visibility,283						invoke: LazyBinding::Bindable(Rc::new(284							closure!(clone name, clone value, clone context_creator, |this, super_obj| {285								Ok(LazyVal::new_resolved(evaluate_named(286									context_creator.create(this, super_obj)?,287									&value,288									name.clone(),289								)?))290							}),291						)),292						location: value.1.clone(),293					},294				);295			}296			Member::Field(FieldMember {297				name,298				params: Some(params),299				value,300				..301			}) => {302				let name = evaluate_field_name(context.clone(), name)?;303				if name.is_none() {304					continue;305				}306				let name = name.unwrap();307				new_members.insert(308					name.clone(),309					ObjMember {310						add: false,311						visibility: Visibility::Hidden,312						invoke: LazyBinding::Bindable(Rc::new(313							closure!(clone value, clone context_creator, clone params, clone name, |this, super_obj| {314								// TODO: Assert315								Ok(LazyVal::new_resolved(evaluate_method(316									context_creator.create(this, super_obj)?,317									name.clone(),318									params.clone(),319									value.clone(),320								)))321							}),322						)),323						location: value.1.clone(),324					},325				);326			}327			Member::BindStmt(_) => {}328			Member::AssertStmt(_) => {}329		}330	}331	let this = ObjValue::new(None, Rc::new(new_members));332	future_this.fill(this.clone());333	Ok(this)334}335336pub fn evaluate_object(context: Context, object: &ObjBody) -> Result<ObjValue> {337	Ok(match object {338		ObjBody::MemberList(members) => evaluate_member_list_object(context, members)?,339		ObjBody::ObjComp(obj) => {340			let future_this = FutureWrapper::new();341			let mut new_members = FxHashMap::default();342			for (k, v) in evaluate_comp(343				context.clone(),344				&|ctx| {345					let new_bindings = FutureWrapper::new();346					let context_creator = ContextCreator(context.clone(), new_bindings.clone());347					let mut bindings: FxHashMap<IStr, LazyBinding> = FxHashMap::with_capacity_and_hasher(obj.pre_locals.len() + obj.post_locals.len(), BuildHasherDefault::default());348					for (n, b) in obj349						.pre_locals350						.iter()351						.chain(obj.post_locals.iter())352						.map(|b| evaluate_binding(b, context_creator.clone()))353					{354						bindings.insert(n, b);355					}356					new_bindings.fill(bindings.clone());357					let ctx = ctx.extend_unbound(bindings, None, None, None)?;358					let key = evaluate(ctx.clone(), &obj.key)?;359					let value = LazyBinding::Bindable(Rc::new(360						closure!(clone ctx, clone obj.value, |this, _super_obj| {361							Ok(LazyVal::new_resolved(evaluate(ctx.clone().extend(FxHashMap::default(), None, this, None), &value)?))362						}),363					));364365					Ok((key, value))366				},367				&obj.compspecs,368			)?369			.unwrap()370			{371				match k {372					Val::Null => {}373					Val::Str(n) => {374						new_members.insert(375							n,376							ObjMember {377								add: false,378								visibility: Visibility::Normal,379								invoke: v,380								location: obj.value.1.clone(),381							},382						);383					}384					v => throw!(FieldMustBeStringGot(v.value_type())),385				}386			}387388			let this = ObjValue::new(None, Rc::new(new_members));389			future_this.fill(this.clone());390			this391		}392	})393}394395pub fn evaluate_apply(396	context: Context,397	value: &LocExpr,398	args: &ArgsDesc,399	loc: Option<&ExprLocation>,400	tailstrict: bool,401) -> Result<Val> {402	let value = evaluate(context.clone(), value)?;403	Ok(match value {404		Val::Func(f) => {405			let body = || f.evaluate(context, loc, args, tailstrict);406			if tailstrict {407				body()?408			} else {409				push(loc, || format!("function <{}> call", f.name()), body)?410			}411		}412		v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),413	})414}415416pub fn evaluate_named(context: Context, lexpr: &LocExpr, name: IStr) -> Result<Val> {417	use Expr::*;418	let LocExpr(expr, _loc) = lexpr;419	Ok(match &**expr {420		Function(params, body) => evaluate_method(context, name, params.clone(), body.clone()),421		_ => evaluate(context, lexpr)?,422	})423}424425pub fn evaluate(context: Context, expr: &LocExpr) -> Result<Val> {426	use Expr::*;427	let LocExpr(expr, loc) = expr;428	Ok(match &**expr {429		Literal(LiteralType::This) => {430			Val::Obj(context.this().clone().ok_or(CantUseSelfOutsideOfObject)?)431		}432		Literal(LiteralType::Super) => Val::Obj(433			context434				.super_obj()435				.clone()436				.ok_or(NoSuperFound)?437				.with_this(context.this().clone().unwrap()),438		),439		Literal(LiteralType::Dollar) => {440			Val::Obj(context.dollar().clone().ok_or(NoTopLevelObjectFound)?)441		}442		Literal(LiteralType::True) => Val::Bool(true),443		Literal(LiteralType::False) => Val::Bool(false),444		Literal(LiteralType::Null) => Val::Null,445		Parened(e) => evaluate(context, e)?,446		Str(v) => Val::Str(v.clone()),447		Num(v) => Val::new_checked_num(*v)?,448		BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, v1, *o, v2)?,449		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,450		Var(name) => push(451			loc.as_ref(),452			|| format!("variable <{}>", name),453			|| context.binding(name.clone())?.evaluate(),454		)?,455		Index(value, index) => {456			match (evaluate(context.clone(), value)?, evaluate(context, index)?) {457				(Val::Obj(v), Val::Str(s)) => {458					let sn = s.clone();459					push(460						loc.as_ref(),461						|| format!("field <{}> access", sn),462						|| {463							if let Some(v) = v.get(s.clone())? {464								Ok(v)465							} else if v.get("__intrinsic_namespace__".into())?.is_some() {466								Ok(Val::Func(Rc::new(FuncVal::Intrinsic(s))))467							} else {468								throw!(NoSuchField(s))469							}470						},471					)?472				}473				(Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(474					ValType::Obj,475					ValType::Str,476					n.value_type(),477				)),478479				(Val::Arr(v), Val::Num(n)) => {480					if n.fract() > f64::EPSILON {481						throw!(FractionalIndex)482					}483					v.get(n as usize)?484						.ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?485				}486				(Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),487				(Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(488					ValType::Arr,489					ValType::Num,490					n.value_type(),491				)),492493				(Val::Str(s), Val::Num(n)) => Val::Str(494					s.chars()495						.skip(n as usize)496						.take(1)497						.collect::<String>()498						.into(),499				),500				(Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(501					ValType::Str,502					ValType::Num,503					n.value_type(),504				)),505506				(v, _) => throw!(CantIndexInto(v.value_type())),507			}508		}509		LocalExpr(bindings, returned) => {510			let mut new_bindings: FxHashMap<IStr, LazyVal> = HashMap::with_capacity_and_hasher(511				bindings.len(),512				BuildHasherDefault::<FxHasher>::default(),513			);514			let future_context = Context::new_future();515			for b in bindings {516				new_bindings.insert(517					b.name.clone(),518					evaluate_binding_in_future(b, future_context.clone()),519				);520			}521			let context = context522				.extend_bound(new_bindings)523				.into_future(future_context);524			evaluate(context, &returned.clone())?525		}526		Arr(items) => {527			let mut out = Vec::with_capacity(items.len());528			for item in items {529				out.push(LazyVal::new(Box::new(530					closure!(clone context, clone item, || {531						evaluate(context.clone(), &item)532					}),533				)));534			}535			Val::Arr(out.into())536		}537		ArrComp(expr, comp_specs) => Val::Arr(538			// First comp_spec should be for_spec, so no "None" possible here539			evaluate_comp(context, &|ctx| evaluate(ctx, expr), comp_specs)?540				.unwrap()541				.into(),542		),543		Obj(body) => Val::Obj(evaluate_object(context, body)?),544		ObjExtend(s, t) => evaluate_add_op(545			&evaluate(context.clone(), s)?,546			&Val::Obj(evaluate_object(context, t)?),547		)?,548		Apply(value, args, tailstrict) => {549			evaluate_apply(context, value, args, loc.as_ref(), *tailstrict)?550		}551		Function(params, body) => {552			evaluate_method(context, "anonymous".into(), params.clone(), body.clone())553		}554		Intrinsic(name) => Val::Func(Rc::new(FuncVal::Intrinsic(name.clone()))),555		AssertExpr(AssertStmt(value, msg), returned) => {556			let assertion_result = push(557				value.1.as_ref(),558				|| "assertion condition".to_owned(),559				|| {560					evaluate(context.clone(), value)?561						.try_cast_bool("assertion condition should be of type `boolean`")562				},563			)?;564			if assertion_result {565				evaluate(context, returned)?566			} else {567				push(568					value.1.as_ref(),569					|| "assertion failure".to_owned(),570					|| {571						if let Some(msg) = msg {572							throw!(AssertionFailed(evaluate(context, msg)?.to_string()?));573						} else {574							throw!(AssertionFailed(Val::Null.to_string()?));575						}576					},577				)?578			}579		}580		ErrorStmt(e) => push(581			loc.as_ref(),582			|| "error statement".to_owned(),583			|| {584				throw!(RuntimeError(585					evaluate(context, e)?.try_cast_str("error text should be of type `string`")?,586				))587			},588		)?,589		IfElse {590			cond,591			cond_then,592			cond_else,593		} => {594			if push(595				loc.as_ref(),596				|| "if condition".to_owned(),597				|| evaluate(context.clone(), &cond.0)?.try_cast_bool("in if condition"),598			)? {599				evaluate(context, cond_then)?600			} else {601				match cond_else {602					Some(v) => evaluate(context, v)?,603					None => Val::Null,604				}605			}606		}607		Import(path) => {608			let mut tmp = loc609				.clone()610				.expect("imports cannot be used without loc_data")611				.0;612			let import_location = Rc::make_mut(&mut tmp);613			import_location.pop();614			push(615				loc.as_ref(),616				|| format!("import {:?}", path),617				|| with_state(|s| s.import_file(import_location, path)),618			)?619		}620		ImportStr(path) => {621			let mut tmp = loc622				.clone()623				.expect("imports cannot be used without loc_data")624				.0;625			let import_location = Rc::make_mut(&mut tmp);626			import_location.pop();627			Val::Str(with_state(|s| s.import_file_str(import_location, path))?)628		}629	})630}
modifiedcrates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -113,6 +113,9 @@
 	BitOr,
 	BitXor,
 
+	Eq,
+	Neq,
+
 	And,
 	Or,
 }
@@ -137,6 +140,8 @@
 				BitAnd => "&",
 				BitOr => "|",
 				BitXor => "^",
+				Eq => "==",
+				New => "!=",
 				And => "&&",
 				Or => "||",
 			}
modifiedcrates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -227,16 +227,8 @@
 				--
 				a:(@) _ binop(<"&">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitAnd, b))}
 				--
-				a:(@) _ binop(<"==">) _ b:@ {loc_expr_todo!(Expr::Apply(
-					el!(Expr::Intrinsic("equals".into())),
-					ArgsDesc(vec![Arg(None, a), Arg(None, b)]),
-					true
-				))}
-				a:(@) _ binop(<"!=">) _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, el!(Expr::Apply(
-					el!(Expr::Intrinsic("equals".into())),
-					ArgsDesc(vec![Arg(None, a), Arg(None, b)]),
-					true
-				))))}
+				a:(@) _ binop(<"==">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Eq, b))}
+				a:(@) _ binop(<"!=">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Neq, b))}
 				--
 				a:(@) _ binop(<"<">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lt, b))}
 				a:(@) _ binop(<">">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gt, b))}