Add debugging interface.

This commit is contained in:
Stephen Chung
2022-01-24 17:04:40 +08:00
parent 182870c9ed
commit fc87dec128
24 changed files with 1255 additions and 414 deletions

View File

@@ -889,8 +889,20 @@ impl Engine {
) -> RhaiResultOf<(Dynamic, Position)> {
Ok((
if let Expr::Stack(slot, _) = arg_expr {
#[cfg(feature = "debugging")]
let active =
self.run_debugger(scope, global, state, lib, this_ptr, arg_expr.into(), level);
#[cfg(feature = "debugging")]
global.debugger.activate(active);
constants[*slot].clone()
} else if let Some(value) = arg_expr.get_literal_value() {
#[cfg(feature = "debugging")]
let active =
self.run_debugger(scope, global, state, lib, this_ptr, arg_expr.into(), level);
#[cfg(feature = "debugging")]
global.debugger.activate(active);
value
} else {
self.eval_expr(scope, global, state, lib, this_ptr, arg_expr, level)?
@@ -1133,9 +1145,17 @@ impl Engine {
// convert to method-call style in order to leverage potential &mut first argument and
// avoid cloning the value
if curry.is_empty() && first_arg.map_or(false, |expr| expr.is_variable_access(false)) {
// func(x, ...) -> x.func(...)
let first_expr = first_arg.unwrap();
#[cfg(feature = "debugging")]
{
let node = first_expr.into();
let active =
self.run_debugger(scope, global, state, lib, this_ptr, node, level);
global.debugger.activate(active);
}
// func(x, ...) -> x.func(...)
a_expr.iter().try_for_each(|expr| {
self.get_arg_value(scope, global, state, lib, this_ptr, level, expr, constants)
.map(|(value, _)| arg_values.push(value.flatten()))
@@ -1215,6 +1235,14 @@ impl Engine {
// If so, convert to method-call style in order to leverage potential
// &mut first argument and avoid cloning the value
if !args_expr.is_empty() && args_expr[0].is_variable_access(true) {
#[cfg(feature = "debugging")]
{
let node = (&args_expr[0]).into();
let active =
self.run_debugger(scope, global, state, lib, this_ptr, node, level);
global.debugger.activate(active);
}
// func(x, ...) -> x.func(...)
arg_values.push(Dynamic::UNIT);

View File

@@ -301,10 +301,7 @@ impl<'a> NativeCallContext<'a> {
self.engine()
.exec_fn_call(
&mut self
.global
.cloned()
.unwrap_or_else(|| GlobalRuntimeState::new()),
&mut self.global.cloned().unwrap_or_else(GlobalRuntimeState::new),
&mut EvalState::new(),
self.lib,
fn_name,

View File

@@ -70,7 +70,9 @@ impl Engine {
}
let orig_scope_len = scope.len();
let orig_mods_len = global.num_imports();
let orig_imports_len = global.num_imports();
#[cfg(feature = "debugging")]
let orig_call_stack_len = global.debugger.call_stack_len();
// Put arguments into scope as variables
scope.extend(fn_def.params.iter().cloned().zip(args.into_iter().map(|v| {
@@ -78,6 +80,19 @@ impl Engine {
mem::take(*v)
})));
// Push a new call stack frame
#[cfg(feature = "debugging")]
global.debugger.push_call_stack_frame(
fn_def.name.clone(),
scope
.iter()
.skip(orig_scope_len)
.map(|(_, _, v)| v.clone())
.collect(),
global.source.clone(),
pos,
);
// Merge in encapsulated environment, if any
let mut lib_merged = StaticVec::with_capacity(lib.len() + 1);
let orig_fn_resolution_caches_len = state.fn_resolution_caches_len();
@@ -144,11 +159,15 @@ impl Engine {
// Remove arguments only, leaving new variables in the scope
scope.remove_range(orig_scope_len, args.len())
}
global.truncate_imports(orig_mods_len);
global.truncate_imports(orig_imports_len);
// Restore state
state.rewind_fn_resolution_caches(orig_fn_resolution_caches_len);
// Pop the call stack
#[cfg(feature = "debugging")]
global.debugger.rewind_call_stack(orig_call_stack_len);
result
}