More code refactor.

This commit is contained in:
Stephen Chung
2022-11-25 20:42:16 +08:00
parent fbe30b8d0e
commit d645d8271c
30 changed files with 422 additions and 434 deletions

View File

@@ -101,7 +101,7 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
Self {
engine,
name: None,
_marker: PhantomData::default(),
_marker: PhantomData,
}
}
}

View File

@@ -81,7 +81,7 @@ impl Engine {
///
/// The [`AST`] is evaluated before calling the function.
/// This allows a script to load the necessary modules.
/// This is usually desired. If not, use [`call_fn_with_options`] instead.
/// This is usually desired. If not, use [`call_fn_with_options`][Engine::call_fn_with_options] instead.
///
/// # Example
///
@@ -276,7 +276,7 @@ impl Engine {
});
#[cfg(feature = "debugging")]
if self.debugger.is_some() {
if self.is_debugger_registered() {
global.debugger_mut().status = crate::eval::DebuggerStatus::Terminate;
let node = &crate::ast::Stmt::Noop(Position::NONE);
self.run_debugger(global, caches, scope, this_ptr, node)?;

View File

@@ -218,13 +218,10 @@ impl Engine {
scripts: impl AsRef<[S]>,
optimization_level: OptimizationLevel,
) -> ParseResult<AST> {
let (stream, tokenizer_control) = self.lex_raw(
scripts.as_ref(),
self.token_mapper.as_ref().map(<_>::as_ref),
);
let (stream, tc) = self.lex_raw(scripts.as_ref(), self.token_mapper.as_deref());
let interned_strings = &mut *locked_write(&self.interned_strings);
let mut state = ParseState::new(scope, interned_strings, tokenizer_control);
let mut _ast = self.parse(&mut stream.peekable(), &mut state, optimization_level)?;
let state = &mut ParseState::new(scope, interned_strings, tc);
let mut _ast = self.parse(stream.peekable(), state, optimization_level)?;
#[cfg(feature = "metadata")]
_ast.set_doc(state.tokenizer_control.borrow().global_comments.join("\n"));
Ok(_ast)
@@ -292,12 +289,9 @@ impl Engine {
script: impl AsRef<str>,
) -> ParseResult<AST> {
let scripts = [script];
let (stream, tokenizer_control) =
self.lex_raw(&scripts, self.token_mapper.as_ref().map(<_>::as_ref));
let mut peekable = stream.peekable();
let (stream, t) = self.lex_raw(&scripts, self.token_mapper.as_deref());
let interned_strings = &mut *locked_write(&self.interned_strings);
let mut state = ParseState::new(scope, interned_strings, tokenizer_control);
self.parse_global_expr(&mut peekable, &mut state, |_| {}, self.optimization_level)
let state = &mut ParseState::new(scope, interned_strings, t);
self.parse_global_expr(stream.peekable(), state, |_| {}, self.optimization_level)
}
}

View File

@@ -259,7 +259,7 @@ impl Engine {
// Make it a custom keyword/symbol if it is disabled or reserved
if (self
.disabled_symbols
.as_ref()
.as_deref()
.map_or(false, |m| m.contains(s))
|| token.map_or(false, |v| v.is_reserved()))
&& !self

View File

@@ -26,7 +26,7 @@ impl Engine {
///
/// This method is deprecated. Use [`run_file`][Engine::run_file] instead.
///
/// This method will be removed in the next majocd cr version.
/// This method will be removed in the next major version.
#[deprecated(since = "1.1.0", note = "use `run_file` instead")]
#[cfg(not(feature = "no_std"))]
#[cfg(not(target_family = "wasm"))]
@@ -137,12 +137,6 @@ impl Engine {
}
/// Call a script function defined in an [`AST`] with multiple [`Dynamic`] arguments.
///
/// The following options are available:
///
/// * whether to evaluate the [`AST`] to load necessary modules before calling the function
/// * whether to rewind the [`Scope`] after the function call
/// * a value for binding to the `this` pointer (if any)
///
/// Not available under `no_function`.
///
/// # Deprecated
@@ -253,12 +247,6 @@ impl Engine {
/// This method is deprecated. Use [`register_indexer_get`][Engine::register_indexer_get] instead.
///
/// This method will be removed in the next major version.
///
/// # Panics
///
/// Panics if the type is [`Array`][crate::Array], [`Map`][crate::Map], [`String`],
/// [`ImmutableString`][crate::ImmutableString], `&str` or [`INT`][crate::INT].
/// Indexers for arrays, object maps, strings and integers cannot be registered.
#[deprecated(since = "1.9.1", note = "use `register_indexer_get` instead")]
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
#[inline(always)]
@@ -284,12 +272,6 @@ impl Engine {
/// This method is deprecated. Use [`register_indexer_set`][Engine::register_indexer_set] instead.
///
/// This method will be removed in the next major version.
///
/// # Panics
///
/// Panics if the type is [`Array`][crate::Array], [`Map`][crate::Map], [`String`],
/// [`ImmutableString`][crate::ImmutableString], `&str` or [`INT`][crate::INT].
/// Indexers for arrays, object maps, strings and integers cannot be registered.
#[deprecated(since = "1.9.1", note = "use `register_indexer_set` instead")]
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
#[inline(always)]
@@ -316,34 +298,6 @@ impl Engine {
/// Use [`register_custom_syntax_with_state_raw`][Engine::register_custom_syntax_with_state_raw] instead.
///
/// This method will be removed in the next major version.
///
/// # WARNING - Low Level API
///
/// This function is very low level.
///
/// * `scope_may_be_changed` specifies variables have been added/removed by this custom syntax.
/// * `parse` is the parsing function.
/// * `func` is the implementation function.
///
/// All custom keywords used as symbols must be manually registered via [`Engine::register_custom_operator`].
/// Otherwise, they won't be recognized.
///
/// # Parsing Function Signature
///
/// The parsing function has the following signature:
///
/// `Fn(symbols: &[ImmutableString], look_ahead: &str) -> Result<Option<ImmutableString>, ParseError>`
///
/// where:
/// * `symbols`: a slice of symbols that have been parsed so far, possibly containing `$expr$` and/or `$block$`;
/// `$ident$` and other literal markers are replaced by the actual text
/// * `look_ahead`: a string slice containing the next symbol that is about to be read
///
/// ## Return value
///
/// * `Ok(None)`: parsing complete and there are no more symbols to match.
/// * `Ok(Some(symbol))`: the next symbol to match, which can also be `$expr$`, `$ident$` or `$block$`.
/// * `Err(ParseError)`: error that is reflected back to the [`Engine`], normally `ParseError(ParseErrorType::BadInput(LexError::ImproperSymbol(message)), Position::NONE)` to indicate a syntax error, but it can be any [`ParseError`][crate::ParseError].
#[deprecated(
since = "1.11.0",
note = "use `register_custom_syntax_with_state_raw` instead"
@@ -368,6 +322,24 @@ impl Engine {
move |context, expressions, _| func(context, expressions),
)
}
/// _(internals)_ Evaluate a list of statements with no `this` pointer.
/// Exported under the `internals` feature only.
///
/// # Deprecated
///
/// This method is deprecated. It will be removed in the next major version.
#[cfg(feature = "internals")]
#[inline(always)]
#[deprecated(since = "1.12.0")]
pub fn eval_statements_raw(
&self,
global: &mut crate::eval::GlobalRuntimeState,
caches: &mut crate::eval::Caches,
scope: &mut Scope,
statements: &[crate::ast::Stmt],
) -> RhaiResult {
self.eval_global_statements(global, caches, scope, statements)
}
}
impl Dynamic {

View File

@@ -121,15 +121,14 @@ impl Engine {
let ast = {
let interned_strings = &mut *locked_write(&self.interned_strings);
let (stream, tokenizer_control) =
self.lex_raw(&scripts, self.token_mapper.as_ref().map(<_>::as_ref));
let (stream, tc) = self.lex_raw(&scripts, self.token_mapper.as_deref());
let mut state = ParseState::new(scope, interned_strings, tokenizer_control);
let state = &mut ParseState::new(scope, interned_strings, tc);
// No need to optimize a lone expression
self.parse_global_expr(
&mut stream.peekable(),
&mut state,
stream.peekable(),
state,
|_| {},
#[cfg(not(feature = "no_optimize"))]
OptimizationLevel::None,
@@ -243,7 +242,7 @@ impl Engine {
let result = self.eval_global_statements(global, caches, scope, statements);
#[cfg(feature = "debugging")]
if self.debugger.is_some() {
if self.is_debugger_registered() {
global.debugger_mut().status = crate::eval::DebuggerStatus::Terminate;
let mut this = Dynamic::NULL;
let node = &crate::ast::Stmt::Noop(Position::NONE);
@@ -263,25 +262,6 @@ impl Engine {
result
}
/// _(internals)_ Evaluate a list of statements with no `this` pointer.
/// Exported under the `internals` feature only.
///
/// This is commonly used to evaluate a list of statements in an [`AST`] or a script function body.
///
/// # WARNING - Low Level API
///
/// This function is very low level.
#[cfg(feature = "internals")]
#[inline(always)]
pub fn eval_statements_raw(
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
scope: &mut Scope,
statements: &[crate::ast::Stmt],
) -> RhaiResult {
self.eval_global_statements(global, caches, scope, statements)
}
}
/// Evaluate a string as a script, returning the result value or an error.

View File

@@ -362,7 +362,7 @@ impl Engine {
+ SendSync
+ 'static,
) -> &mut Self {
self.debugger = Some(Box::new((Box::new(init), Box::new(callback))));
self.debugger_interface = Some(Box::new((Box::new(init), Box::new(callback))));
self
}
}

View File

@@ -120,11 +120,11 @@ impl Engine {
let ast = {
let scope = Scope::new();
let interned_strings = &mut *locked_write(&self.interned_strings);
let mut state = ParseState::new(&scope, interned_strings, tokenizer_control);
let state = &mut ParseState::new(&scope, interned_strings, tokenizer_control);
self.parse_global_expr(
&mut stream.peekable(),
&mut state,
stream.peekable(),
state,
|s| s.flags |= ParseSettingFlags::DISALLOW_UNQUOTED_MAP_PROPERTIES,
#[cfg(not(feature = "no_optimize"))]
OptimizationLevel::None,

View File

@@ -1,7 +1,5 @@
//! Module defining the public API of the Rhai engine.
pub mod type_names;
pub mod eval;
pub mod run;
@@ -24,15 +22,6 @@ pub mod limits;
pub mod events;
pub mod custom_syntax;
pub mod deprecated;
pub mod build_type;
#[cfg(feature = "metadata")]
pub mod definitions;
use crate::{Dynamic, Engine, Identifier};
#[cfg(feature = "no_std")]
@@ -291,3 +280,14 @@ impl Engine {
0
}
}
pub mod type_names;
pub mod custom_syntax;
pub mod build_type;
#[cfg(feature = "metadata")]
pub mod definitions;
pub mod deprecated;

View File

@@ -51,20 +51,14 @@ impl Engine {
) -> AST {
let mut ast = ast;
#[cfg(not(feature = "no_function"))]
let functions = ast
.shared_lib()
.iter_fn()
.filter(|f| f.func.is_script())
.map(|f| f.func.get_script_fn_def().unwrap().clone())
.collect();
let mut _new_ast = crate::optimizer::optimize_into_ast(
self,
let mut _new_ast = self.optimize_into_ast(
scope,
ast.take_statements(),
#[cfg(not(feature = "no_function"))]
functions,
ast.shared_lib()
.iter_fn()
.map(|f| f.func.get_script_fn_def().expect("`ScriptFnDef").clone())
.collect(),
optimization_level,
);

View File

@@ -58,14 +58,10 @@ impl Engine {
pub fn run_with_scope(&self, scope: &mut Scope, script: &str) -> RhaiResultOf<()> {
let scripts = [script];
let ast = {
let (stream, tc) = self.lex_raw(&scripts, self.token_mapper.as_deref());
let interned_strings = &mut *locked_write(&self.interned_strings);
let (stream, tokenizer_control) =
self.lex_raw(&scripts, self.token_mapper.as_ref().map(<_>::as_ref));
let mut state = ParseState::new(scope, interned_strings, tokenizer_control);
self.parse(&mut stream.peekable(), &mut state, self.optimization_level)?
let state = &mut ParseState::new(scope, interned_strings, tc);
self.parse(stream.peekable(), state, self.optimization_level)?
};
self.run_ast_with_scope(scope, &ast)
}
@@ -136,7 +132,7 @@ impl Engine {
}
#[cfg(feature = "debugging")]
if self.debugger.is_some() {
if self.is_debugger_registered() {
global.debugger_mut().status = crate::eval::DebuggerStatus::Terminate;
let mut this = crate::Dynamic::NULL;
let node = &crate::ast::Stmt::Noop(crate::Position::NONE);