Reverse Module::set_plugin_fn.

This commit is contained in:
Stephen Chung 2021-03-26 18:41:28 +08:00
parent 33f88dc813
commit 0afc2a0842
4 changed files with 66 additions and 121 deletions

View File

@ -339,10 +339,10 @@ pub fn set_exported_fn(args: proc_macro::TokenStream) -> proc_macro::TokenStream
let param_names = quote! { None };
proc_macro::TokenStream::from(quote! {
#module_expr.set_plugin_fn(#export_name, FnNamespace::Internal, FnAccess::Public,
#param_names,
&#gen_mod_path::Token::param_types(),
#gen_mod_path::Token());
#module_expr.set_fn(#export_name, FnNamespace::Internal, FnAccess::Public,
#param_names,
&#gen_mod_path::Token::param_types(),
#gen_mod_path::Token().into());
})
}
Err(e) => e.to_compile_error().into(),
@ -388,10 +388,10 @@ pub fn set_exported_global_fn(args: proc_macro::TokenStream) -> proc_macro::Toke
let param_names = quote! { None };
proc_macro::TokenStream::from(quote! {
#module_expr.set_plugin_fn(#export_name, FnNamespace::Global, FnAccess::Public,
#param_names,
&#gen_mod_path::Token::param_types(),
#gen_mod_path::Token());
#module_expr.set_fn(#export_name, FnNamespace::Global, FnAccess::Public,
#param_names,
&#gen_mod_path::Token::param_types(),
#gen_mod_path::Token().into());
})
}
Err(e) => e.to_compile_error().into(),

View File

