eris: refactoring table API so that table methods can see value being inserted
This commit is contained in:
@@ -804,7 +804,7 @@ LUA_API void lua_rawset (lua_State *L, int idx) {
|
|||||||
api_checknelems(L, 2);
|
api_checknelems(L, 2);
|
||||||
t = index2addr(L, idx);
|
t = index2addr(L, idx);
|
||||||
api_check(L, ttistable(t), "table expected");
|
api_check(L, ttistable(t), "table expected");
|
||||||
setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
|
luaH_setvalue(L, hvalue(t), L->top-2, L->top-1);
|
||||||
invalidateTMcache(hvalue(t));
|
invalidateTMcache(hvalue(t));
|
||||||
luaC_barrierback(L, gcvalue(t), L->top-1);
|
luaC_barrierback(L, gcvalue(t), L->top-1);
|
||||||
L->top -= 2;
|
L->top -= 2;
|
||||||
@@ -833,7 +833,7 @@ LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
|
|||||||
t = index2addr(L, idx);
|
t = index2addr(L, idx);
|
||||||
api_check(L, ttistable(t), "table expected");
|
api_check(L, ttistable(t), "table expected");
|
||||||
setpvalue(&k, cast(void *, p));
|
setpvalue(&k, cast(void *, p));
|
||||||
setobj2t(L, luaH_set(L, hvalue(t), &k), L->top - 1);
|
luaH_setvalue(L, hvalue(t), &k, L->top-1);
|
||||||
luaC_barrierback(L, gcvalue(t), L->top - 1);
|
luaC_barrierback(L, gcvalue(t), L->top - 1);
|
||||||
L->top--;
|
L->top--;
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
|
|||||||
@@ -290,7 +290,7 @@ static void freeexp (FuncState *fs, expdesc *e) {
|
|||||||
|
|
||||||
static int addk (FuncState *fs, TValue *key, TValue *v) {
|
static int addk (FuncState *fs, TValue *key, TValue *v) {
|
||||||
lua_State *L = fs->ls->L;
|
lua_State *L = fs->ls->L;
|
||||||
TValue *idx = luaH_set(L, fs->h, key);
|
const TValue *idx = luaH_get(fs->h, key);
|
||||||
Proto *f = fs->f;
|
Proto *f = fs->f;
|
||||||
int k, oldsize;
|
int k, oldsize;
|
||||||
if (ttisnumber(idx)) {
|
if (ttisnumber(idx)) {
|
||||||
@@ -306,7 +306,9 @@ static int addk (FuncState *fs, TValue *key, TValue *v) {
|
|||||||
k = fs->nk;
|
k = fs->nk;
|
||||||
/* numerical value does not need GC barrier;
|
/* numerical value does not need GC barrier;
|
||||||
table has no metatable, so it does not need to invalidate cache */
|
table has no metatable, so it does not need to invalidate cache */
|
||||||
setnvalue(idx, cast_num(k));
|
TValue kv;
|
||||||
|
setnvalue(&kv, cast_num(k));
|
||||||
|
luaH_setvalue (L, fs->h, key, &kv);
|
||||||
luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
|
luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
|
||||||
while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
|
while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
|
||||||
setobj(L, &f->k[k], v);
|
setobj(L, &f->k[k], v);
|
||||||
|
|||||||
@@ -123,17 +123,17 @@ l_noret luaX_syntaxerror (LexState *ls, const char *msg) {
|
|||||||
*/
|
*/
|
||||||
TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
|
TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
|
||||||
lua_State *L = ls->L;
|
lua_State *L = ls->L;
|
||||||
TValue *o; /* entry for `str' */
|
|
||||||
TString *ts = luaS_newlstr(L, str, l); /* create new string */
|
TString *ts = luaS_newlstr(L, str, l); /* create new string */
|
||||||
setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */
|
setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */
|
||||||
o = luaH_set(L, ls->fs->h, L->top - 1);
|
const TValue *o = luaH_get(ls->fs->h, L->top - 1);
|
||||||
if (ttisnil(o)) { /* not in use yet? (see 'addK') */
|
if (ttisnil(o)) { /* not in use yet? (see 'addK') */
|
||||||
/* boolean value does not need GC barrier;
|
/* boolean value does not need GC barrier;
|
||||||
table has no metatable, so it does not need to invalidate cache */
|
table has no metatable, so it does not need to invalidate cache */
|
||||||
setbvalue(o, 1); /* t[string] = true */
|
TValue truev;
|
||||||
|
setbvalue(&truev, 1);
|
||||||
|
luaH_setupdate(L, ls->fs->h, L->top - 1, &truev, o);
|
||||||
luaC_checkGC(L);
|
luaC_checkGC(L);
|
||||||
}
|
} else { /* string already present */
|
||||||
else { /* string already present */
|
|
||||||
ts = rawtsvalue(keyfromval(o)); /* re-use value previously stored */
|
ts = rawtsvalue(keyfromval(o)); /* re-use value previously stored */
|
||||||
}
|
}
|
||||||
L->top--; /* remove string from stack */
|
L->top--; /* remove string from stack */
|
||||||
|
|||||||
@@ -326,7 +326,7 @@ void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) {
|
|||||||
if (!ttisnil(gval(old))) {
|
if (!ttisnil(gval(old))) {
|
||||||
/* doesn't need barrier/invalidate cache, as entry was
|
/* doesn't need barrier/invalidate cache, as entry was
|
||||||
already present in the table */
|
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))
|
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
|
** put new key in its main position; otherwise (colliding node is in its main
|
||||||
** position), new key goes to an empty position.
|
** 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;
|
Node *mp;
|
||||||
if (ttisnil(key)) luaG_runerror(L, "table index is nil");
|
if (ttisnil(key)) luaG_runerror(L, "table index is nil");
|
||||||
else if (ttisnumber(key) && luai_numisnan(L, nvalue(key)))
|
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? */
|
if (n == NULL) { /* cannot find a free place? */
|
||||||
rehash(L, t, key); /* grow table */
|
rehash(L, t, key); /* grow table */
|
||||||
/* whatever called 'newkey' take care of TM cache and GC barrier */
|
/* 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));
|
lua_assert(!isdummy(n));
|
||||||
othern = mainposition(t, gkey(mp));
|
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
|
** beware: when using this function you probably need to check a GC
|
||||||
** barrier and invalidate the TM cache.
|
** barrier and invalidate the TM cache.
|
||||||
*/
|
*/
|
||||||
TValue *luaH_set (lua_State *L, Table *t, const TValue *key) {
|
void luaH_setupdate (lua_State *L, Table *t, const TValue *key, TValue *value, const TValue *getres) {
|
||||||
const TValue *p = luaH_get(t, key);
|
TValue *cell;
|
||||||
if (p != luaO_nilobject)
|
if (getres != luaO_nilobject) {
|
||||||
return cast(TValue *, p);
|
cell = cast(TValue *, getres);
|
||||||
else return luaH_newkey(L, t, key);
|
} 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) {
|
void luaH_setint (lua_State *L, Table *t, int key, TValue *value) {
|
||||||
const TValue *p = luaH_getint(t, key);
|
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 */
|
** Try to find a boundary in table `t'. A `boundary' is an integer index
|
||||||
j++;
|
** 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 */
|
/* find `i' and `j' such that i is present and j is not */
|
||||||
while (!ttisnil(luaH_getint(t, j))) {
|
while (!ttisnil(luaH_getint(t, j))) {
|
||||||
i = 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)
|
#if defined(LUA_DEBUG)
|
||||||
|
|
||||||
|
|||||||
@@ -24,10 +24,10 @@
|
|||||||
|
|
||||||
LUAI_FUNC const TValue *luaH_getint (Table *t, int key);
|
LUAI_FUNC const TValue *luaH_getint (Table *t, int key);
|
||||||
LUAI_FUNC void luaH_setint (lua_State *L, Table *t, int key, TValue *value);
|
LUAI_FUNC void luaH_setint (lua_State *L, Table *t, int key, TValue *value);
|
||||||
|
LUAI_FUNC void luaH_setvalue (lua_State *L, Table *t, const TValue *key, TValue *value);
|
||||||
|
LUAI_FUNC void luaH_setupdate (lua_State *L, Table *t, const TValue *key, TValue *value, const TValue *getres);
|
||||||
LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key);
|
LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key);
|
||||||
LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key);
|
LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key);
|
||||||
LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key);
|
|
||||||
LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key);
|
|
||||||
LUAI_FUNC Table *luaH_new (lua_State *L);
|
LUAI_FUNC Table *luaH_new (lua_State *L);
|
||||||
LUAI_FUNC void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize);
|
LUAI_FUNC void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize);
|
||||||
LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize);
|
LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize);
|
||||||
|
|||||||
@@ -139,19 +139,13 @@ void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
|
|||||||
const TValue *tm;
|
const TValue *tm;
|
||||||
if (ttistable(t)) { /* `t' is a table? */
|
if (ttistable(t)) { /* `t' is a table? */
|
||||||
Table *h = hvalue(t);
|
Table *h = hvalue(t);
|
||||||
TValue *oldval = cast(TValue *, luaH_get(h, key));
|
const TValue *oldval = luaH_get(h, key);
|
||||||
/* if previous value is not nil, there must be a previous entry
|
/* if previous value is not nil, there must be a previous entry
|
||||||
in the table; moreover, a metamethod has no relevance */
|
in the table; moreover, a metamethod has no relevance */
|
||||||
if (!ttisnil(oldval) ||
|
if (!ttisnil(oldval) ||
|
||||||
/* previous value is nil; must check the metamethod */
|
/* previous value is nil; must check the metamethod */
|
||||||
((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL &&
|
((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL)) {
|
||||||
/* no metamethod; is there a previous entry in the table? */
|
luaH_setupdate(L, h, key, val, oldval);
|
||||||
(oldval != luaO_nilobject ||
|
|
||||||
/* no previous entry; must create one. (The next test is
|
|
||||||
always true; we only need the assignment.) */
|
|
||||||
(oldval = luaH_newkey(L, h, key), 1)))) {
|
|
||||||
/* no metamethod and (now) there is an entry with given key */
|
|
||||||
setobj2t(L, oldval, val); /* assign new value to that entry */
|
|
||||||
invalidateTMcache(h);
|
invalidateTMcache(h);
|
||||||
luaC_barrierback(L, obj2gco(h), val);
|
luaC_barrierback(L, obj2gco(h), val);
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user