Move optimization_level into Options.

This commit is contained in:
Stephen Chung 2022-03-29 08:26:42 +08:00
parent 1b6de25007
commit 56f6b181db
6 changed files with 24 additions and 17 deletions

View File

@ -201,7 +201,11 @@ impl Engine {
scope: &Scope,
scripts: impl AsRef<[S]>,
) -> ParseResult<AST> {
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,

View File

@ -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)
}

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.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.

View File

@ -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,

View File

@ -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)

View File

@ -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<Box<crate::func::native::OnProgressCallback>>,
/// 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"))]