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

difftreelog

feat(evaluator) tailstrict calls

Лач2020-06-08parent: #dbab177.patch.diff
in: master

4 files changed

modifiedcrates/jsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/error.rs
+++ b/crates/jsonnet-evaluator/src/error.rs
@@ -9,6 +9,8 @@
 
 	RuntimeError(String),
 	StackOverflow,
+	FractionalIndex,
+	DivisionByZero,
 }
 
 #[derive(Clone, Debug)]
modifiedcrates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/evaluate.rs
+++ b/crates/jsonnet-evaluator/src/evaluate.rs
@@ -135,7 +135,12 @@
 
 		// Num X Num
 		(Val::Num(v1), BinaryOpType::Mul, Val::Num(v2)) => Val::Num(v1 * v2),
-		(Val::Num(v1), BinaryOpType::Div, Val::Num(v2)) => Val::Num(v1 / v2),
+		(Val::Num(v1), BinaryOpType::Div, Val::Num(v2)) => {
+			if *v2 <= f64::EPSILON {
+				create_error(crate::Error::DivisionByZero)?
+			}
+			Val::Num(v1 / v2)
+		}
 
 		(Val::Num(v1), BinaryOpType::Sub, Val::Num(v2)) => Val::Num(v1 - v2),
 
@@ -305,6 +310,7 @@
 	})
 }
 
+#[inline(always)]
 pub fn evaluate(context: Context, expr: &LocExpr) -> Result<Val> {
 	use Expr::*;
 	let locexpr = expr.clone();
@@ -356,11 +362,15 @@
 						create_error(crate::Error::NoSuchField(s))?
 					}
 				}
-				(Val::Arr(v), Val::Num(n)) => v
-					.get(n as usize)
-					.unwrap_or_else(|| panic!("out of bounds"))
-					.clone()
-					.unwrap_if_lazy()?,
+				(Val::Arr(v), Val::Num(n)) => {
+					if n.fract() > f64::EPSILON {
+						create_error(crate::Error::FractionalIndex)?
+					}
+					v.get(n as usize)
+						.unwrap_or_else(|| panic!("out of bounds"))
+						.clone()
+						.unwrap_if_lazy()?
+				}
 				(Val::Str(s), Val::Num(n)) => {
 					Val::Str(s.chars().skip(n as usize).take(1).collect())
 				}
@@ -511,29 +521,52 @@
 							panic!("bad trace call");
 						}
 					}
+					("std", "pow") => {
+						assert_eq!(args.len(), 2);
+						if let (Val::Num(a), Val::Num(b)) = (
+							evaluate(context.clone(), &args[0].1)?,
+							evaluate(context, &args[1].1)?,
+						) {
+							Val::Num(a.powf(b))
+						} else {
+							panic!("bad pow call");
+						}
+					}
 					(ns, name) => panic!("Intristic not found: {}.{}", ns, name),
 				},
-				Val::Func(f) => push(locexpr, "function call".to_owned(), || {
-					f.evaluate(
-						args.clone()
-							.into_iter()
-							.map(move |a| {
-								(
-									a.clone().0,
-									if *tailstrict {
-										Val::Lazy(LazyVal::new_resolved(
-											evaluate(context.clone(), &a.1).unwrap(),
+				Val::Func(f) => {
+					let body = #[inline(always)]
+					|| {
+						f.evaluate(
+							args.clone()
+								.into_iter()
+								.map(
+									#[inline(always)]
+									move |a| {
+										Ok((
+											a.clone().0,
+											if *tailstrict {
+												Val::Lazy(LazyVal::new_resolved(evaluate(
+													context.clone(),
+													&a.1,
+												)?))
+											} else {
+												Val::Lazy(lazy_val!(
+													closure!(clone context, clone a, || evaluate(context.clone(), &a.clone().1))
+												))
+											},
 										))
-									} else {
-										Val::Lazy(lazy_val!(
-											closure!(clone context, clone a, || evaluate(context.clone(), &a.clone().1))
-										))
 									},
 								)
-							})
-							.collect(),
-					)
-				})?,
+								.collect::<Result<Vec<_>>>()?,
+						)
+					};
+					if *tailstrict {
+						body()?
+					} else {
+						push(locexpr, "function call".to_owned(), body)?
+					}
+				}
 				_ => panic!("{:?} is not a function", value),
 			}
 		}
@@ -578,9 +611,7 @@
 				)?
 			} else {
 				match cond_else {
-					Some(v) => push(v.clone(), "if condition 'else' branch".to_owned(), || {
-						evaluate(context, v)
-					})?,
+					Some(v) => evaluate(context, v)?,
 					None => Val::Null,
 				}
 			}
