difftreelog
feat breakpoints
in: master
9 files changed
crates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth1use crate::error::Error::*;2use crate::error::Result;3use crate::{throw, Val};45#[derive(PartialEq, Clone, Copy)]6pub enum ManifestType {7 // Applied in manifestification8 Manifest,9 /// Used for std.manifestJson10 /// Empty array/objects extends to "[\n\n]" instead of "[ ]" as in manifest11 Std,12 /// No line breaks, used in `obj+''`13 ToString,14 /// Minified json15 Minify,16}1718pub struct ManifestJsonOptions<'s> {19 pub padding: &'s str,20 pub mtype: ManifestType,21}2223pub fn manifest_json_ex(val: &Val, options: &ManifestJsonOptions<'_>) -> Result<String> {24 let mut out = String::new();25 manifest_json_ex_buf(val, &mut out, &mut String::new(), options)?;26 Ok(out)27}28fn manifest_json_ex_buf(29 val: &Val,30 buf: &mut String,31 cur_padding: &mut String,32 options: &ManifestJsonOptions<'_>,33) -> Result<()> {34 use std::fmt::Write;35 let mtype = options.mtype;36 match val {37 Val::Bool(v) => {38 if *v {39 buf.push_str("true");40 } else {41 buf.push_str("false");42 }43 }44 Val::Null => buf.push_str("null"),45 Val::Str(s) => escape_string_json_buf(s, buf),46 Val::Num(n) => write!(buf, "{}", n).unwrap(),47 Val::Arr(items) => {48 buf.push('[');49 if !items.is_empty() {50 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {51 buf.push('\n');52 }5354 let old_len = cur_padding.len();55 cur_padding.push_str(options.padding);56 for (i, item) in items.iter().enumerate() {57 if i != 0 {58 buf.push(',');59 if mtype == ManifestType::ToString {60 buf.push(' ');61 } else if mtype != ManifestType::Minify {62 buf.push('\n');63 }64 }65 buf.push_str(cur_padding);66 manifest_json_ex_buf(&item?, buf, cur_padding, options)?;67 }68 cur_padding.truncate(old_len);6970 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {71 buf.push('\n');72 buf.push_str(cur_padding);73 }74 } else if mtype == ManifestType::Std {75 buf.push_str("\n\n");76 buf.push_str(cur_padding);77 } else if mtype == ManifestType::ToString || mtype == ManifestType::Manifest {78 buf.push(' ');79 }80 buf.push(']');81 }82 Val::Obj(obj) => {83 obj.run_assertions()?;84 buf.push('{');85 let fields = obj.fields();86 if !fields.is_empty() {87 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {88 buf.push('\n');89 }9091 let old_len = cur_padding.len();92 cur_padding.push_str(options.padding);93 for (i, field) in fields.into_iter().enumerate() {94 if i != 0 {95 buf.push(',');96 if mtype == ManifestType::ToString {97 buf.push(' ');98 } else if mtype != ManifestType::Minify {99 buf.push('\n');100 }101 }102 buf.push_str(cur_padding);103 escape_string_json_buf(&field, buf);104 buf.push_str(": ");105 crate::push(106 None,107 || format!("field <{}> manifestification", field.clone()),108 || {109 let value = obj.get(field.clone())?.unwrap();110 manifest_json_ex_buf(&value, buf, cur_padding, options)111 },112 )?;113 }114 cur_padding.truncate(old_len);115116 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {117 buf.push('\n');118 buf.push_str(cur_padding);119 }120 } else if mtype == ManifestType::Std {121 buf.push_str("\n\n");122 buf.push_str(cur_padding);123 } else if mtype == ManifestType::ToString || mtype == ManifestType::Manifest {124 buf.push(' ');125 }126 buf.push('}');127 }128 Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),129 };130 Ok(())131}132133pub fn escape_string_json(s: &str) -> String {134 let mut buf = String::new();135 escape_string_json_buf(s, &mut buf);136 buf137}138139fn escape_string_json_buf(s: &str, buf: &mut String) {140 use std::fmt::Write;141 buf.push('"');142 for c in s.chars() {143 match c {144 '"' => buf.push_str("\\\""),145 '\\' => buf.push_str("\\\\"),146 '\u{0008}' => buf.push_str("\\b"),147 '\u{000c}' => buf.push_str("\\f"),148 '\n' => buf.push_str("\\n"),149 '\r' => buf.push_str("\\r"),150 '\t' => buf.push_str("\\t"),151 c if c < 32 as char || (c >= 127 as char && c <= 159 as char) => {152 write!(buf, "\\u{:04x}", c as u32).unwrap()153 }154 c => buf.push(c),155 }156 }157 buf.push('"');158}159160pub struct ManifestYamlOptions<'s> {161 /// Padding before fields, i.e162 /// ```yaml163 /// a:164 /// b:165 /// ## <- this166 /// ```167 pub padding: &'s str,168 /// Padding before array elements in objects169 /// ```yaml170 /// a:171 /// - 1172 /// ## <- this173 /// ```174 pub arr_element_padding: &'s str,175}176177pub fn manifest_yaml_ex(val: &Val, options: &ManifestYamlOptions<'_>) -> Result<String> {178 let mut out = String::new();179 manifest_yaml_ex_buf(val, &mut out, &mut String::new(), options)?;180 Ok(out)181}182fn manifest_yaml_ex_buf(183 val: &Val,184 buf: &mut String,185 cur_padding: &mut String,186 options: &ManifestYamlOptions<'_>,187) -> Result<()> {188 use std::fmt::Write;189 match val {190 Val::Bool(v) => {191 if *v {192 buf.push_str("true")193 } else {194 buf.push_str("false")195 }196 }197 Val::Null => buf.push_str("null"),198 Val::Str(s) => {199 if s.is_empty() {200 buf.push_str("\"\"");201 } else if let Some(s) = s.strip_suffix('\n') {202 buf.push('|');203 for line in s.split('\n') {204 buf.push('\n');205 buf.push_str(options.padding);206 buf.push_str(line);207 }208 } else {209 escape_string_json_buf(s, buf)210 }211 }212 Val::Num(n) => write!(buf, "{}", *n).unwrap(),213 Val::Arr(a) => {214 if a.is_empty() {215 buf.push_str("[]");216 } else {217 for (i, item) in a.iter().enumerate() {218 if i != 0 {219 buf.push('\n');220 buf.push_str(cur_padding);221 }222 let item = item?;223 buf.push('-');224 match &item {225 Val::Arr(a) if !a.is_empty() => {226 buf.push('\n');227 buf.push_str(cur_padding);228 buf.push_str(options.padding);229 }230 _ => buf.push(' '),231 }232 let extra_padding = match &item {233 Val::Arr(a) => !a.is_empty(),234 Val::Obj(o) => !o.is_empty(),235 _ => false,236 };237 let prev_len = cur_padding.len();238 if extra_padding {239 cur_padding.push_str(options.padding);240 }241 manifest_yaml_ex_buf(&item, buf, cur_padding, options)?;242 cur_padding.truncate(prev_len);243 }244 }245 }246 Val::Obj(o) => {247 if o.is_empty() {248 buf.push_str("{}");249 } else {250 for (i, key) in o.fields().iter().enumerate() {251 if i != 0 {252 buf.push('\n');253 buf.push_str(cur_padding);254 }255 escape_string_json_buf(key, buf);256 buf.push(':');257 let prev_len = cur_padding.len();258 let item = o.get(key.clone())?.expect("field exists");259 match &item {260 Val::Arr(a) if !a.is_empty() => {261 buf.push('\n');262 buf.push_str(cur_padding);263 buf.push_str(options.arr_element_padding);264 cur_padding.push_str(options.arr_element_padding);265 }266 Val::Obj(o) if !o.is_empty() => {267 buf.push('\n');268 buf.push_str(cur_padding);269 buf.push_str(options.padding);270 cur_padding.push_str(options.padding);271 }272 _ => buf.push(' '),273 }274 manifest_yaml_ex_buf(&item, buf, cur_padding, options)?;275 cur_padding.truncate(prev_len);276 }277 }278 }279 Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),280 }281 Ok(())282}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.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -1,7 +1,4 @@
-use crate::{
- builtin::{format::FormatError, sort::SortError},
- typed::TypeLocError,
-};
+use crate::{Val, builtin::{format::FormatError, sort::SortError}, typed::TypeLocError};
use jrsonnet_gc::Trace;
use jrsonnet_interner::IStr;
use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};
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 {