difftreelog
feat object assertions
in: master
6 files changed
crates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth1use crate::{2 error::Error::*, map::LayeredHashMap, resolved_lazy_val, FutureWrapper, LazyBinding, LazyVal,3 ObjValue, Result, Val,4};5use jrsonnet_interner::IStr;6use rustc_hash::FxHashMap;7use std::hash::BuildHasherDefault;8use std::{fmt::Debug, rc::Rc};910#[derive(Clone)]11pub struct ContextCreator(pub Context, pub FutureWrapper<FxHashMap<IStr, LazyBinding>>);12impl ContextCreator {13 pub fn create(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<Context> {14 self.0.clone().extend_unbound(15 self.1.clone().unwrap(),16 self.0.dollar().clone().or_else(|| this.clone()),17 this,18 super_obj,19 )20 }21}2223struct ContextInternals {24 dollar: Option<ObjValue>,25 this: Option<ObjValue>,26 super_obj: Option<ObjValue>,27 bindings: LayeredHashMap<LazyVal>,28}29impl Debug for ContextInternals {30 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {31 f.debug_struct("Context")32 .field("this", &self.this.as_ref().map(|e| Rc::as_ptr(&e.0)))33 .field("bindings", &self.bindings)34 .finish()35 }36}3738#[derive(Debug, Clone)]39pub struct Context(Rc<ContextInternals>);40impl Context {41 pub fn new_future() -> FutureWrapper<Self> {42 FutureWrapper::new()43 }4445 pub fn dollar(&self) -> &Option<ObjValue> {46 &self.0.dollar47 }4849 pub fn this(&self) -> &Option<ObjValue> {50 &self.0.this51 }5253 pub fn super_obj(&self) -> &Option<ObjValue> {54 &self.0.super_obj55 }5657 pub fn new() -> Self {58 Self(Rc::new(ContextInternals {59 dollar: None,60 this: None,61 super_obj: None,62 bindings: LayeredHashMap::default(),63 }))64 }6566 pub fn binding(&self, name: IStr) -> Result<LazyVal> {67 Ok(self68 .069 .bindings70 .get(&name)71 .cloned()72 .ok_or(VariableIsNotDefined(name))?)73 }74 pub fn into_future(self, ctx: FutureWrapper<Self>) -> Self {75 {76 ctx.0.borrow_mut().replace(self);77 }78 ctx.unwrap()79 }8081 pub fn with_var(self, name: IStr, value: Val) -> Self {82 let mut new_bindings =83 FxHashMap::with_capacity_and_hasher(1, BuildHasherDefault::default());84 new_bindings.insert(name, resolved_lazy_val!(value));85 self.extend(new_bindings, None, None, None)86 }8788 pub fn extend(89 self,90 new_bindings: FxHashMap<IStr, LazyVal>,91 new_dollar: Option<ObjValue>,92 new_this: Option<ObjValue>,93 new_super_obj: Option<ObjValue>,94 ) -> Self {95 match Rc::try_unwrap(self.0) {96 Ok(mut ctx) => {97 // Extended context aren't used by anything else, we can freely mutate it without cloning98 if let Some(dollar) = new_dollar {99 ctx.dollar = Some(dollar);100 }101 if let Some(this) = new_this {102 ctx.this = Some(this);103 }104 if let Some(super_obj) = new_super_obj {105 ctx.super_obj = Some(super_obj);106 }107 if !new_bindings.is_empty() {108 ctx.bindings = ctx.bindings.extend(new_bindings);109 }110 Self(Rc::new(ctx))111 }112 Err(ctx) => {113 let dollar = new_dollar.or_else(|| ctx.dollar.clone());114 let this = new_this.or_else(|| ctx.this.clone());115 let super_obj = new_super_obj.or_else(|| ctx.super_obj.clone());116 let bindings = if new_bindings.is_empty() {117 ctx.bindings.clone()118 } else {119 ctx.bindings.clone().extend(new_bindings)120 };121 Self(Rc::new(ContextInternals {122 dollar,123 this,124 super_obj,125 bindings,126 }))127 }128 }129 }130 pub fn extend_bound(self, new_bindings: FxHashMap<IStr, LazyVal>) -> Self {131 let new_this = self.0.this.clone();132 let new_super_obj = self.0.super_obj.clone();133 self.extend(new_bindings, None, new_this, new_super_obj)134 }135 pub fn extend_unbound(136 self,137 new_bindings: FxHashMap<IStr, LazyBinding>,138 new_dollar: Option<ObjValue>,139 new_this: Option<ObjValue>,140 new_super_obj: Option<ObjValue>,141 ) -> Result<Self> {142 let this = new_this.or_else(|| self.0.this.clone());143 let super_obj = new_super_obj.or_else(|| self.0.super_obj.clone());144 let mut new =145 FxHashMap::with_capacity_and_hasher(new_bindings.len(), BuildHasherDefault::default());146 for (k, v) in new_bindings.into_iter() {147 new.insert(k, v.evaluate(this.clone(), super_obj.clone())?);148 }149 Ok(self.extend(new, new_dollar, this, super_obj))150 }151 #[cfg(feature = "unstable")]152 pub fn into_weak(self) -> WeakContext {153 WeakContext(Rc::downgrade(&self.0))154 }155}156157impl Default for Context {158 fn default() -> Self {159 Self::new()160 }161}162163impl PartialEq for Context {164 fn eq(&self, other: &Self) -> bool {165 Rc::ptr_eq(&self.0, &other.0)166 }167}168169#[cfg(feature = "unstable")]170#[derive(Debug, Clone)]171pub struct WeakContext(std::rc::Weak<ContextInternals>);172#[cfg(feature = "unstable")]173impl WeakContext {174 pub fn upgrade(&self) -> Context {175 Context(self.0.upgrade().expect("context is removed"))176 }177}178#[cfg(feature = "unstable")]179impl PartialEq for WeakContext {180 fn eq(&self, other: &Self) -> bool {181 self.0.ptr_eq(&other.0)182 }183}1use crate::{2 error::Error::*, map::LayeredHashMap, resolved_lazy_val, FutureWrapper, LazyBinding, LazyVal,3 ObjValue, Result, Val,4};5use jrsonnet_interner::IStr;6use rustc_hash::FxHashMap;7use std::hash::BuildHasherDefault;8use std::{fmt::Debug, rc::Rc};910#[derive(Clone)]11pub struct ContextCreator(pub Context, pub FutureWrapper<FxHashMap<IStr, LazyBinding>>);12impl ContextCreator {13 pub fn create(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<Context> {14 self.0.clone().extend_unbound(15 self.1.clone().unwrap(),16 self.0.dollar().clone().or_else(|| this.clone()),17 this,18 super_obj,19 )20 }21}2223struct ContextInternals {24 dollar: Option<ObjValue>,25 this: Option<ObjValue>,26 super_obj: Option<ObjValue>,27 bindings: LayeredHashMap<LazyVal>,28}29impl Debug for ContextInternals {30 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {31 f.debug_struct("Context")32 .field("this", &self.this.as_ref().map(|e| Rc::as_ptr(&e.0)))33 .field("bindings", &self.bindings)34 .finish()35 }36}3738#[derive(Debug, Clone)]39pub struct Context(Rc<ContextInternals>);40impl Context {41 pub fn new_future() -> FutureWrapper<Self> {42 FutureWrapper::new()43 }4445 pub fn dollar(&self) -> &Option<ObjValue> {46 &self.0.dollar47 }4849 pub fn this(&self) -> &Option<ObjValue> {50 &self.0.this51 }5253 pub fn super_obj(&self) -> &Option<ObjValue> {54 &self.0.super_obj55 }5657 pub fn new() -> Self {58 Self(Rc::new(ContextInternals {59 dollar: None,60 this: None,61 super_obj: None,62 bindings: LayeredHashMap::default(),63 }))64 }6566 pub fn binding(&self, name: IStr) -> Result<LazyVal> {67 Ok(self68 .069 .bindings70 .get(&name)71 .cloned()72 .ok_or(VariableIsNotDefined(name))?)73 }74 pub fn into_future(self, ctx: FutureWrapper<Self>) -> Self {75 {76 ctx.0.borrow_mut().replace(self);77 }78 ctx.unwrap()79 }8081 pub fn with_var(self, name: IStr, value: Val) -> Self {82 let mut new_bindings =83 FxHashMap::with_capacity_and_hasher(1, BuildHasherDefault::default());84 new_bindings.insert(name, resolved_lazy_val!(value));85 self.extend(new_bindings, None, None, None)86 }8788 pub fn with_this_super(self, new_this: ObjValue, new_super_obj: Option<ObjValue>) -> Self {89 self.extend(FxHashMap::default(), None, Some(new_this), new_super_obj)90 }9192 pub fn extend(93 self,94 new_bindings: FxHashMap<IStr, LazyVal>,95 new_dollar: Option<ObjValue>,96 new_this: Option<ObjValue>,97 new_super_obj: Option<ObjValue>,98 ) -> Self {99 match Rc::try_unwrap(self.0) {100 Ok(mut ctx) => {101 // Extended context aren't used by anything else, we can freely mutate it without cloning102 if let Some(dollar) = new_dollar {103 ctx.dollar = Some(dollar);104 }105 if let Some(this) = new_this {106 ctx.this = Some(this);107 }108 if let Some(super_obj) = new_super_obj {109 ctx.super_obj = Some(super_obj);110 }111 if !new_bindings.is_empty() {112 ctx.bindings = ctx.bindings.extend(new_bindings);113 }114 Self(Rc::new(ctx))115 }116 Err(ctx) => {117 let dollar = new_dollar.or_else(|| ctx.dollar.clone());118 let this = new_this.or_else(|| ctx.this.clone());119 let super_obj = new_super_obj.or_else(|| ctx.super_obj.clone());120 let bindings = if new_bindings.is_empty() {121 ctx.bindings.clone()122 } else {123 ctx.bindings.clone().extend(new_bindings)124 };125 Self(Rc::new(ContextInternals {126 dollar,127 this,128 super_obj,129 bindings,130 }))131 }132 }133 }134 pub fn extend_bound(self, new_bindings: FxHashMap<IStr, LazyVal>) -> Self {135 let new_this = self.0.this.clone();136 let new_super_obj = self.0.super_obj.clone();137 self.extend(new_bindings, None, new_this, new_super_obj)138 }139 pub fn extend_unbound(140 self,141 new_bindings: FxHashMap<IStr, LazyBinding>,142 new_dollar: Option<ObjValue>,143 new_this: Option<ObjValue>,144 new_super_obj: Option<ObjValue>,145 ) -> Result<Self> {146 let this = new_this.or_else(|| self.0.this.clone());147 let super_obj = new_super_obj.or_else(|| self.0.super_obj.clone());148 let mut new =149 FxHashMap::with_capacity_and_hasher(new_bindings.len(), BuildHasherDefault::default());150 for (k, v) in new_bindings.into_iter() {151 new.insert(k, v.evaluate(this.clone(), super_obj.clone())?);152 }153 Ok(self.extend(new, new_dollar, this, super_obj))154 }155 #[cfg(feature = "unstable")]156 pub fn into_weak(self) -> WeakContext {157 WeakContext(Rc::downgrade(&self.0))158 }159}160161impl Default for Context {162 fn default() -> Self {163 Self::new()164 }165}166167impl PartialEq for Context {168 fn eq(&self, other: &Self) -> bool {169 Rc::ptr_eq(&self.0, &other.0)170 }171}172173#[cfg(feature = "unstable")]174#[derive(Debug, Clone)]175pub struct WeakContext(std::rc::Weak<ContextInternals>);176#[cfg(feature = "unstable")]177impl WeakContext {178 pub fn upgrade(&self) -> Context {179 Context(self.0.upgrade().expect("context is removed"))180 }181}182#[cfg(feature = "unstable")]183impl PartialEq for WeakContext {184 fn eq(&self, other: &Self) -> bool {185 self.0.ptr_eq(&other.0)186 }187}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.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -1,4 +1,5 @@
use crate::{
+ Context,
error::{Error::*, LocError, Result},
throw, LazyBinding, LazyVal, ObjMember, ObjValue, Val,
};
@@ -76,7 +77,7 @@
},
);
}
- Self::Obj(ObjValue::new(None, Rc::new(entries)))
+ Self::Obj(ObjValue::new(Context::new(), None, Rc::new(entries), Rc::new(Vec::new())))
}
}
}
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; });
}