From e1f82ec5b248a12c5bfef139f1f27bf4292ee3a0 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Sun, 1 Apr 2007 23:43:44 +0200 Subject: [PATCH] [lua] make our modules be extensible (accept new values). Signed-off-by: Pierre Habouzit --- tools/cpkg2c.mll | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/tools/cpkg2c.mll b/tools/cpkg2c.mll index b5ef25a..82be124 100644 --- a/tools/cpkg2c.mll +++ b/tools/cpkg2c.mll @@ -511,7 +511,6 @@ and ext_member m f = parse printf "static int luaM_%s_index(lua_State *L)\n{\n" pkg.name; printf " const char *idx = luaL_checkstring(L, 2);\n\n"; printf " switch (mlua_which_token(idx, -1)) {\n"; - printf " default:\n"; List.iter (function (m, _, _) -> printf " case LTK_%s:\n" (upper m.mname); let push, f, l = (snd m.typ).push in @@ -519,7 +518,8 @@ and ext_member m f = parse printf " %s;\n" (tplize push (sprintf "%s.%s" pkg.name m.mname)); printf " return 1;\n" ) pkg.members; - printf " lua_rawget(L, lua_upvalueindex(2));\n"; + printf " default:\n"; + printf " lua_rawget(L, lua_upvalueindex(1));\n"; printf " return 1;\n"; printf " }\n}\n\n"; @@ -561,6 +561,7 @@ and ext_member m f = parse printf " return 1;\n" ) pkg.members; printf " default:\n"; + printf " lua_rawset(L, lua_upvalueindex(1));\n"; printf " return 1;\n"; printf " }\n}\n"; @@ -576,7 +577,7 @@ and ext_member m f = parse int luaopen_%s(lua_State *L) { - int mt, methods; + int mt, members, methods; %s_init(); @@ -584,18 +585,20 @@ int luaopen_%s(lua_State *L) luaL_openlib(L, \"%s\", luaM_%s_methods, 0); methods = lua_gettop(L); + lua_newtable(L); /* for new members */ + members = lua_gettop(L); + /* create metatable for %s, add it to the registry */ luaL_newmetatable(L, \"%s\"); mt = lua_gettop(L); lua_pushliteral(L, \"__index\"); - lua_pushvalue(L, mt); /* upvalue 1 */ - lua_pushvalue(L, methods); /* upvalue 2 */ - lua_pushcclosure(L, &luaM_%s_index, 2); + lua_pushvalue(L, members); /* upvalue 1 */ + lua_pushcclosure(L, &luaM_%s_index, 1); lua_rawset(L, mt); /* set mt.__index */ lua_pushliteral(L, \"__newindex\"); - lua_newtable(L); /* for new members */ + lua_pushvalue(L, members); /* upvalue 1 */ lua_pushcclosure(L, &luaM_%s_newindex, 1); lua_rawset(L, mt); /* set mt.__newindex */ @@ -605,7 +608,7 @@ int luaopen_%s(lua_State *L) lua_setmetatable(L, methods); - lua_pop(L, 2); /* drop mt + methods */ + lua_pop(L, 3); return 1; } -- 2.20.1