google-protobuf 3.0.0.alpha.2.0 → 3.0.0.alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


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

@@ -99,6 +99,15 @@
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
+
102
111
  #if __STDC_VERSION__ >= 199901L
103
112
  #define UPB_C99
104
113
  #endif
@@ -4805,10 +4814,8 @@ UPB_DEFINE_STRUCT0(upb_byteshandler,
4805
4814
  ));
4806
4815
 
4807
4816
  void upb_byteshandler_init(upb_byteshandler *h);
4808
- void upb_byteshandler_uninit(upb_byteshandler *h);
4809
4817
 
4810
4818
  // Caller must ensure that "d" outlives the handlers.
4811
- // TODO(haberman): support handlerfree function for the data.
4812
4819
  // TODO(haberman): should this have a "freeze" operation? It's not necessary
4813
4820
  // for memory management, but could be useful to force immutability and provide
4814
4821
  // a convenient moment to verify that all registration succeeded.
@@ -4983,12 +4990,17 @@ template <class T> struct disable_if_same<T, T> {};
4983
4990
  template <class T> void DeletePointer(void *p) { delete static_cast<T>(p); }
4984
4991
 
4985
4992
  template <class T1, class T2>
4986
- struct FirstUnlessVoid {
4993
+ struct FirstUnlessVoidOrBool {
4987
4994
  typedef T1 value;
4988
4995
  };
4989
4996
 
4990
4997
  template <class T2>
4991
- struct FirstUnlessVoid<void, T2> {
4998
+ struct FirstUnlessVoidOrBool<void, T2> {
4999
+ typedef T2 value;
5000
+ };
5001
+
5002
+ template <class T2>
5003
+ struct FirstUnlessVoidOrBool<bool, T2> {
4992
5004
  typedef T2 value;
4993
5005
  };
4994
5006
 
@@ -5370,10 +5382,14 @@ inline MethodSig4<R, C, P1, P2, P3, P4> MatchFunc(R (C::*f)(P1, P2, P3, P4)) {
5370
5382
  //
5371
5383
  // 1. If the function returns void, make it return the expected type and with
5372
5384
  // a value that always indicates success.
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).
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.
5377
5393
 
5378
5394
  // Template parameters are FuncN type and desired return type.
5379
5395
  template <class F, class R, class Enable = void>
@@ -5762,10 +5778,13 @@ inline Handler<T>::Handler(F func)
5762
5778
  attr_.SetClosureType(UniquePtrForType<typename F::FuncInfo::Closure>());
5763
5779
 
5764
5780
  // We use the closure type (from the first parameter) if the return type is
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
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
5769
5788
  EffectiveReturn;
5770
5789
  attr_.SetReturnClosureType(UniquePtrForType<EffectiveReturn>());
5771
5790
  }
@@ -5960,9 +5979,7 @@ inline BytesHandler::BytesHandler() {
5960
5979
  upb_byteshandler_init(this);
5961
5980
  }
5962
5981
 
5963
- inline BytesHandler::~BytesHandler() {
5964
- upb_byteshandler_uninit(this);
5965
- }
5982
+ inline BytesHandler::~BytesHandler() {}
5966
5983
 
5967
5984
  } // namespace upb
5968
5985
 
