google-protobuf 3.0.0.alpha.3 → 3.0.0.alpha.3.1.pre

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of google-protobuf might be problematic. Click here for more details.

@@ -99,15 +99,6 @@
99
99
  #define UPB_INLINE static inline
100
100
  #endif
101
101
 
102
- // For use in C/C++ source files (not headers), forces inlining within the file.
103
- #ifdef __GNUC__
104
- #define UPB_FORCEINLINE inline __attribute__((always_inline))
105
- #define UPB_NOINLINE __attribute__((noinline))
106
- #else
107
- #define UPB_FORCEINLINE
108
- #define UPB_NOINLINE
109
- #endif
110
-
111
102
  #if __STDC_VERSION__ >= 199901L
112
103
  #define UPB_C99
113
104
  #endif
@@ -4814,8 +4805,10 @@ UPB_DEFINE_STRUCT0(upb_byteshandler,
4814
4805
  ));
4815
4806
 
4816
4807
  void upb_byteshandler_init(upb_byteshandler *h);
4808
+ void upb_byteshandler_uninit(upb_byteshandler *h);
4817
4809
 
4818
4810
  // Caller must ensure that "d" outlives the handlers.
4811
+ // TODO(haberman): support handlerfree function for the data.
4819
4812
  // TODO(haberman): should this have a "freeze" operation? It's not necessary
4820
4813
  // for memory management, but could be useful to force immutability and provide
4821
4814
  // a convenient moment to verify that all registration succeeded.
@@ -4990,17 +4983,12 @@ template <class T> struct disable_if_same<T, T> {};
4990
4983
  template <class T> void DeletePointer(void *p) { delete static_cast<T>(p); }
4991
4984
 
4992
4985
  template <class T1, class T2>
