Build index when creating a module.

This commit is contained in:
Stephen Chung 2020-11-21 22:18:32 +08:00
parent 254fd16e62
commit e5c9ca23b5
7 changed files with 54 additions and 25 deletions

View File

@ -19,6 +19,7 @@ Breaking changes
---------------- ----------------
* `Module::set_fn`, `Module::set_raw_fn` and `Module::set_fn_XXX_mut` all take an additional parameter of `FnNamespace`. * `Module::set_fn`, `Module::set_raw_fn` and `Module::set_fn_XXX_mut` all take an additional parameter of `FnNamespace`.
* `Module::get_sub_module_mut` is removed.
* `begin`, `end`, `unless` are now reserved keywords. * `begin`, `end`, `unless` are now reserved keywords.
* `EvalPackage` is removed in favor of `Engine::disable_symbol`. * `EvalPackage` is removed in favor of `Engine::disable_symbol`.

View File

@ -162,6 +162,7 @@ pub(crate) fn generate_body(
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]

View File

@ -257,6 +257,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -291,6 +292,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -352,6 +354,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -413,6 +416,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -485,6 +489,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -577,6 +582,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -642,6 +648,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -714,6 +721,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -745,6 +753,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -778,6 +787,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -813,6 +823,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -848,6 +859,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -889,6 +901,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -945,6 +958,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -979,6 +993,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -1041,6 +1056,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -1103,6 +1119,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -1168,6 +1185,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -1209,6 +1227,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -1253,6 +1272,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -1294,6 +1314,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -1337,6 +1358,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -1399,6 +1421,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -1463,6 +1486,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -1528,6 +1552,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -1597,6 +1622,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -1663,6 +1689,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -1733,6 +1760,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -1802,6 +1830,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -1874,6 +1903,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -1888,6 +1918,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -1928,6 +1959,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -1944,6 +1976,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -1958,6 +1991,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -2024,6 +2058,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -2040,6 +2075,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -2054,6 +2090,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -2077,6 +2114,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -2091,6 +2129,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -2116,6 +2155,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -2132,6 +2172,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -2146,6 +2187,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]
@ -2167,6 +2209,7 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index();
m m
} }
#[allow(unused_mut)] #[allow(unused_mut)]

View File

@ -50,7 +50,7 @@ impl ModuleResolver for MyModuleResolver {
// Return EvalAltResult::ErrorInModule upon loading error // Return EvalAltResult::ErrorInModule upon loading error
EvalAltResult::ErrorInModule(path.into(), Box::new(err), pos).into() EvalAltResult::ErrorInModule(path.into(), Box::new(err), pos).into()
)?; )?;
my_module.build_index(); // index it my_module.build_index(); // index it
Rc::new(my_module) // make it shared Rc::new(my_module) // make it shared
} else { } else {
// Return EvalAltResult::ErrorModuleNotFound if the path is invalid // Return EvalAltResult::ErrorModuleNotFound if the path is invalid

View File

@ -2,9 +2,7 @@
use crate::ast::{FnAccess, IdentX}; use crate::ast::{FnAccess, IdentX};
use crate::dynamic::Variant; use crate::dynamic::Variant;
use crate::fn_native::{ use crate::fn_native::{shared_take_or_clone, CallableFunction, FnCallArgs, IteratorFn, SendSync};
shared_make_mut, shared_take_or_clone, CallableFunction, FnCallArgs, IteratorFn, SendSync,
};
use crate::fn_register::by_value as cast_arg; use crate::fn_register::by_value as cast_arg;
use crate::stdlib::{ use crate::stdlib::{
any::TypeId, any::TypeId,
@ -399,23 +397,6 @@ impl Module {
self.modules.get(name).map(|m| m.as_ref()) self.modules.get(name).map(|m| m.as_ref())
} }
/// Get a mutable reference to a sub-module.
///
/// # Example
///
/// ```
/// use rhai::Module;
///
/// let mut module = Module::new();
/// let sub_module = Module::new();
/// module.set_sub_module("question", sub_module);
/// assert!(module.get_sub_module_mut("question").is_some());
/// ```
#[inline(always)]
pub fn get_sub_module_mut(&mut self, name: &str) -> Option<&mut Module> {
self.modules.get_mut(name).map(shared_make_mut)
}
/// Set a sub-module into the module. /// Set a sub-module into the module.
/// ///
/// If there is an existing sub-module of the same name, it is replaced. /// If there is an existing sub-module of the same name, it is replaced.
@ -1537,6 +1518,8 @@ impl Module {
}); });
} }
module.build_index();
Ok(module) Ok(module)
} }
@ -1545,7 +1528,7 @@ impl Module {
/// ///
/// If the module is already indexed, this method has no effect. /// If the module is already indexed, this method has no effect.
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
pub fn build_index(&mut self) { pub fn build_index(&mut self) -> &mut Self {
// Collect a particular module. // Collect a particular module.
fn index_module<'a>( fn index_module<'a>(
module: &'a Module, module: &'a Module,
@ -1645,6 +1628,8 @@ impl Module {
self.all_type_iterators = type_iterators; self.all_type_iterators = type_iterators;
self.indexed = true; self.indexed = true;
} }
self
} }
/// Does a type iterator exist in the entire module tree? /// Does a type iterator exist in the entire module tree?

View File

@ -159,12 +159,10 @@ impl ModuleResolver for FileModuleResolver {
_ => Box::new(EvalAltResult::ErrorInModule(path.to_string(), err, pos)), _ => Box::new(EvalAltResult::ErrorInModule(path.to_string(), err, pos)),
})?; })?;
let mut m = Module::eval_ast_as_new(scope, &ast, engine).map_err(|err| { let m = Module::eval_ast_as_new(scope, &ast, engine).map_err(|err| {
Box::new(EvalAltResult::ErrorInModule(path.to_string(), err, pos)) Box::new(EvalAltResult::ErrorInModule(path.to_string(), err, pos))
})?; })?;
m.build_index();
module = Some(m.into()); module = Some(m.into());
module_ref = module.clone(); module_ref = module.clone();
}; };

View File

@ -131,6 +131,7 @@ macro_rules! def_package {
pub fn new() -> Self { pub fn new() -> Self {
let mut module = $root::Module::new_with_capacity(1024); let mut module = $root::Module::new_with_capacity(1024);
<Self as $root::packages::Package>::init(&mut module); <Self as $root::packages::Package>::init(&mut module);
module.build_index();
Self(module.into()) Self(module.into())
} }
} }