Rename GlobalRuntimeStatemodules -> imports.

This commit is contained in:
Stephen Chung 2022-01-20 08:17:34 +08:00
parent 3d4abeed0e
commit f92894e337
9 changed files with 39 additions and 39 deletions

View File

@ -56,7 +56,7 @@ impl<'x, 'px, 'pt> EvalContext<'_, 'x, 'px, '_, '_, '_, '_, 'pt> {
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
#[inline(always)] #[inline(always)]
pub fn iter_imports(&self) -> impl Iterator<Item = (&str, &Module)> { pub fn iter_imports(&self) -> impl Iterator<Item = (&str, &Module)> {
self.global.iter_modules() self.global.iter_imports()
} }
/// _(internals)_ The current [`GlobalRuntimeState`]. /// _(internals)_ The current [`GlobalRuntimeState`].
/// Exported under the `internals` feature only. /// Exported under the `internals` feature only.

View File

@ -32,12 +32,12 @@ impl Engine {
}; };
if let Some(index) = index { if let Some(index) = index {
let offset = global.num_imported_modules() - index.get(); let offset = global.num_imports() - index.get();
Some(global.get_shared_module(offset).unwrap()) Some(global.get_shared_import(offset).unwrap())
} else { } else {
global global
.find_module(root) .find_import(root)
.map(|n| global.get_shared_module(n).unwrap()) .map(|n| global.get_shared_import(n).unwrap())
.or_else(|| self.global_sub_modules.get(root).cloned()) .or_else(|| self.global_sub_modules.get(root).cloned())
} }
} }

View File

