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 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. * 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 Net features
------------ ------------
@ -21,6 +26,7 @@ Net features
Enhancements 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`. * `CallableFunction` is exported under `internals`.
* The `TypeBuilder` type and `CustomType` trait are no longer marked as volatile. * The `TypeBuilder` type and `CustomType` trait are no longer marked as volatile.
* `FuncArgs` is also implemented for arrays. * `FuncArgs` is also implemented for arrays.

View File

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

View File

@ -604,7 +604,10 @@ fn main() {
#[allow(deprecated)] #[allow(deprecated)]
engine.register_debugger( engine.register_debugger(
// Store the current source in the debugger state // Store the current source in the debugger state
|_| "".into(), |engine, mut debugger| {
debugger.set_state(engine.const_empty_string());
debugger
},
// Main debugging interface // Main debugging interface
move |context, event, node, source, pos| { move |context, event, node, source, pos| {
debug_callback(context, event, node, source, pos, &lines) 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. /// Callback function to initialize the debugger.
#[cfg(not(feature = "sync"))] #[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. /// Callback function to initialize the debugger.
#[cfg(feature = "sync")] #[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. /// Callback function for debugging.
#[cfg(not(feature = "sync"))] #[cfg(not(feature = "sync"))]
@ -268,12 +268,12 @@ impl Debugger {
/// Create a new [`Debugger`]. /// Create a new [`Debugger`].
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
pub const fn new(status: DebuggerStatus, state: Dynamic) -> Self { pub const fn new(status: DebuggerStatus) -> Self {
Self { Self {
status, status,
break_points: Vec::new(), break_points: Vec::new(),
call_stack: Vec::new(), call_stack: Vec::new(),
state, state: Dynamic::UNIT,
} }
} }
/// Get the current call stack. /// Get the current call stack.

View File

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