Simplify code by merging Engine::global_namespace with Engine::global_modules.

This commit is contained in:
Stephen Chung 2021-08-30 10:09:28 +08:00
parent 6f3de4c803
commit 6faa6358f0
4 changed files with 23 additions and 27 deletions

View File

@ -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<Shared<Module>>,
/// 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
.global_modules
.iter()
.find_map(|m| m.get_iter(iter_type))
})
.or_else(|| mods.get_iter(iter_type))
.or_else(|| {
self.global_sub_modules

View File

@ -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<str> + Into<Identifier>,
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,
<T as IntoIterator>::Item: Variant + Clone,
{
self.global_namespace.set_iterable::<T>();
self.global_namespace().set_iterable::<T>();
self
}
/// Register a getter function for a member of a registered type with the [`Engine`].

View File

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

View File

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