X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=tools%2Fcpkg2c.mll;h=2e76dd96f70404680a08662c2f417aa9e4f3617c;hb=b10940c18a5b0c8276d308b0601efac585d33966;hp=b5ef25a27af267ad8e91d71703ea70189e8cfbaa;hpb=5f0d69cc13a116be64a243c07bad259af7d90397;p=apps%2Fmadmutt.git diff --git a/tools/cpkg2c.mll b/tools/cpkg2c.mll index b5ef25a..2e76dd9 100644 --- a/tools/cpkg2c.mll +++ b/tools/cpkg2c.mll @@ -312,11 +312,12 @@ and ext_pkg pkg = parse { let rettype = parse_rettype (lnum lexbuf) pkg.file ret in let args = parse_args (lnum lexbuf) pkg.file args in + let f, l = pkg.file, lnum lexbuf in let body = ext_body pkg (B.create 1024) lexbuf in let m = { rettype = rettype; args = args; fname = fname; - body = body }, pkg.file, lnum lexbuf in + body = body }, f, l in ext_pkg {pkg with methods = m::pkg.methods} lexbuf } | '}' sp* ';' (sp* '\n' as s)? @@ -511,7 +512,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 +519,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 +562,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 +578,7 @@ and ext_member m f = parse int luaopen_%s(lua_State *L) { - int mt, methods; + int mt, members, methods; %s_init(); @@ -584,18 +586,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 +609,7 @@ int luaopen_%s(lua_State *L) lua_setmetatable(L, methods); - lua_pop(L, 2); /* drop mt + methods */ + lua_pop(L, 3); return 1; }