git.delta.rocks / jrsonnet / refs/commits / 5ac33cfd81f9

difftreelog

source

crates/jrsonnet-evaluator/src/evaluate/mod.rs19.0 KiBsourcehistory
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;10use rustc_hash::FxHashMap;1112use self::destructure::destruct;13use crate::{14	Context, Error, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, ResultExt, SupThis, Unbound, Val, arr::ArrValue, bail, destructure::evaluate_dest, error::{ErrorKind::*, suggest_object_fields}, evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op}, function::{CallLocation, FuncDesc, FuncVal}, gc::WithCapacityExt as _, in_frame, typed::Typed, val::{CachedUnbound, IndexableVal, NumValue, StrValue, Thunk}, with_state15};16pub mod destructure;17pub mod operator;1819// This is the amount of bytes that need to be left on the stack before increasing the size.20// It must be at least as large as the stack required by any code that does not call21// `ensure_sufficient_stack`.22const RED_ZONE: usize = 100 * 1024; // 100k2324// Only the first stack that is pushed, grows exponentially (2^n * STACK_PER_RECURSION) from then25// on. This flag has performance relevant characteristics. Don't set it too high.26const STACK_PER_RECURSION: usize = 1024 * 1024; // 1MB2728/// Grows the stack on demand to prevent stack overflow. Call this in strategic locations29/// to "break up" recursive calls. E.g. almost any call to `visit_expr` or equivalent can benefit30/// from this.31///32/// Should not be sprinkled around carelessly, as it causes a little bit of overhead.33#[inline]34pub fn ensure_sufficient_stack<R>(f: impl FnOnce() -> R) -> R {35	stacker::maybe_grow(RED_ZONE, STACK_PER_RECURSION, f)36}3738pub fn evaluate_trivial(expr: &LocExpr) -> Option<Val> {39	fn is_trivial(expr: &LocExpr) -> bool {40		match expr.expr() {41			Expr::Str(_)42			| Expr::Num(_)43			| Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,44			Expr::Arr(a) => a.iter().all(is_trivial),45			Expr::Parened(e) => is_trivial(e),46			_ => false,47		}48	}49	Some(match expr.expr() {50		Expr::Str(s) => Val::string(s.clone()),51		Expr::Num(n) => {52			Val::Num(NumValue::new(*n).expect("parser will not allow non-finite values"))53		}54		Expr::Literal(LiteralType::False) => Val::Bool(false),55		Expr::Literal(LiteralType::True) => Val::Bool(true),56		Expr::Literal(LiteralType::Null) => Val::Null,57		Expr::Arr(n) => {58			if n.iter().any(|e| !is_trivial(e)) {59				return None;60			}61			Val::Arr(ArrValue::eager(62				n.iter()63					.map(evaluate_trivial)64					.map(|e| e.expect("checked trivial"))65					.collect(),66			))67		}68		Expr::Parened(e) => evaluate_trivial(e)?,69		_ => return None,70	})71}7273pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {74	Val::Func(FuncVal::Normal(Cc::new(FuncDesc {75		name,76		ctx,77		params,78		body,79	})))80}8182pub fn evaluate_field_name(ctx: Context, field_name: &FieldName) -> Result<Option<IStr>> {83	Ok(match field_name {84		FieldName::Fixed(n) => Some(n.clone()),85		FieldName::Dyn(expr) => in_frame(86			CallLocation::new(&expr.span()),87			|| "evaluating field name".to_string(),88			|| {89				let value = evaluate(ctx, expr)?;90				if matches!(value, Val::Null) {91					Ok(None)92				} else {93					Ok(Some(IStr::from_untyped(value)?))94				}95			},96		)?,97	})98}99100pub fn evaluate_comp(101	ctx: Context,102	specs: &[CompSpec],103	callback: &mut impl FnMut(Context) -> Result<()>,104) -> Result<()> {105	match specs.first() {106		None => callback(ctx)?,107		Some(CompSpec::IfSpec(IfSpecData(cond))) => {108			if bool::from_untyped(evaluate(ctx.clone(), cond)?)? {109				evaluate_comp(ctx, &specs[1..], callback)?;110			}111		}112		Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(ctx.clone(), expr)? {113			Val::Arr(list) => {114				for item in list.iter_lazy() {115					let fctx = Pending::new();116					let mut new_bindings = FxHashMap::with_capacity(var.capacity_hint());117					destruct(var, item, fctx.clone(), &mut new_bindings)?;118					let ctx = ctx.clone().extend_bindings(new_bindings).into_future(fctx);119120					evaluate_comp(ctx, &specs[1..], callback)?;121				}122			}123			#[cfg(feature = "exp-object-iteration")]124			Val::Obj(obj) => {125				for field in obj.fields(126					// TODO: Should there be ability to preserve iteration order?127					#[cfg(feature = "exp-preserve-order")]128					false,129				) {130					let fctx = Pending::new();131					let mut new_bindings = FxHashMap::with_capacity(var.capacity_hint());132					let obj = obj.clone();133					let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(vec![134						Thunk::evaluated(Val::string(field.clone())),135						Thunk!(move || obj.get(field).transpose().expect(136							"field exists, as field name was obtained from object.fields()",137						)),138					])));139					destruct(var, value, fctx.clone(), &mut new_bindings)?;140					let ctx = ctx141						.clone()142						.extend(new_bindings, None, None, None)143						.into_future(fctx);144145					evaluate_comp(ctx, &specs[1..], callback)?;146				}147			}148			_ => bail!(InComprehensionCanOnlyIterateOverArray),149		},150	}151	Ok(())152}153154trait CloneableUnbound<T>: Unbound<Bound = T> + Clone {}155impl<V, T> CloneableUnbound<T> for V where V: Unbound<Bound = T> + Clone {}156157fn evaluate_object_locals(158	fctx: Context,159	locals: Rc<Vec<BindSpec>>,160) -> impl CloneableUnbound<Context> {161	#[derive(Trace, Clone)]162	struct UnboundLocals {163		fctx: Context,164		locals: Rc<Vec<BindSpec>>,165	}166	impl Unbound for UnboundLocals {167		type Bound = Context;168169		fn bind(&self, sup_this: SupThis) -> Result<Context> {170			let fctx = Context::new_future();171			let mut new_bindings =172				FxHashMap::with_capacity(self.locals.iter().map(BindSpec::capacity_hint).sum());173			for b in self.locals.iter() {174				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;175			}176177			let ctx = self.fctx.clone();178179			let ctx = ctx180				.extend_bindings_sup_this(new_bindings, sup_this)181				.into_future(fctx);182183			Ok(ctx)184		}185	}186187	UnboundLocals { fctx, locals }188}189190pub fn evaluate_field_member<B: Unbound<Bound = Context> + Clone>(191	builder: &mut ObjValueBuilder,192	ctx: Context,193	uctx: B,194	field: &FieldMember,195) -> Result<()> {196	let name = evaluate_field_name(ctx, &field.name)?;197	let Some(name) = name else {198		return Ok(());199	};200201	match field {202		FieldMember {203			plus,204			params: None,205			visibility,206			value,207			..208		} => {209			#[derive(Trace)]210			struct UnboundValue<B: Trace> {211				uctx: B,212				value: LocExpr,213				name: IStr,214			}215			impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {216				type Bound = Val;217				fn bind(&self, sup_this: SupThis) -> Result<Val> {218					evaluate_named(self.uctx.bind(sup_this)?, &self.value, self.name.clone())219				}220			}221222			builder223				.field(name.clone())224				.with_add(*plus)225				.with_visibility(*visibility)226				.with_location(value.span())227				.bindable(UnboundValue {228					uctx,229					value: value.clone(),230					name,231				})?;232		}233		FieldMember {234			params: Some(params),235			visibility,236			value,237			..238		} => {239			#[derive(Trace)]240			struct UnboundMethod<B: Trace> {241				uctx: B,242				value: LocExpr,243				params: ParamsDesc,244				name: IStr,245			}246			impl<B: Unbound<Bound = Context>> Unbound for UnboundMethod<B> {247				type Bound = Val;248				fn bind(&self, sup_this: SupThis) -> Result<Val> {249					Ok(evaluate_method(250						self.uctx.bind(sup_this)?,251						self.name.clone(),252						self.params.clone(),253						self.value.clone(),254					))255				}256			}257258			builder259				.field(name.clone())260				.with_visibility(*visibility)261				.with_location(value.span())262				.bindable(UnboundMethod {263					uctx,264					value: value.clone(),265					params: params.clone(),266					name,267				})?;268		}269	}270	Ok(())271}272273#[allow(clippy::too_many_lines)]274pub fn evaluate_member_list_object(ctx: Context, members: &[Member]) -> Result<ObjValue> {275	let mut builder = ObjValueBuilder::new();276	let locals = Rc::new(277		members278			.iter()279			.filter_map(|m| match m {280				Member::BindStmt(bind) => Some(bind.clone()),281				_ => None,282			})283			.collect::<Vec<_>>(),284	);285286	// We have single context for all fields, so we can cache binds287	let uctx = CachedUnbound::new(evaluate_object_locals(ctx.clone(), locals));288289	for member in members {290		match member {291			Member::Field(field) => {292				evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;293			}294			Member::AssertStmt(stmt) => {295				#[derive(Trace)]296				struct ObjectAssert<B: Trace> {297					uctx: B,298					assert: AssertStmt,299				}300				impl<B: Unbound<Bound = Context>> ObjectAssertion for ObjectAssert<B> {301					fn run(&self, sup_this: SupThis) -> Result<()> {302						let ctx = self.uctx.bind(sup_this)?;303						evaluate_assert(ctx, &self.assert)304					}305				}306				builder.assert(ObjectAssert {307					uctx: uctx.clone(),308					assert: stmt.clone(),309				});310			}311			Member::BindStmt(_) => {312				// Already handled313			}314		}315	}316	Ok(builder.build())317}318319pub fn evaluate_object(ctx: Context, object: &ObjBody) -> Result<ObjValue> {320	Ok(match object {321		ObjBody::MemberList(members) => evaluate_member_list_object(ctx, members)?,322		ObjBody::ObjComp(obj) => {323			let mut builder = ObjValueBuilder::new();324			let locals = Rc::new(325				obj.pre_locals326					.iter()327					.chain(obj.post_locals.iter())328					.cloned()329					.collect::<Vec<_>>(),330			);331			evaluate_comp(ctx, &obj.compspecs, &mut |ctx| {332				let uctx = evaluate_object_locals(ctx.clone(), locals.clone());333334				evaluate_field_member(&mut builder, ctx, uctx, &obj.field)335			})?;336337			builder.build()338		}339	})340}341342pub fn evaluate_apply(343	ctx: Context,344	value: &LocExpr,345	args: &ArgsDesc,346	loc: CallLocation<'_>,347	tailstrict: bool,348) -> Result<Val> {349	let value = evaluate(ctx.clone(), value)?;350	Ok(match value {351		Val::Func(f) => {352			let body = || f.evaluate(ctx, loc, args, tailstrict);353			if tailstrict {354				body()?355			} else {356				in_frame(loc, || format!("function <{}> call", f.name()), body)?357			}358		}359		v => bail!(OnlyFunctionsCanBeCalledGot(v.value_type())),360	})361}362363pub fn evaluate_assert(ctx: Context, assertion: &AssertStmt) -> Result<()> {364	let value = &assertion.0;365	let msg = &assertion.1;366	let assertion_result = in_frame(367		CallLocation::new(&value.span()),368		|| "assertion condition".to_owned(),369		|| bool::from_untyped(evaluate(ctx.clone(), value)?),370	)?;371	if !assertion_result {372		in_frame(373			CallLocation::new(&value.span()),374			|| "assertion failure".to_owned(),375			|| {376				if let Some(msg) = msg {377					bail!(AssertionFailed(evaluate(ctx, msg)?.to_string()?));378				}379				bail!(AssertionFailed(Val::Null.to_string()?));380			},381		)?;382	}383	Ok(())384}385386pub fn evaluate_named(ctx: Context, expr: &LocExpr, name: IStr) -> Result<Val> {387	use Expr::*;388	Ok(match expr.expr() {389		Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),390		_ => evaluate(ctx, expr)?,391	})392}393394#[allow(clippy::too_many_lines)]395pub fn evaluate(ctx: Context, expr: &LocExpr) -> Result<Val> {396	use Expr::*;397398	if let Some(trivial) = evaluate_trivial(expr) {399		return Ok(trivial);400	}401	let loc = expr.span();402	Ok(match expr.expr() {403		Literal(LiteralType::This) => Val::Obj(ctx.try_this()?),404		Literal(LiteralType::Super) => Val::Obj(ctx.try_sup_this()?.standalone_super()?),405		Literal(LiteralType::Dollar) => Val::Obj(ctx.try_dollar()?),406		Literal(LiteralType::True) => Val::Bool(true),407		Literal(LiteralType::False) => Val::Bool(false),408		Literal(LiteralType::Null) => Val::Null,409		Parened(e) => evaluate(ctx, e)?,410		Str(v) => Val::string(v.clone()),411		Num(v) => Val::try_num(*v)?,412		// I have tried to remove special behavior from super by implementing standalone-super413		// expresion, but looks like this case still needs special treatment.414		//415		// Note that other jsonnet implementations will fail on `if value in (super)` expression,416		// because the standalone super literal is not supported, that is because in other417		// implementations `in super` treated differently from `in smth_else`.418		BinaryOp(field, BinaryOpType::In, e)419			if matches!(e.expr(), Expr::Literal(LiteralType::Super)) =>420		{421			let sup_this = ctx.try_sup_this()?;422			// In jsonnet, "field" in e is eager, LHS expression is always executed regardless of super existence.423			// In jrsonnet, however, this wasn't true, this was kept here for compatibility.424			if !sup_this.has_super() {425				return Ok(Val::Bool(false));426			}427			let field = evaluate(ctx, field)?;428			Val::Bool(sup_this.field_in_super(field.to_string()?))429		}430		BinaryOp(v1, o, v2) => evaluate_binary_op_special(ctx, v1, *o, v2)?,431		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,432		Var(name) => in_frame(433			CallLocation::new(&loc),434			|| format!("local <{name}> access"),435			|| ctx.binding(name.clone())?.evaluate(),436		)?,437		Index { indexable, parts } => ensure_sufficient_stack(|| {438			let mut parts = parts.iter();439			let mut indexable = if matches!(indexable.expr(), Expr::Literal(LiteralType::Super)) {440				let part = parts.next().expect("at least part should exist");441				// sup_this existence check might also be skipped here for null-coalesce...442				// But I believe this might cause errors.443				let sup_this = ctx.try_sup_this()?;444				if !sup_this.has_super() {445					#[cfg(feature = "exp-null-coaelse")]446					if part.null_coaelse {447						return Ok(Val::Null);448					}449					bail!(NoSuperFound)450				}451				let name = evaluate(ctx.clone(), &part.value)?;452453				let Val::Str(name) = name else {454					bail!(ValueIndexMustBeTypeGot(455						ValType::Obj,456						ValType::Str,457						name.value_type(),458					))459				};460461				let name = name.into_flat();462				match sup_this463					.get_super(name.clone())464					.with_description_src(&part.value, || format!("field <{name}> access"))?465				{466					Some(v) => v,467					#[cfg(feature = "exp-null-coaelse")]468					None if part.null_coaelse => return Ok(Val::Null),469					None => {470						let suggestions = suggest_object_fields(471							&sup_this.standalone_super().expect("super exists"),472							name.clone(),473						);474475						bail!(NoSuchField(name, suggestions))476					}477				}478			} else {479				evaluate(ctx.clone(), indexable)?480			};481482			for part in parts {483				indexable = match (indexable, evaluate(ctx.clone(), &part.value)?) {484					(Val::Obj(v), Val::Str(key)) => match v485						.get(key.clone().into_flat())486						.with_description_src(&part.value, || format!("field <{key}> access"))?487					{488						Some(v) => v,489						#[cfg(feature = "exp-null-coaelse")]490						None if part.null_coaelse => return Ok(Val::Null),491						None => {492							let suggestions = suggest_object_fields(&v, key.clone().into_flat());493494							return Err(Error::from(NoSuchField(495								key.clone().into_flat(),496								suggestions,497							)))498							.with_description_src(&part.value, || format!("field <{key}> access"));499						}500					},501					(Val::Obj(_), n) => bail!(ValueIndexMustBeTypeGot(502						ValType::Obj,503						ValType::Str,504						n.value_type(),505					)),506					(Val::Arr(v), Val::Num(n)) => {507						let n = n.get();508						if n.fract() > f64::EPSILON {509							bail!(FractionalIndex)510						}511						if n < 0.0 {512							bail!(ArrayBoundsError(n as isize, v.len()));513						}514						v.get(n as usize)?515							.ok_or_else(|| ArrayBoundsError(n as isize, v.len()))?516					}517					(Val::Arr(_), Val::Str(n)) => {518						bail!(AttemptedIndexAnArrayWithString(n.into_flat()))519					}520					(Val::Arr(_), n) => bail!(ValueIndexMustBeTypeGot(521						ValType::Arr,522						ValType::Num,523						n.value_type(),524					)),525526					(Val::Str(s), Val::Num(n)) => Val::Str({527						let v: IStr = s528							.clone()529							.into_flat()530							.chars()531							.skip(n.get() as usize)532							.take(1)533							.collect::<String>()534							.into();535						if v.is_empty() {536							let size = s.into_flat().chars().count();537							bail!(StringBoundsError(n.get() as usize, size))538						}539						StrValue::Flat(v)540					}),541					(Val::Str(_), n) => bail!(ValueIndexMustBeTypeGot(542						ValType::Str,543						ValType::Num,544						n.value_type(),545					)),546					#[cfg(feature = "exp-null-coaelse")]547					(Val::Null, _) if part.null_coaelse => return Ok(Val::Null),548					(v, _) => bail!(CantIndexInto(v.value_type())),549				};550			}551			Ok(indexable)552		})?,553		LocalExpr(bindings, returned) => {554			let mut new_bindings: FxHashMap<IStr, Thunk<Val>> =555				FxHashMap::with_capacity(bindings.iter().map(BindSpec::capacity_hint).sum());556			let fctx = Context::new_future();557			for b in bindings {558				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;559			}560			let ctx = ctx.extend_bindings(new_bindings).into_future(fctx);561			evaluate(ctx, &returned.clone())?562		}563		Arr(items) => {564			if items.is_empty() {565				Val::Arr(ArrValue::empty())566			} else if items.len() == 1 {567				let item = items[0].clone();568				Val::Arr(ArrValue::lazy(vec![Thunk!(move || evaluate(ctx, &item))]))569			} else {570				Val::Arr(ArrValue::expr(ctx, items.iter().cloned()))571			}572		}573		ArrComp(expr, comp_specs) => {574			let mut out = Vec::new();575			evaluate_comp(ctx, comp_specs, &mut |ctx| {576				let expr = expr.clone();577				out.push(Thunk!(move || evaluate(ctx, &expr)));578				Ok(())579			})?;580			Val::Arr(ArrValue::lazy(out))581		}582		Obj(body) => Val::Obj(evaluate_object(ctx, body)?),583		ObjExtend(a, b) => evaluate_add_op(584			&evaluate(ctx.clone(), a)?,585			&Val::Obj(evaluate_object(ctx, b)?),586		)?,587		Apply(value, args, tailstrict) => ensure_sufficient_stack(|| {588			evaluate_apply(ctx, value, args, CallLocation::new(&loc), *tailstrict)589		})?,590		Function(params, body) => {591			evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())592		}593		AssertExpr(assert, returned) => {594			evaluate_assert(ctx.clone(), assert)?;595			evaluate(ctx, returned)?596		}597		ErrorStmt(e) => in_frame(598			CallLocation::new(&loc),599			|| "error statement".to_owned(),600			|| bail!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),601		)?,602		IfElse {603			cond,604			cond_then,605			cond_else,606		} => {607			if in_frame(608				CallLocation::new(&loc),609				|| "if condition".to_owned(),610				|| bool::from_untyped(evaluate(ctx.clone(), &cond.0)?),611			)? {612				evaluate(ctx, cond_then)?613			} else {614				match cond_else {615					Some(v) => evaluate(ctx, v)?,616					None => Val::Null,617				}618			}619		}620		Slice(value, desc) => {621			fn parse_idx<T: Typed>(622				loc: CallLocation<'_>,623				ctx: Context,624				expr: Option<&LocExpr>,625				desc: &'static str,626			) -> Result<Option<T>> {627				if let Some(value) = expr {628					Ok(in_frame(629						loc,630						|| format!("slice {desc}"),631						|| <Option<T>>::from_untyped(evaluate(ctx, value)?),632					)?)633				} else {634					Ok(None)635				}636			}637638			let indexable = evaluate(ctx.clone(), value)?;639			let loc = CallLocation::new(&loc);640641			let start = parse_idx(loc, ctx.clone(), desc.start.as_ref(), "start")?;642			let end = parse_idx(loc, ctx.clone(), desc.end.as_ref(), "end")?;643			let step = parse_idx(loc, ctx, desc.step.as_ref(), "step")?;644645			IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?646		}647		i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {648			let Expr::Str(path) = &path.expr() else {649				bail!("computed imports are not supported")650			};651			let tmp = loc.clone().0;652			with_state(|s| {653				let resolved_path = s.resolve_from(tmp.source_path(), path)?;654				Ok(match i {655					Import(_) => in_frame(656						CallLocation::new(&loc),657						|| format!("import {:?}", path.clone()),658						|| s.import_resolved(resolved_path),659					)?,660					ImportStr(_) => Val::string(s.import_resolved_str(resolved_path)?),661					ImportBin(_) => {662						Val::Arr(ArrValue::bytes(s.import_resolved_bin(resolved_path)?))663					}664					_ => unreachable!(),665				}) as Result<Val>666			})?667		}668	})669}