1use std::rc::Rc;23use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_ir::{6 ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, ExprParams, FieldMember,7 FieldName, ForSpecData, IfSpecData, ImportKind, LiteralType, ObjBody, ObjMembers, Spanned,8 function::ParamName,9};10use jrsonnet_types::ValType;11use rustc_hash::FxHashMap;1213use self::destructure::destruct;14use crate::{15 Context, Error, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, ResultExt,16 SupThis, Unbound, Val,17 arr::ArrValue,18 bail,19 destructure::evaluate_dest,20 error::{ErrorKind::*, suggest_object_fields},21 evaluate::operator::{evaluate_binary_op_special, evaluate_unary_op},22 function::{CallLocation, FuncDesc, FuncVal},23 gc::WithCapacityExt as _,24 in_frame,25 typed::{FromUntyped, IntoUntyped as _, Typed},26 val::{CachedUnbound, IndexableVal, NumValue, StrValue, Thunk},27 with_state,28};29pub mod destructure;30pub mod operator;3132333435const RED_ZONE: usize = 100 * 1024; 36373839const STACK_PER_RECURSION: usize = 1024 * 1024; 40414243444546#[inline]47pub fn ensure_sufficient_stack<R>(f: impl FnOnce() -> R) -> R {48 stacker::maybe_grow(RED_ZONE, STACK_PER_RECURSION, f)49}5051pub fn evaluate_trivial(expr: &Expr) -> Option<Val> {52 fn is_trivial(expr: &Expr) -> bool {53 match expr {54 Expr::Str(_)55 | Expr::Num(_)56 | Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,57 Expr::Arr(a) => a.iter().all(is_trivial),58 _ => false,59 }60 }61 Some(match expr {62 Expr::Str(s) => Val::string(s.clone()),63 Expr::Num(n) => {64 Val::Num(NumValue::new(*n).expect("parser will not allow non-finite values"))65 }66 Expr::Literal(LiteralType::False) => Val::Bool(false),67 Expr::Literal(LiteralType::True) => Val::Bool(true),68 Expr::Literal(LiteralType::Null) => Val::Null,69 Expr::Arr(n) => {70 if n.iter().any(|e| !is_trivial(e)) {71 return None;72 }73 Val::Arr(74 n.iter()75 .map(evaluate_trivial)76 .map(|e| e.expect("checked trivial"))77 .collect(),78 )79 }80 _ => return None,81 })82}8384pub fn evaluate_method(ctx: Context, name: IStr, params: ExprParams, body: Rc<Expr>) -> Val {85 Val::Func(FuncVal::Normal(Cc::new(FuncDesc {86 name,87 ctx,88 params,89 body,90 })))91}9293pub fn evaluate_field_name(ctx: Context, field_name: &Spanned<FieldName>) -> Result<Option<IStr>> {94 Ok(match &field_name.value {95 FieldName::Fixed(n) => Some(n.clone()),96 FieldName::Dyn(expr) => in_frame(97 CallLocation::new(&field_name.span),98 || "evaluating field name".to_string(),99 || {100 let v = evaluate(ctx, expr)?;101 Ok(if matches!(v, Val::Null) {102 None103 } else {104 Some(IStr::from_untyped(v)?)105 })106 },107 )?,108 })109}110111pub fn evaluate_comp(112 ctx: Context,113 specs: &[CompSpec],114 mut guaranteed_reserve: usize,115 callback: &mut impl FnMut(Context, usize) -> Result<()>,116) -> Result<()> {117 match specs.first() {118 None => callback(ctx, guaranteed_reserve)?,119 Some(CompSpec::IfSpec(IfSpecData { cond, span: _ })) => {120 if bool::from_untyped(evaluate(ctx.clone(), cond)?)? {121 evaluate_comp(ctx, &specs[1..], 0, callback)?;122 }123 }124 Some(CompSpec::ForSpec(ForSpecData {125 destruct: into,126 over,127 })) => {128 match evaluate(ctx.clone(), over)? {129 Val::Arr(list) => {130 guaranteed_reserve = guaranteed_reserve.max(1) * list.len();131 for (i, item) in list.iter_lazy().enumerate() {132 let fctx = Pending::new();133 let mut new_bindings = FxHashMap::with_capacity(into.binds_len());134 destruct(into, item, fctx.clone(), &mut new_bindings)?;135 let ctx = ctx.clone().extend_bindings(new_bindings).into_future(fctx);136137 let specs = &specs[1..];138 evaluate_comp(139 ctx,140 specs,141 if i == 0 || !specs.is_empty() {142 guaranteed_reserve143 } else {144 0145 },146 callback,147 )?;148 }149 }150 #[cfg(feature = "exp-object-iteration")]151 Val::Obj(obj) => {152 let fields = obj.fields(153 154 #[cfg(feature = "exp-preserve-order")]155 false,156 );157 guaranteed_reserve = guaranteed_reserve.max(1) * fields.len();158 for field in fields {159 let fctx = Pending::new();160 let mut new_bindings = FxHashMap::with_capacity(into.binds_len());161 let obj = obj.clone();162 let value = Thunk::evaluated(Val::arr(vec![163 Thunk::evaluated(Val::string(field.clone())),164 obj.get_lazy(field).transpose().expect(165 "field exists, as field name was obtained from object.fields()",166 ),167 ]));168 destruct(into, value, fctx.clone(), &mut new_bindings)?;169 let ctx = ctx.clone().extend_bindings(new_bindings).into_future(fctx);170171 evaluate_comp(ctx, &specs[1..], callback)?;172 }173 }174 _ => bail!(InComprehensionCanOnlyIterateOverArray),175 }176 }177 }178 Ok(())179}180181fn evaluate_arr_comp(ctx: Context, expr: &Rc<Expr>, comp_specs: &[CompSpec]) -> Result<ArrValue> {182 'eager: {183 let mut out = Vec::new();184185 if evaluate_comp(ctx.clone(), comp_specs, 0, &mut |ctx, reserve| {186 if reserve != 0 {187 out.reserve(reserve);188 }189 out.push(evaluate(ctx, expr)?);190 Ok(())191 })192 .is_err()193 {194 break 'eager;195 }196197 return Ok(ArrValue::new(out));198 };199 let mut out = Vec::new();200 evaluate_comp(ctx, comp_specs, 0, &mut |ctx, reserve| {201 if reserve != 0 {202 out.reserve(reserve);203 }204 let expr = expr.clone();205 out.push(Thunk!(move || evaluate(ctx, &expr)));206 Ok(())207 })?;208 Ok(ArrValue::new(out))209}210211trait CloneableUnbound<T>: Unbound<Bound = T> + Clone {}212impl<V, T> CloneableUnbound<T> for V where V: Unbound<Bound = T> + Clone {}213214fn evaluate_object_locals(215 fctx: Context,216 locals: Rc<Vec<BindSpec>>,217) -> impl CloneableUnbound<Context> {218 #[derive(Trace, Clone)]219 struct UnboundLocals {220 fctx: Context,221 locals: Rc<Vec<BindSpec>>,222 }223 impl Unbound for UnboundLocals {224 type Bound = Context;225226 fn bind(&self, sup_this: SupThis) -> Result<Context> {227 let fctx = Context::new_future();228 let mut new_bindings =229 FxHashMap::with_capacity(self.locals.iter().map(BindSpec::binds_len).sum());230 for b in self.locals.iter() {231 evaluate_dest(b, fctx.clone(), &mut new_bindings)?;232 }233234 let ctx = self.fctx.clone();235236 let ctx = ctx237 .extend_bindings_sup_this(new_bindings, sup_this)238 .into_future(fctx);239240 Ok(ctx)241 }242 }243244 UnboundLocals { fctx, locals }245}246247pub fn evaluate_field_member<B: Unbound<Bound = Context> + Clone>(248 builder: &mut ObjValueBuilder,249 ctx: Context,250 uctx: B,251 field: &FieldMember,252) -> Result<()> {253 let name = evaluate_field_name(ctx, &field.name)?;254 let Some(name) = name else {255 return Ok(());256 };257258 match field {259 FieldMember {260 plus,261 params: None,262 visibility,263 value,264 ..265 } => {266 #[derive(Trace)]267 struct UnboundValue<B: Trace> {268 uctx: B,269 value: Rc<Expr>,270 name: IStr,271 }272 impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {273 type Bound = Val;274 fn bind(&self, sup_this: SupThis) -> Result<Val> {275 evaluate_named(self.uctx.bind(sup_this)?, &self.value, self.name.clone())276 }277 }278279 builder280 .field(name.clone())281 .with_add(*plus)282 .with_visibility(*visibility)283 .with_location(field.name.span.clone())284 .bindable(UnboundValue {285 uctx,286 value: value.clone(),287 name,288 })?;289 }290 FieldMember {291 params: Some(params),292 visibility,293 value,294 ..295 } => {296 #[derive(Trace)]297 struct UnboundMethod<B: Trace> {298 uctx: B,299 value: Rc<Expr>,300 params: ExprParams,301 name: IStr,302 }303 impl<B: Unbound<Bound = Context>> Unbound for UnboundMethod<B> {304 type Bound = Val;305 fn bind(&self, sup_this: SupThis) -> Result<Val> {306 Ok(evaluate_method(307 self.uctx.bind(sup_this)?,308 self.name.clone(),309 self.params.clone(),310 self.value.clone(),311 ))312 }313 }314315 builder316 .field(name.clone())317 .with_visibility(*visibility)318 319 .bindable(UnboundMethod {320 uctx,321 value: value.clone(),322 params: params.clone(),323 name,324 })?;325 }326 }327 Ok(())328}329330#[derive(Trace, Clone)]331struct DirectUnbound(Context);332impl Unbound for DirectUnbound {333 type Bound = Context;334 fn bind(&self, sup_this: SupThis) -> Result<Context> {335 Ok(self336 .0337 .clone()338 .extend_bindings_sup_this(FxHashMap::new(), sup_this))339 }340}341342#[allow(clippy::too_many_lines)]343pub fn evaluate_member_list_object(344 super_obj: Option<ObjValue>,345 ctx: Context,346 members: &ObjMembers,347) -> Result<ObjValue> {348 #[derive(Trace)]349 struct ObjectAssert<B: Trace> {350 uctx: B,351 asserts: Rc<Vec<AssertStmt>>,352 }353 impl<B: Unbound<Bound = Context>> ObjectAssertion for ObjectAssert<B> {354 fn run(&self, sup_this: SupThis) -> Result<()> {355 let ctx = self.uctx.bind(sup_this)?;356 for assert in &*self.asserts {357 evaluate_assert(ctx.clone(), assert)?;358 }359 Ok(())360 }361 }362363 let mut builder = ObjValueBuilder::new();364 if let Some(super_obj) = super_obj {365 builder.with_super(super_obj);366 }367368 if members.locals.is_empty() {369 370 let uctx = DirectUnbound(ctx.clone());371 for field in &members.fields {372 evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;373 }374 if !members.asserts.is_empty() {375 builder.assert(ObjectAssert {376 uctx,377 asserts: members.asserts.clone(),378 });379 }380 } else {381 let locals = members.locals.clone();382 383 let uctx = CachedUnbound::new(evaluate_object_locals(ctx.clone(), locals));384 for field in &members.fields {385 evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;386 }387 if !members.asserts.is_empty() {388 builder.assert(ObjectAssert {389 uctx,390 asserts: members.asserts.clone(),391 });392 }393 }394395 Ok(builder.build())396}397398pub fn evaluate_object(399 super_obj: Option<ObjValue>,400 ctx: Context,401 object: &ObjBody,402) -> Result<ObjValue> {403 Ok(match object {404 ObjBody::MemberList(members) => evaluate_member_list_object(super_obj, ctx, members)?,405 ObjBody::ObjComp(obj) => {406 let mut builder = ObjValueBuilder::new();407 if let Some(super_obj) = super_obj {408 builder.with_super(super_obj);409 }410 let locals = obj.locals.clone();411 evaluate_comp(ctx, &obj.compspecs, 0, &mut |ctx, reserve| {412 let uctx = evaluate_object_locals(ctx.clone(), locals.clone());413 builder.reserve_cores(reserve);414415 evaluate_field_member(&mut builder, ctx, uctx, &obj.field)416 })?;417418 builder.build()419 }420 })421}422423pub fn evaluate_apply(424 ctx: Context,425 value: &Expr,426 args: &ArgsDesc,427 loc: CallLocation<'_>,428 tailstrict: bool,429) -> Result<Val> {430 let value = evaluate(ctx.clone(), value)?;431 Ok(match value {432 Val::Func(f) => {433 let body = || f.evaluate(ctx, loc, args, tailstrict);434 if tailstrict {435 body()?436 } else {437 in_frame(loc, || format!("function <{}> call", f.name()), body)?438 }439 }440 v => bail!(OnlyFunctionsCanBeCalledGot(v.value_type())),441 })442}443444pub fn evaluate_assert(ctx: Context, assertion: &AssertStmt) -> Result<()> {445 let value = &assertion.0;446 let msg = &assertion.1;447 let assertion_result = in_frame(448 CallLocation::new(&value.span),449 || "assertion condition".to_owned(),450 || bool::from_untyped(evaluate(ctx.clone(), value)?),451 )?;452 if !assertion_result {453 in_frame(454 CallLocation::new(&value.span),455 || "assertion failure".to_owned(),456 || {457 if let Some(msg) = msg {458 bail!(AssertionFailed(evaluate(ctx, msg)?.to_string()?));459 }460 bail!(AssertionFailed(Val::Null.to_string()?));461 },462 )?;463 }464 Ok(())465}466467pub fn evaluate_named_param(ctx: Context, expr: &Expr, name: ParamName) -> Result<Val> {468 match name {469 ParamName::Named(name) => evaluate_named(ctx, expr, name),470 ParamName::Unnamed => evaluate(ctx, expr),471 }472}473474pub fn evaluate_named(ctx: Context, expr: &Expr, name: IStr) -> Result<Val> {475 use Expr::*;476 Ok(match expr {477 Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),478 _ => evaluate(ctx, expr)?,479 })480}481482#[allow(clippy::too_many_lines)]483pub fn evaluate(ctx: Context, expr: &Expr) -> Result<Val> {484 use Expr::*;485486 Ok(match expr {487 Literal(LiteralType::This) => Val::Obj(ctx.try_this()?),488 Literal(LiteralType::Super) => Val::Obj(ctx.try_sup_this()?.standalone_super()?),489 Literal(LiteralType::Dollar) => Val::Obj(ctx.try_dollar()?),490 Literal(LiteralType::True) => Val::Bool(true),491 Literal(LiteralType::False) => Val::Bool(false),492 Literal(LiteralType::Null) => Val::Null,493 Str(v) => Val::string(v.clone()),494 Num(v) => Val::try_num(*v)?,495 496 497 498 499 500 501 BinaryOp(bin)502 if matches!(&bin.rhs, Expr::Literal(LiteralType::Super))503 && bin.op == BinaryOpType::In =>504 {505 let sup_this = ctx.try_sup_this()?;506 507 508 if !sup_this.has_super() {509 return Ok(Val::Bool(false));510 }511 let field = evaluate(ctx, &bin.lhs)?;512 Val::Bool(sup_this.field_in_super(field.to_string()?))513 }514 BinaryOp(bin) => evaluate_binary_op_special(ctx, &bin.lhs, bin.op, &bin.rhs)?,515 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,516 Var(name) => in_frame(517 CallLocation::new(&name.span),518 || format!("local <{}> access", &**name),519 || ctx.binding((**name).clone())?.evaluate(),520 )?,521 Index { indexable, parts } => ensure_sufficient_stack(|| {522 let mut parts = parts.iter();523 let mut indexable = if matches!(&**indexable, Expr::Literal(LiteralType::Super)) {524 let part = parts.next().expect("at least part should exist");525 526 527 let sup_this = ctx.try_sup_this()?;528 if !sup_this.has_super() {529 #[cfg(feature = "exp-null-coaelse")]530 if part.null_coaelse {531 return Ok(Val::Null);532 }533 bail!(NoSuperFound)534 }535 let name = evaluate(ctx.clone(), &part.value)?;536537 let Val::Str(name) = name else {538 bail!(ValueIndexMustBeTypeGot(539 ValType::Obj,540 ValType::Str,541 name.value_type(),542 ))543 };544545 let name = name.into_flat();546 match sup_this547 .get_super(name.clone())548 .with_description_src(&part.span, || format!("field <{name}> access"))?549 {550 Some(v) => v,551 #[cfg(feature = "exp-null-coaelse")]552 None if part.null_coaelse => return Ok(Val::Null),553 None => {554 let suggestions = suggest_object_fields(555 &sup_this.standalone_super().expect("super exists"),556 name.clone(),557 );558559 bail!(NoSuchField(name, suggestions))560 }561 }562 } else {563 evaluate(ctx.clone(), indexable)?564 };565566 for part in parts {567 indexable = match (indexable, evaluate(ctx.clone(), &part.value)?) {568 (Val::Obj(v), Val::Str(key)) => match v569 .get(key.clone().into_flat())570 .with_description_src(&part.span, || format!("field <{key}> access"))?571 {572 Some(v) => v,573 #[cfg(feature = "exp-null-coaelse")]574 None if part.null_coaelse => return Ok(Val::Null),575 None => {576 let suggestions = suggest_object_fields(&v, key.into_flat());577578 return Err(Error::from(NoSuchField(579 key.clone().into_flat(),580 suggestions,581 )))582 .with_description_src(&part.span, || format!("field <{key}> access"));583 }584 },585 (Val::Obj(_), n) => bail!(ValueIndexMustBeTypeGot(586 ValType::Obj,587 ValType::Str,588 n.value_type(),589 )),590 (Val::Arr(v), Val::Num(n)) => {591 let n = n.get();592 if n.fract() > f64::EPSILON {593 bail!(FractionalIndex)594 }595 if n < 0.0 {596 #[expect(597 clippy::cast_possible_truncation,598 reason = "it would be truncated anyway"599 )]600 let n = n as isize;601 bail!(ArrayBoundsError(n, v.len()));602 }603 #[expect(604 clippy::cast_possible_truncation,605 clippy::cast_sign_loss,606 reason = "n is checked postive"607 )]608 v.get(n as usize)?609 .ok_or_else(|| ArrayBoundsError(n as isize, v.len()))?610 }611 (Val::Arr(_), Val::Str(n)) => {612 bail!(AttemptedIndexAnArrayWithString(n.into_flat()))613 }614 (Val::Arr(_), n) => bail!(ValueIndexMustBeTypeGot(615 ValType::Arr,616 ValType::Num,617 n.value_type(),618 )),619620 (Val::Str(s), Val::Num(n)) => Val::Str({621 let n = n.get();622 if n.fract() > f64::EPSILON {623 bail!(FractionalIndex)624 }625 if n < 0.0 {626 #[expect(627 clippy::cast_possible_truncation,628 reason = "it would be truncated anyway"629 )]630 let n = n as isize;631 bail!(ArrayBoundsError(n, s.into_flat().chars().count()));632 }633 #[expect(634 clippy::cast_sign_loss,635 clippy::cast_possible_truncation,636 reason = "n is positive, overflow will truncate as expected"637 )]638 let n = n as usize;639 let v: IStr = s640 .clone()641 .into_flat()642 .chars()643 .skip(n)644 .take(1)645 .collect::<String>()646 .into();647 if v.is_empty() {648 bail!(StringBoundsError(n, s.into_flat().chars().count()))649 }650 StrValue::Flat(v)651 }),652 (Val::Str(_), n) => bail!(ValueIndexMustBeTypeGot(653 ValType::Str,654 ValType::Num,655 n.value_type(),656 )),657 #[cfg(feature = "exp-null-coaelse")]658 (Val::Null, _) if part.null_coaelse => return Ok(Val::Null),659 (v, _) => bail!(CantIndexInto(v.value_type())),660 };661 }662 Ok(indexable)663 })?,664 LocalExpr(bindings, returned) => {665 let mut new_bindings: FxHashMap<IStr, Thunk<Val>> =666 FxHashMap::with_capacity(bindings.iter().map(BindSpec::binds_len).sum());667 let fctx = Context::new_future();668 for b in bindings {669 evaluate_dest(b, fctx.clone(), &mut new_bindings)?;670 }671 let ctx = ctx.extend_bindings(new_bindings).into_future(fctx);672 evaluate(ctx, returned)?673 }674 Arr(items) => {675 if items.is_empty() {676 Val::arr(())677 } else {678 Val::Arr(ArrValue::expr(ctx, items.clone()))679 }680 }681 ArrComp(expr, comp_specs) => Val::Arr(evaluate_arr_comp(ctx, expr, comp_specs)?),682 Obj(body) => Val::Obj(evaluate_object(None, ctx, body)?),683 ObjExtend(a, b) => {684 let base = evaluate(ctx.clone(), a)?;685 match base {686 Val::Obj(base_obj) => Val::Obj(evaluate_object(Some(base_obj), ctx, b)?),687 _ => bail!("ObjExtend lhs should be an object value"),688 }689 }690 Apply(value, args, tailstrict) => ensure_sufficient_stack(|| {691 evaluate_apply(ctx, value, args, CallLocation::new(&args.span), *tailstrict)692 })?,693 Function(params, body) => {694 evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())695 }696 AssertExpr(assert) => {697 evaluate_assert(ctx.clone(), &assert.assert)?;698 evaluate(ctx, &assert.rest)?699 }700 ErrorStmt(s, e) => in_frame(701 CallLocation::new(s),702 || "error statement".to_owned(),703 || bail!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),704 )?,705 IfElse(if_else) => {706 if in_frame(707 CallLocation::new(&if_else.cond.span),708 || "if condition".to_owned(),709 || bool::from_untyped(evaluate(ctx.clone(), &if_else.cond.cond)?),710 )? {711 evaluate(ctx, &if_else.cond_then)?712 } else {713 match &if_else.cond_else {714 Some(v) => evaluate(ctx, v)?,715 None => Val::Null,716 }717 }718 }719 Slice(slice) => {720 fn parse_idx<T: Typed + FromUntyped>(721 ctx: Context,722 expr: Option<&Spanned<Expr>>,723 desc: &'static str,724 ) -> Result<Option<T>> {725 if let Some(value) = expr {726 Ok(in_frame(727 CallLocation::new(&value.span),728 || format!("slice {desc}"),729 || <Option<T>>::from_untyped(evaluate(ctx, value)?),730 )?)731 } else {732 Ok(None)733 }734 }735736 let indexable = evaluate(ctx.clone(), &slice.value)?;737738 let start = parse_idx(ctx.clone(), slice.slice.start.as_ref(), "start")?;739 let end = parse_idx(ctx.clone(), slice.slice.end.as_ref(), "end")?;740 let step = parse_idx(ctx, slice.slice.step.as_ref(), "step")?;741742 IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?743 }744 Import(kind, path) => {745 let Expr::Str(path) = &**path else {746 bail!("computed imports are not supported")747 };748 with_state(|s| {749 let span = &kind.span;750 let resolved_path = s.resolve_from(span.0.source_path(), path)?;751 Ok(match &**kind {752 ImportKind::Normal => in_frame(753 CallLocation::new(span),754 || format!("import {:?}", path.clone()),755 || s.import_resolved(resolved_path),756 )?,757 ImportKind::Str => Val::string(s.import_resolved_str(resolved_path)?),758 ImportKind::Bin => Val::arr(s.import_resolved_bin(resolved_path)?),759 }) as Result<Val>760 })?761 }762 })763}