modifiedcrates/jsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/lib.rs
+++ b/crates/jsonnet-evaluator/src/lib.rs
@@ -2,6 +2,7 @@
 #![feature(type_alias_impl_trait)]
 #![feature(debug_non_exhaustive)]
 #![allow(macro_expanded_macro_exports_accessed_by_absolute_paths)]
+#![feature(stmt_expr_attributes)]
 mod ctx;
 mod dynamic;
 mod error;
@@ -51,6 +52,19 @@
 	}
 }
 
+pub struct EvaluationSettings {
+	max_stack_frames: usize,
+	max_stack_trace_size: usize,
+}
+impl Default for EvaluationSettings {
+	fn default() -> Self {
+		EvaluationSettings {
+			max_stack_frames: 500,
+			max_stack_trace_size: 20,
+		}
+	}
+}
+
 pub struct FileData(String, LocExpr, Option<Val>);
 #[derive(Default)]
 pub struct EvaluationStateInternals {
@@ -60,21 +74,31 @@
 	/// printing stacktraces
 	files: RefCell<HashMap<PathBuf, FileData>>,
 	globals: RefCell<HashMap<String, Val>>,
+
+	settings: EvaluationSettings,
 }
 
 thread_local! {
-	pub static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)
+	/// Contains state for currently executing file
+	/// Global state is fine there
+	pub(crate) static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)
 }
+#[inline(always)]
 pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {
-	EVAL_STATE.with(|s| f(s.borrow().as_ref().unwrap()))
+	EVAL_STATE.with(
+		#[inline(always)]
+		|s| f(s.borrow().as_ref().unwrap()),
+	)
 }
 pub(crate) fn create_error<T>(err: Error) -> Result<T> {
 	with_state(|s| s.error(err))
 }
+#[inline(always)]
 pub(crate) fn push<T>(e: LocExpr, comment: String, f: impl FnOnce() -> Result<T>) -> Result<T> {
 	with_state(|s| s.push(e, comment, f))
 }
 
+/// Maintains stack trace and import resolution
 #[derive(Default, Clone)]
 pub struct EvaluationState(Rc<EvaluationStateInternals>);
 impl EvaluationState {
@@ -189,10 +213,11 @@
 		Context::new().extend(new_bindings, None, None, None)
 	}
 
