diff --git a/src/engine.rs b/src/engine.rs index 79864466..abe08f76 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -898,8 +898,6 @@ impl<'x, 'px, 'pt> EvalContext<'_, 'x, 'px, '_, '_, '_, '_, 'pt> { /// # } /// ``` pub struct Engine { - /// A module containing all functions directly loaded into the Engine. - pub(crate) global_namespace: Module, /// A collection of all modules loaded into the global namespace of the Engine. pub(crate) global_modules: StaticVec>, /// A collection of all sub-modules directly loaded into the Engine. @@ -1034,7 +1032,6 @@ impl Engine { #[must_use] pub fn new_raw() -> Self { let mut engine = Self { - global_namespace: Default::default(), global_modules: Default::default(), global_sub_modules: Default::default(), @@ -1061,7 +1058,10 @@ impl Engine { limits: Default::default(), }; - engine.global_namespace.internal = true; + // Add the global namespace module + let mut global_namespace = Module::new(); + global_namespace.internal = true; + engine.global_modules.push(global_namespace.into()); engine } @@ -2618,13 +2618,9 @@ impl Engine { // 3) Imported modules - functions marked with global namespace // 4) Global sub-modules - functions marked with global namespace let func = self - .global_namespace - .get_iter(iter_type) - .or_else(|| { - self.global_modules - .iter() - .find_map(|m| m.get_iter(iter_type)) - }) + .global_modules + .iter() + .find_map(|m| m.get_iter(iter_type)) .or_else(|| mods.get_iter(iter_type)) .or_else(|| { self.global_sub_modules diff --git a/src/engine_api.rs b/src/engine_api.rs index cca1d706..8e6175b0 100644 --- a/src/engine_api.rs +++ b/src/engine_api.rs @@ -22,6 +22,16 @@ use crate::Array; use crate::Map; impl Engine { + /// Get the global namespace module (which is the last module in `global_modules`). + #[inline(always)] + fn global_namespace(&mut self) -> &mut Module { + Shared::get_mut( + self.global_modules + .last_mut() + .expect("global_modules contains at least one module"), + ) + .expect("global namespace module is never shared") + } /// Register a custom function with the [`Engine`]. /// /// # Example @@ -74,7 +84,7 @@ impl Engine { #[cfg(not(feature = "metadata"))] let param_type_names: Option<[&str; 0]> = None; - self.global_namespace.set_fn( + self.global_namespace().set_fn( name, FnNamespace::Global, FnAccess::Public, @@ -132,7 +142,7 @@ impl Engine { #[cfg(not(feature = "metadata"))] let param_type_names: Option<[&str; 0]> = None; - self.global_namespace.set_fn( + self.global_namespace().set_fn( name, FnNamespace::Global, FnAccess::Public, @@ -171,7 +181,7 @@ impl Engine { N: AsRef + Into, T: Variant + Clone, { - self.global_namespace.set_raw_fn( + self.global_namespace().set_raw_fn( name, FnNamespace::Global, FnAccess::Public, @@ -275,7 +285,7 @@ impl Engine { T: Variant + Clone + IntoIterator, ::Item: Variant + Clone, { - self.global_namespace.set_iterable::(); + self.global_namespace().set_iterable::(); self } /// Register a getter function for a member of a registered type with the [`Engine`]. diff --git a/src/fn_call.rs b/src/fn_call.rs index ec049173..a3e97465 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -198,12 +198,6 @@ impl Engine { source: m.id_raw().cloned(), }) }) - .or_else(|| { - self.global_namespace - .get_fn(hash) - .cloned() - .map(|func| FnResolutionCacheEntry { func, source: None }) - }) .or_else(|| { self.global_modules.iter().find_map(|m| { m.get_fn(hash).cloned().map(|func| FnResolutionCacheEntry { @@ -619,8 +613,6 @@ impl Engine { // First check script-defined functions let result = lib.iter().any(|&m| m.contains_fn(hash_script)) - // Then check registered functions - || self.global_namespace.contains_fn(hash_script) // Then check packages || self.global_modules.iter().any(|m| m.contains_fn(hash_script)) // Then check imported modules diff --git a/src/optimize.rs b/src/optimize.rs index 5225eb61..32c90183 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -148,10 +148,8 @@ impl<'a> OptimizerState<'a> { let hash_params = calc_fn_params_hash(arg_types.iter().cloned()); let hash = combine_hashes(hash_script, hash_params); - // First check registered functions - self.engine.global_namespace.contains_fn(hash) - // Then check packages - || self.engine.global_modules.iter().any(|m| m.contains_fn(hash)) + // First check packages + self.engine.global_modules.iter().any(|m| m.contains_fn(hash)) // Then check sub-modules || self.engine.global_sub_modules.values().any(|m| m.contains_qualified_fn(hash)) }