From 6fa3005e344aadcebaa6825d2589cdbc61b76ae9 Mon Sep 17 00:00:00 2001 From: Alug Date: Fri, 15 May 2026 11:52:34 +0200 Subject: [PATCH] Revert "Blua: 'luaV_fastget' only treats the real fast case (table with a non-nil value at given key, so that it does not need to check metamethods)" This reverts commit 019812db0843e0d8a6678ae0ad13f018603d73a5. --- src/blua/lapi.c | 16 ++++++------ src/blua/lvm.c | 66 +++++++++++++++++++------------------------------ src/blua/lvm.h | 25 ++++++++++--------- 3 files changed, 46 insertions(+), 61 deletions(-) diff --git a/src/blua/lapi.c b/src/blua/lapi.c index bca24a90f..420e4e20b 100644 --- a/src/blua/lapi.c +++ b/src/blua/lapi.c @@ -520,17 +520,17 @@ LUA_API int lua_pushthread (lua_State *L) { */ static void auxgetstr (lua_State *L, TValue *t, const char *k) { - TValue *slot; + TValue *aux; TValue key; TString *str = luaS_new(L, k); api_checkvalidindex(L, t); - if (luaV_fastget(L, t, str, slot, luaH_getstr)) { - setobj2s(L, L->top, slot); + if (luaV_fastget(L, t, str, aux, luaH_getstr)) { + setobj2s(L, L->top, aux); api_incr_top(L); } else { setsvalue(L, &key, str); - luaV_finishget(L, t, L->top, L->top, slot); + luaV_finishget(L, t, L->top, L->top, aux); api_incr_top(L); } lua_unlock(L); @@ -643,17 +643,17 @@ LUA_API void lua_getfenv (lua_State *L, int idx) { static void auxsetstr (lua_State *L, TValue *t, const char *k) { TValue key; - TValue *slot; + TValue *aux; TString *str = luaS_new(L, k); api_checknelems(L, 1); api_checkvalidindex(L, t); - if (luaV_fastset(L, t, str, slot, luaH_setstr, L->top)) { - setobj2t(L, cast(TValue *, slot), L->top - 1); + if (luaV_fastset(L, t, str, aux, luaH_setstr, L->top)) { + setobj2t(L, cast(TValue *, aux), L->top - 1); L->top--; /* pop value */ } else { setsvalue2s(L, &key, str); - luaV_finishset(L, t, &key, L->top - 1, slot); + luaV_finishset(L, t, &key, L->top - 1, aux); L->top--; /* pop value */ } lua_unlock(L); diff --git a/src/blua/lvm.c b/src/blua/lvm.c index 9db792fd6..7540bcdec 100644 --- a/src/blua/lvm.c +++ b/src/blua/lvm.c @@ -106,67 +106,51 @@ static void callTM (lua_State *L, const TValue *f, const TValue *p1, /* -** Finish the table access 'val = t[key]'. -** if 'slot' is NULL, 't' is not a table; otherwise, 'slot' points to -** t[k] entry (which must be nil). +** Complete a table access: if 't' is a table, 'tm' has its metamethod; +** otherwise, 'tm' is NULL. */ void luaV_finishget (lua_State *L, TValue *t, TValue *key, StkId val, - TValue *slot) { + TValue *tm) { int loop; /* counter to avoid infinite loops */ - TValue *tm; /* metamethod */ + lua_assert(tm != NULL || !ttistable(t)); for (loop = 0; loop < MAXTAGLOOP; loop++) { - if (slot == NULL) { /* 't' is not a table? */ - lua_assert(!ttistable(t)); - tm = luaT_gettmbyobj(L, t, TM_INDEX); - if (l_unlikely(ttisnil(tm))) + if (tm == NULL) { /* no metamethod (from a table)? */ + if (l_unlikely(ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))) luaG_typeerror(L, t, "index"); /* no metamethod */ - /* else will try the metamethod */ } - else { /* 't' is a table */ - lua_assert(ttisnil(slot)); - tm = fasttm(L, hvalue(t)->metatable, TM_INDEX); /* table's metamethod */ - if (tm == NULL) { /* no metamethod? */ - setnilvalue(val); /* result is nil */ - return; - } - /* else will try the metamethod */ - } - if (ttisfunction(tm)) { /* is metamethod a function? */ + if (ttisfunction(tm)) { /* metamethod is a function */ callTMres(L, val, tm, t, key); /* call it */ return; } - t = tm; /* else try to access 'tm[key]' */ - if (luaV_fastget(L,t,key,slot,luaH_get)) { /* fast track? */ - setobj2s(L, val, slot); /* done */ + t = tm; /* else repeat access over 'tm' */ + if (luaV_fastget(L,t,key,tm,luaH_get)) { /* try fast track */ + setobj2s(L, val, tm); /* done */ return; } - /* else repeat (tail call 'luaV_finishget') */ + /* else repeat */ } - luaG_runerror(L, "'__index' chain too long; possible loop"); + luaG_runerror(L, "gettable chain too long; possible loop"); } /* -** Finish a table assignment 't[key] = val'. -** If 'slot' is NULL, 't' is not a table. Otherwise, 'slot' points -** to the entry 't[key]', or to 'luaO_nilobject' if there is no such -** entry. (The value at 'slot' must be nil, otherwise 'luaV_fastset' -** would have done the job.) +** Main function for table assignment (invoking metamethods if needed). +** Compute 't[key] = val' */ void luaV_finishset (lua_State *L, TValue *t, TValue *key, - StkId val, TValue *slot) { + StkId val, TValue *oldval) { int loop; /* counter to avoid infinite loops */ for (loop = 0; loop < MAXTAGLOOP; loop++) { TValue *tm; - if (slot != NULL) { - lua_assert(ttistable(t) && ttisnil(slot)); + if (oldval != NULL) { + lua_assert(ttistable(t) && ttisnil(oldval)); Table *h = hvalue(t); /* must check the metamethod */ - /* slot is nil=> look for newindex, slot is not nil => look for usedindex */ - if (!((ttisnil(slot) && ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) != NULL)) || - ((!ttisnil(slot)) && ((tm = fasttm(L, h->metatable, TM_USEDINDEX)) != NULL)))) { + /* oldval is nil=> look for newindex, oldval is not nil => look for usedindex */ + if (!((ttisnil(oldval) && ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) != NULL)) || + ((!ttisnil(oldval)) && ((tm = fasttm(L, h->metatable, TM_USEDINDEX)) != NULL)))) { /* no metamethod and (now) there is an entry with given key */ - setobj2t(L, cast(TValue *, slot), val); + setobj2t(L, cast(TValue *, oldval), val); luaC_barriert(L, h, val); return; } @@ -182,7 +166,7 @@ void luaV_finishset (lua_State *L, TValue *t, TValue *key, return; } t = tm; /* else repeat assignment over 'tm' */ - if (luaV_fastset(L, t, key, slot, luaH_set, val)) + if (luaV_fastset(L, t, key, oldval, luaH_set, val)) return; /* done */ /* else loop */ } @@ -413,9 +397,9 @@ static void Arith (lua_State *L, StkId ra, TValue *rb, ** copy of 'luaV_gettable', but protecting call to potential metamethod ** (which can reallocate the stack) */ -#define gettableProtected(L,t,k,v) { TValue *slot; \ - if (luaV_fastget(L,t,k,slot,luaH_get)) { setobj2s(L, v, slot); } \ - else Protect(luaV_finishget(L,t,k,v,slot)); } +#define gettableProtected(L,t,k,v) { TValue *aux; \ + if (luaV_fastget(L,t,k,aux,luaH_get)) { setobj2s(L, v, aux); } \ + else Protect(luaV_finishget(L,t,k,v,aux)); } /* same for 'luaV_settable' */ diff --git a/src/blua/lvm.h b/src/blua/lvm.h index 0c4c877f2..2f65098f0 100644 --- a/src/blua/lvm.h +++ b/src/blua/lvm.h @@ -23,24 +23,25 @@ /* -** fast track for 'gettable': if 't' is a table and 't[k]' is not nil, -** return 1 with 'slot' pointing to 't[k]' (final result). Otherwise, -** return 0 (meaning it will have to check metamethod) with 'slot' -** pointing to a nil 't[k]' (if 't' is a table) or NULL (otherwise). -** 'f' is the raw get function to use. +** fast track for 'gettable': 1 means 'aux' points to resulted value; +** 0 means 'aux' is metamethod (if 't' is a table) or NULL. 'f' is +** the raw get function to use. */ -#define luaV_fastget(L,t,k,slot,f) \ +#define luaV_fastget(L,t,k,aux,f) \ (!ttistable(t) \ - ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ - : (slot = f(hvalue(t), k), /* else, do raw access */ \ - !ttisnil(slot))) /* result not nil? */ + ? (aux = NULL, 0) /* not a table; 'aux' is NULL and result is 0 */ \ + : (aux = f(hvalue(t), k), /* else, do raw access */ \ + !ttisnil(aux) ? 1 /* result not nil? 'aux' has it */ \ + : (aux = fasttm(L, hvalue(t)->metatable, TM_INDEX), /* get metamethod */\ + aux != NULL ? 0 /* has metamethod? must call it */ \ + : (aux = luaO_nilobject, 1)))) /* else, final result is nil */ /* ** standard implementation for 'gettable' */ -#define luaV_gettable(L,t,k,v) { TValue *slot; \ - if (luaV_fastget(L,t,k,slot,luaH_get)) { setobj2s(L, v, slot); } \ - else luaV_finishget(L,t,k,v,slot); } +#define luaV_gettable(L,t,k,v) { TValue *aux; \ + if (luaV_fastget(L,t,k,aux,luaH_get)) { setobj2s(L, v, aux); } \ + else luaV_finishget(L,t,k,v,aux); } /*