From fcab60bac449d1a86302d1ca2b54b1c58b59cf2b Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Sun, 31 May 2026 12:28:06 +0100 Subject: [PATCH] mlua_derive: Fix `#[lua(meta, field)]` case --- mlua_derive/src/userdata/userdata_impl.rs | 2 +- tests/userdata_macro.rs | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/mlua_derive/src/userdata/userdata_impl.rs b/mlua_derive/src/userdata/userdata_impl.rs index d082bb4..88f0d5a 100644 --- a/mlua_derive/src/userdata/userdata_impl.rs +++ b/mlua_derive/src/userdata/userdata_impl.rs @@ -356,7 +356,7 @@ pub fn userdata_impl(attr: TokenStream, item: TokenStream) -> TokenStream { let lua_name = lua_attr.name(fn_name); if lua_attr.meta { let tokens = quote! { - registry.add_meta_field(#lua_name, #type_path::#fn_name); + registry.add_meta_field(#lua_name, #type_path::#fn_name()); }; registration_calls.push(with_cfg(tokens, &method.attrs)); } else { diff --git a/tests/userdata_macro.rs b/tests/userdata_macro.rs index 169c924..ec802f3 100644 --- a/tests/userdata_macro.rs +++ b/tests/userdata_macro.rs @@ -79,6 +79,11 @@ impl Rectangle { } } + #[lua(meta, field, name = "__answer")] + fn answer() -> u32 { + 42 + } + #[lua(skip)] #[allow(unused)] fn helper() -> u32 { @@ -124,7 +129,7 @@ impl Rectangle { } fn make_lua() -> Lua { - let lua = Lua::new(); + let lua = unsafe { Lua::unsafe_new() }; lua.globals() .set("Rectangle", lua.create_proxy::().unwrap()) .unwrap(); @@ -193,6 +198,12 @@ fn test_rectangle() { assert(other.length == 7, "other length should be 7 after transfer") assert(other:double_length() == 14, "double_length should be 14") + -- meta field + if _VERSION:match("Lua ") then + local mt = debug.getmetatable(rect) + assert(mt.__answer == 42, "__answer meta field should be 42") + end + assert(rect.lua_version == _VERSION, "lua_version should match Lua's _VERSION") -- Consuming method