Add Engine::eval_statements_raw.

This commit is contained in:
Stephen Chung 2022-04-23 13:37:08 +08:00
parent 0ef5c0ec54
commit 1d904f4758
2 changed files with 37 additions and 5 deletions

View File

@ -25,6 +25,7 @@ Enhancements
* A global function `format_map_as_json` is provided which is the same as `to_json` for object maps. * A global function `format_map_as_json` is provided which is the same as `to_json` for object maps.
* `FileModuleResolver` now accepts a custom `Scope` to provide constants for optimization. * `FileModuleResolver` now accepts a custom `Scope` to provide constants for optimization.
* A new low-level method `Engine::call_fn_raw_raw` is added to add speed to repeated function calls. * A new low-level method `Engine::call_fn_raw_raw` is added to add speed to repeated function calls.
* A new low-level method `Engine::eval_statements_raw` is added to evaluate a sequence of statements.
Version 1.6.1 Version 1.6.1

View File

@ -6,9 +6,9 @@ use crate::types::dynamic::Variant;
use crate::{ use crate::{
Dynamic, Engine, Module, OptimizationLevel, Position, RhaiResult, RhaiResultOf, Scope, AST, ERR, Dynamic, Engine, Module, OptimizationLevel, Position, RhaiResult, RhaiResultOf, Scope, AST, ERR,
}; };
use std::any::type_name;
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
use std::{any::type_name, mem};
impl Engine { impl Engine {
/// Evaluate a string. /// Evaluate a string.
@ -211,9 +211,10 @@ impl Engine {
global.source = ast.source_raw().clone(); global.source = ast.source_raw().clone();
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
{ let orig_embedded_module_resolver = mem::replace(
global.embedded_module_resolver = ast.resolver().cloned(); &mut global.embedded_module_resolver,
} ast.resolver().cloned(),
);
let statements = ast.statements(); let statements = ast.statements();
@ -230,6 +231,36 @@ impl Engine {
} else { } else {
&lib[..] &lib[..]
}; };
self.eval_global_statements(scope, global, &mut caches, statements, lib, level)
let result =
self.eval_global_statements(scope, global, &mut caches, statements, lib, level);
#[cfg(not(feature = "no_module"))]
{
global.embedded_module_resolver = orig_embedded_module_resolver;
}
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,
scope: &mut Scope,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
statements: &[crate::ast::Stmt],
lib: &[&Module],
level: usize,
) -> RhaiResult {
self.eval_global_statements(scope, global, caches, statements, lib, level)
} }
} }