@@ -5983,6 +6000,261 @@ inline BytesHandler::~BytesHandler() {
5983
6000
  #endif // UPB_HANDLERS_INL_H_
5984
6001
 
5985
6002
  #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_
5986
6258
  /*
5987
6259
  * upb - a minimalist implementation of protocol buffers.
5988
6260
  *
@@ -6018,27 +6290,6 @@ UPB_DECLARE_TYPE(upb::BufferSource, upb_bufsrc);
6018
6290
  UPB_DECLARE_TYPE(upb::BytesSink, upb_bytessink);
6019
6291
  UPB_DECLARE_TYPE(upb::Sink, upb_sink);
6020
6292
 
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
-
6042
6293
  // A upb::Sink is an object that binds a upb::Handlers object to some runtime
6043
6294
  // state. It represents an endpoint to which data can be sent.
6044
6295
  //
@@ -6598,45 +6849,11 @@ class Reader;
6598
6849
 
6599
6850
  UPB_DECLARE_TYPE(upb::descriptor::Reader, upb_descreader);
6600
6851
 
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
6852
+ #ifdef __cplusplus
6636
6853
 
6637
6854
  // Class that receives descriptor data according to the descriptor.proto schema
6638
6855
  // and use it to build upb::Defs corresponding to that schema.
6639
- UPB_DEFINE_CLASS0(upb::descriptor::Reader,
6856
+ class upb::descriptor::Reader {
6640
6857
  public:
6641
6858
  // These handlers must have come from NewHandlers() and must outlive the
6642
6859
  // Reader.
@@ -6646,11 +6863,7 @@ UPB_DEFINE_CLASS0(upb::descriptor::Reader,
6646
6863
  // to build/memory-manage the handlers at runtime at all). Unfortunately this
6647
6864
  // is a bit tricky to implement for Handlers, but necessary to simplify this
6648
6865
  // interface.
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();
6866
+ static Reader* Create(Environment* env, const Handlers* handlers);
6654
6867
 
6655
6868
  // The reader's input; this is where descriptor.proto data should be sent.
6656
6869
  Sink* input();
@@ -6666,45 +6879,30 @@ UPB_DEFINE_CLASS0(upb::descriptor::Reader,
6666
6879
 
6667
6880
  // Builds and returns handlers for the reader, owned by "owner."
6668
6881
  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;
6675
-
6676
- uint32_t number;
6677
- char *name;
6678
- bool saw_number;
6679
- bool saw_name;
6680
6882
 
6681
- char *default_string;
6883
+ private:
6884
+ UPB_DISALLOW_POD_OPS(Reader, upb::descriptor::Reader);
6885
+ };
6682
6886
 
6683
- upb_fielddef *f;
6684
- ));
6887
+ #endif
6685
6888
 
6686
- UPB_BEGIN_EXTERN_C // {
6889
+ UPB_BEGIN_EXTERN_C
6687
6890
 
6688
6891
  // C API.
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);
6892
+ upb_descreader *upb_descreader_create(upb_env *e, const upb_handlers *h);
6693
6893
  upb_sink *upb_descreader_input(upb_descreader *r);
6694
6894
  upb_def **upb_descreader_getdefs(upb_descreader *r, void *owner, int *n);
6695
6895
  const upb_handlers *upb_descreader_newhandlers(const void *owner);
6696
6896
 
6697
- UPB_END_EXTERN_C // }
6897
+ UPB_END_EXTERN_C
6698
6898
 
6699
6899
  #ifdef __cplusplus
6700
6900
  // C++ implementation details. /////////////////////////////////////////////////
6701
6901
  namespace upb {
6702
6902
  namespace descriptor {
6703
- inline Reader::Reader(const Handlers *h, Status *s) {
6704
- upb_descreader_init(this, h, s);
6903
+ inline Reader* Reader::Create(Environment* e, const Handlers *h) {
6904
+ return upb_descreader_create(e, h);
6705
6905
  }
6706
- inline Reader::~Reader() { upb_descreader_uninit(this); }
6707
- inline void Reader::Reset() { upb_descreader_reset(this); }
6708
6906
  inline Sink* Reader::input() { return upb_descreader_input(this); }
6709
6907
  inline upb::Def** Reader::GetDefs(void* owner, int* n) {
6710
6908
  return upb_descreader_getdefs(this, owner, n);
@@ -6764,44 +6962,6 @@ UPB_DECLARE_TYPE(upb::pb::Decoder, upb_pbdecoder);
6764
6962
  UPB_DECLARE_TYPE(upb::pb::DecoderMethod, upb_pbdecodermethod);
6765
6963
  UPB_DECLARE_TYPE(upb::pb::DecoderMethodOptions, upb_pbdecodermethodopts);
6766
6964
 
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
-
6805
6965
  // The parameters one uses to construct a DecoderMethod.
6806
6966
  // TODO(haberman): move allowjit here? Seems more convenient for users.
6807
6967
  UPB_DEFINE_CLASS0(upb::pb::DecoderMethodOptions,
@@ -6879,22 +7039,31 @@ UPB_DEFINE_STRUCT(upb_pbdecodermethod, upb_refcounted,
6879
7039
  upb_inttable dispatch;
6880
7040
  ));
6881
7041
 
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
+
6882
7050
  // A Decoder receives binary protobuf data on its input sink and pushes the
6883
7051
  // decoded data to its output sink.
6884
- UPB_DEFINE_CLASS0(upb::pb::Decoder,
7052
+ class upb::pb::Decoder {
6885
7053
  public:
6886
7054
  // Constructs a decoder instance for the given method, which must outlive this
6887
7055
  // decoder. Any errors during parsing will be set on the given status, which
6888
7056
  // must also outlive this decoder.
6889
- Decoder(const DecoderMethod* method, Status* status);
6890
- ~Decoder();
7057
+ //
7058
+ // The sink must match the given method.
7059
+ static Decoder* Create(Environment* env, const DecoderMethod* method,
7060
+ Sink* output);
6891
7061
 
6892
7062
  // Returns the DecoderMethod this decoder is parsing from.
6893
- // TODO(haberman): Do users need to be able to rebind this?
6894
7063
  const DecoderMethod* method() const;
6895
7064
 
6896
- // Resets the state of the decoder.
6897
- void Reset();
7065
+ // The sink on which this decoder receives input.
7066
+ BytesSink* input();
6898
7067
 
6899
7068
  // Returns number of bytes successfully parsed.
6900
7069
  //
@@ -6905,76 +7074,25 @@ UPB_DEFINE_CLASS0(upb::pb::Decoder,
6905
7074
  // callback.
6906
7075
  uint64_t BytesParsed() const;
6907
7076
 
6908
- // Resets the output sink of the Decoder.
6909
- // The given sink must match method()->dest_handlers().
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.
6910
7081
  //
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);
6916
-
6917
- // The sink on which this decoder receives input.
6918
- BytesSink* input();
6919
-
6920
- private:
6921
- UPB_DISALLOW_COPY_AND_ASSIGN(Decoder);
6922
- ,
6923
- UPB_DEFINE_STRUCT0(upb_pbdecoder, UPB_QUOTE(
6924
- // Our input sink.
6925
- upb_bytessink input_;
6926
-
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;
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);
6951
7086
 
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;
7087
+ void Reset();
6961
7088
 
6962
- const void *saved_rsp;
6963
- #endif
7089
+ static const size_t kSize = UPB_PB_DECODER_SIZE;
6964
7090
 
6965
- upb_status *status;
7091
+ private:
7092
+ UPB_DISALLOW_POD_OPS(Decoder, upb::pb::Decoder);
7093
+ };
6966
7094
 
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
- )));
7095
+ #endif // __cplusplus
6978
7096
 
6979
7097
  // A class for caching protobuf processing code, whether bytecode for the
6980
7098
  // interpreted decoder or machine code for the JIT.
@@ -7023,14 +7141,15 @@ UPB_DEFINE_STRUCT0(upb_pbcodecache,
7023
7141
 
7024
7142
  UPB_BEGIN_EXTERN_C // {
7025
7143
 
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);
7144
+ upb_pbdecoder *upb_pbdecoder_create(upb_env *e,
7145
+ const upb_pbdecodermethod *method,
7146
+ upb_sink *output);
7030
7147
  const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d);
7031
- bool upb_pbdecoder_resetoutput(upb_pbdecoder *d, upb_sink *sink);
7032
7148
  upb_bytessink *upb_pbdecoder_input(upb_pbdecoder *d);
7033
7149
  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);
7034
7153
 
7035
7154
  void upb_pbdecodermethodopts_init(upb_pbdecodermethodopts *opts,
7036
7155
  const upb_handlers *h);
@@ -7065,27 +7184,27 @@ namespace upb {
7065
7184
 
7066
7185
  namespace pb {
7067
7186
 
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);
7187
+ // static
7188
+ inline Decoder* Decoder::Create(Environment* env, const DecoderMethod* m,
7189
+ Sink* sink) {
7190
+ return upb_pbdecoder_create(env, m, sink);
7073
7191
  }
7074
7192
  inline const DecoderMethod* Decoder::method() const {
7075
7193
  return upb_pbdecoder_method(this);
7076
7194
  }
7077
- inline void Decoder::Reset() {
7078
- upb_pbdecoder_reset(this);
7195
+ inline BytesSink* Decoder::input() {
7196
+ return upb_pbdecoder_input(this);
7079
7197
  }
7080
7198
  inline uint64_t Decoder::BytesParsed() const {
7081
7199
  return upb_pbdecoder_bytesparsed(this);
7082
7200
  }
7083
- inline bool Decoder::ResetOutput(Sink* sink) {
7084
- return upb_pbdecoder_resetoutput(this, sink);
7201
+ inline size_t Decoder::max_nesting() const {
7202
+ return upb_pbdecoder_maxnesting(this);
7085
7203
  }
7086
- inline BytesSink* Decoder::input() {
7087
- return upb_pbdecoder_input(this);
7204
+ inline bool Decoder::set_max_nesting(size_t max) {
7205
+ return upb_pbdecoder_setmaxnesting(this, max);
7088
7206
  }
7207
+ inline void Decoder::Reset() { upb_pbdecoder_reset(this); }
7089
7208
 
7090
7209
  inline DecoderMethodOptions::DecoderMethodOptions(const Handlers* h) {
7091
7210
  upb_pbdecodermethodopts_init(this, h);
@@ -7242,6 +7361,95 @@ typedef struct {
7242
7361
  #endif
7243
7362
  } mgroup;
7244
7363
 
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
+
7245
7453
  // Decoder entry points; used as handlers.
7246
7454
  void *upb_pbdecoder_startbc(void *closure, const void *pc, size_t size_hint);
7247
7455
  void *upb_pbdecoder_startjit(void *closure, const void *hd, size_t size_hint);
@@ -7509,101 +7717,42 @@ UPB_DECLARE_TYPE(upb::pb::Encoder, upb_pb_encoder);
7509
7717
 
7510
7718
  /* upb::pb::Encoder ***********************************************************/
7511
7719
 
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;
7544
-
7545
- UPB_DEFINE_CLASS0(upb::pb::Encoder,
7546
- public:
7547
- Encoder(const upb::Handlers* handlers);
7548
- ~Encoder();
7549
-
7550
- static reffed_ptr<const Handlers> NewHandlers(const upb::MessageDef* msg);
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
7551
7725
 
7552
- // Resets the state of the printer, so that it will expect to begin a new
7553
- // document.
7554
- void Reset();
7726
+ #ifdef __cplusplus
7555
7727
 
7556
- // Resets the output pointer which will serve as our closure.
7557
- void ResetOutput(BytesSink* output);
7728
+ class upb::pb::Encoder {
7729
+ 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);
7558
7734
 
7559
7735
  // The input to the encoder.
7560
7736
  Sink* input();
7561
7737
 
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_;
7738
+ // Creates a new set of handlers for this MessageDef.
7739
+ static reffed_ptr<const Handlers> NewHandlers(const MessageDef* msg);
7569
7740
 
7570
- // The "subclosure" -- used as the inner closure as part of the bytessink
7571
- // protocol.
7572
- void *subc;
7741
+ static const size_t kSize = UPB_PB_ENCODER_SIZE;
7573
7742
 
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;
7578
-
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;
7743
+ private:
7744
+ UPB_DISALLOW_POD_OPS(Encoder, upb::pb::Encoder);
7745
+ };
7591
7746
 
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
- )));
7747
+ #endif
7597
7748
 
