Skip to content

Commit

Permalink
[vm] Improve table clone
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas BOUQUET committed Feb 4, 2025
1 parent b0f1c1f commit 165dcbb
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions VM/src/ltablib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,13 +609,11 @@ static int tisfrozen(lua_State* L)
static void deepclone(lua_State *L) //Clone table at -1
{
lua_newtable(L); //O,T
lua_pushnil(L); //O,T,K
while (lua_next(L,-3)) { //O,T,K,V
lua_pushvalue(L,-2); //O,T,K,V,K
lua_insert(L,-2); //O,T,K,K,V
int iter=0;
while ((iter=lua_rawiter(L,-2,iter))>=0) { //O,T,K,V
if (lua_type(L,-1)==LUA_TTABLE)
deepclone(L);
lua_rawset(L,-4); //O,T,K
lua_rawset(L,-3); //O,T
}
lua_remove(L,-2); //T
}
Expand Down Expand Up @@ -646,13 +644,11 @@ static int tclone(lua_State* L)
}
lua_newtable(L);
}
lua_pushnil(L); //T,K
while (lua_next(L,1)) { //T,K,V
lua_pushvalue(L,-2); //T,K,V,K
lua_insert(L,-2); //T,K,K,V
if (deep&&(lua_type(L,-1)==LUA_TTABLE))
deepclone(L);
lua_rawset(L,-4); //T,K
int iter=0;
while ((iter=lua_rawiter(L,1,iter))>=0) { //T,K,V
if (deep&&(lua_type(L,-1)==LUA_TTABLE))
deepclone(L);
lua_rawset(L,-3); //T
}
return 1; //T
}
Expand Down

0 comments on commit 165dcbb

Please sign in to comment.