@ -164,8 +164,8 @@ pub fn generate_body(
set_fn_statements.push(
syn::parse2::<syn::Stmt>(quote! {
m.set_plugin_fn(#fn_literal, FnNamespace::#ns_str, FnAccess::Public,
#param_names, &[#(#fn_input_types),*], #fn_token_name());
m.set_fn(#fn_literal, FnNamespace::#ns_str, FnAccess::Public,
#param_names, &[#(#fn_input_types),*], #fn_token_name().into());
})
.unwrap(),
);

View File

@ -297,9 +297,9 @@ mod generate_tests {
}
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_plugin_fn("get_mystic_number", FnNamespace::Internal, FnAccess::Public,
m.set_fn("get_mystic_number", FnNamespace::Internal, FnAccess::Public,
Some(get_mystic_number_token::PARAM_NAMES), &[],
get_mystic_number_token());
get_mystic_number_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -351,9 +351,9 @@ mod generate_tests {
}
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_plugin_fn("add_one_to", FnNamespace::Global, FnAccess::Public,
m.set_fn("add_one_to", FnNamespace::Global, FnAccess::Public,
Some(add_one_to_token::PARAM_NAMES), &[TypeId::of::<INT>()],
add_one_to_token());
add_one_to_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -405,9 +405,9 @@ mod generate_tests {
}
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_plugin_fn("add_one_to", FnNamespace::Internal, FnAccess::Public, Some(add_one_to_token::PARAM_NAMES),
m.set_fn("add_one_to", FnNamespace::Internal, FnAccess::Public, Some(add_one_to_token::PARAM_NAMES),
&[TypeId::of::<INT>()],
add_one_to_token());
add_one_to_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -470,12 +470,12 @@ mod generate_tests {
}
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_plugin_fn("add_n", FnNamespace::Internal, FnAccess::Public, Some(add_one_to_token::PARAM_NAMES),
m.set_fn("add_n", FnNamespace::Internal, FnAccess::Public, Some(add_one_to_token::PARAM_NAMES),
&[TypeId::of::<INT>()],
add_one_to_token());
m.set_plugin_fn("add_n", FnNamespace::Internal, FnAccess::Public, Some(add_n_to_token::PARAM_NAMES),
add_one_to_token().into());
m.set_fn("add_n", FnNamespace::Internal, FnAccess::Public, Some(add_n_to_token::PARAM_NAMES),
&[TypeId::of::<INT>(), TypeId::of::<INT>()],
add_n_to_token());
add_n_to_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -545,9 +545,9 @@ mod generate_tests {
}
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_plugin_fn("add_together", FnNamespace::Internal, FnAccess::Public, Some(add_together_token::PARAM_NAMES),
m.set_fn("add_together", FnNamespace::Internal, FnAccess::Public, Some(add_together_token::PARAM_NAMES),
&[TypeId::of::<INT>(), TypeId::of::<INT>()],
add_together_token());
add_together_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -601,15 +601,15 @@ mod generate_tests {
}
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_plugin_fn("add", FnNamespace::Internal, FnAccess::Public, Some(add_together_token::PARAM_NAMES),
m.set_fn("add", FnNamespace::Internal, FnAccess::Public, Some(add_together_token::PARAM_NAMES),
&[TypeId::of::<INT>(), TypeId::of::<INT>()],
add_together_token());
m.set_plugin_fn("+", FnNamespace::Internal, FnAccess::Public, Some(add_together_token::PARAM_NAMES),
add_together_token().into());
m.set_fn("+", FnNamespace::Internal, FnAccess::Public, Some(add_together_token::PARAM_NAMES),
&[TypeId::of::<INT>(), TypeId::of::<INT>()],
add_together_token());
m.set_plugin_fn("add_together", FnNamespace::Internal, FnAccess::Public, Some(add_together_token::PARAM_NAMES),
add_together_token().into());
m.set_fn("add_together", FnNamespace::Internal, FnAccess::Public, Some(add_together_token::PARAM_NAMES),
&[TypeId::of::<INT>(), TypeId::of::<INT>()],
add_together_token());
add_together_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -844,9 +844,9 @@ mod generate_tests {
}
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_plugin_fn("get_mystic_number", FnNamespace::Internal, FnAccess::Public,
m.set_fn("get_mystic_number", FnNamespace::Internal, FnAccess::Public,
Some(get_mystic_number_token::PARAM_NAMES), &[],
get_mystic_number_token());
get_mystic_number_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -928,9 +928,9 @@ mod generate_tests {
}
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_plugin_fn("print_out_to", FnNamespace::Internal, FnAccess::Public, Some(print_out_to_token::PARAM_NAMES),
m.set_fn("print_out_to", FnNamespace::Internal, FnAccess::Public, Some(print_out_to_token::PARAM_NAMES),
&[TypeId::of::<ImmutableString>()],
print_out_to_token());
print_out_to_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -982,9 +982,9 @@ mod generate_tests {
}
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_plugin_fn("print_out_to", FnNamespace::Internal, FnAccess::Public, Some(print_out_to_token::PARAM_NAMES),
m.set_fn("print_out_to", FnNamespace::Internal, FnAccess::Public, Some(print_out_to_token::PARAM_NAMES),
&[TypeId::of::<ImmutableString>()],
print_out_to_token());
print_out_to_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -1037,9 +1037,9 @@ mod generate_tests {
}
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_plugin_fn("foo", FnNamespace::Internal, FnAccess::Public, Some(foo_token::PARAM_NAMES),
m.set_fn("foo", FnNamespace::Internal, FnAccess::Public, Some(foo_token::PARAM_NAMES),
&[TypeId::of::<FLOAT>(), TypeId::of::<INT>()],
foo_token());
foo_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -1092,9 +1092,9 @@ mod generate_tests {
}
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_plugin_fn("increment", FnNamespace::Internal, FnAccess::Public, Some(increment_token::PARAM_NAMES),
m.set_fn("increment", FnNamespace::Internal, FnAccess::Public, Some(increment_token::PARAM_NAMES),
&[TypeId::of::<FLOAT>()],
increment_token());
increment_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -1154,9 +1154,9 @@ mod generate_tests {
}
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_plugin_fn("increment", FnNamespace::Internal, FnAccess::Public, Some(increment_token::PARAM_NAMES),
m.set_fn("increment", FnNamespace::Internal, FnAccess::Public, Some(increment_token::PARAM_NAMES),
&[TypeId::of::<FLOAT>()],
increment_token());
increment_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -1237,9 +1237,9 @@ mod generate_tests {
}
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_plugin_fn("increment", FnNamespace::Internal, FnAccess::Public, Some(increment_token::PARAM_NAMES),
m.set_fn("increment", FnNamespace::Internal, FnAccess::Public, Some(increment_token::PARAM_NAMES),
&[TypeId::of::<FLOAT>()],
increment_token());
increment_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -1319,9 +1319,9 @@ mod generate_tests {
}
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_plugin_fn("get$square", FnNamespace::Global, FnAccess::Public, Some(int_foo_token::PARAM_NAMES),
m.set_fn("get$square", FnNamespace::Global, FnAccess::Public, Some(int_foo_token::PARAM_NAMES),
&[TypeId::of::<u64>()],
int_foo_token());
int_foo_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -1379,12 +1379,12 @@ mod generate_tests {
}
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_plugin_fn("square", FnNamespace::Internal, FnAccess::Public, Some(int_foo_token::PARAM_NAMES),
m.set_fn("square", FnNamespace::Internal, FnAccess::Public, Some(int_foo_token::PARAM_NAMES),
&[TypeId::of::<u64>()],
int_foo_token());
m.set_plugin_fn("get$square", FnNamespace::Global, FnAccess::Public, Some(int_foo_token::PARAM_NAMES),
int_foo_token().into());
m.set_fn("get$square", FnNamespace::Global, FnAccess::Public, Some(int_foo_token::PARAM_NAMES),
&[TypeId::of::<u64>()],
int_foo_token());
int_foo_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -1442,9 +1442,9 @@ mod generate_tests {
}
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_plugin_fn("set$squared", FnNamespace::Global, FnAccess::Public, Some(int_foo_token::PARAM_NAMES),
m.set_fn("set$squared", FnNamespace::Global, FnAccess::Public, Some(int_foo_token::PARAM_NAMES),
&[TypeId::of::<u64>(), TypeId::of::<u64>()],
int_foo_token());
int_foo_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -1503,12 +1503,12 @@ mod generate_tests {
}
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_plugin_fn("set_sq", FnNamespace::Internal, FnAccess::Public, Some(int_foo_token::PARAM_NAMES),
m.set_fn("set_sq", FnNamespace::Internal, FnAccess::Public, Some(int_foo_token::PARAM_NAMES),
&[TypeId::of::<u64>(), TypeId::of::<u64>()],
int_foo_token());
m.set_plugin_fn("set$squared", FnNamespace::Global, FnAccess::Public, Some(int_foo_token::PARAM_NAMES),
int_foo_token().into());
m.set_fn("set$squared", FnNamespace::Global, FnAccess::Public, Some(int_foo_token::PARAM_NAMES),
&[TypeId::of::<u64>(), TypeId::of::<u64>()],
int_foo_token());
int_foo_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -1567,9 +1567,9 @@ mod generate_tests {
}
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_plugin_fn("index$get$", FnNamespace::Global, FnAccess::Public, Some(get_by_index_token::PARAM_NAMES),
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());
get_by_index_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -1628,12 +1628,12 @@ mod generate_tests {
}
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_plugin_fn("get", FnNamespace::Internal, FnAccess::Public, Some(get_by_index_token::PARAM_NAMES),
m.set_fn("get", FnNamespace::Internal, FnAccess::Public, Some(get_by_index_token::PARAM_NAMES),
&[TypeId::of::<MyCollection>(), TypeId::of::<u64>()],
get_by_index_token());
m.set_plugin_fn("index$get$", FnNamespace::Global, FnAccess::Public, Some(get_by_index_token::PARAM_NAMES),
get_by_index_token().into());
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());
get_by_index_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -1692,9 +1692,9 @@ mod generate_tests {
}
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_plugin_fn("index$set$", FnNamespace::Global, FnAccess::Public, Some(set_by_index_token::PARAM_NAMES),
m.set_fn("index$set$", FnNamespace::Global, FnAccess::Public, Some(set_by_index_token::PARAM_NAMES),
&[TypeId::of::<MyCollection>(), TypeId::of::<u64>(), TypeId::of::<FLOAT>()],
set_by_index_token());
set_by_index_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -1754,12 +1754,12 @@ mod generate_tests {
}
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_plugin_fn("set", FnNamespace::Internal, FnAccess::Public, Some(set_by_index_token::PARAM_NAMES),
m.set_fn("set", FnNamespace::Internal, FnAccess::Public, Some(set_by_index_token::PARAM_NAMES),
&[TypeId::of::<MyCollection>(), TypeId::of::<u64>(), TypeId::of::<FLOAT>()],
set_by_index_token());
m.set_plugin_fn("index$set$", FnNamespace::Global, FnAccess::Public, Some(set_by_index_token::PARAM_NAMES),
set_by_index_token().into());
m.set_fn("index$set$", FnNamespace::Global, FnAccess::Public, Some(set_by_index_token::PARAM_NAMES),
&[TypeId::of::<MyCollection>(), TypeId::of::<u64>(), TypeId::of::<FLOAT>()],
set_by_index_token());
set_by_index_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]

View File

@ -718,61 +718,6 @@ impl Module {
hash_fn
}
/// Set a plugin function into the [`Module`], returning a hash key.
///
/// If there is an existing Rust function of the same hash, it is replaced.
///
/// # WARNING - Low Level API
///
/// This function is very low level. It is intended for plugins system use only.
#[inline(always)]
pub fn set_plugin_fn(
&mut self,
name: impl AsRef<str> + Into<ImmutableString>,
namespace: FnNamespace,
access: FnAccess,
_arg_names: Option<&[&str]>,
arg_types: &[TypeId],
func: impl PluginFunction + SendSync + 'static,
) -> u64 {
let is_method = func.is_method_call();
let param_types: StaticVec<_> = arg_types
.iter()
.cloned()
.enumerate()
.map(|(i, type_id)| Self::map_type(!is_method || i > 0, type_id))
.collect();
#[cfg(feature = "metadata")]
let param_names = _arg_names
.iter()
.flat_map(|p| p.iter())
.map(|&arg| self.interned_strings.get(arg))
.collect();
let hash_fn = calc_native_fn_hash(empty(), &name, &param_types);
self.functions.insert(
hash_fn,
Box::new(FuncInfo {
name: self.interned_strings.get(name),
namespace,
access,
params: param_types.len(),
param_types,
#[cfg(feature = "metadata")]
param_names,
func: func.into(),
}),
);
self.indexed = false;
self.contains_indexed_global_functions = false;
hash_fn
}
/// Set a Rust function taking a reference to the scripting [`Engine`][crate::Engine],
/// the current set of functions, plus a list of mutable [`Dynamic`] references
/// into the [`Module`], returning a hash key.