From 8bcb771281300dc4e61f1289dcefee810add69ec Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 10 Dec 2022 22:37:13 +0800 Subject: [PATCH] Refine auto_restore syntax. --- src/eval/chaining.rs | 35 ++++++++++++----------------------- src/eval/debugger.rs | 8 +++----- src/eval/expr.rs | 4 ++-- src/eval/stmt.rs | 18 +++++------------- src/func/call.rs | 8 ++++---- src/types/interner.rs | 9 +-------- src/types/position_none.rs | 20 ++------------------ src/types/restore.rs | 12 ++++++++++++ 8 files changed, 41 insertions(+), 73 deletions(-) diff --git a/src/eval/chaining.rs b/src/eval/chaining.rs index e4baa002..82b6551a 100644 --- a/src/eval/chaining.rs +++ b/src/eval/chaining.rs @@ -2,7 +2,7 @@ #![cfg(any(not(feature = "no_index"), not(feature = "no_object")))] use super::{Caches, GlobalRuntimeState, Target}; -use crate::ast::{ASTFlags, Expr, OpAssignment}; +use crate::ast::{ASTFlags, BinaryExpr, Expr, OpAssignment}; use crate::config::hashing::SusLock; use crate::engine::{FN_IDX_GET, FN_IDX_SET}; use crate::tokenizer::NO_TOKEN; @@ -346,7 +346,7 @@ impl Engine { ) -> RhaiResult { let chain_type = ChainType::from(expr); - let crate::ast::BinaryExpr { lhs, rhs } = match expr { + let BinaryExpr { lhs, rhs } = match expr { #[cfg(not(feature = "no_index"))] Expr::Index(x, ..) => &**x, #[cfg(not(feature = "no_object"))] @@ -378,11 +378,9 @@ impl Engine { idx_values.push(rhs.get_literal_value().unwrap()) } // All other patterns - evaluate the arguments chain - _ => { - self.eval_dot_index_chain_arguments( - global, caches, scope, this_ptr, expr, rhs, idx_values, - )?; - } + _ => self.eval_dot_index_chain_arguments( + global, caches, scope, this_ptr, expr, rhs, idx_values, + )?, } match lhs { @@ -462,7 +460,7 @@ impl Engine { Expr::Index(x, ..) | Expr::Dot(x, ..) if !parent.options().contains(ASTFlags::BREAK) => { - let crate::ast::BinaryExpr { lhs, rhs, .. } = &**x; + let BinaryExpr { lhs, rhs, .. } = &**x; let mut _arg_values = FnArgsVec::new_const(); @@ -690,17 +688,14 @@ impl Engine { let reset = self.run_debugger_with_reset(global, caches, scope, this_ptr, rhs)?; #[cfg(feature = "debugging")] - auto_restore!(global if reset.is_some() => move |g| g.debugger_mut().reset_status(reset)); + auto_restore!(global if Some(reset) => move |g| g.debugger_mut().reset_status(reset)); let crate::ast::FnCallExpr { name, hashes, args, .. } = &**x; // Truncate the index values upon exit - auto_restore! { - idx_values => truncate; - let offset = idx_values.len() - args.len(); - } + auto_restore! { idx_values => truncate; let offset = idx_values.len() - args.len(); } let call_args = &mut idx_values[offset..]; let arg1_pos = args.get(0).map_or(Position::NONE, Expr::position); @@ -862,17 +857,14 @@ impl Engine { global, caches, scope, this_ptr, _node, )?; #[cfg(feature = "debugging")] - auto_restore!(global if reset.is_some() => move |g| g.debugger_mut().reset_status(reset)); + auto_restore!(global if Some(reset) => move |g| g.debugger_mut().reset_status(reset)); let crate::ast::FnCallExpr { name, hashes, args, .. } = &**x; // Truncate the index values upon exit - auto_restore! { - idx_values => truncate; - let offset = idx_values.len() - args.len(); - } + auto_restore! { idx_values => truncate; let offset = idx_values.len() - args.len(); } let call_args = &mut idx_values[offset..]; let arg1_pos = args.get(0).map_or(Position::NONE, Expr::position); @@ -983,17 +975,14 @@ impl Engine { global, caches, scope, this_ptr, _node, )?; #[cfg(feature = "debugging")] - auto_restore!(global if reset.is_some() => move |g| g.debugger_mut().reset_status(reset)); + auto_restore!(global if Some(reset) => move |g| g.debugger_mut().reset_status(reset)); let crate::ast::FnCallExpr { name, hashes, args, .. } = &**f; // Truncate the index values upon exit - auto_restore! { - idx_values => truncate; - let offset = idx_values.len() - args.len(); - } + auto_restore! { idx_values => truncate; let offset = idx_values.len() - args.len(); } let call_args = &mut idx_values[offset..]; let pos1 = args.get(0).map_or(Position::NONE, Expr::position); diff --git a/src/eval/debugger.rs b/src/eval/debugger.rs index 62c6fde7..f1ffb39a 100644 --- a/src/eval/debugger.rs +++ b/src/eval/debugger.rs @@ -314,14 +314,12 @@ impl Debugger { None } } - /// Override the status of this [`Debugger`] if it is [`Some`] the current status is + /// Override the status of this [`Debugger`] if the current status is /// [`CONTINUE`][DebuggerStatus::CONTINUE]. #[inline(always)] - pub(crate) fn reset_status(&mut self, status: Option) { + pub(crate) fn reset_status(&mut self, status: DebuggerStatus) { if self.status == DebuggerStatus::CONTINUE { - if let Some(cmd) = status { - self.status = cmd; - } + self.status = status; } } /// Returns the first break-point triggered by a particular [`AST` Node][ASTNode]. diff --git a/src/eval/expr.rs b/src/eval/expr.rs index 9f104691..75e0520c 100644 --- a/src/eval/expr.rs +++ b/src/eval/expr.rs @@ -234,7 +234,7 @@ impl Engine { #[cfg(feature = "debugging")] let reset = self.run_debugger_with_reset(global, caches, scope, this_ptr, expr)?; #[cfg(feature = "debugging")] - auto_restore!(global if reset.is_some() => move |g| g.debugger_mut().reset_status(reset)); + auto_restore!(global if Some(reset) => move |g| g.debugger_mut().reset_status(reset)); self.track_operation(global, expr.position())?; @@ -265,7 +265,7 @@ impl Engine { #[cfg(feature = "debugging")] let reset = self.run_debugger_with_reset(global, caches, scope, this_ptr, expr)?; #[cfg(feature = "debugging")] - auto_restore!(global if reset.is_some() => move |g| g.debugger_mut().reset_status(reset)); + auto_restore!(global if Some(reset) => move |g| g.debugger_mut().reset_status(reset)); self.track_operation(global, expr.position())?; diff --git a/src/eval/stmt.rs b/src/eval/stmt.rs index 99b90f44..9fef726f 100644 --- a/src/eval/stmt.rs +++ b/src/eval/stmt.rs @@ -41,10 +41,7 @@ impl Engine { } // Restore scope at end of block if necessary - auto_restore! { - scope if restore_orig_state => rewind; - let orig_scope_len = scope.len(); - } + auto_restore! { scope if restore_orig_state => rewind; let orig_scope_len = scope.len(); } // Restore global state at end of block if necessary let orig_always_search_scope = global.always_search_scope; @@ -208,7 +205,7 @@ impl Engine { #[cfg(feature = "debugging")] let reset = self.run_debugger_with_reset(global, caches, scope, this_ptr, stmt)?; #[cfg(feature = "debugging")] - auto_restore!(global if reset.is_some() => move |g| g.debugger_mut().reset_status(reset)); + auto_restore!(global if Some(reset) => move |g| g.debugger_mut().reset_status(reset)); // Coded this way for better branch prediction. // Popular branches are lifted out of the `match` statement into their own branches. @@ -519,10 +516,8 @@ impl Engine { let iter_func = iter_func.ok_or_else(|| ERR::ErrorFor(expr.start_position()))?; // Restore scope at end of statement - auto_restore! { - scope => rewind; - let orig_scope_len = scope.len(); - } + auto_restore! { scope => rewind; let orig_scope_len = scope.len(); } + // Add the loop variables let counter_index = if counter.is_empty() { usize::MAX @@ -649,10 +644,7 @@ impl Engine { }; // Restore scope at end of block - auto_restore! { - scope if !catch_var.is_empty() => rewind; - let orig_scope_len = scope.len(); - } + auto_restore! { scope if !catch_var.is_empty() => rewind; let orig_scope_len = scope.len(); } if !catch_var.is_empty() { scope.push(catch_var.name.clone(), err_value); diff --git a/src/func/call.rs b/src/func/call.rs index 7a0fafec..a01d283d 100644 --- a/src/func/call.rs +++ b/src/func/call.rs @@ -725,7 +725,7 @@ impl Engine { }) }); #[cfg(feature = "debugging")] - auto_restore!(global if reset.is_some() => move |g| g.debugger_mut().reset_status(reset)); + auto_restore!(global if Some(reset) => move |g| g.debugger_mut().reset_status(reset)); self.eval_expr(global, caches, scope, this_ptr, arg_expr) .map(|r| (r, arg_expr.start_position())) @@ -1549,14 +1549,14 @@ impl Engine { } // Normal function call - let (first_arg, args) = args.split_first().map_or_else( + let (first_arg, rest_args) = args.split_first().map_or_else( || (None, args.as_ref()), |(first, rest)| (Some(first), rest), ); self.make_function_call( - global, caches, scope, this_ptr, name, op_token, first_arg, args, *hashes, *capture, - pos, + global, caches, scope, this_ptr, name, op_token, first_arg, rest_args, *hashes, + *capture, pos, ) } } diff --git a/src/types/interner.rs b/src/types/interner.rs index 34fe3789..ce704bc4 100644 --- a/src/types/interner.rs +++ b/src/types/interner.rs @@ -92,14 +92,7 @@ impl StringsInterner { let result = match self.cache.entry(hash) { Entry::Occupied(e) => return e.get().clone(), - Entry::Vacant(e) => { - let value = mapper(text); - - if value.strong_count() > 1 { - return value; - } - e.insert(value).clone() - } + Entry::Vacant(e) => e.insert(mapper(text)).clone(), }; // Throttle the cache upon exit diff --git a/src/types/position_none.rs b/src/types/position_none.rs index c0ced769..e6a65d16 100644 --- a/src/types/position_none.rs +++ b/src/types/position_none.rs @@ -10,7 +10,7 @@ use std::{ }; /// A location (line number + character position) in the input script. -#[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)] +#[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy, Default)] pub struct Position; impl Position { @@ -81,14 +81,6 @@ impl Position { } } -impl Default for Position { - #[inline(always)] - #[must_use] - fn default() -> Self { - Self - } -} - impl fmt::Display for Position { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "none") @@ -119,17 +111,9 @@ impl AddAssign for Position { /// _(internals)_ A span consisting of a starting and an ending [positions][Position]. /// Exported under the `internals` feature only. -#[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)] +#[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy, Default)] pub struct Span; -impl Default for Span { - #[inline(always)] - #[must_use] - fn default() -> Self { - Self - } -} - impl Span { /// Empty [`Span`]. pub const NONE: Self = Self; diff --git a/src/types/restore.rs b/src/types/restore.rs index eb11e84b..ac41cfb4 100644 --- a/src/types/restore.rs +++ b/src/types/restore.rs @@ -36,6 +36,18 @@ macro_rules! auto_restore { ($var:ident = $value:expr => $restore:expr) => { let $var = &mut *crate::types::RestoreOnDrop::lock($value, $restore); }; + ($var:ident if Some($guard:ident) => $restore:expr) => { + auto_restore!($var = ($var) if Some($guard) => $restore); + }; + ($var:ident = ( $value:expr ) if Some($guard:ident) => $restore:expr) => { + let mut __rx__; + let $var = if let Some($guard) = $guard { + __rx__ = crate::types::RestoreOnDrop::lock($value, $restore); + &mut *__rx__ + } else { + &mut *$value + }; + }; ($var:ident if $guard:expr => $restore:expr) => { auto_restore!($var = ($var) if $guard => $restore); };