From 1e972d40f1d1d7833228c64a4c694a1777c99911 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Fri, 18 Feb 2011 19:21:19 -0800 Subject: Bring lua extension up to date with new symtab APIs. --- Makefile | 2 +- lang_ext/lua/test.lua | 13 ++++++++++++- lang_ext/lua/upb.c | 38 +++++++++++++++++++++++++++++++++----- src/upb.h | 2 ++ src/upb_decoder.c | 6 ++++-- 5 files changed, 52 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 6801f63..3e11417 100644 --- a/Makefile +++ b/Makefile @@ -150,7 +150,7 @@ src/upb_decoder_x64.o: src/upb_decoder_x64.asm src/upb_decoder_x64.lo: src/upb_decoder_x64.asm $(E) NASM $< - $(Q) nasm -Ox src/upb_decoder_x64.asm -o src/upb_decoder_x64.o -f macho64 + $(Q) nasm -Ox src/upb_decoder_x64.asm -o src/upb_decoder_x64.lo -f macho64 # Function to expand a wildcard pattern recursively. rwildcard=$(strip $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2)$(filter $(subst *,%,$2),$d))) diff --git a/lang_ext/lua/test.lua b/lang_ext/lua/test.lua index a5b8d82..a49cebc 100644 --- a/lang_ext/lua/test.lua +++ b/lang_ext/lua/test.lua @@ -3,7 +3,18 @@ require "upb" symtab = upb.symtab() -symtab:add_descriptorproto() +f = io.open("../../src/descriptor.pb") +if not f then + error("Couldn't open descriptor.pb, try running 'make descriptorgen'") +end +symtab:parsedesc(f:read("*all")) + +f = io.open("../../benchmarks/google_messages.proto.pb") +if not f then + error("Couldn't open google_messages.proto.pb, try running 'make benchmarks'") +end +symtab:parsedesc(f:read("*all")) + 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 c520b08..bf1eb02 100644 --- a/lang_ext/lua/upb.c +++ b/lang_ext/lua/upb.c @@ -9,11 +9,27 @@ #include #include "lauxlib.h" #include "upb_def.h" +#include "upb_glue.h" void lupb_pushstring(lua_State *L, upb_string *str) { lua_pushlstring(L, upb_string_getrobuf(str), upb_string_len(str)); } +void lupb_checkstatus(lua_State *L, upb_status *s) { + if (!upb_ok(s)) { + upb_printerr(s); + // Need to copy the string to the stack, so we can free it and not leak + // it (since luaL_error() does not return). + size_t len = upb_string_len(s->str); + char buf[len+1]; + memcpy(buf, upb_string_getrobuf(s->str), len); + buf[len] = '\0'; + upb_status_uninit(s); + luaL_error(L, "%s", buf); + } + upb_status_uninit(s); +} + /* object cache ***************************************************************/ // We cache all the lua objects (userdata) we vend in a weak table, indexed by @@ -83,7 +99,7 @@ static void lupb_def_getorcreate(lua_State *L, upb_def *def) { luaL_error(L, "unknown deftype %d", def->type); type_name = NULL; // Placate the compiler. } - return lupb_cache_getorcreate(L, def, type_name, lupb_nop, lupb_def_unref); + lupb_cache_getorcreate(L, def, type_name, lupb_nop, lupb_def_unref); } // msgdef @@ -285,17 +301,22 @@ static int lupb_symtab_getdefs(lua_State *L) { return 1; } -static int lupb_symtab_add_descriptorproto(lua_State *L) { +static int lupb_symtab_parsedesc(lua_State *L) { lupb_symtab *s = lupb_symtab_check(L, 1); - upb_symtab_add_descriptorproto(s->symtab); - return 0; // No args to return. + size_t len; + const char *str = luaL_checklstring(L, 2, &len); + upb_status status = UPB_STATUS_INIT; + upb_string desc_str = UPB_STACK_STRING_LEN(str, len); + upb_parsedesc(s->symtab, &desc_str, &status); + lupb_checkstatus(L, &status); + return 0; } static const struct luaL_Reg lupb_symtab_m[] = { - {"add_descriptorproto", lupb_symtab_add_descriptorproto}, //{"addfds", lupb_symtab_addfds}, {"getdefs", lupb_symtab_getdefs}, {"lookup", lupb_symtab_lookup}, + {"parsedesc", lupb_symtab_parsedesc}, //{"resolve", lupb_symtab_resolve}, {NULL, NULL} }; @@ -314,8 +335,15 @@ static int lupb_symtab_new(lua_State *L) { return 1; } +static int lupb_getfdsdef(lua_State *L) { + lupb_cache_getorcreate( + L, upb_getfdsdef(), "upb.msgdef", lupb_nop, lupb_def_unref); + return 1; +} + static const struct luaL_Reg lupb_toplevel_m[] = { {"symtab", lupb_symtab_new}, + {"fdsdef", lupb_getfdsdef}, {NULL, NULL} }; diff --git a/src/upb.h b/src/upb.h index 0698748..fe576eb 100644 --- a/src/upb.h +++ b/src/upb.h @@ -265,6 +265,8 @@ INLINE void upb_status_init(upb_status *status) { void upb_status_uninit(upb_status *status); +// Caller owns a ref on the returned string. +upb_string *upb_status_tostring(upb_status *status); void upb_printerr(upb_status *status); void upb_clearerr(upb_status *status); void upb_seterr(upb_status *status, enum upb_status_code code, const char *msg, diff --git a/src/upb_decoder.c b/src/upb_decoder.c index 40ab790..8b10522 100644 --- a/src/upb_decoder.c +++ b/src/upb_decoder.c @@ -327,7 +327,7 @@ void upb_decoder_run(upb_src *src, upb_status *status) { //d->bytes_parsed_slow = 0; // TODO: handle UPB_SKIPSUBMSG -#define CHECK_FLOW(expr) if ((expr) == UPB_BREAK) { /*assert(!upb_ok(status));*/ goto err; } +#define CHECK_FLOW(expr) if ((expr) == UPB_BREAK) { /*assert(!upb_ok(status));*/ goto callback_err; } #define CHECK(expr) if (!expr) { assert(!upb_ok(status)); goto err; } CHECK_FLOW(upb_dispatch_startmsg(&d->dispatcher)); @@ -469,11 +469,13 @@ void upb_decoder_run(upb_src *src, upb_status *status) { CHECK_FLOW(upb_dispatch_value(&d->dispatcher, f, val)); } -err: +callback_err: upb_copyerr(status, d->dispatcher.top->handlers.status); if (upb_ok(status)) { upb_seterr(status, UPB_ERROR, "Callback returned UPB_BREAK"); } +err: + assert(!upb_ok(status)); } void upb_decoder_sethandlers(upb_src *src, upb_handlers *handlers) { -- cgit v1.2.3