'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.
This commit is contained in:
Roberto Ierusalimschy
2024-03-21 11:23:21 -03:00
parent ce6f5502c9
commit 0593256707
10 changed files with 139 additions and 133 deletions

69
lapi.c
View File

@@ -353,7 +353,7 @@ LUA_API void lua_arith (lua_State *L, int op) {
}
/* first operand at top - 2, second at top - 1; result go to top - 2 */
luaO_arith(L, op, s2v(L->top.p - 2), s2v(L->top.p - 1), L->top.p - 2);
L->top.p--; /* remove second operand */
L->top.p--; /* pop second operand */
lua_unlock(L);
}
@@ -666,47 +666,49 @@ LUA_API int lua_pushthread (lua_State *L) {
static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
TValue aux;
int tag;
TString *str = luaS_new(L, k);
luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, aux);
if (!isemptyV(aux)) {
luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, tag);
if (!tagisempty(tag)) {
api_incr_top(L);
}
else {
setsvalue2s(L, L->top.p, str);
api_incr_top(L);
luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, aux);
tag = luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, tag);
}
lua_unlock(L);
return ttype(s2v(L->top.p - 1));
return novariant(tag);
}
static TValue getGlobalTable (lua_State *L) {
static void getGlobalTable (lua_State *L, TValue *gt) {
Table *registry = hvalue(&G(L)->l_registry);
return luaH_getint(registry, LUA_RIDX_GLOBALS);
int tag = luaH_getint(registry, LUA_RIDX_GLOBALS, gt);
(void)tag; /* avoid not-used warnings when checks are off */
api_check(L, novariant(tag) == LUA_TTABLE, "global table must exist");
}
LUA_API int lua_getglobal (lua_State *L, const char *name) {
TValue gt;
lua_lock(L);
gt = getGlobalTable(L);
getGlobalTable(L, &gt);
return auxgetstr(L, &gt, name);
}
LUA_API int lua_gettable (lua_State *L, int idx) {
TValue aux;
int tag;
TValue *t;
lua_lock(L);
api_checkpop(L, 1);
t = index2value(L, idx);
luaV_fastget(t, s2v(L->top.p - 1), s2v(L->top.p - 1), luaH_get, aux);
if (isemptyV(aux))
luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, aux);
luaV_fastget(t, s2v(L->top.p - 1), s2v(L->top.p - 1), luaH_get, tag);
if (tagisempty(tag))
tag = luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, tag);
lua_unlock(L);
return ttype(s2v(L->top.p - 1));
return novariant(tag);
}
@@ -718,29 +720,27 @@ LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
TValue *t;
TValue aux;
int tag;
lua_lock(L);
t = index2value(L, idx);
luaV_fastgeti(t, n, s2v(L->top.p), aux);
if (isemptyV(aux)) {
luaV_fastgeti(t, n, s2v(L->top.p), tag);
if (tagisempty(tag)) {
TValue key;
setivalue(&key, n);
luaV_finishget(L, t, &key, L->top.p, aux);
tag = luaV_finishget(L, t, &key, L->top.p, tag);
}
api_incr_top(L);
lua_unlock(L);
return ttype(s2v(L->top.p - 1));
return novariant(tag);
}
l_sinline int finishrawget (lua_State *L, TValue res) {
if (isemptyV(res)) /* avoid copying empty items to the stack */
static int finishrawget (lua_State *L, int tag) {
if (tagisempty(tag)) /* avoid copying empty items to the stack */
setnilvalue(s2v(L->top.p));
else
setobjV(L, s2v(L->top.p), res);
api_incr_top(L);
lua_unlock(L);
return ttypeV(res);
return novariant(tag);
}
@@ -753,23 +753,23 @@ l_sinline Table *gettable (lua_State *L, int idx) {
LUA_API int lua_rawget (lua_State *L, int idx) {
Table *t;
TValue res;
int tag;
lua_lock(L);
api_checkpop(L, 1);
t = gettable(L, idx);
res = luaH_get(t, s2v(L->top.p - 1));
tag = luaH_get(t, s2v(L->top.p - 1), s2v(L->top.p - 1));
L->top.p--; /* pop key */
return finishrawget(L, res);
return finishrawget(L, tag);
}
LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
Table *t;
TValue aux;
int tag;
lua_lock(L);
t = gettable(L, idx);
luaH_fastgeti(t, n, s2v(L->top.p), aux);
return finishrawget(L, aux);
luaH_fastgeti(t, n, s2v(L->top.p), tag);
return finishrawget(L, tag);
}
@@ -779,7 +779,7 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
lua_lock(L);
t = gettable(L, idx);
setpvalue(&k, cast_voidp(p));
return finishrawget(L, luaH_get(t, &k));
return finishrawget(L, luaH_get(t, &k, s2v(L->top.p)));
}
@@ -872,7 +872,7 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
LUA_API void lua_setglobal (lua_State *L, const char *name) {
TValue gt;
lua_lock(L); /* unlock done in 'auxsetstr' */
gt = getGlobalTable(L);
getGlobalTable(L, &gt);
auxsetstr(L, &gt, name);
}
@@ -1122,7 +1122,8 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
LClosure *f = clLvalue(s2v(L->top.p - 1)); /* get new function */
if (f->nupvalues >= 1) { /* does it have an upvalue? */
/* get global table from registry */
TValue gt = getGlobalTable(L);
TValue gt;
getGlobalTable(L, &gt);
/* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
setobj(L, f->upvals[0]->v.p, &gt);
luaC_barrier(L, f->upvals[0], &gt);
@@ -1266,7 +1267,7 @@ LUA_API int lua_next (lua_State *L, int idx) {
if (more)
api_incr_top(L);
else /* no more elements */
L->top.p -= 1; /* remove key */
L->top.p--; /* pop key */
lua_unlock(L);
return more;
}