+	#[inline(always)]
 	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() > 500 {
+			if stack.len() > self.0.settings.max_stack_frames {
 				drop(stack);
 				return self.error(Error::StackOverflow);
 			} else {
@@ -209,7 +234,16 @@
 		}
 	}
 	pub fn stack_trace(&self) -> StackTrace {
-		StackTrace(self.0.stack.borrow().iter().rev().cloned().collect())
+		StackTrace(
+			self.0
+				.stack
+				.borrow()
+				.iter()
+				.rev()
+				.take(self.0.settings.max_stack_trace_size)
+				.cloned()
+				.collect(),
+		)
 	}
 	pub fn error<T>(&self, err: Error) -> Result<T> {
 		Err(LocError(err, self.stack_trace()))
modifiedcrates/jsonnet-evaluator/src/val.rsdiffbeforeafterboth
before · crates/jsonnet-evaluator/src/val.rs
1use crate::{2	create_error, Context, Error, FunctionDefault, FunctionRhs, LazyBinding, ObjValue, Result,3};4use closure::closure;5use jsonnet_parser::{Param, ParamsDesc};6use std::{7	cell::RefCell,8	collections::HashMap,9	fmt::{Debug, Display},10	rc::Rc,11};1213struct LazyValInternals {14	pub f: Option<Box<dyn Fn() -> Result<Val>>>,15	pub cached: RefCell<Option<Val>>,16}17#[derive(Clone)]18pub struct LazyVal(Rc<LazyValInternals>);19impl LazyVal {20	pub fn new(f: Box<dyn Fn() -> Result<Val>>) -> Self {21		LazyVal(Rc::new(LazyValInternals {22			f: Some(f),23			cached: RefCell::new(None),24		}))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	}32	pub fn evaluate(&self) -> Result<Val> {33		{34			let cached = self.0.cached.borrow();35			if cached.is_some() {36				return Ok(cached.clone().unwrap());37			}38		}39		let result = (self.0.f.as_ref().unwrap())()?;40		self.0.cached.borrow_mut().replace(result.clone());41		Ok(result)42	}43}4445#[macro_export]46macro_rules! lazy_val {47	($f: expr) => {48		$crate::LazyVal::new(Box::new($f))49	};50}51#[macro_export]52macro_rules! resolved_lazy_val {53	($f: expr) => {54		$crate::LazyVal::new_resolved($f)55	};56}57impl Debug for LazyVal {58	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {59		if self.0.cached.borrow().is_some() {60			write!(f, "{:?}", self.0.cached.borrow().clone().unwrap())61		} else {62			write!(f, "Lazy")63		}64	}65}66impl PartialEq for LazyVal {67	fn eq(&self, other: &Self) -> bool {68		Rc::ptr_eq(&self.0, &other.0)69	}70}7172#[derive(Debug, PartialEq, Clone)]73pub struct FuncDesc {74	pub ctx: Context,75	pub params: ParamsDesc,76	pub eval_rhs: FunctionRhs,77	pub eval_default: FunctionDefault,78}79impl FuncDesc {80	// TODO: Check for unset variables81	pub fn evaluate(&self, args: Vec<(Option<String>, Val)>) -> Result<Val> {82		let mut new_bindings: HashMap<String, LazyBinding> = HashMap::new();83		let future_ctx = Context::new_future();8485		for Param(name, default) in self.params.with_defaults() {86			let default = default.unwrap();87			let eval_default = self.eval_default.clone();88			new_bindings.insert(89				name,90				LazyBinding::Bound(lazy_val!(91					closure!(clone future_ctx, clone eval_default, clone default, || (eval_default.clone()).092					(future_ctx.clone().unwrap(), default.clone()))93				)),94			);95		}96		for (name, val) in args.clone().into_iter().filter(|e| e.0.is_some()) {97			new_bindings.insert(98				name.as_ref().unwrap().clone(),99				LazyBinding::Bound(resolved_lazy_val!(val.clone())),100			);101		}102		for (i, param) in self.params.0.iter().enumerate() {103			if let Some((None, val)) = args.get(i) {104				new_bindings.insert(105					param.0.clone(),106					LazyBinding::Bound(resolved_lazy_val!(val.clone())),107				);108			}109		}110		let ctx = self111			.ctx112			.extend(new_bindings, None, None, None)?113			.into_future(future_ctx);114		self.eval_rhs.0(ctx)115	}116}117118#[derive(Debug)]119pub enum ValType {120	Bool,121	Null,122	Str,123	Num,124	Arr,125	Obj,126	Func,127}128impl ValType {129	pub fn name(&self) -> &'static str {130		use ValType::*;131		match self {132			Bool => "boolean",133			Null => "null",134			Str => "string",135			Num => "number",136			Arr => "array",137			Obj => "object",138			Func => "function",139		}140	}141}142impl Display for ValType {143	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {144		write!(f, "{}", self.name())145	}146}147148#[derive(Debug, PartialEq, Clone)]149pub enum Val {150	Bool(bool),151	Null,152	Str(String),153	Num(f64),154	Lazy(LazyVal),155	Arr(Vec<Val>),156	Obj(ObjValue),157	Func(FuncDesc),158159	// Library functions implemented in native160	Intristic(String, String),161}162impl Val {163	pub fn try_cast_bool(self, context: &'static str) -> Result<bool> {164		match self.unwrap_if_lazy()? {165			Val::Bool(v) => Ok(v),166			v => create_error(Error::TypeMismatch(167				context,168				vec![ValType::Bool],169				v.value_type()?,170			)),171		}172	}173	pub fn try_cast_str(self, context: &'static str) -> Result<String> {174		match self.unwrap_if_lazy()? {175			Val::Str(v) => Ok(v),176			v => create_error(Error::TypeMismatch(177				context,178				vec![ValType::Str],179				v.value_type()?,180			)),181		}182	}183	pub fn unwrap_if_lazy(self) -> Result<Self> {184		Ok(if let Val::Lazy(v) = self {185			v.evaluate()?.unwrap_if_lazy()?186		} else {187			self188		})189	}190	pub fn value_type(&self) -> Result<ValType> {191		Ok(match self {192			Val::Str(..) => ValType::Str,193			Val::Num(..) => ValType::Num,194			Val::Arr(..) => ValType::Arr,195			Val::Obj(..) => ValType::Obj,196			Val::Func(..) => ValType::Func,197			Val::Bool(_) => ValType::Bool,198			Val::Null => ValType::Null,199			Val::Intristic(_, _) => ValType::Func,200			Val::Lazy(_) => self.clone().unwrap_if_lazy()?.value_type()?,201		})202	}203}