diff options
author | Josh Haberman <haberman@google.com> | 2013-05-11 16:45:38 -0700 |
---|---|---|
committer | Josh Haberman <haberman@google.com> | 2013-05-11 16:45:38 -0700 |
commit | cfdb9907cb87d15eaab72ceefbfa42fd7a4c3127 (patch) | |
tree | 63f5d70ad64daeeb4ffc777c2c3afd50e2e281b1 /upb/pb/glue.c | |
parent | 7d3e2bd2c4cfd1296d1d6f996d7548de26540d41 (diff) |
Synced with 3 months of Google-internal development.
Major changes:
- Got rid of all bytestream interfaces in favor of
using regular handlers.
- new Pipeline object represents a upb pipeline, does
bump allocation internally to manage memory.
- proto2 support now can handle extensions.
Diffstat (limited to 'upb/pb/glue.c')
-rw-r--r-- | upb/pb/glue.c | 46 |
1 files changed, 25 insertions, 21 deletions
diff --git a/upb/pb/glue.c b/upb/pb/glue.c index 4e69c0c..bcde039 100644 --- a/upb/pb/glue.c +++ b/upb/pb/glue.c @@ -16,33 +16,37 @@ upb_def **upb_load_defs_from_descriptor(const char *str, size_t len, int *n, void *owner, upb_status *status) { - upb_stringsrc strsrc; - upb_stringsrc_init(&strsrc); - upb_stringsrc_reset(&strsrc, str, len); + // Create handlers. + const upb_handlers *reader_h = upb_descreader_gethandlers(&reader_h); + const upb_handlers *decoder_h = + upb_pbdecoder_gethandlers(reader_h, false, &decoder_h); - const upb_handlers *h = upb_descreader_newhandlers(&h); - upb_decoderplan *p = upb_decoderplan_new(h, false); - upb_decoder d; - upb_decoder_init(&d); - upb_handlers_unref(h, &h); - upb_descreader r; - upb_descreader_init(&r); - upb_decoder_resetplan(&d, p); - upb_decoder_resetinput(&d, upb_stringsrc_allbytes(&strsrc), &r); + // Create pipeline. + upb_pipeline pipeline; + upb_pipeline_init(&pipeline, NULL, 0, upb_realloc, NULL); + upb_pipeline_donateref(&pipeline, reader_h, &reader_h); + upb_pipeline_donateref(&pipeline, decoder_h, &decoder_h); - upb_success_t ret = upb_decoder_decode(&d); - if (status) upb_status_copy(status, upb_decoder_status(&d)); - upb_stringsrc_uninit(&strsrc); - upb_decoder_uninit(&d); - upb_decoderplan_unref(p); - if (ret != UPB_OK) { - upb_descreader_uninit(&r); + // Create sinks. + upb_sink *reader_sink = upb_pipeline_newsink(&pipeline, reader_h); + upb_sink *decoder_sink = upb_pipeline_newsink(&pipeline, decoder_h); + upb_pbdecoder *d = upb_sinkframe_userdata(upb_sink_base(decoder_sink)); + upb_pbdecoder_resetsink(d, reader_sink); + + // Push input data. + bool ok = upb_bytestream_putstr(decoder_sink, str, len); + + if (status) upb_status_copy(status, upb_pipeline_status(&pipeline)); + if (!ok) { + upb_pipeline_uninit(&pipeline); return NULL; } - upb_def **defs = upb_descreader_getdefs(&r, owner, n); + + upb_descreader *r = upb_sinkframe_userdata(upb_sink_base(reader_sink)); + upb_def **defs = upb_descreader_getdefs(r, owner, n); upb_def **defscopy = malloc(sizeof(upb_def*) * (*n)); memcpy(defscopy, defs, sizeof(upb_def*) * (*n)); - upb_descreader_uninit(&r); + upb_pipeline_uninit(&pipeline); return defscopy; } |