Refactor OptimizationLevel.

This commit is contained in:
Stephen Chung 2022-02-07 21:03:39 +08:00
parent 556b2393f5
commit 187a20fd8b
9 changed files with 34 additions and 36 deletions

View File

@ -1,7 +1,7 @@
//! Module that defines the public compilation API of [`Engine`].
use crate::parser::{ParseResult, ParseState};
use crate::{Engine, Scope, AST};
use crate::{Engine, OptimizationLevel, Scope, AST};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@ -196,12 +196,7 @@ impl Engine {
scope: &Scope,
scripts: impl AsRef<[S]>,
) -> ParseResult<AST> {
self.compile_with_scope_and_optimization_level(
scope,
scripts,
#[cfg(not(feature = "no_optimize"))]
self.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.
///
@ -215,7 +210,7 @@ impl Engine {
&self,
scope: &Scope,
scripts: impl AsRef<[S]>,
#[cfg(not(feature = "no_optimize"))] optimization_level: crate::OptimizationLevel,
optimization_level: OptimizationLevel,
) -> ParseResult<AST> {
let (stream, tokenizer_control) = self.lex_raw(
scripts.as_ref(),
@ -226,7 +221,6 @@ impl Engine {
&mut stream.peekable(),
&mut state,
scope,
#[cfg(not(feature = "no_optimize"))]
optimization_level,
)
}
@ -298,13 +292,7 @@ impl Engine {
let mut peekable = stream.peekable();
let mut state = ParseState::new(self, tokenizer_control);
self.parse_global_expr(
&mut peekable,
&mut state,
scope,
#[cfg(not(feature = "no_optimize"))]
self.optimization_level,
)
self.parse_global_expr(&mut peekable, &mut state, scope, self.optimization_level)
}
/// Parse a JSON string into an [object map][crate::Map].
/// This is a light-weight alternative to using, say,
@ -400,7 +388,9 @@ impl Engine {
&mut state,
&scope,
#[cfg(not(feature = "no_optimize"))]
crate::OptimizationLevel::None,
OptimizationLevel::None,
#[cfg(feature = "no_optimize")]
OptimizationLevel::default(),
)?;
if has_null {
scope.push_constant("null", ());

View File

@ -3,7 +3,9 @@
use crate::eval::{EvalState, GlobalRuntimeState};
use crate::parser::ParseState;
use crate::types::dynamic::Variant;
use crate::{Dynamic, Engine, Module, Position, RhaiResult, RhaiResultOf, Scope, AST, ERR};
use crate::{
Dynamic, Engine, Module, OptimizationLevel, Position, RhaiResult, RhaiResultOf, Scope, AST, ERR,
};
use std::any::type_name;
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@ -64,7 +66,6 @@ impl Engine {
let ast = self.compile_with_scope_and_optimization_level(
scope,
&[script],
#[cfg(not(feature = "no_optimize"))]
self.optimization_level,
)?;
self.eval_ast_with_scope(scope, &ast)
@ -122,7 +123,9 @@ impl Engine {
&mut state,
scope,
#[cfg(not(feature = "no_optimize"))]
crate::OptimizationLevel::None,
OptimizationLevel::None,
#[cfg(feature = "no_optimize")]
OptimizationLevel::default(),
)?;
self.eval_ast_with_scope(scope, &ast)

View File

@ -30,7 +30,6 @@ impl Engine {
&mut stream.peekable(),
&mut state,
scope,
#[cfg(not(feature = "no_optimize"))]
self.optimization_level,
)?;

View File

@ -1,8 +1,5 @@
use rhai::{Engine, EvalAltResult, Position};
#[cfg(not(feature = "no_optimize"))]
use rhai::OptimizationLevel;
use std::{env, fs::File, io::Read, path::Path, process::exit};
fn eprint_error(input: &str, mut err: EvalAltResult) {
@ -53,7 +50,7 @@ fn main() {
let mut engine = Engine::new();
#[cfg(not(feature = "no_optimize"))]
engine.set_optimization_level(OptimizationLevel::Full);
engine.set_optimization_level(rhai::OptimizationLevel::Full);
let mut f = match File::open(&filename) {
Err(err) => {

View File

@ -8,7 +8,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::*;
@ -131,8 +132,7 @@ pub struct Engine {
pub(crate) progress: Option<Box<crate::func::native::OnProgressCallback>>,
/// Optimize the [`AST`][crate::AST] after compilation.
#[cfg(not(feature = "no_optimize"))]
pub(crate) optimization_level: crate::OptimizationLevel,
pub(crate) optimization_level: OptimizationLevel,
/// Language options.
pub(crate) options: crate::api::options::LanguageOptions,
@ -287,8 +287,7 @@ impl Engine {
#[cfg(not(feature = "unchecked"))]
progress: None,
#[cfg(not(feature = "no_optimize"))]
optimization_level: crate::OptimizationLevel::default(),
optimization_level: OptimizationLevel::default(),
options: crate::api::options::LanguageOptions::new(),

View File

@ -10,7 +10,7 @@ use std::prelude::v1::*;
impl Engine {
/// Recursively calculate the sizes of a value.
///
/// Sizes returned are `(`[`Array`][crate::Array], [`Map`][crate::Map] and `String)`.
/// Sizes returned are `(` [`Array`][crate::Array], [`Map`][crate::Map] and [`String`] `)`.
///
/// # Panics
///

View File

@ -12,7 +12,8 @@ use crate::engine::{
use crate::eval::{EvalState, GlobalRuntimeState};
use crate::{
calc_fn_hash, calc_fn_params_hash, combine_hashes, Dynamic, Engine, FnArgsVec, FnPtr,
Identifier, ImmutableString, Module, Position, RhaiResult, RhaiResultOf, Scope, ERR,
Identifier, ImmutableString, Module, OptimizationLevel, Position, RhaiResult, RhaiResultOf,
Scope, ERR,
};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@ -1450,7 +1451,9 @@ impl Engine {
&Scope::new(),
&[script],
#[cfg(not(feature = "no_optimize"))]
crate::OptimizationLevel::None,
OptimizationLevel::None,
#[cfg(feature = "no_optimize")]
OptimizationLevel::default(),
)?;
// If new functions are defined within the eval string, it is an error

View File

@ -233,6 +233,10 @@ pub mod serde;
#[cfg(not(feature = "no_optimize"))]
pub use optimizer::OptimizationLevel;
/// Placeholder for the optimization level.
#[cfg(feature = "no_optimize")]
pub type OptimizationLevel = ();
// Expose internal data structures.
#[cfg(feature = "internals")]

View File

@ -16,7 +16,7 @@ use crate::types::dynamic::AccessMode;
use crate::types::StringsInterner;
use crate::{
calc_fn_hash, Dynamic, Engine, ExclusiveRange, Identifier, ImmutableString, InclusiveRange,
LexError, ParseError, Position, Scope, Shared, StaticVec, AST, INT, PERR,
LexError, OptimizationLevel, ParseError, Position, Scope, Shared, StaticVec, AST, INT, PERR,
};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@ -3387,9 +3387,10 @@ impl Engine {
input: &mut TokenStream,
state: &mut ParseState,
scope: &Scope,
#[cfg(not(feature = "no_optimize"))] optimization_level: crate::OptimizationLevel,
optimization_level: OptimizationLevel,
) -> ParseResult<AST> {
let _scope = scope;
let _optimization_level = optimization_level;
let mut functions = BTreeMap::new();
let settings = ParseSettings {
@ -3517,9 +3518,11 @@ impl Engine {
input: &mut TokenStream,
state: &mut ParseState,
scope: &Scope,
#[cfg(not(feature = "no_optimize"))] optimization_level: crate::OptimizationLevel,
optimization_level: OptimizationLevel,
) -> ParseResult<AST> {
let _scope = scope;
let _optimization_level = optimization_level;
let (statements, _lib) = self.parse_global_level(input, state)?;
#[cfg(not(feature = "no_optimize"))]