difftreelog
refactor(evaluator) extract shared function call parsing code
in: master
8 files changed
cmds/jrsonnet/src/main.rsdiffbeforeafterboth--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -90,7 +90,7 @@
let opts: Opts = Opts::parse();
let evaluator = jsonnet_evaluator::EvaluationState::default();
if !opts.no_stdlib {
- evaluator.add_stdlib();
+ evaluator.with_stdlib();
}
let mut input = current_dir().unwrap();
input.push(opts.input.clone());
@@ -105,7 +105,7 @@
let v = match opts.format {
Format::Json => {
if opts.no_stdlib {
- evaluator.add_stdlib();
+ evaluator.with_stdlib();
}
evaluator.add_global("__tmp__to_json__".to_owned(), v);
let v = evaluator.parse_evaluate_raw(&format!(
@@ -122,7 +122,7 @@
}
Format::Yaml => {
if opts.no_stdlib {
- evaluator.add_stdlib();
+ evaluator.with_stdlib();
}
evaluator.add_global("__tmp__to_yaml__".to_owned(), v);
let v = evaluator
crates/jsonnet-evaluator/src/ctx.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/ctx.rs
+++ b/crates/jsonnet-evaluator/src/ctx.rs
@@ -1,5 +1,6 @@
use crate::{
- future_wrapper, rc_fn_helper, resolved_lazy_val, LazyBinding, LazyVal, ObjValue, Result, Val,
+ future_wrapper, map::LayeredHashMap, rc_fn_helper, resolved_lazy_val, LazyBinding, LazyVal,
+ ObjValue, Result, Val,
};
use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};
@@ -11,12 +12,11 @@
future_wrapper!(Context, FutureContext);
-#[derive(Debug)]
struct ContextInternals {
dollar: Option<ObjValue>,
this: Option<ObjValue>,
super_obj: Option<ObjValue>,
- bindings: Rc<HashMap<String, LazyVal>>,
+ bindings: LayeredHashMap<String, LazyVal>,
}
pub struct Context(Rc<ContextInternals>);
impl Debug for Context {
@@ -48,7 +48,7 @@
dollar: None,
this: None,
super_obj: None,
- bindings: Rc::new(HashMap::new()),
+ bindings: LayeredHashMap::default(),
}))
}
@@ -65,14 +65,14 @@
}
pub fn with_var(&self, name: String, value: Val) -> Result<Context> {
- let mut new_bindings: HashMap<_, LazyBinding> = HashMap::new();
- new_bindings.insert(name, LazyBinding::Bound(resolved_lazy_val!(value.clone())));
+ let mut new_bindings = HashMap::new();
+ new_bindings.insert(name, resolved_lazy_val!(value));
self.extend(new_bindings, None, None, None)
}
pub fn extend(
&self,
- new_bindings: HashMap<String, LazyBinding>,
+ new_bindings: HashMap<String, LazyVal>,
new_dollar: Option<ObjValue>,
new_this: Option<ObjValue>,
new_super_obj: Option<ObjValue>,
@@ -83,14 +83,7 @@
let bindings = if new_bindings.is_empty() {
self.0.bindings.clone()
} else {
- let mut new = HashMap::new(); // = self.0.bindings.clone();
- for (k, v) in self.0.bindings.iter() {
- new.insert(k.clone(), v.clone());
- }
- for (k, v) in new_bindings.into_iter() {
- new.insert(k, v.evaluate(this.clone(), super_obj.clone())?);
- }
- Rc::new(new)
+ self.0.bindings.extend(new_bindings)
};
Ok(Context(Rc::new(ContextInternals {
dollar,
@@ -99,6 +92,21 @@
bindings,
})))
}
+ pub fn extend_unbound(
+ &self,
+ new_bindings: HashMap<String, LazyBinding>,
+ new_dollar: Option<ObjValue>,
+ new_this: Option<ObjValue>,
+ new_super_obj: Option<ObjValue>,
+ ) -> Result<Context> {
+ let this = new_this.or_else(|| self.0.this.clone());
+ let super_obj = new_super_obj.or_else(|| self.0.super_obj.clone());
+ let mut new = HashMap::new();
+ for (k, v) in new_bindings.into_iter() {
+ new.insert(k, v.evaluate(this.clone(), super_obj.clone())?);
+ }
+ self.extend(new, new_dollar, this, super_obj)
+ }
}
impl Default for Context {
crates/jsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/error.rs
+++ b/crates/jsonnet-evaluator/src/error.rs
@@ -7,6 +7,11 @@
TypeMismatch(&'static str, Vec<ValType>, ValType),
NoSuchField(String),
+ UnknownFunctionParameter(String),
+ BindingParameterASecondTime(String),
+ TooManyArgsFunctionHas(usize),
+ FunctionParameterNotBoundInCall(String),
+
RuntimeError(String),
StackOverflow,
FractionalIndex,
crates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth1use crate::{2 binding, context_creator, create_error, function_default, function_rhs, future_wrapper,3 lazy_val, push, Context, ContextCreator, FuncDesc, LazyBinding, LazyVal, ObjMember, ObjValue,4 Result, Val,5};6use closure::closure;7use jsonnet_parser::{8 ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, FieldMember, ForSpecData,9 IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc, UnaryOpType, Visibility,10};11use std::{12 collections::{BTreeMap, HashMap},13 rc::Rc,14};1516pub fn evaluate_binding(b: &BindSpec, context_creator: ContextCreator) -> (String, LazyBinding) {17 let b = b.clone();18 if let Some(args) = &b.params {19 let args = args.clone();20 (21 b.name.clone(),22 LazyBinding::Bindable(Rc::new(move |this, super_obj| {23 Ok(lazy_val!(24 closure!(clone b, clone args, clone context_creator, || Ok(evaluate_method(25 context_creator.0(this.clone(), super_obj.clone())?,26 &b.value,27 args.clone()28 )))29 ))30 })),31 )32 } else {33 (34 b.name.clone(),35 LazyBinding::Bindable(Rc::new(move |this, super_obj| {36 Ok(lazy_val!(closure!(clone context_creator, clone b, ||37 push(b.value.clone(), "thunk".to_owned(), ||{38 evaluate(39 context_creator.0(this.clone(), super_obj.clone())?,40 &b.value41 )42 })43 )))44 })),45 )46 }47}4849pub fn evaluate_method(ctx: Context, expr: &LocExpr, arg_spec: ParamsDesc) -> Val {50 Val::Func(FuncDesc {51 ctx,52 params: arg_spec,53 eval_rhs: function_rhs!(closure!(clone expr, |ctx| evaluate(ctx, &expr))),54 eval_default: function_default!(closure!(|ctx, default| evaluate(ctx, &default))),55 })56}5758pub fn evaluate_field_name(59 context: Context,60 field_name: &jsonnet_parser::FieldName,61) -> Result<Option<String>> {62 Ok(match field_name {63 jsonnet_parser::FieldName::Fixed(n) => Some(n.clone()),64 jsonnet_parser::FieldName::Dyn(expr) => {65 let value = evaluate(context, expr)?.unwrap_if_lazy()?;66 if matches!(value, Val::Null) {67 None68 } else {69 Some(value.try_cast_str("dynamic field name")?)70 }71 }72 })73}7475pub fn evaluate_unary_op(op: UnaryOpType, b: &Val) -> Result<Val> {76 Ok(match (op, b) {77 (o, Val::Lazy(l)) => evaluate_unary_op(o, &l.evaluate()?)?,78 (UnaryOpType::Not, Val::Bool(v)) => Val::Bool(!v),79 (UnaryOpType::Minus, Val::Num(n)) => Val::Num(-*n),80 (UnaryOpType::BitNot, Val::Num(n)) => Val::Num(!(*n as i32) as f64),81 (op, o) => panic!("unary op not implemented: {:?} {:?}", op, o),82 })83}8485pub(crate) fn evaluate_add_op(a: &Val, b: &Val) -> Result<Val> {86 Ok(match (a, b) {87 (Val::Str(v1), Val::Str(v2)) => Val::Str(v1.to_owned() + &v2),8889 (Val::Str(v1), Val::Num(v2)) => Val::Str(format!("{}{}", v1, v2)),90 (Val::Num(v1), Val::Str(v2)) => Val::Str(format!("{}{}", v1, v2)),91 (Val::Str(v1), Val::Bool(v2)) => Val::Str(format!("{}{}", v1, v2)),92 (Val::Bool(v1), Val::Str(v2)) => Val::Str(format!("{}{}", v1, v2)),93 (Val::Str(v1), Val::Null) => Val::Str(format!("{}null", v1)),94 (Val::Null, Val::Str(v2)) => Val::Str(format!("null{}", v2)),9596 (Val::Obj(v1), Val::Obj(v2)) => Val::Obj(v2.with_super(v1.clone())),97 (Val::Arr(a), Val::Arr(b)) => Val::Arr([&a[..], &b[..]].concat()),98 (Val::Num(v1), Val::Num(v2)) => Val::Num(v1 + v2),99 _ => panic!("can't add: {:?} and {:?}", a, b),100 })101}102103pub fn evaluate_binary_op_special(104 context: Context,105 a: &LocExpr,106 op: BinaryOpType,107 b: &LocExpr,108) -> Result<Val> {109 Ok(110 match (evaluate(context.clone(), &a)?.unwrap_if_lazy()?, op, b) {111 (Val::Bool(true), BinaryOpType::Or, _o) => Val::Bool(true),112 (Val::Bool(false), BinaryOpType::And, _o) => Val::Bool(false),113 (a, op, eb) => {114 evaluate_binary_op_normal(&a, op, &evaluate(context, eb)?.unwrap_if_lazy()?)?115 }116 },117 )118}119120pub fn evaluate_binary_op_normal(a: &Val, op: BinaryOpType, b: &Val) -> Result<Val> {121 Ok(match (a, op, b) {122 (a, BinaryOpType::Add, b) => evaluate_add_op(a, b)?,123124 (Val::Str(v1), BinaryOpType::Mul, Val::Num(v2)) => Val::Str(v1.repeat(*v2 as usize)),125126 // Bool X Bool127 (Val::Bool(a), BinaryOpType::And, Val::Bool(b)) => Val::Bool(*a && *b),128 (Val::Bool(a), BinaryOpType::Or, Val::Bool(b)) => Val::Bool(*a || *b),129130 // Str X Str131 (Val::Str(v1), BinaryOpType::Lt, Val::Str(v2)) => Val::Bool(v1 < v2),132 (Val::Str(v1), BinaryOpType::Gt, Val::Str(v2)) => Val::Bool(v1 > v2),133 (Val::Str(v1), BinaryOpType::Lte, Val::Str(v2)) => Val::Bool(v1 <= v2),134 (Val::Str(v1), BinaryOpType::Gte, Val::Str(v2)) => Val::Bool(v1 >= v2),135136 // Num X Num137 (Val::Num(v1), BinaryOpType::Mul, Val::Num(v2)) => Val::Num(v1 * v2),138 (Val::Num(v1), BinaryOpType::Div, Val::Num(v2)) => {139 if *v2 <= f64::EPSILON {140 create_error(crate::Error::DivisionByZero)?141 }142 Val::Num(v1 / v2)143 }144145 (Val::Num(v1), BinaryOpType::Sub, Val::Num(v2)) => Val::Num(v1 - v2),146147 (Val::Num(v1), BinaryOpType::Lt, Val::Num(v2)) => Val::Bool(v1 < v2),148 (Val::Num(v1), BinaryOpType::Gt, Val::Num(v2)) => Val::Bool(v1 > v2),149 (Val::Num(v1), BinaryOpType::Lte, Val::Num(v2)) => Val::Bool(v1 <= v2),150 (Val::Num(v1), BinaryOpType::Gte, Val::Num(v2)) => Val::Bool(v1 >= v2),151152 (Val::Num(v1), BinaryOpType::BitAnd, Val::Num(v2)) => {153 Val::Num(((*v1 as i32) & (*v2 as i32)) as f64)154 }155 (Val::Num(v1), BinaryOpType::BitOr, Val::Num(v2)) => {156 Val::Num(((*v1 as i32) | (*v2 as i32)) as f64)157 }158 (Val::Num(v1), BinaryOpType::BitXor, Val::Num(v2)) => {159 Val::Num(((*v1 as i32) ^ (*v2 as i32)) as f64)160 }161 (Val::Num(v1), BinaryOpType::Lhs, Val::Num(v2)) => {162 Val::Num(((*v1 as i32) << (*v2 as i32)) as f64)163 }164 (Val::Num(v1), BinaryOpType::Rhs, Val::Num(v2)) => {165 Val::Num(((*v1 as i32) >> (*v2 as i32)) as f64)166 }167168 _ => panic!("no rules for binary operation: {:?} {:?} {:?}", a, op, b),169 })170}171172future_wrapper!(HashMap<String, LazyBinding>, FutureNewBindings);173future_wrapper!(ObjValue, FutureObjValue);174175pub fn evaluate_comp(176 context: Context,177 value: &LocExpr,178 specs: &[CompSpec],179) -> Result<Option<Vec<Val>>> {180 Ok(match specs.get(0) {181 None => Some(vec![evaluate(context, &value)?]),182 Some(CompSpec::IfSpec(IfSpecData(cond))) => {183 if evaluate(context.clone(), &cond)?.try_cast_bool("if spec")? {184 evaluate_comp(context, value, &specs[1..])?185 } else {186 None187 }188 }189 Some(CompSpec::ForSpec(ForSpecData(var, expr))) => {190 match evaluate(context.clone(), &expr)?.unwrap_if_lazy()? {191 Val::Arr(list) => {192 let mut out = Vec::new();193 for item in list {194 let item = item.clone().unwrap_if_lazy()?;195 out.push(evaluate_comp(196 context.with_var(var.clone(), item)?,197 value,198 &specs[1..],199 )?);200 }201 Some(out.iter().flatten().flatten().cloned().collect())202 }203 _ => panic!("for expression evaluated to non-iterable value"),204 }205 }206 })207}208209// TODO: Asserts210pub fn evaluate_object(context: Context, object: ObjBody) -> Result<ObjValue> {211 Ok(match object {212 ObjBody::MemberList(members) => {213 let new_bindings = FutureNewBindings::new();214 let future_this = FutureObjValue::new();215 let context_creator = context_creator!(216 closure!(clone context, clone new_bindings, clone future_this, |this: Option<ObjValue>, super_obj: Option<ObjValue>| {217 Ok(context.clone().extend(218 new_bindings.clone().unwrap(),219 context.clone().dollar().clone().or_else(||this.clone()),220 Some(this.unwrap()),221 super_obj222 )?)223 })224 );225 {226 let mut bindings: HashMap<String, LazyBinding> = HashMap::new();227 for (n, b) in members228 .iter()229 .filter_map(|m| match m {230 Member::BindStmt(b) => Some(b.clone()),231 _ => None,232 })233 .map(|b| evaluate_binding(&b, context_creator.clone()))234 {235 bindings.insert(n, b);236 }237 new_bindings.fill(bindings);238 }239240 let mut new_members = BTreeMap::new();241 for member in members.into_iter() {242 match member {243 Member::Field(FieldMember {244 name,245 plus,246 params: None,247 visibility,248 value,249 }) => {250 let name = evaluate_field_name(context.clone(), &name)?;251 if name.is_none() {252 continue;253 }254 let name = name.unwrap();255 new_members.insert(256 name.clone(),257 ObjMember {258 add: plus,259 visibility: visibility.clone(),260 invoke: binding!(261 closure!(clone name, clone value, clone context_creator, |this, super_obj| {262 push(value.clone(), "object ".to_owned()+&name+" field", ||{263 let context = context_creator.0(this, super_obj)?;264 evaluate(265 context,266 &value,267 )?.unwrap_if_lazy()268 })269 })270 ),271 },272 );273 }274 Member::Field(FieldMember {275 name,276 params: Some(params),277 value,278 ..279 }) => {280 let name = evaluate_field_name(context.clone(), &name)?;281 if name.is_none() {282 continue;283 }284 let name = name.unwrap();285 new_members.insert(286 name,287 ObjMember {288 add: false,289 visibility: Visibility::Hidden,290 invoke: binding!(291 closure!(clone value, clone context_creator, |this, super_obj| {292 // TODO: Assert293 Ok(evaluate_method(294 context_creator.0(this, super_obj)?,295 &value.clone(),296 params.clone(),297 ))298 })299 ),300 },301 );302 }303 Member::BindStmt(_) => {}304 Member::AssertStmt(_) => {}305 }306 }307 future_this.fill(ObjValue::new(None, Rc::new(new_members)))308 }309 _ => todo!(),310 })311}312313#[inline(always)]314pub fn evaluate(context: Context, expr: &LocExpr) -> Result<Val> {315 use Expr::*;316 let locexpr = expr.clone();317 let LocExpr(expr, loc) = expr;318 Ok(match &**expr {319 Literal(LiteralType::This) => Val::Obj(320 context321 .this()322 .clone()323 .unwrap_or_else(|| panic!("this not found")),324 ),325 Literal(LiteralType::Dollar) => Val::Obj(326 context327 .dollar()328 .clone()329 .unwrap_or_else(|| panic!("dollar not found")),330 ),331 Literal(LiteralType::True) => Val::Bool(true),332 Literal(LiteralType::False) => Val::Bool(false),333 Literal(LiteralType::Null) => Val::Null,334 Parened(e) => evaluate(context, e)?,335 Str(v) => Val::Str(v.clone()),336 Num(v) => Val::Num(*v),337 BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, &v1, *o, &v2)?,338 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,339 Var(name) => push(locexpr, "var".to_owned(), || {340 Val::Lazy(context.binding(&name)).unwrap_if_lazy()341 })?,342 Index(LocExpr(v, _), index) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {343 let name = evaluate(context.clone(), index)?.try_cast_str("object index")?;344 context345 .super_obj()346 .clone()347 .expect("no super found")348 .get_raw(&name, &context.this().clone().expect("no this found"))?349 .expect("value not found")350 }351 Index(value, index) => {352 match (353 evaluate(context.clone(), value)?.unwrap_if_lazy()?,354 evaluate(context, index)?,355 ) {356 (Val::Obj(v), Val::Str(s)) => {357 if let Some(v) = v.get(&s)? {358 v.unwrap_if_lazy()?359 } else if let Some(Val::Str(n)) = v.get("__intristic_namespace__")? {360 Val::Intristic(n, s)361 } else {362 create_error(crate::Error::NoSuchField(s))?363 }364 }365 (Val::Arr(v), Val::Num(n)) => {366 if n.fract() > f64::EPSILON {367 create_error(crate::Error::FractionalIndex)?368 }369 v.get(n as usize)370 .unwrap_or_else(|| panic!("out of bounds"))371 .clone()372 .unwrap_if_lazy()?373 }374 (Val::Str(s), Val::Num(n)) => {375 Val::Str(s.chars().skip(n as usize).take(1).collect())376 }377 (v, i) => todo!("not implemented: {:?}[{:?}]", v, i.unwrap_if_lazy()),378 }379 }380 LocalExpr(bindings, returned) => {381 let mut new_bindings: HashMap<String, LazyBinding> = HashMap::new();382 let future_context = Context::new_future();383384 let context_creator = context_creator!(385 closure!(clone future_context, |_, _| Ok(future_context.clone().unwrap()))386 );387388 for (k, v) in bindings389 .iter()390 .map(|b| evaluate_binding(b, context_creator.clone()))391 {392 new_bindings.insert(k, v);393 }394395 let context = context396 .extend(new_bindings, None, None, None)?397 .into_future(future_context);398 evaluate(context, &returned.clone())?399 }400 Arr(items) => {401 let mut out = Vec::with_capacity(items.len());402 for item in items {403 out.push(Val::Lazy(lazy_val!(404 closure!(clone context, clone item, || {405 evaluate(context.clone(), &item)406 })407 )));408 }409 Val::Arr(out)410 }411 ArrComp(expr, compspecs) => Val::Arr(412 // First compspec should be forspec, so no "None" possible here413 evaluate_comp(context, expr, compspecs)?.unwrap(),414 ),415 Obj(body) => Val::Obj(evaluate_object(context, body.clone())?),416 ObjExtend(s, t) => evaluate_add_op(417 &evaluate(context.clone(), s)?,418 &Val::Obj(evaluate_object(context, t.clone())?),419 )?,420 Apply(value, ArgsDesc(args), tailstrict) => {421 let value = evaluate(context.clone(), value)?.unwrap_if_lazy()?;422 match value {423 Val::Intristic(ns, name) => match (&ns as &str, &name as &str) {424 // arr/string/function425 ("std", "length") => {426 assert_eq!(args.len(), 1);427 let expr = &args.get(0).unwrap().1;428 match evaluate(context, expr)? {429 Val::Str(n) => Val::Num(n.chars().count() as f64),430 Val::Arr(i) => Val::Num(i.len() as f64),431 Val::Obj(o) => Val::Num(o.fields().len() as f64),432 v => panic!("can't get length of {:?}", v),433 }434 }435 // any436 ("std", "type") => {437 assert_eq!(args.len(), 1);438 let expr = &args.get(0).unwrap().1;439 Val::Str(evaluate(context, expr)?.value_type()?.name().to_owned())440 }441 // length, idx=>any442 ("std", "makeArray") => {443 assert_eq!(args.len(), 2);444 if let (Val::Num(v), Val::Func(d)) = (445 evaluate(context.clone(), &args[0].1)?,446 evaluate(context, &args[1].1)?,447 ) {448 assert!(v >= 0.0);449 let mut out = Vec::with_capacity(v as usize);450 for i in 0..v as usize {451 out.push(d.evaluate(vec![(None, Val::Num(i as f64))])?)452 }453 Val::Arr(out)454 } else {455 panic!("bad makeArray call");456 }457 }458 // string459 ("std", "codepoint") => {460 assert_eq!(args.len(), 1);461 if let Val::Str(s) = evaluate(context, &args[0].1)? {462 assert!(463 s.chars().count() == 1,464 "std.codepoint should receive single char string"465 );466 Val::Num(s.chars().take(1).next().unwrap() as u32 as f64)467 } else {468 panic!("bad codepoint call");469 }470 }471 // object, includeHidden472 ("std", "objectFieldsEx") => {473 assert_eq!(args.len(), 2);474 if let (Val::Obj(body), Val::Bool(_include_hidden)) = (475 evaluate(context.clone(), &args[0].1)?,476 evaluate(context, &args[1].1)?,477 ) {478 // TODO: handle visibility (_include_hidden)479 Val::Arr(body.fields().into_iter().map(Val::Str).collect())480 } else {481 panic!("bad objectFieldsEx call");482 }483 }484 ("std", "primitiveEquals") => {485 assert_eq!(args.len(), 2);486 let (a, b) = (487 evaluate(context.clone(), &args[0].1)?,488 evaluate(context, &args[1].1)?,489 );490 Val::Bool(a == b)491 }492 ("std", "modulo") => {493 assert_eq!(args.len(), 2);494 if let (Val::Num(a), Val::Num(b)) = (495 evaluate(context.clone(), &args[0].1)?,496 evaluate(context, &args[1].1)?,497 ) {498 Val::Num(a % b)499 } else {500 panic!("bad modulo call");501 }502 }503 ("std", "floor") => {504 assert_eq!(args.len(), 1);505 if let Val::Num(a) = evaluate(context, &args[0].1)? {506 Val::Num(a.floor())507 } else {508 panic!("bad floor call");509 }510 }511 ("std", "trace") => {512 assert_eq!(args.len(), 2);513 if let (Val::Str(a), b) = (514 evaluate(context.clone(), &args[0].1)?,515 evaluate(context, &args[1].1)?,516 ) {517 // TODO: Line numbers as in original jsonnet518 println!("TRACE: {}", a);519 b520 } else {521 panic!("bad trace call");522 }523 }524 ("std", "pow") => {525 assert_eq!(args.len(), 2);526 if let (Val::Num(a), Val::Num(b)) = (527 evaluate(context.clone(), &args[0].1)?,528 evaluate(context, &args[1].1)?,529 ) {530 Val::Num(a.powf(b))531 } else {532 panic!("bad pow call");533 }534 }535 (ns, name) => panic!("Intristic not found: {}.{}", ns, name),536 },537 Val::Func(f) => {538 let body = #[inline(always)]539 || {540 f.evaluate(541 args.clone()542 .into_iter()543 .map(544 #[inline(always)]545 move |a| {546 Ok((547 a.clone().0,548 if *tailstrict {549 Val::Lazy(LazyVal::new_resolved(evaluate(550 context.clone(),551 &a.1,552 )?))553 } else {554 Val::Lazy(lazy_val!(555 closure!(clone context, clone a, || evaluate(context.clone(), &a.clone().1))556 ))557 },558 ))559 },560 )561 .collect::<Result<Vec<_>>>()?,562 )563 };564 if *tailstrict {565 body()?566 } else {567 push(locexpr, "function call".to_owned(), body)?568 }569 }570 _ => panic!("{:?} is not a function", value),571 }572 }573 Function(params, body) => evaluate_method(context, body, params.clone()),574 AssertExpr(AssertStmt(value, msg), returned) => {575 let assertion_result = push(value.clone(), "assertion condition".to_owned(), || {576 evaluate(context.clone(), &value)?577 .try_cast_bool("assertion condition should be boolean")578 })?;579 if assertion_result {580 push(581 returned.clone(),582 "assert 'return' branch".to_owned(),583 || evaluate(context, returned),584 )?585 } else if let Some(msg) = msg {586 panic!(587 "assertion failed ({:?}): {}",588 value,589 evaluate(context, msg)?.try_cast_str("assertion message should be string")?590 );591 } else {592 panic!("assertion failed ({:?}): no message", value);593 }594 }595 Error(e) => create_error(crate::Error::RuntimeError(596 evaluate(context, e)?.try_cast_str("error text should be string")?,597 ))?,598 IfElse {599 cond,600 cond_then,601 cond_else,602 } => {603 let condition_result = push(cond.0.clone(), "if condition".to_owned(), || {604 evaluate(context.clone(), &cond.0)?.try_cast_bool("if condition should be boolean")605 })?;606 if condition_result {607 push(608 cond_then.clone(),609 "if condition 'then' branch".to_owned(),610 || evaluate(context, cond_then),611 )?612 } else {613 match cond_else {614 Some(v) => evaluate(context, v)?,615 None => Val::Null,616 }617 }618 }619 _ => panic!(620 "evaluation not implemented: {:?}",621 LocExpr(expr.clone(), loc.clone())622 ),623 })624}1use crate::{2 binding, context_creator, create_error, future_wrapper, lazy_val, push, Context,3 ContextCreator, FuncDesc, LazyBinding, ObjMember, ObjValue, Result, Val,4};5use closure::closure;6use jsonnet_parser::{7 el, Arg, ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, FieldMember,8 ForSpecData, IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc, UnaryOpType,9 Visibility,10};11use std::{12 collections::{BTreeMap, HashMap},13 rc::Rc,14};1516pub fn evaluate_binding(b: &BindSpec, context_creator: ContextCreator) -> (String, LazyBinding) {17 let b = b.clone();18 if let Some(params) = &b.params {19 let params = params.clone();20 (21 b.name.clone(),22 LazyBinding::Bindable(Rc::new(move |this, super_obj| {23 Ok(lazy_val!(24 closure!(clone b, clone params, clone context_creator, || Ok(evaluate_method(25 context_creator.0(this.clone(), super_obj.clone())?,26 params.clone(),27 b.value.clone(),28 )))29 ))30 })),31 )32 } else {33 (34 b.name.clone(),35 LazyBinding::Bindable(Rc::new(move |this, super_obj| {36 Ok(lazy_val!(closure!(clone context_creator, clone b, ||37 push(b.value.clone(), "thunk".to_owned(), ||{38 evaluate(39 context_creator.0(this.clone(), super_obj.clone())?,40 &b.value41 )42 })43 )))44 })),45 )46 }47}4849pub fn evaluate_method(ctx: Context, params: ParamsDesc, body: LocExpr) -> Val {50 Val::Func(FuncDesc { ctx, params, body })51}5253pub fn evaluate_field_name(54 context: Context,55 field_name: &jsonnet_parser::FieldName,56) -> Result<Option<String>> {57 Ok(match field_name {58 jsonnet_parser::FieldName::Fixed(n) => Some(n.clone()),59 jsonnet_parser::FieldName::Dyn(expr) => {60 let value = evaluate(context, expr)?.unwrap_if_lazy()?;61 if matches!(value, Val::Null) {62 None63 } else {64 Some(value.try_cast_str("dynamic field name")?)65 }66 }67 })68}6970pub fn evaluate_unary_op(op: UnaryOpType, b: &Val) -> Result<Val> {71 Ok(match (op, b) {72 (o, Val::Lazy(l)) => evaluate_unary_op(o, &l.evaluate()?)?,73 (UnaryOpType::Not, Val::Bool(v)) => Val::Bool(!v),74 (UnaryOpType::Minus, Val::Num(n)) => Val::Num(-*n),75 (UnaryOpType::BitNot, Val::Num(n)) => Val::Num(!(*n as i32) as f64),76 (op, o) => panic!("unary op not implemented: {:?} {:?}", op, o),77 })78}7980pub(crate) fn evaluate_add_op(a: &Val, b: &Val) -> Result<Val> {81 Ok(match (a, b) {82 (Val::Str(v1), Val::Str(v2)) => Val::Str(v1.to_owned() + &v2),8384 (Val::Str(v1), Val::Num(v2)) => Val::Str(format!("{}{}", v1, v2)),85 (Val::Num(v1), Val::Str(v2)) => Val::Str(format!("{}{}", v1, v2)),86 (Val::Str(v1), Val::Bool(v2)) => Val::Str(format!("{}{}", v1, v2)),87 (Val::Bool(v1), Val::Str(v2)) => Val::Str(format!("{}{}", v1, v2)),88 (Val::Str(v1), Val::Null) => Val::Str(format!("{}null", v1)),89 (Val::Null, Val::Str(v2)) => Val::Str(format!("null{}", v2)),9091 (Val::Obj(v1), Val::Obj(v2)) => Val::Obj(v2.with_super(v1.clone())),92 (Val::Arr(a), Val::Arr(b)) => Val::Arr([&a[..], &b[..]].concat()),93 (Val::Num(v1), Val::Num(v2)) => Val::Num(v1 + v2),94 _ => panic!("can't add: {:?} and {:?}", a, b),95 })96}9798pub fn evaluate_binary_op_special(99 context: Context,100 a: &LocExpr,101 op: BinaryOpType,102 b: &LocExpr,103) -> Result<Val> {104 Ok(105 match (evaluate(context.clone(), &a)?.unwrap_if_lazy()?, op, b) {106 (Val::Bool(true), BinaryOpType::Or, _o) => Val::Bool(true),107 (Val::Bool(false), BinaryOpType::And, _o) => Val::Bool(false),108 (a, op, eb) => {109 evaluate_binary_op_normal(&a, op, &evaluate(context, eb)?.unwrap_if_lazy()?)?110 }111 },112 )113}114115pub fn evaluate_binary_op_normal(a: &Val, op: BinaryOpType, b: &Val) -> Result<Val> {116 Ok(match (a, op, b) {117 (a, BinaryOpType::Add, b) => evaluate_add_op(a, b)?,118119 (Val::Str(v1), BinaryOpType::Mul, Val::Num(v2)) => Val::Str(v1.repeat(*v2 as usize)),120121 // Bool X Bool122 (Val::Bool(a), BinaryOpType::And, Val::Bool(b)) => Val::Bool(*a && *b),123 (Val::Bool(a), BinaryOpType::Or, Val::Bool(b)) => Val::Bool(*a || *b),124125 // Str X Str126 (Val::Str(v1), BinaryOpType::Lt, Val::Str(v2)) => Val::Bool(v1 < v2),127 (Val::Str(v1), BinaryOpType::Gt, Val::Str(v2)) => Val::Bool(v1 > v2),128 (Val::Str(v1), BinaryOpType::Lte, Val::Str(v2)) => Val::Bool(v1 <= v2),129 (Val::Str(v1), BinaryOpType::Gte, Val::Str(v2)) => Val::Bool(v1 >= v2),130131 // Num X Num132 (Val::Num(v1), BinaryOpType::Mul, Val::Num(v2)) => Val::Num(v1 * v2),133 (Val::Num(v1), BinaryOpType::Div, Val::Num(v2)) => {134 if *v2 <= f64::EPSILON {135 create_error(crate::Error::DivisionByZero)?136 }137 Val::Num(v1 / v2)138 }139140 (Val::Num(v1), BinaryOpType::Sub, Val::Num(v2)) => Val::Num(v1 - v2),141142 (Val::Num(v1), BinaryOpType::Lt, Val::Num(v2)) => Val::Bool(v1 < v2),143 (Val::Num(v1), BinaryOpType::Gt, Val::Num(v2)) => Val::Bool(v1 > v2),144 (Val::Num(v1), BinaryOpType::Lte, Val::Num(v2)) => Val::Bool(v1 <= v2),145 (Val::Num(v1), BinaryOpType::Gte, Val::Num(v2)) => Val::Bool(v1 >= v2),146147 (Val::Num(v1), BinaryOpType::BitAnd, Val::Num(v2)) => {148 Val::Num(((*v1 as i32) & (*v2 as i32)) as f64)149 }150 (Val::Num(v1), BinaryOpType::BitOr, Val::Num(v2)) => {151 Val::Num(((*v1 as i32) | (*v2 as i32)) as f64)152 }153 (Val::Num(v1), BinaryOpType::BitXor, Val::Num(v2)) => {154 Val::Num(((*v1 as i32) ^ (*v2 as i32)) as f64)155 }156 (Val::Num(v1), BinaryOpType::Lhs, Val::Num(v2)) => {157 Val::Num(((*v1 as i32) << (*v2 as i32)) as f64)158 }159 (Val::Num(v1), BinaryOpType::Rhs, Val::Num(v2)) => {160 Val::Num(((*v1 as i32) >> (*v2 as i32)) as f64)161 }162163 _ => panic!("no rules for binary operation: {:?} {:?} {:?}", a, op, b),164 })165}166167future_wrapper!(HashMap<String, LazyBinding>, FutureNewBindings);168future_wrapper!(ObjValue, FutureObjValue);169170pub fn evaluate_comp(171 context: Context,172 value: &LocExpr,173 specs: &[CompSpec],174) -> Result<Option<Vec<Val>>> {175 Ok(match specs.get(0) {176 None => Some(vec![evaluate(context, &value)?]),177 Some(CompSpec::IfSpec(IfSpecData(cond))) => {178 if evaluate(context.clone(), &cond)?.try_cast_bool("if spec")? {179 evaluate_comp(context, value, &specs[1..])?180 } else {181 None182 }183 }184 Some(CompSpec::ForSpec(ForSpecData(var, expr))) => {185 match evaluate(context.clone(), &expr)?.unwrap_if_lazy()? {186 Val::Arr(list) => {187 let mut out = Vec::new();188 for item in list {189 let item = item.clone().unwrap_if_lazy()?;190 out.push(evaluate_comp(191 context.with_var(var.clone(), item)?,192 value,193 &specs[1..],194 )?);195 }196 Some(out.iter().flatten().flatten().cloned().collect())197 }198 _ => panic!("for expression evaluated to non-iterable value"),199 }200 }201 })202}203204// TODO: Asserts205pub fn evaluate_object(context: Context, object: ObjBody) -> Result<ObjValue> {206 Ok(match object {207 ObjBody::MemberList(members) => {208 let new_bindings = FutureNewBindings::new();209 let future_this = FutureObjValue::new();210 let context_creator = context_creator!(211 closure!(clone context, clone new_bindings, clone future_this, |this: Option<ObjValue>, super_obj: Option<ObjValue>| {212 Ok(context.clone().extend_unbound(213 new_bindings.clone().unwrap(),214 context.clone().dollar().clone().or_else(||this.clone()),215 Some(this.unwrap()),216 super_obj217 )?)218 })219 );220 {221 let mut bindings: HashMap<String, LazyBinding> = HashMap::new();222 for (n, b) in members223 .iter()224 .filter_map(|m| match m {225 Member::BindStmt(b) => Some(b.clone()),226 _ => None,227 })228 .map(|b| evaluate_binding(&b, context_creator.clone()))229 {230 bindings.insert(n, b);231 }232 new_bindings.fill(bindings);233 }234235 let mut new_members = BTreeMap::new();236 for member in members.into_iter() {237 match member {238 Member::Field(FieldMember {239 name,240 plus,241 params: None,242 visibility,243 value,244 }) => {245 let name = evaluate_field_name(context.clone(), &name)?;246 if name.is_none() {247 continue;248 }249 let name = name.unwrap();250 new_members.insert(251 name.clone(),252 ObjMember {253 add: plus,254 visibility: visibility.clone(),255 invoke: binding!(256 closure!(clone name, clone value, clone context_creator, |this, super_obj| {257 push(value.clone(), "object ".to_owned()+&name+" field", ||{258 let context = context_creator.0(this, super_obj)?;259 evaluate(260 context,261 &value,262 )?.unwrap_if_lazy()263 })264 })265 ),266 },267 );268 }269 Member::Field(FieldMember {270 name,271 params: Some(params),272 value,273 ..274 }) => {275 let name = evaluate_field_name(context.clone(), &name)?;276 if name.is_none() {277 continue;278 }279 let name = name.unwrap();280 new_members.insert(281 name,282 ObjMember {283 add: false,284 visibility: Visibility::Hidden,285 invoke: binding!(286 closure!(clone value, clone context_creator, |this, super_obj| {287 // TODO: Assert288 Ok(evaluate_method(289 context_creator.0(this, super_obj)?,290 params.clone(),291 value.clone(),292 ))293 })294 ),295 },296 );297 }298 Member::BindStmt(_) => {}299 Member::AssertStmt(_) => {}300 }301 }302 future_this.fill(ObjValue::new(None, Rc::new(new_members)))303 }304 _ => todo!(),305 })306}307308#[inline(always)]309pub fn evaluate(context: Context, expr: &LocExpr) -> Result<Val> {310 use Expr::*;311 let locexpr = expr.clone();312 let LocExpr(expr, loc) = expr;313 Ok(match &**expr {314 Literal(LiteralType::This) => Val::Obj(315 context316 .this()317 .clone()318 .unwrap_or_else(|| panic!("this not found")),319 ),320 Literal(LiteralType::Dollar) => Val::Obj(321 context322 .dollar()323 .clone()324 .unwrap_or_else(|| panic!("dollar not found")),325 ),326 Literal(LiteralType::True) => Val::Bool(true),327 Literal(LiteralType::False) => Val::Bool(false),328 Literal(LiteralType::Null) => Val::Null,329 Parened(e) => evaluate(context, e)?,330 Str(v) => Val::Str(v.clone()),331 Num(v) => Val::Num(*v),332 BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, &v1, *o, &v2)?,333 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,334 Var(name) => push(locexpr, "var".to_owned(), || {335 Val::Lazy(context.binding(&name)).unwrap_if_lazy()336 })?,337 Index(LocExpr(v, _), index) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {338 let name = evaluate(context.clone(), index)?.try_cast_str("object index")?;339 context340 .super_obj()341 .clone()342 .expect("no super found")343 .get_raw(&name, &context.this().clone().expect("no this found"))?344 .expect("value not found")345 }346 Index(value, index) => {347 match (348 evaluate(context.clone(), value)?.unwrap_if_lazy()?,349 evaluate(context, index)?,350 ) {351 (Val::Obj(v), Val::Str(s)) => {352 if let Some(v) = v.get(&s)? {353 v.unwrap_if_lazy()?354 } else if let Some(Val::Str(n)) = v.get("__intristic_namespace__")? {355 Val::Intristic(n, s)356 } else {357 create_error(crate::Error::NoSuchField(s))?358 }359 }360 (Val::Arr(v), Val::Num(n)) => {361 if n.fract() > f64::EPSILON {362 create_error(crate::Error::FractionalIndex)?363 }364 v.get(n as usize)365 .unwrap_or_else(|| panic!("out of bounds"))366 .clone()367 .unwrap_if_lazy()?368 }369 (Val::Str(s), Val::Num(n)) => {370 Val::Str(s.chars().skip(n as usize).take(1).collect())371 }372 (v, i) => todo!("not implemented: {:?}[{:?}]", v, i.unwrap_if_lazy()),373 }374 }375 LocalExpr(bindings, returned) => {376 let mut new_bindings: HashMap<String, LazyBinding> = HashMap::new();377 let future_context = Context::new_future();378379 let context_creator = context_creator!(380 closure!(clone future_context, |_, _| Ok(future_context.clone().unwrap()))381 );382383 for (k, v) in bindings384 .iter()385 .map(|b| evaluate_binding(b, context_creator.clone()))386 {387 new_bindings.insert(k, v);388 }389390 let context = context391 .extend_unbound(new_bindings, None, None, None)?392 .into_future(future_context);393 evaluate(context, &returned.clone())?394 }395 Arr(items) => {396 let mut out = Vec::with_capacity(items.len());397 for item in items {398 out.push(Val::Lazy(lazy_val!(399 closure!(clone context, clone item, || {400 evaluate(context.clone(), &item)401 })402 )));403 }404 Val::Arr(out)405 }406 ArrComp(expr, compspecs) => Val::Arr(407 // First compspec should be forspec, so no "None" possible here408 evaluate_comp(context, expr, compspecs)?.unwrap(),409 ),410 Obj(body) => Val::Obj(evaluate_object(context, body.clone())?),411 ObjExtend(s, t) => evaluate_add_op(412 &evaluate(context.clone(), s)?,413 &Val::Obj(evaluate_object(context, t.clone())?),414 )?,415 Apply(value, args, tailstrict) => {416 let value = evaluate(context.clone(), value)?.unwrap_if_lazy()?;417 match value {418 Val::Intristic(ns, name) => match (&ns as &str, &name as &str) {419 // arr/string/function420 ("std", "length") => {421 assert_eq!(args.len(), 1);422 let expr = &args.get(0).unwrap().1;423 match evaluate(context, expr)? {424 Val::Str(n) => Val::Num(n.chars().count() as f64),425 Val::Arr(i) => Val::Num(i.len() as f64),426 Val::Obj(o) => Val::Num(o.fields().len() as f64),427 v => panic!("can't get length of {:?}", v),428 }429 }430 // any431 ("std", "type") => {432 assert_eq!(args.len(), 1);433 let expr = &args.get(0).unwrap().1;434 Val::Str(evaluate(context, expr)?.value_type()?.name().to_owned())435 }436 // length, idx=>any437 ("std", "makeArray") => {438 assert_eq!(args.len(), 2);439 if let (Val::Num(v), Val::Func(d)) = (440 evaluate(context.clone(), &args[0].1)?,441 evaluate(context, &args[1].1)?,442 ) {443 assert!(v >= 0.0);444 let mut out = Vec::with_capacity(v as usize);445 for i in 0..v as usize {446 let call_ctx =447 Context::new().with_var("v".to_owned(), Val::Num(i as f64))?;448 out.push(d.evaluate(449 call_ctx,450 &ArgsDesc(vec![Arg(None, el!(Expr::Var("v".to_owned())))]),451 true,452 )?)453 }454 Val::Arr(out)455 } else {456 panic!("bad makeArray call");457 }458 }459 // string460 ("std", "codepoint") => {461 assert_eq!(args.len(), 1);462 if let Val::Str(s) = evaluate(context, &args[0].1)? {463 assert!(464 s.chars().count() == 1,465 "std.codepoint should receive single char string"466 );467 Val::Num(s.chars().take(1).next().unwrap() as u32 as f64)468 } else {469 panic!("bad codepoint call");470 }471 }472 // object, includeHidden473 ("std", "objectFieldsEx") => {474 assert_eq!(args.len(), 2);475 if let (Val::Obj(body), Val::Bool(_include_hidden)) = (476 evaluate(context.clone(), &args[0].1)?,477 evaluate(context, &args[1].1)?,478 ) {479 // TODO: handle visibility (_include_hidden)480 Val::Arr(body.fields().into_iter().map(Val::Str).collect())481 } else {482 panic!("bad objectFieldsEx call");483 }484 }485 ("std", "primitiveEquals") => {486 assert_eq!(args.len(), 2);487 let (a, b) = (488 evaluate(context.clone(), &args[0].1)?,489 evaluate(context, &args[1].1)?,490 );491 Val::Bool(a == b)492 }493 ("std", "modulo") => {494 assert_eq!(args.len(), 2);495 if let (Val::Num(a), Val::Num(b)) = (496 evaluate(context.clone(), &args[0].1)?,497 evaluate(context, &args[1].1)?,498 ) {499 Val::Num(a % b)500 } else {501 panic!("bad modulo call");502 }503 }504 ("std", "floor") => {505 assert_eq!(args.len(), 1);506 if let Val::Num(a) = evaluate(context, &args[0].1)? {507 Val::Num(a.floor())508 } else {509 panic!("bad floor call");510 }511 }512 ("std", "trace") => {513 assert_eq!(args.len(), 2);514 if let (Val::Str(a), b) = (515 evaluate(context.clone(), &args[0].1)?,516 evaluate(context, &args[1].1)?,517 ) {518 // TODO: Line numbers as in original jsonnet519 println!("TRACE: {}", a);520 b521 } else {522 panic!("bad trace call");523 }524 }525 ("std", "pow") => {526 assert_eq!(args.len(), 2);527 if let (Val::Num(a), Val::Num(b)) = (528 evaluate(context.clone(), &args[0].1)?,529 evaluate(context, &args[1].1)?,530 ) {531 Val::Num(a.powf(b))532 } else {533 panic!("bad pow call");534 }535 }536 (ns, name) => panic!("Intristic not found: {}.{}", ns, name),537 },538 Val::Func(f) => {539 let body = #[inline(always)]540 || f.evaluate(context, args, *tailstrict);541 if *tailstrict {542 body()?543 } else {544 push(locexpr, "function call".to_owned(), body)?545 }546 }547 _ => panic!("{:?} is not a function", value),548 }549 }550 Function(params, body) => evaluate_method(context, params.clone(), body.clone()),551 AssertExpr(AssertStmt(value, msg), returned) => {552 let assertion_result = push(value.clone(), "assertion condition".to_owned(), || {553 evaluate(context.clone(), &value)?554 .try_cast_bool("assertion condition should be boolean")555 })?;556 if assertion_result {557 push(558 returned.clone(),559 "assert 'return' branch".to_owned(),560 || evaluate(context, returned),561 )?562 } else if let Some(msg) = msg {563 panic!(564 "assertion failed ({:?}): {}",565 value,566 evaluate(context, msg)?.try_cast_str("assertion message should be string")?567 );568 } else {569 panic!("assertion failed ({:?}): no message", value);570 }571 }572 Error(e) => create_error(crate::Error::RuntimeError(573 evaluate(context, e)?.try_cast_str("error text should be string")?,574 ))?,575 IfElse {576 cond,577 cond_then,578 cond_else,579 } => {580 let condition_result = push(cond.0.clone(), "if condition".to_owned(), || {581 evaluate(context.clone(), &cond.0)?.try_cast_bool("if condition should be boolean")582 })?;583 if condition_result {584 push(585 cond_then.clone(),586 "if condition 'then' branch".to_owned(),587 || evaluate(context, cond_then),588 )?589 } else {590 match cond_else {591 Some(v) => evaluate(context, v)?,592 None => Val::Null,593 }594 }595 }596 _ => panic!(597 "evaluation not implemented: {:?}",598 LocExpr(expr.clone(), loc.clone())599 ),600 })601}crates/jsonnet-evaluator/src/function.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/function.rs
+++ b/crates/jsonnet-evaluator/src/function.rs
@@ -0,0 +1,80 @@
+use crate::{create_error, evaluate, lazy_val, resolved_lazy_val, Context, Error, Result};
+use closure::closure;
+use jsonnet_parser::{ArgsDesc, ParamsDesc};
+use std::collections::HashMap;
+
+/// Creates correct [context](Context) for function body evaluation, returning error on invalid call
+///
+/// * `ctx` used for passed argument expressions execution, and for body execution (if `body_ctx` is not set)
+/// * `body_ctx` used for default parameter values execution, and for body execution (if set)
+/// * `params` function parameters definition
+/// * `args` passed function arguments
+/// * `tailstruct` if true - function arguments is eager executed, otherwise - lazy
+pub fn parse_function_call(
+ ctx: Context,
+ body_ctx: Option<Context>,
+ params: &ParamsDesc,
+ args: &ArgsDesc,
+ tailstrict: bool,
+) -> Result<Context> {
+ inline_parse_function_call(ctx, body_ctx, params, args, tailstrict)
+}
+
+/// See [parse_function_call](parse_function_call)
+///
+/// ## Notes
+/// This function is always inlined for tailstrict
+#[inline(always)]
+pub(crate) fn inline_parse_function_call(
+ ctx: Context,
+ body_ctx: Option<Context>,
+ params: &ParamsDesc,
+ args: &ArgsDesc,
+ tailstrict: bool,
+) -> Result<Context> {
+ let mut out = HashMap::new();
+ let mut positioned_args = vec![None; params.0.len()];
+ for (id, arg) in args.iter().enumerate() {
+ let idx = if let Some(name) = &arg.0 {
+ params.iter().position(|p| &p.0 == name).ok_or_else(|| {
+ create_error::<()>(Error::UnknownFunctionParameter(name.clone()))
+ .err()
+ .unwrap()
+ })?
+ } else {
+ id
+ };
+
+ if idx >= params.len() {
+ create_error(Error::TooManyArgsFunctionHas(params.len()))?;
+ }
+ if positioned_args[idx].is_some() {
+ create_error(Error::BindingParameterASecondTime(params[idx].0.clone()))?;
+ }
+ positioned_args[idx] = Some(arg.1.clone());
+ }
+ // Fill defaults
+ for (id, p) in params.iter().enumerate() {
+ let (ctx, expr) = if let Some(arg) = &positioned_args[id] {
+ (ctx.clone(), arg)
+ } else if let Some(default) = &p.1 {
+ (
+ body_ctx
+ .clone()
+ .expect("no default context set for call with defined default parameter value"),
+ default,
+ )
+ } else {
+ create_error(Error::FunctionParameterNotBoundInCall(p.0.clone()))?;
+ unreachable!()
+ };
+ let val = if tailstrict {
+ resolved_lazy_val!(evaluate(ctx.clone(), expr)?)
+ } else {
+ lazy_val!(closure!(clone ctx, clone expr, ||evaluate(ctx.clone(), &expr)))
+ };
+ out.insert(p.0.clone(), val);
+ }
+
+ Ok(body_ctx.unwrap_or(ctx).extend(out, None, None, None)?)
+}
crates/jsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/lib.rs
+++ b/crates/jsonnet-evaluator/src/lib.rs
@@ -8,6 +8,7 @@
mod error;
mod evaluate;
mod function;
+mod map;
mod obj;
mod val;
@@ -15,6 +16,7 @@
pub use dynamic::*;
pub use error::*;
pub use evaluate::*;
+pub use function::parse_function_call;
use jsonnet_parser::*;
pub use obj::*;
use std::{cell::RefCell, collections::HashMap, fmt::Debug, path::PathBuf, rc::Rc};
@@ -24,17 +26,12 @@
Binding,
binding,
dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<Val>
-);
-rc_fn_helper!(FunctionRhs, function_rhs, dyn Fn(Context) -> Result<Val>);
-rc_fn_helper!(
- FunctionDefault,
- function_default,
- dyn Fn(Context, LocExpr) -> Result<Val>
);
+type BindableFn = dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<LazyVal>;
#[derive(Clone)]
pub enum LazyBinding {
- Bindable(Rc<dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<LazyVal>>),
+ Bindable(Rc<BindableFn>),
Bound(LazyVal),
}
@@ -59,7 +56,7 @@
impl Default for EvaluationSettings {
fn default() -> Self {
EvaluationSettings {
- max_stack_frames: 500,
+ max_stack_frames: 200,
max_stack_trace_size: 20,
}
}
@@ -181,7 +178,7 @@
self.0.globals.borrow_mut().insert(name, value);
}
- pub fn add_stdlib(&self) {
+ pub fn with_stdlib(&self) -> &Self {
self.begin_state();
use jsonnet_stdlib::STDLIB_STR;
if cfg!(feature = "serialized-stdlib") {
@@ -199,6 +196,7 @@
let val = self.evaluate_file(&PathBuf::from("std.jsonnet")).unwrap();
self.add_global("std".to_owned(), val);
self.end_state();
+ self
}
pub fn create_default_context(&self) -> Result<Context> {
@@ -210,7 +208,7 @@
LazyBinding::Bound(resolved_lazy_val!(value.clone())),
);
}
- Context::new().extend(new_bindings, None, None, None)
+ Context::new().extend_unbound(new_bindings, None, None, None)
}
#[inline(always)]
@@ -297,7 +295,7 @@
#[test]
fn eval_state_standard() {
let state = EvaluationState::default();
- state.add_stdlib();
+ state.with_stdlib();
assert_eq!(
state
.parse_evaluate_raw(r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#)
@@ -308,106 +306,47 @@
macro_rules! eval {
($str: expr) => {
- evaluate(
- Context::new(),
- EvaluationState::default(),
- &parse(
- $str,
- &ParserSettings {
- loc_data: true,
- file_name: "test.jsonnet".to_owned(),
- },
- )
- .unwrap(),
- )
+ EvaluationState::default()
+ .with_stdlib()
+ .parse_evaluate_raw($str)
+ .unwrap()
};
}
-
- macro_rules! eval_stdlib {
+ macro_rules! eval_json {
($str: expr) => {{
- let std = "local std = ".to_owned() + jsonnet_stdlib::STDLIB_STR + ";";
- evaluate(
- Context::new(),
- EvaluationState::default(),
- &parse(
- &(std + $str),
- &ParserSettings {
- loc_data: true,
- file_name: "test.jsonnet".to_owned(),
- },
- )
- .unwrap(),
- )
+ let evaluator = EvaluationState::default();
+ evaluator.with_stdlib();
+ let val = evaluator.parse_evaluate_raw($str).unwrap();
+ evaluator.add_global("__tmp__to_yaml__".to_owned(), val);
+ evaluator
+ .parse_evaluate_raw("std.manifestJsonEx(__tmp__to_yaml__, \"\")")
+ .unwrap()
+ .try_cast_str("there should be json string")
+ .unwrap()
+ .clone()
+ .replace("\n", "")
}};
}
+ /// Asserts given code returns `true`
macro_rules! assert_eval {
($str: expr) => {
- assert_eq!(
- evaluate(
- Context::new(),
- EvaluationState::default(),
- &parse(
- $str,
- &ParserSettings {
- loc_data: true,
- file_name: "test.jsonnet".to_owned(),
- }
- )
- .unwrap()
- ),
- Val::Bool(true)
- )
+ assert_eq!(eval!($str), Val::Bool(true))
};
}
- macro_rules! assert_json {
- ($str: expr, $out: expr) => {
- assert_eq!(
- format!(
- "{}",
- evaluate(
- Context::new(),
- EvaluationState::default(),
- &parse(
- $str,
- &ParserSettings {
- loc_data: true,
- file_name: "test.jsonnet".to_owned(),
- }
- )
- .unwrap()
- )
- ),
- $out
- )
+
+ /// Asserts given code returns `false`
+ macro_rules! assert_eval_neg {
+ ($str: expr) => {
+ assert_eq!(eval!($str), Val::Bool(false))
};
}
- macro_rules! assert_json_stdlib {
+ macro_rules! assert_json {
($str: expr, $out: expr) => {
- assert_eq!(format!("{}", eval_stdlib!($str)), $out)
- };
- }
- macro_rules! assert_eval_neg {
- ($str: expr) => {
- assert_eq!(
- evaluate(
- Context::new(),
- EvaluationState::default(),
- &parse(
- $str,
- &ParserSettings {
- loc_data: true,
- file_name: "test.jsonnet".to_owned(),
- }
- )
- .unwrap()
- ),
- Val::Bool(false)
- )
+ assert_eq!(eval_json!($str), $out.replace("\t", ""))
};
}
- /*
/// Sanity checking, before trusting to another tests
#[test]
fn equality_operator() {
@@ -435,6 +374,23 @@
}
#[test]
+ fn function_contexts() {
+ assert_eval!(
+ r#"
+ local k = {
+ t(name = self.h): [self.h, name],
+ h: 3,
+ };
+ local f = {
+ t: k.t(),
+ h: 4,
+ };
+ f.t[0] == f.t[1]
+ "#
+ );
+ }
+
+ #[test]
fn local() {
assert_eval!("local a = 2; local b = 3; a + b == 5");
assert_eval!("local a = 1, b = a + 1; a + b == 3");
@@ -446,19 +402,20 @@
assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);
}
+ /// FIXME: This test gets stackoverflow in debug build
#[test]
fn object_inheritance() {
- assert_json!("{a: self.b} + {b:3}", r#"{"a":3,"b":3}"#);
+ assert_json!("{a: self.b} + {b:3}", r#"{"a": 3,"b": 3}"#);
}
#[test]
fn test_object() {
- assert_json!("{a:2}", r#"{"a":2}"#);
- assert_json!("{a:2+2}", r#"{"a":4}"#);
- assert_json!("{a:2}+{b:2}", r#"{"a":2,"b":2}"#);
- assert_json!("{b:3}+{b:2}", r#"{"b":2}"#);
- assert_json!("{b:3}+{b+:2}", r#"{"b":5}"#);
- assert_json!("local test='a'; {[test]:2}", r#"{"a":2}"#);
+ assert_json!("{a:2}", r#"{"a": 2}"#);
+ assert_json!("{a:2+2}", r#"{"a": 4}"#);
+ assert_json!("{a:2}+{b:2}", r#"{"a": 2,"b": 2}"#);
+ assert_json!("{b:3}+{b:2}", r#"{"b": 2}"#);
+ assert_json!("{b:3}+{b+:2}", r#"{"b": 5}"#);
+ assert_json!("local test='a'; {[test]:2}", r#"{"a": 2}"#);
assert_json!(
r#"
{
@@ -466,7 +423,7 @@
welcome: "Hello " + self.name + "!",
}
"#,
- r#"{"name":"Alice","welcome":"Hello Alice!"}"#
+ r#"{"name": "Alice","welcome": "Hello Alice!"}"#
);
assert_json!(
r#"
@@ -477,7 +434,7 @@
name: "Bob"
}
"#,
- r#"{"name":"Bob","welcome":"Hello Bob!"}"#
+ r#"{"name": "Bob","welcome": "Hello Bob!"}"#
);
}
@@ -501,11 +458,11 @@
#[test]
fn object_locals() {
- assert_json!(r#"{local a = 3, b: a}"#, r#"{"b":3}"#);
- assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b":3}"#);
+ assert_json!(r#"{local a = 3, b: a}"#, r#"{"b": 3}"#);
+ assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b": 3}"#);
assert_json!(
r#"{local a = function (b) {[b]:4}, test: a("test")}"#,
- r#"{"test":{"test":4}}"#
+ r#"{"test": {"test": 4}}"#
);
}
@@ -529,7 +486,7 @@
fn indirect_self() {
// `self` assigned to `me` was lost when being
// referenced from field
- eval_stdlib!(
+ eval!(
r#"{
local me = self,
a: 3,
@@ -541,47 +498,47 @@
// We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly
#[test]
fn std_assert_ok() {
- eval_stdlib!("std.assertEqual(4.5 << 2, 16)");
+ eval!("std.assertEqual(4.5 << 2, 16)");
}
#[test]
#[should_panic]
fn std_assert_failure() {
- eval_stdlib!("std.assertEqual(4.5 << 2, 15)");
+ eval!("std.assertEqual(4.5 << 2, 15)");
}
#[test]
fn string_is_string() {
assert_eq!(
- eval_stdlib!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),
+ eval!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),
Val::Bool(false)
);
}
#[test]
fn base64_works() {
- assert_json_stdlib!(r#"std.base64("test")"#, r#""dGVzdA==""#);
+ assert_json!(r#"std.base64("test")"#, r#""dGVzdA==""#);
}
#[test]
fn utf8_chars() {
- assert_json_stdlib!(
+ assert_json!(
r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,
- r#"{"c":128526,"l":1}"#
+ r#"{"c": 128526,"l": 1}"#
)
}
#[test]
fn json() {
- assert_json_stdlib!(
+ assert_json!(
r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,
- r#""{\n"a": 3,\n"b": 4,\n"c": 6\n}""#
+ r#""{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}""#
);
}
#[test]
fn test() {
- assert_json_stdlib!(
+ assert_json!(
r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,
"[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"
);
@@ -617,5 +574,4 @@
"#
);
}
- */
}
crates/jsonnet-evaluator/src/map.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/jsonnet-evaluator/src/map.rs
@@ -0,0 +1,46 @@
+use std::{borrow::Borrow, collections::HashMap, hash::Hash, rc::Rc};
+
+#[derive(Default, Debug)]
+struct LayeredHashMapInternals<K: Hash, V> {
+ parent: Option<LayeredHashMap<K, V>>,
+ current: HashMap<K, V>,
+}
+
+#[derive(Debug)]
+pub struct LayeredHashMap<K: Hash, V>(Rc<LayeredHashMapInternals<K, V>>);
+
+impl<K: Hash + Eq, V> LayeredHashMap<K, V> {
+ pub fn extend(&self, new_layer: HashMap<K, V>) -> Self {
+ let super_map = self.clone();
+ LayeredHashMap(Rc::new(LayeredHashMapInternals {
+ parent: Some(super_map),
+ current: new_layer,
+ }))
+ }
+
+ pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V>
+ where
+ K: Borrow<Q>,
+ Q: Hash + Eq,
+ {
+ (self.0)
+ .current
+ .get(&key)
+ .or_else(|| self.0.parent.as_ref().and_then(|p| p.get(key)))
+ }
+}
+
+impl<K: Hash, V> Clone for LayeredHashMap<K, V> {
+ fn clone(&self) -> Self {
+ LayeredHashMap(self.0.clone())
+ }
+}
+
+impl<K: Hash + Eq, V> Default for LayeredHashMap<K, V> {
+ fn default() -> Self {
+ LayeredHashMap(Rc::new(LayeredHashMapInternals {
+ parent: None,
+ current: HashMap::new(),
+ }))
+ }
+}
crates/jsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/val.rs
+++ b/crates/jsonnet-evaluator/src/val.rs
@@ -1,11 +1,9 @@
use crate::{
- create_error, Context, Error, FunctionDefault, FunctionRhs, LazyBinding, ObjValue, Result,
+ create_error, evaluate, function::inline_parse_function_call, Context, Error, ObjValue, Result,
};
-use closure::closure;
-use jsonnet_parser::{Param, ParamsDesc};
+use jsonnet_parser::{ArgsDesc, LocExpr, ParamsDesc};
use std::{
cell::RefCell,
- collections::HashMap,
fmt::{Debug, Display},
rc::Rc,
};
@@ -73,47 +71,21 @@
pub struct FuncDesc {
pub ctx: Context,
pub params: ParamsDesc,
- pub eval_rhs: FunctionRhs,
- pub eval_default: FunctionDefault,
+ pub body: LocExpr,
}
impl FuncDesc {
// TODO: Check for unset variables
/// This function is always inlined to make tailstrict work
#[inline(always)]
- pub fn evaluate(&self, args: Vec<(Option<String>, Val)>) -> Result<Val> {
- let mut new_bindings: HashMap<String, LazyBinding> = HashMap::new();
- let future_ctx = Context::new_future();
-
- for Param(name, default) in self.params.with_defaults() {
- let default = default.unwrap();
- let eval_default = self.eval_default.clone();
- new_bindings.insert(
- name,
- LazyBinding::Bound(lazy_val!(
- closure!(clone future_ctx, clone eval_default, clone default, || (eval_default.clone()).0
- (future_ctx.clone().unwrap(), default.clone()))
- )),
- );
- }
- for (name, val) in args.clone().into_iter().filter(|e| e.0.is_some()) {
- new_bindings.insert(
- name.as_ref().unwrap().clone(),
- LazyBinding::Bound(resolved_lazy_val!(val.clone())),
- );
- }
- for (i, param) in self.params.0.iter().enumerate() {
- if let Some((None, val)) = args.get(i) {
- new_bindings.insert(
- param.0.clone(),
- LazyBinding::Bound(resolved_lazy_val!(val.clone())),
- );
- }
- }
- let ctx = self
- .ctx
- .extend(new_bindings, None, None, None)?
- .into_future(future_ctx);
- self.eval_rhs.0(ctx)
+ pub fn evaluate(&self, call_ctx: Context, args: &ArgsDesc, tailstrict: bool) -> Result<Val> {
+ let ctx = inline_parse_function_call(
+ call_ctx,
+ Some(self.ctx.clone()),
+ &self.params,
+ args,
+ tailstrict,
+ )?;
+ evaluate(ctx, &self.body)
}
}