From 1d904f47583ae2d4c5a2c82212405bbb229d7356 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 23 Apr 2022 13:37:08 +0800 Subject: [PATCH] Add Engine::eval_statements_raw. --- CHANGELOG.md | 1 + src/api/eval.rs | 41 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a297a01..833c3b71 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ Enhancements * 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. * 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 diff --git a/src/api/eval.rs b/src/api/eval.rs index 094fd117..4b23958a 100644 --- a/src/api/eval.rs +++ b/src/api/eval.rs @@ -6,9 +6,9 @@ use crate::types::dynamic::Variant; 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::*; +use std::{any::type_name, mem}; impl Engine { /// Evaluate a string. @@ -211,9 +211,10 @@ impl Engine { global.source = ast.source_raw().clone(); #[cfg(not(feature = "no_module"))] - { - global.embedded_module_resolver = ast.resolver().cloned(); - } + let orig_embedded_module_resolver = mem::replace( + &mut global.embedded_module_resolver, + ast.resolver().cloned(), + ); let statements = ast.statements(); @@ -230,6 +231,36 @@ impl Engine { } else { &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) } }