difftreelog
feat(evaluator) tailstrict functions
in: master
5 files changed
crates/jsonnet-evaluator/src/ctx.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/ctx.rs
+++ b/crates/jsonnet-evaluator/src/ctx.rs
@@ -1,8 +1,6 @@
use crate::{
- future_wrapper, lazy_binding, lazy_val, rc_fn_helper, LazyBinding, LazyVal, ObjValue, Result,
- Val,
+ future_wrapper, rc_fn_helper, resolved_lazy_val, LazyBinding, LazyVal, ObjValue, Result, Val,
};
-use closure::closure;
use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};
rc_fn_helper!(
@@ -68,12 +66,7 @@
pub fn with_var(&self, name: String, value: Val) -> Result<Context> {
let mut new_bindings: HashMap<_, LazyBinding> = HashMap::new();
- new_bindings.insert(
- name,
- lazy_binding!(
- closure!(clone value, |_t, _s|Ok(lazy_val!(closure!(clone value, ||Ok(value.clone())))))
- ),
- );
+ new_bindings.insert(name, LazyBinding::Bound(resolved_lazy_val!(value.clone())));
self.extend(new_bindings, None, None, None)
}
@@ -95,7 +88,7 @@
new.insert(k.clone(), v.clone());
}
for (k, v) in new_bindings.into_iter() {
- new.insert(k, v.0(this.clone(), super_obj.clone())?);
+ new.insert(k, v.evaluate(this.clone(), super_obj.clone())?);
}
Rc::new(new)
};
crates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/evaluate.rs
+++ b/crates/jsonnet-evaluator/src/evaluate.rs
@@ -1,7 +1,7 @@
use crate::{
binding, context_creator, create_error, function_default, function_rhs, future_wrapper,
- lazy_binding, lazy_val, push, Context, ContextCreator, FuncDesc, LazyBinding, ObjMember,
- ObjValue, Result, Val,
+ lazy_val, push, Context, ContextCreator, FuncDesc, LazyBinding, LazyVal, ObjMember, ObjValue,
+ Result, Val,
};
use closure::closure;
use jsonnet_parser::{
@@ -19,18 +19,20 @@
let args = args.clone();
(
b.name.clone(),
- lazy_binding!(move |this, super_obj| Ok(lazy_val!(
- closure!(clone b, clone args, clone context_creator, || Ok(evaluate_method(
- context_creator.0(this.clone(), super_obj.clone())?,
- &b.value,
- args.clone()
- )))
- ))),
+ LazyBinding::Bindable(Rc::new(move |this, super_obj| {
+ Ok(lazy_val!(
+ closure!(clone b, clone args, clone context_creator, || Ok(evaluate_method(
+ context_creator.0(this.clone(), super_obj.clone())?,
+ &b.value,
+ args.clone()
+ )))
+ ))
+ })),
)
} else {
(
b.name.clone(),
- lazy_binding!(move |this, super_obj| {
+ LazyBinding::Bindable(Rc::new(move |this, super_obj| {
Ok(lazy_val!(closure!(clone context_creator, clone b, ||
push(b.value.clone(), "thunk".to_owned(), ||{
evaluate(
@@ -39,7 +41,7 @@
)
})
)))
- }),
+ })),
)
}
}
@@ -405,7 +407,7 @@
&evaluate(context.clone(), s)?,
&Val::Obj(evaluate_object(context, t.clone())?),
)?,
- Apply(value, ArgsDesc(args)) => {
+ Apply(value, ArgsDesc(args), tailstrict) => {
let value = evaluate(context.clone(), value)?.unwrap_if_lazy()?;
match value {
Val::Intristic(ns, name) => match (&ns as &str, &name as &str) {
@@ -502,7 +504,8 @@
evaluate(context.clone(), &args[0].1)?,
evaluate(context, &args[1].1)?,
) {
- println!("{}", a);
+ // TODO: Line numbers as in original jsonnet
+ println!("TRACE: {}", a);
b
} else {
panic!("bad trace call");
@@ -517,9 +520,15 @@
.map(move |a| {
(
a.clone().0,
- Val::Lazy(lazy_val!(
- closure!(clone context, clone a, || evaluate(context.clone(), &a.clone().1))
- )),
+ if *tailstrict {
+ Val::Lazy(LazyVal::new_resolved(
+ evaluate(context.clone(), &a.1).unwrap(),
+ ))
+ } else {
+ Val::Lazy(lazy_val!(
+ closure!(clone context, clone a, || evaluate(context.clone(), &a.clone().1))
+ ))
+ },
)
})
.collect(),
crates/jsonnet-evaluator/src/function.rsdiffbeforeafterbothno changes
crates/jsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/lib.rs
+++ b/crates/jsonnet-evaluator/src/lib.rs
@@ -6,28 +6,23 @@
mod dynamic;
mod error;
mod evaluate;
+mod function;
mod obj;
mod val;
-use closure::closure;
pub use ctx::*;
pub use dynamic::*;
pub use error::*;
pub use evaluate::*;
use jsonnet_parser::*;
pub use obj::*;
-use std::{cell::RefCell, collections::HashMap, path::PathBuf, rc::Rc};
+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>
-);
-rc_fn_helper!(
- LazyBinding,
- lazy_binding,
- dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<LazyVal>
);
rc_fn_helper!(FunctionRhs, function_rhs, dyn Fn(Context) -> Result<Val>);
rc_fn_helper!(
@@ -36,6 +31,26 @@
dyn Fn(Context, LocExpr) -> Result<Val>
);
+#[derive(Clone)]
+pub enum LazyBinding {
+ Bindable(Rc<dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<LazyVal>>),
+ Bound(LazyVal),
+}
+
+impl Debug for LazyBinding {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "LazyBinding")
+ }
+}
+impl LazyBinding {
+ pub fn evaluate(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {
+ match self {
+ LazyBinding::Bindable(v) => v(this, super_obj),
+ LazyBinding::Bound(v) => Ok(v.clone()),
+ }
+ }
+}
+
pub struct FileData(String, LocExpr, Option<Val>);
#[derive(Default)]
pub struct EvaluationStateInternals {
@@ -168,9 +183,7 @@
for (name, value) in globals.iter() {
new_bindings.insert(
name.clone(),
- lazy_binding!(
- closure!(clone value, |_self, _super_obj| Ok(lazy_val!(closure!(clone value, ||Ok(value.clone())))))
- ),
+ LazyBinding::Bound(resolved_lazy_val!(value.clone())),
);
}
Context::new().extend(new_bindings, None, None, None)
@@ -179,7 +192,7 @@
pub fn push<T>(&self, e: LocExpr, comment: String, f: impl FnOnce() -> Result<T>) -> Result<T> {
{
let mut stack = self.0.stack.borrow_mut();
- if stack.len() > 5000 {
+ if stack.len() > 500 {
drop(stack);
return self.error(Error::StackOverflow);
} else {
crates/jsonnet-evaluator/src/val.rsdiffbeforeafterboth1use crate::{1use crate::{2 create_error, lazy_binding, Context, Error, FunctionDefault, FunctionRhs, LazyBinding,2 create_error, Context, Error, FunctionDefault, FunctionRhs, LazyBinding, ObjValue, Result,3 ObjValue, Result,4};3};5use closure::closure;4use closure::closure;12};11};131214struct LazyValInternals {13struct LazyValInternals {15 pub f: Box<dyn Fn() -> Result<Val>>,14 pub f: Option<Box<dyn Fn() -> Result<Val>>>,16 pub cached: RefCell<Option<Val>>,15 pub cached: RefCell<Option<Val>>,17}16}18#[derive(Clone)]17#[derive(Clone)]19pub struct LazyVal(Rc<LazyValInternals>);18pub struct LazyVal(Rc<LazyValInternals>);20impl LazyVal {19impl LazyVal {21 pub fn new(f: Box<dyn Fn() -> Result<Val>>) -> Self {20 pub fn new(f: Box<dyn Fn() -> Result<Val>>) -> Self {22 LazyVal(Rc::new(LazyValInternals {21 LazyVal(Rc::new(LazyValInternals {23 f,22 f: Some(f),24 cached: RefCell::new(None),23 cached: RefCell::new(None),25 }))24 }))26 }25 }26 pub fn new_resolved(val: Val) -> Self {27 LazyVal(Rc::new(LazyValInternals {28 f: None,29 cached: RefCell::new(Some(val)),30 }))31 }27 pub fn evaluate(&self) -> Result<Val> {32 pub fn evaluate(&self) -> Result<Val> {28 {33 {29 let cached = self.0.cached.borrow();34 let cached = self.0.cached.borrow();30 if cached.is_some() {35 if cached.is_some() {31 return Ok(cached.clone().unwrap());36 return Ok(cached.clone().unwrap());32 }37 }33 }38 }34 let result = (self.0.f)()?;39 let result = (self.0.f.as_ref().unwrap())()?;35 self.0.cached.borrow_mut().replace(result.clone());40 self.0.cached.borrow_mut().replace(result.clone());36 Ok(result)41 Ok(result)37 }42 }42 $crate::LazyVal::new(Box::new($f))48 $crate::LazyVal::new(Box::new($f))43 };49 };44}50}51#[macro_export]52macro_rules! resolved_lazy_val {53 ($f: expr) => {54 $crate::LazyVal::new_resolved($f)55 };56}45impl Debug for LazyVal {57impl Debug for LazyVal {46 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {58 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {47 if self.0.cached.borrow().is_some() {59 if self.0.cached.borrow().is_some() {75 let eval_default = self.eval_default.clone();87 let eval_default = self.eval_default.clone();76 new_bindings.insert(88 new_bindings.insert(77 name,89 name,78 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()).090 LazyBinding::Bound(lazy_val!(91 closure!(clone future_ctx, clone eval_default, clone default, || (eval_default.clone()).079 (future_ctx.clone().unwrap(), default.clone())))))),92 (future_ctx.clone().unwrap(), default.clone()))93 )),80 );94 );81 }95 }82 for (name, val) in args.clone().into_iter().filter(|e| e.0.is_some()) {96 for (name, val) in args.clone().into_iter().filter(|e| e.0.is_some()) {83 new_bindings.insert(97 new_bindings.insert(84 name.as_ref().unwrap().clone(),98 name.as_ref().unwrap().clone(),85 lazy_binding!(99 LazyBinding::Bound(resolved_lazy_val!(val.clone())),86 closure!(clone val, |_, _| Ok(lazy_val!(closure!(clone val, || Ok(val.clone())))))87 ),88 );100 );89 }101 }90 for (i, param) in self.params.0.iter().enumerate() {102 for (i, param) in self.params.0.iter().enumerate() {91 if let Some((None, val)) = args.get(i) {103 if let Some((None, val)) = args.get(i) {92 new_bindings.insert(104 new_bindings.insert(93 param.0.clone(),105 param.0.clone(),94 lazy_binding!(106 LazyBinding::Bound(resolved_lazy_val!(val.clone())),95 closure!(clone val, |_, _| Ok(lazy_val!(closure!(clone val, || Ok(val.clone())))))96 ),97 );107 );98 }108 }