Re-index imported modules if they are not yet indexed.

This commit is contained in:
Stephen Chung
2020-11-08 14:29:54 +08:00
parent 9a669ffe29
commit 1e07e4356e
6 changed files with 55 additions and 20 deletions

View File

@@ -60,23 +60,17 @@ pub struct FuncInfo {
pub struct Module {
/// Sub-modules.
modules: HashMap<String, Module>,
/// Module variables.
variables: HashMap<String, Dynamic>,
/// Flattened collection of all module variables, including those in sub-modules.
all_variables: HashMap<u64, Dynamic, StraightHasherBuilder>,
/// External Rust functions.
functions: HashMap<u64, FuncInfo, StraightHasherBuilder>,
/// Iterator functions, keyed by the type producing the iterator.
type_iterators: HashMap<TypeId, IteratorFn>,
/// Flattened collection of all external Rust functions, native or scripted,
/// including those in sub-modules.
all_functions: HashMap<u64, CallableFunction, StraightHasherBuilder>,
/// Is the module indexed?
indexed: bool,
}
@@ -181,6 +175,23 @@ impl Module {
&& self.type_iterators.is_empty()
}
/// Is the module indexed?
///
/// # Example
///
/// ```
/// use rhai::Module;
///
/// let mut module = Module::new();
/// assert!(!module.is_indexed());
///
/// module.build_index();
/// assert!(module.is_indexed());
/// ```
pub fn is_indexed(&self) -> bool {
self.indexed
}
/// Clone the module, optionally skipping the index.
#[inline(always)]
fn do_clone(&self, clone_index: bool) -> Self {
@@ -1120,7 +1131,7 @@ impl Module {
/// Name and Position in `EvalAltResult` are None and must be set afterwards.
///
/// The `u64` hash is calculated by the function `crate::calc_native_fn_hash` and must match
/// the hash calculated by `index_all_sub_modules`.
/// the hash calculated by `build_index`.
#[inline(always)]
pub(crate) fn get_qualified_fn(&self, hash_qualified_fn: u64) -> Option<&CallableFunction> {
self.all_functions.get(&hash_qualified_fn)
@@ -1396,10 +1407,12 @@ impl Module {
Ok(module)
}
/// Scan through all the sub-modules in the module build an index of all
/// variables and external Rust functions via hashing.
/// Scan through all the sub-modules in the module and build a hash index of all
/// variables and functions as one flattened namespace.
///
/// If the module is already indexed, this method has no effect.
#[cfg(not(feature = "no_module"))]
pub(crate) fn index_all_sub_modules(&mut self) {
pub fn build_index(&mut self) {
// Collect a particular module.
fn index_module<'a>(
module: &'a Module,

View File

@@ -155,7 +155,7 @@ impl ModuleResolver for FileModuleResolver {
Box::new(EvalAltResult::ErrorInModule(path.to_string(), err, pos))
})?;
m.index_all_sub_modules();
m.build_index();
let m: Shared<Module> = m.into();
module = Some(m.clone());

View File

@@ -50,7 +50,7 @@ impl StaticModuleResolver {
/// Add a module keyed by its path.
#[inline(always)]
pub fn insert(&mut self, path: impl Into<String>, mut module: Module) {
module.index_all_sub_modules();
module.build_index();
self.0.insert(path.into(), module.into());
}
/// Remove a module given its path.