This commit is contained in:
ccuser44 2025-01-16 23:18:03 +00:00 committed by GitHub
commit 518a1ec517
Signed by: DevComp
GPG key ID: B5690EEEBB952194
4 changed files with 7 additions and 8 deletions

View file

@ -49,7 +49,7 @@ struct NativeContext
int (*luaH_getn)(Table* t) = nullptr; int (*luaH_getn)(Table* t) = nullptr;
Table* (*luaH_new)(lua_State* L, int narray, int lnhash) = nullptr; Table* (*luaH_new)(lua_State* L, int narray, int lnhash) = nullptr;
Table* (*luaH_clone)(lua_State* L, Table* tt) = nullptr; Table* (*luaH_clone)(lua_State* L, Table* tt, bool raw) = nullptr;
void (*luaH_resizearray)(lua_State* L, Table* t, int nasize) = nullptr; void (*luaH_resizearray)(lua_State* L, Table* t, int nasize) = nullptr;
TValue* (*luaH_setnum)(lua_State* L, Table* t, int key); TValue* (*luaH_setnum)(lua_State* L, Table* t, int key);

View file

@ -793,11 +793,10 @@ int luaH_getn(Table* t)
} }
} }
Table* luaH_clone(lua_State* L, Table* tt) Table* luaH_clone(lua_State* L, Table* tt, bool raw)
{ {
Table* t = luaM_newgco(L, Table, sizeof(Table), L->activememcat); Table* t = luaM_newgco(L, Table, sizeof(Table), L->activememcat);
luaC_init(L, t, LUA_TTABLE); luaC_init(L, t, LUA_TTABLE);
t->metatable = tt->metatable;
t->tmcache = tt->tmcache; t->tmcache = tt->tmcache;
t->array = NULL; t->array = NULL;
t->sizearray = 0; t->sizearray = 0;
@ -808,6 +807,9 @@ Table* luaH_clone(lua_State* L, Table* tt)
t->node = cast_to(LuaNode*, dummynode); t->node = cast_to(LuaNode*, dummynode);
t->lastfree = 0; t->lastfree = 0;
if (!raw) // Prevent unauthorized assigning of locked metatables
t->metatable = tt->metatable;
if (tt->sizearray) if (tt->sizearray)
{ {
t->array = luaM_newarray(L, tt->sizearray, TValue, t->memcat); t->array = luaM_newarray(L, tt->sizearray, TValue, t->memcat);

View file

@ -27,7 +27,7 @@ LUAI_FUNC void luaH_resizehash(lua_State* L, Table* t, int nhsize);
LUAI_FUNC void luaH_free(lua_State* L, Table* t, struct lua_Page* page); LUAI_FUNC void luaH_free(lua_State* L, Table* t, struct lua_Page* page);
LUAI_FUNC int luaH_next(lua_State* L, Table* t, StkId key); LUAI_FUNC int luaH_next(lua_State* L, Table* t, StkId key);
LUAI_FUNC int luaH_getn(Table* t); LUAI_FUNC int luaH_getn(Table* t);
LUAI_FUNC Table* luaH_clone(lua_State* L, Table* tt); LUAI_FUNC Table* luaH_clone(lua_State* L, Table* tt, bool raw);
LUAI_FUNC void luaH_clear(Table* tt); LUAI_FUNC void luaH_clear(Table* tt);
#define luaH_setslot(L, t, slot, key) (invalidateTMcache(t), (slot == luaO_nilobject ? luaH_newkey(L, t, key) : cast_to(TValue*, slot))) #define luaH_setslot(L, t, slot, key) (invalidateTMcache(t), (slot == luaO_nilobject ? luaH_newkey(L, t, key) : cast_to(TValue*, slot)))

View file

@ -585,10 +585,7 @@ static int tisfrozen(lua_State* L)
static int tclone(lua_State* L) static int tclone(lua_State* L)
{ {
luaL_checktype(L, 1, LUA_TTABLE); luaL_checktype(L, 1, LUA_TTABLE);
luaL_argcheck(L, !luaL_getmetafield(L, 1, "__metatable"), 1, "table has a protected metatable"); Table* tt = luaH_clone(L, hvalue(L->base), luaL_getmetafield(L, 1, "__metatable") ? true : false);
Table* tt = luaH_clone(L, hvalue(L->base));
TValue v; TValue v;
sethvalue(L, &v, tt); sethvalue(L, &v, tt);
luaA_pushobject(L, &v); luaA_pushobject(L, &v);