difftreelog
refactor(evaluator) replace Binding with LazyBinding
in: master
4 files changed
crates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/evaluate.rs
+++ b/crates/jsonnet-evaluator/src/evaluate.rs
@@ -1,6 +1,6 @@
use crate::{
- binding, context_creator, create_error, future_wrapper, lazy_val, push, with_state, Context,
- ContextCreator, FuncDesc, LazyBinding, ObjMember, ObjValue, Result, Val,
+ context_creator, create_error, future_wrapper, lazy_val, push, with_state, Context,
+ ContextCreator, FuncDesc, LazyBinding, LazyVal, ObjMember, ObjValue, Result, Val,
};
use closure::closure;
use jsonnet_parser::{
@@ -252,17 +252,17 @@
ObjMember {
add: plus,
visibility: visibility.clone(),
- invoke: binding!(
+ invoke: LazyBinding::Bindable(Rc::new(
closure!(clone name, clone value, clone context_creator, |this, super_obj| {
- push(value.clone(), "object ".to_owned()+&name+" field", ||{
+ Ok(LazyVal::new_resolved(push(value.clone(), "object ".to_owned()+&name+" field", ||{
let context = context_creator.0(this, super_obj)?;
evaluate(
context,
&value,
)?.unwrap_if_lazy()
- })
- })
- ),
+ })?))
+ }),
+ )),
},
);
}
@@ -282,16 +282,16 @@
ObjMember {
add: false,
visibility: Visibility::Hidden,
- invoke: binding!(
+ invoke: LazyBinding::Bindable(Rc::new(
closure!(clone value, clone context_creator, |this, super_obj| {
// TODO: Assert
- Ok(evaluate_method(
+ Ok(LazyVal::new_resolved(evaluate_method(
context_creator.0(this, super_obj)?,
params.clone(),
value.clone(),
- ))
- })
- ),
+ )))
+ }),
+ )),
},
);
}
crates/jsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/lib.rs
+++ b/crates/jsonnet-evaluator/src/lib.rs
@@ -22,12 +22,6 @@
use std::{cell::RefCell, collections::HashMap, fmt::Debug, path::PathBuf, rc::Rc};
pub use val::*;
-rc_fn_helper!(
- Binding,
- binding,
- dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<Val>
-);
-
type BindableFn = dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<LazyVal>;
#[derive(Clone)]
pub enum LazyBinding {
crates/jsonnet-evaluator/src/map.rsdiffbeforeafterboth1use std::{borrow::Borrow, collections::HashMap, hash::Hash, rc::Rc};23#[derive(Default, Debug)]4struct LayeredHashMapInternals<K: Hash, V> {5 parent: Option<LayeredHashMap<K, V>>,6 current: HashMap<K, V>,7}89#[derive(Debug)]10pub struct LayeredHashMap<K: Hash, V>(Rc<LayeredHashMapInternals<K, V>>);1112impl<K: Hash + Eq, V> LayeredHashMap<K, V> {13 pub fn extend(&self, new_layer: HashMap<K, V>) -> Self {14 let super_map = self.clone();15 LayeredHashMap(Rc::new(LayeredHashMapInternals {16 parent: Some(super_map),17 current: new_layer,18 }))19 }2021 pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V>22 where23 K: Borrow<Q>,24 Q: Hash + Eq,25 {26 (self.0)27 .current28 .get(&key)29 .or_else(|| self.0.parent.as_ref().and_then(|p| p.get(key)))30 }31}3233impl<K: Hash, V> Clone for LayeredHashMap<K, V> {34 fn clone(&self) -> Self {35 LayeredHashMap(self.0.clone())36 }37}3839impl<K: Hash + Eq, V> Default for LayeredHashMap<K, V> {40 fn default() -> Self {41 LayeredHashMap(Rc::new(LayeredHashMapInternals {42 parent: None,43 current: HashMap::new(),44 }))45 }46}1use std::{borrow::Borrow, collections::HashMap, hash::Hash, rc::Rc};23#[derive(Default, Debug)]4struct LayeredHashMapInternals<K: Hash, V> {5 parent: Option<LayeredHashMap<K, V>>,6 current: HashMap<K, V>,7}89#[derive(Debug)]10pub struct LayeredHashMap<K: Hash, V>(Rc<LayeredHashMapInternals<K, V>>);1112impl<K: Hash + Eq, V> LayeredHashMap<K, V> {13 pub fn extend(&self, new_layer: HashMap<K, V>) -> Self {14 let super_map = self.clone();15 LayeredHashMap(Rc::new(LayeredHashMapInternals {16 parent: Some(super_map),17 current: new_layer,18 }))19 }2021 #[inline(always)]22 pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V>23 where24 K: Borrow<Q>,25 Q: Hash + Eq,26 {27 (self.0)28 .current29 .get(&key)30 .or_else(|| self.0.parent.as_ref().and_then(|p| p.get(key)))31 }32}3334impl<K: Hash, V> Clone for LayeredHashMap<K, V> {35 fn clone(&self) -> Self {36 LayeredHashMap(self.0.clone())37 }38}3940impl<K: Hash + Eq, V> Default for LayeredHashMap<K, V> {41 fn default() -> Self {42 LayeredHashMap(Rc::new(LayeredHashMapInternals {43 parent: None,44 current: HashMap::new(),45 }))46 }47}crates/jsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/obj.rs
+++ b/crates/jsonnet-evaluator/src/obj.rs
@@ -1,4 +1,4 @@
-use crate::{evaluate_add_op, Binding, Result, Val};
+use crate::{evaluate_add_op, LazyBinding, Result, Val};
use indexmap::IndexMap;
use jsonnet_parser::Visibility;
use std::{
@@ -12,7 +12,7 @@
pub struct ObjMember {
pub add: bool,
pub visibility: Visibility,
- pub invoke: Binding,
+ pub invoke: LazyBinding,
}
#[derive(Debug)]
@@ -102,12 +102,16 @@
}
pub(crate) fn get_raw(&self, key: &str, real_this: &ObjValue) -> Result<Option<Val>> {
match (self.0.this_entries.get(key), &self.0.super_obj) {
- (Some(k), None) => Ok(Some(k.invoke.0(
- Some(real_this.clone()),
- self.0.super_obj.clone(),
- )?)),
+ (Some(k), None) => Ok(Some(
+ k.invoke
+ .evaluate(Some(real_this.clone()), self.0.super_obj.clone())?
+ .evaluate()?,
+ )),
(Some(k), Some(s)) => {
- let our = k.invoke.0(Some(real_this.clone()), self.0.super_obj.clone())?;
+ let our = k
+ .invoke
+ .evaluate(Some(real_this.clone()), self.0.super_obj.clone())?
+ .evaluate()?;
if k.add {
s.get_raw(key, real_this)?
.map_or(Ok(Some(our.clone())), |v| {