difftreelog
refactor no need to use CacheUnbound if there is no locals at all
in: master
1 file changed
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth1use std::rc::Rc;23use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_ir::{6 function::ParamName, ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, ExprParams,7 FieldMember, FieldName, ForSpecData, IfSpecData, ImportKind, LiteralType, ObjBody, ObjMembers,8 Spanned,9};10use jrsonnet_types::ValType;11use rustc_hash::FxHashMap;1213use self::destructure::destruct;14use crate::{15 arr::ArrValue,16 bail,17 destructure::evaluate_dest,18 error::{suggest_object_fields, ErrorKind::*},19 evaluate::operator::{evaluate_binary_op_special, evaluate_unary_op},20 function::{CallLocation, FuncDesc, FuncVal},21 gc::WithCapacityExt as _,22 in_frame,23 typed::{FromUntyped, IntoUntyped as _, Typed},24 val::{CachedUnbound, IndexableVal, NumValue, StrValue, Thunk},25 with_state, Context, Error, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result,26 ResultExt, SupThis, Unbound, Val,27};28pub mod destructure;29pub mod operator;3031// This is the amount of bytes that need to be left on the stack before increasing the size.32// It must be at least as large as the stack required by any code that does not call33// `ensure_sufficient_stack`.34const RED_ZONE: usize = 100 * 1024; // 100k3536// Only the first stack that is pushed, grows exponentially (2^n * STACK_PER_RECURSION) from then37// on. This flag has performance relevant characteristics. Don't set it too high.38const STACK_PER_RECURSION: usize = 1024 * 1024; // 1MB3940/// Grows the stack on demand to prevent stack overflow. Call this in strategic locations41/// to "break up" recursive calls. E.g. almost any call to `visit_expr` or equivalent can benefit42/// from this.43///44/// Should not be sprinkled around carelessly, as it causes a little bit of overhead.45#[inline]46pub fn ensure_sufficient_stack<R>(f: impl FnOnce() -> R) -> R {47 stacker::maybe_grow(RED_ZONE, STACK_PER_RECURSION, f)48}4950pub fn evaluate_trivial(expr: &Expr) -> Option<Val> {51 fn is_trivial(expr: &Expr) -> bool {52 match expr {53 Expr::Str(_)54 | Expr::Num(_)55 | Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,56 Expr::Arr(a) => a.iter().all(is_trivial),57 _ => false,58 }59 }60 Some(match expr {61 Expr::Str(s) => Val::string(s.clone()),62 Expr::Num(n) => {63 Val::Num(NumValue::new(*n).expect("parser will not allow non-finite values"))64 }65 Expr::Literal(LiteralType::False) => Val::Bool(false),66 Expr::Literal(LiteralType::True) => Val::Bool(true),67 Expr::Literal(LiteralType::Null) => Val::Null,68 Expr::Arr(n) => {69 if n.iter().any(|e| !is_trivial(e)) {70 return None;71 }72 Val::Arr(ArrValue::eager(73 n.iter()74 .map(evaluate_trivial)75 .map(|e| e.expect("checked trivial"))76 .collect(),77 ))78 }79 _ => return None,80 })81}8283pub fn evaluate_method(ctx: Context, name: IStr, params: ExprParams, body: Rc<Expr>) -> Val {84 Val::Func(FuncVal::Normal(Cc::new(FuncDesc {85 name,86 ctx,87 params,88 body,89 })))90}9192pub fn evaluate_field_name(ctx: Context, field_name: &Spanned<FieldName>) -> Result<Option<IStr>> {93 Ok(match &field_name.value {94 FieldName::Fixed(n) => Some(n.clone()),95 FieldName::Dyn(expr) => in_frame(96 CallLocation::new(&field_name.span),97 || "evaluating field name".to_string(),98 || {99 let v = evaluate(ctx, expr)?;100 Ok(if matches!(v, Val::Null) {101 None102 } else {103 Some(IStr::from_untyped(v)?)104 })105 },106 )?,107 })108}109110pub fn evaluate_comp(111 ctx: Context,112 specs: &[CompSpec],113 callback: &mut impl FnMut(Context) -> Result<()>,114) -> Result<()> {115 match specs.first() {116 None => callback(ctx)?,117 Some(CompSpec::IfSpec(IfSpecData { cond, span: _ })) => {118 if bool::from_untyped(evaluate(ctx.clone(), cond)?)? {119 evaluate_comp(ctx, &specs[1..], callback)?;120 }121 }122 Some(CompSpec::ForSpec(ForSpecData {123 destruct: into,124 over,125 })) => {126 match evaluate(ctx.clone(), over)? {127 Val::Arr(list) => {128 for item in list.iter_lazy() {129 let fctx = Pending::new();130 let mut new_bindings = FxHashMap::with_capacity(into.binds_len());131 destruct(into, item, fctx.clone(), &mut new_bindings)?;132 let ctx = ctx.clone().extend_bindings(new_bindings).into_future(fctx);133134 evaluate_comp(ctx, &specs[1..], callback)?;135 }136 }137 #[cfg(feature = "exp-object-iteration")]138 Val::Obj(obj) => {139 for field in obj.fields(140 // TODO: Should there be ability to preserve iteration order?141 #[cfg(feature = "exp-preserve-order")]142 false,143 ) {144 let fctx = Pending::new();145 let mut new_bindings = FxHashMap::with_capacity(into.binds_len());146 let obj = obj.clone();147 let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(vec![148 Thunk::evaluated(Val::string(field.clone())),149 Thunk!(move || obj.get(field).transpose().expect(150 "field exists, as field name was obtained from object.fields()",151 )),152 ])));153 destruct(into, value, fctx.clone(), &mut new_bindings)?;154 let ctx = ctx.clone().extend_bindings(new_bindings).into_future(fctx);155156 evaluate_comp(ctx, &specs[1..], callback)?;157 }158 }159 _ => bail!(InComprehensionCanOnlyIterateOverArray),160 }161 }162 }163 Ok(())164}165166trait CloneableUnbound<T>: Unbound<Bound = T> + Clone {}167impl<V, T> CloneableUnbound<T> for V where V: Unbound<Bound = T> + Clone {}168169fn evaluate_object_locals(170 fctx: Context,171 locals: Rc<Vec<BindSpec>>,172) -> impl CloneableUnbound<Context> {173 #[derive(Trace, Clone)]174 struct UnboundLocals {175 fctx: Context,176 locals: Rc<Vec<BindSpec>>,177 }178 impl Unbound for UnboundLocals {179 type Bound = Context;180181 fn bind(&self, sup_this: SupThis) -> Result<Context> {182 let fctx = Context::new_future();183 let mut new_bindings =184 FxHashMap::with_capacity(self.locals.iter().map(BindSpec::binds_len).sum());185 for b in self.locals.iter() {186 evaluate_dest(b, fctx.clone(), &mut new_bindings)?;187 }188189 let ctx = self.fctx.clone();190191 let ctx = ctx192 .extend_bindings_sup_this(new_bindings, sup_this)193 .into_future(fctx);194195 Ok(ctx)196 }197 }198199 UnboundLocals { fctx, locals }200}201202pub fn evaluate_field_member<B: Unbound<Bound = Context> + Clone>(203 builder: &mut ObjValueBuilder,204 ctx: Context,205 uctx: B,206 field: &FieldMember,207) -> Result<()> {208 let name = evaluate_field_name(ctx, &field.name)?;209 let Some(name) = name else {210 return Ok(());211 };212213 match field {214 FieldMember {215 plus,216 params: None,217 visibility,218 value,219 ..220 } => {221 #[derive(Trace)]222 struct UnboundValue<B: Trace> {223 uctx: B,224 value: Rc<Expr>,225 name: IStr,226 }227 impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {228 type Bound = Val;229 fn bind(&self, sup_this: SupThis) -> Result<Val> {230 evaluate_named(self.uctx.bind(sup_this)?, &self.value, self.name.clone())231 }232 }233234 builder235 .field(name.clone())236 .with_add(*plus)237 .with_visibility(*visibility)238 .with_location(field.name.span.clone())239 .bindable(UnboundValue {240 uctx,241 value: value.clone(),242 name,243 })?;244 }245 FieldMember {246 params: Some(params),247 visibility,248 value,249 ..250 } => {251 #[derive(Trace)]252 struct UnboundMethod<B: Trace> {253 uctx: B,254 value: Rc<Expr>,255 params: ExprParams,256 name: IStr,257 }258 impl<B: Unbound<Bound = Context>> Unbound for UnboundMethod<B> {259 type Bound = Val;260 fn bind(&self, sup_this: SupThis) -> Result<Val> {261 Ok(evaluate_method(262 self.uctx.bind(sup_this)?,263 self.name.clone(),264 self.params.clone(),265 self.value.clone(),266 ))267 }268 }269270 builder271 .field(name.clone())272 .with_visibility(*visibility)273 // .with_location(value.span())274 .bindable(UnboundMethod {275 uctx,276 value: value.clone(),277 params: params.clone(),278 name,279 })?;280 }281 }282 Ok(())283}284285#[allow(clippy::too_many_lines)]286pub fn evaluate_member_list_object(287 super_obj: Option<ObjValue>,288 ctx: Context,289 members: &ObjMembers,290) -> Result<ObjValue> {291 let mut builder = ObjValueBuilder::new();292 if let Some(super_obj) = super_obj {293 builder.with_super(super_obj);294 }295296 let locals = members.locals.clone();297298 // We have single context for all fields, so we can cache binds299 let uctx = CachedUnbound::new(evaluate_object_locals(ctx.clone(), locals));300301 for field in &members.fields {302 evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;303 }304305 if !members.asserts.is_empty() {306 #[derive(Trace)]307 struct ObjectAssert<B: Trace> {308 uctx: B,309 asserts: Rc<Vec<AssertStmt>>,310 }311 impl<B: Unbound<Bound = Context>> ObjectAssertion for ObjectAssert<B> {312 fn run(&self, sup_this: SupThis) -> Result<()> {313 let ctx = self.uctx.bind(sup_this)?;314 for assert in &*self.asserts {315 evaluate_assert(ctx.clone(), assert)?;316 }317 Ok(())318 }319 }320 builder.assert(ObjectAssert {321 uctx,322 asserts: members.asserts.clone(),323 });324 }325326 Ok(builder.build())327}328329pub fn evaluate_object(330 super_obj: Option<ObjValue>,331 ctx: Context,332 object: &ObjBody,333) -> Result<ObjValue> {334 Ok(match object {335 ObjBody::MemberList(members) => evaluate_member_list_object(super_obj, ctx, members)?,336 ObjBody::ObjComp(obj) => {337 let mut builder = ObjValueBuilder::new();338 if let Some(super_obj) = super_obj {339 builder.with_super(super_obj);340 }341 let locals = obj.locals.clone();342 evaluate_comp(ctx, &obj.compspecs, &mut |ctx| {343 let uctx = evaluate_object_locals(ctx.clone(), locals.clone());344345 evaluate_field_member(&mut builder, ctx, uctx, &obj.field)346 })?;347348 builder.build()349 }350 })351}352353pub fn evaluate_apply(354 ctx: Context,355 value: &Expr,356 args: &ArgsDesc,357 loc: CallLocation<'_>,358 tailstrict: bool,359) -> Result<Val> {360 let value = evaluate(ctx.clone(), value)?;361 Ok(match value {362 Val::Func(f) => {363 let body = || f.evaluate(ctx, loc, args, tailstrict);364 if tailstrict {365 body()?366 } else {367 in_frame(loc, || format!("function <{}> call", f.name()), body)?368 }369 }370 v => bail!(OnlyFunctionsCanBeCalledGot(v.value_type())),371 })372}373374pub fn evaluate_assert(ctx: Context, assertion: &AssertStmt) -> Result<()> {375 let value = &assertion.0;376 let msg = &assertion.1;377 let assertion_result = in_frame(378 CallLocation::new(&value.span),379 || "assertion condition".to_owned(),380 || bool::from_untyped(evaluate(ctx.clone(), value)?),381 )?;382 if !assertion_result {383 in_frame(384 CallLocation::new(&value.span),385 || "assertion failure".to_owned(),386 || {387 if let Some(msg) = msg {388 bail!(AssertionFailed(evaluate(ctx, msg)?.to_string()?));389 }390 bail!(AssertionFailed(Val::Null.to_string()?));391 },392 )?;393 }394 Ok(())395}396397pub fn evaluate_named_param(ctx: Context, expr: &Expr, name: ParamName) -> Result<Val> {398 match name {399 ParamName::Named(name) => evaluate_named(ctx, expr, name),400 ParamName::Unnamed => evaluate(ctx, expr),401 }402}403404pub fn evaluate_named(ctx: Context, expr: &Expr, name: IStr) -> Result<Val> {405 use Expr::*;406 Ok(match expr {407 Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),408 _ => evaluate(ctx, expr)?,409 })410}411412#[allow(clippy::too_many_lines)]413pub fn evaluate(ctx: Context, expr: &Expr) -> Result<Val> {414 use Expr::*;415416 if let Some(trivial) = evaluate_trivial(expr) {417 return Ok(trivial);418 }419 Ok(match expr {420 Literal(LiteralType::This) => Val::Obj(ctx.try_this()?),421 Literal(LiteralType::Super) => Val::Obj(ctx.try_sup_this()?.standalone_super()?),422 Literal(LiteralType::Dollar) => Val::Obj(ctx.try_dollar()?),423 Literal(LiteralType::True) => Val::Bool(true),424 Literal(LiteralType::False) => Val::Bool(false),425 Literal(LiteralType::Null) => Val::Null,426 Str(v) => Val::string(v.clone()),427 Num(v) => Val::try_num(*v)?,428 // I have tried to remove special behavior from super by implementing standalone-super429 // expresion, but looks like this case still needs special treatment.430 //431 // Note that other jsonnet implementations will fail on `if value in (super)` expression,432 // because the standalone super literal is not supported, that is because in other433 // implementations `in super` treated differently from `in smth_else`.434 BinaryOp(bin)435 if matches!(&bin.rhs, Expr::Literal(LiteralType::Super))436 && bin.op == BinaryOpType::In =>437 {438 let sup_this = ctx.try_sup_this()?;439 // In jsonnet, "field" in e is eager, LHS expression is always executed regardless of super existence.440 // In jrsonnet, however, this wasn't true, this was kept here for compatibility.441 if !sup_this.has_super() {442 return Ok(Val::Bool(false));443 }444 let field = evaluate(ctx, &bin.lhs)?;445 Val::Bool(sup_this.field_in_super(field.to_string()?))446 }447 BinaryOp(bin) => evaluate_binary_op_special(ctx, &bin.lhs, bin.op, &bin.rhs)?,448 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,449 Var(name) => in_frame(450 CallLocation::new(&name.span),451 || format!("local <{}> access", &**name),452 || ctx.binding((**name).clone())?.evaluate(),453 )?,454 Index { indexable, parts } => ensure_sufficient_stack(|| {455 let mut parts = parts.iter();456 let mut indexable = if matches!(&**indexable, Expr::Literal(LiteralType::Super)) {457 let part = parts.next().expect("at least part should exist");458 // sup_this existence check might also be skipped here for null-coalesce...459 // But I believe this might cause errors.460 let sup_this = ctx.try_sup_this()?;461 if !sup_this.has_super() {462 #[cfg(feature = "exp-null-coaelse")]463 if part.null_coaelse {464 return Ok(Val::Null);465 }466 bail!(NoSuperFound)467 }468 let name = evaluate(ctx.clone(), &part.value)?;469470 let Val::Str(name) = name else {471 bail!(ValueIndexMustBeTypeGot(472 ValType::Obj,473 ValType::Str,474 name.value_type(),475 ))476 };477478 let name = name.into_flat();479 match sup_this480 .get_super(name.clone())481 .with_description_src(&part.span, || format!("field <{name}> access"))?482 {483 Some(v) => v,484 #[cfg(feature = "exp-null-coaelse")]485 None if part.null_coaelse => return Ok(Val::Null),486 None => {487 let suggestions = suggest_object_fields(488 &sup_this.standalone_super().expect("super exists"),489 name.clone(),490 );491492 bail!(NoSuchField(name, suggestions))493 }494 }495 } else {496 evaluate(ctx.clone(), indexable)?497 };498499 for part in parts {500 indexable = match (indexable, evaluate(ctx.clone(), &part.value)?) {501 (Val::Obj(v), Val::Str(key)) => match v502 .get(key.clone().into_flat())503 .with_description_src(&part.span, || format!("field <{key}> access"))?504 {505 Some(v) => v,506 #[cfg(feature = "exp-null-coaelse")]507 None if part.null_coaelse => return Ok(Val::Null),508 None => {509 let suggestions = suggest_object_fields(&v, key.clone().into_flat());510511 return Err(Error::from(NoSuchField(512 key.clone().into_flat(),513 suggestions,514 )))515 .with_description_src(&part.span, || format!("field <{key}> access"));516 }517 },518 (Val::Obj(_), n) => bail!(ValueIndexMustBeTypeGot(519 ValType::Obj,520 ValType::Str,521 n.value_type(),522 )),523 (Val::Arr(v), Val::Num(n)) => {524 let n = n.get();525 if n.fract() > f64::EPSILON {526 bail!(FractionalIndex)527 }528 if n < 0.0 {529 bail!(ArrayBoundsError(n as isize, v.len()));530 }531 v.get(n as usize)?532 .ok_or_else(|| ArrayBoundsError(n as isize, v.len()))?533 }534 (Val::Arr(_), Val::Str(n)) => {535 bail!(AttemptedIndexAnArrayWithString(n.into_flat()))536 }537 (Val::Arr(_), n) => bail!(ValueIndexMustBeTypeGot(538 ValType::Arr,539 ValType::Num,540 n.value_type(),541 )),542543 (Val::Str(s), Val::Num(n)) => Val::Str({544 let n = n.get();545 if n.fract() > f64::EPSILON {546 bail!(FractionalIndex)547 }548 if n < 0.0 {549 bail!(ArrayBoundsError(n as isize, s.into_flat().chars().count()));550 }551 let v: IStr = s552 .clone()553 .into_flat()554 .chars()555 .skip(n as usize)556 .take(1)557 .collect::<String>()558 .into();559 if v.is_empty() {560 bail!(StringBoundsError(n as usize, s.into_flat().chars().count()))561 }562 StrValue::Flat(v)563 }),564 (Val::Str(_), n) => bail!(ValueIndexMustBeTypeGot(565 ValType::Str,566 ValType::Num,567 n.value_type(),568 )),569 #[cfg(feature = "exp-null-coaelse")]570 (Val::Null, _) if part.null_coaelse => return Ok(Val::Null),571 (v, _) => bail!(CantIndexInto(v.value_type())),572 };573 }574 Ok(indexable)575 })?,576 LocalExpr(bindings, returned) => {577 let mut new_bindings: FxHashMap<IStr, Thunk<Val>> =578 FxHashMap::with_capacity(bindings.iter().map(BindSpec::binds_len).sum());579 let fctx = Context::new_future();580 for b in bindings {581 evaluate_dest(b, fctx.clone(), &mut new_bindings)?;582 }583 let ctx = ctx.extend_bindings(new_bindings).into_future(fctx);584 evaluate(ctx, returned)?585 }586 Arr(items) => {587 if items.is_empty() {588 Val::Arr(ArrValue::empty())589 } else {590 Val::Arr(ArrValue::expr(ctx, items.clone()))591 }592 }593 ArrComp(expr, comp_specs) => {594 let mut out = Vec::new();595 evaluate_comp(ctx, comp_specs, &mut |ctx| {596 let expr = expr.clone();597 out.push(Thunk!(move || evaluate(ctx, &expr)));598 Ok(())599 })?;600 Val::Arr(ArrValue::lazy(out))601 }602 Obj(body) => Val::Obj(evaluate_object(None, ctx, body)?),603 ObjExtend(a, b) => {604 let base = evaluate(ctx.clone(), a)?;605 match base {606 Val::Obj(base_obj) => Val::Obj(evaluate_object(Some(base_obj), ctx, b)?),607 _ => bail!("ObjExtend lhs should be an object value"),608 }609 }610 Apply(value, args, tailstrict) => ensure_sufficient_stack(|| {611 evaluate_apply(ctx, value, args, CallLocation::new(&args.span), *tailstrict)612 })?,613 Function(params, body) => {614 evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())615 }616 AssertExpr(assert) => {617 evaluate_assert(ctx.clone(), &assert.assert)?;618 evaluate(ctx, &assert.rest)?619 }620 ErrorStmt(s, e) => in_frame(621 CallLocation::new(s),622 || "error statement".to_owned(),623 || bail!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),624 )?,625 IfElse(if_else) => {626 if in_frame(627 CallLocation::new(&if_else.cond.span),628 || "if condition".to_owned(),629 || bool::from_untyped(evaluate(ctx.clone(), &if_else.cond.cond)?),630 )? {631 evaluate(ctx, &if_else.cond_then)?632 } else {633 match &if_else.cond_else {634 Some(v) => evaluate(ctx, v)?,635 None => Val::Null,636 }637 }638 }639 Slice(slice) => {640 fn parse_idx<T: Typed + FromUntyped>(641 ctx: Context,642 expr: Option<&Spanned<Expr>>,643 desc: &'static str,644 ) -> Result<Option<T>> {645 if let Some(value) = expr {646 Ok(in_frame(647 CallLocation::new(&value.span),648 || format!("slice {desc}"),649 || <Option<T>>::from_untyped(evaluate(ctx, value)?),650 )?)651 } else {652 Ok(None)653 }654 }655656 let indexable = evaluate(ctx.clone(), &slice.value)?;657658 let start = parse_idx(ctx.clone(), slice.slice.start.as_ref(), "start")?;659 let end = parse_idx(ctx.clone(), slice.slice.end.as_ref(), "end")?;660 let step = parse_idx(ctx, slice.slice.step.as_ref(), "step")?;661662 IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?663 }664 Import(kind, path) => {665 let Expr::Str(path) = &**path else {666 bail!("computed imports are not supported")667 };668 with_state(|s| {669 let span = &kind.span;670 let resolved_path = s.resolve_from(span.0.source_path(), path)?;671 Ok(match &**kind {672 ImportKind::Normal => in_frame(673 CallLocation::new(span),674 || format!("import {:?}", path.clone()),675 || s.import_resolved(resolved_path),676 )?,677 ImportKind::Str => Val::string(s.import_resolved_str(resolved_path)?),678 ImportKind::Bin => {679 Val::Arr(ArrValue::bytes(s.import_resolved_bin(resolved_path)?))680 }681 }) as Result<Val>682 })?683 }684 })685}1use std::rc::Rc;23use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_ir::{6 function::ParamName, ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, ExprParams,7 FieldMember, FieldName, ForSpecData, IfSpecData, ImportKind, LiteralType, ObjBody, ObjMembers,8 Spanned,9};10use jrsonnet_types::ValType;11use rustc_hash::FxHashMap;1213use self::destructure::destruct;14use crate::{15 arr::ArrValue,16 bail,17 destructure::evaluate_dest,18 error::{suggest_object_fields, ErrorKind::*},19 evaluate::operator::{evaluate_binary_op_special, evaluate_unary_op},20 function::{CallLocation, FuncDesc, FuncVal},21 gc::WithCapacityExt as _,22 in_frame,23 typed::{FromUntyped, IntoUntyped as _, Typed},24 val::{CachedUnbound, IndexableVal, NumValue, StrValue, Thunk},25 with_state, Context, Error, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result,26 ResultExt, SupThis, Unbound, Val,27};28pub mod destructure;29pub mod operator;3031// This is the amount of bytes that need to be left on the stack before increasing the size.32// It must be at least as large as the stack required by any code that does not call33// `ensure_sufficient_stack`.34const RED_ZONE: usize = 100 * 1024; // 100k3536// Only the first stack that is pushed, grows exponentially (2^n * STACK_PER_RECURSION) from then37// on. This flag has performance relevant characteristics. Don't set it too high.38const STACK_PER_RECURSION: usize = 1024 * 1024; // 1MB3940/// Grows the stack on demand to prevent stack overflow. Call this in strategic locations41/// to "break up" recursive calls. E.g. almost any call to `visit_expr` or equivalent can benefit42/// from this.43///44/// Should not be sprinkled around carelessly, as it causes a little bit of overhead.45#[inline]46pub fn ensure_sufficient_stack<R>(f: impl FnOnce() -> R) -> R {47 stacker::maybe_grow(RED_ZONE, STACK_PER_RECURSION, f)48}4950pub fn evaluate_trivial(expr: &Expr) -> Option<Val> {51 fn is_trivial(expr: &Expr) -> bool {52 match expr {53 Expr::Str(_)54 | Expr::Num(_)55 | Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,56 Expr::Arr(a) => a.iter().all(is_trivial),57 _ => false,58 }59 }60 Some(match expr {61 Expr::Str(s) => Val::string(s.clone()),62 Expr::Num(n) => {63 Val::Num(NumValue::new(*n).expect("parser will not allow non-finite values"))64 }65 Expr::Literal(LiteralType::False) => Val::Bool(false),66 Expr::Literal(LiteralType::True) => Val::Bool(true),67 Expr::Literal(LiteralType::Null) => Val::Null,68 Expr::Arr(n) => {69 if n.iter().any(|e| !is_trivial(e)) {70 return None;71 }72 Val::Arr(ArrValue::eager(73 n.iter()74 .map(evaluate_trivial)75 .map(|e| e.expect("checked trivial"))76 .collect(),77 ))78 }79 _ => return None,80 })81}8283pub fn evaluate_method(ctx: Context, name: IStr, params: ExprParams, body: Rc<Expr>) -> Val {84 Val::Func(FuncVal::Normal(Cc::new(FuncDesc {85 name,86 ctx,87 params,88 body,89 })))90}9192pub fn evaluate_field_name(ctx: Context, field_name: &Spanned<FieldName>) -> Result<Option<IStr>> {93 Ok(match &field_name.value {94 FieldName::Fixed(n) => Some(n.clone()),95 FieldName::Dyn(expr) => in_frame(96 CallLocation::new(&field_name.span),97 || "evaluating field name".to_string(),98 || {99 let v = evaluate(ctx, expr)?;100 Ok(if matches!(v, Val::Null) {101 None102 } else {103 Some(IStr::from_untyped(v)?)104 })105 },106 )?,107 })108}109110pub fn evaluate_comp(111 ctx: Context,112 specs: &[CompSpec],113 callback: &mut impl FnMut(Context) -> Result<()>,114) -> Result<()> {115 match specs.first() {116 None => callback(ctx)?,117 Some(CompSpec::IfSpec(IfSpecData { cond, span: _ })) => {118 if bool::from_untyped(evaluate(ctx.clone(), cond)?)? {119 evaluate_comp(ctx, &specs[1..], callback)?;120 }121 }122 Some(CompSpec::ForSpec(ForSpecData {123 destruct: into,124 over,125 })) => {126 match evaluate(ctx.clone(), over)? {127 Val::Arr(list) => {128 for item in list.iter_lazy() {129 let fctx = Pending::new();130 let mut new_bindings = FxHashMap::with_capacity(into.binds_len());131 destruct(into, item, fctx.clone(), &mut new_bindings)?;132 let ctx = ctx.clone().extend_bindings(new_bindings).into_future(fctx);133134 evaluate_comp(ctx, &specs[1..], callback)?;135 }136 }137 #[cfg(feature = "exp-object-iteration")]138 Val::Obj(obj) => {139 for field in obj.fields(140 // TODO: Should there be ability to preserve iteration order?141 #[cfg(feature = "exp-preserve-order")]142 false,143 ) {144 let fctx = Pending::new();145 let mut new_bindings = FxHashMap::with_capacity(into.binds_len());146 let obj = obj.clone();147 let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(vec![148 Thunk::evaluated(Val::string(field.clone())),149 Thunk!(move || obj.get(field).transpose().expect(150 "field exists, as field name was obtained from object.fields()",151 )),152 ])));153 destruct(into, value, fctx.clone(), &mut new_bindings)?;154 let ctx = ctx.clone().extend_bindings(new_bindings).into_future(fctx);155156 evaluate_comp(ctx, &specs[1..], callback)?;157 }158 }159 _ => bail!(InComprehensionCanOnlyIterateOverArray),160 }161 }162 }163 Ok(())164}165166trait CloneableUnbound<T>: Unbound<Bound = T> + Clone {}167impl<V, T> CloneableUnbound<T> for V where V: Unbound<Bound = T> + Clone {}168169fn evaluate_object_locals(170 fctx: Context,171 locals: Rc<Vec<BindSpec>>,172) -> impl CloneableUnbound<Context> {173 #[derive(Trace, Clone)]174 struct UnboundLocals {175 fctx: Context,176 locals: Rc<Vec<BindSpec>>,177 }178 impl Unbound for UnboundLocals {179 type Bound = Context;180181 fn bind(&self, sup_this: SupThis) -> Result<Context> {182 let fctx = Context::new_future();183 let mut new_bindings =184 FxHashMap::with_capacity(self.locals.iter().map(BindSpec::binds_len).sum());185 for b in self.locals.iter() {186 evaluate_dest(b, fctx.clone(), &mut new_bindings)?;187 }188189 let ctx = self.fctx.clone();190191 let ctx = ctx192 .extend_bindings_sup_this(new_bindings, sup_this)193 .into_future(fctx);194195 Ok(ctx)196 }197 }198199 UnboundLocals { fctx, locals }200}201202pub fn evaluate_field_member<B: Unbound<Bound = Context> + Clone>(203 builder: &mut ObjValueBuilder,204 ctx: Context,205 uctx: B,206 field: &FieldMember,207) -> Result<()> {208 let name = evaluate_field_name(ctx, &field.name)?;209 let Some(name) = name else {210 return Ok(());211 };212213 match field {214 FieldMember {215 plus,216 params: None,217 visibility,218 value,219 ..220 } => {221 #[derive(Trace)]222 struct UnboundValue<B: Trace> {223 uctx: B,224 value: Rc<Expr>,225 name: IStr,226 }227 impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {228 type Bound = Val;229 fn bind(&self, sup_this: SupThis) -> Result<Val> {230 evaluate_named(self.uctx.bind(sup_this)?, &self.value, self.name.clone())231 }232 }233234 builder235 .field(name.clone())236 .with_add(*plus)237 .with_visibility(*visibility)238 .with_location(field.name.span.clone())239 .bindable(UnboundValue {240 uctx,241 value: value.clone(),242 name,243 })?;244 }245 FieldMember {246 params: Some(params),247 visibility,248 value,249 ..250 } => {251 #[derive(Trace)]252 struct UnboundMethod<B: Trace> {253 uctx: B,254 value: Rc<Expr>,255 params: ExprParams,256 name: IStr,257 }258 impl<B: Unbound<Bound = Context>> Unbound for UnboundMethod<B> {259 type Bound = Val;260 fn bind(&self, sup_this: SupThis) -> Result<Val> {261 Ok(evaluate_method(262 self.uctx.bind(sup_this)?,263 self.name.clone(),264 self.params.clone(),265 self.value.clone(),266 ))267 }268 }269270 builder271 .field(name.clone())272 .with_visibility(*visibility)273 // .with_location(value.span())274 .bindable(UnboundMethod {275 uctx,276 value: value.clone(),277 params: params.clone(),278 name,279 })?;280 }281 }282 Ok(())283}284285#[derive(Trace, Clone)]286struct DirectUnbound(Context);287impl Unbound for DirectUnbound {288 type Bound = Context;289 fn bind(&self, sup_this: SupThis) -> Result<Context> {290 Ok(self291 .0292 .clone()293 .extend_bindings_sup_this(FxHashMap::new(), sup_this))294 }295}296297#[allow(clippy::too_many_lines)]298pub fn evaluate_member_list_object(299 super_obj: Option<ObjValue>,300 ctx: Context,301 members: &ObjMembers,302) -> Result<ObjValue> {303 #[derive(Trace)]304 struct ObjectAssert<B: Trace> {305 uctx: B,306 asserts: Rc<Vec<AssertStmt>>,307 }308 impl<B: Unbound<Bound = Context>> ObjectAssertion for ObjectAssert<B> {309 fn run(&self, sup_this: SupThis) -> Result<()> {310 let ctx = self.uctx.bind(sup_this)?;311 for assert in &*self.asserts {312 evaluate_assert(ctx.clone(), assert)?;313 }314 Ok(())315 }316 }317318 let mut builder = ObjValueBuilder::new();319 if let Some(super_obj) = super_obj {320 builder.with_super(super_obj);321 }322323 if members.locals.is_empty() {324 // We can use the same context for all field evaluation, it doesn't depends on locals, only on this/super325 let uctx = DirectUnbound(ctx.clone());326 for field in &members.fields {327 evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;328 }329 if !members.asserts.is_empty() {330 builder.assert(ObjectAssert {331 uctx,332 asserts: members.asserts.clone(),333 });334 }335 } else {336 let locals = members.locals.clone();337 // We have single context for all fields, so we can cache them together338 let uctx = CachedUnbound::new(evaluate_object_locals(ctx.clone(), locals));339 for field in &members.fields {340 evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;341 }342 if !members.asserts.is_empty() {343 builder.assert(ObjectAssert {344 uctx,345 asserts: members.asserts.clone(),346 });347 }348 }349350 Ok(builder.build())351}352353pub fn evaluate_object(354 super_obj: Option<ObjValue>,355 ctx: Context,356 object: &ObjBody,357) -> Result<ObjValue> {358 Ok(match object {359 ObjBody::MemberList(members) => evaluate_member_list_object(super_obj, ctx, members)?,360 ObjBody::ObjComp(obj) => {361 let mut builder = ObjValueBuilder::new();362 if let Some(super_obj) = super_obj {363 builder.with_super(super_obj);364 }365 let locals = obj.locals.clone();366 evaluate_comp(ctx, &obj.compspecs, &mut |ctx| {367 let uctx = evaluate_object_locals(ctx.clone(), locals.clone());368369 evaluate_field_member(&mut builder, ctx, uctx, &obj.field)370 })?;371372 builder.build()373 }374 })375}376377pub fn evaluate_apply(378 ctx: Context,379 value: &Expr,380 args: &ArgsDesc,381 loc: CallLocation<'_>,382 tailstrict: bool,383) -> Result<Val> {384 let value = evaluate(ctx.clone(), value)?;385 Ok(match value {386 Val::Func(f) => {387 let body = || f.evaluate(ctx, loc, args, tailstrict);388 if tailstrict {389 body()?390 } else {391 in_frame(loc, || format!("function <{}> call", f.name()), body)?392 }393 }394 v => bail!(OnlyFunctionsCanBeCalledGot(v.value_type())),395 })396}397398pub fn evaluate_assert(ctx: Context, assertion: &AssertStmt) -> Result<()> {399 let value = &assertion.0;400 let msg = &assertion.1;401 let assertion_result = in_frame(402 CallLocation::new(&value.span),403 || "assertion condition".to_owned(),404 || bool::from_untyped(evaluate(ctx.clone(), value)?),405 )?;406 if !assertion_result {407 in_frame(408 CallLocation::new(&value.span),409 || "assertion failure".to_owned(),410 || {411 if let Some(msg) = msg {412 bail!(AssertionFailed(evaluate(ctx, msg)?.to_string()?));413 }414 bail!(AssertionFailed(Val::Null.to_string()?));415 },416 )?;417 }418 Ok(())419}420421pub fn evaluate_named_param(ctx: Context, expr: &Expr, name: ParamName) -> Result<Val> {422 match name {423 ParamName::Named(name) => evaluate_named(ctx, expr, name),424 ParamName::Unnamed => evaluate(ctx, expr),425 }426}427428pub fn evaluate_named(ctx: Context, expr: &Expr, name: IStr) -> Result<Val> {429 use Expr::*;430 Ok(match expr {431 Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),432 _ => evaluate(ctx, expr)?,433 })434}435436#[allow(clippy::too_many_lines)]437pub fn evaluate(ctx: Context, expr: &Expr) -> Result<Val> {438 use Expr::*;439440 if let Some(trivial) = evaluate_trivial(expr) {441 return Ok(trivial);442 }443 Ok(match expr {444 Literal(LiteralType::This) => Val::Obj(ctx.try_this()?),445 Literal(LiteralType::Super) => Val::Obj(ctx.try_sup_this()?.standalone_super()?),446 Literal(LiteralType::Dollar) => Val::Obj(ctx.try_dollar()?),447 Literal(LiteralType::True) => Val::Bool(true),448 Literal(LiteralType::False) => Val::Bool(false),449 Literal(LiteralType::Null) => Val::Null,450 Str(v) => Val::string(v.clone()),451 Num(v) => Val::try_num(*v)?,452 // I have tried to remove special behavior from super by implementing standalone-super453 // expresion, but looks like this case still needs special treatment.454 //455 // Note that other jsonnet implementations will fail on `if value in (super)` expression,456 // because the standalone super literal is not supported, that is because in other457 // implementations `in super` treated differently from `in smth_else`.458 BinaryOp(bin)459 if matches!(&bin.rhs, Expr::Literal(LiteralType::Super))460 && bin.op == BinaryOpType::In =>461 {462 let sup_this = ctx.try_sup_this()?;463 // In jsonnet, "field" in e is eager, LHS expression is always executed regardless of super existence.464 // In jrsonnet, however, this wasn't true, this was kept here for compatibility.465 if !sup_this.has_super() {466 return Ok(Val::Bool(false));467 }468 let field = evaluate(ctx, &bin.lhs)?;469 Val::Bool(sup_this.field_in_super(field.to_string()?))470 }471 BinaryOp(bin) => evaluate_binary_op_special(ctx, &bin.lhs, bin.op, &bin.rhs)?,472 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,473 Var(name) => in_frame(474 CallLocation::new(&name.span),475 || format!("local <{}> access", &**name),476 || ctx.binding((**name).clone())?.evaluate(),477 )?,478 Index { indexable, parts } => ensure_sufficient_stack(|| {479 let mut parts = parts.iter();480 let mut indexable = if matches!(&**indexable, Expr::Literal(LiteralType::Super)) {481 let part = parts.next().expect("at least part should exist");482 // sup_this existence check might also be skipped here for null-coalesce...483 // But I believe this might cause errors.484 let sup_this = ctx.try_sup_this()?;485 if !sup_this.has_super() {486 #[cfg(feature = "exp-null-coaelse")]487 if part.null_coaelse {488 return Ok(Val::Null);489 }490 bail!(NoSuperFound)491 }492 let name = evaluate(ctx.clone(), &part.value)?;493494 let Val::Str(name) = name else {495 bail!(ValueIndexMustBeTypeGot(496 ValType::Obj,497 ValType::Str,498 name.value_type(),499 ))500 };501502 let name = name.into_flat();503 match sup_this504 .get_super(name.clone())505 .with_description_src(&part.span, || format!("field <{name}> access"))?506 {507 Some(v) => v,508 #[cfg(feature = "exp-null-coaelse")]509 None if part.null_coaelse => return Ok(Val::Null),510 None => {511 let suggestions = suggest_object_fields(512 &sup_this.standalone_super().expect("super exists"),513 name.clone(),514 );515516 bail!(NoSuchField(name, suggestions))517 }518 }519 } else {520 evaluate(ctx.clone(), indexable)?521 };522523 for part in parts {524 indexable = match (indexable, evaluate(ctx.clone(), &part.value)?) {525 (Val::Obj(v), Val::Str(key)) => match v526 .get(key.clone().into_flat())527 .with_description_src(&part.span, || format!("field <{key}> access"))?528 {529 Some(v) => v,530 #[cfg(feature = "exp-null-coaelse")]531 None if part.null_coaelse => return Ok(Val::Null),532 None => {533 let suggestions = suggest_object_fields(&v, key.clone().into_flat());534535 return Err(Error::from(NoSuchField(536 key.clone().into_flat(),537 suggestions,538 )))539 .with_description_src(&part.span, || format!("field <{key}> access"));540 }541 },542 (Val::Obj(_), n) => bail!(ValueIndexMustBeTypeGot(543 ValType::Obj,544 ValType::Str,545 n.value_type(),546 )),547 (Val::Arr(v), Val::Num(n)) => {548 let n = n.get();549 if n.fract() > f64::EPSILON {550 bail!(FractionalIndex)551 }552 if n < 0.0 {553 bail!(ArrayBoundsError(n as isize, v.len()));554 }555 v.get(n as usize)?556 .ok_or_else(|| ArrayBoundsError(n as isize, v.len()))?557 }558 (Val::Arr(_), Val::Str(n)) => {559 bail!(AttemptedIndexAnArrayWithString(n.into_flat()))560 }561 (Val::Arr(_), n) => bail!(ValueIndexMustBeTypeGot(562 ValType::Arr,563 ValType::Num,564 n.value_type(),565 )),566567 (Val::Str(s), Val::Num(n)) => Val::Str({568 let n = n.get();569 if n.fract() > f64::EPSILON {570 bail!(FractionalIndex)571 }572 if n < 0.0 {573 bail!(ArrayBoundsError(n as isize, s.into_flat().chars().count()));574 }575 let v: IStr = s576 .clone()577 .into_flat()578 .chars()579 .skip(n as usize)580 .take(1)581 .collect::<String>()582 .into();583 if v.is_empty() {584 bail!(StringBoundsError(n as usize, s.into_flat().chars().count()))585 }586 StrValue::Flat(v)587 }),588 (Val::Str(_), n) => bail!(ValueIndexMustBeTypeGot(589 ValType::Str,590 ValType::Num,591 n.value_type(),592 )),593 #[cfg(feature = "exp-null-coaelse")]594 (Val::Null, _) if part.null_coaelse => return Ok(Val::Null),595 (v, _) => bail!(CantIndexInto(v.value_type())),596 };597 }598 Ok(indexable)599 })?,600 LocalExpr(bindings, returned) => {601 let mut new_bindings: FxHashMap<IStr, Thunk<Val>> =602 FxHashMap::with_capacity(bindings.iter().map(BindSpec::binds_len).sum());603 let fctx = Context::new_future();604 for b in bindings {605 evaluate_dest(b, fctx.clone(), &mut new_bindings)?;606 }607 let ctx = ctx.extend_bindings(new_bindings).into_future(fctx);608 evaluate(ctx, returned)?609 }610 Arr(items) => {611 if items.is_empty() {612 Val::Arr(ArrValue::empty())613 } else {614 Val::Arr(ArrValue::expr(ctx, items.clone()))615 }616 }617 ArrComp(expr, comp_specs) => {618 let mut out = Vec::new();619 evaluate_comp(ctx, comp_specs, &mut |ctx| {620 let expr = expr.clone();621 out.push(Thunk!(move || evaluate(ctx, &expr)));622 Ok(())623 })?;624 Val::Arr(ArrValue::lazy(out))625 }626 Obj(body) => Val::Obj(evaluate_object(None, ctx, body)?),627 ObjExtend(a, b) => {628 let base = evaluate(ctx.clone(), a)?;629 match base {630 Val::Obj(base_obj) => Val::Obj(evaluate_object(Some(base_obj), ctx, b)?),631 _ => bail!("ObjExtend lhs should be an object value"),632 }633 }634 Apply(value, args, tailstrict) => ensure_sufficient_stack(|| {635 evaluate_apply(ctx, value, args, CallLocation::new(&args.span), *tailstrict)636 })?,637 Function(params, body) => {638 evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())639 }640 AssertExpr(assert) => {641 evaluate_assert(ctx.clone(), &assert.assert)?;642 evaluate(ctx, &assert.rest)?643 }644 ErrorStmt(s, e) => in_frame(645 CallLocation::new(s),646 || "error statement".to_owned(),647 || bail!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),648 )?,649 IfElse(if_else) => {650 if in_frame(651 CallLocation::new(&if_else.cond.span),652 || "if condition".to_owned(),653 || bool::from_untyped(evaluate(ctx.clone(), &if_else.cond.cond)?),654 )? {655 evaluate(ctx, &if_else.cond_then)?656 } else {657 match &if_else.cond_else {658 Some(v) => evaluate(ctx, v)?,659 None => Val::Null,660 }661 }662 }663 Slice(slice) => {664 fn parse_idx<T: Typed + FromUntyped>(665 ctx: Context,666 expr: Option<&Spanned<Expr>>,667 desc: &'static str,668 ) -> Result<Option<T>> {669 if let Some(value) = expr {670 Ok(in_frame(671 CallLocation::new(&value.span),672 || format!("slice {desc}"),673 || <Option<T>>::from_untyped(evaluate(ctx, value)?),674 )?)675 } else {676 Ok(None)677 }678 }679680 let indexable = evaluate(ctx.clone(), &slice.value)?;681682 let start = parse_idx(ctx.clone(), slice.slice.start.as_ref(), "start")?;683 let end = parse_idx(ctx.clone(), slice.slice.end.as_ref(), "end")?;684 let step = parse_idx(ctx, slice.slice.step.as_ref(), "step")?;685686 IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?687 }688 Import(kind, path) => {689 let Expr::Str(path) = &**path else {690 bail!("computed imports are not supported")691 };692 with_state(|s| {693 let span = &kind.span;694 let resolved_path = s.resolve_from(span.0.source_path(), path)?;695 Ok(match &**kind {696 ImportKind::Normal => in_frame(697 CallLocation::new(span),698 || format!("import {:?}", path.clone()),699 || s.import_resolved(resolved_path),700 )?,701 ImportKind::Str => Val::string(s.import_resolved_str(resolved_path)?),702 ImportKind::Bin => {703 Val::Arr(ArrValue::bytes(s.import_resolved_bin(resolved_path)?))704 }705 }) as Result<Val>706 })?707 }708 })709}