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

difftreelog

fixup! fix formatting

Yaroslav Bolyukin2024-05-19parent: #afe1286.patch.diff
in: master

3 files changed

modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -1,5 +1,8 @@
 use std::{
-	cmp::Ordering, convert::Infallible, fmt::{Debug, Display}, path::PathBuf
+	cmp::Ordering,
+	convert::Infallible,
+	fmt::{Debug, Display},
+	path::PathBuf,
 };
 
 use jrsonnet_gcmodule::Trace;
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/evaluate/mod.rs
1use std::rc::Rc;23use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_parser::{6	ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, FieldMember, FieldName,7	ForSpecData, IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc,8};9use jrsonnet_types::ValType;1011use self::destructure::destruct;12use crate::{13	arr::ArrValue,14	bail,15	destructure::evaluate_dest,16	error::{suggest_object_fields, ErrorKind::*},17	evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},18	function::{CallLocation, FuncDesc, FuncVal},19	typed::Typed,20	val::{CachedUnbound, IndexableVal, NumValue, StrValue, Thunk, ThunkValue},21	Context, Error, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result,22	ResultExt, State, Unbound, Val,23};24pub mod destructure;25pub mod operator;2627pub fn evaluate_trivial(expr: &LocExpr) -> Option<Val> {28	fn is_trivial(expr: &LocExpr) -> bool {29		match &*expr.0 {30			Expr::Str(_)31			| Expr::Num(_)32			| Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,33			Expr::Arr(a) => a.iter().all(is_trivial),34			Expr::Parened(e) => is_trivial(e),35			_ => false,36		}37	}38	Some(match &*expr.0 {39		Expr::Str(s) => Val::string(s.clone()),40		Expr::Num(n) => Val::Num(NumValue::new(*n).expect("parser will not allow non-finite values")),41		Expr::Literal(LiteralType::False) => Val::Bool(false),42		Expr::Literal(LiteralType::True) => Val::Bool(true),43		Expr::Literal(LiteralType::Null) => Val::Null,44		Expr::Arr(n) => {45			if n.iter().any(|e| !is_trivial(e)) {46				return None;47			}48			Val::Arr(ArrValue::eager(49				n.iter()50					.map(evaluate_trivial)51					.map(|e| e.expect("checked trivial"))52					.collect(),53			))54		}55		Expr::Parened(e) => evaluate_trivial(e)?,56		_ => return None,57	})58}5960pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {61	Val::Func(FuncVal::Normal(Cc::new(FuncDesc {62		name,63		ctx,64		params,65		body,66	})))67}6869pub fn evaluate_field_name(ctx: Context, field_name: &FieldName) -> Result<Option<IStr>> {70	Ok(match field_name {71		FieldName::Fixed(n) => Some(n.clone()),72		FieldName::Dyn(expr) => State::push(73			CallLocation::new(&expr.1),74			|| "evaluating field name".to_string(),75			|| {76				let value = evaluate(ctx, expr)?;77				if matches!(value, Val::Null) {78					Ok(None)79				} else {80					Ok(Some(IStr::from_untyped(value)?))81				}82			},83		)?,84	})85}8687pub fn evaluate_comp(88	ctx: Context,89	specs: &[CompSpec],90	callback: &mut impl FnMut(Context) -> Result<()>,91) -> Result<()> {92	match specs.first() {93		None => callback(ctx)?,94		Some(CompSpec::IfSpec(IfSpecData(cond))) => {95			if bool::from_untyped(evaluate(ctx.clone(), cond)?)? {96				evaluate_comp(ctx, &specs[1..], callback)?;97			}98		}99		Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(ctx.clone(), expr)? {100			Val::Arr(list) => {101				for item in list.iter_lazy() {102					let fctx = Pending::new();103					let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());104					destruct(var, item, fctx.clone(), &mut new_bindings)?;105					let ctx = ctx106						.clone()107						.extend(new_bindings, None, None, None)108						.into_future(fctx);109110					evaluate_comp(ctx, &specs[1..], callback)?;111				}112			}113			#[cfg(feature = "exp-object-iteration")]114			Val::Obj(obj) => {115				for field in obj.fields(116					// TODO: Should there be ability to preserve iteration order?117					#[cfg(feature = "exp-preserve-order")]118					false,119				) {120					#[derive(Trace)]121					struct ObjectFieldThunk {122						obj: ObjValue,123						field: IStr,124					}125					impl ThunkValue for ObjectFieldThunk {126						type Output = Val;127128						fn get(self: Box<Self>) -> Result<Self::Output> {129							self.obj.get(self.field).transpose().expect(130								"field exists, as field name was obtained from object.fields()",131							)132						}133					}134135					let fctx = Pending::new();136					let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());137					let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(vec![138						Thunk::evaluated(Val::string(field.clone())),139						Thunk::new(ObjectFieldThunk {140							field: field.clone(),141							obj: obj.clone(),142						}),143					])));144					destruct(var, value, fctx.clone(), &mut new_bindings)?;145					let ctx = ctx146						.clone()147						.extend(new_bindings, None, None, None)148						.into_future(fctx);149150					evaluate_comp(ctx, &specs[1..], callback)?;151				}152			}153			_ => bail!(InComprehensionCanOnlyIterateOverArray),154		},155	}156	Ok(())157}158159trait CloneableUnbound<T>: Unbound<Bound = T> + Clone {}160impl<V, T> CloneableUnbound<T> for V where V: Unbound<Bound = T> + Clone {}161162fn evaluate_object_locals(163	fctx: Pending<Context>,164	locals: Rc<Vec<BindSpec>>,165) -> impl CloneableUnbound<Context> {166	#[derive(Trace, Clone)]167	struct UnboundLocals {168		fctx: Pending<Context>,169		locals: Rc<Vec<BindSpec>>,170	}171	impl Unbound for UnboundLocals {172		type Bound = Context;173174		fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Context> {175			let fctx = Context::new_future();176			let mut new_bindings =177				GcHashMap::with_capacity(self.locals.iter().map(BindSpec::capacity_hint).sum());178			for b in self.locals.iter() {179				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;180			}181182			let ctx = self.fctx.unwrap();183			let new_dollar = ctx.dollar().cloned().or_else(|| this.clone());184185			let ctx = ctx186				.extend(new_bindings, new_dollar, sup, this)187				.into_future(fctx);188189			Ok(ctx)190		}191	}192193	UnboundLocals { fctx, locals }194}195196pub fn evaluate_field_member<B: Unbound<Bound = Context> + Clone>(197	builder: &mut ObjValueBuilder,198	ctx: Context,199	uctx: B,200	field: &FieldMember,201) -> Result<()> {202	let name = evaluate_field_name(ctx, &field.name)?;203	let Some(name) = name else {204		return Ok(());205	};206207	match field {208		FieldMember {209			plus,210			params: None,211			visibility,212			value,213			..214		} => {215			#[derive(Trace)]216			struct UnboundValue<B: Trace> {217				uctx: B,218				value: LocExpr,219				name: IStr,220			}221			impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {222				type Bound = Val;223				fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {224					evaluate_named(self.uctx.bind(sup, this)?, &self.value, self.name.clone())225				}226			}227228			builder229				.field(name.clone())230				.with_add(*plus)231				.with_visibility(*visibility)232				.with_location(value.1.clone())233				.bindable(UnboundValue {234					uctx,235					value: value.clone(),236					name,237				})?;238		}239		FieldMember {240			params: Some(params),241			visibility,242			value,243			..244		} => {245			#[derive(Trace)]246			struct UnboundMethod<B: Trace> {247				uctx: B,248				value: LocExpr,249				params: ParamsDesc,250				name: IStr,251			}252			impl<B: Unbound<Bound = Context>> Unbound for UnboundMethod<B> {253				type Bound = Val;254				fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {255					Ok(evaluate_method(256						self.uctx.bind(sup, this)?,257						self.name.clone(),258						self.params.clone(),259						self.value.clone(),260					))261				}262			}263264			builder265				.field(name.clone())266				.with_visibility(*visibility)267				.with_location(value.1.clone())268				.bindable(UnboundMethod {269					uctx,270					value: value.clone(),271					params: params.clone(),272					name,273				})?;274		}275	}276	Ok(())277}278279#[allow(clippy::too_many_lines)]280pub fn evaluate_member_list_object(ctx: Context, members: &[Member]) -> Result<ObjValue> {281	let mut builder = ObjValueBuilder::new();282	let locals = Rc::new(283		members284			.iter()285			.filter_map(|m| match m {286				Member::BindStmt(bind) => Some(bind.clone()),287				_ => None,288			})289			.collect::<Vec<_>>(),290	);291292	let fctx = Context::new_future();293294	// We have single context for all fields, so we can cache binds295	let uctx = CachedUnbound::new(evaluate_object_locals(fctx.clone(), locals));296297	for member in members {298		match member {299			Member::Field(field) => {300				evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;301			}302			Member::AssertStmt(stmt) => {303				#[derive(Trace)]304				struct ObjectAssert<B: Trace> {305					uctx: B,306					assert: AssertStmt,307				}308				impl<B: Unbound<Bound = Context>> ObjectAssertion for ObjectAssert<B> {309					fn run(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<()> {310						let ctx = self.uctx.bind(sup, this)?;311						evaluate_assert(ctx, &self.assert)312					}313				}314				builder.assert(ObjectAssert {315					uctx: uctx.clone(),316					assert: stmt.clone(),317				});318			}319			Member::BindStmt(_) => {320				// Already handled321			}322		}323	}324	let this = builder.build();325	fctx.fill(ctx.extend(GcHashMap::new(), None, None, Some(this.clone())));326	Ok(this)327}328329pub fn evaluate_object(ctx: Context, object: &ObjBody) -> Result<ObjValue> {330	Ok(match object {331		ObjBody::MemberList(members) => evaluate_member_list_object(ctx, members)?,332		ObjBody::ObjComp(obj) => {333			let mut builder = ObjValueBuilder::new();334			let locals = Rc::new(335				obj.pre_locals336					.iter()337					.chain(obj.post_locals.iter())338					.cloned()339					.collect::<Vec<_>>(),340			);341			let mut ctxs = vec![];342			evaluate_comp(ctx, &obj.compspecs, &mut |ctx| {343				let fctx = Context::new_future();344				ctxs.push((ctx.clone(), fctx.clone()));345				let uctx = evaluate_object_locals(fctx, locals.clone());346347				evaluate_field_member(&mut builder, ctx, uctx, &obj.field)348			})?;349350			let this = builder.build();351			for (ctx, fctx) in ctxs {352				let _ctx = ctx353					.extend(GcHashMap::new(), None, None, Some(this.clone()))354					.into_future(fctx);355			}356			this357		}358	})359}360361pub fn evaluate_apply(362	ctx: Context,363	value: &LocExpr,364	args: &ArgsDesc,365	loc: CallLocation<'_>,366	tailstrict: bool,367) -> Result<Val> {368	let value = evaluate(ctx.clone(), value)?;369	Ok(match value {370		Val::Func(f) => {371			let body = || f.evaluate(ctx, loc, args, tailstrict);372			if tailstrict {373				body()?374			} else {375				State::push(loc, || format!("function <{}> call", f.name()), body)?376			}377		}378		v => bail!(OnlyFunctionsCanBeCalledGot(v.value_type())),379	})380}381382pub fn evaluate_assert(ctx: Context, assertion: &AssertStmt) -> Result<()> {383	let value = &assertion.0;384	let msg = &assertion.1;385	let assertion_result = State::push(386		CallLocation::new(&value.1),387		|| "assertion condition".to_owned(),388		|| bool::from_untyped(evaluate(ctx.clone(), value)?),389	)?;390	if !assertion_result {391		State::push(392			CallLocation::new(&value.1),393			|| "assertion failure".to_owned(),394			|| {395				if let Some(msg) = msg {396					bail!(AssertionFailed(evaluate(ctx, msg)?.to_string()?));397				}398				bail!(AssertionFailed(Val::Null.to_string()?));399			},400		)?;401	}402	Ok(())403}404405pub fn evaluate_named(ctx: Context, expr: &LocExpr, name: IStr) -> Result<Val> {406	use Expr::*;407	let LocExpr(raw_expr, _loc) = expr;408	Ok(match &**raw_expr {409		Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),410		_ => evaluate(ctx, expr)?,411	})412}413414#[allow(clippy::too_many_lines)]415pub fn evaluate(ctx: Context, expr: &LocExpr) -> Result<Val> {416	use Expr::*;417418	if let Some(trivial) = evaluate_trivial(expr) {419		return Ok(trivial);420	}421	let LocExpr(expr, loc) = expr;422	Ok(match &**expr {423		Literal(LiteralType::This) => {424			Val::Obj(ctx.this().ok_or(CantUseSelfOutsideOfObject)?.clone())425		}426		Literal(LiteralType::Super) => Val::Obj(427			ctx.super_obj().ok_or(NoSuperFound)?.with_this(428				ctx.this()429					.expect("if super exists - then this should too")430					.clone(),431			),432		),433		Literal(LiteralType::Dollar) => {434			Val::Obj(ctx.dollar().ok_or(NoTopLevelObjectFound)?.clone())435		}436		Literal(LiteralType::True) => Val::Bool(true),437		Literal(LiteralType::False) => Val::Bool(false),438		Literal(LiteralType::Null) => Val::Null,439		Parened(e) => evaluate(ctx, e)?,440		Str(v) => Val::string(v.clone()),441		Num(v) => Val::try_num(*v)?,442		// I have tried to remove special behavior from super by implementing standalone-super443		// expresion, but looks like this case still needs special treatment.444		//445		// Note that other jsonnet implementations will fail on `if value in (super)` expression,446		// because the standalone super literal is not supported, that is because in other447		// implementations `in super` treated differently from in `smth_else`.448		BinaryOp(field, BinaryOpType::In, e)449			if matches!(&*e.0, Expr::Literal(LiteralType::Super)) =>450		{451			let Some(super_obj) = ctx.super_obj() else {452				return Ok(Val::Bool(false));453			};454			let field = evaluate(ctx.clone(), field)?;455			Val::Bool(super_obj.has_field_ex(field.to_string()?, true))456		}457		BinaryOp(v1, o, v2) => evaluate_binary_op_special(ctx, v1, *o, v2)?,458		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,459		Var(name) => State::push(460			CallLocation::new(loc),461			|| format!("variable <{name}> access"),462			|| ctx.binding(name.clone())?.evaluate(),463		)?,464		Index { indexable, parts } => {465			let mut parts = parts.iter();466			let mut indexable = match &indexable {467				// Cheaper to execute than creating object with overriden `this`468				LocExpr(v, _) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {469					let part = parts.next().expect("at least part should exist");470					let Some(super_obj) = ctx.super_obj() else {471						#[cfg(feature = "exp-null-coaelse")]472						if part.null_coaelse {473							return Ok(Val::Null);474						}475						bail!(NoSuperFound)476					};477					let name = evaluate(ctx.clone(), &part.value)?;478479					let Val::Str(name) = name else {480						bail!(ValueIndexMustBeTypeGot(481							ValType::Obj,482							ValType::Str,483							name.value_type(),484						))485					};486487					let this = ctx488						.this()489						.expect("no this found, while super present, should not happen");490					let name = name.into_flat();491					match super_obj492						.get_for(name.clone(), this.clone())493						.with_description_src(&part.value, || format!("field <{name}> access"))?494					{495						Some(v) => v,496						#[cfg(feature = "exp-null-coaelse")]497						None if part.null_coaelse => return Ok(Val::Null),498						None => {499							let suggestions = suggest_object_fields(super_obj, name.clone());500501							bail!(NoSuchField(name, suggestions))502						}503					}504				}505				e => evaluate(ctx.clone(), e)?,506			};507508			for part in parts {509				indexable = match (indexable, evaluate(ctx.clone(), &part.value)?) {510					(Val::Obj(v), Val::Str(key)) => match v511						.get(key.clone().into_flat())512						.with_description_src(&part.value, || format!("field <{key}> access"))?513					{514						Some(v) => v,515						#[cfg(feature = "exp-null-coaelse")]516						None if part.null_coaelse => return Ok(Val::Null),517						None => {518							let suggestions = suggest_object_fields(&v, key.clone().into_flat());519520							return Err(Error::from(NoSuchField(521								key.clone().into_flat(),522								suggestions,523							)))524							.with_description_src(&part.value, || format!("field <{key}> access"));525						}526					},527					(Val::Obj(_), n) => bail!(ValueIndexMustBeTypeGot(528						ValType::Obj,529						ValType::Str,530						n.value_type(),531					)),532					(Val::Arr(v), Val::Num(n)) => {533						let n = n.get();534						if n.fract() > f64::EPSILON {535							bail!(FractionalIndex)536						}537						if n < 0.0 {538							bail!(ArrayBoundsError(n as isize, v.len()));539						}540						v.get(n as usize)?541							.ok_or_else(|| ArrayBoundsError(n as isize, v.len()))?542					}543					(Val::Arr(_), Val::Str(n)) => {544						bail!(AttemptedIndexAnArrayWithString(n.into_flat()))545					}546					(Val::Arr(_), n) => bail!(ValueIndexMustBeTypeGot(547						ValType::Arr,548						ValType::Num,549						n.value_type(),550					)),551552					(Val::Str(s), Val::Num(n)) => Val::Str({553						let v: IStr = s554							.clone()555							.into_flat()556							.chars()557							.skip(n.get() as usize)558							.take(1)559							.collect::<String>()560							.into();561						if v.is_empty() {562							let size = s.into_flat().chars().count();563							bail!(StringBoundsError(n.get() as usize, size))564						}565						StrValue::Flat(v)566					}),567					(Val::Str(_), n) => bail!(ValueIndexMustBeTypeGot(568						ValType::Str,569						ValType::Num,570						n.value_type(),571					)),572					#[cfg(feature = "exp-null-coaelse")]573					(Val::Null, _) if part.null_coaelse => return Ok(Val::Null),574					(v, _) => bail!(CantIndexInto(v.value_type())),575				};576			}577			indexable578		}579		LocalExpr(bindings, returned) => {580			let mut new_bindings: GcHashMap<IStr, Thunk<Val>> =581				GcHashMap::with_capacity(bindings.iter().map(BindSpec::capacity_hint).sum());582			let fctx = Context::new_future();583			for b in bindings {584				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;585			}586			let ctx = ctx.extend(new_bindings, None, None, None).into_future(fctx);587			evaluate(ctx, &returned.clone())?588		}589		Arr(items) => {590			if items.is_empty() {591				Val::Arr(ArrValue::empty())592			} else if items.len() == 1 {593				#[derive(Trace)]594				struct ArrayElement {595					ctx: Context,596					item: LocExpr,597				}598				impl ThunkValue for ArrayElement {599					type Output = Val;600					fn get(self: Box<Self>) -> Result<Val> {601						evaluate(self.ctx, &self.item)602					}603				}604				Val::Arr(ArrValue::lazy(vec![Thunk::new(ArrayElement {605					ctx,606					item: items[0].clone(),607				})]))608			} else {609				Val::Arr(ArrValue::expr(ctx, items.iter().cloned()))610			}611		}612		ArrComp(expr, comp_specs) => {613			let mut out = Vec::new();614			evaluate_comp(ctx, comp_specs, &mut |ctx| {615				#[derive(Trace)]616				struct EvaluateThunk {617					ctx: Context,618					expr: LocExpr,619				}620				impl ThunkValue for EvaluateThunk {621					type Output = Val;622					fn get(self: Box<Self>) -> Result<Val> {623						evaluate(self.ctx, &self.expr)624					}625				}626				out.push(Thunk::new(EvaluateThunk {627					ctx,628					expr: expr.clone(),629				}));630				Ok(())631			})?;632			Val::Arr(ArrValue::lazy(out))633		}634		Obj(body) => Val::Obj(evaluate_object(ctx, body)?),635		ObjExtend(a, b) => evaluate_add_op(636			&evaluate(ctx.clone(), a)?,637			&Val::Obj(evaluate_object(ctx, b)?),638		)?,639		Apply(value, args, tailstrict) => {640			evaluate_apply(ctx, value, args, CallLocation::new(loc), *tailstrict)?641		}642		Function(params, body) => {643			evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())644		}645		AssertExpr(assert, returned) => {646			evaluate_assert(ctx.clone(), assert)?;647			evaluate(ctx, returned)?648		}649		ErrorStmt(e) => State::push(650			CallLocation::new(loc),651			|| "error statement".to_owned(),652			|| bail!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),653		)?,654		IfElse {655			cond,656			cond_then,657			cond_else,658		} => {659			if State::push(660				CallLocation::new(loc),661				|| "if condition".to_owned(),662				|| bool::from_untyped(evaluate(ctx.clone(), &cond.0)?),663			)? {664				evaluate(ctx, cond_then)?665			} else {666				match cond_else {667					Some(v) => evaluate(ctx, v)?,668					None => Val::Null,669				}670			}671		}672		Slice(value, desc) => {673			fn parse_idx<T: Typed>(674				loc: CallLocation<'_>,675				ctx: &Context,676				expr: Option<&LocExpr>,677				desc: &'static str,678			) -> Result<Option<T>> {679				if let Some(value) = expr {680					Ok(Some(State::push(681						loc,682						|| format!("slice {desc}"),683						|| T::from_untyped(evaluate(ctx.clone(), value)?),684					)?))685				} else {686					Ok(None)687				}688			}689690			let indexable = evaluate(ctx.clone(), value)?;691			let loc = CallLocation::new(loc);692693			let start = parse_idx(loc, &ctx, desc.start.as_ref(), "start")?;694			let end = parse_idx(loc, &ctx, desc.end.as_ref(), "end")?;695			let step = parse_idx(loc, &ctx, desc.step.as_ref(), "step")?;696697			IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?698		}699		i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {700			let Expr::Str(path) = &*path.0 else {701				bail!("computed imports are not supported")702			};703			let tmp = loc.clone().0;704			let s = ctx.state();705			let resolved_path = s.resolve_from(tmp.source_path(), path as &str)?;706			match i {707				Import(_) => State::push(708					CallLocation::new(loc),709					|| format!("import {:?}", path.clone()),710					|| s.import_resolved(resolved_path),711				)?,712				ImportStr(_) => Val::string(s.import_resolved_str(resolved_path)?),713				ImportBin(_) => Val::Arr(ArrValue::bytes(s.import_resolved_bin(resolved_path)?)),714				_ => unreachable!(),715			}716		}717	})718}
after · crates/jrsonnet-evaluator/src/evaluate/mod.rs
1use std::rc::Rc;23use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_parser::{6	ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, FieldMember, FieldName,7	ForSpecData, IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc,8};9use jrsonnet_types::ValType;1011use self::destructure::destruct;12use crate::{13	arr::ArrValue,14	bail,15	destructure::evaluate_dest,16	error::{suggest_object_fields, ErrorKind::*},17	evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},18	function::{CallLocation, FuncDesc, FuncVal},19	typed::Typed,20	val::{CachedUnbound, IndexableVal, NumValue, StrValue, Thunk, ThunkValue},21	Context, Error, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result,22	ResultExt, State, Unbound, Val,23};24pub mod destructure;25pub mod operator;2627pub fn evaluate_trivial(expr: &LocExpr) -> Option<Val> {28	fn is_trivial(expr: &LocExpr) -> bool {29		match &*expr.0 {30			Expr::Str(_)31			| Expr::Num(_)32			| Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,33			Expr::Arr(a) => a.iter().all(is_trivial),34			Expr::Parened(e) => is_trivial(e),35			_ => false,36		}37	}38	Some(match &*expr.0 {39		Expr::Str(s) => Val::string(s.clone()),40		Expr::Num(n) => {41			Val::Num(NumValue::new(*n).expect("parser will not allow non-finite values"))42		}43		Expr::Literal(LiteralType::False) => Val::Bool(false),44		Expr::Literal(LiteralType::True) => Val::Bool(true),45		Expr::Literal(LiteralType::Null) => Val::Null,46		Expr::Arr(n) => {47			if n.iter().any(|e| !is_trivial(e)) {48				return None;49			}50			Val::Arr(ArrValue::eager(51				n.iter()52					.map(evaluate_trivial)53					.map(|e| e.expect("checked trivial"))54					.collect(),55			))56		}57		Expr::Parened(e) => evaluate_trivial(e)?,58		_ => return None,59	})60}6162pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {63	Val::Func(FuncVal::Normal(Cc::new(FuncDesc {64		name,65		ctx,66		params,67		body,68	})))69}7071pub fn evaluate_field_name(ctx: Context, field_name: &FieldName) -> Result<Option<IStr>> {72	Ok(match field_name {73		FieldName::Fixed(n) => Some(n.clone()),74		FieldName::Dyn(expr) => State::push(75			CallLocation::new(&expr.1),76			|| "evaluating field name".to_string(),77			|| {78				let value = evaluate(ctx, expr)?;79				if matches!(value, Val::Null) {80					Ok(None)81				} else {82					Ok(Some(IStr::from_untyped(value)?))83				}84			},85		)?,86	})87}8889pub fn evaluate_comp(90	ctx: Context,91	specs: &[CompSpec],92	callback: &mut impl FnMut(Context) -> Result<()>,93) -> Result<()> {94	match specs.first() {95		None => callback(ctx)?,96		Some(CompSpec::IfSpec(IfSpecData(cond))) => {97			if bool::from_untyped(evaluate(ctx.clone(), cond)?)? {98				evaluate_comp(ctx, &specs[1..], callback)?;99			}100		}101		Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(ctx.clone(), expr)? {102			Val::Arr(list) => {103				for item in list.iter_lazy() {104					let fctx = Pending::new();105					let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());106					destruct(var, item, fctx.clone(), &mut new_bindings)?;107					let ctx = ctx108						.clone()109						.extend(new_bindings, None, None, None)110						.into_future(fctx);111112					evaluate_comp(ctx, &specs[1..], callback)?;113				}114			}115			#[cfg(feature = "exp-object-iteration")]116			Val::Obj(obj) => {117				for field in obj.fields(118					// TODO: Should there be ability to preserve iteration order?119					#[cfg(feature = "exp-preserve-order")]120					false,121				) {122					#[derive(Trace)]123					struct ObjectFieldThunk {124						obj: ObjValue,125						field: IStr,126					}127					impl ThunkValue for ObjectFieldThunk {128						type Output = Val;129130						fn get(self: Box<Self>) -> Result<Self::Output> {131							self.obj.get(self.field).transpose().expect(132								"field exists, as field name was obtained from object.fields()",133							)134						}135					}136137					let fctx = Pending::new();138					let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());139					let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(vec![140						Thunk::evaluated(Val::string(field.clone())),141						Thunk::new(ObjectFieldThunk {142							field: field.clone(),143							obj: obj.clone(),144						}),145					])));146					destruct(var, value, fctx.clone(), &mut new_bindings)?;147					let ctx = ctx148						.clone()149						.extend(new_bindings, None, None, None)150						.into_future(fctx);151152					evaluate_comp(ctx, &specs[1..], callback)?;153				}154			}155			_ => bail!(InComprehensionCanOnlyIterateOverArray),156		},157	}158	Ok(())159}160161trait CloneableUnbound<T>: Unbound<Bound = T> + Clone {}162impl<V, T> CloneableUnbound<T> for V where V: Unbound<Bound = T> + Clone {}163164fn evaluate_object_locals(165	fctx: Pending<Context>,166	locals: Rc<Vec<BindSpec>>,167) -> impl CloneableUnbound<Context> {168	#[derive(Trace, Clone)]169	struct UnboundLocals {170		fctx: Pending<Context>,171		locals: Rc<Vec<BindSpec>>,172	}173	impl Unbound for UnboundLocals {174		type Bound = Context;175176		fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Context> {177			let fctx = Context::new_future();178			let mut new_bindings =179				GcHashMap::with_capacity(self.locals.iter().map(BindSpec::capacity_hint).sum());180			for b in self.locals.iter() {181				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;182			}183184			let ctx = self.fctx.unwrap();185			let new_dollar = ctx.dollar().cloned().or_else(|| this.clone());186187			let ctx = ctx188				.extend(new_bindings, new_dollar, sup, this)189				.into_future(fctx);190191			Ok(ctx)192		}193	}194195	UnboundLocals { fctx, locals }196}197198pub fn evaluate_field_member<B: Unbound<Bound = Context> + Clone>(199	builder: &mut ObjValueBuilder,200	ctx: Context,201	uctx: B,202	field: &FieldMember,203) -> Result<()> {204	let name = evaluate_field_name(ctx, &field.name)?;205	let Some(name) = name else {206		return Ok(());207	};208209	match field {210		FieldMember {211			plus,212			params: None,213			visibility,214			value,215			..216		} => {217			#[derive(Trace)]218			struct UnboundValue<B: Trace> {219				uctx: B,220				value: LocExpr,221				name: IStr,222			}223			impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {224				type Bound = Val;225				fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {226					evaluate_named(self.uctx.bind(sup, this)?, &self.value, self.name.clone())227				}228			}229230			builder231				.field(name.clone())232				.with_add(*plus)233				.with_visibility(*visibility)234				.with_location(value.1.clone())235				.bindable(UnboundValue {236					uctx,237					value: value.clone(),238					name,239				})?;240		}241		FieldMember {242			params: Some(params),243			visibility,244			value,245			..246		} => {247			#[derive(Trace)]248			struct UnboundMethod<B: Trace> {249				uctx: B,250				value: LocExpr,251				params: ParamsDesc,252				name: IStr,253			}254			impl<B: Unbound<Bound = Context>> Unbound for UnboundMethod<B> {255				type Bound = Val;256				fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {257					Ok(evaluate_method(258						self.uctx.bind(sup, this)?,259						self.name.clone(),260						self.params.clone(),261						self.value.clone(),262					))263				}264			}265266			builder267				.field(name.clone())268				.with_visibility(*visibility)269				.with_location(value.1.clone())270				.bindable(UnboundMethod {271					uctx,272					value: value.clone(),273					params: params.clone(),274					name,275				})?;276		}277	}278	Ok(())279}280281#[allow(clippy::too_many_lines)]282pub fn evaluate_member_list_object(ctx: Context, members: &[Member]) -> Result<ObjValue> {283	let mut builder = ObjValueBuilder::new();284	let locals = Rc::new(285		members286			.iter()287			.filter_map(|m| match m {288				Member::BindStmt(bind) => Some(bind.clone()),289				_ => None,290			})291			.collect::<Vec<_>>(),292	);293294	let fctx = Context::new_future();295296	// We have single context for all fields, so we can cache binds297	let uctx = CachedUnbound::new(evaluate_object_locals(fctx.clone(), locals));298299	for member in members {300		match member {301			Member::Field(field) => {302				evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;303			}304			Member::AssertStmt(stmt) => {305				#[derive(Trace)]306				struct ObjectAssert<B: Trace> {307					uctx: B,308					assert: AssertStmt,309				}310				impl<B: Unbound<Bound = Context>> ObjectAssertion for ObjectAssert<B> {311					fn run(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<()> {312						let ctx = self.uctx.bind(sup, this)?;313						evaluate_assert(ctx, &self.assert)314					}315				}316				builder.assert(ObjectAssert {317					uctx: uctx.clone(),318					assert: stmt.clone(),319				});320			}321			Member::BindStmt(_) => {322				// Already handled323			}324		}325	}326	let this = builder.build();327	fctx.fill(ctx.extend(GcHashMap::new(), None, None, Some(this.clone())));328	Ok(this)329}330331pub fn evaluate_object(ctx: Context, object: &ObjBody) -> Result<ObjValue> {332	Ok(match object {333		ObjBody::MemberList(members) => evaluate_member_list_object(ctx, members)?,334		ObjBody::ObjComp(obj) => {335			let mut builder = ObjValueBuilder::new();336			let locals = Rc::new(337				obj.pre_locals338					.iter()339					.chain(obj.post_locals.iter())340					.cloned()341					.collect::<Vec<_>>(),342			);343			let mut ctxs = vec![];344			evaluate_comp(ctx, &obj.compspecs, &mut |ctx| {345				let fctx = Context::new_future();346				ctxs.push((ctx.clone(), fctx.clone()));347				let uctx = evaluate_object_locals(fctx, locals.clone());348349				evaluate_field_member(&mut builder, ctx, uctx, &obj.field)350			})?;351352			let this = builder.build();353			for (ctx, fctx) in ctxs {354				let _ctx = ctx355					.extend(GcHashMap::new(), None, None, Some(this.clone()))356					.into_future(fctx);357			}358			this359		}360	})361}362363pub fn evaluate_apply(364	ctx: Context,365	value: &LocExpr,366	args: &ArgsDesc,367	loc: CallLocation<'_>,368	tailstrict: bool,369) -> Result<Val> {370	let value = evaluate(ctx.clone(), value)?;371	Ok(match value {372		Val::Func(f) => {373			let body = || f.evaluate(ctx, loc, args, tailstrict);374			if tailstrict {375				body()?376			} else {377				State::push(loc, || format!("function <{}> call", f.name()), body)?378			}379		}380		v => bail!(OnlyFunctionsCanBeCalledGot(v.value_type())),381	})382}383384pub fn evaluate_assert(ctx: Context, assertion: &AssertStmt) -> Result<()> {385	let value = &assertion.0;386	let msg = &assertion.1;387	let assertion_result = State::push(388		CallLocation::new(&value.1),389		|| "assertion condition".to_owned(),390		|| bool::from_untyped(evaluate(ctx.clone(), value)?),391	)?;392	if !assertion_result {393		State::push(394			CallLocation::new(&value.1),395			|| "assertion failure".to_owned(),396			|| {397				if let Some(msg) = msg {398					bail!(AssertionFailed(evaluate(ctx, msg)?.to_string()?));399				}400				bail!(AssertionFailed(Val::Null.to_string()?));401			},402		)?;403	}404	Ok(())405}406407pub fn evaluate_named(ctx: Context, expr: &LocExpr, name: IStr) -> Result<Val> {408	use Expr::*;409	let LocExpr(raw_expr, _loc) = expr;410	Ok(match &**raw_expr {411		Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),412		_ => evaluate(ctx, expr)?,413	})414}415416#[allow(clippy::too_many_lines)]417pub fn evaluate(ctx: Context, expr: &LocExpr) -> Result<Val> {418	use Expr::*;419420	if let Some(trivial) = evaluate_trivial(expr) {421		return Ok(trivial);422	}423	let LocExpr(expr, loc) = expr;424	Ok(match &**expr {425		Literal(LiteralType::This) => {426			Val::Obj(ctx.this().ok_or(CantUseSelfOutsideOfObject)?.clone())427		}428		Literal(LiteralType::Super) => Val::Obj(429			ctx.super_obj().ok_or(NoSuperFound)?.with_this(430				ctx.this()431					.expect("if super exists - then this should too")432					.clone(),433			),434		),435		Literal(LiteralType::Dollar) => {436			Val::Obj(ctx.dollar().ok_or(NoTopLevelObjectFound)?.clone())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(ctx, e)?,442		Str(v) => Val::string(v.clone()),443		Num(v) => Val::try_num(*v)?,444		// I have tried to remove special behavior from super by implementing standalone-super445		// expresion, but looks like this case still needs special treatment.446		//447		// Note that other jsonnet implementations will fail on `if value in (super)` expression,448		// because the standalone super literal is not supported, that is because in other449		// implementations `in super` treated differently from in `smth_else`.450		BinaryOp(field, BinaryOpType::In, e)451			if matches!(&*e.0, Expr::Literal(LiteralType::Super)) =>452		{453			let Some(super_obj) = ctx.super_obj() else {454				return Ok(Val::Bool(false));455			};456			let field = evaluate(ctx.clone(), field)?;457			Val::Bool(super_obj.has_field_ex(field.to_string()?, true))458		}459		BinaryOp(v1, o, v2) => evaluate_binary_op_special(ctx, v1, *o, v2)?,460		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,461		Var(name) => State::push(462			CallLocation::new(loc),463			|| format!("variable <{name}> access"),464			|| ctx.binding(name.clone())?.evaluate(),465		)?,466		Index { indexable, parts } => {467			let mut parts = parts.iter();468			let mut indexable = match &indexable {469				// Cheaper to execute than creating object with overriden `this`470				LocExpr(v, _) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {471					let part = parts.next().expect("at least part should exist");472					let Some(super_obj) = ctx.super_obj() else {473						#[cfg(feature = "exp-null-coaelse")]474						if part.null_coaelse {475							return Ok(Val::Null);476						}477						bail!(NoSuperFound)478					};479					let name = evaluate(ctx.clone(), &part.value)?;480481					let Val::Str(name) = name else {482						bail!(ValueIndexMustBeTypeGot(483							ValType::Obj,484							ValType::Str,485							name.value_type(),486						))487					};488489					let this = ctx490						.this()491						.expect("no this found, while super present, should not happen");492					let name = name.into_flat();493					match super_obj494						.get_for(name.clone(), this.clone())495						.with_description_src(&part.value, || format!("field <{name}> access"))?496					{497						Some(v) => v,498						#[cfg(feature = "exp-null-coaelse")]499						None if part.null_coaelse => return Ok(Val::Null),500						None => {501							let suggestions = suggest_object_fields(super_obj, name.clone());502503							bail!(NoSuchField(name, suggestions))504						}505					}506				}507				e => evaluate(ctx.clone(), e)?,508			};509510			for part in parts {511				indexable = match (indexable, evaluate(ctx.clone(), &part.value)?) {512					(Val::Obj(v), Val::Str(key)) => match v513						.get(key.clone().into_flat())514						.with_description_src(&part.value, || format!("field <{key}> access"))?515					{516						Some(v) => v,517						#[cfg(feature = "exp-null-coaelse")]518						None if part.null_coaelse => return Ok(Val::Null),519						None => {520							let suggestions = suggest_object_fields(&v, key.clone().into_flat());521522							return Err(Error::from(NoSuchField(523								key.clone().into_flat(),524								suggestions,525							)))526							.with_description_src(&part.value, || format!("field <{key}> access"));527						}528					},529					(Val::Obj(_), n) => bail!(ValueIndexMustBeTypeGot(530						ValType::Obj,531						ValType::Str,532						n.value_type(),533					)),534					(Val::Arr(v), Val::Num(n)) => {535						let n = n.get();536						if n.fract() > f64::EPSILON {537							bail!(FractionalIndex)538						}539						if n < 0.0 {540							bail!(ArrayBoundsError(n as isize, v.len()));541						}542						v.get(n as usize)?543							.ok_or_else(|| ArrayBoundsError(n as isize, v.len()))?544					}545					(Val::Arr(_), Val::Str(n)) => {546						bail!(AttemptedIndexAnArrayWithString(n.into_flat()))547					}548					(Val::Arr(_), n) => bail!(ValueIndexMustBeTypeGot(549						ValType::Arr,550						ValType::Num,551						n.value_type(),552					)),553554					(Val::Str(s), Val::Num(n)) => Val::Str({555						let v: IStr = s556							.clone()557							.into_flat()558							.chars()559							.skip(n.get() as usize)560							.take(1)561							.collect::<String>()562							.into();563						if v.is_empty() {564							let size = s.into_flat().chars().count();565							bail!(StringBoundsError(n.get() as usize, size))566						}567						StrValue::Flat(v)568					}),569					(Val::Str(_), n) => bail!(ValueIndexMustBeTypeGot(570						ValType::Str,571						ValType::Num,572						n.value_type(),573					)),574					#[cfg(feature = "exp-null-coaelse")]575					(Val::Null, _) if part.null_coaelse => return Ok(Val::Null),576					(v, _) => bail!(CantIndexInto(v.value_type())),577				};578			}579			indexable580		}581		LocalExpr(bindings, returned) => {582			let mut new_bindings: GcHashMap<IStr, Thunk<Val>> =583				GcHashMap::with_capacity(bindings.iter().map(BindSpec::capacity_hint).sum());584			let fctx = Context::new_future();585			for b in bindings {586				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;587			}588			let ctx = ctx.extend(new_bindings, None, None, None).into_future(fctx);589			evaluate(ctx, &returned.clone())?590		}591		Arr(items) => {592			if items.is_empty() {593				Val::Arr(ArrValue::empty())594			} else if items.len() == 1 {595				#[derive(Trace)]596				struct ArrayElement {597					ctx: Context,598					item: LocExpr,599				}600				impl ThunkValue for ArrayElement {601					type Output = Val;602					fn get(self: Box<Self>) -> Result<Val> {603						evaluate(self.ctx, &self.item)604					}605				}606				Val::Arr(ArrValue::lazy(vec![Thunk::new(ArrayElement {607					ctx,608					item: items[0].clone(),609				})]))610			} else {611				Val::Arr(ArrValue::expr(ctx, items.iter().cloned()))612			}613		}614		ArrComp(expr, comp_specs) => {615			let mut out = Vec::new();616			evaluate_comp(ctx, comp_specs, &mut |ctx| {617				#[derive(Trace)]618				struct EvaluateThunk {619					ctx: Context,620					expr: LocExpr,621				}622				impl ThunkValue for EvaluateThunk {623					type Output = Val;624					fn get(self: Box<Self>) -> Result<Val> {625						evaluate(self.ctx, &self.expr)626					}627				}628				out.push(Thunk::new(EvaluateThunk {629					ctx,630					expr: expr.clone(),631				}));632				Ok(())633			})?;634			Val::Arr(ArrValue::lazy(out))635		}636		Obj(body) => Val::Obj(evaluate_object(ctx, body)?),637		ObjExtend(a, b) => evaluate_add_op(638			&evaluate(ctx.clone(), a)?,639			&Val::Obj(evaluate_object(ctx, b)?),640		)?,641		Apply(value, args, tailstrict) => {642			evaluate_apply(ctx, value, args, CallLocation::new(loc), *tailstrict)?643		}644		Function(params, body) => {645			evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())646		}647		AssertExpr(assert, returned) => {648			evaluate_assert(ctx.clone(), assert)?;649			evaluate(ctx, returned)?650		}651		ErrorStmt(e) => State::push(652			CallLocation::new(loc),653			|| "error statement".to_owned(),654			|| bail!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),655		)?,656		IfElse {657			cond,658			cond_then,659			cond_else,660		} => {661			if State::push(662				CallLocation::new(loc),663				|| "if condition".to_owned(),664				|| bool::from_untyped(evaluate(ctx.clone(), &cond.0)?),665			)? {666				evaluate(ctx, cond_then)?667			} else {668				match cond_else {669					Some(v) => evaluate(ctx, v)?,670					None => Val::Null,671				}672			}673		}674		Slice(value, desc) => {675			fn parse_idx<T: Typed>(676				loc: CallLocation<'_>,677				ctx: &Context,678				expr: Option<&LocExpr>,679				desc: &'static str,680			) -> Result<Option<T>> {681				if let Some(value) = expr {682					Ok(Some(State::push(683						loc,684						|| format!("slice {desc}"),685						|| T::from_untyped(evaluate(ctx.clone(), value)?),686					)?))687				} else {688					Ok(None)689				}690			}691692			let indexable = evaluate(ctx.clone(), value)?;693			let loc = CallLocation::new(loc);694695			let start = parse_idx(loc, &ctx, desc.start.as_ref(), "start")?;696			let end = parse_idx(loc, &ctx, desc.end.as_ref(), "end")?;697			let step = parse_idx(loc, &ctx, desc.step.as_ref(), "step")?;698699			IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?700		}701		i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {702			let Expr::Str(path) = &*path.0 else {703				bail!("computed imports are not supported")704			};705			let tmp = loc.clone().0;706			let s = ctx.state();707			let resolved_path = s.resolve_from(tmp.source_path(), path as &str)?;708			match i {709				Import(_) => State::push(710					CallLocation::new(loc),711					|| format!("import {:?}", path.clone()),712					|| s.import_resolved(resolved_path),713				)?,714				ImportStr(_) => Val::string(s.import_resolved_str(resolved_path)?),715				ImportBin(_) => Val::Arr(ArrValue::bytes(s.import_resolved_bin(resolved_path)?)),716				_ => unreachable!(),717			}718		}719	})720}
modifiedcrates/jrsonnet-rowan-parser/src/marker.rsdiffbeforeafterboth
--- a/crates/jrsonnet-rowan-parser/src/marker.rs
+++ b/crates/jrsonnet-rowan-parser/src/marker.rs
@@ -141,12 +141,7 @@
 		new_m
 	}
 	/// Create new node around existing marker, not counting anything that comes after it
-	fn wrap_raw(
-		self,
-		p: &mut Parser,
-		kind: SyntaxKind,
-		error: Option<SyntaxError>,
-	) -> Self {
+	fn wrap_raw(self, p: &mut Parser, kind: SyntaxKind, error: Option<SyntaxError>) -> Self {
 		let new_m = p.start();
 		match &mut p.events[self.start_event_idx] {
 			Event::Start { forward_parent, .. } => {