difftreelog
refactor split evaluation code
in: master
5 files changed
crates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate.rs
+++ /dev/null
@@ -1,830 +0,0 @@
-use crate::{
- equals, error::Error::*, push, throw, with_state, ArrValue, Bindable, Context, ContextCreator,
- FuncDesc, FuncVal, FutureWrapper, LazyBinding, LazyVal, LazyValValue, ObjMember, ObjValue,
- ObjectAssertion, Result, Val,
-};
-use jrsonnet_gc::{Gc, Trace};
-use jrsonnet_interner::IStr;
-use jrsonnet_parser::{
- ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, ExprLocation, FieldMember,
- ForSpecData, IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc, UnaryOpType,
- Visibility,
-};
-use jrsonnet_types::ValType;
-use rustc_hash::{FxHashMap, FxHasher};
-use std::{collections::HashMap, hash::BuildHasherDefault};
-
-pub fn evaluate_binding_in_future(
- b: &BindSpec,
- context_creator: FutureWrapper<Context>,
-) -> LazyVal {
- let b = b.clone();
- if let Some(params) = &b.params {
- let params = params.clone();
-
- #[derive(Trace)]
- #[trivially_drop]
- struct LazyMethodBinding {
- context_creator: FutureWrapper<Context>,
- name: IStr,
- params: ParamsDesc,
- value: LocExpr,
- }
- impl LazyValValue for LazyMethodBinding {
- fn get(self: Box<Self>) -> Result<Val> {
- Ok(evaluate_method(
- self.context_creator.unwrap(),
- self.name,
- self.params,
- self.value,
- ))
- }
- }
-
- LazyVal::new(Box::new(LazyMethodBinding {
- context_creator,
- name: b.name.clone(),
- params,
- value: b.value.clone(),
- }))
- } else {
- #[derive(Trace)]
- #[trivially_drop]
- struct LazyNamedBinding {
- context_creator: FutureWrapper<Context>,
- name: IStr,
- value: LocExpr,
- }
- impl LazyValValue for LazyNamedBinding {
- fn get(self: Box<Self>) -> Result<Val> {
- evaluate_named(self.context_creator.unwrap(), &self.value, self.name)
- }
- }
- LazyVal::new(Box::new(LazyNamedBinding {
- context_creator,
- name: b.name.clone(),
- value: b.value,
- }))
- }
-}
-
-pub fn evaluate_binding(b: &BindSpec, context_creator: ContextCreator) -> (IStr, LazyBinding) {
- let b = b.clone();
- if let Some(params) = &b.params {
- let params = params.clone();
-
- #[derive(Trace)]
- #[trivially_drop]
- struct BindableMethodLazyVal {
- this: Option<ObjValue>,
- super_obj: Option<ObjValue>,
-
- context_creator: ContextCreator,
- name: IStr,
- params: ParamsDesc,
- value: LocExpr,
- }
- impl LazyValValue for BindableMethodLazyVal {
- fn get(self: Box<Self>) -> Result<Val> {
- Ok(evaluate_method(
- self.context_creator.create(self.this, self.super_obj)?,
- self.name,
- self.params,
- self.value,
- ))
- }
- }
-
- #[derive(Trace)]
- #[trivially_drop]
- struct BindableMethod {
- context_creator: ContextCreator,
- name: IStr,
- params: ParamsDesc,
- value: LocExpr,
- }
- impl Bindable for BindableMethod {
- fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {
- Ok(LazyVal::new(Box::new(BindableMethodLazyVal {
- this,
- super_obj,
-
- context_creator: self.context_creator.clone(),
- name: self.name.clone(),
- params: self.params.clone(),
- value: self.value.clone(),
- })))
- }
- }
-
- (
- b.name.clone(),
- LazyBinding::Bindable(Gc::new(Box::new(BindableMethod {
- context_creator,
- name: b.name.clone(),
- params,
- value: b.value.clone(),
- }))),
- )
- } else {
- #[derive(Trace)]
- #[trivially_drop]
- struct BindableNamedLazyVal {
- this: Option<ObjValue>,
- super_obj: Option<ObjValue>,
-
- context_creator: ContextCreator,
- name: IStr,
- value: LocExpr,
- }
- impl LazyValValue for BindableNamedLazyVal {
- fn get(self: Box<Self>) -> Result<Val> {
- evaluate_named(
- self.context_creator.create(self.this, self.super_obj)?,
- &self.value,
- self.name,
- )
- }
- }
-
- #[derive(Trace)]
- #[trivially_drop]
- struct BindableNamed {
- context_creator: ContextCreator,
- name: IStr,
- value: LocExpr,
- }
- impl Bindable for BindableNamed {
- fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {
- Ok(LazyVal::new(Box::new(BindableNamedLazyVal {
- this,
- super_obj,
-
- context_creator: self.context_creator.clone(),
- name: self.name.clone(),
- value: self.value.clone(),
- })))
- }
- }
-
- (
- b.name.clone(),
- LazyBinding::Bindable(Gc::new(Box::new(BindableNamed {
- context_creator,
- name: b.name.clone(),
- value: b.value.clone(),
- }))),
- )
- }
-}
-
-pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {
- Val::Func(Gc::new(FuncVal::Normal(FuncDesc {
- name,
- ctx,
- params,
- body,
- })))
-}
-
-pub fn evaluate_field_name(
- context: Context,
- field_name: &jrsonnet_parser::FieldName,
-) -> Result<Option<IStr>> {
- Ok(match field_name {
- jrsonnet_parser::FieldName::Fixed(n) => Some(n.clone()),
- jrsonnet_parser::FieldName::Dyn(expr) => {
- let value = evaluate(context, expr)?;
- if matches!(value, Val::Null) {
- None
- } else {
- Some(value.try_cast_str("dynamic field name")?)
- }
- }
- })
-}
-
-pub fn evaluate_unary_op(op: UnaryOpType, b: &Val) -> Result<Val> {
- Ok(match (op, b) {
- (UnaryOpType::Not, Val::Bool(v)) => Val::Bool(!v),
- (UnaryOpType::Minus, Val::Num(n)) => Val::Num(-*n),
- (UnaryOpType::BitNot, Val::Num(n)) => Val::Num(!(*n as i32) as f64),
- (op, o) => throw!(UnaryOperatorDoesNotOperateOnType(op, o.value_type())),
- })
-}
-
-pub fn evaluate_add_op(a: &Val, b: &Val) -> Result<Val> {
- Ok(match (a, b) {
- (Val::Str(v1), Val::Str(v2)) => Val::Str(((**v1).to_owned() + v2).into()),
-
- // Can't use generic json serialization way, because it depends on number to string concatenation (std.jsonnet:890)
- (Val::Num(n), Val::Str(o)) => Val::Str(format!("{}{}", n, o).into()),
- (Val::Str(o), Val::Num(n)) => Val::Str(format!("{}{}", o, n).into()),
-
- (Val::Str(s), o) => Val::Str(format!("{}{}", s, o.clone().to_string()?).into()),
- (o, Val::Str(s)) => Val::Str(format!("{}{}", o.clone().to_string()?, s).into()),
-
- (Val::Obj(v1), Val::Obj(v2)) => Val::Obj(v2.extend_from(v1.clone())),
- (Val::Arr(a), Val::Arr(b)) => {
- let mut out = Vec::with_capacity(a.len() + b.len());
- out.extend(a.iter_lazy());
- out.extend(b.iter_lazy());
- Val::Arr(out.into())
- }
- (Val::Num(v1), Val::Num(v2)) => Val::new_checked_num(v1 + v2)?,
- _ => throw!(BinaryOperatorDoesNotOperateOnValues(
- BinaryOpType::Add,
- a.value_type(),
- b.value_type(),
- )),
- })
-}
-
-pub fn evaluate_binary_op_special(
- context: Context,
- a: &LocExpr,
- op: BinaryOpType,
- b: &LocExpr,
-) -> Result<Val> {
- Ok(match (evaluate(context.clone(), a)?, op, b) {
- (Val::Bool(true), BinaryOpType::Or, _o) => Val::Bool(true),
- (Val::Bool(false), BinaryOpType::And, _o) => Val::Bool(false),
- (a, op, eb) => evaluate_binary_op_normal(&a, op, &evaluate(context, eb)?)?,
- })
-}
-
-pub fn evaluate_binary_op_normal(a: &Val, op: BinaryOpType, b: &Val) -> Result<Val> {
- Ok(match (a, op, b) {
- (a, BinaryOpType::Add, b) => evaluate_add_op(a, b)?,
-
- (a, BinaryOpType::Eq, b) => Val::Bool(equals(a, b)?),
- (a, BinaryOpType::Neq, b) => Val::Bool(!equals(a, b)?),
-
- (Val::Str(v1), BinaryOpType::Mul, Val::Num(v2)) => Val::Str(v1.repeat(*v2 as usize).into()),
-
- // Bool X Bool
- (Val::Bool(a), BinaryOpType::And, Val::Bool(b)) => Val::Bool(*a && *b),
- (Val::Bool(a), BinaryOpType::Or, Val::Bool(b)) => Val::Bool(*a || *b),
-
- // Str X Str
- (Val::Str(v1), BinaryOpType::Lt, Val::Str(v2)) => Val::Bool(v1 < v2),
- (Val::Str(v1), BinaryOpType::Gt, Val::Str(v2)) => Val::Bool(v1 > v2),
- (Val::Str(v1), BinaryOpType::Lte, Val::Str(v2)) => Val::Bool(v1 <= v2),
- (Val::Str(v1), BinaryOpType::Gte, Val::Str(v2)) => Val::Bool(v1 >= v2),
-
- // Num X Num
- (Val::Num(v1), BinaryOpType::Mul, Val::Num(v2)) => Val::new_checked_num(v1 * v2)?,
- (Val::Num(v1), BinaryOpType::Div, Val::Num(v2)) => {
- if *v2 <= f64::EPSILON {
- throw!(DivisionByZero)
- }
- Val::new_checked_num(v1 / v2)?
- }
-
- (Val::Num(v1), BinaryOpType::Sub, Val::Num(v2)) => Val::new_checked_num(v1 - v2)?,
-
- (Val::Num(v1), BinaryOpType::Lt, Val::Num(v2)) => Val::Bool(v1 < v2),
- (Val::Num(v1), BinaryOpType::Gt, Val::Num(v2)) => Val::Bool(v1 > v2),
- (Val::Num(v1), BinaryOpType::Lte, Val::Num(v2)) => Val::Bool(v1 <= v2),
- (Val::Num(v1), BinaryOpType::Gte, Val::Num(v2)) => Val::Bool(v1 >= v2),
-
- (Val::Num(v1), BinaryOpType::BitAnd, Val::Num(v2)) => {
- Val::Num(((*v1 as i32) & (*v2 as i32)) as f64)
- }
- (Val::Num(v1), BinaryOpType::BitOr, Val::Num(v2)) => {
- Val::Num(((*v1 as i32) | (*v2 as i32)) as f64)
- }
- (Val::Num(v1), BinaryOpType::BitXor, Val::Num(v2)) => {
- Val::Num(((*v1 as i32) ^ (*v2 as i32)) as f64)
- }
- (Val::Num(v1), BinaryOpType::Lhs, Val::Num(v2)) => {
- if *v2 < 0.0 {
- throw!(RuntimeError("shift by negative exponent".into()))
- }
- Val::Num(((*v1 as i32) << (*v2 as i32)) as f64)
- }
- (Val::Num(v1), BinaryOpType::Rhs, Val::Num(v2)) => {
- if *v2 < 0.0 {
- throw!(RuntimeError("shift by negative exponent".into()))
- }
- Val::Num(((*v1 as i32) >> (*v2 as i32)) as f64)
- }
-
- _ => throw!(BinaryOperatorDoesNotOperateOnValues(
- op,
- a.value_type(),
- b.value_type(),
- )),
- })
-}
-
-pub fn evaluate_comp(
- context: Context,
- specs: &[CompSpec],
- callback: &mut impl FnMut(Context) -> Result<()>,
-) -> Result<()> {
- match specs.get(0) {
- None => callback(context)?,
- Some(CompSpec::IfSpec(IfSpecData(cond))) => {
- if evaluate(context.clone(), cond)?.try_cast_bool("if spec")? {
- evaluate_comp(context, &specs[1..], callback)?
- }
- }
- Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(context.clone(), expr)? {
- Val::Arr(list) => {
- for item in list.iter() {
- evaluate_comp(
- context.clone().with_var(var.clone(), item?.clone()),
- &specs[1..],
- callback,
- )?
- }
- }
- _ => throw!(InComprehensionCanOnlyIterateOverArray),
- },
- }
- Ok(())
-}
-
-pub fn evaluate_member_list_object(context: Context, members: &[Member]) -> Result<ObjValue> {
- let new_bindings = FutureWrapper::new();
- let future_this = FutureWrapper::new();
- let context_creator = ContextCreator(context.clone(), new_bindings.clone());
- {
- let mut bindings: FxHashMap<IStr, LazyBinding> =
- FxHashMap::with_capacity_and_hasher(members.len(), BuildHasherDefault::default());
- for (n, b) in members
- .iter()
- .filter_map(|m| match m {
- Member::BindStmt(b) => Some(b.clone()),
- _ => None,
- })
- .map(|b| evaluate_binding(&b, context_creator.clone()))
- {
- bindings.insert(n, b);
- }
- new_bindings.fill(bindings);
- }
-
- let mut new_members = FxHashMap::default();
- let mut assertions: Vec<Box<dyn ObjectAssertion>> = Vec::new();
- for member in members.iter() {
- match member {
- Member::Field(FieldMember {
- name,
- plus,
- params: None,
- visibility,
- value,
- }) => {
- let name = evaluate_field_name(context.clone(), name)?;
- if name.is_none() {
- continue;
- }
- let name = name.unwrap();
-
- #[derive(Trace)]
- #[trivially_drop]
- struct ObjMemberBinding {
- context_creator: ContextCreator,
- value: LocExpr,
- name: IStr,
- }
- impl Bindable for ObjMemberBinding {
- fn bind(
- &self,
- this: Option<ObjValue>,
- super_obj: Option<ObjValue>,
- ) -> Result<LazyVal> {
- Ok(LazyVal::new_resolved(evaluate_named(
- self.context_creator.create(this, super_obj)?,
- &self.value,
- self.name.clone(),
- )?))
- }
- }
- new_members.insert(
- name.clone(),
- ObjMember {
- add: *plus,
- visibility: *visibility,
- invoke: LazyBinding::Bindable(Gc::new(Box::new(ObjMemberBinding {
- context_creator: context_creator.clone(),
- value: value.clone(),
- name,
- }))),
- location: value.1.clone(),
- },
- );
- }
- Member::Field(FieldMember {
- name,
- params: Some(params),
- value,
- ..
- }) => {
- let name = evaluate_field_name(context.clone(), name)?;
- if name.is_none() {
- continue;
- }
- let name = name.unwrap();
- #[derive(Trace)]
- #[trivially_drop]
- struct ObjMemberBinding {
- context_creator: ContextCreator,
- value: LocExpr,
- params: ParamsDesc,
- name: IStr,
- }
- impl Bindable for ObjMemberBinding {
- fn bind(
- &self,
- this: Option<ObjValue>,
- super_obj: Option<ObjValue>,
- ) -> Result<LazyVal> {
- Ok(LazyVal::new_resolved(evaluate_method(
- self.context_creator.create(this, super_obj)?,
- self.name.clone(),
- self.params.clone(),
- self.value.clone(),
- )))
- }
- }
- new_members.insert(
- name.clone(),
- ObjMember {
- add: false,
- visibility: Visibility::Hidden,
- invoke: LazyBinding::Bindable(Gc::new(Box::new(ObjMemberBinding {
- context_creator: context_creator.clone(),
- value: value.clone(),
- params: params.clone(),
- name,
- }))),
- location: value.1.clone(),
- },
- );
- }
- Member::BindStmt(_) => {}
- Member::AssertStmt(stmt) => {
- #[derive(Trace)]
- #[trivially_drop]
- struct ObjectAssert {
- context_creator: ContextCreator,
- assert: AssertStmt,
- }
- impl ObjectAssertion for ObjectAssert {
- fn run(
- &self,
- this: Option<ObjValue>,
- super_obj: Option<ObjValue>,
- ) -> Result<()> {
- let ctx = self.context_creator.create(this, super_obj)?;
- evaluate_assert(ctx, &self.assert)
- }
- }
- assertions.push(Box::new(ObjectAssert {
- context_creator: context_creator.clone(),
- assert: stmt.clone(),
- }));
- }
- }
- }
- let this = ObjValue::new(None, Gc::new(new_members), Gc::new(assertions));
- future_this.fill(this.clone());
- Ok(this)
-}
-
-pub fn evaluate_object(context: Context, object: &ObjBody) -> Result<ObjValue> {
- Ok(match object {
- ObjBody::MemberList(members) => evaluate_member_list_object(context, members)?,
- ObjBody::ObjComp(obj) => {
- let future_this = FutureWrapper::new();
- let mut new_members = FxHashMap::default();
- evaluate_comp(context.clone(), &obj.compspecs, &mut |ctx| {
- let new_bindings = FutureWrapper::new();
- let context_creator = ContextCreator(context.clone(), new_bindings.clone());
- let mut bindings: FxHashMap<IStr, LazyBinding> =
- FxHashMap::with_capacity_and_hasher(
- obj.pre_locals.len() + obj.post_locals.len(),
- BuildHasherDefault::default(),
- );
- for (n, b) in obj
- .pre_locals
- .iter()
- .chain(obj.post_locals.iter())
- .map(|b| evaluate_binding(b, context_creator.clone()))
- {
- bindings.insert(n, b);
- }
- new_bindings.fill(bindings.clone());
- let ctx = ctx.extend_unbound(bindings, None, None, None)?;
- let key = evaluate(ctx.clone(), &obj.key)?;
-
- match key {
- Val::Null => {}
- Val::Str(n) => {
- #[derive(Trace)]
- #[trivially_drop]
- struct ObjCompBinding {
- context: Context,
- value: LocExpr,
- }
- impl Bindable for ObjCompBinding {
- fn bind(
- &self,
- this: Option<ObjValue>,
- _super_obj: Option<ObjValue>,
- ) -> Result<LazyVal> {
- Ok(LazyVal::new_resolved(evaluate(
- self.context.clone().extend(
- FxHashMap::default(),
- None,
- this,
- None,
- ),
- &self.value,
- )?))
- }
- }
- new_members.insert(
- n,
- ObjMember {
- add: false,
- visibility: Visibility::Normal,
- invoke: LazyBinding::Bindable(Gc::new(Box::new(ObjCompBinding {
- context: ctx,
- value: obj.value.clone(),
- }))),
- location: obj.value.1.clone(),
- },
- );
- }
- v => throw!(FieldMustBeStringGot(v.value_type())),
- }
-
- Ok(())
- })?;
-
- let this = ObjValue::new(None, Gc::new(new_members), Gc::new(Vec::new()));
- future_this.fill(this.clone());
- this
- }
- })
-}
-
-pub fn evaluate_apply(
- context: Context,
- value: &LocExpr,
- args: &ArgsDesc,
- loc: Option<&ExprLocation>,
- tailstrict: bool,
-) -> Result<Val> {
- let value = evaluate(context.clone(), value)?;
- Ok(match value {
- Val::Func(f) => {
- let body = || f.evaluate(context, loc, args, tailstrict);
- if tailstrict {
- body()?
- } else {
- push(loc, || format!("function <{}> call", f.name()), body)?
- }
- }
- v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),
- })
-}
-
-pub fn evaluate_assert(context: Context, assertion: &AssertStmt) -> Result<()> {
- let value = &assertion.0;
- let msg = &assertion.1;
- let assertion_result = push(
- value.1.as_ref(),
- || "assertion condition".to_owned(),
- || {
- evaluate(context.clone(), value)?
- .try_cast_bool("assertion condition should be of type `boolean`")
- },
- )?;
- if !assertion_result {
- push(
- value.1.as_ref(),
- || "assertion failure".to_owned(),
- || {
- if let Some(msg) = msg {
- throw!(AssertionFailed(evaluate(context, msg)?.to_string()?));
- } else {
- throw!(AssertionFailed(Val::Null.to_string()?));
- }
- },
- )?
- }
- Ok(())
-}
-
-pub fn evaluate_named(context: Context, lexpr: &LocExpr, name: IStr) -> Result<Val> {
- use Expr::*;
- let LocExpr(expr, _loc) = lexpr;
- Ok(match &**expr {
- Function(params, body) => evaluate_method(context, name, params.clone(), body.clone()),
- _ => evaluate(context, lexpr)?,
- })
-}
-
-pub fn evaluate(context: Context, expr: &LocExpr) -> Result<Val> {
- use Expr::*;
- let LocExpr(expr, loc) = expr;
- Ok(match &**expr {
- Literal(LiteralType::This) => {
- Val::Obj(context.this().clone().ok_or(CantUseSelfOutsideOfObject)?)
- }
- Literal(LiteralType::Super) => Val::Obj(
- context
- .super_obj()
- .clone()
- .ok_or(NoSuperFound)?
- .with_this(context.this().clone().unwrap()),
- ),
- Literal(LiteralType::Dollar) => {
- Val::Obj(context.dollar().clone().ok_or(NoTopLevelObjectFound)?)
- }
- Literal(LiteralType::True) => Val::Bool(true),
- Literal(LiteralType::False) => Val::Bool(false),
- Literal(LiteralType::Null) => Val::Null,
- Parened(e) => evaluate(context, e)?,
- Str(v) => Val::Str(v.clone()),
- Num(v) => Val::new_checked_num(*v)?,
- BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, v1, *o, v2)?,
- UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,
- Var(name) => push(
- loc.as_ref(),
- || format!("variable <{}>", name),
- || context.binding(name.clone())?.evaluate(),
- )?,
- Index(value, index) => {
- match (evaluate(context.clone(), value)?, evaluate(context, index)?) {
- (Val::Obj(v), Val::Str(s)) => {
- let sn = s.clone();
- push(
- loc.as_ref(),
- || format!("field <{}> access", sn),
- || {
- if let Some(v) = v.get(s.clone())? {
- Ok(v)
- } else if v.get("__intrinsic_namespace__".into())?.is_some() {
- Ok(Val::Func(Gc::new(FuncVal::Intrinsic(s))))
- } else {
- throw!(NoSuchField(s))
- }
- },
- )?
- }
- (Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(
- ValType::Obj,
- ValType::Str,
- n.value_type(),
- )),
-
- (Val::Arr(v), Val::Num(n)) => {
- if n.fract() > f64::EPSILON {
- throw!(FractionalIndex)
- }
- v.get(n as usize)?
- .ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?
- }
- (Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),
- (Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(
- ValType::Arr,
- ValType::Num,
- n.value_type(),
- )),
-
- (Val::Str(s), Val::Num(n)) => Val::Str(
- s.chars()
- .skip(n as usize)
- .take(1)
- .collect::<String>()
- .into(),
- ),
- (Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(
- ValType::Str,
- ValType::Num,
- n.value_type(),
- )),
-
- (v, _) => throw!(CantIndexInto(v.value_type())),
- }
- }
- LocalExpr(bindings, returned) => {
- let mut new_bindings: FxHashMap<IStr, LazyVal> = HashMap::with_capacity_and_hasher(
- bindings.len(),
- BuildHasherDefault::<FxHasher>::default(),
- );
- let future_context = Context::new_future();
- for b in bindings {
- new_bindings.insert(
- b.name.clone(),
- evaluate_binding_in_future(b, future_context.clone()),
- );
- }
- let context = context
- .extend_bound(new_bindings)
- .into_future(future_context);
- evaluate(context, &returned.clone())?
- }
- Arr(items) => {
- let mut out = Vec::with_capacity(items.len());
- for item in items {
- // TODO: Implement ArrValue::Lazy with same context for every element?
- #[derive(Trace)]
- #[trivially_drop]
- struct ArrayElement {
- context: Context,
- item: LocExpr,
- }
- impl LazyValValue for ArrayElement {
- fn get(self: Box<Self>) -> Result<Val> {
- evaluate(self.context, &self.item)
- }
- }
- out.push(LazyVal::new(Box::new(ArrayElement {
- context: context.clone(),
- item: item.clone(),
- })));
- }
- Val::Arr(out.into())
- }
- ArrComp(expr, comp_specs) => {
- let mut out = Vec::new();
- evaluate_comp(context, comp_specs, &mut |ctx| {
- out.push(evaluate(ctx, expr)?);
- Ok(())
- })?;
- Val::Arr(ArrValue::Eager(Gc::new(out)))
- }
- Obj(body) => Val::Obj(evaluate_object(context, body)?),
- ObjExtend(s, t) => evaluate_add_op(
- &evaluate(context.clone(), s)?,
- &Val::Obj(evaluate_object(context, t)?),
- )?,
- Apply(value, args, tailstrict) => {
- evaluate_apply(context, value, args, loc.as_ref(), *tailstrict)?
- }
- Function(params, body) => {
- evaluate_method(context, "anonymous".into(), params.clone(), body.clone())
- }
- Intrinsic(name) => Val::Func(Gc::new(FuncVal::Intrinsic(name.clone()))),
- AssertExpr(assert, returned) => {
- evaluate_assert(context.clone(), assert)?;
- evaluate(context, returned)?
- }
- ErrorStmt(e) => push(
- loc.as_ref(),
- || "error statement".to_owned(),
- || {
- throw!(RuntimeError(
- evaluate(context, e)?.try_cast_str("error text should be of type `string`")?,
- ))
- },
- )?,
- IfElse {
- cond,
- cond_then,
- cond_else,
- } => {
- if push(
- loc.as_ref(),
- || "if condition".to_owned(),
- || evaluate(context.clone(), &cond.0)?.try_cast_bool("in if condition"),
- )? {
- evaluate(context, cond_then)?
- } else {
- match cond_else {
- Some(v) => evaluate(context, v)?,
- None => Val::Null,
- }
- }
- }
- Import(path) => {
- let tmp = loc
- .clone()
- .expect("imports cannot be used without loc_data")
- .0;
- let mut import_location = tmp.to_path_buf();
- import_location.pop();
- push(
- loc.as_ref(),
- || format!("import {:?}", path),
- || with_state(|s| s.import_file(&import_location, path)),
- )?
- }
- ImportStr(path) => {
- let tmp = loc
- .clone()
- .expect("imports cannot be used without loc_data")
- .0;
- let mut import_location = tmp.to_path_buf();
- import_location.pop();
- Val::Str(with_state(|s| s.import_file_str(&import_location, path))?)
- }
- })
-}
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -0,0 +1,718 @@
+use crate::{
+ error::Error::*,
+ evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},
+ push, throw, with_state, ArrValue, Bindable, Context, ContextCreator, FuncDesc, FuncVal,
+ FutureWrapper, LazyBinding, LazyVal, LazyValValue, ObjMember, ObjValue, ObjectAssertion,
+ Result, Val,
+};
+use jrsonnet_gc::{Gc, Trace};
+use jrsonnet_interner::IStr;
+use jrsonnet_parser::{
+ ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, ExprLocation, FieldMember, ForSpecData,
+ IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc, Visibility,
+};
+use jrsonnet_types::ValType;
+use rustc_hash::{FxHashMap, FxHasher};
+use std::{collections::HashMap, hash::BuildHasherDefault};
+pub mod operator;
+
+pub fn evaluate_binding_in_future(
+ b: &BindSpec,
+ context_creator: FutureWrapper<Context>,
+) -> LazyVal {
+ let b = b.clone();
+ if let Some(params) = &b.params {
+ let params = params.clone();
+
+ #[derive(Trace)]
+ #[trivially_drop]
+ struct LazyMethodBinding {
+ context_creator: FutureWrapper<Context>,
+ name: IStr,
+ params: ParamsDesc,
+ value: LocExpr,
+ }
+ impl LazyValValue for LazyMethodBinding {
+ fn get(self: Box<Self>) -> Result<Val> {
+ Ok(evaluate_method(
+ self.context_creator.unwrap(),
+ self.name,
+ self.params,
+ self.value,
+ ))
+ }
+ }
+
+ LazyVal::new(Box::new(LazyMethodBinding {
+ context_creator,
+ name: b.name.clone(),
+ params,
+ value: b.value.clone(),
+ }))
+ } else {
+ #[derive(Trace)]
+ #[trivially_drop]
+ struct LazyNamedBinding {
+ context_creator: FutureWrapper<Context>,
+ name: IStr,
+ value: LocExpr,
+ }
+ impl LazyValValue for LazyNamedBinding {
+ fn get(self: Box<Self>) -> Result<Val> {
+ evaluate_named(self.context_creator.unwrap(), &self.value, self.name)
+ }
+ }
+ LazyVal::new(Box::new(LazyNamedBinding {
+ context_creator,
+ name: b.name.clone(),
+ value: b.value,
+ }))
+ }
+}
+
+pub fn evaluate_binding(b: &BindSpec, context_creator: ContextCreator) -> (IStr, LazyBinding) {
+ let b = b.clone();
+ if let Some(params) = &b.params {
+ let params = params.clone();
+
+ #[derive(Trace)]
+ #[trivially_drop]
+ struct BindableMethodLazyVal {
+ this: Option<ObjValue>,
+ super_obj: Option<ObjValue>,
+
+ context_creator: ContextCreator,
+ name: IStr,
+ params: ParamsDesc,
+ value: LocExpr,
+ }
+ impl LazyValValue for BindableMethodLazyVal {
+ fn get(self: Box<Self>) -> Result<Val> {
+ Ok(evaluate_method(
+ self.context_creator.create(self.this, self.super_obj)?,
+ self.name,
+ self.params,
+ self.value,
+ ))
+ }
+ }
+
+ #[derive(Trace)]
+ #[trivially_drop]
+ struct BindableMethod {
+ context_creator: ContextCreator,
+ name: IStr,
+ params: ParamsDesc,
+ value: LocExpr,
+ }
+ impl Bindable for BindableMethod {
+ fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {
+ Ok(LazyVal::new(Box::new(BindableMethodLazyVal {
+ this,
+ super_obj,
+
+ context_creator: self.context_creator.clone(),
+ name: self.name.clone(),
+ params: self.params.clone(),
+ value: self.value.clone(),
+ })))
+ }
+ }
+
+ (
+ b.name.clone(),
+ LazyBinding::Bindable(Gc::new(Box::new(BindableMethod {
+ context_creator,
+ name: b.name.clone(),
+ params,
+ value: b.value.clone(),
+ }))),
+ )
+ } else {
+ #[derive(Trace)]
+ #[trivially_drop]
+ struct BindableNamedLazyVal {
+ this: Option<ObjValue>,
+ super_obj: Option<ObjValue>,
+
+ context_creator: ContextCreator,
+ name: IStr,
+ value: LocExpr,
+ }
+ impl LazyValValue for BindableNamedLazyVal {
+ fn get(self: Box<Self>) -> Result<Val> {
+ evaluate_named(
+ self.context_creator.create(self.this, self.super_obj)?,
+ &self.value,
+ self.name,
+ )
+ }
+ }
+
+ #[derive(Trace)]
+ #[trivially_drop]
+ struct BindableNamed {
+ context_creator: ContextCreator,
+ name: IStr,
+ value: LocExpr,
+ }
+ impl Bindable for BindableNamed {
+ fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {
+ Ok(LazyVal::new(Box::new(BindableNamedLazyVal {
+ this,
+ super_obj,
+
+ context_creator: self.context_creator.clone(),
+ name: self.name.clone(),
+ value: self.value.clone(),
+ })))
+ }
+ }
+
+ (
+ b.name.clone(),
+ LazyBinding::Bindable(Gc::new(Box::new(BindableNamed {
+ context_creator,
+ name: b.name.clone(),
+ value: b.value.clone(),
+ }))),
+ )
+ }
+}
+
+pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {
+ Val::Func(Gc::new(FuncVal::Normal(FuncDesc {
+ name,
+ ctx,
+ params,
+ body,
+ })))
+}
+
+pub fn evaluate_field_name(
+ context: Context,
+ field_name: &jrsonnet_parser::FieldName,
+) -> Result<Option<IStr>> {
+ Ok(match field_name {
+ jrsonnet_parser::FieldName::Fixed(n) => Some(n.clone()),
+ jrsonnet_parser::FieldName::Dyn(expr) => {
+ let value = evaluate(context, expr)?;
+ if matches!(value, Val::Null) {
+ None
+ } else {
+ Some(value.try_cast_str("dynamic field name")?)
+ }
+ }
+ })
+}
+
+pub fn evaluate_comp(
+ context: Context,
+ specs: &[CompSpec],
+ callback: &mut impl FnMut(Context) -> Result<()>,
+) -> Result<()> {
+ match specs.get(0) {
+ None => callback(context)?,
+ Some(CompSpec::IfSpec(IfSpecData(cond))) => {
+ if evaluate(context.clone(), cond)?.try_cast_bool("if spec")? {
+ evaluate_comp(context, &specs[1..], callback)?
+ }
+ }
+ Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(context.clone(), expr)? {
+ Val::Arr(list) => {
+ for item in list.iter() {
+ evaluate_comp(
+ context.clone().with_var(var.clone(), item?.clone()),
+ &specs[1..],
+ callback,
+ )?
+ }
+ }
+ _ => throw!(InComprehensionCanOnlyIterateOverArray),
+ },
+ }
+ Ok(())
+}
+
+pub fn evaluate_member_list_object(context: Context, members: &[Member]) -> Result<ObjValue> {
+ let new_bindings = FutureWrapper::new();
+ let future_this = FutureWrapper::new();
+ let context_creator = ContextCreator(context.clone(), new_bindings.clone());
+ {
+ let mut bindings: FxHashMap<IStr, LazyBinding> =
+ FxHashMap::with_capacity_and_hasher(members.len(), BuildHasherDefault::default());
+ for (n, b) in members
+ .iter()
+ .filter_map(|m| match m {
+ Member::BindStmt(b) => Some(b.clone()),
+ _ => None,
+ })
+ .map(|b| evaluate_binding(&b, context_creator.clone()))
+ {
+ bindings.insert(n, b);
+ }
+ new_bindings.fill(bindings);
+ }
+
+ let mut new_members = FxHashMap::default();
+ let mut assertions: Vec<Box<dyn ObjectAssertion>> = Vec::new();
+ for member in members.iter() {
+ match member {
+ Member::Field(FieldMember {
+ name,
+ plus,
+ params: None,
+ visibility,
+ value,
+ }) => {
+ let name = evaluate_field_name(context.clone(), name)?;
+ if name.is_none() {
+ continue;
+ }
+ let name = name.unwrap();
+
+ #[derive(Trace)]
+ #[trivially_drop]
+ struct ObjMemberBinding {
+ context_creator: ContextCreator,
+ value: LocExpr,
+ name: IStr,
+ }
+ impl Bindable for ObjMemberBinding {
+ fn bind(
+ &self,
+ this: Option<ObjValue>,
+ super_obj: Option<ObjValue>,
+ ) -> Result<LazyVal> {
+ Ok(LazyVal::new_resolved(evaluate_named(
+ self.context_creator.create(this, super_obj)?,
+ &self.value,
+ self.name.clone(),
+ )?))
+ }
+ }
+ new_members.insert(
+ name.clone(),
+ ObjMember {
+ add: *plus,
+ visibility: *visibility,
+ invoke: LazyBinding::Bindable(Gc::new(Box::new(ObjMemberBinding {
+ context_creator: context_creator.clone(),
+ value: value.clone(),
+ name,
+ }))),
+ location: value.1.clone(),
+ },
+ );
+ }
+ Member::Field(FieldMember {
+ name,
+ params: Some(params),
+ value,
+ ..
+ }) => {
+ let name = evaluate_field_name(context.clone(), name)?;
+ if name.is_none() {
+ continue;
+ }
+ let name = name.unwrap();
+ #[derive(Trace)]
+ #[trivially_drop]
+ struct ObjMemberBinding {
+ context_creator: ContextCreator,
+ value: LocExpr,
+ params: ParamsDesc,
+ name: IStr,
+ }
+ impl Bindable for ObjMemberBinding {
+ fn bind(
+ &self,
+ this: Option<ObjValue>,
+ super_obj: Option<ObjValue>,
+ ) -> Result<LazyVal> {
+ Ok(LazyVal::new_resolved(evaluate_method(
+ self.context_creator.create(this, super_obj)?,
+ self.name.clone(),
+ self.params.clone(),
+ self.value.clone(),
+ )))
+ }
+ }
+ new_members.insert(
+ name.clone(),
+ ObjMember {
+ add: false,
+ visibility: Visibility::Hidden,
+ invoke: LazyBinding::Bindable(Gc::new(Box::new(ObjMemberBinding {
+ context_creator: context_creator.clone(),
+ value: value.clone(),
+ params: params.clone(),
+ name,
+ }))),
+ location: value.1.clone(),
+ },
+ );
+ }
+ Member::BindStmt(_) => {}
+ Member::AssertStmt(stmt) => {
+ #[derive(Trace)]
+ #[trivially_drop]
+ struct ObjectAssert {
+ context_creator: ContextCreator,
+ assert: AssertStmt,
+ }
+ impl ObjectAssertion for ObjectAssert {
+ fn run(
+ &self,
+ this: Option<ObjValue>,
+ super_obj: Option<ObjValue>,
+ ) -> Result<()> {
+ let ctx = self.context_creator.create(this, super_obj)?;
+ evaluate_assert(ctx, &self.assert)
+ }
+ }
+ assertions.push(Box::new(ObjectAssert {
+ context_creator: context_creator.clone(),
+ assert: stmt.clone(),
+ }));
+ }
+ }
+ }
+ let this = ObjValue::new(None, Gc::new(new_members), Gc::new(assertions));
+ future_this.fill(this.clone());
+ Ok(this)
+}
+
+pub fn evaluate_object(context: Context, object: &ObjBody) -> Result<ObjValue> {
+ Ok(match object {
+ ObjBody::MemberList(members) => evaluate_member_list_object(context, members)?,
+ ObjBody::ObjComp(obj) => {
+ let future_this = FutureWrapper::new();
+ let mut new_members = FxHashMap::default();
+ evaluate_comp(context.clone(), &obj.compspecs, &mut |ctx| {
+ let new_bindings = FutureWrapper::new();
+ let context_creator = ContextCreator(context.clone(), new_bindings.clone());
+ let mut bindings: FxHashMap<IStr, LazyBinding> =
+ FxHashMap::with_capacity_and_hasher(
+ obj.pre_locals.len() + obj.post_locals.len(),
+ BuildHasherDefault::default(),
+ );
+ for (n, b) in obj
+ .pre_locals
+ .iter()
+ .chain(obj.post_locals.iter())
+ .map(|b| evaluate_binding(b, context_creator.clone()))
+ {
+ bindings.insert(n, b);
+ }
+ new_bindings.fill(bindings.clone());
+ let ctx = ctx.extend_unbound(bindings, None, None, None)?;
+ let key = evaluate(ctx.clone(), &obj.key)?;
+
+ match key {
+ Val::Null => {}
+ Val::Str(n) => {
+ #[derive(Trace)]
+ #[trivially_drop]
+ struct ObjCompBinding {
+ context: Context,
+ value: LocExpr,
+ }
+ impl Bindable for ObjCompBinding {
+ fn bind(
+ &self,
+ this: Option<ObjValue>,
+ _super_obj: Option<ObjValue>,
+ ) -> Result<LazyVal> {
+ Ok(LazyVal::new_resolved(evaluate(
+ self.context.clone().extend(
+ FxHashMap::default(),
+ None,
+ this,
+ None,
+ ),
+ &self.value,
+ )?))
+ }
+ }
+ new_members.insert(
+ n,
+ ObjMember {
+ add: false,
+ visibility: Visibility::Normal,
+ invoke: LazyBinding::Bindable(Gc::new(Box::new(ObjCompBinding {
+ context: ctx,
+ value: obj.value.clone(),
+ }))),
+ location: obj.value.1.clone(),
+ },
+ );
+ }
+ v => throw!(FieldMustBeStringGot(v.value_type())),
+ }
+
+ Ok(())
+ })?;
+
+ let this = ObjValue::new(None, Gc::new(new_members), Gc::new(Vec::new()));
+ future_this.fill(this.clone());
+ this
+ }
+ })
+}
+
+pub fn evaluate_apply(
+ context: Context,
+ value: &LocExpr,
+ args: &ArgsDesc,
+ loc: Option<&ExprLocation>,
+ tailstrict: bool,
+) -> Result<Val> {
+ let value = evaluate(context.clone(), value)?;
+ Ok(match value {
+ Val::Func(f) => {
+ let body = || f.evaluate(context, loc, args, tailstrict);
+ if tailstrict {
+ body()?
+ } else {
+ push(loc, || format!("function <{}> call", f.name()), body)?
+ }
+ }
+ v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),
+ })
+}
+
+pub fn evaluate_assert(context: Context, assertion: &AssertStmt) -> Result<()> {
+ let value = &assertion.0;
+ let msg = &assertion.1;
+ let assertion_result = push(
+ value.1.as_ref(),
+ || "assertion condition".to_owned(),
+ || {
+ evaluate(context.clone(), value)?
+ .try_cast_bool("assertion condition should be of type `boolean`")
+ },
+ )?;
+ if !assertion_result {
+ push(
+ value.1.as_ref(),
+ || "assertion failure".to_owned(),
+ || {
+ if let Some(msg) = msg {
+ throw!(AssertionFailed(evaluate(context, msg)?.to_string()?));
+ } else {
+ throw!(AssertionFailed(Val::Null.to_string()?));
+ }
+ },
+ )?
+ }
+ Ok(())
+}
+
+pub fn evaluate_named(context: Context, lexpr: &LocExpr, name: IStr) -> Result<Val> {
+ use Expr::*;
+ let LocExpr(expr, _loc) = lexpr;
+ Ok(match &**expr {
+ Function(params, body) => evaluate_method(context, name, params.clone(), body.clone()),
+ _ => evaluate(context, lexpr)?,
+ })
+}
+
+pub fn evaluate(context: Context, expr: &LocExpr) -> Result<Val> {
+ use Expr::*;
+ let LocExpr(expr, loc) = expr;
+ Ok(match &**expr {
+ Literal(LiteralType::This) => {
+ Val::Obj(context.this().clone().ok_or(CantUseSelfOutsideOfObject)?)
+ }
+ Literal(LiteralType::Super) => Val::Obj(
+ context
+ .super_obj()
+ .clone()
+ .ok_or(NoSuperFound)?
+ .with_this(context.this().clone().unwrap()),
+ ),
+ Literal(LiteralType::Dollar) => {
+ Val::Obj(context.dollar().clone().ok_or(NoTopLevelObjectFound)?)
+ }
+ Literal(LiteralType::True) => Val::Bool(true),
+ Literal(LiteralType::False) => Val::Bool(false),
+ Literal(LiteralType::Null) => Val::Null,
+ Parened(e) => evaluate(context, e)?,
+ Str(v) => Val::Str(v.clone()),
+ Num(v) => Val::new_checked_num(*v)?,
+ BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, v1, *o, v2)?,
+ UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,
+ Var(name) => push(
+ loc.as_ref(),
+ || format!("variable <{}>", name),
+ || context.binding(name.clone())?.evaluate(),
+ )?,
+ Index(value, index) => {
+ match (evaluate(context.clone(), value)?, evaluate(context, index)?) {
+ (Val::Obj(v), Val::Str(s)) => {
+ let sn = s.clone();
+ push(
+ loc.as_ref(),
+ || format!("field <{}> access", sn),
+ || {
+ if let Some(v) = v.get(s.clone())? {
+ Ok(v)
+ } else if v.get("__intrinsic_namespace__".into())?.is_some() {
+ Ok(Val::Func(Gc::new(FuncVal::Intrinsic(s))))
+ } else {
+ throw!(NoSuchField(s))
+ }
+ },
+ )?
+ }
+ (Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(
+ ValType::Obj,
+ ValType::Str,
+ n.value_type(),
+ )),
+
+ (Val::Arr(v), Val::Num(n)) => {
+ if n.fract() > f64::EPSILON {
+ throw!(FractionalIndex)
+ }
+ v.get(n as usize)?
+ .ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?
+ }
+ (Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),
+ (Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(
+ ValType::Arr,
+ ValType::Num,
+ n.value_type(),
+ )),
+
+ (Val::Str(s), Val::Num(n)) => Val::Str(
+ s.chars()
+ .skip(n as usize)
+ .take(1)
+ .collect::<String>()
+ .into(),
+ ),
+ (Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(
+ ValType::Str,
+ ValType::Num,
+ n.value_type(),
+ )),
+
+ (v, _) => throw!(CantIndexInto(v.value_type())),
+ }
+ }
+ LocalExpr(bindings, returned) => {
+ let mut new_bindings: FxHashMap<IStr, LazyVal> = HashMap::with_capacity_and_hasher(
+ bindings.len(),
+ BuildHasherDefault::<FxHasher>::default(),
+ );
+ let future_context = Context::new_future();
+ for b in bindings {
+ new_bindings.insert(
+ b.name.clone(),
+ evaluate_binding_in_future(b, future_context.clone()),
+ );
+ }
+ let context = context
+ .extend_bound(new_bindings)
+ .into_future(future_context);
+ evaluate(context, &returned.clone())?
+ }
+ Arr(items) => {
+ let mut out = Vec::with_capacity(items.len());
+ for item in items {
+ // TODO: Implement ArrValue::Lazy with same context for every element?
+ #[derive(Trace)]
+ #[trivially_drop]
+ struct ArrayElement {
+ context: Context,
+ item: LocExpr,
+ }
+ impl LazyValValue for ArrayElement {
+ fn get(self: Box<Self>) -> Result<Val> {
+ evaluate(self.context, &self.item)
+ }
+ }
+ out.push(LazyVal::new(Box::new(ArrayElement {
+ context: context.clone(),
+ item: item.clone(),
+ })));
+ }
+ Val::Arr(out.into())
+ }
+ ArrComp(expr, comp_specs) => {
+ let mut out = Vec::new();
+ evaluate_comp(context, comp_specs, &mut |ctx| {
+ out.push(evaluate(ctx, expr)?);
+ Ok(())
+ })?;
+ Val::Arr(ArrValue::Eager(Gc::new(out)))
+ }
+ Obj(body) => Val::Obj(evaluate_object(context, body)?),
+ ObjExtend(s, t) => evaluate_add_op(
+ &evaluate(context.clone(), s)?,
+ &Val::Obj(evaluate_object(context, t)?),
+ )?,
+ Apply(value, args, tailstrict) => {
+ evaluate_apply(context, value, args, loc.as_ref(), *tailstrict)?
+ }
+ Function(params, body) => {
+ evaluate_method(context, "anonymous".into(), params.clone(), body.clone())
+ }
+ Intrinsic(name) => Val::Func(Gc::new(FuncVal::Intrinsic(name.clone()))),
+ AssertExpr(assert, returned) => {
+ evaluate_assert(context.clone(), assert)?;
+ evaluate(context, returned)?
+ }
+ ErrorStmt(e) => push(
+ loc.as_ref(),
+ || "error statement".to_owned(),
+ || {
+ throw!(RuntimeError(
+ evaluate(context, e)?.try_cast_str("error text should be of type `string`")?,
+ ))
+ },
+ )?,
+ IfElse {
+ cond,
+ cond_then,
+ cond_else,
+ } => {
+ if push(
+ loc.as_ref(),
+ || "if condition".to_owned(),
+ || evaluate(context.clone(), &cond.0)?.try_cast_bool("in if condition"),
+ )? {
+ evaluate(context, cond_then)?
+ } else {
+ match cond_else {
+ Some(v) => evaluate(context, v)?,
+ None => Val::Null,
+ }
+ }
+ }
+ Import(path) => {
+ let tmp = loc
+ .clone()
+ .expect("imports cannot be used without loc_data")
+ .0;
+ let mut import_location = tmp.to_path_buf();
+ import_location.pop();
+ push(
+ loc.as_ref(),
+ || format!("import {:?}", path),
+ || with_state(|s| s.import_file(&import_location, path)),
+ )?
+ }
+ ImportStr(path) => {
+ let tmp = loc
+ .clone()
+ .expect("imports cannot be used without loc_data")
+ .0;
+ let mut import_location = tmp.to_path_buf();
+ import_location.pop();
+ Val::Str(with_state(|s| s.import_file_str(&import_location, path))?)
+ }
+ })
+}
crates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/jrsonnet-evaluator/src/evaluate/operator.rs
@@ -0,0 +1,118 @@
+use crate::{equals, evaluate, Context, Val};
+use crate::{error::Error::*, throw, Result};
+use jrsonnet_parser::{BinaryOpType, LocExpr, UnaryOpType};
+
+pub fn evaluate_unary_op(op: UnaryOpType, b: &Val) -> Result<Val> {
+ use UnaryOpType::*;
+ use Val::*;
+ Ok(match (op, b) {
+ (Not, Bool(v)) => Bool(!v),
+ (Minus, Num(n)) => Num(-*n),
+ (BitNot, Num(n)) => Num(!(*n as i32) as f64),
+ (op, o) => throw!(UnaryOperatorDoesNotOperateOnType(op, o.value_type())),
+ })
+}
+
+pub fn evaluate_add_op(a: &Val, b: &Val) -> Result<Val> {
+ use Val::*;
+ Ok(match (a, b) {
+ (Str(v1), Str(v2)) => Str(((**v1).to_owned() + v2).into()),
+
+ // Can't use generic json serialization way, because it depends on number to string concatenation (std.jsonnet:890)
+ (Num(n), Str(o)) => Str(format!("{}{}", n, o).into()),
+ (Str(o), Num(n)) => Str(format!("{}{}", o, n).into()),
+
+ (Str(s), o) => Str(format!("{}{}", s, o.clone().to_string()?).into()),
+ (o, Str(s)) => Str(format!("{}{}", o.clone().to_string()?, s).into()),
+
+ (Obj(v1), Obj(v2)) => Obj(v2.extend_from(v1.clone())),
+ (Arr(a), Arr(b)) => {
+ let mut out = Vec::with_capacity(a.len() + b.len());
+ out.extend(a.iter_lazy());
+ out.extend(b.iter_lazy());
+ Arr(out.into())
+ }
+ (Num(v1), Num(v2)) => Val::new_checked_num(v1 + v2)?,
+ _ => throw!(BinaryOperatorDoesNotOperateOnValues(
+ BinaryOpType::Add,
+ a.value_type(),
+ b.value_type(),
+ )),
+ })
+}
+
+pub fn evaluate_binary_op_special(
+ context: Context,
+ a: &LocExpr,
+ op: BinaryOpType,
+ b: &LocExpr,
+) -> Result<Val> {
+ use BinaryOpType::*;
+ use Val::*;
+ Ok(match (evaluate(context.clone(), a)?, op, b) {
+ (Bool(true), Or, _o) => Val::Bool(true),
+ (Bool(false), And, _o) => Val::Bool(false),
+ (a, op, eb) => evaluate_binary_op_normal(&a, op, &evaluate(context, eb)?)?,
+ })
+}
+
+pub fn evaluate_binary_op_normal(a: &Val, op: BinaryOpType, b: &Val) -> Result<Val> {
+ use BinaryOpType::*;
+ use Val::*;
+ Ok(match (a, op, b) {
+ (a, Add, b) => evaluate_add_op(a, b)?,
+
+ (a, Eq, b) => Bool(equals(a, b)?),
+ (a, Neq, b) => Bool(!equals(a, b)?),
+
+ (Str(v1), Mul, Num(v2)) => Str(v1.repeat(*v2 as usize).into()),
+
+ // Bool X Bool
+ (Bool(a), And, Bool(b)) => Bool(*a && *b),
+ (Bool(a), Or, Bool(b)) => Bool(*a || *b),
+
+ // Str X Str
+ (Str(v1), Lt, Str(v2)) => Bool(v1 < v2),
+ (Str(v1), Gt, Str(v2)) => Bool(v1 > v2),
+ (Str(v1), Lte, Str(v2)) => Bool(v1 <= v2),
+ (Str(v1), Gte, Str(v2)) => Bool(v1 >= v2),
+
+ // Num X Num
+ (Num(v1), Mul, Num(v2)) => Val::new_checked_num(v1 * v2)?,
+ (Num(v1), Div, Num(v2)) => {
+ if *v2 <= f64::EPSILON {
+ throw!(DivisionByZero)
+ }
+ Val::new_checked_num(v1 / v2)?
+ }
+
+ (Num(v1), Sub, Num(v2)) => Val::new_checked_num(v1 - v2)?,
+
+ (Num(v1), Lt, Num(v2)) => Bool(v1 < v2),
+ (Num(v1), Gt, Num(v2)) => Bool(v1 > v2),
+ (Num(v1), Lte, Num(v2)) => Bool(v1 <= v2),
+ (Num(v1), Gte, Num(v2)) => Bool(v1 >= v2),
+
+ (Num(v1), BitAnd, Num(v2)) => Num(((*v1 as i32) & (*v2 as i32)) as f64),
+ (Num(v1), BitOr, Num(v2)) => Num(((*v1 as i32) | (*v2 as i32)) as f64),
+ (Num(v1), BitXor, Num(v2)) => Num(((*v1 as i32) ^ (*v2 as i32)) as f64),
+ (Num(v1), Lhs, Num(v2)) => {
+ if *v2 < 0.0 {
+ throw!(RuntimeError("shift by negative exponent".into()))
+ }
+ Num(((*v1 as i32) << (*v2 as i32)) as f64)
+ }
+ (Num(v1), Rhs, Num(v2)) => {
+ if *v2 < 0.0 {
+ throw!(RuntimeError("shift by negative exponent".into()))
+ }
+ Num(((*v1 as i32) >> (*v2 as i32)) as f64)
+ }
+
+ _ => throw!(BinaryOperatorDoesNotOperateOnValues(
+ op,
+ a.value_type(),
+ b.value_type(),
+ )),
+ })
+}
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth1#![cfg_attr(feature = "unstable", feature(stmt_expr_attributes))]2#![warn(clippy::all, clippy::nursery)]3#![allow(4 macro_expanded_macro_exports_accessed_by_absolute_paths,5 clippy::ptr_arg6)]78mod builtin;9mod ctx;10mod dynamic;11pub mod error;12mod evaluate;13mod function;14mod import;15mod integrations;16mod map;17pub mod native;18mod obj;19pub mod trace;20pub mod typed;21mod val;2223pub use ctx::*;24pub use dynamic::*;25use error::{Error::*, LocError, Result, StackTraceElement};26pub use evaluate::*;27pub use function::parse_function_call;28pub use import::*;29use jrsonnet_gc::{Finalize, Gc, Trace};30pub use jrsonnet_interner::IStr;31use jrsonnet_parser::*;32use native::NativeCallback;33pub use obj::*;34use rustc_hash::FxHashMap;35use std::{36 cell::{Ref, RefCell, RefMut},37 collections::HashMap,38 fmt::Debug,39 hash::BuildHasherDefault,40 path::{Path, PathBuf},41 rc::Rc,42};43use trace::{offset_to_location, CodeLocation, CompactFormat, TraceFormat};44pub use val::*;4546pub trait Bindable: Trace {47 fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal>;48}49#[derive(Trace, Finalize, Clone)]50pub enum LazyBinding {51 Bindable(Gc<Box<dyn Bindable>>),52 Bound(LazyVal),53}5455impl Debug for LazyBinding {56 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {57 write!(f, "LazyBinding")58 }59}60impl LazyBinding {61 pub fn evaluate(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {62 match self {63 Self::Bindable(v) => v.bind(this, super_obj),64 Self::Bound(v) => Ok(v.clone()),65 }66 }67}6869pub struct EvaluationSettings {70 /// Limits recursion by limiting the number of stack frames71 pub max_stack: usize,72 /// Limits amount of stack trace items preserved73 pub max_trace: usize,74 /// Used for s`td.extVar`75 pub ext_vars: HashMap<IStr, Val>,76 /// Used for ext.native77 pub ext_natives: HashMap<IStr, Gc<NativeCallback>>,78 /// TLA vars79 pub tla_vars: HashMap<IStr, Val>,80 /// Global variables are inserted in default context81 pub globals: HashMap<IStr, Val>,82 /// Used to resolve file locations/contents83 pub import_resolver: Box<dyn ImportResolver>,84 /// Used in manifestification functions85 pub manifest_format: ManifestFormat,86 /// Used for bindings87 pub trace_format: Box<dyn TraceFormat>,88}89impl Default for EvaluationSettings {90 fn default() -> Self {91 Self {92 max_stack: 200,93 max_trace: 20,94 globals: Default::default(),95 ext_vars: Default::default(),96 ext_natives: Default::default(),97 tla_vars: Default::default(),98 import_resolver: Box::new(DummyImportResolver),99 manifest_format: ManifestFormat::Json(4),100 trace_format: Box::new(CompactFormat {101 padding: 4,102 resolver: trace::PathResolver::Absolute,103 }),104 }105 }106}107108#[derive(Default)]109struct EvaluationData {110 /// Used for stack overflow detection, stacktrace is populated on unwind111 stack_depth: usize,112 /// Contains file source codes and evaluation results for imports and pretty-printed stacktraces113 files: HashMap<Rc<Path>, FileData>,114 str_files: HashMap<Rc<Path>, IStr>,115}116117pub struct FileData {118 source_code: IStr,119 parsed: LocExpr,120 evaluated: Option<Val>,121}122#[derive(Default)]123pub struct EvaluationStateInternals {124 /// Internal state125 data: RefCell<EvaluationData>,126 /// Settings, safe to change at runtime127 settings: RefCell<EvaluationSettings>,128}129130thread_local! {131 /// Contains the state for a currently executed file.132 /// Global state is fine here.133 pub(crate) static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)134}135pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {136 EVAL_STATE.with(|s| f(s.borrow().as_ref().unwrap()))137}138pub(crate) fn push<T>(139 e: Option<&ExprLocation>,140 frame_desc: impl FnOnce() -> String,141 f: impl FnOnce() -> Result<T>,142) -> Result<T> {143 with_state(|s| s.push(e, frame_desc, f))144}145146pub fn push_stack_frame<T>(147 e: Option<&ExprLocation>,148 frame_desc: impl FnOnce() -> String,149 f: impl FnOnce() -> Result<T>,150) -> Result<T> {151 push(e, frame_desc, f)152}153154/// Maintains stack trace and import resolution155#[derive(Default, Clone)]156pub struct EvaluationState(Rc<EvaluationStateInternals>);157158impl EvaluationState {159 /// Parses and adds file as loaded160 pub fn add_file(&self, path: Rc<Path>, source_code: IStr) -> Result<()> {161 self.add_parsed_file(162 path.clone(),163 source_code.clone(),164 parse(165 &source_code,166 &ParserSettings {167 file_name: path.clone(),168 loc_data: true,169 },170 )171 .map_err(|error| ImportSyntaxError {172 error: Box::new(error),173 path: path.to_owned(),174 source_code,175 })?,176 )?;177178 Ok(())179 }180181 /// Adds file by source code and parsed expr182 pub fn add_parsed_file(183 &self,184 name: Rc<Path>,185 source_code: IStr,186 parsed: LocExpr,187 ) -> Result<()> {188 self.data_mut().files.insert(189 name,190 FileData {191 source_code,192 parsed,193 evaluated: None,194 },195 );196197 Ok(())198 }199 pub fn get_source(&self, name: &Path) -> Option<IStr> {200 let ro_map = &self.data().files;201 ro_map.get(name).map(|value| value.source_code.clone())202 }203 pub fn map_source_locations(&self, file: &Path, locs: &[usize]) -> Vec<CodeLocation> {204 offset_to_location(&self.get_source(file).unwrap(), locs)205 }206207 pub(crate) fn import_file(&self, from: &Path, path: &Path) -> Result<Val> {208 let file_path = self.resolve_file(from, path)?;209 {210 let data = self.data();211 let files = &data.files;212 if files.contains_key(&file_path as &Path) {213 drop(data);214 return self.evaluate_loaded_file_raw(&file_path);215 }216 }217 let contents = self.load_file_contents(&file_path)?;218 self.add_file(file_path.clone(), contents)?;219 self.evaluate_loaded_file_raw(&file_path)220 }221 pub(crate) fn import_file_str(&self, from: &Path, path: &Path) -> Result<IStr> {222 let path = self.resolve_file(from, path)?;223 if !self.data().str_files.contains_key(&path) {224 let file_str = self.load_file_contents(&path)?;225 self.data_mut().str_files.insert(path.clone(), file_str);226 }227 Ok(self.data().str_files.get(&path).cloned().unwrap())228 }229230 fn evaluate_loaded_file_raw(&self, name: &Path) -> Result<Val> {231 let expr: LocExpr = {232 let ro_map = &self.data().files;233 let value = ro_map234 .get(name)235 .unwrap_or_else(|| panic!("file not added: {:?}", name));236 if let Some(ref evaluated) = value.evaluated {237 return Ok(evaluated.clone());238 }239 value.parsed.clone()240 };241 let value = evaluate(self.create_default_context(), &expr)?;242 {243 self.data_mut()244 .files245 .get_mut(name)246 .unwrap()247 .evaluated248 .replace(value.clone());249 }250 Ok(value)251 }252253 /// Adds standard library global variable (std) to this evaluator254 pub fn with_stdlib(&self) -> &Self {255 use jrsonnet_stdlib::STDLIB_STR;256 let std_path: Rc<Path> = PathBuf::from("std.jsonnet").into();257 self.run_in_state(|| {258 self.add_parsed_file(259 std_path.clone(),260 STDLIB_STR.to_owned().into(),261 builtin::get_parsed_stdlib(),262 )263 .unwrap();264 let val = self.evaluate_loaded_file_raw(&std_path).unwrap();265 self.settings_mut().globals.insert("std".into(), val);266 });267 self268 }269270 /// Creates context with all passed global variables271 pub fn create_default_context(&self) -> Context {272 let globals = &self.settings().globals;273 let mut new_bindings: FxHashMap<IStr, LazyVal> =274 FxHashMap::with_capacity_and_hasher(globals.len(), BuildHasherDefault::default());275 for (name, value) in globals.iter() {276 new_bindings.insert(name.clone(), LazyVal::new_resolved(value.clone()));277 }278 Context::new().extend_bound(new_bindings)279 }280281 /// Executes code creating a new stack frame282 pub fn push<T>(283 &self,284 e: Option<&ExprLocation>,285 frame_desc: impl FnOnce() -> String,286 f: impl FnOnce() -> Result<T>,287 ) -> Result<T> {288 {289 let mut data = self.data_mut();290 let stack_depth = &mut data.stack_depth;291 if *stack_depth > self.max_stack() {292 // Error creation uses data, so i drop guard here293 drop(data);294 throw!(StackOverflow);295 } else {296 *stack_depth += 1;297 }298 }299 let result = f();300 self.data_mut().stack_depth -= 1;301 if let Err(mut err) = result {302 err.trace_mut().0.push(StackTraceElement {303 location: e.cloned(),304 desc: frame_desc(),305 });306 return Err(err);307 }308 result309 }310311 /// Runs passed function in state (required if function needs to modify stack trace)312 pub fn run_in_state<T>(&self, f: impl FnOnce() -> T) -> T {313 EVAL_STATE.with(|v| {314 let has_state = v.borrow().is_some();315 if !has_state {316 v.borrow_mut().replace(self.clone());317 }318 let result = f();319 if !has_state {320 v.borrow_mut().take();321 }322 result323 })324 }325326 pub fn stringify_err(&self, e: &LocError) -> String {327 let mut out = String::new();328 self.settings()329 .trace_format330 .write_trace(&mut out, self, e)331 .unwrap();332 out333 }334335 pub fn manifest(&self, val: Val) -> Result<IStr> {336 self.run_in_state(|| val.manifest(&self.manifest_format()))337 }338 pub fn manifest_multi(&self, val: Val) -> Result<Vec<(IStr, IStr)>> {339 self.run_in_state(|| val.manifest_multi(&self.manifest_format()))340 }341 pub fn manifest_stream(&self, val: Val) -> Result<Vec<IStr>> {342 self.run_in_state(|| val.manifest_stream(&self.manifest_format()))343 }344345 /// If passed value is function then call with set TLA346 pub fn with_tla(&self, val: Val) -> Result<Val> {347 self.run_in_state(|| {348 Ok(match val {349 Val::Func(func) => push(350 None,351 || "during TLA call".to_owned(),352 || {353 func.evaluate_map(354 self.create_default_context(),355 &self.settings().tla_vars,356 true,357 )358 },359 )?,360 v => v,361 })362 })363 }364}365366/// Internals367impl EvaluationState {368 fn data(&self) -> Ref<EvaluationData> {369 self.0.data.borrow()370 }371 fn data_mut(&self) -> RefMut<EvaluationData> {372 self.0.data.borrow_mut()373 }374 pub fn settings(&self) -> Ref<EvaluationSettings> {375 self.0.settings.borrow()376 }377 pub fn settings_mut(&self) -> RefMut<EvaluationSettings> {378 self.0.settings.borrow_mut()379 }380}381382/// Raw methods evaluate passed values but don't perform TLA execution383impl EvaluationState {384 pub fn evaluate_file_raw(&self, name: &Path) -> Result<Val> {385 self.run_in_state(|| self.import_file(&std::env::current_dir().expect("cwd"), name))386 }387 pub fn evaluate_file_raw_nocwd(&self, name: &Path) -> Result<Val> {388 self.run_in_state(|| self.import_file(&PathBuf::from("."), name))389 }390 /// Parses and evaluates the given snippet391 pub fn evaluate_snippet_raw(&self, source: Rc<Path>, code: IStr) -> Result<Val> {392 let parsed = parse(393 &code,394 &ParserSettings {395 file_name: source.clone(),396 loc_data: true,397 },398 )399 .map_err(|e| ImportSyntaxError {400 path: source.clone(),401 source_code: code.clone(),402 error: Box::new(e),403 })?;404 self.add_parsed_file(source, code, parsed.clone())?;405 self.evaluate_expr_raw(parsed)406 }407 /// Evaluates the parsed expression408 pub fn evaluate_expr_raw(&self, code: LocExpr) -> Result<Val> {409 self.run_in_state(|| evaluate(self.create_default_context(), &code))410 }411}412413/// Settings utilities414impl EvaluationState {415 pub fn add_ext_var(&self, name: IStr, value: Val) {416 self.settings_mut().ext_vars.insert(name, value);417 }418 pub fn add_ext_str(&self, name: IStr, value: IStr) {419 self.add_ext_var(name, Val::Str(value));420 }421 pub fn add_ext_code(&self, name: IStr, code: IStr) -> Result<()> {422 let value =423 self.evaluate_snippet_raw(PathBuf::from(format!("ext_code {}", name)).into(), code)?;424 self.add_ext_var(name, value);425 Ok(())426 }427428 pub fn add_tla(&self, name: IStr, value: Val) {429 self.settings_mut().tla_vars.insert(name, value);430 }431 pub fn add_tla_str(&self, name: IStr, value: IStr) {432 self.add_tla(name, Val::Str(value));433 }434 pub fn add_tla_code(&self, name: IStr, code: IStr) -> Result<()> {435 let value =436 self.evaluate_snippet_raw(PathBuf::from(format!("tla_code {}", name)).into(), code)?;437 self.add_tla(name, value);438 Ok(())439 }440441 pub fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {442 self.settings().import_resolver.resolve_file(from, path)443 }444 pub fn load_file_contents(&self, path: &Path) -> Result<IStr> {445 self.settings().import_resolver.load_file_contents(path)446 }447448 pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {449 Ref::map(self.settings(), |s| &*s.import_resolver)450 }451 pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {452 self.settings_mut().import_resolver = resolver;453 }454455 pub fn add_native(&self, name: IStr, cb: Gc<NativeCallback>) {456 self.settings_mut().ext_natives.insert(name, cb);457 }458459 pub fn manifest_format(&self) -> ManifestFormat {460 self.settings().manifest_format.clone()461 }462 pub fn set_manifest_format(&self, format: ManifestFormat) {463 self.settings_mut().manifest_format = format;464 }465466 pub fn trace_format(&self) -> Ref<dyn TraceFormat> {467 Ref::map(self.settings(), |s| &*s.trace_format)468 }469 pub fn set_trace_format(&self, format: Box<dyn TraceFormat>) {470 self.settings_mut().trace_format = format;471 }472473 pub fn max_trace(&self) -> usize {474 self.settings().max_trace475 }476 pub fn set_max_trace(&self, trace: usize) {477 self.settings_mut().max_trace = trace;478 }479480 pub fn max_stack(&self) -> usize {481 self.settings().max_stack482 }483 pub fn set_max_stack(&self, trace: usize) {484 self.settings_mut().max_stack = trace;485 }486}487488#[cfg(test)]489pub mod tests {490 use super::Val;491 use crate::{492 error::Error::*, native::NativeCallbackHandler, primitive_equals, EvaluationState,493 };494 use jrsonnet_gc::{Finalize, Gc, Trace};495 use jrsonnet_interner::IStr;496 use jrsonnet_parser::*;497 use std::{498 path::{Path, PathBuf},499 rc::Rc,500 };501502 #[test]503 #[should_panic]504 fn eval_state_stacktrace() {505 let state = EvaluationState::default();506 state.run_in_state(|| {507 state508 .push(509 Some(&ExprLocation(PathBuf::from("test1.jsonnet").into(), 10, 20)),510 || "outer".to_owned(),511 || {512 state.push(513 Some(&ExprLocation(PathBuf::from("test2.jsonnet").into(), 30, 40)),514 || "inner".to_owned(),515 || Err(RuntimeError("".into()).into()),516 )?;517 Ok(())518 },519 )520 .unwrap();521 });522 }523524 #[test]525 fn eval_state_standard() {526 let state = EvaluationState::default();527 state.with_stdlib();528 assert!(primitive_equals(529 &state530 .evaluate_snippet_raw(531 PathBuf::from("raw.jsonnet").into(),532 r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#.into()533 )534 .unwrap(),535 &Val::Bool(true),536 )537 .unwrap());538 }539540 macro_rules! eval {541 ($str: expr) => {542 EvaluationState::default()543 .with_stdlib()544 .evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())545 .unwrap()546 };547 }548 macro_rules! eval_json {549 ($str: expr) => {{550 let evaluator = EvaluationState::default();551 evaluator.with_stdlib();552 evaluator.run_in_state(|| {553 evaluator554 .evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())555 .unwrap()556 .to_json(0)557 .unwrap()558 .replace("\n", "")559 })560 }};561 }562563 /// Asserts given code returns `true`564 macro_rules! assert_eval {565 ($str: expr) => {566 assert!(primitive_equals(&eval!($str), &Val::Bool(true)).unwrap())567 };568 }569570 /// Asserts given code returns `false`571 macro_rules! assert_eval_neg {572 ($str: expr) => {573 assert!(primitive_equals(&eval!($str), &Val::Bool(false)).unwrap())574 };575 }576 macro_rules! assert_json {577 ($str: expr, $out: expr) => {578 assert_eq!(eval_json!($str), $out.replace("\t", ""))579 };580 }581582 /// Sanity checking, before trusting to another tests583 #[test]584 fn equality_operator() {585 assert_eval!("2 == 2");586 assert_eval_neg!("2 != 2");587 assert_eval!("2 != 3");588 assert_eval_neg!("2 == 3");589 assert_eval!("'Hello' == 'Hello'");590 assert_eval_neg!("'Hello' != 'Hello'");591 assert_eval!("'Hello' != 'World'");592 assert_eval_neg!("'Hello' == 'World'");593 }594595 #[test]596 fn math_evaluation() {597 assert_eval!("2 + 2 * 2 == 6");598 assert_eval!("3 + (2 + 2 * 2) == 9");599 }600601 #[test]602 fn string_concat() {603 assert_eval!("'Hello' + 'World' == 'HelloWorld'");604 assert_eval!("'Hello' * 3 == 'HelloHelloHello'");605 assert_eval!("'Hello' + 'World' * 3 == 'HelloWorldWorldWorld'");606 }607608 #[test]609 fn faster_join() {610 assert_eval!("std.join([0,0], [[1,2],[3,4],[5,6]]) == [1,2,0,0,3,4,0,0,5,6]");611 assert_eval!("std.join(',', ['1','2','3','4']) == '1,2,3,4'");612 }613614 #[test]615 fn function_contexts() {616 assert_eval!(617 r#"618 local k = {619 t(name = self.h): [self.h, name],620 h: 3,621 };622 local f = {623 t: k.t(),624 h: 4,625 };626 f.t[0] == f.t[1]627 "#628 );629 }630631 #[test]632 fn local() {633 assert_eval!("local a = 2; local b = 3; a + b == 5");634 assert_eval!("local a = 1, b = a + 1; a + b == 3");635 assert_eval!("local a = 1; local a = 2; a == 2");636 }637638 #[test]639 fn object_lazyness() {640 assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);641 }642643 #[test]644 fn object_inheritance() {645 assert_json!("{a: self.b} + {b:3}", r#"{"a": 3,"b": 3}"#);646 }647648 #[test]649 fn object_assertion_success() {650 eval!("{assert \"a\" in self} + {a:2}");651 }652653 #[test]654 fn object_assertion_error() {655 eval!("{assert \"a\" in self}");656 }657658 #[test]659 fn lazy_args() {660 eval!("local test(a) = 2; test(error '3')");661 }662663 #[test]664 #[should_panic]665 fn tailstrict_args() {666 eval!("local test(a) = 2; test(error '3') tailstrict");667 }668669 #[test]670 #[should_panic]671 fn no_binding_error() {672 eval!("a");673 }674675 #[test]676 fn test_object() {677 assert_json!("{a:2}", r#"{"a": 2}"#);678 assert_json!("{a:2+2}", r#"{"a": 4}"#);679 assert_json!("{a:2}+{b:2}", r#"{"a": 2,"b": 2}"#);680 assert_json!("{b:3}+{b:2}", r#"{"b": 2}"#);681 assert_json!("{b:3}+{b+:2}", r#"{"b": 5}"#);682 assert_json!("local test='a'; {[test]:2}", r#"{"a": 2}"#);683 assert_json!(684 r#"685 {686 name: "Alice",687 welcome: "Hello " + self.name + "!",688 }689 "#,690 r#"{"name": "Alice","welcome": "Hello Alice!"}"#691 );692 assert_json!(693 r#"694 {695 name: "Alice",696 welcome: "Hello " + self.name + "!",697 } + {698 name: "Bob"699 }700 "#,701 r#"{"name": "Bob","welcome": "Hello Bob!"}"#702 );703 }704705 #[test]706 fn functions() {707 assert_json!(r#"local a = function(b, c = 2) b + c; a(2)"#, "4");708 assert_json!(709 r#"local a = function(b, c = "Dear") b + c + d, d = "World"; a("Hello")"#,710 r#""HelloDearWorld""#711 );712 }713714 #[test]715 fn local_methods() {716 assert_json!(r#"local a(b, c = 2) = b + c; a(2)"#, "4");717 assert_json!(718 r#"local a(b, c = "Dear") = b + c + d, d = "World"; a("Hello")"#,719 r#""HelloDearWorld""#720 );721 }722723 #[test]724 fn object_locals() {725 assert_json!(r#"{local a = 3, b: a}"#, r#"{"b": 3}"#);726 assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b": 3}"#);727 assert_json!(728 r#"{local a = function (b) {[b]:4}, test: a("test")}"#,729 r#"{"test": {"test": 4}}"#730 );731 }732733 #[test]734 fn object_comp() {735 assert_json!(736 r#"{local t = "a", ["h"+i+"_"+z]: if "h"+(i-1)+"_"+z in self then t+1 else 0+t for i in [1,2,3] for z in [2,3,4] if z != i}"#,737 "{\"h1_2\": \"0a\",\"h1_3\": \"0a\",\"h1_4\": \"0a\",\"h2_3\": \"a1\",\"h2_4\": \"a1\",\"h3_2\": \"0a\",\"h3_4\": \"a1\"}"738 )739 }740741 #[test]742 fn direct_self() {743 println!(744 "{:#?}",745 eval!(746 r#"747 {748 local me = self,749 a: 3,750 b(): me.a,751 }752 "#753 )754 );755 }756757 #[test]758 fn indirect_self() {759 // `self` assigned to `me` was lost when being760 // referenced from field761 eval!(762 r#"{763 local me = self,764 a: 3,765 b: me.a,766 }.b"#767 );768 }769770 // We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly771 #[test]772 fn std_assert_ok() {773 eval!("std.assertEqual(4.5 << 2, 16)");774 }775776 #[test]777 #[should_panic]778 fn std_assert_failure() {779 eval!("std.assertEqual(4.5 << 2, 15)");780 }781782 #[test]783 fn string_is_string() {784 assert!(primitive_equals(785 &eval!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),786 &Val::Bool(false),787 )788 .unwrap());789 }790791 #[test]792 fn base64_works() {793 assert_json!(r#"std.base64("test")"#, r#""dGVzdA==""#);794 }795796 #[test]797 fn utf8_chars() {798 assert_json!(799 r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,800 r#"{"c": 128526,"l": 1}"#801 )802 }803804 #[test]805 fn json() {806 assert_json!(807 r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,808 r#""{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}""#809 );810 }811812 #[test]813 fn parse_json() {814 assert_json!(815 r#"std.parseJson('{"a": -1,"b": 1,"c": 3.141,"d": []}')"#,816 r#"{"a": -1,"b": 1,"c": 3.141,"d": []}"#817 );818 // TODO: this should in fact fail as is no proper JSON syntax819 assert_json!(820 r#"std.parseJson("{a:-1, b:1, c:3.141, d:[]}")"#,821 r#"{"a": -1,"b": 1,"c": 3.141,"d": []}"#822 );823 // TODO: this is also no valid JSON824 assert_json!(r#"std.parseJson('local x = 2; x * x')"#, r#"4"#);825 }826827 #[test]828 fn test() {829 assert_json!(830 r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,831 "[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"832 );833 }834835 #[test]836 fn sjsonnet() {837 eval!(838 r#"839 local x0 = {k: 1};840 local x1 = {k: x0.k + x0.k};841 local x2 = {k: x1.k + x1.k};842 local x3 = {k: x2.k + x2.k};843 local x4 = {k: x3.k + x3.k};844 local x5 = {k: x4.k + x4.k};845 local x6 = {k: x5.k + x5.k};846 local x7 = {k: x6.k + x6.k};847 local x8 = {k: x7.k + x7.k};848 local x9 = {k: x8.k + x8.k};849 local x10 = {k: x9.k + x9.k};850 local x11 = {k: x10.k + x10.k};851 local x12 = {k: x11.k + x11.k};852 local x13 = {k: x12.k + x12.k};853 local x14 = {k: x13.k + x13.k};854 local x15 = {k: x14.k + x14.k};855 local x16 = {k: x15.k + x15.k};856 local x17 = {k: x16.k + x16.k};857 local x18 = {k: x17.k + x17.k};858 local x19 = {k: x18.k + x18.k};859 local x20 = {k: x19.k + x19.k};860 local x21 = {k: x20.k + x20.k};861 x21.k862 "#863 );864 }865866 // This test is commented out by default, because of huge compilation slowdown867 // #[bench]868 // fn bench_codegen(b: &mut Bencher) {869 // b.iter(|| {870 // #[allow(clippy::all)]871 // let stdlib = {872 // use jrsonnet_parser::*;873 // include!(concat!(env!("OUT_DIR"), "/stdlib.rs"))874 // };875 // stdlib876 // })877 // }878879 /*880 #[bench]881 fn bench_serialize(b: &mut Bencher) {882 b.iter(|| {883 bincode::deserialize::<jrsonnet_parser::LocExpr>(include_bytes!(concat!(884 env!("OUT_DIR"),885 "/stdlib.bincode"886 )))887 .expect("deserialize stdlib")888 })889 }890891 #[bench]892 fn bench_parse(b: &mut Bencher) {893 b.iter(|| {894 jrsonnet_parser::parse(895 jrsonnet_stdlib::STDLIB_STR,896 &jrsonnet_parser::ParserSettings {897 loc_data: true,898 file_name: Rc::new(PathBuf::from("std.jsonnet")),899 },900 )901 })902 }903 */904905 #[test]906 fn equality() {907 println!(908 "{:?}",909 jrsonnet_parser::parse(910 "{ x: 1, y: 2 } == { x: 1, y: 2 }",911 &ParserSettings {912 file_name: PathBuf::from("equality").into(),913 loc_data: true,914 }915 )916 );917 assert_eval!("{ x: 1, y: 2 } == { x: 1, y: 2 }")918 }919920 #[test]921 fn native_ext() -> crate::error::Result<()> {922 use super::native::NativeCallback;923 let evaluator = EvaluationState::default();924925 evaluator.with_stdlib();926927 #[derive(Trace, Finalize)]928 struct NativeAdd;929 impl NativeCallbackHandler for NativeAdd {930 fn call(&self, from: Option<Rc<Path>>, args: &[Val]) -> crate::error::Result<Val> {931 assert_eq!(932 &from.unwrap() as &Path,933 &PathBuf::from("native_caller.jsonnet")934 );935 match (&args[0], &args[1]) {936 (Val::Num(a), Val::Num(b)) => Ok(Val::Num(a + b)),937 (_, _) => unreachable!(),938 }939 }940 }941 evaluator.settings_mut().ext_natives.insert(942 "native_add".into(),943 Gc::new(NativeCallback::new(944 ParamsDesc(Rc::new(vec![945 Param("a".into(), None),946 Param("b".into(), None),947 ])),948 Box::new(NativeAdd),949 )),950 );951 evaluator.evaluate_snippet_raw(952 PathBuf::from("native_caller.jsonnet").into(),953 "std.assertEqual(std.native(\"native_add\")(1, 2), 3)".into(),954 )?;955 Ok(())956 }957958 #[test]959 fn constant_intrinsic() -> crate::error::Result<()> {960 assert_eval!(961 "local std2 = std; local std = std2 { primitiveEquals(a, b):: false }; 1 == 1"962 );963 Ok(())964 }965966 #[test]967 fn standalone_super() -> crate::error::Result<()> {968 assert_eval!(969 r#"970 local obj = {971 a: 1,972 b: 2,973 c: 3,974 };975 local test = obj + {976 fields: std.objectFields(super),977 d: 5,978 };979 test.fields == ['a', 'b', 'c']980 "#981 );982 Ok(())983 }984985 #[test]986 fn comp_self() -> crate::error::Result<()> {987 assert_eval!(988 r#"989 std.objectFields({990 a:{991 [name]: name for name in std.objectFields(self)992 },993 b: 2,994 c: 3,995 }.a) == ['a', 'b', 'c']996 "#997 );998999 Ok(())1000 }10011002 struct TestImportResolver(IStr);1003 impl crate::import::ImportResolver for TestImportResolver {1004 fn resolve_file(&self, _: &Path, _: &Path) -> crate::error::Result<Rc<Path>> {1005 Ok(PathBuf::from("/test").into())1006 }10071008 fn load_file_contents(&self, _: &Path) -> crate::error::Result<IStr> {1009 Ok(self.0.clone())1010 }10111012 unsafe fn as_any(&self) -> &dyn std::any::Any {1013 panic!()1014 }1015 }10161017 #[test]1018 fn issue_23() {1019 let state = EvaluationState::default();1020 state.set_import_resolver(Box::new(TestImportResolver(r#"import "/test""#.into())));1021 let _ = state.evaluate_file_raw(&PathBuf::from("/test"));1022 }10231024 #[test]1025 fn issue_40() {1026 let state = EvaluationState::default();1027 state.with_stdlib();10281029 let error = state1030 .evaluate_snippet_raw(1031 PathBuf::from("issue40.jsonnet").into(),1032 r#"1033 local conf = {1034 n: ""1035 };1036 1037 local result = conf + {1038 assert std.isNumber(self.n): "is number"1039 };10401041 std.manifestJsonEx(result, "")1042 "#1043 .into(),1044 )1045 .unwrap_err();1046 assert_eq!(error.error().to_string(), "assert failed: is number");1047 }1048}1#![cfg_attr(feature = "unstable", feature(stmt_expr_attributes))]2#![warn(clippy::all, clippy::nursery)]3#![allow(4 macro_expanded_macro_exports_accessed_by_absolute_paths,5 clippy::ptr_arg6)]78mod builtin;9mod ctx;10mod dynamic;11pub mod error;12mod evaluate;13mod function;14mod import;15mod integrations;16mod map;17pub mod native;18mod obj;19pub mod trace;20pub mod typed;21mod val;2223pub use ctx::*;24pub use dynamic::*;25use error::{Error::*, LocError, Result, StackTraceElement};26pub use evaluate::*;27pub use function::parse_function_call;28pub use import::*;29use jrsonnet_gc::{Finalize, Gc, Trace};30pub use jrsonnet_interner::IStr;31use jrsonnet_parser::*;32use native::NativeCallback;33pub use obj::*;34use rustc_hash::FxHashMap;35use std::{36 cell::{Ref, RefCell, RefMut},37 collections::HashMap,38 fmt::Debug,39 hash::BuildHasherDefault,40 path::{Path, PathBuf},41 rc::Rc,42};43use trace::{offset_to_location, CodeLocation, CompactFormat, TraceFormat};44pub use val::*;4546pub trait Bindable: Trace {47 fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal>;48}49#[derive(Trace, Finalize, Clone)]50pub enum LazyBinding {51 Bindable(Gc<Box<dyn Bindable>>),52 Bound(LazyVal),53}5455impl Debug for LazyBinding {56 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {57 write!(f, "LazyBinding")58 }59}60impl LazyBinding {61 pub fn evaluate(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {62 match self {63 Self::Bindable(v) => v.bind(this, super_obj),64 Self::Bound(v) => Ok(v.clone()),65 }66 }67}6869pub struct EvaluationSettings {70 /// Limits recursion by limiting the number of stack frames71 pub max_stack: usize,72 /// Limits amount of stack trace items preserved73 pub max_trace: usize,74 /// Used for s`td.extVar`75 pub ext_vars: HashMap<IStr, Val>,76 /// Used for ext.native77 pub ext_natives: HashMap<IStr, Gc<NativeCallback>>,78 /// TLA vars79 pub tla_vars: HashMap<IStr, Val>,80 /// Global variables are inserted in default context81 pub globals: HashMap<IStr, Val>,82 /// Used to resolve file locations/contents83 pub import_resolver: Box<dyn ImportResolver>,84 /// Used in manifestification functions85 pub manifest_format: ManifestFormat,86 /// Used for bindings87 pub trace_format: Box<dyn TraceFormat>,88}89impl Default for EvaluationSettings {90 fn default() -> Self {91 Self {92 max_stack: 200,93 max_trace: 20,94 globals: Default::default(),95 ext_vars: Default::default(),96 ext_natives: Default::default(),97 tla_vars: Default::default(),98 import_resolver: Box::new(DummyImportResolver),99 manifest_format: ManifestFormat::Json(4),100 trace_format: Box::new(CompactFormat {101 padding: 4,102 resolver: trace::PathResolver::Absolute,103 }),104 }105 }106}107108#[derive(Default)]109struct EvaluationData {110 /// Used for stack overflow detection, stacktrace is populated on unwind111 stack_depth: usize,112 /// Contains file source codes and evaluation results for imports and pretty-printed stacktraces113 files: HashMap<Rc<Path>, FileData>,114 str_files: HashMap<Rc<Path>, IStr>,115}116117pub struct FileData {118 source_code: IStr,119 parsed: LocExpr,120 evaluated: Option<Val>,121}122#[derive(Default)]123pub struct EvaluationStateInternals {124 /// Internal state125 data: RefCell<EvaluationData>,126 /// Settings, safe to change at runtime127 settings: RefCell<EvaluationSettings>,128}129130thread_local! {131 /// Contains the state for a currently executed file.132 /// Global state is fine here.133 pub(crate) static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)134}135pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {136 EVAL_STATE.with(|s| f(s.borrow().as_ref().unwrap()))137}138pub(crate) fn push<T>(139 e: Option<&ExprLocation>,140 frame_desc: impl FnOnce() -> String,141 f: impl FnOnce() -> Result<T>,142) -> Result<T> {143 with_state(|s| s.push(e, frame_desc, f))144}145146pub fn push_stack_frame<T>(147 e: Option<&ExprLocation>,148 frame_desc: impl FnOnce() -> String,149 f: impl FnOnce() -> Result<T>,150) -> Result<T> {151 push(e, frame_desc, f)152}153154/// Maintains stack trace and import resolution155#[derive(Default, Clone)]156pub struct EvaluationState(Rc<EvaluationStateInternals>);157158impl EvaluationState {159 /// Parses and adds file as loaded160 pub fn add_file(&self, path: Rc<Path>, source_code: IStr) -> Result<()> {161 self.add_parsed_file(162 path.clone(),163 source_code.clone(),164 parse(165 &source_code,166 &ParserSettings {167 file_name: path.clone(),168 loc_data: true,169 },170 )171 .map_err(|error| ImportSyntaxError {172 error: Box::new(error),173 path: path.to_owned(),174 source_code,175 })?,176 )?;177178 Ok(())179 }180181 /// Adds file by source code and parsed expr182 pub fn add_parsed_file(183 &self,184 name: Rc<Path>,185 source_code: IStr,186 parsed: LocExpr,187 ) -> Result<()> {188 self.data_mut().files.insert(189 name,190 FileData {191 source_code,192 parsed,193 evaluated: None,194 },195 );196197 Ok(())198 }199 pub fn get_source(&self, name: &Path) -> Option<IStr> {200 let ro_map = &self.data().files;201 ro_map.get(name).map(|value| value.source_code.clone())202 }203 pub fn map_source_locations(&self, file: &Path, locs: &[usize]) -> Vec<CodeLocation> {204 offset_to_location(&self.get_source(file).unwrap(), locs)205 }206207 pub(crate) fn import_file(&self, from: &Path, path: &Path) -> Result<Val> {208 let file_path = self.resolve_file(from, path)?;209 {210 let data = self.data();211 let files = &data.files;212 if files.contains_key(&file_path as &Path) {213 drop(data);214 return self.evaluate_loaded_file_raw(&file_path);215 }216 }217 let contents = self.load_file_contents(&file_path)?;218 self.add_file(file_path.clone(), contents)?;219 self.evaluate_loaded_file_raw(&file_path)220 }221 pub(crate) fn import_file_str(&self, from: &Path, path: &Path) -> Result<IStr> {222 let path = self.resolve_file(from, path)?;223 if !self.data().str_files.contains_key(&path) {224 let file_str = self.load_file_contents(&path)?;225 self.data_mut().str_files.insert(path.clone(), file_str);226 }227 Ok(self.data().str_files.get(&path).cloned().unwrap())228 }229230 fn evaluate_loaded_file_raw(&self, name: &Path) -> Result<Val> {231 let expr: LocExpr = {232 let ro_map = &self.data().files;233 let value = ro_map234 .get(name)235 .unwrap_or_else(|| panic!("file not added: {:?}", name));236 if let Some(ref evaluated) = value.evaluated {237 return Ok(evaluated.clone());238 }239 value.parsed.clone()240 };241 let value = evaluate(self.create_default_context(), &expr)?;242 {243 self.data_mut()244 .files245 .get_mut(name)246 .unwrap()247 .evaluated248 .replace(value.clone());249 }250 Ok(value)251 }252253 /// Adds standard library global variable (std) to this evaluator254 pub fn with_stdlib(&self) -> &Self {255 use jrsonnet_stdlib::STDLIB_STR;256 let std_path: Rc<Path> = PathBuf::from("std.jsonnet").into();257 self.run_in_state(|| {258 self.add_parsed_file(259 std_path.clone(),260 STDLIB_STR.to_owned().into(),261 builtin::get_parsed_stdlib(),262 )263 .unwrap();264 let val = self.evaluate_loaded_file_raw(&std_path).unwrap();265 self.settings_mut().globals.insert("std".into(), val);266 });267 self268 }269270 /// Creates context with all passed global variables271 pub fn create_default_context(&self) -> Context {272 let globals = &self.settings().globals;273 let mut new_bindings: FxHashMap<IStr, LazyVal> =274 FxHashMap::with_capacity_and_hasher(globals.len(), BuildHasherDefault::default());275 for (name, value) in globals.iter() {276 new_bindings.insert(name.clone(), LazyVal::new_resolved(value.clone()));277 }278 Context::new().extend_bound(new_bindings)279 }280281 /// Executes code creating a new stack frame282 pub fn push<T>(283 &self,284 e: Option<&ExprLocation>,285 frame_desc: impl FnOnce() -> String,286 f: impl FnOnce() -> Result<T>,287 ) -> Result<T> {288 {289 let mut data = self.data_mut();290 let stack_depth = &mut data.stack_depth;291 if *stack_depth > self.max_stack() {292 // Error creation uses data, so i drop guard here293 drop(data);294 throw!(StackOverflow);295 } else {296 *stack_depth += 1;297 }298 }299 let result = f();300 self.data_mut().stack_depth -= 1;301 if let Err(mut err) = result {302 err.trace_mut().0.push(StackTraceElement {303 location: e.cloned(),304 desc: frame_desc(),305 });306 return Err(err);307 }308 result309 }310311 /// Runs passed function in state (required if function needs to modify stack trace)312 pub fn run_in_state<T>(&self, f: impl FnOnce() -> T) -> T {313 EVAL_STATE.with(|v| {314 let has_state = v.borrow().is_some();315 if !has_state {316 v.borrow_mut().replace(self.clone());317 }318 let result = f();319 if !has_state {320 v.borrow_mut().take();321 }322 result323 })324 }325326 pub fn stringify_err(&self, e: &LocError) -> String {327 let mut out = String::new();328 self.settings()329 .trace_format330 .write_trace(&mut out, self, e)331 .unwrap();332 out333 }334335 pub fn manifest(&self, val: Val) -> Result<IStr> {336 self.run_in_state(|| val.manifest(&self.manifest_format()))337 }338 pub fn manifest_multi(&self, val: Val) -> Result<Vec<(IStr, IStr)>> {339 self.run_in_state(|| val.manifest_multi(&self.manifest_format()))340 }341 pub fn manifest_stream(&self, val: Val) -> Result<Vec<IStr>> {342 self.run_in_state(|| val.manifest_stream(&self.manifest_format()))343 }344345 /// If passed value is function then call with set TLA346 pub fn with_tla(&self, val: Val) -> Result<Val> {347 self.run_in_state(|| {348 Ok(match val {349 Val::Func(func) => push(350 None,351 || "during TLA call".to_owned(),352 || {353 func.evaluate_map(354 self.create_default_context(),355 &self.settings().tla_vars,356 true,357 )358 },359 )?,360 v => v,361 })362 })363 }364}365366/// Internals367impl EvaluationState {368 fn data(&self) -> Ref<EvaluationData> {369 self.0.data.borrow()370 }371 fn data_mut(&self) -> RefMut<EvaluationData> {372 self.0.data.borrow_mut()373 }374 pub fn settings(&self) -> Ref<EvaluationSettings> {375 self.0.settings.borrow()376 }377 pub fn settings_mut(&self) -> RefMut<EvaluationSettings> {378 self.0.settings.borrow_mut()379 }380}381382/// Raw methods evaluate passed values but don't perform TLA execution383impl EvaluationState {384 pub fn evaluate_file_raw(&self, name: &Path) -> Result<Val> {385 self.run_in_state(|| self.import_file(&std::env::current_dir().expect("cwd"), name))386 }387 pub fn evaluate_file_raw_nocwd(&self, name: &Path) -> Result<Val> {388 self.run_in_state(|| self.import_file(&PathBuf::from("."), name))389 }390 /// Parses and evaluates the given snippet391 pub fn evaluate_snippet_raw(&self, source: Rc<Path>, code: IStr) -> Result<Val> {392 let parsed = parse(393 &code,394 &ParserSettings {395 file_name: source.clone(),396 loc_data: true,397 },398 )399 .map_err(|e| ImportSyntaxError {400 path: source.clone(),401 source_code: code.clone(),402 error: Box::new(e),403 })?;404 self.add_parsed_file(source, code, parsed.clone())?;405 self.evaluate_expr_raw(parsed)406 }407 /// Evaluates the parsed expression408 pub fn evaluate_expr_raw(&self, code: LocExpr) -> Result<Val> {409 self.run_in_state(|| evaluate(self.create_default_context(), &code))410 }411}412413/// Settings utilities414impl EvaluationState {415 pub fn add_ext_var(&self, name: IStr, value: Val) {416 self.settings_mut().ext_vars.insert(name, value);417 }418 pub fn add_ext_str(&self, name: IStr, value: IStr) {419 self.add_ext_var(name, Val::Str(value));420 }421 pub fn add_ext_code(&self, name: IStr, code: IStr) -> Result<()> {422 let value =423 self.evaluate_snippet_raw(PathBuf::from(format!("ext_code {}", name)).into(), code)?;424 self.add_ext_var(name, value);425 Ok(())426 }427428 pub fn add_tla(&self, name: IStr, value: Val) {429 self.settings_mut().tla_vars.insert(name, value);430 }431 pub fn add_tla_str(&self, name: IStr, value: IStr) {432 self.add_tla(name, Val::Str(value));433 }434 pub fn add_tla_code(&self, name: IStr, code: IStr) -> Result<()> {435 let value =436 self.evaluate_snippet_raw(PathBuf::from(format!("tla_code {}", name)).into(), code)?;437 self.add_tla(name, value);438 Ok(())439 }440441 pub fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {442 self.settings().import_resolver.resolve_file(from, path)443 }444 pub fn load_file_contents(&self, path: &Path) -> Result<IStr> {445 self.settings().import_resolver.load_file_contents(path)446 }447448 pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {449 Ref::map(self.settings(), |s| &*s.import_resolver)450 }451 pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {452 self.settings_mut().import_resolver = resolver;453 }454455 pub fn add_native(&self, name: IStr, cb: Gc<NativeCallback>) {456 self.settings_mut().ext_natives.insert(name, cb);457 }458459 pub fn manifest_format(&self) -> ManifestFormat {460 self.settings().manifest_format.clone()461 }462 pub fn set_manifest_format(&self, format: ManifestFormat) {463 self.settings_mut().manifest_format = format;464 }465466 pub fn trace_format(&self) -> Ref<dyn TraceFormat> {467 Ref::map(self.settings(), |s| &*s.trace_format)468 }469 pub fn set_trace_format(&self, format: Box<dyn TraceFormat>) {470 self.settings_mut().trace_format = format;471 }472473 pub fn max_trace(&self) -> usize {474 self.settings().max_trace475 }476 pub fn set_max_trace(&self, trace: usize) {477 self.settings_mut().max_trace = trace;478 }479480 pub fn max_stack(&self) -> usize {481 self.settings().max_stack482 }483 pub fn set_max_stack(&self, trace: usize) {484 self.settings_mut().max_stack = trace;485 }486}487488#[cfg(test)]489pub mod tests {490 use super::Val;491 use crate::{492 error::Error::*, native::NativeCallbackHandler, primitive_equals, EvaluationState,493 };494 use jrsonnet_gc::{Finalize, Gc, Trace};495 use jrsonnet_interner::IStr;496 use jrsonnet_parser::*;497 use std::{498 path::{Path, PathBuf},499 rc::Rc,500 };501502 #[test]503 #[should_panic]504 fn eval_state_stacktrace() {505 let state = EvaluationState::default();506 state.run_in_state(|| {507 state508 .push(509 Some(&ExprLocation(PathBuf::from("test1.jsonnet").into(), 10, 20)),510 || "outer".to_owned(),511 || {512 state.push(513 Some(&ExprLocation(PathBuf::from("test2.jsonnet").into(), 30, 40)),514 || "inner".to_owned(),515 || Err(RuntimeError("".into()).into()),516 )?;517 Ok(())518 },519 )520 .unwrap();521 });522 }523524 #[test]525 fn eval_state_standard() {526 let state = EvaluationState::default();527 state.with_stdlib();528 assert!(primitive_equals(529 &state530 .evaluate_snippet_raw(531 PathBuf::from("raw.jsonnet").into(),532 r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#.into()533 )534 .unwrap(),535 &Val::Bool(true),536 )537 .unwrap());538 }539540 macro_rules! eval {541 ($str: expr) => {542 EvaluationState::default()543 .with_stdlib()544 .evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())545 .unwrap()546 };547 }548 macro_rules! eval_json {549 ($str: expr) => {{550 let evaluator = EvaluationState::default();551 evaluator.with_stdlib();552 evaluator.run_in_state(|| {553 evaluator554 .evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())555 .unwrap()556 .to_json(0)557 .unwrap()558 .replace("\n", "")559 })560 }};561 }562563 /// Asserts given code returns `true`564 macro_rules! assert_eval {565 ($str: expr) => {566 assert!(primitive_equals(&eval!($str), &Val::Bool(true)).unwrap())567 };568 }569570 /// Asserts given code returns `false`571 macro_rules! assert_eval_neg {572 ($str: expr) => {573 assert!(primitive_equals(&eval!($str), &Val::Bool(false)).unwrap())574 };575 }576 macro_rules! assert_json {577 ($str: expr, $out: expr) => {578 assert_eq!(eval_json!($str), $out.replace("\t", ""))579 };580 }581582 /// Sanity checking, before trusting to another tests583 #[test]584 fn equality_operator() {585 assert_eval!("2 == 2");586 assert_eval_neg!("2 != 2");587 assert_eval!("2 != 3");588 assert_eval_neg!("2 == 3");589 assert_eval!("'Hello' == 'Hello'");590 assert_eval_neg!("'Hello' != 'Hello'");591 assert_eval!("'Hello' != 'World'");592 assert_eval_neg!("'Hello' == 'World'");593 }594595 #[test]596 fn math_evaluation() {597 assert_eval!("2 + 2 * 2 == 6");598 assert_eval!("3 + (2 + 2 * 2) == 9");599 }600601 #[test]602 fn string_concat() {603 assert_eval!("'Hello' + 'World' == 'HelloWorld'");604 assert_eval!("'Hello' * 3 == 'HelloHelloHello'");605 assert_eval!("'Hello' + 'World' * 3 == 'HelloWorldWorldWorld'");606 }607608 #[test]609 fn faster_join() {610 assert_eval!("std.join([0,0], [[1,2],[3,4],[5,6]]) == [1,2,0,0,3,4,0,0,5,6]");611 assert_eval!("std.join(',', ['1','2','3','4']) == '1,2,3,4'");612 }613614 #[test]615 fn function_contexts() {616 assert_eval!(617 r#"618 local k = {619 t(name = self.h): [self.h, name],620 h: 3,621 };622 local f = {623 t: k.t(),624 h: 4,625 };626 f.t[0] == f.t[1]627 "#628 );629 }630631 #[test]632 fn local() {633 assert_eval!("local a = 2; local b = 3; a + b == 5");634 assert_eval!("local a = 1, b = a + 1; a + b == 3");635 assert_eval!("local a = 1; local a = 2; a == 2");636 }637638 #[test]639 fn object_lazyness() {640 assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);641 }642643 #[test]644 fn object_inheritance() {645 assert_json!("{a: self.b} + {b:3}", r#"{"a": 3,"b": 3}"#);646 }647648 #[test]649 fn object_assertion_success() {650 eval!("{assert \"a\" in self} + {a:2}");651 }652653 #[test]654 fn object_assertion_error() {655 eval!("{assert \"a\" in self}");656 }657658 #[test]659 fn lazy_args() {660 eval!("local test(a) = 2; test(error '3')");661 }662663 #[test]664 #[should_panic]665 fn tailstrict_args() {666 eval!("local test(a) = 2; test(error '3') tailstrict");667 }668669 #[test]670 #[should_panic]671 fn no_binding_error() {672 eval!("a");673 }674675 #[test]676 fn test_object() {677 assert_json!("{a:2}", r#"{"a": 2}"#);678 assert_json!("{a:2+2}", r#"{"a": 4}"#);679 assert_json!("{a:2}+{b:2}", r#"{"a": 2,"b": 2}"#);680 assert_json!("{b:3}+{b:2}", r#"{"b": 2}"#);681 assert_json!("{b:3}+{b+:2}", r#"{"b": 5}"#);682 assert_json!("local test='a'; {[test]:2}", r#"{"a": 2}"#);683 assert_json!(684 r#"685 {686 name: "Alice",687 welcome: "Hello " + self.name + "!",688 }689 "#,690 r#"{"name": "Alice","welcome": "Hello Alice!"}"#691 );692 assert_json!(693 r#"694 {695 name: "Alice",696 welcome: "Hello " + self.name + "!",697 } + {698 name: "Bob"699 }700 "#,701 r#"{"name": "Bob","welcome": "Hello Bob!"}"#702 );703 }704705 #[test]706 fn functions() {707 assert_json!(r#"local a = function(b, c = 2) b + c; a(2)"#, "4");708 assert_json!(709 r#"local a = function(b, c = "Dear") b + c + d, d = "World"; a("Hello")"#,710 r#""HelloDearWorld""#711 );712 }713714 #[test]715 fn local_methods() {716 assert_json!(r#"local a(b, c = 2) = b + c; a(2)"#, "4");717 assert_json!(718 r#"local a(b, c = "Dear") = b + c + d, d = "World"; a("Hello")"#,719 r#""HelloDearWorld""#720 );721 }722723 #[test]724 fn object_locals() {725 assert_json!(r#"{local a = 3, b: a}"#, r#"{"b": 3}"#);726 assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b": 3}"#);727 assert_json!(728 r#"{local a = function (b) {[b]:4}, test: a("test")}"#,729 r#"{"test": {"test": 4}}"#730 );731 }732733 #[test]734 fn object_comp() {735 assert_json!(736 r#"{local t = "a", ["h"+i+"_"+z]: if "h"+(i-1)+"_"+z in self then t+1 else 0+t for i in [1,2,3] for z in [2,3,4] if z != i}"#,737 "{\"h1_2\": \"0a\",\"h1_3\": \"0a\",\"h1_4\": \"0a\",\"h2_3\": \"a1\",\"h2_4\": \"a1\",\"h3_2\": \"0a\",\"h3_4\": \"a1\"}"738 )739 }740741 #[test]742 fn direct_self() {743 println!(744 "{:#?}",745 eval!(746 r#"747 {748 local me = self,749 a: 3,750 b(): me.a,751 }752 "#753 )754 );755 }756757 #[test]758 fn indirect_self() {759 // `self` assigned to `me` was lost when being760 // referenced from field761 eval!(762 r#"{763 local me = self,764 a: 3,765 b: me.a,766 }.b"#767 );768 }769770 // We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly771 #[test]772 fn std_assert_ok() {773 eval!("std.assertEqual(4.5 << 2, 16)");774 }775776 #[test]777 #[should_panic]778 fn std_assert_failure() {779 eval!("std.assertEqual(4.5 << 2, 15)");780 }781782 #[test]783 fn string_is_string() {784 assert!(primitive_equals(785 &eval!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),786 &Val::Bool(false),787 )788 .unwrap());789 }790791 #[test]792 fn base64_works() {793 assert_json!(r#"std.base64("test")"#, r#""dGVzdA==""#);794 }795796 #[test]797 fn utf8_chars() {798 assert_json!(799 r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,800 r#"{"c": 128526,"l": 1}"#801 )802 }803804 #[test]805 fn json() {806 assert_json!(807 r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,808 r#""{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}""#809 );810 }811812 #[test]813 fn parse_json() {814 assert_json!(815 r#"std.parseJson('{"a": -1,"b": 1,"c": 3.141,"d": []}')"#,816 r#"{"a": -1,"b": 1,"c": 3.141,"d": []}"#817 );818 // TODO: this should in fact fail as is no proper JSON syntax819 assert_json!(820 r#"std.parseJson("{a:-1, b:1, c:3.141, d:[]}")"#,821 r#"{"a": -1,"b": 1,"c": 3.141,"d": []}"#822 );823 // TODO: this is also no valid JSON824 assert_json!(r#"std.parseJson('local x = 2; x * x')"#, r#"4"#);825 }826827 #[test]828 fn test() {829 assert_json!(830 r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,831 "[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"832 );833 }834835 #[test]836 fn sjsonnet() {837 eval!(838 r#"839 local x0 = {k: 1};840 local x1 = {k: x0.k + x0.k};841 local x2 = {k: x1.k + x1.k};842 local x3 = {k: x2.k + x2.k};843 local x4 = {k: x3.k + x3.k};844 local x5 = {k: x4.k + x4.k};845 local x6 = {k: x5.k + x5.k};846 local x7 = {k: x6.k + x6.k};847 local x8 = {k: x7.k + x7.k};848 local x9 = {k: x8.k + x8.k};849 local x10 = {k: x9.k + x9.k};850 local x11 = {k: x10.k + x10.k};851 local x12 = {k: x11.k + x11.k};852 local x13 = {k: x12.k + x12.k};853 local x14 = {k: x13.k + x13.k};854 local x15 = {k: x14.k + x14.k};855 local x16 = {k: x15.k + x15.k};856 local x17 = {k: x16.k + x16.k};857 local x18 = {k: x17.k + x17.k};858 local x19 = {k: x18.k + x18.k};859 local x20 = {k: x19.k + x19.k};860 local x21 = {k: x20.k + x20.k};861 x21.k862 "#863 );864 }865866 // This test is commented out by default, because of huge compilation slowdown867 // #[bench]868 // fn bench_codegen(b: &mut Bencher) {869 // b.iter(|| {870 // #[allow(clippy::all)]871 // let stdlib = {872 // use jrsonnet_parser::*;873 // include!(concat!(env!("OUT_DIR"), "/stdlib.rs"))874 // };875 // stdlib876 // })877 // }878879 /*880 #[bench]881 fn bench_serialize(b: &mut Bencher) {882 b.iter(|| {883 bincode::deserialize::<jrsonnet_parser::LocExpr>(include_bytes!(concat!(884 env!("OUT_DIR"),885 "/stdlib.bincode"886 )))887 .expect("deserialize stdlib")888 })889 }890891 #[bench]892 fn bench_parse(b: &mut Bencher) {893 b.iter(|| {894 jrsonnet_parser::parse(895 jrsonnet_stdlib::STDLIB_STR,896 &jrsonnet_parser::ParserSettings {897 loc_data: true,898 file_name: Rc::new(PathBuf::from("std.jsonnet")),899 },900 )901 })902 }903 */904905 #[test]906 fn equality() {907 println!(908 "{:?}",909 jrsonnet_parser::parse(910 "{ x: 1, y: 2 } == { x: 1, y: 2 }",911 &ParserSettings {912 file_name: PathBuf::from("equality").into(),913 loc_data: true,914 }915 )916 );917 assert_eval!("{ x: 1, y: 2 } == { x: 1, y: 2 }")918 }919920 #[test]921 fn native_ext() -> crate::error::Result<()> {922 use super::native::NativeCallback;923 let evaluator = EvaluationState::default();924925 evaluator.with_stdlib();926927 #[derive(Trace, Finalize)]928 struct NativeAdd;929 impl NativeCallbackHandler for NativeAdd {930 fn call(&self, from: Option<Rc<Path>>, args: &[Val]) -> crate::error::Result<Val> {931 assert_eq!(932 &from.unwrap() as &Path,933 &PathBuf::from("native_caller.jsonnet")934 );935 match (&args[0], &args[1]) {936 (Val::Num(a), Val::Num(b)) => Ok(Val::Num(a + b)),937 (_, _) => unreachable!(),938 }939 }940 }941 evaluator.settings_mut().ext_natives.insert(942 "native_add".into(),943 Gc::new(NativeCallback::new(944 ParamsDesc(Rc::new(vec![945 Param("a".into(), None),946 Param("b".into(), None),947 ])),948 Box::new(NativeAdd),949 )),950 );951 evaluator.evaluate_snippet_raw(952 PathBuf::from("native_caller.jsonnet").into(),953 "std.assertEqual(std.native(\"native_add\")(1, 2), 3)".into(),954 )?;955 Ok(())956 }957958 #[test]959 fn constant_intrinsic() -> crate::error::Result<()> {960 assert_eval!(961 "local std2 = std; local std = std2 { primitiveEquals(a, b):: false }; 1 == 1"962 );963 Ok(())964 }965966 #[test]967 fn standalone_super() -> crate::error::Result<()> {968 assert_eval!(969 r#"970 local obj = {971 a: 1,972 b: 2,973 c: 3,974 };975 local test = obj + {976 fields: std.objectFields(super),977 d: 5,978 };979 test.fields == ['a', 'b', 'c']980 "#981 );982 Ok(())983 }984985 #[test]986 fn comp_self() -> crate::error::Result<()> {987 assert_eval!(988 r#"989 std.objectFields({990 a:{991 [name]: name for name in std.objectFields(self)992 },993 b: 2,994 c: 3,995 }.a) == ['a', 'b', 'c']996 "#997 );998999 Ok(())1000 }10011002 struct TestImportResolver(IStr);1003 impl crate::import::ImportResolver for TestImportResolver {1004 fn resolve_file(&self, _: &Path, _: &Path) -> crate::error::Result<Rc<Path>> {1005 Ok(PathBuf::from("/test").into())1006 }10071008 fn load_file_contents(&self, _: &Path) -> crate::error::Result<IStr> {1009 Ok(self.0.clone())1010 }10111012 unsafe fn as_any(&self) -> &dyn std::any::Any {1013 panic!()1014 }1015 }10161017 #[test]1018 fn issue_23() {1019 let state = EvaluationState::default();1020 state.set_import_resolver(Box::new(TestImportResolver(r#"import "/test""#.into())));1021 let _ = state.evaluate_file_raw(&PathBuf::from("/test"));1022 }10231024 #[test]1025 fn issue_40() {1026 let state = EvaluationState::default();1027 state.with_stdlib();10281029 let error = state1030 .evaluate_snippet_raw(1031 PathBuf::from("issue40.jsonnet").into(),1032 r#"1033 local conf = {1034 n: ""1035 };10361037 local result = conf + {1038 assert std.isNumber(self.n): "is number"1039 };10401041 std.manifestJsonEx(result, "")1042 "#1043 .into(),1044 )1045 .unwrap_err();1046 assert_eq!(error.error().to_string(), "assert failed: is number");1047 }1048}crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -1,4 +1,5 @@
-use crate::{evaluate_add_op, LazyBinding, Result, Val};
+use crate::operator::evaluate_add_op;
+use crate::{LazyBinding, Result, Val};
use jrsonnet_gc::{Gc, GcCell, Trace};
use jrsonnet_interner::IStr;
use jrsonnet_parser::{ExprLocation, Visibility};