From 0593256707ceddb1bc9cd4b25b822a7fbcfedd66 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 21 Mar 2024 11:23:21 -0300 Subject: 'luaH_get' functions return tag of the result Undoing previous commit. Returning TValue increases code size without any visible gains. Returning the tag is a little simpler than returning a special code (HOK/HNOTFOUND) and the tag is useful by itself in some cases. --- lvm.c | 61 ++++++++++++++++++++++++++++++------------------------------- 1 file changed, 30 insertions(+), 31 deletions(-) (limited to 'lvm.c') diff --git a/lvm.c b/lvm.c index a251f423..cfa9961b 100644 --- a/lvm.c +++ b/lvm.c @@ -285,13 +285,12 @@ static int floatforloop (StkId ra) { /* -** Finish the table access 'val = t[key]'. +** Finish the table access 'val = t[key]' and return the tag of the result. */ -void luaV_finishget_ (lua_State *L, const TValue *t, TValue *key, StkId val, - int tag) { +int luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, + int tag) { int loop; /* counter to avoid infinite loops */ const TValue *tm; /* metamethod */ - TValue aux; for (loop = 0; loop < MAXTAGLOOP; loop++) { if (tag == LUA_VNOTABLE) { /* 't' is not a table? */ lua_assert(!ttistable(t)); @@ -304,22 +303,22 @@ void luaV_finishget_ (lua_State *L, const TValue *t, TValue *key, StkId val, tm = fasttm(L, hvalue(t)->metatable, TM_INDEX); /* table's metamethod */ if (tm == NULL) { /* no metamethod? */ setnilvalue(s2v(val)); /* result is nil */ - return; + return LUA_VNIL; } /* else will try the metamethod */ } if (ttisfunction(tm)) { /* is metamethod a function? */ luaT_callTMres(L, tm, t, key, val); /* call it */ - return; + return ttypetag(s2v(val)); } t = tm; /* else try to access 'tm[key]' */ - luaV_fastget(t, key, s2v(val), luaH_get, aux); - if (!isemptyV(aux)) - return; /* done */ + luaV_fastget(t, key, s2v(val), luaH_get, tag); + if (!tagisempty(tag)) + return tag; /* done */ /* else repeat (tail call 'luaV_finishget') */ - tag = ttypetagV(aux); } luaG_runerror(L, "'__index' chain too long; possible loop"); + return 0; /* to avoid warnings */ } @@ -1247,36 +1246,36 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *upval = cl->upvals[GETARG_B(i)]->v.p; TValue *rc = KC(i); TString *key = tsvalue(rc); /* key must be a short string */ - TValue aux; - luaV_fastget(upval, key, s2v(ra), luaH_getshortstr, aux); - if (isemptyV(aux)) - Protect(luaV_finishget(L, upval, rc, ra, aux)); + int tag; + luaV_fastget(upval, key, s2v(ra), luaH_getshortstr, tag); + if (tagisempty(tag)) + Protect(luaV_finishget(L, upval, rc, ra, tag)); vmbreak; } vmcase(OP_GETTABLE) { StkId ra = RA(i); TValue *rb = vRB(i); TValue *rc = vRC(i); - TValue aux; + int tag; if (ttisinteger(rc)) { /* fast track for integers? */ - luaV_fastgeti(rb, ivalue(rc), s2v(ra), aux); + luaV_fastgeti(rb, ivalue(rc), s2v(ra), tag); } else - luaV_fastget(rb, rc, s2v(ra), luaH_get, aux); - if (isemptyV(aux)) /* fast track for integers? */ - Protect(luaV_finishget(L, rb, rc, ra, aux)); + luaV_fastget(rb, rc, s2v(ra), luaH_get, tag); + if (tagisempty(tag)) + Protect(luaV_finishget(L, rb, rc, ra, tag)); vmbreak; } vmcase(OP_GETI) { StkId ra = RA(i); TValue *rb = vRB(i); int c = GETARG_C(i); - TValue aux; - luaV_fastgeti(rb, c, s2v(ra), aux); - if (isemptyV(aux)) { + int tag; + luaV_fastgeti(rb, c, s2v(ra), tag); + if (tagisempty(tag)) { TValue key; setivalue(&key, c); - Protect(luaV_finishget(L, rb, &key, ra, aux)); + Protect(luaV_finishget(L, rb, &key, ra, tag)); } vmbreak; } @@ -1285,10 +1284,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *rb = vRB(i); TValue *rc = KC(i); TString *key = tsvalue(rc); /* key must be a short string */ - TValue aux; - luaV_fastget(rb, key, s2v(ra), luaH_getshortstr, aux); - if (isemptyV(aux)) - Protect(luaV_finishget(L, rb, rc, ra, aux)); + int tag; + luaV_fastget(rb, key, s2v(ra), luaH_getshortstr, tag); + if (tagisempty(tag)) + Protect(luaV_finishget(L, rb, rc, ra, tag)); vmbreak; } vmcase(OP_SETTABUP) { @@ -1370,14 +1369,14 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_SELF) { StkId ra = RA(i); - TValue aux; + int tag; TValue *rb = vRB(i); TValue *rc = RKC(i); TString *key = tsvalue(rc); /* key must be a string */ setobj2s(L, ra + 1, rb); - luaV_fastget(rb, key, s2v(ra), luaH_getstr, aux); - if (isemptyV(aux)) - Protect(luaV_finishget(L, rb, rc, ra, aux)); + luaV_fastget(rb, key, s2v(ra), luaH_getstr, tag); + if (tagisempty(tag)) + Protect(luaV_finishget(L, rb, rc, ra, tag)); vmbreak; } vmcase(OP_ADDI) { -- cgit v1.2.3-55-g6feb