Merge branch 'newarray' into nextversion

This commit is contained in:
Roberto Ierusalimschy
2023-11-07 17:26:15 -03:00
12 changed files with 529 additions and 274 deletions

110
lapi.c
View File

@@ -637,50 +637,44 @@ LUA_API int lua_pushthread (lua_State *L) {
l_sinline int auxgetstr (lua_State *L, const TValue *t, const char *k) {
const TValue *slot;
int hres;
TString *str = luaS_new(L, k);
if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
setobj2s(L, L->top.p, slot);
luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, hres);
if (hres == HOK) {
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, slot);
luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, hres);
}
lua_unlock(L);
return ttype(s2v(L->top.p - 1));
}
/*
** Get the global table in the registry. Since all predefined
** indices in the registry were inserted right when the registry
** was created and never removed, they must always be in the array
** part of the registry.
*/
#define getGtable(L) \
(&hvalue(&G(L)->l_registry)->array[LUA_RIDX_GLOBALS - 1])
static void getGlobalTable (lua_State *L, TValue *gt) {
Table *registry = hvalue(&G(L)->l_registry);
luaH_getint(registry, LUA_RIDX_GLOBALS, gt);
}
LUA_API int lua_getglobal (lua_State *L, const char *name) {
const TValue *G;
TValue gt;
lua_lock(L);
G = getGtable(L);
return auxgetstr(L, G, name);
getGlobalTable(L, &gt);
return auxgetstr(L, &gt, name);
}
LUA_API int lua_gettable (lua_State *L, int idx) {
const TValue *slot;
int hres;
TValue *t;
lua_lock(L);
t = index2value(L, idx);
if (luaV_fastget(L, t, s2v(L->top.p - 1), slot, luaH_get)) {
setobj2s(L, L->top.p - 1, slot);
}
else
luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot);
luaV_fastget(t, s2v(L->top.p - 1), s2v(L->top.p - 1), luaH_get, hres);
if (hres != HOK)
luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, hres);
lua_unlock(L);
return ttype(s2v(L->top.p - 1));
}
@@ -694,16 +688,14 @@ 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;
const TValue *slot;
int hres;
lua_lock(L);
t = index2value(L, idx);
if (luaV_fastgeti(L, t, n, slot)) {
setobj2s(L, L->top.p, slot);
}
else {
TValue aux;
setivalue(&aux, n);
luaV_finishget(L, t, &aux, L->top.p, slot);
luaV_fastgeti(t, n, s2v(L->top.p), hres);
if (hres != HOK) {
TValue key;
setivalue(&key, n);
luaV_finishget(L, t, &key, L->top.p, hres);
}
api_incr_top(L);
lua_unlock(L);
@@ -711,11 +703,9 @@ LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
}
l_sinline int finishrawget (lua_State *L, const TValue *val) {
if (isempty(val)) /* avoid copying empty items to the stack */
l_sinline int finishrawget (lua_State *L, int hres) {
if (hres != HOK) /* avoid copying empty items to the stack */
setnilvalue(s2v(L->top.p));
else
setobj2s(L, L->top.p, val);
api_incr_top(L);
lua_unlock(L);
return ttype(s2v(L->top.p - 1));
@@ -731,13 +721,13 @@ static Table *gettable (lua_State *L, int idx) {
LUA_API int lua_rawget (lua_State *L, int idx) {
Table *t;
const TValue *val;
int hres;
lua_lock(L);
api_checknelems(L, 1);
t = gettable(L, idx);
val = luaH_get(t, s2v(L->top.p - 1));
hres = luaH_get(t, s2v(L->top.p - 1), s2v(L->top.p - 1));
L->top.p--; /* remove key */
return finishrawget(L, val);
return finishrawget(L, hres);
}
@@ -745,7 +735,7 @@ LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
Table *t;
lua_lock(L);
t = gettable(L, idx);
return finishrawget(L, luaH_getint(t, n));
return finishrawget(L, luaH_getint(t, n, s2v(L->top.p)));
}
@@ -755,7 +745,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)));
}
@@ -827,17 +817,18 @@ LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) {
** t[k] = value at the top of the stack (where 'k' is a string)
*/
static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
const TValue *slot;
int hres;
TString *str = luaS_new(L, k);
api_checknelems(L, 1);
if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
luaV_finishfastset(L, t, slot, s2v(L->top.p - 1));
luaV_fastset(t, str, s2v(L->top.p - 1), hres, luaH_psetstr);
if (hres == HOK) {
luaV_finishfastset(L, t, s2v(L->top.p - 1));
L->top.p--; /* pop value */
}
else {
setsvalue2s(L, L->top.p, str); /* push 'str' (to make it a TValue) */
api_incr_top(L);
luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), slot);
luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), hres);
L->top.p -= 2; /* pop value and key */
}
lua_unlock(L); /* lock done by caller */
@@ -845,24 +836,25 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
LUA_API void lua_setglobal (lua_State *L, const char *name) {
const TValue *G;
TValue gt;
lua_lock(L); /* unlock done in 'auxsetstr' */
G = getGtable(L);
auxsetstr(L, G, name);
getGlobalTable(L, &gt);
auxsetstr(L, &gt, name);
}
LUA_API void lua_settable (lua_State *L, int idx) {
TValue *t;
const TValue *slot;
int hres;
lua_lock(L);
api_checknelems(L, 2);
t = index2value(L, idx);
if (luaV_fastget(L, t, s2v(L->top.p - 2), slot, luaH_get)) {
luaV_finishfastset(L, t, slot, s2v(L->top.p - 1));
luaV_fastset(t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres, luaH_pset);
if (hres == HOK) {
luaV_finishfastset(L, t, s2v(L->top.p - 1));
}
else
luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), slot);
luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres);
L->top.p -= 2; /* pop index and value */
lua_unlock(L);
}
@@ -876,17 +868,18 @@ LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
TValue *t;
const TValue *slot;
int hres;
lua_lock(L);
api_checknelems(L, 1);
t = index2value(L, idx);
if (luaV_fastgeti(L, t, n, slot)) {
luaV_finishfastset(L, t, slot, s2v(L->top.p - 1));
luaV_fastseti(t, n, s2v(L->top.p - 1), hres);
if (hres == HOK) {
luaV_finishfastset(L, t, s2v(L->top.p - 1));
}
else {
TValue aux;
setivalue(&aux, n);
luaV_finishset(L, t, &aux, s2v(L->top.p - 1), slot);
TValue temp;
setivalue(&temp, n);
luaV_finishset(L, t, &temp, s2v(L->top.p - 1), hres);
}
L->top.p--; /* pop value */
lua_unlock(L);
@@ -1096,10 +1089,11 @@ 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 */
const TValue *gt = getGtable(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);
setobj(L, f->upvals[0]->v.p, &gt);
luaC_barrier(L, f->upvals[0], &gt);
}
}
lua_unlock(L);