git.delta.rocks / jrsonnet / refs/commits / 90cacba70c63

difftreelog

perf(evaluator) emove expr from stacktraces

Лач2020-06-25parent: #f5b0724.patch.diff
in: master

4 files changed

modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
271 };271 };
272 for item in trace.0.iter() {272 for item in trace.0.iter() {
273 let desc = &item.1;273 let desc = &item.1;
274 if (item.0).1.is_none() {
275 continue;
276 }
277 let source = (item.0).1.clone().unwrap();274 let source = item.0.clone();
278 let code = evaluator.get_source(&source.0);275 let code = evaluator.get_source(&source.0);
279 if code.is_none() {276 if code.is_none() {
280 continue;277 continue;
modifiedcrates/jsonnet-evaluator/src/error.rsdiffbeforeafterboth
1use crate::ValType;1use crate::ValType;
2use jsonnet_parser::LocExpr;2use jsonnet_parser::ExprLocation;
3use std::path::PathBuf;3use std::{path::PathBuf, rc::Rc};
44
5#[derive(Debug, Clone)]5#[derive(Debug, Clone)]
6pub enum Error {6pub enum Error {
38}38}
3939
40#[derive(Clone, Debug)]40#[derive(Clone, Debug)]
41pub struct StackTraceElement(pub LocExpr, pub String);41pub struct StackTraceElement(pub Rc<ExprLocation>, pub String);
42#[derive(Debug, Clone)]42#[derive(Debug, Clone)]
43pub struct StackTrace(pub Vec<StackTraceElement>);43pub struct StackTrace(pub Vec<StackTraceElement>);
4444
modifiedcrates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
35 b.name.clone(),35 b.name.clone(),
36 LazyBinding::Bindable(Rc::new(move |this, super_obj| {36 LazyBinding::Bindable(Rc::new(move |this, super_obj| {
37 Ok(lazy_val!(closure!(clone context_creator, clone b, ||37 Ok(lazy_val!(closure!(clone context_creator, clone b, ||
38 push(b.value.clone(), "thunk".to_owned(), ||{38 push(&b.value.1, "thunk", ||{
39 evaluate(39 evaluate(
40 context_creator.0(this.clone(), super_obj.clone())?,40 context_creator.0(this.clone(), super_obj.clone())?,
41 &b.value41 &b.value
255 visibility: visibility.clone(),255 visibility: visibility.clone(),
256 invoke: LazyBinding::Bindable(Rc::new(256 invoke: LazyBinding::Bindable(Rc::new(
257 closure!(clone name, clone value, clone context_creator, |this, super_obj| {257 closure!(clone name, clone value, clone context_creator, |this, super_obj| {
258 Ok(LazyVal::new_resolved(push(value.clone(), "object ".to_owned()+&name+" field", ||{258 Ok(LazyVal::new_resolved(push(&value.1, "object field", ||{
259 let context = context_creator.0(this, super_obj)?;259 let context = context_creator.0(this, super_obj)?;
260 evaluate(260 evaluate(
261 context,261 context,
371371
372pub fn evaluate(context: Context, expr: &LocExpr) -> Result<Val> {372pub fn evaluate(context: Context, expr: &LocExpr) -> Result<Val> {
373 use Expr::*;373 use Expr::*;
374 let locexpr = expr.clone();
375 let LocExpr(expr, loc) = expr;374 let LocExpr(expr, loc) = expr;
376 Ok(match &**expr {375 Ok(match &**expr {
377 Literal(LiteralType::This) => Val::Obj(376 Literal(LiteralType::This) => Val::Obj(
394 Num(v) => Val::Num(*v),393 Num(v) => Val::Num(*v),
395 BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, &v1, *o, &v2)?,394 BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, &v1, *o, &v2)?,
396 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,395 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,
397 Var(name) => push(locexpr, "var".to_owned(), || {396 Var(name) => push(loc, "var", || {
398 Val::Lazy(context.binding(&name)?).unwrap_if_lazy()397 Val::Lazy(context.binding(&name)?).unwrap_if_lazy()
399 })?,398 })?,
400 Index(LocExpr(v, _), index) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {399 Index(LocExpr(v, _), index) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {
733 if *tailstrict {732 if *tailstrict {
734 body()?733 body()?
735 } else {734 } else {
736 push(locexpr, "function call".to_owned(), body)?735 push(loc, "function call", body)?
737 }736 }
738 }737 }
739 _ => panic!("{:?} is not a function", value),738 _ => panic!("{:?} is not a function", value),
740 }739 }
741 }740 }
742 Function(params, body) => evaluate_method(context, params.clone(), body.clone()),741 Function(params, body) => evaluate_method(context, params.clone(), body.clone()),
743 AssertExpr(AssertStmt(value, msg), returned) => {742 AssertExpr(AssertStmt(value, msg), returned) => {
744 let assertion_result = push(value.clone(), "assertion condition".to_owned(), || {743 let assertion_result = push(&value.1, "assertion condition", || {
745 evaluate(context.clone(), &value)?744 evaluate(context.clone(), &value)?
746 .try_cast_bool("assertion condition should be boolean")745 .try_cast_bool("assertion condition should be boolean")
747 })?;746 })?;
748 if assertion_result {747 if assertion_result {
749 push(
750 returned.clone(),
751 "assert 'return' branch".to_owned(),
752 || evaluate(context, returned),748 evaluate(context, returned)?
753 )?
754 } else if let Some(msg) = msg {749 } else if let Some(msg) = msg {
755 panic!(750 panic!(
756 "assertion failed ({:?}): {}",751 "assertion failed ({:?}): {}",
modifiedcrates/jsonnet-evaluator/src/lib.rsdiffbeforeafterboth
88pub(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}
94100
95/// Maintains stack trace and import resolution101/// Maintains stack trace and import resolution
247 Context::new().extend_unbound(new_bindings, None, None, None)253 Context::new().extend_unbound(new_bindings, None, None, None)
248 }254 }
249255
250 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};
306317
307 #[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 state
311 .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();