2020-09-05 21:10:46 +02:00
#[ cfg(test) ]
mod module_tests {
use crate ::module ::Module ;
use proc_macro2 ::TokenStream ;
use quote ::quote ;
#[ test ]
fn empty_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod empty { }
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert! ( item_mod . fns ( ) . is_empty ( ) ) ;
assert! ( item_mod . consts ( ) . is_empty ( ) ) ;
}
#[ test ]
fn one_factory_fn_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_fn {
pub fn get_mystic_number ( ) -> INT {
42
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert! ( item_mod . consts ( ) . is_empty ( ) ) ;
assert_eq! ( item_mod . fns ( ) . len ( ) , 1 ) ;
assert_eq! ( item_mod . fns ( ) [ 0 ] . name ( ) . to_string ( ) , " get_mystic_number " ) ;
assert_eq! ( item_mod . fns ( ) [ 0 ] . arg_count ( ) , 0 ) ;
assert_eq! (
item_mod . fns ( ) [ 0 ] . return_type ( ) . unwrap ( ) ,
& syn ::parse2 ::< syn ::Type > ( quote! { INT } ) . unwrap ( )
) ;
}
2021-12-21 09:14:07 +01:00
#[ test ]
2021-12-21 15:16:03 +01:00
fn one_factory_fn_with_comments_module ( ) {
2021-12-21 09:14:07 +01:00
let input_tokens : TokenStream = quote! {
pub mod one_fn {
/// This is a doc-comment.
/// Another line.
/** block doc-comment */
// Regular comment
/// Final line.
/** doc-comment
in multiple lines
* /
pub fn get_mystic_number ( ) -> INT {
42
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert! ( item_mod . consts ( ) . is_empty ( ) ) ;
assert_eq! ( item_mod . fns ( ) . len ( ) , 1 ) ;
assert_eq! ( item_mod . fns ( ) [ 0 ] . name ( ) . to_string ( ) , " get_mystic_number " ) ;
2021-12-21 15:16:03 +01:00
assert_eq! (
item_mod . fns ( ) [ 0 ]
. comments ( )
. iter ( )
. cloned ( )
. collect ::< Vec < _ > > ( ) ,
vec! [
" /// This is a doc-comment. " ,
" /// Another line. " ,
" /// block doc-comment " ,
" /// Final line. " ,
" /** doc-comment \n in multiple lines \n */ "
]
) ;
2021-12-21 09:14:07 +01:00
assert_eq! ( item_mod . fns ( ) [ 0 ] . arg_count ( ) , 0 ) ;
assert_eq! (
item_mod . fns ( ) [ 0 ] . return_type ( ) . unwrap ( ) ,
& syn ::parse2 ::< syn ::Type > ( quote! { INT } ) . unwrap ( )
) ;
}
2020-09-05 21:10:46 +02:00
#[ test ]
fn one_single_arg_fn_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_fn {
pub fn add_one_to ( x : INT ) -> INT {
x + 1
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert! ( item_mod . consts ( ) . is_empty ( ) ) ;
assert_eq! ( item_mod . fns ( ) . len ( ) , 1 ) ;
assert_eq! ( item_mod . fns ( ) [ 0 ] . name ( ) . to_string ( ) , " add_one_to " ) ;
assert_eq! ( item_mod . fns ( ) [ 0 ] . arg_count ( ) , 1 ) ;
assert_eq! (
item_mod . fns ( ) [ 0 ] . arg_list ( ) . next ( ) . unwrap ( ) ,
& syn ::parse2 ::< syn ::FnArg > ( quote! { x : INT } ) . unwrap ( )
) ;
assert_eq! (
item_mod . fns ( ) [ 0 ] . return_type ( ) . unwrap ( ) ,
& syn ::parse2 ::< syn ::Type > ( quote! { INT } ) . unwrap ( )
) ;
}
#[ test ]
fn one_double_arg_fn_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_fn {
pub fn add_together ( x : INT , y : INT ) -> INT {
x + y
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
let mut args = item_mod . fns ( ) [ 0 ] . arg_list ( ) ;
assert! ( item_mod . consts ( ) . is_empty ( ) ) ;
assert_eq! ( item_mod . fns ( ) . len ( ) , 1 ) ;
assert_eq! ( item_mod . fns ( ) [ 0 ] . name ( ) . to_string ( ) , " add_together " ) ;
assert_eq! ( item_mod . fns ( ) [ 0 ] . arg_count ( ) , 2 ) ;
assert_eq! (
args . next ( ) . unwrap ( ) ,
& syn ::parse2 ::< syn ::FnArg > ( quote! { x : INT } ) . unwrap ( )
) ;
assert_eq! (
args . next ( ) . unwrap ( ) ,
& syn ::parse2 ::< syn ::FnArg > ( quote! { y : INT } ) . unwrap ( )
) ;
assert! ( args . next ( ) . is_none ( ) ) ;
assert_eq! (
item_mod . fns ( ) [ 0 ] . return_type ( ) . unwrap ( ) ,
& syn ::parse2 ::< syn ::Type > ( quote! { INT } ) . unwrap ( )
) ;
}
#[ test ]
fn one_constant_nested_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_constant {
pub mod it_is {
pub const MYSTIC_NUMBER : INT = 42 ;
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert! ( item_mod . fns ( ) . is_empty ( ) ) ;
assert! ( item_mod . consts ( ) . is_empty ( ) ) ;
2021-02-18 10:42:49 +01:00
assert_eq! ( item_mod . sub_modules ( ) . len ( ) , 1 ) ;
assert_eq! ( & item_mod . sub_modules ( ) [ 0 ] . consts ( ) [ 0 ] . 0 , " MYSTIC_NUMBER " ) ;
2020-09-05 21:10:46 +02:00
assert_eq! (
2021-02-18 10:42:49 +01:00
item_mod . sub_modules ( ) [ 0 ] . consts ( ) [ 0 ] . 2 ,
2020-09-05 21:10:46 +02:00
syn ::parse2 ::< syn ::Expr > ( quote! { 42 } ) . unwrap ( )
) ;
}
#[ test ]
fn one_skipped_fn_nested_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_fn {
pub mod skip_this {
#[ rhai_fn(skip) ]
pub fn get_mystic_number ( ) -> INT {
42
}
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert! ( item_mod . fns ( ) . is_empty ( ) ) ;
assert! ( item_mod . consts ( ) . is_empty ( ) ) ;
2021-02-18 10:42:49 +01:00
assert_eq! ( item_mod . sub_modules ( ) . len ( ) , 1 ) ;
assert_eq! ( item_mod . sub_modules ( ) [ 0 ] . fns ( ) . len ( ) , 1 ) ;
assert! ( item_mod . sub_modules ( ) [ 0 ] . fns ( ) [ 0 ] . skipped ( ) ) ;
assert! ( item_mod . sub_modules ( ) [ 0 ] . consts ( ) . is_empty ( ) ) ;
assert! ( item_mod . sub_modules ( ) [ 0 ] . sub_modules ( ) . is_empty ( ) ) ;
2020-09-05 21:10:46 +02:00
}
#[ test ]
fn one_skipped_nested_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_fn {
#[ rhai_mod(skip) ]
pub mod skip_this {
pub fn get_mystic_number ( ) -> INT {
42
}
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert! ( item_mod . fns ( ) . is_empty ( ) ) ;
assert! ( item_mod . consts ( ) . is_empty ( ) ) ;
2021-02-18 10:42:49 +01:00
assert_eq! ( item_mod . sub_modules ( ) . len ( ) , 1 ) ;
assert! ( item_mod . sub_modules ( ) [ 0 ] . skipped ( ) ) ;
2020-09-05 21:10:46 +02:00
}
#[ test ]
fn one_constant_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_constant {
pub const MYSTIC_NUMBER : INT = 42 ;
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert! ( item_mod . fns ( ) . is_empty ( ) ) ;
assert_eq! ( item_mod . consts ( ) . len ( ) , 1 ) ;
assert_eq! ( & item_mod . consts ( ) [ 0 ] . 0 , " MYSTIC_NUMBER " ) ;
assert_eq! (
2020-09-13 17:13:07 +02:00
item_mod . consts ( ) [ 0 ] . 2 ,
2020-09-05 21:10:46 +02:00
syn ::parse2 ::< syn ::Expr > ( quote! { 42 } ) . unwrap ( )
) ;
}
#[ test ]
fn one_skipped_fn_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_fn {
#[ rhai_fn(skip) ]
pub fn get_mystic_number ( ) -> INT {
42
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_eq! ( item_mod . fns ( ) . len ( ) , 1 ) ;
assert! ( item_mod . fns ( ) [ 0 ] . skipped ( ) ) ;
assert! ( item_mod . consts ( ) . is_empty ( ) ) ;
}
#[ test ]
fn one_private_constant_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_constant {
const MYSTIC_NUMBER : INT = 42 ;
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert! ( item_mod . fns ( ) . is_empty ( ) ) ;
assert! ( item_mod . consts ( ) . is_empty ( ) ) ;
}
}
#[ cfg(test) ]
mod generate_tests {
use crate ::module ::Module ;
use proc_macro2 ::TokenStream ;
use quote ::quote ;
fn assert_streams_eq ( actual : TokenStream , expected : TokenStream ) {
let actual = actual . to_string ( ) ;
let expected = expected . to_string ( ) ;
if & actual ! = & expected {
let mut counter = 0 ;
2020-09-17 05:56:10 +02:00
let _iter = actual . chars ( ) . zip ( expected . chars ( ) ) . skip_while ( | ( a , e ) | {
2020-09-14 05:40:23 +02:00
if * a = = * e {
counter + = 1 ;
true
} else {
false
}
} ) ;
2020-09-19 12:18:40 +02:00
let ( _actual_diff , _expected_diff ) = {
2020-09-05 21:10:46 +02:00
let mut actual_diff = String ::new ( ) ;
let mut expected_diff = String ::new ( ) ;
2020-09-17 05:56:10 +02:00
for ( a , e ) in _iter . take ( 50 ) {
2020-09-05 21:10:46 +02:00
actual_diff . push ( a ) ;
expected_diff . push ( e ) ;
}
( actual_diff , expected_diff )
} ;
2020-09-18 06:31:41 +02:00
eprintln! ( " actual != expected, diverge at char {} " , counter ) ;
2020-09-19 12:18:40 +02:00
// eprintln!(" actual: {}", _actual_diff);
// eprintln!("expected: {}", _expected_diff);
// assert!(false);
2020-09-05 21:10:46 +02:00
}
2020-09-17 05:37:23 +02:00
assert_eq! ( actual , expected ) ;
2020-09-05 21:10:46 +02:00
}
#[ test ]
fn empty_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod empty { }
} ;
let expected_tokens = quote! {
pub mod empty {
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-05 21:10:46 +02:00
m
}
2020-09-14 05:40:23 +02:00
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
if flatten { } else { }
}
2020-09-05 21:10:46 +02:00
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
#[ test ]
fn one_factory_fn_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_fn {
pub fn get_mystic_number ( ) -> INT {
42
}
}
} ;
let expected_tokens = quote! {
pub mod one_fn {
pub fn get_mystic_number ( ) -> INT {
42
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-14 05:40:23 +02:00
m
}
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2021-03-26 11:41:28 +01:00
m . set_fn ( " get_mystic_number " , FnNamespace ::Internal , FnAccess ::Public ,
2021-03-25 07:02:50 +01:00
Some ( get_mystic_number_token ::PARAM_NAMES ) , & [ ] ,
2021-03-26 11:41:28 +01:00
get_mystic_number_token ( ) . into ( ) ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
2020-09-05 21:10:46 +02:00
}
#[ allow(non_camel_case_types) ]
2021-03-25 07:02:50 +01:00
pub struct get_mystic_number_token ( ) ;
impl get_mystic_number_token {
pub const PARAM_NAMES : & 'static [ & 'static str ] = & [ " INT " ] ;
#[ inline(always) ] pub fn param_types ( ) -> [ TypeId ; 0 usize ] { [ ] }
}
2020-09-05 21:10:46 +02:00
impl PluginFunction for get_mystic_number_token {
2021-03-22 16:11:23 +01:00
#[ inline(always) ]
2021-03-19 03:30:30 +01:00
fn call ( & self , context : NativeCallContext , args : & mut [ & mut Dynamic ] ) -> RhaiResult {
2020-09-05 21:10:46 +02:00
Ok ( Dynamic ::from ( get_mystic_number ( ) ) )
}
2021-03-22 16:11:23 +01:00
#[ inline(always) ] fn is_method_call ( & self ) -> bool { false }
2020-09-05 21:10:46 +02:00
}
}
} ;
2021-12-21 09:14:07 +01:00
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
#[ test ]
2021-12-21 15:16:03 +01:00
fn one_factory_fn_with_comments_module ( ) {
2021-12-21 09:14:07 +01:00
let input_tokens : TokenStream = quote! {
pub mod one_fn {
/// This is a doc-comment.
/// Another line.
/** block doc-comment */
// Regular comment
/// Final line.
/** doc-comment
in multiple lines
* /
pub fn get_mystic_number ( ) -> INT {
42
}
}
} ;
let expected_tokens = quote! {
pub mod one_fn {
pub fn get_mystic_number ( ) -> INT {
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 . build_index ( ) ;
m
}
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2021-12-21 15:16:03 +01:00
m . set_fn_with_comments ( " get_mystic_number " , FnNamespace ::Internal , FnAccess ::Public ,
Some ( get_mystic_number_token ::PARAM_NAMES ) , & [ ] , & [ " /// This is a doc-comment. " , " /// Another line. " , " /// block doc-comment " , " /// Final line. " , " /** doc-comment \n in multiple lines \n */ " ] ,
2021-12-21 09:14:07 +01:00
get_mystic_number_token ( ) . into ( ) ) ;
if flatten { } else { }
}
#[ allow(non_camel_case_types) ]
pub struct get_mystic_number_token ( ) ;
impl get_mystic_number_token {
pub const PARAM_NAMES : & 'static [ & 'static str ] = & [ " INT " ] ;
#[ inline(always) ] pub fn param_types ( ) -> [ TypeId ; 0 usize ] { [ ] }
}
impl PluginFunction for get_mystic_number_token {
#[ inline(always) ]
fn call ( & self , context : NativeCallContext , args : & mut [ & mut Dynamic ] ) -> RhaiResult {
Ok ( Dynamic ::from ( get_mystic_number ( ) ) )
}
#[ inline(always) ] fn is_method_call ( & self ) -> bool { false }
}
}
} ;
2020-09-05 21:10:46 +02:00
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
2020-11-17 05:09:56 +01:00
#[ test ]
fn one_single_arg_global_fn_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_global_fn {
#[ rhai_fn(global) ]
pub fn add_one_to ( x : INT ) -> INT {
x + 1
}
}
} ;
let expected_tokens = quote! {
pub mod one_global_fn {
pub fn add_one_to ( x : INT ) -> INT {
x + 1
}
#[ allow(unused_imports) ]
use super ::* ;
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-11-17 05:09:56 +01:00
m
}
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2021-03-26 11:41:28 +01:00
m . set_fn ( " add_one_to " , FnNamespace ::Global , FnAccess ::Public ,
2021-03-25 07:02:50 +01:00
Some ( add_one_to_token ::PARAM_NAMES ) , & [ TypeId ::of ::< INT > ( ) ] ,
2021-03-26 11:41:28 +01:00
add_one_to_token ( ) . into ( ) ) ;
2020-11-17 05:09:56 +01:00
if flatten { } else { }
}
#[ allow(non_camel_case_types) ]
2021-03-25 07:02:50 +01:00
pub struct add_one_to_token ( ) ;
impl add_one_to_token {
pub const PARAM_NAMES : & 'static [ & 'static str ] = & [ " x: INT " , " INT " ] ;
#[ inline(always) ] pub fn param_types ( ) -> [ TypeId ; 1 usize ] { [ TypeId ::of ::< INT > ( ) ] }
}
2020-11-17 05:09:56 +01:00
impl PluginFunction for add_one_to_token {
2021-03-22 16:11:23 +01:00
#[ inline(always) ]
2021-03-19 03:30:30 +01:00
fn call ( & self , context : NativeCallContext , args : & mut [ & mut Dynamic ] ) -> RhaiResult {
2020-11-17 05:09:56 +01:00
let arg0 = mem ::take ( args [ 0 usize ] ) . cast ::< INT > ( ) ;
Ok ( Dynamic ::from ( add_one_to ( arg0 ) ) )
}
2021-03-22 16:11:23 +01:00
#[ inline(always) ] fn is_method_call ( & self ) -> bool { false }
2020-11-17 05:09:56 +01:00
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
2020-09-05 21:10:46 +02:00
#[ test ]
fn one_single_arg_fn_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_fn {
pub fn add_one_to ( x : INT ) -> INT {
x + 1
}
}
} ;
let expected_tokens = quote! {
pub mod one_fn {
pub fn add_one_to ( x : INT ) -> INT {
x + 1
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-14 05:40:23 +02:00
m
}
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2021-03-26 11:41:28 +01:00
m . set_fn ( " add_one_to " , FnNamespace ::Internal , FnAccess ::Public , Some ( add_one_to_token ::PARAM_NAMES ) ,
2021-03-25 07:02:50 +01:00
& [ TypeId ::of ::< INT > ( ) ] ,
2021-03-26 11:41:28 +01:00
add_one_to_token ( ) . into ( ) ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
2020-09-05 21:10:46 +02:00
}
#[ allow(non_camel_case_types) ]
2021-03-25 07:02:50 +01:00
pub struct add_one_to_token ( ) ;
impl add_one_to_token {
pub const PARAM_NAMES : & 'static [ & 'static str ] = & [ " x: INT " , " INT " ] ;
#[ inline(always) ] pub fn param_types ( ) -> [ TypeId ; 1 usize ] { [ TypeId ::of ::< INT > ( ) ] }
}
2020-09-05 21:10:46 +02:00
impl PluginFunction for add_one_to_token {
2021-03-22 16:11:23 +01:00
#[ inline(always) ]
2021-03-19 03:30:30 +01:00
fn call ( & self , context : NativeCallContext , args : & mut [ & mut Dynamic ] ) -> RhaiResult {
2020-09-23 04:38:02 +02:00
let arg0 = mem ::take ( args [ 0 usize ] ) . cast ::< INT > ( ) ;
2020-09-05 21:10:46 +02:00
Ok ( Dynamic ::from ( add_one_to ( arg0 ) ) )
}
2021-03-22 16:11:23 +01:00
#[ inline(always) ] fn is_method_call ( & self ) -> bool { false }
2020-09-05 21:10:46 +02:00
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
#[ test ]
fn two_fn_overload_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod two_fns {
#[ rhai_fn(name = " add_n " ) ]
pub fn add_one_to ( x : INT ) -> INT {
x + 1
}
#[ rhai_fn(name = " add_n " ) ]
pub fn add_n_to ( x : INT , y : INT ) -> INT {
x + y
}
}
} ;
let expected_tokens = quote! {
pub mod two_fns {
pub fn add_one_to ( x : INT ) -> INT {
x + 1
}
pub fn add_n_to ( x : INT , y : INT ) -> INT {
x + y
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-14 05:40:23 +02:00
m
}
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2021-03-26 11:41:28 +01:00
m . set_fn ( " add_n " , FnNamespace ::Internal , FnAccess ::Public , Some ( add_one_to_token ::PARAM_NAMES ) ,
2021-03-25 07:02:50 +01:00
& [ TypeId ::of ::< INT > ( ) ] ,
2021-03-26 11:41:28 +01:00
add_one_to_token ( ) . into ( ) ) ;
m . set_fn ( " add_n " , FnNamespace ::Internal , FnAccess ::Public , Some ( add_n_to_token ::PARAM_NAMES ) ,
2021-03-25 07:02:50 +01:00
& [ TypeId ::of ::< INT > ( ) , TypeId ::of ::< INT > ( ) ] ,
2021-03-26 11:41:28 +01:00
add_n_to_token ( ) . into ( ) ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
2020-09-05 21:10:46 +02:00
}
#[ allow(non_camel_case_types) ]
2021-03-25 07:02:50 +01:00
pub struct add_one_to_token ( ) ;
impl add_one_to_token {
pub const PARAM_NAMES : & 'static [ & 'static str ] = & [ " x: INT " , " INT " ] ;
#[ inline(always) ] pub fn param_types ( ) -> [ TypeId ; 1 usize ] { [ TypeId ::of ::< INT > ( ) ] }
}
2020-09-05 21:10:46 +02:00
impl PluginFunction for add_one_to_token {
2021-03-22 16:11:23 +01:00
#[ inline(always) ]
2021-03-19 03:30:30 +01:00
fn call ( & self , context : NativeCallContext , args : & mut [ & mut Dynamic ] ) -> RhaiResult {
2020-09-23 04:38:02 +02:00
let arg0 = mem ::take ( args [ 0 usize ] ) . cast ::< INT > ( ) ;
2020-09-05 21:10:46 +02:00
Ok ( Dynamic ::from ( add_one_to ( arg0 ) ) )
}
2021-03-22 16:11:23 +01:00
#[ inline(always) ] fn is_method_call ( & self ) -> bool { false }
2020-09-05 21:10:46 +02:00
}
#[ allow(non_camel_case_types) ]
2021-03-25 07:02:50 +01:00
pub struct add_n_to_token ( ) ;
impl add_n_to_token {
pub const PARAM_NAMES : & 'static [ & 'static str ] = & [ " x: INT " , " y: INT " , " INT " ] ;
#[ inline(always) ] pub fn param_types ( ) -> [ TypeId ; 2 usize ] { [ TypeId ::of ::< INT > ( ) , TypeId ::of ::< INT > ( ) ] }
}
2020-09-05 21:10:46 +02:00
impl PluginFunction for add_n_to_token {
2021-03-22 16:11:23 +01:00
#[ inline(always) ]
2021-03-19 03:30:30 +01:00
fn call ( & self , context : NativeCallContext , args : & mut [ & mut Dynamic ] ) -> RhaiResult {
2020-09-23 04:38:02 +02:00
let arg0 = mem ::take ( args [ 0 usize ] ) . cast ::< INT > ( ) ;
let arg1 = mem ::take ( args [ 1 usize ] ) . cast ::< INT > ( ) ;
2020-09-05 21:10:46 +02:00
Ok ( Dynamic ::from ( add_n_to ( arg0 , arg1 ) ) )
}
2021-03-22 16:11:23 +01:00
#[ inline(always) ] fn is_method_call ( & self ) -> bool { false }
2020-09-05 21:10:46 +02:00
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
#[ test ]
fn one_double_arg_fn_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_fn {
pub fn add_together ( x : INT , y : INT ) -> INT {
x + y
}
}
} ;
let expected_tokens = quote! {
pub mod one_fn {
pub fn add_together ( x : INT , y : INT ) -> INT {
x + y
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-14 05:40:23 +02:00
m
}
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2021-03-26 11:41:28 +01:00
m . set_fn ( " add_together " , FnNamespace ::Internal , FnAccess ::Public , Some ( add_together_token ::PARAM_NAMES ) ,
2021-03-25 07:02:50 +01:00
& [ TypeId ::of ::< INT > ( ) , TypeId ::of ::< INT > ( ) ] ,
2021-03-26 11:41:28 +01:00
add_together_token ( ) . into ( ) ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
2020-09-13 00:10:43 +02:00
}
#[ allow(non_camel_case_types) ]
2021-03-25 07:02:50 +01:00
pub struct add_together_token ( ) ;
impl add_together_token {
pub const PARAM_NAMES : & 'static [ & 'static str ] = & [ " x: INT " , " y: INT " , " INT " ] ;
#[ inline(always) ] pub fn param_types ( ) -> [ TypeId ; 2 usize ] { [ TypeId ::of ::< INT > ( ) , TypeId ::of ::< INT > ( ) ] }
}
2020-09-13 00:10:43 +02:00
impl PluginFunction for add_together_token {
2021-03-22 16:11:23 +01:00
#[ inline(always) ]
2021-03-19 03:30:30 +01:00
fn call ( & self , context : NativeCallContext , args : & mut [ & mut Dynamic ] ) -> RhaiResult {
2020-09-23 04:38:02 +02:00
let arg0 = mem ::take ( args [ 0 usize ] ) . cast ::< INT > ( ) ;
let arg1 = mem ::take ( args [ 1 usize ] ) . cast ::< INT > ( ) ;
2020-09-13 00:10:43 +02:00
Ok ( Dynamic ::from ( add_together ( arg0 , arg1 ) ) )
}
2021-03-22 16:11:23 +01:00
#[ inline(always) ] fn is_method_call ( & self ) -> bool { false }
2020-09-13 00:10:43 +02:00
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
#[ test ]
fn one_double_rename_fn_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_fn {
#[ rhai_fn(name = " add " , name = " + " , name = " add_together " ) ]
pub fn add_together ( x : INT , y : INT ) -> INT {
x + y
}
}
} ;
let expected_tokens = quote! {
pub mod one_fn {
pub fn add_together ( x : INT , y : INT ) -> INT {
x + y
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-13 00:10:43 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-14 05:40:23 +02:00
m
}
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2021-03-26 11:41:28 +01:00
m . set_fn ( " add " , FnNamespace ::Internal , FnAccess ::Public , Some ( add_together_token ::PARAM_NAMES ) ,
2021-03-25 07:02:50 +01:00
& [ TypeId ::of ::< INT > ( ) , TypeId ::of ::< INT > ( ) ] ,
2021-03-26 11:41:28 +01:00
add_together_token ( ) . into ( ) ) ;
m . set_fn ( " + " , FnNamespace ::Internal , FnAccess ::Public , Some ( add_together_token ::PARAM_NAMES ) ,
2021-03-25 07:02:50 +01:00
& [ TypeId ::of ::< INT > ( ) , TypeId ::of ::< INT > ( ) ] ,
2021-03-26 11:41:28 +01:00
add_together_token ( ) . into ( ) ) ;
m . set_fn ( " add_together " , FnNamespace ::Internal , FnAccess ::Public , Some ( add_together_token ::PARAM_NAMES ) ,
2021-03-25 07:02:50 +01:00
& [ TypeId ::of ::< INT > ( ) , TypeId ::of ::< INT > ( ) ] ,
2021-03-26 11:41:28 +01:00
add_together_token ( ) . into ( ) ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
2020-09-05 21:10:46 +02:00
}
#[ allow(non_camel_case_types) ]
2021-03-25 07:02:50 +01:00
pub struct add_together_token ( ) ;
impl add_together_token {
pub const PARAM_NAMES : & 'static [ & 'static str ] = & [ " x: INT " , " y: INT " , " INT " ] ;
#[ inline(always) ] pub fn param_types ( ) -> [ TypeId ; 2 usize ] { [ TypeId ::of ::< INT > ( ) , TypeId ::of ::< INT > ( ) ] }
}
2020-09-05 21:10:46 +02:00
impl PluginFunction for add_together_token {
2021-03-22 16:11:23 +01:00
#[ inline(always) ]
2021-03-19 03:30:30 +01:00
fn call ( & self , context : NativeCallContext , args : & mut [ & mut Dynamic ] ) -> RhaiResult {
2020-09-23 04:38:02 +02:00
let arg0 = mem ::take ( args [ 0 usize ] ) . cast ::< INT > ( ) ;
let arg1 = mem ::take ( args [ 1 usize ] ) . cast ::< INT > ( ) ;
2020-09-05 21:10:46 +02:00
Ok ( Dynamic ::from ( add_together ( arg0 , arg1 ) ) )
}
2021-03-22 16:11:23 +01:00
#[ inline(always) ] fn is_method_call ( & self ) -> bool { false }
2020-09-05 21:10:46 +02:00
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
2020-09-14 05:40:23 +02:00
#[ 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 ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-14 05:40:23 +02:00
m
}
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2020-09-17 05:56:10 +02:00
m . set_var ( " MYSTIC_NUMBER " , MYSTIC_NUMBER ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
2020-09-05 21:10:46 +02:00
#[ test ]
fn one_constant_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_constant {
pub const MYSTIC_NUMBER : INT = 42 ;
}
} ;
let expected_tokens = quote! {
pub mod one_constant {
pub const MYSTIC_NUMBER : INT = 42 ;
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-05 21:10:46 +02:00
m
}
2020-09-14 05:40:23 +02:00
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2020-09-17 05:56:10 +02:00
m . set_var ( " MYSTIC_NUMBER " , MYSTIC_NUMBER ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
}
2020-09-05 21:10:46 +02:00
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
#[ test ]
fn one_constant_module_imports_preserved ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_constant {
pub use rhai ::INT ;
pub const MYSTIC_NUMBER : INT = 42 ;
}
} ;
let expected_tokens = quote! {
pub mod one_constant {
pub use rhai ::INT ;
pub const MYSTIC_NUMBER : INT = 42 ;
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-05 21:10:46 +02:00
m
}
2020-09-14 05:40:23 +02:00
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2020-09-17 05:56:10 +02:00
m . set_var ( " MYSTIC_NUMBER " , MYSTIC_NUMBER ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
}
2020-09-05 21:10:46 +02:00
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
#[ test ]
fn one_private_fn_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_fn {
fn get_mystic_number ( ) -> INT {
42
}
}
} ;
let expected_tokens = quote! {
pub mod one_fn {
fn get_mystic_number ( ) -> INT {
42
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-05 21:10:46 +02:00
m
}
2020-09-14 05:40:23 +02:00
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
if flatten { } else { }
}
2020-09-05 21:10:46 +02:00
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
#[ test ]
fn one_skipped_fn_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_fn {
#[ rhai_fn(skip) ]
pub fn get_mystic_number ( ) -> INT {
42
}
}
} ;
let expected_tokens = quote! {
pub mod one_fn {
pub fn get_mystic_number ( ) -> INT {
42
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-05 21:10:46 +02:00
m
}
2020-09-14 05:40:23 +02:00
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
if flatten { } else { }
}
2020-09-05 21:10:46 +02:00
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
#[ test ]
2021-02-18 10:42:49 +01:00
fn one_skipped_sub_module ( ) {
2020-09-05 21:10:46 +02:00
let input_tokens : TokenStream = quote! {
pub mod one_fn {
pub fn get_mystic_number ( ) -> INT {
42
}
#[ rhai_mod(skip) ]
pub mod inner_secrets {
pub const SECRET_NUMBER : INT = 86 ;
}
}
} ;
let expected_tokens = quote! {
pub mod one_fn {
pub fn get_mystic_number ( ) -> INT {
42
}
pub mod inner_secrets {
pub const SECRET_NUMBER : INT = 86 ;
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-14 05:40:23 +02:00
m
}
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2021-03-26 11:41:28 +01:00
m . set_fn ( " get_mystic_number " , FnNamespace ::Internal , FnAccess ::Public ,
2021-03-25 07:02:50 +01:00
Some ( get_mystic_number_token ::PARAM_NAMES ) , & [ ] ,
2021-03-26 11:41:28 +01:00
get_mystic_number_token ( ) . into ( ) ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
2020-09-05 21:10:46 +02:00
}
#[ allow(non_camel_case_types) ]
2021-03-25 07:02:50 +01:00
pub struct get_mystic_number_token ( ) ;
impl get_mystic_number_token {
pub const PARAM_NAMES : & 'static [ & 'static str ] = & [ " INT " ] ;
#[ inline(always) ] pub fn param_types ( ) -> [ TypeId ; 0 usize ] { [ ] }
}
2020-09-05 21:10:46 +02:00
impl PluginFunction for get_mystic_number_token {
2021-03-22 16:11:23 +01:00
#[ inline(always) ]
2021-03-19 03:30:30 +01:00
fn call ( & self , context : NativeCallContext , args : & mut [ & mut Dynamic ] ) -> RhaiResult {
2020-09-05 21:10:46 +02:00
Ok ( Dynamic ::from ( get_mystic_number ( ) ) )
}
2021-03-22 16:11:23 +01:00
#[ inline(always) ] fn is_method_call ( & self ) -> bool { false }
2020-09-05 21:10:46 +02:00
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
#[ test ]
fn one_private_constant_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_constant {
const MYSTIC_NUMBER : INT = 42 ;
}
} ;
let expected_tokens = quote! {
pub mod one_constant {
const MYSTIC_NUMBER : INT = 42 ;
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-05 21:10:46 +02:00
m
}
2020-09-14 05:40:23 +02:00
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
if flatten { } else { }
}
2020-09-05 21:10:46 +02:00
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
#[ test ]
fn one_str_arg_fn_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod str_fn {
pub fn print_out_to ( x : & str ) {
x + 1
}
}
} ;
let expected_tokens = quote! {
pub mod str_fn {
pub fn print_out_to ( x : & str ) {
x + 1
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-14 05:40:23 +02:00
m
}
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2021-03-26 11:41:28 +01:00
m . set_fn ( " print_out_to " , FnNamespace ::Internal , FnAccess ::Public , Some ( print_out_to_token ::PARAM_NAMES ) ,
2021-03-25 07:02:50 +01:00
& [ TypeId ::of ::< ImmutableString > ( ) ] ,
2021-03-26 11:41:28 +01:00
print_out_to_token ( ) . into ( ) ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
2020-09-05 21:10:46 +02:00
}
#[ allow(non_camel_case_types) ]
2021-03-25 07:02:50 +01:00
pub struct print_out_to_token ( ) ;
impl print_out_to_token {
pub const PARAM_NAMES : & 'static [ & 'static str ] = & [ " x: &str " , " () " ] ;
#[ inline(always) ] pub fn param_types ( ) -> [ TypeId ; 1 usize ] { [ TypeId ::of ::< ImmutableString > ( ) ] }
}
2020-09-05 21:10:46 +02:00
impl PluginFunction for print_out_to_token {
2021-03-22 16:11:23 +01:00
#[ inline(always) ]
2021-03-19 03:30:30 +01:00
fn call ( & self , context : NativeCallContext , args : & mut [ & mut Dynamic ] ) -> RhaiResult {
2021-07-24 10:24:59 +02:00
let arg0 = mem ::take ( args [ 0 usize ] ) . into_immutable_string ( ) . unwrap ( ) ;
2020-09-05 21:10:46 +02:00
Ok ( Dynamic ::from ( print_out_to ( & arg0 ) ) )
}
2021-03-22 16:11:23 +01:00
#[ inline(always) ] fn is_method_call ( & self ) -> bool { false }
2020-09-19 12:18:40 +02:00
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
#[ test ]
fn one_string_arg_fn_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod str_fn {
pub fn print_out_to ( x : String ) {
x + 1
}
}
} ;
let expected_tokens = quote! {
pub mod str_fn {
pub fn print_out_to ( x : String ) {
x + 1
}
#[ allow(unused_imports) ]
use super ::* ;
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-19 12:18:40 +02:00
m
}
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2021-03-26 11:41:28 +01:00
m . set_fn ( " print_out_to " , FnNamespace ::Internal , FnAccess ::Public , Some ( print_out_to_token ::PARAM_NAMES ) ,
2021-03-25 07:02:50 +01:00
& [ TypeId ::of ::< ImmutableString > ( ) ] ,
2021-03-26 11:41:28 +01:00
print_out_to_token ( ) . into ( ) ) ;
2020-09-19 12:18:40 +02:00
if flatten { } else { }
}
#[ allow(non_camel_case_types) ]
2021-03-25 07:02:50 +01:00
pub struct print_out_to_token ( ) ;
impl print_out_to_token {
pub const PARAM_NAMES : & 'static [ & 'static str ] = & [ " x: String " , " () " ] ;
#[ inline(always) ] pub fn param_types ( ) -> [ TypeId ; 1 usize ] { [ TypeId ::of ::< ImmutableString > ( ) ] }
}
2020-09-19 12:18:40 +02:00
impl PluginFunction for print_out_to_token {
2021-03-22 16:11:23 +01:00
#[ inline(always) ]
2021-03-19 03:30:30 +01:00
fn call ( & self , context : NativeCallContext , args : & mut [ & mut Dynamic ] ) -> RhaiResult {
2021-07-24 10:24:59 +02:00
let arg0 = mem ::take ( args [ 0 usize ] ) . into_string ( ) . unwrap ( ) ;
2020-09-19 12:18:40 +02:00
Ok ( Dynamic ::from ( print_out_to ( arg0 ) ) )
}
2021-03-22 16:11:23 +01:00
#[ inline(always) ] fn is_method_call ( & self ) -> bool { false }
2020-09-05 21:10:46 +02:00
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
2021-02-19 16:13:41 +01:00
#[ test ]
fn mut_ref_pure_fn_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod ref_fn {
#[ rhai_fn(pure) ]
pub fn foo ( x : & mut FLOAT , y : INT ) -> FLOAT {
* x + y as FLOAT
}
}
} ;
let expected_tokens = quote! {
pub mod ref_fn {
pub fn foo ( x : & mut FLOAT , y : INT ) -> FLOAT {
* x + y as FLOAT
}
#[ allow(unused_imports) ]
use super ::* ;
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
rhai_generate_into_module ( & mut m , false ) ;
m . build_index ( ) ;
m
}
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2021-03-26 11:41:28 +01:00
m . set_fn ( " foo " , FnNamespace ::Internal , FnAccess ::Public , Some ( foo_token ::PARAM_NAMES ) ,
2021-03-25 07:02:50 +01:00
& [ TypeId ::of ::< FLOAT > ( ) , TypeId ::of ::< INT > ( ) ] ,
2021-03-26 11:41:28 +01:00
foo_token ( ) . into ( ) ) ;
2021-02-19 16:13:41 +01:00
if flatten { } else { }
}
#[ allow(non_camel_case_types) ]
2021-03-25 07:02:50 +01:00
pub struct foo_token ( ) ;
impl foo_token {
pub const PARAM_NAMES : & 'static [ & 'static str ] = & [ " x: &mut FLOAT " , " y: INT " , " FLOAT " ] ;
#[ inline(always) ] pub fn param_types ( ) -> [ TypeId ; 2 usize ] { [ TypeId ::of ::< FLOAT > ( ) , TypeId ::of ::< INT > ( ) ] }
}
2021-02-19 16:13:41 +01:00
impl PluginFunction for foo_token {
2021-03-22 16:11:23 +01:00
#[ inline(always) ]
2021-03-19 03:30:30 +01:00
fn call ( & self , context : NativeCallContext , args : & mut [ & mut Dynamic ] ) -> RhaiResult {
2021-02-19 16:13:41 +01:00
let arg1 = mem ::take ( args [ 1 usize ] ) . cast ::< INT > ( ) ;
let arg0 = & mut args [ 0 usize ] . write_lock ::< FLOAT > ( ) . unwrap ( ) ;
Ok ( Dynamic ::from ( foo ( arg0 , arg1 ) ) )
}
2021-03-22 16:11:23 +01:00
#[ inline(always) ] fn is_method_call ( & self ) -> bool { true }
2021-02-19 16:13:41 +01:00
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
2020-09-05 21:10:46 +02:00
#[ test ]
fn one_mut_ref_fn_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod ref_fn {
pub fn increment ( x : & mut FLOAT ) {
* x + = 1.0 as FLOAT ;
}
}
} ;
let expected_tokens = quote! {
pub mod ref_fn {
pub fn increment ( x : & mut FLOAT ) {
* x + = 1.0 as FLOAT ;
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-14 05:40:23 +02:00
m
}
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2021-03-26 11:41:28 +01:00
m . set_fn ( " increment " , FnNamespace ::Internal , FnAccess ::Public , Some ( increment_token ::PARAM_NAMES ) ,
2021-03-25 07:02:50 +01:00
& [ TypeId ::of ::< FLOAT > ( ) ] ,
2021-03-26 11:41:28 +01:00
increment_token ( ) . into ( ) ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
2020-09-05 21:10:46 +02:00
}
#[ allow(non_camel_case_types) ]
2021-03-25 07:02:50 +01:00
pub struct increment_token ( ) ;
impl increment_token {
pub const PARAM_NAMES : & 'static [ & 'static str ] = & [ " x: &mut FLOAT " , " () " ] ;
#[ inline(always) ] pub fn param_types ( ) -> [ TypeId ; 1 usize ] { [ TypeId ::of ::< FLOAT > ( ) ] }
}
2020-09-05 21:10:46 +02:00
impl PluginFunction for increment_token {
2021-03-22 16:11:23 +01:00
#[ inline(always) ]
2021-03-19 03:30:30 +01:00
fn call ( & self , context : NativeCallContext , args : & mut [ & mut Dynamic ] ) -> RhaiResult {
2021-02-19 16:13:41 +01:00
if args [ 0 usize ] . is_read_only ( ) {
2021-10-19 17:52:58 +02:00
return Err ( EvalAltResult ::ErrorAssignmentToConstant ( " x " . to_string ( ) , Position ::NONE ) . into ( ) ) ;
2021-02-19 16:13:41 +01:00
}
2020-09-30 16:55:40 +02:00
let arg0 = & mut args [ 0 usize ] . write_lock ::< FLOAT > ( ) . unwrap ( ) ;
2020-09-05 21:10:46 +02:00
Ok ( Dynamic ::from ( increment ( arg0 ) ) )
}
2021-03-22 16:11:23 +01:00
#[ inline(always) ] fn is_method_call ( & self ) -> bool { true }
2020-09-05 21:10:46 +02:00
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
#[ test ]
fn one_fn_nested_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_fn {
pub mod it_is {
pub fn increment ( x : & mut FLOAT ) {
* x + = 1.0 as FLOAT ;
}
}
}
} ;
let expected_tokens = quote! {
pub mod one_fn {
pub mod it_is {
pub fn increment ( x : & mut FLOAT ) {
* x + = 1.0 as FLOAT ;
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-14 05:40:23 +02:00
m
}
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2021-03-26 11:41:28 +01:00
m . set_fn ( " increment " , FnNamespace ::Internal , FnAccess ::Public , Some ( increment_token ::PARAM_NAMES ) ,
2021-03-25 07:02:50 +01:00
& [ TypeId ::of ::< FLOAT > ( ) ] ,
2021-03-26 11:41:28 +01:00
increment_token ( ) . into ( ) ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
2020-09-05 21:10:46 +02:00
}
#[ allow(non_camel_case_types) ]
2021-03-25 07:02:50 +01:00
pub struct increment_token ( ) ;
impl increment_token {
pub const PARAM_NAMES : & 'static [ & 'static str ] = & [ " x: &mut FLOAT " , " () " ] ;
#[ inline(always) ] pub fn param_types ( ) -> [ TypeId ; 1 usize ] { [ TypeId ::of ::< FLOAT > ( ) ] }
}
2020-09-05 21:10:46 +02:00
impl PluginFunction for increment_token {
2021-03-22 16:11:23 +01:00
#[ inline(always) ]
2021-03-19 03:30:30 +01:00
fn call ( & self , context : NativeCallContext , args : & mut [ & mut Dynamic ] ) -> RhaiResult {
2021-02-19 16:13:41 +01:00
if args [ 0 usize ] . is_read_only ( ) {
2021-10-19 17:52:58 +02:00
return Err ( EvalAltResult ::ErrorAssignmentToConstant ( " x " . to_string ( ) , Position ::NONE ) . into ( ) ) ;
2021-02-19 16:13:41 +01:00
}
2020-09-30 16:55:40 +02:00
let arg0 = & mut args [ 0 usize ] . write_lock ::< FLOAT > ( ) . unwrap ( ) ;
2020-09-05 21:10:46 +02:00
Ok ( Dynamic ::from ( increment ( arg0 ) ) )
}
2021-03-22 16:11:23 +01:00
#[ inline(always) ] fn is_method_call ( & self ) -> bool { true }
2020-09-05 21:10:46 +02:00
}
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-05 21:10:46 +02:00
m
}
2020-09-14 05:40:23 +02:00
#[ 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 ( ) ) ; }
}
}
2020-09-05 21:10:46 +02:00
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
#[ test ]
fn one_fn_with_cfg_module ( ) {
let input_tokens : TokenStream = 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 ;
}
}
}
} ;
let expected_tokens = quote! {
pub mod one_fn {
#[ cfg(not(feature = " no_float " )) ]
2020-09-17 05:56:10 +02:00
2020-09-05 21:10:46 +02:00
pub mod it_is {
pub fn increment ( x : & mut FLOAT ) {
* x + = 1.0 as FLOAT ;
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-14 05:40:23 +02:00
m
}
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2021-03-26 11:41:28 +01:00
m . set_fn ( " increment " , FnNamespace ::Internal , FnAccess ::Public , Some ( increment_token ::PARAM_NAMES ) ,
2021-03-25 07:02:50 +01:00
& [ TypeId ::of ::< FLOAT > ( ) ] ,
2021-03-26 11:41:28 +01:00
increment_token ( ) . into ( ) ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
2020-09-05 21:10:46 +02:00
}
#[ allow(non_camel_case_types) ]
2021-03-25 07:02:50 +01:00
pub struct increment_token ( ) ;
impl increment_token {
pub const PARAM_NAMES : & 'static [ & 'static str ] = & [ " x: &mut FLOAT " , " () " ] ;
#[ inline(always) ] pub fn param_types ( ) -> [ TypeId ; 1 usize ] { [ TypeId ::of ::< FLOAT > ( ) ] }
}
2020-09-05 21:10:46 +02:00
impl PluginFunction for increment_token {
2021-03-22 16:11:23 +01:00
#[ inline(always) ]
2021-03-19 03:30:30 +01:00
fn call ( & self , context : NativeCallContext , args : & mut [ & mut Dynamic ] ) -> RhaiResult {
2021-02-19 16:13:41 +01:00
if args [ 0 usize ] . is_read_only ( ) {
2021-10-19 17:52:58 +02:00
return Err ( EvalAltResult ::ErrorAssignmentToConstant ( " x " . to_string ( ) , Position ::NONE ) . into ( ) ) ;
2021-02-19 16:13:41 +01:00
}
2020-09-30 16:55:40 +02:00
let arg0 = & mut args [ 0 usize ] . write_lock ::< FLOAT > ( ) . unwrap ( ) ;
2020-09-05 21:10:46 +02:00
Ok ( Dynamic ::from ( increment ( arg0 ) ) )
}
2021-03-22 16:11:23 +01:00
#[ inline(always) ] fn is_method_call ( & self ) -> bool { true }
2020-09-05 21:10:46 +02:00
}
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-05 21:10:46 +02:00
m
}
2020-09-14 05:40:23 +02:00
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
if flatten {
2021-03-29 06:46:46 +02:00
{
#[ cfg(not(feature = " no_float " )) ]
2020-09-14 05:40:23 +02:00
self ::it_is ::rhai_generate_into_module ( m , flatten ) ;
}
} else {
2021-03-29 06:46:46 +02:00
{
#[ cfg(not(feature = " no_float " )) ]
2020-09-14 05:40:23 +02:00
m . set_sub_module ( " it_is " , self ::it_is ::rhai_module_generate ( ) ) ;
}
}
}
2020-09-05 21:10:46 +02:00
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
2020-09-12 21:56:02 +02:00
#[ test ]
fn one_getter_fn_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_fn {
#[ rhai_fn(get = " square " ) ]
pub fn int_foo ( x : & mut u64 ) -> u64 {
( * x ) * ( * x )
}
}
} ;
let expected_tokens = quote! {
pub mod one_fn {
pub fn int_foo ( x : & mut u64 ) -> u64 {
( * x ) * ( * x )
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-12 21:56:02 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-14 05:40:23 +02:00
m
}
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2021-03-26 11:41:28 +01:00
m . set_fn ( " get$square " , FnNamespace ::Global , FnAccess ::Public , Some ( int_foo_token ::PARAM_NAMES ) ,
2021-03-25 07:02:50 +01:00
& [ TypeId ::of ::< u64 > ( ) ] ,
2021-03-26 11:41:28 +01:00
int_foo_token ( ) . into ( ) ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
2020-09-12 21:56:02 +02:00
}
#[ allow(non_camel_case_types) ]
2021-03-25 07:02:50 +01:00
pub struct int_foo_token ( ) ;
impl int_foo_token {
pub const PARAM_NAMES : & 'static [ & 'static str ] = & [ " x: &mut u64 " , " u64 " ] ;
#[ inline(always) ] pub fn param_types ( ) -> [ TypeId ; 1 usize ] { [ TypeId ::of ::< u64 > ( ) ] }
}
2020-09-12 21:56:02 +02:00
impl PluginFunction for int_foo_token {
2021-03-22 16:11:23 +01:00
#[ inline(always) ]
2021-03-19 03:30:30 +01:00
fn call ( & self , context : NativeCallContext , args : & mut [ & mut Dynamic ] ) -> RhaiResult {
2021-02-19 16:13:41 +01:00
if args [ 0 usize ] . is_read_only ( ) {
2021-10-19 17:52:58 +02:00
return Err ( EvalAltResult ::ErrorAssignmentToConstant ( " x " . to_string ( ) , Position ::NONE ) . into ( ) ) ;
2021-02-19 16:13:41 +01:00
}
2020-09-30 16:55:40 +02:00
let arg0 = & mut args [ 0 usize ] . write_lock ::< u64 > ( ) . unwrap ( ) ;
2020-09-12 21:56:02 +02:00
Ok ( Dynamic ::from ( int_foo ( arg0 ) ) )
}
2021-03-22 16:11:23 +01:00
#[ inline(always) ] fn is_method_call ( & self ) -> bool { true }
2020-09-12 21:56:02 +02:00
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
2020-09-13 00:20:51 +02:00
#[ test ]
fn one_getter_and_rename_fn_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_fn {
#[ rhai_fn(name = " square " , get = " square " ) ]
pub fn int_foo ( x : & mut u64 ) -> u64 {
( * x ) * ( * x )
}
}
} ;
let expected_tokens = quote! {
pub mod one_fn {
pub fn int_foo ( x : & mut u64 ) -> u64 {
( * x ) * ( * x )
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-13 00:20:51 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-14 05:40:23 +02:00
m
}
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2021-03-26 11:41:28 +01:00
m . set_fn ( " square " , FnNamespace ::Internal , FnAccess ::Public , Some ( int_foo_token ::PARAM_NAMES ) ,
2021-03-25 07:02:50 +01:00
& [ TypeId ::of ::< u64 > ( ) ] ,
2021-03-26 11:41:28 +01:00
int_foo_token ( ) . into ( ) ) ;
m . set_fn ( " get$square " , FnNamespace ::Global , FnAccess ::Public , Some ( int_foo_token ::PARAM_NAMES ) ,
2021-03-25 07:02:50 +01:00
& [ TypeId ::of ::< u64 > ( ) ] ,
2021-03-26 11:41:28 +01:00
int_foo_token ( ) . into ( ) ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
2020-09-13 00:20:51 +02:00
}
#[ allow(non_camel_case_types) ]
2021-03-25 07:02:50 +01:00
pub struct int_foo_token ( ) ;
impl int_foo_token {
pub const PARAM_NAMES : & 'static [ & 'static str ] = & [ " x: &mut u64 " , " u64 " ] ;
#[ inline(always) ] pub fn param_types ( ) -> [ TypeId ; 1 usize ] { [ TypeId ::of ::< u64 > ( ) ] }
}
2020-09-13 00:20:51 +02:00
impl PluginFunction for int_foo_token {
2021-03-22 16:11:23 +01:00
#[ inline(always) ]
2021-03-19 03:30:30 +01:00
fn call ( & self , context : NativeCallContext , args : & mut [ & mut Dynamic ] ) -> RhaiResult {
2021-02-19 16:13:41 +01:00
if args [ 0 usize ] . is_read_only ( ) {
2021-10-19 17:52:58 +02:00
return Err ( EvalAltResult ::ErrorAssignmentToConstant ( " x " . to_string ( ) , Position ::NONE ) . into ( ) ) ;
2021-02-19 16:13:41 +01:00
}
2020-09-30 16:55:40 +02:00
let arg0 = & mut args [ 0 usize ] . write_lock ::< u64 > ( ) . unwrap ( ) ;
2020-09-13 00:20:51 +02:00
Ok ( Dynamic ::from ( int_foo ( arg0 ) ) )
}
2021-03-22 16:11:23 +01:00
#[ inline(always) ] fn is_method_call ( & self ) -> bool { true }
2020-09-13 00:20:51 +02:00
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
2020-09-12 21:56:02 +02:00
#[ test ]
fn one_setter_fn_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_fn {
#[ rhai_fn(set = " squared " ) ]
2020-09-13 05:28:41 +02:00
pub fn int_foo ( x : & mut u64 , y : u64 ) {
* x = y * y
2020-09-12 21:56:02 +02:00
}
}
} ;
let expected_tokens = quote! {
pub mod one_fn {
2020-09-13 05:28:41 +02:00
pub fn int_foo ( x : & mut u64 , y : u64 ) {
* x = y * y
2020-09-12 21:56:02 +02:00
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-12 21:56:02 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-14 05:40:23 +02:00
m
}
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2021-03-26 11:41:28 +01:00
m . set_fn ( " set$squared " , FnNamespace ::Global , FnAccess ::Public , Some ( int_foo_token ::PARAM_NAMES ) ,
2021-03-25 07:02:50 +01:00
& [ TypeId ::of ::< u64 > ( ) , TypeId ::of ::< u64 > ( ) ] ,
2021-03-26 11:41:28 +01:00
int_foo_token ( ) . into ( ) ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
2020-09-12 21:56:02 +02:00
}
#[ allow(non_camel_case_types) ]
2021-03-25 07:02:50 +01:00
pub struct int_foo_token ( ) ;
impl int_foo_token {
pub const PARAM_NAMES : & 'static [ & 'static str ] = & [ " x: &mut u64 " , " y: u64 " , " () " ] ;
#[ inline(always) ] pub fn param_types ( ) -> [ TypeId ; 2 usize ] { [ TypeId ::of ::< u64 > ( ) , TypeId ::of ::< u64 > ( ) ] }
}
2020-09-12 21:56:02 +02:00
impl PluginFunction for int_foo_token {
2021-03-22 16:11:23 +01:00
#[ inline(always) ]
2021-03-19 03:30:30 +01:00
fn call ( & self , context : NativeCallContext , args : & mut [ & mut Dynamic ] ) -> RhaiResult {
2021-02-19 16:13:41 +01:00
if args [ 0 usize ] . is_read_only ( ) {
2021-10-19 17:52:58 +02:00
return Err ( EvalAltResult ::ErrorAssignmentToConstant ( " x " . to_string ( ) , Position ::NONE ) . into ( ) ) ;
2021-02-19 16:13:41 +01:00
}
2020-09-23 04:38:02 +02:00
let arg1 = mem ::take ( args [ 1 usize ] ) . cast ::< u64 > ( ) ;
2020-09-30 16:55:40 +02:00
let arg0 = & mut args [ 0 usize ] . write_lock ::< u64 > ( ) . unwrap ( ) ;
2020-09-13 05:28:41 +02:00
Ok ( Dynamic ::from ( int_foo ( arg0 , arg1 ) ) )
2020-09-12 21:56:02 +02:00
}
2021-03-22 16:11:23 +01:00
#[ inline(always) ] fn is_method_call ( & self ) -> bool { true }
2020-09-12 21:56:02 +02:00
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
2020-09-13 00:20:51 +02:00
#[ test ]
fn one_setter_and_rename_fn_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_fn {
#[ rhai_fn(name = " set_sq " , set = " squared " ) ]
2020-09-13 05:28:41 +02:00
pub fn int_foo ( x : & mut u64 , y : u64 ) {
* x = y * y
2020-09-13 00:20:51 +02:00
}
}
} ;
let expected_tokens = quote! {
pub mod one_fn {
2020-09-13 05:28:41 +02:00
pub fn int_foo ( x : & mut u64 , y : u64 ) {
* x = y * y
2020-09-13 00:20:51 +02:00
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-13 00:20:51 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-14 05:40:23 +02:00
m
}
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2021-03-26 11:41:28 +01:00
m . set_fn ( " set_sq " , FnNamespace ::Internal , FnAccess ::Public , Some ( int_foo_token ::PARAM_NAMES ) ,
2021-03-25 07:02:50 +01:00
& [ TypeId ::of ::< u64 > ( ) , TypeId ::of ::< u64 > ( ) ] ,
2021-03-26 11:41:28 +01:00
int_foo_token ( ) . into ( ) ) ;
m . set_fn ( " set$squared " , FnNamespace ::Global , FnAccess ::Public , Some ( int_foo_token ::PARAM_NAMES ) ,
2021-03-25 07:02:50 +01:00
& [ TypeId ::of ::< u64 > ( ) , TypeId ::of ::< u64 > ( ) ] ,
2021-03-26 11:41:28 +01:00
int_foo_token ( ) . into ( ) ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
2020-09-13 00:20:51 +02:00
}
#[ allow(non_camel_case_types) ]
2021-03-25 07:02:50 +01:00
pub struct int_foo_token ( ) ;
impl int_foo_token {
pub const PARAM_NAMES : & 'static [ & 'static str ] = & [ " x: &mut u64 " , " y: u64 " , " () " ] ;
#[ inline(always) ] pub fn param_types ( ) -> [ TypeId ; 2 usize ] { [ TypeId ::of ::< u64 > ( ) , TypeId ::of ::< u64 > ( ) ] }
}
2020-09-13 00:20:51 +02:00
impl PluginFunction for int_foo_token {
2021-03-22 16:11:23 +01:00
#[ inline(always) ]
2021-03-19 03:30:30 +01:00
fn call ( & self , context : NativeCallContext , args : & mut [ & mut Dynamic ] ) -> RhaiResult {
2021-02-19 16:13:41 +01:00
if args [ 0 usize ] . is_read_only ( ) {
2021-10-19 17:52:58 +02:00
return Err ( EvalAltResult ::ErrorAssignmentToConstant ( " x " . to_string ( ) , Position ::NONE ) . into ( ) ) ;
2021-02-19 16:13:41 +01:00
}
2020-09-23 04:38:02 +02:00
let arg1 = mem ::take ( args [ 1 usize ] ) . cast ::< u64 > ( ) ;
2020-09-30 16:55:40 +02:00
let arg0 = & mut args [ 0 usize ] . write_lock ::< u64 > ( ) . unwrap ( ) ;
2020-09-13 05:28:41 +02:00
Ok ( Dynamic ::from ( int_foo ( arg0 , arg1 ) ) )
2020-09-13 00:20:51 +02:00
}
2021-03-22 16:11:23 +01:00
#[ inline(always) ] fn is_method_call ( & self ) -> bool { true }
2020-09-13 00:20:51 +02:00
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
2020-09-12 22:22:09 +02:00
#[ test ]
fn one_index_getter_fn_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_index_fn {
#[ rhai_fn(index_get) ]
pub fn get_by_index ( x : & mut MyCollection , i : u64 ) -> FLOAT {
x . get ( i )
}
}
} ;
let expected_tokens = quote! {
pub mod one_index_fn {
pub fn get_by_index ( x : & mut MyCollection , i : u64 ) -> FLOAT {
x . get ( i )
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-12 22:22:09 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-14 05:40:23 +02:00
m
}
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2021-03-26 11:41:28 +01:00
m . set_fn ( " index$get$ " , FnNamespace ::Global , FnAccess ::Public , Some ( get_by_index_token ::PARAM_NAMES ) ,
2021-03-25 07:02:50 +01:00
& [ TypeId ::of ::< MyCollection > ( ) , TypeId ::of ::< u64 > ( ) ] ,
2021-03-26 11:41:28 +01:00
get_by_index_token ( ) . into ( ) ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
2020-09-12 22:22:09 +02:00
}
#[ allow(non_camel_case_types) ]
2021-03-25 07:02:50 +01:00
pub struct get_by_index_token ( ) ;
impl get_by_index_token {
pub const PARAM_NAMES : & 'static [ & 'static str ] = & [ " x: &mut MyCollection " , " i: u64 " , " FLOAT " ] ;
#[ inline(always) ] pub fn param_types ( ) -> [ TypeId ; 2 usize ] { [ TypeId ::of ::< MyCollection > ( ) , TypeId ::of ::< u64 > ( ) ] }
}
2020-09-12 22:22:09 +02:00
impl PluginFunction for get_by_index_token {
2021-03-22 16:11:23 +01:00
#[ inline(always) ]
2021-03-19 03:30:30 +01:00
fn call ( & self , context : NativeCallContext , args : & mut [ & mut Dynamic ] ) -> RhaiResult {
2021-02-19 16:13:41 +01:00
if args [ 0 usize ] . is_read_only ( ) {
2021-10-19 17:52:58 +02:00
return Err ( EvalAltResult ::ErrorAssignmentToConstant ( " x " . to_string ( ) , Position ::NONE ) . into ( ) ) ;
2021-02-19 16:13:41 +01:00
}
2020-09-23 04:38:02 +02:00
let arg1 = mem ::take ( args [ 1 usize ] ) . cast ::< u64 > ( ) ;
2020-09-30 16:55:40 +02:00
let arg0 = & mut args [ 0 usize ] . write_lock ::< MyCollection > ( ) . unwrap ( ) ;
2020-09-12 22:22:09 +02:00
Ok ( Dynamic ::from ( get_by_index ( arg0 , arg1 ) ) )
}
2021-03-22 16:11:23 +01:00
#[ inline(always) ] fn is_method_call ( & self ) -> bool { true }
2020-09-12 22:22:09 +02:00
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
2021-10-20 09:30:11 +02:00
#[ test ]
fn one_index_getter_fn_with_cfg_attr_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_index_fn {
#[ cfg(hello) ]
#[ rhai_fn(index_get) ]
#[ some_other_attr ]
pub fn get_by_index ( x : & mut MyCollection , i : u64 ) -> FLOAT {
x . get ( i )
}
}
} ;
let expected_tokens = quote! {
pub mod one_index_fn {
#[ cfg(hello) ]
#[ some_other_attr ]
pub fn get_by_index ( x : & mut MyCollection , i : u64 ) -> FLOAT {
x . get ( i )
}
#[ allow(unused_imports) ]
use super ::* ;
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
rhai_generate_into_module ( & mut m , false ) ;
m . build_index ( ) ;
m
}
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
#[ cfg(hello) ]
m . set_fn ( " index$get$ " , FnNamespace ::Global , FnAccess ::Public , Some ( get_by_index_token ::PARAM_NAMES ) ,
& [ TypeId ::of ::< MyCollection > ( ) , TypeId ::of ::< u64 > ( ) ] ,
get_by_index_token ( ) . into ( ) ) ;
if flatten { } else { }
}
#[ cfg(hello) ]
#[ allow(non_camel_case_types) ]
pub struct get_by_index_token ( ) ;
#[ cfg(hello) ]
impl get_by_index_token {
pub const PARAM_NAMES : & 'static [ & 'static str ] = & [ " x: &mut MyCollection " , " i: u64 " , " FLOAT " ] ;
#[ inline(always) ] pub fn param_types ( ) -> [ TypeId ; 2 usize ] { [ TypeId ::of ::< MyCollection > ( ) , TypeId ::of ::< u64 > ( ) ] }
}
#[ cfg(hello) ]
impl PluginFunction for get_by_index_token {
#[ inline(always) ]
fn call ( & self , context : NativeCallContext , args : & mut [ & mut Dynamic ] ) -> RhaiResult {
if args [ 0 usize ] . is_read_only ( ) {
return Err ( EvalAltResult ::ErrorAssignmentToConstant ( " x " . to_string ( ) , Position ::NONE ) . into ( ) ) ;
}
let arg1 = mem ::take ( args [ 1 usize ] ) . cast ::< u64 > ( ) ;
let arg0 = & mut args [ 0 usize ] . write_lock ::< MyCollection > ( ) . unwrap ( ) ;
Ok ( Dynamic ::from ( get_by_index ( arg0 , arg1 ) ) )
}
#[ inline(always) ] fn is_method_call ( & self ) -> bool { true }
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
2020-09-13 00:20:51 +02:00
#[ test ]
fn one_index_getter_and_rename_fn_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_index_fn {
#[ rhai_fn(name = " get " , index_get) ]
pub fn get_by_index ( x : & mut MyCollection , i : u64 ) -> FLOAT {
x . get ( i )
}
}
} ;
let expected_tokens = quote! {
pub mod one_index_fn {
pub fn get_by_index ( x : & mut MyCollection , i : u64 ) -> FLOAT {
x . get ( i )
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-13 00:20:51 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-14 05:40:23 +02:00
m
}
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2021-03-26 11:41:28 +01:00
m . set_fn ( " get " , FnNamespace ::Internal , FnAccess ::Public , Some ( get_by_index_token ::PARAM_NAMES ) ,
2021-03-25 07:02:50 +01:00
& [ TypeId ::of ::< MyCollection > ( ) , TypeId ::of ::< u64 > ( ) ] ,
2021-03-26 11:41:28 +01:00
get_by_index_token ( ) . into ( ) ) ;
m . set_fn ( " index$get$ " , FnNamespace ::Global , FnAccess ::Public , Some ( get_by_index_token ::PARAM_NAMES ) ,
2021-03-25 07:02:50 +01:00
& [ TypeId ::of ::< MyCollection > ( ) , TypeId ::of ::< u64 > ( ) ] ,
2021-03-26 11:41:28 +01:00
get_by_index_token ( ) . into ( ) ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
2020-09-13 00:20:51 +02:00
}
#[ allow(non_camel_case_types) ]
2021-03-25 07:02:50 +01:00
pub struct get_by_index_token ( ) ;
impl get_by_index_token {
pub const PARAM_NAMES : & 'static [ & 'static str ] = & [ " x: &mut MyCollection " , " i: u64 " , " FLOAT " ] ;
#[ inline(always) ] pub fn param_types ( ) -> [ TypeId ; 2 usize ] { [ TypeId ::of ::< MyCollection > ( ) , TypeId ::of ::< u64 > ( ) ] }
}
2020-09-13 00:20:51 +02:00
impl PluginFunction for get_by_index_token {
2021-03-22 16:11:23 +01:00
#[ inline(always) ]
2021-03-19 03:30:30 +01:00
fn call ( & self , context : NativeCallContext , args : & mut [ & mut Dynamic ] ) -> RhaiResult {
2021-02-19 16:13:41 +01:00
if args [ 0 usize ] . is_read_only ( ) {
2021-10-19 17:52:58 +02:00
return Err ( EvalAltResult ::ErrorAssignmentToConstant ( " x " . to_string ( ) , Position ::NONE ) . into ( ) ) ;
2021-02-19 16:13:41 +01:00
}
2020-09-23 04:38:02 +02:00
let arg1 = mem ::take ( args [ 1 usize ] ) . cast ::< u64 > ( ) ;
2020-09-30 16:55:40 +02:00
let arg0 = & mut args [ 0 usize ] . write_lock ::< MyCollection > ( ) . unwrap ( ) ;
2020-09-13 00:20:51 +02:00
Ok ( Dynamic ::from ( get_by_index ( arg0 , arg1 ) ) )
}
2021-03-22 16:11:23 +01:00
#[ inline(always) ] fn is_method_call ( & self ) -> bool { true }
2020-09-13 00:20:51 +02:00
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
2020-09-12 22:22:09 +02:00
#[ test ]
fn one_index_setter_fn_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_index_fn {
#[ rhai_fn(index_set) ]
pub fn set_by_index ( x : & mut MyCollection , i : u64 , item : FLOAT ) {
x . entry ( i ) . set ( item )
}
}
} ;
let expected_tokens = quote! {
pub mod one_index_fn {
pub fn set_by_index ( x : & mut MyCollection , i : u64 , item : FLOAT ) {
x . entry ( i ) . set ( item )
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-12 22:22:09 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-14 05:40:23 +02:00
m
}
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2021-03-26 11:41:28 +01:00
m . set_fn ( " index$set$ " , FnNamespace ::Global , FnAccess ::Public , Some ( set_by_index_token ::PARAM_NAMES ) ,
2021-03-25 07:02:50 +01:00
& [ TypeId ::of ::< MyCollection > ( ) , TypeId ::of ::< u64 > ( ) , TypeId ::of ::< FLOAT > ( ) ] ,
2021-03-26 11:41:28 +01:00
set_by_index_token ( ) . into ( ) ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
2020-09-12 22:22:09 +02:00
}
#[ allow(non_camel_case_types) ]
2021-03-25 07:02:50 +01:00
pub struct set_by_index_token ( ) ;
impl set_by_index_token {
pub const PARAM_NAMES : & 'static [ & 'static str ] = & [ " x: &mut MyCollection " , " i: u64 " , " item: FLOAT " , " () " ] ;
#[ inline(always) ] pub fn param_types ( ) -> [ TypeId ; 3 usize ] { [ TypeId ::of ::< MyCollection > ( ) , TypeId ::of ::< u64 > ( ) , TypeId ::of ::< FLOAT > ( ) ] }
}
2020-09-13 00:20:51 +02:00
impl PluginFunction for set_by_index_token {
2021-03-22 16:11:23 +01:00
#[ inline(always) ]
2021-03-19 03:30:30 +01:00
fn call ( & self , context : NativeCallContext , args : & mut [ & mut Dynamic ] ) -> RhaiResult {
2021-02-19 16:13:41 +01:00
if args [ 0 usize ] . is_read_only ( ) {
2021-10-19 17:52:58 +02:00
return Err ( EvalAltResult ::ErrorAssignmentToConstant ( " x " . to_string ( ) , Position ::NONE ) . into ( ) ) ;
2021-02-19 16:13:41 +01:00
}
2020-09-23 04:38:02 +02:00
let arg1 = mem ::take ( args [ 1 usize ] ) . cast ::< u64 > ( ) ;
let arg2 = mem ::take ( args [ 2 usize ] ) . cast ::< FLOAT > ( ) ;
2020-09-30 16:55:40 +02:00
let arg0 = & mut args [ 0 usize ] . write_lock ::< MyCollection > ( ) . unwrap ( ) ;
2020-09-13 00:20:51 +02:00
Ok ( Dynamic ::from ( set_by_index ( arg0 , arg1 , arg2 ) ) )
}
2021-03-22 16:11:23 +01:00
#[ inline(always) ] fn is_method_call ( & self ) -> bool { true }
2020-09-13 00:20:51 +02:00
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
#[ test ]
fn one_index_setter_and_rename_fn_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_index_fn {
#[ rhai_fn(name = " set " , index_set) ]
pub fn set_by_index ( x : & mut MyCollection , i : u64 , item : FLOAT ) {
x . entry ( i ) . set ( item )
}
}
} ;
let expected_tokens = quote! {
pub mod one_index_fn {
pub fn set_by_index ( x : & mut MyCollection , i : u64 , item : FLOAT ) {
x . entry ( i ) . set ( item )
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-13 00:20:51 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-14 05:40:23 +02:00
m
}
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2021-03-26 11:41:28 +01:00
m . set_fn ( " set " , FnNamespace ::Internal , FnAccess ::Public , Some ( set_by_index_token ::PARAM_NAMES ) ,
2021-03-25 07:02:50 +01:00
& [ TypeId ::of ::< MyCollection > ( ) , TypeId ::of ::< u64 > ( ) , TypeId ::of ::< FLOAT > ( ) ] ,
2021-03-26 11:41:28 +01:00
set_by_index_token ( ) . into ( ) ) ;
m . set_fn ( " index$set$ " , FnNamespace ::Global , FnAccess ::Public , Some ( set_by_index_token ::PARAM_NAMES ) ,
2021-03-25 07:02:50 +01:00
& [ TypeId ::of ::< MyCollection > ( ) , TypeId ::of ::< u64 > ( ) , TypeId ::of ::< FLOAT > ( ) ] ,
2021-03-26 11:41:28 +01:00
set_by_index_token ( ) . into ( ) ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
2020-09-13 00:20:51 +02:00
}
#[ allow(non_camel_case_types) ]
2021-03-25 07:02:50 +01:00
pub struct set_by_index_token ( ) ;
impl set_by_index_token {
pub const PARAM_NAMES : & 'static [ & 'static str ] = & [ " x: &mut MyCollection " , " i: u64 " , " item: FLOAT " , " () " ] ;
#[ inline(always) ] pub fn param_types ( ) -> [ TypeId ; 3 usize ] { [ TypeId ::of ::< MyCollection > ( ) , TypeId ::of ::< u64 > ( ) , TypeId ::of ::< FLOAT > ( ) ] }
}
2020-09-12 22:22:09 +02:00
impl PluginFunction for set_by_index_token {
2021-03-22 16:11:23 +01:00
#[ inline(always) ]
2021-03-19 03:30:30 +01:00
fn call ( & self , context : NativeCallContext , args : & mut [ & mut Dynamic ] ) -> RhaiResult {
2021-02-19 16:13:41 +01:00
if args [ 0 usize ] . is_read_only ( ) {
2021-10-19 17:52:58 +02:00
return Err ( EvalAltResult ::ErrorAssignmentToConstant ( " x " . to_string ( ) , Position ::NONE ) . into ( ) ) ;
2021-02-19 16:13:41 +01:00
}
2020-09-23 04:38:02 +02:00
let arg1 = mem ::take ( args [ 1 usize ] ) . cast ::< u64 > ( ) ;
let arg2 = mem ::take ( args [ 2 usize ] ) . cast ::< FLOAT > ( ) ;
2020-09-30 16:55:40 +02:00
let arg0 = & mut args [ 0 usize ] . write_lock ::< MyCollection > ( ) . unwrap ( ) ;
2020-09-12 22:22:09 +02:00
Ok ( Dynamic ::from ( set_by_index ( arg0 , arg1 , arg2 ) ) )
}
2021-03-22 16:11:23 +01:00
#[ inline(always) ] fn is_method_call ( & self ) -> bool { true }
2020-09-12 22:22:09 +02:00
}
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
2020-09-05 21:10:46 +02:00
#[ test ]
fn one_constant_nested_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod one_constant {
pub mod it_is {
pub const MYSTIC_NUMBER : INT = 42 ;
}
}
} ;
let expected_tokens = quote! {
pub mod one_constant {
pub mod it_is {
pub const MYSTIC_NUMBER : INT = 42 ;
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-05 21:10:46 +02:00
m
}
2020-09-14 05:40:23 +02:00
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2020-09-17 05:56:10 +02:00
m . set_var ( " MYSTIC_NUMBER " , MYSTIC_NUMBER ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
}
2020-09-05 21:10:46 +02:00
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-05 21:10:46 +02:00
m
}
2020-09-14 05:40:23 +02:00
#[ 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 ( ) ) ; }
}
}
2020-09-05 21:10:46 +02:00
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
#[ test ]
fn dual_constant_nested_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod two_constants {
pub mod first_is {
pub const MYSTIC_NUMBER : INT = 42 ;
}
pub mod second_is {
2021-10-20 09:30:11 +02:00
#[ cfg(hello) ]
2020-09-05 21:10:46 +02:00
pub const SPECIAL_CPU_NUMBER : INT = 68000 ;
}
}
} ;
let expected_tokens = quote! {
pub mod two_constants {
pub mod first_is {
pub const MYSTIC_NUMBER : INT = 42 ;
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-05 21:10:46 +02:00
m
}
2020-09-14 05:40:23 +02:00
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2020-09-17 05:56:10 +02:00
m . set_var ( " MYSTIC_NUMBER " , MYSTIC_NUMBER ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
}
2020-09-05 21:10:46 +02:00
}
pub mod second_is {
2021-10-20 09:30:11 +02:00
#[ cfg(hello) ]
2020-09-05 21:10:46 +02:00
pub const SPECIAL_CPU_NUMBER : INT = 68000 ;
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-05 21:10:46 +02:00
m
}
2020-09-14 05:40:23 +02:00
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2021-10-20 09:30:11 +02:00
#[ cfg(hello) ]
2020-09-17 05:56:10 +02:00
m . set_var ( " SPECIAL_CPU_NUMBER " , SPECIAL_CPU_NUMBER ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
}
2020-09-05 21:10:46 +02:00
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-05 21:10:46 +02:00
m
}
2020-09-14 05:40:23 +02:00
#[ 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 ( ) ) ; }
}
}
2020-09-05 21:10:46 +02:00
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
#[ test ]
fn deep_tree_nested_module ( ) {
let input_tokens : TokenStream = quote! {
pub mod heap_root {
pub const VALUE : INT = 100 ;
pub mod left {
pub const VALUE : INT = 19 ;
pub mod left {
pub const VALUE : INT = 17 ;
pub mod left {
pub const VALUE : INT = 2 ;
}
pub mod right {
pub const VALUE : INT = 7 ;
}
}
pub mod right {
pub const VALUE : INT = 3 ;
}
}
pub mod right {
pub const VALUE : INT = 36 ;
pub mod left {
pub const VALUE : INT = 25 ;
}
pub mod right {
pub const VALUE : INT = 1 ;
}
}
}
} ;
let expected_tokens = quote! {
pub mod heap_root {
pub const VALUE : INT = 100 ;
pub mod left {
pub const VALUE : INT = 19 ;
pub mod left {
pub const VALUE : INT = 17 ;
pub mod left {
pub const VALUE : INT = 2 ;
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-05 21:10:46 +02:00
m
}
2020-09-14 05:40:23 +02:00
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2020-09-17 05:56:10 +02:00
m . set_var ( " VALUE " , VALUE ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
}
2020-09-05 21:10:46 +02:00
}
pub mod right {
pub const VALUE : INT = 7 ;
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-05 21:10:46 +02:00
m
}
2020-09-14 05:40:23 +02:00
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2020-09-17 05:56:10 +02:00
m . set_var ( " VALUE " , VALUE ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
}
2020-09-05 21:10:46 +02:00
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-05 21:10:46 +02:00
m
}
2020-09-14 05:40:23 +02:00
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2020-09-17 05:56:10 +02:00
m . set_var ( " VALUE " , VALUE ) ;
2020-09-14 05:40:23 +02:00
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 ( ) ) ; }
}
}
2020-09-05 21:10:46 +02:00
}
pub mod right {
pub const VALUE : INT = 3 ;
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-05 21:10:46 +02:00
m
}
2020-09-14 05:40:23 +02:00
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2020-09-17 05:56:10 +02:00
m . set_var ( " VALUE " , VALUE ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
}
2020-09-05 21:10:46 +02:00
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-05 21:10:46 +02:00
m
}
2020-09-14 05:40:23 +02:00
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2020-09-17 05:56:10 +02:00
m . set_var ( " VALUE " , VALUE ) ;
2020-09-14 05:40:23 +02:00
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 ( ) ) ; }
}
}
2020-09-05 21:10:46 +02:00
}
pub mod right {
pub const VALUE : INT = 36 ;
pub mod left {
pub const VALUE : INT = 25 ;
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-05 21:10:46 +02:00
m
}
2020-09-14 05:40:23 +02:00
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2020-09-17 05:56:10 +02:00
m . set_var ( " VALUE " , VALUE ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
}
2020-09-05 21:10:46 +02:00
}
pub mod right {
pub const VALUE : INT = 1 ;
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-05 21:10:46 +02:00
m
}
2020-09-14 05:40:23 +02:00
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2020-09-17 05:56:10 +02:00
m . set_var ( " VALUE " , VALUE ) ;
2020-09-14 05:40:23 +02:00
if flatten { } else { }
}
2020-09-05 21:10:46 +02:00
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-05 21:10:46 +02:00
m
}
2020-09-14 05:40:23 +02:00
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2020-09-17 05:56:10 +02:00
m . set_var ( " VALUE " , VALUE ) ;
2020-09-14 05:40:23 +02:00
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 ( ) ) ; }
}
}
2020-09-05 21:10:46 +02:00
}
#[ allow(unused_imports) ]
use super ::* ;
2020-09-14 05:40:23 +02:00
2020-09-05 21:10:46 +02:00
pub fn rhai_module_generate ( ) -> Module {
let mut m = Module ::new ( ) ;
2020-09-14 05:40:23 +02:00
rhai_generate_into_module ( & mut m , false ) ;
2020-11-21 15:18:32 +01:00
m . build_index ( ) ;
2020-09-05 21:10:46 +02:00
m
}
2020-09-14 05:40:23 +02:00
#[ allow(unused_mut) ]
pub fn rhai_generate_into_module ( m : & mut Module , flatten : bool ) {
2020-09-17 05:56:10 +02:00
m . set_var ( " VALUE " , VALUE ) ;
2020-09-14 05:40:23 +02:00
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 ( ) ) ; }
}
}
2020-09-05 21:10:46 +02:00
}
} ;
let item_mod = syn ::parse2 ::< Module > ( input_tokens ) . unwrap ( ) ;
assert_streams_eq ( item_mod . generate ( ) , expected_tokens ) ;
}
}