Module::eval_ast_as_new will merge global and module namespaces if private_namespace is true.

This commit is contained in:
Stephen Chung 2020-09-28 18:53:03 +08:00
parent 5e43f2e5a4
commit 2123b0a279

View File

@ -3,7 +3,7 @@
use crate::any::{Dynamic, Variant}; use crate::any::{Dynamic, Variant};
use crate::calc_fn_hash; use crate::calc_fn_hash;
use crate::engine::Engine; use crate::engine::Engine;
use crate::fn_native::{CallableFunction, FnCallArgs, FnPtr, IteratorFn, SendSync}; use crate::fn_native::{CallableFunction, FnCallArgs, IteratorFn, SendSync};
use crate::fn_register::by_value as cast_arg; use crate::fn_register::by_value as cast_arg;
use crate::parser::{FnAccess, FnAccess::Public}; use crate::parser::{FnAccess, FnAccess::Public};
use crate::result::EvalAltResult; use crate::result::EvalAltResult;
@ -1215,15 +1215,12 @@ impl Module {
/// Create a new `Module` by evaluating an `AST`. /// Create a new `Module` by evaluating an `AST`.
/// ///
/// ### `private_namespace` parameter /// ### `merge_namespaces` parameter
/// ///
/// * If `true`, the entire `AST` is encapsulated into each function as a private namespace, /// * If `true`, the entire `AST` is encapsulated into each function, allowing functions
/// allowing functions to cross-call each other. Functions are searched in the module's /// to cross-call each other. Functions in the global namespace, plus all functions
/// private namespace, and the global namespace cannot be accessed. /// defined in the module, are _merged_ into a _unified_ namespace before each call.
/// /// Therefore, all functions will be found, at the expense of some performance.
/// Under this scenario, if any argument is a function pointer, then the function referred
/// to by the function pointer is also _merged_ into the module's private namespace before
/// the function call, so passing simple closures to functions work as expected.
/// ///
/// * If `false`, each function is registered independently and cannot cross-call /// * If `false`, each function is registered independently and cannot cross-call
/// each other. Functions are searched in the global namespace. /// each other. Functions are searched in the global namespace.
@ -1246,7 +1243,7 @@ impl Module {
pub fn eval_ast_as_new( pub fn eval_ast_as_new(
mut scope: Scope, mut scope: Scope,
ast: &AST, ast: &AST,
private_namespace: bool, merge_namespaces: bool,
engine: &Engine, engine: &Engine,
) -> FuncReturn<Self> { ) -> FuncReturn<Self> {
let mut mods = Imports::new(); let mut mods = Imports::new();
@ -1272,7 +1269,7 @@ impl Module {
}); });
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
if private_namespace { if merge_namespaces {
ast.iter_functions(|access, name, num_args| match access { ast.iter_functions(|access, name, num_args| match access {
FnAccess::Private => (), FnAccess::Private => (),
FnAccess::Public => { FnAccess::Public => {
@ -1283,31 +1280,13 @@ impl Module {
name, name,
num_args, num_args,
move |engine: &Engine, lib: &Module, args: &mut [&mut Dynamic]| { move |engine: &Engine, lib: &Module, args: &mut [&mut Dynamic]| {
// Get anonymous functions let mut lib_merged = lib.clone();
let anon_fns: StaticVec<_> = args lib_merged.merge(&ast_lib);
.iter()
.filter(|&arg| arg.is::<FnPtr>())
.map(|arg| arg.read_lock::<FnPtr>().unwrap().get_fn_name().clone())
.collect();
let mut lib_merged;
let call_lib = if anon_fns.is_empty() {
// No anonymous functions - just pass the AST through
&ast_lib
} else {
// If there are anonymous functions, merge them in
lib_merged = ast_lib.clone();
lib_merged.merge_filtered(&lib, &mut |_, name, _| {
anon_fns.iter().find(|s| s.as_str() == name).is_some()
});
&lib_merged
};
engine engine
.call_fn_dynamic_raw( .call_fn_dynamic_raw(
&mut Scope::new(), &mut Scope::new(),
call_lib, &lib_merged,
&fn_name, &fn_name,
&mut None, &mut None,
args, args,