diff --git a/codegen/src/lib.rs b/codegen/src/lib.rs index 4ab0f628..d985757b 100644 --- a/codegen/src/lib.rs +++ b/codegen/src/lib.rs @@ -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(), diff --git a/codegen/src/rhai_module.rs b/codegen/src/rhai_module.rs index c21384aa..1fafd99e 100644 --- a/codegen/src/rhai_module.rs +++ b/codegen/src/rhai_module.rs @@ -164,8 +164,8 @@ pub fn generate_body( set_fn_statements.push( syn::parse2::(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(), ); diff --git a/codegen/src/test/module.rs b/codegen/src/test/module.rs index 22f75fdf..fdf4cf12 100644 --- a/codegen/src/test/module.rs +++ b/codegen/src/test/module.rs @@ -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::()], - 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::()], - 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::()], - 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::(), TypeId::of::()], - 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::(), TypeId::of::()], - 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::(), TypeId::of::()], - 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::(), TypeId::of::()], - 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::(), TypeId::of::()], - 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::()], - 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::()], - 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::(), TypeId::of::()], - 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::()], - 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::()], - 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::()], - 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::()], - 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::()], - 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::()], - 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::(), TypeId::of::()], - 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::(), TypeId::of::()], - 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::(), TypeId::of::()], - 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::(), TypeId::of::()], - 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::(), TypeId::of::()], - 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::(), TypeId::of::()], - 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::(), TypeId::of::(), TypeId::of::()], - 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::(), TypeId::of::(), TypeId::of::()], - 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::(), TypeId::of::(), TypeId::of::()], - set_by_index_token()); + set_by_index_token().into()); if flatten {} else {} } #[allow(non_camel_case_types)] diff --git a/src/module/mod.rs b/src/module/mod.rs index d10fa983..f3cf1812 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -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 + Into, - 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, ¶m_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.