Allow modules to have their own __index for magic purposes.

Also fix stack leaks.

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Pierre Habouzit 2008-06-28 00:52:12 +02:00 committed by Julien Danjou
parent 1b6543025c
commit 577cec41b1
1 changed files with 8 additions and 6 deletions

14
lua.c
View File

@ -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