diff --git a/lua.c b/lua.c index 23292688..a18376e9 100644 --- a/lua.c +++ b/lua.c @@ -428,13 +428,15 @@ luaA_openlib(lua_State *L, const char *name, const struct luaL_reg methods[], const struct luaL_reg meta[]) { - luaL_newmetatable(L, name); - lua_pushliteral(L, "__index"); - lua_pushvalue(L, -2); /* dup metatable*/ - lua_settable(L, -3); /* metatable.__index = metatable */ + luaL_newmetatable(L, name); /* 1 */ + lua_pushvalue(L, -1); /* dup metatable 2 */ + lua_setfield(L, -2, "__index"); /* metatable.__index = metatable 1 */ - luaL_register(L, NULL, meta); - luaL_register(L, name, methods); + luaL_register(L, NULL, meta); /* 1 */ + luaL_register(L, name, methods); /* 2 */ + lua_pushvalue(L, -1); /* dup self as metatable 3 */ + lua_setmetatable(L, -2); /* set self as metatable 2 */ + lua_pop(L, 2); } static int