Merge pull request #56 from schungx/plugins_dev
Add combine_with_exported_module and a type cast fix for constants.
This commit is contained in:
commit
206f21c26f
@ -152,6 +152,19 @@ pub fn exported_module(module_path: proc_macro::TokenStream) -> proc_macro::Toke
|
||||
proc_macro::TokenStream::from(tokens)
|
||||
}
|
||||
|
||||
#[proc_macro]
|
||||
pub fn combine_with_exported_module(args: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let (module_expr, _export_name, module_path) = match crate::register::parse_register_macro(args)
|
||||
{
|
||||
Ok(triple) => triple,
|
||||
Err(e) => return e.to_compile_error().into(),
|
||||
};
|
||||
let tokens = quote! {
|
||||
#module_path::rhai_generate_into_module(#module_expr, true);
|
||||
};
|
||||
proc_macro::TokenStream::from(tokens)
|
||||
}
|
||||
|
||||
#[proc_macro]
|
||||
pub fn register_exported_fn(args: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let (engine_expr, export_name, rust_modpath) = match crate::register::parse_register_macro(args)
|
||||
|
@ -140,12 +140,13 @@ impl Parse for Module {
|
||||
ref expr,
|
||||
ident,
|
||||
attrs,
|
||||
ty,
|
||||
..
|
||||
}) => {
|
||||
// #[cfg] attributes are not allowed on const declarations
|
||||
crate::attrs::deny_cfg_attr(&attrs)?;
|
||||
if let syn::Visibility::Public(_) = vis {
|
||||
consts.push((ident.to_string(), expr.as_ref().clone()));
|
||||
consts.push((ident.to_string(), ty.clone(), expr.as_ref().clone()));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
@ -191,6 +192,7 @@ impl Parse for Module {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl Module {
|
||||
pub fn attrs(&self) -> Option<&Vec<syn::Attribute>> {
|
||||
self.mod_all.as_ref().map(|m| &m.attrs)
|
||||
|
@ -6,7 +6,7 @@ use crate::attrs::ExportScope;
|
||||
use crate::function::ExportedFn;
|
||||
use crate::module::Module;
|
||||
|
||||
pub(crate) type ExportedConst = (String, syn::Expr);
|
||||
pub(crate) type ExportedConst = (String, Box<syn::Type>, syn::Expr);
|
||||
|
||||
pub(crate) fn generate_body(
|
||||
fns: &mut [ExportedFn],
|
||||
@ -17,13 +17,15 @@ pub(crate) fn generate_body(
|
||||
let mut set_fn_stmts: Vec<syn::Stmt> = Vec::new();
|
||||
let mut set_const_stmts: Vec<syn::Stmt> = Vec::new();
|
||||
let mut add_mod_blocks: Vec<syn::ExprBlock> = Vec::new();
|
||||
let mut set_flattened_mod_blocks: Vec<syn::ExprBlock> = Vec::new();
|
||||
let str_type_path = syn::parse2::<syn::Path>(quote! { str }).unwrap();
|
||||
|
||||
for (const_name, const_expr) in consts {
|
||||
for (const_name, _, _) in consts {
|
||||
let const_literal = syn::LitStr::new(&const_name, proc_macro2::Span::call_site());
|
||||
let const_ref = syn::Ident::new(&const_name, proc_macro2::Span::call_site());
|
||||
set_const_stmts.push(
|
||||
syn::parse2::<syn::Stmt>(quote! {
|
||||
m.set_var(#const_literal, #const_expr);
|
||||
m.set_var(#const_literal, #const_ref);
|
||||
})
|
||||
.unwrap(),
|
||||
);
|
||||
@ -54,6 +56,14 @@ pub(crate) fn generate_body(
|
||||
})
|
||||
.unwrap(),
|
||||
);
|
||||
set_flattened_mod_blocks.push(
|
||||
syn::parse2::<syn::ExprBlock>(quote! {
|
||||
#(#cfg_attrs)* {
|
||||
self::#module_name::rhai_generate_into_module(m, flatten);
|
||||
}
|
||||
})
|
||||
.unwrap(),
|
||||
);
|
||||
}
|
||||
|
||||
// NB: these are token streams, because reparsing messes up "> >" vs ">>"
|
||||
@ -129,13 +139,22 @@ pub(crate) fn generate_body(
|
||||
pub mod generate_info {
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
#(#set_fn_stmts)*
|
||||
#(#set_const_stmts)*
|
||||
#(#add_mod_blocks)*
|
||||
m
|
||||
|
||||
if flatten {
|
||||
#(#set_flattened_mod_blocks)*
|
||||
} else {
|
||||
#(#add_mod_blocks)*
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -237,21 +237,29 @@ mod generate_tests {
|
||||
let expected = expected.to_string();
|
||||
if &actual != &expected {
|
||||
let mut counter = 0;
|
||||
let iter = actual
|
||||
.chars()
|
||||
.zip(expected.chars())
|
||||
.inspect(|_| counter += 1)
|
||||
.skip_while(|(a, e)| *a == *e);
|
||||
let _iter = actual.chars().zip(expected.chars()).skip_while(|(a, e)| {
|
||||
if *a == *e {
|
||||
counter += 1;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
});
|
||||
eprintln!("actual != expected, diverge at char {}", counter);
|
||||
/*
|
||||
let (actual_diff, expected_diff) = {
|
||||
let mut actual_diff = String::new();
|
||||
let mut expected_diff = String::new();
|
||||
for (a, e) in iter.take(50) {
|
||||
for (a, e) in _iter.take(50) {
|
||||
actual_diff.push(a);
|
||||
expected_diff.push(e);
|
||||
}
|
||||
(actual_diff, expected_diff)
|
||||
};
|
||||
eprintln!("actual != expected, diverge at char {}", counter);
|
||||
eprintln!(" actual: {}", actual_diff);
|
||||
eprintln!("expected: {}", expected_diff);
|
||||
assert!(false);
|
||||
*/
|
||||
}
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
|
@ -109,7 +109,7 @@ mod module_tests {
|
||||
assert_eq!(item_mod.submodules().len(), 1);
|
||||
assert_eq!(&item_mod.submodules()[0].consts()[0].0, "MYSTIC_NUMBER");
|
||||
assert_eq!(
|
||||
item_mod.submodules()[0].consts()[0].1,
|
||||
item_mod.submodules()[0].consts()[0].2,
|
||||
syn::parse2::<syn::Expr>(quote! { 42 }).unwrap()
|
||||
);
|
||||
}
|
||||
@ -170,7 +170,7 @@ mod module_tests {
|
||||
assert_eq!(item_mod.consts().len(), 1);
|
||||
assert_eq!(&item_mod.consts()[0].0, "MYSTIC_NUMBER");
|
||||
assert_eq!(
|
||||
item_mod.consts()[0].1,
|
||||
item_mod.consts()[0].2,
|
||||
syn::parse2::<syn::Expr>(quote! { 42 }).unwrap()
|
||||
);
|
||||
}
|
||||
@ -218,21 +218,29 @@ mod generate_tests {
|
||||
let expected = expected.to_string();
|
||||
if &actual != &expected {
|
||||
let mut counter = 0;
|
||||
let iter = actual
|
||||
.chars()
|
||||
.zip(expected.chars())
|
||||
.inspect(|_| counter += 1)
|
||||
.skip_while(|(a, e)| *a == *e);
|
||||
let (_actual_diff, _expected_diff) = {
|
||||
let _iter = actual.chars().zip(expected.chars()).skip_while(|(a, e)| {
|
||||
if *a == *e {
|
||||
counter += 1;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
});
|
||||
eprintln!("actual != expected, diverge at char {}", counter);
|
||||
/*
|
||||
let (actual_diff, expected_diff) = {
|
||||
let mut actual_diff = String::new();
|
||||
let mut expected_diff = String::new();
|
||||
for (a, e) in iter.take(50) {
|
||||
for (a, e) in _iter.take(50) {
|
||||
actual_diff.push(a);
|
||||
expected_diff.push(e);
|
||||
}
|
||||
(actual_diff, expected_diff)
|
||||
};
|
||||
eprintln!("actual != expected, diverge at char {}", counter);
|
||||
eprintln!(" actual: {}", actual_diff);
|
||||
eprintln!("expected: {}", expected_diff);
|
||||
assert!(false);
|
||||
*/
|
||||
}
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
@ -247,11 +255,16 @@ mod generate_tests {
|
||||
pub mod empty {
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
if flatten {} else {}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@ -276,12 +289,17 @@ mod generate_tests {
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_fn("get_mystic_number", FnAccess::Public, &[],
|
||||
CallableFunction::from_plugin(get_mystic_number_token()));
|
||||
m
|
||||
if flatten {} else {}
|
||||
}
|
||||
#[allow(non_camel_case_types)]
|
||||
struct get_mystic_number_token();
|
||||
@ -333,12 +351,17 @@ mod generate_tests {
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_fn("add_one_to", FnAccess::Public, &[core::any::TypeId::of::<INT>()],
|
||||
CallableFunction::from_plugin(add_one_to_token()));
|
||||
m
|
||||
if flatten {} else {}
|
||||
}
|
||||
#[allow(non_camel_case_types)]
|
||||
struct add_one_to_token();
|
||||
@ -402,17 +425,21 @@ mod generate_tests {
|
||||
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_fn("add_n", FnAccess::Public, &[core::any::TypeId::of::<INT>()],
|
||||
CallableFunction::from_plugin(add_one_to_token()));
|
||||
m.set_fn("add_n", FnAccess::Public, &[core::any::TypeId::of::<INT>(),
|
||||
core::any::TypeId::of::<INT>()],
|
||||
CallableFunction::from_plugin(add_n_to_token()));
|
||||
m
|
||||
if flatten {} else {}
|
||||
}
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
struct add_one_to_token();
|
||||
impl PluginFunction for add_one_to_token {
|
||||
@ -494,13 +521,18 @@ mod generate_tests {
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_fn("add_together", FnAccess::Public, &[core::any::TypeId::of::<INT>(),
|
||||
core::any::TypeId::of::<INT>()],
|
||||
CallableFunction::from_plugin(add_together_token()));
|
||||
m
|
||||
if flatten {} else {}
|
||||
}
|
||||
#[allow(non_camel_case_types)]
|
||||
struct add_together_token();
|
||||
@ -556,9 +588,14 @@ mod generate_tests {
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_fn("add", FnAccess::Public, &[core::any::TypeId::of::<INT>(),
|
||||
core::any::TypeId::of::<INT>()],
|
||||
CallableFunction::from_plugin(add_together_token()));
|
||||
@ -568,7 +605,7 @@ mod generate_tests {
|
||||
m.set_fn("add_together", FnAccess::Public, &[core::any::TypeId::of::<INT>(),
|
||||
core::any::TypeId::of::<INT>()],
|
||||
CallableFunction::from_plugin(add_together_token()));
|
||||
m
|
||||
if flatten {} else {}
|
||||
}
|
||||
#[allow(non_camel_case_types)]
|
||||
struct add_together_token();
|
||||
@ -606,6 +643,43 @@ mod generate_tests {
|
||||
assert_streams_eq(item_mod.generate(), expected_tokens);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_constant_type_module() {
|
||||
let input_tokens: TokenStream = quote! {
|
||||
pub mod one_constant {
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Foo(pub INT);
|
||||
|
||||
pub const MYSTIC_NUMBER: Foo = Foo(42);
|
||||
}
|
||||
};
|
||||
|
||||
let expected_tokens = quote! {
|
||||
pub mod one_constant {
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Foo(pub INT);
|
||||
|
||||
pub const MYSTIC_NUMBER: Foo = Foo(42);
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_var("MYSTIC_NUMBER", MYSTIC_NUMBER);
|
||||
if flatten {} else {}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let item_mod = syn::parse2::<Module>(input_tokens).unwrap();
|
||||
assert_streams_eq(item_mod.generate(), expected_tokens);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_constant_module() {
|
||||
let input_tokens: TokenStream = quote! {
|
||||
@ -619,12 +693,17 @@ mod generate_tests {
|
||||
pub const MYSTIC_NUMBER: INT = 42;
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
m.set_var("MYSTIC_NUMBER", 42);
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_var("MYSTIC_NUMBER", MYSTIC_NUMBER);
|
||||
if flatten {} else {}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@ -647,12 +726,17 @@ mod generate_tests {
|
||||
pub const MYSTIC_NUMBER: INT = 42;
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
m.set_var("MYSTIC_NUMBER", 42);
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_var("MYSTIC_NUMBER", MYSTIC_NUMBER);
|
||||
if flatten {} else {}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@ -677,11 +761,16 @@ mod generate_tests {
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
if flatten {} else {}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@ -707,11 +796,16 @@ mod generate_tests {
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
if flatten {} else {}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@ -743,12 +837,17 @@ mod generate_tests {
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_fn("get_mystic_number", FnAccess::Public, &[],
|
||||
CallableFunction::from_plugin(get_mystic_number_token()));
|
||||
m
|
||||
if flatten {} else {}
|
||||
}
|
||||
#[allow(non_camel_case_types)]
|
||||
struct get_mystic_number_token();
|
||||
@ -796,11 +895,16 @@ mod generate_tests {
|
||||
const MYSTIC_NUMBER: INT = 42;
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
if flatten {} else {}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@ -825,13 +929,18 @@ mod generate_tests {
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_fn("print_out_to", FnAccess::Public,
|
||||
&[core::any::TypeId::of::<ImmutableString>()],
|
||||
CallableFunction::from_plugin(print_out_to_token()));
|
||||
m
|
||||
if flatten {} else {}
|
||||
}
|
||||
#[allow(non_camel_case_types)]
|
||||
struct print_out_to_token();
|
||||
@ -884,13 +993,18 @@ mod generate_tests {
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_fn("increment", FnAccess::Public,
|
||||
&[core::any::TypeId::of::<FLOAT>()],
|
||||
CallableFunction::from_plugin(increment_token()));
|
||||
m
|
||||
if flatten {} else {}
|
||||
}
|
||||
#[allow(non_camel_case_types)]
|
||||
struct increment_token();
|
||||
@ -946,13 +1060,18 @@ mod generate_tests {
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_fn("increment", FnAccess::Public,
|
||||
&[core::any::TypeId::of::<FLOAT>()],
|
||||
CallableFunction::from_plugin(increment_token()));
|
||||
m
|
||||
if flatten {} else {}
|
||||
}
|
||||
#[allow(non_camel_case_types)]
|
||||
struct increment_token();
|
||||
@ -984,12 +1103,20 @@ mod generate_tests {
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
{ m.set_sub_module("it_is", self::it_is::rhai_module_generate()); }
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
if flatten {
|
||||
{ self::it_is::rhai_generate_into_module(m, flatten); }
|
||||
} else {
|
||||
{ m.set_sub_module("it_is", self::it_is::rhai_module_generate()); }
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@ -1013,19 +1140,25 @@ mod generate_tests {
|
||||
let expected_tokens = quote! {
|
||||
pub mod one_fn {
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
|
||||
pub mod it_is {
|
||||
pub fn increment(x: &mut FLOAT) {
|
||||
*x += 1.0 as FLOAT;
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_fn("increment", FnAccess::Public,
|
||||
&[core::any::TypeId::of::<FLOAT>()],
|
||||
CallableFunction::from_plugin(increment_token()));
|
||||
m
|
||||
if flatten {} else {}
|
||||
}
|
||||
#[allow(non_camel_case_types)]
|
||||
struct increment_token();
|
||||
@ -1057,14 +1190,24 @@ mod generate_tests {
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
#[cfg(not(feature = "no_float"))] {
|
||||
m.set_sub_module("it_is", self::it_is::rhai_module_generate());
|
||||
}
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
if flatten {
|
||||
#[cfg(not(feature = "no_float"))] {
|
||||
self::it_is::rhai_generate_into_module(m, flatten);
|
||||
}
|
||||
} else {
|
||||
#[cfg(not(feature = "no_float"))] {
|
||||
m.set_sub_module("it_is", self::it_is::rhai_module_generate());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@ -1090,12 +1233,17 @@ mod generate_tests {
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_fn("get$square", FnAccess::Public, &[core::any::TypeId::of::<u64>()],
|
||||
CallableFunction::from_plugin(int_foo_token()));
|
||||
m
|
||||
if flatten {} else {}
|
||||
}
|
||||
#[allow(non_camel_case_types)]
|
||||
struct int_foo_token();
|
||||
@ -1149,14 +1297,19 @@ mod generate_tests {
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_fn("square", FnAccess::Public, &[core::any::TypeId::of::<u64>()],
|
||||
CallableFunction::from_plugin(int_foo_token()));
|
||||
m.set_fn("get$square", FnAccess::Public, &[core::any::TypeId::of::<u64>()],
|
||||
CallableFunction::from_plugin(int_foo_token()));
|
||||
m
|
||||
if flatten {} else {}
|
||||
}
|
||||
#[allow(non_camel_case_types)]
|
||||
struct int_foo_token();
|
||||
@ -1210,14 +1363,19 @@ mod generate_tests {
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_fn("set$squared", FnAccess::Public,
|
||||
&[core::any::TypeId::of::<u64>(),
|
||||
core::any::TypeId::of::<u64>()],
|
||||
CallableFunction::from_plugin(int_foo_token()));
|
||||
m
|
||||
if flatten {} else {}
|
||||
}
|
||||
#[allow(non_camel_case_types)]
|
||||
struct int_foo_token();
|
||||
@ -1272,9 +1430,14 @@ mod generate_tests {
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_fn("set_sq", FnAccess::Public,
|
||||
&[core::any::TypeId::of::<u64>(),
|
||||
core::any::TypeId::of::<u64>()],
|
||||
@ -1283,7 +1446,7 @@ mod generate_tests {
|
||||
&[core::any::TypeId::of::<u64>(),
|
||||
core::any::TypeId::of::<u64>()],
|
||||
CallableFunction::from_plugin(int_foo_token()));
|
||||
m
|
||||
if flatten {} else {}
|
||||
}
|
||||
#[allow(non_camel_case_types)]
|
||||
struct int_foo_token();
|
||||
@ -1338,14 +1501,19 @@ mod generate_tests {
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_fn("index$get$", FnAccess::Public,
|
||||
&[core::any::TypeId::of::<MyCollection>(),
|
||||
core::any::TypeId::of::<u64>()],
|
||||
CallableFunction::from_plugin(get_by_index_token()));
|
||||
m
|
||||
if flatten {} else {}
|
||||
}
|
||||
#[allow(non_camel_case_types)]
|
||||
struct get_by_index_token();
|
||||
@ -1401,9 +1569,14 @@ mod generate_tests {
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_fn("get", FnAccess::Public,
|
||||
&[core::any::TypeId::of::<MyCollection>(),
|
||||
core::any::TypeId::of::<u64>()],
|
||||
@ -1412,7 +1585,7 @@ mod generate_tests {
|
||||
&[core::any::TypeId::of::<MyCollection>(),
|
||||
core::any::TypeId::of::<u64>()],
|
||||
CallableFunction::from_plugin(get_by_index_token()));
|
||||
m
|
||||
if flatten {} else {}
|
||||
}
|
||||
#[allow(non_camel_case_types)]
|
||||
struct get_by_index_token();
|
||||
@ -1468,15 +1641,20 @@ mod generate_tests {
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_fn("index$set$", FnAccess::Public,
|
||||
&[core::any::TypeId::of::<MyCollection>(),
|
||||
core::any::TypeId::of::<u64>(),
|
||||
core::any::TypeId::of::<FLOAT>()],
|
||||
CallableFunction::from_plugin(set_by_index_token()));
|
||||
m
|
||||
if flatten {} else {}
|
||||
}
|
||||
#[allow(non_camel_case_types)]
|
||||
struct set_by_index_token();
|
||||
@ -1534,9 +1712,14 @@ mod generate_tests {
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_fn("set", FnAccess::Public,
|
||||
&[core::any::TypeId::of::<MyCollection>(),
|
||||
core::any::TypeId::of::<u64>(),
|
||||
@ -1547,7 +1730,7 @@ mod generate_tests {
|
||||
core::any::TypeId::of::<u64>(),
|
||||
core::any::TypeId::of::<FLOAT>()],
|
||||
CallableFunction::from_plugin(set_by_index_token()));
|
||||
m
|
||||
if flatten {} else {}
|
||||
}
|
||||
#[allow(non_camel_case_types)]
|
||||
struct set_by_index_token();
|
||||
@ -1603,21 +1786,34 @@ mod generate_tests {
|
||||
pub const MYSTIC_NUMBER: INT = 42;
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
m.set_var("MYSTIC_NUMBER", 42);
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_var("MYSTIC_NUMBER", MYSTIC_NUMBER);
|
||||
if flatten {} else {}
|
||||
}
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
{ m.set_sub_module("it_is", self::it_is::rhai_module_generate()); }
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
if flatten {
|
||||
{ self::it_is::rhai_generate_into_module(m, flatten); }
|
||||
} else {
|
||||
{ m.set_sub_module("it_is", self::it_is::rhai_module_generate()); }
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@ -1644,33 +1840,52 @@ mod generate_tests {
|
||||
pub const MYSTIC_NUMBER: INT = 42;
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
m.set_var("MYSTIC_NUMBER", 42);
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_var("MYSTIC_NUMBER", MYSTIC_NUMBER);
|
||||
if flatten {} else {}
|
||||
}
|
||||
}
|
||||
pub mod second_is {
|
||||
pub const SPECIAL_CPU_NUMBER: INT = 68000;
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
m.set_var("SPECIAL_CPU_NUMBER", 68000);
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_var("SPECIAL_CPU_NUMBER", SPECIAL_CPU_NUMBER);
|
||||
if flatten {} else {}
|
||||
}
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
{ m.set_sub_module("first_is", self::first_is::rhai_module_generate()); }
|
||||
{ m.set_sub_module("second_is", self::second_is::rhai_module_generate()); }
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
if flatten {
|
||||
{ self::first_is::rhai_generate_into_module(m, flatten); }
|
||||
{ self::second_is::rhai_generate_into_module(m, flatten); }
|
||||
} else {
|
||||
{ m.set_sub_module("first_is", self::first_is::rhai_module_generate()); }
|
||||
{ m.set_sub_module("second_is", self::second_is::rhai_module_generate()); }
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@ -1721,56 +1936,91 @@ mod generate_tests {
|
||||
pub const VALUE: INT = 2;
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
m.set_var("VALUE", 2);
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_var("VALUE", VALUE);
|
||||
if flatten {} else {}
|
||||
}
|
||||
}
|
||||
pub mod right {
|
||||
pub const VALUE: INT = 7;
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
m.set_var("VALUE", 7);
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_var("VALUE", VALUE);
|
||||
if flatten {} else {}
|
||||
}
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
m.set_var("VALUE", 17);
|
||||
{ m.set_sub_module("left", self::left::rhai_module_generate()); }
|
||||
{ m.set_sub_module("right", self::right::rhai_module_generate()); }
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_var("VALUE", VALUE);
|
||||
|
||||
if flatten {
|
||||
{ self::left::rhai_generate_into_module(m, flatten); }
|
||||
{ self::right::rhai_generate_into_module(m, flatten); }
|
||||
} else {
|
||||
{ m.set_sub_module("left", self::left::rhai_module_generate()); }
|
||||
{ m.set_sub_module("right", self::right::rhai_module_generate()); }
|
||||
}
|
||||
}
|
||||
}
|
||||
pub mod right {
|
||||
pub const VALUE: INT = 3;
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
m.set_var("VALUE", 3);
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_var("VALUE", VALUE);
|
||||
if flatten {} else {}
|
||||
}
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
m.set_var("VALUE", 19);
|
||||
{ m.set_sub_module("left", self::left::rhai_module_generate()); }
|
||||
{ m.set_sub_module("right", self::right::rhai_module_generate()); }
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_var("VALUE", VALUE);
|
||||
|
||||
if flatten {
|
||||
{ self::left::rhai_generate_into_module(m, flatten); }
|
||||
{ self::right::rhai_generate_into_module(m, flatten); }
|
||||
} else {
|
||||
{ m.set_sub_module("left", self::left::rhai_module_generate()); }
|
||||
{ m.set_sub_module("right", self::right::rhai_module_generate()); }
|
||||
}
|
||||
}
|
||||
}
|
||||
pub mod right {
|
||||
pub const VALUE: INT = 36;
|
||||
@ -1778,45 +2028,75 @@ mod generate_tests {
|
||||
pub const VALUE: INT = 25;
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
m.set_var("VALUE", 25);
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_var("VALUE", VALUE);
|
||||
if flatten {} else {}
|
||||
}
|
||||
}
|
||||
pub mod right {
|
||||
pub const VALUE: INT = 1;
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
m.set_var("VALUE", 1);
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_var("VALUE", VALUE);
|
||||
if flatten {} else {}
|
||||
}
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
m.set_var("VALUE", 36);
|
||||
{ m.set_sub_module("left", self::left::rhai_module_generate()); }
|
||||
{ m.set_sub_module("right", self::right::rhai_module_generate()); }
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_var("VALUE", VALUE);
|
||||
|
||||
if flatten {
|
||||
{ self::left::rhai_generate_into_module(m, flatten); }
|
||||
{ self::right::rhai_generate_into_module(m, flatten); }
|
||||
} else {
|
||||
{ m.set_sub_module("left", self::left::rhai_module_generate()); }
|
||||
{ m.set_sub_module("right", self::right::rhai_module_generate()); }
|
||||
}
|
||||
}
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
#[allow(unused_mut)]
|
||||
|
||||
pub fn rhai_module_generate() -> Module {
|
||||
let mut m = Module::new();
|
||||
m.set_var("VALUE", 100);
|
||||
{ m.set_sub_module("left", self::left::rhai_module_generate()); }
|
||||
{ m.set_sub_module("right", self::right::rhai_module_generate()); }
|
||||
rhai_generate_into_module(&mut m, false);
|
||||
m
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
|
||||
m.set_var("VALUE", VALUE);
|
||||
|
||||
if flatten {
|
||||
{ self::left::rhai_generate_into_module(m, flatten); }
|
||||
{ self::right::rhai_generate_into_module(m, flatten); }
|
||||
} else {
|
||||
{ m.set_sub_module("left", self::left::rhai_module_generate()); }
|
||||
{ m.set_sub_module("right", self::right::rhai_module_generate()); }
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -24,6 +24,9 @@ use rhai::plugins::*; // a "prelude" import for macros
|
||||
|
||||
#[export_module]
|
||||
mod my_module {
|
||||
// This constant will be registered as a the constant variable 'SOME_NUMBER'.
|
||||
pub const SOME_NUMBER: i64 = 42;
|
||||
|
||||
// This function will be registered as 'greet'.
|
||||
pub fn greet(name: &str) -> String {
|
||||
format!("hello, {}!", name)
|
||||
|
@ -63,8 +63,11 @@ By far the easiest way to create a custom module is to call `Module::merge_flatt
|
||||
|
||||
In fact, this exactly is how Rhai's built-in packages, such as `BasicMathPackage`, are implemented.
|
||||
|
||||
`rhai::plugins::exported_module!` generates a module from the [plugins][plugin module] definition,
|
||||
and `Module::merge_flatten` consumes its, adding all its registered functions into the package itself.
|
||||
`rhai::plugins::combine_with_exported_module!` adds all functions and constants from the
|
||||
[plugins][plugin module] definition into the package itself.
|
||||
|
||||
All sub-modules are _flattened_ (i.e. all functions and constants defined within sub-modules are registered
|
||||
at the top level) and so there will not be any sub-modules added to the package.
|
||||
|
||||
```rust
|
||||
// Import necessary types and traits.
|
||||
@ -84,6 +87,14 @@ mod my_module {
|
||||
pub fn get_num() -> i64 {
|
||||
42
|
||||
}
|
||||
|
||||
// This is a sub-module, but if using combine_with_exported_module!, it will
|
||||
// be flattened and all functions registered at the top level.
|
||||
pub mod my_sub_module {
|
||||
pub fn get_sub_num() -> i64 {
|
||||
0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Define the package 'MyPackage'.
|
||||
@ -94,7 +105,19 @@ def_package!(rhai:MyPackage:"My own personal super package", module, {
|
||||
BasicArrayPackage::init(module);
|
||||
BasicMapPackage::init(module);
|
||||
|
||||
// Merge the plugin module into the custom package.
|
||||
module.merge_flatten(exported_module!(my_module));
|
||||
// Merge all registered functions and constants from the plugin module into the custom package.
|
||||
//
|
||||
// Functions in the sub-module 'my_sub_module' are flattened and registered at the top level
|
||||
// instead of in a sub-module.
|
||||
//
|
||||
// The text string name in the middle parameter can be anything and is reserved for future use;
|
||||
// it is recommended to be an ID string that uniquely identifies the module.
|
||||
//
|
||||
// This call ends up registering three functions at the top level of the package:
|
||||
// 1) greet
|
||||
// 2) get_num
|
||||
// 3) get_sub_num (flattened from sub-module 'my_sub_module')
|
||||
//
|
||||
combine_with_exported_module!(module, "my-functions", my_module));
|
||||
});
|
||||
```
|
||||
|
@ -185,7 +185,7 @@ macro_rules! gen_signed_functions {
|
||||
|
||||
macro_rules! reg_functions {
|
||||
($mod_name:ident += $root:ident ; $($arg_type:ident),+ ) => { $(
|
||||
$mod_name.combine_flatten(exported_module!($root::$arg_type::functions));
|
||||
combine_with_exported_module!($mod_name, "arithmetic", $root::$arg_type::functions);
|
||||
)* }
|
||||
}
|
||||
|
||||
@ -208,8 +208,8 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, {
|
||||
// Basic arithmetic for floating-point
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
{
|
||||
lib.combine_flatten(exported_module!(f32_functions));
|
||||
lib.combine_flatten(exported_module!(f64_functions));
|
||||
combine_with_exported_module!(lib, "f32", f32_functions);
|
||||
combine_with_exported_module!(lib, "f64", f64_functions);
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -58,7 +58,7 @@ macro_rules! reg_functions {
|
||||
}
|
||||
|
||||
def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, {
|
||||
lib.combine_flatten(exported_module!(array_functions));
|
||||
combine_with_exported_module!(lib, "array", array_functions);
|
||||
|
||||
reg_functions!(lib += basic; INT, bool, char, ImmutableString, FnPtr, Array, Unit);
|
||||
|
||||
|
@ -5,7 +5,7 @@ use crate::plugin::*;
|
||||
use crate::result::EvalAltResult;
|
||||
|
||||
def_package!(crate:EvalPackage:"Disable 'eval'.", lib, {
|
||||
lib.combine_flatten(exported_module!(eval_override));
|
||||
combine_with_exported_module!(lib, "eval", eval_override);
|
||||
});
|
||||
|
||||
#[export_module]
|
||||
|
@ -3,7 +3,7 @@ use crate::fn_native::FnPtr;
|
||||
use crate::plugin::*;
|
||||
|
||||
def_package!(crate:BasicFnPackage:"Basic Fn functions.", lib, {
|
||||
lib.combine_flatten(exported_module!(fn_ptr_functions));
|
||||
combine_with_exported_module!(lib, "FnPtr", fn_ptr_functions);
|
||||
});
|
||||
|
||||
#[export_module]
|
||||
|
@ -45,7 +45,7 @@ macro_rules! gen_cmp_functions {
|
||||
|
||||
macro_rules! reg_functions {
|
||||
($mod_name:ident += $root:ident ; $($arg_type:ident),+) => { $(
|
||||
$mod_name.combine_flatten(exported_module!($root::$arg_type::functions));
|
||||
combine_with_exported_module!($mod_name, "logic", $root::$arg_type::functions);
|
||||
)* }
|
||||
}
|
||||
|
||||
|
@ -9,7 +9,7 @@ use crate::plugin::*;
|
||||
use crate::stdlib::vec::Vec;
|
||||
|
||||
def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, {
|
||||
lib.combine_flatten(exported_module!(map_functions));
|
||||
combine_with_exported_module!(lib, "map", map_functions);
|
||||
});
|
||||
|
||||
#[export_module]
|
||||
|
@ -48,10 +48,10 @@ def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, {
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
{
|
||||
// Floating point functions
|
||||
lib.combine_flatten(exported_module!(float_functions));
|
||||
combine_with_exported_module!(lib, "float", float_functions);
|
||||
|
||||
// Trig functions
|
||||
lib.combine_flatten(exported_module!(trig_functions));
|
||||
combine_with_exported_module!(lib, "trig", trig_functions);
|
||||
|
||||
reg_functions!(lib += basic_to_float::to_float(INT));
|
||||
|
||||
|
@ -57,7 +57,7 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
reg_functions!(lib += float; f32, f64);
|
||||
|
||||
lib.combine_flatten(exported_module!(string_functions));
|
||||
combine_with_exported_module!(lib, "string", string_functions);
|
||||
|
||||
lib.set_raw_fn(
|
||||
"pad",
|
||||
|
@ -21,7 +21,7 @@ use instant::Instant;
|
||||
|
||||
def_package!(crate:BasicTimePackage:"Basic timing utilities.", lib, {
|
||||
// Register date/time functions
|
||||
lib.combine_flatten(exported_module!(time_functions));
|
||||
combine_with_exported_module!(lib, "time", time_functions);
|
||||
});
|
||||
|
||||
#[export_module]
|
||||
|
@ -1,5 +1,6 @@
|
||||
#![cfg(not(any(feature = "no_index", feature = "no_module")))]
|
||||
|
||||
use rhai::module_resolvers::StaticModuleResolver;
|
||||
use rhai::plugin::*;
|
||||
use rhai::{Engine, EvalAltResult, INT};
|
||||
|
||||
@ -10,6 +11,8 @@ mod test {
|
||||
pub mod special_array_package {
|
||||
use rhai::{Array, INT};
|
||||
|
||||
pub const MYSTIC_NUMBER: INT = 42;
|
||||
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
pub mod feature {
|
||||
use rhai::{Array, Dynamic, EvalAltResult};
|
||||
@ -66,7 +69,7 @@ fn test_plugins_package() -> Result<(), Box<EvalAltResult>> {
|
||||
let mut engine = Engine::new();
|
||||
|
||||
let mut m = Module::new();
|
||||
m.combine_flatten(exported_module!(test::special_array_package));
|
||||
combine_with_exported_module!(&mut m, "test", test::special_array_package);
|
||||
engine.load_package(m);
|
||||
|
||||
reg_functions!(engine += greet::single(INT, bool, char));
|
||||
@ -83,5 +86,14 @@ fn test_plugins_package() -> Result<(), Box<EvalAltResult>> {
|
||||
"6 kitties"
|
||||
);
|
||||
|
||||
let mut resolver = StaticModuleResolver::new();
|
||||
resolver.insert("test", exported_module!(test::special_array_package));
|
||||
|
||||
engine.set_module_resolver(Some(resolver));
|
||||
assert_eq!(
|
||||
engine.eval::<INT>(r#"import "test" as test; test::MYSTIC_NUMBER"#)?,
|
||||
42
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user