tomaka / hlua

Rust library to interface with Lua
MIT License
510 stars 48 forks source link

How to set __index metatable on userdata to table with methods? #137

Closed mkpankov closed 7 years ago

mkpankov commented 7 years ago

I'm trying to implement object-oriented access to my custom type like described here. I want to be able to do foo:bar(args) in Lua, where bar is a Rust function and foo is userdata.

I thought I could set __index like so:

implement_lua_push!(SerialPortLua, |mut metatable| {
    metatable.set("__index", XXX);
});

but the problem is metatable.set accepts a hlua::function, not hlua::LuaTable.

Is there any other way? Maybe I'm missing something and index metatable should be set differently?

tomaka commented 7 years ago

You're supposed to call metatable.empty_array("__index"), which will return a LuaTable which you can then fill.

mkpankov commented 7 years ago

Thanks!