Change fast_ops to options.

This commit is contained in:
Stephen Chung
2022-09-03 15:15:42 +08:00
parent 43c4d7e3ca
commit 06dea067b7
8 changed files with 53 additions and 48 deletions

View File

@@ -7,26 +7,28 @@ use std::prelude::v1::*;
bitflags! {
/// Bit-flags containing all language options for the [`Engine`].
pub struct LangOptions: u8 {
pub struct LangOptions: u16 {
/// Is `if`-expression allowed?
const IF_EXPR = 0b_0000_0001;
const IF_EXPR = 0b_0000_0000_0001;
/// Is `switch` expression allowed?
const SWITCH_EXPR = 0b_0000_0010;
const SWITCH_EXPR = 0b_0000_0000_0010;
/// Is statement-expression allowed?
const STMT_EXPR = 0b_0000_0100;
const STMT_EXPR = 0b_0000_0000_0100;
/// Is anonymous function allowed?
#[cfg(not(feature = "no_function"))]
const ANON_FN = 0b_0000_1000;
const ANON_FN = 0b_0000_0000_1000;
/// Is looping allowed?
const LOOPING = 0b_0001_0000;
const LOOPING = 0b_0000_0001_0000;
/// Is variables shadowing allowed?
const SHADOW = 0b_0010_0000;
const SHADOW = 0b_0000_0010_0000;
/// Strict variables mode?
const STRICT_VAR = 0b_0100_0000;
const STRICT_VAR = 0b_0000_0100_0000;
/// Raise error if an object map property does not exist?
/// Returns `()` if `false`.
#[cfg(not(feature = "no_object"))]
const FAIL_ON_INVALID_MAP_PROPERTY = 0b_1000_0000;
const FAIL_ON_INVALID_MAP_PROPERTY = 0b_0000_1000_0000;
/// Fast operators mode?
const FAST_OPS = 0b_0001_0000_0000;
}
}
@@ -158,4 +160,16 @@ impl Engine {
self.options
.set(LangOptions::FAIL_ON_INVALID_MAP_PROPERTY, enable);
}
/// Is fast operators mode enabled?
/// Default is `false`.
#[inline(always)]
#[must_use]
pub const fn fast_operators(&self) -> bool {
self.options.contains(LangOptions::FAST_OPS)
}
/// Set whether fast operators mode is enabled.
#[inline(always)]
pub fn set_fast_operators(&mut self, enable: bool) {
self.options.set(LangOptions::FAST_OPS, enable);
}
}

View File

@@ -228,8 +228,7 @@ impl Engine {
..
} = expr;
#[cfg(feature = "fast_ops")]
if *std_ops {
if *std_ops && self.fast_operators() {
let mut lhs = self
.get_arg_value(scope, global, caches, lib, this_ptr, &args[0], level)?
.0

View File

@@ -1181,7 +1181,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, _chaining: bool) {
return;
}
// Overloaded operators can override built-in.
_ if x.args.len() == 2 && (cfg!(feature = "fast_ops") || !has_native_fn_override(state.engine, x.hashes.native, &arg_types)) => {
_ if x.args.len() == 2 && (state.engine.fast_operators() || !has_native_fn_override(state.engine, x.hashes.native, &arg_types)) => {
if let Some(result) = get_builtin_binary_op_fn(&x.name, &arg_values[0], &arg_values[1])
.and_then(|f| {
#[cfg(not(feature = "no_function"))]