difftreelog
feat breakpoints
in: master
9 files changed
crates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/manifest.rs
@@ -1,5 +1,6 @@
use crate::error::Error::*;
use crate::error::Result;
+use crate::push_frame;
use crate::{throw, Val};
#[derive(PartialEq, Clone, Copy)]
@@ -102,12 +103,13 @@
buf.push_str(cur_padding);
escape_string_json_buf(&field, buf);
buf.push_str(": ");
- crate::push(
+ push_frame(
None,
|| format!("field <{}> manifestification", field.clone()),
|| {
let value = obj.get(field.clone())?.unwrap();
- manifest_json_ex_buf(&value, buf, cur_padding, options)
+ manifest_json_ex_buf(&value, buf, cur_padding, options)?;
+ Ok(Val::Null)
},
)?;
}
crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -3,8 +3,8 @@
equals,
error::{Error::*, Result},
operator::evaluate_mod_op,
- parse_args, primitive_equals, push, throw, with_state, ArrValue, Context, EvaluationState,
- FuncVal, IndexableVal, LazyVal, Val,
+ parse_args, primitive_equals, push_frame, throw, with_state, ArrValue, Context,
+ EvaluationState, FuncVal, IndexableVal, LazyVal, Val,
};
use format::{format_arr, format_obj};
use jrsonnet_gc::Gc;
@@ -23,7 +23,7 @@
pub mod sort;
pub fn std_format(str: IStr, vals: Val) -> Result<Val> {
- push(
+ push_frame(
Some(&ExprLocation(Rc::from(PathBuf::from("std.jsonnet")), 0, 0)),
|| format!("std.format of {}", str),
|| {
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth1use crate::{2 builtin::{format::FormatError, sort::SortError},3 typed::TypeLocError,4};5use jrsonnet_gc::Trace;6use jrsonnet_interner::IStr;7use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};8use jrsonnet_types::ValType;9use std::{10 path::{Path, PathBuf},11 rc::Rc,12};13use thiserror::Error;1415#[derive(Error, Debug, Clone, Trace)]16#[trivially_drop]17pub enum Error {18 #[error("intrinsic not found: {0}")]19 IntrinsicNotFound(IStr),20 #[error("argument reordering in intrisics not supported yet")]21 IntrinsicArgumentReorderingIsNotSupportedYet,2223 #[error("operator {0} does not operate on type {1}")]24 UnaryOperatorDoesNotOperateOnType(UnaryOpType, ValType),25 #[error("binary operation {1} {0} {2} is not implemented")]26 BinaryOperatorDoesNotOperateOnValues(BinaryOpType, ValType, ValType),2728 #[error("no top level object in this context")]29 NoTopLevelObjectFound,30 #[error("self is only usable inside objects")]31 CantUseSelfOutsideOfObject,32 #[error("no super found")]33 NoSuperFound,3435 #[error("for loop can only iterate over arrays")]36 InComprehensionCanOnlyIterateOverArray,3738 #[error("array out of bounds: {0} is not within [0,{1})")]39 ArrayBoundsError(usize, usize),4041 #[error("assert failed: {0}")]42 AssertionFailed(IStr),4344 #[error("variable is not defined: {0}")]45 VariableIsNotDefined(IStr),46 #[error("type mismatch: expected {}, got {2} {0}", .1.iter().map(|e| format!("{}", e)).collect::<Vec<_>>().join(", "))]47 TypeMismatch(&'static str, Vec<ValType>, ValType),48 #[error("no such field: {0}")]49 NoSuchField(IStr),5051 #[error("only functions can be called, got {0}")]52 OnlyFunctionsCanBeCalledGot(ValType),53 #[error("parameter {0} is not defined")]54 UnknownFunctionParameter(String),55 #[error("argument {0} is already bound")]56 BindingParameterASecondTime(IStr),57 #[error("too many args, function has {0}")]58 TooManyArgsFunctionHas(usize),59 #[error("function argument is not passed: {0}")]60 FunctionParameterNotBoundInCall(IStr),6162 #[error("external variable is not defined: {0}")]63 UndefinedExternalVariable(IStr),64 #[error("native is not defined: {0}")]65 UndefinedExternalFunction(IStr),6667 #[error("field name should be string, got {0}")]68 FieldMustBeStringGot(ValType),6970 #[error("attempted to index array with string {0}")]71 AttemptedIndexAnArrayWithString(IStr),72 #[error("{0} index type should be {1}, got {2}")]73 ValueIndexMustBeTypeGot(ValType, ValType, ValType),74 #[error("cant index into {0}")]75 CantIndexInto(ValType),76 #[error("{0} is not indexable")]77 ValueIsNotIndexable(ValType),7879 #[error("super can't be used standalone")]80 StandaloneSuper,8182 #[error("can't resolve {1} from {0}")]83 ImportFileNotFound(PathBuf, PathBuf),84 #[error("resolved file not found: {0}")]85 ResolvedFileNotFound(PathBuf),86 #[error("imported file is not valid utf-8: {0:?}")]87 ImportBadFileUtf8(PathBuf),88 #[error("tried to import {1} from {0}, but imports is not supported")]89 ImportNotSupported(PathBuf, PathBuf),90 #[error(91 "syntax error, expected one of {}, got {:?}",92 .error.expected,93 .source_code.chars().nth(error.location.offset).map(|c| c.to_string()).unwrap_or_else(|| "EOF".into())94 )]95 ImportSyntaxError {96 path: Rc<Path>,97 source_code: IStr,98 #[unsafe_ignore_trace]99 error: Box<jrsonnet_parser::ParseError>,100 },101102 #[error("runtime error: {0}")]103 RuntimeError(IStr),104 #[error("stack overflow, try to reduce recursion, or set --max-stack to bigger value")]105 StackOverflow,106 #[error("infinite recursion detected")]107 RecursiveLazyValueEvaluation,108 #[error("tried to index by fractional value")]109 FractionalIndex,110 #[error("attempted to divide by zero")]111 DivisionByZero,112113 #[error("string manifest output is not an string")]114 StringManifestOutputIsNotAString,115 #[error("stream manifest output is not an array")]116 StreamManifestOutputIsNotAArray,117 #[error("multi manifest output is not an object")]118 MultiManifestOutputIsNotAObject,119120 #[error("cant recurse stream manifest")]121 StreamManifestOutputCannotBeRecursed,122 #[error("stream manifest output cannot consist of raw strings")]123 StreamManifestCannotNestString,124125 #[error("{0}")]126 ImportCallbackError(String),127 #[error("invalid unicode codepoint: {0}")]128 InvalidUnicodeCodepointGot(u32),129130 #[error("format error: {0}")]131 Format(#[from] FormatError),132 #[error("type error: {0}")]133 TypeError(TypeLocError),134 #[error("sort error: {0}")]135 Sort(#[from] SortError),136137 #[cfg(feature = "anyhow-error")]138 #[error(transparent)]139 Other(Rc<anyhow::Error>),140}141142#[cfg(feature = "anyhow-error")]143impl From<anyhow::Error> for LocError {144 fn from(e: anyhow::Error) -> Self {145 Self::new(Error::Other(Rc::new(e)))146 }147}148149impl From<Error> for LocError {150 fn from(e: Error) -> Self {151 Self::new(e)152 }153}154155#[derive(Clone, Debug, Trace)]156#[trivially_drop]157pub struct StackTraceElement {158 pub location: Option<ExprLocation>,159 pub desc: String,160}161#[derive(Debug, Clone, Trace)]162#[trivially_drop]163pub struct StackTrace(pub Vec<StackTraceElement>);164165#[derive(Debug, Clone, Trace)]166#[trivially_drop]167pub struct LocError(Box<(Error, StackTrace)>);168impl LocError {169 pub fn new(e: Error) -> Self {170 Self(Box::new((e, StackTrace(vec![]))))171 }172173 pub const fn error(&self) -> &Error {174 &(self.0).0175 }176 pub fn error_mut(&mut self) -> &mut Error {177 &mut (self.0).0178 }179 pub const fn trace(&self) -> &StackTrace {180 &(self.0).1181 }182 pub fn trace_mut(&mut self) -> &mut StackTrace {183 &mut (self.0).1184 }185}186187pub type Result<V> = std::result::Result<V, LocError>;188189#[macro_export]190macro_rules! throw {191 ($e: expr) => {192 return Err($e.into())193 };194}crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -2,7 +2,7 @@
builtin::std_slice,
error::Error::*,
evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},
- push, throw, with_state, ArrValue, Bindable, Context, ContextCreator, FuncDesc, FuncVal,
+ push_frame, throw, with_state, ArrValue, Bindable, Context, ContextCreator, FuncDesc, FuncVal,
FutureWrapper, LazyBinding, LazyVal, LazyValValue, ObjValue, ObjValueBuilder, ObjectAssertion,
Result, Val,
};
@@ -464,7 +464,7 @@
if tailstrict {
body()?
} else {
- push(loc, || format!("function <{}> call", f.name()), body)?
+ push_frame(loc, || format!("function <{}> call", f.name()), body)?
}
}
v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),
@@ -474,7 +474,7 @@
pub fn evaluate_assert(context: Context, assertion: &AssertStmt) -> Result<()> {
let value = &assertion.0;
let msg = &assertion.1;
- let assertion_result = push(
+ let assertion_result = push_frame(
value.1.as_ref(),
|| "assertion condition".to_owned(),
|| {
@@ -483,7 +483,7 @@
},
)?;
if !assertion_result {
- push(
+ push_frame(
value.1.as_ref(),
|| "assertion failure".to_owned(),
|| {
@@ -510,6 +510,7 @@
pub fn evaluate(context: Context, expr: &LocExpr) -> Result<Val> {
use Expr::*;
let LocExpr(expr, loc) = expr;
+ // let bp = with_state(|s| s.0.stop_at.borrow().clone());
Ok(match &**expr {
Literal(LiteralType::This) => {
Val::Obj(context.this().clone().ok_or(CantUseSelfOutsideOfObject)?)
@@ -532,7 +533,7 @@
Num(v) => Val::new_checked_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(
+ Var(name) => push_frame(
loc.as_ref(),
|| format!("variable <{}>", name),
|| context.binding(name.clone())?.evaluate(),
@@ -541,7 +542,7 @@
match (evaluate(context.clone(), value)?, evaluate(context, index)?) {
(Val::Obj(v), Val::Str(s)) => {
let sn = s.clone();
- push(
+ push_frame(
loc.as_ref(),
|| format!("field <{}> access", sn),
|| {
@@ -652,7 +653,7 @@
evaluate_assert(context.clone(), assert)?;
evaluate(context, returned)?
}
- ErrorStmt(e) => push(
+ ErrorStmt(e) => push_frame(
loc.as_ref(),
|| "error statement".to_owned(),
|| {
@@ -666,7 +667,7 @@
cond_then,
cond_else,
} => {
- if push(
+ if push_frame(
loc.as_ref(),
|| "if condition".to_owned(),
|| evaluate(context.clone(), &cond.0)?.try_cast_bool("in if condition"),
@@ -708,7 +709,7 @@
.0;
let mut import_location = tmp.to_path_buf();
import_location.pop();
- push(
+ push_frame(
loc.as_ref(),
|| format!("import {:?}", path),
|| with_state(|s| s.import_file(&import_location, path)),
crates/jrsonnet-evaluator/src/function.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function.rs
+++ b/crates/jrsonnet-evaluator/src/function.rs
@@ -243,7 +243,7 @@
($ctx: expr, $fn_name: expr, $args: expr, $total_args: expr, [
$($id: expr, $name: ident: $ty: expr $(=>$match: path)?);+ $(;)?
], $handler:block) => {{
- use $crate::{error::Error::*, throw, evaluate, push_stack_frame, typed::CheckType};
+ use $crate::{error::Error::*, throw, evaluate, push_frame, typed::CheckType};
let args = $args;
if args.unnamed.len() + args.named.len() > $total_args {
@@ -263,7 +263,7 @@
} else {
&$args.unnamed[$id]
};
- let $name = push_stack_frame(None, || format!("evaluating argument"), || {
+ let $name = push_frame(None, || format!("evaluating argument"), || {
let value = evaluate($ctx.clone(), &$name)?;
$ty.check(&value)?;
Ok(value)
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -40,7 +40,7 @@
path::{Path, PathBuf},
rc::Rc,
};
-use trace::{offset_to_location, CodeLocation, CompactFormat, TraceFormat};
+use trace::{location_to_offset, offset_to_location, CodeLocation, CompactFormat, TraceFormat};
pub use val::*;
pub trait Bindable: Trace {
@@ -109,6 +109,10 @@
struct EvaluationData {
/// Used for stack overflow detection, stacktrace is populated on unwind
stack_depth: usize,
+ /// Updated every time stack entry is popt
+ stack_generation: usize,
+
+ breakpoints: Breakpoints,
/// Contains file source codes and evaluation results for imports and pretty-printed stacktraces
files: HashMap<Rc<Path>, FileData>,
str_files: HashMap<Rc<Path>, IStr>,
@@ -119,6 +123,38 @@
parsed: LocExpr,
evaluated: Option<Val>,
}
+
+pub struct Breakpoint {
+ loc: ExprLocation,
+ collected: RefCell<HashMap<usize, (usize, Vec<Result<Val>>)>>,
+}
+#[derive(Default)]
+struct Breakpoints(Vec<Rc<Breakpoint>>);
+impl Breakpoints {
+ fn insert(
+ &self,
+ stack_depth: usize,
+ stack_generation: usize,
+ loc: &ExprLocation,
+ result: Result<Val>,
+ ) -> Result<Val> {
+ if self.0.is_empty() {
+ return result;
+ }
+ for item in self.0.iter() {
+ if item.loc.belongs_to(loc) {
+ let mut collected = item.collected.borrow_mut();
+ let (depth, vals) = collected.entry(stack_generation).or_default();
+ if stack_depth > *depth {
+ vals.clear();
+ }
+ vals.push(result.clone());
+ }
+ }
+ result
+ }
+}
+
#[derive(Default)]
pub struct EvaluationStateInternals {
/// Internal state
@@ -135,7 +171,7 @@
pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {
EVAL_STATE.with(|s| f(s.borrow().as_ref().unwrap()))
}
-pub(crate) fn push<T>(
+pub(crate) fn push_frame<T>(
e: Option<&ExprLocation>,
frame_desc: impl FnOnce() -> String,
f: impl FnOnce() -> Result<T>,
@@ -143,12 +179,12 @@
with_state(|s| s.push(e, frame_desc, f))
}
-pub fn push_stack_frame<T>(
+pub(crate) fn push_val_frame(
e: Option<&ExprLocation>,
frame_desc: impl FnOnce() -> String,
- f: impl FnOnce() -> Result<T>,
-) -> Result<T> {
- push(e, frame_desc, f)
+ f: impl FnOnce() -> Result<Val>,
+) -> Result<Val> {
+ with_state(|s| s.push(e, frame_desc, f))
}
/// Maintains stack trace and import resolution
@@ -178,6 +214,15 @@
Ok(())
}
+ pub fn reset_evaluation_state(&self, name: &Path) {
+ self.data_mut()
+ .files
+ .get_mut(name)
+ .unwrap()
+ .evaluated
+ .take();
+ }
+
/// Adds file by source code and parsed expr
pub fn add_parsed_file(
&self,
@@ -203,8 +248,15 @@
pub fn map_source_locations(&self, file: &Path, locs: &[usize]) -> Vec<CodeLocation> {
offset_to_location(&self.get_source(file).unwrap(), locs)
}
-
- pub(crate) fn import_file(&self, from: &Path, path: &Path) -> Result<Val> {
+ pub fn map_from_source_location(
+ &self,
+ file: &Path,
+ line: usize,
+ column: usize,
+ ) -> Option<usize> {
+ location_to_offset(&self.get_source(file).unwrap(), line, column)
+ }
+ pub fn import_file(&self, from: &Path, path: &Path) -> Result<Val> {
let file_path = self.resolve_file(from, path)?;
{
let data = self.data();
@@ -297,7 +349,54 @@
}
}
let result = f();
- self.data_mut().stack_depth -= 1;
+ {
+ let mut data = self.data_mut();
+ data.stack_depth -= 1;
+ data.stack_generation += 1;
+ // if let Some(e) = e {
+ // result =
+ // data.breakpoints
+ // .insert(data.stack_depth, data.stack_generation, &e, result)
+ // }
+ }
+ if let Err(mut err) = result {
+ err.trace_mut().0.push(StackTraceElement {
+ location: e.cloned(),
+ desc: frame_desc(),
+ });
+ return Err(err);
+ }
+ result
+ }
+ /// Executes code creating a new stack frame
+ pub fn push_val(
+ &self,
+ e: Option<&ExprLocation>,
+ frame_desc: impl FnOnce() -> String,
+ f: impl FnOnce() -> Result<Val>,
+ ) -> Result<Val> {
+ {
+ let mut data = self.data_mut();
+ let stack_depth = &mut data.stack_depth;
+ if *stack_depth > self.max_stack() {
+ // Error creation uses data, so i drop guard here
+ drop(data);
+ throw!(StackOverflow);
+ } else {
+ *stack_depth += 1;
+ }
+ }
+ let mut result = f();
+ {
+ let mut data = self.data_mut();
+ data.stack_depth -= 1;
+ data.stack_generation += 1;
+ if let Some(e) = e {
+ result =
+ data.breakpoints
+ .insert(data.stack_depth, data.stack_generation, &e, result)
+ }
+ }
if let Err(mut err) = result {
err.trace_mut().0.push(StackTraceElement {
location: e.cloned(),
@@ -322,7 +421,26 @@
result
})
}
+ pub fn run_in_state_with_breakpoint(
+ &self,
+ bp: Rc<Breakpoint>,
+ f: impl FnOnce() -> Result<()>,
+ ) -> Result<()> {
+ {
+ let mut data = self.data_mut();
+ data.breakpoints.0.push(bp);
+ }
+
+ let result = self.run_in_state(f);
+ {
+ let mut data = self.data_mut();
+ data.breakpoints.0.pop();
+ }
+
+ result
+ }
+
pub fn stringify_err(&self, e: &LocError) -> String {
let mut out = String::new();
self.settings()
@@ -346,7 +464,7 @@
pub fn with_tla(&self, val: Val) -> Result<Val> {
self.run_in_state(|| {
Ok(match val {
- Val::Func(func) => push(
+ Val::Func(func) => push_frame(
None,
|| "during TLA call".to_owned(),
|| {
@@ -514,7 +632,7 @@
|| "inner".to_owned(),
|| Err(RuntimeError("".into()).into()),
)?;
- Ok(())
+ Ok(Val::Null)
},
)
.unwrap();
crates/jrsonnet-evaluator/src/trace/location.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/trace/location.rs
+++ b/crates/jrsonnet-evaluator/src/trace/location.rs
@@ -9,6 +9,18 @@
pub line_end_offset: usize,
}
+pub fn location_to_offset(mut file: &str, mut line: usize, column: usize) -> Option<usize> {
+ let mut offset = 0;
+ while line > 1 {
+ let pos = file.find('\n')?;
+ offset += pos + 1;
+ file = &file[pos + 1..];
+ line -= 1;
+ }
+ offset += column - 1;
+ Some(offset)
+}
+
pub fn offset_to_location(file: &str, offsets: &[usize]) -> Vec<CodeLocation> {
if offsets.is_empty() {
return vec![];
crates/jrsonnet-evaluator/src/typed.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed.rs
+++ b/crates/jrsonnet-evaluator/src/typed.rs
@@ -2,7 +2,7 @@
use crate::{
error::{Error, LocError, Result},
- push, Val,
+ push_frame, Val,
};
use jrsonnet_gc::Trace;
use jrsonnet_parser::ExprLocation;
@@ -103,7 +103,7 @@
path: impl Fn() -> ValuePathItem,
item: impl Fn() -> Result<()>,
) -> Result<()> {
- push(location, error_reason, || match item() {
+ push_frame(location, error_reason, || match item() {
Ok(_) => Ok(()),
Err(mut e) => {
if let Error::TypeError(e) = &mut e.error_mut() {
crates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -356,6 +356,11 @@
#[derive(Clone, PartialEq, Trace)]
#[trivially_drop]
pub struct ExprLocation(pub Rc<Path>, pub usize, pub usize);
+impl ExprLocation {
+ pub fn belongs_to(&self, other: &ExprLocation) -> bool {
+ other.0 == self.0 && other.1 <= self.1 && other.2 >= self.2
+ }
+}
impl Debug for ExprLocation {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {