Merge pull request #283 from schungx/master

Module resolvers return shared modules.
This commit is contained in:
Stephen Chung 2020-11-08 17:53:06 +08:00 committed by GitHub
commit f58e1d368a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 222 additions and 157 deletions

View File

@ -18,7 +18,7 @@ jobs:
os: [ubuntu-latest]
flags:
- ""
- "--features serde"
- "--features serde,internals"
- "--features unchecked"
- "--features sync"
- "--features no_optimize"

View File

@ -4,10 +4,24 @@ Rhai Release Notes
Version 0.19.5
==============
This version fixes a bug that prevents compilation with the `internals` feature.
It also speeds up importing modules.
Bug fixes
---------
* Fixes compilation error when using the `internals` feature. Bug introduced in `0.19.4`.
Breaking changes
----------------
* Modules imported at global level can now be accessed in functions.
* `ModuleResolver::resolve` now returns `Shared<Module>` for better resources sharing when loading modules.
Enhancements
------------
* Modules imported via `import` statements at global level can now be used in functions. There is no longer any need to re-`import` the modules at the beginning of each function block.
Version 0.19.4

View File

@ -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 |

View File

@ -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 |

View File

@ -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.

View File

@ -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:

View File

@ -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

View File

@ -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

View File

@ -13,7 +13,11 @@ 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`]).
The module should call `Module::build_index` on the target module before returning.
This method flattens the entire module tree and _indexes_ it for fast function name resolution.
If the module is already indexed, calling this method has no effect.
* If the path does not resolve to a valid module, return `EvalAltResult::ErrorModuleNotFound`.
@ -37,14 +41,17 @@ 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|
let mut my_module =
load_secret_module(path) // load the custom module
.map_err(|err|
// Return EvalAltResult::ErrorInModule upon loading error
EvalAltResult::ErrorInModule(err.to_string(), pos).into()
)
EvalAltResult::ErrorInModule(path.into(), Box::new(err), pos).into()
)?;
my_module.build_index(); // index it
Rc::new(my_module) // make it shared
} else {
// Return EvalAltResult::ErrorModuleNotFound if the path is invalid
Err(EvalAltResult::ErrorModuleNotFound(path.into(), pos).into())

View File

@ -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`
---------------------

View File

@ -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_. |

View File

