2020-09-05 21:10:46 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod function_tests {
|
|
|
|
use crate::function::ExportedFn;
|
|
|
|
|
|
|
|
use proc_macro2::TokenStream;
|
|
|
|
use quote::quote;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn minimal_fn() {
|
|
|
|
let input_tokens: TokenStream = quote! {
|
|
|
|
pub fn do_nothing() { }
|
|
|
|
};
|
|
|
|
|
|
|
|
let item_fn = syn::parse2::<ExportedFn>(input_tokens).unwrap();
|
|
|
|
assert_eq!(&item_fn.name().to_string(), "do_nothing");
|
|
|
|
assert!(!item_fn.mutable_receiver());
|
|
|
|
assert!(item_fn.is_public());
|
|
|
|
assert!(item_fn.return_type().is_none());
|
|
|
|
assert_eq!(item_fn.arg_list().count(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn one_arg_fn() {
|
|
|
|
let input_tokens: TokenStream = quote! {
|
|
|
|
pub fn do_something(x: usize) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
let item_fn = syn::parse2::<ExportedFn>(input_tokens).unwrap();
|
|
|
|
assert_eq!(&item_fn.name().to_string(), "do_something");
|
|
|
|
assert_eq!(item_fn.arg_list().count(), 1);
|
|
|
|
assert!(!item_fn.mutable_receiver());
|
|
|
|
assert!(item_fn.is_public());
|
|
|
|
assert!(item_fn.return_type().is_none());
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
item_fn.arg_list().next().unwrap(),
|
|
|
|
&syn::parse2::<syn::FnArg>(quote! { x: usize }).unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn two_arg_fn() {
|
|
|
|
let input_tokens: TokenStream = quote! {
|
|
|
|
pub fn do_something(x: usize, y: f32) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
let item_fn = syn::parse2::<ExportedFn>(input_tokens).unwrap();
|
|
|
|
assert_eq!(&item_fn.name().to_string(), "do_something");
|
|
|
|
assert_eq!(item_fn.arg_list().count(), 2);
|
|
|
|
assert!(!item_fn.mutable_receiver());
|
|
|
|
assert!(item_fn.is_public());
|
|
|
|
assert!(item_fn.return_type().is_none());
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
item_fn.arg_list().next().unwrap(),
|
|
|
|
&syn::parse2::<syn::FnArg>(quote! { x: usize }).unwrap()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
item_fn.arg_list().nth(1).unwrap(),
|
|
|
|
&syn::parse2::<syn::FnArg>(quote! { y: f32 }).unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn usize_returning_fn() {
|
|
|
|
let input_tokens: TokenStream = quote! {
|
|
|
|
pub fn get_magic_number() -> usize { 42 }
|
|
|
|
};
|
|
|
|
|
|
|
|
let item_fn = syn::parse2::<ExportedFn>(input_tokens).unwrap();
|
|
|
|
assert_eq!(&item_fn.name().to_string(), "get_magic_number");
|
|
|
|
assert!(!item_fn.mutable_receiver());
|
|
|
|
assert!(item_fn.is_public());
|
|
|
|
assert_eq!(item_fn.arg_list().count(), 0);
|
|
|
|
assert_eq!(
|
|
|
|
item_fn.return_type().unwrap(),
|
|
|
|
&syn::Type::Path(syn::TypePath {
|
|
|
|
qself: None,
|
|
|
|
path: syn::parse2::<syn::Path>(quote! { usize }).unwrap()
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ref_returning_fn() {
|
|
|
|
let input_tokens: TokenStream = quote! {
|
|
|
|
pub fn get_magic_phrase() -> &'static str { "open sesame" }
|
|
|
|
};
|
|
|
|
|
|
|
|
let err = syn::parse2::<ExportedFn>(input_tokens).unwrap_err();
|
2020-10-15 07:28:22 +02:00
|
|
|
assert_eq!(
|
|
|
|
format!("{}", err),
|
|
|
|
"Rhai functions cannot return references"
|
|
|
|
);
|
2020-09-05 21:10:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ptr_returning_fn() {
|
|
|
|
let input_tokens: TokenStream = quote! {
|
|
|
|
pub fn get_magic_phrase() -> *const str { "open sesame" }
|
|
|
|
};
|
|
|
|
|
|
|
|
let err = syn::parse2::<ExportedFn>(input_tokens).unwrap_err();
|
2020-10-15 07:28:22 +02:00
|
|
|
assert_eq!(format!("{}", err), "Rhai functions cannot return pointers");
|
2020-09-05 21:10:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ref_arg_fn() {
|
|
|
|
let input_tokens: TokenStream = quote! {
|
|
|
|
pub fn greet(who: &Person) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
let err = syn::parse2::<ExportedFn>(input_tokens).unwrap_err();
|
|
|
|
assert_eq!(
|
|
|
|
format!("{}", err),
|
|
|
|
"references from Rhai in this position must be mutable"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ref_second_arg_fn() {
|
|
|
|
let input_tokens: TokenStream = quote! {
|
|
|
|
pub fn greet(count: usize, who: &Person) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
let err = syn::parse2::<ExportedFn>(input_tokens).unwrap_err();
|
|
|
|
assert_eq!(
|
|
|
|
format!("{}", err),
|
|
|
|
"this type in this position passes from Rhai by value"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mut_ref_second_arg_fn() {
|
|
|
|
let input_tokens: TokenStream = quote! {
|
|
|
|
pub fn give(item_name: &str, who: &mut Person) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
let err = syn::parse2::<ExportedFn>(input_tokens).unwrap_err();
|
|
|
|
assert_eq!(
|
|
|
|
format!("{}", err),
|
|
|
|
"this type in this position passes from Rhai by value"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn str_arg_fn() {
|
|
|
|
let input_tokens: TokenStream = quote! {
|
|
|
|
pub fn log(message: &str) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
let item_fn = syn::parse2::<ExportedFn>(input_tokens).unwrap();
|
|
|
|
assert_eq!(&item_fn.name().to_string(), "log");
|
|
|
|
assert_eq!(item_fn.arg_list().count(), 1);
|
|
|
|
assert!(!item_fn.mutable_receiver());
|
|
|
|
assert!(item_fn.is_public());
|
|
|
|
assert!(item_fn.return_type().is_none());
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
item_fn.arg_list().next().unwrap(),
|
|
|
|
&syn::parse2::<syn::FnArg>(quote! { message: &str }).unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn str_second_arg_fn() {
|
|
|
|
let input_tokens: TokenStream = quote! {
|
|
|
|
pub fn log(level: usize, message: &str) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
let item_fn = syn::parse2::<ExportedFn>(input_tokens).unwrap();
|
|
|
|
assert_eq!(&item_fn.name().to_string(), "log");
|
|
|
|
assert_eq!(item_fn.arg_list().count(), 2);
|
|
|
|
assert!(!item_fn.mutable_receiver());
|
|
|
|
assert!(item_fn.is_public());
|
|
|
|
assert!(item_fn.return_type().is_none());
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
item_fn.arg_list().next().unwrap(),
|
|
|
|
&syn::parse2::<syn::FnArg>(quote! { level: usize }).unwrap()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
item_fn.arg_list().nth(1).unwrap(),
|
|
|
|
&syn::parse2::<syn::FnArg>(quote! { message: &str }).unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn private_fn() {
|
|
|
|
let input_tokens: TokenStream = quote! {
|
|
|
|
fn do_nothing() { }
|
|
|
|
};
|
|
|
|
|
|
|
|
let item_fn = syn::parse2::<ExportedFn>(input_tokens).unwrap();
|
|
|
|
assert_eq!(&item_fn.name().to_string(), "do_nothing");
|
|
|
|
assert!(!item_fn.mutable_receiver());
|
|
|
|
assert!(!item_fn.is_public());
|
|
|
|
assert!(item_fn.return_type().is_none());
|
|
|
|
assert_eq!(item_fn.arg_list().count(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn receiver_fn() {
|
|
|
|
let input_tokens: TokenStream = quote! {
|
|
|
|
pub fn act_upon(&mut self) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
let item_fn = syn::parse2::<ExportedFn>(input_tokens).unwrap();
|
|
|
|
assert_eq!(&item_fn.name().to_string(), "act_upon");
|
|
|
|
assert!(item_fn.mutable_receiver());
|
|
|
|
assert!(item_fn.is_public());
|
|
|
|
assert!(item_fn.return_type().is_none());
|
|
|
|
assert_eq!(item_fn.arg_list().count(), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn immutable_receiver_fn() {
|
|
|
|
let input_tokens: TokenStream = quote! {
|
|
|
|
pub fn act_upon(&self) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
let item_fn = syn::parse2::<ExportedFn>(input_tokens).unwrap();
|
|
|
|
assert_eq!(&item_fn.name().to_string(), "act_upon");
|
|
|
|
assert!(item_fn.mutable_receiver());
|
|
|
|
assert!(item_fn.is_public());
|
|
|
|
assert!(item_fn.return_type().is_none());
|
|
|
|
assert_eq!(item_fn.arg_list().count(), 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod generate_tests {
|
|
|
|
use crate::function::ExportedFn;
|
|
|
|
|
|
|
|
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 minimal_fn() {
|
|
|
|
let input_tokens: TokenStream = quote! {
|
|
|
|
pub fn do_nothing() { }
|
|
|
|
};
|
|
|
|
|
|
|
|
let expected_tokens = quote! {
|
2021-02-21 06:51:24 +01:00
|
|
|
#[automatically_derived]
|
2020-09-05 21:10:46 +02:00
|
|
|
pub mod rhai_fn_do_nothing {
|
|
|
|
use super::*;
|
2021-03-25 07:02:50 +01:00
|
|
|
pub struct Token();
|
|
|
|
impl Token {
|
|
|
|
pub const PARAM_NAMES: &'static [&'static str] = &["()"];
|
|
|
|
#[inline(always)] pub fn param_types() -> [TypeId; 0usize] { [] }
|
|
|
|
}
|
2020-09-05 21:10:46 +02:00
|
|
|
impl PluginFunction for Token {
|
2021-03-22 16:11:23 +01:00
|
|
|
#[inline(always)] fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
|
2020-09-05 21:10:46 +02:00
|
|
|
Ok(Dynamic::from(do_nothing()))
|
|
|
|
}
|
|
|
|
|
2021-03-22 16:11:23 +01:00
|
|
|
#[inline(always)] fn is_method_call(&self) -> bool { false }
|
|
|
|
#[inline(always)] fn is_variadic(&self) -> bool { false }
|
2020-09-05 21:10:46 +02:00
|
|
|
}
|
2021-02-21 07:11:19 +01:00
|
|
|
#[allow(unused)]
|
2021-03-25 07:02:50 +01:00
|
|
|
#[inline(always)] pub fn dynamic_result_fn() -> RhaiResult {
|
2020-10-20 03:21:41 +02:00
|
|
|
Ok(Dynamic::from(do_nothing()))
|
2020-09-05 21:10:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let item_fn = syn::parse2::<ExportedFn>(input_tokens).unwrap();
|
|
|
|
assert_streams_eq(item_fn.generate(), expected_tokens);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn one_arg_usize_fn() {
|
|
|
|
let input_tokens: TokenStream = quote! {
|
|
|
|
pub fn do_something(x: usize) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
let expected_tokens = quote! {
|
2021-02-21 06:51:24 +01:00
|
|
|
#[automatically_derived]
|
2020-09-05 21:10:46 +02:00
|
|
|
pub mod rhai_fn_do_something {
|
|
|
|
use super::*;
|
2021-03-25 07:02:50 +01:00
|
|
|
pub struct Token();
|
|
|
|
impl Token {
|
|
|
|
pub const PARAM_NAMES: &'static [&'static str] = &["x: usize", "()"];
|
|
|
|
#[inline(always)] pub fn param_types() -> [TypeId; 1usize] { [TypeId::of::<usize>()] }
|
|
|
|
}
|
2020-09-05 21:10:46 +02:00
|
|
|
impl PluginFunction for 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[0usize]).cast::<usize>();
|
2020-09-05 21:10:46 +02:00
|
|
|
Ok(Dynamic::from(do_something(arg0)))
|
|
|
|
}
|
|
|
|
|
2021-03-22 16:11:23 +01:00
|
|
|
#[inline(always)] fn is_method_call(&self) -> bool { false }
|
|
|
|
#[inline(always)] fn is_variadic(&self) -> bool { false }
|
2020-09-05 21:10:46 +02:00
|
|
|
}
|
2021-02-21 07:11:19 +01:00
|
|
|
#[allow(unused)]
|
2021-03-25 07:02:50 +01:00
|
|
|
#[inline(always)] pub fn dynamic_result_fn(x: usize) -> RhaiResult {
|
2020-10-20 03:21:41 +02:00
|
|
|
Ok(Dynamic::from(do_something(x)))
|
2020-09-05 21:10:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let item_fn = syn::parse2::<ExportedFn>(input_tokens).unwrap();
|
|
|
|
assert_streams_eq(item_fn.generate(), expected_tokens);
|
|
|
|
}
|
|
|
|
|
2020-10-18 15:47:34 +02:00
|
|
|
#[test]
|
|
|
|
fn one_arg_fn_with_context() {
|
|
|
|
let input_tokens: TokenStream = quote! {
|
|
|
|
pub fn do_something(context: NativeCallContext, x: usize) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
let expected_tokens = quote! {
|
2021-02-21 06:51:24 +01:00
|
|
|
#[automatically_derived]
|
2020-10-18 15:47:34 +02:00
|
|
|
pub mod rhai_fn_do_something {
|
|
|
|
use super::*;
|
2021-03-25 07:02:50 +01:00
|
|
|
pub struct Token();
|
|
|
|
impl Token {
|
|
|
|
pub const PARAM_NAMES: &'static [&'static str] = &["x: usize", "()"];
|
|
|
|
#[inline(always)] pub fn param_types() -> [TypeId; 1usize] { [TypeId::of::<usize>()] }
|
|
|
|
}
|
2020-10-18 15:47:34 +02:00
|
|
|
impl PluginFunction for 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-10-18 15:47:34 +02:00
|
|
|
let arg0 = mem::take(args[0usize]).cast::<usize>();
|
|
|
|
Ok(Dynamic::from(do_something(context, arg0)))
|
|
|
|
}
|
|
|
|
|
2021-03-22 16:11:23 +01:00
|
|
|
#[inline(always)] fn is_method_call(&self) -> bool { false }
|
|
|
|
#[inline(always)] fn is_variadic(&self) -> bool { false }
|
2020-10-18 15:47:34 +02:00
|
|
|
}
|
2021-02-21 07:11:19 +01:00
|
|
|
#[allow(unused)]
|
2021-03-25 07:02:50 +01:00
|
|
|
#[inline(always)] pub fn dynamic_result_fn(context: NativeCallContext, x: usize) -> RhaiResult {
|
2020-10-20 03:21:41 +02:00
|
|
|
Ok(Dynamic::from(do_something(context, x)))
|
2020-10-18 15:47:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let item_fn = syn::parse2::<ExportedFn>(input_tokens).unwrap();
|
|
|
|
assert!(item_fn.pass_context());
|
|
|
|
assert_streams_eq(item_fn.generate(), expected_tokens);
|
|
|
|
}
|
|
|
|
|
2020-10-15 07:28:22 +02:00
|
|
|
#[test]
|
|
|
|
fn return_dynamic() {
|
|
|
|
let input_tokens: TokenStream = quote! {
|
|
|
|
pub fn return_dynamic() -> (((rhai::Dynamic))) {
|
|
|
|
().into()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let expected_tokens = quote! {
|
2021-02-21 06:51:24 +01:00
|
|
|
#[automatically_derived]
|
2020-10-15 07:28:22 +02:00
|
|
|
pub mod rhai_fn_return_dynamic {
|
|
|
|
use super::*;
|
2021-03-25 07:02:50 +01:00
|
|
|
pub struct Token();
|
|
|
|
impl Token {
|
|
|
|
pub const PARAM_NAMES: &'static [&'static str] = &["rhai::Dynamic"];
|
|
|
|
#[inline(always)] pub fn param_types() -> [TypeId; 0usize] { [] }
|
|
|
|
}
|
2020-10-15 07:28:22 +02:00
|
|
|
impl PluginFunction for 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-03-22 04:18:09 +01:00
|
|
|
Ok(Dynamic::from(return_dynamic()))
|
2020-10-15 07:28:22 +02:00
|
|
|
}
|
|
|
|
|
2021-03-22 16:11:23 +01:00
|
|
|
#[inline(always)] fn is_method_call(&self) -> bool { false }
|
|
|
|
#[inline(always)] fn is_variadic(&self) -> bool { false }
|
2020-10-15 07:28:22 +02:00
|
|
|
}
|
2021-02-21 07:11:19 +01:00
|
|
|
#[allow(unused)]
|
2021-03-25 07:02:50 +01:00
|
|
|
#[inline(always)] pub fn dynamic_result_fn() -> RhaiResult {
|
2021-03-22 04:18:09 +01:00
|
|
|
Ok(Dynamic::from(return_dynamic()))
|
2020-10-15 07:28:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let item_fn = syn::parse2::<ExportedFn>(input_tokens).unwrap();
|
|
|
|
assert_streams_eq(item_fn.generate(), expected_tokens);
|
|
|
|
}
|
|
|
|
|
2020-09-05 21:10:46 +02:00
|
|
|
#[test]
|
|
|
|
fn one_arg_usize_fn_impl() {
|
|
|
|
let input_tokens: TokenStream = quote! {
|
|
|
|
pub fn do_something(x: usize) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
let expected_tokens = quote! {
|
2021-03-25 07:02:50 +01:00
|
|
|
impl TestStruct {
|
|
|
|
pub const PARAM_NAMES: &'static [&'static str] = &["x: usize", "()"];
|
|
|
|
#[inline(always)] pub fn param_types() -> [TypeId; 1usize] { [TypeId::of::<usize>()] }
|
|
|
|
}
|
2020-12-16 07:57:28 +01:00
|
|
|
impl PluginFunction for TestStruct {
|
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[0usize]).cast::<usize>();
|
2020-09-05 21:10:46 +02:00
|
|
|
Ok(Dynamic::from(do_something(arg0)))
|
|
|
|
}
|
|
|
|
|
2021-03-22 16:11:23 +01:00
|
|
|
#[inline(always)] fn is_method_call(&self) -> bool { false }
|
|
|
|
#[inline(always)] fn is_variadic(&self) -> bool { false }
|
2021-03-25 07:02:50 +01:00
|
|
|
}
|
2020-09-05 21:10:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let item_fn = syn::parse2::<ExportedFn>(input_tokens).unwrap();
|
2020-12-16 07:57:28 +01:00
|
|
|
assert_streams_eq(item_fn.generate_impl("TestStruct"), expected_tokens);
|
2020-09-05 21:10:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn two_arg_returning_usize_fn() {
|
|
|
|
let input_tokens: TokenStream = quote! {
|
|
|
|
pub fn add_together(x: usize, y: usize) -> usize { x + y }
|
|
|
|
};
|
|
|
|
|
|
|
|
let expected_tokens = quote! {
|
2021-02-21 06:51:24 +01:00
|
|
|
#[automatically_derived]
|
2020-09-05 21:10:46 +02:00
|
|
|
pub mod rhai_fn_add_together {
|
|
|
|
use super::*;
|
2021-03-25 07:02:50 +01:00
|
|
|
pub struct Token();
|
|
|
|
impl Token {
|
|
|
|
pub const PARAM_NAMES: &'static [&'static str] = &["x: usize", "y: usize", "usize"];
|
|
|
|
#[inline(always)] pub fn param_types() -> [TypeId; 2usize] { [TypeId::of::<usize>(), TypeId::of::<usize>()] }
|
|
|
|
}
|
2020-09-05 21:10:46 +02:00
|
|
|
impl PluginFunction for 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[0usize]).cast::<usize>();
|
|
|
|
let arg1 = mem::take(args[1usize]).cast::<usize>();
|
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 }
|
|
|
|
#[inline(always)] fn is_variadic(&self) -> bool { false }
|
2020-09-05 21:10:46 +02:00
|
|
|
}
|
2021-02-21 07:11:19 +01:00
|
|
|
#[allow(unused)]
|
2021-03-25 07:02:50 +01:00
|
|
|
#[inline(always)] pub fn dynamic_result_fn(x: usize, y: usize) -> RhaiResult {
|
2020-10-20 03:21:41 +02:00
|
|
|
Ok(Dynamic::from(add_together(x, y)))
|
2020-09-05 21:10:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let item_fn = syn::parse2::<ExportedFn>(input_tokens).unwrap();
|
|
|
|
assert_streams_eq(item_fn.generate(), expected_tokens);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mut_arg_usize_fn() {
|
|
|
|
let input_tokens: TokenStream = quote! {
|
|
|
|
pub fn increment(x: &mut usize, y: usize) { *x += y; }
|
|
|
|
};
|
|
|
|
|
|
|
|
let expected_tokens = quote! {
|
2021-02-21 06:51:24 +01:00
|
|
|
#[automatically_derived]
|
2020-09-05 21:10:46 +02:00
|
|
|
pub mod rhai_fn_increment {
|
|
|
|
use super::*;
|
2021-03-25 07:02:50 +01:00
|
|
|
pub struct Token();
|
|
|
|
impl Token {
|
|
|
|
pub const PARAM_NAMES: &'static [&'static str] = &["x: &mut usize", "y: usize", "()"];
|
|
|
|
#[inline(always)] pub fn param_types() -> [TypeId; 2usize] { [TypeId::of::<usize>(), TypeId::of::<usize>()] }
|
|
|
|
}
|
2020-09-05 21:10:46 +02:00
|
|
|
impl PluginFunction for 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[0usize].is_read_only() {
|
|
|
|
return Err(Box::new(
|
|
|
|
EvalAltResult::ErrorAssignmentToConstant("x".to_string(), Position::NONE)
|
|
|
|
));
|
|
|
|
}
|
2020-09-23 04:38:02 +02:00
|
|
|
let arg1 = mem::take(args[1usize]).cast::<usize>();
|
2020-09-30 16:55:40 +02:00
|
|
|
let arg0 = &mut args[0usize].write_lock::<usize>().unwrap();
|
2020-09-05 21:10:46 +02:00
|
|
|
Ok(Dynamic::from(increment(arg0, arg1)))
|
|
|
|
}
|
|
|
|
|
2021-03-22 16:11:23 +01:00
|
|
|
#[inline(always)] fn is_method_call(&self) -> bool { true }
|
|
|
|
#[inline(always)] fn is_variadic(&self) -> bool { false }
|
2020-09-05 21:10:46 +02:00
|
|
|
}
|
2021-02-21 07:11:19 +01:00
|
|
|
#[allow(unused)]
|
2021-03-25 07:02:50 +01:00
|
|
|
#[inline(always)] pub fn dynamic_result_fn(x: &mut usize, y: usize) -> RhaiResult {
|
2020-10-20 03:21:41 +02:00
|
|
|
Ok(Dynamic::from(increment(x, y)))
|
2020-09-05 21:10:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let item_fn = syn::parse2::<ExportedFn>(input_tokens).unwrap();
|
|
|
|
assert!(item_fn.mutable_receiver());
|
|
|
|
assert_streams_eq(item_fn.generate(), expected_tokens);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn str_arg_fn() {
|
|
|
|
let input_tokens: TokenStream = quote! {
|
|
|
|
pub fn special_print(message: &str) { eprintln!("----{}----", message); }
|
|
|
|
};
|
|
|
|
|
|
|
|
let expected_tokens = quote! {
|
2021-02-21 06:51:24 +01:00
|
|
|
#[automatically_derived]
|
2020-09-05 21:10:46 +02:00
|
|
|
pub mod rhai_fn_special_print {
|
|
|
|
use super::*;
|
2021-03-25 07:02:50 +01:00
|
|
|
pub struct Token();
|
|
|
|
impl Token {
|
|
|
|
pub const PARAM_NAMES: &'static [&'static str] = &["message: &str", "()"];
|
|
|
|
#[inline(always)] pub fn param_types() -> [TypeId; 1usize] { [TypeId::of::<ImmutableString>()] }
|
|
|
|
}
|
2020-09-05 21:10:46 +02:00
|
|
|
impl PluginFunction for 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-20 08:23:14 +02:00
|
|
|
let arg0 = mem::take(args[0usize]).take_immutable_string().unwrap();
|
2020-09-05 21:10:46 +02:00
|
|
|
Ok(Dynamic::from(special_print(&arg0)))
|
|
|
|
}
|
|
|
|
|
2021-03-22 16:11:23 +01:00
|
|
|
#[inline(always)] fn is_method_call(&self) -> bool { false }
|
|
|
|
#[inline(always)] fn is_variadic(&self) -> bool { false }
|
2020-09-05 21:10:46 +02:00
|
|
|
}
|
2021-02-21 07:11:19 +01:00
|
|
|
#[allow(unused)]
|
2021-03-25 07:02:50 +01:00
|
|
|
#[inline(always)] pub fn dynamic_result_fn(message: &str) -> RhaiResult {
|
2020-10-20 03:21:41 +02:00
|
|
|
Ok(Dynamic::from(special_print(message)))
|
2020-09-05 21:10:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let item_fn = syn::parse2::<ExportedFn>(input_tokens).unwrap();
|
|
|
|
assert!(!item_fn.mutable_receiver());
|
|
|
|
assert_streams_eq(item_fn.generate(), expected_tokens);
|
|
|
|
}
|
|
|
|
}
|