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:
parent
1b6543025c
commit
577cec41b1
14
lua.c
14
lua.c
|
@ -428,13 +428,15 @@ luaA_openlib(lua_State *L, const char *name,
|
||||||
const struct luaL_reg methods[],
|
const struct luaL_reg methods[],
|
||||||
const struct luaL_reg meta[])
|
const struct luaL_reg meta[])
|
||||||
{
|
{
|
||||||
luaL_newmetatable(L, name);
|
luaL_newmetatable(L, name); /* 1 */
|
||||||
lua_pushliteral(L, "__index");
|
lua_pushvalue(L, -1); /* dup metatable 2 */
|
||||||
lua_pushvalue(L, -2); /* dup metatable*/
|
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable 1 */
|
||||||
lua_settable(L, -3); /* metatable.__index = metatable */
|
|
||||||
|
|
||||||
luaL_register(L, NULL, meta);
|
luaL_register(L, NULL, meta); /* 1 */
|
||||||
luaL_register(L, name, methods);
|
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
|
static int
|
||||||
|
|
Loading…
Reference in New Issue