diff --git a/src/bin/rhai-dbg.rs b/src/bin/rhai-dbg.rs index 93bc6b6d..db285f7a 100644 --- a/src/bin/rhai-dbg.rs +++ b/src/bin/rhai-dbg.rs @@ -60,12 +60,7 @@ fn print_current_source( lines: &[String], window: (usize, usize), ) { - let current_source = &mut *context - .global_runtime_state_mut() - .debugger - .state_mut() - .write_lock::() - .unwrap(); + let current_source = &mut *context.tag_mut().write_lock::().unwrap(); let src = source.unwrap_or(""); if src != current_source { println!( diff --git a/src/eval/debugger.rs b/src/eval/debugger.rs index ebd07b78..a9ece2a5 100644 --- a/src/eval/debugger.rs +++ b/src/eval/debugger.rs @@ -249,8 +249,6 @@ impl fmt::Display for CallStackFrame { pub struct Debugger { /// The current status command. pub(crate) status: DebuggerStatus, - /// The current state. - state: Dynamic, /// The current set of break-points. break_points: Vec, /// The current function call stack. @@ -258,37 +256,16 @@ pub struct Debugger { } impl Debugger { - /// Create a new [`Debugger`] based on an [`Engine`]. + /// Create a new [`Debugger`]. #[inline(always)] #[must_use] - pub fn new(engine: &Engine) -> Self { + pub fn new(status: DebuggerStatus) -> Self { Self { - status: if engine.debugger.is_some() { - DebuggerStatus::Init - } else { - DebuggerStatus::CONTINUE - }, - state: if let Some((ref init, ..)) = engine.debugger { - init() - } else { - Dynamic::UNIT - }, + status, break_points: Vec::new(), call_stack: Vec::new(), } } - /// Get a reference to the current state. - #[inline(always)] - #[must_use] - pub fn state(&self) -> &Dynamic { - &self.state - } - /// Get a mutable reference to the current state. - #[inline(always)] - #[must_use] - pub fn state_mut(&mut self) -> &mut Dynamic { - &mut self.state - } /// Get the current call stack. #[inline(always)] #[must_use] diff --git a/src/eval/global_state.rs b/src/eval/global_state.rs index d24b04e0..bdcbb2ca 100644 --- a/src/eval/global_state.rs +++ b/src/eval/global_state.rs @@ -97,9 +97,23 @@ impl GlobalRuntimeState<'_> { #[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_function"))] constants: None, + + #[cfg(not(feature = "debugging"))] tag: Dynamic::UNIT, #[cfg(feature = "debugging")] - debugger: crate::eval::Debugger::new(_engine), + tag: if let Some((ref init, ..)) = engine.debugger { + init() + } else { + Dynamic::UNIT + }, + + #[cfg(feature = "debugging")] + debugger: crate::eval::Debugger::new(if engine.debugger.is_some() { + crate::eval::DebuggerStatus::Init + } else { + crate::eval::DebuggerStatus::CONTINUE + }), + dummy: PhantomData::default(), } } diff --git a/tests/debugging.rs b/tests/debugging.rs index aaba2b2c..8f5b8cf9 100644 --- a/tests/debugging.rs +++ b/tests/debugging.rs @@ -56,17 +56,11 @@ fn test_debugger_state() -> Result<(), Box> { Dynamic::from_map(state) }, |mut context, _, _, _, _| { - // Get global runtime state - let global = context.global_runtime_state_mut(); - - // Get debugger - let debugger = &mut global.debugger; - // Print debugger state - which is an object map - println!("Current state = {}", debugger.state()); + println!("Current state = {}", context.tag()); // Modify state - let mut state = debugger.state_mut().write_lock::().unwrap(); + let mut state = context.tag_mut().write_lock::().unwrap(); let hello = state.get("hello").unwrap().as_int().unwrap(); state.insert("hello".into(), (hello + 1).into()); state.insert("foo".into(), true.into());