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

difftreelog

perf switch to external storage for object comp

Yaroslav Bolyukin2021-05-23parent: #1cc9e6c.patch.diff
in: master

1 file 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	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	let mut assertions = Vec::new();265	for member in members.iter() {266		match member {267			Member::Field(FieldMember {268				name,269				plus,270				params: None,271				visibility,272				value,273			}) => {274				let name = evaluate_field_name(context.clone(), name)?;275				if name.is_none() {276					continue;277				}278				let name = name.unwrap();279				new_members.insert(280					name.clone(),281					ObjMember {282						add: *plus,283						visibility: *visibility,284						invoke: LazyBinding::Bindable(Rc::new(285							closure!(clone name, clone value, clone context_creator, |this, super_obj| {286								Ok(LazyVal::new_resolved(evaluate_named(287									context_creator.create(this, super_obj)?,288									&value,289									name.clone(),290								)?))291							}),292						)),293						location: value.1.clone(),294					},295				);296			}297			Member::Field(FieldMember {298				name,299				params: Some(params),300				value,301				..302			}) => {303				let name = evaluate_field_name(context.clone(), name)?;304				if name.is_none() {305					continue;306				}307				let name = name.unwrap();308				new_members.insert(309					name.clone(),310					ObjMember {311						add: false,312						visibility: Visibility::Hidden,313						invoke: LazyBinding::Bindable(Rc::new(314							closure!(clone value, clone context_creator, clone params, clone name, |this, super_obj| {315								// TODO: Assert316								Ok(LazyVal::new_resolved(evaluate_method(317									context_creator.create(this, super_obj)?,318									name.clone(),319									params.clone(),320									value.clone(),321								)))322							}),323						)),324						location: value.1.clone(),325					},326				);327			}328			Member::BindStmt(_) => {}329			Member::AssertStmt(stmt) => {330				assertions.push(stmt.clone());331			}332		}333	}334	let this = ObjValue::new(context, None, Rc::new(new_members), Rc::new(assertions));335	future_this.fill(this.clone());336	Ok(this)337}338339pub fn evaluate_object(context: Context, object: &ObjBody) -> Result<ObjValue> {340	Ok(match object {341		ObjBody::MemberList(members) => evaluate_member_list_object(context, members)?,342		ObjBody::ObjComp(obj) => {343			let future_this = FutureWrapper::new();344			let mut new_members = FxHashMap::default();345			for (k, v) in evaluate_comp(346				context.clone(),347				&|ctx| {348					let new_bindings = FutureWrapper::new();349					let context_creator = ContextCreator(context.clone(), new_bindings.clone());350					let mut bindings: FxHashMap<IStr, LazyBinding> = FxHashMap::with_capacity_and_hasher(obj.pre_locals.len() + obj.post_locals.len(), BuildHasherDefault::default());351					for (n, b) in obj352						.pre_locals353						.iter()354						.chain(obj.post_locals.iter())355						.map(|b| evaluate_binding(b, context_creator.clone()))356					{357						bindings.insert(n, b);358					}359					new_bindings.fill(bindings.clone());360					let ctx = ctx.extend_unbound(bindings, None, None, None)?;361					let key = evaluate(ctx.clone(), &obj.key)?;362					let value = LazyBinding::Bindable(Rc::new(363						closure!(clone ctx, clone obj.value, |this, _super_obj| {364							Ok(LazyVal::new_resolved(evaluate(ctx.clone().extend(FxHashMap::default(), None, this, None), &value)?))365						}),366					));367368					Ok((key, value))369				},370				&obj.compspecs,371			)?372			.unwrap()373			{374				match k {375					Val::Null => {}376					Val::Str(n) => {377						new_members.insert(378							n,379							ObjMember {380								add: false,381								visibility: Visibility::Normal,382								invoke: v,383								location: obj.value.1.clone(),384							},385						);386					}387					v => throw!(FieldMustBeStringGot(v.value_type())),388				}389			}390391			let this = ObjValue::new(context, None, Rc::new(new_members), Rc::new(Vec::new()));392			future_this.fill(this.clone());393			this394		}395	})396}397398pub fn evaluate_apply(399	context: Context,400	value: &LocExpr,401	args: &ArgsDesc,402	loc: Option<&ExprLocation>,403	tailstrict: bool,404) -> Result<Val> {405	let value = evaluate(context.clone(), value)?;406	Ok(match value {407		Val::Func(f) => {408			let body = || f.evaluate(context, loc, args, tailstrict);409			if tailstrict {410				body()?411			} else {412				push(loc, || format!("function <{}> call", f.name()), body)?413			}414		}415		v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),416	})417}418419pub fn evaluate_assert(420	context: Context,421	assertion: &AssertStmt,422) -> Result<()> {423	let value = &assertion.0;424	let msg = &assertion.1;425	let assertion_result = push(426		value.1.as_ref(),427		|| "assertion condition".to_owned(),428		|| {429			evaluate(context.clone(), value)?430				.try_cast_bool("assertion condition should be of type `boolean`")431		},432	)?;433	if !assertion_result {434		push(435			value.1.as_ref(),436			|| "assertion failure".to_owned(),437			|| {438				if let Some(msg) = msg {439					throw!(AssertionFailed(evaluate(context, msg)?.to_string()?));440				} else {441					throw!(AssertionFailed(Val::Null.to_string()?));442				}443			},444		)?445	}446	Ok(())447}448449pub fn evaluate_named(context: Context, lexpr: &LocExpr, name: IStr) -> Result<Val> {450	use Expr::*;451	let LocExpr(expr, _loc) = lexpr;452	Ok(match &**expr {453		Function(params, body) => evaluate_method(context, name, params.clone(), body.clone()),454		_ => evaluate(context, lexpr)?,455	})456}457458pub fn evaluate(context: Context, expr: &LocExpr) -> Result<Val> {459	use Expr::*;460	let LocExpr(expr, loc) = expr;461	Ok(match &**expr {462		Literal(LiteralType::This) => {463			Val::Obj(context.this().clone().ok_or(CantUseSelfOutsideOfObject)?)464		}465		Literal(LiteralType::Super) => Val::Obj(466			context467				.super_obj()468				.clone()469				.ok_or(NoSuperFound)?470				.with_this(context.this().clone().unwrap()),471		),472		Literal(LiteralType::Dollar) => {473			Val::Obj(context.dollar().clone().ok_or(NoTopLevelObjectFound)?)474		}475		Literal(LiteralType::True) => Val::Bool(true),476		Literal(LiteralType::False) => Val::Bool(false),477		Literal(LiteralType::Null) => Val::Null,478		Parened(e) => evaluate(context, e)?,479		Str(v) => Val::Str(v.clone()),480		Num(v) => Val::new_checked_num(*v)?,481		BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, v1, *o, v2)?,482		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,483		Var(name) => push(484			loc.as_ref(),485			|| format!("variable <{}>", name),486			|| context.binding(name.clone())?.evaluate(),487		)?,488		Index(value, index) => {489			match (evaluate(context.clone(), value)?, evaluate(context, index)?) {490				(Val::Obj(v), Val::Str(s)) => {491					let sn = s.clone();492					push(493						loc.as_ref(),494						|| format!("field <{}> access", sn),495						|| {496							if let Some(v) = v.get(s.clone())? {497								Ok(v)498							} else if v.get("__intrinsic_namespace__".into())?.is_some() {499								Ok(Val::Func(Rc::new(FuncVal::Intrinsic(s))))500							} else {501								throw!(NoSuchField(s))502							}503						},504					)?505				}506				(Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(507					ValType::Obj,508					ValType::Str,509					n.value_type(),510				)),511512				(Val::Arr(v), Val::Num(n)) => {513					if n.fract() > f64::EPSILON {514						throw!(FractionalIndex)515					}516					v.get(n as usize)?517						.ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?518				}519				(Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),520				(Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(521					ValType::Arr,522					ValType::Num,523					n.value_type(),524				)),525526				(Val::Str(s), Val::Num(n)) => Val::Str(527					s.chars()528						.skip(n as usize)529						.take(1)530						.collect::<String>()531						.into(),532				),533				(Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(534					ValType::Str,535					ValType::Num,536					n.value_type(),537				)),538539				(v, _) => throw!(CantIndexInto(v.value_type())),540			}541		}542		LocalExpr(bindings, returned) => {543			let mut new_bindings: FxHashMap<IStr, LazyVal> = HashMap::with_capacity_and_hasher(544				bindings.len(),545				BuildHasherDefault::<FxHasher>::default(),546			);547			let future_context = Context::new_future();548			for b in bindings {549				new_bindings.insert(550					b.name.clone(),551					evaluate_binding_in_future(b, future_context.clone()),552				);553			}554			let context = context555				.extend_bound(new_bindings)556				.into_future(future_context);557			evaluate(context, &returned.clone())?558		}559		Arr(items) => {560			let mut out = Vec::with_capacity(items.len());561			for item in items {562				out.push(LazyVal::new(Box::new(563					closure!(clone context, clone item, || {564						evaluate(context.clone(), &item)565					}),566				)));567			}568			Val::Arr(out.into())569		}570		ArrComp(expr, comp_specs) => Val::Arr(571			// First comp_spec should be for_spec, so no "None" possible here572			evaluate_comp(context, &|ctx| evaluate(ctx, expr), comp_specs)?573				.unwrap()574				.into(),575		),576		Obj(body) => Val::Obj(evaluate_object(context, body)?),577		ObjExtend(s, t) => evaluate_add_op(578			&evaluate(context.clone(), s)?,579			&Val::Obj(evaluate_object(context, t)?),580		)?,581		Apply(value, args, tailstrict) => {582			evaluate_apply(context, value, args, loc.as_ref(), *tailstrict)?583		}584		Function(params, body) => {585			evaluate_method(context, "anonymous".into(), params.clone(), body.clone())586		}587		Intrinsic(name) => Val::Func(Rc::new(FuncVal::Intrinsic(name.clone()))),588		AssertExpr(assert, returned) => {589			evaluate_assert(context.clone(), &assert)?;590			evaluate(context, returned)?591		}592		ErrorStmt(e) => push(593			loc.as_ref(),594			|| "error statement".to_owned(),595			|| {596				throw!(RuntimeError(597					evaluate(context, e)?.try_cast_str("error text should be of type `string`")?,598				))599			},600		)?,601		IfElse {602			cond,603			cond_then,604			cond_else,605		} => {606			if push(607				loc.as_ref(),608				|| "if condition".to_owned(),609				|| evaluate(context.clone(), &cond.0)?.try_cast_bool("in if condition"),610			)? {611				evaluate(context, cond_then)?612			} else {613				match cond_else {614					Some(v) => evaluate(context, v)?,615					None => Val::Null,616				}617			}618		}619		Import(path) => {620			let mut tmp = loc621				.clone()622				.expect("imports cannot be used without loc_data")623				.0;624			let import_location = Rc::make_mut(&mut tmp);625			import_location.pop();626			push(627				loc.as_ref(),628				|| format!("import {:?}", path),629				|| with_state(|s| s.import_file(import_location, path)),630			)?631		}632		ImportStr(path) => {633			let mut tmp = loc634				.clone()635				.expect("imports cannot be used without loc_data")636				.0;637			let import_location = Rc::make_mut(&mut tmp);638			import_location.pop();639			Val::Str(with_state(|s| s.import_file_str(import_location, path))?)640		}641	})642}
after · crates/jrsonnet-evaluator/src/evaluate.rs
1use crate::{2	equals, error::Error::*, lazy_val, push, throw, with_state, ArrValue, Context, ContextCreator,3	FuncDesc, FuncVal, 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		(a, BinaryOpType::Eq, b) => Val::Bool(equals(&a, &b)?),151		(a, BinaryOpType::Neq, b) => Val::Bool(!equals(&a, &b)?),152153		(Val::Str(v1), BinaryOpType::Mul, Val::Num(v2)) => Val::Str(v1.repeat(*v2 as usize).into()),154155		// Bool X Bool156		(Val::Bool(a), BinaryOpType::And, Val::Bool(b)) => Val::Bool(*a && *b),157		(Val::Bool(a), BinaryOpType::Or, Val::Bool(b)) => Val::Bool(*a || *b),158159		// Str X Str160		(Val::Str(v1), BinaryOpType::Lt, Val::Str(v2)) => Val::Bool(v1 < v2),161		(Val::Str(v1), BinaryOpType::Gt, Val::Str(v2)) => Val::Bool(v1 > v2),162		(Val::Str(v1), BinaryOpType::Lte, Val::Str(v2)) => Val::Bool(v1 <= v2),163		(Val::Str(v1), BinaryOpType::Gte, Val::Str(v2)) => Val::Bool(v1 >= v2),164165		// Num X Num166		(Val::Num(v1), BinaryOpType::Mul, Val::Num(v2)) => Val::new_checked_num(v1 * v2)?,167		(Val::Num(v1), BinaryOpType::Div, Val::Num(v2)) => {168			if *v2 <= f64::EPSILON {169				throw!(DivisionByZero)170			}171			Val::new_checked_num(v1 / v2)?172		}173174		(Val::Num(v1), BinaryOpType::Sub, Val::Num(v2)) => Val::new_checked_num(v1 - v2)?,175176		(Val::Num(v1), BinaryOpType::Lt, Val::Num(v2)) => Val::Bool(v1 < v2),177		(Val::Num(v1), BinaryOpType::Gt, Val::Num(v2)) => Val::Bool(v1 > v2),178		(Val::Num(v1), BinaryOpType::Lte, Val::Num(v2)) => Val::Bool(v1 <= v2),179		(Val::Num(v1), BinaryOpType::Gte, Val::Num(v2)) => Val::Bool(v1 >= v2),180181		(Val::Num(v1), BinaryOpType::BitAnd, Val::Num(v2)) => {182			Val::Num(((*v1 as i32) & (*v2 as i32)) as f64)183		}184		(Val::Num(v1), BinaryOpType::BitOr, Val::Num(v2)) => {185			Val::Num(((*v1 as i32) | (*v2 as i32)) as f64)186		}187		(Val::Num(v1), BinaryOpType::BitXor, Val::Num(v2)) => {188			Val::Num(((*v1 as i32) ^ (*v2 as i32)) as f64)189		}190		(Val::Num(v1), BinaryOpType::Lhs, Val::Num(v2)) => {191			if *v2 < 0.0 {192				throw!(RuntimeError("shift by negative exponent".into()))193			}194			Val::Num(((*v1 as i32) << (*v2 as i32)) as f64)195		}196		(Val::Num(v1), BinaryOpType::Rhs, Val::Num(v2)) => {197			if *v2 < 0.0 {198				throw!(RuntimeError("shift by negative exponent".into()))199			}200			Val::Num(((*v1 as i32) >> (*v2 as i32)) as f64)201		}202203		_ => throw!(BinaryOperatorDoesNotOperateOnValues(204			op,205			a.value_type(),206			b.value_type(),207		)),208	})209}210211pub fn evaluate_comp(212	context: Context,213	specs: &[CompSpec],214	callback: &mut impl FnMut(Context) -> Result<()>,215) -> Result<()> {216	match specs.get(0) {217		None => callback(context)?,218		Some(CompSpec::IfSpec(IfSpecData(cond))) => {219			if evaluate(context.clone(), cond)?.try_cast_bool("if spec")? {220				evaluate_comp(context, &specs[1..], callback)?221			}222		}223		Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(context.clone(), expr)? {224			Val::Arr(list) => {225				for item in list.iter() {226					evaluate_comp(227						context.clone().with_var(var.clone(), item?.clone()),228						&specs[1..],229						callback,230					)?231				}232			}233			_ => throw!(InComprehensionCanOnlyIterateOverArray),234		},235	}236	Ok(())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	let mut assertions = Vec::new();261	for member in members.iter() {262		match member {263			Member::Field(FieldMember {264				name,265				plus,266				params: None,267				visibility,268				value,269			}) => {270				let name = evaluate_field_name(context.clone(), name)?;271				if name.is_none() {272					continue;273				}274				let name = name.unwrap();275				new_members.insert(276					name.clone(),277					ObjMember {278						add: *plus,279						visibility: *visibility,280						invoke: LazyBinding::Bindable(Rc::new(281							closure!(clone name, clone value, clone context_creator, |this, super_obj| {282								Ok(LazyVal::new_resolved(evaluate_named(283									context_creator.create(this, super_obj)?,284									&value,285									name.clone(),286								)?))287							}),288						)),289						location: value.1.clone(),290					},291				);292			}293			Member::Field(FieldMember {294				name,295				params: Some(params),296				value,297				..298			}) => {299				let name = evaluate_field_name(context.clone(), name)?;300				if name.is_none() {301					continue;302				}303				let name = name.unwrap();304				new_members.insert(305					name.clone(),306					ObjMember {307						add: false,308						visibility: Visibility::Hidden,309						invoke: LazyBinding::Bindable(Rc::new(310							closure!(clone value, clone context_creator, clone params, clone name, |this, super_obj| {311								// TODO: Assert312								Ok(LazyVal::new_resolved(evaluate_method(313									context_creator.create(this, super_obj)?,314									name.clone(),315									params.clone(),316									value.clone(),317								)))318							}),319						)),320						location: value.1.clone(),321					},322				);323			}324			Member::BindStmt(_) => {}325			Member::AssertStmt(stmt) => {326				assertions.push(stmt.clone());327			}328		}329	}330	let this = ObjValue::new(context, None, Rc::new(new_members), Rc::new(assertions));331	future_this.fill(this.clone());332	Ok(this)333}334335pub fn evaluate_object(context: Context, object: &ObjBody) -> Result<ObjValue> {336	Ok(match object {337		ObjBody::MemberList(members) => evaluate_member_list_object(context, members)?,338		ObjBody::ObjComp(obj) => {339			let future_this = FutureWrapper::new();340			let mut new_members = FxHashMap::default();341			evaluate_comp(context.clone(), &obj.compspecs, &mut |ctx| {342				let new_bindings = FutureWrapper::new();343				let context_creator = ContextCreator(context.clone(), new_bindings.clone());344				let mut bindings: FxHashMap<IStr, LazyBinding> =345					FxHashMap::with_capacity_and_hasher(346						obj.pre_locals.len() + obj.post_locals.len(),347						BuildHasherDefault::default(),348					);349				for (n, b) in obj350					.pre_locals351					.iter()352					.chain(obj.post_locals.iter())353					.map(|b| evaluate_binding(b, context_creator.clone()))354				{355					bindings.insert(n, b);356				}357				new_bindings.fill(bindings.clone());358				let ctx = ctx.extend_unbound(bindings, None, None, None)?;359				let key = evaluate(ctx.clone(), &obj.key)?;360361				match key {362					Val::Null => {}363					Val::Str(n) => {364						new_members.insert(365							n,366							ObjMember {367								add: false,368								visibility: Visibility::Normal,369								invoke: LazyBinding::Bindable(Rc::new(370									closure!(clone ctx, clone obj.value, |this, _super_obj| {371										Ok(LazyVal::new_resolved(evaluate(ctx.clone().extend(FxHashMap::default(), None, this, None), &value)?))372									}),373								)),374								location: obj.value.1.clone(),375							},376						);377					}378					v => throw!(FieldMustBeStringGot(v.value_type())),379				}380381				Ok(())382			})?;383384			let this = ObjValue::new(context, None, Rc::new(new_members), Rc::new(Vec::new()));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_assert(context: Context, assertion: &AssertStmt) -> Result<()> {413	let value = &assertion.0;414	let msg = &assertion.1;415	let assertion_result = push(416		value.1.as_ref(),417		|| "assertion condition".to_owned(),418		|| {419			evaluate(context.clone(), value)?420				.try_cast_bool("assertion condition should be of type `boolean`")421		},422	)?;423	if !assertion_result {424		push(425			value.1.as_ref(),426			|| "assertion failure".to_owned(),427			|| {428				if let Some(msg) = msg {429					throw!(AssertionFailed(evaluate(context, msg)?.to_string()?));430				} else {431					throw!(AssertionFailed(Val::Null.to_string()?));432				}433			},434		)?435	}436	Ok(())437}438439pub fn evaluate_named(context: Context, lexpr: &LocExpr, name: IStr) -> Result<Val> {440	use Expr::*;441	let LocExpr(expr, _loc) = lexpr;442	Ok(match &**expr {443		Function(params, body) => evaluate_method(context, name, params.clone(), body.clone()),444		_ => evaluate(context, lexpr)?,445	})446}447448pub fn evaluate(context: Context, expr: &LocExpr) -> Result<Val> {449	use Expr::*;450	let LocExpr(expr, loc) = expr;451	Ok(match &**expr {452		Literal(LiteralType::This) => {453			Val::Obj(context.this().clone().ok_or(CantUseSelfOutsideOfObject)?)454		}455		Literal(LiteralType::Super) => Val::Obj(456			context457				.super_obj()458				.clone()459				.ok_or(NoSuperFound)?460				.with_this(context.this().clone().unwrap()),461		),462		Literal(LiteralType::Dollar) => {463			Val::Obj(context.dollar().clone().ok_or(NoTopLevelObjectFound)?)464		}465		Literal(LiteralType::True) => Val::Bool(true),466		Literal(LiteralType::False) => Val::Bool(false),467		Literal(LiteralType::Null) => Val::Null,468		Parened(e) => evaluate(context, e)?,469		Str(v) => Val::Str(v.clone()),470		Num(v) => Val::new_checked_num(*v)?,471		BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, v1, *o, v2)?,472		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,473		Var(name) => push(474			loc.as_ref(),475			|| format!("variable <{}>", name),476			|| context.binding(name.clone())?.evaluate(),477		)?,478		Index(value, index) => {479			match (evaluate(context.clone(), value)?, evaluate(context, index)?) {480				(Val::Obj(v), Val::Str(s)) => {481					let sn = s.clone();482					push(483						loc.as_ref(),484						|| format!("field <{}> access", sn),485						|| {486							if let Some(v) = v.get(s.clone())? {487								Ok(v)488							} else if v.get("__intrinsic_namespace__".into())?.is_some() {489								Ok(Val::Func(Rc::new(FuncVal::Intrinsic(s))))490							} else {491								throw!(NoSuchField(s))492							}493						},494					)?495				}496				(Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(497					ValType::Obj,498					ValType::Str,499					n.value_type(),500				)),501502				(Val::Arr(v), Val::Num(n)) => {503					if n.fract() > f64::EPSILON {504						throw!(FractionalIndex)505					}506					v.get(n as usize)?507						.ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?508				}509				(Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),510				(Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(511					ValType::Arr,512					ValType::Num,513					n.value_type(),514				)),515516				(Val::Str(s), Val::Num(n)) => Val::Str(517					s.chars()518						.skip(n as usize)519						.take(1)520						.collect::<String>()521						.into(),522				),523				(Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(524					ValType::Str,525					ValType::Num,526					n.value_type(),527				)),528529				(v, _) => throw!(CantIndexInto(v.value_type())),530			}531		}532		LocalExpr(bindings, returned) => {533			let mut new_bindings: FxHashMap<IStr, LazyVal> = HashMap::with_capacity_and_hasher(534				bindings.len(),535				BuildHasherDefault::<FxHasher>::default(),536			);537			let future_context = Context::new_future();538			for b in bindings {539				new_bindings.insert(540					b.name.clone(),541					evaluate_binding_in_future(b, future_context.clone()),542				);543			}544			let context = context545				.extend_bound(new_bindings)546				.into_future(future_context);547			evaluate(context, &returned.clone())?548		}549		Arr(items) => {550			let mut out = Vec::with_capacity(items.len());551			for item in items {552				out.push(LazyVal::new(Box::new(553					closure!(clone context, clone item, || {554						evaluate(context.clone(), &item)555					}),556				)));557			}558			Val::Arr(out.into())559		}560		ArrComp(expr, comp_specs) => {561			let mut out = Vec::new();562			evaluate_comp(context, comp_specs, &mut |ctx| {563				out.push(evaluate(ctx, expr)?);564				Ok(())565			})?;566			Val::Arr(ArrValue::Eager(Rc::new(out)))567		}568		Obj(body) => Val::Obj(evaluate_object(context, body)?),569		ObjExtend(s, t) => evaluate_add_op(570			&evaluate(context.clone(), s)?,571			&Val::Obj(evaluate_object(context, t)?),572		)?,573		Apply(value, args, tailstrict) => {574			evaluate_apply(context, value, args, loc.as_ref(), *tailstrict)?575		}576		Function(params, body) => {577			evaluate_method(context, "anonymous".into(), params.clone(), body.clone())578		}579		Intrinsic(name) => Val::Func(Rc::new(FuncVal::Intrinsic(name.clone()))),580		AssertExpr(assert, returned) => {581			evaluate_assert(context.clone(), &assert)?;582			evaluate(context, returned)?583		}584		ErrorStmt(e) => push(585			loc.as_ref(),586			|| "error statement".to_owned(),587			|| {588				throw!(RuntimeError(589					evaluate(context, e)?.try_cast_str("error text should be of type `string`")?,590				))591			},592		)?,593		IfElse {594			cond,595			cond_then,596			cond_else,597		} => {598			if push(599				loc.as_ref(),600				|| "if condition".to_owned(),601				|| evaluate(context.clone(), &cond.0)?.try_cast_bool("in if condition"),602			)? {603				evaluate(context, cond_then)?604			} else {605				match cond_else {606					Some(v) => evaluate(context, v)?,607					None => Val::Null,608				}609			}610		}611		Import(path) => {612			let mut tmp = loc613				.clone()614				.expect("imports cannot be used without loc_data")615				.0;616			let import_location = Rc::make_mut(&mut tmp);617			import_location.pop();618			push(619				loc.as_ref(),620				|| format!("import {:?}", path),621				|| with_state(|s| s.import_file(import_location, path)),622			)?623		}624		ImportStr(path) => {625			let mut tmp = loc626				.clone()627				.expect("imports cannot be used without loc_data")628				.0;629			let import_location = Rc::make_mut(&mut tmp);630			import_location.pop();631			Val::Str(with_state(|s| s.import_file_str(import_location, path))?)632		}633	})634}