google-protobuf 3.4.1.1-universal-darwin → 3.5.0-universal-darwin

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.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c45266f471e20e3e1a1583b86021d6a22d5a4713
4
- data.tar.gz: 91d9c710a6748f856d8add39f8f2b5df99bab7df
3
+ metadata.gz: bda9a713b39181ff8326f55819809a1cd9e97f92
4
+ data.tar.gz: 25fa899f5c99e0bf0e6dc07e1fcd02e6feea5237
5
5
  SHA512:
6
- metadata.gz: 171dd6b026440856a3c31c38864b04a4955faa9073010e22568447b903b6326a7023c9bca7e189f3112e4da631dd686fc30bb856b9edea49a5db4f13c6524b92
7
- data.tar.gz: 481f4b66e84bd1431dd6601c56881f8b55ee24f68a9015873053a380b2ce263a3651b8e4ed15f455084501a1718fe49d00af026c29ba3f46237582307c82db3d
6
+ metadata.gz: 8a3d506f87bcf1db665ae4e78fa81f86739f6d62cc3fc03c62279160109bff1be3a3360415002cb62ae1cb79f3d471e3738d95ca7f1237f050b0a810c13cee04
7
+ data.tar.gz: ad620df5df814513bfded4d5bec6addb817a6fb7b62b9900dac2930467a30d10ee4addffbe6c4420149531fc9847cbe1e971b3ba02c469abd5527857da0de960
@@ -228,7 +228,6 @@ DEFINE_CLASS(Descriptor, "Google::Protobuf::Descriptor");
228
228
  void Descriptor_mark(void* _self) {
229
229
  Descriptor* self = _self;
230
230
  rb_gc_mark(self->klass);
231
- rb_gc_mark(self->typeclass_references);
232
231
  }
233
232
 
