difftreelog
perf(evaluator) emove expr from stacktraces
in: master
4 files changed
cmds/jrsonnet/src/main.rsdiffbeforeafterboth--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -271,10 +271,7 @@
};
for item in trace.0.iter() {
let desc = &item.1;
- if (item.0).1.is_none() {
- continue;
- }
- let source = (item.0).1.clone().unwrap();
+ let source = item.0.clone();
let code = evaluator.get_source(&source.0);
if code.is_none() {
continue;
crates/jsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/error.rs
+++ b/crates/jsonnet-evaluator/src/error.rs
@@ -1,6 +1,6 @@
use crate::ValType;
-use jsonnet_parser::LocExpr;
-use std::path::PathBuf;
+use jsonnet_parser::ExprLocation;
+use std::{path::PathBuf, rc::Rc};
#[derive(Debug, Clone)]
pub enum Error {
@@ -38,7 +38,7 @@
}
#[derive(Clone, Debug)]
-pub struct StackTraceElement(pub LocExpr, pub String);
+pub struct StackTraceElement(pub Rc<ExprLocation>, pub String);
#[derive(Debug, Clone)]
pub struct StackTrace(pub Vec<StackTraceElement>);
crates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/evaluate.rs
+++ b/crates/jsonnet-evaluator/src/evaluate.rs
@@ -35,7 +35,7 @@
b.name.clone(),
LazyBinding::Bindable(Rc::new(move |this, super_obj| {
Ok(lazy_val!(closure!(clone context_creator, clone b, ||
- push(b.value.clone(), "thunk".to_owned(), ||{
+ push(&b.value.1, "thunk", ||{
evaluate(
context_creator.0(this.clone(), super_obj.clone())?,
&b.value
@@ -255,7 +255,7 @@
visibility: visibility.clone(),
invoke: LazyBinding::Bindable(Rc::new(
closure!(clone name, clone value, clone context_creator, |this, super_obj| {
- Ok(LazyVal::new_resolved(push(value.clone(), "object ".to_owned()+&name+" field", ||{
+ Ok(LazyVal::new_resolved(push(&value.1, "object field", ||{
let context = context_creator.0(this, super_obj)?;
evaluate(
context,
@@ -371,7 +371,6 @@
pub fn evaluate(context: Context, expr: &LocExpr) -> Result<Val> {
use Expr::*;
- let locexpr = expr.clone();
let LocExpr(expr, loc) = expr;
Ok(match &**expr {
Literal(LiteralType::This) => Val::Obj(
@@ -394,7 +393,7 @@
Num(v) => Val::Num(*v),
BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, &v1, *o, &v2)?,
UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,
- Var(name) => push(locexpr, "var".to_owned(), || {
+ Var(name) => push(loc, "var", || {
Val::Lazy(context.binding(&name)?).unwrap_if_lazy()
})?,
Index(LocExpr(v, _), index) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {
@@ -733,7 +732,7 @@
if *tailstrict {
body()?
} else {
- push(locexpr, "function call".to_owned(), body)?
+ push(loc, "function call", body)?
}
}
_ => panic!("{:?} is not a function", value),
@@ -741,16 +740,12 @@
}
Function(params, body) => evaluate_method(context, params.clone(), body.clone()),
AssertExpr(AssertStmt(value, msg), returned) => {
- let assertion_result = push(value.clone(), "assertion condition".to_owned(), || {
+ let assertion_result = push(&value.1, "assertion condition", || {
evaluate(context.clone(), &value)?
.try_cast_bool("assertion condition should be boolean")
})?;
if assertion_result {
- push(
- returned.clone(),
- "assert 'return' branch".to_owned(),
- || evaluate(context, returned),
- )?
+ evaluate(context, returned)?
} else if let Some(msg) = msg {
panic!(
"assertion failed ({:?}): {}",
crates/jsonnet-evaluator/src/lib.rsdiffbeforeafterboth88pub(crate) fn create_error<T>(err: Error) -> Result<T> {86pub(crate) fn create_error<T>(err: Error) -> Result<T> {89 with_state(|s| s.error(err))87 with_state(|s| s.error(err))90}88}91pub(crate) fn push<T>(e: LocExpr, comment: String, f: impl FnOnce() -> Result<T>) -> Result<T> {89pub(crate) fn push<T>(90 e: &Option<Rc<ExprLocation>>,91 comment: &str,92 f: impl FnOnce() -> Result<T>,93) -> Result<T> {94 if e.is_some() {92 with_state(|s| s.push(e, comment, f))95 with_state(|s| s.push(e.clone().unwrap(), comment.to_owned(), f))96 } else {97 f()98 }93}99}9410095/// Maintains stack trace and import resolution101/// Maintains stack trace and import resolution247 Context::new().extend_unbound(new_bindings, None, None, None)253 Context::new().extend_unbound(new_bindings, None, None, None)248 }254 }249255250 pub fn push<T>(&self, e: LocExpr, comment: String, f: impl FnOnce() -> Result<T>) -> Result<T> {256 pub fn push<T>(257 &self,258 e: Rc<ExprLocation>,259 comment: String,260 f: impl FnOnce() -> Result<T>,261 ) -> Result<T> {302 use super::Val;313 use super::Val;303 use crate::EvaluationState;314 use crate::EvaluationState;304 use jsonnet_parser::*;315 use jsonnet_parser::*;305 use std::path::PathBuf;316 use std::{path::PathBuf, rc::Rc};306317307 #[test]318 #[test]308 fn eval_state_stacktrace() {319 fn eval_state_stacktrace() {309 let state = EvaluationState::default();320 let state = EvaluationState::default();310 state321 state311 .push(322 .push(312 loc_expr!(313 Expr::Num(0.0),323 Rc::new(ExprLocation(PathBuf::from("test1.jsonnet"), 10, 20)),314 true,315 (PathBuf::from("test1.jsonnet"), 10, 20)316 ),317 "outer".to_owned(),324 "outer".to_owned(),318 || {325 || {319 state.push(326 state.push(320 loc_expr!(321 Expr::Num(0.0),327 Rc::new(ExprLocation(PathBuf::from("test2.jsonnet"), 30, 40)),322 true,323 (PathBuf::from("test2.jsonnet"), 30, 40)324 ),325 "inner".to_owned(),328 "inner".to_owned(),326 || {329 || {327 state.print_stack_trace();330 state.print_stack_trace();