google-protobuf 3.19.4 → 3.25.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/ext/google/protobuf_c/Rakefile +3 -0
  3. data/ext/google/protobuf_c/convert.c +121 -155
  4. data/ext/google/protobuf_c/convert.h +15 -37
  5. data/ext/google/protobuf_c/defs.c +223 -239
  6. data/ext/google/protobuf_c/defs.h +22 -47
  7. data/ext/google/protobuf_c/extconf.rb +13 -5
  8. data/ext/google/protobuf_c/glue.c +21 -0
  9. data/ext/google/protobuf_c/map.c +109 -137
  10. data/ext/google/protobuf_c/map.h +10 -36
  11. data/ext/google/protobuf_c/message.c +445 -386
  12. data/ext/google/protobuf_c/message.h +25 -47
  13. data/ext/google/protobuf_c/protobuf.c +101 -228
  14. data/ext/google/protobuf_c/protobuf.h +37 -42
  15. data/ext/google/protobuf_c/repeated_field.c +91 -113
  16. data/ext/google/protobuf_c/repeated_field.h +9 -34
  17. data/ext/google/protobuf_c/ruby-upb.c +12236 -6993
  18. data/ext/google/protobuf_c/ruby-upb.h +12127 -3787
  19. data/ext/google/protobuf_c/shared_convert.c +64 -0
  20. data/ext/google/protobuf_c/shared_convert.h +26 -0
  21. data/ext/google/protobuf_c/shared_message.c +65 -0
  22. data/ext/google/protobuf_c/shared_message.h +25 -0
  23. data/ext/google/protobuf_c/third_party/utf8_range/LICENSE +22 -0
  24. data/ext/google/protobuf_c/third_party/utf8_range/naive.c +92 -0
  25. data/ext/google/protobuf_c/third_party/utf8_range/range2-neon.c +157 -0
  26. data/ext/google/protobuf_c/third_party/utf8_range/range2-sse.c +170 -0
  27. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.h +21 -0
  28. data/ext/google/protobuf_c/wrap_memcpy.c +7 -29
  29. data/lib/google/protobuf/any_pb.rb +24 -5
  30. data/lib/google/protobuf/api_pb.rb +26 -23
  31. data/lib/google/protobuf/descriptor_dsl.rb +8 -1
  32. data/lib/google/protobuf/descriptor_pb.rb +43 -225
  33. data/lib/google/protobuf/duration_pb.rb +24 -5
  34. data/lib/google/protobuf/empty_pb.rb +24 -3
  35. data/lib/google/protobuf/ffi/descriptor.rb +154 -0
  36. data/lib/google/protobuf/ffi/descriptor_pool.rb +70 -0
  37. data/lib/google/protobuf/ffi/enum_descriptor.rb +161 -0
  38. data/lib/google/protobuf/ffi/ffi.rb +213 -0
  39. data/lib/google/protobuf/ffi/field_descriptor.rb +309 -0
  40. data/lib/google/protobuf/ffi/file_descriptor.rb +48 -0
  41. data/lib/google/protobuf/ffi/internal/arena.rb +66 -0
  42. data/lib/google/protobuf/ffi/internal/convert.rb +305 -0
  43. data/lib/google/protobuf/ffi/internal/pointer_helper.rb +35 -0
  44. data/lib/google/protobuf/ffi/internal/type_safety.rb +25 -0
  45. data/lib/google/protobuf/ffi/map.rb +396 -0
  46. data/lib/google/protobuf/ffi/message.rb +641 -0
  47. data/lib/google/protobuf/ffi/object_cache.rb +30 -0
  48. data/lib/google/protobuf/ffi/oneof_descriptor.rb +88 -0
  49. data/lib/google/protobuf/ffi/repeated_field.rb +503 -0
  50. data/lib/google/protobuf/field_mask_pb.rb +24 -4
  51. data/lib/google/protobuf/message_exts.rb +10 -28
  52. data/lib/google/protobuf/object_cache.rb +97 -0
  53. data/lib/google/protobuf/plugin_pb.rb +47 -0
  54. data/lib/google/protobuf/repeated_field.rb +18 -28
  55. data/lib/google/protobuf/source_context_pb.rb +24 -4
  56. data/lib/google/protobuf/struct_pb.rb +24 -20
  57. data/lib/google/protobuf/timestamp_pb.rb +24 -5
  58. data/lib/google/protobuf/type_pb.rb +26 -68
  59. data/lib/google/protobuf/well_known_types.rb +16 -40
  60. data/lib/google/protobuf/wrappers_pb.rb +24 -28
  61. data/lib/google/protobuf.rb +32 -50
  62. data/lib/google/protobuf_ffi.rb +50 -0
  63. data/lib/google/protobuf_native.rb +20 -0
  64. data/lib/google/tasks/ffi.rake +102 -0
  65. metadata +82 -14
  66. data/tests/basic.rb +0 -648
  67. data/tests/generated_code_test.rb +0 -23
  68. data/tests/stress.rb +0 -38
