Engine::register_global_module and Engine::register_static_module must be passed shared Module.

This commit is contained in:
Stephen Chung 2020-12-23 10:08:43 +08:00
parent 15fb03218c
commit f55a56c6bc
11 changed files with 23 additions and 22 deletions

View File

@ -4,12 +4,15 @@ Rhai Release Notes
Version 0.19.9 Version 0.19.9
============== ==============
This version removes the confusing differences between _packages_ and _modules_
by unifying the terminology and API under the global umbrella of _modules_.
Breaking changes Breaking changes
---------------- ----------------
* `Engine::load_package` is renamed `Engine::register_global_module`. * `Engine::load_package` is renamed `Engine::register_global_module` and now must explicitly pass a shared [`Module`].
* `Engine::register_module` is renamed `Engine::register_static_module` and now must explicitly pass a shared [`Module`].
* `Package::get` is renamed `Package::as_shared_module`. * `Package::get` is renamed `Package::as_shared_module`.
* `Engine::register_module` is renamed `Engine::register_static_module`.
Version 0.19.8 Version 0.19.8

View File

@ -20,7 +20,7 @@ fn bench_eval_module(bench: &mut Bencher) {
let module = Module::eval_ast_as_new(Default::default(), &ast, &engine).unwrap(); let module = Module::eval_ast_as_new(Default::default(), &ast, &engine).unwrap();
engine.register_static_module("testing", module); engine.register_static_module("testing", module.into());
let ast = engine let ast = engine
.compile( .compile(

View File

@ -160,7 +160,7 @@ pub fn export_fn(
/// ///
/// let module = exported_module!(my_plugin_module); /// let module = exported_module!(my_plugin_module);
/// ///
/// engine.register_global_module(module); /// engine.register_global_module(module.into);
/// ///
/// assert_eq!(engine.eval::<i64>("foo(bar())")?, 42); /// assert_eq!(engine.eval::<i64>("foo(bar())")?, 42);
/// # Ok(()) /// # Ok(())
@ -203,7 +203,7 @@ pub fn export_module(
/// ///
/// let module = exported_module!(my_plugin_module); /// let module = exported_module!(my_plugin_module);
/// ///
/// engine.register_global_module(module); /// engine.register_global_module(module.into());
/// ///
/// assert_eq!(engine.eval::<i64>("foo(bar())")?, 42); /// assert_eq!(engine.eval::<i64>("foo(bar())")?, 42);
/// # Ok(()) /// # Ok(())
@ -250,7 +250,7 @@ pub fn exported_module(module_path: proc_macro::TokenStream) -> proc_macro::Toke
/// let mut module = Module::new(); /// let mut module = Module::new();
/// combine_with_exported_module!(&mut module, "my_plugin_module_ID", my_plugin_module); /// combine_with_exported_module!(&mut module, "my_plugin_module_ID", my_plugin_module);
/// ///
/// engine.register_global_module(module); /// engine.register_global_module(module.into());
/// ///
/// assert_eq!(engine.eval::<i64>("foo(bar())")?, 42); /// assert_eq!(engine.eval::<i64>("foo(bar())")?, 42);
/// # Ok(()) /// # Ok(())
@ -324,7 +324,7 @@ pub fn register_exported_fn(args: proc_macro::TokenStream) -> proc_macro::TokenS
/// let mut module = Module::new(); /// let mut module = Module::new();
/// set_exported_fn!(module, "func", my_plugin_function); /// set_exported_fn!(module, "func", my_plugin_function);
/// ///
/// engine.register_global_module(module); /// engine.register_global_module(module.into());
/// ///
/// assert_eq!(engine.eval::<i64>("func(21)")?, 42); /// assert_eq!(engine.eval::<i64>("func(21)")?, 42);
/// # Ok(()) /// # Ok(())
@ -366,7 +366,7 @@ pub fn set_exported_fn(args: proc_macro::TokenStream) -> proc_macro::TokenStream
/// let mut module = Module::new(); /// let mut module = Module::new();
/// set_exported_global_fn!(module, "func", my_plugin_function); /// set_exported_global_fn!(module, "func", my_plugin_function);
/// ///
/// engine.register_static_module("test", module); /// engine.register_static_module("test", module.into());
/// ///
/// assert_eq!(engine.eval::<i64>("func(21)")?, 42); /// assert_eq!(engine.eval::<i64>("func(21)")?, 42);
/// # Ok(()) /// # Ok(())

View File

@ -257,7 +257,7 @@ mod multiple_fn_rename {
fn multiple_fn_rename_test() -> Result<(), Box<EvalAltResult>> { fn multiple_fn_rename_test() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new(); let mut engine = Engine::new();
let m = rhai::exported_module!(crate::multiple_fn_rename::my_adds); let m = rhai::exported_module!(crate::multiple_fn_rename::my_adds);
engine.register_global_module(m); engine.register_global_module(m.into());
let output_array = engine.eval::<Array>( let output_array = engine.eval::<Array>(
r#" r#"

View File

@ -732,9 +732,9 @@ impl Engine {
/// When searching for functions, modules loaded later are preferred. /// When searching for functions, modules loaded later are preferred.
/// In other words, loaded modules are searched in reverse order. /// In other words, loaded modules are searched in reverse order.
#[inline(always)] #[inline(always)]
pub fn register_global_module(&mut self, package: impl Into<Shared<Module>>) -> &mut Self { pub fn register_global_module(&mut self, module: Shared<Module>) -> &mut Self {
// Insert the module into the front // Insert the module into the front
self.global_modules.insert(0, package.into()); self.global_modules.insert(0, module);
self self
} }
/// Register a shared [`Module`][crate::Module] as a static module namespace with the [`Engine`]. /// Register a shared [`Module`][crate::Module] as a static module namespace with the [`Engine`].
@ -754,7 +754,7 @@ impl Engine {
/// module.set_fn_1("calc", |x: i64| Ok(x + 1)); /// module.set_fn_1("calc", |x: i64| Ok(x + 1));
/// ///
/// // Register the module as a fixed sub-module /// // Register the module as a fixed sub-module
/// engine.register_static_module("CalcService", module); /// engine.register_static_module("CalcService", module.into());
/// ///
/// assert_eq!(engine.eval::<i64>("CalcService::calc(41)")?, 42); /// assert_eq!(engine.eval::<i64>("CalcService::calc(41)")?, 42);
/// # Ok(()) /// # Ok(())
@ -764,10 +764,8 @@ impl Engine {
pub fn register_static_module( pub fn register_static_module(
&mut self, &mut self,
name: impl Into<crate::ImmutableString>, name: impl Into<crate::ImmutableString>,
module: impl Into<Shared<Module>>, module: Shared<Module>,
) -> &mut Self { ) -> &mut Self {
let module = module.into();
if !module.is_indexed() { if !module.is_indexed() {
// Index the module (making a clone copy if necessary) if it is not indexed // Index the module (making a clone copy if necessary) if it is not indexed
let mut module = crate::fn_native::shared_take_or_clone(module); let mut module = crate::fn_native::shared_take_or_clone(module);

View File

@ -101,7 +101,7 @@ fn test_for_module_iterator() -> Result<(), Box<EvalAltResult>> {
let mut module = Module::new(); let mut module = Module::new();
module.set_sub_module("inner", sub_module); module.set_sub_module("inner", sub_module);
engine.register_static_module("testing", module); engine.register_static_module("testing", module.into());
let script = r#" let script = r#"
let item = testing::inner::new_ts(); let item = testing::inner::new_ts();

View File

@ -62,7 +62,7 @@ fn test_functions_namespaces() -> Result<(), Box<EvalAltResult>> {
let hash = m.set_fn_0("test", || Ok(999 as INT)); let hash = m.set_fn_0("test", || Ok(999 as INT));
m.update_fn_namespace(hash, FnNamespace::Global); m.update_fn_namespace(hash, FnNamespace::Global);
engine.register_static_module("hello", m); engine.register_static_module("hello", m.into());
assert_eq!(engine.eval::<INT>("test()")?, 999); assert_eq!(engine.eval::<INT>("test()")?, 999);
assert_eq!(engine.eval::<INT>("fn test() { 123 } test()")?, 123); assert_eq!(engine.eval::<INT>("fn test() { 123 } test()")?, 123);

View File

@ -44,7 +44,7 @@ fn test_module_sub_module() -> Result<(), Box<EvalAltResult>> {
assert_eq!(m2.get_var_value::<INT>("answer").unwrap(), 41); assert_eq!(m2.get_var_value::<INT>("answer").unwrap(), 41);
let mut engine = Engine::new(); let mut engine = Engine::new();
engine.register_static_module("question", module); engine.register_static_module("question", module.into());
assert_eq!(engine.eval::<INT>("question::MYSTIC_NUMBER")?, 42); assert_eq!(engine.eval::<INT>("question::MYSTIC_NUMBER")?, 42);
assert!(engine.eval::<INT>("MYSTIC_NUMBER").is_err()); assert!(engine.eval::<INT>("MYSTIC_NUMBER").is_err());

View File

@ -37,7 +37,7 @@ fn test_packages_with_script() -> Result<(), Box<EvalAltResult>> {
let ast = engine.compile("fn foo(x) { x + 1 } fn bar(x) { foo(x) + 1 }")?; let ast = engine.compile("fn foo(x) { x + 1 } fn bar(x) { foo(x) + 1 }")?;
let module = Module::eval_ast_as_new(Scope::new(), &ast, &engine)?; let module = Module::eval_ast_as_new(Scope::new(), &ast, &engine)?;
engine.register_global_module(module); engine.register_global_module(module.into());
assert_eq!(engine.eval::<INT>("foo(41)")?, 42); assert_eq!(engine.eval::<INT>("foo(41)")?, 42);
assert_eq!(engine.eval::<INT>("bar(40)")?, 42); assert_eq!(engine.eval::<INT>("bar(40)")?, 42);

View File

@ -77,7 +77,7 @@ fn test_plugins_package() -> Result<(), Box<EvalAltResult>> {
let mut m = Module::new(); let mut m = Module::new();
combine_with_exported_module!(&mut m, "test", test::special_array_package); combine_with_exported_module!(&mut m, "test", test::special_array_package);
engine.register_global_module(m); engine.register_global_module(m.into());
reg_functions!(engine += greet::single(INT, bool, char)); reg_functions!(engine += greet::single(INT, bool, char));
@ -95,7 +95,7 @@ fn test_plugins_package() -> Result<(), Box<EvalAltResult>> {
"6 kitties" "6 kitties"
); );
engine.register_static_module("test", exported_module!(test::special_array_package)); engine.register_static_module("test", exported_module!(test::special_array_package).into());
assert_eq!(engine.eval::<INT>("test::MYSTIC_NUMBER")?, 42); assert_eq!(engine.eval::<INT>("test::MYSTIC_NUMBER")?, 42);

View File

@ -52,7 +52,7 @@ fn test_generated_ops() -> Result<(), Box<EvalAltResult>> {
register_in_bulk!(m, add, i8, i16, i32, i64); register_in_bulk!(m, add, i8, i16, i32, i64);
register_in_bulk!(m, mul, i8, i16, i32, i64); register_in_bulk!(m, mul, i8, i16, i32, i64);
engine.register_global_module(m); engine.register_global_module(m.into());
#[cfg(feature = "only_i32")] #[cfg(feature = "only_i32")]
assert_eq!(engine.eval::<INT>("let a = 0; add_i32(a, 1)")?, 1); assert_eq!(engine.eval::<INT>("let a = 0; add_i32(a, 1)")?, 1);