Change debugger init signature.

This commit is contained in:
Stephen Chung 2022-11-25 13:20:03 +08:00
parent 6db9870fb1
commit fbe30b8d0e
6 changed files with 25 additions and 12 deletions

View File

@ -4,11 +4,16 @@ Rhai Release Notes
Version 1.12.0
==============
Buf fixes
Bug fixes
---------
* Integer numbers that are too large to deserialize into `INT` now fall back to `Decimal` or `FLOAT` instead of silently truncating.
Breaking API changes
--------------------
* The callback for initializing a debugger instance has changed to `Fn(&Engine, Debugger) -> Debugger`. This allows more control over the initial setup of the debugger.
Net features
------------
@ -21,6 +26,7 @@ Net features
Enhancements
------------
* Optimizations have been done to key data structures to minimize size and creation time, which involves turning rarely-used fields into `Option<Box<T>>`. This resulted in some speed improvements.
* `CallableFunction` is exported under `internals`.
* The `TypeBuilder` type and `CustomType` trait are no longer marked as volatile.
* `FuncArgs` is also implemented for arrays.

View File

@ -349,7 +349,9 @@ impl Engine {
#[inline(always)]
pub fn register_debugger(
&mut self,
init: impl Fn(&Self) -> Dynamic + SendSync + 'static,
init: impl Fn(&Self, crate::debugger::Debugger) -> crate::debugger::Debugger
+ SendSync
+ 'static,
callback: impl Fn(
EvalContext,
crate::eval::DebuggerEvent,

View File

@ -604,7 +604,10 @@ fn main() {
#[allow(deprecated)]
engine.register_debugger(
// Store the current source in the debugger state
|_| "".into(),
|engine, mut debugger| {
debugger.set_state(engine.const_empty_string());
debugger
},
// Main debugging interface
move |context, event, node, source, pos| {
debug_callback(context, event, node, source, pos, &lines)

View File

@ -10,10 +10,10 @@ use std::{fmt, iter::repeat, mem};
/// Callback function to initialize the debugger.
#[cfg(not(feature = "sync"))]
pub type OnDebuggingInit = dyn Fn(&Engine) -> Dynamic;
pub type OnDebuggingInit = dyn Fn(&Engine, Debugger) -> Debugger;
/// Callback function to initialize the debugger.
#[cfg(feature = "sync")]
pub type OnDebuggingInit = dyn Fn(&Engine) -> Dynamic + Send + Sync;
pub type OnDebuggingInit = dyn Fn(&Engine, Debugger) -> Debugger + Send + Sync;
/// Callback function for debugging.
#[cfg(not(feature = "sync"))]
@ -268,12 +268,12 @@ impl Debugger {
/// Create a new [`Debugger`].
#[inline(always)]
#[must_use]
pub const fn new(status: DebuggerStatus, state: Dynamic) -> Self {
pub const fn new(status: DebuggerStatus) -> Self {
Self {
status,
break_points: Vec::new(),
call_stack: Vec::new(),
state,
state: Dynamic::UNIT,
}
}
/// Get the current call stack.

View File

@ -104,7 +104,8 @@ impl GlobalRuntimeState {
#[cfg(feature = "debugging")]
debugger: engine.debugger.as_ref().map(|x| {
crate::eval::Debugger::new(crate::eval::DebuggerStatus::Init, (x.0)(engine))
let dbg = crate::eval::Debugger::new(crate::eval::DebuggerStatus::Init);
(x.0)(engine, dbg)
}),
}
}

View File

@ -12,7 +12,7 @@ fn test_debugging() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
engine.register_debugger(
|_| Dynamic::UNIT,
|_, dbg| dbg,
|_, _, _, _, _| Ok(rhai::debugger::DebuggerCommand::Continue),
);
@ -47,19 +47,20 @@ fn test_debugger_state() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
engine.register_debugger(
|_| {
|_, mut debugger| {
// Say, use an object map for the debugger state
let mut state = Map::new();
// Initialize properties
state.insert("hello".into(), (42 as INT).into());
state.insert("foo".into(), false.into());
Dynamic::from_map(state)
debugger.set_state(state);
debugger
},
|mut context, _, _, _, _| {
// Print debugger state - which is an object map
println!(
"Current state = {}",
context.global_runtime_state_mut().debugger().state()
context.global_runtime_state().debugger().state()
);
// Modify state