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.
#[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_function"))]
pub environ: Option<EncapsulatedEnviron>,
pub environ: Option<crate::Shared<EncapsulatedEnviron>>,
/// Function name.
pub name: ImmutableString,
/// Function access mode.

View File

@ -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()

View File

@ -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);
});