google-protobuf 4.27.2 → 4.35.1

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.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/ext/google/protobuf_c/convert.c +32 -14
  3. data/ext/google/protobuf_c/defs.c +475 -133
  4. data/ext/google/protobuf_c/extconf.rb +20 -10
  5. data/ext/google/protobuf_c/glue.c +63 -0
  6. data/ext/google/protobuf_c/map.c +145 -56
  7. data/ext/google/protobuf_c/map.h +6 -2
  8. data/ext/google/protobuf_c/message.c +180 -97
  9. data/ext/google/protobuf_c/message.h +2 -2
  10. data/ext/google/protobuf_c/protobuf.c +14 -13
  11. data/ext/google/protobuf_c/protobuf.h +3 -15
  12. data/ext/google/protobuf_c/repeated_field.c +134 -53
  13. data/ext/google/protobuf_c/repeated_field.h +5 -1
  14. data/ext/google/protobuf_c/ruby-upb.c +14522 -12698
  15. data/ext/google/protobuf_c/ruby-upb.h +8273 -4619
  16. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.c +22 -282
  17. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.h +2 -1
  18. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range_neon.inc +117 -0
  19. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range_sse.inc +272 -0
  20. data/lib/google/protobuf/any_pb.rb +1 -1
  21. data/lib/google/protobuf/api_pb.rb +2 -2
  22. data/lib/google/protobuf/descriptor_pb.rb +6 -2
  23. data/lib/google/protobuf/duration_pb.rb +1 -1
  24. data/lib/google/protobuf/empty_pb.rb +1 -1
  25. data/lib/google/protobuf/ffi/descriptor.rb +11 -2
  26. data/lib/google/protobuf/ffi/descriptor_pool.rb +7 -1
  27. data/lib/google/protobuf/ffi/enum_descriptor.rb +10 -0
  28. data/lib/google/protobuf/ffi/ffi.rb +5 -2
  29. data/lib/google/protobuf/ffi/field_descriptor.rb +16 -0
  30. data/lib/google/protobuf/ffi/file_descriptor.rb +36 -0
  31. data/lib/google/protobuf/ffi/internal/arena.rb +0 -6
  32. data/lib/google/protobuf/ffi/internal/convert.rb +9 -6
  33. data/lib/google/protobuf/ffi/internal/pointer_helper.rb +2 -1
  34. data/lib/google/protobuf/ffi/map.rb +47 -23
  35. data/lib/google/protobuf/ffi/message.rb +188 -64
  36. data/lib/google/protobuf/ffi/method_descriptor.rb +11 -1
  37. data/lib/google/protobuf/ffi/oneof_descriptor.rb +10 -0
  38. data/lib/google/protobuf/ffi/repeated_field.rb +42 -16
  39. data/lib/google/protobuf/ffi/service_descriptor.rb +11 -1
  40. data/lib/google/protobuf/field_mask_pb.rb +1 -1
  41. data/lib/google/protobuf/message_exts.rb +4 -0
  42. data/lib/google/protobuf/plugin_pb.rb +1 -1
  43. data/lib/google/protobuf/source_context_pb.rb +1 -1
  44. data/lib/google/protobuf/struct_pb.rb +1 -1
  45. data/lib/google/protobuf/timestamp_pb.rb +1 -1
  46. data/lib/google/protobuf/type_pb.rb +1 -1
  47. data/lib/google/protobuf/wrappers_pb.rb +1 -1
  48. data/lib/google/protobuf_ffi.rb +3 -2
  49. data/lib/google/tasks/ffi.rake +1 -1
  50. metadata +29 -34
  51. data/ext/google/protobuf_c/wrap_memcpy.c +0 -29
@@ -44,8 +44,9 @@ static RepeatedField* ruby_to_RepeatedField(VALUE _self) {
44
44
  }
45
45
 
46
46
  static upb_Array* RepeatedField_GetMutable(VALUE _self) {
47
- rb_check_frozen(_self);
48
- return (upb_Array*)ruby_to_RepeatedField(_self)->array;
47
+ const upb_Array* array = ruby_to_RepeatedField(_self)->array;
48
+ Protobuf_CheckNotFrozen(_self, upb_Array_IsFrozen(array));
49
+ return (upb_Array*)array;
49
50
  }
50
51
 
