Module resolver returns shared module.
This commit is contained in:
@@ -121,6 +121,7 @@ where:
|
||||
| `context` | `&mut EvalContext` | mutable reference to the current evaluation _context_ |
|
||||
| - `context.scope` | `&mut Scope` | mutable reference to the current [`Scope`]; variables can be added to/removed from it |
|
||||
| - `context.engine()` | `&Engine` | reference to the current [`Engine`] |
|
||||
| - `context.imports()` | `&Imports` | reference to the current stack of [modules] imported via `import` statements |
|
||||
| - `context.iter_namespaces()` | `impl Iterator<Item = &Module>` | iterator of the namespaces (as [modules]) containing all script-defined functions |
|
||||
| - `context.this_ptr()` | `Option<&Dynamic>` | reference to the current bound [`this`] pointer, if any |
|
||||
| - `context.call_level()` | `usize` | the current nesting level of function calls |
|
||||
|
@@ -74,6 +74,7 @@ where:
|
||||
| `context` | `&EvalContext` | reference to the current evaluation _context_ |
|
||||
| - `context.scope` | `&Scope` | reference to the current [`Scope`] containing all variables up to the current evaluation position |
|
||||
| - `context.engine()` | `&Engine` | reference to the current [`Engine`] |
|
||||
| - `context.imports()` | `&Imports` | reference to the current stack of [modules] imported via `import` statements |
|
||||
| - `context.iter_namespaces()` | `impl Iterator<Item = &Module>` | iterator of the namespaces (as [modules]) containing all script-defined functions |
|
||||
| - `context.this_ptr()` | `Option<&Dynamic>` | reference to the current bound [`this`] pointer, if any |
|
||||
| - `context.call_level()` | `usize` | the current nesting level of function calls |
|
||||
|
@@ -30,6 +30,8 @@ There is one _global_ namespace for every [`Engine`], which includes:
|
||||
|
||||
* All the Rust functions defined in [packages] that are loaded into the [`Engine`].
|
||||
|
||||
* All the [modules] imported via [`import`] statements.
|
||||
|
||||
In addition, during evaluation of an [`AST`], all script-defined functions bundled together within
|
||||
the [`AST`] are added to the global namespace and override any existing registered functions of
|
||||
the same names and number of parameters.
|
||||
|
@@ -223,7 +223,14 @@ engine.register_raw_fn("super_call",
|
||||
------------------
|
||||
|
||||
`FnPtr::call_dynamic` takes a parameter of type `NativeCallContext` which holds the _native call context_
|
||||
of the particular call to a registered Rust function.
|
||||
of the particular call to a registered Rust function. It is a type that exposes the following:
|
||||
|
||||
| Field | Type | Description |
|
||||
| ------------------- | :-----------------------------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `engine()` | `&Engine` | the current [`Engine`], with all configurations and settings.<br/>This is sometimes useful for calling a script-defined function within the same evaluation context using [`Engine::call_fn`][`call_fn`], or calling a [function pointer]. |
|
||||
| `imports()` | `Option<&Imports>` | reference to the current stack of [modules] imported via `import` statements (if any) |
|
||||
| `iter_namespaces()` | `impl Iterator<Item = &Module>` | iterator of the namespaces (as [modules]) containing all script-defined functions |
|
||||
|
||||
|
||||
This type is normally provided by the [`Engine`] (e.g. when using [`Engine::register_fn_raw`](../rust/register-raw.md)).
|
||||
However, it may also be manually constructed from a tuple:
|
||||
|
@@ -85,11 +85,11 @@ specially by the plugins system.
|
||||
|
||||
`NativeCallContext` is a type that encapsulates the current _native call context_ and exposes the following:
|
||||
|
||||
* `NativeCallContext::engine(): &Engine` - the current [`Engine`], with all configurations and settings.
|
||||
This is sometimes useful for calling a script-defined function within the same evaluation context
|
||||
using [`Engine::call_fn`][`call_fn`].
|
||||
|
||||
* `NativeCallContext::namespace(): &Module` - the global namespace of script-defined functions, as a [`Module`].
|
||||
| Field | Type | Description |
|
||||
| ------------------- | :-----------------------------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `engine()` | `&Engine` | the current [`Engine`], with all configurations and settings.<br/>This is sometimes useful for calling a script-defined function within the same evaluation context using [`Engine::call_fn`][`call_fn`], or calling a [function pointer]. |
|
||||
| `imports()` | `Option<&Imports>` | reference to the current stack of [modules] imported via `import` statements (if any) |
|
||||
| `iter_namespaces()` | `impl Iterator<Item = &Module>` | iterator of the namespaces (as [modules]) containing all script-defined functions |
|
||||
|
||||
This first parameter, if exists, will be stripped before all other processing. It is _virtual_.
|
||||
Most importantly, it does _not_ count as a parameter to the function and there is no need to provide
|
||||
|
@@ -342,11 +342,11 @@ specially by the plugins system.
|
||||
|
||||
`NativeCallContext` is a type that encapsulates the current _native call context_ and exposes the following:
|
||||
|
||||
* `NativeCallContext::engine(): &Engine` - the current [`Engine`], with all configurations and settings.
|
||||
This is sometimes useful for calling a script-defined function within the same evaluation context
|
||||
using [`Engine::call_fn`][`call_fn`].
|
||||
|
||||
* `NativeCallContext::namespace(): &Module` - the global namespace of script-defined functions, as a [`Module`].
|
||||
| Field | Type | Description |
|
||||
| ------------------- | :-----------------------------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `engine()` | `&Engine` | the current [`Engine`], with all configurations and settings.<br/>This is sometimes useful for calling a script-defined function within the same evaluation context using [`Engine::call_fn`][`call_fn`], or calling a [function pointer]. |
|
||||
| `imports()` | `Option<&Imports>` | reference to the current stack of [modules] imported via `import` statements (if any) |
|
||||
| `iter_namespaces()` | `impl Iterator<Item = &Module>` | iterator of the namespaces (as [modules]) containing all script-defined functions |
|
||||
|
||||
This first parameter, if exists, will be stripped before all other processing. It is _virtual_.
|
||||
Most importantly, it does _not_ count as a parameter to the function and there is no need to provide
|
||||
|
@@ -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())
|
||||
|
@@ -29,6 +29,8 @@ Loads a script file (based off the current directory) with `.rhai` extension.
|
||||
All functions in the _global_ namespace, plus all those defined in the same module,
|
||||
are _merged_ into a _unified_ namespace.
|
||||
|
||||
Modules are also _cached_ so a script file is only evaluated _once_, even when repeatedly imported.
|
||||
|
||||
```rust
|
||||
------------------
|
||||
| my_module.rhai |
|
||||
@@ -122,10 +124,6 @@ m::greet(); // prints "hello! from module!"
|
||||
|
||||
The base directory can be changed via the `FileModuleResolver::new_with_path` constructor function.
|
||||
|
||||
### Returning a module instead
|
||||
|
||||
`FileModuleResolver::create_module` loads a script file and returns a module with the standard behavior.
|
||||
|
||||
|
||||
`StaticModuleResolver`
|
||||
---------------------
|
||||
|
@@ -70,6 +70,7 @@ where:
|
||||
| `T` | `impl Clone` | return type of the function |
|
||||
| `context` | `NativeCallContext` | the current _native call context_ |
|
||||
| - `context.engine()` | `&Engine` | the current [`Engine`], with all configurations and settings.<br/>This is sometimes useful for calling a script-defined function within the same evaluation context using [`Engine::call_fn`][`call_fn`], or calling a [function pointer]. |
|
||||
| - `context.imports()` | `Option<&Imports>` | reference to the current stack of [modules] imported via `import` statements (if any) |
|
||||
| - `context.iter_namespaces()` | `impl Iterator<Item = &Module>` | iterator of the namespaces (as [modules]) containing all script-defined functions |
|
||||
| `args` | `&mut [&mut Dynamic]` | a slice containing `&mut` references to [`Dynamic`] values.<br/>The slice is guaranteed to contain enough arguments _of the correct types_. |
|
||||
|
||||
|
Reference in New Issue
Block a user