Skip to content

Commit

Permalink
Improve luaH_initmetatable
Browse files Browse the repository at this point in the history
  • Loading branch information
Sainan committed Aug 24, 2023
1 parent b35b9e0 commit cf1ee74
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/ltable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,19 +643,22 @@ Table *luaH_new (lua_State *L) {

#ifndef PLUTO_NO_DEFAULT_TABLE_METATABLE
void luaH_initmetatable (lua_State *L, Table *t) {
lua_pushnil(L); /* space on the stack where the metatable will go */
if (ttisnil(&G(L)->table_mt)) {
/* create metatable */
Table *table_mt = luaH_new(L);
sethvalue(L, &G(L)->table_mt, table_mt);
lua_pushnil(L);
/* assign to stack value */
sethvalue(L, s2v(L->top.p - 1), table_mt);
/* set __index */
lua_pushstring(L, "__index");
lua_getglobal(L, "table");
lua_settable(L, -3);
}
else {
lua_pushnil(L);
sethvalue(L, s2v(L->top.p - 1), hvalue(&G(L)->table_mt));
}
/* set stack value as metatable and pop */
t->metatable = hvalue(s2v(L->top.p - 1));
lua_pop(L, 1);
}
Expand Down

0 comments on commit cf1ee74

Please sign in to comment.