@ -77,26 +77,26 @@ impl GlobalRuntimeState {
/// Get the length of the stack of globally-imported [modules][Module]. /// Get the length of the stack of globally-imported [modules][Module].
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
pub fn num_imported_modules(&self) -> usize { pub fn num_imports(&self) -> usize {
self.keys.len() self.keys.len()
} }
/// Get the globally-imported [module][Module] at a particular index. /// Get the globally-imported [module][Module] at a particular index.
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
pub fn get_shared_module(&self, index: usize) -> Option<Shared<Module>> { pub fn get_shared_import(&self, index: usize) -> Option<Shared<Module>> {
self.modules.get(index).cloned() self.modules.get(index).cloned()
} }
/// Get a mutable reference to the globally-imported [module][Module] at a particular index. /// Get a mutable reference to the globally-imported [module][Module] at a particular index.
#[allow(dead_code)] #[allow(dead_code)]
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
pub(crate) fn get_shared_module_mut(&mut self, index: usize) -> Option<&mut Shared<Module>> { pub(crate) fn get_shared_import_mut(&mut self, index: usize) -> Option<&mut Shared<Module>> {
self.modules.get_mut(index) self.modules.get_mut(index)
} }
/// Get the index of a globally-imported [module][Module] by name. /// Get the index of a globally-imported [module][Module] by name.
#[inline] #[inline]
#[must_use] #[must_use]
pub fn find_module(&self, name: &str) -> Option<usize> { pub fn find_import(&self, name: &str) -> Option<usize> {
let len = self.keys.len(); let len = self.keys.len();
self.keys.iter().rev().enumerate().find_map(|(i, key)| { self.keys.iter().rev().enumerate().find_map(|(i, key)| {
@ -109,20 +109,20 @@ impl GlobalRuntimeState {
} }
/// Push an imported [module][Module] onto the stack. /// Push an imported [module][Module] onto the stack.
#[inline(always)] #[inline(always)]
pub fn push_module(&mut self, name: impl Into<Identifier>, module: impl Into<Shared<Module>>) { pub fn push_import(&mut self, name: impl Into<Identifier>, module: impl Into<Shared<Module>>) {
self.keys.push(name.into()); self.keys.push(name.into());
self.modules.push(module.into()); self.modules.push(module.into());
} }
/// Truncate the stack of globally-imported [modules][Module] to a particular length. /// Truncate the stack of globally-imported [modules][Module] to a particular length.
#[inline(always)] #[inline(always)]
pub fn truncate_modules(&mut self, size: usize) { pub fn truncate_imports(&mut self, size: usize) {
self.keys.truncate(size); self.keys.truncate(size);
self.modules.truncate(size); self.modules.truncate(size);
} }
/// Get an iterator to the stack of globally-imported [modules][Module] in reverse order. /// Get an iterator to the stack of globally-imported [modules][Module] in reverse order.
#[allow(dead_code)] #[allow(dead_code)]
#[inline] #[inline]
pub fn iter_modules(&self) -> impl Iterator<Item = (&str, &Module)> { pub fn iter_imports(&self) -> impl Iterator<Item = (&str, &Module)> {
self.keys self.keys
.iter() .iter()
.rev() .rev()
@ -132,26 +132,26 @@ impl GlobalRuntimeState {
/// Get an iterator to the stack of globally-imported [modules][Module] in reverse order. /// Get an iterator to the stack of globally-imported [modules][Module] in reverse order.
#[allow(dead_code)] #[allow(dead_code)]
#[inline] #[inline]
pub(crate) fn iter_modules_raw(&self) -> impl Iterator<Item = (&Identifier, &Shared<Module>)> { pub(crate) fn iter_imports_raw(&self) -> impl Iterator<Item = (&Identifier, &Shared<Module>)> {
self.keys.iter().rev().zip(self.modules.iter().rev()) self.keys.iter().rev().zip(self.modules.iter().rev())
} }
/// Get an iterator to the stack of globally-imported [modules][Module] in forward order. /// Get an iterator to the stack of globally-imported [modules][Module] in forward order.
#[allow(dead_code)] #[allow(dead_code)]
#[inline] #[inline]
pub(crate) fn scan_modules_raw(&self) -> impl Iterator<Item = (&Identifier, &Shared<Module>)> { pub(crate) fn scan_imports_raw(&self) -> impl Iterator<Item = (&Identifier, &Shared<Module>)> {
self.keys.iter().zip(self.modules.iter()) self.keys.iter().zip(self.modules.iter())
} }
/// Does the specified function hash key exist in the stack of globally-imported [modules][Module]? /// Does the specified function hash key exist in the stack of globally-imported [modules][Module]?
#[allow(dead_code)] #[allow(dead_code)]
#[inline] #[inline]
#[must_use] #[must_use]
pub fn contains_fn(&self, hash: u64) -> bool { pub fn contains_qualified_fn(&self, hash: u64) -> bool {
self.modules.iter().any(|m| m.contains_qualified_fn(hash)) self.modules.iter().any(|m| m.contains_qualified_fn(hash))
} }
/// Get the specified function via its hash key from the stack of globally-imported [modules][Module]. /// Get the specified function via its hash key from the stack of globally-imported [modules][Module].
#[inline] #[inline]
#[must_use] #[must_use]
pub fn get_fn(&self, hash: u64) -> Option<(&CallableFunction, Option<&str>)> { pub fn get_qualified_fn(&self, hash: u64) -> Option<(&CallableFunction, Option<&str>)> {
self.modules self.modules
.iter() .iter()
.rev() .rev()

View File

@ -28,7 +28,7 @@ impl Engine {
let orig_always_search_scope = state.always_search_scope; let orig_always_search_scope = state.always_search_scope;
let orig_scope_len = scope.len(); let orig_scope_len = scope.len();
let orig_mods_len = global.num_imported_modules(); let orig_mods_len = global.num_imports();
let orig_fn_resolution_caches_len = state.fn_resolution_caches_len(); let orig_fn_resolution_caches_len = state.fn_resolution_caches_len();
if restore_orig_state { if restore_orig_state {
@ -38,7 +38,7 @@ impl Engine {
let mut result = Dynamic::UNIT; let mut result = Dynamic::UNIT;
for stmt in statements { for stmt in statements {
let _mods_len = global.num_imported_modules(); let _mods_len = global.num_imports();
result = self.eval_stmt( result = self.eval_stmt(
scope, scope,
@ -56,7 +56,7 @@ impl Engine {
// Get the extra modules - see if any functions are marked global. // Get the extra modules - see if any functions are marked global.
// Without global functions, the extra modules never affect function resolution. // Without global functions, the extra modules never affect function resolution.
if global if global
.scan_modules_raw() .scan_imports_raw()
.skip(_mods_len) .skip(_mods_len)
.any(|(_, m)| m.contains_indexed_global_functions()) .any(|(_, m)| m.contains_indexed_global_functions())
{ {
@ -82,7 +82,7 @@ impl Engine {
if restore_orig_state { if restore_orig_state {
scope.rewind(orig_scope_len); scope.rewind(orig_scope_len);
state.scope_level -= 1; state.scope_level -= 1;
global.truncate_modules(orig_mods_len); global.truncate_imports(orig_mods_len);
// The impact of new local variables goes away at the end of a block // The impact of new local variables goes away at the end of a block
// because any new variables introduced will go out of scope // because any new variables introduced will go out of scope
@ -778,9 +778,9 @@ impl Engine {
// Index the module (making a clone copy if necessary) if it is not indexed // Index the module (making a clone copy if necessary) if it is not indexed
let mut module = crate::func::native::shared_take_or_clone(module); let mut module = crate::func::native::shared_take_or_clone(module);
module.build_index(); module.build_index();
global.push_module(name, module); global.push_import(name, module);
} else { } else {
global.push_module(name, module); global.push_import(name, module);
} }
} }

View File

@ -179,9 +179,9 @@ impl Engine {
/// Search order: /// Search order:
/// 1) AST - script functions in the AST /// 1) AST - script functions in the AST
/// 2) Global namespace - functions registered via Engine::register_XXX /// 2) Global namespace - functions registered via Engine::register_XXX
/// 3) Global modules - packages /// 3) Global registered modules - packages
/// 4) Imported modules - functions marked with global namespace /// 4) Imported modules - functions marked with global namespace
/// 5) Global sub-modules - functions marked with global namespace /// 5) Static registered modules
#[must_use] #[must_use]
fn resolve_fn<'s>( fn resolve_fn<'s>(
&self, &self,
@ -220,7 +220,7 @@ impl Engine {
loop { loop {
let func = lib let func = lib
.iter() .iter()
.find_map(|m| { .find_map(|&m| {
m.get_fn(hash).cloned().map(|func| FnResolutionCacheEntry { m.get_fn(hash).cloned().map(|func| FnResolutionCacheEntry {
func, func,
source: m.id_raw().clone(), source: m.id_raw().clone(),
@ -235,13 +235,13 @@ impl Engine {
}) })
}) })
.or_else(|| { .or_else(|| {
global global.get_qualified_fn(hash).map(|(func, source)| {
.get_fn(hash) FnResolutionCacheEntry {
.map(|(func, source)| FnResolutionCacheEntry {
func: func.clone(), func: func.clone(),
source: source source: source
.map_or_else(|| Identifier::new_const(), Into::into), .map_or_else(|| Identifier::new_const(), Into::into),
}) }
})
}) })
.or_else(|| { .or_else(|| {
self.global_sub_modules.values().find_map(|m| { self.global_sub_modules.values().find_map(|m| {

View File

@ -199,7 +199,7 @@ impl<'a> NativeCallContext<'a> {
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
#[inline] #[inline]
pub fn iter_imports(&self) -> impl Iterator<Item = (&str, &Module)> { pub fn iter_imports(&self) -> impl Iterator<Item = (&str, &Module)> {
self.global.iter().flat_map(|&m| m.iter_modules()) self.global.iter().flat_map(|&m| m.iter_imports())
} }
/// Get an iterator over the current set of modules imported via `import` statements in reverse order. /// Get an iterator over the current set of modules imported via `import` statements in reverse order.
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
@ -208,7 +208,7 @@ impl<'a> NativeCallContext<'a> {
pub(crate) fn iter_imports_raw( pub(crate) fn iter_imports_raw(
&self, &self,
) -> impl Iterator<Item = (&crate::Identifier, &Shared<Module>)> { ) -> impl Iterator<Item = (&crate::Identifier, &Shared<Module>)> {
self.global.iter().flat_map(|&m| m.iter_modules_raw()) self.global.iter().flat_map(|&m| m.iter_imports_raw())
} }
/// _(internals)_ The current [`GlobalRuntimeState`], if any. /// _(internals)_ The current [`GlobalRuntimeState`], if any.
/// Exported under the `internals` feature only. /// Exported under the `internals` feature only.

View File

@ -70,7 +70,7 @@ impl Engine {
} }
let orig_scope_len = scope.len(); let orig_scope_len = scope.len();
let orig_mods_len = global.num_imported_modules(); let orig_mods_len = global.num_imports();
// Put arguments into scope as variables // Put arguments into scope as variables
scope.extend(fn_def.params.iter().cloned().zip(args.into_iter().map(|v| { scope.extend(fn_def.params.iter().cloned().zip(args.into_iter().map(|v| {
@ -100,7 +100,7 @@ impl Engine {
modules modules
.iter() .iter()
.cloned() .cloned()
.for_each(|(n, m)| global.push_module(n, m)); .for_each(|(n, m)| global.push_import(n, m));
} }
// Evaluate the function // Evaluate the function
@ -144,7 +144,7 @@ impl Engine {
// Remove arguments only, leaving new variables in the scope // Remove arguments only, leaving new variables in the scope
scope.remove_range(orig_scope_len, args.len()) scope.remove_range(orig_scope_len, args.len())
} }
global.truncate_modules(orig_mods_len); global.truncate_imports(orig_mods_len);
// Restore state // Restore state
state.rewind_fn_resolution_caches(orig_fn_resolution_caches_len); state.rewind_fn_resolution_caches(orig_fn_resolution_caches_len);
@ -172,7 +172,7 @@ impl Engine {
// Then check the global namespace and packages // Then check the global namespace and packages
|| self.global_modules.iter().any(|m| m.contains_fn(hash_script)) || self.global_modules.iter().any(|m| m.contains_fn(hash_script))
// Then check imported modules // Then check imported modules
|| global.map_or(false, |m| m.contains_fn(hash_script)) || global.map_or(false, |m| m.contains_qualified_fn(hash_script))
// Then check sub-modules // Then check sub-modules
|| self.global_sub_modules.values().any(|m| m.contains_qualified_fn(hash_script)); || self.global_sub_modules.values().any(|m| m.contains_qualified_fn(hash_script));

View File

@ -1447,14 +1447,14 @@ impl Module {
/// Merge another [`Module`] into this [`Module`]. /// Merge another [`Module`] into this [`Module`].
#[inline(always)] #[inline(always)]
pub fn merge(&mut self, other: &Self) -> &mut Self { pub fn merge(&mut self, other: &Self) -> &mut Self {
self.merge_filtered(other, &|_, _, _, _, _| true) self.merge_filtered(other, |_, _, _, _, _| true)
} }
/// Merge another [`Module`] into this [`Module`] based on a filter predicate. /// Merge another [`Module`] into this [`Module`] based on a filter predicate.
pub(crate) fn merge_filtered( pub(crate) fn merge_filtered(
&mut self, &mut self,
other: &Self, other: &Self,
_filter: &impl Fn(FnNamespace, FnAccess, bool, &str, usize) -> bool, _filter: impl Fn(FnNamespace, FnAccess, bool, &str, usize) -> bool + Copy,
) -> &mut Self { ) -> &mut Self {
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
other.modules.iter().for_each(|(k, v)| { other.modules.iter().for_each(|(k, v)| {
@ -1665,7 +1665,7 @@ impl Module {
) -> RhaiResultOf<Self> { ) -> RhaiResultOf<Self> {
let mut scope = scope; let mut scope = scope;
let mut global = crate::eval::GlobalRuntimeState::new(); let mut global = crate::eval::GlobalRuntimeState::new();
let orig_mods_len = global.num_imported_modules(); let orig_mods_len = global.num_imports();
// Run the script // Run the script
engine.eval_ast_with_scope_raw(&mut scope, &mut global, &ast, 0)?; engine.eval_ast_with_scope_raw(&mut scope, &mut global, &ast, 0)?;

View File

@ -96,7 +96,7 @@ impl<'a> OptimizerState<'a> {
pub fn restore_var(&mut self, len: usize) { pub fn restore_var(&mut self, len: usize) {
self.variables.truncate(len) self.variables.truncate(len)
} }
/// Add a new constant to the list. /// Add a new variable to the list.
#[inline(always)] #[inline(always)]
pub fn push_var( pub fn push_var(
&mut self, &mut self,