From fbe30b8d0eb77db9ec91f81fb5c4042fc45e6402 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 25 Nov 2022 13:20:03 +0800 Subject: [PATCH] Change debugger init signature. --- CHANGELOG.md | 8 +++++++- src/api/events.rs | 4 +++- src/bin/rhai-dbg.rs | 5 ++++- src/eval/debugger.rs | 8 ++++---- src/eval/global_state.rs | 3 ++- tests/debugging.rs | 9 +++++---- 6 files changed, 25 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 58425903..869eba34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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>`. 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. diff --git a/src/api/events.rs b/src/api/events.rs index eff11efd..75499edf 100644 --- a/src/api/events.rs +++ b/src/api/events.rs @@ -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, diff --git a/src/bin/rhai-dbg.rs b/src/bin/rhai-dbg.rs index b47f7a6d..bee052b7 100644 --- a/src/bin/rhai-dbg.rs +++ b/src/bin/rhai-dbg.rs @@ -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) diff --git a/src/eval/debugger.rs b/src/eval/debugger.rs index 93663293..14d0246a 100644 --- a/src/eval/debugger.rs +++ b/src/eval/debugger.rs @@ -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. diff --git a/src/eval/global_state.rs b/src/eval/global_state.rs index 408f71b0..1a9308e0 100644 --- a/src/eval/global_state.rs +++ b/src/eval/global_state.rs @@ -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) }), } } diff --git a/tests/debugging.rs b/tests/debugging.rs index 762ed9d2..46b419ca 100644 --- a/tests/debugging.rs +++ b/tests/debugging.rs @@ -12,7 +12,7 @@ fn test_debugging() -> Result<(), Box> { 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> { 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