From 69bb5534ef77881d7b3d5e1a7349ecf11387fd48 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 24 Aug 2022 20:59:11 +0800 Subject: [PATCH] Share encapsulated environment. --- src/ast/script_fn.rs | 2 +- src/func/script.rs | 13 +++++++------ src/module/mod.rs | 12 +++++++----- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/ast/script_fn.rs b/src/ast/script_fn.rs index 1ce664e0..d40d6a5a 100644 --- a/src/ast/script_fn.rs +++ b/src/ast/script_fn.rs @@ -36,7 +36,7 @@ pub struct ScriptFnDef { /// Encapsulated AST environment, if any. #[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_function"))] - pub environ: Option, + pub environ: Option>, /// Function name. pub name: ImmutableString, /// Function access mode. diff --git a/src/func/script.rs b/src/func/script.rs index eeb41693..691ac0f6 100644 --- a/src/func/script.rs +++ b/src/func/script.rs @@ -111,12 +111,13 @@ impl Engine { let mut lib_merged = crate::StaticVec::with_capacity(lib.len() + 1); #[cfg(not(feature = "no_module"))] - let (lib, constants) = if let Some(crate::ast::EncapsulatedEnviron { - lib: ref fn_lib, - ref imports, - ref constants, - }) = fn_def.environ - { + let (lib, constants) = if let Some(ref environ) = fn_def.environ { + let crate::ast::EncapsulatedEnviron { + lib: fn_lib, + imports, + constants, + } = environ.as_ref(); + imports .iter() .cloned() diff --git a/src/module/mod.rs b/src/module/mod.rs index d82d529c..06fb0729 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -2086,6 +2086,12 @@ impl Module { // Non-private functions defined become module functions #[cfg(not(feature = "no_function"))] { + let environ = Shared::new(crate::ast::EncapsulatedEnviron { + lib: ast.shared_lib().clone(), + imports: imports.clone().into_boxed_slice(), + constants: constants.clone(), + }); + ast.shared_lib() .iter_fn() .filter(|&f| match f.metadata.access { @@ -2102,11 +2108,7 @@ impl Module { .clone(); // Encapsulate AST environment - func.environ = Some(crate::ast::EncapsulatedEnviron { - lib: ast.shared_lib().clone(), - imports: imports.clone().into_boxed_slice(), - constants: constants.clone(), - }); + func.environ = Some(environ.clone()); module.set_script_fn(func); });