difftreelog
feat object assertions
in: master
6 files changed
crates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -85,6 +85,10 @@
self.extend(new_bindings, None, None, None)
}
+ pub fn with_this_super(self, new_this: ObjValue, new_super_obj: Option<ObjValue>) -> Self {
+ self.extend(FxHashMap::default(), None, Some(new_this), new_super_obj)
+ }
+
pub fn extend(
self,
new_bindings: FxHashMap<IStr, LazyVal>,
crates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate.rs
@@ -261,6 +261,7 @@
}
let mut new_members = FxHashMap::default();
+ let mut assertions = Vec::new();
for member in members.iter() {
match member {
Member::Field(FieldMember {
@@ -325,10 +326,12 @@
);
}
Member::BindStmt(_) => {}
- Member::AssertStmt(_) => {}
+ Member::AssertStmt(stmt) => {
+ assertions.push(stmt.clone());
+ }
}
}
- let this = ObjValue::new(None, Rc::new(new_members));
+ let this = ObjValue::new(context, None, Rc::new(new_members), Rc::new(assertions));
future_this.fill(this.clone());
Ok(this)
}
@@ -385,7 +388,7 @@
}
}
- let this = ObjValue::new(None, Rc::new(new_members));
+ let this = ObjValue::new(context, None, Rc::new(new_members), Rc::new(Vec::new()));
future_this.fill(this.clone());
this
}
@@ -413,6 +416,36 @@
})
}
+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;
@@ -552,30 +585,9 @@
evaluate_method(context, "anonymous".into(), params.clone(), body.clone())
}
Intrinsic(name) => Val::Func(Rc::new(FuncVal::Intrinsic(name.clone()))),
- AssertExpr(AssertStmt(value, msg), returned) => {
- 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 {
- evaluate(context, returned)?
- } else {
- 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()?));
- }
- },
- )?
- }
+ AssertExpr(assert, returned) => {
+ evaluate_assert(context.clone(), &assert)?;
+ evaluate(context, returned)?
}
ErrorStmt(e) => push(
loc.as_ref(),
crates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth1use crate::{2 error::{Error::*, LocError, Result},3 throw, LazyBinding, LazyVal, ObjMember, ObjValue, Val,4};5use jrsonnet_parser::Visibility;6use rustc_hash::FxHasher;7use serde_json::{Map, Number, Value};8use std::{9 collections::HashMap,10 convert::{TryFrom, TryInto},11 hash::BuildHasherDefault,12 rc::Rc,13};1415impl TryFrom<&Val> for Value {16 type Error = LocError;17 fn try_from(v: &Val) -> Result<Self> {18 Ok(match v {19 Val::Bool(b) => Self::Bool(*b),20 Val::Null => Self::Null,21 Val::Str(s) => Self::String((s as &str).into()),22 Val::Num(n) => Self::Number(if n.fract() <= f64::EPSILON {23 (*n as i64).into()24 } else {25 Number::from_f64(*n).expect("to json number")26 }),27 Val::Arr(a) => {28 let mut out = Vec::with_capacity(a.len());29 for item in a.iter() {30 out.push((&item?).try_into()?);31 }32 Self::Array(out)33 }34 Val::Obj(o) => {35 let mut out = Map::new();36 for key in o.fields() {37 out.insert(38 (&key as &str).into(),39 (&o.get(key)?.expect("field exists")).try_into()?,40 );41 }42 Self::Object(out)43 }44 Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),45 })46 }47}4849impl From<&Value> for Val {50 fn from(v: &Value) -> Self {51 match v {52 Value::Null => Self::Null,53 Value::Bool(v) => Self::Bool(*v),54 Value::Number(n) => Self::Num(n.as_f64().expect("as f64")),55 Value::String(s) => Self::Str((s as &str).into()),56 Value::Array(a) => {57 let mut out = Vec::with_capacity(a.len());58 for v in a {59 out.push(LazyVal::new_resolved(v.into()));60 }61 Self::Arr(out.into())62 }63 Value::Object(o) => {64 let mut entries = HashMap::with_capacity_and_hasher(65 o.len(),66 BuildHasherDefault::<FxHasher>::default(),67 );68 for (k, v) in o {69 entries.insert(70 (k as &str).into(),71 ObjMember {72 add: false,73 visibility: Visibility::Normal,74 invoke: LazyBinding::Bound(LazyVal::new_resolved(v.into())),75 location: None,76 },77 );78 }79 Self::Obj(ObjValue::new(None, Rc::new(entries)))80 }81 }82 }83}crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -1,7 +1,7 @@
-use crate::{evaluate_add_op, LazyBinding, Result, Val};
+use crate::{Context, evaluate_add_op, evaluate_assert, LazyBinding, Result, Val};
use jrsonnet_interner::IStr;
-use jrsonnet_parser::{ExprLocation, Visibility};
-use rustc_hash::FxHashMap;
+use jrsonnet_parser::{ExprLocation, LocExpr, Visibility, AssertStmt};
+use rustc_hash::{FxHashMap, FxHashSet};
use std::hash::{Hash, Hasher};
use std::{cell::RefCell, fmt::Debug, hash::BuildHasherDefault, rc::Rc};
@@ -17,7 +17,10 @@
type CacheKey = (IStr, ObjValue);
#[derive(Debug)]
pub struct ObjValueInternals {
+ context: Context,
super_obj: Option<ObjValue>,
+ assertions: Rc<Vec<AssertStmt>>,
+ assertions_ran: RefCell<FxHashSet<ObjValue>>,
this_obj: Option<ObjValue>,
this_entries: Rc<FxHashMap<IStr, ObjMember>>,
value_cache: RefCell<FxHashMap<CacheKey, Option<Val>>>,
@@ -51,26 +54,32 @@
}
impl ObjValue {
- pub fn new(super_obj: Option<Self>, this_entries: Rc<FxHashMap<IStr, ObjMember>>) -> Self {
+ pub fn new(context: Context, super_obj: Option<Self>, this_entries: Rc<FxHashMap<IStr, ObjMember>>, assertions: Rc<Vec<AssertStmt>>) -> Self {
Self(Rc::new(ObjValueInternals {
+ context,
super_obj,
+ assertions,
+ assertions_ran: RefCell::new(FxHashSet::default()),
this_obj: None,
this_entries,
value_cache: RefCell::new(FxHashMap::default()),
}))
}
pub fn new_empty() -> Self {
- Self::new(None, Rc::new(FxHashMap::default()))
+ Self::new(Context::new(), None, Rc::new(FxHashMap::default()), Rc::new(Vec::new()))
}
pub fn extend_from(&self, super_obj: Self) -> Self {
match &self.0.super_obj {
- None => Self::new(Some(super_obj), self.0.this_entries.clone()),
- Some(v) => Self::new(Some(v.extend_from(super_obj)), self.0.this_entries.clone()),
+ None => Self::new(self.0.context.clone(), Some(super_obj), self.0.this_entries.clone(), self.0.assertions.clone()),
+ Some(v) => Self::new(self.0.context.clone(), Some(v.extend_from(super_obj)), self.0.this_entries.clone(), self.0.assertions.clone()),
}
}
pub fn with_this(&self, this_obj: Self) -> Self {
Self(Rc::new(ObjValueInternals {
+ context: self.0.context.clone(),
super_obj: self.0.super_obj.clone(),
+ assertions: self.0.assertions.clone(),
+ assertions_ran: RefCell::new(FxHashSet::default()),
this_obj: Some(this_obj),
this_entries: self.0.this_entries.clone(),
value_cache: RefCell::new(FxHashMap::default()),
@@ -167,16 +176,17 @@
}
pub fn get(&self, key: IStr) -> Result<Option<Val>> {
+ self.run_assertions(self.0.this_obj.as_ref().unwrap_or(self))?;
self.get_raw(key, self.0.this_obj.as_ref())
}
pub fn extend_with_field(self, key: IStr, value: ObjMember) -> Self {
let mut new = FxHashMap::with_capacity_and_hasher(1, BuildHasherDefault::default());
new.insert(key, value);
- Self::new(Some(self), Rc::new(new))
+ Self::new(Context::new(), Some(self), Rc::new(new), Rc::new(Vec::new()))
}
- pub(crate) fn get_raw(&self, key: IStr, real_this: Option<&Self>) -> Result<Option<Val>> {
+ pub fn get_raw(&self, key: IStr, real_this: Option<&Self>) -> Result<Option<Val>> {
let real_this = real_this.unwrap_or(self);
let cache_key = (key.clone(), real_this.clone());
@@ -210,6 +220,24 @@
.evaluate(Some(real_this.clone()), self.0.super_obj.clone())?
.evaluate()
}
+ fn run_assertions(&self, real_this: &Self) -> Result<()> {
+ if self.0.assertions_ran.borrow().contains(&real_this) {
+ // Assertions already ran
+ } else {
+ self.0.assertions_ran.borrow_mut().insert(real_this.clone());
+ for assertion in self.0.assertions.iter() {
+ println!("{:#?}", assertion);
+ if let Err(e) = evaluate_assert(self.0.context.clone().with_this_super(real_this.clone(), self.0.super_obj.clone()), &assertion) {
+ self.0.assertions_ran.borrow_mut().remove(&real_this);
+ return Err(e)
+ }
+ }
+ if let Some(super_obj) = &self.0.super_obj {
+ super_obj.run_assertions(&real_this)?;
+ }
+ }
+ Ok(())
+ }
pub fn ptr_eq(a: &Self, b: &Self) -> bool {
Rc::ptr_eq(&a.0, &b.0)
crates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -40,7 +40,7 @@
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
-#[derive(Debug, PartialEq)]
+#[derive(Clone, Debug, PartialEq)]
pub struct AssertStmt(pub LocExpr, pub Option<LocExpr>);
#[cfg_attr(feature = "serialize", derive(Serialize))]
flake.nixdiffbeforeafterboth--- a/flake.nix
+++ b/flake.nix
@@ -12,7 +12,7 @@
pname = "jrsonnet";
version = "0.1.0";
src = self;
- cargoSha256 = "sha256-5RzjO9McVqG8+1+p+wRvygYCnemPjUAVB9TpWOp2ipA=";
+ cargoSha256 = "sha256-6VhaQi3L2LWzR0cq7oRG81MDbrKJbzSNPcvYSoQ5ISo=";
};
in { defaultPackage = jrsonnet; });
}