@@ -1,63 +1,54 @@
1
1
  // Protocol Buffers - Google's data interchange format
2
2
  // Copyright 2014 Google Inc. All rights reserved.
3
- // https://developers.google.com/protocol-buffers/
4
3
  //
5
- // Redistribution and use in source and binary forms, with or without
6
- // modification, are permitted provided that the following conditions are
7
- // met:
8
- //
9
- // * Redistributions of source code must retain the above copyright
10
- // notice, this list of conditions and the following disclaimer.
11
- // * Redistributions in binary form must reproduce the above
12
- // copyright notice, this list of conditions and the following disclaimer
13
- // in the documentation and/or other materials provided with the
14
- // distribution.
15
- // * Neither the name of Google Inc. nor the names of its
16
- // contributors may be used to endorse or promote products derived from
17
- // this software without specific prior written permission.
18
- //
19
- // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
- // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
- // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
- // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
- // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
- // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
- // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
- // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
- // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
- // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
- // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4
+ // Use of this source code is governed by a BSD-style
5
+ // license that can be found in the LICENSE file or at
6
+ // https://developers.google.com/open-source/licenses/bsd
30
7
 
31
8
  #ifndef __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__
32
9
  #define __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__
33
10
 
34
- #include <ruby/ruby.h>
35
- #include <ruby/vm.h>
11
+ // Ruby 3+ defines NDEBUG itself, see: https://bugs.ruby-lang.org/issues/18777
12
+ #ifdef NDEBUG
13
+ #include <ruby.h>
14
+ #else
15
+ #include <ruby.h>
16
+ #undef NDEBUG
17
+ #endif
18
+
19
+ #include <ruby/version.h>
20
+
21
+ #if RUBY_API_VERSION_CODE < 20700
22
+ #error Protobuf requires Ruby >= 2.7
23
+ #endif
24
+
25
+ #include <assert.h> // Must be included after the NDEBUG logic above.
36
26
  #include <ruby/encoding.h>
27
+ #include <ruby/vm.h>
37
28
 
38
- #include "ruby-upb.h"
39
29
  #include "defs.h"
30
+ #include "ruby-upb.h"
40
31
 
41
32
  // These operate on a map field (i.e., a repeated field of submessages whose
42
33
  // submessage type is a map-entry msgdef).
43
- const upb_fielddef* map_field_key(const upb_fielddef* field);
44
- const upb_fielddef* map_field_value(const upb_fielddef* field);
34
+ const upb_FieldDef* map_field_key(const upb_FieldDef* field);
35
+ const upb_FieldDef* map_field_value(const upb_FieldDef* field);
45
36
 
46
37
  // -----------------------------------------------------------------------------
47
38
  // Arena
48
39
  // -----------------------------------------------------------------------------
49
40
 
50
- // A Ruby object that wraps an underlying upb_arena. Any objects that are
41
+ // A Ruby object that wraps an underlying upb_Arena. Any objects that are
51
42
  // allocated from this arena should reference the Arena in rb_gc_mark(), to
52
43
  // ensure that the object's underlying memory outlives any Ruby object that can
53
44
  // reach it.
54
45
 
55
46
  VALUE Arena_new();
56
- upb_arena *Arena_get(VALUE arena);
47
+ upb_Arena* Arena_get(VALUE arena);
57
48
 
58
49
  // Fuses this arena to another, throwing a Ruby exception if this is not
59
50
  // possible.
60
- void Arena_fuse(VALUE arena, upb_arena *other);
51
+ void Arena_fuse(VALUE arena, upb_Arena* other);
61
52
 
62
53
  // Pins this Ruby object to the lifetime of this arena, so that as long as the
63
54
  // arena is alive this object will not be collected.
@@ -76,10 +67,9 @@ void Arena_Pin(VALUE arena, VALUE obj);
76
67
  // being collected (though in Ruby <2.7 is it effectively strong, due to
77
68
  // implementation limitations).
78
69
 
79
- // Adds an entry to the cache. The "arena" parameter must give the arena that
80
- // "key" was allocated from. In Ruby <2.7.0, it will be used to remove the key
81
- // from the cache when the arena is destroyed.
82
- void ObjectCache_Add(const void* key, VALUE val);
70
+ // Tries to add a new entry to the cache, returning the newly installed value or
71
+ // the pre-existing entry.
72
+ VALUE ObjectCache_TryAdd(const void* key, VALUE val);
83
73
 
84
74
  // Returns the cached object for this key, if any. Otherwise returns Qnil.
85
75
  VALUE ObjectCache_Get(const void* key);
