From 4b0c4ca7fb0aa9207af3398e04534b23fbb88f27 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Fri, 9 Sep 2016 14:03:25 -0700 Subject: New upb_msg code and Lua bindings around it. There are still some things that are unfinished, but we are at parity with what Lua had before. --- Makefile | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'Makefile') diff --git a/Makefile b/Makefile index 142a5cc..6521cb7 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,7 @@ all: lib tests tools/upbc lua python testall: test pythontest # Set this to have user-specific flags (especially things like -O0 and -g). -USER_CPPFLAGS?= +USER_CPPFLAGS?=-O0 -g -ffunction-sections -fdata-sections -I/usr/include/lua5.2 -UNDEBUG # Build with "make WITH_JIT=yes" (or anything besides "no") to enable the JIT. WITH_JIT=no @@ -150,8 +150,8 @@ make_objs_cc = $$(patsubst upb/$$(pc).cc,obj/upb/$$(pc).$(1),$$($$(call to_srcs, upb_SRCS = \ upb/def.c \ upb/handlers.c \ + upb/msg.c \ upb/refcounted.c \ - upb/shim/shim.c \ upb/symtab.c \ upb/table.c \ upb/upb.c \ @@ -479,9 +479,9 @@ LUA_LIB_DEPS = \ lib/libupb.descriptor_pic.a \ lib/libupb_pic.a \ -upb/bindings/lua/upb_c.so: upb/bindings/lua/upb.c $(LUA_LIB_DEPS) - $(E) CC upb/bindings/lua/upb.c - $(Q) $(CC) $(OPT) $(CSTD) $(WARNFLAGS) $(CPPFLAGS) $(CFLAGS) -fpic -shared -o $@ $< $(LUA_LDFLAGS) $(LUA_LIB_DEPS) +upb/bindings/lua/upb_c.so: upb/bindings/lua/upb.c upb/bindings/lua/def.c upb/bindings/lua/msg.c $(LUA_LIB_DEPS) + $(E) 'CC upb/bindings/lua/{upb,def,msg}.c' + $(Q) $(CC) $(OPT) $(CSTD) $(WARNFLAGS) $(CPPFLAGS) $(CFLAGS) -fpic -shared -o $@ $^ $(LUA_LDFLAGS) upb/bindings/lua/upb/table_c.so: upb/bindings/lua/upb/table.c lib/libupb_pic.a $(E) CC upb/bindings/lua/upb/table.c -- cgit v1.2.3 From 16ca9309b35ddadd103d1542818ea75f0a567c92 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Tue, 29 Nov 2016 18:44:41 +0000 Subject: Removed some temporary code and fixed a few tests. --- Makefile | 2 +- upb/bindings/lua/def.c | 24 ++++--- upb/msg.c | 168 +++---------------------------------------------- upb/upb.h | 5 +- 4 files changed, 23 insertions(+), 176 deletions(-) (limited to 'Makefile') diff --git a/Makefile b/Makefile index 6521cb7..62af57c 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,7 @@ all: lib tests tools/upbc lua python testall: test pythontest # Set this to have user-specific flags (especially things like -O0 and -g). -USER_CPPFLAGS?=-O0 -g -ffunction-sections -fdata-sections -I/usr/include/lua5.2 -UNDEBUG +USER_CPPFLAGS?= # Build with "make WITH_JIT=yes" (or anything besides "no") to enable the JIT. WITH_JIT=no diff --git a/upb/bindings/lua/def.c b/upb/bindings/lua/def.c index 7e41782..c2df140 100644 --- a/upb/bindings/lua/def.c +++ b/upb/bindings/lua/def.c @@ -54,11 +54,13 @@ void *lupb_refcounted_check(lua_State *L, int narg, const char *type) { void *ud = lua_touserdata(L, narg); void *ret; - if (ud) { - memcpy(&ret, ud, sizeof(ret)); - if (!ret) { - luaL_error(L, "called into dead object"); - } + if (!ud) { + luaL_typerror(L, narg, "refcounted"); + } + + memcpy(&ret, ud, sizeof(ret)); + if (!ret) { + luaL_error(L, "called into dead object"); } luaL_checkudata(L, narg, type); @@ -164,11 +166,13 @@ static const upb_def *lupb_def_check(lua_State *L, int narg) { void *ud, *ud2; ud = lua_touserdata(L, narg); - if (ud) { - memcpy(&ret, ud, sizeof(ret)); - if (!ret) { - luaL_error(L, "called into dead object"); - } + if (!ud) { + luaL_typerror(L, narg, "upb def"); + } + + memcpy(&ret, ud, sizeof(ret)); + if (!ret) { + luaL_error(L, "called into dead object"); } ud2 = luaL_testudata(L, narg, LUPB_MSGDEF); diff --git a/upb/msg.c b/upb/msg.c index 113dce9..3440b81 100644 --- a/upb/msg.c +++ b/upb/msg.c @@ -15,25 +15,6 @@ static size_t div_round_up(size_t n, size_t d) { return (n + d - 1) / d; } -#if 0 -static void *alloc(void *p, size_t *ofs, size_t size, size_t n, size_t align) { - char *obj; - - *ofs = align_up(*ofs, align); - obj = (char*)p + *ofs; - *ofs += n; - return *ofs <= size ? obj : NULL; -} - -static void addsize(size_t *size, size_t mysize, size_t myalign) { - *size = align_up(*size, myalign); - *size += mysize; -} - -static void upb_array_packedsize(const upb_array *arr, size_t *size); -static void upb_map_packedsize(const upb_map *map, size_t *size); -#endif - bool upb_fieldtype_mapkeyok(upb_fieldtype_t type) { return type == UPB_TYPE_BOOL || type == UPB_TYPE_INT32 || type == UPB_TYPE_UINT32 || type == UPB_TYPE_INT64 || @@ -95,46 +76,19 @@ static uint8_t upb_msg_fieldsize(const upb_fielddef *f) { } } +/* TODO(haberman): this is broken right now because upb_msgval can contain + * a char* / size_t pair, which is too big for a upb_value. To fix this + * we'll probably need to dynamically allocate a upb_msgval and store a + * pointer to that in the tables for extensions/maps. */ static upb_value upb_toval(upb_msgval val) { upb_value ret; - memcpy(&ret, &val, sizeof(upb_msgval)); + memcpy(&ret, &val, sizeof(upb_value)); return ret; } static upb_msgval upb_msgval_fromval(upb_value val) { upb_msgval ret; - memcpy(&ret, &val, sizeof(upb_msgval)); - return ret; -} - -static upb_value upb_msgval_toval(upb_msgval val, upb_fieldtype_t type) { - upb_value ret; - UPB_UNUSED(type); - memcpy(&ret, &val, sizeof(upb_msgval)); -#ifndef NDEBUG - switch (type) { - case UPB_TYPE_FLOAT: - return upb_value_float(upb_msgval_getfloat(val)); - case UPB_TYPE_DOUBLE: - return upb_value_double(upb_msgval_getdouble(val)); - case UPB_TYPE_BOOL: - return upb_value_bool(upb_msgval_getbool(val)); - case UPB_TYPE_STRING: - case UPB_TYPE_BYTES: - return upb_value_constptr(val.str.ptr); /* TODO */ - case UPB_TYPE_MESSAGE: - return upb_value_constptr(upb_msgval_getmsg(val)); - case UPB_TYPE_ENUM: - case UPB_TYPE_INT32: - return upb_value_int32(upb_msgval_getint32(val)); - case UPB_TYPE_UINT32: - return upb_value_uint32(upb_msgval_getuint32(val)); - case UPB_TYPE_INT64: - return upb_value_int64(upb_msgval_getint64(val)); - case UPB_TYPE_UINT64: - return upb_value_uint64(upb_msgval_getuint64(val)); - } -#endif + memcpy(&ret, &val, sizeof(upb_value)); return ret; } @@ -615,92 +569,6 @@ upb_alloc *upb_msg_alloc(const upb_msg *msg, const upb_msglayout *l) { return alloc; } -#if 0 -void upb_msg_packedsize(const void *msg, const upb_msglayout *l, size_t *size) { - upb_msg_field_iter i; - upb_inttable *exttab; - - if (!msg) { - return; - } - - addsize(size, l->size, l->align); - - exttab = upb_msg_trygetextdict(msg, l); - if (exttab) { - upb_inttable_packedsize(exttab, size); - } - - /* TODO(haberman): should there be an easier (and maybe more efficient) way of - * iterating over only strings/submessages/repeated fields? */ - for(upb_msg_field_begin(&i, l->msgdef); - !upb_msg_field_done(&i); - upb_msg_field_next(&i)) { - upb_fielddef *f = upb_msg_iter_field(&i); - if (upb_fielddef_isseq(f)) { - upb_array_packedsize(upb_msgval_getarr(upb_msg_get(msg, f, l)), size); - } else if (upb_fielddef_ismap(f)) { - upb_map_packedsize(upb_msgval_getmap(upb_msg_get(msg, f, l)), size); - } else if (upb_fielddef_issubmsg(f)) { - upb_msg_packedsize(upb_msgval_getmsg(upb_msg_get(msg, f, l)), - upb_msglayout_sublayout(l, f), size); - } else if (upb_fielddef_isstring(f)) { - addsize(size, upb_msg_get(msg, f, l).str.len, 1); - } - } -} - -void *upb_msg_pack(const void *msg, const upb_msglayout *l, - void *p, size_t *ofs, size_t size) { - upb_msg_field_iter i; - void *packed_msg = alloc(p, ofs, size, l->size, l->align); - upb_inttable *exttab = upb_msg_trygetextdict(msg, l); - - memcpy(packed_msg, msg, l->size); - - if (exttab) { - upb_inttable *packed_exttab = upb_inttable_pack(exttab, p, ofs, size); - - if (!packed_exttab) { - return NULL; - } - - /* TODO(haberman): need to copy over repeated/submsg/string exts. */ - DEREF(packed_msg, l->extdict_offset, upb_inttable*) = packed_exttab; - } - - for(upb_msg_field_begin(&i, l->msgdef); - !upb_msg_field_done(&i); - upb_msg_field_next(&i)) { - upb_fielddef *f = upb_msg_iter_field(&i); - upb_msgval v; - - if (upb_fielddef_isprimitive(f) && !upb_fielddef_isseq(f)) { - continue; - } - - v = upb_msg_get(msg, f, l); - - if (upb_fielddef_isseq(f)) { - v = upb_msgval_arr(upb_array_pack(upb_msgval_getarr(v), p, ofs, size)); - } else if (upb_fielddef_ismap(f)) { - v = upb_msgval_map(upb_map_pack(upb_msgval_getmap(v), p, ofs, size)); - } else if (upb_fielddef_issubmsg(f)) { - const upb_msglayout *sl = upb_msglayout_sublayout(l, f); - v = upb_msgval_msg(upb_msg_pack(upb_msgval_getmsg(v), sl, p, ofs, size)); - } else if (upb_fielddef_isstring(f)) { - void *packed_str = alloc(p, ofs, size, v.str.len, 1); - memcpy(packed_str, v.str.ptr, v.str.len); - v = upb_msgval_str(packed_str, v.str.len); - } - - upb_msg_set(packed_msg, f, v, l, NULL); - } - - return packed_msg; -} -#endif - bool upb_msg_has(const upb_msg *msg, const upb_fielddef *f, const upb_msglayout *l) { @@ -827,17 +695,6 @@ void upb_array_free(upb_array *arr) { upb_free(arr->alloc, arr); } -#if 0 -void upb_array_packedsize(const upb_array *arr, size_t *size) { - if (!arr) { - return; - } - - addsize(size, sizeof(upb_array), upb_alignof(upb_array)); - addsize(size, arr->len * arr->element_size, arr->element_size); -} -#endif - size_t upb_array_size(const upb_array *arr) { return arr->len; } @@ -947,17 +804,6 @@ size_t upb_map_sizeof(upb_fieldtype_t ktype, upb_fieldtype_t vtype) { return sizeof(upb_map); } -#if 0 -void upb_map_packedsize(const upb_map *map, size_t *size) { - if (!map) { - return; - } - - addsize(size, sizeof(upb_map), upb_alignof(upb_map)); - upb_strtable_packedsize(&map->strtab, size); -} -#endif - bool upb_map_init(upb_map *map, upb_fieldtype_t ktype, upb_fieldtype_t vtype, upb_alloc *a) { upb_ctype_t vtabtype = upb_fieldtotabtype(vtype); @@ -1028,7 +874,7 @@ bool upb_map_set(upb_map *map, upb_msgval key, upb_msgval val, upb_msgval *removed) { const char *key_str; size_t key_len; - upb_value tabval = upb_msgval_toval(val, map->val_type); + upb_value tabval = upb_toval(val); upb_value removedtabval; upb_alloc *a = map->alloc; diff --git a/upb/upb.h b/upb/upb.h index aafc993..a23f413 100644 --- a/upb/upb.h +++ b/upb/upb.h @@ -511,6 +511,7 @@ bool upb_arena_addcleanup(upb_arena *a, upb_cleanup_func *func, void *ud); size_t upb_arena_bytesallocated(const upb_arena *a); void upb_arena_setnextblocksize(upb_arena *a, size_t size); void upb_arena_setmaxblocksize(upb_arena *a, size_t size); +UPB_INLINE upb_alloc *upb_arena_alloc(upb_arena *a) { return (upb_alloc*)a; } UPB_END_EXTERN_C @@ -591,10 +592,6 @@ struct upb_arena { void *future2; }; -UPB_BEGIN_EXTERN_C -UPB_INLINE upb_alloc *upb_arena_alloc(upb_arena *a) { return &a->alloc; } -UPB_END_EXTERN_C - /* upb::Environment ***********************************************************/ -- cgit v1.2.3 From e977c0af03fa2aa366ca7ecffcf007f186357c60 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Tue, 29 Nov 2016 19:54:12 +0000 Subject: Fixed more bugs surfaced by Travis. --- Makefile | 2 ++ upb/bindings/googlepb/proto2.cc | 8 ++++---- upb/msg.h | 16 ++++++++++++---- 3 files changed, 18 insertions(+), 8 deletions(-) (limited to 'Makefile') diff --git a/Makefile b/Makefile index 62af57c..cf915c1 100644 --- a/Makefile +++ b/Makefile @@ -250,6 +250,8 @@ obj/upb/%.lo: upb/%.cc | $$(@D)/. # Regenerating the auto-generated files in upb/. upb/descriptor/descriptor.pb: upb/descriptor/descriptor.proto + $(E) PROTOC upb/descriptor/descriptor.proto + $(Q) protoc upb/descriptor/descriptor.proto -oupb/descriptor/descriptor.pb # "genfiles" includes Proto schemas we need for tests # For the moment we check in the *.upbdefs.* generated files so that people diff --git a/upb/bindings/googlepb/proto2.cc b/upb/bindings/googlepb/proto2.cc index bfd5ba2..ebfedca 100644 --- a/upb/bindings/googlepb/proto2.cc +++ b/upb/bindings/googlepb/proto2.cc @@ -12,10 +12,10 @@ #include -#include "upb/def.h" #include "upb/bindings/googlepb/proto1.int.h" +#include "upb/def.h" #include "upb/handlers.h" -#include "upb/shim/shim.h" +#include "upb/msg.h" #include "upb/sink.h" namespace { @@ -593,8 +593,8 @@ case goog::FieldDescriptor::cpptype: \ SetStartRepeatedField(proto2_f, r, f, h); CHKRET(h->SetValueHandler(f, UpbMakeHandlerT(AppendPrimitive))); } else { - CHKRET(upb::Shim::Set(h, f, GetOffset(proto2_f, r), - GetHasbit(proto2_f, r))); + CHKRET(upb_msg_setscalarhandler(h, f, GetOffset(proto2_f, r), + GetHasbit(proto2_f, r))); } } } diff --git a/upb/msg.h b/upb/msg.h index 912d6ac..2d1a261 100644 --- a/upb/msg.h +++ b/upb/msg.h @@ -32,8 +32,13 @@ #ifdef __cplusplus namespace upb { -class MessageLayout; +class Array; +class Map; +class MapIterator; class MessageFactory; +class MessageLayout; +class Visitor; +class VisitorPlan; } #endif @@ -110,6 +115,9 @@ typedef union { int64_t i64; uint32_t u32; uint64_t u64; + const upb_map* map; + const upb_msg* msg; + const upb_array* arr; const void* ptr; struct { const char *ptr; @@ -137,10 +145,10 @@ ACCESSORS(int32, i32, int32_t) ACCESSORS(int64, i64, int64_t) ACCESSORS(uint32, u32, uint32_t) ACCESSORS(uint64, u64, uint64_t) -ACCESSORS(map, ptr, const upb_map*) -ACCESSORS(msg, ptr, const upb_msg*) +ACCESSORS(map, map, const upb_map*) +ACCESSORS(msg, msg, const upb_msg*) ACCESSORS(ptr, ptr, const void*) -ACCESSORS(arr, ptr, const upb_array*) +ACCESSORS(arr, arr, const upb_array*) #undef ACCESSORS -- cgit v1.2.3