Use combine_flatten for plugin modules.

This commit is contained in:
Stephen Chung 2020-08-21 21:48:45 +08:00
parent 40f71320f3
commit 08977e2a62
12 changed files with 64 additions and 19 deletions

View File

@ -4,6 +4,15 @@ Rhai Release Notes
Version 0.19.0
==============
New features
------------
* Adds `Module::combine_flatten` to combine two modules while flattening to the root level.
Version 0.18.2
==============
Bug fixes
---------

View File

@ -931,6 +931,24 @@ impl Module {
/// Combine another module into this module.
/// The other module is consumed to merge into this module.
pub fn combine(&mut self, other: Self) -> &mut Self {
self.modules.extend(other.modules.into_iter());
self.variables.extend(other.variables.into_iter());
self.functions.extend(other.functions.into_iter());
self.type_iterators.extend(other.type_iterators.into_iter());
self.all_functions.clear();
self.all_variables.clear();
self.indexed = false;
self
}
/// Combine another module into this module.
/// The other module is consumed to merge into this module.
/// Sub-modules are flattened onto the root module, with higher level overriding lower level.
pub fn combine_flatten(&mut self, other: Self) -> &mut Self {
other.modules.into_iter().for_each(|(_, m)| {
self.combine_flatten(m);
});
self.variables.extend(other.variables.into_iter());
self.functions.extend(other.functions.into_iter());
self.type_iterators.extend(other.type_iterators.into_iter());
@ -942,15 +960,26 @@ impl Module {
/// Merge another module into this module.
pub fn merge(&mut self, other: &Self) -> &mut Self {
self.merge_filtered(other, |_, _, _| true)
self.merge_filtered(other, &|_, _, _| true)
}
/// Merge another module into this module, with only selected script-defined functions based on a filter predicate.
pub(crate) fn merge_filtered(
&mut self,
other: &Self,
_filter: impl Fn(FnAccess, &str, usize) -> bool,
_filter: &impl Fn(FnAccess, &str, usize) -> bool,
) -> &mut Self {
#[cfg(not(feature = "no_function"))]
for (k, v) in &other.modules {
let mut m = Self::new();
m.merge_filtered(v, _filter);
self.modules.insert(k.clone(), m);
}
#[cfg(feature = "no_function")]
self.modules
.extend(other.modules.iter().map(|(k, v)| (k.clone(), v.clone())));
self.variables
.extend(other.variables.iter().map(|(k, v)| (k.clone(), v.clone())));

View File

@ -59,7 +59,7 @@ macro_rules! reg_pad {
}
def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, {
lib.combine(exported_module!(array_functions));
lib.combine_flatten(exported_module!(array_functions));
reg_functions!(lib += basic; INT, bool, char, ImmutableString, FnPtr, Array, Unit);
reg_pad!(lib, INT, bool, char, ImmutableString, FnPtr, Array, Unit);

View File

@ -3,7 +3,7 @@ use crate::fn_native::FnPtr;
use crate::plugin::*;
def_package!(crate:BasicFnPackage:"Basic Fn functions.", lib, {
lib.combine(exported_module!(fn_ptr_functions));
lib.combine_flatten(exported_module!(fn_ptr_functions));
});
#[export_module]

View File

@ -47,7 +47,7 @@ macro_rules! gen_cmp_functions {
macro_rules! reg_functions {
($mod_name:ident += $root:ident ; $($arg_type:ident),+) => {
$($mod_name.combine(exported_module!($root::$arg_type::functions));)*
$($mod_name.combine_flatten(exported_module!($root::$arg_type::functions));)*
}
}

View File

@ -9,11 +9,11 @@ use crate::plugin::*;
use crate::stdlib::vec::Vec;
def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, {
lib.combine(exported_module!(map_functions));
lib.combine_flatten(exported_module!(map_functions));
// Register map access functions
#[cfg(not(feature = "no_index"))]
lib.combine(exported_module!(index_functions));
lib.combine_flatten(exported_module!(index_functions));
});
#[export_module]

View File

@ -26,10 +26,10 @@ def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, {
#[cfg(not(feature = "no_float"))]
{
// Floating point functions
lib.combine(exported_module!(float_functions));
lib.combine_flatten(exported_module!(float_functions));
// Trig functions
lib.combine(exported_module!(trig_functions));
lib.combine_flatten(exported_module!(trig_functions));
// Register conversion functions
lib.set_fn_1("to_float", |x: INT| Ok(x as FLOAT));

View File

@ -60,9 +60,9 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str
reg_functions!(lib += float; f32, f64);
#[cfg(not(feature = "no_index"))]
lib.combine(exported_module!(index_functions));
lib.combine_flatten(exported_module!(index_functions));
lib.combine(exported_module!(string_functions));
lib.combine_flatten(exported_module!(string_functions));
lib.set_raw_fn(
"pad",

View File

@ -26,7 +26,7 @@ use instant::Instant;
def_package!(crate:BasicTimePackage:"Basic timing utilities.", lib, {
// Register date/time functions
lib.combine(exported_module!(time_functions));
lib.combine_flatten(exported_module!(time_functions));
});
#[export_module]

View File

@ -133,7 +133,7 @@ impl AST {
filter: impl Fn(FnAccess, &str, usize) -> bool,
) -> Self {
let mut functions: Module = Default::default();
functions.merge_filtered(&self.1, filter);
functions.merge_filtered(&self.1, &filter);
Self(Default::default(), functions)
}
@ -266,7 +266,7 @@ impl AST {
};
let mut functions = functions.clone();
functions.merge_filtered(&other.1, filter);
functions.merge_filtered(&other.1, &filter);
Self::new(ast, functions)
}

View File

@ -17,6 +17,7 @@ use crate::stdlib::boxed::Box;
impl Engine {
/// Load a new package into the `Engine`.
/// Anything that can be converted into a `PackageLibrary` is accepted, including a simple `Module`.
///
/// When searching for functions, packages loaded later are preferred.
/// In other words, loaded packages are searched in reverse order.

View File

@ -10,11 +10,16 @@ mod test {
pub mod special_array_package {
use rhai::{Array, INT};
#[cfg(not(feature = "no_object"))]
#[rhai_mod()]
pub mod feature {
#[rhai_fn(get = "foo", return_raw)]
#[inline(always)]
pub fn foo(array: &mut Array) -> Result<Dynamic, Box<EvalAltResult>> {
Ok(array[0].clone())
}
}
#[rhai_fn(name = "test")]
#[inline(always)]
pub fn len(array: &mut Array, mul: INT) -> INT {
@ -59,7 +64,8 @@ gen_unary_functions!(greet = make_greeting(INT, bool, char) -> String);
fn test_plugins_package() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
let m = exported_module!(test::special_array_package);
let mut m = Module::new();
m.combine_flatten(exported_module!(test::special_array_package));
engine.load_package(m);
reg_functions!(engine += greet::single(INT, bool, char));