7598
7749
  UPB_BEGIN_EXTERN_C
7599
7750
 
7600
7751
  const upb_handlers *upb_pb_encoder_newhandlers(const upb_msgdef *m,
7601
7752
  const void *owner);
7602
- void upb_pb_encoder_reset(upb_pb_encoder *e);
7603
7753
  upb_sink *upb_pb_encoder_input(upb_pb_encoder *p);
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);
7754
+ upb_pb_encoder* upb_pb_encoder_create(upb_env* e, const upb_handlers* h,
7755
+ upb_bytessink* output);
7607
7756
 
7608
7757
  UPB_END_EXTERN_C
7609
7758
 
@@ -7611,17 +7760,9 @@ UPB_END_EXTERN_C
7611
7760
 
7612
7761
  namespace upb {
7613
7762
  namespace pb {
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);
7763
+ inline Encoder* Encoder::Create(Environment* env, const Handlers* handlers,
7764
+ BytesSink* output) {
7765
+ return upb_pb_encoder_create(env, handlers, output);
7625
7766
  }
7626
7767
  inline Sink* Encoder::input() {
7627
7768
  return upb_pb_encoder_input(this);
@@ -7739,58 +7880,51 @@ class TextPrinter;
7739
7880
 
7740
7881
  UPB_DECLARE_TYPE(upb::pb::TextPrinter, upb_textprinter);
7741
7882
 
7742
- UPB_DEFINE_CLASS0(upb::pb::TextPrinter,
7883
+ #ifdef __cplusplus
7884
+
7885
+ class upb::pb::TextPrinter {
7743
7886
  public:
7744
7887
  // The given handlers must have come from NewHandlers(). It must outlive the
7745
7888
  // TextPrinter.
7746
- explicit TextPrinter(const upb::Handlers* handlers);
7889
+ static TextPrinter *Create(Environment *env, const upb::Handlers *handlers,
7890
+ BytesSink *output);
7747
7891
 
7748
7892
  void SetSingleLineMode(bool single_line);
7749
7893
 
7750
- bool ResetOutput(BytesSink* output);
7751
7894
  Sink* input();
7752
7895
 
7753
7896
  // If handler caching becomes a requirement we can add a code cache as in
7754
7897
  // decoder.h
7755
7898
  static reffed_ptr<const Handlers> NewHandlers(const MessageDef* md);
7899
+ };
7756
7900
 
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
- ));
7901
+ #endif
7766
7902
 
7767
- UPB_BEGIN_EXTERN_C // {
7903
+ UPB_BEGIN_EXTERN_C
7768
7904
 
7769
7905
  // C API.
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);
7906
+ upb_textprinter *upb_textprinter_create(upb_env *env, const upb_handlers *h,
7907
+ upb_bytessink *output);
7773
7908
  void upb_textprinter_setsingleline(upb_textprinter *p, bool single_line);
