difftreelog
fix make jsonnet tests partially pass
in: master
3 files changed
crates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/evaluate.rs
+++ b/crates/jsonnet-evaluator/src/evaluate.rs
@@ -54,11 +54,16 @@
pub fn evaluate_field_name(
context: Context,
field_name: &jsonnet_parser::FieldName,
-) -> Result<String> {
+) -> Result<Option<String>> {
Ok(match field_name {
- jsonnet_parser::FieldName::Fixed(n) => n.clone(),
+ jsonnet_parser::FieldName::Fixed(n) => Some(n.clone()),
jsonnet_parser::FieldName::Dyn(expr) => {
- evaluate(context, expr)?.try_cast_str("dynamic field name")?
+ let value = evaluate(context, expr)?.unwrap_if_lazy()?;
+ if matches!(value, Val::Null) {
+ None
+ } else {
+ Some(value.try_cast_str("dynamic field name")?)
+ }
}
})
}
@@ -234,6 +239,10 @@
value,
}) => {
let name = evaluate_field_name(context.clone(), &name)?;
+ if name.is_none() {
+ continue;
+ }
+ let name = name.unwrap();
new_members.insert(
name.clone(),
ObjMember {
@@ -260,6 +269,10 @@
..
}) => {
let name = evaluate_field_name(context.clone(), &name)?;
+ if name.is_none() {
+ continue;
+ }
+ let name = name.unwrap();
new_members.insert(
name,
ObjMember {
@@ -299,12 +312,6 @@
.clone()
.unwrap_or_else(|| panic!("this not found")),
),
- Literal(LiteralType::Super) => Val::Obj(
- context
- .super_obj()
- .clone()
- .unwrap_or_else(|| panic!("super not found")),
- ),
Literal(LiteralType::Dollar) => Val::Obj(
context
.dollar()
@@ -320,6 +327,15 @@
BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, &v1, *o, &v2)?,
UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,
Var(name) => Val::Lazy(context.binding(&name)).unwrap_if_lazy()?,
+ Index(LocExpr(v, _), index) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {
+ let name = evaluate(context.clone(), index)?.try_cast_str("object index")?;
+ context
+ .super_obj()
+ .clone()
+ .expect("no super found")
+ .get_raw(&name, &context.this().clone().expect("no this found"))?
+ .expect("value not found")
+ }
Index(value, index) => {
match (
evaluate(context.clone(), value)?.unwrap_if_lazy()?,
@@ -381,6 +397,10 @@
evaluate_comp(context, expr, compspecs)?.unwrap(),
),
Obj(body) => Val::Obj(evaluate_object(context, body.clone())?),
+ ObjExtend(s, t) => evaluate_add_op(
+ &evaluate(context.clone(), s)?,
+ &Val::Obj(evaluate_object(context, t.clone())?),
+ )?,
Apply(value, ArgsDesc(args)) => {
let value = evaluate(context.clone(), value)?.unwrap_if_lazy()?;
match value {
@@ -408,7 +428,7 @@
evaluate(context.clone(), &args[0].1)?,
evaluate(context, &args[1].1)?,
) {
- assert!(v > 0.0);
+ assert!(v >= 0.0);
let mut out = Vec::with_capacity(v as usize);
for i in 0..v as usize {
out.push(d.evaluate(vec![(None, Val::Num(i as f64))])?)
@@ -452,6 +472,25 @@
);
Val::Bool(a == b)
}
+ ("std", "modulo") => {
+ assert_eq!(args.len(), 2);
+ if let (Val::Num(a), Val::Num(b)) = (
+ evaluate(context.clone(), &args[0].1)?,
+ evaluate(context, &args[1].1)?,
+ ) {
+ Val::Num(a % b)
+ } else {
+ panic!("bad modulo call");
+ }
+ }
+ ("std", "floor") => {
+ assert_eq!(args.len(), 1);
+ if let Val::Num(a) = evaluate(context, &args[0].1)? {
+ Val::Num(a.floor())
+ } else {
+ panic!("bad floor call");
+ }
+ }
(ns, name) => panic!("Intristic not found: {}.{}", ns, name),
},
Val::Func(f) => push(locexpr, "function call".to_owned(), || {
@@ -516,7 +555,7 @@
Some(v) => push(v.clone(), "if condition 'else' branch".to_owned(), || {
evaluate(context, v)
})?,
- None => Val::Bool(false),
+ None => Val::Null,
}
}
}
crates/jsonnet-evaluator/src/obj.rsdiffbeforeafterboth1use crate::{evaluate_add_op, Binding, Result, Val};2use jsonnet_parser::Visibility;3use std::{4 cell::RefCell,5 collections::{BTreeMap, BTreeSet, HashMap},6 fmt::Debug,7 rc::Rc,8};910#[derive(Debug)]11pub struct ObjMember {12 pub add: bool,13 pub visibility: Visibility,14 pub invoke: Binding,15}1617#[derive(Debug)]18pub struct ObjValueInternals {19 super_obj: Option<ObjValue>,20 this_entries: Rc<BTreeMap<String, ObjMember>>,21 value_cache: RefCell<HashMap<String, Val>>,22}23pub struct ObjValue(pub(crate) Rc<ObjValueInternals>);24impl Debug for ObjValue {25 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {26 if let Some(super_obj) = self.0.super_obj.as_ref() {27 if f.alternate() {28 write!(f, "{:#?}", super_obj)?;29 } else {30 write!(f, "{:?}", super_obj)?;31 }32 write!(f, " + ")?;33 }34 let mut debug = f.debug_struct("ObjValue");35 for (name, member) in self.0.this_entries.iter() {36 debug.field(name, member);37 }38 debug.finish_non_exhaustive()39 }40}4142impl ObjValue {43 pub fn new(44 super_obj: Option<ObjValue>,45 this_entries: Rc<BTreeMap<String, ObjMember>>,46 ) -> ObjValue {47 ObjValue(Rc::new(ObjValueInternals {48 super_obj,49 this_entries,50 value_cache: RefCell::new(HashMap::new()),51 }))52 }53 pub fn with_super(&self, super_obj: ObjValue) -> ObjValue {54 match &self.0.super_obj {55 None => ObjValue::new(Some(super_obj), self.0.this_entries.clone()),56 Some(v) => ObjValue::new(Some(v.with_super(super_obj)), self.0.this_entries.clone()),57 }58 }59 pub fn fields(&self) -> BTreeSet<String> {60 let mut fields = BTreeSet::new();61 self.0.this_entries.keys().for_each(|k| {62 fields.insert(k.clone());63 });64 if self.0.super_obj.is_some() {65 for field in self.0.super_obj.clone().unwrap().fields() {66 fields.insert(field);67 }68 }69 fields70 }71 pub fn get(&self, key: &str) -> Result<Option<Val>> {72 if let Some(v) = self.0.value_cache.borrow().get(key) {73 return Ok(Some(v.clone()));74 }75 if let Some(v) = self.get_raw(key, self)? {76 let v = v.unwrap_if_lazy()?;77 self.078 .value_cache79 .borrow_mut()80 .insert(key.to_owned(), v.clone());81 Ok(Some(v))82 } else {83 Ok(None)84 }85 }86 fn get_raw(&self, key: &str, real_this: &ObjValue) -> Result<Option<Val>> {87 match (self.0.this_entries.get(key), &self.0.super_obj) {88 (Some(k), None) => Ok(Some(k.invoke.0(89 Some(real_this.clone()),90 self.0.super_obj.clone(),91 )?)),92 (Some(k), Some(s)) => {93 let our = k.invoke.0(Some(real_this.clone()), self.0.super_obj.clone())?;94 if k.add {95 s.get_raw(key, real_this)?96 .map_or(Ok(Some(our.clone())), |v| {97 Ok(Some(evaluate_add_op(&v, &our)?))98 })99 } else {100 Ok(Some(our))101 }102 }103 (None, Some(s)) => s.get_raw(key, real_this),104 (None, None) => Ok(None),105 }106 }107}108impl Clone for ObjValue {109 fn clone(&self) -> Self {110 ObjValue(self.0.clone())111 }112}113impl PartialEq for ObjValue {114 fn eq(&self, other: &Self) -> bool {115 Rc::ptr_eq(&self.0, &other.0)116 }117}1use crate::{evaluate_add_op, Binding, Result, Val};2use jsonnet_parser::Visibility;3use std::{4 cell::RefCell,5 collections::{BTreeMap, BTreeSet, HashMap},6 fmt::Debug,7 rc::Rc,8};910#[derive(Debug)]11pub struct ObjMember {12 pub add: bool,13 pub visibility: Visibility,14 pub invoke: Binding,15}1617#[derive(Debug)]18pub struct ObjValueInternals {19 super_obj: Option<ObjValue>,20 this_entries: Rc<BTreeMap<String, ObjMember>>,21 value_cache: RefCell<HashMap<String, Val>>,22}23pub struct ObjValue(pub(crate) Rc<ObjValueInternals>);24impl Debug for ObjValue {25 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {26 if let Some(super_obj) = self.0.super_obj.as_ref() {27 if f.alternate() {28 write!(f, "{:#?}", super_obj)?;29 } else {30 write!(f, "{:?}", super_obj)?;31 }32 write!(f, " + ")?;33 }34 let mut debug = f.debug_struct("ObjValue");35 for (name, member) in self.0.this_entries.iter() {36 debug.field(name, member);37 }38 debug.finish_non_exhaustive()39 }40}4142impl ObjValue {43 pub fn new(44 super_obj: Option<ObjValue>,45 this_entries: Rc<BTreeMap<String, ObjMember>>,46 ) -> ObjValue {47 ObjValue(Rc::new(ObjValueInternals {48 super_obj,49 this_entries,50 value_cache: RefCell::new(HashMap::new()),51 }))52 }53 pub fn with_super(&self, super_obj: ObjValue) -> ObjValue {54 match &self.0.super_obj {55 None => ObjValue::new(Some(super_obj), self.0.this_entries.clone()),56 Some(v) => ObjValue::new(Some(v.with_super(super_obj)), self.0.this_entries.clone()),57 }58 }59 pub fn fields(&self) -> BTreeSet<String> {60 let mut fields = BTreeSet::new();61 self.0.this_entries.keys().for_each(|k| {62 fields.insert(k.clone());63 });64 if self.0.super_obj.is_some() {65 for field in self.0.super_obj.clone().unwrap().fields() {66 fields.insert(field);67 }68 }69 fields70 }71 pub fn get(&self, key: &str) -> Result<Option<Val>> {72 if let Some(v) = self.0.value_cache.borrow().get(key) {73 return Ok(Some(v.clone()));74 }75 if let Some(v) = self.get_raw(key, self)? {76 let v = v.unwrap_if_lazy()?;77 self.078 .value_cache79 .borrow_mut()80 .insert(key.to_owned(), v.clone());81 Ok(Some(v))82 } else {83 Ok(None)84 }85 }86 pub(crate) fn get_raw(&self, key: &str, real_this: &ObjValue) -> Result<Option<Val>> {87 match (self.0.this_entries.get(key), &self.0.super_obj) {88 (Some(k), None) => Ok(Some(k.invoke.0(89 Some(real_this.clone()),90 self.0.super_obj.clone(),91 )?)),92 (Some(k), Some(s)) => {93 let our = k.invoke.0(Some(real_this.clone()), self.0.super_obj.clone())?;94 if k.add {95 s.get_raw(key, real_this)?96 .map_or(Ok(Some(our.clone())), |v| {97 Ok(Some(evaluate_add_op(&v, &our)?))98 })99 } else {100 Ok(Some(our))101 }102 }103 (None, Some(s)) => s.get_raw(key, real_this),104 (None, None) => Ok(None),105 }106 }107}108impl Clone for ObjValue {109 fn clone(&self) -> Self {110 ObjValue(self.0.clone())111 }112}113impl PartialEq for ObjValue {114 fn eq(&self, other: &Self) -> bool {115 Rc::ptr_eq(&self.0, &other.0)116 }117}crates/jsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/val.rs
+++ b/crates/jsonnet-evaluator/src/val.rs
@@ -3,7 +3,7 @@
ObjValue, Result,
};
use closure::closure;
-use jsonnet_parser::ParamsDesc;
+use jsonnet_parser::{Param, ParamsDesc};
use std::{
cell::RefCell,
collections::HashMap,
@@ -70,19 +70,15 @@
let mut new_bindings: HashMap<String, LazyBinding> = HashMap::new();
let future_ctx = Context::new_future();
- // self.params
- // .with_defaults()
- // .into_iter()
- // .for_each(|Param(name, default)| {
- // let default = Rc::new(*default.unwrap());
- // new_bindings.insert(
- // name,
- // binding!(move |_, _| Val::Lazy(lazy_val!(|| self
- // .eval_default
- // .0
- // .default(future_ctx.unwrap(), *default.clone())))),
- // );
- // });
+ for Param(name, default) in self.params.with_defaults() {
+ let default = default.unwrap();
+ let eval_default = self.eval_default.clone();
+ new_bindings.insert(
+ name,
+ lazy_binding!(closure!(clone future_ctx, clone default, clone eval_default, |_, _| Ok(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(),