Code cleanup.

This commit is contained in:
Stephen Chung
2022-10-30 18:43:18 +08:00
parent 8b773aa15e
commit 22ee12531c
12 changed files with 246 additions and 286 deletions

View File

@@ -269,7 +269,7 @@ impl Engine {
if op_info.is_op_assignment() {
let args = &mut [target.as_mut()];
let (mut orig_val, ..) = self
.call_native_fn(
.exec_native_fn_call(
global, caches, lib, getter, *hash_get, args, is_ref_mut,
false, *pos, level,
)
@@ -303,7 +303,7 @@ impl Engine {
}
let args = &mut [target.as_mut(), &mut new_val];
self.call_native_fn(
self.exec_native_fn_call(
global, caches, lib, setter, *hash_set, args, is_ref_mut, false, *pos,
level,
)
@@ -330,7 +330,7 @@ impl Engine {
let ((getter, hash_get), _, name) = &**x;
let args = &mut [target.as_mut()];
self.call_native_fn(
self.exec_native_fn_call(
global, caches, lib, getter, *hash_get, args, is_ref_mut, false, *pos,
level,
)
@@ -429,7 +429,7 @@ impl Engine {
// Assume getters are always pure
let (mut val, ..) = self
.call_native_fn(
.exec_native_fn_call(
global, caches, lib, getter, *hash_get, args, is_ref_mut,
false, pos, level,
)
@@ -465,7 +465,7 @@ impl Engine {
// Re-use args because the first &mut parameter will not be consumed
let mut arg_values = [target.as_mut(), val.as_mut()];
let args = &mut arg_values;
self.call_native_fn(
self.exec_native_fn_call(
global, caches, lib, setter, *hash_set, args, is_ref_mut,
false, pos, level,
)
@@ -764,7 +764,7 @@ impl Engine {
let pos = Position::NONE;
let level = level + 1;
self.call_native_fn(
self.exec_native_fn_call(
global, caches, lib, fn_name, hash, args, true, false, pos, level,
)
.map(|(r, ..)| r)
@@ -789,7 +789,7 @@ impl Engine {
let pos = Position::NONE;
let level = level + 1;
self.call_native_fn(
self.exec_native_fn_call(
global, caches, lib, fn_name, hash, args, is_ref_mut, false, pos, level,
)
}

View File

@@ -430,7 +430,7 @@ impl Engine {
}
/// Run the debugger callback if there is a debugging interface registered.
///
/// Returns `Some` if the debugger needs to be reactivated at the end of the block, statement or
/// Returns [`Some`] if the debugger needs to be reactivated at the end of the block, statement or
/// function call.
///
/// It is up to the [`Engine`] to reactivate the debugger.
@@ -452,7 +452,7 @@ impl Engine {
}
/// Run the debugger callback.
///
/// Returns `Some` if the debugger needs to be reactivated at the end of the block, statement or
/// Returns [`Some`] if the debugger needs to be reactivated at the end of the block, statement or
/// function call.
///
/// It is up to the [`Engine`] to reactivate the debugger.
@@ -498,7 +498,7 @@ impl Engine {
}
/// Run the debugger callback unconditionally.
///
/// Returns `Some` if the debugger needs to be reactivated at the end of the block, statement or
/// Returns [`Some`] if the debugger needs to be reactivated at the end of the block, statement or
/// function call.
///
/// It is up to the [`Engine`] to reactivate the debugger.

View File

@@ -1,9 +1,8 @@
//! Module defining functions for evaluating an expression.
use super::{Caches, EvalContext, GlobalRuntimeState, Target};
use crate::ast::{Expr, FnCallExpr, OpAssignment};
use crate::ast::{Expr, OpAssignment};
use crate::engine::{KEYWORD_THIS, OP_CONCAT};
use crate::func::get_builtin_binary_op_fn;
use crate::types::dynamic::AccessMode;
use crate::{Dynamic, Engine, Module, Position, RhaiResult, RhaiResultOf, Scope, ERR};
use std::num::NonZeroUsize;
@@ -206,89 +205,6 @@ impl Engine {
Ok((val.into(), var_pos))
}
/// Evaluate a function call expression.
pub(crate) fn eval_fn_call_expr(
&self,
scope: &mut Scope,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[&Module],
this_ptr: &mut Option<&mut Dynamic>,
expr: &FnCallExpr,
pos: Position,
level: usize,
) -> RhaiResult {
let FnCallExpr {
name,
hashes,
args,
operator_token,
..
} = expr;
// Short-circuit native binary operator call if under Fast Operators mode
if operator_token.is_some() && self.fast_operators() && args.len() == 2 {
let mut lhs = self
.get_arg_value(scope, global, caches, lib, this_ptr, &args[0], level)?
.0
.flatten();
let mut rhs = self
.get_arg_value(scope, global, caches, lib, this_ptr, &args[1], level)?
.0
.flatten();
let operands = &mut [&mut lhs, &mut rhs];
if let Some(func) =
get_builtin_binary_op_fn(operator_token.as_ref().unwrap(), operands[0], operands[1])
{
// Built-in found
let context = (self, name.as_str(), None, &*global, lib, pos, level + 1).into();
return func(context, operands);
}
return self
.exec_fn_call(
None, global, caches, lib, name, *hashes, operands, false, false, pos, level,
)
.map(|(v, ..)| v);
}
#[cfg(not(feature = "no_module"))]
if !expr.namespace.is_empty() {
// Qualified function call
let hash = hashes.native();
let namespace = &expr.namespace;
return self.make_qualified_function_call(
scope, global, caches, lib, this_ptr, namespace, name, args, hash, pos, level,
);
}
// Normal function call
let (first_arg, args) = args.split_first().map_or_else(
|| (None, args.as_ref()),
|(first, rest)| (Some(first), rest),
);
self.make_function_call(
scope,
global,
caches,
lib,
this_ptr,
name,
first_arg,
args,
*hashes,
expr.capture_parent_scope,
expr.operator_token.as_ref(),
pos,
level,
)
}
/// Evaluate an expression.
//
// # Implementation Notes
@@ -312,7 +228,7 @@ impl Engine {
// Function calls should account for a relatively larger portion of expressions because
// binary operators are also function calls.
if let Expr::FnCall(x, ..) = expr {
if let Expr::FnCall(x, pos) = expr {
#[cfg(feature = "debugging")]
let reset_debugger =
self.run_debugger_with_reset(scope, global, lib, this_ptr, expr, level)?;
@@ -320,7 +236,7 @@ impl Engine {
self.track_operation(global, expr.position())?;
let result =
self.eval_fn_call_expr(scope, global, caches, lib, this_ptr, x, x.pos, level);
self.eval_fn_call_expr(scope, global, caches, lib, this_ptr, x, *pos, level);
#[cfg(feature = "debugging")]
global.debugger.reset_status(reset_debugger);

View File

@@ -153,7 +153,7 @@ impl Engine {
let op_assign = op_assign.literal_syntax();
let op = op.literal_syntax();
match self.call_native_fn(
match self.exec_native_fn_call(
global, caches, lib, op_assign, hash, args, true, true, *op_pos, level,
) {
Ok(_) => (),
@@ -161,7 +161,7 @@ impl Engine {
{
// Expand to `var = var op rhs`
*args[0] = self
.call_native_fn(
.exec_native_fn_call(
global, caches, lib, op, *hash_op, args, true, false, *op_pos, level,
)
.map_err(|err| err.fill_position(op_info.pos))?
@@ -207,11 +207,11 @@ impl Engine {
// Popular branches are lifted out of the `match` statement into their own branches.
// Function calls should account for a relatively larger portion of statements.
if let Stmt::FnCall(x, ..) = stmt {
if let Stmt::FnCall(x, pos) = stmt {
self.track_operation(global, stmt.position())?;
let result =
self.eval_fn_call_expr(scope, global, caches, lib, this_ptr, x, x.pos, level);
self.eval_fn_call_expr(scope, global, caches, lib, this_ptr, x, *pos, level);
#[cfg(feature = "debugging")]
global.debugger.reset_status(reset_debugger);
@@ -1006,4 +1006,28 @@ impl Engine {
result
}
/// Evaluate a list of statements with no `this` pointer.
/// This is commonly used to evaluate a list of statements in an [`AST`][crate::AST] or a script function body.
#[inline]
pub(crate) fn eval_global_statements(
&self,
scope: &mut Scope,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
statements: &[Stmt],
lib: &[&Module],
level: usize,
) -> RhaiResult {
self.eval_stmt_block(
scope, global, caches, lib, &mut None, statements, false, level,
)
.or_else(|err| match *err {
ERR::Return(out, ..) => Ok(out),
ERR::LoopBreak(..) => {
unreachable!("no outer loop scope to break out of")
}
_ => Err(err),
})
}
}