lua-users home
lua-l archive

Re: nested __index

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It is a more general question why in this __indexing chain when finally found __index metamethod is a function, it is called with last table in chain as argument, but not the first one who actually triggered indexing. Yes, i could store a link to initial table somewhere, and then track back who actually triggered initial indexing, but this would require "parent" table be aware of inherited "children".
lvm.c:
void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, const TValue *slot) {
 int loop; /* counter to avoid infinite loops */
 const TValue *tm; /* metamethod */
 const TValue *t0 = t;
...
// luaT_callTMres(L, tm, t, key, val); /* call it */
 luaT_callTMres(L, tm, t0, key, val); /* call it */
On 2023年8月25日 21:51:56 -0400
 David Favro <lua@meta-dynamic.com> wrote:
On August 25, 2023 12:58:40 PM EDT, temp 213 qwe <temp213@gorodok.net> wrote:
"child" table could store a link to "parent" (in __index actually), but if at the end of nested __indexing process we got last "parent" as self, there is no way to get a child who actually triggered it, or?
If I understand correctly what you are asking, this is a common request. You have two options, either use functions for __index all the way down and choose a technique to carry a reference to the original table into successive calls, or modify the Lua runtime to pass in the originating table to the final __index function as a third parameter. I use a personal fork of Lua with this modification amongst others, and I imagine that I am not the only one. See for instance: http://lua-users.org/lists/lua-l/2016-02/msg00287.html
-- David

AltStyle によって変換されたページ (->オリジナル) /