diff --git a/src/api/compile.rs b/src/api/compile.rs index 4a3c0490..53402446 100644 --- a/src/api/compile.rs +++ b/src/api/compile.rs @@ -201,7 +201,11 @@ impl Engine { scope: &Scope, scripts: impl AsRef<[S]>, ) -> ParseResult { - self.compile_with_scope_and_optimization_level(scope, scripts, self.optimization_level) + self.compile_with_scope_and_optimization_level( + scope, + scripts, + self.options.optimization_level, + ) } /// Join a list of strings and compile into an [`AST`] using own scope at a specific optimization level. /// @@ -297,7 +301,12 @@ impl Engine { let mut peekable = stream.peekable(); let mut state = ParseState::new(self, tokenizer_control); - self.parse_global_expr(&mut peekable, &mut state, scope, self.optimization_level) + self.parse_global_expr( + &mut peekable, + &mut state, + scope, + self.options.optimization_level, + ) } /// Parse a JSON string into an [object map][crate::Map]. /// This is a light-weight alternative to using, say, diff --git a/src/api/eval.rs b/src/api/eval.rs index 20d4655b..f83da77f 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.optimization_level, + self.options.optimization_level, )?; self.eval_ast_with_scope(scope, &ast) } diff --git a/src/api/optimize.rs b/src/api/optimize.rs index 7589c2fd..dd0ca118 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.optimization_level = optimization_level; + self.options.optimization_level = optimization_level; self } @@ -20,7 +20,7 @@ impl Engine { #[inline(always)] #[must_use] pub const fn optimization_level(&self) -> OptimizationLevel { - self.optimization_level + self.options.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 619be34c..dba11f4b 100644 --- a/src/api/options.rs +++ b/src/api/options.rs @@ -1,12 +1,14 @@ //! Settings for [`Engine`]'s language options. -use crate::Engine; +use crate::{Engine, OptimizationLevel}; #[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? @@ -33,6 +35,11 @@ impl LanguageOptions { #[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, diff --git a/src/api/run.rs b/src/api/run.rs index bad510a7..25fd69c7 100644 --- a/src/api/run.rs +++ b/src/api/run.rs @@ -30,7 +30,7 @@ impl Engine { &mut stream.peekable(), &mut state, scope, - self.optimization_level, + self.options.optimization_level, )?; self.run_ast_with_scope(scope, &ast) diff --git a/src/engine.rs b/src/engine.rs index 6b7614af..192416d0 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -9,8 +9,7 @@ use crate::packages::{Package, StandardPackage}; use crate::tokenizer::Token; use crate::types::dynamic::Union; use crate::{ - Dynamic, Identifier, ImmutableString, Module, OptimizationLevel, Position, RhaiResult, Shared, - StaticVec, + Dynamic, Identifier, ImmutableString, Module, Position, RhaiResult, Shared, StaticVec, }; #[cfg(feature = "no_std")] use std::prelude::v1::*; @@ -129,9 +128,6 @@ pub struct Engine { #[cfg(not(feature = "unchecked"))] pub(crate) progress: Option>, - /// Optimize the [`AST`][crate::AST] after compilation. - pub(crate) optimization_level: OptimizationLevel, - /// Language options. pub(crate) options: LanguageOptions, @@ -167,9 +163,6 @@ impl fmt::Debug for Engine { #[cfg(not(feature = "unchecked"))] f.field("progress", &self.progress.is_some()); - #[cfg(not(feature = "no_optimize"))] - f.field("optimization_level", &self.optimization_level); - f.field("options", &self.options); #[cfg(not(feature = "unchecked"))] @@ -274,8 +267,6 @@ impl Engine { #[cfg(not(feature = "unchecked"))] progress: None, - optimization_level: OptimizationLevel::default(), - options: LanguageOptions::new(), #[cfg(not(feature = "unchecked"))]