234
233
  void Descriptor_free(void* _self) {
@@ -283,7 +282,6 @@ VALUE Descriptor_alloc(VALUE klass) {
283
282
  self->pb_serialize_handlers = NULL;
284
283
  self->json_serialize_handlers = NULL;
285
284
  self->json_serialize_handlers_preserve = NULL;
286
- self->typeclass_references = rb_ary_new();
287
285
  return ret;
288
286
  }
289
287
 
@@ -1635,7 +1633,7 @@ VALUE Builder_alloc(VALUE klass) {
1635
1633
  Builder* self = ALLOC(Builder);
1636
1634
  VALUE ret = TypedData_Wrap_Struct(
1637
1635
  klass, &_Builder_type, self);
1638
- self->pending_list = rb_ary_new();
1636
+ self->pending_list = Qnil;
1639
1637
  self->defs = NULL;
1640
1638
  return ret;
1641
1639
  }
@@ -1645,11 +1643,24 @@ void Builder_register(VALUE module) {
1645
1643
  rb_define_alloc_func(klass, Builder_alloc);
1646
1644
  rb_define_method(klass, "add_message", Builder_add_message, 1);
1647
1645
  rb_define_method(klass, "add_enum", Builder_add_enum, 1);
1646
+ rb_define_method(klass, "initialize", Builder_initialize, 0);
1648
1647
  rb_define_method(klass, "finalize_to_pool", Builder_finalize_to_pool, 1);
1649
1648
  cBuilder = klass;
1650
1649
  rb_gc_register_address(&cBuilder);
1651
1650
  }
1652
1651
 
1652
+ /*
1653
+ * call-seq:
1654
+ * Builder.new(d) => builder
1655
+ *
1656
+ * Create a new message builder.
1657
+ */
1658
+ VALUE Builder_initialize(VALUE _self) {
1659
+ DEFINE_SELF(Builder, self, _self);
1660
+ self->pending_list = rb_ary_new();
1661
+ return Qnil;
1662
+ }
1663
+
1653
1664
  /*
1654
1665
  * call-seq:
1655
1666
  * Builder.add_message(name, &block)
@@ -44,6 +44,56 @@ VALUE noleak_rb_str_cat(VALUE rb_str, const char *str, long len) {
44
44
  return rb_str;
45
45
  }
46
46
 
47
+ // The code below also comes from upb's prototype Ruby binding, developed by
48
+ // haberman@.
49
+
50
+ /* stringsink *****************************************************************/
51
+
52
+ static void *stringsink_start(void *_sink, const void *hd, size_t size_hint) {
53
+ stringsink *sink = _sink;
54
+ sink->len = 0;
55
+ return sink;
56
+ }
57
+
58
+ static size_t stringsink_string(void *_sink, const void *hd, const char *ptr,
59
+ size_t len, const upb_bufhandle *handle) {
60
+ stringsink *sink = _sink;
61
+ size_t new_size = sink->size;
62
+
63
+ UPB_UNUSED(hd);
64
+ UPB_UNUSED(handle);
65
+
66
+ while (sink->len + len > new_size) {
67
+ new_size *= 2;
68
+ }
69
+
70
+ if (new_size != sink->size) {
71
+ sink->ptr = realloc(sink->ptr, new_size);
72
+ sink->size = new_size;
73
+ }
74
+
75
+ memcpy(sink->ptr + sink->len, ptr, len);
76
+ sink->len += len;
77
+
78
+ return len;
79
+ }
80
+
81
+ void stringsink_init(stringsink *sink) {
82
+ upb_byteshandler_init(&sink->handler);
83
+ upb_byteshandler_setstartstr(&sink->handler, stringsink_start, NULL);
84
+ upb_byteshandler_setstring(&sink->handler, stringsink_string, NULL);
85
+
86
+ upb_bytessink_reset(&sink->sink, &sink->handler, sink);
87
+
88
+ sink->size = 32;
89
+ sink->ptr = malloc(sink->size);
90
+ sink->len = 0;
91
+ }
92
+
93
+ void stringsink_uninit(stringsink *sink) {
94
+ free(sink->ptr);
95
+ }
96
+
47
97
  // -----------------------------------------------------------------------------
48
98
  // Parsing.
49
99
  // -----------------------------------------------------------------------------
@@ -613,6 +663,20 @@ static void add_handlers_for_oneof_field(upb_handlers *h,
613
663
  upb_handlerattr_uninit(&attr);
614
664
  }
615
665
 
666
+ static bool unknown_field_handler(void* closure, const void* hd,
667
+ const char* buf, size_t size) {
668
+ UPB_UNUSED(hd);
669
+
670
+ MessageHeader* msg = (MessageHeader*)closure;
671
+ if (msg->unknown_fields == NULL) {
672
+ msg->unknown_fields = malloc(sizeof(stringsink));
673
+ stringsink_init(msg->unknown_fields);
674
+ }
675
+
676
+ stringsink_string(msg->unknown_fields, NULL, buf, size, NULL);
677
+
678
+ return true;
679
+ }
616
680
 
617
681
  static void add_handlers_for_message(const void *closure, upb_handlers *h) {
618
682
  const upb_msgdef* msgdef = upb_handlers_msgdef(h);
@@ -634,6 +698,9 @@ static void add_handlers_for_message(const void *closure, upb_handlers *h) {
634
698
  desc->layout = create_layout(desc->msgdef);
635
699
  }
636
700
 
701
+ upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
702
+ upb_handlers_setunknown(h, unknown_field_handler, &attr);
703
+
637
704
  for (upb_msg_field_begin(&i, desc->msgdef);
638
705
  !upb_msg_field_done(&i);
639
706
  upb_msg_field_next(&i)) {
@@ -831,65 +898,6 @@ VALUE Message_decode_json(VALUE klass, VALUE data) {
831
898
  // -----------------------------------------------------------------------------
832
899
  // Serializing.
833
900
  // -----------------------------------------------------------------------------
834
- //
835
- // The code below also comes from upb's prototype Ruby binding, developed by
836
- // haberman@.
837
-
838
- /* stringsink *****************************************************************/
839
-
840
- // This should probably be factored into a common upb component.
841
-
842
- typedef struct {
843
- upb_byteshandler handler;
844
- upb_bytessink sink;
845
- char *ptr;
846
- size_t len, size;
847
- } stringsink;
848
-
849
- static void *stringsink_start(void *_sink, const void *hd, size_t size_hint) {
850
- stringsink *sink = _sink;
851
- sink->len = 0;
852
- return sink;
853
- }
854
-
855
- static size_t stringsink_string(void *_sink, const void *hd, const char *ptr,
856
- size_t len, const upb_bufhandle *handle) {
857
- stringsink *sink = _sink;
858
- size_t new_size = sink->size;
859
-
860
- UPB_UNUSED(hd);
861
- UPB_UNUSED(handle);
862
-
863
- while (sink->len + len > new_size) {
864
- new_size *= 2;
865
- }
866
-
867
- if (new_size != sink->size) {
868
- sink->ptr = realloc(sink->ptr, new_size);
869
- sink->size = new_size;
870
- }
871
-
872
- memcpy(sink->ptr + sink->len, ptr, len);
873
- sink->len += len;
874
-
875
- return len;
876
- }
877
-
878
- void stringsink_init(stringsink *sink) {
879
- upb_byteshandler_init(&sink->handler);
880
- upb_byteshandler_setstartstr(&sink->handler, stringsink_start, NULL);
881
- upb_byteshandler_setstring(&sink->handler, stringsink_string, NULL);
882
-
883
- upb_bytessink_reset(&sink->sink, &sink->handler, sink);
884
-
885
- sink->size = 32;
886
- sink->ptr = malloc(sink->size);
887
- sink->len = 0;
888
- }
889
-
890
- void stringsink_uninit(stringsink *sink) {
891
- free(sink->ptr);
892
- }
893
901
 
894
902
  /* msgvisitor *****************************************************************/
895
903
 
@@ -1171,6 +1179,11 @@ static void putmsg(VALUE msg_rb, const Descriptor* desc,
1171
1179
  }
1172
1180
  }
1173
1181
 
1182
+ stringsink* unknown = msg->unknown_fields;
1183
+ if (unknown != NULL) {
1184
+ upb_sink_putunknown(sink, unknown->ptr, unknown->len);
1185
+ }
1186
+
1174
1187
  upb_sink_endmsg(sink, &status);
1175
1188
  }
1176
1189
 
@@ -44,6 +44,11 @@ void Message_mark(void* _self) {
44
44
  }
45
45
 
46
46
  void Message_free(void* self) {
47
+ stringsink* unknown = ((MessageHeader *)self)->unknown_fields;
48
+ if (unknown != NULL) {
49
+ stringsink_uninit(unknown);
50
+ free(unknown);
51
+ }
47
52
  xfree(self);
48
53
  }
49
54
 
@@ -67,6 +72,8 @@ VALUE Message_alloc(VALUE klass) {
67
72
  msg->descriptor = desc;
68
73
  rb_ivar_set(ret, descriptor_instancevar_interned, descriptor);
69
74
 
75
+ msg->unknown_fields = NULL;
76
+
70
77
  layout_init(desc->layout, Message_data(msg));
71
78
 
72
79
  return ret;
@@ -217,20 +224,32 @@ VALUE Message_respond_to_missing(int argc, VALUE* argv, VALUE _self) {
217
224
  return Qtrue;
218
225
  }
219
226
 
227
+ VALUE create_submsg_from_hash(const upb_fielddef *f, VALUE hash) {
228
+ const upb_def *d = upb_fielddef_subdef(f);
229
+ assert(d != NULL);
230
+
231
+ VALUE descriptor = get_def_obj(d);
232
+ VALUE msgclass = rb_funcall(descriptor, rb_intern("msgclass"), 0, NULL);
233
+
234
+ VALUE args[1] = { hash };
235
+ return rb_class_new_instance(1, args, msgclass);
236
+ }
237
+
220
238
  int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
221
239
  MessageHeader* self;
222
- VALUE method_str;
223
- char* name;
240
+ char *name;
224
241
  const upb_fielddef* f;
225
242
  TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
226
243
 
227
- if (!SYMBOL_P(key)) {
244
+ if (TYPE(key) == T_STRING) {
245
+ name = RSTRING_PTR(key);
246
+ } else if (TYPE(key) == T_SYMBOL) {
247
+ name = RSTRING_PTR(rb_id2str(SYM2ID(key)));
248
+ } else {
228
249
  rb_raise(rb_eArgError,
229
- "Expected symbols as hash keys in initialization map.");
250
+ "Expected string or symbols as hash keys when initializing proto from hash.");
230
251
  }
231
252
 
232
- method_str = rb_id2str(SYM2ID(key));
233
- name = RSTRING_PTR(method_str);
234
253
  f = upb_msgdef_ntofz(self->descriptor->msgdef, name);
235
254
  if (f == NULL) {
236
255
  rb_raise(rb_eArgError,
@@ -255,9 +274,18 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
255
274
  }
256
275
  ary = layout_get(self->descriptor->layout, Message_data(self), f);
257
276
  for (int i = 0; i < RARRAY_LEN(val); i++) {
258
- RepeatedField_push(ary, rb_ary_entry(val, i));
277
+ VALUE entry = rb_ary_entry(val, i);
278
+ if (TYPE(entry) == T_HASH && upb_fielddef_issubmsg(f)) {
279
+ entry = create_submsg_from_hash(f, entry);
280
+ }
281
+
282
+ RepeatedField_push(ary, entry);
259
283
  }
260
284
  } else {
285
+ if (TYPE(val) == T_HASH && upb_fielddef_issubmsg(f)) {
286
+ val = create_submsg_from_hash(f, val);
287
+ }
288
+
261
289
  layout_set(self->descriptor->layout, Message_data(self), f, val);
262
290
  }
263
291
  return 0;
@@ -419,6 +447,13 @@ VALUE Message_to_h(VALUE _self) {
419
447
  msg_value = Map_to_h(msg_value);
420
448
  } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
421
449
  msg_value = RepeatedField_to_ary(msg_value);
450
+
451
+ if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) {
452
+ for (int i = 0; i < RARRAY_LEN(msg_value); i++) {
453
+ VALUE elem = rb_ary_entry(msg_value, i);
454
+ rb_ary_store(msg_value, i, Message_to_h(elem));
455
+ }
456
+ }
422
457
  } else if (msg_value != Qnil &&
423
458
  upb_fielddef_type(field) == UPB_TYPE_MESSAGE) {
424
459
  msg_value = Message_to_h(msg_value);
@@ -110,6 +110,6 @@ void Init_protobuf_c() {
110
110
  kRubyStringASCIIEncoding = rb_usascii_encoding();
111
111
  kRubyString8bitEncoding = rb_ascii8bit_encoding();
112
112
 
113
- upb_def_to_ruby_obj_map = rb_hash_new();
114
113
  rb_gc_register_address(&upb_def_to_ruby_obj_map);
114
+ upb_def_to_ruby_obj_map = rb_hash_new();
115
115
  }
@@ -116,10 +116,6 @@ struct Descriptor {
116
116
  const upb_handlers* pb_serialize_handlers;
117
117
  const upb_handlers* json_serialize_handlers;
118
118
  const upb_handlers* json_serialize_handlers_preserve;
119
- // Handlers hold type class references for sub-message fields directly in some
120
- // cases. We need to keep these rooted because they might otherwise be
121
- // collected.
122
- VALUE typeclass_references;
123
119
  };
124
120
 
125
121
  struct FieldDescriptor {
@@ -280,6 +276,7 @@ void Builder_free(void* _self);
280
276
  VALUE Builder_alloc(VALUE klass);
281
277
  void Builder_register(VALUE module);
282
278
  Builder* ruby_to_Builder(VALUE value);
279
+ VALUE Builder_initialize(VALUE _self);
283
280
  VALUE Builder_add_message(VALUE _self, VALUE name);
284
281
  VALUE Builder_add_enum(VALUE _self, VALUE name);
285
282
  VALUE Builder_finalize_to_pool(VALUE _self, VALUE pool_rb);
@@ -478,8 +475,20 @@ VALUE layout_inspect(MessageLayout* layout, void* storage);
478
475
  // Message class creation.
479
476
  // -----------------------------------------------------------------------------
480
477
 
478
+ // This should probably be factored into a common upb component.
479
+
480
+ typedef struct {
481
+ upb_byteshandler handler;
482
+ upb_bytessink sink;
483
+ char *ptr;
484
+ size_t len, size;
485
+ } stringsink;
486
+
487
+ void stringsink_uninit(stringsink *sink);
488
+
481
489
  struct MessageHeader {
482
- Descriptor* descriptor; // kept alive by self.class.descriptor reference.
490
+ Descriptor* descriptor; // kept alive by self.class.descriptor reference.
491
+ stringsink* unknown_fields; // store unknown fields in decoding.
483
492
  // Data comes after this.
484
493
  };
485
494
 
@@ -176,6 +176,15 @@ void native_slot_set_value_and_case(upb_fieldtype_t type, VALUE type_class,
176
176
  break;
177
177
  }
178
178
  case UPB_TYPE_STRING:
179
+ if (CLASS_OF(value) == rb_cSymbol) {
180
+ value = rb_funcall(value, rb_intern("to_s"), 0, NULL);
181
+ } else if (CLASS_OF(value) != rb_cString) {
182
+ rb_raise(rb_eTypeError, "Invalid argument for string field.");
183
+ }
184
+
185
+ DEREF(memory, VALUE) = native_slot_encode_and_freeze_string(type, value);
186
+ break;
187
+
179
188
  case UPB_TYPE_BYTES: {
180
189
  if (CLASS_OF(value) != rb_cString) {
181
190
  rb_raise(rb_eTypeError, "Invalid argument for string field.");
@@ -197,7 +206,9 @@ void native_slot_set_value_and_case(upb_fieldtype_t type, VALUE type_class,
197
206
  }
198
207
  case UPB_TYPE_ENUM: {
199
208
  int32_t int_val = 0;
200
- if (!is_ruby_num(value) && TYPE(value) != T_SYMBOL) {
209
+ if (TYPE(value) == T_STRING) {
210
+ value = rb_funcall(value, rb_intern("to_sym"), 0, NULL);
211
+ } else if (!is_ruby_num(value) && TYPE(value) != T_SYMBOL) {
201
212
  rb_raise(rb_eTypeError,
202
213
  "Expected number or symbol type for enum field.");
203
214
  }