Skip to content

Commit

Permalink
Merge pull request #558 from schungx/master
Browse files Browse the repository at this point in the history
Cleanup for 1.7.0.
  • Loading branch information
schungx committed May 4, 2022
2 parents 7d80c43 + 2a57bd9 commit 4fff1d8
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 60 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Expand Up @@ -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
-------
Expand Down
2 changes: 1 addition & 1 deletion src/api/custom_syntax.rs
Expand Up @@ -331,7 +331,7 @@ impl Engine {
///
/// The implementation function has the following signature:
///
/// > `Fn(symbols: &[ImmutableString], look_ahead: &str) -> Result<Option<ImmutableString>, ParseError>`
/// `Fn(symbols: &[ImmutableString], look_ahead: &str) -> Result<Option<ImmutableString>, ParseError>`
///
/// where:
/// * `symbols`: a slice of symbols that have been parsed so far, possibly containing `$expr$` and/or `$block$`;
Expand Down
12 changes: 5 additions & 7 deletions src/api/events.rs
Expand Up @@ -27,7 +27,7 @@ impl Engine {
///
/// # Callback Function Signature
///
/// > `Fn(name: &str, index: usize, context: EvalContext) -> Result<Option<Dynamic>, Box<EvalAltResult>>`
/// `Fn(name: &str, index: usize, context: EvalContext) -> Result<Option<Dynamic>, Box<EvalAltResult>>`
///
/// where:
/// * `name`: name of the variable.
Expand Down Expand Up @@ -87,7 +87,7 @@ impl Engine {
///
/// # Callback Function Signature
///
/// > `Fn(is_runtime: bool, info: VarInfo, context: EvalContext) -> Result<bool, Box<EvalAltResult>>`
/// `Fn(is_runtime: bool, info: VarInfo, context: EvalContext) -> Result<bool, Box<EvalAltResult>>`
///
/// where:
/// * `is_runtime`: `true` if the variable definition event happens during runtime, `false` if during compilation.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -210,9 +210,7 @@ impl Engine {
///
/// # Callback Function Signature
///
/// The callback function signature takes the following form:
///
/// > `Fn(counter: u64) -> Option<Dynamic>`
/// `Fn(counter: u64) -> Option<Dynamic>`
///
/// ## Return value
///
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/ast/flags.rs
Expand Up @@ -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,
Expand Down
7 changes: 1 addition & 6 deletions src/bin/rhai-dbg.rs
Expand Up @@ -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::<ImmutableString>()
.unwrap();
let current_source = &mut *context.tag_mut().write_lock::<ImmutableString>().unwrap();
let src = source.unwrap_or("");
if src != current_source {
println!(
Expand Down
29 changes: 3 additions & 26 deletions src/eval/debugger.rs
Expand Up @@ -249,46 +249,23 @@ 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<BreakPoint>,
/// The current function call stack.
call_stack: Vec<CallStackFrame>,
}

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]
Expand Down
16 changes: 15 additions & 1 deletion src/eval/global_state.rs
Expand Up @@ -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(),
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/module/mod.rs
Expand Up @@ -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.
///
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/optimizer.rs
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/types/dynamic.rs
Expand Up @@ -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,
Expand Down
10 changes: 2 additions & 8 deletions tests/debugging.rs
Expand Up @@ -56,17 +56,11 @@ fn test_debugger_state() -> Result<(), Box<EvalAltResult>> {
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::<Map>().unwrap();
let mut state = context.tag_mut().write_lock::<Map>().unwrap();
let hello = state.get("hello").unwrap().as_int().unwrap();
state.insert("hello".into(), (hello + 1).into());
state.insert("foo".into(), true.into());
Expand Down
12 changes: 3 additions & 9 deletions tests/var_scope.rs
Expand Up @@ -202,15 +202,9 @@ fn test_var_def_filter() -> Result<(), Box<EvalAltResult>> {
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!(
Expand Down

0 comments on commit 4fff1d8

Please sign in to comment.