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

135
lvm.c
View File

@@ -286,15 +286,13 @@ static int floatforloop (StkId ra) {
/*
** 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 empty).
*/
void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val,
const TValue *slot) {
int hres) {
int loop; /* counter to avoid infinite loops */
const TValue *tm; /* metamethod */
for (loop = 0; loop < MAXTAGLOOP; loop++) {
if (slot == NULL) { /* 't' is not a table? */
if (hres == HNOTATABLE) { /* 't' is not a table? */
lua_assert(!ttistable(t));
tm = luaT_gettmbyobj(L, t, TM_INDEX);
if (l_unlikely(notm(tm)))
@@ -302,7 +300,6 @@ void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val,
/* else will try the metamethod */
}
else { /* 't' is a table */
lua_assert(isempty(slot));
tm = fasttm(L, hvalue(t)->metatable, TM_INDEX); /* table's metamethod */
if (tm == NULL) { /* no metamethod? */
setnilvalue(s2v(val)); /* result is nil */
@@ -315,10 +312,9 @@ void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val,
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 */
return;
}
luaV_fastget(t, key, s2v(val), luaH_get, hres);
if (hres == HOK)
return; /* done */
/* else repeat (tail call 'luaV_finishget') */
}
luaG_runerror(L, "'__index' chain too long; possible loop");
@@ -327,22 +323,17 @@ void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val,
/*
** 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 a value with an absent key if there
** is no such entry. (The value at 'slot' must be empty, otherwise
** 'luaV_fastget' would have done the job.)
*/
void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
TValue *val, const TValue *slot) {
TValue *val, int hres) {
int loop; /* counter to avoid infinite loops */
for (loop = 0; loop < MAXTAGLOOP; loop++) {
const TValue *tm; /* '__newindex' metamethod */
if (slot != NULL) { /* is 't' a table? */
if (hres != HNOTATABLE) { /* is 't' a table? */
Table *h = hvalue(t); /* save 't' table */
lua_assert(isempty(slot)); /* slot must be empty */
tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */
if (tm == NULL) { /* no metamethod? */
luaH_finishset(L, h, key, slot, val); /* set new value */
luaH_finishset(L, h, key, val, hres); /* set new value */
invalidateTMcache(h);
luaC_barrierback(L, obj2gco(h), val);
return;
@@ -360,10 +351,9 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
return;
}
t = tm; /* else repeat assignment over 'tm' */
if (luaV_fastget(L, t, key, slot, luaH_get)) {
luaV_finishfastset(L, t, slot, val);
luaV_fastset(t, key, val, hres, luaH_pset);
if (hres == HOK)
return; /* done */
}
/* else 'return luaV_finishset(L, t, key, val, slot)' (loop) */
}
luaG_runerror(L, "'__newindex' chain too long; possible loop");
@@ -1252,114 +1242,109 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
}
vmcase(OP_GETTABUP) {
StkId ra = RA(i);
const TValue *slot;
TValue *upval = cl->upvals[GETARG_B(i)]->v.p;
TValue *rc = KC(i);
TString *key = tsvalue(rc); /* key must be a short string */
if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) {
setobj2s(L, ra, slot);
}
else
Protect(luaV_finishget(L, upval, rc, ra, slot));
int hres;
luaV_fastget(upval, key, s2v(ra), luaH_getshortstr, hres);
if (hres != HOK)
Protect(luaV_finishget(L, upval, rc, ra, hres));
vmbreak;
}
vmcase(OP_GETTABLE) {
StkId ra = RA(i);
const TValue *slot;
TValue *rb = vRB(i);
TValue *rc = vRC(i);
lua_Unsigned n;
if (ttisinteger(rc) /* fast track for integers? */
? (cast_void(n = ivalue(rc)), luaV_fastgeti(L, rb, n, slot))
: luaV_fastget(L, rb, rc, slot, luaH_get)) {
setobj2s(L, ra, slot);
int hres;
if (ttisinteger(rc)) { /* fast track for integers? */
luaV_fastgeti(rb, ivalue(rc), s2v(ra), hres);
}
else
Protect(luaV_finishget(L, rb, rc, ra, slot));
luaV_fastget(rb, rc, s2v(ra), luaH_get, hres);
if (hres != HOK) /* fast track for integers? */
Protect(luaV_finishget(L, rb, rc, ra, hres));
vmbreak;
}
vmcase(OP_GETI) {
StkId ra = RA(i);
const TValue *slot;
TValue *rb = vRB(i);
int c = GETARG_C(i);
if (luaV_fastgeti(L, rb, c, slot)) {
setobj2s(L, ra, slot);
}
else {
int hres;
luaV_fastgeti(rb, c, s2v(ra), hres);
if (hres != HOK) {
TValue key;
setivalue(&key, c);
Protect(luaV_finishget(L, rb, &key, ra, slot));
Protect(luaV_finishget(L, rb, &key, ra, hres));
}
vmbreak;
}
vmcase(OP_GETFIELD) {
StkId ra = RA(i);
const TValue *slot;
TValue *rb = vRB(i);
TValue *rc = KC(i);
TString *key = tsvalue(rc); /* key must be a short string */
if (luaV_fastget(L, rb, key, slot, luaH_getshortstr)) {
setobj2s(L, ra, slot);
}
else
Protect(luaV_finishget(L, rb, rc, ra, slot));
int hres;
luaV_fastget(rb, key, s2v(ra), luaH_getshortstr, hres);
if (hres != HOK)
Protect(luaV_finishget(L, rb, rc, ra, hres));
vmbreak;
}
vmcase(OP_SETTABUP) {
const TValue *slot;
int hres;
TValue *upval = cl->upvals[GETARG_A(i)]->v.p;
TValue *rb = KB(i);
TValue *rc = RKC(i);
TString *key = tsvalue(rb); /* key must be a short string */
if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) {
luaV_finishfastset(L, upval, slot, rc);
}
luaV_fastset(upval, key, rc, hres, luaH_psetshortstr);
if (hres == HOK)
luaV_finishfastset(L, upval, rc);
else
Protect(luaV_finishset(L, upval, rb, rc, slot));
Protect(luaV_finishset(L, upval, rb, rc, hres));
vmbreak;
}
vmcase(OP_SETTABLE) {
StkId ra = RA(i);
const TValue *slot;
int hres;
TValue *rb = vRB(i); /* key (table is in 'ra') */
TValue *rc = RKC(i); /* value */
lua_Unsigned n;
if (ttisinteger(rb) /* fast track for integers? */
? (cast_void(n = ivalue(rb)), luaV_fastgeti(L, s2v(ra), n, slot))
: luaV_fastget(L, s2v(ra), rb, slot, luaH_get)) {
luaV_finishfastset(L, s2v(ra), slot, rc);
if (ttisinteger(rb)) { /* fast track for integers? */
luaV_fastseti(s2v(ra), ivalue(rb), rc, hres);
}
else {
luaV_fastset(s2v(ra), rb, rc, hres, luaH_pset);
}
if (hres == HOK)
luaV_finishfastset(L, s2v(ra), rc);
else
Protect(luaV_finishset(L, s2v(ra), rb, rc, slot));
Protect(luaV_finishset(L, s2v(ra), rb, rc, hres));
vmbreak;
}
vmcase(OP_SETI) {
StkId ra = RA(i);
const TValue *slot;
int c = GETARG_B(i);
int hres;
int b = GETARG_B(i);
TValue *rc = RKC(i);
if (luaV_fastgeti(L, s2v(ra), c, slot)) {
luaV_finishfastset(L, s2v(ra), slot, rc);
}
luaV_fastseti(s2v(ra), b, rc, hres);
if (hres == HOK)
luaV_finishfastset(L, s2v(ra), rc);
else {
TValue key;
setivalue(&key, c);
Protect(luaV_finishset(L, s2v(ra), &key, rc, slot));
setivalue(&key, b);
Protect(luaV_finishset(L, s2v(ra), &key, rc, hres));
}
vmbreak;
}
vmcase(OP_SETFIELD) {
StkId ra = RA(i);
const TValue *slot;
int hres;
TValue *rb = KB(i);
TValue *rc = RKC(i);
TString *key = tsvalue(rb); /* key must be a short string */
if (luaV_fastget(L, s2v(ra), key, slot, luaH_getshortstr)) {
luaV_finishfastset(L, s2v(ra), slot, rc);
}
luaV_fastset(s2v(ra), key, rc, hres, luaH_psetshortstr);
if (hres == HOK)
luaV_finishfastset(L, s2v(ra), rc);
else
Protect(luaV_finishset(L, s2v(ra), rb, rc, slot));
Protect(luaV_finishset(L, s2v(ra), rb, rc, hres));
vmbreak;
}
vmcase(OP_NEWTABLE) {
@@ -1383,16 +1368,14 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
}
vmcase(OP_SELF) {
StkId ra = RA(i);
const TValue *slot;
int hres;
TValue *rb = vRB(i);
TValue *rc = RKC(i);
TString *key = tsvalue(rc); /* key must be a string */
setobj2s(L, ra + 1, rb);
if (luaV_fastget(L, rb, key, slot, luaH_getstr)) {
setobj2s(L, ra, slot);
}
else
Protect(luaV_finishget(L, rb, rc, ra, slot));
luaV_fastget(rb, key, s2v(ra), luaH_getstr, hres);
if (hres != HOK)
Protect(luaV_finishget(L, rb, rc, ra, hres));
vmbreak;
}
vmcase(OP_ADDI) {
@@ -1873,7 +1856,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
luaH_resizearray(L, h, last); /* preallocate it at once */
for (; n > 0; n--) {
TValue *val = s2v(ra + n);
setobj2t(L, &h->array[last - 1], val);
obj2arr(h, last, val);
last--;
luaC_barrierback(L, obj2gco(h), val);
}