Add short-cut to !.

This commit is contained in:
Stephen Chung
2022-11-30 14:11:57 +08:00
parent fc4c8731f0
commit acadb58f4f
3 changed files with 42 additions and 5 deletions

View File

@@ -325,9 +325,9 @@ impl Engine {
}
}
/// # Main Entry-Point
/// # Main Entry-Point (Native by Name)
///
/// Call a native Rust function registered with the [`Engine`].
/// Call a native Rust function registered with the [`Engine`] by name.
///
/// # WARNING
///
@@ -543,9 +543,9 @@ impl Engine {
}
}
/// # Main Entry-Point
/// # Main Entry-Point (By Name)
///
/// Perform an actual function call, native Rust or scripted, taking care of special functions.
/// Perform an actual function call, native Rust or scripted, by name, taking care of special functions.
///
/// # WARNING
///
@@ -1463,6 +1463,8 @@ impl Engine {
self.eval_global_statements(global, caches, scope, statements)
}
/// # Main Entry-Point (`FnCallExpr`)
///
/// Evaluate a function call expression.
pub(crate) fn eval_fn_call_expr(
&self,
@@ -1486,6 +1488,22 @@ impl Engine {
let op_token = op_token.clone();
// Short-circuit native unary operator call if under Fast Operators mode
if op_token == Token::Bang && self.fast_operators() && args.len() == 1 {
let mut value = self
.get_arg_value(global, caches, scope, this_ptr, &args[0])?
.0
.flatten();
return value.as_bool().and_then(|r| Ok((!r).into())).or_else(|_| {
let operand = &mut [&mut value];
self.exec_fn_call(
global, caches, None, name, op_token, *hashes, operand, false, false, pos,
)
.map(|(v, ..)| v)
});
}
// Short-circuit native binary operator call if under Fast Operators mode
if op_token != NO_TOKEN && self.fast_operators() && args.len() == 2 {
let mut lhs = self