git.delta.rocks / jrsonnet / refs/commits / abdb122c59b2

difftreelog

refactor(evaluator) replace Binding with LazyBinding

Лач2020-06-11parent: #96f2d11.patch.diff
in: master

4 files changed

modifiedcrates/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(),
-										))
-									})
-								),
+										)))
+									}),
+								)),
 							},
 						);
 					}
modifiedcrates/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 {
modifiedcrates/jsonnet-evaluator/src/map.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/map.rs
+++ b/crates/jsonnet-evaluator/src/map.rs
@@ -18,6 +18,7 @@
 		}))
 	}
 
+	#[inline(always)]
 	pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V>
 	where
 		K: Borrow<Q>,
modifiedcrates/jsonnet-evaluator/src/obj.rsdiffbeforeafterboth
before · crates/jsonnet-evaluator/src/obj.rs
1use crate::{evaluate_add_op, Binding, Result, Val};2use indexmap::IndexMap;3use jsonnet_parser::Visibility;4use std::{5	cell::RefCell,6	collections::{BTreeMap, HashMap},7	fmt::Debug,8	rc::Rc,9};1011#[derive(Debug)]12pub struct ObjMember {13	pub add: bool,14	pub visibility: Visibility,15	pub invoke: Binding,16}1718#[derive(Debug)]19pub struct ObjValueInternals {20	super_obj: Option<ObjValue>,21	this_entries: Rc<BTreeMap<String, ObjMember>>,22	value_cache: RefCell<HashMap<String, Val>>,23}24pub struct ObjValue(pub(crate) Rc<ObjValueInternals>);25impl Debug for ObjValue {26	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {27		if let Some(super_obj) = self.0.super_obj.as_ref() {28			if f.alternate() {29				write!(f, "{:#?}", super_obj)?;30			} else {31				write!(f, "{:?}", super_obj)?;32			}33			write!(f, " + ")?;34		}35		let mut debug = f.debug_struct("ObjValue");36		for (name, member) in self.0.this_entries.iter() {37			debug.field(name, member);38		}39		debug.finish_non_exhaustive()40	}41}4243impl ObjValue {44	pub fn new(45		super_obj: Option<ObjValue>,46		this_entries: Rc<BTreeMap<String, ObjMember>>,47	) -> ObjValue {48		ObjValue(Rc::new(ObjValueInternals {49			super_obj,50			this_entries,51			value_cache: RefCell::new(HashMap::new()),52		}))53	}54	pub fn with_super(&self, super_obj: ObjValue) -> ObjValue {55		match &self.0.super_obj {56			None => ObjValue::new(Some(super_obj), self.0.this_entries.clone()),57			Some(v) => ObjValue::new(Some(v.with_super(super_obj)), self.0.this_entries.clone()),58		}59	}60	pub fn enum_fields(&self, handler: &impl Fn(&str, &Visibility)) {61		if let Some(s) = &self.0.super_obj {62			s.enum_fields(handler);63		}64		for (name, member) in self.0.this_entries.iter() {65			handler(&name, &member.visibility);66		}67	}68	pub fn fields_visibility(&self) -> IndexMap<String, bool> {69		let out = Rc::new(RefCell::new(IndexMap::new()));70		self.enum_fields(&|name, visibility| {71			let mut out = out.borrow_mut();72			match visibility {73				Visibility::Normal => {74					if !out.contains_key(name) {75						out.insert(name.to_owned(), true);76					}77				}78				Visibility::Hidden => {79					out.insert(name.to_owned(), false);80				}81				Visibility::Unhide => {82					out.insert(name.to_owned(), true);83				}84			};85		});86		Rc::try_unwrap(out).unwrap().into_inner()87	}88	pub fn get(&self, key: &str) -> Result<Option<Val>> {89		if let Some(v) = self.0.value_cache.borrow().get(key) {90			return Ok(Some(v.clone()));91		}92		if let Some(v) = self.get_raw(key, self)? {93			let v = v.unwrap_if_lazy()?;94			self.095				.value_cache96				.borrow_mut()97				.insert(key.to_owned(), v.clone());98			Ok(Some(v))99		} else {100			Ok(None)101		}102	}103	pub(crate) fn get_raw(&self, key: &str, real_this: &ObjValue) -> Result<Option<Val>> {104		match (self.0.this_entries.get(key), &self.0.super_obj) {105			(Some(k), None) => Ok(Some(k.invoke.0(106				Some(real_this.clone()),107				self.0.super_obj.clone(),108			)?)),109			(Some(k), Some(s)) => {110				let our = k.invoke.0(Some(real_this.clone()), self.0.super_obj.clone())?;111				if k.add {112					s.get_raw(key, real_this)?113						.map_or(Ok(Some(our.clone())), |v| {114							Ok(Some(evaluate_add_op(&v, &our)?))115						})116				} else {117					Ok(Some(our))118				}119			}120			(None, Some(s)) => s.get_raw(key, real_this),121			(None, None) => Ok(None),122		}123	}124}125impl Clone for ObjValue {126	fn clone(&self) -> Self {127		ObjValue(self.0.clone())128	}129}130impl PartialEq for ObjValue {131	fn eq(&self, other: &Self) -> bool {132		Rc::ptr_eq(&self.0, &other.0)133	}134}
after · crates/jsonnet-evaluator/src/obj.rs
1use crate::{evaluate_add_op, LazyBinding, Result, Val};2use indexmap::IndexMap;3use jsonnet_parser::Visibility;4use std::{5	cell::RefCell,6	collections::{BTreeMap, HashMap},7	fmt::Debug,8	rc::Rc,9};1011#[derive(Debug)]12pub struct ObjMember {13	pub add: bool,14	pub visibility: Visibility,15	pub invoke: LazyBinding,16}1718#[derive(Debug)]19pub struct ObjValueInternals {20	super_obj: Option<ObjValue>,21	this_entries: Rc<BTreeMap<String, ObjMember>>,22	value_cache: RefCell<HashMap<String, Val>>,23}24pub struct ObjValue(pub(crate) Rc<ObjValueInternals>);25impl Debug for ObjValue {26	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {27		if let Some(super_obj) = self.0.super_obj.as_ref() {28			if f.alternate() {29				write!(f, "{:#?}", super_obj)?;30			} else {31				write!(f, "{:?}", super_obj)?;32			}33			write!(f, " + ")?;34		}35		let mut debug = f.debug_struct("ObjValue");36		for (name, member) in self.0.this_entries.iter() {37			debug.field(name, member);38		}39		debug.finish_non_exhaustive()40	}41}4243impl ObjValue {44	pub fn new(45		super_obj: Option<ObjValue>,46		this_entries: Rc<BTreeMap<String, ObjMember>>,47	) -> ObjValue {48		ObjValue(Rc::new(ObjValueInternals {49			super_obj,50			this_entries,51			value_cache: RefCell::new(HashMap::new()),52		}))53	}54	pub fn with_super(&self, super_obj: ObjValue) -> ObjValue {55		match &self.0.super_obj {56			None => ObjValue::new(Some(super_obj), self.0.this_entries.clone()),57			Some(v) => ObjValue::new(Some(v.with_super(super_obj)), self.0.this_entries.clone()),58		}59	}60	pub fn enum_fields(&self, handler: &impl Fn(&str, &Visibility)) {61		if let Some(s) = &self.0.super_obj {62			s.enum_fields(handler);63		}64		for (name, member) in self.0.this_entries.iter() {65			handler(&name, &member.visibility);66		}67	}68	pub fn fields_visibility(&self) -> IndexMap<String, bool> {69		let out = Rc::new(RefCell::new(IndexMap::new()));70		self.enum_fields(&|name, visibility| {71			let mut out = out.borrow_mut();72			match visibility {73				Visibility::Normal => {74					if !out.contains_key(name) {75						out.insert(name.to_owned(), true);76					}77				}78				Visibility::Hidden => {79					out.insert(name.to_owned(), false);80				}81				Visibility::Unhide => {82					out.insert(name.to_owned(), true);83				}84			};85		});86		Rc::try_unwrap(out).unwrap().into_inner()87	}88	pub fn get(&self, key: &str) -> Result<Option<Val>> {89		if let Some(v) = self.0.value_cache.borrow().get(key) {90			return Ok(Some(v.clone()));91		}92		if let Some(v) = self.get_raw(key, self)? {93			let v = v.unwrap_if_lazy()?;94			self.095				.value_cache96				.borrow_mut()97				.insert(key.to_owned(), v.clone());98			Ok(Some(v))99		} else {100			Ok(None)101		}102	}103	pub(crate) fn get_raw(&self, key: &str, real_this: &ObjValue) -> Result<Option<Val>> {104		match (self.0.this_entries.get(key), &self.0.super_obj) {105			(Some(k), None) => Ok(Some(106				k.invoke107					.evaluate(Some(real_this.clone()), self.0.super_obj.clone())?108					.evaluate()?,109			)),110			(Some(k), Some(s)) => {111				let our = k112					.invoke113					.evaluate(Some(real_this.clone()), self.0.super_obj.clone())?114					.evaluate()?;115				if k.add {116					s.get_raw(key, real_this)?117						.map_or(Ok(Some(our.clone())), |v| {118							Ok(Some(evaluate_add_op(&v, &our)?))119						})120				} else {121					Ok(Some(our))122				}123			}124			(None, Some(s)) => s.get_raw(key, real_this),125			(None, None) => Ok(None),126		}127	}128}129impl Clone for ObjValue {130	fn clone(&self) -> Self {131		ObjValue(self.0.clone())132	}133}134impl PartialEq for ObjValue {135	fn eq(&self, other: &Self) -> bool {136		Rc::ptr_eq(&self.0, &other.0)137	}138}