static void test_mapentry_check() { upb_status s = UPB_STATUS_INIT; upb_msgdef *m = upb_msgdef_new(&m); upb_fielddef *f = upb_fielddef_new(&f); upb_symtab *symtab = upb_symtab_new(&symtab); upb_msgdef *subm = upb_msgdef_new(&subm); upb_def *defs[2]; upb_msgdef_setfullname(m, "TestMessage", &s); upb_fielddef_setname(f, "field1", &s); upb_fielddef_setnumber(f, 1, &s); upb_fielddef_setlabel(f, UPB_LABEL_OPTIONAL); upb_fielddef_settype(f, UPB_TYPE_MESSAGE); upb_fielddef_setsubdefname(f, ".MapEntry", &s); upb_msgdef_addfield(m, f, &f, &s); ASSERT(upb_ok(&s)); upb_msgdef_setfullname(subm, "MapEntry", &s); upb_msgdef_setmapentry(subm, true); defs[0] = upb_msgdef_upcast_mutable(m); defs[1] = upb_msgdef_upcast_mutable(subm); upb_symtab_add(symtab, defs, 2, NULL, &s); /* Should not have succeeded: non-repeated field pointing to a MapEntry. */ ASSERT(!upb_ok(&s)); upb_fielddef_setlabel(f, UPB_LABEL_REPEATED); upb_symtab_add(symtab, defs, 2, NULL, &s); ASSERT(upb_ok(&s)); upb_symtab_unref(symtab, &symtab); upb_msgdef_unref(subm, &subm); upb_msgdef_unref(m, &m); }
static void test_symbol_resolution() { upb_status s = UPB_STATUS_INIT; upb_def *defs[2]; upb_msgdef *m1; upb_msgdef *m2; upb_msgdef *m3; upb_fielddef *m3_field1; upb_fielddef *m3_field2; upb_symtab *symtab = upb_symtab_new(&symtab); ASSERT(symtab); /* m1 has name "A.B.C" and no fields. We'll add it to the symtab now. */ m1 = upb_msgdef_new(&m1); ASSERT(m1); ASSERT_STATUS(upb_msgdef_setfullname(m1, "A.B.C", &s), &s); ASSERT_STATUS(upb_symtab_add(symtab, (upb_def**)&m1, 1, NULL, &s), &s); /* m2 has name "D.E" and no fields. We'll add it in the same batch as m3 * below. */ m2 = upb_msgdef_new(&m2); ASSERT(m2); ASSERT_STATUS(upb_msgdef_setfullname(m2, "D.E", &s), &s); /* m3 has name "F.G" and two fields, of type A.B.C and D.E respectively. We'll * add it in the same batch as m2 above. */ m3 = upb_msgdef_new(&m3); ASSERT(m3); ASSERT_STATUS(upb_msgdef_setfullname(m3, "F.G", &s), &s); m3_field1 = upb_fielddef_new(&m3_field1); ASSERT_STATUS(upb_fielddef_setname(m3_field1, "field1", &s), &s); ASSERT_STATUS(upb_fielddef_setnumber(m3_field1, 1, &s), &s); upb_fielddef_setlabel(m3_field1, UPB_LABEL_OPTIONAL); upb_fielddef_settype(m3_field1, UPB_TYPE_MESSAGE); ASSERT_STATUS(upb_fielddef_setsubdefname(m3_field1, ".A.B.C", &s), &s); ASSERT_STATUS(upb_msgdef_addfield(m3, m3_field1, NULL, &s), &s); m3_field2 = upb_fielddef_new(&m3_field2); ASSERT_STATUS(upb_fielddef_setname(m3_field2, "field2", &s), &s); ASSERT_STATUS(upb_fielddef_setnumber(m3_field2, 2, &s), &s); upb_fielddef_setlabel(m3_field2, UPB_LABEL_OPTIONAL); upb_fielddef_settype(m3_field2, UPB_TYPE_MESSAGE); ASSERT_STATUS(upb_fielddef_setsubdefname(m3_field2, ".D.E", &s), &s); ASSERT_STATUS(upb_msgdef_addfield(m3, m3_field2, NULL, &s), &s); defs[0] = upb_msgdef_upcast_mutable(m2); defs[1] = upb_msgdef_upcast_mutable(m3); ASSERT_STATUS(upb_symtab_add(symtab, defs, 2, NULL, &s), &s); upb_fielddef_unref(m3_field2, &m3_field2); upb_fielddef_unref(m3_field1, &m3_field1); upb_msgdef_unref(m3, &m3); upb_msgdef_unref(m2, &m2); upb_msgdef_unref(m1, &m1); upb_symtab_unref(symtab, &symtab); }
static void test_empty_symtab() { upb_symtab *s = upb_symtab_new(&s); upb_symtab_iter i; for (upb_symtab_begin(&i, s, UPB_DEF_ANY); !upb_symtab_done(&i); upb_symtab_next(&i)) { ASSERT(false); /* Should not get here. */ } upb_symtab_unref(s, &s); }
static void test_oneofs() { upb_status s = UPB_STATUS_INIT; bool ok = true; upb_def *subm_defs[1]; upb_symtab *symtab = upb_symtab_new(&symtab); upb_msgdef *subm = upb_msgdef_newnamed("SubMessage", &symtab); upb_msgdef *m = upb_msgdef_newnamed("TestMessage", &symtab); upb_oneofdef *o = upb_oneofdef_new(&o); const upb_oneofdef *lookup_o; const upb_fielddef *lookup_field; upb_def *defs[1]; ASSERT(symtab != NULL); /* Create a test message for fields to refer to. */ upb_msgdef_addfield(subm, newfield("field1", 1, UPB_TYPE_INT32, UPB_LABEL_OPTIONAL, NULL, &symtab), &symtab, NULL); subm_defs[0] = upb_msgdef_upcast_mutable(subm); ASSERT_STATUS(upb_symtab_add(symtab, subm_defs, 1, &symtab, &s), &s); ASSERT(upb_msgdef_numoneofs(m) == 0); ASSERT(upb_oneofdef_numfields(o) == 0); ASSERT(upb_oneofdef_name(o) == NULL); ok = upb_oneofdef_setname(o, "test_oneof", &s); ASSERT_STATUS(ok, &s); ok = upb_oneofdef_addfield(o, newfield("field1", 1, UPB_TYPE_INT32, UPB_LABEL_OPTIONAL, NULL, &symtab), &symtab, NULL); ASSERT_STATUS(ok, &s); ok = upb_oneofdef_addfield(o, newfield("field2", 2, UPB_TYPE_MESSAGE, UPB_LABEL_OPTIONAL, ".SubMessage", &symtab), &symtab, NULL); ASSERT_STATUS(ok, &s); ok = upb_msgdef_addoneof(m, o, NULL, &s); ASSERT_STATUS(ok, &s); defs[0] = upb_msgdef_upcast_mutable(m); ASSERT_STATUS(upb_symtab_add(symtab, defs, 1, &symtab, &s), &s); ASSERT(upb_msgdef_numoneofs(m) == 1); lookup_o = upb_msgdef_ntooz(m, "test_oneof"); ASSERT(lookup_o == o); lookup_field = upb_oneofdef_ntofz(o, "field1"); ASSERT(lookup_field != NULL && upb_fielddef_number(lookup_field) == 1); upb_symtab_unref(symtab, &symtab); upb_oneofdef_unref(o, &o); }
static void test_cycles_in_replacement() { upb_symtab *s = upb_symtab_new(&s); upb_msgdef *m = upb_msgdef_newnamed("M", &s); upb_status status = UPB_STATUS_INIT; upb_msgdef_addfield(m, newfield("m", 1, UPB_TYPE_MESSAGE, UPB_LABEL_OPTIONAL, ".M", &s), &s, NULL); ASSERT_STATUS(upb_symtab_add(s, (upb_def**)&m, 1, &s, &status), &status); ASSERT_STATUS(upb_symtab_add(s, NULL, 0, &s, &status), &status); }
static bool initialize() { // Initialize upb state, decode descriptor. upb_status status = UPB_STATUS_INIT; upb_symtab *s = upb_symtab_new(); upb_string *fds_str = upb_strreadfile(MESSAGE_DESCRIPTOR_FILE); if(fds_str == NULL) { fprintf(stderr, "Couldn't read " MESSAGE_DESCRIPTOR_FILE ":"), upb_printerr(&status); return false; } upb_parsedesc(s, fds_str, &status); upb_string_unref(fds_str); if(!upb_ok(&status)) { fprintf(stderr, "Error importing " MESSAGE_DESCRIPTOR_FILE ":"); upb_printerr(&status); return false; } def = upb_dyncast_msgdef(upb_symtab_lookup(s, UPB_STRLIT(MESSAGE_NAME))); if(!def) { fprintf(stderr, "Error finding symbol '" UPB_STRFMT "'.\n", UPB_STRARG(UPB_STRLIT(MESSAGE_NAME))); return false; } upb_symtab_unref(s); // Read the message data itself. input_str = upb_strreadfile(MESSAGE_FILE); if(input_str == NULL) { fprintf(stderr, "Error reading " MESSAGE_FILE "\n"); return false; } upb_status_uninit(&status); msg = upb_msg_new(def); upb_stringsrc_init(&strsrc); upb_handlers_init(&h, def); upb_msg_regdhandlers(&h); upb_decoder_init(&d, &h); if (!BYREF) { // Pretend the input string is stack-allocated, which will force its data // to be copied instead of referenced. There is no good reason to do this, // except to benchmark against proto2 more fairly, which in its open-source // release does not support referencing the input string. input_str->refcount.v = _UPB_STRING_REFCOUNT_STACK; } return true; }
int main() { upb_symtab *s = upb_symtab_new(); // Will be empty atm since we haven't added anything to the symtab. int count; const upb_def **defs = upb_symtab_getdefs(s, &count, UPB_DEF_ANY); for (int i = 0; i < count; i++) { upb_def_unref(defs[i]); } free(defs); upb_symtab_unref(s); return 0; }
static upb_symtab *load_test_proto(void *owner) { upb_symtab *s = upb_symtab_new(owner); upb_status status = UPB_STATUS_INIT; ASSERT(s); if (!upb_load_descriptor_file_into_symtab(s, descriptor_file, &status)) { fprintf(stderr, "Error loading descriptor file: %s\n", upb_status_errmsg(&status)); ASSERT(false); } ASSERT(!upb_symtab_isfrozen(s)); upb_symtab_freeze(s); ASSERT(upb_symtab_isfrozen(s)); return s; }
static void test_replacement() { upb_symtab *s = upb_symtab_new(&s); upb_enumdef *e2; upb_msgdef *m2; upb_enumdef *e; upb_status status = UPB_STATUS_INIT; upb_def *newdefs[3]; upb_def *newdefs2[1]; const upb_msgdef *m3; upb_msgdef *m = upb_msgdef_newnamed("MyMessage", &s); upb_msgdef_addfield(m, newfield("field1", 1, UPB_TYPE_ENUM, UPB_LABEL_OPTIONAL, ".MyEnum", &s), &s, NULL); m2 = upb_msgdef_newnamed("MyMessage2", &s); e = upb_enumdef_newnamed("MyEnum", &s); ASSERT_STATUS(upb_enumdef_addval(e, "VAL1", 1, &status), &status); newdefs[0] = upb_msgdef_upcast_mutable(m); newdefs[1] = upb_msgdef_upcast_mutable(m2); newdefs[2] = upb_enumdef_upcast_mutable(e); ASSERT_STATUS(upb_symtab_add(s, newdefs, 3, &s, &status), &status); /* Try adding a new definition of MyEnum, MyMessage should get replaced with * a new version. */ e2 = upb_enumdef_newnamed("MyEnum", &s); ASSERT_STATUS(upb_enumdef_addval(e2, "VAL1", 1, &status), &status); newdefs2[0] = upb_enumdef_upcast_mutable(e2); ASSERT_STATUS(upb_symtab_add(s, newdefs2, 1, &s, &status), &status); m3 = upb_symtab_lookupmsg(s, "MyMessage"); ASSERT(m3); /* Must be different because it points to MyEnum which was replaced. */ ASSERT(m3 != m); m3 = upb_symtab_lookupmsg(s, "MyMessage2"); /* Should be the same because it was not replaced, nor were any defs that * are reachable from it. */ ASSERT(m3 == m2); upb_symtab_unref(s, &s); }
static bool initialize() { // Initialize upb state, decode descriptor. upb_status status = UPB_STATUS_INIT; upb_symtab *s = upb_symtab_new(); upb_load_descriptor_file_into_symtab(s, MESSAGE_DESCRIPTOR_FILE, &status); if(!upb_ok(&status)) { fprintf(stderr, "Error reading descriptor: %s\n", upb_status_getstr(&status)); return false; } def = upb_dyncast_msgdef_const(upb_symtab_lookup(s, MESSAGE_NAME)); if(!def) { fprintf(stderr, "Error finding symbol '%s'.\n", MESSAGE_NAME); return false; } upb_symtab_unref(s); // Read the message data itself. char *str = upb_readfile(MESSAGE_FILE, &len); if(str == NULL) { fprintf(stderr, "Error reading " MESSAGE_FILE "\n"); return false; } upb_status_uninit(&status); for (int i = 0; i < NUM_MESSAGES; i++) msg[i] = upb_stdmsg_new(def); upb_stringsrc_init(&strsrc); upb_stringsrc_reset(&strsrc, str, len); upb_handlers *h = upb_handlers_new(); upb_accessors_reghandlers(h, def); if (!JIT) h->should_jit = false; upb_decoder_init(&d, h); upb_handlers_unref(h); if (!BYREF) { // TODO: use byref/byval accessors. } return true; }
int main() { upb_symtab *symtab = upb_symtab_new(); upb_symtab_add_descriptorproto(symtab); upb_def *fds = upb_symtab_lookup( symtab, UPB_STRLIT("google.protobuf.FileDescriptorSet")); upb_stdio *in = upb_stdio_new(); upb_stdio_reset(in, stdin); upb_stdio *out = upb_stdio_new(); upb_stdio_reset(out, stdout); upb_decoder d; upb_decoder_init(&d, upb_downcast_msgdef(fds)); upb_decoder_reset(&d, upb_stdio_bytesrc(in)); upb_textprinter *p = upb_textprinter_new(); upb_handlers handlers; upb_handlers_init(&handlers); upb_textprinter_reset(p, &handlers, upb_stdio_bytesink(out), false); upb_src *src = upb_decoder_src(&d); upb_src_sethandlers(src, &handlers); upb_status status = UPB_STATUS_INIT; upb_src_run(src, &status); assert(upb_ok(&status)); upb_status_uninit(&status); upb_stdio_free(in); upb_stdio_free(out); upb_decoder_uninit(&d); upb_textprinter_free(p); upb_def_unref(fds); upb_symtab_unref(symtab); // Prevent C library from holding buffers open, so Valgrind doesn't see // memory leaks. fclose(stdin); fclose(stdout); }
static bool initialize() { // Initialize upb state, decode descriptor. upb_status status = UPB_STATUS_INIT; upb_symtab *s = upb_symtab_new(&s); upb_load_descriptor_file_into_symtab(s, MESSAGE_DESCRIPTOR_FILE, &status); if(!upb_ok(&status)) { fprintf(stderr, "Error reading descriptor: %s\n", upb_status_getstr(&status)); return false; } def = upb_dyncast_msgdef_const(upb_symtab_lookup(s, MESSAGE_NAME, &def)); if(!def) { fprintf(stderr, "Error finding symbol '%s'.\n", MESSAGE_NAME); return false; } upb_symtab_unref(s, &s); // Read the message data itself. input_str = upb_readfile(MESSAGE_FILE, &input_len); if(input_str == NULL) { fprintf(stderr, "Error reading " MESSAGE_FILE "\n"); return false; } upb_handlers *handlers = upb_handlers_new(); // Cause all messages to be read, but do nothing when they are. upb_handlerset hset = {NULL, NULL, value, startsubmsg, NULL, NULL, NULL}; upb_handlers_reghandlerset(handlers, def, &hset); upb_decoder_init(&decoder); plan = upb_decoderplan_new(handlers, JIT); upb_decoder_resetplan(&decoder, plan, 0); upb_handlers_unref(handlers); upb_stringsrc_init(&stringsrc); return true; }
static PyObject *PyUpb_SymbolTable_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds) { return PyUpb_ObjCacheGet(upb_symtab_new(), subtype); }
int main(int argc, char *argv[]) { if (argc < 3) { fprintf(stderr, "Usage: stream_transcode <descfile> <msgname>\n"); return 1; } upb_symtab *symtab = upb_symtab_new(); size_t desc_len; const char *desc = upb_readfile(argv[1], &desc_len); if (!desc) { fprintf(stderr, "Couldn't open descriptor file: %s\n", argv[1]); return 1; } upb_status status = UPB_STATUS_INIT; upb_load_descriptor_into_symtab(symtab, desc, desc_len, &status); if (!upb_ok(&status)) { fprintf(stderr, "Error parsing descriptor: %s", upb_status_getstr(&status)); return 1; } free((void*)desc); const upb_def *md = upb_symtab_lookup(symtab, argv[2]); if (!md) { fprintf(stderr, "Descriptor did not contain message: %s\n", argv[2]); return 1; } const upb_msgdef *m = upb_dyncast_msgdef_const(md); if (!m) { fprintf(stderr, "Def was not a msgdef.\n"); return 1; } upb_stdio in, out; upb_stdio_init(&in); upb_stdio_init(&out); upb_stdio_reset(&in, stdin); upb_stdio_reset(&out, stdout); upb_handlers *handlers = upb_handlers_new(); upb_textprinter *p = upb_textprinter_new(); upb_textprinter_reset(p, upb_stdio_bytesink(&out), false); upb_textprinter_reghandlers(handlers, m); upb_decoder d; upb_decoder_init(&d, handlers); upb_decoder_reset(&d, upb_stdio_bytesrc(&in), 0, UPB_NONDELIMITED, p); upb_status_clear(&status); upb_decoder_decode(&d, &status); if (!upb_ok(&status)) { fprintf(stderr, "Error parsing input: %s", upb_status_getstr(&status)); } upb_status_uninit(&status); upb_stdio_uninit(&in); upb_stdio_uninit(&out); upb_decoder_uninit(&d); upb_textprinter_free(p); upb_def_unref(UPB_UPCAST(m)); upb_symtab_unref(symtab); // Prevent C library from holding buffers open, so Valgrind doesn't see // memory leaks. fclose(stdin); fclose(stdout); }
if (generated_pool_php == NULL) { MAKE_STD_ZVAL(generated_pool_php); Z_TYPE_P(generated_pool_php) = IS_OBJECT; generated_pool = ALLOC(DescriptorPool); descriptor_pool_init_c_instance(generated_pool TSRMLS_CC); Z_OBJ_HANDLE_P(generated_pool_php) = zend_objects_store_put( generated_pool, NULL, (zend_objects_free_object_storage_t)descriptor_pool_free, NULL TSRMLS_CC); Z_OBJ_HT_P(generated_pool_php) = zend_get_std_object_handlers(); } } static void descriptor_pool_init_c_instance(DescriptorPool *pool TSRMLS_DC) { zend_object_std_init(&pool->std, descriptor_pool_type TSRMLS_CC); pool->symtab = upb_symtab_new(&pool->symtab); ALLOC_HASHTABLE(pool->pending_list); zend_hash_init(pool->pending_list, 1, NULL, ZVAL_PTR_DTOR, 0); } static void descriptor_pool_free_c(DescriptorPool *pool TSRMLS_DC) { upb_symtab_unref(pool->symtab, &pool->symtab); zend_hash_destroy(pool->pending_list); FREE_HASHTABLE(pool->pending_list); } static void validate_enumdef(const upb_enumdef *enumdef) { // Verify that an entry exists with integer value 0. (This is the default // value.)
/* * call-seq: * DescriptorPool.new => pool * * Creates a new, empty, descriptor pool. */ VALUE DescriptorPool_alloc(VALUE klass) { DescriptorPool* self = ALLOC(DescriptorPool); self->symtab = upb_symtab_new(&self->symtab); return TypedData_Wrap_Struct(klass, &_DescriptorPool_type, self); }