@ -3,7 +3,7 @@
use crate::ast::{BinaryExpr, Expr, FnCallInfo, Ident, IdentX, ReturnType, Stmt};
use crate::dynamic::{map_std_type_name, Dynamic, Union, Variant};
use crate::fn_call::run_builtin_op_assignment;
use crate::fn_native::{Callback, FnPtr, OnVarCallback};
use crate::fn_native::{shared_try_take, Callback, FnPtr, OnVarCallback, Shared};
use crate::module::{Module, ModuleRef};
use crate::optimize::OptimizationLevel;
use crate::packages::{Package, PackagesCollection, StandardPackage};
@ -73,48 +73,49 @@ pub type Map = HashMap<ImmutableString, Dynamic>;
//
// We cannot use &str or Cow<str> here because `eval` may load a module and the module name will live beyond
// the AST of the eval script text. The best we can do is a shared reference.
//
// `Imports` is implemented as two `Vec`'s of exactly the same length. That's because a `Module` is large,
// so packing the import names together improves cache locality.
#[derive(Debug, Clone, Default)]
pub struct Imports(StaticVec<Module>, StaticVec<ImmutableString>);
pub struct Imports(StaticVec<(Shared<Module>, ImmutableString)>);
impl Imports {
/// Get the length of this stack of imported modules.
pub fn len(&self) -> usize {
self.0.len()
}
/// Is this stack of imported modules empty?
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
/// Get the imported module at a particular index.
pub fn get(&self, index: usize) -> Option<&Module> {
self.0.get(index)
pub fn get(&self, index: usize) -> Option<Shared<Module>> {
self.0.get(index).map(|(m, _)| m).cloned()
}
/// Get the index of an imported module by name.
pub fn find(&self, name: &str) -> Option<usize> {
self.1
self.0
.iter()
.enumerate()
.rev()
.find(|(_, key)| key.as_str() == name)
.find(|(_, (_, key))| key.as_str() == name)
.map(|(index, _)| index)
}
/// Push an imported module onto the stack.
pub fn push(&mut self, name: impl Into<ImmutableString>, module: Module) {
self.0.push(module);
self.1.push(name.into());
pub fn push(&mut self, name: impl Into<ImmutableString>, module: impl Into<Shared<Module>>) {
self.0.push((module.into(), name.into()));
}
/// Truncate the stack of imported modules to a particular length.
pub fn truncate(&mut self, size: usize) {
self.0.truncate(size);
self.1.truncate(size);
}
/// Get an iterator to this stack of imported modules.
#[allow(dead_code)]
pub fn iter(&self) -> impl Iterator<Item = (&str, &Module)> {
self.1.iter().map(|name| name.as_str()).zip(self.0.iter())
pub fn iter(&self) -> impl Iterator<Item = (&str, Shared<Module>)> {
self.0
.iter()
.map(|(module, name)| (name.as_str(), module.clone()))
}
/// Get a consuming iterator to this stack of imported modules.
pub fn into_iter(self) -> impl Iterator<Item = (ImmutableString, Module)> {
self.1.into_iter().zip(self.0.into_iter())
pub fn into_iter(self) -> impl Iterator<Item = (ImmutableString, Shared<Module>)> {
self.0.into_iter().map(|(module, name)| (name, module))
}
}
@ -630,11 +631,11 @@ fn default_print(_s: &str) {
/// Search for a module within an imports stack.
/// Position in `EvalAltResult` is `None` and must be set afterwards.
pub fn search_imports<'s>(
mods: &'s Imports,
pub fn search_imports(
mods: &Imports,
state: &mut State,
modules: &ModuleRef,
) -> Result<&'s Module, Box<EvalAltResult>> {
) -> Result<Shared<Module>, Box<EvalAltResult>> {
let Ident { name: root, pos } = &modules[0];
// Qualified - check if the root module is directly indexed
@ -1487,7 +1488,9 @@ impl Engine {
calc_native_fn_hash(empty(), OP_FUNC, args.iter().map(|a| a.type_id()));
if self
.call_native_fn(state, lib, OP_FUNC, hash, args, false, false, def_value)
.call_native_fn(
mods, state, lib, OP_FUNC, hash, args, false, false, def_value,
)
.map_err(|err| err.fill_position(rhs.position()))?
.0
.as_bool()
@ -1805,9 +1808,9 @@ impl Engine {
// Overriding exact implementation
if func.is_plugin_fn() {
func.get_plugin_fn().call((self, lib).into(), args)?;
func.get_plugin_fn().call((self, mods, lib).into(), args)?;
} else {
func.get_native_fn()((self, lib).into(), args)?;
func.get_native_fn()((self, mods, lib).into(), args)?;
}
}
// Built-in op-assignment function
@ -2126,11 +2129,18 @@ impl Engine {
.try_cast::<ImmutableString>()
{
if let Some(resolver) = &self.module_resolver {
let mut module = resolver.resolve(self, &path, expr.position())?;
let module = resolver.resolve(self, &path, expr.position())?;
if let Some(name_def) = alias {
module.index_all_sub_modules();
if !module.is_indexed() {
// Index the module (making a clone copy if necessary) if it is not indexed
let mut module =
shared_try_take(module).unwrap_or_else(|m| m.as_ref().clone());
module.build_index();
mods.push(name_def.name.clone(), module);
} else {
mods.push(name_def.name.clone(), module);
}
}
state.modules += 1;

View File

@ -1658,15 +1658,16 @@ impl Engine {
mut ast: AST,
optimization_level: OptimizationLevel,
) -> AST {
let lib = if cfg!(not(feature = "no_function")) {
ast.lib()
#[cfg(not(feature = "no_function"))]
let lib = ast
.lib()
.iter_fn()
.filter(|f| f.func.is_script())
.map(|f| f.func.get_fn_def().clone())
.collect()
} else {
Default::default()
};
.map(|f| (**f.func.get_fn_def()).clone())
.collect();
#[cfg(feature = "no_function")]
let lib = Default::default();
let stmt = mem::take(ast.statements_mut());
optimize_into_ast(self, scope, stmt, lib, optimization_level)

View File

@ -178,6 +178,7 @@ impl Engine {
/// **DO NOT** reuse the argument values unless for the first `&mut` argument - all others are silently replaced by `()`!
pub(crate) fn call_native_fn(
&self,
mods: &mut Imports,
state: &mut State,
lib: &[&Module],
fn_name: &str,
@ -205,9 +206,9 @@ impl Engine {
// Run external function
let result = if func.is_plugin_fn() {
func.get_plugin_fn().call((self, lib).into(), args)
func.get_plugin_fn().call((self, mods, lib).into(), args)
} else {
func.get_native_fn()((self, lib).into(), args)
func.get_native_fn()((self, mods, lib).into(), args)
};
// Restore the original reference
@ -588,6 +589,7 @@ impl Engine {
} else {
// If it is a native function, redirect it
self.call_native_fn(
mods,
state,
lib,
fn_name,
@ -602,7 +604,7 @@ impl Engine {
// Normal native function call
_ => self.call_native_fn(
state, lib, fn_name, hash_fn, args, is_ref, pub_only, def_val,
mods, state, lib, fn_name, hash_fn, args, is_ref, pub_only, def_val,
),
}
}
@ -1178,15 +1180,14 @@ impl Engine {
}
let args = args.as_mut();
let fn_def = f.get_shared_fn_def().clone();
let new_scope = &mut Default::default();
let fn_def = f.get_fn_def().clone();
self.call_script_fn(new_scope, mods, state, lib, &mut None, &fn_def, args, level)
}
Some(f) if f.is_plugin_fn() => {
f.get_plugin_fn().call((self, lib).into(), args.as_mut())
}
Some(f) if f.is_plugin_fn() => f
.get_plugin_fn()
.clone()
.call((self, mods, lib).into(), args.as_mut()),
Some(f) if f.is_native() => {
if !f.is_method() {
// Clone first argument
@ -1197,7 +1198,7 @@ impl Engine {
}
}
f.get_native_fn()((self, lib).into(), args.as_mut())
f.get_native_fn().clone()((self, mods, lib).into(), args.as_mut())
}
Some(_) => unreachable!(),
None if def_val.is_some() => Ok(def_val.unwrap().into()),

View File

@ -2,7 +2,7 @@
use crate::ast::{FnAccess, ScriptFnDef};
use crate::dynamic::Dynamic;
use crate::engine::{Engine, EvalContext};
use crate::engine::{Engine, EvalContext, Imports};
use crate::module::Module;
use crate::plugin::PluginFunction;
use crate::result::EvalAltResult;
@ -50,28 +50,50 @@ pub type Locked<T> = RwLock<T>;
/// Context of native Rust function call.
#[derive(Debug, Copy, Clone)]
pub struct NativeCallContext<'e, 'm, 'pm: 'm> {
pub struct NativeCallContext<'e, 'a, 'm, 'pm: 'm> {
engine: &'e Engine,
mods: Option<&'a Imports>,
lib: &'m [&'pm Module],
}
impl<'e, 'a, 'm, 'pm: 'm, M: AsRef<[&'pm Module]> + ?Sized>
From<(&'e Engine, &'a mut Imports, &'m M)> for NativeCallContext<'e, 'a, 'm, 'pm>
{
fn from(value: (&'e Engine, &'a mut Imports, &'m M)) -> Self {
Self {
engine: value.0,
mods: Some(value.1),
lib: value.2.as_ref(),
}
}
}
impl<'e, 'm, 'pm: 'm, M: AsRef<[&'pm Module]> + ?Sized> From<(&'e Engine, &'m M)>
for NativeCallContext<'e, 'm, 'pm>
for NativeCallContext<'e, '_, 'm, 'pm>
{
fn from(value: (&'e Engine, &'m M)) -> Self {
Self {
engine: value.0,
mods: None,
lib: value.1.as_ref(),
}
}
}
impl<'e, 'm, 'pm> NativeCallContext<'e, 'm, 'pm> {
impl<'e, 'a, 'm, 'pm> NativeCallContext<'e, 'a, 'm, 'pm> {
/// The current `Engine`.
#[inline(always)]
pub fn engine(&self) -> &'e Engine {
self.engine
}
/// _[INTERNALS]_ The current set of modules imported via `import` statements.
/// Available under the `internals` feature only.
#[cfg(feature = "internals")]
#[cfg(not(feature = "no_module"))]
#[inline(always)]
pub fn imports(&self) -> Option<&Imports> {
self.mods
}
/// Get an iterator over the namespaces containing definition of all script-defined functions.
#[inline(always)]
pub fn iter_namespaces(&self) -> impl Iterator<Item = &'pm Module> + 'm {
@ -181,9 +203,11 @@ impl FnPtr {
args.insert(0, obj);
}
let mut mods = ctx.mods.cloned().unwrap_or_default();
ctx.engine()
.exec_fn_call(
&mut Default::default(),
&mut mods,
&mut Default::default(),
ctx.lib,
fn_name,
@ -396,14 +420,14 @@ impl CallableFunction {
Self::Script(f) => f.access,
}
}
/// Get a reference to a native Rust function.
/// Get a shared reference to a native Rust function.
///
/// # Panics
///
/// Panics if the `CallableFunction` is not `Pure` or `Method`.
pub fn get_native_fn(&self) -> &FnAny {
pub fn get_native_fn(&self) -> &Shared<FnAny> {
match self {
Self::Pure(f) | Self::Method(f) => f.as_ref(),
Self::Pure(f) | Self::Method(f) => f,
Self::Iterator(_) | Self::Plugin(_) => unreachable!(),
#[cfg(not(feature = "no_function"))]
@ -416,25 +440,12 @@ impl CallableFunction {
///
/// Panics if the `CallableFunction` is not `Script`.
#[cfg(not(feature = "no_function"))]
pub fn get_shared_fn_def(&self) -> &Shared<ScriptFnDef> {
pub fn get_fn_def(&self) -> &Shared<ScriptFnDef> {
match self {
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) | Self::Plugin(_) => unreachable!(),
Self::Script(f) => f,
}
}
/// Get a reference to a script-defined function definition.
///
/// # Panics
///
/// Panics if the `CallableFunction` is not `Script`.
pub fn get_fn_def(&self) -> &ScriptFnDef {
match self {
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) | Self::Plugin(_) => unreachable!(),
#[cfg(not(feature = "no_function"))]
Self::Script(f) => f,
}
}
/// Get a reference to an iterator function.
///
/// # Panics
@ -449,14 +460,14 @@ impl CallableFunction {
Self::Script(_) => unreachable!(),
}
}
/// Get a reference to a plugin function.
/// Get a shared reference to a plugin function.
///
/// # Panics
///
/// Panics if the `CallableFunction` is not `Plugin`.
pub fn get_plugin_fn<'s>(&'s self) -> &FnPlugin {
pub fn get_plugin_fn<'s>(&'s self) -> &Shared<FnPlugin> {
match self {
Self::Plugin(f) => f.as_ref(),
Self::Plugin(f) => f,
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) => unreachable!(),
#[cfg(not(feature = "no_function"))]

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,26 @@ impl Module {
&& self.type_iterators.is_empty()
}
/// Is the module indexed?
///
/// # Example
///
/// ```
/// use rhai::Module;
///
/// let mut module = Module::new();
/// assert!(!module.is_indexed());
///
/// # #[cfg(not(feature = "no_module"))]
/// # {
/// 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 {
@ -329,7 +343,7 @@ impl Module {
&& fn_name == name
},
)
.map(|FuncInfo { func, .. }| func.get_shared_fn_def())
.map(|FuncInfo { func, .. }| func.get_fn_def())
}
/// Does a sub-module exist in the module?
@ -1120,7 +1134,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)
@ -1265,6 +1279,7 @@ impl Module {
/// Get an iterator to the functions in the module.
#[cfg(not(feature = "no_optimize"))]
#[cfg(not(feature = "no_function"))]
#[inline(always)]
pub(crate) fn iter_fn(&self) -> impl Iterator<Item = &FuncInfo> {
self.functions.values()
@ -1286,7 +1301,7 @@ impl Module {
.values()
.map(|f| &f.func)
.filter(|f| f.is_script())
.map(CallableFunction::get_shared_fn_def)
.map(CallableFunction::get_fn_def)
.map(|f| {
let func = f.clone();
(f.access, f.name.as_str(), f.params.len(), func)
@ -1374,7 +1389,7 @@ impl Module {
// Modules left in the scope become sub-modules
mods.into_iter().for_each(|(alias, m)| {
module.modules.insert(alias.to_string(), m);
module.modules.insert(alias.to_string(), m.as_ref().clone());
});
// Non-private functions defined become module functions
@ -1395,10 +1410,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

@ -1,4 +1,5 @@
use crate::engine::Engine;
use crate::fn_native::Shared;
use crate::module::{Module, ModuleResolver};
use crate::result::EvalAltResult;
use crate::token::Position;
@ -90,7 +91,7 @@ impl ModuleResolver for ModuleResolversCollection {
engine: &Engine,
path: &str,
pos: Position,
) -> Result<Module, Box<EvalAltResult>> {
) -> Result<Shared<Module>, Box<EvalAltResult>> {
for resolver in self.0.iter() {
match resolver.resolve(engine, path, pos) {
Ok(module) => return Ok(module),

View File

@ -1,6 +1,5 @@
use crate::ast::AST;
use crate::engine::Engine;
use crate::fn_native::Locked;
use crate::fn_native::{Locked, Shared};
use crate::module::{Module, ModuleResolver};
use crate::result::EvalAltResult;
use crate::token::Position;
@ -40,7 +39,7 @@ use crate::stdlib::{boxed::Box, collections::HashMap, path::PathBuf, string::Str
pub struct FileModuleResolver {
path: PathBuf,
extension: String,
cache: Locked<HashMap<PathBuf, AST>>,
cache: Locked<HashMap<PathBuf, Shared<Module>>>,
}
impl Default for FileModuleResolver {
@ -119,16 +118,6 @@ impl FileModuleResolver {
pub fn new() -> Self {
Default::default()
}
/// Create a `Module` from a file path.
#[inline(always)]
pub fn create_module<P: Into<PathBuf>>(
&self,
engine: &Engine,
path: &str,
) -> Result<Module, Box<EvalAltResult>> {
self.resolve(engine, path, Default::default())
}
}
impl ModuleResolver for FileModuleResolver {
@ -137,48 +126,51 @@ impl ModuleResolver for FileModuleResolver {
engine: &Engine,
path: &str,
pos: Position,
) -> Result<Module, Box<EvalAltResult>> {
) -> Result<Shared<Module>, Box<EvalAltResult>> {
// Construct the script file path
let mut file_path = self.path.clone();
file_path.push(path);
file_path.set_extension(&self.extension); // Force extension
let scope = Default::default();
let module;
// See if it is cached
let ast = {
let mut module = None;
let module_ref = {
#[cfg(not(feature = "sync"))]
let c = self.cache.borrow();
#[cfg(feature = "sync")]
let c = self.cache.read().unwrap();
if let Some(ast) = c.get(&file_path) {
module = Module::eval_ast_as_new(scope, ast, engine).map_err(|err| {
Box::new(EvalAltResult::ErrorInModule(path.to_string(), err, pos))
})?;
None
if let Some(module) = c.get(&file_path) {
module.clone()
} else {
// Load the file and compile it if not found
let ast = engine.compile_file(file_path.clone()).map_err(|err| {
Box::new(EvalAltResult::ErrorInModule(path.to_string(), err, pos))
})?;
module = Module::eval_ast_as_new(scope, &ast, engine).map_err(|err| {
let mut m = Module::eval_ast_as_new(scope, &ast, engine).map_err(|err| {
Box::new(EvalAltResult::ErrorInModule(path.to_string(), err, pos))
})?;
Some(ast)
m.build_index();
let m: Shared<Module> = m.into();
module = Some(m.clone());
m
}
};
if let Some(ast) = ast {
if let Some(module) = module {
// Put it into the cache
#[cfg(not(feature = "sync"))]
self.cache.borrow_mut().insert(file_path, ast);
self.cache.borrow_mut().insert(file_path, module);
#[cfg(feature = "sync")]
self.cache.write().unwrap().insert(file_path, ast);
self.cache.write().unwrap().insert(file_path, module);
}
Ok(module)
Ok(module_ref)
}
}

View File

@ -1,5 +1,5 @@
use crate::engine::Engine;
use crate::fn_native::SendSync;
use crate::fn_native::{SendSync, Shared};
use crate::module::Module;
use crate::result::EvalAltResult;
use crate::token::Position;
@ -28,5 +28,5 @@ pub trait ModuleResolver: SendSync {
engine: &Engine,
path: &str,
pos: Position,
) -> Result<Module, Box<EvalAltResult>>;
) -> Result<Shared<Module>, Box<EvalAltResult>>;
}

View File

@ -1,4 +1,5 @@
use crate::engine::Engine;
use crate::fn_native::Shared;
use crate::module::{Module, ModuleResolver};
use crate::result::EvalAltResult;
use crate::token::Position;
@ -23,7 +24,7 @@ use crate::stdlib::{boxed::Box, collections::HashMap, ops::AddAssign, string::St
/// engine.set_module_resolver(Some(resolver));
/// ```
#[derive(Debug, Clone, Default)]
pub struct StaticModuleResolver(HashMap<String, Module>);
pub struct StaticModuleResolver(HashMap<String, Shared<Module>>);
impl StaticModuleResolver {
/// Create a new `StaticModuleResolver`.
@ -48,12 +49,13 @@ impl StaticModuleResolver {
}
/// Add a module keyed by its path.
#[inline(always)]
pub fn insert(&mut self, path: impl Into<String>, module: Module) {
self.0.insert(path.into(), module);
pub fn insert(&mut self, path: impl Into<String>, mut module: Module) {
module.build_index();
self.0.insert(path.into(), module.into());
}
/// Remove a module given its path.
#[inline(always)]
pub fn remove(&mut self, path: &str) -> Option<Module> {
pub fn remove(&mut self, path: &str) -> Option<Shared<Module>> {
self.0.remove(path)
}
/// Does the path exist?
@ -63,17 +65,12 @@ impl StaticModuleResolver {
}
/// Get an iterator of all the modules.
#[inline(always)]
pub fn iter(&self) -> impl Iterator<Item = (&str, &Module)> {
self.0.iter().map(|(k, v)| (k.as_str(), v))
pub fn iter(&self) -> impl Iterator<Item = (&str, Shared<Module>)> {
self.0.iter().map(|(k, v)| (k.as_str(), v.clone()))
}
/// Get a mutable iterator of all the modules.
#[inline(always)]
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&str, &mut Module)> {
self.0.iter_mut().map(|(k, v)| (k.as_str(), v))
}
/// Get a mutable iterator of all the modules.
#[inline(always)]
pub fn into_iter(self) -> impl Iterator<Item = (String, Module)> {
pub fn into_iter(self) -> impl Iterator<Item = (String, Shared<Module>)> {
self.0.into_iter()
}
/// Get an iterator of all the module paths.
@ -83,13 +80,8 @@ impl StaticModuleResolver {
}
/// Get an iterator of all the modules.
#[inline(always)]
pub fn values(&self) -> impl Iterator<Item = &Module> {
self.0.values()
}
/// Get a mutable iterator of all the modules.
#[inline(always)]
pub fn values_mut(&mut self) -> impl Iterator<Item = &mut Module> {
self.0.values_mut()
pub fn values<'a>(&'a self) -> impl Iterator<Item = Shared<Module>> + 'a {
self.0.values().map(|m| m.clone())
}
/// Remove all modules.
#[inline(always)]
@ -118,7 +110,12 @@ impl StaticModuleResolver {
impl ModuleResolver for StaticModuleResolver {
#[inline(always)]
fn resolve(&self, _: &Engine, path: &str, pos: Position) -> Result<Module, Box<EvalAltResult>> {
fn resolve(
&self,
_: &Engine,
path: &str,
pos: Position,
) -> Result<Shared<Module>, Box<EvalAltResult>> {
self.0
.get(path)
.cloned()

View File

@ -140,6 +140,7 @@ fn call_fn_with_constant_arguments(
state
.engine
.call_native_fn(
&mut Default::default(),
&mut Default::default(),
state.lib,
fn_name,

View File

@ -32,7 +32,7 @@ use crate::{
use crate::stdlib::{
borrow::Cow,
boxed::Box,
collections::{HashMap, HashSet},
collections::HashMap,
format,
hash::Hash,
iter::empty,
@ -45,6 +45,9 @@ use crate::stdlib::{
#[cfg(not(feature = "no_function"))]
use crate::stdlib::hash::Hasher;
#[cfg(not(feature = "no_closure"))]
use crate::stdlib::collections::HashSet;
#[cfg(not(feature = "no_std"))]
#[cfg(not(feature = "no_function"))]
use crate::stdlib::collections::hash_map::DefaultHasher;