@@ -93,10 +83,11 @@ typedef struct StringBuilder StringBuilder;
93
83
 
94
84
  StringBuilder* StringBuilder_New();
95
85
  void StringBuilder_Free(StringBuilder* b);
96
- void StringBuilder_Printf(StringBuilder* b, const char *fmt, ...);
86
+ void StringBuilder_Printf(StringBuilder* b, const char* fmt, ...);
97
87
  VALUE StringBuilder_ToRubyString(StringBuilder* b);
98
88
 
99
- void StringBuilder_PrintMsgval(StringBuilder* b, upb_msgval val, TypeInfo info);
89
+ void StringBuilder_PrintMsgval(StringBuilder* b, upb_MessageValue val,
90
+ TypeInfo info);
100
91
 
101
92
  // -----------------------------------------------------------------------------
102
93
  // Utilities.
@@ -105,9 +96,13 @@ void StringBuilder_PrintMsgval(StringBuilder* b, upb_msgval val, TypeInfo info);
105
96
  extern VALUE cTypeError;
106
97
 
107
98
  #ifdef NDEBUG
108
- #define PBRUBY_ASSERT(expr) do {} while (false && (expr))
99
+ #define PBRUBY_ASSERT(expr) \
100
+ do { \
101
+ } while (false && (expr))
109
102
  #else
