A lot more tests are working now.

pull/13171/head
Joshua Haberman 6 years ago
parent d2f9bec5c6
commit 48863ea0be
  1. 10
      BUILD
  2. 36
      tests/json/test_json.cc
  3. 157
      tests/pb/test_decoder.cc
  4. 42
      tests/test_handlers.c
  5. 10
      tests/test_util.h
  6. 3
      upb/bindings/stdc++/string.h
  7. 2
      upb/handlers-inl.h
  8. 11
      upb/handlers.c
  9. 2
      upb/handlers.h
  10. 140
      upb/json/parser.c
  11. 138
      upb/json/parser.h
  12. 34
      upb/json/parser.rl
  13. 32
      upb/json/printer.c
  14. 66
      upb/json/printer.h
  15. 11
      upb/pb/decoder.c
  16. 11
      upb/pb/decoder.h
  17. 14
      upb/pb/encoder.c
  18. 12
      upb/pb/encoder.h
  19. 8
      upb/sink.c
  20. 28
      upb/sink.h

10
BUILD

@ -166,16 +166,6 @@ upb_proto_reflection_library(
deps = ["descriptor_proto"], deps = ["descriptor_proto"],
) )
cc_test(
name = "test_handlers",
srcs = ["tests/test_handlers.c"],
deps = [
":descriptor_upbproto",
":upb_pb",
":upb_test",
],
)
proto_library( proto_library(
name = "test_decoder_proto", name = "test_decoder_proto",
srcs = [ srcs = [

@ -144,7 +144,7 @@ class StringSink {
} }
~StringSink() { } ~StringSink() { }
upb_bytessink* Sink() { return &bytessink_; } upb_bytessink Sink() { return bytessink_; }
const std::string& Data() { return s_; } const std::string& Data() { return s_; }
@ -169,16 +169,15 @@ class StringSink {
void test_json_roundtrip_message(const char* json_src, void test_json_roundtrip_message(const char* json_src,
const char* json_expected, const char* json_expected,
const upb::Handlers* serialize_handlers, const upb::Handlers* serialize_handlers,
const upb::json::ParserMethod* parser_method, const upb::json::ParserMethodPtr parser_method,
int seam) { int seam) {
VerboseParserEnvironment env(verbose); VerboseParserEnvironment env(verbose);
StringSink data_sink; StringSink data_sink;
upb::json::Printer* printer = upb::json::Printer::Create( upb::json::PrinterPtr printer = upb::json::PrinterPtr::Create(
env.env(), serialize_handlers, data_sink.Sink()); env.env(), serialize_handlers, data_sink.Sink());
upb::json::Parser* parser = upb::json::ParserPtr parser = upb::json::ParserPtr::Create(
upb::json::Parser::Create( env.env(), parser_method, NULL, printer.input(), false);
env.env(), parser_method, NULL, printer->input(), false); env.ResetBytesSink(parser.input());
env.ResetBytesSink(parser->input());
env.Reset(json_src, strlen(json_src), false, false); env.Reset(json_src, strlen(json_src), false, false);
bool ok = env.Start() && bool ok = env.Start() &&
@ -203,16 +202,16 @@ void test_json_roundtrip_message(const char* json_src,
// Starts with a message in JSON format, parses and directly serializes again, // Starts with a message in JSON format, parses and directly serializes again,
// and compares the result. // and compares the result.
void test_json_roundtrip() { void test_json_roundtrip() {
upb::SymbolTable* symtab = upb::SymbolTable::New(); upb::SymbolTable symtab;
upb::HandlerCache* serialize_handlercache = upb::json::Printer::NewCache(false); upb::HandlerCache serialize_handlercache(
upb::json::CodeCache* parse_codecache = upb::json::CodeCache::New(); upb::json::PrinterPtr::NewCache(false));
upb::json::CodeCache parse_codecache;
const upb::MessageDef* md = upb_test_json_TestMessage_getmsgdef(symtab); upb::MessageDefPtr md(upb_test_json_TestMessage_getmsgdef(symtab.ptr()));
ASSERT(md); ASSERT(md);
const upb::Handlers* serialize_handlers = serialize_handlercache->Get(md); const upb::Handlers* serialize_handlers = serialize_handlercache.Get(md);
const upb::json::ParserMethod* parser_method = parse_codecache->Get(md); const upb::json::ParserMethodPtr parser_method = parse_codecache.Get(md);
ASSERT(serialize_handlers); ASSERT(serialize_handlers);
ASSERT(parser_method);
for (const TestCase* test_case = kTestRoundtripMessages; for (const TestCase* test_case = kTestRoundtripMessages;
test_case->input != NULL; test_case++) { test_case->input != NULL; test_case++) {
@ -227,9 +226,8 @@ void test_json_roundtrip() {
} }
} }
upb::HandlerCache::Free(serialize_handlercache); serialize_handlercache = upb::json::PrinterPtr::NewCache(true);
serialize_handlercache = upb::json::Printer::NewCache(true); serialize_handlers = serialize_handlercache.Get(md);
serialize_handlers = serialize_handlercache->Get(md);
for (const TestCase* test_case = kTestRoundtripMessagesPreserve; for (const TestCase* test_case = kTestRoundtripMessagesPreserve;
test_case->input != NULL; test_case++) { test_case->input != NULL; test_case++) {
@ -243,10 +241,6 @@ void test_json_roundtrip() {
serialize_handlers, parser_method, i); serialize_handlers, parser_method, i);
} }
} }
upb::HandlerCache::Free(serialize_handlercache);
upb::json::CodeCache::Free(parse_codecache);
upb::SymbolTable::Free(symtab);
} }
extern "C" { extern "C" {

@ -279,7 +279,7 @@ int* startstr(int* depth, const uint32_t* num, size_t size_hint) {
} }
size_t value_string(int* depth, const uint32_t* num, const char* buf, size_t value_string(int* depth, const uint32_t* num, const char* buf,
size_t n, const upb::BufferHandle* handle) { size_t n, const upb_bufhandle* handle) {
UPB_UNUSED(num); UPB_UNUSED(num);
UPB_UNUSED(depth); UPB_UNUSED(depth);
check_stack_alignment(); check_stack_alignment();
@ -348,13 +348,13 @@ void free_uint32(void *val) {
} }
template<class T, bool F(int*, const uint32_t*, T)> template<class T, bool F(int*, const uint32_t*, T)>
void doreg(upb_handlers *h, uint32_t num) { void doreg(upb::HandlersPtr h, uint32_t num) {
const upb_fielddef *f = upb_msgdef_itof(upb_handlers_msgdef(h), num); upb::FieldDefPtr f = h.message_def().FindFieldByNumber(num);
ASSERT(f); ASSERT(f);
ASSERT(h->SetValueHandler<T>(f, UpbBindT(F, new uint32_t(num)))); ASSERT(h.SetValueHandler<T>(f, UpbBind(F, new uint32_t(num))));
if (f->IsSequence()) { if (f.IsSequence()) {
ASSERT(h->SetStartSequenceHandler(f, UpbBind(startseq, new uint32_t(num)))); ASSERT(h.SetStartSequenceHandler(f, UpbBind(startseq, new uint32_t(num))));
ASSERT(h->SetEndSequenceHandler(f, UpbBind(endseq, new uint32_t(num)))); ASSERT(h.SetEndSequenceHandler(f, UpbBind(endseq, new uint32_t(num))));
} }
} }
@ -368,7 +368,7 @@ uint32_t rep_fn(uint32_t fn) {
#define UNKNOWN_FIELD 666 #define UNKNOWN_FIELD 666
template <class T, bool F(int*, const uint32_t*, T)> template <class T, bool F(int*, const uint32_t*, T)>
void reg(upb_handlers *h, upb_descriptortype_t type) { void reg(upb::HandlersPtr h, upb_descriptortype_t type) {
// We register both a repeated and a non-repeated field for every type. // We register both a repeated and a non-repeated field for every type.
// For the non-repeated field we make the field number the same as the // For the non-repeated field we make the field number the same as the
// type. For the repeated field we make it a function of the type. // type. For the repeated field we make it a function of the type.
@ -376,39 +376,40 @@ void reg(upb_handlers *h, upb_descriptortype_t type) {
doreg<T, F>(h, rep_fn(type)); doreg<T, F>(h, rep_fn(type));
} }
void regseq(upb::Handlers* h, const upb::FieldDef* f, uint32_t num) { void regseq(upb::HandlersPtr h, upb::FieldDefPtr f, uint32_t num) {
ASSERT(h->SetStartSequenceHandler(f, UpbBind(startseq, new uint32_t(num)))); ASSERT(h.SetStartSequenceHandler(f, UpbBind(startseq, new uint32_t(num))));
ASSERT(h->SetEndSequenceHandler(f, UpbBind(endseq, new uint32_t(num)))); ASSERT(h.SetEndSequenceHandler(f, UpbBind(endseq, new uint32_t(num))));
} }
void reg_subm(upb_handlers *h, uint32_t num) { void reg_subm(upb::HandlersPtr h, uint32_t num) {
const upb_fielddef *f = upb_msgdef_itof(upb_handlers_msgdef(h), num); upb::FieldDefPtr f = h.message_def().FindFieldByNumber(num);
ASSERT(f); ASSERT(f);
if (f->IsSequence()) regseq(h, f, num); if (f.IsSequence()) regseq(h, f, num);
ASSERT( ASSERT(
h->SetStartSubMessageHandler(f, UpbBind(startsubmsg, new uint32_t(num)))); h.SetStartSubMessageHandler(f, UpbBind(startsubmsg, new uint32_t(num))));
ASSERT(h->SetEndSubMessageHandler(f, UpbBind(endsubmsg, new uint32_t(num)))); ASSERT(h.SetEndSubMessageHandler(f, UpbBind(endsubmsg, new uint32_t(num))));
} }
void reg_str(upb_handlers *h, uint32_t num) { void reg_str(upb::HandlersPtr h, uint32_t num) {
const upb_fielddef *f = upb_msgdef_itof(upb_handlers_msgdef(h), num); upb::FieldDefPtr f = h.message_def().FindFieldByNumber(num);
ASSERT(f); ASSERT(f);
if (f->IsSequence()) regseq(h, f, num); if (f.IsSequence()) regseq(h, f, num);
ASSERT(h->SetStartStringHandler(f, UpbBind(startstr, new uint32_t(num)))); ASSERT(h.SetStartStringHandler(f, UpbBind(startstr, new uint32_t(num))));
ASSERT(h->SetEndStringHandler(f, UpbBind(endstr, new uint32_t(num)))); ASSERT(h.SetEndStringHandler(f, UpbBind(endstr, new uint32_t(num))));
ASSERT(h->SetStringHandler(f, UpbBind(value_string, new uint32_t(num)))); ASSERT(h.SetStringHandler(f, UpbBind(value_string, new uint32_t(num))));
} }
struct HandlerRegisterData { struct HandlerRegisterData {
TestMode mode; TestMode mode;
}; };
void callback(const void *closure, upb_handlers *h) { void callback(const void *closure, upb::Handlers* h_ptr) {
upb::HandlersPtr h(h_ptr);
const HandlerRegisterData* data = const HandlerRegisterData* data =
static_cast<const HandlerRegisterData*>(closure); static_cast<const HandlerRegisterData*>(closure);
if (data->mode == ALL_HANDLERS) { if (data->mode == ALL_HANDLERS) {
h->SetStartMessageHandler(UpbMakeHandler(startmsg)); h.SetStartMessageHandler(UpbMakeHandler(startmsg));
h->SetEndMessageHandler(UpbMakeHandler(endmsg)); h.SetEndMessageHandler(UpbMakeHandler(endmsg));
// Register handlers for each type. // Register handlers for each type.
reg<double, value_double>(h, UPB_DESCRIPTOR_TYPE_DOUBLE); reg<double, value_double>(h, UPB_DESCRIPTOR_TYPE_DOUBLE);
@ -436,7 +437,7 @@ void callback(const void *closure, upb_handlers *h) {
reg_subm(h, UPB_DESCRIPTOR_TYPE_MESSAGE); reg_subm(h, UPB_DESCRIPTOR_TYPE_MESSAGE);
reg_subm(h, rep_fn(UPB_DESCRIPTOR_TYPE_MESSAGE)); reg_subm(h, rep_fn(UPB_DESCRIPTOR_TYPE_MESSAGE));
if (h->message_def()->full_name() == std::string("DecoderTest")) { if (h.message_def().full_name() == std::string("DecoderTest")) {
reg_subm(h, UPB_DESCRIPTOR_TYPE_GROUP); reg_subm(h, UPB_DESCRIPTOR_TYPE_GROUP);
reg_subm(h, rep_fn(UPB_DESCRIPTOR_TYPE_GROUP)); reg_subm(h, rep_fn(UPB_DESCRIPTOR_TYPE_GROUP));
} }
@ -446,25 +447,16 @@ void callback(const void *closure, upb_handlers *h) {
} }
} }
upb::reffed_ptr<const upb::Handlers> NewHandlers(upb::SymbolTable* symtab,
TestMode mode) {
HandlerRegisterData handlerdata;
handlerdata.mode = mode;
return upb::Handlers::NewFrozen(DecoderTest_getmsgdef(symtab), callback,
&handlerdata);
}
/* Running of test cases ******************************************************/ /* Running of test cases ******************************************************/
const upb::Handlers *global_handlers; const upb::Handlers *global_handlers;
const upb::pb::DecoderMethod *global_method; upb::pb::DecoderMethodPtr global_method;
upb::pb::Decoder* CreateDecoder(upb::Environment* env, upb::pb::DecoderPtr CreateDecoder(upb::Environment* env,
const upb::pb::DecoderMethod* method, upb::pb::DecoderMethodPtr method,
upb::Sink* sink) { upb::Sink sink) {
upb::pb::Decoder *ret = upb::pb::Decoder::Create(env, method, sink); upb::pb::DecoderPtr ret = upb::pb::DecoderPtr::Create(env, method, sink);
ASSERT(ret != NULL); ret.set_max_nesting(MAX_NESTING);
ret->set_max_nesting(MAX_NESTING);
return ret; return ret;
} }
@ -479,7 +471,7 @@ uint32_t Hash(const string& proto, const string* expected_output, size_t seam1,
return hash; return hash;
} }
void CheckBytesParsed(const upb::pb::Decoder& decoder, size_t ofs) { void CheckBytesParsed(upb::pb::DecoderPtr decoder, size_t ofs) {
// We can't have parsed more data than the decoder callback is telling us it // We can't have parsed more data than the decoder callback is telling us it
// parsed. // parsed.
ASSERT(decoder.BytesParsed() <= ofs); ASSERT(decoder.BytesParsed() <= ofs);
@ -491,7 +483,7 @@ void CheckBytesParsed(const upb::pb::Decoder& decoder, size_t ofs) {
} }
static bool parse(VerboseParserEnvironment* env, static bool parse(VerboseParserEnvironment* env,
const upb::pb::Decoder& decoder, int bytes) { upb::pb::DecoderPtr decoder, int bytes) {
CheckBytesParsed(decoder, env->ofs()); CheckBytesParsed(decoder, env->ofs());
bool ret = env->ParseBuffer(bytes); bool ret = env->ParseBuffer(bytes);
if (ret) { if (ret) {
@ -501,11 +493,11 @@ static bool parse(VerboseParserEnvironment* env,
return ret; return ret;
} }
void do_run_decoder(VerboseParserEnvironment* env, upb::pb::Decoder* decoder, void do_run_decoder(VerboseParserEnvironment* env, upb::pb::DecoderPtr decoder,
const string& proto, const string* expected_output, const string& proto, const string* expected_output,
size_t i, size_t j, bool may_skip) { size_t i, size_t j, bool may_skip) {
env->Reset(proto.c_str(), proto.size(), may_skip, expected_output == NULL); env->Reset(proto.c_str(), proto.size(), may_skip, expected_output == NULL);
decoder->Reset(); decoder.Reset();
testhash = Hash(proto, expected_output, i, j, may_skip); testhash = Hash(proto, expected_output, i, j, may_skip);
if (filter_hash && testhash != filter_hash) return; if (filter_hash && testhash != filter_hash) return;
@ -515,7 +507,7 @@ void do_run_decoder(VerboseParserEnvironment* env, upb::pb::Decoder* decoder,
if (filter_hash) { if (filter_hash) {
fprintf(stderr, "RUNNING TEST CASE, hash=%x\n", testhash); fprintf(stderr, "RUNNING TEST CASE, hash=%x\n", testhash);
fprintf(stderr, "JIT on: %s\n", fprintf(stderr, "JIT on: %s\n",
global_method->is_native() ? "true" : "false"); global_method.is_native() ? "true" : "false");
fprintf(stderr, "Input (len=%u): ", (unsigned)proto.size()); fprintf(stderr, "Input (len=%u): ", (unsigned)proto.size());
PrintBinary(proto); PrintBinary(proto);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
@ -534,9 +526,9 @@ void do_run_decoder(VerboseParserEnvironment* env, upb::pb::Decoder* decoder,
} }
bool ok = env->Start() && bool ok = env->Start() &&
parse(env, *decoder, i) && parse(env, decoder, i) &&
parse(env, *decoder, j - i) && parse(env, decoder, j - i) &&
parse(env, *decoder, -1) && parse(env, decoder, -1) &&
env->End(); env->End();
ASSERT(env->CheckConsistency()); ASSERT(env->CheckConsistency());
@ -564,8 +556,8 @@ void do_run_decoder(VerboseParserEnvironment* env, upb::pb::Decoder* decoder,
void run_decoder(const string& proto, const string* expected_output) { void run_decoder(const string& proto, const string* expected_output) {
VerboseParserEnvironment env(filter_hash != 0); VerboseParserEnvironment env(filter_hash != 0);
upb::Sink sink(global_handlers, &closures[0]); upb::Sink sink(global_handlers, &closures[0]);
upb::pb::Decoder *decoder = CreateDecoder(env.env(), global_method, &sink); upb::pb::DecoderPtr decoder = CreateDecoder(env.env(), global_method, sink);
env.ResetBytesSink(decoder->input()); env.ResetBytesSink(decoder.input());
for (size_t i = 0; i < proto.size(); i++) { for (size_t i = 0; i < proto.size(); i++) {
for (size_t j = i; j < UPB_MIN(proto.size(), i + 5); j++) { for (size_t j = i; j < UPB_MIN(proto.size(), i + 5); j++) {
do_run_decoder(&env, decoder, proto, expected_output, i, j, true); do_run_decoder(&env, decoder, proto, expected_output, i, j, true);
@ -883,9 +875,9 @@ void test_valid() {
upb::Environment env; upb::Environment env;
env.ReportErrorsTo(&status); env.ReportErrorsTo(&status);
upb::Sink sink(global_handlers, &closures[0]); upb::Sink sink(global_handlers, &closures[0]);
upb::pb::Decoder* decoder = CreateDecoder(&env, global_method, &sink); upb::pb::DecoderPtr decoder = CreateDecoder(&env, global_method, sink);
output.clear(); output.clear();
bool ok = upb::BufferSource::PutBuffer("", 0, decoder->input()); bool ok = upb::PutBuffer(std::string(), decoder.input());
ASSERT(ok); ASSERT(ok);
ASSERT(status.ok()); ASSERT(status.ok());
if (test_mode == ALL_HANDLERS) { if (test_mode == ALL_HANDLERS) {
@ -1133,23 +1125,22 @@ void test_valid() {
run_decoder(buf, &textbuf); run_decoder(buf, &textbuf);
} }
upb::reffed_ptr<const upb::pb::DecoderMethod> NewMethod( void empty_callback(const void *closure, upb::Handlers* h_ptr) {}
const upb::Handlers* dest_handlers, bool allow_jit) {
upb::pb::CodeCache cache;
cache.set_allow_jit(allow_jit);
return cache.GetDecoderMethod(upb::pb::DecoderMethodOptions(dest_handlers));
}
void test_emptyhandlers(upb::SymbolTable* symtab, bool allowjit) { void test_emptyhandlers(upb::SymbolTable* symtab, bool allowjit) {
// Create an empty handlers to make sure that the decoder can handle empty // Create an empty handlers to make sure that the decoder can handle empty
// messages. // messages.
const upb::MessageDef* md = Empty_getmsgdef(symtab); HandlerRegisterData handlerdata;
upb::reffed_ptr<upb::Handlers> h(upb::Handlers::New(md)); handlerdata.mode = test_mode;
bool ok = h->Freeze(NULL);
ASSERT(ok); upb::HandlerCache handler_cache(empty_callback, &handlerdata);
upb::reffed_ptr<const upb::pb::DecoderMethod> method = upb::pb::CodeCache pb_code_cache(&handler_cache);
NewMethod(h.get(), allowjit);
ASSERT(method.get()); pb_code_cache.set_allow_jit(allowjit);
upb::MessageDefPtr md = upb::MessageDefPtr(Empty_getmsgdef(symtab->ptr()));
global_handlers = handler_cache.Get(md);
global_method = pb_code_cache.Get(md);
// TODO: also test the case where a message has fields, but the fields are // TODO: also test the case where a message has fields, but the fields are
// submessage fields and have no handlers. This also results in a decoder // submessage fields and have no handlers. This also results in a decoder
@ -1169,9 +1160,9 @@ upb::reffed_ptr<const upb::pb::DecoderMethod> method =
}; };
for (int i = 0; testdata[i].data; i++) { for (int i = 0; testdata[i].data; i++) {
VerboseParserEnvironment env(filter_hash != 0); VerboseParserEnvironment env(filter_hash != 0);
upb::Sink sink(method->dest_handlers(), &closures[0]); upb::Sink sink(global_method.dest_handlers(), &closures[0]);
upb::pb::Decoder* decoder = CreateDecoder(env.env(), method.get(), &sink); upb::pb::DecoderPtr decoder = CreateDecoder(env.env(), global_method, sink);
env.ResetBytesSink(decoder->input()); env.ResetBytesSink(decoder.input());
env.Reset(testdata[i].data, testdata[i].length, true, false); env.Reset(testdata[i].data, testdata[i].length, true, false);
ASSERT(env.Start()); ASSERT(env.Start());
ASSERT(env.ParseBuffer(-1)); ASSERT(env.ParseBuffer(-1));
@ -1181,24 +1172,25 @@ upb::reffed_ptr<const upb::pb::DecoderMethod> method =
} }
void run_tests(bool use_jit) { void run_tests(bool use_jit) {
upb::reffed_ptr<const upb::pb::DecoderMethod> method; HandlerRegisterData handlerdata;
upb::reffed_ptr<const upb::Handlers> handlers; handlerdata.mode = test_mode;
upb::SymbolTable* symtab = upb::SymbolTable::New();
handlers = NewHandlers(symtab, test_mode); upb::SymbolTable symtab;
global_handlers = handlers.get(); upb::HandlerCache handler_cache(callback, &handlerdata);
upb::pb::CodeCache pb_code_cache(&handler_cache);
method = NewMethod(handlers.get(), use_jit); pb_code_cache.set_allow_jit(use_jit);
global_method = method.get();
ASSERT(use_jit == global_method->is_native()); upb::MessageDefPtr md(DecoderTest_getmsgdef(symtab.ptr()));
global_handlers = handler_cache.Get(md);
global_method = pb_code_cache.Get(md);
ASSERT(use_jit == global_method.is_native());
completed = 0; completed = 0;
test_invalid(); test_invalid();
test_valid(); test_valid();
test_emptyhandlers(symtab, use_jit); test_emptyhandlers(&symtab, use_jit);
upb::SymbolTable::Free(symtab);
} }
void run_test_suite() { void run_test_suite() {
@ -1218,9 +1210,6 @@ int run_tests(int argc, char *argv[]) {
closures[i] = i; closures[i] = i;
} }
upb::reffed_ptr<const upb::pb::DecoderMethod> method;
upb::reffed_ptr<const upb::Handlers> handlers;
// Count tests. // Count tests.
count = &total; count = &total;
total = 0; total = 0;

@ -1,42 +0,0 @@
#include <stdlib.h>
#include <string.h>
#include "google/protobuf/descriptor.upbdefs.h"
#include "upb/handlers.h"
#include "upb_test.h"
static bool startmsg(void *c, const void *hd) {
UPB_UNUSED(c);
UPB_UNUSED(hd);
return true;
}
static void test_error() {
/* Test creating handlers of a static msgdef. */
upb_symtab *s = upb_symtab_new();
const upb_msgdef *m = google_protobuf_DescriptorProto_getmsgdef(s);
upb_handlers *h = upb_handlers_new(m, &h);
/* Attempt to set the same handler twice causes error. */
ASSERT(upb_ok(upb_handlers_status(h)));
upb_handlers_setstartmsg(h, &startmsg, NULL);
ASSERT(upb_ok(upb_handlers_status(h)));
upb_handlers_setstartmsg(h, &startmsg, NULL);
ASSERT(!upb_ok(upb_handlers_status(h)));
ASSERT(!upb_handlers_freeze(&h, 1, NULL));
/* Clearing the error will let us proceed. */
upb_handlers_clearerr(h);
ASSERT(upb_handlers_freeze(&h, 1, NULL));
ASSERT(upb_handlers_isfrozen(h));
upb_handlers_unref(h, &h);
upb_symtab_free(s);
}
int run_tests(int argc, char *argv[]) {
UPB_UNUSED(argc);
UPB_UNUSED(argv);
test_error();
return 0;
}

@ -78,14 +78,14 @@ class VerboseParserEnvironment {
if (verbose_) { if (verbose_) {
fprintf(stderr, "Calling start()\n"); fprintf(stderr, "Calling start()\n");
} }
return sink_->Start(len_, &subc_); return sink_.Start(len_, &subc_);
} }
bool End() { bool End() {
if (verbose_) { if (verbose_) {
fprintf(stderr, "Calling end()\n"); fprintf(stderr, "Calling end()\n");
} }
end_ok_ = sink_->End(); end_ok_ = sink_.End();
end_ok_set_ = true; end_ok_set_ = true;
return end_ok_; return end_ok_;
@ -137,7 +137,7 @@ class VerboseParserEnvironment {
(unsigned)bytes, (unsigned)ofs_, (unsigned)(ofs_ + bytes)); (unsigned)bytes, (unsigned)ofs_, (unsigned)(ofs_ + bytes));
} }
int parsed = sink_->PutBuffer(subc_, buf2, bytes, &global_handle); int parsed = sink_.PutBuffer(subc_, buf2, bytes, &global_handle);
free(buf2); free(buf2);
if (verbose_) { if (verbose_) {
@ -170,7 +170,7 @@ class VerboseParserEnvironment {
return true; return true;
} }
void ResetBytesSink(upb::BytesSink* sink) { void ResetBytesSink(upb::BytesSink sink) {
sink_ = sink; sink_ = sink;
} }
@ -181,7 +181,7 @@ class VerboseParserEnvironment {
private: private:
upb::Environment env_; upb::Environment env_;
upb::BytesSink* sink_; upb::BytesSink sink_;
const char* buf_; const char* buf_;
size_t len_; size_t len_;
bool verbose_; bool verbose_;

@ -48,11 +48,12 @@ class StringSink {
explicit StringSink(T* target) { explicit StringSink(T* target) {
// TODO(haberman): we need to avoid rebuilding a new handler every time, // TODO(haberman): we need to avoid rebuilding a new handler every time,
// but with class globals disallowed for google3 C++ this is tricky. // but with class globals disallowed for google3 C++ this is tricky.
upb_byteshandler_init(&handler_);
FillStringHandler<T>::SetHandler(&handler_); FillStringHandler<T>::SetHandler(&handler_);
input_.Reset(&handler_, target); input_.Reset(&handler_, target);
} }
BytesSink* input() { return &input_; } BytesSink input() { return input_; }
private: private:
upb_byteshandler handler_; upb_byteshandler handler_;

@ -858,7 +858,7 @@ inline Handler<T>::Handler(F func)
: registered_(false), : registered_(false),
cleanup_data_(func.GetData()), cleanup_data_(func.GetData()),
cleanup_func_(func.GetCleanup()) { cleanup_func_(func.GetCleanup()) {
upb_handlerattr_sethandlerdata(&attr_, func.GetData()); attr_.handler_data = func.GetData();
typedef typename ReturnOf<T>::Return Return; typedef typename ReturnOf<T>::Return Return;
typedef typename ConvertParams<F, T>::Func ConvertedParamsFunc; typedef typename ConvertParams<F, T>::Func ConvertedParamsFunc;
typedef typename MaybeWrapReturn<ConvertedParamsFunc, Return>::Func typedef typename MaybeWrapReturn<ConvertedParamsFunc, Return>::Func

@ -286,6 +286,10 @@ const upb_handlers *upb_handlers_getsubhandlers_sel(const upb_handlers *h,
const upb_msgdef *upb_handlers_msgdef(const upb_handlers *h) { return h->msg; } const upb_msgdef *upb_handlers_msgdef(const upb_handlers *h) { return h->msg; }
bool upb_handlers_addcleanup(upb_handlers *h, void *p, upb_handlerfree *func) {
return upb_handlercache_addcleanup(h->cache, p, func);
}
upb_handlertype_t upb_handlers_getprimitivehandlertype(const upb_fielddef *f) { upb_handlertype_t upb_handlers_getprimitivehandlertype(const upb_fielddef *f) {
switch (upb_fielddef_type(f)) { switch (upb_fielddef_type(f)) {
case UPB_TYPE_INT32: case UPB_TYPE_INT32:
@ -470,12 +474,13 @@ void upb_handlercache_free(upb_handlercache *cache) {
upb_gfree(cache); upb_gfree(cache);
} }
bool upb_handlers_addcleanup(upb_handlers *h, void *p, upb_handlerfree *func) { bool upb_handlercache_addcleanup(upb_handlercache *c, void *p,
upb_handlerfree *func) {
bool ok; bool ok;
if (upb_inttable_lookupptr(&h->cache->cleanup_, p, NULL)) { if (upb_inttable_lookupptr(&c->cleanup_, p, NULL)) {
return false; return false;
} }
ok = upb_inttable_insertptr(&h->cache->cleanup_, p, upb_value_fptr(func)); ok = upb_inttable_insertptr(&c->cleanup_, p, upb_value_fptr(func));
UPB_ASSERT(ok); UPB_ASSERT(ok);
return true; return true;
} }

@ -602,6 +602,8 @@ upb_handlercache *upb_handlercache_new(upb_handlers_callback *callback,
void upb_handlercache_free(upb_handlercache *cache); void upb_handlercache_free(upb_handlercache *cache);
const upb_handlers *upb_handlercache_get(upb_handlercache *cache, const upb_handlers *upb_handlercache_get(upb_handlercache *cache,
const upb_msgdef *md); const upb_msgdef *md);
bool upb_handlercache_addcleanup(upb_handlercache *h, void *p,
upb_handlerfree *hfree);
UPB_END_EXTERN_C UPB_END_EXTERN_C

@ -301,13 +301,13 @@ static void json_parser_any_frame_set_payload_type(
/* Initialize encoder. */ /* Initialize encoder. */
h = upb_handlercache_get(frame->encoder_handlercache, payload_type); h = upb_handlercache_get(frame->encoder_handlercache, payload_type);
encoder = upb_pb_encoder_create(p->env, h, &frame->stringsink.sink); encoder = upb_pb_encoder_create(p->env, h, frame->stringsink.sink);
/* Initialize parser. */ /* Initialize parser. */
parser_method = upb_json_codecache_get(frame->parser_codecache, payload_type); parser_method = upb_json_codecache_get(frame->parser_codecache, payload_type);
upb_sink_reset(&frame->sink, h, encoder); upb_sink_reset(&frame->sink, h, encoder);
frame->parser = upb_json_parser_create(p->env, parser_method, p->symtab, frame->parser = upb_json_parser_create(p->env, parser_method, p->symtab,
&frame->sink, p->ignore_json_unknown); frame->sink, p->ignore_json_unknown);
} }
static void json_parser_any_frame_free(upb_jsonparser_any_frame *frame) { static void json_parser_any_frame_free(upb_jsonparser_any_frame *frame) {
@ -383,9 +383,12 @@ static bool check_stack(upb_json_parser *p) {
static void set_name_table(upb_json_parser *p, upb_jsonparser_frame *frame) { static void set_name_table(upb_json_parser *p, upb_jsonparser_frame *frame) {
upb_value v; upb_value v;
const upb_json_codecache *cache = p->method->cache; const upb_json_codecache *cache = p->method->cache;
bool ok = upb_inttable_lookupptr(&cache->methods, frame->m, &v); bool ok;
const upb_json_parsermethod *method = upb_value_getptr(v); const upb_json_parsermethod *method;
ok = upb_inttable_lookupptr(&cache->methods, frame->m, &v);
UPB_ASSERT(ok); UPB_ASSERT(ok);
method = upb_value_getconstptr(v);
frame->name_table = &method->name_table; frame->name_table = &method->name_table;
} }
@ -2416,11 +2419,11 @@ static bool is_string_wrapper_object(upb_json_parser *p) {
* final state once, when the closing '"' is seen. */ * final state once, when the closing '"' is seen. */
#line 2578 "upb/json/parser.rl" #line 2581 "upb/json/parser.rl"
#line 2424 "upb/json/parser.c" #line 2427 "upb/json/parser.c"
static const char _json_actions[] = { static const char _json_actions[] = {
0, 1, 0, 1, 1, 1, 3, 1, 0, 1, 0, 1, 1, 1, 3, 1,
4, 1, 6, 1, 7, 1, 8, 1, 4, 1, 6, 1, 7, 1, 8, 1,
@ -2667,7 +2670,7 @@ static const int json_en_value_machine = 75;
static const int json_en_main = 1; static const int json_en_main = 1;
#line 2581 "upb/json/parser.rl" #line 2584 "upb/json/parser.rl"
size_t parse(void *closure, const void *hd, const char *buf, size_t size, size_t parse(void *closure, const void *hd, const char *buf, size_t size,
const upb_bufhandle *handle) { const upb_bufhandle *handle) {
@ -2690,7 +2693,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size,
capture_resume(parser, buf); capture_resume(parser, buf);
#line 2694 "upb/json/parser.c" #line 2697 "upb/json/parser.c"
{ {
int _klen; int _klen;
unsigned int _trans; unsigned int _trans;
@ -2765,83 +2768,83 @@ _match:
switch ( *_acts++ ) switch ( *_acts++ )
{ {
case 1: case 1:
#line 2429 "upb/json/parser.rl" #line 2432 "upb/json/parser.rl"
{ p--; {cs = stack[--top]; goto _again;} } { p--; {cs = stack[--top]; goto _again;} }
break; break;
case 2: case 2:
#line 2431 "upb/json/parser.rl" #line 2434 "upb/json/parser.rl"
{ p--; {stack[top++] = cs; cs = 23;goto _again;} } { p--; {stack[top++] = cs; cs = 23;goto _again;} }
break; break;
case 3: case 3:
#line 2435 "upb/json/parser.rl" #line 2438 "upb/json/parser.rl"
{ start_text(parser, p); } { start_text(parser, p); }
break; break;
case 4: case 4:
#line 2436 "upb/json/parser.rl" #line 2439 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_text(parser, p)); } { CHECK_RETURN_TOP(end_text(parser, p)); }
break; break;
case 5: case 5:
#line 2442 "upb/json/parser.rl" #line 2445 "upb/json/parser.rl"
{ start_hex(parser); } { start_hex(parser); }
break; break;
case 6: case 6:
#line 2443 "upb/json/parser.rl" #line 2446 "upb/json/parser.rl"
{ hexdigit(parser, p); } { hexdigit(parser, p); }
break; break;
case 7: case 7:
#line 2444 "upb/json/parser.rl" #line 2447 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_hex(parser)); } { CHECK_RETURN_TOP(end_hex(parser)); }
break; break;
case 8: case 8:
#line 2450 "upb/json/parser.rl" #line 2453 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(escape(parser, p)); } { CHECK_RETURN_TOP(escape(parser, p)); }
break; break;
case 9: case 9:
#line 2456 "upb/json/parser.rl" #line 2459 "upb/json/parser.rl"
{ p--; {cs = stack[--top]; goto _again;} } { p--; {cs = stack[--top]; goto _again;} }
break; break;
case 10: case 10:
#line 2468 "upb/json/parser.rl" #line 2471 "upb/json/parser.rl"
{ start_duration_base(parser, p); } { start_duration_base(parser, p); }
break; break;
case 11: case 11:
#line 2469 "upb/json/parser.rl" #line 2472 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_duration_base(parser, p)); } { CHECK_RETURN_TOP(end_duration_base(parser, p)); }
break; break;
case 12: case 12:
#line 2471 "upb/json/parser.rl" #line 2474 "upb/json/parser.rl"
{ p--; {cs = stack[--top]; goto _again;} } { p--; {cs = stack[--top]; goto _again;} }
break; break;
case 13: case 13:
#line 2476 "upb/json/parser.rl" #line 2479 "upb/json/parser.rl"
{ start_timestamp_base(parser, p); } { start_timestamp_base(parser, p); }
break; break;
case 14: case 14:
#line 2477 "upb/json/parser.rl" #line 2480 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_timestamp_base(parser, p)); } { CHECK_RETURN_TOP(end_timestamp_base(parser, p)); }
break; break;
case 15: case 15:
#line 2479 "upb/json/parser.rl" #line 2482 "upb/json/parser.rl"
{ start_timestamp_fraction(parser, p); } { start_timestamp_fraction(parser, p); }
break; break;
case 16: case 16:
#line 2480 "upb/json/parser.rl" #line 2483 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); } { CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); }
break; break;
case 17: case 17:
#line 2482 "upb/json/parser.rl" #line 2485 "upb/json/parser.rl"
{ start_timestamp_zone(parser, p); } { start_timestamp_zone(parser, p); }
break; break;
case 18: case 18:
#line 2483 "upb/json/parser.rl" #line 2486 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); } { CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); }
break; break;
case 19: case 19:
#line 2485 "upb/json/parser.rl" #line 2488 "upb/json/parser.rl"
{ p--; {cs = stack[--top]; goto _again;} } { p--; {cs = stack[--top]; goto _again;} }
break; break;
case 20: case 20:
#line 2490 "upb/json/parser.rl" #line 2493 "upb/json/parser.rl"
{ {
if (is_wellknown_msg(parser, UPB_WELLKNOWN_TIMESTAMP)) { if (is_wellknown_msg(parser, UPB_WELLKNOWN_TIMESTAMP)) {
{stack[top++] = cs; cs = 47;goto _again;} {stack[top++] = cs; cs = 47;goto _again;}
@ -2853,11 +2856,11 @@ _match:
} }
break; break;
case 21: case 21:
#line 2501 "upb/json/parser.rl" #line 2504 "upb/json/parser.rl"
{ p--; {stack[top++] = cs; cs = 75;goto _again;} } { p--; {stack[top++] = cs; cs = 75;goto _again;} }
break; break;
case 22: case 22:
#line 2506 "upb/json/parser.rl" #line 2509 "upb/json/parser.rl"
{ {
if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) {
start_any_member(parser, p); start_any_member(parser, p);
@ -2867,11 +2870,11 @@ _match:
} }
break; break;
case 23: case 23:
#line 2513 "upb/json/parser.rl" #line 2516 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_membername(parser)); } { CHECK_RETURN_TOP(end_membername(parser)); }
break; break;
case 24: case 24:
#line 2516 "upb/json/parser.rl" #line 2519 "upb/json/parser.rl"
{ {
if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) {
end_any_member(parser, p); end_any_member(parser, p);
@ -2881,7 +2884,7 @@ _match:
} }
break; break;
case 25: case 25:
#line 2527 "upb/json/parser.rl" #line 2530 "upb/json/parser.rl"
{ {
if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) {
start_any_object(parser, p); start_any_object(parser, p);
@ -2891,7 +2894,7 @@ _match:
} }
break; break;
case 26: case 26:
#line 2536 "upb/json/parser.rl" #line 2539 "upb/json/parser.rl"
{ {
if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) {
CHECK_RETURN_TOP(end_any_object(parser, p)); CHECK_RETURN_TOP(end_any_object(parser, p));
@ -2901,54 +2904,54 @@ _match:
} }
break; break;
case 27: case 27:
#line 2548 "upb/json/parser.rl" #line 2551 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(start_array(parser)); } { CHECK_RETURN_TOP(start_array(parser)); }
break; break;
case 28: case 28:
#line 2552 "upb/json/parser.rl" #line 2555 "upb/json/parser.rl"
{ end_array(parser); } { end_array(parser); }
break; break;
case 29: case 29:
#line 2557 "upb/json/parser.rl" #line 2560 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(start_number(parser, p)); } { CHECK_RETURN_TOP(start_number(parser, p)); }
break; break;
case 30: case 30:
#line 2558 "upb/json/parser.rl" #line 2561 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_number(parser, p)); } { CHECK_RETURN_TOP(end_number(parser, p)); }
break; break;
case 31: case 31:
#line 2560 "upb/json/parser.rl" #line 2563 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(start_stringval(parser)); } { CHECK_RETURN_TOP(start_stringval(parser)); }
break; break;
case 32: case 32:
#line 2561 "upb/json/parser.rl" #line 2564 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_stringval(parser)); } { CHECK_RETURN_TOP(end_stringval(parser)); }
break; break;
case 33: case 33:
#line 2563 "upb/json/parser.rl" #line 2566 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_bool(parser, true)); } { CHECK_RETURN_TOP(end_bool(parser, true)); }
break; break;
case 34: case 34:
#line 2565 "upb/json/parser.rl" #line 2568 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_bool(parser, false)); } { CHECK_RETURN_TOP(end_bool(parser, false)); }
break; break;
case 35: case 35:
#line 2567 "upb/json/parser.rl" #line 2570 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_null(parser)); } { CHECK_RETURN_TOP(end_null(parser)); }
break; break;
case 36: case 36:
#line 2569 "upb/json/parser.rl" #line 2572 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(start_subobject_full(parser)); } { CHECK_RETURN_TOP(start_subobject_full(parser)); }
break; break;
case 37: case 37:
#line 2570 "upb/json/parser.rl" #line 2573 "upb/json/parser.rl"
{ end_subobject_full(parser); } { end_subobject_full(parser); }
break; break;
case 38: case 38:
#line 2575 "upb/json/parser.rl" #line 2578 "upb/json/parser.rl"
{ p--; {cs = stack[--top]; goto _again;} } { p--; {cs = stack[--top]; goto _again;} }
break; break;
#line 2952 "upb/json/parser.c" #line 2955 "upb/json/parser.c"
} }
} }
@ -2965,32 +2968,32 @@ _again:
while ( __nacts-- > 0 ) { while ( __nacts-- > 0 ) {
switch ( *__acts++ ) { switch ( *__acts++ ) {
case 0: case 0:
#line 2427 "upb/json/parser.rl" #line 2430 "upb/json/parser.rl"
{ p--; {cs = stack[--top]; if ( p == pe ) { p--; {cs = stack[--top]; if ( p == pe )
goto _test_eof; goto _test_eof;
goto _again;} } goto _again;} }
break; break;
case 30: case 30:
#line 2558 "upb/json/parser.rl" #line 2561 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_number(parser, p)); } { CHECK_RETURN_TOP(end_number(parser, p)); }
break; break;
case 33: case 33:
#line 2563 "upb/json/parser.rl" #line 2566 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_bool(parser, true)); } { CHECK_RETURN_TOP(end_bool(parser, true)); }
break; break;
case 34: case 34:
#line 2565 "upb/json/parser.rl" #line 2568 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_bool(parser, false)); } { CHECK_RETURN_TOP(end_bool(parser, false)); }
break; break;
case 35: case 35:
#line 2567 "upb/json/parser.rl" #line 2570 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_null(parser)); } { CHECK_RETURN_TOP(end_null(parser)); }
break; break;
case 37: case 37:
#line 2570 "upb/json/parser.rl" #line 2573 "upb/json/parser.rl"
{ end_subobject_full(parser); } { end_subobject_full(parser); }
break; break;
#line 2994 "upb/json/parser.c" #line 2997 "upb/json/parser.c"
} }
} }
} }
@ -2998,7 +3001,7 @@ goto _again;} }
_out: {} _out: {}
} }
#line 2603 "upb/json/parser.rl" #line 2606 "upb/json/parser.rl"
if (p != pe) { if (p != pe) {
upb_status_seterrf(&parser->status, "Parse error at '%.*s'\n", pe - p, p); upb_status_seterrf(&parser->status, "Parse error at '%.*s'\n", pe - p, p);
@ -3046,13 +3049,13 @@ static void json_parser_reset(upb_json_parser *p) {
/* Emit Ragel initialization of the parser. */ /* Emit Ragel initialization of the parser. */
#line 3050 "upb/json/parser.c" #line 3053 "upb/json/parser.c"
{ {
cs = json_start; cs = json_start;
top = 0; top = 0;
} }
#line 2650 "upb/json/parser.rl" #line 2653 "upb/json/parser.rl"
p->current_state = cs; p->current_state = cs;
p->parser_top = top; p->parser_top = top;
accumulate_clear(p); accumulate_clear(p);
@ -3067,7 +3070,7 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c,
upb_msg_field_iter i; upb_msg_field_iter i;
upb_alloc *alloc = upb_arena_alloc(&c->arena); upb_alloc *alloc = upb_arena_alloc(&c->arena);
upb_json_parsermethod *m = upb_gmalloc(sizeof(*m)); upb_json_parsermethod *m = upb_malloc(alloc, sizeof(*m));
m->cache = c; m->cache = c;
@ -3090,7 +3093,7 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c,
size_t len = upb_fielddef_getjsonname(f, NULL, 0); size_t len = upb_fielddef_getjsonname(f, NULL, 0);
buf = upb_malloc(alloc, len); buf = upb_malloc(alloc, len);
upb_fielddef_getjsonname(f, buf, len); upb_fielddef_getjsonname(f, buf, len);
upb_strtable_insert3(&m->name_table, buf, len, v, alloc); upb_strtable_insert3(&m->name_table, buf, strlen(buf), v, alloc);
if (strcmp(buf, upb_fielddef_name(f)) != 0) { if (strcmp(buf, upb_fielddef_name(f)) != 0) {
/* Since the JSON name is different from the regular field name, add an /* Since the JSON name is different from the regular field name, add an
@ -3109,7 +3112,7 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c,
upb_json_parser *upb_json_parser_create(upb_env *env, upb_json_parser *upb_json_parser_create(upb_env *env,
const upb_json_parsermethod *method, const upb_json_parsermethod *method,
const upb_symtab* symtab, const upb_symtab* symtab,
upb_sink *output, upb_sink output,
bool ignore_json_unknown) { bool ignore_json_unknown) {
#ifndef NDEBUG #ifndef NDEBUG
const size_t size_before = upb_env_bytesallocated(env); const size_t size_before = upb_env_bytesallocated(env);
@ -3125,8 +3128,8 @@ upb_json_parser *upb_json_parser_create(upb_env *env,
upb_bytessink_reset(&p->input_, &method->input_handler_, p); upb_bytessink_reset(&p->input_, &method->input_handler_, p);
json_parser_reset(p); json_parser_reset(p);
upb_sink_reset(&p->top->sink, output->handlers, output->closure); p->top->sink = output;
p->top->m = upb_handlers_msgdef(output->handlers); p->top->m = upb_handlers_msgdef(output.handlers);
if (is_wellknown_msg(p, UPB_WELLKNOWN_ANY)) { if (is_wellknown_msg(p, UPB_WELLKNOWN_ANY)) {
p->top->is_any = true; p->top->is_any = true;
p->top->any_frame = json_parser_any_frame_new(p); p->top->any_frame = json_parser_any_frame_new(p);
@ -3146,8 +3149,8 @@ upb_json_parser *upb_json_parser_create(upb_env *env,
return p; return p;
} }
upb_bytessink *upb_json_parser_input(upb_json_parser *p) { upb_bytessink upb_json_parser_input(upb_json_parser *p) {
return &p->input_; return p->input_;
} }
const upb_byteshandler *upb_json_parsermethod_inputhandler( const upb_byteshandler *upb_json_parsermethod_inputhandler(
@ -3174,21 +3177,22 @@ void upb_json_codecache_free(upb_json_codecache *c) {
upb_gfree(c); upb_gfree(c);
} }
upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c, const upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c,
const upb_msgdef *md) { const upb_msgdef *md) {
upb_json_parsermethod *m; upb_json_parsermethod *m;
upb_value v; upb_value v;
upb_msg_field_iter i; upb_msg_field_iter i;
upb_alloc *alloc = upb_arena_alloc(&c->arena);
if (upb_inttable_lookupptr(&c->methods, md, &v)) { if (upb_inttable_lookupptr(&c->methods, md, &v)) {
return upb_value_getptr(v); return upb_value_getconstptr(v);
} }
m = parsermethod_new(c, md); m = parsermethod_new(c, md);
v = upb_value_ptr(m); v = upb_value_constptr(m);
if (!m) return NULL; if (!m) return NULL;
if (!upb_inttable_insertptr(&c->methods, m, v)) return NULL; if (!upb_inttable_insertptr2(&c->methods, md, v, alloc)) return NULL;
/* Populate parser methods for all submessages, so the name tables will /* Populate parser methods for all submessages, so the name tables will
* be available during parsing. */ * be available during parsing. */

@ -14,17 +14,44 @@
namespace upb { namespace upb {
namespace json { namespace json {
class CodeCache; class CodeCache;
class Parser; class ParserPtr;
class ParserMethod; class ParserMethodPtr;
} /* namespace json */ } /* namespace json */
} /* namespace upb */ } /* namespace upb */
#endif #endif
UPB_DECLARE_TYPE(upb::json::Parser, upb_json_parser) /* upb_json_parsermethod ******************************************************/
UPB_DECLARE_TYPE(upb::json::ParserMethod, upb_json_parsermethod)
UPB_DECLARE_TYPE(upb::json::CodeCache, upb_json_codecache)
/* upb::json::Parser **********************************************************/ struct upb_json_parsermethod;
typedef struct upb_json_parsermethod upb_json_parsermethod;
UPB_BEGIN_EXTERN_C
const upb_byteshandler* upb_json_parsermethod_inputhandler(
const upb_json_parsermethod* m);
UPB_END_EXTERN_C
#ifdef __cplusplus
class upb::json::ParserMethodPtr {
public:
ParserMethodPtr() : ptr_(nullptr) {}
ParserMethodPtr(const upb_json_parsermethod* ptr) : ptr_(ptr) {}
const upb_json_parsermethod* ptr() const { return ptr_; }
const BytesHandler* input_handler() const {
return upb_json_parsermethod_inputhandler(ptr());
}
private:
const upb_json_parsermethod* ptr_;
};
#endif /* __cplusplus */
/* upb_json_parser ************************************************************/
/* Preallocation hint: parser won't allocate more bytes than this when first /* Preallocation hint: parser won't allocate more bytes than this when first
* constructed. This hint may be an overestimate for some build configurations. * constructed. This hint may be an overestimate for some build configurations.
@ -32,89 +59,72 @@ UPB_DECLARE_TYPE(upb::json::CodeCache, upb_json_codecache)
* it may be an underestimate. */ * it may be an underestimate. */
#define UPB_JSON_PARSER_SIZE 5712 #define UPB_JSON_PARSER_SIZE 5712
struct upb_json_parser;
typedef struct upb_json_parser upb_json_parser;
UPB_BEGIN_EXTERN_C
upb_json_parser*
upb_json_parser_create(upb_env* e, const upb_json_parsermethod* m,
const upb_symtab* symtab, upb_sink output,
bool ignore_json_unknown);
upb_bytessink upb_json_parser_input(upb_json_parser* p);
UPB_END_EXTERN_C
#ifdef __cplusplus #ifdef __cplusplus
/* Parses an incoming BytesStream, pushing the results to the destination /* Parses an incoming BytesStream, pushing the results to the destination
* sink. */ * sink. */
class upb::json::Parser { class upb::json::ParserPtr {
public: public:
static Parser* Create(Environment* env, const ParserMethod* method, ParserPtr(upb_json_parser* ptr) : ptr_(ptr) {}
const SymbolTable* symtab,
Sink* output, bool ignore_json_unknown);
BytesSink* input(); static ParserPtr Create(Environment* env, ParserMethodPtr method,
SymbolTable* symtab, Sink output,
bool ignore_json_unknown) {
upb_symtab* symtab_ptr = symtab ? symtab->ptr() : nullptr;
return ParserPtr(upb_json_parser_create(
env, method.ptr(), symtab_ptr, output.sink(), ignore_json_unknown));
}
private: BytesSink input() { return upb_json_parser_input(ptr_); }
UPB_DISALLOW_POD_OPS(Parser, upb::json::Parser)
};
class upb::json::ParserMethod {
public:
/* The input handlers for this decoder method. */
const BytesHandler* input_handler() const;
private: private:
UPB_DISALLOW_POD_OPS(ParserMethod, upb::json::ParserMethod) upb_json_parser* ptr_;
}; };
class upb::json::CodeCache { #endif /* __cplusplus */
public:
static CodeCache* New();
static void Free(CodeCache* cache);
/* Returns a DecoderMethod that can push data to the given handlers.
* If a suitable method already exists, it will be returned from the cache. */
const ParserMethod *Get(const MessageDef* md);
private: /* upb_json_codecache *********************************************************/
UPB_DISALLOW_POD_OPS(CodeCache, upb::json::CodeCache)
};
#endif struct upb_json_codecache;
typedef struct upb_json_codecache upb_json_codecache;
UPB_BEGIN_EXTERN_C UPB_BEGIN_EXTERN_C
upb_json_parser* upb_json_parser_create(upb_env* e,
const upb_json_parsermethod* m,
const upb_symtab* symtab,
upb_sink* output,
bool ignore_json_unknown);
upb_bytessink *upb_json_parser_input(upb_json_parser *p);
const upb_byteshandler *upb_json_parsermethod_inputhandler(
const upb_json_parsermethod *m);
upb_json_codecache *upb_json_codecache_new(); upb_json_codecache *upb_json_codecache_new();
void upb_json_codecache_free(upb_json_codecache *cache); void upb_json_codecache_free(upb_json_codecache *cache);
upb_json_parsermethod* upb_json_codecache_get(upb_json_codecache* cache, const upb_json_parsermethod* upb_json_codecache_get(upb_json_codecache* cache,
const upb_msgdef* md); const upb_msgdef* md);
UPB_END_EXTERN_C UPB_END_EXTERN_C
#ifdef __cplusplus #ifdef __cplusplus
namespace upb { class upb::json::CodeCache {
namespace json { public:
inline Parser* Parser::Create(Environment* env, const ParserMethod* method, CodeCache() : ptr_(upb_json_codecache_new(), upb_json_codecache_free) {}
const SymbolTable* symtab,
Sink* output, bool ignore_json_unknown) {
return upb_json_parser_create(
env, method, symtab, output, ignore_json_unknown);
}
inline BytesSink* Parser::input() {
return upb_json_parser_input(this);
}
inline const BytesHandler* ParserMethod::input_handler() const {
return upb_json_parsermethod_inputhandler(this);
}
/* static */
inline const ParserMethod* CodeCache::Get(const MessageDef* md) {
return upb_json_codecache_get(this, md);
}
} /* namespace json */ /* Returns a DecoderMethod that can push data to the given handlers.
} /* namespace upb */ * If a suitable method already exists, it will be returned from the cache. */
ParserMethodPtr Get(MessageDefPtr md) {
return upb_json_codecache_get(ptr_.get(), md.ptr());
}
private:
std::unique_ptr<upb_json_codecache, decltype(&upb_json_codecache_free)> ptr_;
};
#endif #endif

@ -299,13 +299,13 @@ static void json_parser_any_frame_set_payload_type(
/* Initialize encoder. */ /* Initialize encoder. */
h = upb_handlercache_get(frame->encoder_handlercache, payload_type); h = upb_handlercache_get(frame->encoder_handlercache, payload_type);
encoder = upb_pb_encoder_create(p->env, h, &frame->stringsink.sink); encoder = upb_pb_encoder_create(p->env, h, frame->stringsink.sink);
/* Initialize parser. */ /* Initialize parser. */
parser_method = upb_json_codecache_get(frame->parser_codecache, payload_type); parser_method = upb_json_codecache_get(frame->parser_codecache, payload_type);
upb_sink_reset(&frame->sink, h, encoder); upb_sink_reset(&frame->sink, h, encoder);
frame->parser = upb_json_parser_create(p->env, parser_method, p->symtab, frame->parser = upb_json_parser_create(p->env, parser_method, p->symtab,
&frame->sink, p->ignore_json_unknown); frame->sink, p->ignore_json_unknown);
} }
static void json_parser_any_frame_free(upb_jsonparser_any_frame *frame) { static void json_parser_any_frame_free(upb_jsonparser_any_frame *frame) {
@ -381,9 +381,12 @@ static bool check_stack(upb_json_parser *p) {
static void set_name_table(upb_json_parser *p, upb_jsonparser_frame *frame) { static void set_name_table(upb_json_parser *p, upb_jsonparser_frame *frame) {
upb_value v; upb_value v;
const upb_json_codecache *cache = p->method->cache; const upb_json_codecache *cache = p->method->cache;
bool ok = upb_inttable_lookupptr(&cache->methods, frame->m, &v); bool ok;
const upb_json_parsermethod *method = upb_value_getptr(v); const upb_json_parsermethod *method;
ok = upb_inttable_lookupptr(&cache->methods, frame->m, &v);
UPB_ASSERT(ok); UPB_ASSERT(ok);
method = upb_value_getconstptr(v);
frame->name_table = &method->name_table; frame->name_table = &method->name_table;
} }
@ -2661,7 +2664,7 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c,
upb_msg_field_iter i; upb_msg_field_iter i;
upb_alloc *alloc = upb_arena_alloc(&c->arena); upb_alloc *alloc = upb_arena_alloc(&c->arena);
upb_json_parsermethod *m = upb_gmalloc(sizeof(*m)); upb_json_parsermethod *m = upb_malloc(alloc, sizeof(*m));
m->cache = c; m->cache = c;
@ -2684,7 +2687,7 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c,
size_t len = upb_fielddef_getjsonname(f, NULL, 0); size_t len = upb_fielddef_getjsonname(f, NULL, 0);
buf = upb_malloc(alloc, len); buf = upb_malloc(alloc, len);
upb_fielddef_getjsonname(f, buf, len); upb_fielddef_getjsonname(f, buf, len);
upb_strtable_insert3(&m->name_table, buf, len, v, alloc); upb_strtable_insert3(&m->name_table, buf, strlen(buf), v, alloc);
if (strcmp(buf, upb_fielddef_name(f)) != 0) { if (strcmp(buf, upb_fielddef_name(f)) != 0) {
/* Since the JSON name is different from the regular field name, add an /* Since the JSON name is different from the regular field name, add an
@ -2703,7 +2706,7 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c,
upb_json_parser *upb_json_parser_create(upb_env *env, upb_json_parser *upb_json_parser_create(upb_env *env,
const upb_json_parsermethod *method, const upb_json_parsermethod *method,
const upb_symtab* symtab, const upb_symtab* symtab,
upb_sink *output, upb_sink output,
bool ignore_json_unknown) { bool ignore_json_unknown) {
#ifndef NDEBUG #ifndef NDEBUG
const size_t size_before = upb_env_bytesallocated(env); const size_t size_before = upb_env_bytesallocated(env);
@ -2719,8 +2722,8 @@ upb_json_parser *upb_json_parser_create(upb_env *env,
upb_bytessink_reset(&p->input_, &method->input_handler_, p); upb_bytessink_reset(&p->input_, &method->input_handler_, p);
json_parser_reset(p); json_parser_reset(p);
upb_sink_reset(&p->top->sink, output->handlers, output->closure); p->top->sink = output;
p->top->m = upb_handlers_msgdef(output->handlers); p->top->m = upb_handlers_msgdef(output.handlers);
if (is_wellknown_msg(p, UPB_WELLKNOWN_ANY)) { if (is_wellknown_msg(p, UPB_WELLKNOWN_ANY)) {
p->top->is_any = true; p->top->is_any = true;
p->top->any_frame = json_parser_any_frame_new(p); p->top->any_frame = json_parser_any_frame_new(p);
@ -2740,8 +2743,8 @@ upb_json_parser *upb_json_parser_create(upb_env *env,
return p; return p;
} }
upb_bytessink *upb_json_parser_input(upb_json_parser *p) { upb_bytessink upb_json_parser_input(upb_json_parser *p) {
return &p->input_; return p->input_;
} }
const upb_byteshandler *upb_json_parsermethod_inputhandler( const upb_byteshandler *upb_json_parsermethod_inputhandler(
@ -2768,21 +2771,22 @@ void upb_json_codecache_free(upb_json_codecache *c) {
upb_gfree(c); upb_gfree(c);
} }
upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c, const upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c,
const upb_msgdef *md) { const upb_msgdef *md) {
upb_json_parsermethod *m; upb_json_parsermethod *m;
upb_value v; upb_value v;
upb_msg_field_iter i; upb_msg_field_iter i;
upb_alloc *alloc = upb_arena_alloc(&c->arena);
if (upb_inttable_lookupptr(&c->methods, md, &v)) { if (upb_inttable_lookupptr(&c->methods, md, &v)) {
return upb_value_getptr(v); return upb_value_getconstptr(v);
} }
m = parsermethod_new(c, md); m = parsermethod_new(c, md);
v = upb_value_ptr(m); v = upb_value_constptr(m);
if (!m) return NULL; if (!m) return NULL;
if (!upb_inttable_insertptr(&c->methods, m, v)) return NULL; if (!upb_inttable_insertptr2(&c->methods, md, v, alloc)) return NULL;
/* Populate parser methods for all submessages, so the name tables will /* Populate parser methods for all submessages, so the name tables will
* be available during parsing. */ * be available during parsing. */

@ -13,7 +13,7 @@ struct upb_json_printer {
upb_sink input_; upb_sink input_;
/* BytesSink closure. */ /* BytesSink closure. */
void *subc_; void *subc_;
upb_bytessink *output_; upb_bytessink output_;
/* We track the depth so that we know when to emit startstr/endstr on the /* We track the depth so that we know when to emit startstr/endstr on the
* output. */ * output. */
@ -87,7 +87,7 @@ strpc *newstrpc_str(upb_handlers *h, const char * str) {
static void print_data( static void print_data(
upb_json_printer *p, const char *buf, unsigned int len) { upb_json_printer *p, const char *buf, unsigned int len) {
/* TODO: Will need to change if we support pushback from the sink. */ /* TODO: Will need to change if we support pushback from the sink. */
size_t n = upb_bytessink_putbuf(p->output_, p->subc_, buf, len, NULL); size_t n = upb_bytessink_putbuf(&p->output_, p->subc_, buf, len, NULL);
UPB_ASSERT(n == len); UPB_ASSERT(n == len);
} }
@ -369,7 +369,7 @@ static bool printer_startmsg(void *closure, const void *handler_data) {
upb_json_printer *p = closure; upb_json_printer *p = closure;
UPB_UNUSED(handler_data); UPB_UNUSED(handler_data);
if (p->depth_ == 0) { if (p->depth_ == 0) {
upb_bytessink_start(p->output_, 0, &p->subc_); upb_bytessink_start(&p->output_, 0, &p->subc_);
} }
start_frame(p); start_frame(p);
return true; return true;
@ -381,7 +381,7 @@ static bool printer_endmsg(void *closure, const void *handler_data, upb_status *
UPB_UNUSED(s); UPB_UNUSED(s);
end_frame(p); end_frame(p);
if (p->depth_ == 0) { if (p->depth_ == 0) {
upb_bytessink_end(p->output_); upb_bytessink_end(&p->output_);
} }
return true; return true;
} }
@ -770,7 +770,7 @@ static bool printer_startdurationmsg(void *closure, const void *handler_data) {
upb_json_printer *p = closure; upb_json_printer *p = closure;
UPB_UNUSED(handler_data); UPB_UNUSED(handler_data);
if (p->depth_ == 0) { if (p->depth_ == 0) {
upb_bytessink_start(p->output_, 0, &p->subc_); upb_bytessink_start(&p->output_, 0, &p->subc_);
} }
return true; return true;
} }
@ -828,7 +828,7 @@ static bool printer_enddurationmsg(void *closure, const void *handler_data,
print_data(p, "\"", 1); print_data(p, "\"", 1);
if (p->depth_ == 0) { if (p->depth_ == 0) {
upb_bytessink_end(p->output_); upb_bytessink_end(&p->output_);
} }
UPB_UNUSED(handler_data); UPB_UNUSED(handler_data);
@ -839,7 +839,7 @@ static bool printer_starttimestampmsg(void *closure, const void *handler_data) {
upb_json_printer *p = closure; upb_json_printer *p = closure;
UPB_UNUSED(handler_data); UPB_UNUSED(handler_data);
if (p->depth_ == 0) { if (p->depth_ == 0) {
upb_bytessink_start(p->output_, 0, &p->subc_); upb_bytessink_start(&p->output_, 0, &p->subc_);
} }
return true; return true;
} }
@ -902,7 +902,7 @@ static bool printer_endtimestampmsg(void *closure, const void *handler_data,
print_data(p, "\"", 1); print_data(p, "\"", 1);
if (p->depth_ == 0) { if (p->depth_ == 0) {
upb_bytessink_end(p->output_); upb_bytessink_end(&p->output_);
} }
UPB_UNUSED(handler_data); UPB_UNUSED(handler_data);
@ -914,7 +914,7 @@ static bool printer_startmsg_noframe(void *closure, const void *handler_data) {
upb_json_printer *p = closure; upb_json_printer *p = closure;
UPB_UNUSED(handler_data); UPB_UNUSED(handler_data);
if (p->depth_ == 0) { if (p->depth_ == 0) {
upb_bytessink_start(p->output_, 0, &p->subc_); upb_bytessink_start(&p->output_, 0, &p->subc_);
} }
return true; return true;
} }
@ -925,7 +925,7 @@ static bool printer_endmsg_noframe(
UPB_UNUSED(handler_data); UPB_UNUSED(handler_data);
UPB_UNUSED(s); UPB_UNUSED(s);
if (p->depth_ == 0) { if (p->depth_ == 0) {
upb_bytessink_end(p->output_); upb_bytessink_end(&p->output_);
} }
return true; return true;
} }
@ -1253,7 +1253,7 @@ static void json_printer_reset(upb_json_printer *p) {
/* Public API *****************************************************************/ /* Public API *****************************************************************/
upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h, upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h,
upb_bytessink *output) { upb_bytessink output) {
#ifndef NDEBUG #ifndef NDEBUG
size_t size_before = upb_env_bytesallocated(e); size_t size_before = upb_env_bytesallocated(e);
#endif #endif
@ -1273,12 +1273,16 @@ upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h,
return p; return p;
} }
upb_sink *upb_json_printer_input(upb_json_printer *p) { upb_sink upb_json_printer_input(upb_json_printer *p) {
return &p->input_; return p->input_;
} }
upb_handlercache *upb_json_printer_newcache(bool preserve_proto_fieldnames) { upb_handlercache *upb_json_printer_newcache(bool preserve_proto_fieldnames) {
upb_json_printercache *cache = upb_gmalloc(sizeof(*cache)); upb_json_printercache *cache = upb_gmalloc(sizeof(*cache));
upb_handlercache *ret = upb_handlercache_new(printer_sethandlers, cache);
cache->preserve_fieldnames = preserve_proto_fieldnames; cache->preserve_fieldnames = preserve_proto_fieldnames;
return upb_handlercache_new(printer_sethandlers, cache); upb_handlercache_addcleanup(ret, cache, upb_gfree);
return ret;
} }

@ -12,44 +12,24 @@
#ifdef __cplusplus #ifdef __cplusplus
namespace upb { namespace upb {
namespace json { namespace json {
class Printer; class PrinterPtr;
} /* namespace json */ } /* namespace json */
} /* namespace upb */ } /* namespace upb */
#endif #endif
UPB_DECLARE_TYPE(upb::json::Printer, upb_json_printer) /* upb_json_printer ***********************************************************/
/* upb::json::Printer *********************************************************/
#define UPB_JSON_PRINTER_SIZE 192 #define UPB_JSON_PRINTER_SIZE 192
#ifdef __cplusplus struct upb_json_printer;
typedef struct upb_json_printer upb_json_printer;
/* Prints an incoming stream of data to a BytesSink in JSON format. */
class upb::json::Printer {
public:
static Printer* Create(Environment* env, const upb::Handlers* handlers,
BytesSink* output);
/* The input to the printer. */
Sink* input();
static const size_t kSize = UPB_JSON_PRINTER_SIZE;
static upb_handlercache* NewCache(bool preserve_proto_fieldnames);
private:
UPB_DISALLOW_POD_OPS(Printer, upb::json::Printer)
};
#endif
UPB_BEGIN_EXTERN_C UPB_BEGIN_EXTERN_C
/* Native C API. */ /* Native C API. */
upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h, upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h,
upb_bytessink *output); upb_bytessink output);
upb_sink *upb_json_printer_input(upb_json_printer *p); upb_sink upb_json_printer_input(upb_json_printer *p);
const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md, const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md,
bool preserve_fieldnames, bool preserve_fieldnames,
const void *owner); const void *owner);
@ -60,19 +40,29 @@ UPB_END_EXTERN_C
#ifdef __cplusplus #ifdef __cplusplus
namespace upb { /* Prints an incoming stream of data to a BytesSink in JSON format. */
namespace json { class upb::json::PrinterPtr {
inline Printer* Printer::Create(Environment* env, const upb::Handlers* handlers, public:
BytesSink* output) { PrinterPtr(upb_json_printer* ptr) : ptr_(ptr) {}
return upb_json_printer_create(env, handlers, output);
} static PrinterPtr Create(Environment *env, const upb::Handlers *handlers,
inline Sink* Printer::input() { return upb_json_printer_input(this); } BytesSink output) {
inline upb_handlercache* Printer::NewCache(bool preserve_proto_fieldnames) { return PrinterPtr(upb_json_printer_create(env, handlers, output.sink()));
}
/* The input to the printer. */
Sink input() { return upb_json_printer_input(ptr_); }
static const size_t kSize = UPB_JSON_PRINTER_SIZE;
static HandlerCache NewCache(bool preserve_proto_fieldnames) {
return upb_json_printer_newcache(preserve_proto_fieldnames); return upb_json_printer_newcache(preserve_proto_fieldnames);
} }
} /* namespace json */
} /* namespace upb */
#endif private:
upb_json_printer* ptr_;
};
#endif /* __cplusplus */
#endif /* UPB_JSON_TYPED_PRINTER_H_ */ #endif /* UPB_JSON_TYPED_PRINTER_H_ */

@ -993,7 +993,7 @@ void upb_pbdecoder_reset(upb_pbdecoder *d) {
} }
upb_pbdecoder *upb_pbdecoder_create(upb_env *e, const upb_pbdecodermethod *m, upb_pbdecoder *upb_pbdecoder_create(upb_env *e, const upb_pbdecodermethod *m,
upb_sink *sink) { upb_sink sink) {
const size_t default_max_nesting = 64; const size_t default_max_nesting = 64;
#ifndef NDEBUG #ifndef NDEBUG
size_t size_before = upb_env_bytesallocated(e); size_t size_before = upb_env_bytesallocated(e);
@ -1017,12 +1017,11 @@ upb_pbdecoder *upb_pbdecoder_create(upb_env *e, const upb_pbdecodermethod *m,
upb_pbdecoder_reset(d); upb_pbdecoder_reset(d);
upb_bytessink_reset(&d->input_, &m->input_handler_, d); upb_bytessink_reset(&d->input_, &m->input_handler_, d);
UPB_ASSERT(sink);
if (d->method_->dest_handlers_) { if (d->method_->dest_handlers_) {
if (sink->handlers != d->method_->dest_handlers_) if (sink.handlers != d->method_->dest_handlers_)
return NULL; return NULL;
} }
upb_sink_reset(&d->top->sink, sink->handlers, sink->closure); d->top->sink = sink;
/* If this fails, increase the value in decoder.h. */ /* If this fails, increase the value in decoder.h. */
UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(e) - size_before <= UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(e) - size_before <=
@ -1038,8 +1037,8 @@ const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d) {
return d->method_; return d->method_;
} }
upb_bytessink *upb_pbdecoder_input(upb_pbdecoder *d) { upb_bytessink upb_pbdecoder_input(upb_pbdecoder *d) {
return &d->input_; return d->input_;
} }
size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d) { size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d) {

@ -56,6 +56,7 @@ UPB_END_EXTERN_C
* Handlers. */ * Handlers. */
class upb::pb::DecoderMethodPtr { class upb::pb::DecoderMethodPtr {
public: public:
DecoderMethodPtr() : ptr_(nullptr) {}
DecoderMethodPtr(const upb_pbdecodermethod* ptr) : ptr_(ptr) {} DecoderMethodPtr(const upb_pbdecodermethod* ptr) : ptr_(ptr) {}
const upb_pbdecodermethod* ptr() { return ptr_; } const upb_pbdecodermethod* ptr() { return ptr_; }
@ -98,9 +99,9 @@ UPB_BEGIN_EXTERN_C
upb_pbdecoder *upb_pbdecoder_create(upb_env *e, upb_pbdecoder *upb_pbdecoder_create(upb_env *e,
const upb_pbdecodermethod *method, const upb_pbdecodermethod *method,
upb_sink *output); upb_sink output);
const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d); const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d);
upb_bytessink *upb_pbdecoder_input(upb_pbdecoder *d); upb_bytessink upb_pbdecoder_input(upb_pbdecoder *d);
uint64_t upb_pbdecoder_bytesparsed(const upb_pbdecoder *d); uint64_t upb_pbdecoder_bytesparsed(const upb_pbdecoder *d);
size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d); size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d);
bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max); bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max);
@ -124,8 +125,8 @@ class upb::pb::DecoderPtr {
* *
* The sink must match the given method. */ * The sink must match the given method. */
static DecoderPtr Create(Environment *env, DecoderMethodPtr method, static DecoderPtr Create(Environment *env, DecoderMethodPtr method,
upb_sink *output) { upb::Sink output) {
return DecoderPtr(upb_pbdecoder_create(env, method.ptr(), output)); return DecoderPtr(upb_pbdecoder_create(env, method.ptr(), output.sink()));
} }
/* Returns the DecoderMethod this decoder is parsing from. */ /* Returns the DecoderMethod this decoder is parsing from. */
@ -134,7 +135,7 @@ class upb::pb::DecoderPtr {
} }
/* The sink on which this decoder receives input. */ /* The sink on which this decoder receives input. */
upb_bytessink* input() { return upb_pbdecoder_input(ptr()); } BytesSink input() { return BytesSink(upb_pbdecoder_input(ptr())); }
/* Returns number of bytes successfully parsed. /* Returns number of bytes successfully parsed.
* *

@ -95,7 +95,7 @@ struct upb_pb_encoder {
/* Our input and output. */ /* Our input and output. */
upb_sink input_; upb_sink input_;
upb_bytessink *output_; upb_bytessink output_;
/* The "subclosure" -- used as the inner closure as part of the bytessink /* The "subclosure" -- used as the inner closure as part of the bytessink
* protocol. */ * protocol. */
@ -127,7 +127,7 @@ struct upb_pb_encoder {
/* TODO(haberman): handle pushback */ /* TODO(haberman): handle pushback */
static void putbuf(upb_pb_encoder *e, const char *buf, size_t len) { static void putbuf(upb_pb_encoder *e, const char *buf, size_t len) {
size_t n = upb_bytessink_putbuf(e->output_, e->subc, buf, len, NULL); size_t n = upb_bytessink_putbuf(&e->output_, e->subc, buf, len, NULL);
UPB_ASSERT(n == len); UPB_ASSERT(n == len);
} }
@ -353,7 +353,7 @@ static bool startmsg(void *c, const void *hd) {
upb_pb_encoder *e = c; upb_pb_encoder *e = c;
UPB_UNUSED(hd); UPB_UNUSED(hd);
if (e->depth++ == 0) { if (e->depth++ == 0) {
upb_bytessink_start(e->output_, 0, &e->subc); upb_bytessink_start(&e->output_, 0, &e->subc);
} }
return true; return true;
} }
@ -363,7 +363,7 @@ static bool endmsg(void *c, const void *hd, upb_status *status) {
UPB_UNUSED(hd); UPB_UNUSED(hd);
UPB_UNUSED(status); UPB_UNUSED(status);
if (--e->depth == 0) { if (--e->depth == 0) {
upb_bytessink_end(e->output_); upb_bytessink_end(&e->output_);
} }
return true; return true;
} }
@ -527,7 +527,7 @@ upb_handlercache *upb_pb_encoder_newcache() {
} }
upb_pb_encoder *upb_pb_encoder_create(upb_env *env, const upb_handlers *h, upb_pb_encoder *upb_pb_encoder_create(upb_env *env, const upb_handlers *h,
upb_bytessink *output) { upb_bytessink output) {
const size_t initial_bufsize = 256; const size_t initial_bufsize = 256;
const size_t initial_segbufsize = 16; const size_t initial_segbufsize = 16;
/* TODO(haberman): make this configurable. */ /* TODO(haberman): make this configurable. */
@ -556,7 +556,7 @@ upb_pb_encoder *upb_pb_encoder_create(upb_env *env, const upb_handlers *h,
e->env = env; e->env = env;
e->output_ = output; e->output_ = output;
e->subc = output->closure; e->subc = output.closure;
e->ptr = e->buf; e->ptr = e->buf;
/* If this fails, increase the value in encoder.h. */ /* If this fails, increase the value in encoder.h. */
@ -565,4 +565,4 @@ upb_pb_encoder *upb_pb_encoder_create(upb_env *env, const upb_handlers *h,
return e; return e;
} }
upb_sink *upb_pb_encoder_input(upb_pb_encoder *e) { return &e->input_; } upb_sink upb_pb_encoder_input(upb_pb_encoder *e) { return e->input_; }

@ -30,16 +30,16 @@ class EncoderPtr;
* constructed. This hint may be an overestimate for some build configurations. * constructed. This hint may be an overestimate for some build configurations.
* But if the decoder library is upgraded without recompiling the application, * But if the decoder library is upgraded without recompiling the application,
* it may be an underestimate. */ * it may be an underestimate. */
#define UPB_PB_ENCODER_SIZE 768 #define UPB_PB_ENCODER_SIZE 784
struct upb_pb_encoder; struct upb_pb_encoder;
typedef struct upb_pb_encoder upb_pb_encoder; typedef struct upb_pb_encoder upb_pb_encoder;
UPB_BEGIN_EXTERN_C UPB_BEGIN_EXTERN_C
upb_sink *upb_pb_encoder_input(upb_pb_encoder *p); upb_sink upb_pb_encoder_input(upb_pb_encoder *p);
upb_pb_encoder* upb_pb_encoder_create(upb_env* e, const upb_handlers* h, upb_pb_encoder* upb_pb_encoder_create(upb_env* e, const upb_handlers* h,
upb_bytessink* output); upb_bytessink output);
upb_handlercache *upb_pb_encoder_newcache(); upb_handlercache *upb_pb_encoder_newcache();
@ -56,12 +56,12 @@ class upb::pb::EncoderPtr {
/* Creates a new encoder in the given environment. The Handlers must have /* Creates a new encoder in the given environment. The Handlers must have
* come from NewHandlers() below. */ * come from NewHandlers() below. */
static EncoderPtr Create(Environment* env, const Handlers* handlers, static EncoderPtr Create(Environment* env, const Handlers* handlers,
BytesSink* output) { BytesSink output) {
return EncoderPtr(upb_pb_encoder_create(env, handlers, output->ptr())); return EncoderPtr(upb_pb_encoder_create(env, handlers, output.sink()));
} }
/* The input to the encoder. */ /* The input to the encoder. */
upb_sink* input() { return upb_pb_encoder_input(ptr()); } upb::Sink input() { return upb_pb_encoder_input(ptr()); }
/* Creates a new set of handlers for this MessageDef. */ /* Creates a new set of handlers for this MessageDef. */
static HandlerCache NewCache() { static HandlerCache NewCache() {

@ -1,17 +1,17 @@
#include "upb/sink.h" #include "upb/sink.h"
bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink *sink) { bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink sink) {
void *subc; void *subc;
bool ret; bool ret;
upb_bufhandle handle = UPB_BUFHANDLE_INIT; upb_bufhandle handle = UPB_BUFHANDLE_INIT;
handle.buf = buf; handle.buf = buf;
ret = upb_bytessink_start(sink, len, &subc); ret = upb_bytessink_start(&sink, len, &subc);
if (ret && len != 0) { if (ret && len != 0) {
ret = (upb_bytessink_putbuf(sink, subc, buf, len, &handle) >= len); ret = (upb_bytessink_putbuf(&sink, subc, buf, len, &handle) >= len);
} }
if (ret) { if (ret) {
ret = upb_bytessink_end(sink); ret = upb_bytessink_end(&sink);
} }
return ret; return ret;
} }

@ -237,6 +237,17 @@ class upb::Sink {
/* Constructor with no initialization; must be Reset() before use. */ /* Constructor with no initialization; must be Reset() before use. */
Sink() {} Sink() {}
Sink(const Sink&) = default;
Sink& operator=(const Sink&) = default;
Sink(const upb_sink& sink) : sink_(sink) {}
Sink &operator=(const upb_sink &sink) {
sink_ = sink;
return *this;
}
upb_sink sink() { return sink_; }
/* Constructs a new sink for the given frozen handlers and closure. /* Constructs a new sink for the given frozen handlers and closure.
* *
* TODO: once the Handlers know the expected closure type, verify that T * TODO: once the Handlers know the expected closure type, verify that T
@ -406,7 +417,16 @@ class upb::BytesSink {
public: public:
BytesSink() {} BytesSink() {}
upb_bytessink* ptr() { return &sink_; } BytesSink(const BytesSink&) = default;
BytesSink& operator=(const BytesSink&) = default;
BytesSink(const upb_bytessink& sink) : sink_(sink) {}
BytesSink &operator=(const upb_bytessink &sink) {
sink_ = sink;
return *this;
}
upb_bytessink sink() { return sink_; }
/* Constructs a new sink for the given frozen handlers and closure. /* Constructs a new sink for the given frozen handlers and closure.
* *
@ -444,15 +464,15 @@ class upb::BytesSink {
UPB_BEGIN_EXTERN_C UPB_BEGIN_EXTERN_C
bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink *sink); bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink sink);
UPB_END_EXTERN_C UPB_END_EXTERN_C
#ifdef __cplusplus #ifdef __cplusplus
namespace upb { namespace upb {
template <class T> bool PutBuffer(const T& str, upb_bytessink* sink) { template <class T> bool PutBuffer(const T& str, BytesSink sink) {
return upb_bufsrc_putbuf(str.c_str(), str.size(), sink); return upb_bufsrc_putbuf(str.c_str(), str.size(), sink.sink());
} }
} }

Loading…
Cancel
Save