Skip to main content

LuaUserdata

Derive Macro LuaUserdata 

Source
#[derive(LuaUserdata)]
{
    // Attributes available to this derive:
    #[lua_type_name]
}
Expand description

Emits the luna_core::vm::LuaUserdata trait impl for a struct.

The companion attribute macro lua_userdata_methods (applied to an impl block of the same struct) injects a hidden __luna_userdata_register associated fn that the derive’s add_methods() body forwards to.

Accepts #[lua_type_name = "Foo"] at the struct level to override the default type_name() (which falls back to the struct’s Rust ident). Embedders who don’t need to override can omit the attr.

§Example

use luna_jit_derive::{LuaUserdata, lua_userdata_methods};
use luna_core::vm::{LuaError, MetaMethod, UserdataMethods, Vm};

#[derive(LuaUserdata)]
#[lua_type_name = "Counter"]
struct Counter { value: i64 }

#[lua_userdata_methods]
impl Counter {
    #[lua_method("get")]
    fn get(&self, _vm: &mut Vm, _: ()) -> Result<i64, LuaError> {
        Ok(self.value)
    }
}