google-protobuf 3.25.2 → 4.26.1

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.

Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/ext/google/protobuf_c/convert.c +7 -4
  3. data/ext/google/protobuf_c/defs.c +40 -27
  4. data/ext/google/protobuf_c/extconf.rb +1 -1
  5. data/ext/google/protobuf_c/map.c +12 -19
  6. data/ext/google/protobuf_c/map.h +1 -1
  7. data/ext/google/protobuf_c/message.c +41 -77
  8. data/ext/google/protobuf_c/message.h +1 -1
  9. data/ext/google/protobuf_c/protobuf.c +19 -6
  10. data/ext/google/protobuf_c/repeated_field.c +6 -15
  11. data/ext/google/protobuf_c/repeated_field.h +1 -1
  12. data/ext/google/protobuf_c/ruby-upb.c +11788 -10795
  13. data/ext/google/protobuf_c/ruby-upb.h +5164 -4242
  14. data/ext/google/protobuf_c/shared_convert.c +5 -3
  15. data/ext/google/protobuf_c/shared_convert.h +2 -2
  16. data/ext/google/protobuf_c/shared_message.c +8 -6
  17. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.c +467 -0
  18. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.h +9 -8
  19. data/lib/google/protobuf/any_pb.rb +1 -22
  20. data/lib/google/protobuf/api_pb.rb +1 -24
  21. data/lib/google/protobuf/descriptor_pb.rb +2 -23
  22. data/lib/google/protobuf/duration_pb.rb +1 -22
  23. data/lib/google/protobuf/empty_pb.rb +1 -22
  24. data/lib/google/protobuf/ffi/descriptor.rb +2 -3
  25. data/lib/google/protobuf/ffi/enum_descriptor.rb +1 -1
  26. data/lib/google/protobuf/ffi/ffi.rb +3 -1
  27. data/lib/google/protobuf/ffi/field_descriptor.rb +10 -1
  28. data/lib/google/protobuf/ffi/file_descriptor.rb +1 -13
  29. data/lib/google/protobuf/ffi/internal/convert.rb +7 -23
  30. data/lib/google/protobuf/ffi/map.rb +13 -11
  31. data/lib/google/protobuf/ffi/message.rb +10 -13
  32. data/lib/google/protobuf/ffi/object_cache.rb +3 -3
  33. data/lib/google/protobuf/ffi/oneof_descriptor.rb +1 -1
  34. data/lib/google/protobuf/ffi/repeated_field.rb +12 -10
  35. data/lib/google/protobuf/field_mask_pb.rb +1 -22
  36. data/lib/google/protobuf/internal/object_cache.rb +99 -0
  37. data/lib/google/protobuf/plugin_pb.rb +2 -24
  38. data/lib/google/protobuf/repeated_field.rb +1 -2
  39. data/lib/google/protobuf/source_context_pb.rb +1 -22
  40. data/lib/google/protobuf/struct_pb.rb +1 -22
  41. data/lib/google/protobuf/timestamp_pb.rb +1 -22
  42. data/lib/google/protobuf/type_pb.rb +1 -24
  43. data/lib/google/protobuf/wrappers_pb.rb +1 -22
  44. data/lib/google/protobuf.rb +1 -1
  45. data/lib/google/protobuf_ffi.rb +1 -2
  46. data/lib/google/protobuf_native.rb +0 -1
  47. data/lib/google/tasks/ffi.rake +1 -3
  48. metadata +8 -11
  49. data/ext/google/protobuf_c/third_party/utf8_range/naive.c +0 -92
  50. data/ext/google/protobuf_c/third_party/utf8_range/range2-neon.c +0 -157
  51. data/ext/google/protobuf_c/third_party/utf8_range/range2-sse.c +0 -170
  52. data/lib/google/protobuf/descriptor_dsl.rb +0 -465
  53. data/lib/google/protobuf/object_cache.rb +0 -97
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ecbdd6c962badd3e6890658b04e4fd82b6885a005f91f56ab6cf4df20d1b9302
4
- data.tar.gz: f9f2f58969fcb2a9dfb3ecbc907d05129c6bf66f772920bc3a67ef0a5388e8af
3
+ metadata.gz: 6b75ef03befb2ebc24da2bd1395901962c7b0ba053ebf3dbf9e48c4414e89452
4
+ data.tar.gz: 1ade6d79bcca36e4dafe613c0392c14d5cbadcb721c49cec0651699750b567a8
5
5
  SHA512:
