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! {
|
|
|
|
#[allow(unused)]
|
|
|
|
pub mod rhai_fn_do_nothing {
|
|
|
|
use super::*;
|
|
|
|
struct Token();
|
|
|
|
impl PluginFunction for Token {
|
2020-10-18 15:47:34 +02:00
|
|
|
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
|
2020-09-05 21:10:46 +02:00
|
|
|
debug_assert_eq!(args.len(), 0usize,
|
|
|
|
"wrong arg count: {} != {}", args.len(), 0usize);
|
|
|
|
Ok(Dynamic::from(do_nothing()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_method_call(&self) -> bool { false }
|
2020-09-30 16:55:40 +02:00
|
|
|
fn is_variadic(&self) -> bool { false }
|
2020-09-05 21:10:46 +02:00
|
|
|
fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) }
|
2020-11-22 10:21:34 +01:00
|
|
|
fn input_names(&self) -> Box<[&'static str]> {
|
|
|
|
new_vec![].into_boxed_slice()
|
|
|
|
}
|
2020-09-05 21:10:46 +02:00
|
|
|
fn input_types(&self) -> Box<[TypeId]> {
|
|
|
|
new_vec![].into_boxed_slice()
|
|
|
|
}
|
2020-11-22 15:15:17 +01:00
|
|
|
fn return_type(&self) -> &'static str {
|
|
|
|
"()"
|
|
|
|
}
|
2020-09-05 21:10:46 +02:00
|
|
|
}
|
|
|
|
pub fn token_callable() -> CallableFunction {
|
2020-09-29 07:06:48 +02:00
|
|
|
Token().into()
|
2020-09-05 21:10:46 +02:00
|
|
|
}
|
2020-11-22 10:21:34 +01:00
|
|
|
pub fn token_input_names() -> Box<[&'static str]> {
|
|
|
|
Token().input_names()
|
|
|
|
}
|
2020-09-05 21:10:46 +02:00
|
|
|
pub fn token_input_types() -> Box<[TypeId]> {
|
|
|
|
Token().input_types()
|
|
|
|
}
|
2020-11-22 15:15:17 +01:00
|
|
|
pub fn token_return_type() -> &'static str {
|
|
|
|
Token().return_type()
|
|
|
|
}
|
2020-10-15 07:28:22 +02:00
|
|
|
pub fn dynamic_result_fn() -> Result<Dynamic, Box<EvalAltResult> > {
|
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! {
|
|
|
|
#[allow(unused)]
|
|
|
|
pub mod rhai_fn_do_something {
|
|
|
|
use super::*;
|
|
|
|
struct Token();
|
|
|
|
impl PluginFunction for Token {
|
2020-10-18 15:47:34 +02:00
|
|
|
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
|
2020-09-05 21:10:46 +02:00
|
|
|
debug_assert_eq!(args.len(), 1usize,
|
|
|
|
"wrong arg count: {} != {}", args.len(), 1usize);
|
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)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_method_call(&self) -> bool { false }
|
2020-09-30 16:55:40 +02:00
|
|
|
fn is_variadic(&self) -> bool { false }
|
2020-09-05 21:10:46 +02:00
|
|
|
fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) }
|
2020-11-22 10:21:34 +01:00
|
|
|
fn input_names(&self) -> Box<[&'static str]> {
|
|
|
|
new_vec!["x: usize"].into_boxed_slice()
|
|
|
|
}
|
2020-09-05 21:10:46 +02:00
|
|
|
fn input_types(&self) -> Box<[TypeId]> {
|
|
|
|
new_vec![TypeId::of::<usize>()].into_boxed_slice()
|
|
|
|
}
|
2020-11-22 15:15:17 +01:00
|
|
|
fn return_type(&self) -> &'static str {
|
|
|
|
"()"
|
|
|
|
}
|
2020-09-05 21:10:46 +02:00
|
|
|
}
|
|
|
|
pub fn token_callable() -> CallableFunction {
|
2020-09-29 07:06:48 +02:00
|
|
|
Token().into()
|
2020-09-05 21:10:46 +02:00
|
|
|
}
|
2020-11-22 10:21:34 +01:00
|
|
|
pub fn token_input_names() -> Box<[&'static str]> {
|
|
|
|
Token().input_names()
|
|
|
|
}
|
2020-09-05 21:10:46 +02:00
|
|
|
pub fn token_input_types() -> Box<[TypeId]> {
|
|
|
|
Token().input_types()
|
|
|
|
}
|
2020-11-22 15:15:17 +01:00
|
|
|
pub fn token_return_type() -> &'static str {
|
|
|
|
Token().return_type()
|
|
|
|
}
|
2020-10-15 07:28:22 +02:00
|
|
|
pub fn dynamic_result_fn(x: usize) -> Result<Dynamic, Box<EvalAltResult> > {
|
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! {
|
|
|
|
#[allow(unused)]
|
|
|
|
pub mod rhai_fn_do_something {
|
|
|
|
use super::*;
|
|
|
|
struct Token();
|
|
|
|
impl PluginFunction for Token {
|
|
|
|
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
|
|
|
|
debug_assert_eq!(args.len(), 1usize,
|
|
|
|
"wrong arg count: {} != {}", args.len(), 1usize);
|
|
|
|
let arg0 = mem::take(args[0usize]).cast::<usize>();
|
|
|
|
Ok(Dynamic::from(do_something(context, arg0)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_method_call(&self) -> bool { false }
|
|
|
|
fn is_variadic(&self) -> bool { false }
|
|
|
|
fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) }
|
2020-11-22 10:21:34 +01:00
|
|
|
fn input_names(&self) -> Box<[&'static str]> {
|
|
|
|
new_vec!["x: usize"].into_boxed_slice()
|
|
|
|
}
|
2020-10-18 15:47:34 +02:00
|
|
|
fn input_types(&self) -> Box<[TypeId]> {
|
|
|
|
new_vec![TypeId::of::<usize>()].into_boxed_slice()
|
|
|
|
}
|
2020-11-22 15:15:17 +01:00
|
|
|
fn return_type(&self) -> &'static str {
|
|
|
|
"()"
|
|
|
|
}
|
2020-10-18 15:47:34 +02:00
|
|
|
}
|
|
|
|
pub fn token_callable() -> CallableFunction {
|
|
|
|
Token().into()
|
|
|
|
}
|
2020-11-22 10:21:34 +01:00
|
|
|
pub fn token_input_names() -> Box<[&'static str]> {
|
|
|
|
Token().input_names()
|
|
|
|
}
|
2020-10-18 15:47:34 +02:00
|
|
|
pub fn token_input_types() -> Box<[TypeId]> {
|
|
|
|
Token().input_types()
|
|
|
|
}
|
2020-11-22 15:15:17 +01:00
|
|
|
pub fn token_return_type() -> &'static str {
|
|
|
|
Token().return_type()
|
|
|
|
}
|
2020-10-18 15:47:34 +02:00
|
|
|
pub fn dynamic_result_fn(context: NativeCallContext, x: usize) -> Result<Dynamic, Box<EvalAltResult> > {
|
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! {
|
|
|
|
#[allow(unused)]
|
|
|
|
pub mod rhai_fn_return_dynamic {
|
|
|
|
use super::*;
|
|
|
|
struct Token();
|
|
|
|
impl PluginFunction for Token {
|
2020-10-18 15:47:34 +02:00
|
|
|
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
|
2020-10-15 07:28:22 +02:00
|
|
|
debug_assert_eq!(args.len(), 0usize,
|
|
|
|
"wrong arg count: {} != {}", args.len(), 0usize);
|
|
|
|
Ok(return_dynamic())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_method_call(&self) -> bool { false }
|
|
|
|
fn is_variadic(&self) -> bool { false }
|
|
|
|
fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) }
|
2020-11-22 10:21:34 +01:00
|
|
|
fn input_names(&self) -> Box<[&'static str]> {
|
|
|
|
new_vec![].into_boxed_slice()
|
|
|
|
}
|
2020-10-15 07:28:22 +02:00
|
|
|
fn input_types(&self) -> Box<[TypeId]> {
|
|
|
|
new_vec![].into_boxed_slice()
|
|
|
|
}
|
2020-11-22 15:15:17 +01:00
|
|
|
fn return_type(&self) -> &'static str {
|
|
|
|
"rhai::Dynamic"
|
|
|
|
}
|
2020-10-15 07:28:22 +02:00
|
|
|
}
|
|
|
|
pub fn token_callable() -> CallableFunction {
|
|
|
|
Token().into()
|
|
|
|
}
|
2020-11-22 10:21:34 +01:00
|
|
|
pub fn token_input_names() -> Box<[&'static str]> {
|
|
|
|
Token().input_names()
|
|
|
|
}
|
2020-10-15 07:28:22 +02:00
|
|
|
pub fn token_input_types() -> Box<[TypeId]> {
|
|
|
|
Token().input_types()
|
|
|
|
}
|
2020-11-22 15:15:17 +01:00
|
|
|
pub fn token_return_type() -> &'static str {
|
|
|
|
Token().return_type()
|
|
|
|
}
|
2020-10-15 07:28:22 +02:00
|
|
|
pub fn dynamic_result_fn() -> Result<Dynamic, Box<EvalAltResult> > {
|
2020-10-20 03:21:41 +02:00
|
|
|
Ok(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! {
|
|
|
|
impl PluginFunction for MyType {
|
2020-10-18 15:47:34 +02:00
|
|
|
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
|
2020-09-05 21:10:46 +02:00
|
|
|
debug_assert_eq!(args.len(), 1usize,
|
|
|
|
"wrong arg count: {} != {}", args.len(), 1usize);
|
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)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_method_call(&self) -> bool { false }
|
2020-09-30 16:55:40 +02:00
|
|
|
fn is_variadic(&self) -> bool { false }
|
2020-09-05 21:10:46 +02:00
|
|
|
fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(MyType()) }
|
2020-11-22 10:21:34 +01:00
|
|
|
fn input_names(&self) -> Box<[&'static str]> {
|
|
|
|
new_vec!["x: usize"].into_boxed_slice()
|
|
|
|
}
|
2020-09-05 21:10:46 +02:00
|
|
|
fn input_types(&self) -> Box<[TypeId]> {
|
|
|
|
new_vec![TypeId::of::<usize>()].into_boxed_slice()
|
|
|
|
}
|
2020-11-22 15:15:17 +01:00
|
|
|
fn return_type(&self) -> &'static str {
|
|
|
|
"()"
|
|
|
|
}
|
|
|
|
}
|
2020-09-05 21:10:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let item_fn = syn::parse2::<ExportedFn>(input_tokens).unwrap();
|
|
|
|
assert_streams_eq(item_fn.generate_impl("MyType"), expected_tokens);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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! {
|
|
|
|
#[allow(unused)]
|
|
|
|
pub mod rhai_fn_add_together {
|
|
|
|
use super::*;
|
|
|
|
struct Token();
|
|
|
|
impl PluginFunction for Token {
|
2020-10-18 15:47:34 +02:00
|
|
|
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
|
2020-09-05 21:10:46 +02:00
|
|
|
debug_assert_eq!(args.len(), 2usize,
|
|
|
|
"wrong arg count: {} != {}", args.len(), 2usize);
|
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)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_method_call(&self) -> bool { false }
|
2020-09-30 16:55:40 +02:00
|
|
|
fn is_variadic(&self) -> bool { false }
|
2020-09-05 21:10:46 +02:00
|
|
|
fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) }
|
2020-11-22 10:21:34 +01:00
|
|
|
fn input_names(&self) -> Box<[&'static str]> {
|
|
|
|
new_vec!["x: usize", "y: usize"].into_boxed_slice()
|
|
|
|
}
|
2020-09-05 21:10:46 +02:00
|
|
|
fn input_types(&self) -> Box<[TypeId]> {
|
|
|
|
new_vec![TypeId::of::<usize>(),
|
|
|
|
TypeId::of::<usize>()].into_boxed_slice()
|
|
|
|
}
|
2020-11-22 15:15:17 +01:00
|
|
|
fn return_type(&self) -> &'static str {
|
|
|
|
"usize"
|
|
|
|
}
|
2020-09-05 21:10:46 +02:00
|
|
|
}
|
|
|
|
pub fn token_callable() -> CallableFunction {
|
2020-09-29 07:06:48 +02:00
|
|
|
Token().into()
|
2020-09-05 21:10:46 +02:00
|
|
|
}
|
2020-11-22 10:21:34 +01:00
|
|
|
pub fn token_input_names() -> Box<[&'static str]> {
|
|
|
|
Token().input_names()
|
|
|
|
}
|
2020-09-05 21:10:46 +02:00
|
|
|
pub fn token_input_types() -> Box<[TypeId]> {
|
|
|
|
Token().input_types()
|
|
|
|
}
|
2020-11-22 15:15:17 +01:00
|
|
|
pub fn token_return_type() -> &'static str {
|
|
|
|
Token().return_type()
|
|
|
|
}
|
2020-10-15 07:28:22 +02:00
|
|
|
pub fn dynamic_result_fn(x: usize, y: usize) -> Result<Dynamic, Box<EvalAltResult> > {
|
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! {
|
|
|
|
#[allow(unused)]
|
|
|
|
pub mod rhai_fn_increment {
|
|
|
|
use super::*;
|
|
|
|
struct Token();
|
|
|
|
impl PluginFunction for Token {
|
2020-10-18 15:47:34 +02:00
|
|
|
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
|
2020-09-05 21:10:46 +02:00
|
|
|
debug_assert_eq!(args.len(), 2usize,
|
|
|
|
"wrong arg count: {} != {}", args.len(), 2usize);
|
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)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_method_call(&self) -> bool { true }
|
2020-09-30 16:55:40 +02:00
|
|
|
fn is_variadic(&self) -> bool { false }
|
2020-09-05 21:10:46 +02:00
|
|
|
fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) }
|
2020-11-22 10:21:34 +01:00
|
|
|
fn input_names(&self) -> Box<[&'static str]> {
|
|
|
|
new_vec!["x: &mut usize", "y: usize"].into_boxed_slice()
|
|
|
|
}
|
2020-09-05 21:10:46 +02:00
|
|
|
fn input_types(&self) -> Box<[TypeId]> {
|
|
|
|
new_vec![TypeId::of::<usize>(),
|
|
|
|
TypeId::of::<usize>()].into_boxed_slice()
|
|
|
|
}
|
2020-11-22 15:15:17 +01:00
|
|
|
fn return_type(&self) -> &'static str {
|
|
|
|
"()"
|
|
|
|
}
|
2020-09-05 21:10:46 +02:00
|
|
|
}
|
|
|
|
pub fn token_callable() -> CallableFunction {
|
2020-09-29 07:06:48 +02:00
|
|
|
Token().into()
|
2020-09-05 21:10:46 +02:00
|
|
|
}
|
2020-11-22 10:21:34 +01:00
|
|
|
pub fn token_input_names() -> Box<[&'static str]> {
|
|
|
|
Token().input_names()
|
|
|
|
}
|
2020-09-05 21:10:46 +02:00
|
|
|
pub fn token_input_types() -> Box<[TypeId]> {
|
|
|
|
Token().input_types()
|
|
|
|
}
|
2020-11-22 15:15:17 +01:00
|
|
|
pub fn token_return_type() -> &'static str {
|
|
|
|
Token().return_type()
|
|
|
|
}
|
2020-10-15 07:28:22 +02:00
|
|
|
pub fn dynamic_result_fn(x: &mut usize, y: usize) -> Result<Dynamic, Box<EvalAltResult> > {
|
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! {
|
|
|
|
#[allow(unused)]
|
|
|
|
pub mod rhai_fn_special_print {
|
|
|
|
use super::*;
|
|
|
|
struct Token();
|
|
|
|
impl PluginFunction for Token {
|
2020-10-18 15:47:34 +02:00
|
|
|
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
|
2020-09-05 21:10:46 +02:00
|
|
|
debug_assert_eq!(args.len(), 1usize,
|
|
|
|
"wrong arg count: {} != {}", args.len(), 1usize);
|
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)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_method_call(&self) -> bool { false }
|
2020-09-30 16:55:40 +02:00
|
|
|
fn is_variadic(&self) -> bool { false }
|
2020-09-05 21:10:46 +02:00
|
|
|
fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) }
|
2020-11-22 10:21:34 +01:00
|
|
|
fn input_names(&self) -> Box<[&'static str]> {
|
|
|
|
new_vec!["message: &str"].into_boxed_slice()
|
|
|
|
}
|
2020-09-05 21:10:46 +02:00
|
|
|
fn input_types(&self) -> Box<[TypeId]> {
|
|
|
|
new_vec![TypeId::of::<ImmutableString>()].into_boxed_slice()
|
|
|
|
}
|
2020-11-22 15:15:17 +01:00
|
|
|
fn return_type(&self) -> &'static str {
|
|
|
|
"()"
|
|
|
|
}
|
2020-09-05 21:10:46 +02:00
|
|
|
}
|
|
|
|
pub fn token_callable() -> CallableFunction {
|
2020-09-29 07:06:48 +02:00
|
|
|
Token().into()
|
2020-09-05 21:10:46 +02:00
|
|
|
}
|
2020-11-22 10:21:34 +01:00
|
|
|
pub fn token_input_names() -> Box<[&'static str]> {
|
|
|
|
Token().input_names()
|
|
|
|
}
|
2020-09-05 21:10:46 +02:00
|
|
|
pub fn token_input_types() -> Box<[TypeId]> {
|
|
|
|
Token().input_types()
|
|
|
|
}
|
2020-11-22 15:15:17 +01:00
|
|
|
pub fn token_return_type() -> &'static str {
|
|
|
|
Token().return_type()
|
|
|
|
}
|
2020-10-15 07:28:22 +02:00
|
|
|
pub fn dynamic_result_fn(message: &str) -> Result<Dynamic, Box<EvalAltResult> > {
|
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);
|
|
|
|
}
|
|
|
|
}
|