7774
7909
  upb_sink *upb_textprinter_input(upb_textprinter *p);
7775
7910
 
7776
7911
  const upb_handlers *upb_textprinter_newhandlers(const upb_msgdef *m,
7777
7912
  const void *owner);
7778
7913
 
7779
- UPB_END_EXTERN_C // }
7914
+ UPB_END_EXTERN_C
7780
7915
 
7781
7916
  #ifdef __cplusplus
7782
7917
 
7783
7918
  namespace upb {
7784
7919
  namespace pb {
7785
- inline TextPrinter::TextPrinter(const upb::Handlers* handlers) {
7786
- upb_textprinter_init(this, handlers);
7920
+ inline TextPrinter *TextPrinter::Create(Environment *env,
7921
+ const upb::Handlers *handlers,
7922
+ BytesSink *output) {
7923
+ return upb_textprinter_create(env, handlers, output);
7787
7924
  }
7788
7925
  inline void TextPrinter::SetSingleLineMode(bool single_line) {
7789
7926
  upb_textprinter_setsingleline(this, single_line);
7790
7927
  }
7791
- inline bool TextPrinter::ResetOutput(BytesSink* output) {
7792
- return upb_textprinter_resetoutput(this, output);
7793
- }
7794
7928
  inline Sink* TextPrinter::input() {
7795
7929
  return upb_textprinter_input(this);
7796
7930
  }
@@ -7829,96 +7963,32 @@ class Parser;
7829
7963
 
7830
7964
  UPB_DECLARE_TYPE(upb::json::Parser, upb_json_parser);
7831
7965
 
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;
7857
-
7858
-
7859
7966
  /* upb::json::Parser **********************************************************/
7860
7967
 
7861
- #define UPB_JSON_MAX_DEPTH 64
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
+
7974
+ #ifdef __cplusplus
7862
7975
 
7863
7976
  // Parses an incoming BytesStream, pushing the results to the destination sink.
7864
- UPB_DEFINE_CLASS0(upb::json::Parser,
7977
+ class upb::json::Parser {
7865
7978
  public:
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();
7979
+ static Parser* Create(Environment* env, Sink* output);
7872
7980
 
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.
7878
7981
  BytesSink* input();
7879
- ,
7880
- UPB_DEFINE_STRUCT0(upb_json_parser,
7881
- upb_byteshandler input_handler_;
7882
- upb_bytessink input_;
7883
-
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;
7888
-
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
7982
 
7909
- // Input capture. See details in parser.rl.
7910
- const char *capture;
7983
+ private:
7984
+ UPB_DISALLOW_POD_OPS(Parser, upb::json::Parser);
7985
+ };
7911
7986
 
7912
- // Intermediate result of parsing a unicode escape sequence.
7913
- uint32_t digit;
7914
- ));
7987
+ #endif
7915
7988
 
7916
7989
  UPB_BEGIN_EXTERN_C
7917
7990
 
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);
7991
+ upb_json_parser *upb_json_parser_create(upb_env *e, upb_sink *output);
7922
7992
  upb_bytessink *upb_json_parser_input(upb_json_parser *p);
7923
7993
 
7924
7994
  UPB_END_EXTERN_C
@@ -7927,11 +7997,8 @@ UPB_END_EXTERN_C
7927
7997
 
7928
7998
  namespace upb {
7929
7999
  namespace json {
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);
8000
+ inline Parser* Parser::Create(Environment* env, Sink* output) {
8001
+ return upb_json_parser_create(env, output);
7935
8002
  }
7936
8003
  inline BytesSink* Parser::input() {
7937
8004
  return upb_json_parser_input(this);
@@ -7970,71 +8037,48 @@ UPB_DECLARE_TYPE(upb::json::Printer, upb_json_printer);
7970
8037
 
7971
8038
  /* upb::json::Printer *********************************************************/
7972
8039
 
7973
- // Prints an incoming stream of data to a BytesSink in JSON format.
7974
- UPB_DEFINE_CLASS0(upb::json::Printer,
7975
- public:
7976
- Printer(const upb::Handlers* handlers);
7977
- ~Printer();
8040
+ #define UPB_JSON_PRINTER_SIZE 168
7978
8041
 
7979
- // Resets the state of the printer, so that it will expect to begin a new
7980
- // document.
7981
- void Reset();
8042
+ #ifdef __cplusplus
7982
8043
 
7983
- // Resets the output pointer which will serve as our closure. Implies
7984
- // Reset().
7985
- void ResetOutput(BytesSink* output);
8044
+ // Prints an incoming stream of data to a BytesSink in JSON format.
8045
+ class upb::json::Printer {
8046
+ public:
8047
+ static Printer* Create(Environment* env, const upb::Handlers* handlers,
8048
+ BytesSink* output);
7986
8049
 
7987
8050
  // The input to the printer.
7988
8051
  Sink* input();
7989
8052
 
7990
8053
  // Returns handlers for printing according to the specified schema.
7991
8054
  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
- ));
8012
8055
 
8013
- UPB_BEGIN_EXTERN_C // {
8056
+ static const size_t kSize = UPB_JSON_PRINTER_SIZE;
8014
8057
 
8015
- // Native C API.
8058
+ private:
8059
+ UPB_DISALLOW_POD_OPS(Printer, upb::json::Printer);
8060
+ };
8061
+
8062
+ #endif
8016
8063
 
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);
8064
+ UPB_BEGIN_EXTERN_C
8065
+
8066
+ // Native C API.
8067
+ upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h,
8068
+ upb_bytessink *output);
8021
8069
  upb_sink *upb_json_printer_input(upb_json_printer *p);
8022
8070
  const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md,
8023
8071
  const void *owner);
8024
8072
 
8025
- UPB_END_EXTERN_C // }
8073
+ UPB_END_EXTERN_C
8026
8074
 
8027
8075
  #ifdef __cplusplus
8028
8076
 
8029
8077
  namespace upb {
8030
8078
  namespace json {
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);
8079
+ inline Printer* Printer::Create(Environment* env, const upb::Handlers* handlers,
8080
+ BytesSink* output) {
8081
+ return upb_json_printer_create(env, handlers, output);
8038
8082
  }
8039
8083
  inline Sink* Printer::input() { return upb_json_printer_input(this); }
8040
8084
  inline reffed_ptr<const Handlers> Printer::NewHandlers(