Allow #[cfg(...)] in plugin functions.

This commit is contained in:
Stephen Chung
2021-10-20 15:30:11 +08:00
parent 0265af415d
commit 3f2dd23e6e
8 changed files with 141 additions and 38 deletions

View File

@@ -281,6 +281,7 @@ pub struct ExportedFn {
pass_context: bool,
mut_receiver: bool,
params: ExportedFnParams,
cfg_attrs: Vec<syn::Attribute>,
}
impl Parse for ExportedFn {
@@ -294,8 +295,7 @@ impl Parse for ExportedFn {
syn::parse2::<syn::Path>(quote! { rhai::NativeCallContext }).unwrap();
let mut pass_context = false;
// #[cfg] attributes are not allowed on functions due to what is generated for them
crate::attrs::deny_cfg_attr(&fn_all.attrs)?;
let cfg_attrs = crate::attrs::collect_cfg_attr(&fn_all.attrs);
let visibility = fn_all.vis;
@@ -403,6 +403,7 @@ impl Parse for ExportedFn {
pass_context,
mut_receiver,
params: Default::default(),
cfg_attrs,
})
}
}
@@ -414,6 +415,10 @@ impl ExportedFn {
&self.params
}
pub fn cfg_attrs(&self) -> &[syn::Attribute] {
&self.cfg_attrs
}
pub fn update_scope(&mut self, parent_scope: &ExportScope) {
let keep = match (self.params.skip, parent_scope) {
(true, _) => false,
@@ -498,6 +503,10 @@ impl ExportedFn {
}
}
pub fn set_cfg_attrs(&mut self, cfg_attrs: Vec<syn::Attribute>) {
self.cfg_attrs = cfg_attrs
}
pub fn set_params(&mut self, mut params: ExportedFnParams) -> syn::Result<()> {
// Several issues are checked here to avoid issues with diagnostics caused by raising them later.
//
@@ -831,11 +840,19 @@ impl ExportedFn {
#[cfg(not(feature = "metadata"))]
let param_names = quote! {};
let cfg_attrs: Vec<_> = self
.cfg_attrs()
.iter()
.map(syn::Attribute::to_token_stream)
.collect();
quote! {
#(#cfg_attrs)*
impl #type_name {
#param_names
#[inline(always)] pub fn param_types() -> [TypeId; #arg_count] { [#(#input_type_exprs),*] }
}
#(#cfg_attrs)*
impl PluginFunction for #type_name {
#[inline(always)]
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {