eris: add general purpose 'flag bits' to lua tables'

This commit is contained in:
2021-07-25 20:34:38 -04:00
parent 9f342854e1
commit bcd5f044d2
5 changed files with 50 additions and 9 deletions

View File

@@ -905,6 +905,9 @@ static void
p_literaltable(Info *info) { /* ... tbl */
eris_checkstack(info->L, 3);
unsigned char bits = lua_getflagbits(info->L, -1);
WRITE_VALUE(bits, uint8_t);
/* Persist all key / value pairs. */
lua_pushnil(info->L); /* ... tbl nil */
while (lua_next(info->L, -2)) { /* ... tbl k v */
@@ -943,6 +946,8 @@ u_literaltable(Info *info) { /* ... */
eris_checkstack(info->L, 3);
lua_newtable(info->L); /* ... tbl */
unsigned char bits = READ_VALUE(uint8_t);
lua_setflagbits(info->L, -1, bits);
/* Preregister table for handling of cycles (keys, values or metatable). */
registerobject(info);

View File

@@ -898,6 +898,38 @@ LUA_API void lua_setuservalue (lua_State *L, int idx) {
}
LUA_API void lua_setflagbits (lua_State *L, int idx, unsigned char bits) {
TValue *obj;
Table *tab;
lua_lock(L);
obj = index2addr(L, idx);
api_check(L, ttistable(obj), "table expected");
tab = hvalue(obj);
tab->flagbits = bits;
lua_unlock(L);
}
LUA_API void lua_modflagbits (lua_State *L, int idx, unsigned char set, unsigned char clear) {
TValue *obj;
Table *tab;
lua_lock(L);
obj = index2addr(L, idx);
api_check(L, ttistable(obj), "table expected");
tab = hvalue(obj);
tab->flagbits |= set;
tab->flagbits &= (~clear);
lua_unlock(L);
}
LUA_API unsigned char lua_getflagbits (lua_State *L, int idx) {
TValue *obj;
Table *tab;
obj = index2addr(L, idx);
api_check(L, ttistable(obj), "table expected");
tab = hvalue(obj);
return tab->flagbits;
}
/*
** `load' and `call' functions (run Lua code)
*/

View File

@@ -556,6 +556,7 @@ typedef struct Node {
typedef struct Table {
CommonHeader;
lu_byte flags; /* 1<<p means tagmethod(p) is not present */
lu_byte flagbits; /* flag bits reserved for the user */
lu_byte lsizenode; /* log2 of size of `node' array */
int sizearray; /* size of `array' array */
int nnkeys; /* number of non-nil keys in both array and hash */

View File

@@ -509,6 +509,7 @@ Table *luaH_new (lua_State *L) {
Table *t = &luaC_newobj(L, LUA_TTABLE, sizeof(Table), NULL, 0)->h;
t->metatable = NULL;
t->flags = cast_byte(~0);
t->flagbits = 0;
t->array = NULL;
t->sizearray = 0;
t->node = cast(Node *, dummynode);

View File

@@ -237,15 +237,17 @@ LUA_API void (lua_getuservalue) (lua_State *L, int idx);
/*
** set functions (stack -> Lua)
*/
LUA_API void (lua_setglobal) (lua_State *L, const char *var);
LUA_API void (lua_settable) (lua_State *L, int idx);
LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k);
LUA_API void (lua_rawset) (lua_State *L, int idx);
LUA_API void (lua_rawseti) (lua_State *L, int idx, int n);
LUA_API void (lua_rawsetp) (lua_State *L, int idx, const void *p);
LUA_API int (lua_setmetatable) (lua_State *L, int objindex);
LUA_API void (lua_setuservalue) (lua_State *L, int idx);
LUA_API void (lua_setglobal) (lua_State *L, const char *var);
LUA_API void (lua_settable) (lua_State *L, int idx);
LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k);
LUA_API void (lua_rawset) (lua_State *L, int idx);
LUA_API void (lua_rawseti) (lua_State *L, int idx, int n);
LUA_API void (lua_rawsetp) (lua_State *L, int idx, const void *p);
LUA_API int (lua_setmetatable) (lua_State *L, int objindex);
LUA_API void (lua_setuservalue) (lua_State *L, int idx);
LUA_API void (lua_setflagbits) (lua_State *L, int idx, unsigned char bits);
LUA_API void (lua_modflagbits) (lua_State *L, int idx, unsigned char set, unsigned char clear);
LUA_API unsigned char (lua_getflagbits) (lua_State *L, int idx);
/*
** 'load' and 'call' functions (load and run Lua code)