From 3bf8b1fadb6f51c463b1376391f87b0195f057e4 Mon Sep 17 00:00:00 2001 From: Josh Yelon Date: Tue, 6 Jul 2021 19:32:20 -0400 Subject: [PATCH] eris: refactoring table API so that table methods can see value being inserted --- luprex/eris-master/src/lapi.c | 4 +-- luprex/eris-master/src/lcode.c | 6 ++-- luprex/eris-master/src/llex.c | 10 +++--- luprex/eris-master/src/ltable.c | 60 +++++++++++++-------------------- luprex/eris-master/src/ltable.h | 4 +-- luprex/eris-master/src/lvm.c | 12 ++----- 6 files changed, 40 insertions(+), 56 deletions(-) diff --git a/luprex/eris-master/src/lapi.c b/luprex/eris-master/src/lapi.c index 7fd8409f..5c7794bb 100644 --- a/luprex/eris-master/src/lapi.c +++ b/luprex/eris-master/src/lapi.c @@ -804,7 +804,7 @@ LUA_API void lua_rawset (lua_State *L, int idx) { api_checknelems(L, 2); t = index2addr(L, idx); 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)); luaC_barrierback(L, gcvalue(t), L->top-1); L->top -= 2; @@ -833,7 +833,7 @@ LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { t = index2addr(L, idx); api_check(L, ttistable(t), "table expected"); 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); L->top--; lua_unlock(L); diff --git a/luprex/eris-master/src/lcode.c b/luprex/eris-master/src/lcode.c index 820b95c0..d39de516 100644 --- a/luprex/eris-master/src/lcode.c +++ b/luprex/eris-master/src/lcode.c @@ -290,7 +290,7 @@ static void freeexp (FuncState *fs, expdesc *e) { static int addk (FuncState *fs, TValue *key, TValue *v) { 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; int k, oldsize; if (ttisnumber(idx)) { @@ -306,7 +306,9 @@ static int addk (FuncState *fs, TValue *key, TValue *v) { k = fs->nk; /* numerical value does not need GC barrier; 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"); while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]); setobj(L, &f->k[k], v); diff --git a/luprex/eris-master/src/llex.c b/luprex/eris-master/src/llex.c index 32cdcf1c..ee6d8030 100644 --- a/luprex/eris-master/src/llex.c +++ b/luprex/eris-master/src/llex.c @@ -123,17 +123,17 @@ l_noret luaX_syntaxerror (LexState *ls, const char *msg) { */ TString *luaX_newstring (LexState *ls, const char *str, size_t l) { lua_State *L = ls->L; - TValue *o; /* entry for `str' */ TString *ts = luaS_newlstr(L, str, l); /* create new string */ 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') */ /* boolean value does not need GC barrier; 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); - } - else { /* string already present */ + } else { /* string already present */ ts = rawtsvalue(keyfromval(o)); /* re-use value previously stored */ } L->top--; /* remove string from stack */ diff --git a/luprex/eris-master/src/ltable.c b/luprex/eris-master/src/ltable.c index 85a31071..e8a43392 100644 --- a/luprex/eris-master/src/ltable.c +++ b/luprex/eris-master/src/ltable.c @@ -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) diff --git a/luprex/eris-master/src/ltable.h b/luprex/eris-master/src/ltable.h index d69449b2..4969cd35 100644 --- a/luprex/eris-master/src/ltable.h +++ b/luprex/eris-master/src/ltable.h @@ -24,10 +24,10 @@ 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_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_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 void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize); LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize); diff --git a/luprex/eris-master/src/lvm.c b/luprex/eris-master/src/lvm.c index 141b9fd1..5dcd3c54 100644 --- a/luprex/eris-master/src/lvm.c +++ b/luprex/eris-master/src/lvm.c @@ -139,19 +139,13 @@ void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { const TValue *tm; if (ttistable(t)) { /* `t' is a table? */ 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 in the table; moreover, a metamethod has no relevance */ if (!ttisnil(oldval) || /* previous value is nil; must check the metamethod */ - ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL && - /* no metamethod; is there a previous entry in the table? */ - (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 */ + ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL)) { + luaH_setupdate(L, h, key, val, oldval); invalidateTMcache(h); luaC_barrierback(L, obj2gco(h), val); return;