git.delta.rocks / jrsonnet / refs/commits / 09a4bd7b68d6

difftreelog

feat breakpoints

Yaroslav Bolyukin2021-11-23parent: #8e60cf1.patch.diff
in: master

9 files changed

modifiedcrates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth
1use crate::error::Error::*;1use crate::error::Error::*;
2use crate::error::Result;2use crate::error::Result;
3use crate::push_frame;
3use crate::{throw, Val};4use crate::{throw, Val};
45
5#[derive(PartialEq, Clone, Copy)]6#[derive(PartialEq, Clone, Copy)]
102 buf.push_str(cur_padding);103 buf.push_str(cur_padding);
103 escape_string_json_buf(&field, buf);104 escape_string_json_buf(&field, buf);
104 buf.push_str(": ");105 buf.push_str(": ");
105 crate::push(106 push_frame(
106 None,107 None,
107 || format!("field <{}> manifestification", field.clone()),108 || format!("field <{}> manifestification", field.clone()),
108 || {109 || {
109 let value = obj.get(field.clone())?.unwrap();110 let value = obj.get(field.clone())?.unwrap();
110 manifest_json_ex_buf(&value, buf, cur_padding, options)111 manifest_json_ex_buf(&value, buf, cur_padding, options)?;
112 Ok(Val::Null)
111 },113 },
112 )?;114 )?;
113 }115 }
modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
3 equals,3 equals,
4 error::{Error::*, Result},4 error::{Error::*, Result},
5 operator::evaluate_mod_op,5 operator::evaluate_mod_op,
6 parse_args, primitive_equals, push, throw, with_state, ArrValue, Context, EvaluationState,6 parse_args, primitive_equals, push_frame, throw, with_state, ArrValue, Context,
7 FuncVal, IndexableVal, LazyVal, Val,7 EvaluationState, FuncVal, IndexableVal, LazyVal, Val,
8};8};
9use format::{format_arr, format_obj};9use format::{format_arr, format_obj};
23pub mod sort;23pub mod sort;
2424
25pub fn std_format(str: IStr, vals: Val) -> Result<Val> {25pub fn std_format(str: IStr, vals: Val) -> Result<Val> {
26 push(26 push_frame(
27 Some(&ExprLocation(Rc::from(PathBuf::from("std.jsonnet")), 0, 0)),27 Some(&ExprLocation(Rc::from(PathBuf::from("std.jsonnet")), 0, 0)),
28 || format!("std.format of {}", str),28 || format!("std.format of {}", str),
29 || {29 || {
modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
1use crate::{1use crate::{Val, builtin::{format::FormatError, sort::SortError}, typed::TypeLocError};
2 builtin::{format::FormatError, sort::SortError},
3 typed::TypeLocError,
4};
5use jrsonnet_gc::Trace;2use jrsonnet_gc::Trace;
6use jrsonnet_interner::IStr;3use jrsonnet_interner::IStr;
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
2 builtin::std_slice,2 builtin::std_slice,
3 error::Error::*,3 error::Error::*,
4 evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},4 evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},
5 push, throw, with_state, ArrValue, Bindable, Context, ContextCreator, FuncDesc, FuncVal,5 push_frame, throw, with_state, ArrValue, Bindable, Context, ContextCreator, FuncDesc, FuncVal,
6 FutureWrapper, LazyBinding, LazyVal, LazyValValue, ObjValue, ObjValueBuilder, ObjectAssertion,6 FutureWrapper, LazyBinding, LazyVal, LazyValValue, ObjValue, ObjValueBuilder, ObjectAssertion,
7 Result, Val,7 Result, Val,
8};8};
464 if tailstrict {464 if tailstrict {
465 body()?465 body()?
466 } else {466 } else {
467 push(loc, || format!("function <{}> call", f.name()), body)?467 push_frame(loc, || format!("function <{}> call", f.name()), body)?
468 }468 }
469 }469 }
470 v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),470 v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),
474pub fn evaluate_assert(context: Context, assertion: &AssertStmt) -> Result<()> {474pub fn evaluate_assert(context: Context, assertion: &AssertStmt) -> Result<()> {
475 let value = &assertion.0;475 let value = &assertion.0;
476 let msg = &assertion.1;476 let msg = &assertion.1;
477 let assertion_result = push(477 let assertion_result = push_frame(
478 value.1.as_ref(),478 value.1.as_ref(),
479 || "assertion condition".to_owned(),479 || "assertion condition".to_owned(),
480 || {480 || {
483 },483 },
484 )?;484 )?;
485 if !assertion_result {485 if !assertion_result {
486 push(486 push_frame(
487 value.1.as_ref(),487 value.1.as_ref(),
488 || "assertion failure".to_owned(),488 || "assertion failure".to_owned(),
489 || {489 || {
510pub fn evaluate(context: Context, expr: &LocExpr) -> Result<Val> {510pub fn evaluate(context: Context, expr: &LocExpr) -> Result<Val> {
511 use Expr::*;511 use Expr::*;
512 let LocExpr(expr, loc) = expr;512 let LocExpr(expr, loc) = expr;
513 // let bp = with_state(|s| s.0.stop_at.borrow().clone());
513 Ok(match &**expr {514 Ok(match &**expr {
514 Literal(LiteralType::This) => {515 Literal(LiteralType::This) => {
515 Val::Obj(context.this().clone().ok_or(CantUseSelfOutsideOfObject)?)516 Val::Obj(context.this().clone().ok_or(CantUseSelfOutsideOfObject)?)
532 Num(v) => Val::new_checked_num(*v)?,533 Num(v) => Val::new_checked_num(*v)?,
533 BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, v1, *o, v2)?,534 BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, v1, *o, v2)?,
534 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,535 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,
535 Var(name) => push(536 Var(name) => push_frame(
536 loc.as_ref(),537 loc.as_ref(),
537 || format!("variable <{}>", name),538 || format!("variable <{}>", name),
538 || context.binding(name.clone())?.evaluate(),539 || context.binding(name.clone())?.evaluate(),
541 match (evaluate(context.clone(), value)?, evaluate(context, index)?) {542 match (evaluate(context.clone(), value)?, evaluate(context, index)?) {
542 (Val::Obj(v), Val::Str(s)) => {543 (Val::Obj(v), Val::Str(s)) => {
543 let sn = s.clone();544 let sn = s.clone();
544 push(545 push_frame(
545 loc.as_ref(),546 loc.as_ref(),
546 || format!("field <{}> access", sn),547 || format!("field <{}> access", sn),
547 || {548 || {
652 evaluate_assert(context.clone(), assert)?;653 evaluate_assert(context.clone(), assert)?;
653 evaluate(context, returned)?654 evaluate(context, returned)?
654 }655 }
655 ErrorStmt(e) => push(656 ErrorStmt(e) => push_frame(
656 loc.as_ref(),657 loc.as_ref(),
657 || "error statement".to_owned(),658 || "error statement".to_owned(),
658 || {659 || {
666 cond_then,667 cond_then,
667 cond_else,668 cond_else,
668 } => {669 } => {
669 if push(670 if push_frame(
670 loc.as_ref(),671 loc.as_ref(),
671 || "if condition".to_owned(),672 || "if condition".to_owned(),
672 || evaluate(context.clone(), &cond.0)?.try_cast_bool("in if condition"),673 || evaluate(context.clone(), &cond.0)?.try_cast_bool("in if condition"),
708 .0;709 .0;
709 let mut import_location = tmp.to_path_buf();710 let mut import_location = tmp.to_path_buf();
710 import_location.pop();711 import_location.pop();
711 push(712 push_frame(
712 loc.as_ref(),713 loc.as_ref(),
713 || format!("import {:?}", path),714 || format!("import {:?}", path),
714 || with_state(|s| s.import_file(&import_location, path)),715 || with_state(|s| s.import_file(&import_location, path)),
modifiedcrates/jrsonnet-evaluator/src/function.rsdiffbeforeafterboth
243 ($ctx: expr, $fn_name: expr, $args: expr, $total_args: expr, [243 ($ctx: expr, $fn_name: expr, $args: expr, $total_args: expr, [
244 $($id: expr, $name: ident: $ty: expr $(=>$match: path)?);+ $(;)?244 $($id: expr, $name: ident: $ty: expr $(=>$match: path)?);+ $(;)?
245 ], $handler:block) => {{245 ], $handler:block) => {{
246 use $crate::{error::Error::*, throw, evaluate, push_stack_frame, typed::CheckType};246 use $crate::{error::Error::*, throw, evaluate, push_frame, typed::CheckType};
247247
248 let args = $args;248 let args = $args;
249 if args.unnamed.len() + args.named.len() > $total_args {249 if args.unnamed.len() + args.named.len() > $total_args {
263 } else {263 } else {
264 &$args.unnamed[$id]264 &$args.unnamed[$id]
265 };265 };
266 let $name = push_stack_frame(None, || format!("evaluating argument"), || {266 let $name = push_frame(None, || format!("evaluating argument"), || {
267 let value = evaluate($ctx.clone(), &$name)?;267 let value = evaluate($ctx.clone(), &$name)?;
268 $ty.check(&value)?;268 $ty.check(&value)?;
269 Ok(value)269 Ok(value)
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
40 path::{Path, PathBuf},40 path::{Path, PathBuf},
41 rc::Rc,41 rc::Rc,
42};42};
43use trace::{offset_to_location, CodeLocation, CompactFormat, TraceFormat};43use trace::{location_to_offset, offset_to_location, CodeLocation, CompactFormat, TraceFormat};
44pub use val::*;44pub use val::*;
4545
46pub trait Bindable: Trace {46pub trait Bindable: Trace {
109struct EvaluationData {109struct EvaluationData {
110 /// Used for stack overflow detection, stacktrace is populated on unwind110 /// Used for stack overflow detection, stacktrace is populated on unwind
111 stack_depth: usize,111 stack_depth: usize,
112 /// Updated every time stack entry is popt
113 stack_generation: usize,
114
115 breakpoints: Breakpoints,
112 /// Contains file source codes and evaluation results for imports and pretty-printed stacktraces116 /// Contains file source codes and evaluation results for imports and pretty-printed stacktraces
113 files: HashMap<Rc<Path>, FileData>,117 files: HashMap<Rc<Path>, FileData>,
114 str_files: HashMap<Rc<Path>, IStr>,118 str_files: HashMap<Rc<Path>, IStr>,
120 evaluated: Option<Val>,124 evaluated: Option<Val>,
121}125}
126
127pub struct Breakpoint {
128 loc: ExprLocation,
129 collected: RefCell<HashMap<usize, (usize, Vec<Result<Val>>)>>,
130}
131#[derive(Default)]
132struct Breakpoints(Vec<Rc<Breakpoint>>);
133impl Breakpoints {
134 fn insert(
135 &self,
136 stack_depth: usize,
137 stack_generation: usize,
138 loc: &ExprLocation,
139 result: Result<Val>,
140 ) -> Result<Val> {
141 if self.0.is_empty() {
142 return result;
143 }
144 for item in self.0.iter() {
145 if item.loc.belongs_to(loc) {
146 let mut collected = item.collected.borrow_mut();
147 let (depth, vals) = collected.entry(stack_generation).or_default();
148 if stack_depth > *depth {
149 vals.clear();
150 }
151 vals.push(result.clone());
152 }
153 }
154 result
155 }
156}
157
122#[derive(Default)]158#[derive(Default)]
123pub struct EvaluationStateInternals {159pub struct EvaluationStateInternals {
135pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {171pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {
136 EVAL_STATE.with(|s| f(s.borrow().as_ref().unwrap()))172 EVAL_STATE.with(|s| f(s.borrow().as_ref().unwrap()))
137}173}
138pub(crate) fn push<T>(174pub(crate) fn push_frame<T>(
139 e: Option<&ExprLocation>,175 e: Option<&ExprLocation>,
140 frame_desc: impl FnOnce() -> String,176 frame_desc: impl FnOnce() -> String,
141 f: impl FnOnce() -> Result<T>,177 f: impl FnOnce() -> Result<T>,
142) -> Result<T> {178) -> Result<T> {
143 with_state(|s| s.push(e, frame_desc, f))179 with_state(|s| s.push(e, frame_desc, f))
144}180}
145181
146pub fn push_stack_frame<T>(182pub(crate) fn push_val_frame(
147 e: Option<&ExprLocation>,183 e: Option<&ExprLocation>,
148 frame_desc: impl FnOnce() -> String,184 frame_desc: impl FnOnce() -> String,
149 f: impl FnOnce() -> Result<T>,185 f: impl FnOnce() -> Result<Val>,
150) -> Result<T> {186) -> Result<Val> {
151 push(e, frame_desc, f)187 with_state(|s| s.push(e, frame_desc, f))
152}188}
153189
154/// Maintains stack trace and import resolution190/// Maintains stack trace and import resolution
178 Ok(())214 Ok(())
179 }215 }
216
217 pub fn reset_evaluation_state(&self, name: &Path) {
218 self.data_mut()
219 .files
220 .get_mut(name)
221 .unwrap()
222 .evaluated
223 .take();
224 }
180225
181 /// Adds file by source code and parsed expr226 /// Adds file by source code and parsed expr
182 pub fn add_parsed_file(227 pub fn add_parsed_file(
204 offset_to_location(&self.get_source(file).unwrap(), locs)249 offset_to_location(&self.get_source(file).unwrap(), locs)
205 }250 }
206
207 pub(crate) fn import_file(&self, from: &Path, path: &Path) -> Result<Val> {251 pub fn map_from_source_location(
252 &self,
253 file: &Path,
254 line: usize,
255 column: usize,
256 ) -> Option<usize> {
257 location_to_offset(&self.get_source(file).unwrap(), line, column)
258 }
259 pub fn import_file(&self, from: &Path, path: &Path) -> Result<Val> {
208 let file_path = self.resolve_file(from, path)?;260 let file_path = self.resolve_file(from, path)?;
209 {261 {
210 let data = self.data();262 let data = self.data();
297 }349 }
298 }350 }
299 let result = f();351 let result = f();
352 {
300 self.data_mut().stack_depth -= 1;353 let mut data = self.data_mut();
354 data.stack_depth -= 1;
355 data.stack_generation += 1;
356 // if let Some(e) = e {
357 // result =
358 // data.breakpoints
359 // .insert(data.stack_depth, data.stack_generation, &e, result)
360 // }
361 }
301 if let Err(mut err) = result {362 if let Err(mut err) = result {
302 err.trace_mut().0.push(StackTraceElement {363 err.trace_mut().0.push(StackTraceElement {
303 location: e.cloned(),364 location: e.cloned(),
307 }368 }
308 result369 result
309 }370 }
371 /// Executes code creating a new stack frame
372 pub fn push_val(
373 &self,
374 e: Option<&ExprLocation>,
375 frame_desc: impl FnOnce() -> String,
376 f: impl FnOnce() -> Result<Val>,
377 ) -> Result<Val> {
378 {
379 let mut data = self.data_mut();
380 let stack_depth = &mut data.stack_depth;
381 if *stack_depth > self.max_stack() {
382 // Error creation uses data, so i drop guard here
383 drop(data);
384 throw!(StackOverflow);
385 } else {
386 *stack_depth += 1;
387 }
388 }
389 let mut result = f();
390 {
391 let mut data = self.data_mut();
392 data.stack_depth -= 1;
393 data.stack_generation += 1;
394 if let Some(e) = e {
395 result =
396 data.breakpoints
397 .insert(data.stack_depth, data.stack_generation, &e, result)
398 }
399 }
400 if let Err(mut err) = result {
401 err.trace_mut().0.push(StackTraceElement {
402 location: e.cloned(),
403 desc: frame_desc(),
404 });
405 return Err(err);
406 }
407 result
408 }
310409
311 /// Runs passed function in state (required if function needs to modify stack trace)410 /// Runs passed function in state (required if function needs to modify stack trace)
312 pub fn run_in_state<T>(&self, f: impl FnOnce() -> T) -> T {411 pub fn run_in_state<T>(&self, f: impl FnOnce() -> T) -> T {
322 result421 result
323 })422 })
324 }423 }
424 pub fn run_in_state_with_breakpoint(
425 &self,
426 bp: Rc<Breakpoint>,
427 f: impl FnOnce() -> Result<()>,
428 ) -> Result<()> {
429 {
430 let mut data = self.data_mut();
431 data.breakpoints.0.push(bp);
432 }
433
434 let result = self.run_in_state(f);
435
436 {
437 let mut data = self.data_mut();
438 data.breakpoints.0.pop();
439 }
440
441 result
442 }
325443
326 pub fn stringify_err(&self, e: &LocError) -> String {444 pub fn stringify_err(&self, e: &LocError) -> String {
327 let mut out = String::new();445 let mut out = String::new();
346 pub fn with_tla(&self, val: Val) -> Result<Val> {464 pub fn with_tla(&self, val: Val) -> Result<Val> {
347 self.run_in_state(|| {465 self.run_in_state(|| {
348 Ok(match val {466 Ok(match val {
349 Val::Func(func) => push(467 Val::Func(func) => push_frame(
350 None,468 None,
351 || "during TLA call".to_owned(),469 || "during TLA call".to_owned(),
352 || {470 || {
514 || "inner".to_owned(),632 || "inner".to_owned(),
515 || Err(RuntimeError("".into()).into()),633 || Err(RuntimeError("".into()).into()),
516 )?;634 )?;
517 Ok(())635 Ok(Val::Null)
518 },636 },
519 )637 )
520 .unwrap();638 .unwrap();
modifiedcrates/jrsonnet-evaluator/src/trace/location.rsdiffbeforeafterboth
9 pub line_end_offset: usize,9 pub line_end_offset: usize,
10}10}
11
12pub fn location_to_offset(mut file: &str, mut line: usize, column: usize) -> Option<usize> {
13 let mut offset = 0;
14 while line > 1 {
15 let pos = file.find('\n')?;
16 offset += pos + 1;
17 file = &file[pos + 1..];
18 line -= 1;
19 }
20 offset += column - 1;
21 Some(offset)
22}
1123
12pub fn offset_to_location(file: &str, offsets: &[usize]) -> Vec<CodeLocation> {24pub fn offset_to_location(file: &str, offsets: &[usize]) -> Vec<CodeLocation> {
13 if offsets.is_empty() {25 if offsets.is_empty() {
modifiedcrates/jrsonnet-evaluator/src/typed.rsdiffbeforeafterboth
22
3use crate::{3use crate::{
4 error::{Error, LocError, Result},4 error::{Error, LocError, Result},
5 push, Val,5 push_frame, Val,
6};6};
7use jrsonnet_gc::Trace;7use jrsonnet_gc::Trace;
8use jrsonnet_parser::ExprLocation;8use jrsonnet_parser::ExprLocation;
103 path: impl Fn() -> ValuePathItem,103 path: impl Fn() -> ValuePathItem,
104 item: impl Fn() -> Result<()>,104 item: impl Fn() -> Result<()>,
105) -> Result<()> {105) -> Result<()> {
106 push(location, error_reason, || match item() {106 push_frame(location, error_reason, || match item() {
107 Ok(_) => Ok(()),107 Ok(_) => Ok(()),
108 Err(mut e) => {108 Err(mut e) => {
109 if let Error::TypeError(e) = &mut e.error_mut() {109 if let Error::TypeError(e) = &mut e.error_mut() {
modifiedcrates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth
356#[derive(Clone, PartialEq, Trace)]356#[derive(Clone, PartialEq, Trace)]
357#[trivially_drop]357#[trivially_drop]
358pub struct ExprLocation(pub Rc<Path>, pub usize, pub usize);358pub struct ExprLocation(pub Rc<Path>, pub usize, pub usize);
359impl ExprLocation {
360 pub fn belongs_to(&self, other: &ExprLocation) -> bool {
361 other.0 == self.0 && other.1 <= self.1 && other.2 >= self.2
362 }
363}
359364
360impl Debug for ExprLocation {365impl Debug for ExprLocation {
361 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {366 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {