Share encapsulated environment.

This commit is contained in:
Stephen Chung 2022-08-24 20:59:11 +08:00
parent b40ca9e40d
commit 69bb5534ef
3 changed files with 15 additions and 12 deletions

View File

@ -36,7 +36,7 @@ pub struct ScriptFnDef {
/// Encapsulated AST environment, if any. /// Encapsulated AST environment, if any.
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
pub environ: Option<EncapsulatedEnviron>, pub environ: Option<crate::Shared<EncapsulatedEnviron>>,
/// Function name. /// Function name.
pub name: ImmutableString, pub name: ImmutableString,
/// Function access mode. /// Function access mode.

View File

@ -111,12 +111,13 @@ impl Engine {
let mut lib_merged = crate::StaticVec::with_capacity(lib.len() + 1); let mut lib_merged = crate::StaticVec::with_capacity(lib.len() + 1);
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
let (lib, constants) = if let Some(crate::ast::EncapsulatedEnviron { let (lib, constants) = if let Some(ref environ) = fn_def.environ {
lib: ref fn_lib, let crate::ast::EncapsulatedEnviron {
ref imports, lib: fn_lib,
ref constants, imports,
}) = fn_def.environ constants,
{ } = environ.as_ref();
imports imports
.iter() .iter()
.cloned() .cloned()

View File

@ -2086,6 +2086,12 @@ impl Module {
// Non-private functions defined become module functions // Non-private functions defined become module functions
#[cfg(not(feature = "no_function"))] #[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() ast.shared_lib()
.iter_fn() .iter_fn()
.filter(|&f| match f.metadata.access { .filter(|&f| match f.metadata.access {
@ -2102,11 +2108,7 @@ impl Module {
.clone(); .clone();
// Encapsulate AST environment // Encapsulate AST environment
func.environ = Some(crate::ast::EncapsulatedEnviron { func.environ = Some(environ.clone());
lib: ast.shared_lib().clone(),
imports: imports.clone().into_boxed_slice(),
constants: constants.clone(),
});
module.set_script_fn(func); module.set_script_fn(func);
}); });