4993
- struct FirstUnlessVoidOrBool {
4986
+ struct FirstUnlessVoid {
4994
4987
  typedef T1 value;
4995
4988
  };
4996
4989
 
4997
4990
  template <class T2>
4998
- struct FirstUnlessVoidOrBool<void, T2> {
4999
- typedef T2 value;
5000
- };
5001
-
5002
- template <class T2>
5003
- struct FirstUnlessVoidOrBool<bool, T2> {
4991
+ struct FirstUnlessVoid<void, T2> {
5004
4992
  typedef T2 value;
5005
4993
  };
5006
4994
 
@@ -5382,14 +5370,10 @@ inline MethodSig4<R, C, P1, P2, P3, P4> MatchFunc(R (C::*f)(P1, P2, P3, P4)) {
5382
5370
  //
5383
5371
  // 1. If the function returns void, make it return the expected type and with
5384
5372
  // a value that always indicates success.
5385
- // 2. If the function returns bool, make it return the expected type with a
5386
- // value that indicates success or failure.
5387
- //
5388
- // The "expected type" for return is:
5389
- // 1. void* for start handlers. If the closure parameter has a different type
5390
- // we will cast it to void* for the return in the success case.
5391
- // 2. size_t for string buffer handlers.
5392
- // 3. bool for everything else.
5373
+ // 2. If the function is expected to return void* but doesn't, wrap it so it
5374
+ // does (either by returning the closure param if the wrapped function
5375
+ // returns void or by casting a different pointer type to void* for
5376
+ // return).
5393
5377
 
5394
5378
  // Template parameters are FuncN type and desired return type.
5395
5379
  template <class F, class R, class Enable = void>
@@ -5778,13 +5762,10 @@ inline Handler<T>::Handler(F func)
5778
5762
  attr_.SetClosureType(UniquePtrForType<typename F::FuncInfo::Closure>());
5779
5763
 
5780
5764
  // We use the closure type (from the first parameter) if the return type is
5781
- // void or bool, since these are the two cases we wrap to return the closure's
5782
- // type anyway.
5783
- //
5784
- // This is all nonsense for non START* handlers, but it doesn't matter because
5785
- // in that case the value will be ignored.
5786
- typedef typename FirstUnlessVoidOrBool<typename F::FuncInfo::Return,
5787
- typename F::FuncInfo::Closure>::value
5765
+ // void. This is all nonsense for non START* handlers, but it doesn't matter
5766
+ // because in that case the value will be ignored.
5767
+ typedef typename FirstUnlessVoid<typename F::FuncInfo::Return,
5768
+ typename F::FuncInfo::Closure>::value
5788
5769
  EffectiveReturn;
5789
5770
  attr_.SetReturnClosureType(UniquePtrForType<EffectiveReturn>());
5790
5771
  }
@@ -5979,7 +5960,9 @@ inline BytesHandler::BytesHandler() {
5979
5960
  upb_byteshandler_init(this);
5980
5961
  }
5981
5962
 
5982
- inline BytesHandler::~BytesHandler() {}
5963
+ inline BytesHandler::~BytesHandler() {
5964
+ upb_byteshandler_uninit(this);
5965
+ }
5983
5966
 
5984
5967
  } // namespace upb
5985
5968
 
@@ -6000,261 +5983,6 @@ inline BytesHandler::~BytesHandler() {}
6000
5983
  #endif // UPB_HANDLERS_INL_H_
6001
5984
 
6002
5985
  #endif // UPB_HANDLERS_H
6003
- /*
6004
- * upb - a minimalist implementation of protocol buffers.
6005
- *
6006
- * Copyright (c) 2014 Google Inc. See LICENSE for details.
6007
- * Author: Josh Haberman <jhaberman@gmail.com>
6008
- *
6009
- * A upb::Environment provides a means for injecting malloc and an
6010
- * error-reporting callback into encoders/decoders. This allows them to be
6011
- * independent of nearly all assumptions about their actual environment.
6012
- *
6013
- * It is also a container for allocating the encoders/decoders themselves that
6014
- * insulates clients from knowing their actual size. This provides ABI
6015
- * compatibility even if the size of the objects change. And this allows the
6016
- * structure definitions to be in the .c files instead of the .h files, making
6017
- * the .h files smaller and more readable.
6018
- */
6019
-
6020
-
6021
- #ifndef UPB_ENV_H_
6022
- #define UPB_ENV_H_
6023
-
6024
- #ifdef __cplusplus
6025
- namespace upb {
6026
- class Environment;
6027
- class SeededAllocator;
6028
- }
6029
- #endif
6030
-
6031
- UPB_DECLARE_TYPE(upb::Environment, upb_env);
6032
- UPB_DECLARE_TYPE(upb::SeededAllocator, upb_seededalloc);
6033
-
6034
- typedef void *upb_alloc_func(void *ud, void *ptr, size_t oldsize, size_t size);
6035
- typedef void upb_cleanup_func(void *ud);
6036
- typedef bool upb_error_func(void *ud, const upb_status *status);
6037
-
6038
- // An environment is *not* thread-safe.
6039
- UPB_DEFINE_CLASS0(upb::Environment,
6040
- public:
6041
- Environment();
6042
- ~Environment();
6043
-
6044
- // Set a custom memory allocation function for the environment. May ONLY
6045
- // be called before any calls to Malloc()/Realloc()/AddCleanup() below.
6046
- // If this is not called, the system realloc() function will be used.
6047
- // The given user pointer "ud" will be passed to the allocation function.
6048
- //
6049
- // The allocation function will not receive corresponding "free" calls. it
6050
- // must ensure that the memory is valid for the lifetime of the Environment,
6051
- // but it may be reclaimed any time thereafter. The likely usage is that
6052
- // "ud" points to a stateful allocator, and that the allocator frees all
6053
- // memory, arena-style, when it is destroyed. In this case the allocator must
6054
- // outlive the Environment. Another possibility is that the allocation
6055
- // function returns GC-able memory that is guaranteed to be GC-rooted for the
6056
- // life of the Environment.
6057
- void SetAllocationFunction(upb_alloc_func* alloc, void* ud);
6058
-
6059
- template<class T>
6060
- void SetAllocator(T* allocator) {
6061
- SetAllocationFunction(allocator->GetAllocationFunction(), allocator);
6062
- }
6063
-
6064
- // Set a custom error reporting function.
6065
- void SetErrorFunction(upb_error_func* func, void* ud);
6066
-
6067
- // Set the error reporting function to simply copy the status to the given
6068
- // status and abort.
6069
- void ReportErrorsTo(Status* status);
6070
-
6071
- // Returns true if all allocations and AddCleanup() calls have succeeded,
6072
- // and no errors were reported with ReportError() (except ones that recovered
6073
- // successfully).
6074
- bool ok() const;
6075
-
6076
- //////////////////////////////////////////////////////////////////////////////
6077
- // Functions for use by encoders/decoders.
6078
-
6079
- // Reports an error to this environment's callback, returning true if
6080
- // the caller should try to recover.
6081
- bool ReportError(const Status* status);
6082
-
6083
- // Allocate memory. Uses the environment's allocation function.
6084
- //
6085
- // There is no need to free(). All memory will be freed automatically, but is
6086
- // guaranteed to outlive the Environment.
6087
- void* Malloc(size_t size);
6088
-
6089
- // Reallocate memory. Preserves "oldsize" bytes from the existing buffer
6090
- // Requires: oldsize <= existing_size.
6091
- //
6092
- // TODO(haberman): should we also enforce that oldsize <= size?
6093
- void* Realloc(void* ptr, size_t oldsize, size_t size);
6094
-
6095
- // Add a cleanup function to run when the environment is destroyed.
6096
- // Returns false on out-of-memory.
6097
- //
6098
- // The first call to AddCleanup() after SetAllocationFunction() is guaranteed
6099
- // to return true -- this makes it possible to robustly set a cleanup handler
6100
- // for a custom allocation function.
6101
- bool AddCleanup(upb_cleanup_func* func, void* ud);
6102
-
6103
- // Total number of bytes that have been allocated. It is undefined what
6104
- // Realloc() does to this counter.
6105
- size_t BytesAllocated() const;
6106
-
6107
- private:
6108
- UPB_DISALLOW_COPY_AND_ASSIGN(Environment);
6109
- ,
6110
- UPB_DEFINE_STRUCT0(upb_env,
6111
- bool ok_;
6112
- size_t bytes_allocated;
6113
-
6114
- // Alloc function.
6115
- upb_alloc_func *alloc;
6116
- void *alloc_ud;
6117
-
6118
- // Error-reporting function.
6119
- upb_error_func *err;
6120
- void *err_ud;
6121
-
6122
- // Userdata for default alloc func.
6123
- void *default_alloc_ud;
6124
-
6125
- // Cleanup entries. Pointer to a cleanup_ent, defined in env.c
6126
- void *cleanup_head;
6127
-
6128
- // For future expansion, since the size of this struct is exposed to users.
6129
- void *future1;
6130
- void *future2;
6131
- ));
6132
-
6133
- UPB_BEGIN_EXTERN_C
6134
-
6135
- void upb_env_init(upb_env *e);
6136
- void upb_env_uninit(upb_env *e);
6137
- void upb_env_setallocfunc(upb_env *e, upb_alloc_func *func, void *ud);
6138
- void upb_env_seterrorfunc(upb_env *e, upb_error_func *func, void *ud);
6139
- void upb_env_reporterrorsto(upb_env *e, upb_status *status);
6140
- bool upb_env_ok(const upb_env *e);
6141
- bool upb_env_reporterror(upb_env *e, const upb_status *status);
6142
- void *upb_env_malloc(upb_env *e, size_t size);
6143
- void *upb_env_realloc(upb_env *e, void *ptr, size_t oldsize, size_t size);
6144
- bool upb_env_addcleanup(upb_env *e, upb_cleanup_func *func, void *ud);
6145
- size_t upb_env_bytesallocated(const upb_env *e);
6146
-
6147
- UPB_END_EXTERN_C
6148
-
6149
- // An allocator that allocates from an initial memory region (likely the stack)
6150
- // before falling back to another allocator.
6151
- UPB_DEFINE_CLASS0(upb::SeededAllocator,
6152
- public:
6153
- SeededAllocator(void *mem, size_t len);
6154
- ~SeededAllocator();
6155
-
6156
- // Set a custom fallback memory allocation function for the allocator, to use
6157
- // once the initial region runs out.
6158
- //
6159
- // May ONLY be called before GetAllocationFunction(). If this is not
6160
- // called, the system realloc() will be the fallback allocator.
6161
- void SetFallbackAllocator(upb_alloc_func *alloc, void *ud);
6162
-
6163
- // Gets the allocation function for this allocator.
6164
- upb_alloc_func* GetAllocationFunction();
6165
-
6166
- private:
6167
- UPB_DISALLOW_COPY_AND_ASSIGN(SeededAllocator);
6168
- ,
6169
- UPB_DEFINE_STRUCT0(upb_seededalloc,
6170
- // Fallback alloc function.
6171
- upb_alloc_func *alloc;
6172
- upb_cleanup_func *alloc_cleanup;
6173
- void *alloc_ud;
6174
- bool need_cleanup;
6175
- bool returned_allocfunc;
6176
-
6177
- // Userdata for default alloc func.
6178
- void *default_alloc_ud;
6179
-
6180
- // Pointers for the initial memory region.
6181
- char *mem_base;
6182
- char *mem_ptr;
6183
- char *mem_limit;
6184
-
6185
- // For future expansion, since the size of this struct is exposed to users.
6186
- void *future1;
6187
- void *future2;
6188
- ));
6189
-
6190
- UPB_BEGIN_EXTERN_C
6191
-
6192
- void upb_seededalloc_init(upb_seededalloc *a, void *mem, size_t len);
6193
- void upb_seededalloc_uninit(upb_seededalloc *a);
6194
- void upb_seededalloc_setfallbackalloc(upb_seededalloc *a, upb_alloc_func *func,
6195
- void *ud);
6196
- upb_alloc_func *upb_seededalloc_getallocfunc(upb_seededalloc *a);
6197
-
6198
- UPB_END_EXTERN_C
6199
-
6200
- #ifdef __cplusplus
6201
-
6202
- namespace upb {
6203
-
6204
- inline Environment::Environment() {
6205
- upb_env_init(this);
6206
- }
6207
- inline Environment::~Environment() {
6208
- upb_env_uninit(this);
6209
- }
6210
- inline void Environment::SetAllocationFunction(upb_alloc_func *alloc,
6211
- void *ud) {
6212
- upb_env_setallocfunc(this, alloc, ud);
6213
- }
6214
- inline void Environment::SetErrorFunction(upb_error_func *func, void *ud) {
6215
- upb_env_seterrorfunc(this, func, ud);
6216
- }
6217
- inline void Environment::ReportErrorsTo(Status* status) {
6218
- upb_env_reporterrorsto(this, status);
6219
- }
6220
- inline bool Environment::ok() const {
6221
- return upb_env_ok(this);
6222
- }
6223
- inline bool Environment::ReportError(const Status* status) {
6224
- return upb_env_reporterror(this, status);
6225
- }
6226
- inline void *Environment::Malloc(size_t size) {
6227
- return upb_env_malloc(this, size);
6228
- }
6229
- inline void *Environment::Realloc(void *ptr, size_t oldsize, size_t size) {
6230
- return upb_env_realloc(this, ptr, oldsize, size);
6231
- }
6232
- inline bool Environment::AddCleanup(upb_cleanup_func *func, void *ud) {
6233
- return upb_env_addcleanup(this, func, ud);
6234
- }
6235
- inline size_t Environment::BytesAllocated() const {
6236
- return upb_env_bytesallocated(this);
6237
- }
6238
-
6239
- inline SeededAllocator::SeededAllocator(void *mem, size_t len) {
6240
- upb_seededalloc_init(this, mem, len);
6241
- }
6242
- inline SeededAllocator::~SeededAllocator() {
6243
- upb_seededalloc_uninit(this);
6244
- }
6245
- inline void SeededAllocator::SetFallbackAllocator(upb_alloc_func *alloc,
6246
- void *ud) {
6247
- upb_seededalloc_setfallbackalloc(this, alloc, ud);
6248
- }
6249
- inline upb_alloc_func *SeededAllocator::GetAllocationFunction() {
6250
- return upb_seededalloc_getallocfunc(this);
6251
- }
6252
-
6253
- } // namespace upb
6254
-
6255
- #endif // __cplusplus
6256
-
6257
- #endif // UPB_ENV_H_
6258
5986
  /*
6259
5987
  * upb - a minimalist implementation of protocol buffers.
6260
5988
  *
@@ -6290,6 +6018,27 @@ UPB_DECLARE_TYPE(upb::BufferSource, upb_bufsrc);
6290
6018
  UPB_DECLARE_TYPE(upb::BytesSink, upb_bytessink);
6291
6019
  UPB_DECLARE_TYPE(upb::Sink, upb_sink);
6292
6020
 
6021
+ // Internal-only struct for the sink.
6022
+ struct upb_sinkframe {
6023
+ UPB_PRIVATE_FOR_CPP
6024
+ const upb_handlers *h;
6025
+ void *closure;
6026
+
6027
+ // For any frames besides the top, this is the END* callback that will run
6028
+ // when the subframe is popped (for example, for a "sequence" frame the frame
6029
+ // above it will be a UPB_HANDLER_ENDSEQ handler). But this is only
6030
+ // necessary for assertion checking inside upb_sink and can be omitted if the
6031
+ // sink has only one caller.
6032
+ //
6033
+ // TODO(haberman): have a mechanism for ensuring that a sink only has one
6034
+ // caller.
6035
+ upb_selector_t selector;
6036
+ };
6037
+
6038
+ // The maximum nesting depth that upb::Sink will allow. Matches proto2's limit.
6039
+ // TODO: make this a runtime-settable property of Sink.
6040
+ #define UPB_SINK_MAX_NESTING 64
6041
+
6293
6042
  // A upb::Sink is an object that binds a upb::Handlers object to some runtime
6294
6043
  // state. It represents an endpoint to which data can be sent.
6295
6044
  //
@@ -6849,11 +6598,45 @@ class Reader;
6849
6598
 
6850
6599
  UPB_DECLARE_TYPE(upb::descriptor::Reader, upb_descreader);
6851
6600
 
6852
- #ifdef __cplusplus
6601
+ // Internal-only structs used by Reader.
6602
+
6603
+ // upb_deflist is an internal-only dynamic array for storing a growing list of
6604
+ // upb_defs.
6605
+ typedef struct {
6606
+ UPB_PRIVATE_FOR_CPP
6607
+ upb_def **defs;
6608
+ size_t len;
6609
+ size_t size;
6610
+ bool owned;
6611
+ } upb_deflist;
6612
+
6613
+ // We keep a stack of all the messages scopes we are currently in, as well as
6614
+ // the top-level file scope. This is necessary to correctly qualify the
6615
+ // definitions that are contained inside. "name" tracks the name of the
6616
+ // message or package (a bare name -- not qualified by any enclosing scopes).
6617
+ typedef struct {
6618
+ UPB_PRIVATE_FOR_CPP
6619
+ char *name;
6620
+ // Index of the first def that is under this scope. For msgdefs, the
6621
+ // msgdef itself is at start-1.
6622
+ int start;
6623
+ } upb_descreader_frame;
6624
+
6625
+ // The maximum number of nested declarations that are allowed, ie.
6626
+ // message Foo {
6627
+ // message Bar {
6628
+ // message Baz {
6629
+ // }
6630
+ // }
6631
+ // }
6632
+ //
6633
+ // This is a resource limit that affects how big our runtime stack can grow.
6634
+ // TODO: make this a runtime-settable property of the Reader instance.
6635
+ #define UPB_MAX_MESSAGE_NESTING 64
6853
6636
 
6854
6637
  // Class that receives descriptor data according to the descriptor.proto schema
6855
6638
  // and use it to build upb::Defs corresponding to that schema.
6856
- class upb::descriptor::Reader {
6639
+ UPB_DEFINE_CLASS0(upb::descriptor::Reader,
6857
6640
  public:
6858
6641
  // These handlers must have come from NewHandlers() and must outlive the
6859
6642
  // Reader.
@@ -6863,7 +6646,11 @@ class upb::descriptor::Reader {
6863
6646
  // to build/memory-manage the handlers at runtime at all). Unfortunately this
6864
6647
  // is a bit tricky to implement for Handlers, but necessary to simplify this
6865
6648
  // interface.
6866
- static Reader* Create(Environment* env, const Handlers* handlers);
6649
+ Reader(const Handlers* handlers, Status* status);
6650
+ ~Reader();
6651
+
6652
+ // Resets the reader's state and discards any defs it may have built.
6653
+ void Reset();
6867
6654
 
6868
6655
  // The reader's input; this is where descriptor.proto data should be sent.
6869
6656
  Sink* input();
@@ -6879,30 +6666,45 @@ class upb::descriptor::Reader {
6879
6666
 
6880
6667
  // Builds and returns handlers for the reader, owned by "owner."
6881
6668
  static Handlers* NewHandlers(const void* owner);
6669
+ ,
6670
+ UPB_DEFINE_STRUCT0(upb_descreader,
6671
+ upb_sink sink;
6672
+ upb_deflist defs;
6673
+ upb_descreader_frame stack[UPB_MAX_MESSAGE_NESTING];
6674
+ int stack_len;
6882
6675
 
6883
- private:
6884
- UPB_DISALLOW_POD_OPS(Reader, upb::descriptor::Reader);
6885
- };
6676
+ uint32_t number;
6677
+ char *name;
6678
+ bool saw_number;
6679
+ bool saw_name;
6886
6680
 
6887
- #endif
6681
+ char *default_string;
6888
6682
 
6889
- UPB_BEGIN_EXTERN_C
6683
+ upb_fielddef *f;
6684
+ ));
6685
+
6686
+ UPB_BEGIN_EXTERN_C // {
6890
6687
 
6891
6688
  // C API.
6892
- upb_descreader *upb_descreader_create(upb_env *e, const upb_handlers *h);
6689
+ void upb_descreader_init(upb_descreader *r, const upb_handlers *handlers,
6690
+ upb_status *status);
6691
+ void upb_descreader_uninit(upb_descreader *r);
6692
+ void upb_descreader_reset(upb_descreader *r);
6893
6693
  upb_sink *upb_descreader_input(upb_descreader *r);
6894
6694
  upb_def **upb_descreader_getdefs(upb_descreader *r, void *owner, int *n);
6895
6695
  const upb_handlers *upb_descreader_newhandlers(const void *owner);
6896
6696
 
6897
- UPB_END_EXTERN_C
6697
+ UPB_END_EXTERN_C // }
6898
6698
 
6899
6699
  #ifdef __cplusplus
6900
6700
  // C++ implementation details. /////////////////////////////////////////////////
6901
6701
  namespace upb {
6902
6702
  namespace descriptor {
6903
- inline Reader* Reader::Create(Environment* e, const Handlers *h) {
6904
- return upb_descreader_create(e, h);
6703
+ inline Reader::Reader(const Handlers *h, Status *s) {
6704
+ upb_descreader_init(this, h, s);
6905
6705
  }
6706
+ inline Reader::~Reader() { upb_descreader_uninit(this); }
6707
+ inline void Reader::Reset() { upb_descreader_reset(this); }
6906
6708
  inline Sink* Reader::input() { return upb_descreader_input(this); }
6907
6709
  inline upb::Def** Reader::GetDefs(void* owner, int* n) {
6908
6710
  return upb_descreader_getdefs(this, owner, n);
@@ -6962,6 +6764,44 @@ UPB_DECLARE_TYPE(upb::pb::Decoder, upb_pbdecoder);
6962
6764
  UPB_DECLARE_TYPE(upb::pb::DecoderMethod, upb_pbdecodermethod);
6963
6765
  UPB_DECLARE_TYPE(upb::pb::DecoderMethodOptions, upb_pbdecodermethodopts);
6964
6766
 
6767
+ // The maximum that any submessages can be nested. Matches proto2's limit.
6768
+ // This specifies the size of the decoder's statically-sized array and therefore
6769
+ // setting it high will cause the upb::pb::Decoder object to be larger.
6770
+ //
6771
+ // If necessary we can add a runtime-settable property to Decoder that allow
6772
+ // this to be larger than the compile-time setting, but this would add
6773
+ // complexity, particularly since we would have to decide how/if to give users
6774
+ // the ability to set a custom memory allocation function.
6775
+ #define UPB_DECODER_MAX_NESTING 64
6776
+
6777
+ // Internal-only struct used by the decoder.
6778
+ typedef struct {
6779
+ UPB_PRIVATE_FOR_CPP
6780
+ // Space optimization note: we store two pointers here that the JIT
6781
+ // doesn't need at all; the upb_handlers* inside the sink and
6782
+ // the dispatch table pointer. We can optimze so that the JIT uses
6783
+ // smaller stack frames than the interpreter. The only thing we need
6784
+ // to guarantee is that the fallback routines can find end_ofs.
6785
+ upb_sink sink;
6786
+
6787
+ // The absolute stream offset of the end-of-frame delimiter.
6788
+ // Non-delimited frames (groups and non-packed repeated fields) reuse the
6789
+ // delimiter of their parent, even though the frame may not end there.
6790
+ //
6791
+ // NOTE: the JIT stores a slightly different value here for non-top frames.
6792
+ // It stores the value relative to the end of the enclosed message. But the
6793
+ // top frame is still stored the same way, which is important for ensuring
6794
+ // that calls from the JIT into C work correctly.
6795
+ uint64_t end_ofs;
6796
+ const uint32_t *base;
6797
+
6798
+ // 0 indicates a length-delimited field.
6799
+ // A positive number indicates a known group.
6800
+ // A negative number indicates an unknown group.
6801
+ int32_t groupnum;
6802
+ upb_inttable *dispatch; // Not used by the JIT.
6803
+ } upb_pbdecoder_frame;
6804
+
6965
6805
  // The parameters one uses to construct a DecoderMethod.
6966
6806
  // TODO(haberman): move allowjit here? Seems more convenient for users.
6967
6807
  UPB_DEFINE_CLASS0(upb::pb::DecoderMethodOptions,
@@ -7039,31 +6879,22 @@ UPB_DEFINE_STRUCT(upb_pbdecodermethod, upb_refcounted,
7039
6879
  upb_inttable dispatch;
7040
6880
  ));
7041
6881
 
7042
- // Preallocation hint: decoder won't allocate more bytes than this when first
7043
- // constructed. This hint may be an overestimate for some build configurations.
7044
- // But if the decoder library is upgraded without recompiling the application,
7045
- // it may be an underestimate.
7046
- #define UPB_PB_DECODER_SIZE 4400
7047
-
7048
- #ifdef __cplusplus
7049
-
7050
6882
  // A Decoder receives binary protobuf data on its input sink and pushes the
7051
6883
  // decoded data to its output sink.
7052
- class upb::pb::Decoder {
6884
+ UPB_DEFINE_CLASS0(upb::pb::Decoder,
7053
6885
  public:
7054
6886
  // Constructs a decoder instance for the given method, which must outlive this
7055
6887
  // decoder. Any errors during parsing will be set on the given status, which
7056
6888
  // must also outlive this decoder.
7057
- //
7058
- // The sink must match the given method.
7059
- static Decoder* Create(Environment* env, const DecoderMethod* method,
7060
- Sink* output);
6889
+ Decoder(const DecoderMethod* method, Status* status);
6890
+ ~Decoder();
7061
6891
 
7062
6892
  // Returns the DecoderMethod this decoder is parsing from.
6893
+ // TODO(haberman): Do users need to be able to rebind this?
7063
6894
  const DecoderMethod* method() const;
7064
6895
 
7065
- // The sink on which this decoder receives input.
7066
- BytesSink* input();
6896
+ // Resets the state of the decoder.
6897
+ void Reset();
7067
6898
 
7068
6899
  // Returns number of bytes successfully parsed.
7069
6900
  //
@@ -7074,25 +6905,76 @@ class upb::pb::Decoder {
7074
6905
  // callback.
7075
6906
  uint64_t BytesParsed() const;
7076
6907
 
7077
- // Gets/sets the parsing nexting limit. If the total number of nested
7078
- // submessages and repeated fields hits this limit, parsing will fail. This
7079
- // is a resource limit that controls the amount of memory used by the parsing
7080
- // stack.
6908
+ // Resets the output sink of the Decoder.
6909
+ // The given sink must match method()->dest_handlers().
7081
6910
  //
7082
- // Setting the limit will fail if the parser is currently suspended at a depth
7083
- // greater than this, or if memory allocation of the stack fails.
7084
- size_t max_nesting() const;
7085
- bool set_max_nesting(size_t max);
7086
-
7087
- void Reset();
6911
+ // This must be called at least once before the decoder can be used. It may
6912
+ // only be called with the decoder is in a state where it was just created or
6913
+ // reset with pipeline.Reset(). The given sink must be from the same pipeline
6914
+ // as this decoder.
6915
+ bool ResetOutput(Sink* sink);
7088
6916
 
7089
- static const size_t kSize = UPB_PB_DECODER_SIZE;
6917
+ // The sink on which this decoder receives input.
6918
+ BytesSink* input();
7090
6919
 
7091
6920
  private:
7092
- UPB_DISALLOW_POD_OPS(Decoder, upb::pb::Decoder);
7093
- };
6921
+ UPB_DISALLOW_COPY_AND_ASSIGN(Decoder);
6922
+ ,
6923
+ UPB_DEFINE_STRUCT0(upb_pbdecoder, UPB_QUOTE(
6924
+ // Our input sink.
6925
+ upb_bytessink input_;
7094
6926
 
7095
- #endif // __cplusplus
6927
+ // The decoder method we are parsing with (owned).
6928
+ const upb_pbdecodermethod *method_;
6929
+
6930
+ size_t call_len;
6931
+ const uint32_t *pc, *last;
6932
+
6933
+ // Current input buffer and its stream offset.
6934
+ const char *buf, *ptr, *end, *checkpoint;
6935
+
6936
+ // End of the delimited region, relative to ptr, or NULL if not in this buf.
6937
+ const char *delim_end;
6938
+
6939
+ // End of the delimited region, relative to ptr, or end if not in this buf.
6940
+ const char *data_end;
6941
+
6942
+ // Overall stream offset of "buf."
6943
+ uint64_t bufstart_ofs;
6944
+
6945
+ // Buffer for residual bytes not parsed from the previous buffer.
6946
+ // The maximum number of residual bytes we require is 12; a five-byte
6947
+ // unknown tag plus an eight-byte value, less one because the value
6948
+ // is only a partial value.
6949
+ char residual[12];
6950
+ char *residual_end;
6951
+
6952
+ // Stores the user buffer passed to our decode function.
6953
+ const char *buf_param;
6954
+ size_t size_param;
6955
+ const upb_bufhandle *handle;
6956
+
6957
+ #ifdef UPB_USE_JIT_X64
6958
+ // Used momentarily by the generated code to store a value while a user
6959
+ // function is called.
6960
+ uint32_t tmp_len;
6961
+
6962
+ const void *saved_rsp;
6963
+ #endif
6964
+
6965
+ upb_status *status;
6966
+
6967
+ // Our internal stack.
6968
+ upb_pbdecoder_frame *top, *limit;
6969
+ upb_pbdecoder_frame stack[UPB_DECODER_MAX_NESTING];
6970
+ #ifdef UPB_USE_JIT_X64
6971
+ // Each native stack frame needs two pointers, plus we need a few frames for
6972
+ // the enter/exit trampolines.
6973
+ const uint32_t *callstack[(UPB_DECODER_MAX_NESTING * 2) + 10];
6974
+ #else
6975
+ const uint32_t *callstack[UPB_DECODER_MAX_NESTING];
6976
+ #endif
6977
+ )));
7096
6978
 
7097
6979
  // A class for caching protobuf processing code, whether bytecode for the
7098
6980
  // interpreted decoder or machine code for the JIT.
@@ -7141,15 +7023,14 @@ UPB_DEFINE_STRUCT0(upb_pbcodecache,
7141
7023
 
7142
7024
  UPB_BEGIN_EXTERN_C // {
7143
7025
 
7144
- upb_pbdecoder *upb_pbdecoder_create(upb_env *e,
7145
- const upb_pbdecodermethod *method,
7146
- upb_sink *output);
7026
+ void upb_pbdecoder_init(upb_pbdecoder *d, const upb_pbdecodermethod *method,
7027
+ upb_status *status);
7028
+ void upb_pbdecoder_uninit(upb_pbdecoder *d);
7029
+ void upb_pbdecoder_reset(upb_pbdecoder *d);
7147
7030
  const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d);
7031
+ bool upb_pbdecoder_resetoutput(upb_pbdecoder *d, upb_sink *sink);
7148
7032
  upb_bytessink *upb_pbdecoder_input(upb_pbdecoder *d);
7149
7033
  uint64_t upb_pbdecoder_bytesparsed(const upb_pbdecoder *d);
7150
- size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d);
7151
- bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max);
7152
- void upb_pbdecoder_reset(upb_pbdecoder *d);
7153
7034
 
7154
7035
  void upb_pbdecodermethodopts_init(upb_pbdecodermethodopts *opts,
7155
7036
  const upb_handlers *h);
@@ -7184,27 +7065,27 @@ namespace upb {
7184
7065
 
7185
7066
  namespace pb {
7186
7067
 
7187
- // static
7188
- inline Decoder* Decoder::Create(Environment* env, const DecoderMethod* m,
7189
- Sink* sink) {
7190
- return upb_pbdecoder_create(env, m, sink);
7068
+ inline Decoder::Decoder(const DecoderMethod* m, Status* s) {
7069
+ upb_pbdecoder_init(this, m, s);
7070
+ }
7071
+ inline Decoder::~Decoder() {
7072
+ upb_pbdecoder_uninit(this);
7191
7073
  }
7192
7074
  inline const DecoderMethod* Decoder::method() const {
7193
7075
  return upb_pbdecoder_method(this);
7194
7076
  }
7195
- inline BytesSink* Decoder::input() {
7196
- return upb_pbdecoder_input(this);
7077
+ inline void Decoder::Reset() {
7078
+ upb_pbdecoder_reset(this);
7197
7079
  }
7198
7080
  inline uint64_t Decoder::BytesParsed() const {
7199
7081
  return upb_pbdecoder_bytesparsed(this);
7200
7082
  }
7201
- inline size_t Decoder::max_nesting() const {
7202
- return upb_pbdecoder_maxnesting(this);
7083
+ inline bool Decoder::ResetOutput(Sink* sink) {
7084
+ return upb_pbdecoder_resetoutput(this, sink);
7203
7085
  }
7204
- inline bool Decoder::set_max_nesting(size_t max) {
7205
- return upb_pbdecoder_setmaxnesting(this, max);
7086
+ inline BytesSink* Decoder::input() {
7087
+ return upb_pbdecoder_input(this);
7206
7088
  }
7207
- inline void Decoder::Reset() { upb_pbdecoder_reset(this); }
7208
7089
 
7209
7090
  inline DecoderMethodOptions::DecoderMethodOptions(const Handlers* h) {
7210
7091
  upb_pbdecodermethodopts_init(this, h);
@@ -7361,95 +7242,6 @@ typedef struct {
7361
7242
  #endif
7362
7243
  } mgroup;
7363
7244
 
7364
- // The maximum that any submessages can be nested. Matches proto2's limit.
7365
- // This specifies the size of the decoder's statically-sized array and therefore
7366
- // setting it high will cause the upb::pb::Decoder object to be larger.
7367
- //
7368
- // If necessary we can add a runtime-settable property to Decoder that allow
7369
- // this to be larger than the compile-time setting, but this would add
7370
- // complexity, particularly since we would have to decide how/if to give users
7371
- // the ability to set a custom memory allocation function.
7372
- #define UPB_DECODER_MAX_NESTING 64
7373
-
7374
- // Internal-only struct used by the decoder.
7375
- typedef struct {
7376
- // Space optimization note: we store two pointers here that the JIT
7377
- // doesn't need at all; the upb_handlers* inside the sink and
7378
- // the dispatch table pointer. We can optimze so that the JIT uses
7379
- // smaller stack frames than the interpreter. The only thing we need
7380
- // to guarantee is that the fallback routines can find end_ofs.
7381
- upb_sink sink;
7382
-
7383
- // The absolute stream offset of the end-of-frame delimiter.
7384
- // Non-delimited frames (groups and non-packed repeated fields) reuse the
7385
- // delimiter of their parent, even though the frame may not end there.
7386
- //
7387
- // NOTE: the JIT stores a slightly different value here for non-top frames.
7388
- // It stores the value relative to the end of the enclosed message. But the
7389
- // top frame is still stored the same way, which is important for ensuring
7390
- // that calls from the JIT into C work correctly.
7391
- uint64_t end_ofs;
7392
- const uint32_t *base;
7393
-
7394
- // 0 indicates a length-delimited field.
7395
- // A positive number indicates a known group.
7396
- // A negative number indicates an unknown group.
7397
- int32_t groupnum;
7398
- upb_inttable *dispatch; // Not used by the JIT.
7399
- } upb_pbdecoder_frame;
7400
-
7401
- struct upb_pbdecoder {
7402
- upb_env *env;
7403
-
7404
- // Our input sink.
7405
- upb_bytessink input_;
7406
-
7407
- // The decoder method we are parsing with (owned).
7408
- const upb_pbdecodermethod *method_;
7409
-
7410
- size_t call_len;
7411
- const uint32_t *pc, *last;
7412
-
7413
- // Current input buffer and its stream offset.
7414
- const char *buf, *ptr, *end, *checkpoint;
7415
-
7416
- // End of the delimited region, relative to ptr, or NULL if not in this buf.
7417
- const char *delim_end;
7418
-
7419
- // End of the delimited region, relative to ptr, or end if not in this buf.
7420
- const char *data_end;
7421
-
7422
- // Overall stream offset of "buf."
7423
- uint64_t bufstart_ofs;
7424
-
7425
- // Buffer for residual bytes not parsed from the previous buffer.
7426
- // The maximum number of residual bytes we require is 12; a five-byte
7427
- // unknown tag plus an eight-byte value, less one because the value
7428
- // is only a partial value.
7429
- char residual[12];
7430
- char *residual_end;
7431
-
7432
- // Stores the user buffer passed to our decode function.
7433
- const char *buf_param;
7434
- size_t size_param;
7435
- const upb_bufhandle *handle;
7436
-
7437
- // Our internal stack.
7438
- upb_pbdecoder_frame *stack, *top, *limit;
7439
- const uint32_t **callstack;
7440
- size_t stack_size;
7441
-
7442
- upb_status *status;
7443
-
7444
- #ifdef UPB_USE_JIT_X64
7445
- // Used momentarily by the generated code to store a value while a user
7446
- // function is called.
7447
- uint32_t tmp_len;
7448
-
7449
- const void *saved_rsp;
7450
- #endif
7451
- };
7452
-
7453
7245
  // Decoder entry points; used as handlers.
7454
7246
  void *upb_pbdecoder_startbc(void *closure, const void *pc, size_t size_hint);
7455
7247
  void *upb_pbdecoder_startjit(void *closure, const void *hd, size_t size_hint);
@@ -7717,42 +7509,101 @@ UPB_DECLARE_TYPE(upb::pb::Encoder, upb_pb_encoder);
7717
7509
 
7718
7510
  /* upb::pb::Encoder ***********************************************************/
7719
7511
 
7720
- // Preallocation hint: decoder won't allocate more bytes than this when first
7721
- // constructed. This hint may be an overestimate for some build configurations.
7722
- // But if the decoder library is upgraded without recompiling the application,
7723
- // it may be an underestimate.
7724
- #define UPB_PB_ENCODER_SIZE 768
7725
-
7726
- #ifdef __cplusplus
7512
+ // The output buffer is divided into segments; a segment is a string of data
7513
+ // that is "ready to go" -- it does not need any varint lengths inserted into
7514
+ // the middle. The seams between segments are where varints will be inserted
7515
+ // once they are known.
7516
+ //
7517
+ // We also use the concept of a "run", which is a range of encoded bytes that
7518
+ // occur at a single submessage level. Every segment contains one or more runs.
7519
+ //
7520
+ // A segment can span messages. Consider:
7521
+ //
7522
+ // .--Submessage lengths---------.
7523
+ // | | |
7524
+ // | V V
7525
+ // V | |--------------- | |-----------------
7526
+ // Submessages: | |-----------------------------------------------
7527
+ // Top-level msg: ------------------------------------------------------------
7528
+ //
7529
+ // Segments: ----- ------------------- -----------------
7530
+ // Runs: *---- *--------------*--- *----------------
7531
+ // (* marks the start)
7532
+ //
7533
+ // Note that the top-level menssage is not in any segment because it does not
7534
+ // have any length preceding it.
7535
+ //
7536
+ // A segment is only interrupted when another length needs to be inserted. So
7537
+ // observe how the second segment spans both the inner submessage and part of
7538
+ // the next enclosing message.
7539
+ typedef struct {
7540
+ UPB_PRIVATE_FOR_CPP
7541
+ uint32_t msglen; // The length to varint-encode before this segment.
7542
+ uint32_t seglen; // Length of the segment.
7543
+ } upb_pb_encoder_segment;
7727
7544
 
7728
- class upb::pb::Encoder {
7545
+ UPB_DEFINE_CLASS0(upb::pb::Encoder,
7729
7546
  public:
7730
- // Creates a new encoder in the given environment. The Handlers must have
7731
- // come from NewHandlers() below.
7732
- static Encoder* Create(Environment* env, const Handlers* handlers,
7733
- BytesSink* output);
7547
+ Encoder(const upb::Handlers* handlers);
7548
+ ~Encoder();
7549
+
7550
+ static reffed_ptr<const Handlers> NewHandlers(const upb::MessageDef* msg);
7551
+
7552
+ // Resets the state of the printer, so that it will expect to begin a new
7553
+ // document.
7554
+ void Reset();
7555
+
7556
+ // Resets the output pointer which will serve as our closure.
7557
+ void ResetOutput(BytesSink* output);
7734
7558
 
7735
7559
  // The input to the encoder.
7736
7560
  Sink* input();
7737
7561
 
7738
- // Creates a new set of handlers for this MessageDef.
7739
- static reffed_ptr<const Handlers> NewHandlers(const MessageDef* msg);
7562
+ private:
7563
+ UPB_DISALLOW_COPY_AND_ASSIGN(Encoder);
7564
+ ,
7565
+ UPB_DEFINE_STRUCT0(upb_pb_encoder, UPB_QUOTE(
7566
+ // Our input and output.
7567
+ upb_sink input_;
7568
+ upb_bytessink *output_;
7740
7569
 
7741
- static const size_t kSize = UPB_PB_ENCODER_SIZE;
7570
+ // The "subclosure" -- used as the inner closure as part of the bytessink
7571
+ // protocol.
7572
+ void *subc;
7742
7573
 
7743
- private:
7744
- UPB_DISALLOW_POD_OPS(Encoder, upb::pb::Encoder);
7745
- };
7574
+ // The output buffer and limit, and our current write position. "buf"
7575
+ // initially points to "initbuf", but is dynamically allocated if we need to
7576
+ // grow beyond the initial size.
7577
+ char *buf, *ptr, *limit;
7746
7578
 
7747
- #endif
7579
+ // The beginning of the current run, or undefined if we are at the top level.
7580
+ char *runbegin;
7581
+
7582
+ // The list of segments we are accumulating.
7583
+ upb_pb_encoder_segment *segbuf, *segptr, *seglimit;
7584
+
7585
+ // The stack of enclosing submessages. Each entry in the stack points to the
7586
+ // segment where this submessage's length is being accumulated.
7587
+ int stack[UPB_PBENCODER_MAX_NESTING], *top, *stacklimit;
7588
+
7589
+ // Depth of startmsg/endmsg calls.
7590
+ int depth;
7591
+
7592
+ // Initial buffers for the output buffer and segment buffer. If we outgrow
7593
+ // these we will dynamically allocate bigger ones.
7594
+ char initbuf[256];
7595
+ upb_pb_encoder_segment seginitbuf[32];
7596
+ )));
7748
7597
 
7749
7598
  UPB_BEGIN_EXTERN_C
7750
7599
 
7751
7600
  const upb_handlers *upb_pb_encoder_newhandlers(const upb_msgdef *m,
7752
7601
  const void *owner);
7602
+ void upb_pb_encoder_reset(upb_pb_encoder *e);
7753
7603
  upb_sink *upb_pb_encoder_input(upb_pb_encoder *p);
7754
- upb_pb_encoder* upb_pb_encoder_create(upb_env* e, const upb_handlers* h,
7755
- upb_bytessink* output);
7604
+ void upb_pb_encoder_init(upb_pb_encoder *e, const upb_handlers *h);
7605
+ void upb_pb_encoder_resetoutput(upb_pb_encoder *e, upb_bytessink *output);
7606
+ void upb_pb_encoder_uninit(upb_pb_encoder *e);
7756
7607
 
7757
7608
  UPB_END_EXTERN_C
7758
7609
 
@@ -7760,9 +7611,17 @@ UPB_END_EXTERN_C
7760
7611
 
7761
7612
  namespace upb {
7762
7613
  namespace pb {
7763
- inline Encoder* Encoder::Create(Environment* env, const Handlers* handlers,
7764
- BytesSink* output) {
7765
- return upb_pb_encoder_create(env, handlers, output);
7614
+ inline Encoder::Encoder(const upb::Handlers* handlers) {
7615
+ upb_pb_encoder_init(this, handlers);
7616
+ }
7617
+ inline Encoder::~Encoder() {
7618
+ upb_pb_encoder_uninit(this);
7619
+ }
7620
+ inline void Encoder::Reset() {
7621
+ upb_pb_encoder_reset(this);
7622
+ }
7623
+ inline void Encoder::ResetOutput(BytesSink* output) {
7624
+ upb_pb_encoder_resetoutput(this, output);
7766
7625
  }
7767
7626
  inline Sink* Encoder::input() {
7768
7627
  return upb_pb_encoder_input(this);
@@ -7880,51 +7739,58 @@ class TextPrinter;
7880
7739
 
7881
7740
  UPB_DECLARE_TYPE(upb::pb::TextPrinter, upb_textprinter);
7882
7741
 
7883
- #ifdef __cplusplus
7884
-
7885
- class upb::pb::TextPrinter {
7742
+ UPB_DEFINE_CLASS0(upb::pb::TextPrinter,
7886
7743
  public:
7887
7744
  // The given handlers must have come from NewHandlers(). It must outlive the
7888
7745
  // TextPrinter.
7889
- static TextPrinter *Create(Environment *env, const upb::Handlers *handlers,
7890
- BytesSink *output);
7746
+ explicit TextPrinter(const upb::Handlers* handlers);
7891
7747
 
7892
7748
  void SetSingleLineMode(bool single_line);
7893
7749
 
7750
+ bool ResetOutput(BytesSink* output);
7894
7751
  Sink* input();
7895
7752
 
7896
7753
  // If handler caching becomes a requirement we can add a code cache as in
7897
7754
  // decoder.h
7898
7755
  static reffed_ptr<const Handlers> NewHandlers(const MessageDef* md);
7899
- };
7900
7756
 
7901
- #endif
7757
+ private:
7758
+ ,
7759
+ UPB_DEFINE_STRUCT0(upb_textprinter,
7760
+ upb_sink input_;
7761
+ upb_bytessink *output_;
7762
+ int indent_depth_;
7763
+ bool single_line_;
7764
+ void *subc;
7765
+ ));
7902
7766
 
7903
- UPB_BEGIN_EXTERN_C
7767
+ UPB_BEGIN_EXTERN_C // {
7904
7768
 
7905
7769
  // C API.
7906
- upb_textprinter *upb_textprinter_create(upb_env *env, const upb_handlers *h,
7907
- upb_bytessink *output);
7770
+ void upb_textprinter_init(upb_textprinter *p, const upb_handlers *h);
7771
+ void upb_textprinter_uninit(upb_textprinter *p);
7772
+ bool upb_textprinter_resetoutput(upb_textprinter *p, upb_bytessink *output);
7908
7773
  void upb_textprinter_setsingleline(upb_textprinter *p, bool single_line);
7909
7774
  upb_sink *upb_textprinter_input(upb_textprinter *p);
7910
7775
 
7911
7776
  const upb_handlers *upb_textprinter_newhandlers(const upb_msgdef *m,
7912
7777
  const void *owner);
7913
7778
 
7914
- UPB_END_EXTERN_C
7779
+ UPB_END_EXTERN_C // }
7915
7780
 
7916
7781
  #ifdef __cplusplus
7917
7782
 
7918
7783
  namespace upb {
7919
7784
  namespace pb {
7920
- inline TextPrinter *TextPrinter::Create(Environment *env,
7921
- const upb::Handlers *handlers,
7922
- BytesSink *output) {
7923
- return upb_textprinter_create(env, handlers, output);
7785
+ inline TextPrinter::TextPrinter(const upb::Handlers* handlers) {
7786
+ upb_textprinter_init(this, handlers);
7924
7787
  }
7925
7788
  inline void TextPrinter::SetSingleLineMode(bool single_line) {
7926
7789
  upb_textprinter_setsingleline(this, single_line);
7927
7790
  }
7791
+ inline bool TextPrinter::ResetOutput(BytesSink* output) {
7792
+ return upb_textprinter_resetoutput(this, output);
7793
+ }
7928
7794
  inline Sink* TextPrinter::input() {
7929
7795
  return upb_textprinter_input(this);
7930
7796
  }
@@ -7963,32 +7829,96 @@ class Parser;
7963
7829
 
7964
7830
  UPB_DECLARE_TYPE(upb::json::Parser, upb_json_parser);
7965
7831
 
7966
- /* upb::json::Parser **********************************************************/
7832
+ // Internal-only struct used by the parser. A parser frame corresponds
7833
+ // one-to-one with a handler (sink) frame.
7834
+ typedef struct {
7835
+ UPB_PRIVATE_FOR_CPP
7836
+ upb_sink sink;
7837
+ // The current message in which we're parsing, and the field whose value we're
7838
+ // expecting next.
7839
+ const upb_msgdef *m;
7840
+ const upb_fielddef *f;
7841
+
7842
+ // We are in a repeated-field context, ready to emit mapentries as
7843
+ // submessages. This flag alters the start-of-object (open-brace) behavior to
7844
+ // begin a sequence of mapentry messages rather than a single submessage.
7845
+ bool is_map;
7846
+ // We are in a map-entry message context. This flag is set when parsing the
7847
+ // value field of a single map entry and indicates to all value-field parsers
7848
+ // (subobjects, strings, numbers, and bools) that the map-entry submessage
7849
+ // should end as soon as the value is parsed.
7850
+ bool is_mapentry;
7851
+ // If |is_map| or |is_mapentry| is true, |mapfield| refers to the parent
7852
+ // message's map field that we're currently parsing. This differs from |f|
7853
+ // because |f| is the field in the *current* message (i.e., the map-entry
7854
+ // message itself), not the parent's field that leads to this map.
7855
+ const upb_fielddef *mapfield;
7856
+ } upb_jsonparser_frame;
7967
7857
 
7968
- // Preallocation hint: parser won't allocate more bytes than this when first
7969
- // constructed. This hint may be an overestimate for some build configurations.
7970
- // But if the parser library is upgraded without recompiling the application,
7971
- // it may be an underestimate.
7972
- #define UPB_JSON_PARSER_SIZE 3568
7973
7858
 
7974
- #ifdef __cplusplus
7859
+ /* upb::json::Parser **********************************************************/
7860
+
7861
+ #define UPB_JSON_MAX_DEPTH 64
7975
7862
 
7976
7863
  // Parses an incoming BytesStream, pushing the results to the destination sink.
7977
- class upb::json::Parser {
7864
+ UPB_DEFINE_CLASS0(upb::json::Parser,
7978
7865
  public:
7979
- static Parser* Create(Environment* env, Sink* output);
7866
+ Parser(Status* status);
7867
+ ~Parser();
7868
+
7869
+ // Resets the state of the printer, so that it will expect to begin a new
7870
+ // document.
7871
+ void Reset();
7980
7872
 
7873
+ // Resets the output pointer which will serve as our closure. Implies
7874
+ // Reset().
7875
+ void ResetOutput(Sink* output);
7876
+
7877
+ // The input to the printer.
7981
7878
  BytesSink* input();
7879
+ ,
7880
+ UPB_DEFINE_STRUCT0(upb_json_parser,
7881
+ upb_byteshandler input_handler_;
7882
+ upb_bytessink input_;
7982
7883
 
7983
- private:
7984
- UPB_DISALLOW_POD_OPS(Parser, upb::json::Parser);
7985
- };
7884
+ // Stack to track the JSON scopes we are in.
7885
+ upb_jsonparser_frame stack[UPB_JSON_MAX_DEPTH];
7886
+ upb_jsonparser_frame *top;
7887
+ upb_jsonparser_frame *limit;
7986
7888
 
7987
- #endif
7889
+ upb_status *status;
7890
+
7891
+ // Ragel's internal parsing stack for the parsing state machine.
7892
+ int current_state;
7893
+ int parser_stack[UPB_JSON_MAX_DEPTH];
7894
+ int parser_top;
7895
+
7896
+ // The handle for the current buffer.
7897
+ const upb_bufhandle *handle;
7898
+
7899
+ // Accumulate buffer. See details in parser.rl.
7900
+ const char *accumulated;
7901
+ size_t accumulated_len;
7902
+ char *accumulate_buf;
7903
+ size_t accumulate_buf_size;
7904
+
7905
+ // Multi-part text data. See details in parser.rl.
7906
+ int multipart_state;
7907
+ upb_selector_t string_selector;
7908
+
7909
+ // Input capture. See details in parser.rl.
7910
+ const char *capture;
7911
+
7912
+ // Intermediate result of parsing a unicode escape sequence.
7913
+ uint32_t digit;
7914
+ ));
7988
7915
 
7989
7916
  UPB_BEGIN_EXTERN_C
7990
7917
 
7991
- upb_json_parser *upb_json_parser_create(upb_env *e, upb_sink *output);
7918
+ void upb_json_parser_init(upb_json_parser *p, upb_status *status);
7919
+ void upb_json_parser_uninit(upb_json_parser *p);
7920
+ void upb_json_parser_reset(upb_json_parser *p);
7921
+ void upb_json_parser_resetoutput(upb_json_parser *p, upb_sink *output);
7992
7922
  upb_bytessink *upb_json_parser_input(upb_json_parser *p);
7993
7923
 
7994
7924
  UPB_END_EXTERN_C
@@ -7997,8 +7927,11 @@ UPB_END_EXTERN_C
7997
7927
 
7998
7928
  namespace upb {
7999
7929
  namespace json {
8000
- inline Parser* Parser::Create(Environment* env, Sink* output) {
8001
- return upb_json_parser_create(env, output);
7930
+ inline Parser::Parser(Status* status) { upb_json_parser_init(this, status); }
7931
+ inline Parser::~Parser() { upb_json_parser_uninit(this); }
7932
+ inline void Parser::Reset() { upb_json_parser_reset(this); }
7933
+ inline void Parser::ResetOutput(Sink* output) {
7934
+ upb_json_parser_resetoutput(this, output);
8002
7935
  }
8003
7936
  inline BytesSink* Parser::input() {
8004
7937
  return upb_json_parser_input(this);
@@ -8037,48 +7970,71 @@ UPB_DECLARE_TYPE(upb::json::Printer, upb_json_printer);
8037
7970
 
8038
7971
  /* upb::json::Printer *********************************************************/
8039
7972
 
8040
- #define UPB_JSON_PRINTER_SIZE 168
8041
-
8042
- #ifdef __cplusplus
8043
-
8044
7973
  // Prints an incoming stream of data to a BytesSink in JSON format.
8045
- class upb::json::Printer {
7974
+ UPB_DEFINE_CLASS0(upb::json::Printer,
8046
7975
  public:
8047
- static Printer* Create(Environment* env, const upb::Handlers* handlers,
8048
- BytesSink* output);
7976
+ Printer(const upb::Handlers* handlers);
7977
+ ~Printer();
7978
+
7979
+ // Resets the state of the printer, so that it will expect to begin a new
7980
+ // document.
7981
+ void Reset();
7982
+
7983
+ // Resets the output pointer which will serve as our closure. Implies
7984
+ // Reset().
7985
+ void ResetOutput(BytesSink* output);
8049
7986
 
8050
7987
  // The input to the printer.
8051
7988
  Sink* input();
8052
7989
 
8053
7990
  // Returns handlers for printing according to the specified schema.
8054
7991
  static reffed_ptr<const Handlers> NewHandlers(const upb::MessageDef* md);
7992
+ ,
7993
+ UPB_DEFINE_STRUCT0(upb_json_printer,
7994
+ upb_sink input_;
7995
+ // BytesSink closure.
7996
+ void *subc_;
7997
+ upb_bytessink *output_;
7998
+
7999
+ // We track the depth so that we know when to emit startstr/endstr on the
8000
+ // output.
8001
+ int depth_;
8002
+ // Have we emitted the first element? This state is necessary to emit commas
8003
+ // without leaving a trailing comma in arrays/maps. We keep this state per
8004
+ // frame depth.
8005
+ //
8006
+ // Why max_depth * 2? UPB_MAX_HANDLER_DEPTH counts depth as nested messages.
8007
+ // We count frames (contexts in which we separate elements by commas) as both
8008
+ // repeated fields and messages (maps), and the worst case is a
8009
+ // message->repeated field->submessage->repeated field->... nesting.
8010
+ bool first_elem_[UPB_MAX_HANDLER_DEPTH * 2];
8011
+ ));
8055
8012
 
8056
- static const size_t kSize = UPB_JSON_PRINTER_SIZE;
8057
-
8058
- private:
8059
- UPB_DISALLOW_POD_OPS(Printer, upb::json::Printer);
8060
- };
8061
-
8062
- #endif
8063
-
8064
- UPB_BEGIN_EXTERN_C
8013
+ UPB_BEGIN_EXTERN_C // {
8065
8014
 
8066
8015
  // Native C API.
8067
- upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h,
8068
- upb_bytessink *output);
8016
+
8017
+ void upb_json_printer_init(upb_json_printer *p, const upb_handlers *h);
8018
+ void upb_json_printer_uninit(upb_json_printer *p);
8019
+ void upb_json_printer_reset(upb_json_printer *p);
8020
+ void upb_json_printer_resetoutput(upb_json_printer *p, upb_bytessink *output);
8069
8021
  upb_sink *upb_json_printer_input(upb_json_printer *p);
8070
8022
  const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md,
8071
8023
  const void *owner);
8072
8024
 
8073
- UPB_END_EXTERN_C
8025
+ UPB_END_EXTERN_C // }
8074
8026
 
8075
8027
  #ifdef __cplusplus
8076
8028
 
8077
8029
  namespace upb {
8078
8030
  namespace json {
8079
- inline Printer* Printer::Create(Environment* env, const upb::Handlers* handlers,
8080
- BytesSink* output) {
8081
- return upb_json_printer_create(env, handlers, output);
8031
+ inline Printer::Printer(const upb::Handlers* handlers) {
8032
+ upb_json_printer_init(this, handlers);
8033
+ }
8034
+ inline Printer::~Printer() { upb_json_printer_uninit(this); }
8035
+ inline void Printer::Reset() { upb_json_printer_reset(this); }
8036
+ inline void Printer::ResetOutput(BytesSink* output) {
8037
+ upb_json_printer_resetoutput(this, output);
8082
8038
  }
8083
8039
  inline Sink* Printer::input() { return upb_json_printer_input(this); }
8084
8040
  inline reffed_ptr<const Handlers> Printer::NewHandlers(