eris: refactoring table API so that table methods can see value being inserted

This commit is contained in:
2021-07-06 19:32:20 -04:00
parent a12133243c
commit 3bf8b1fadb
6 changed files with 40 additions and 56 deletions

View File

@@ -326,7 +326,7 @@ void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) {
if (!ttisnil(gval(old))) {
/* doesn't need barrier/invalidate cache, as entry was
already present in the table */
setobjt2t(L, luaH_set(L, t, gkey(old)), gval(old));
luaH_setvalue(L, t, gkey(old), gval(old));
}
}
if (!isdummy(nold))
@@ -402,7 +402,7 @@ static Node *getfreepos (Table *t) {
** put new key in its main position; otherwise (colliding node is in its main
** position), new key goes to an empty position.
*/
TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
static TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
Node *mp;
if (ttisnil(key)) luaG_runerror(L, "table index is nil");
else if (ttisnumber(key) && luai_numisnan(L, nvalue(key)))
@@ -414,7 +414,10 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
if (n == NULL) { /* cannot find a free place? */
rehash(L, t, key); /* grow table */
/* whatever called 'newkey' take care of TM cache and GC barrier */
return luaH_set(L, t, key); /* insert key into grown table */
const TValue *p = luaH_get(t, key);
if (p != luaO_nilobject)
return cast(TValue *, p);
return luaH_newkey(L, t, key);
}
lua_assert(!isdummy(n));
othern = mainposition(t, gkey(mp));
@@ -507,13 +510,19 @@ const TValue *luaH_get (Table *t, const TValue *key) {
** beware: when using this function you probably need to check a GC
** barrier and invalidate the TM cache.
*/
TValue *luaH_set (lua_State *L, Table *t, const TValue *key) {
const TValue *p = luaH_get(t, key);
if (p != luaO_nilobject)
return cast(TValue *, p);
else return luaH_newkey(L, t, key);
void luaH_setupdate (lua_State *L, Table *t, const TValue *key, TValue *value, const TValue *getres) {
TValue *cell;
if (getres != luaO_nilobject) {
cell = cast(TValue *, getres);
} else {
cell = luaH_newkey(L, t, key);
}
setobj2t(L, cell, value);
}
void luaH_setvalue (lua_State *L, Table *t, const TValue *key, TValue *value) {
luaH_setupdate(L, t, key, value, luaH_get(t, key));
}
void luaH_setint (lua_State *L, Table *t, int key, TValue *value) {
const TValue *p = luaH_getint(t, key);
@@ -529,9 +538,13 @@ void luaH_setint (lua_State *L, Table *t, int key, TValue *value) {
}
static int unbound_search (Table *t, unsigned int j) {
unsigned int i = j; /* i is zero or a present index */
j++;
/*
** Try to find a boundary in table `t'. A `boundary' is an integer index
** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil).
*/
int luaH_getn (Table *t) {
unsigned int j = 1;
unsigned int i = 0;
/* find `i' and `j' such that i is present and j is not */
while (!ttisnil(luaH_getint(t, j))) {
i = j;
@@ -553,31 +566,6 @@ static int unbound_search (Table *t, unsigned int j) {
}
/*
** Try to find a boundary in table `t'. A `boundary' is an integer index
** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil).
*/
int luaH_getn (Table *t) {
return unbound_search(t, 0);
// unsigned int j = t->sizearray;
// if (j > 0 && ttisnil(&t->array[j - 1])) {
// /* there is a boundary in the array part: (binary) search for it */
// unsigned int i = 0;
// while (j - i > 1) {
// unsigned int m = (i+j)/2;
// if (ttisnil(&t->array[m - 1])) j = m;
// else i = m;
// }
// return i;
// }
// /* else must find a boundary in hash part */
// else if (isdummy(t->node)) /* hash part is empty? */
// return j; /* that is easy... */
// else return unbound_search(t, j);
}
#if defined(LUA_DEBUG)