[Tarantool-patches] [PATCH v20 6/7] box: implement box.lib module
Cyrill Gorcunov
gorcunov at gmail.com
Wed Apr 7 23:37:15 MSK 2021
On Wed, Apr 07, 2021 at 10:28:09PM +0200, Vladislav Shpilevoy wrote:
> >>
> >> Actually this snippet comes from popen code where
> >> we have wrappers on lua level and test for metamethods.
> >> While it doesn't hurt it makes no much sense in current
> >> code, thanks! Will drop.
> >
> > Heh, in real we need these to handle metamethods such as
> > "module:load". I added a comment into the code.
>
> For methods you need to use the metatype. It is defined as
> luaL_register_type(), where you can add methods, not just
> __index. The latter is more for, well, indexing. Getting an
> attribute, for example. See an example in lua/popen.c.
>
> Methods should not end up in __index.
Wait. Here is what I have now
void
box_lua_lib_init(struct lua_State *L)
{
func_hash = mh_strnptr_new();
if (func_hash == NULL)
panic("box.lib: Can't allocate func hash table");
static const struct luaL_Reg top_methods[] = {
{ "load", lbox_module_load },
{ NULL, NULL },
};
luaL_register(L, "box.lib", top_methods);
lua_pop(L, 1);
static const struct luaL_Reg lbox_module_methods[] = {
{ "unload", lbox_module_unload },
{ "load", lbox_module_load_func },
{ "__index", lbox_module_index },
{ "__serialize", lbox_module_serialize },
{ "__gc", lbox_module_gc },
{ NULL, NULL },
};
luaL_register_type(L, uname_lib, lbox_module_methods);
...
}
If I don't lookup for key being some nonnil value in __index
(ie those 5 lines) then module:load simply stop working. As
far as I understand the __index is called when Lua looks up
for a key in table. It sends "load" there and since it is
in metatable it executes lbox_module_load_func as it should.
What I'm missing?
More information about the Tarantool-patches
mailing list