|
| 1 | +use proc_macro::TokenStream; |
| 2 | +use quote::{format_ident, quote}; |
| 3 | +use syn::{Attribute, Data, DeriveInput, Error, Fields, FieldsNamed, Meta, parse_macro_input}; |
| 4 | + |
| 5 | +use crate::attr::LuaAttr; |
| 6 | + |
| 7 | +/// Parse all `#[lua(...)]` attributes on a field, merging them into one `LuaAttr`. |
| 8 | +fn parse_field_lua_attr(attrs: &[Attribute]) -> syn::Result<LuaAttr> { |
| 9 | + let mut lua_attr = LuaAttr::default(); |
| 10 | + for attr in attrs { |
| 11 | + if attr.path().is_ident("lua") |
| 12 | + && let Meta::List(_) = &attr.meta |
| 13 | + { |
| 14 | + attr.parse_nested_meta(|meta| lua_attr.parse_inner(meta))?; |
| 15 | + } |
| 16 | + } |
| 17 | + Ok(lua_attr) |
| 18 | +} |
| 19 | + |
| 20 | +/// Strip `#[lua(...)]` attributes from a field, keeping all others. |
| 21 | +fn strip_lua_attrs(attrs: &[Attribute]) -> Vec<Attribute> { |
| 22 | + (attrs.iter()) |
| 23 | + .filter(|attr| !attr.path().is_ident("lua")) |
| 24 | + .cloned() |
| 25 | + .collect() |
| 26 | +} |
| 27 | + |
| 28 | +pub fn userdata_type(attr: TokenStream, item: TokenStream) -> TokenStream { |
| 29 | + if !attr.is_empty() { |
| 30 | + return Error::new_spanned( |
| 31 | + proc_macro2::TokenStream::from(attr), |
| 32 | + "`#[userdata]` does not accept arguments", |
| 33 | + ) |
| 34 | + .to_compile_error() |
| 35 | + .into(); |
| 36 | + } |
| 37 | + |
| 38 | + let mut input = parse_macro_input!(item as DeriveInput); |
| 39 | + let type_name = &input.ident; |
| 40 | + |
| 41 | + let mut named_fields: Option<&mut FieldsNamed> = match &mut input.data { |
| 42 | + Data::Struct(data) => match &mut data.fields { |
| 43 | + Fields::Named(fields) => Some(fields), |
| 44 | + Fields::Unnamed(_) | Fields::Unit => None, |
| 45 | + }, |
| 46 | + Data::Enum(_) => None, |
| 47 | + Data::Union(_) => { |
| 48 | + return Error::new_spanned(&input, "`#[userdata]` cannot be applied to unions") |
| 49 | + .to_compile_error() |
| 50 | + .into(); |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + // Check for generic type parameters (not supported) |
| 55 | + let has_type_params = input.generics.type_params().next().is_some(); |
| 56 | + if has_type_params { |
| 57 | + return Error::new_spanned( |
| 58 | + &input.generics, |
| 59 | + "`#[userdata]` does not support generic type parameters. Wrap the generic type in a concrete newtype instead." |
| 60 | + ) |
| 61 | + .to_compile_error() |
| 62 | + .into(); |
| 63 | + } |
| 64 | + |
| 65 | + let mut field_registrations = Vec::new(); |
| 66 | + if let Some(fields) = &mut named_fields { |
| 67 | + for field in &fields.named { |
| 68 | + let field_name = field.ident.as_ref().unwrap(); |
| 69 | + |
| 70 | + let lua_attr = try_compile!(parse_field_lua_attr(&field.attrs)); |
| 71 | + if lua_attr.skip { |
| 72 | + continue; |
| 73 | + } |
| 74 | + |
| 75 | + let lua_name = lua_attr.name.unwrap_or_else(|| field_name.to_string()); |
| 76 | + |
| 77 | + // Assume get/set by default (unless explicitly specified) |
| 78 | + let (has_get, has_set) = if lua_attr.get || lua_attr.set { |
| 79 | + (lua_attr.get, lua_attr.set) |
| 80 | + } else { |
| 81 | + (true, true) |
| 82 | + }; |
| 83 | + |
| 84 | + if has_get { |
| 85 | + field_registrations.push(quote! { |
| 86 | + registry.add_field_method_get(#lua_name, |_lua, this| Ok(this.#field_name.clone())); |
| 87 | + }); |
| 88 | + } |
| 89 | + if has_set { |
| 90 | + field_registrations.push(quote! { |
| 91 | + registry.add_field_method_set(#lua_name, |_lua, this, val| { |
| 92 | + this.#field_name = val; |
| 93 | + Ok(()) |
| 94 | + }); |
| 95 | + }); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Strip mlua-specific attributes from fields before re-emitting |
| 100 | + for field in &mut fields.named { |
| 101 | + field.attrs = strip_lua_attrs(&field.attrs); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + let registration_type_name = format_ident!("__MluaUserDataRegistration_{type_name}"); |
| 106 | + let register_fields_fn_name = format_ident!("__mlua_register_{type_name}_fields"); |
| 107 | + |
| 108 | + let output = quote! { |
| 109 | + #input |
| 110 | + |
| 111 | + #[doc(hidden)] |
| 112 | + #[allow(non_camel_case_types)] |
| 113 | + struct #registration_type_name { |
| 114 | + register: fn(&mut ::mlua::userdata::UserDataRegistry<#type_name>), |
| 115 | + } |
| 116 | + |
| 117 | + ::mlua::__inventory::collect!(#registration_type_name); |
| 118 | + |
| 119 | + #[allow(non_snake_case)] |
| 120 | + fn #register_fields_fn_name(registry: &mut ::mlua::userdata::UserDataRegistry<#type_name>) { |
| 121 | + use ::mlua::userdata::UserDataFields as _; |
| 122 | + #(#field_registrations)* |
| 123 | + } |
| 124 | + |
| 125 | + ::mlua::__inventory::submit! { |
| 126 | + #registration_type_name { register: #register_fields_fn_name } |
| 127 | + } |
| 128 | + |
| 129 | + impl ::mlua::userdata::UserData for #type_name { |
| 130 | + fn register(registry: &mut ::mlua::userdata::UserDataRegistry<Self>) { |
| 131 | + for item in ::mlua::__inventory::iter::<#registration_type_name> { |
| 132 | + (item.register)(registry); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + }; |
| 137 | + |
| 138 | + output.into() |
| 139 | +} |
0 commit comments