diff --git a/src/ltable.cpp b/src/ltable.cpp index b11202acb6..6d466ff098 100644 --- a/src/ltable.cpp +++ b/src/ltable.cpp @@ -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); }