More code refactor.

This commit is contained in:
Stephen Chung
2022-11-25 20:42:16 +08:00
parent fbe30b8d0e
commit d645d8271c
30 changed files with 422 additions and 434 deletions

View File

@@ -5,8 +5,8 @@ use super::{Caches, GlobalRuntimeState, Target};
use crate::ast::{ASTFlags, Expr, OpAssignment};
use crate::config::hashing::SusLock;
use crate::engine::{FN_IDX_GET, FN_IDX_SET};
use crate::types::dynamic::Union;
use crate::types::RestoreOnDrop;
use crate::tokenizer::Token;
use crate::types::{dynamic::Union, RestoreOnDrop};
use crate::{
calc_fn_hash, Dynamic, Engine, FnArgsVec, Position, RhaiResult, RhaiResultOf, Scope, ERR,
};
@@ -76,7 +76,7 @@ impl Engine {
global,
caches,
FN_IDX_GET,
None,
Token::NonToken,
hash_idx().0,
&mut [target, idx],
true,
@@ -105,7 +105,7 @@ impl Engine {
global,
caches,
FN_IDX_SET,
None,
Token::NonToken,
hash_idx().1,
&mut [target, idx, new_val],
is_ref_mut,
@@ -766,7 +766,14 @@ impl Engine {
let args = &mut [target.as_mut()];
let (mut orig_val, ..) = self
.exec_native_fn_call(
global, caches, getter, None, *hash_get, args, is_ref_mut, *pos,
global,
caches,
getter,
Token::NonToken,
*hash_get,
args,
is_ref_mut,
*pos,
)
.or_else(|err| match *err {
// Try an indexer if property does not exist
@@ -799,7 +806,14 @@ impl Engine {
let args = &mut [target.as_mut(), &mut new_val];
self.exec_native_fn_call(
global, caches, setter, None, *hash_set, args, is_ref_mut, *pos,
global,
caches,
setter,
Token::NonToken,
*hash_set,
args,
is_ref_mut,
*pos,
)
.or_else(|err| match *err {
// Try an indexer if property does not exist
@@ -825,7 +839,14 @@ impl Engine {
let ((getter, hash_get), _, name) = &**x;
let args = &mut [target.as_mut()];
self.exec_native_fn_call(
global, caches, getter, None, *hash_get, args, is_ref_mut, *pos,
global,
caches,
getter,
Token::NonToken,
*hash_get,
args,
is_ref_mut,
*pos,
)
.map_or_else(
|err| match *err {
@@ -921,7 +942,13 @@ impl Engine {
// Assume getters are always pure
let (mut val, ..) = self
.exec_native_fn_call(
global, caches, getter, None, *hash_get, args, is_ref_mut,
global,
caches,
getter,
Token::NonToken,
*hash_get,
args,
is_ref_mut,
pos,
)
.or_else(|err| match *err {
@@ -955,7 +982,13 @@ impl Engine {
let mut arg_values = [target.as_mut(), val.as_mut()];
let args = &mut arg_values;
self.exec_native_fn_call(
global, caches, setter, None, *hash_set, args, is_ref_mut,
global,
caches,
setter,
Token::NonToken,
*hash_set,
args,
is_ref_mut,
pos,
)
.or_else(

View File

@@ -415,7 +415,7 @@ impl Engine {
this_ptr: &mut Dynamic,
node: impl Into<ASTNode<'a>>,
) -> RhaiResultOf<()> {
if self.debugger.is_some() {
if self.is_debugger_registered() {
if let Some(cmd) =
self.run_debugger_with_reset_raw(global, caches, scope, this_ptr, node)?
{
@@ -440,7 +440,7 @@ impl Engine {
this_ptr: &mut Dynamic,
node: impl Into<ASTNode<'a>>,
) -> RhaiResultOf<Option<DebuggerStatus>> {
if self.debugger.is_some() {
if self.is_debugger_registered() {
self.run_debugger_with_reset_raw(global, caches, scope, this_ptr, node)
} else {
Ok(None)
@@ -512,7 +512,7 @@ impl Engine {
let src = src.as_ref().map(|s| s.as_str());
let context = crate::EvalContext::new(self, global, caches, scope, this_ptr);
if let Some(ref x) = self.debugger {
if let Some(ref x) = self.debugger_interface {
let (.., ref on_debugger) = **x;
let command = on_debugger(context, event, node, src, node.position())?;

View File

@@ -8,7 +8,7 @@ use std::prelude::v1::*;
/// Collection of globally-defined constants.
#[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_function"))]
pub type GlobalConstants =
pub type SharedGlobalConstants =
crate::Shared<crate::Locked<std::collections::BTreeMap<ImmutableString, Dynamic>>>;
/// _(internals)_ Global runtime states.
@@ -67,12 +67,12 @@ pub struct GlobalRuntimeState {
/// Interior mutability is needed because it is shared in order to aid in cloning.
#[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_function"))]
pub constants: Option<GlobalConstants>,
pub constants: Option<SharedGlobalConstants>,
/// Custom state that can be used by the external host.
pub tag: Dynamic,
/// Debugging interface.
#[cfg(feature = "debugging")]
pub(crate) debugger: Option<super::Debugger>,
pub(crate) debugger: Option<Box<super::Debugger>>,
}
impl GlobalRuntimeState {
@@ -103,9 +103,9 @@ impl GlobalRuntimeState {
tag: engine.default_tag().clone(),
#[cfg(feature = "debugging")]
debugger: engine.debugger.as_ref().map(|x| {
debugger: engine.debugger_interface.as_ref().map(|x| {
let dbg = crate::eval::Debugger::new(crate::eval::DebuggerStatus::Init);
(x.0)(engine, dbg)
(x.0)(engine, dbg).into()
}),
}
}
@@ -116,7 +116,7 @@ impl GlobalRuntimeState {
#[inline]
#[must_use]
pub fn num_imports(&self) -> usize {
self.modules.as_ref().map_or(0, |m| m.len())
self.modules.as_deref().map_or(0, crate::StaticVec::len)
}
/// Get the globally-imported [module][crate::Module] at a particular index.
///
@@ -139,7 +139,7 @@ impl GlobalRuntimeState {
&mut self,
index: usize,
) -> Option<&mut crate::SharedModule> {
self.modules.as_mut().and_then(|m| m.get_mut(index))
self.modules.as_deref_mut().and_then(|m| m.get_mut(index))
}
/// Get the index of a globally-imported [module][crate::Module] by name.
///
@@ -184,8 +184,8 @@ impl GlobalRuntimeState {
self.imports = None;
self.modules = None;
} else if self.imports.is_some() {
self.imports.as_mut().unwrap().truncate(size);
self.modules.as_mut().unwrap().truncate(size);
self.imports.as_deref_mut().unwrap().truncate(size);
self.modules.as_deref_mut().unwrap().truncate(size);
}
}
/// Get an iterator to the stack of globally-imported [modules][crate::Module] in reverse order.
@@ -235,7 +235,7 @@ impl GlobalRuntimeState {
#[cfg(not(feature = "no_module"))]
#[inline]
pub(crate) fn may_contain_dynamic_fn(&self, hash_script: u64) -> bool {
self.modules.as_ref().map_or(false, |m| {
self.modules.as_deref().map_or(false, |m| {
m.iter().any(|m| m.may_contain_dynamic_fn(hash_script))
})
}
@@ -324,7 +324,7 @@ impl GlobalRuntimeState {
/// Panics if the debugging interface is not set.
#[cfg(feature = "debugging")]
pub fn debugger_mut(&mut self) -> &mut super::Debugger {
self.debugger.as_mut().unwrap()
self.debugger.as_deref_mut().unwrap()
}
}

View File

@@ -17,10 +17,10 @@ pub use debugger::{
OnDebuggerCallback, OnDebuggingInit,
};
pub use eval_context::EvalContext;
pub use global_state::GlobalRuntimeState;
#[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_function"))]
pub use global_state::GlobalConstants;
pub use global_state::GlobalRuntimeState;
pub use global_state::SharedGlobalConstants;
#[cfg(not(feature = "no_index"))]
pub use target::calc_offset_len;
pub use target::{calc_index, Target};

View File

@@ -129,7 +129,8 @@ impl Engine {
let args = &mut [&mut *lock_guard, &mut new_val];
if self.fast_operators() {
if let Some(func) = get_builtin_op_assignment_fn(op_assign_token, args[0], args[1])
if let Some(func) =
get_builtin_op_assignment_fn(op_assign_token.clone(), args[0], args[1])
{
// Built-in found
let op = op_assign_token.literal_syntax();
@@ -145,7 +146,7 @@ impl Engine {
let op_assign = op_assign_token.literal_syntax();
let op = op_token.literal_syntax();
let token = Some(op_assign_token);
let token = op_assign_token.clone();
match self
.exec_native_fn_call(global, caches, op_assign, token, hash, args, true, *op_pos)
@@ -154,7 +155,7 @@ impl Engine {
Err(err) if matches!(*err, ERR::ErrorFunctionNotFound(ref f, ..) if f.starts_with(op_assign)) =>
{
// Expand to `var = var op rhs`
let token = Some(op_token);
let token = op_token.clone();
*args[0] = self
.exec_native_fn_call(