Move return type to param_names.

This commit is contained in:
Stephen Chung 2021-03-22 23:11:23 +08:00
parent 39fb78293c
commit 7a0032fc89
6 changed files with 391 additions and 566 deletions

View File

@ -591,9 +591,8 @@ impl ExportedFn {
syn::Ident::new(&format!("rhai_fn_{}", self.name()), self.name().span());
let impl_block = self.generate_impl("Token");
let callable_block = self.generate_callable("Token");
let input_names_block = self.generate_input_names("Token");
let param_names_block = self.generate_param_names("Token");
let input_types_block = self.generate_input_types("Token");
let return_type_block = self.generate_return_type("Token");
let dyn_result_fn_block = self.generate_dynamic_fn();
let vis = self.visibility;
quote! {
@ -603,10 +602,8 @@ impl ExportedFn {
struct Token();
#impl_block
#callable_block
#input_names_block
#param_names_block
#input_types_block
#return_type_block
#[allow(unused)]
#dyn_result_fn_block
}
}
@ -650,6 +647,8 @@ impl ExportedFn {
}
} else {
quote_spanned! { return_span =>
#[allow(unused)]
#[inline(always)]
pub #dynamic_signature {
Ok(Dynamic::from(#name(#(#arguments),*)))
}
@ -664,21 +663,23 @@ impl ExportedFn {
self.name().span(),
);
quote! {
#[inline(always)]
pub fn #callable_fn_name() -> CallableFunction {
#token_name().into()
}
}
}
pub fn generate_input_names(&self, on_type_name: &str) -> proc_macro2::TokenStream {
pub fn generate_param_names(&self, on_type_name: &str) -> proc_macro2::TokenStream {
let token_name: syn::Ident = syn::Ident::new(on_type_name, self.name().span());
let input_names_fn_name: syn::Ident = syn::Ident::new(
&format!("{}_input_names", on_type_name.to_lowercase()),
let param_names_fn_name: syn::Ident = syn::Ident::new(
&format!("{}_param_names", on_type_name.to_lowercase()),
self.name().span(),
);
quote! {
pub fn #input_names_fn_name() -> Box<[&'static str]> {
#token_name().input_names()
#[inline(always)]
pub fn #param_names_fn_name() -> Box<[&'static str]> {
#token_name().param_names()
}
}
}
@ -690,25 +691,13 @@ impl ExportedFn {
self.name().span(),
);
quote! {
#[inline(always)]
pub fn #input_types_fn_name() -> Box<[TypeId]> {
#token_name().input_types()
}
}
}
pub fn generate_return_type(&self, on_type_name: &str) -> proc_macro2::TokenStream {
let token_name: syn::Ident = syn::Ident::new(on_type_name, self.name().span());
let return_type_fn_name: syn::Ident = syn::Ident::new(
&format!("{}_return_type", on_type_name.to_lowercase()),
self.name().span(),
);
quote! {
pub fn #return_type_fn_name() -> &'static str {
#token_name().return_type()
}
}
}
pub fn generate_impl(&self, on_type_name: &str) -> proc_macro2::TokenStream {
let sig_name = self.name().clone();
let arg_count = self.arg_count();
@ -885,24 +874,22 @@ impl ExportedFn {
let type_name = syn::Ident::new(on_type_name, proc_macro2::Span::call_site());
quote! {
impl PluginFunction for #type_name {
#[inline(always)]
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), #arg_count, "wrong arg count: {} != {}", args.len(), #arg_count);
#(#unpack_statements)*
#return_expr
}
fn is_method_call(&self) -> bool { #is_method_call }
fn is_variadic(&self) -> bool { false }
fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(#type_name()) }
fn input_names(&self) -> Box<[&'static str]> {
new_vec![#(#input_type_names),*].into_boxed_slice()
#[inline(always)] fn is_method_call(&self) -> bool { #is_method_call }
#[inline(always)] fn is_variadic(&self) -> bool { false }
#[inline(always)] fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(#type_name()) }
#[inline(always)] fn param_names(&self) -> Box<[&'static str]> {
new_vec![#(#input_type_names,)* #return_type].into_boxed_slice()
}
fn input_types(&self) -> Box<[TypeId]> {
#[inline(always)] fn input_types(&self) -> Box<[TypeId]> {
new_vec![#(#input_type_exprs),*].into_boxed_slice()
}
fn return_type(&self) -> &'static str {
#return_type
}
}
}
}

View File

@ -340,7 +340,7 @@ pub fn set_exported_fn(args: proc_macro::TokenStream) -> proc_macro::TokenStream
let gen_mod_path = crate::register::generated_module_path(&rust_mod_path);
let tokens = quote! {
#module_expr.set_fn(#export_name, FnNamespace::Internal, FnAccess::Public,
Some(#gen_mod_path::token_input_names().as_ref()),
Some(#gen_mod_path::token_param_names().as_ref()),
#gen_mod_path::token_input_types().as_ref(),
#gen_mod_path::token_callable());
};
@ -382,7 +382,7 @@ pub fn set_exported_global_fn(args: proc_macro::TokenStream) -> proc_macro::Toke
let gen_mod_path = crate::register::generated_module_path(&rust_mod_path);
let tokens = quote! {
#module_expr.set_fn(#export_name, FnNamespace::Global, FnAccess::Public,
Some(#gen_mod_path::token_input_names().as_ref()),
Some(#gen_mod_path::token_param_names().as_ref()),
#gen_mod_path::token_input_types().as_ref(),
#gen_mod_path::token_callable());
};

View File

@ -185,9 +185,8 @@ pub fn generate_body(
});
gen_fn_tokens.push(function.generate_impl(&fn_token_name.to_string()));
gen_fn_tokens.push(function.generate_callable(&fn_token_name.to_string()));
gen_fn_tokens.push(function.generate_input_names(&fn_token_name.to_string()));
gen_fn_tokens.push(function.generate_param_names(&fn_token_name.to_string()));
gen_fn_tokens.push(function.generate_input_types(&fn_token_name.to_string()));
gen_fn_tokens.push(function.generate_return_type(&fn_token_name.to_string()));
}
let mut generate_fn_call = syn::parse2::<syn::ItemMod>(quote! {

View File

@ -277,39 +277,33 @@ mod generate_tests {
use super::*;
struct Token();
impl PluginFunction for Token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
#[inline(always)] fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 0usize,
"wrong arg count: {} != {}", args.len(), 0usize);
Ok(Dynamic::from(do_nothing()))
}
fn is_method_call(&self) -> bool { false }
fn is_variadic(&self) -> bool { false }
fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) }
fn input_names(&self) -> Box<[&'static str]> {
new_vec![].into_boxed_slice()
#[inline(always)] fn is_method_call(&self) -> bool { false }
#[inline(always)] fn is_variadic(&self) -> bool { false }
#[inline(always)] fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) }
#[inline(always)] fn param_names(&self) -> Box<[&'static str]> {
new_vec!["()"].into_boxed_slice()
}
fn input_types(&self) -> Box<[TypeId]> {
#[inline(always)] fn input_types(&self) -> Box<[TypeId]> {
new_vec![].into_boxed_slice()
}
fn return_type(&self) -> &'static str {
"()"
}
}
pub fn token_callable() -> CallableFunction {
#[inline(always)] pub fn token_callable() -> CallableFunction {
Token().into()
}
pub fn token_input_names() -> Box<[&'static str]> {
Token().input_names()
#[inline(always)] pub fn token_param_names() -> Box<[&'static str]> {
Token().param_names()
}
pub fn token_input_types() -> Box<[TypeId]> {
#[inline(always)] pub fn token_input_types() -> Box<[TypeId]> {
Token().input_types()
}
pub fn token_return_type() -> &'static str {
Token().return_type()
}
#[allow(unused)]
pub fn dynamic_result_fn() -> Result<Dynamic, Box<EvalAltResult> > {
#[inline(always)] pub fn dynamic_result_fn() -> Result<Dynamic, Box<EvalAltResult> > {
Ok(Dynamic::from(do_nothing()))
}
}
@ -331,6 +325,7 @@ mod generate_tests {
use super::*;
struct Token();
impl PluginFunction for Token {
#[inline(always)]
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 1usize,
"wrong arg count: {} != {}", args.len(), 1usize);
@ -338,33 +333,27 @@ mod generate_tests {
Ok(Dynamic::from(do_something(arg0)))
}
fn is_method_call(&self) -> bool { false }
fn is_variadic(&self) -> bool { false }
fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) }
fn input_names(&self) -> Box<[&'static str]> {
new_vec!["x: usize"].into_boxed_slice()
#[inline(always)] fn is_method_call(&self) -> bool { false }
#[inline(always)] fn is_variadic(&self) -> bool { false }
#[inline(always)] fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) }
#[inline(always)] fn param_names(&self) -> Box<[&'static str]> {
new_vec!["x: usize", "()"].into_boxed_slice()
}
fn input_types(&self) -> Box<[TypeId]> {
#[inline(always)] fn input_types(&self) -> Box<[TypeId]> {
new_vec![TypeId::of::<usize>()].into_boxed_slice()
}
fn return_type(&self) -> &'static str {
"()"
}
}
pub fn token_callable() -> CallableFunction {
#[inline(always)] pub fn token_callable() -> CallableFunction {
Token().into()
}
pub fn token_input_names() -> Box<[&'static str]> {
Token().input_names()
#[inline(always)] pub fn token_param_names() -> Box<[&'static str]> {
Token().param_names()
}
pub fn token_input_types() -> Box<[TypeId]> {
#[inline(always)] pub fn token_input_types() -> Box<[TypeId]> {
Token().input_types()
}
pub fn token_return_type() -> &'static str {
Token().return_type()
}
#[allow(unused)]
pub fn dynamic_result_fn(x: usize) -> Result<Dynamic, Box<EvalAltResult> > {
#[inline(always)] pub fn dynamic_result_fn(x: usize) -> Result<Dynamic, Box<EvalAltResult> > {
Ok(Dynamic::from(do_something(x)))
}
}
@ -386,6 +375,7 @@ mod generate_tests {
use super::*;
struct Token();
impl PluginFunction for Token {
#[inline(always)]
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 1usize,
"wrong arg count: {} != {}", args.len(), 1usize);
@ -393,33 +383,27 @@ mod generate_tests {
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()) }
fn input_names(&self) -> Box<[&'static str]> {
new_vec!["x: usize"].into_boxed_slice()
#[inline(always)] fn is_method_call(&self) -> bool { false }
#[inline(always)] fn is_variadic(&self) -> bool { false }
#[inline(always)] fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) }
#[inline(always)] fn param_names(&self) -> Box<[&'static str]> {
new_vec!["x: usize", "()"].into_boxed_slice()
}
fn input_types(&self) -> Box<[TypeId]> {
#[inline(always)] fn input_types(&self) -> Box<[TypeId]> {
new_vec![TypeId::of::<usize>()].into_boxed_slice()
}
fn return_type(&self) -> &'static str {
"()"
}
}
pub fn token_callable() -> CallableFunction {
#[inline(always)] pub fn token_callable() -> CallableFunction {
Token().into()
}
pub fn token_input_names() -> Box<[&'static str]> {
Token().input_names()
#[inline(always)] pub fn token_param_names() -> Box<[&'static str]> {
Token().param_names()
}
pub fn token_input_types() -> Box<[TypeId]> {
#[inline(always)] pub fn token_input_types() -> Box<[TypeId]> {
Token().input_types()
}
pub fn token_return_type() -> &'static str {
Token().return_type()
}
#[allow(unused)]
pub fn dynamic_result_fn(context: NativeCallContext, x: usize) -> Result<Dynamic, Box<EvalAltResult> > {
#[inline(always)] pub fn dynamic_result_fn(context: NativeCallContext, x: usize) -> Result<Dynamic, Box<EvalAltResult> > {
Ok(Dynamic::from(do_something(context, x)))
}
}
@ -444,39 +428,34 @@ mod generate_tests {
use super::*;
struct Token();
impl PluginFunction for Token {
#[inline(always)]
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 0usize,
"wrong arg count: {} != {}", args.len(), 0usize);
Ok(Dynamic::from(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()) }
fn input_names(&self) -> Box<[&'static str]> {
new_vec![].into_boxed_slice()
#[inline(always)] fn is_method_call(&self) -> bool { false }
#[inline(always)] fn is_variadic(&self) -> bool { false }
#[inline(always)] fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) }
#[inline(always)] fn param_names(&self) -> Box<[&'static str]> {
new_vec!["rhai::Dynamic"].into_boxed_slice()
}
fn input_types(&self) -> Box<[TypeId]> {
#[inline(always)] fn input_types(&self) -> Box<[TypeId]> {
new_vec![].into_boxed_slice()
}
fn return_type(&self) -> &'static str {
"rhai::Dynamic"
}
}
pub fn token_callable() -> CallableFunction {
#[inline(always)] pub fn token_callable() -> CallableFunction {
Token().into()
}
pub fn token_input_names() -> Box<[&'static str]> {
Token().input_names()
#[inline(always)] pub fn token_param_names() -> Box<[&'static str]> {
Token().param_names()
}
pub fn token_input_types() -> Box<[TypeId]> {
#[inline(always)] pub fn token_input_types() -> Box<[TypeId]> {
Token().input_types()
}
pub fn token_return_type() -> &'static str {
Token().return_type()
}
#[allow(unused)]
pub fn dynamic_result_fn() -> Result<Dynamic, Box<EvalAltResult> > {
#[inline(always)] pub fn dynamic_result_fn() -> Result<Dynamic, Box<EvalAltResult> > {
Ok(Dynamic::from(return_dynamic()))
}
}
@ -494,6 +473,7 @@ mod generate_tests {
let expected_tokens = quote! {
impl PluginFunction for TestStruct {
#[inline(always)]
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 1usize,
"wrong arg count: {} != {}", args.len(), 1usize);
@ -501,18 +481,15 @@ mod generate_tests {
Ok(Dynamic::from(do_something(arg0)))
}
fn is_method_call(&self) -> bool { false }
fn is_variadic(&self) -> bool { false }
fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(TestStruct()) }
fn input_names(&self) -> Box<[&'static str]> {
new_vec!["x: usize"].into_boxed_slice()
#[inline(always)] fn is_method_call(&self) -> bool { false }
#[inline(always)] fn is_variadic(&self) -> bool { false }
#[inline(always)] fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(TestStruct()) }
#[inline(always)] fn param_names(&self) -> Box<[&'static str]> {
new_vec!["x: usize", "()"].into_boxed_slice()
}
fn input_types(&self) -> Box<[TypeId]> {
#[inline(always)] fn input_types(&self) -> Box<[TypeId]> {
new_vec![TypeId::of::<usize>()].into_boxed_slice()
}
fn return_type(&self) -> &'static str {
"()"
}
}
};
@ -532,6 +509,7 @@ mod generate_tests {
use super::*;
struct Token();
impl PluginFunction for Token {
#[inline(always)]
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 2usize,
"wrong arg count: {} != {}", args.len(), 2usize);
@ -540,34 +518,28 @@ mod generate_tests {
Ok(Dynamic::from(add_together(arg0, arg1)))
}
fn is_method_call(&self) -> bool { false }
fn is_variadic(&self) -> bool { false }
fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) }
fn input_names(&self) -> Box<[&'static str]> {
new_vec!["x: usize", "y: usize"].into_boxed_slice()
#[inline(always)] fn is_method_call(&self) -> bool { false }
#[inline(always)] fn is_variadic(&self) -> bool { false }
#[inline(always)] fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) }
#[inline(always)] fn param_names(&self) -> Box<[&'static str]> {
new_vec!["x: usize", "y: usize", "usize"].into_boxed_slice()
}
fn input_types(&self) -> Box<[TypeId]> {
#[inline(always)] fn input_types(&self) -> Box<[TypeId]> {
new_vec![TypeId::of::<usize>(),
TypeId::of::<usize>()].into_boxed_slice()
}
fn return_type(&self) -> &'static str {
"usize"
}
}
pub fn token_callable() -> CallableFunction {
#[inline(always)] pub fn token_callable() -> CallableFunction {
Token().into()
}
pub fn token_input_names() -> Box<[&'static str]> {
Token().input_names()
#[inline(always)] pub fn token_param_names() -> Box<[&'static str]> {
Token().param_names()
}
pub fn token_input_types() -> Box<[TypeId]> {
#[inline(always)] pub fn token_input_types() -> Box<[TypeId]> {
Token().input_types()
}
pub fn token_return_type() -> &'static str {
Token().return_type()
}
#[allow(unused)]
pub fn dynamic_result_fn(x: usize, y: usize) -> Result<Dynamic, Box<EvalAltResult> > {
#[inline(always)] pub fn dynamic_result_fn(x: usize, y: usize) -> Result<Dynamic, Box<EvalAltResult> > {
Ok(Dynamic::from(add_together(x, y)))
}
}
@ -589,6 +561,7 @@ mod generate_tests {
use super::*;
struct Token();
impl PluginFunction for Token {
#[inline(always)]
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 2usize,
"wrong arg count: {} != {}", args.len(), 2usize);
@ -602,34 +575,28 @@ mod generate_tests {
Ok(Dynamic::from(increment(arg0, arg1)))
}
fn is_method_call(&self) -> bool { true }
fn is_variadic(&self) -> bool { false }
fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) }
fn input_names(&self) -> Box<[&'static str]> {
new_vec!["x: &mut usize", "y: usize"].into_boxed_slice()
#[inline(always)] fn is_method_call(&self) -> bool { true }
#[inline(always)] fn is_variadic(&self) -> bool { false }
#[inline(always)] fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) }
#[inline(always)] fn param_names(&self) -> Box<[&'static str]> {
new_vec!["x: &mut usize", "y: usize", "()"].into_boxed_slice()
}
fn input_types(&self) -> Box<[TypeId]> {
#[inline(always)] fn input_types(&self) -> Box<[TypeId]> {
new_vec![TypeId::of::<usize>(),
TypeId::of::<usize>()].into_boxed_slice()
}
fn return_type(&self) -> &'static str {
"()"
}
}
pub fn token_callable() -> CallableFunction {
#[inline(always)] pub fn token_callable() -> CallableFunction {
Token().into()
}
pub fn token_input_names() -> Box<[&'static str]> {
Token().input_names()
#[inline(always)] pub fn token_param_names() -> Box<[&'static str]> {
Token().param_names()
}
pub fn token_input_types() -> Box<[TypeId]> {
#[inline(always)] pub fn token_input_types() -> Box<[TypeId]> {
Token().input_types()
}
pub fn token_return_type() -> &'static str {
Token().return_type()
}
#[allow(unused)]
pub fn dynamic_result_fn(x: &mut usize, y: usize) -> Result<Dynamic, Box<EvalAltResult> > {
#[inline(always)] pub fn dynamic_result_fn(x: &mut usize, y: usize) -> Result<Dynamic, Box<EvalAltResult> > {
Ok(Dynamic::from(increment(x, y)))
}
}
@ -652,6 +619,7 @@ mod generate_tests {
use super::*;
struct Token();
impl PluginFunction for Token {
#[inline(always)]
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 1usize,
"wrong arg count: {} != {}", args.len(), 1usize);
@ -659,33 +627,27 @@ mod generate_tests {
Ok(Dynamic::from(special_print(&arg0)))
}
fn is_method_call(&self) -> bool { false }
fn is_variadic(&self) -> bool { false }
fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) }
fn input_names(&self) -> Box<[&'static str]> {
new_vec!["message: &str"].into_boxed_slice()
#[inline(always)] fn is_method_call(&self) -> bool { false }
#[inline(always)] fn is_variadic(&self) -> bool { false }
#[inline(always)] fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) }
#[inline(always)] fn param_names(&self) -> Box<[&'static str]> {
new_vec!["message: &str", "()"].into_boxed_slice()
}
fn input_types(&self) -> Box<[TypeId]> {
#[inline(always)] fn input_types(&self) -> Box<[TypeId]> {
new_vec![TypeId::of::<ImmutableString>()].into_boxed_slice()
}
fn return_type(&self) -> &'static str {
"()"
}
}
pub fn token_callable() -> CallableFunction {
#[inline(always)] pub fn token_callable() -> CallableFunction {
Token().into()
}
pub fn token_input_names() -> Box<[&'static str]> {
Token().input_names()
#[inline(always)] pub fn token_param_names() -> Box<[&'static str]> {
Token().param_names()
}
pub fn token_input_types() -> Box<[TypeId]> {
#[inline(always)] pub fn token_input_types() -> Box<[TypeId]> {
Token().input_types()
}
pub fn token_return_type() -> &'static str {
Token().return_type()
}
#[allow(unused)]
pub fn dynamic_result_fn(message: &str) -> Result<Dynamic, Box<EvalAltResult> > {
#[inline(always)] pub fn dynamic_result_fn(message: &str) -> Result<Dynamic, Box<EvalAltResult> > {
Ok(Dynamic::from(special_print(message)))
}
}

File diff suppressed because it is too large Load Diff

View File

@ -30,12 +30,9 @@ pub trait PluginFunction {
/// Convert a plugin function into a boxed trait object.
fn clone_boxed(&self) -> Box<dyn PluginFunction>;
/// Return a boxed slice of the names of the function's parameters.
fn input_names(&self) -> Box<[&'static str]>;
/// Return a boxed slice of the names of the function's parameters and return type.
fn param_names(&self) -> Box<[&'static str]>;
/// Return a boxed slice of type ID's of the function's parameters.
fn input_types(&self) -> Box<[TypeId]>;
/// Return a string slice of the function's return type.
fn return_type(&self) -> &'static str;
}