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. --- ltable.c | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) (limited to 'ltable.c') diff --git a/ltable.c b/ltable.c index f675f39b..f62f36bc 100644 --- a/ltable.c +++ b/ltable.c @@ -904,14 +904,23 @@ static int hashkeyisempty (Table *t, lua_Integer key) { } -TValue luaH_getint (Table *t, lua_Integer key) { +static int finishnodeget (const TValue *val, TValue *res) { + if (!ttisnil(val)) { + setobj(((lua_State*)NULL), res, val); + } + return ttypetag(val); +} + + +int luaH_getint (Table *t, lua_Integer key, TValue *res) { if (keyinarray(t, key)) { - TValue res; - arr2objV(t, key, res); - return res; + int tag = *getArrTag(t, key - 1); + if (!tagisempty(tag)) + farr2val(t, key, tag, res); + return tag; } - else - return *getintfromhash(t, key); + else + return finishnodeget(getintfromhash(t, key), res); } @@ -934,8 +943,8 @@ const TValue *luaH_Hgetshortstr (Table *t, TString *key) { } -TValue luaH_getshortstr (Table *t, TString *key) { - return *luaH_Hgetshortstr(t, key); +int luaH_getshortstr (Table *t, TString *key, TValue *res) { + return finishnodeget(luaH_Hgetshortstr(t, key), res); } @@ -950,8 +959,8 @@ static const TValue *Hgetstr (Table *t, TString *key) { } -TValue luaH_getstr (Table *t, TString *key) { - return *Hgetstr(t, key); +int luaH_getstr (Table *t, TString *key, TValue *res) { + return finishnodeget(Hgetstr(t, key), res); } @@ -967,31 +976,34 @@ TString *luaH_getstrkey (Table *t, TString *key) { /* ** main search function */ -TValue luaH_get (Table *t, const TValue *key) { +int luaH_get (Table *t, const TValue *key, TValue *res) { + const TValue *slot; switch (ttypetag(key)) { case LUA_VSHRSTR: - return *luaH_Hgetshortstr(t, tsvalue(key)); + slot = luaH_Hgetshortstr(t, tsvalue(key)); break; case LUA_VNUMINT: - return luaH_getint(t, ivalue(key)); + return luaH_getint(t, ivalue(key), res); case LUA_VNIL: - return absentkey; + slot = &absentkey; break; case LUA_VNUMFLT: { lua_Integer k; if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */ - return luaH_getint(t, k); /* use specialized version */ + return luaH_getint(t, k, res); /* use specialized version */ /* else... */ } /* FALLTHROUGH */ default: - return *getgeneric(t, key, 0); + slot = getgeneric(t, key, 0); + break; } + return finishnodeget(slot, res); } static int finishnodeset (Table *t, const TValue *slot, TValue *val) { if (!ttisnil(slot)) { - setobj(cast(lua_State*, NULL), cast(TValue*, slot), val); + setobj(((lua_State*)NULL), cast(TValue*, slot), val); return HOK; /* success */ } else if (isabstkey(slot)) @@ -1005,7 +1017,7 @@ static int rawfinishnodeset (const TValue *slot, TValue *val) { if (isabstkey(slot)) return 0; /* no slot with that key */ else { - setobj(cast(lua_State*, NULL), cast(TValue*, slot), val); + setobj(((lua_State*)NULL), cast(TValue*, slot), val); return 1; /* success */ } } -- cgit v1.2.3-55-g6feb