6
- metadata.gz: ab0941d5808b400e54b12b0fe8b73ff91877d6b87f0fb825c659adc6c946d5fe59cf4d3312c3a46dd91c74c1923d7bc2b2ba369aeba49e66f0a51c62e1d60d2e
7
- data.tar.gz: 6fbbdc1b0b376cea8297627d96addfb8d6cf0b0fc0acd618cc2f282e039565afed133d51e2d4a63369ac3d26376917e5b574bde1eb3a96bd3b56af747bdfa553
6
+ metadata.gz: 87a7972ae24b8f883a5dd5fe215184afa703097f3252bc2a4a7e7306c09bdc6d23ce72ea3b0742b3e60928dc46a71fb85df600fc199802b5b8f0db88806204a9
7
+ data.tar.gz: cdda124179218236560080105ba0ed5f5dde5137a01fb9db8691533cc4bdcb907d076fe24685227928f3f41cdf456c82e9d067f38ed9a01f18289d6e26504467
@@ -141,7 +141,7 @@ upb_MessageValue Convert_RubyToUpb(VALUE value, const char* name,
141
141
  VALUE utf8 = rb_enc_from_encoding(rb_utf8_encoding());
142
142
  if (rb_obj_class(value) == rb_cSymbol) {
143
143
  value = rb_funcall(value, rb_intern("to_s"), 0);
144
- } else if (rb_obj_class(value) != rb_cString) {
144
+ } else if (!rb_obj_is_kind_of(value, rb_cString)) {
145
145
  rb_raise(cTypeError,
146
146
  "Invalid argument for string field '%s' (given %s).", name,
147
147
  rb_class2name(CLASS_OF(value)));
@@ -208,7 +208,8 @@ upb_MessageValue Convert_RubyToUpb(VALUE value, const char* name,
208
208
  }
209
209
  break;
210
210
  default:
211
- break;
211
+ rb_raise(cTypeError,
212
+ "Convert_RubyToUpb(): Unexpected type %d", (int)type_info.type);
212
213
  }
213
214
 
214
215
  return ret;
@@ -296,7 +297,8 @@ bool Msgval_IsEqual(upb_MessageValue val1, upb_MessageValue val2,
296
297
  if (upb_Status_IsOk(&status)) {
297
298
  return return_value;
298
299
  } else {
299
- rb_raise(rb_eRuntimeError, upb_Status_ErrorMessage(&status));
300
+ rb_raise(rb_eRuntimeError, "Msgval_IsEqual(): %s",
301
+ upb_Status_ErrorMessage(&status));
300
302
  }
301
303
  }
302
304
 
@@ -309,6 +311,7 @@ uint64_t Msgval_GetHash(upb_MessageValue val, TypeInfo type_info,
309
311
  if (upb_Status_IsOk(&status)) {
310
312
  return return_value;
311
313
  } else {
312
- rb_raise(rb_eRuntimeError, upb_Status_ErrorMessage(&status));
314
+ rb_raise(rb_eRuntimeError, "Msgval_GetHash(): %s",
315
+ upb_Status_ErrorMessage(&status));
313
316
  }
314
317
  }
@@ -489,7 +489,7 @@ static VALUE FileDescriptor_alloc(VALUE klass) {
489
489
  * call-seq:
490
490
  * FileDescriptor.new => file
491
491
  *
492
- * Returns a new file descriptor. The syntax must be set before it's passed
492
+ * Returns a new file descriptor. May
493
493
  * to a builder.
494
494
  */
495
495
  static VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
@@ -519,28 +519,6 @@ static VALUE FileDescriptor_name(VALUE _self) {
519
519
  return name == NULL ? Qnil : rb_str_new2(name);
520
520
  }
521
521
 
522
- /*
523
- * call-seq:
524
- * FileDescriptor.syntax => syntax
525
- *
526
- * Returns this file descriptors syntax.
527
- *
528
- * Valid syntax versions are:
529
- * :proto2 or :proto3.
530
- */
531
- static VALUE FileDescriptor_syntax(VALUE _self) {
532
- FileDescriptor* self = ruby_to_FileDescriptor(_self);
533
-
534
- switch (upb_FileDef_Syntax(self->filedef)) {
535
- case kUpb_Syntax_Proto3:
536
- return ID2SYM(rb_intern("proto3"));
537
- case kUpb_Syntax_Proto2:
538
- return ID2SYM(rb_intern("proto2"));
539
- default:
540
- return Qnil;
541
- }
542
- }
543
-
544
522
  /*
545
523
  * call-seq:
546
524
  * FileDescriptor.options => options
@@ -564,7 +542,6 @@ static void FileDescriptor_register(VALUE module) {
564
542
  rb_define_alloc_func(klass, FileDescriptor_alloc);
565
543
  rb_define_method(klass, "initialize", FileDescriptor_initialize, 3);
566
544
  rb_define_method(klass, "name", FileDescriptor_name, 0);
567
- rb_define_method(klass, "syntax", FileDescriptor_syntax, 0);
568
545
  rb_define_method(klass, "options", FileDescriptor_options, 0);
569
546
  rb_gc_register_address(&cFileDescriptor);
570
547
  cFileDescriptor = klass;
@@ -736,6 +713,28 @@ static VALUE FieldDescriptor_default(VALUE _self) {
736
713
  return Convert_UpbToRuby(default_val, TypeInfo_get(self->fielddef), Qnil);
737
714
  }
738
715
 
716
+ /*
717
+ * call-seq:
718
+ * FieldDescriptor.has_presence? => bool
719
+ *
720
+ * Returns whether this field tracks presence.
721
+ */
722
+ static VALUE FieldDescriptor_has_presence(VALUE _self) {
723
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
724
+ return upb_FieldDef_HasPresence(self->fielddef) ? Qtrue : Qfalse;
725
+ }
726
+
727
+ /*
728
+ * call-seq:
729
+ * FieldDescriptor.is_packed? => bool
730
+ *
731
+ * Returns whether this is a repeated field that uses packed encoding.
732
+ */
733
+ static VALUE FieldDescriptor_is_packed(VALUE _self) {
734
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
735
+ return upb_FieldDef_IsPacked(self->fielddef) ? Qtrue : Qfalse;
736
+ }
737
+
739
738
  /*
740
739
  * call-seq:
741
740
  * FieldDescriptor.json_name => json_name
@@ -862,7 +861,7 @@ static VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
862
861
  static VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
863
862
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
864
863
  const upb_MessageDef* m;
865
- const upb_MessageDef* msg = Message_Get(msg_rb, &m);
864
+ const upb_Message* msg = Message_Get(msg_rb, &m);
866
865
 
867
866
  if (m != upb_FieldDef_ContainingType(self->fielddef)) {
868
867
  rb_raise(cTypeError, "has method called on wrong message type");
@@ -882,7 +881,7 @@ static VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
882
881
  static VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {
883
882
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
884
883
  const upb_MessageDef* m;
885
- upb_MessageDef* msg = Message_GetMutable(msg_rb, &m);
884
+ upb_Message* msg = Message_GetMutable(msg_rb, &m);
886
885
 
887
886
  if (m != upb_FieldDef_ContainingType(self->fielddef)) {
888
887
  rb_raise(cTypeError, "has method called on wrong message type");
@@ -903,7 +902,7 @@ static VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {
903
902
  static VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value) {
904
903
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
905
904
  const upb_MessageDef* m;
906
- upb_MessageDef* msg = Message_GetMutable(msg_rb, &m);
905
+ upb_Message* msg = Message_GetMutable(msg_rb, &m);
907
906
  upb_Arena* arena = Arena_get(Message_GetArena(msg_rb));
908
907
  upb_MessageValue msgval;
909
908
 
@@ -943,6 +942,8 @@ static void FieldDescriptor_register(VALUE module) {
943
942
  rb_define_method(klass, "name", FieldDescriptor_name, 0);
944
943
  rb_define_method(klass, "type", FieldDescriptor__type, 0);
945
944
  rb_define_method(klass, "default", FieldDescriptor_default, 0);
945
+ rb_define_method(klass, "has_presence?", FieldDescriptor_has_presence, 0);
946
+ rb_define_method(klass, "is_packed?", FieldDescriptor_is_packed, 0);
946
947
  rb_define_method(klass, "json_name", FieldDescriptor_json_name, 0);
947
948
  rb_define_method(klass, "label", FieldDescriptor_label, 0);
948
949
  rb_define_method(klass, "number", FieldDescriptor_number, 0);
@@ -1163,6 +1164,17 @@ static VALUE EnumDescriptor_file_descriptor(VALUE _self) {
1163
1164
  upb_EnumDef_File(self->enumdef));
1164
1165
  }
1165
1166
 
1167
+ /*
1168
+ * call-seq:
1169
+ * EnumDescriptor.is_closed? => bool
1170
+ *
1171
+ * Returns whether this enum is open or closed.
1172
+ */
1173
+ static VALUE EnumDescriptor_is_closed(VALUE _self) {
1174
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1175
+ return upb_EnumDef_IsClosed(self->enumdef) ? Qtrue : Qfalse;
1176
+ }
1177
+
1166
1178
  /*
1167
1179
  * call-seq:
1168
1180
  * EnumDescriptor.name => name
@@ -1275,6 +1287,7 @@ static void EnumDescriptor_register(VALUE module) {
1275
1287
  rb_define_method(klass, "each", EnumDescriptor_each, 0);
1276
1288
  rb_define_method(klass, "enummodule", EnumDescriptor_enummodule, 0);
1277
1289
  rb_define_method(klass, "file_descriptor", EnumDescriptor_file_descriptor, 0);
1290
+ rb_define_method(klass, "is_closed?", EnumDescriptor_is_closed, 0);
1278
1291
  rb_define_method(klass, "options", EnumDescriptor_options, 0);
1279
1292
  rb_include_module(klass, rb_mEnumerable);
1280
1293
  rb_gc_register_address(&cEnumDescriptor);
@@ -22,7 +22,7 @@ $INCFLAGS += " -I$(srcdir)/third_party/utf8_range"
22
22
 
23
23
  $srcs = ["protobuf.c", "convert.c", "defs.c", "message.c",
24
24
  "repeated_field.c", "map.c", "ruby-upb.c", "wrap_memcpy.c",
25
- "naive.c", "range2-neon.c", "range2-sse.c", "shared_convert.c",
25
+ "utf8_range.c", "shared_convert.c",
26
26
  "shared_message.c"]
27
27
 
28
28
  create_makefile(ext_name)
@@ -38,9 +38,11 @@ static void Map_mark(void* _self) {
38
38
  rb_gc_mark(self->arena);
39
39
  }
40
40
 
41
+ static size_t Map_memsize(const void* _self) { return sizeof(Map); }
42
+
41
43
  const rb_data_type_t Map_type = {
42
44
  "Google::Protobuf::Map",
43
- {Map_mark, RUBY_DEFAULT_FREE, NULL},
45
+ {Map_mark, RUBY_DEFAULT_FREE, Map_memsize},
44
46
  .flags = RUBY_TYPED_FREE_IMMEDIATELY,
45
47
  };
46
48
 
@@ -212,7 +214,7 @@ static VALUE Map_merge_into_self(VALUE _self, VALUE hashmap) {
212
214
  Map* self = ruby_to_Map(_self);
213
215
  Map* other = ruby_to_Map(hashmap);
214
216
  upb_Arena* arena = Arena_get(self->arena);
215
- upb_Message* self_msg = Map_GetMutable(_self);
217
+ upb_Map* self_map = Map_GetMutable(_self);
216
218
 
217
219
  Arena_fuse(other->arena, arena);
218
220
 
@@ -225,7 +227,7 @@ static VALUE Map_merge_into_self(VALUE _self, VALUE hashmap) {
225
227
  size_t iter = kUpb_Map_Begin;
226
228
  upb_MessageValue key, val;
227
229
  while (upb_Map_Next(other->map, &key, &val, &iter)) {
228
- upb_Map_Set(self_msg, key, val, arena);
230
+ upb_Map_Set(self_map, key, val, arena);
229
231
  }
230
232
  } else {
231
233
  rb_raise(rb_eArgError, "Unknown type merging into Map");
@@ -444,7 +446,7 @@ static VALUE Map_delete(VALUE _self, VALUE key) {
444
446
  Convert_RubyToUpb(key, "", Map_keyinfo(self), NULL);
445
447
  upb_MessageValue val_upb;
446
448
 
447
- if (upb_Map_Delete(self->map, key_upb, &val_upb)) {
449
+ if (upb_Map_Delete(Map_GetMutable(_self), key_upb, &val_upb)) {
448
450
  return Convert_UpbToRuby(val_upb, self->value_type_info, self->arena);
449
451
  } else {
450
452
  return Qnil;
@@ -563,22 +565,13 @@ VALUE Map_eq(VALUE _self, VALUE _other) {
563
565
  * Freezes the message object. We have to intercept this so we can pin the
564
566
  * Ruby object into memory so we don't forget it's frozen.
565
567
  */
566
- static VALUE Map_freeze(VALUE _self) {
568
+ VALUE Map_freeze(VALUE _self) {
567
569
  Map* self = ruby_to_Map(_self);
568
- if (!RB_OBJ_FROZEN(_self)) {
569
- Arena_Pin(self->arena, _self);
570
- RB_OBJ_FREEZE(_self);
571
- }
572
- return _self;
573
- }
574
570
 
575
- /*
576
- * Deep freezes the map and values recursively.
577
- * Internal use only.
578
- */
579
- VALUE Map_internal_deep_freeze(VALUE _self) {
580
- Map* self = ruby_to_Map(_self);
581
- Map_freeze(_self);
571
+ if (RB_OBJ_FROZEN(_self)) return _self;
572
+ Arena_Pin(self->arena, _self);
573
+ RB_OBJ_FREEZE(_self);
574
+
582
575
  if (self->value_type_info.type == kUpb_CType_Message) {
583
576
  size_t iter = kUpb_Map_Begin;
584
577
  upb_MessageValue key, val;
@@ -586,7 +579,7 @@ VALUE Map_internal_deep_freeze(VALUE _self) {
586
579
  while (upb_Map_Next(self->map, &key, &val, &iter)) {
587
580
  VALUE val_val =
588
581
  Convert_UpbToRuby(val, self->value_type_info, self->arena);
589
- Message_internal_deep_freeze(val_val);
582
+ Message_freeze(val_val);
590
583
  }
591
584
  }
592
585
  return _self;
@@ -39,6 +39,6 @@ extern VALUE cMap;
39
39
  void Map_register(VALUE module);
40
40
 
41
41
  // Recursively freeze map
42
- VALUE Map_internal_deep_freeze(VALUE _self);
42
+ VALUE Map_freeze(VALUE _self);
43
43
 
44
44
  #endif // RUBY_PROTOBUF_MAP_H_
@@ -44,9 +44,11 @@ static void Message_mark(void* _self) {
44
44
  rb_gc_mark(self->arena);
45
45
  }
46
46
 
47
+ static size_t Message_memsize(const void* _self) { return sizeof(Message); }
48
+
47
49
  static rb_data_type_t Message_type = {
48
50
  "Google::Protobuf::Message",
49
- {Message_mark, RUBY_DEFAULT_FREE, NULL},
51
+ {Message_mark, RUBY_DEFAULT_FREE, Message_memsize},
50
52
  .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
51
53
  };
52
54
 
@@ -488,7 +490,8 @@ static int Map_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
488
490
  k = Convert_RubyToUpb(key, "", map_init->key_type, NULL);
489
491
 
490
492
  if (map_init->val_type.type == kUpb_CType_Message && TYPE(val) == T_HASH) {
491
- upb_MiniTable* t = upb_MessageDef_MiniTable(map_init->val_type.def.msgdef);
493
+ const upb_MiniTable* t =
494
+ upb_MessageDef_MiniTable(map_init->val_type.def.msgdef);
492
495
  upb_Message* msg = upb_Message_New(t, map_init->arena);
493
496
  Message_InitFromValue(msg, map_init->val_type.def.msgdef, val,
494
497
  map_init->arena);
@@ -519,7 +522,7 @@ static upb_MessageValue MessageValue_FromValue(VALUE val, TypeInfo info,
519
522
  upb_Arena* arena) {
520
523
  if (info.type == kUpb_CType_Message) {
521
524
  upb_MessageValue msgval;
522
- upb_MiniTable* t = upb_MessageDef_MiniTable(info.def.msgdef);
525
+ const upb_MiniTable* t = upb_MessageDef_MiniTable(info.def.msgdef);
523
526
  upb_Message* msg = upb_Message_New(t, arena);
524
527
  Message_InitFromValue(msg, info.def.msgdef, val, arena);
525
528
  msgval.msg_val = msg;
@@ -635,7 +638,7 @@ static VALUE Message_initialize(int argc, VALUE* argv, VALUE _self) {
635
638
  Message* self = ruby_to_Message(_self);
636
639
  VALUE arena_rb = Arena_new();
637
640
  upb_Arena* arena = Arena_get(arena_rb);
638
- upb_MiniTable* t = upb_MessageDef_MiniTable(self->msgdef);
641
+ const upb_MiniTable* t = upb_MessageDef_MiniTable(self->msgdef);
639
642
  upb_Message* msg = upb_Message_New(t, arena);
640
643
 
641
644
  Message_InitPtr(_self, msg, arena_rb);
@@ -660,11 +663,8 @@ static VALUE Message_dup(VALUE _self) {
660
663
  Message* self = ruby_to_Message(_self);
661
664
  VALUE new_msg = rb_class_new_instance(0, NULL, CLASS_OF(_self));
662
665
  Message* new_msg_self = ruby_to_Message(new_msg);
663
- size_t size = upb_MessageDef_MiniTable(self->msgdef)->size;
664
-
665
- // TODO
666
- // TODO
667
- memcpy((upb_Message*)new_msg_self->msg, self->msg, size);
666
+ const upb_MiniTable* m = upb_MessageDef_MiniTable(self->msgdef);
667
+ upb_Message_ShallowCopy((upb_Message*)new_msg_self->msg, self->msg, m);
668
668
  Arena_fuse(self->arena, Arena_get(new_msg_self->arena));
669
669
  return new_msg;
670
670
  }
@@ -678,7 +678,8 @@ bool Message_Equal(const upb_Message* m1, const upb_Message* m2,
678
678
  if (upb_Status_IsOk(&status)) {
679
679
  return return_value;
680
680
  } else {
681
- rb_raise(cParseError, upb_Status_ErrorMessage(&status));
681
+ rb_raise(cParseError, "Message_Equal(): %s",
682
+ upb_Status_ErrorMessage(&status));
682
683
  }
683
684
  }
684
685
 
@@ -709,7 +710,8 @@ uint64_t Message_Hash(const upb_Message* msg, const upb_MessageDef* m,
709
710
  if (upb_Status_IsOk(&status)) {
710
711
  return return_value;
711
712
  } else {
712
- rb_raise(cParseError, upb_Status_ErrorMessage(&status));
713
+ rb_raise(cParseError, "Message_Hash(): %s",
714
+ upb_Status_ErrorMessage(&status));
713
715
  }
714
716
  }
715
717
 
@@ -766,58 +768,34 @@ static VALUE Message_CreateHash(const upb_Message* msg,
766
768
  if (!msg) return Qnil;
767
769
 
768
770
  VALUE hash = rb_hash_new();
769
- int n = upb_MessageDef_FieldCount(m);
770
- bool is_proto2;
771
-
772
- // We currently have a few behaviors that are specific to proto2.
773
- // This is unfortunate, we should key behaviors off field attributes (like
774
- // whether a field has presence), not proto2 vs. proto3. We should see if we
775
- // can change this without breaking users.
776
- is_proto2 = upb_MessageDef_Syntax(m) == kUpb_Syntax_Proto2;
777
-
778
- for (int i = 0; i < n; i++) {
779
- const upb_FieldDef* field = upb_MessageDef_Field(m, i);
780
- TypeInfo type_info = TypeInfo_get(field);
781
- upb_MessageValue msgval;
782
- VALUE msg_value;
783
- VALUE msg_key;
784
-
785
- if (!is_proto2 && upb_FieldDef_IsSubMessage(field) &&
786
- !upb_FieldDef_IsRepeated(field) &&
787
- !upb_Message_HasFieldByDef(msg, field)) {
788
- // TODO: Legacy behavior, remove when we fix the is_proto2 differences.
789
- msg_key = ID2SYM(rb_intern(upb_FieldDef_Name(field)));
790
- rb_hash_aset(hash, msg_key, Qnil);
791
- continue;
792
- }
771
+ size_t iter = kUpb_Message_Begin;
772
+ const upb_DefPool* pool = upb_FileDef_Pool(upb_MessageDef_File(m));
773
+ const upb_FieldDef* field;
774
+ upb_MessageValue val;
793
775
 
794
- // Do not include fields that are not present (oneof or optional fields).
795
- if (is_proto2 && upb_FieldDef_HasPresence(field) &&
796
- !upb_Message_HasFieldByDef(msg, field)) {
776
+ while (upb_Message_Next(msg, m, pool, &field, &val, &iter)) {
777
+ if (upb_FieldDef_IsExtension(field)) {
778
+ // TODO: allow extensions once we have decided what naming scheme the
779
+ // symbol should use. eg. :"[pkg.ext]"
797
780
  continue;
798
781
  }
799
782
 
800
- msg_key = ID2SYM(rb_intern(upb_FieldDef_Name(field)));
801
- msgval = upb_Message_GetFieldByDef(msg, field);
802
-
803
- // Proto2 omits empty map/repeated filds also.
783
+ TypeInfo type_info = TypeInfo_get(field);
784
+ VALUE msg_value;
804
785
 
805
786
  if (upb_FieldDef_IsMap(field)) {
806
787
  const upb_MessageDef* entry_m = upb_FieldDef_MessageSubDef(field);
807
788
  const upb_FieldDef* key_f = upb_MessageDef_FindFieldByNumber(entry_m, 1);
808
789
  const upb_FieldDef* val_f = upb_MessageDef_FindFieldByNumber(entry_m, 2);
809
790
  upb_CType key_type = upb_FieldDef_CType(key_f);
810
- msg_value = Map_CreateHash(msgval.map_val, key_type, TypeInfo_get(val_f));
791
+ msg_value = Map_CreateHash(val.map_val, key_type, TypeInfo_get(val_f));
811
792
  } else if (upb_FieldDef_IsRepeated(field)) {
812
- if (is_proto2 &&
813
- (!msgval.array_val || upb_Array_Size(msgval.array_val) == 0)) {
814
- continue;
815
- }
816
- msg_value = RepeatedField_CreateArray(msgval.array_val, type_info);
793
+ msg_value = RepeatedField_CreateArray(val.array_val, type_info);
817
794
  } else {
818
- msg_value = Scalar_CreateHash(msgval, type_info);
795
+ msg_value = Scalar_CreateHash(val, type_info);
819
796
  }
820
797
 
798
+ VALUE msg_key = ID2SYM(rb_intern(upb_FieldDef_Name(field)));
821
799
  rb_hash_aset(hash, msg_key, msg_value);
822
800
  }
823
801
 
@@ -850,22 +828,12 @@ static VALUE Message_to_h(VALUE _self) {
850
828
  * Freezes the message object. We have to intercept this so we can pin the
851
829
  * Ruby object into memory so we don't forget it's frozen.
852
830
  */
853
- static VALUE Message_freeze(VALUE _self) {
831
+ VALUE Message_freeze(VALUE _self) {
854
832
  Message* self = ruby_to_Message(_self);
855
- if (!RB_OBJ_FROZEN(_self)) {
856
- Arena_Pin(self->arena, _self);
857
- RB_OBJ_FREEZE(_self);
858
- }
859
- return _self;
860
- }
861
833
 
862
- /*
863
- * Deep freezes the message object recursively.
864
- * Internal use only.
865
- */
866
- VALUE Message_internal_deep_freeze(VALUE _self) {
867
- Message* self = ruby_to_Message(_self);
868
- Message_freeze(_self);
834
+ if (RB_OBJ_FROZEN(_self)) return _self;
835
+ Arena_Pin(self->arena, _self);
836
+ RB_OBJ_FREEZE(_self);
869
837
 
870
838
  int n = upb_MessageDef_FieldCount(self->msgdef);
871
839
  for (int i = 0; i < n; i++) {
@@ -874,11 +842,11 @@ VALUE Message_internal_deep_freeze(VALUE _self) {
874
842
 
875
843
  if (field != Qnil) {
876
844
  if (upb_FieldDef_IsMap(f)) {
877
- Map_internal_deep_freeze(field);
845
+ Map_freeze(field);
878
846
  } else if (upb_FieldDef_IsRepeated(f)) {
879
- RepeatedField_internal_deep_freeze(field);
847
+ RepeatedField_freeze(field);
880
848
  } else if (upb_FieldDef_IsSubMessage(f)) {
881
- Message_internal_deep_freeze(field);
849
+ Message_freeze(field);
882
850
  }
883
851
  }
884
852
  }
@@ -987,7 +955,7 @@ VALUE Message_decode_bytes(int size, const char* bytes, int options,
987
955
  rb_raise(cParseError, "Error occurred during parsing");
988
956
  }
989
957
  if (freeze) {
990
- Message_internal_deep_freeze(msg_rb);
958
+ Message_freeze(msg_rb);
991
959
  }
992
960
  return msg_rb;
993
961
  }
@@ -1009,9 +977,6 @@ static VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass) {
1009
977
  int options = 0;
1010
978
  upb_Status status;
1011
979
 
1012
- // TODO: use this message's pool instead.
1013
- const upb_DefPool* symtab = DescriptorPool_GetSymtab(generated_pool);
1014
-
1015
980
  if (argc < 1 || argc > 2) {
1016
981
  rb_raise(rb_eArgError, "Expected 1 or 2 arguments.");
1017
982
  }
@@ -1045,8 +1010,9 @@ static VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass) {
1045
1010
  }
1046
1011
 
1047
1012
  upb_Status_Clear(&status);
1013
+ const upb_DefPool* pool = upb_FileDef_Pool(upb_MessageDef_File(msg->msgdef));
1048
1014
  if (!upb_JsonDecode(RSTRING_PTR(data), RSTRING_LEN(data),
1049
- (upb_Message*)msg->msg, msg->msgdef, symtab, options,
1015
+ (upb_Message*)msg->msg, msg->msgdef, pool, options,
1050
1016
  Arena_get(msg->arena), &status)) {
1051
1017
  rb_raise(cParseError, "Error occurred during parsing: %s",
1052
1018
  upb_Status_ErrorMessage(&status));
@@ -1125,9 +1091,6 @@ static VALUE Message_encode_json(int argc, VALUE* argv, VALUE klass) {
1125
1091
  size_t size;
1126
1092
  upb_Status status;
1127
1093
 
1128
- // TODO: use this message's pool instead.
1129
- const upb_DefPool* symtab = DescriptorPool_GetSymtab(generated_pool);
1130
-
1131
1094
  if (argc < 1 || argc > 2) {
1132
1095
  rb_raise(rb_eArgError, "Expected 1 or 2 arguments.");
1133
1096
  }
@@ -1162,8 +1125,9 @@ static VALUE Message_encode_json(int argc, VALUE* argv, VALUE klass) {
1162
1125
  }
1163
1126
 
1164
1127
  upb_Status_Clear(&status);
1165
- size = upb_JsonEncode(msg->msg, msg->msgdef, symtab, options, buf,
1166
- sizeof(buf), &status);
1128
+ const upb_DefPool* pool = upb_FileDef_Pool(upb_MessageDef_File(msg->msgdef));
1129
+ size = upb_JsonEncode(msg->msg, msg->msgdef, pool, options, buf, sizeof(buf),
1130
+ &status);
1167
1131
 
1168
1132
  if (!upb_Status_IsOk(&status)) {
1169
1133
  rb_raise(cParseError, "Error occurred during encoding: %s",
@@ -1173,7 +1137,7 @@ static VALUE Message_encode_json(int argc, VALUE* argv, VALUE klass) {
1173
1137
  VALUE ret;
1174
1138
  if (size >= sizeof(buf)) {
1175
1139
  char* buf2 = malloc(size + 1);
1176
- upb_JsonEncode(msg->msg, msg->msgdef, symtab, options, buf2, size + 1,
1140
+ upb_JsonEncode(msg->msg, msg->msgdef, pool, options, buf2, size + 1,
1177
1141
  &status);
1178
1142
  ret = rb_str_new(buf2, size);
1179
1143
  free(buf2);
@@ -78,7 +78,7 @@ VALUE Message_decode_bytes(int size, const char* bytes, int options,
78
78
  VALUE klass, bool freeze);
79
79
 
80
80
  // Recursively freeze message
81
- VALUE Message_internal_deep_freeze(VALUE _self);
81
+ VALUE Message_freeze(VALUE _self);
82
82
 
83
83
  // Call at startup to register all types in this module.
84
84
  void Message_register(VALUE protobuf);
@@ -164,11 +164,23 @@ static void Arena_free(void *data) {
164
164
  xfree(arena);
165
165
  }
166
166
 
167
+ static size_t Arena_memsize(const void *data) {
168
+ const Arena *arena = data;
169
+ size_t fused_count;
170
+ size_t memsize = upb_Arena_SpaceAllocated(arena->arena, &fused_count);
171
+ if (fused_count > 1) {
172
+ // If other arena were fused we attribute an equal
173
+ // share of memory usage to each one.
174
+ memsize /= fused_count;
175
+ }
176
+ return memsize + sizeof(Arena);
177
+ }
178
+
167
179
  static VALUE cArena;
168
180
 
169
181
  const rb_data_type_t Arena_type = {
170
182
  "Google::Protobuf::Internal::Arena",
171
- {Arena_mark, Arena_free, NULL},
183
+ {Arena_mark, Arena_free, Arena_memsize},
172
184
  .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
173
185
  };
174
186
 
@@ -241,16 +253,17 @@ static void ObjectCache_Init(VALUE protobuf) {
241
253
  item_try_add = rb_intern("try_add");
242
254
 
243
255
  rb_gc_register_address(&weak_obj_cache);
256
+ VALUE internal = rb_const_get(protobuf, rb_intern("Internal"));
244
257
  #if SIZEOF_LONG >= SIZEOF_VALUE
245
- VALUE cache_class = rb_const_get(protobuf, rb_intern("ObjectCache"));
258
+ VALUE cache_class = rb_const_get(internal, rb_intern("ObjectCache"));
246
259
  #else
247
- VALUE cache_class = rb_const_get(protobuf, rb_intern("LegacyObjectCache"));
260
+ VALUE cache_class = rb_const_get(internal, rb_intern("LegacyObjectCache"));
248
261
  #endif
249
262
 
250
263
  weak_obj_cache = rb_class_new_instance(0, NULL, cache_class);
251
- rb_const_set(protobuf, rb_intern("OBJECT_CACHE"), weak_obj_cache);
252
- rb_const_set(protobuf, rb_intern("SIZEOF_LONG"), INT2NUM(SIZEOF_LONG));
253
- rb_const_set(protobuf, rb_intern("SIZEOF_VALUE"), INT2NUM(SIZEOF_VALUE));
264
+ rb_const_set(internal, rb_intern("OBJECT_CACHE"), weak_obj_cache);
265
+ rb_const_set(internal, rb_intern("SIZEOF_LONG"), INT2NUM(SIZEOF_LONG));
266
+ rb_const_set(internal, rb_intern("SIZEOF_VALUE"), INT2NUM(SIZEOF_VALUE));
254
267
  }
255
268
 
256
269
  static VALUE ObjectCache_GetKey(const void *key) {
@@ -478,29 +478,20 @@ VALUE RepeatedField_eq(VALUE _self, VALUE _other) {
478
478
  * Freezes the repeated field. We have to intercept this so we can pin the Ruby
479
479
  * object into memory so we don't forget it's frozen.
480
480
  */
481
- static VALUE RepeatedField_freeze(VALUE _self) {
481
+ VALUE RepeatedField_freeze(VALUE _self) {
482
482
  RepeatedField* self = ruby_to_RepeatedField(_self);
483
- if (!RB_OBJ_FROZEN(_self)) {
484
- Arena_Pin(self->arena, _self);
485
- RB_OBJ_FREEZE(_self);
486
- }
487
- return _self;
488
- }
489
483
 
490
- /*
491
- * Deep freezes the repeated field and values recursively.
492
- * Internal use only.
493
- */
494
- VALUE RepeatedField_internal_deep_freeze(VALUE _self) {
495
- RepeatedField* self = ruby_to_RepeatedField(_self);
496
- RepeatedField_freeze(_self);
484
+ if (RB_OBJ_FROZEN(_self)) return _self;
485
+ Arena_Pin(self->arena, _self);
486
+ RB_OBJ_FREEZE(_self);
487
+
497
488
  if (self->type_info.type == kUpb_CType_Message) {
498
489
  int size = upb_Array_Size(self->array);
499
490
  int i;
500
491
  for (i = 0; i < size; i++) {
501
492
  upb_MessageValue msgval = upb_Array_Get(self->array, i);
502
493
  VALUE val = Convert_UpbToRuby(msgval, self->type_info, self->arena);
503
- Message_internal_deep_freeze(val);
494
+ Message_freeze(val);
504
495
  }
505
496
  }
506
497
  return _self;
@@ -36,6 +36,6 @@ extern VALUE cRepeatedField;
36
36
  void RepeatedField_register(VALUE module);
37
37
 
38
38
  // Recursively freeze RepeatedField.
39
- VALUE RepeatedField_internal_deep_freeze(VALUE _self);
39
+ VALUE RepeatedField_freeze(VALUE _self);
40
40
 
41
41
  #endif // RUBY_PROTOBUF_REPEATED_FIELD_H_