Delay creation of global module until actually needed.

This commit is contained in:
Stephen Chung 2021-04-17 18:10:57 +08:00
parent 1be7e60be2
commit 9a8da93145
2 changed files with 9 additions and 5 deletions

View File

@ -2517,7 +2517,15 @@ impl Engine {
let (var_name, _alias): (Cow<'_, str>, _) = if state.is_global() { let (var_name, _alias): (Cow<'_, str>, _) = if state.is_global() {
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
if entry_type == AccessMode::ReadOnly { if entry_type == AccessMode::ReadOnly {
let global = mods.get_mut(mods.find(KEYWORD_GLOBAL).unwrap()).unwrap(); let index = if let Some(index) = mods.find(KEYWORD_GLOBAL) {
index
} else {
// Create automatic global module
mods.push(crate::engine::KEYWORD_GLOBAL, Module::new());
mods.len() - 1
};
let global = mods.get_mut(index).unwrap();
let global = Shared::get_mut(global).unwrap(); let global = Shared::get_mut(global).unwrap();
global.set_var(name.clone(), value.clone()); global.set_var(name.clone(), value.clone());
global.build_index(); global.build_index();

View File

@ -826,10 +826,6 @@ impl Engine {
lib: &[&Module], lib: &[&Module],
level: usize, level: usize,
) -> RhaiResult { ) -> RhaiResult {
// Create the global module
#[cfg(not(feature = "no_function"))]
mods.push(crate::engine::KEYWORD_GLOBAL, Module::new());
self.eval_stmt_block(scope, mods, state, lib, &mut None, statements, false, level) self.eval_stmt_block(scope, mods, state, lib, &mut None, statements, false, level)
.or_else(|err| match *err { .or_else(|err| match *err {
EvalAltResult::Return(out, _) => Ok(out), EvalAltResult::Return(out, _) => Ok(out),