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

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Thu Apr 8 00:04:57 MSK 2021


On 07.04.2021 22:45, Cyrill Gorcunov via Tarantool-patches wrote:
> On Wed, Apr 07, 2021 at 11:37:15PM +0300, Cyrill Gorcunov wrote:
> ...
>>
>> 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?
> 
> Look into fiber code
> 
> static const struct luaL_Reg lbox_fiber_meta [] = {
> 	...
> 	{"__serialize", lbox_fiber_serialize},
> 	{"__tostring", lbox_fiber_tostring},
> 	{"join", lbox_fiber_join},
> 	{"set_joinable", lbox_fiber_set_joinable},
> 	{"wakeup", lbox_fiber_wakeup},
> 	{"__index", lbox_fiber_index},
> 	{NULL, NULL}
> };
> 
> static int
> lbox_fiber_index(struct lua_State *L)
> {
> 	if (lua_gettop(L) < 2)
> 		return 0;
> 	if (lua_isstring(L, 2) && strcmp(lua_tostring(L, 2), "storage") == 0)
> 		return lbox_fiber_storage(L);
> 
> -->	/* Get value from metatable */
> 	lua_getmetatable(L, 1);
> 	lua_pushvalue(L, 2);
> 	lua_gettable(L, -2);
> 	return 1;
> }
> 
> So I need to do the same

Yeah, I see now. I thought that __index is called only for the not
present fields. And it is true, but only if they are real fields of
a concrete object, not metafields.

In this example:

	mt = {
	    call = function() print('do call') end,
	    __index = function(self, key)
	        print(key)
	    end,
	}
	t = setmetatable({}, mt)

an attempt to do t.call or t:call() always goes through __index.
While in this example:

	mt = {
	    __index = function(self, key)
	        print(key)
	    end,
	}
	t = setmetatable({call = function() print('do call') end}, mt)

the same code bypasses __index. Your case is the first one, and it
seems we can't do anything about it so far.


More information about the Tarantool-patches mailing list