From cfdb9907cb87d15eaab72ceefbfa42fd7a4c3127 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Sat, 11 May 2013 16:45:38 -0700 Subject: Synced with 3 months of Google-internal development. Major changes: - Got rid of all bytestream interfaces in favor of using regular handlers. - new Pipeline object represents a upb pipeline, does bump allocation internally to manage memory. - proto2 support now can handle extensions. --- bindings/cpp/upb/pb/decoder.hpp | 81 ----------------- bindings/lua/table.c | 8 +- bindings/lua/test.lua | 1 + bindings/lua/upb.c | 192 +++++++++++++++++++++++++++------------- 4 files changed, 136 insertions(+), 146 deletions(-) delete mode 100644 bindings/cpp/upb/pb/decoder.hpp (limited to 'bindings') diff --git a/bindings/cpp/upb/pb/decoder.hpp b/bindings/cpp/upb/pb/decoder.hpp deleted file mode 100644 index 950e9e2..0000000 --- a/bindings/cpp/upb/pb/decoder.hpp +++ /dev/null @@ -1,81 +0,0 @@ -// -// upb - a minimalist implementation of protocol buffers. -// -// Copyright (c) 2011 Google Inc. See LICENSE for details. -// Author: Josh Haberman -// -// upb::Decoder is a high performance, streaming decoder for protobuf -// data that works by getting its input data from a ubp::ByteRegion and calling -// into a upb::Handlers. -// -// A DecoderPlan contains whatever data structures and generated (JIT-ted) code -// are necessary to decode protobuf data of a specific type to a specific set -// of handlers. By generating the plan ahead of time, we avoid having to -// redo this work every time we decode. -// -// A DecoderPlan is threadsafe, meaning that it can be used concurrently by -// different upb::Decoders in different threads. However, the upb::Decoders are -// *not* thread-safe. - -#ifndef UPB_PB_DECODER_HPP -#define UPB_PB_DECODER_HPP - -#include "upb/pb/decoder.h" - -#include "upb/bytestream.h" -#include "upb/upb.h" - -namespace upb { - -class DecoderPlan : public upb_decoderplan { - public: - static DecoderPlan* New(const Handlers* h, bool allow_jit) { - return static_cast(upb_decoderplan_new(h, allow_jit)); - } - void Unref() { upb_decoderplan_unref(this); } - - // Returns true if the plan contains JIT-ted code. This may not be the same - // as the "allowjit" parameter to the constructor if support for JIT-ting was - // not compiled in. - bool HasJitCode() { return upb_decoderplan_hasjitcode(this); } - - private: - DecoderPlan() {} // Only constructed by New -}; - -class Decoder : public upb_decoder { - public: - Decoder() { upb_decoder_init(this); } - ~Decoder() { upb_decoder_uninit(this); } - - // Resets the plan that the decoder will parse from. This will also reset the - // decoder's input to be uninitialized -- ResetInput() must be called before - // parsing can occur. The plan must live until the decoder is destroyed or - // reset to a different plan. - // - // Must be called before ResetInput() or Decode(). - void ResetPlan(DecoderPlan* plan) { upb_decoder_resetplan(this, plan); } - - // Resets the input of the decoder. This puts it in a state where it has not - // seen any data, and expects the next data to be from the beginning of a new - // protobuf. - // - // ResetInput() must be called before Decode() but may be called more than - // once. "input" must live until the decoder destroyed or ResetInput is - // called again. "c" is the closure that will be passed to the handlers. - void ResetInput(ByteRegion* byte_region, void* c) { - upb_decoder_resetinput(this, byte_region, c); - } - - // Decodes serialized data (calling Handlers as the data is parsed) until - // error or EOF (see status() for details). - Status::Success Decode() { return upb_decoder_decode(this); } - - const upb::Status& status() { - return static_cast(*upb_decoder_status(this)); - } -}; - -} // namespace upb - -#endif diff --git a/bindings/lua/table.c b/bindings/lua/table.c index 31b92d2..450730a 100644 --- a/bindings/lua/table.c +++ b/bindings/lua/table.c @@ -31,16 +31,16 @@ static void lupbtable_setnum(lua_State *L, int tab, const char *key, lua_setfield(L, tab - 1, key); } -static void lupbtable_pushval(lua_State *L, upb_value val, upb_ctype_t type) { +static void lupbtable_pushval(lua_State *L, _upb_value val, upb_ctype_t type) { switch (type) { case UPB_CTYPE_INT32: - lua_pushnumber(L, upb_value_getint32(val)); + lua_pushnumber(L, val.int32); break; case UPB_CTYPE_PTR: - lupb_def_pushwrapper(L, upb_value_getptr(val), NULL); + lupb_def_pushwrapper(L, val.ptr, NULL); break; case UPB_CTYPE_CSTR: - lua_pushstring(L, upb_value_getcstr(val)); + lua_pushstring(L, val.cstr); break; default: luaL_error(L, "Unexpected type: %d", type); diff --git a/bindings/lua/test.lua b/bindings/lua/test.lua index 6b162a9..fc4db70 100644 --- a/bindings/lua/test.lua +++ b/bindings/lua/test.lua @@ -13,6 +13,7 @@ function test_fielddef() assert_false(f:is_frozen()) assert_nil(f:number()) assert_nil(f:name()) + assert_nil(f:type()) assert_equal(upb.LABEL_OPTIONAL, f:label()) f:set_name("foo_field") diff --git a/bindings/lua/upb.c b/bindings/lua/upb.c index 1a1d7c0..29de9f0 100644 --- a/bindings/lua/upb.c +++ b/bindings/lua/upb.c @@ -14,7 +14,7 @@ #include #include "lauxlib.h" #include "bindings/lua/upb.h" -#include "upb/bytestream.h" +#include "upb/handlers.h" #include "upb/pb/glue.h" // Lua metatable types. @@ -75,7 +75,7 @@ static uint32_t lupb_checkint32(lua_State *L, int narg, const char *name) { // Converts a number or bool from Lua -> upb_value. static upb_value lupb_getvalue(lua_State *L, int narg, upb_fieldtype_t type) { upb_value val; - if (type == UPB_TYPE(BOOL)) { + if (type == UPB_TYPE_BOOL) { if (!lua_isboolean(L, narg)) luaL_error(L, "Must explicitly pass true or false for boolean fields"); upb_value_setbool(&val, lua_toboolean(L, narg)); @@ -83,41 +83,35 @@ static upb_value lupb_getvalue(lua_State *L, int narg, upb_fieldtype_t type) { // Numeric type. lua_Number num = luaL_checknumber(L, narg); switch (type) { - case UPB_TYPE(INT32): - case UPB_TYPE(SINT32): - case UPB_TYPE(SFIXED32): - case UPB_TYPE(ENUM): + case UPB_TYPE_INT32: + case UPB_TYPE_ENUM: if (num > INT32_MAX || num < INT32_MIN || num != rint(num)) luaL_error(L, "Cannot convert %f to 32-bit integer", num); upb_value_setint32(&val, num); break; - case UPB_TYPE(INT64): - case UPB_TYPE(SINT64): - case UPB_TYPE(SFIXED64): + case UPB_TYPE_INT64: if (num > INT64_MAX || num < INT64_MIN || num != rint(num)) luaL_error(L, "Cannot convert %f to 64-bit integer", num); upb_value_setint64(&val, num); break; - case UPB_TYPE(UINT32): - case UPB_TYPE(FIXED32): + case UPB_TYPE_UINT32: if (num > UINT32_MAX || num < 0 || num != rint(num)) luaL_error(L, "Cannot convert %f to unsigned 32-bit integer", num); upb_value_setuint32(&val, num); break; - case UPB_TYPE(UINT64): - case UPB_TYPE(FIXED64): + case UPB_TYPE_UINT64: if (num > UINT64_MAX || num < 0 || num != rint(num)) luaL_error(L, "Cannot convert %f to unsigned 64-bit integer", num); upb_value_setuint64(&val, num); break; - case UPB_TYPE(DOUBLE): + case UPB_TYPE_DOUBLE: if (num > DBL_MAX || num < -DBL_MAX) { // This could happen if lua_Number was long double. luaL_error(L, "Cannot convert %f to double", num); } upb_value_setdouble(&val, num); break; - case UPB_TYPE(FLOAT): + case UPB_TYPE_FLOAT: if (num > FLT_MAX || num < -FLT_MAX) luaL_error(L, "Cannot convert %f to float", num); upb_value_setfloat(&val, num); @@ -131,34 +125,21 @@ static upb_value lupb_getvalue(lua_State *L, int narg, upb_fieldtype_t type) { // Converts a upb_value -> Lua value. static void lupb_pushvalue(lua_State *L, upb_value val, upb_fieldtype_t type) { switch (type) { - case UPB_TYPE(INT32): - case UPB_TYPE(SINT32): - case UPB_TYPE(SFIXED32): - case UPB_TYPE(ENUM): + case UPB_TYPE_INT32: + case UPB_TYPE_ENUM: lua_pushnumber(L, upb_value_getint32(val)); break; - case UPB_TYPE(INT64): - case UPB_TYPE(SINT64): - case UPB_TYPE(SFIXED64): + case UPB_TYPE_INT64: lua_pushnumber(L, upb_value_getint64(val)); break; - case UPB_TYPE(UINT32): - case UPB_TYPE(FIXED32): + case UPB_TYPE_UINT32: lua_pushnumber(L, upb_value_getuint32(val)); break; - case UPB_TYPE(UINT64): - case UPB_TYPE(FIXED64): + case UPB_TYPE_UINT64: lua_pushnumber(L, upb_value_getuint64(val)); break; - case UPB_TYPE(DOUBLE): + case UPB_TYPE_DOUBLE: lua_pushnumber(L, upb_value_getdouble(val)); break; - case UPB_TYPE(FLOAT): + case UPB_TYPE_FLOAT: lua_pushnumber(L, upb_value_getfloat(val)); break; - case UPB_TYPE(BOOL): + case UPB_TYPE_BOOL: lua_pushboolean(L, upb_value_getbool(val)); break; - case UPB_TYPE(STRING): - case UPB_TYPE(BYTES): { - const upb_byteregion *r = upb_value_getbyteregion(val); - size_t len; - const char *str = upb_byteregion_getptr(r, 0, &len); - lua_pushlstring(L, str, len); - } default: luaL_error(L, "internal error"); } } @@ -341,7 +322,7 @@ static void lupb_fielddef_dosetdefault(lua_State *L, upb_fielddef *f, int type = lua_type(L, narg); upb_fieldtype_t upbtype = upb_fielddef_type(f); if (type == LUA_TSTRING) { - if (!upb_fielddef_isstring(f) && upbtype != UPB_TYPE(ENUM)) + if (!upb_fielddef_isstring(f) && upbtype != UPB_TYPE_ENUM) luaL_argerror(L, narg, "field does not expect a string default"); size_t len; const char *str = lua_tolstring(L, narg, &len); @@ -387,6 +368,21 @@ static void lupb_fielddef_dosettype(lua_State *L, upb_fielddef *f, int narg) { luaL_argerror(L, narg, "invalid field type"); } +static void lupb_fielddef_dosetintfmt(lua_State *L, upb_fielddef *f, int narg) { + int32_t intfmt = luaL_checknumber(L, narg); + if (!upb_fielddef_settype(f, intfmt)) + luaL_argerror(L, narg, "invalid field intfmt"); +} + +static void lupb_fielddef_dosettagdelim(lua_State *L, upb_fielddef *f, + int narg) { + if (!lua_isboolean(L, narg)) + luaL_argerror(L, narg, "tagdelim value must be boolean"); + int32_t tagdelim = luaL_checknumber(L, narg); + if (!upb_fielddef_settagdelim(f, tagdelim)) + luaL_argerror(L, narg, "invalid field tagdelim"); +} + // Setter API calls. These use the setter functions above. static int lupb_fielddef_setdefault(lua_State *L) { @@ -425,6 +421,18 @@ static int lupb_fielddef_settype(lua_State *L) { return 0; } +static int lupb_fielddef_setintfmt(lua_State *L) { + upb_fielddef *f = lupb_fielddef_checkmutable(L, 1); + lupb_fielddef_dosetintfmt(L, f, 2); + return 0; +} + +static int lupb_fielddef_settagdelim(lua_State *L) { + upb_fielddef *f = lupb_fielddef_checkmutable(L, 1); + lupb_fielddef_dosettagdelim(L, f, 2); + return 0; +} + // Constructor and other methods. static int lupb_fielddef_new(lua_State *L) { @@ -470,11 +478,22 @@ static int lupb_fielddef_default(lua_State *L) { const upb_fielddef *f = lupb_fielddef_check(L, 1); upb_fieldtype_t type = upb_fielddef_type(f); if (upb_fielddef_default_is_symbolic(f)) - type = UPB_TYPE(STRING); + type = UPB_TYPE_STRING; lupb_pushvalue(L, upb_fielddef_default(f), type); return 1; } +static int lupb_fielddef_getsel(lua_State *L) { + const upb_fielddef *f = lupb_fielddef_check(L, 1); + upb_selector_t sel; + if (upb_getselector(f, luaL_checknumber(L, 2), &sel)) { + lua_pushnumber(L, sel); + return 1; + } else { + return 0; + } +} + static int lupb_fielddef_label(lua_State *L) { const upb_fielddef *f = lupb_fielddef_check(L, 1); lua_pushnumber(L, upb_fielddef_label(f)); @@ -530,7 +549,22 @@ static int lupb_fielddef_subdefname(lua_State *L) { static int lupb_fielddef_type(lua_State *L) { const upb_fielddef *f = lupb_fielddef_check(L, 1); - lua_pushnumber(L, upb_fielddef_type(f)); + if (upb_fielddef_typeisset(f)) + lua_pushnumber(L, upb_fielddef_type(f)); + else + lua_pushnil(L); + return 1; +} + +static int lupb_fielddef_intfmt(lua_State *L) { + const upb_fielddef *f = lupb_fielddef_check(L, 1); + lua_pushnumber(L, upb_fielddef_intfmt(f)); + return 1; +} + +static int lupb_fielddef_istagdelim(lua_State *L) { + const upb_fielddef *f = lupb_fielddef_check(L, 1); + lua_pushboolean(L, upb_fielddef_istagdelim(f)); return 1; } @@ -545,7 +579,10 @@ static const struct luaL_Reg lupb_fielddef_m[] = { LUPB_COMMON_DEF_METHODS {"default", lupb_fielddef_default}, + {"getsel", lupb_fielddef_getsel}, {"has_subdef", lupb_fielddef_hassubdef}, + {"intfmt", lupb_fielddef_intfmt}, + {"istagdelim", lupb_fielddef_istagdelim}, {"label", lupb_fielddef_label}, {"msgdef", lupb_fielddef_msgdef}, {"name", lupb_def_fullname}, // name() is just an alias for fullname() @@ -561,6 +598,8 @@ static const struct luaL_Reg lupb_fielddef_m[] = { {"set_subdef", lupb_fielddef_setsubdef}, {"set_subdef_name", lupb_fielddef_setsubdefname}, {"set_type", lupb_fielddef_settype}, + {"set_intfmt", lupb_fielddef_setintfmt}, + {"set_tagdelim", lupb_fielddef_settagdelim}, // Internal-only. {"_selector_base", lupb_fielddef_selectorbase}, @@ -1040,28 +1079,44 @@ int luaopen_upb(lua_State *L) { lua_call(L, 1, 0); // Register constants. - lupb_setfieldi(L, "LABEL_OPTIONAL", UPB_LABEL(OPTIONAL)); - lupb_setfieldi(L, "LABEL_REQUIRED", UPB_LABEL(REQUIRED)); - lupb_setfieldi(L, "LABEL_REPEATED", UPB_LABEL(REPEATED)); - - lupb_setfieldi(L, "TYPE_DOUBLE", UPB_TYPE(DOUBLE)); - lupb_setfieldi(L, "TYPE_FLOAT", UPB_TYPE(FLOAT)); - lupb_setfieldi(L, "TYPE_INT64", UPB_TYPE(INT64)); - lupb_setfieldi(L, "TYPE_UINT64", UPB_TYPE(UINT64)); - lupb_setfieldi(L, "TYPE_INT32", UPB_TYPE(INT32)); - lupb_setfieldi(L, "TYPE_FIXED64", UPB_TYPE(FIXED64)); - lupb_setfieldi(L, "TYPE_FIXED32", UPB_TYPE(FIXED32)); - lupb_setfieldi(L, "TYPE_BOOL", UPB_TYPE(BOOL)); - lupb_setfieldi(L, "TYPE_STRING", UPB_TYPE(STRING)); - lupb_setfieldi(L, "TYPE_GROUP", UPB_TYPE(GROUP)); - lupb_setfieldi(L, "TYPE_MESSAGE", UPB_TYPE(MESSAGE)); - lupb_setfieldi(L, "TYPE_BYTES", UPB_TYPE(BYTES)); - lupb_setfieldi(L, "TYPE_UINT32", UPB_TYPE(UINT32)); - lupb_setfieldi(L, "TYPE_ENUM", UPB_TYPE(ENUM)); - lupb_setfieldi(L, "TYPE_SFIXED32", UPB_TYPE(SFIXED32)); - lupb_setfieldi(L, "TYPE_SFIXED64", UPB_TYPE(SFIXED64)); - lupb_setfieldi(L, "TYPE_SINT32", UPB_TYPE(SINT32)); - lupb_setfieldi(L, "TYPE_SINT64", UPB_TYPE(SINT64)); + lupb_setfieldi(L, "LABEL_OPTIONAL", UPB_LABEL_OPTIONAL); + lupb_setfieldi(L, "LABEL_REQUIRED", UPB_LABEL_REQUIRED); + lupb_setfieldi(L, "LABEL_REPEATED", UPB_LABEL_REPEATED); + + lupb_setfieldi(L, "TYPE_DOUBLE", UPB_TYPE_DOUBLE); + lupb_setfieldi(L, "TYPE_FLOAT", UPB_TYPE_FLOAT); + lupb_setfieldi(L, "TYPE_INT64", UPB_TYPE_INT64); + lupb_setfieldi(L, "TYPE_UINT64", UPB_TYPE_UINT64); + lupb_setfieldi(L, "TYPE_INT32", UPB_TYPE_INT32); + lupb_setfieldi(L, "TYPE_BOOL", UPB_TYPE_BOOL); + lupb_setfieldi(L, "TYPE_STRING", UPB_TYPE_STRING); + lupb_setfieldi(L, "TYPE_MESSAGE", UPB_TYPE_MESSAGE); + lupb_setfieldi(L, "TYPE_BYTES", UPB_TYPE_BYTES); + lupb_setfieldi(L, "TYPE_UINT32", UPB_TYPE_UINT32); + lupb_setfieldi(L, "TYPE_ENUM", UPB_TYPE_ENUM); + + lupb_setfieldi(L, "INTFMT_VARIABLE", UPB_INTFMT_VARIABLE); + lupb_setfieldi(L, "INTFMT_FIXED", UPB_INTFMT_FIXED); + lupb_setfieldi(L, "INTFMT_ZIGZAG", UPB_INTFMT_ZIGZAG); + + lupb_setfieldi(L, "DESCRIPTOR_TYPE_DOUBLE", UPB_DESCRIPTOR_TYPE_DOUBLE); + lupb_setfieldi(L, "DESCRIPTOR_TYPE_FLOAT", UPB_DESCRIPTOR_TYPE_FLOAT); + lupb_setfieldi(L, "DESCRIPTOR_TYPE_INT64", UPB_DESCRIPTOR_TYPE_INT64); + lupb_setfieldi(L, "DESCRIPTOR_TYPE_UINT64", UPB_DESCRIPTOR_TYPE_UINT64); + lupb_setfieldi(L, "DESCRIPTOR_TYPE_INT32", UPB_DESCRIPTOR_TYPE_INT32); + lupb_setfieldi(L, "DESCRIPTOR_TYPE_FIXED64", UPB_DESCRIPTOR_TYPE_FIXED64); + lupb_setfieldi(L, "DESCRIPTOR_TYPE_FIXED32", UPB_DESCRIPTOR_TYPE_FIXED32); + lupb_setfieldi(L, "DESCRIPTOR_TYPE_BOOL", UPB_DESCRIPTOR_TYPE_BOOL); + lupb_setfieldi(L, "DESCRIPTOR_TYPE_STRING", UPB_DESCRIPTOR_TYPE_STRING); + lupb_setfieldi(L, "DESCRIPTOR_TYPE_GROUP", UPB_DESCRIPTOR_TYPE_GROUP); + lupb_setfieldi(L, "DESCRIPTOR_TYPE_MESSAGE", UPB_DESCRIPTOR_TYPE_MESSAGE); + lupb_setfieldi(L, "DESCRIPTOR_TYPE_BYTES", UPB_DESCRIPTOR_TYPE_BYTES); + lupb_setfieldi(L, "DESCRIPTOR_TYPE_UINT32", UPB_DESCRIPTOR_TYPE_UINT32); + lupb_setfieldi(L, "DESCRIPTOR_TYPE_ENUM", UPB_DESCRIPTOR_TYPE_ENUM); + lupb_setfieldi(L, "DESCRIPTOR_TYPE_SFIXED32", UPB_DESCRIPTOR_TYPE_SFIXED32); + lupb_setfieldi(L, "DESCRIPTOR_TYPE_SFIXED64", UPB_DESCRIPTOR_TYPE_SFIXED64); + lupb_setfieldi(L, "DESCRIPTOR_TYPE_SINT32", UPB_DESCRIPTOR_TYPE_SINT32); + lupb_setfieldi(L, "DESCRIPTOR_TYPE_SINT64", UPB_DESCRIPTOR_TYPE_SINT64); lupb_setfieldi(L, "DEF_MSG", UPB_DEF_MSG); lupb_setfieldi(L, "DEF_FIELD", UPB_DEF_FIELD); @@ -1069,6 +1124,21 @@ int luaopen_upb(lua_State *L) { lupb_setfieldi(L, "DEF_SERVICE", UPB_DEF_SERVICE); lupb_setfieldi(L, "DEF_ANY", UPB_DEF_ANY); + lupb_setfieldi(L, "UPB_HANDLER_INT32", UPB_HANDLER_INT32); + lupb_setfieldi(L, "UPB_HANDLER_INT64", UPB_HANDLER_INT64); + lupb_setfieldi(L, "UPB_HANDLER_UINT32", UPB_HANDLER_UINT32); + lupb_setfieldi(L, "UPB_HANDLER_UINT64", UPB_HANDLER_UINT64); + lupb_setfieldi(L, "UPB_HANDLER_FLOAT", UPB_HANDLER_FLOAT); + lupb_setfieldi(L, "UPB_HANDLER_DOUBLE", UPB_HANDLER_DOUBLE); + lupb_setfieldi(L, "UPB_HANDLER_BOOL", UPB_HANDLER_BOOL); + lupb_setfieldi(L, "UPB_HANDLER_STARTSTR", UPB_HANDLER_STARTSTR); + lupb_setfieldi(L, "UPB_HANDLER_STRING", UPB_HANDLER_STRING); + lupb_setfieldi(L, "UPB_HANDLER_ENDSTR", UPB_HANDLER_ENDSTR); + lupb_setfieldi(L, "UPB_HANDLER_STARTSUBMSG", UPB_HANDLER_STARTSUBMSG); + lupb_setfieldi(L, "UPB_HANDLER_ENDSUBMSG", UPB_HANDLER_ENDSUBMSG); + lupb_setfieldi(L, "UPB_HANDLER_STARTSEQ", UPB_HANDLER_STARTSEQ); + lupb_setfieldi(L, "UPB_HANDLER_ENDSEQ", UPB_HANDLER_ENDSEQ); + return 1; // Return package table. } -- cgit v1.2.3