[Tarantool-patches] [PATCH v20 6/7] box: implement box.lib module

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Wed Apr 7 23:28:09 MSK 2021


On 07.04.2021 22:22, Cyrill Gorcunov wrote:
> On Wed, Apr 07, 2021 at 07:59:35PM +0300, Cyrill Gorcunov wrote:
>>>> +/** Handle __index request for a module object. */
>>>> +static int
>>>> +lbox_module_index(struct lua_State *L)
>>>> +{
>>>> +	lua_getmetatable(L, 1);
>>>> +	lua_pushvalue(L, 2);
>>>> +	lua_rawget(L, -2);
>>>> +	if (!lua_isnil(L, -1))
>>>> +		return 1;
>>>
>>> 6. What is happening here in these 5 lines?
>>
>> 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.


More information about the Tarantool-patches mailing list