From 4f74d2f96a0f2aaa07451e4e00b7a4ff4d79330d Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 2 May 2022 12:14:53 +0800 Subject: [PATCH 1/3] Minor cleanup. --- src/api/custom_syntax.rs | 2 +- src/api/events.rs | 12 +++++------- tests/var_scope.rs | 12 +++--------- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/api/custom_syntax.rs b/src/api/custom_syntax.rs index 978ba920..b7c1f48b 100644 --- a/src/api/custom_syntax.rs +++ b/src/api/custom_syntax.rs @@ -331,7 +331,7 @@ impl Engine { /// /// The implementation function has the following signature: /// - /// > `Fn(symbols: &[ImmutableString], look_ahead: &str) -> Result, ParseError>` + /// `Fn(symbols: &[ImmutableString], look_ahead: &str) -> Result, ParseError>` /// /// where: /// * `symbols`: a slice of symbols that have been parsed so far, possibly containing `$expr$` and/or `$block$`; diff --git a/src/api/events.rs b/src/api/events.rs index 7ac2e2c5..ec7b5022 100644 --- a/src/api/events.rs +++ b/src/api/events.rs @@ -27,7 +27,7 @@ impl Engine { /// /// # Callback Function Signature /// - /// > `Fn(name: &str, index: usize, context: EvalContext) -> Result, Box>` + /// `Fn(name: &str, index: usize, context: EvalContext) -> Result, Box>` /// /// where: /// * `name`: name of the variable. @@ -87,7 +87,7 @@ impl Engine { /// /// # Callback Function Signature /// - /// > `Fn(is_runtime: bool, info: VarInfo, context: EvalContext) -> Result>` + /// `Fn(is_runtime: bool, info: VarInfo, context: EvalContext) -> Result>` /// /// where: /// * `is_runtime`: `true` if the variable definition event happens during runtime, `false` if during compilation. @@ -148,7 +148,7 @@ impl Engine { /// /// # Callback Function Signature /// - /// > `Fn(token: Token, pos: Position, state: &TokenizeState) -> Token` + /// `Fn(token: Token, pos: Position, state: &TokenizeState) -> Token` /// /// where: /// * [`token`][crate::tokenizer::Token]: current token parsed @@ -210,9 +210,7 @@ impl Engine { /// /// # Callback Function Signature /// - /// The callback function signature takes the following form: - /// - /// > `Fn(counter: u64) -> Option` + /// `Fn(counter: u64) -> Option` /// /// ## Return value /// @@ -295,7 +293,7 @@ impl Engine { /// /// The callback function signature passed takes the following form: /// - /// > `Fn(text: &str, source: Option<&str>, pos: Position)` + /// `Fn(text: &str, source: Option<&str>, pos: Position)` /// /// where: /// * `text`: the text to display diff --git a/tests/var_scope.rs b/tests/var_scope.rs index 4e50c3dd..014ec42b 100644 --- a/tests/var_scope.rs +++ b/tests/var_scope.rs @@ -202,15 +202,9 @@ fn test_var_def_filter() -> Result<(), Box> { let ast = engine.compile("let x = 42;")?; engine.run_ast(&ast)?; - engine.on_def_var(|_, info, mut ctx| { - if ctx.tag().is::<()>() { - *ctx.tag_mut() = rhai::Dynamic::ONE; - } - println!("Tag = {}", ctx.tag()); - match (info.name, info.nesting_level) { - ("x", 0 | 1) => Ok(false), - _ => Ok(true), - } + engine.on_def_var(|_, info, _| match (info.name, info.nesting_level) { + ("x", 0 | 1) => Ok(false), + _ => Ok(true), }); assert_eq!( From 516f5a82a0f70f34971771b4b14a47f08d3be4bb Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 3 May 2022 21:55:01 +0800 Subject: [PATCH 2/3] Use tag for debugger state. --- src/bin/rhai-dbg.rs | 7 +------ src/eval/debugger.rs | 29 +++-------------------------- src/eval/global_state.rs | 16 +++++++++++++++- tests/debugging.rs | 10 ++-------- 4 files changed, 21 insertions(+), 41 deletions(-) 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()); From 2a57bd9d2583d6c237a1874165c3ef744aa5ad99 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 3 May 2022 21:55:08 +0800 Subject: [PATCH 3/3] Mark some types as non_exhaustive. --- CHANGELOG.md | 5 +++-- src/ast/flags.rs | 1 + src/module/mod.rs | 2 ++ src/optimizer.rs | 1 + src/types/dynamic.rs | 1 + 5 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 28e36210..d1cf25db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,11 +15,12 @@ Script-breaking changes * _Strict Variables Mode_ no longer returns an error when an undeclared variable matches a variable/constant in the provided external `Scope`. -Changes to unstable API's -------------------------- +Potentially breaking API changes +-------------------------------- * The `Engine::on_var` and `Engine::on_parse_token` API's are now marked unstable/volatile. * The closures passed to `Engine::on_var`, `Engine::on_def_var` and `Engine::register_debugger` take `EvalContext` instead of `&EvalContext` or `&mut EvalContext`. +* The following enum's are marked `non_exhaustive`: `AccessMode`, `FnAccess`, `FnNamespace`, `FnMetadata`, `OptimizationLevel` New API ------- diff --git a/src/ast/flags.rs b/src/ast/flags.rs index 6d2506ba..34a3bbaa 100644 --- a/src/ast/flags.rs +++ b/src/ast/flags.rs @@ -6,6 +6,7 @@ use std::prelude::v1::*; /// A type representing the access mode of a function. #[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] +#[non_exhaustive] pub enum FnAccess { /// Private function. Private, diff --git a/src/module/mod.rs b/src/module/mod.rs index ea9b7ebe..5ad8e48e 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -23,6 +23,7 @@ use std::{ /// A type representing the namespace of a function. #[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] +#[non_exhaustive] pub enum FnNamespace { /// Module namespace only. /// @@ -34,6 +35,7 @@ pub enum FnNamespace { /// A type containing all metadata for a registered function. #[derive(Debug, Clone, Eq, PartialEq, Hash)] +#[non_exhaustive] pub struct FnMetadata { /// Function namespace. pub namespace: FnNamespace, diff --git a/src/optimizer.rs b/src/optimizer.rs index f18b295b..69731719 100644 --- a/src/optimizer.rs +++ b/src/optimizer.rs @@ -23,6 +23,7 @@ use std::{ /// Level of optimization performed. #[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)] +#[non_exhaustive] pub enum OptimizationLevel { /// No optimization performed. None, diff --git a/src/types/dynamic.rs b/src/types/dynamic.rs index 792d008b..0b5e891c 100644 --- a/src/types/dynamic.rs +++ b/src/types/dynamic.rs @@ -127,6 +127,7 @@ impl dyn Variant { /// _(internals)_ Modes of access. /// Exported under the `internals` feature only. #[derive(Debug, Eq, PartialEq, Hash, Copy, Clone)] +#[non_exhaustive] pub enum AccessMode { /// Mutable. ReadWrite,