Rename GlobalRuntimeStatemodules -> imports.
This commit is contained in:
parent
3d4abeed0e
commit
f92894e337
@ -56,7 +56,7 @@ impl<'x, 'px, 'pt> EvalContext<'_, 'x, 'px, '_, '_, '_, '_, 'pt> {
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
#[inline(always)]
|
||||
pub fn iter_imports(&self) -> impl Iterator<Item = (&str, &Module)> {
|
||||
self.global.iter_modules()
|
||||
self.global.iter_imports()
|
||||
}
|
||||
/// _(internals)_ The current [`GlobalRuntimeState`].
|
||||
/// Exported under the `internals` feature only.
|
||||
|
@ -32,12 +32,12 @@ impl Engine {
|
||||
};
|
||||
|
||||
if let Some(index) = index {
|
||||
let offset = global.num_imported_modules() - index.get();
|
||||
Some(global.get_shared_module(offset).unwrap())
|
||||
let offset = global.num_imports() - index.get();
|
||||
Some(global.get_shared_import(offset).unwrap())
|
||||
} else {
|
||||
global
|
||||
.find_module(root)
|
||||
.map(|n| global.get_shared_module(n).unwrap())
|
||||
.find_import(root)
|
||||
.map(|n| global.get_shared_import(n).unwrap())
|
||||
.or_else(|| self.global_sub_modules.get(root).cloned())
|
||||
}
|
||||
}
|
||||
|
@ -77,26 +77,26 @@ impl GlobalRuntimeState {
|
||||
/// Get the length of the stack of globally-imported [modules][Module].
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
pub fn num_imported_modules(&self) -> usize {
|
||||
pub fn num_imports(&self) -> usize {
|
||||
self.keys.len()
|
||||
}
|
||||
/// Get the globally-imported [module][Module] at a particular index.
|
||||
#[inline(always)]
|
||||
#[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()
|
||||
}
|
||||
/// Get a mutable reference to the globally-imported [module][Module] at a particular index.
|
||||
#[allow(dead_code)]
|
||||
#[inline(always)]
|
||||
#[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)
|
||||
}
|
||||
/// Get the index of a globally-imported [module][Module] by name.
|
||||
#[inline]
|
||||
#[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();
|
||||
|
||||
self.keys.iter().rev().enumerate().find_map(|(i, key)| {
|
||||
@ -109,20 +109,20 @@ impl GlobalRuntimeState {
|
||||
}
|
||||
/// Push an imported [module][Module] onto the stack.
|
||||
#[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.modules.push(module.into());
|
||||
}
|
||||
/// Truncate the stack of globally-imported [modules][Module] to a particular length.
|
||||
#[inline(always)]
|
||||
pub fn truncate_modules(&mut self, size: usize) {
|
||||
pub fn truncate_imports(&mut self, size: usize) {
|
||||
self.keys.truncate(size);
|
||||
self.modules.truncate(size);
|
||||
}
|
||||
/// Get an iterator to the stack of globally-imported [modules][Module] in reverse order.
|
||||
#[allow(dead_code)]
|
||||
#[inline]
|
||||
pub fn iter_modules(&self) -> impl Iterator<Item = (&str, &Module)> {
|
||||
pub fn iter_imports(&self) -> impl Iterator<Item = (&str, &Module)> {
|
||||
self.keys
|
||||
.iter()
|
||||
.rev()
|
||||
@ -132,26 +132,26 @@ impl GlobalRuntimeState {
|
||||
/// Get an iterator to the stack of globally-imported [modules][Module] in reverse order.
|
||||
#[allow(dead_code)]
|
||||
#[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())
|
||||
}
|
||||
/// Get an iterator to the stack of globally-imported [modules][Module] in forward order.
|
||||
#[allow(dead_code)]
|
||||
#[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())
|
||||
}
|
||||
/// Does the specified function hash key exist in the stack of globally-imported [modules][Module]?
|
||||
#[allow(dead_code)]
|
||||
#[inline]
|
||||
#[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))
|
||||
}
|
||||
/// Get the specified function via its hash key from the stack of globally-imported [modules][Module].
|
||||
#[inline]
|
||||
#[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
|
||||
.iter()
|
||||
.rev()
|
||||
|
@ -28,7 +28,7 @@ impl Engine {
|
||||
|
||||
let orig_always_search_scope = state.always_search_scope;
|
||||
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();
|
||||
|
||||
if restore_orig_state {
|
||||
@ -38,7 +38,7 @@ impl Engine {
|
||||
let mut result = Dynamic::UNIT;
|
||||
|
||||
for stmt in statements {
|
||||
let _mods_len = global.num_imported_modules();
|
||||
let _mods_len = global.num_imports();
|
||||
|
||||
result = self.eval_stmt(
|
||||
scope,
|
||||
@ -56,7 +56,7 @@ impl Engine {
|
||||
// Get the extra modules - see if any functions are marked global.
|
||||
// Without global functions, the extra modules never affect function resolution.
|
||||
if global
|
||||
.scan_modules_raw()
|
||||
.scan_imports_raw()
|
||||
.skip(_mods_len)
|
||||
.any(|(_, m)| m.contains_indexed_global_functions())
|
||||
{
|
||||
@ -82,7 +82,7 @@ impl Engine {
|
||||
if restore_orig_state {
|
||||
scope.rewind(orig_scope_len);
|
||||
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
|
||||
// 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
|
||||
let mut module = crate::func::native::shared_take_or_clone(module);
|
||||
module.build_index();
|
||||
global.push_module(name, module);
|
||||
global.push_import(name, module);
|
||||
} else {
|
||||
global.push_module(name, module);
|
||||
global.push_import(name, module);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -179,9 +179,9 @@ impl Engine {
|
||||
/// Search order:
|
||||
/// 1) AST - script functions in the AST
|
||||
/// 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
|
||||
/// 5) Global sub-modules - functions marked with global namespace
|
||||
/// 5) Static registered modules
|
||||
#[must_use]
|
||||
fn resolve_fn<'s>(
|
||||
&self,
|
||||
@ -220,7 +220,7 @@ impl Engine {
|
||||
loop {
|
||||
let func = lib
|
||||
.iter()
|
||||
.find_map(|m| {
|
||||
.find_map(|&m| {
|
||||
m.get_fn(hash).cloned().map(|func| FnResolutionCacheEntry {
|
||||
func,
|
||||
source: m.id_raw().clone(),
|
||||
@ -235,12 +235,12 @@ impl Engine {
|
||||
})
|
||||
})
|
||||
.or_else(|| {
|
||||
global
|
||||
.get_fn(hash)
|
||||
.map(|(func, source)| FnResolutionCacheEntry {
|
||||
global.get_qualified_fn(hash).map(|(func, source)| {
|
||||
FnResolutionCacheEntry {
|
||||
func: func.clone(),
|
||||
source: source
|
||||
.map_or_else(|| Identifier::new_const(), Into::into),
|
||||
}
|
||||
})
|
||||
})
|
||||
.or_else(|| {
|
||||
|
@ -199,7 +199,7 @@ impl<'a> NativeCallContext<'a> {
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
#[inline]
|
||||
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.
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
@ -208,7 +208,7 @@ impl<'a> NativeCallContext<'a> {
|
||||
pub(crate) fn iter_imports_raw(
|
||||
&self,
|
||||
) -> 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.
|
||||
/// Exported under the `internals` feature only.
|
||||
|
@ -70,7 +70,7 @@ impl Engine {
|
||||
}
|
||||
|
||||
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
|
||||
scope.extend(fn_def.params.iter().cloned().zip(args.into_iter().map(|v| {
|
||||
@ -100,7 +100,7 @@ impl Engine {
|
||||
modules
|
||||
.iter()
|
||||
.cloned()
|
||||
.for_each(|(n, m)| global.push_module(n, m));
|
||||
.for_each(|(n, m)| global.push_import(n, m));
|
||||
}
|
||||
|
||||
// Evaluate the function
|
||||
@ -144,7 +144,7 @@ impl Engine {
|
||||
// Remove arguments only, leaving new variables in the scope
|
||||
scope.remove_range(orig_scope_len, args.len())
|
||||
}
|
||||
global.truncate_modules(orig_mods_len);
|
||||
global.truncate_imports(orig_mods_len);
|
||||
|
||||
// Restore state
|
||||
state.rewind_fn_resolution_caches(orig_fn_resolution_caches_len);
|
||||
@ -172,7 +172,7 @@ impl Engine {
|
||||
// Then check the global namespace and packages
|
||||
|| self.global_modules.iter().any(|m| m.contains_fn(hash_script))
|
||||
// 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
|
||||
|| self.global_sub_modules.values().any(|m| m.contains_qualified_fn(hash_script));
|
||||
|
||||
|
@ -1447,14 +1447,14 @@ impl Module {
|
||||
/// Merge another [`Module`] into this [`Module`].
|
||||
#[inline(always)]
|
||||
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.
|
||||
pub(crate) fn merge_filtered(
|
||||
&mut self,
|
||||
other: &Self,
|
||||
_filter: &impl Fn(FnNamespace, FnAccess, bool, &str, usize) -> bool,
|
||||
_filter: impl Fn(FnNamespace, FnAccess, bool, &str, usize) -> bool + Copy,
|
||||
) -> &mut Self {
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
other.modules.iter().for_each(|(k, v)| {
|
||||
@ -1665,7 +1665,7 @@ impl Module {
|
||||
) -> RhaiResultOf<Self> {
|
||||
let mut scope = scope;
|
||||
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
|
||||
engine.eval_ast_with_scope_raw(&mut scope, &mut global, &ast, 0)?;
|
||||
|
@ -96,7 +96,7 @@ impl<'a> OptimizerState<'a> {
|
||||
pub fn restore_var(&mut self, len: usize) {
|
||||
self.variables.truncate(len)
|
||||
}
|
||||
/// Add a new constant to the list.
|
||||
/// Add a new variable to the list.
|
||||
#[inline(always)]
|
||||
pub fn push_var(
|
||||
&mut self,
|
||||
|
Loading…
Reference in New Issue
Block a user