From 7af638ff2d1ed136e47c0db4b62d2d0be920bb0b Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Mon, 14 Feb 2011 23:52:29 -0800 Subject: Revive Lua extension. It builds and you can inspect a symtab. Still need to expose streaming and message based interfaces. --- Makefile | 29 ++++++++++++++++------------- README | 1 + lang_ext/lua/test.lua | 9 +++++++++ lang_ext/lua/upb.c | 34 +++++++++++++++++++++++++--------- 4 files changed, 51 insertions(+), 22 deletions(-) create mode 100644 lang_ext/lua/test.lua diff --git a/Makefile b/Makefile index d09ab89..00905d7 100644 --- a/Makefile +++ b/Makefile @@ -136,19 +136,6 @@ src/upb_def.lo: src/upb_def.c rwildcard=$(strip $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2)$(filter $(subst *,%,$2),$d))) -ifeq ($(shell uname), Darwin) - CPPFLAGS += -I/usr/include/lua5.1 - LDFLAGS += -L/usr/local/lib -llua -else - CFLAGS += $(strip $(shell pkg-config --silence-errors --cflags lua || pkg-config --cflags lua5.1)) - LDFLAGS += $(strip $(shell pkg-config --silence-errors --libs lua || pkg-config --libs lua5.1)) -endif - - - -lang_ext/lua/upb.so: lang_ext/lua/upb.lo - $(CC) $(CFLAGS) $(CPPFLAGS) -shared -o $@ $< src/libupb_pic.a - # Regenerating the auto-generated files in src/. src/descriptor.pb: src/descriptor.proto @@ -326,3 +313,19 @@ benchmarks/b.parsetostruct_googlemessage2.proto2_compiled: \ -DMESSAGE_HFILE=\"google_messages.pb.h\" \ benchmarks/google_messages.pb.cc -lprotobuf -lpthread + +# Lua extension ################################################################## + +ifeq ($(shell uname), Darwin) + CPPFLAGS += -I/usr/include/lua5.1 + LDFLAGS += -L/usr/local/lib -llua +else + CFLAGS += $(strip $(shell pkg-config --silence-errors --cflags lua || pkg-config --cflags lua5.1)) + LDFLAGS += $(strip $(shell pkg-config --silence-errors --libs lua || pkg-config --libs lua5.1)) +endif + +LUAEXT=lang_ext/lua/upb.so +lua: $(LUAEXT) +lang_ext/lua/upb.so: lang_ext/lua/upb.lo $(LIBUPB_PIC) + @echo CC lang_ext/lua/upb.c + @$(CC) $(CFLAGS) $(CPPFLAGS) -shared -o $@ $< src/libupb_pic.a diff --git a/README b/README index e2c9951..a241946 100644 --- a/README +++ b/README @@ -9,6 +9,7 @@ $ make Other useful targets: $ make test $ make benchmark +$ make lua (requires lua libraries to be installed) Issue tracking is on Google Code: http://code.google.com/p/upb/issues/list diff --git a/lang_ext/lua/test.lua b/lang_ext/lua/test.lua new file mode 100644 index 0000000..a5b8d82 --- /dev/null +++ b/lang_ext/lua/test.lua @@ -0,0 +1,9 @@ + +require "upb" + +symtab = upb.symtab() + +symtab:add_descriptorproto() +for _, def in ipairs(symtab:getdefs(-1)) do + print(def:name()) +end diff --git a/lang_ext/lua/upb.c b/lang_ext/lua/upb.c index 5ab07ba..c520b08 100644 --- a/lang_ext/lua/upb.c +++ b/lang_ext/lua/upb.c @@ -101,6 +101,12 @@ static int lupb_msgdef_gc(lua_State *L) { static void lupb_fielddef_getorcreate(lua_State *L, upb_fielddef *f); +static int lupb_msgdef_name(lua_State *L) { + upb_msgdef *m = lupb_msgdef_check(L, 1); + lupb_pushstring(L, m->base.fqname); + return 1; +} + static int lupb_msgdef_fieldbyname(lua_State *L) { upb_msgdef *m = lupb_msgdef_check(L, 1); size_t len; @@ -135,27 +141,36 @@ static const struct luaL_Reg lupb_msgdef_mm[] = { static const struct luaL_Reg lupb_msgdef_m[] = { {"fieldbyname", lupb_msgdef_fieldbyname}, {"fieldbynum", lupb_msgdef_fieldbynum}, + {"name", lupb_msgdef_name}, {NULL, NULL} }; // enumdef -static lupb_def *lupb_enumdef_check(lua_State *L, int narg) { - return luaL_checkudata(L, narg, "upb.enumdef"); +static upb_enumdef *lupb_enumdef_check(lua_State *L, int narg) { + lupb_def *ldef = luaL_checkudata(L, narg, "upb.enumdef"); + return upb_downcast_enumdef(ldef->def); } static int lupb_enumdef_gc(lua_State *L) { - lupb_def *ldef = lupb_enumdef_check(L, 1); - upb_def_unref(ldef->def); + upb_enumdef *e = lupb_enumdef_check(L, 1); + upb_def_unref(UPB_UPCAST(e)); return 0; } +static int lupb_enumdef_name(lua_State *L) { + upb_enumdef *e = lupb_enumdef_check(L, 1); + lupb_pushstring(L, e->base.fqname); + return 1; +} + static const struct luaL_Reg lupb_enumdef_mm[] = { {"__gc", lupb_enumdef_gc}, {NULL, NULL} }; static const struct luaL_Reg lupb_enumdef_m[] = { + {"name", lupb_enumdef_name}, {NULL, NULL} }; @@ -258,11 +273,10 @@ static int lupb_symtab_getdefs(lua_State *L) { upb_def **defs = upb_symtab_getdefs(s->symtab, &count, type); // Create the table in which we will return the defs. - lua_createtable(L, 0, count); + lua_createtable(L, count, 0); for (int i = 0; i < count; i++) { upb_def *def = defs[i]; - upb_string *name = def->fqname; - lupb_pushstring(L, name); + lua_pushnumber(L, i + 1); // 1-based array. lupb_def_getorcreate(L, def); // Add it to our return table. lua_settable(L, -3); @@ -309,11 +323,13 @@ static const struct luaL_Reg lupb_toplevel_m[] = { static void lupb_register_type(lua_State *L, const char *name, const luaL_Reg *m, const luaL_Reg *mm) { luaL_newmetatable(L, name); - luaL_register(L, NULL, mm); + luaL_register(L, NULL, mm); // Register all mm in the metatable. lua_createtable(L, 0, 0); if (m) { + // Methods go in the mt's __index method. This implies that you can't + // implement __index and also set methods yourself. luaL_register(L, NULL, m); - lua_setfield(L, -2, "__index"); + lua_setfield(L, -2, "__index"); } lua_pop(L, 1); // The mt. } -- cgit v1.2.3