From 8b33d813a3d47d4daeaaef03b7e42a51f6931f79 Mon Sep 17 00:00:00 2001 From: meir Date: Sun, 6 Feb 2022 13:43:18 +0200 Subject: Added support for Lua readonly tables. The new feature can be turned off and on using the new `lua_enablereadonlytable` Lua API. --- deps/lua/src/lapi.c | 14 ++++++++++++++ deps/lua/src/ldebug.c | 1 - deps/lua/src/lobject.h | 3 ++- deps/lua/src/ltable.c | 1 + deps/lua/src/lua.h | 2 ++ deps/lua/src/lvm.c | 2 ++ 6 files changed, 21 insertions(+), 2 deletions(-) (limited to 'deps') diff --git a/deps/lua/src/lapi.c b/deps/lua/src/lapi.c index 5d5145d2e..1a9455629 100644 --- a/deps/lua/src/lapi.c +++ b/deps/lua/src/lapi.c @@ -674,6 +674,8 @@ LUA_API void lua_rawset (lua_State *L, int idx) { api_checknelems(L, 2); t = index2adr(L, idx); api_check(L, ttistable(t)); + if (hvalue(t)->readonly) + luaG_runerror(L, "Attempt to modify a readonly table"); setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1); luaC_barriert(L, hvalue(t), L->top-1); L->top -= 2; @@ -687,6 +689,8 @@ LUA_API void lua_rawseti (lua_State *L, int idx, int n) { api_checknelems(L, 1); o = index2adr(L, idx); api_check(L, ttistable(o)); + if (hvalue(o)->readonly) + luaG_runerror(L, "Attempt to modify a readonly table"); setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1); luaC_barriert(L, hvalue(o), L->top-1); L->top--; @@ -709,6 +713,8 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { } switch (ttype(obj)) { case LUA_TTABLE: { + if (hvalue(obj)->readonly) + luaG_runerror(L, "Attempt to modify a readonly table"); hvalue(obj)->metatable = mt; if (mt) luaC_objbarriert(L, hvalue(obj), mt); @@ -1085,3 +1091,11 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { return name; } +LUA_API void lua_enablereadonlytable (lua_State *L, int objindex, int enabled) { + const TValue* o = index2adr(L, objindex); + api_check(L, ttistable(o)); + Table* t = hvalue(o); + api_check(L, t != hvalue(registry(L))); + t->readonly = enabled; +} + diff --git a/deps/lua/src/ldebug.c b/deps/lua/src/ldebug.c index 50ad3d380..4940e65a6 100644 --- a/deps/lua/src/ldebug.c +++ b/deps/lua/src/ldebug.c @@ -80,7 +80,6 @@ LUA_API int lua_gethookcount (lua_State *L) { return L->basehookcount; } - LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { int status; CallInfo *ci; diff --git a/deps/lua/src/lobject.h b/deps/lua/src/lobject.h index f1e447ef3..10e46b178 100644 --- a/deps/lua/src/lobject.h +++ b/deps/lua/src/lobject.h @@ -337,7 +337,8 @@ typedef struct Node { typedef struct Table { CommonHeader; - lu_byte flags; /* 1<

array = NULL; t->sizearray = 0; t->lsizenode = 0; + t->readonly = 0; t->node = cast(Node *, dummynode); setarrayvector(L, t, narray); setnodevector(L, t, nhash); diff --git a/deps/lua/src/lua.h b/deps/lua/src/lua.h index a4b73e743..e478d14c0 100644 --- a/deps/lua/src/lua.h +++ b/deps/lua/src/lua.h @@ -358,6 +358,8 @@ struct lua_Debug { int i_ci; /* active function */ }; +LUA_API void lua_enablereadonlytable (lua_State *L, int index, int enabled); + /* }====================================================================== */ diff --git a/deps/lua/src/lvm.c b/deps/lua/src/lvm.c index e0a0cd852..5cd313b5e 100644 --- a/deps/lua/src/lvm.c +++ b/deps/lua/src/lvm.c @@ -138,6 +138,8 @@ 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); + if (h->readonly) + luaG_runerror(L, "Attempt to modify a readonly table"); TValue *oldval = luaH_set(L, h, key); /* do a primitive set */ if (!ttisnil(oldval) || /* result is no nil? */ (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */ -- cgit v1.2.1