110
- #define PBRUBY_ASSERT(expr) assert(expr)
103
+ #define PBRUBY_ASSERT(expr) \
104
+ if (!(expr)) \
105
+ rb_bug("Assertion failed at %s:%d, expr: %s", __FILE__, __LINE__, #expr)
111
106
  #endif
112
107
 
113
108
  #define PBRUBY_MAX(x, y) (((x) > (y)) ? (x) : (y))
@@ -1,32 +1,9 @@
1
1
  // Protocol Buffers - Google's data interchange format
2
2
  // Copyright 2014 Google Inc. All rights reserved.
3
- // https://developers.google.com/protocol-buffers/
4
3
  //
5
- // Redistribution and use in source and binary forms, with or without
6
- // modification, are permitted provided that the following conditions are
7
- // met:
8
- //
9
- // * Redistributions of source code must retain the above copyright
10
- // notice, this list of conditions and the following disclaimer.
11
- // * Redistributions in binary form must reproduce the above
12
- // copyright notice, this list of conditions and the following disclaimer
13
- // in the documentation and/or other materials provided with the
14
- // distribution.
15
- // * Neither the name of Google Inc. nor the names of its
16
- // contributors may be used to endorse or promote products derived from
17
- // this software without specific prior written permission.
18
- //
19
- // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
- // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
- // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
- // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
- // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
- // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
- // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
- // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
- // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
- // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
- // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4
+ // Use of this source code is governed by a BSD-style
5
+ // license that can be found in the LICENSE file or at
6
+ // https://developers.google.com/open-source/licenses/bsd
30
7
 
31
8
  #include "repeated_field.h"
32
9
 
@@ -40,10 +17,10 @@
40
17
  // -----------------------------------------------------------------------------
41
18
 
42
19
  typedef struct {
43
- const upb_array *array; // Can get as mutable when non-frozen.
20
+ const upb_Array* array; // Can get as mutable when non-frozen.
44
21
  TypeInfo type_info;
45
22
  VALUE type_class; // To GC-root the msgdef/enumdef in type_info.
46
- VALUE arena; // To GC-root the upb_array.
23
+ VALUE arena; // To GC-root the upb_Array.
47
24
  } RepeatedField;
48
25
 
49
26
  VALUE cRepeatedField;
@@ -55,9 +32,9 @@ static void RepeatedField_mark(void* _self) {
55
32
  }
56
33
 
57
34
  const rb_data_type_t RepeatedField_type = {
58
- "Google::Protobuf::RepeatedField",
59
- { RepeatedField_mark, RUBY_DEFAULT_FREE, NULL },
60
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
35
+ "Google::Protobuf::RepeatedField",
36
+ {RepeatedField_mark, RUBY_DEFAULT_FREE, NULL},
37
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
61
38
  };
62
39
 
63
40
  static RepeatedField* ruby_to_RepeatedField(VALUE _self) {
@@ -66,9 +43,9 @@ static RepeatedField* ruby_to_RepeatedField(VALUE _self) {
66
43
  return self;
67
44
  }
68
45
 
69
- static upb_array *RepeatedField_GetMutable(VALUE _self) {
46
+ static upb_Array* RepeatedField_GetMutable(VALUE _self) {
70
47
  rb_check_frozen(_self);
71
- return (upb_array*)ruby_to_RepeatedField(_self)->array;
48
+ return (upb_Array*)ruby_to_RepeatedField(_self)->array;
72
49
  }
73
50
 
74
51
  VALUE RepeatedField_alloc(VALUE klass) {
@@ -79,7 +56,7 @@ VALUE RepeatedField_alloc(VALUE klass) {
79
56
  return TypedData_Wrap_Struct(klass, &RepeatedField_type, self);
80
57
  }
81
58
 
82
- VALUE RepeatedField_GetRubyWrapper(upb_array* array, TypeInfo type_info,
59
+ VALUE RepeatedField_GetRubyWrapper(upb_Array* array, TypeInfo type_info,
83
60
  VALUE arena) {
84
61
  PBRUBY_ASSERT(array);
85
62
  VALUE val = ObjectCache_Get(array);
@@ -87,42 +64,44 @@ VALUE RepeatedField_GetRubyWrapper(upb_array* array, TypeInfo type_info,
87
64
  if (val == Qnil) {
88
65
  val = RepeatedField_alloc(cRepeatedField);
89
66
  RepeatedField* self;
90
- ObjectCache_Add(array, val);
91
67
  TypedData_Get_Struct(val, RepeatedField, &RepeatedField_type, self);
92
68
  self->array = array;
93
69
  self->arena = arena;
94
70
  self->type_info = type_info;
95
- if (self->type_info.type == UPB_TYPE_MESSAGE) {
71
+ if (self->type_info.type == kUpb_CType_Message) {
96
72
  self->type_class = Descriptor_DefToClass(type_info.def.msgdef);
97
73
  }
74
+ val = ObjectCache_TryAdd(array, val);
98
75
  }
99
76
 
100
77
  PBRUBY_ASSERT(ruby_to_RepeatedField(val)->type_info.type == type_info.type);
101
78
  PBRUBY_ASSERT(ruby_to_RepeatedField(val)->type_info.def.msgdef ==
102
79
  type_info.def.msgdef);
80
+ PBRUBY_ASSERT(ruby_to_RepeatedField(val)->array == array);
81
+
103
82
  return val;
104
83
  }
105
84
 
106
85
  static VALUE RepeatedField_new_this_type(RepeatedField* from) {
107
86
  VALUE arena_rb = Arena_new();
108
- upb_array *array = upb_array_new(Arena_get(arena_rb), from->type_info.type);
87
+ upb_Array* array = upb_Array_New(Arena_get(arena_rb), from->type_info.type);
109
88
  VALUE ret = RepeatedField_GetRubyWrapper(array, from->type_info, arena_rb);
110
89
  PBRUBY_ASSERT(ruby_to_RepeatedField(ret)->type_class == from->type_class);
111
90
  return ret;
112
91
  }
113
92
 
114
- void RepeatedField_Inspect(StringBuilder* b, const upb_array* array,
93
+ void RepeatedField_Inspect(StringBuilder* b, const upb_Array* array,
115
94
  TypeInfo info) {
116
95
  bool first = true;
117
96
  StringBuilder_Printf(b, "[");
118
- size_t n = array ? upb_array_size(array) : 0;
97
+ size_t n = array ? upb_Array_Size(array) : 0;
119
98
  for (size_t i = 0; i < n; i++) {
120
99
  if (first) {
121
100
  first = false;
122
101
  } else {
123
102
  StringBuilder_Printf(b, ", ");
124
103
  }
125
- StringBuilder_PrintMsgval(b, upb_array_get(array, i), info);
104
+ StringBuilder_PrintMsgval(b, upb_Array_Get(array, i), info);
126
105
  }
127
106
  StringBuilder_Printf(b, "]");
128
107
  }
@@ -132,24 +111,24 @@ VALUE RepeatedField_deep_copy(VALUE _self) {
132
111
  VALUE new_rptfield = RepeatedField_new_this_type(self);
133
112
  RepeatedField* new_self = ruby_to_RepeatedField(new_rptfield);
134
113
  VALUE arena_rb = new_self->arena;
135
- upb_array *new_array = RepeatedField_GetMutable(new_rptfield);
136
- upb_arena *arena = Arena_get(arena_rb);
137
- size_t elements = upb_array_size(self->array);
114
+ upb_Array* new_array = RepeatedField_GetMutable(new_rptfield);
115
+ upb_Arena* arena = Arena_get(arena_rb);
116
+ size_t elements = upb_Array_Size(self->array);
138
117
 
139
- upb_array_resize(new_array, elements, arena);
118
+ upb_Array_Resize(new_array, elements, arena);
140
119
 
141
- size_t size = upb_array_size(self->array);
120
+ size_t size = upb_Array_Size(self->array);
142
121
  for (size_t i = 0; i < size; i++) {
143
- upb_msgval msgval = upb_array_get(self->array, i);
144
- upb_msgval copy = Msgval_DeepCopy(msgval, self->type_info, arena);
145
- upb_array_set(new_array, i, copy);
122
+ upb_MessageValue msgval = upb_Array_Get(self->array, i);
123
+ upb_MessageValue copy = Msgval_DeepCopy(msgval, self->type_info, arena);
124
+ upb_Array_Set(new_array, i, copy);
146
125
  }
147
126
 
148
127
  return new_rptfield;
149
128
  }
150
129
 
151
- const upb_array* RepeatedField_GetUpbArray(VALUE val, const upb_fielddef* field,
152
- upb_arena* arena) {
130
+ const upb_Array* RepeatedField_GetUpbArray(VALUE val, const upb_FieldDef* field,
131
+ upb_Arena* arena) {
153
132
  RepeatedField* self;
154
133
  TypeInfo type_info = TypeInfo_get(field);
155
134
 
@@ -173,17 +152,17 @@ const upb_array* RepeatedField_GetUpbArray(VALUE val, const upb_fielddef* field,
173
152
 
174
153
  static int index_position(VALUE _index, RepeatedField* repeated_field) {
175
154
  int index = NUM2INT(_index);
176
- if (index < 0) index += upb_array_size(repeated_field->array);
155
+ if (index < 0) index += upb_Array_Size(repeated_field->array);
177
156
  return index;
178
157
  }
179
158
 
180
159
  static VALUE RepeatedField_subarray(RepeatedField* self, long beg, long len) {
181
- size_t size = upb_array_size(self->array);
160
+ size_t size = upb_Array_Size(self->array);
182
161
  VALUE ary = rb_ary_new2(size);
183
162
  long i;
184
163
 
185
164
  for (i = beg; i < beg + len; i++) {
186
- upb_msgval msgval = upb_array_get(self->array, i);
165
+ upb_MessageValue msgval = upb_Array_Get(self->array, i);
187
166
  VALUE elem = Convert_UpbToRuby(msgval, self->type_info, self->arena);
188
167
  rb_ary_push(ary, elem);
189
168
  }
@@ -200,18 +179,17 @@ static VALUE RepeatedField_subarray(RepeatedField* self, long beg, long len) {
200
179
  */
201
180
  static VALUE RepeatedField_each(VALUE _self) {
202
181
  RepeatedField* self = ruby_to_RepeatedField(_self);
203
- int size = upb_array_size(self->array);
182
+ int size = upb_Array_Size(self->array);
204
183
  int i;
205
184
 
206
185
  for (i = 0; i < size; i++) {
207
- upb_msgval msgval = upb_array_get(self->array, i);
186
+ upb_MessageValue msgval = upb_Array_Get(self->array, i);
208
187
  VALUE val = Convert_UpbToRuby(msgval, self->type_info, self->arena);
209
188
  rb_yield(val);
210
189
  }
211
190
  return _self;
212
191
  }
213
192
 
214
-
215
193
  /*
216
194
  * call-seq:
217
195
  * RepeatedField.[](index) => value
@@ -220,20 +198,20 @@ static VALUE RepeatedField_each(VALUE _self) {
220
198
  */
221
199
  static VALUE RepeatedField_index(int argc, VALUE* argv, VALUE _self) {
222
200
  RepeatedField* self = ruby_to_RepeatedField(_self);
223
- long size = upb_array_size(self->array);
201
+ long size = upb_Array_Size(self->array);
224
202
 
225
203
  VALUE arg = argv[0];
226
204
  long beg, len;
227
205
 
228
- if (argc == 1){
206
+ if (argc == 1) {
229
207
  if (FIXNUM_P(arg)) {
230
208
  /* standard case */
231
- upb_msgval msgval;
209
+ upb_MessageValue msgval;
232
210
  int index = index_position(argv[0], self);
233
- if (index < 0 || (size_t)index >= upb_array_size(self->array)) {
211
+ if (index < 0 || (size_t)index >= upb_Array_Size(self->array)) {
234
212
  return Qnil;
235
213
  }
236
- msgval = upb_array_get(self->array, index);
214
+ msgval = upb_Array_Get(self->array, index);
237
215
  return Convert_UpbToRuby(msgval, self->type_info, self->arena);
238
216
  } else {
239
217
  /* check if idx is Range */
@@ -269,10 +247,10 @@ static VALUE RepeatedField_index(int argc, VALUE* argv, VALUE _self) {
269
247
  */
270
248
  static VALUE RepeatedField_index_set(VALUE _self, VALUE _index, VALUE val) {
271
249
  RepeatedField* self = ruby_to_RepeatedField(_self);
272
- int size = upb_array_size(self->array);
273
- upb_array *array = RepeatedField_GetMutable(_self);
274
- upb_arena *arena = Arena_get(self->arena);
275
- upb_msgval msgval = Convert_RubyToUpb(val, "", self->type_info, arena);
250
+ int size = upb_Array_Size(self->array);
251
+ upb_Array* array = RepeatedField_GetMutable(_self);
252
+ upb_Arena* arena = Arena_get(self->arena);
253
+ upb_MessageValue msgval = Convert_RubyToUpb(val, "", self->type_info, arena);
276
254
 
277
255
  int index = index_position(_index, self);
278
256
  if (index < 0 || index >= (INT_MAX - 1)) {
@@ -280,17 +258,17 @@ static VALUE RepeatedField_index_set(VALUE _self, VALUE _index, VALUE val) {
280
258
  }
281
259
 
282
260
  if (index >= size) {
283
- upb_array_resize(array, index + 1, arena);
284
- upb_msgval fill;
261
+ upb_Array_Resize(array, index + 1, arena);
262
+ upb_MessageValue fill;
285
263
  memset(&fill, 0, sizeof(fill));
286
264
  for (int i = size; i < index; i++) {
287
265
  // Fill default values.
288
- // TODO(haberman): should this happen at the upb level?
289
- upb_array_set(array, i, fill);
266
+ // TODO: should this happen at the upb level?
267
+ upb_Array_Set(array, i, fill);
290
268
  }
291
269
  }
292
270
 
293
- upb_array_set(array, index, msgval);
271
+ upb_Array_Set(array, index, msgval);
294
272
  return Qnil;
295
273
  }
296
274
 
@@ -302,13 +280,14 @@ static VALUE RepeatedField_index_set(VALUE _self, VALUE _index, VALUE val) {
302
280
  */
303
281
  static VALUE RepeatedField_push_vararg(int argc, VALUE* argv, VALUE _self) {
304
282
  RepeatedField* self = ruby_to_RepeatedField(_self);
305
- upb_arena *arena = Arena_get(self->arena);
306
- upb_array *array = RepeatedField_GetMutable(_self);
283
+ upb_Arena* arena = Arena_get(self->arena);
284
+ upb_Array* array = RepeatedField_GetMutable(_self);
307
285
  int i;
308
286
 
309
287
  for (i = 0; i < argc; i++) {
310
- upb_msgval msgval = Convert_RubyToUpb(argv[i], "", self->type_info, arena);
311
- upb_array_append(array, msgval, arena);
288
+ upb_MessageValue msgval =
289
+ Convert_RubyToUpb(argv[i], "", self->type_info, arena);
290
+ upb_Array_Append(array, msgval, arena);
312
291
  }
313
292
 
314
293
  return _self;
@@ -322,11 +301,11 @@ static VALUE RepeatedField_push_vararg(int argc, VALUE* argv, VALUE _self) {
322
301
  */
323
302
  static VALUE RepeatedField_push(VALUE _self, VALUE val) {
324
303
  RepeatedField* self = ruby_to_RepeatedField(_self);
325
- upb_arena *arena = Arena_get(self->arena);
326
- upb_array *array = RepeatedField_GetMutable(_self);
304
+ upb_Arena* arena = Arena_get(self->arena);
305
+ upb_Array* array = RepeatedField_GetMutable(_self);
327
306
 
328
- upb_msgval msgval = Convert_RubyToUpb(val, "", self->type_info, arena);
329
- upb_array_append(array, msgval, arena);
307
+ upb_MessageValue msgval = Convert_RubyToUpb(val, "", self->type_info, arena);
308
+ upb_Array_Append(array, msgval, arena);
330
309
 
331
310
  return _self;
332
311
  }
@@ -336,19 +315,19 @@ static VALUE RepeatedField_push(VALUE _self, VALUE val) {
336
315
  */
337
316
  static VALUE RepeatedField_pop_one(VALUE _self) {
338
317
  RepeatedField* self = ruby_to_RepeatedField(_self);
339
- size_t size = upb_array_size(self->array);
340
- upb_array *array = RepeatedField_GetMutable(_self);
341
- upb_msgval last;
318
+ size_t size = upb_Array_Size(self->array);
319
+ upb_Array* array = RepeatedField_GetMutable(_self);
320
+ upb_MessageValue last;
342
321
  VALUE ret;
343
322
 
344
323
  if (size == 0) {
345
324
  return Qnil;
346
325
  }
347
326
 
348
- last = upb_array_get(self->array, size - 1);
327
+ last = upb_Array_Get(self->array, size - 1);
349
328
  ret = Convert_UpbToRuby(last, self->type_info, self->arena);
350
329
 
351
- upb_array_resize(array, size - 1, Arena_get(self->arena));
330
+ upb_Array_Resize(array, size - 1, Arena_get(self->arena));
352
331
  return ret;
353
332
  }
354
333
 
@@ -360,11 +339,11 @@ static VALUE RepeatedField_pop_one(VALUE _self) {
360
339
  */
361
340
  static VALUE RepeatedField_replace(VALUE _self, VALUE list) {
362
341
  RepeatedField* self = ruby_to_RepeatedField(_self);
363
- upb_array *array = RepeatedField_GetMutable(_self);
342
+ upb_Array* array = RepeatedField_GetMutable(_self);
364
343
  int i;
365
344
 
366
345
  Check_Type(list, T_ARRAY);
367
- upb_array_resize(array, 0, Arena_get(self->arena));
346
+ upb_Array_Resize(array, 0, Arena_get(self->arena));
368
347
 
369
348
  for (i = 0; i < RARRAY_LEN(list); i++) {
370
349
  RepeatedField_push(_self, rb_ary_entry(list, i));
@@ -381,8 +360,8 @@ static VALUE RepeatedField_replace(VALUE _self, VALUE list) {
381
360
  */
382
361
  static VALUE RepeatedField_clear(VALUE _self) {
383
362
  RepeatedField* self = ruby_to_RepeatedField(_self);
384
- upb_array *array = RepeatedField_GetMutable(_self);
385
- upb_array_resize(array, 0, Arena_get(self->arena));
363
+ upb_Array* array = RepeatedField_GetMutable(_self);
364
+ upb_Array_Resize(array, 0, Arena_get(self->arena));
386
365
  return _self;
387
366
  }
388
367
 
@@ -394,7 +373,7 @@ static VALUE RepeatedField_clear(VALUE _self) {
394
373
  */
395
374
  static VALUE RepeatedField_length(VALUE _self) {
396
375
  RepeatedField* self = ruby_to_RepeatedField(_self);
397
- return INT2NUM(upb_array_size(self->array));
376
+ return INT2NUM(upb_Array_Size(self->array));
398
377
  }
399
378
 
400
379
  /*
@@ -408,16 +387,16 @@ static VALUE RepeatedField_dup(VALUE _self) {
408
387
  RepeatedField* self = ruby_to_RepeatedField(_self);
409
388
  VALUE new_rptfield = RepeatedField_new_this_type(self);
410
389
  RepeatedField* new_rptfield_self = ruby_to_RepeatedField(new_rptfield);
411
- upb_array *new_array = RepeatedField_GetMutable(new_rptfield);
412
- upb_arena* arena = Arena_get(new_rptfield_self->arena);
413
- int size = upb_array_size(self->array);
390
+ upb_Array* new_array = RepeatedField_GetMutable(new_rptfield);
391
+ upb_Arena* arena = Arena_get(new_rptfield_self->arena);
392
+ int size = upb_Array_Size(self->array);
414
393
  int i;
415
394
 
416
395
  Arena_fuse(self->arena, arena);
417
396
 
418
397
  for (i = 0; i < size; i++) {
419
- upb_msgval msgval = upb_array_get(self->array, i);
420
- upb_array_append(new_array, msgval, arena);
398
+ upb_MessageValue msgval = upb_Array_Get(self->array, i);
399
+ upb_Array_Append(new_array, msgval, arena);
421
400
  }
422
401
 
423
402
  return new_rptfield;
@@ -432,12 +411,12 @@ static VALUE RepeatedField_dup(VALUE _self) {
432
411
  */
433
412
  VALUE RepeatedField_to_ary(VALUE _self) {
434
413
  RepeatedField* self = ruby_to_RepeatedField(_self);
435
- int size = upb_array_size(self->array);
414
+ int size = upb_Array_Size(self->array);
436
415
  VALUE ary = rb_ary_new2(size);
437
416
  int i;
438
417
 
439
418
  for (i = 0; i < size; i++) {
440
- upb_msgval msgval = upb_array_get(self->array, i);
419
+ upb_MessageValue msgval = upb_Array_Get(self->array, i);
441
420
  VALUE val = Convert_UpbToRuby(msgval, self->type_info, self->arena);
442
421
  rb_ary_push(ary, val);
443
422
  }
@@ -473,17 +452,17 @@ VALUE RepeatedField_eq(VALUE _self, VALUE _other) {
473
452
 
474
453
  self = ruby_to_RepeatedField(_self);
475
454
  other = ruby_to_RepeatedField(_other);
476
- size_t n = upb_array_size(self->array);
455
+ size_t n = upb_Array_Size(self->array);
477
456
 
478
457
  if (self->type_info.type != other->type_info.type ||
479
458
  self->type_class != other->type_class ||
480
- upb_array_size(other->array) != n) {
459
+ upb_Array_Size(other->array) != n) {
481
460
  return Qfalse;
482
461
  }
483
462
 
484
463
  for (size_t i = 0; i < n; i++) {
485
- upb_msgval val1 = upb_array_get(self->array, i);
486
- upb_msgval val2 = upb_array_get(other->array, i);
464
+ upb_MessageValue val1 = upb_Array_Get(self->array, i);
465
+ upb_MessageValue val2 = upb_Array_Get(other->array, i);
487
466
  if (!Msgval_IsEqual(val1, val2, self->type_info)) {
488
467
  return Qfalse;
489
468
  }
@@ -517,10 +496,10 @@ static VALUE RepeatedField_freeze(VALUE _self) {
517
496
  VALUE RepeatedField_hash(VALUE _self) {
518
497
  RepeatedField* self = ruby_to_RepeatedField(_self);
519
498
  uint64_t hash = 0;
520
- size_t n = upb_array_size(self->array);
499
+ size_t n = upb_Array_Size(self->array);
521
500
 
522
501
  for (size_t i = 0; i < n; i++) {
523
- upb_msgval val = upb_array_get(self->array, i);
502
+ upb_MessageValue val = upb_Array_Get(self->array, i);
524
503
  hash = Msgval_GetHash(val, self->type_info, hash);
525
504
  }
526
505
 
@@ -549,10 +528,10 @@ VALUE RepeatedField_plus(VALUE _self, VALUE list) {
549
528
  RepeatedField* self = ruby_to_RepeatedField(_self);
550
529
  RepeatedField* list_rptfield = ruby_to_RepeatedField(list);
551
530
  RepeatedField* dupped = ruby_to_RepeatedField(dupped_);
552
- upb_array *dupped_array = RepeatedField_GetMutable(dupped_);
553
- upb_arena* arena = Arena_get(dupped->arena);
531
+ upb_Array* dupped_array = RepeatedField_GetMutable(dupped_);
532
+ upb_Arena* arena = Arena_get(dupped->arena);
554
533
  Arena_fuse(list_rptfield->arena, arena);
555
- int size = upb_array_size(list_rptfield->array);
534
+ int size = upb_Array_Size(list_rptfield->array);
556
535
  int i;
557
536
 
558
537
  if (self->type_info.type != list_rptfield->type_info.type ||
@@ -562,8 +541,8 @@ VALUE RepeatedField_plus(VALUE _self, VALUE list) {
562
541
  }
563
542
 
564
543
  for (i = 0; i < size; i++) {
565
- upb_msgval msgval = upb_array_get(list_rptfield->array, i);
566
- upb_array_append(dupped_array, msgval, arena);
544
+ upb_MessageValue msgval = upb_Array_Get(list_rptfield->array, i);
545
+ upb_Array_Append(dupped_array, msgval, arena);
567
546
  }
568
547
  } else {
569
548
  rb_raise(rb_eArgError, "Unknown type appending to RepeatedField");
@@ -601,7 +580,7 @@ VALUE RepeatedField_concat(VALUE _self, VALUE list) {
601
580
  */
602
581
  VALUE RepeatedField_init(int argc, VALUE* argv, VALUE _self) {
603
582
  RepeatedField* self = ruby_to_RepeatedField(_self);
604
- upb_arena *arena;
583
+ upb_Arena* arena;
605
584
  VALUE ary = Qnil;
606
585
 
607
586
  self->arena = Arena_new();
@@ -612,8 +591,9 @@ VALUE RepeatedField_init(int argc, VALUE* argv, VALUE _self) {
612
591
  }
613
592
 
614
593
  self->type_info = TypeInfo_FromClass(argc, argv, 0, &self->type_class, &ary);
615
- self->array = upb_array_new(arena, self->type_info.type);
616
- ObjectCache_Add(self->array, _self);
594
+ self->array = upb_Array_New(arena, self->type_info.type);
595
+ VALUE stored_val = ObjectCache_TryAdd(self->array, _self);
596
+ PBRUBY_ASSERT(stored_val == _self);
617
597
 
618
598
  if (ary != Qnil) {
619
599
  if (!RB_TYPE_P(ary, T_ARRAY)) {
@@ -627,14 +607,12 @@ VALUE RepeatedField_init(int argc, VALUE* argv, VALUE _self) {
627
607
  }
628
608
 
629
609
  void RepeatedField_register(VALUE module) {
630
- VALUE klass = rb_define_class_under(
631
- module, "RepeatedField", rb_cObject);
610
+ VALUE klass = rb_define_class_under(module, "RepeatedField", rb_cObject);
632
611
  rb_define_alloc_func(klass, RepeatedField_alloc);
633
612
  rb_gc_register_address(&cRepeatedField);
634
613
  cRepeatedField = klass;
635
614
 
636
- rb_define_method(klass, "initialize",
637
- RepeatedField_init, -1);
615
+ rb_define_method(klass, "initialize", RepeatedField_init, -1);
638
616
  rb_define_method(klass, "each", RepeatedField_each, 0);
639
617
  rb_define_method(klass, "[]", RepeatedField_index, -1);
640
618
  rb_define_method(klass, "at", RepeatedField_index, -1);