Minor cleanup.

This commit is contained in:
Stephen Chung 2022-12-09 20:42:55 +08:00
parent 30ff208104
commit f15a9a7c9c
9 changed files with 41 additions and 38 deletions

View File

@ -18,9 +18,9 @@ use std::{
#[non_exhaustive] #[non_exhaustive]
#[must_use] #[must_use]
pub struct CallFnOptions<'t> { pub struct CallFnOptions<'t> {
/// A value for binding to the `this` pointer (if any). /// A value for binding to the `this` pointer (if any). Default [`None`].
pub this_ptr: Option<&'t mut Dynamic>, pub this_ptr: Option<&'t mut Dynamic>,
/// The custom state of this evaluation run (if any), overrides [`Engine::default_tag`]. /// The custom state of this evaluation run (if any), overrides [`Engine::default_tag`]. Default [`None`].
pub tag: Option<Dynamic>, pub tag: Option<Dynamic>,
/// Evaluate the [`AST`] to load necessary modules before calling the function? Default `true`. /// Evaluate the [`AST`] to load necessary modules before calling the function? Default `true`.
pub eval_ast: bool, pub eval_ast: bool,
@ -172,13 +172,13 @@ impl Engine {
args.parse(&mut arg_values); args.parse(&mut arg_values);
self._call_fn( self._call_fn(
options,
scope, scope,
&mut GlobalRuntimeState::new(self), &mut GlobalRuntimeState::new(self),
&mut Caches::new(), &mut Caches::new(),
ast, ast,
name.as_ref(), name.as_ref(),
arg_values.as_mut(), arg_values.as_mut(),
options,
) )
.and_then(|result| { .and_then(|result| {
// Bail out early if the return type needs no cast // Bail out early if the return type needs no cast
@ -207,13 +207,13 @@ impl Engine {
#[inline(always)] #[inline(always)]
pub(crate) fn _call_fn( pub(crate) fn _call_fn(
&self, &self,
options: CallFnOptions,
scope: &mut Scope, scope: &mut Scope,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
ast: &AST, ast: &AST,
name: &str, name: &str,
arg_values: &mut [Dynamic], arg_values: &mut [Dynamic],
options: CallFnOptions,
) -> RhaiResult { ) -> RhaiResult {
let statements = ast.statements(); let statements = ast.statements();

View File

@ -167,13 +167,13 @@ impl Engine {
}; };
self._call_fn( self._call_fn(
options,
scope, scope,
&mut crate::eval::GlobalRuntimeState::new(self), &mut crate::eval::GlobalRuntimeState::new(self),
&mut crate::eval::Caches::new(), &mut crate::eval::Caches::new(),
ast, ast,
name.as_ref(), name.as_ref(),
arg_values.as_mut(), arg_values.as_mut(),
options,
) )
} }
/// Register a custom fallible function with the [`Engine`]. /// Register a custom fallible function with the [`Engine`].

View File

@ -250,9 +250,9 @@ impl Engine {
#[cfg(feature = "debugging")] #[cfg(feature = "debugging")]
if self.is_debugger_registered() { if self.is_debugger_registered() {
global.debugger_mut().status = crate::eval::DebuggerStatus::Terminate; global.debugger_mut().status = crate::eval::DebuggerStatus::Terminate;
let mut this = Dynamic::NULL; let mut this_ptr = Dynamic::NULL;
let node = &crate::ast::Stmt::Noop(Position::NONE); let node = &crate::ast::Stmt::Noop(Position::NONE);
self.run_debugger(global, caches, scope, &mut this, node)?; self.run_debugger(global, caches, scope, &mut this_ptr, node)?;
} }
Ok(r) Ok(r)
}) })

View File

@ -22,7 +22,7 @@ bitflags! {
/// Is looping allowed? /// Is looping allowed?
const LOOPING = 0b_0000_0010_0000; const LOOPING = 0b_0000_0010_0000;
/// Is variables shadowing allowed? /// Is variables shadowing allowed?
const SHADOW = 0b_0000_0100_0000; const SHADOWING = 0b_0000_0100_0000;
/// Strict variables mode? /// Strict variables mode?
const STRICT_VAR = 0b_0000_1000_0000; const STRICT_VAR = 0b_0000_1000_0000;
/// Raise error if an object map property does not exist? /// Raise error if an object map property does not exist?
@ -43,7 +43,7 @@ impl LangOptions {
| Self::SWITCH_EXPR | Self::SWITCH_EXPR
| Self::STMT_EXPR | Self::STMT_EXPR
| Self::LOOPING | Self::LOOPING
| Self::SHADOW | Self::SHADOWING
| Self::FAST_OPS | Self::FAST_OPS
| { | {
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
@ -148,12 +148,12 @@ impl Engine {
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
pub const fn allow_shadowing(&self) -> bool { pub const fn allow_shadowing(&self) -> bool {
self.options.contains(LangOptions::SHADOW) self.options.contains(LangOptions::SHADOWING)
} }
/// Set whether variables shadowing is allowed. /// Set whether variables shadowing is allowed.
#[inline(always)] #[inline(always)]
pub fn set_allow_shadowing(&mut self, enable: bool) -> &mut Self { pub fn set_allow_shadowing(&mut self, enable: bool) -> &mut Self {
self.options.set(LangOptions::SHADOW, enable); self.options.set(LangOptions::SHADOWING, enable);
self self
} }
/// Is strict variables mode enabled? /// Is strict variables mode enabled?

View File

@ -131,9 +131,9 @@ impl Engine {
#[cfg(feature = "debugging")] #[cfg(feature = "debugging")]
if self.is_debugger_registered() { if self.is_debugger_registered() {
global.debugger_mut().status = crate::eval::DebuggerStatus::Terminate; global.debugger_mut().status = crate::eval::DebuggerStatus::Terminate;
let mut this = crate::Dynamic::NULL; let mut this_ptr = crate::Dynamic::NULL;
let node = &crate::ast::Stmt::Noop(crate::Position::NONE); let node = &crate::ast::Stmt::Noop(crate::Position::NONE);
self.run_debugger(global, caches, scope, &mut this, node)?; self.run_debugger(global, caches, scope, &mut this_ptr, node)?;
} }
Ok(()) Ok(())
}) })

View File

@ -392,13 +392,19 @@ impl Engine {
self.run_debugger(global, caches, scope, this_ptr, lhs)?; self.run_debugger(global, caches, scope, this_ptr, lhs)?;
self.track_operation(global, *var_pos)?; self.track_operation(global, *var_pos)?;
let mut target = self.search_namespace(global, caches, scope, this_ptr, lhs)?; let target = &mut self.search_namespace(global, caches, scope, this_ptr, lhs)?;
let mut this_ptr = Dynamic::NULL;
let obj_ptr = &mut target;
let mut this = Dynamic::NULL;
self.eval_dot_index_chain_raw( self.eval_dot_index_chain_raw(
global, caches, &mut this, lhs, expr, obj_ptr, rhs, idx_values, new_val, global,
caches,
&mut this_ptr,
lhs,
expr,
target,
rhs,
idx_values,
new_val,
) )
} }
// {expr}.??? = ??? or {expr}[???] = ??? // {expr}.??? = ??? or {expr}[???] = ???

View File

@ -882,9 +882,9 @@ impl Engine {
scope: &mut Scope, scope: &mut Scope,
statements: &[Stmt], statements: &[Stmt],
) -> RhaiResult { ) -> RhaiResult {
let mut this = Dynamic::NULL; let mut this_ptr = Dynamic::NULL;
self.eval_stmt_block(global, caches, scope, &mut this, statements, false) self.eval_stmt_block(global, caches, scope, &mut this_ptr, statements, false)
.or_else(|err| match *err { .or_else(|err| match *err {
ERR::Return(out, ..) => Ok(out), ERR::Return(out, ..) => Ok(out),
ERR::LoopBreak(..) => { ERR::LoopBreak(..) => {

View File

@ -429,7 +429,7 @@ impl Engine {
}; };
if trigger { if trigger {
let scope = &mut Scope::new(); let scope = &mut Scope::new();
let mut this = Dynamic::NULL; let mut this_ptr = Dynamic::NULL;
let node = crate::ast::Stmt::Noop(pos); let node = crate::ast::Stmt::Noop(pos);
let node = (&node).into(); let node = (&node).into();
let event = match _result { let event = match _result {
@ -438,7 +438,7 @@ impl Engine {
}; };
if let Err(err) = if let Err(err) =
self.run_debugger_raw(global, caches, scope, &mut this, node, event) self.run_debugger_raw(global, caches, scope, &mut this_ptr, node, event)
{ {
_result = Err(err); _result = Err(err);
} }
@ -681,9 +681,9 @@ impl Engine {
auto_restore!(args = (_args) if swap => move |a| backup.restore_first_arg(a)); auto_restore!(args = (_args) if swap => move |a| backup.restore_first_arg(a));
let mut this = Dynamic::NULL; let mut this_ptr = Dynamic::NULL;
self.call_script_fn(global, caches, scope, &mut this, func, args, true, pos) self.call_script_fn(global, caches, scope, &mut this_ptr, func, args, true, pos)
} }
.map(|r| (r, false)); .map(|r| (r, false));
} }
@ -1248,7 +1248,7 @@ impl Engine {
pos: Position, pos: Position,
) -> RhaiResult { ) -> RhaiResult {
let mut arg_values = FnArgsVec::with_capacity(args_expr.len()); let mut arg_values = FnArgsVec::with_capacity(args_expr.len());
let mut args = FnArgsVec::with_capacity(args_expr.len()); let args = &mut FnArgsVec::with_capacity(args_expr.len());
let mut first_arg_value = None; let mut first_arg_value = None;
if args_expr.is_empty() { if args_expr.is_empty() {
@ -1366,16 +1366,14 @@ impl Engine {
match func { match func {
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
Some(f) if f.is_script() => { Some(f) if f.is_script() => {
let fn_def = f.get_script_fn_def().expect("script-defined function"); let f = f.get_script_fn_def().expect("script-defined function");
let new_scope = &mut Scope::new(); let scope = &mut Scope::new();
let mut this = Dynamic::NULL; let mut this_ptr = Dynamic::NULL;
let orig_source = mem::replace(&mut global.source, module.id_raw().cloned()); let orig_source = mem::replace(&mut global.source, module.id_raw().cloned());
auto_restore!(global => move |g| g.source = orig_source); auto_restore!(global => move |g| g.source = orig_source);
self.call_script_fn( self.call_script_fn(global, caches, scope, &mut this_ptr, f, args, true, pos)
global, caches, new_scope, &mut this, fn_def, &mut args, true, pos,
)
} }
Some(f) if f.is_plugin_fn() => { Some(f) if f.is_plugin_fn() => {
@ -1388,7 +1386,7 @@ impl Engine {
if !f.is_pure() && !args.is_empty() && args[0].is_read_only() { if !f.is_pure() && !args.is_empty() && args[0].is_read_only() {
Err(ERR::ErrorNonPureMethodCallOnConstant(fn_name.to_string(), pos).into()) Err(ERR::ErrorNonPureMethodCallOnConstant(fn_name.to_string(), pos).into())
} else { } else {
f.call(context, &mut args) f.call(context, args)
.and_then(|r| self.check_data_size(r, pos)) .and_then(|r| self.check_data_size(r, pos))
} }
} }
@ -1400,19 +1398,19 @@ impl Engine {
} else { } else {
None None
}; };
func(context, &mut args).and_then(|r| self.check_data_size(r, pos)) func(context, args).and_then(|r| self.check_data_size(r, pos))
} }
Some(f) => unreachable!("unknown function type: {:?}", f), Some(f) => unreachable!("unknown function type: {:?}", f),
None => { None => {
let sig = if namespace.is_empty() { let sig = if namespace.is_empty() {
self.gen_fn_call_signature(fn_name, &args) self.gen_fn_call_signature(fn_name, args)
} else { } else {
format!( format!(
"{namespace}{}{}", "{namespace}{}{}",
crate::tokenizer::Token::DoubleColon.literal_syntax(), crate::tokenizer::Token::DoubleColon.literal_syntax(),
self.gen_fn_call_signature(fn_name, &args) self.gen_fn_call_signature(fn_name, args)
) )
}; };

View File

@ -2890,9 +2890,8 @@ impl Engine {
will_shadow, will_shadow,
}; };
let caches = &mut Caches::new(); let caches = &mut Caches::new();
let mut this = Dynamic::NULL; let mut this_ptr = Dynamic::NULL;
let context = EvalContext::new(self, global, caches, stack, &mut this_ptr);
let context = EvalContext::new(self, global, caches, stack, &mut this);
match filter(false, info, context) { match filter(false, info, context) {
Ok(true) => (), Ok(true) => (),