diff --git a/src/api/compile.rs b/src/api/compile.rs index ab82f90a..2250b641 100644 --- a/src/api/compile.rs +++ b/src/api/compile.rs @@ -201,11 +201,7 @@ impl Engine { scope: &Scope, scripts: impl AsRef<[S]>, ) -> ParseResult { - 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) } } diff --git a/src/api/eval.rs b/src/api/eval.rs index 0abb366c..aec12d99 100644 --- a/src/api/eval.rs +++ b/src/api/eval.rs @@ -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) } diff --git a/src/api/optimize.rs b/src/api/optimize.rs index dd0ca118..7589c2fd 100644 --- a/src/api/optimize.rs +++ b/src/api/optimize.rs @@ -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. diff --git a/src/api/options.rs b/src/api/options.rs index dba11f4b..1a098926 100644 --- a/src/api/options.rs +++ b/src/api/options.rs @@ -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); } } diff --git a/src/api/run.rs b/src/api/run.rs index b79a168d..673fa19e 100644 --- a/src/api/run.rs +++ b/src/api/run.rs @@ -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) } diff --git a/src/ast/flags.rs b/src/ast/flags.rs index 34a3bbaa..837a9d80 100644 --- a/src/ast/flags.rs +++ b/src/ast/flags.rs @@ -15,7 +15,7 @@ pub enum FnAccess { } bitflags! { - /// _(internals)_ A type that holds a configuration option with bit-flags. + /// _(internals)_ Bit-flags containing [`AST`][crate::AST] node configuration options. /// Exported under the `internals` feature only. pub struct ASTFlags: u8 { /// No options for the [`AST`][crate::AST] node. diff --git a/src/engine.rs b/src/engine.rs index da83207e..8a4e8841 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1,7 +1,7 @@ //! Main module defining the script evaluation [`Engine`]. use crate::api::custom_syntax::CustomSyntax; -use crate::api::options::LanguageOptions; +use crate::api::options::LangOptions; use crate::func::native::{ OnDebugCallback, OnDefVarCallback, OnParseTokenCallback, OnPrintCallback, OnVarCallback, }; @@ -9,7 +9,8 @@ use crate::packages::{Package, StandardPackage}; use crate::tokenizer::Token; use crate::types::dynamic::Union; use crate::{ - Dynamic, Identifier, ImmutableString, Module, Position, RhaiResult, Shared, StaticVec, + Dynamic, Identifier, ImmutableString, Module, OptimizationLevel, Position, RhaiResult, Shared, + StaticVec, }; #[cfg(feature = "no_std")] use std::prelude::v1::*; @@ -129,7 +130,10 @@ pub struct Engine { pub(crate) progress: Option>, /// Language options. - pub(crate) options: LanguageOptions, + pub(crate) options: LangOptions, + + /// Script optimization level. + pub optimization_level: OptimizationLevel, /// Max limits. #[cfg(not(feature = "unchecked"))] @@ -274,7 +278,12 @@ impl Engine { #[cfg(not(feature = "unchecked"))] progress: None, - options: LanguageOptions::new(), + options: LangOptions::new(), + + #[cfg(not(feature = "no_optimize"))] + optimization_level: OptimizationLevel::Simple, + #[cfg(feature = "no_optimize")] + optimization_level: (), #[cfg(not(feature = "unchecked"))] limits: crate::api::limits::Limits::new(), diff --git a/src/parser.rs b/src/parser.rs index 6726d1fe..0086f813 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -2,7 +2,7 @@ use crate::api::custom_syntax::{markers::*, CustomSyntax}; use crate::api::events::VarDefInfo; -use crate::api::options::LanguageOptions; +use crate::api::options::LangOptions; use crate::ast::{ ASTFlags, BinaryExpr, ConditionalStmtBlock, CustomExpr, Expr, FnCallExpr, FnCallHashes, Ident, OpAssignment, ScriptFnDef, Stmt, StmtBlock, StmtBlockContainer, SwitchCases, TryCatchBlock, @@ -215,7 +215,7 @@ struct ParseSettings { /// Is the current position inside a loop? is_breakable: bool, /// Language options in effect (overrides Engine options). - options: LanguageOptions, + options: LangOptions, /// Current expression nesting level. level: usize, /// Current position. @@ -501,7 +501,10 @@ impl Engine { #[cfg(feature = "no_function")] let relax = false; - if !relax && settings.options.strict_var && index.is_none() { + if !relax + && settings.options.contains(LangOptions::STRICT_VAR) + && index.is_none() + { return Err(PERR::ModuleUndefined(namespace.root().to_string()) .into_err(namespace.position())); } @@ -561,7 +564,10 @@ impl Engine { #[cfg(feature = "no_function")] let relax = false; - if !relax && settings.options.strict_var && index.is_none() { + if !relax + && settings.options.contains(LangOptions::STRICT_VAR) + && index.is_none() + { return Err(PERR::ModuleUndefined(namespace.root().to_string()) .into_err(namespace.position())); } @@ -1234,7 +1240,7 @@ impl Engine { } // { - block statement as expression - Token::LeftBrace if settings.options.allow_stmt_expr => { + Token::LeftBrace if settings.options.contains(LangOptions::STMT_EXPR) => { match self.parse_block(input, state, lib, settings.level_up())? { block @ Stmt::Block(..) => Expr::Stmt(Box::new(block.into())), stmt => unreachable!("Stmt::Block expected but gets {:?}", stmt), @@ -1245,19 +1251,21 @@ impl Engine { Token::LeftParen => self.parse_paren_expr(input, state, lib, settings.level_up())?, // If statement is allowed to act as expressions - Token::If if settings.options.allow_if_expr => Expr::Stmt(Box::new( + Token::If if settings.options.contains(LangOptions::IF_EXPR) => Expr::Stmt(Box::new( self.parse_if(input, state, lib, settings.level_up())? .into(), )), // Switch statement is allowed to act as expressions - Token::Switch if settings.options.allow_switch_expr => Expr::Stmt(Box::new( - self.parse_switch(input, state, lib, settings.level_up())? - .into(), - )), + Token::Switch if settings.options.contains(LangOptions::SWITCH_EXPR) => { + Expr::Stmt(Box::new( + self.parse_switch(input, state, lib, settings.level_up())? + .into(), + )) + } // | ... #[cfg(not(feature = "no_function"))] - Token::Pipe | Token::Or if settings.options.allow_anonymous_fn => { + Token::Pipe | Token::Or if settings.options.contains(LangOptions::ANON_FN) => { let mut new_state = ParseState::new(self, state.scope, state.tokenizer_control.clone()); @@ -1266,6 +1274,17 @@ impl Engine { new_state.max_expr_depth = self.max_function_expr_depth(); } + let mut options = self.options; + options.set( + LangOptions::STRICT_VAR, + if cfg!(feature = "no_closure") { + settings.options.contains(LangOptions::STRICT_VAR) + } else { + // A capturing closure can access variables not defined locally + false + }, + ); + let new_settings = ParseSettings { is_global: false, is_function_scope: true, @@ -1273,15 +1292,7 @@ impl Engine { is_closure_scope: true, is_breakable: false, level: 0, - options: LanguageOptions { - strict_var: if cfg!(feature = "no_closure") { - settings.options.strict_var - } else { - // A capturing closure can access variables not defined locally - false - }, - ..self.options - }, + options, ..settings }; @@ -1292,7 +1303,7 @@ impl Engine { |crate::ast::Ident { name, pos }| { let index = state.access_var(name, *pos); - if settings.options.strict_var + if settings.options.contains(LangOptions::STRICT_VAR) && !settings.is_closure_scope && index.is_none() && !state.scope.contains(name) @@ -1439,7 +1450,7 @@ impl Engine { _ => { let index = state.access_var(&s, settings.pos); - if settings.options.strict_var + if settings.options.contains(LangOptions::STRICT_VAR) && index.is_none() && !state.scope.contains(&s) { @@ -1674,7 +1685,10 @@ impl Engine { #[cfg(feature = "no_function")] let relax = false; - if !relax && settings.options.strict_var && index.is_none() { + if !relax + && settings.options.contains(LangOptions::STRICT_VAR) + && index.is_none() + { return Err(PERR::ModuleUndefined(namespace.root().to_string()) .into_err(namespace.position())); } @@ -3072,6 +3086,12 @@ impl Engine { new_state.max_expr_depth = self.max_function_expr_depth(); } + let mut options = self.options; + options.set( + LangOptions::STRICT_VAR, + settings.options.contains(LangOptions::STRICT_VAR), + ); + let new_settings = ParseSettings { is_global: false, is_function_scope: true, @@ -3079,10 +3099,7 @@ impl Engine { is_closure_scope: false, is_breakable: false, level: 0, - options: LanguageOptions { - strict_var: settings.options.strict_var, - ..self.options - }, + options, pos, ..settings }; @@ -3541,6 +3558,11 @@ impl Engine { ) -> ParseResult { let mut functions = BTreeMap::new(); + let mut options = self.options; + options.remove(LangOptions::IF_EXPR | LangOptions::SWITCH_EXPR | LangOptions::STMT_EXPR); + #[cfg(not(feature = "no_function"))] + options.remove(LangOptions::ANON_FN); + let settings = ParseSettings { is_global: true, #[cfg(not(feature = "no_function"))] @@ -3550,14 +3572,7 @@ impl Engine { is_closure_scope: false, is_breakable: false, level: 0, - options: LanguageOptions { - allow_if_expr: false, - allow_switch_expr: false, - allow_stmt_expr: false, - #[cfg(not(feature = "no_function"))] - allow_anonymous_fn: false, - ..self.options - }, + options, pos: Position::NONE, }; let expr = self.parse_expr(input, state, &mut functions, settings)?;