Module resolver returns shared module.

This commit is contained in:
Stephen Chung
2020-11-07 23:33:21 +08:00
parent d5d70367fa
commit b3d318ef7f
20 changed files with 158 additions and 136 deletions

View File

@@ -13,7 +13,7 @@ which contains only one function: `resolve`.
When Rhai prepares to load a module, `ModuleResolver::resolve` is called with the name
of the _module path_ (i.e. the path specified in the [`import`] statement).
* Upon success, it should return a [`Module`].
* Upon success, it should return an [`Rc<Module>`][module] (or `Arc<Module>` under [`sync`]).
* If the path does not resolve to a valid module, return `EvalAltResult::ErrorModuleNotFound`.
@@ -37,14 +37,15 @@ impl ModuleResolver for MyModuleResolver {
engine: &Engine, // reference to the current 'Engine'
path: &str, // the module path
pos: Position, // position of the 'import' statement
) -> Result<Module, Box<EvalAltResult>> {
) -> Result<Rc<Module>, Box<EvalAltResult>> {
// Check module path.
if is_valid_module_path(path) {
// Load the custom module.
load_secret_module(path).map_err(|err|
// Return EvalAltResult::ErrorInModule upon loading error
EvalAltResult::ErrorInModule(err.to_string(), pos).into()
)
load_secret_module(path) // load the custom module
.map(Rc::new) // share it
.map_err(|err|
// Return EvalAltResult::ErrorInModule upon loading error
EvalAltResult::ErrorInModule(err.to_string(), pos).into()
)
} else {
// Return EvalAltResult::ErrorModuleNotFound if the path is invalid
Err(EvalAltResult::ErrorModuleNotFound(path.into(), pos).into())