51
52
  VALUE RepeatedField_alloc(VALUE klass) {
@@ -56,9 +57,32 @@ VALUE RepeatedField_alloc(VALUE klass) {
56
57
  return TypedData_Wrap_Struct(klass, &RepeatedField_type, self);
57
58
  }
58
59
 
59
- VALUE RepeatedField_GetRubyWrapper(upb_Array* array, TypeInfo type_info,
60
+ VALUE RepeatedField_EmptyFrozen(const upb_FieldDef* f) {
61
+ PBRUBY_ASSERT(upb_FieldDef_IsRepeated(f));
62
+ VALUE val = ObjectCache_Get(f);
63
+
64
+ if (val == Qnil) {
65
+ val = RepeatedField_alloc(cRepeatedField);
66
+ RepeatedField* self;
67
+ TypedData_Get_Struct(val, RepeatedField, &RepeatedField_type, self);
68
+ self->arena = Arena_new();
69
+ TypeInfo type_info = TypeInfo_get(f);
70
+ self->array = upb_Array_New(Arena_get(self->arena), type_info.type);
71
+ self->type_info = type_info;
72
+ if (self->type_info.type == kUpb_CType_Message) {
73
+ self->type_class = Descriptor_DefToClass(type_info.def.msgdef);
74
+ }
75
+ val = ObjectCache_TryAdd(f, RepeatedField_freeze(val));
76
+ }
77
+ PBRUBY_ASSERT(RB_OBJ_FROZEN(val));
78
+ PBRUBY_ASSERT(upb_Array_IsFrozen(ruby_to_RepeatedField(val)->array));
79
+ return val;
80
+ }
81
+
82
+ VALUE RepeatedField_GetRubyWrapper(const upb_Array* array, TypeInfo type_info,
60
83
  VALUE arena) {
61
84
  PBRUBY_ASSERT(array);
85
+ PBRUBY_ASSERT(arena != Qnil);
62
86
  VALUE val = ObjectCache_Get(array);
63
87
 
64
88
  if (val == Qnil) {
@@ -78,7 +102,6 @@ VALUE RepeatedField_GetRubyWrapper(upb_Array* array, TypeInfo type_info,
78
102
  PBRUBY_ASSERT(ruby_to_RepeatedField(val)->type_info.def.msgdef ==
79
103
  type_info.def.msgdef);
80
104
  PBRUBY_ASSERT(ruby_to_RepeatedField(val)->array == array);
81
-
82
105
  return val;
83
106
  }
84
107
 
@@ -169,13 +192,20 @@ static VALUE RepeatedField_subarray(RepeatedField* self, long beg, long len) {
169
192
  return ary;
170
193
  }
171
194
 
195
+ /**
196
+ * ruby-doc: RepeatedField
197
+ *
198
+ */
199
+
172
200
  /*
173
- * call-seq:
174
- * RepeatedField.each(&block)
201
+ * ruby-doc: RepeatedField#each
175
202
  *
176
203
  * Invokes the block once for each element of the repeated field. RepeatedField
177
204
  * also includes Enumerable; combined with this method, the repeated field thus
178
205
  * acts like an ordinary Ruby sequence.
206
+ *
207
+ * @yield [Object]
208
+ * @return [self]
179
209
  */
180
210
  static VALUE RepeatedField_each(VALUE _self) {
181
211
  RepeatedField* self = ruby_to_RepeatedField(_self);
@@ -191,10 +221,12 @@ static VALUE RepeatedField_each(VALUE _self) {
191
221
  }
192
222
 
193
223
  /*
194
- * call-seq:
195
- * RepeatedField.[](index) => value
224
+ * ruby-doc: RepeatedField#[]
196
225
  *
197
226
  * Accesses the element at the given index. Returns nil on out-of-bounds
227
+ *
228
+ * @param index [Integer]
229
+ * @return [Object,nil]
198
230
  */
199
231
  static VALUE RepeatedField_index(int argc, VALUE* argv, VALUE _self) {
200
232
  RepeatedField* self = ruby_to_RepeatedField(_self);
@@ -239,11 +271,14 @@ static VALUE RepeatedField_index(int argc, VALUE* argv, VALUE _self) {
239
271
  }
240
272
 
241
273
  /*
242
- * call-seq:
243
- * RepeatedField.[]=(index, value)
274
+ * ruby-doc: RepeatedField#[]=
244
275
  *
245
276
  * Sets the element at the given index. On out-of-bounds assignments, extends
246
277
  * the array and fills the hole (if any) with default values.
278
+ *
279
+ * @param index [Integer]
280
+ * @param value [Object
281
+ * @return [nil]
247
282
  */
248
283
  static VALUE RepeatedField_index_set(VALUE _self, VALUE _index, VALUE val) {
249
284
  RepeatedField* self = ruby_to_RepeatedField(_self);
@@ -273,10 +308,12 @@ static VALUE RepeatedField_index_set(VALUE _self, VALUE _index, VALUE val) {
273
308
  }
274
309
 
275
310
  /*
276
- * call-seq:
277
- * RepeatedField.push(value, ...)
311
+ * ruby-doc: RepeatedField#push
278
312
  *
279
313
  * Adds a new element to the repeated field.
314
+ *
315
+ * @param value [Object]
316
+ * @return [self]
280
317
  */
281
318
  static VALUE RepeatedField_push_vararg(int argc, VALUE* argv, VALUE _self) {
282
319
  RepeatedField* self = ruby_to_RepeatedField(_self);
@@ -294,10 +331,12 @@ static VALUE RepeatedField_push_vararg(int argc, VALUE* argv, VALUE _self) {
294
331
  }
295
332
 
296
333
  /*
297
- * call-seq:
298
- * RepeatedField.<<(value)
334
+ * ruby-doc: RepeatedField#<<
299
335
  *
300
336
  * Adds a new element to the repeated field.
337
+ *
338
+ * @param value [Object]
339
+ * @return [self]
301
340
  */
302
341
  static VALUE RepeatedField_push(VALUE _self, VALUE val) {
303
342
  RepeatedField* self = ruby_to_RepeatedField(_self);
@@ -332,10 +371,12 @@ static VALUE RepeatedField_pop_one(VALUE _self) {
332
371
  }
333
372
 
334
373
  /*
335
- * call-seq:
336
- * RepeatedField.replace(list)
374
+ * ruby-doc: RepeatedField#replace
337
375
  *
338
376
  * Replaces the contents of the repeated field with the given list of elements.
377
+ *
378
+ * @param list [Array]
379
+ * @return [Array]
339
380
  */
340
381
  static VALUE RepeatedField_replace(VALUE _self, VALUE list) {
341
382
  RepeatedField* self = ruby_to_RepeatedField(_self);
@@ -353,10 +394,11 @@ static VALUE RepeatedField_replace(VALUE _self, VALUE list) {
353
394
  }
354
395
 
355
396
  /*
356
- * call-seq:
357
- * RepeatedField.clear
397
+ * ruby-doc: RepeatedField#clear
358
398
  *
359
399
  * Clears (removes all elements from) this repeated field.
400
+ *
401
+ * @return [self]
360
402
  */
361
403
  static VALUE RepeatedField_clear(VALUE _self) {
362
404
  RepeatedField* self = ruby_to_RepeatedField(_self);
@@ -366,10 +408,11 @@ static VALUE RepeatedField_clear(VALUE _self) {
366
408
  }
367
409
 
368
410
  /*
369
- * call-seq:
370
- * RepeatedField.length
411
+ * ruby-doc: RepeatedField#length
371
412
  *
372
413
  * Returns the length of this repeated field.
414
+ *
415
+ * @return [Integer]
373
416
  */
374
417
  static VALUE RepeatedField_length(VALUE _self) {
375
418
  RepeatedField* self = ruby_to_RepeatedField(_self);
@@ -377,11 +420,12 @@ static VALUE RepeatedField_length(VALUE _self) {
377
420
  }
378
421
 
379
422
  /*
380
- * call-seq:
381
- * RepeatedField.dup => repeated_field
423
+ * ruby-doc: RepeatedField#dup
382
424
  *
383
425
  * Duplicates this repeated field with a shallow copy. References to all
384
426
  * non-primitive element objects (e.g., submessages) are shared.
427
+ *
428
+ * @return [RepeatedField]
385
429
  */
386
430
  static VALUE RepeatedField_dup(VALUE _self) {
387
431
  RepeatedField* self = ruby_to_RepeatedField(_self);
@@ -403,11 +447,12 @@ static VALUE RepeatedField_dup(VALUE _self) {
403
447
  }
404
448
 
405
449
  /*
406
- * call-seq:
407
- * RepeatedField.to_ary => array
450
+ * ruby-doc: RepeatedField#to_ary
408
451
  *
409
452
  * Used when converted implicitly into array, e.g. compared to an Array.
410
453
  * Also called as a fallback of Object#to_a
454
+ *
455
+ * @return [Array]
411
456
  */
412
457
  VALUE RepeatedField_to_ary(VALUE _self) {
413
458
  RepeatedField* self = ruby_to_RepeatedField(_self);
@@ -425,8 +470,7 @@ VALUE RepeatedField_to_ary(VALUE _self) {
425
470
  }
426
471
 
427
472
  /*
428
- * call-seq:
429
- * RepeatedField.==(other) => boolean
473
+ * ruby-doc: RepeatedField#==
430
474
  *
431
475
  * Compares this repeated field to another. Repeated fields are equal if their
432
476
  * element types are equal, their lengths are equal, and each element is equal.
@@ -436,6 +480,9 @@ VALUE RepeatedField_to_ary(VALUE _self) {
436
480
  * Repeated fields with dissimilar element types are never equal, even if value
437
481
  * comparison (for example, between integers and floats) would have otherwise
438
482
  * indicated that every element has equal value.
483
+ *
484
+ * @param other [RepeatedField]
485
+ * @return [Boolean]
439
486
  */
440
487
  VALUE RepeatedField_eq(VALUE _self, VALUE _other) {
441
488
  RepeatedField* self;
@@ -472,36 +519,59 @@ VALUE RepeatedField_eq(VALUE _self, VALUE _other) {
472
519
  }
473
520
 
474
521
  /*
475
- * call-seq:
476
- * RepeatedField.freeze => self
522
+ * ruby-doc: RepeatedField#frozen?
477
523
  *
478
- * Freezes the repeated field. We have to intercept this so we can pin the Ruby
479
- * object into memory so we don't forget it's frozen.
524
+ * Returns true if the repeated field is frozen in either Ruby or the underlying
525
+ * representation. Freezes the Ruby repeated field object if it is not already
526
+ * frozen in Ruby but it is frozen in the underlying representation.
527
+ *
528
+ * @return [Boolean]
480
529
  */
481
- VALUE RepeatedField_freeze(VALUE _self) {
530
+ VALUE RepeatedField_frozen(VALUE _self) {
482
531
  RepeatedField* self = ruby_to_RepeatedField(_self);
532
+ if (!upb_Array_IsFrozen(self->array)) {
533
+ PBRUBY_ASSERT(!RB_OBJ_FROZEN(_self));
534
+ return Qfalse;
535
+ }
483
536
 
484
- if (RB_OBJ_FROZEN(_self)) return _self;
485
- Arena_Pin(self->arena, _self);
486
- RB_OBJ_FREEZE(_self);
537
+ // Lazily freeze the Ruby wrapper.
538
+ if (!RB_OBJ_FROZEN(_self)) RB_OBJ_FREEZE(_self);
539
+ return Qtrue;
540
+ }
487
541
 
488
- if (self->type_info.type == kUpb_CType_Message) {
489
- int size = upb_Array_Size(self->array);
490
- int i;
491
- for (i = 0; i < size; i++) {
492
- upb_MessageValue msgval = upb_Array_Get(self->array, i);
493
- VALUE val = Convert_UpbToRuby(msgval, self->type_info, self->arena);
494
- Message_freeze(val);
542
+ /*
543
+ * ruby-doc: RepeatedField#freeze
544
+ *
545
+ * Freezes the repeated field object. We have to intercept this so we can freeze
546
+ * the underlying representation, not just the Ruby wrapper.
547
+ *
548
+ * @return [self]
549
+ */
550
+ VALUE RepeatedField_freeze(VALUE _self) {
551
+ RepeatedField* self = ruby_to_RepeatedField(_self);
552
+ if (RB_OBJ_FROZEN(_self)) {
553
+ PBRUBY_ASSERT(upb_Array_IsFrozen(self->array));
554
+ return _self;
555
+ }
556
+
557
+ if (!upb_Array_IsFrozen(self->array)) {
558
+ if (self->type_info.type == kUpb_CType_Message) {
559
+ upb_Array_Freeze(RepeatedField_GetMutable(_self),
560
+ upb_MessageDef_MiniTable(self->type_info.def.msgdef));
561
+ } else {
562
+ upb_Array_Freeze(RepeatedField_GetMutable(_self), NULL);
495
563
  }
496
564
  }
565
+ RB_OBJ_FREEZE(_self);
497
566
  return _self;
498
567
  }
499
568
 
500
569
  /*
501
- * call-seq:
502
- * RepeatedField.hash => hash_value
570
+ * ruby-doc: RepeatedField#hash
503
571
  *
504
572
  * Returns a hash value computed from this repeated field's elements.
573
+ *
574
+ * @return [Integer]
505
575
  */
506
576
  VALUE RepeatedField_hash(VALUE _self) {
507
577
  RepeatedField* self = ruby_to_RepeatedField(_self);
@@ -517,12 +587,14 @@ VALUE RepeatedField_hash(VALUE _self) {
517
587
  }
518
588
 
519
589
  /*
520
- * call-seq:
521
- * RepeatedField.+(other) => repeated field
590
+ * ruby-doc: RepeatedField#+
522
591
  *
523
592
  * Returns a new repeated field that contains the concatenated list of this
524
593
  * repeated field's elements and other's elements. The other (second) list may
525
594
  * be either another repeated field or a Ruby array.
595
+ *
596
+ * @param other [Array,RepeatedField]
597
+ * @return [RepeatedField]
526
598
  */
527
599
  VALUE RepeatedField_plus(VALUE _self, VALUE list) {
528
600
  VALUE dupped_ = RepeatedField_dup(_self);
@@ -562,10 +634,12 @@ VALUE RepeatedField_plus(VALUE _self, VALUE list) {
562
634
  }
563
635
 
564
636
  /*
565
- * call-seq:
566
- * RepeatedField.concat(other) => self
637
+ * ruby-doc: RepeatedField#concat
567
638
  *
568
639
  * concats the passed in array to self. Returns a Ruby array.
640
+ *
641
+ * @param other [RepeatedField]
642
+ * @return [Array]
569
643
  */
570
644
  VALUE RepeatedField_concat(VALUE _self, VALUE list) {
571
645
  int i;
@@ -578,15 +652,21 @@ VALUE RepeatedField_concat(VALUE _self, VALUE list) {
578
652
  }
579
653
 
580
654
  /*
581
- * call-seq:
582
- * RepeatedField.new(type, type_class = nil, initial_elems = [])
655
+ * ruby-doc: RepeatedField#initialize
583
656
  *
584
657
  * Creates a new repeated field. The provided type must be a Ruby symbol, and
585
- * can take on the same values as those accepted by FieldDescriptor#type=. If
658
+ * can take on the same values as those accepted by {FieldDescriptor#type=}. If
586
659
  * the type is :message or :enum, type_class must be non-nil, and must be the
587
- * Ruby class or module returned by Descriptor#msgclass or
588
- * EnumDescriptor#enummodule, respectively. An initial list of elements may also
589
- * be provided.
660
+ * Ruby class or module returned by {Descriptor#msgclass} or
661
+ * {EnumDescriptor#enummodule}, respectively. An initial list of elements may
662
+ * also be provided.
663
+ *
664
+ * @param type [Symbol]
665
+ * @param type_class [Class<AbstractMessage>, Module]
666
+ * @paramdefault type_class nil
667
+ * @param initial_elems [Array]
668
+ * @paramdefault initial_elems []
669
+ * @return [RepeatedField]
590
670
  */
591
671
  VALUE RepeatedField_init(int argc, VALUE* argv, VALUE _self) {
592
672
  RepeatedField* self = ruby_to_RepeatedField(_self);
@@ -640,6 +720,7 @@ void RepeatedField_register(VALUE module) {
640
720
  rb_define_method(klass, "==", RepeatedField_eq, 1);
641
721
  rb_define_method(klass, "to_ary", RepeatedField_to_ary, 0);
642
722
  rb_define_method(klass, "freeze", RepeatedField_freeze, 0);
723
+ rb_define_method(klass, "frozen?", RepeatedField_frozen, 0);
643
724
  rb_define_method(klass, "hash", RepeatedField_hash, 0);
644
725
  rb_define_method(klass, "+", RepeatedField_plus, 1);
645
726
  rb_define_method(klass, "concat", RepeatedField_concat, 1);
@@ -11,9 +11,13 @@
11
11
  #include "protobuf.h"
12
12
  #include "ruby-upb.h"
13
13
 
14
+ // Returns a frozen sentinel Ruby wrapper object for an empty upb_Array of the
15
+ // type specified by the field. Creates one if it doesn't exist.
16
+ VALUE RepeatedField_EmptyFrozen(const upb_FieldDef* f);
17
+
14
18
  // Returns a Ruby wrapper object for the given upb_Array, which will be created
15
19
  // if one does not exist already.
16
- VALUE RepeatedField_GetRubyWrapper(upb_Array* msg, TypeInfo type_info,
20
+ VALUE RepeatedField_GetRubyWrapper(const upb_Array* msg, TypeInfo type_info,
17
21
  VALUE arena);
18
22
 
19
23
  // Gets the underlying upb_Array for this Ruby RepeatedField object, which must