Use bit-flags for options.

This commit is contained in:
Stephen Chung
2022-05-19 21:40:22 +08:00
parent 42d2718e24
commit 130b93d029
8 changed files with 124 additions and 121 deletions

View File

@@ -201,11 +201,7 @@ impl Engine {
scope: &Scope,
scripts: impl AsRef<[S]>,
) -> ParseResult<AST> {
self.compile_with_scope_and_optimization_level(
scope,
scripts,
self.options.optimization_level,
)
self.compile_with_scope_and_optimization_level(scope, scripts, self.optimization_level)
}
/// Join a list of strings and compile into an [`AST`] using own scope at a specific optimization level.
///
@@ -296,6 +292,6 @@ impl Engine {
let mut peekable = stream.peekable();
let mut state = ParseState::new(self, scope, tokenizer_control);
self.parse_global_expr(&mut peekable, &mut state, self.options.optimization_level)
self.parse_global_expr(&mut peekable, &mut state, self.optimization_level)
}
}

View File

@@ -67,7 +67,7 @@ impl Engine {
let ast = self.compile_with_scope_and_optimization_level(
scope,
&[script],
self.options.optimization_level,
self.optimization_level,
)?;
self.eval_ast_with_scope(scope, &ast)
}

View File

@@ -9,7 +9,7 @@ impl Engine {
/// Not available under `no_optimize`.
#[inline(always)]
pub fn set_optimization_level(&mut self, optimization_level: OptimizationLevel) -> &mut Self {
self.options.optimization_level = optimization_level;
self.optimization_level = optimization_level;
self
}
@@ -20,7 +20,7 @@ impl Engine {
#[inline(always)]
#[must_use]
pub const fn optimization_level(&self) -> OptimizationLevel {
self.options.optimization_level
self.optimization_level
}
/// Optimize the [`AST`] with constants defined in an external Scope.

View File

@@ -1,62 +1,47 @@
//! Settings for [`Engine`]'s language options.
use crate::{Engine, OptimizationLevel};
use crate::Engine;
use bitflags::bitflags;
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
/// A type containing all language options for the [`Engine`].
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct LanguageOptions {
/// Script optimization level.
pub optimization_level: OptimizationLevel,
/// Is `if`-expression allowed?
pub allow_if_expr: bool,
/// Is `switch` expression allowed?
pub allow_switch_expr: bool,
/// Is statement-expression allowed?
pub allow_stmt_expr: bool,
/// Is anonymous function allowed?
#[cfg(not(feature = "no_function"))]
pub allow_anonymous_fn: bool,
/// Is looping allowed?
pub allow_looping: bool,
/// Is variables shadowing allowed?
pub allow_shadowing: bool,
/// Strict variables mode?
pub strict_var: bool,
/// Raise error if an object map property does not exist?
/// Returns `()` if `false`.
#[cfg(not(feature = "no_object"))]
pub fail_on_invalid_map_property: bool,
}
impl LanguageOptions {
/// Create a new [`Options`] with default values.
#[inline(always)]
pub const fn new() -> Self {
Self {
#[cfg(not(feature = "no_optimize"))]
optimization_level: OptimizationLevel::Simple,
#[cfg(feature = "no_optimize")]
optimization_level: (),
allow_if_expr: true,
allow_switch_expr: true,
allow_stmt_expr: true,
#[cfg(not(feature = "no_function"))]
allow_anonymous_fn: true,
allow_looping: true,
strict_var: false,
allow_shadowing: true,
#[cfg(not(feature = "no_object"))]
fail_on_invalid_map_property: false,
}
bitflags! {
/// Bit-flags containing all language options for the [`Engine`].
pub struct LangOptions: u8 {
/// Is `if`-expression allowed?
const IF_EXPR = 0b_00000001;
/// Is `switch` expression allowed?
const SWITCH_EXPR = 0b_00000010;
/// Is statement-expression allowed?
const STMT_EXPR = 0b_00000100;
/// Is anonymous function allowed?
#[cfg(not(feature = "no_function"))]
const ANON_FN = 0b_00001000;
/// Is looping allowed?
const LOOPING = 0b_00010000;
/// Is variables shadowing allowed?
const SHADOW = 0b_00100000;
/// Strict variables mode?
const STRICT_VAR = 0b_01000000;
/// 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_10000000;
}
}
impl Default for LanguageOptions {
fn default() -> Self {
Self::new()
impl LangOptions {
/// Create a new [`Options`] with default values.
#[inline(always)]
pub fn new() -> Self {
Self::IF_EXPR | Self::SWITCH_EXPR | Self::STMT_EXPR | Self::LOOPING | Self::SHADOW | {
#[cfg(not(feature = "no_function"))]
{
Self::ANON_FN
}
#[cfg(feature = "no_function")]
0
}
}
}
@@ -65,34 +50,34 @@ impl Engine {
/// Default is `true`.
#[inline(always)]
pub const fn allow_if_expression(&self) -> bool {
self.options.allow_if_expr
self.options.contains(LangOptions::IF_EXPR)
}
/// Set whether `if`-expression is allowed.
#[inline(always)]
pub fn set_allow_if_expression(&mut self, enable: bool) {
self.options.allow_if_expr = enable;
self.options.set(LangOptions::IF_EXPR, enable)
}
/// Is `switch` expression allowed?
/// Default is `true`.
#[inline(always)]
pub const fn allow_switch_expression(&self) -> bool {
self.options.allow_switch_expr
self.options.contains(LangOptions::SWITCH_EXPR)
}
/// Set whether `switch` expression is allowed.
#[inline(always)]
pub fn set_allow_switch_expression(&mut self, enable: bool) {
self.options.allow_switch_expr = enable;
self.options.set(LangOptions::SWITCH_EXPR, enable);
}
/// Is statement-expression allowed?
/// Default is `true`.
#[inline(always)]
pub const fn allow_statement_expression(&self) -> bool {
self.options.allow_stmt_expr
self.options.contains(LangOptions::STMT_EXPR)
}
/// Set whether statement-expression is allowed.
#[inline(always)]
pub fn set_allow_statement_expression(&mut self, enable: bool) {
self.options.allow_stmt_expr = enable;
self.options.set(LangOptions::STMT_EXPR, enable);
}
/// Is anonymous function allowed?
/// Default is `true`.
@@ -101,7 +86,7 @@ impl Engine {
#[cfg(not(feature = "no_function"))]
#[inline(always)]
pub const fn allow_anonymous_fn(&self) -> bool {
self.options.allow_anonymous_fn
self.options.contains(LangOptions::ANON_FN)
}
/// Set whether anonymous function is allowed.
///
@@ -109,40 +94,40 @@ impl Engine {
#[cfg(not(feature = "no_function"))]
#[inline(always)]
pub fn set_allow_anonymous_fn(&mut self, enable: bool) {
self.options.allow_anonymous_fn = enable;
self.options.set(LangOptions::ANON_FN, enable);
}
/// Is looping allowed?
/// Default is `true`.
#[inline(always)]
pub const fn allow_looping(&self) -> bool {
self.options.allow_looping
self.options.contains(LangOptions::LOOPING)
}
/// Set whether looping is allowed.
#[inline(always)]
pub fn set_allow_looping(&mut self, enable: bool) {
self.options.allow_looping = enable;
self.options.set(LangOptions::LOOPING, enable);
}
/// Is variables shadowing allowed?
/// Default is `true`.
#[inline(always)]
pub const fn allow_shadowing(&self) -> bool {
self.options.allow_shadowing
self.options.contains(LangOptions::SHADOW)
}
/// Set whether variables shadowing is allowed.
#[inline(always)]
pub fn set_allow_shadowing(&mut self, enable: bool) {
self.options.allow_shadowing = enable;
self.options.set(LangOptions::SHADOW, enable);
}
/// Is strict variables mode enabled?
/// Default is `false`.
#[inline(always)]
pub const fn strict_variables(&self) -> bool {
self.options.strict_var
self.options.contains(LangOptions::STRICT_VAR)
}
/// Set whether strict variables mode is enabled.
#[inline(always)]
pub fn set_strict_variables(&mut self, enable: bool) {
self.options.strict_var = enable;
self.options.set(LangOptions::STRICT_VAR, enable);
}
/// Raise error if an object map property does not exist?
/// Default is `false`.
@@ -151,7 +136,8 @@ impl Engine {
#[cfg(not(feature = "no_object"))]
#[inline(always)]
pub const fn fail_on_invalid_map_property(&self) -> bool {
self.options.fail_on_invalid_map_property
self.options
.contains(LangOptions::FAIL_ON_INVALID_MAP_PROPERTY)
}
/// Set whether to raise error if an object map property does not exist.
///
@@ -159,6 +145,7 @@ impl Engine {
#[cfg(not(feature = "no_object"))]
#[inline(always)]
pub fn set_fail_on_invalid_map_property(&mut self, enable: bool) {
self.options.fail_on_invalid_map_property = enable;
self.options
.set(LangOptions::FAIL_ON_INVALID_MAP_PROPERTY, enable);
}
}

View File

@@ -26,11 +26,7 @@ impl Engine {
self.lex_raw(&scripts, self.token_mapper.as_ref().map(Box::as_ref));
let mut state = ParseState::new(self, scope, tokenizer_control);
let ast = self.parse(
&mut stream.peekable(),
&mut state,
self.options.optimization_level,
)?;
let ast = self.parse(&mut stream.peekable(), &mut state, self.optimization_level)?;
self.run_ast_with_scope(scope, &ast)
}