Refactor fast operators.

This commit is contained in:
Stephen Chung
2022-09-25 18:22:48 +08:00
parent ce56c43bc0
commit ece522ce2f
4 changed files with 39 additions and 89 deletions

View File

@@ -5,7 +5,7 @@ use crate::api::events::VarDefInfo;
use crate::ast::{
ASTFlags, BinaryExpr, Expr, Ident, OpAssignment, Stmt, SwitchCasesCollection, TryCatchBlock,
};
use crate::func::get_hasher;
use crate::func::{get_builtin_op_assignment_fn, get_hasher};
use crate::types::dynamic::{AccessMode, Union};
use crate::{
Dynamic, Engine, ImmutableString, Module, Position, RhaiResult, RhaiResultOf, Scope, ERR, INT,
@@ -145,6 +145,19 @@ impl Engine {
let args = &mut [&mut *lock_guard, &mut new_val];
let level = level + 1;
if self.fast_operators() {
if let Some(func) = get_builtin_op_assignment_fn(op_assign, args[0], args[1]) {
// Built-in found
let context = (self, op_assign, None, &*global, lib, op_pos, level).into();
let result = func(context, args).map(|_| ());
#[cfg(not(feature = "unchecked"))]
self.check_data_size(args[0], root.1)?;
return result;
}
}
match self.call_native_fn(
global, caches, lib, op_assign, hash, args, true, true, op_pos, level,
) {
@@ -155,16 +168,13 @@ impl Engine {
Err(err) if matches!(*err, ERR::ErrorFunctionNotFound(ref f, ..) if f.starts_with(op_assign)) =>
{
// Expand to `var = var op rhs`
let (value, ..) = self
*args[0] = self
.call_native_fn(
global, caches, lib, op, hash_op, args, true, false, op_pos, level,
)
.map_err(|err| err.fill_position(op_info.pos))?;
#[cfg(not(feature = "unchecked"))]
self.check_data_size(&value, root.1)?;
*args[0] = value.flatten();
.map_err(|err| err.fill_position(op_info.pos))?
.0
.flatten();
}
Err(err) => return Err(err),
}