Revive Lua extension.

It builds and you can inspect a symtab.
Still need to expose streaming and message
based interfaces.
pull/13171/head
Joshua Haberman 14 years ago
parent bdb28b5a45
commit 7af638ff2d
  1. 29
      Makefile
  2. 1
      README
  3. 9
      lang_ext/lua/test.lua
  4. 34
      lang_ext/lua/upb.c

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

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

@ -0,0 +1,9 @@
require "upb"
symtab = upb.symtab()
symtab:add_descriptorproto()
for _, def in ipairs(symtab:getdefs(-1)) do
print(def:name())
end

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

Loading…
Cancel
Save