google-protobuf 3.14.0-x86-mingw32 → 3.15.0.rc.1-x86-mingw32

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.

@@ -28,49 +28,162 @@
28
28
  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
29
  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
30
 
31
+ #include "repeated_field.h"
32
+
33
+ #include "convert.h"
34
+ #include "defs.h"
35
+ #include "message.h"
31
36
  #include "protobuf.h"
37
+ #include "third_party/wyhash/wyhash.h"
32
38
 
33
39
  // -----------------------------------------------------------------------------
34
40
  // Repeated field container type.
35
41
  // -----------------------------------------------------------------------------
36
42
 
43
+ typedef struct {
44
+ const upb_array *array; // Can get as mutable when non-frozen.
45
+ TypeInfo type_info;
46
+ VALUE type_class; // To GC-root the msgdef/enumdef in type_info.
47
+ VALUE arena; // To GC-root the upb_array.
48
+ } RepeatedField;
49
+
50
+ VALUE cRepeatedField;
51
+
52
+ static void RepeatedField_mark(void* _self) {
53
+ RepeatedField* self = (RepeatedField*)_self;
54
+ rb_gc_mark(self->type_class);
55
+ rb_gc_mark(self->arena);
56
+ }
57
+
37
58
  const rb_data_type_t RepeatedField_type = {
38
59
  "Google::Protobuf::RepeatedField",
39
- { RepeatedField_mark, RepeatedField_free, NULL },
60
+ { RepeatedField_mark, RUBY_DEFAULT_FREE, NULL },
61
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
40
62
  };
41
63
 
42
- VALUE cRepeatedField;
43
-
44
- RepeatedField* ruby_to_RepeatedField(VALUE _self) {
64
+ static RepeatedField* ruby_to_RepeatedField(VALUE _self) {
45
65
  RepeatedField* self;
46
66
  TypedData_Get_Struct(_self, RepeatedField, &RepeatedField_type, self);
47
67
  return self;
48
68
  }
49
69
 
50
- void* RepeatedField_memoryat(RepeatedField* self, int index, int element_size) {
51
- return ((uint8_t *)self->elements) + index * element_size;
70
+ static upb_array *RepeatedField_GetMutable(VALUE _self) {
71
+ rb_check_frozen(_self);
72
+ return (upb_array*)ruby_to_RepeatedField(_self)->array;
73
+ }
74
+
75
+ VALUE RepeatedField_alloc(VALUE klass) {
76
+ RepeatedField* self = ALLOC(RepeatedField);
77
+ self->arena = Qnil;
78
+ self->type_class = Qnil;
79
+ self->array = NULL;
80
+ return TypedData_Wrap_Struct(klass, &RepeatedField_type, self);
81
+ }
82
+
83
+ VALUE RepeatedField_GetRubyWrapper(upb_array* array, TypeInfo type_info,
84
+ VALUE arena) {
85
+ PBRUBY_ASSERT(array);
86
+ VALUE val = ObjectCache_Get(array);
87
+
88
+ if (val == Qnil) {
89
+ val = RepeatedField_alloc(cRepeatedField);
90
+ RepeatedField* self;
91
+ ObjectCache_Add(array, val, Arena_get(arena));
92
+ TypedData_Get_Struct(val, RepeatedField, &RepeatedField_type, self);
93
+ self->array = array;
94
+ self->arena = arena;
95
+ self->type_info = type_info;
96
+ if (self->type_info.type == UPB_TYPE_MESSAGE) {
97
+ self->type_class = Descriptor_DefToClass(type_info.def.msgdef);
98
+ }
99
+ }
100
+
101
+ PBRUBY_ASSERT(ruby_to_RepeatedField(val)->type_info.type == type_info.type);
102
+ PBRUBY_ASSERT(ruby_to_RepeatedField(val)->type_info.def.msgdef ==
103
+ type_info.def.msgdef);
104
+ return val;
105
+ }
106
+
107
+ static VALUE RepeatedField_new_this_type(RepeatedField* from) {
108
+ VALUE arena_rb = Arena_new();
109
+ upb_array *array = upb_array_new(Arena_get(arena_rb), from->type_info.type);
110
+ VALUE ret = RepeatedField_GetRubyWrapper(array, from->type_info, arena_rb);
111
+ PBRUBY_ASSERT(ruby_to_RepeatedField(ret)->type_class == from->type_class);
112
+ return ret;
113
+ }
114
+
115
+ void RepeatedField_Inspect(StringBuilder* b, const upb_array* array,
116
+ TypeInfo info) {
117
+ bool first = true;
118
+ StringBuilder_Printf(b, "[");
119
+ size_t n = array ? upb_array_size(array) : 0;
120
+ for (size_t i = 0; i < n; i++) {
121
+ if (first) {
122
+ first = false;
123
+ } else {
124
+ StringBuilder_Printf(b, ", ");
125
+ }
126
+ StringBuilder_PrintMsgval(b, upb_array_get(array, i), info);
127
+ }
128
+ StringBuilder_Printf(b, "]");
129
+ }
130
+
131
+ VALUE RepeatedField_deep_copy(VALUE _self) {
132
+ RepeatedField* self = ruby_to_RepeatedField(_self);
133
+ VALUE new_rptfield = RepeatedField_new_this_type(self);
134
+ RepeatedField* new_self = ruby_to_RepeatedField(new_rptfield);
135
+ VALUE arena_rb = new_self->arena;
136
+ upb_array *new_array = RepeatedField_GetMutable(new_rptfield);
137
+ upb_arena *arena = Arena_get(arena_rb);
138
+ size_t elements = upb_array_size(self->array);
139
+
140
+ upb_array_resize(new_array, elements, arena);
141
+
142
+ size_t size = upb_array_size(self->array);
143
+ for (size_t i = 0; i < size; i++) {
144
+ upb_msgval msgval = upb_array_get(self->array, i);
145
+ upb_msgval copy = Msgval_DeepCopy(msgval, self->type_info, arena);
146
+ upb_array_set(new_array, i, copy);
147
+ }
148
+
149
+ return new_rptfield;
150
+ }
151
+
152
+ const upb_array* RepeatedField_GetUpbArray(VALUE val, const upb_fielddef *field) {
153
+ RepeatedField* self;
154
+ TypeInfo type_info = TypeInfo_get(field);
155
+
156
+ if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
157
+ RTYPEDDATA_TYPE(val) != &RepeatedField_type) {
158
+ rb_raise(cTypeError, "Expected repeated field array");
159
+ }
160
+
161
+ self = ruby_to_RepeatedField(val);
162
+ if (self->type_info.type != type_info.type) {
163
+ rb_raise(cTypeError, "Repeated field array has wrong element type");
164
+ }
165
+
166
+ if (self->type_info.def.msgdef != type_info.def.msgdef) {
167
+ rb_raise(cTypeError, "Repeated field array has wrong message/enum class");
168
+ }
169
+
170
+ return self->array;
52
171
  }
53
172
 
54
173
  static int index_position(VALUE _index, RepeatedField* repeated_field) {
55
174
  int index = NUM2INT(_index);
56
- if (index < 0 && repeated_field->size > 0) {
57
- index = repeated_field->size + index;
58
- }
175
+ if (index < 0) index += upb_array_size(repeated_field->array);
59
176
  return index;
60
177
  }
61
178
 
62
- VALUE RepeatedField_subarray(VALUE _self, long beg, long len) {
63
- RepeatedField* self = ruby_to_RepeatedField(_self);
64
- int element_size = native_slot_size(self->field_type);
65
- upb_fieldtype_t field_type = self->field_type;
66
- VALUE field_type_class = self->field_type_class;
67
- size_t off = beg * element_size;
68
- VALUE ary = rb_ary_new2(len);
69
- int i;
179
+ static VALUE RepeatedField_subarray(RepeatedField* self, long beg, long len) {
180
+ size_t size = upb_array_size(self->array);
181
+ VALUE ary = rb_ary_new2(size);
182
+ long i;
70
183
 
71
- for (i = beg; i < beg + len; i++, off += element_size) {
72
- void* mem = ((uint8_t *)self->elements) + off;
73
- VALUE elem = native_slot_get(field_type, field_type_class, mem);
184
+ for (i = beg; i < beg + len; i++) {
185
+ upb_msgval msgval = upb_array_get(self->array, i);
186
+ VALUE elem = Convert_UpbToRuby(msgval, self->type_info, self->arena);
74
187
  rb_ary_push(ary, elem);
75
188
  }
76
189
  return ary;
@@ -84,17 +197,14 @@ VALUE RepeatedField_subarray(VALUE _self, long beg, long len) {
84
197
  * also includes Enumerable; combined with this method, the repeated field thus
85
198
  * acts like an ordinary Ruby sequence.
86
199
  */
87
- VALUE RepeatedField_each(VALUE _self) {
200
+ static VALUE RepeatedField_each(VALUE _self) {
88
201
  RepeatedField* self = ruby_to_RepeatedField(_self);
89
- upb_fieldtype_t field_type = self->field_type;
90
- VALUE field_type_class = self->field_type_class;
91
- int element_size = native_slot_size(field_type);
92
- size_t off = 0;
202
+ int size = upb_array_size(self->array);
93
203
  int i;
94
204
 
95
- for (i = 0; i < self->size; i++, off += element_size) {
96
- void* memory = (void *) (((uint8_t *)self->elements) + off);
97
- VALUE val = native_slot_get(field_type, field_type_class, memory);
205
+ for (i = 0; i < size; i++) {
206
+ upb_msgval msgval = upb_array_get(self->array, i);
207
+ VALUE val = Convert_UpbToRuby(msgval, self->type_info, self->arena);
98
208
  rb_yield(val);
99
209
  }
100
210
  return _self;
@@ -107,11 +217,9 @@ VALUE RepeatedField_each(VALUE _self) {
107
217
  *
108
218
  * Accesses the element at the given index. Returns nil on out-of-bounds
109
219
  */
110
- VALUE RepeatedField_index(int argc, VALUE* argv, VALUE _self) {
220
+ static VALUE RepeatedField_index(int argc, VALUE* argv, VALUE _self) {
111
221
  RepeatedField* self = ruby_to_RepeatedField(_self);
112
- int element_size = native_slot_size(self->field_type);
113
- upb_fieldtype_t field_type = self->field_type;
114
- VALUE field_type_class = self->field_type_class;
222
+ long size = upb_array_size(self->array);
115
223
 
116
224
  VALUE arg = argv[0];
117
225
  long beg, len;
@@ -119,35 +227,36 @@ VALUE RepeatedField_index(int argc, VALUE* argv, VALUE _self) {
119
227
  if (argc == 1){
120
228
  if (FIXNUM_P(arg)) {
121
229
  /* standard case */
122
- void* memory;
230
+ upb_msgval msgval;
123
231
  int index = index_position(argv[0], self);
124
- if (index < 0 || index >= self->size) {
232
+ if (index < 0 || (size_t)index >= upb_array_size(self->array)) {
125
233
  return Qnil;
126
234
  }
127
- memory = RepeatedField_memoryat(self, index, element_size);
128
- return native_slot_get(field_type, field_type_class, memory);
129
- }else{
235
+ msgval = upb_array_get(self->array, index);
236
+ return Convert_UpbToRuby(msgval, self->type_info, self->arena);
237
+ } else {
130
238
  /* check if idx is Range */
131
- switch (rb_range_beg_len(arg, &beg, &len, self->size, 0)) {
239
+ switch (rb_range_beg_len(arg, &beg, &len, size, 0)) {
132
240
  case Qfalse:
133
241
  break;
134
242
  case Qnil:
135
243
  return Qnil;
136
244
  default:
137
- return RepeatedField_subarray(_self, beg, len);
245
+ return RepeatedField_subarray(self, beg, len);
138
246
  }
139
247
  }
140
248
  }
249
+
141
250
  /* assume 2 arguments */
142
251
  beg = NUM2LONG(argv[0]);
143
252
  len = NUM2LONG(argv[1]);
144
253
  if (beg < 0) {
145
- beg += self->size;
254
+ beg += size;
146
255
  }
147
- if (beg >= self->size) {
256
+ if (beg >= size) {
148
257
  return Qnil;
149
258
  }
150
- return RepeatedField_subarray(_self, beg, len);
259
+ return RepeatedField_subarray(self, beg, len);
151
260
  }
152
261
 
153
262
  /*
@@ -157,128 +266,88 @@ VALUE RepeatedField_index(int argc, VALUE* argv, VALUE _self) {
157
266
  * Sets the element at the given index. On out-of-bounds assignments, extends
158
267
  * the array and fills the hole (if any) with default values.
159
268
  */
160
- VALUE RepeatedField_index_set(VALUE _self, VALUE _index, VALUE val) {
269
+ static VALUE RepeatedField_index_set(VALUE _self, VALUE _index, VALUE val) {
161
270
  RepeatedField* self = ruby_to_RepeatedField(_self);
162
- upb_fieldtype_t field_type = self->field_type;
163
- VALUE field_type_class = self->field_type_class;
164
- int element_size = native_slot_size(field_type);
165
- void* memory;
271
+ int size = upb_array_size(self->array);
272
+ upb_array *array = RepeatedField_GetMutable(_self);
273
+ upb_arena *arena = Arena_get(self->arena);
274
+ upb_msgval msgval = Convert_RubyToUpb(val, "", self->type_info, arena);
166
275
 
167
276
  int index = index_position(_index, self);
168
277
  if (index < 0 || index >= (INT_MAX - 1)) {
169
278
  return Qnil;
170
279
  }
171
- if (index >= self->size) {
172
- upb_fieldtype_t field_type = self->field_type;
173
- int element_size = native_slot_size(field_type);
174
- int i;
175
280
 
176
- RepeatedField_reserve(self, index + 1);
177
- for (i = self->size; i <= index; i++) {
178
- void* elem = RepeatedField_memoryat(self, i, element_size);
179
- native_slot_init(field_type, elem);
281
+ if (index >= size) {
282
+ upb_array_resize(array, index + 1, arena);
283
+ upb_msgval fill;
284
+ memset(&fill, 0, sizeof(fill));
285
+ for (int i = size; i < index; i++) {
286
+ // Fill default values.
287
+ // TODO(haberman): should this happen at the upb level?
288
+ upb_array_set(array, i, fill);
180
289
  }
181
- self->size = index + 1;
182
290
  }
183
291
 
184
- memory = RepeatedField_memoryat(self, index, element_size);
185
- native_slot_set("", field_type, field_type_class, memory, val);
292
+ upb_array_set(array, index, msgval);
186
293
  return Qnil;
187
294
  }
188
295
 
189
- static int kInitialSize = 8;
190
-
191
- void RepeatedField_reserve(RepeatedField* self, int new_size) {
192
- void* old_elems = self->elements;
193
- int elem_size = native_slot_size(self->field_type);
194
- if (new_size <= self->capacity) {
195
- return;
196
- }
197
- if (self->capacity == 0) {
198
- self->capacity = kInitialSize;
199
- }
200
- while (self->capacity < new_size) {
201
- self->capacity *= 2;
202
- }
203
- self->elements = ALLOC_N(uint8_t, elem_size * self->capacity);
204
- if (old_elems != NULL) {
205
- memcpy(self->elements, old_elems, self->size * elem_size);
206
- xfree(old_elems);
207
- }
208
- }
209
-
210
296
  /*
211
297
  * call-seq:
212
- * RepeatedField.push(value)
298
+ * RepeatedField.push(value, ...)
213
299
  *
214
300
  * Adds a new element to the repeated field.
215
301
  */
216
- VALUE RepeatedField_push(VALUE _self, VALUE val) {
302
+ static VALUE RepeatedField_push_vararg(int argc, VALUE* argv, VALUE _self) {
217
303
  RepeatedField* self = ruby_to_RepeatedField(_self);
218
- upb_fieldtype_t field_type = self->field_type;
219
- int element_size = native_slot_size(field_type);
220
- void* memory;
221
-
222
- RepeatedField_reserve(self, self->size + 1);
223
- memory = (void *) (((uint8_t *)self->elements) + self->size * element_size);
224
- native_slot_set("", field_type, self->field_type_class, memory, val);
225
- // native_slot_set may raise an error; bump size only after set.
226
- self->size++;
227
- return _self;
228
- }
229
-
230
- VALUE RepeatedField_push_vararg(VALUE _self, VALUE args) {
304
+ upb_arena *arena = Arena_get(self->arena);
305
+ upb_array *array = RepeatedField_GetMutable(_self);
231
306
  int i;
232
- for (i = 0; i < RARRAY_LEN(args); i++) {
233
- RepeatedField_push(_self, rb_ary_entry(args, i));
307
+
308
+ for (i = 0; i < argc; i++) {
309
+ upb_msgval msgval = Convert_RubyToUpb(argv[i], "", self->type_info, arena);
310
+ upb_array_append(array, msgval, arena);
234
311
  }
312
+
235
313
  return _self;
236
314
  }
237
315
 
238
- // Used by parsing handlers.
239
- void RepeatedField_push_native(VALUE _self, void* data) {
316
+ /*
317
+ * call-seq:
318
+ * RepeatedField.<<(value)
319
+ *
320
+ * Adds a new element to the repeated field.
321
+ */
322
+ static VALUE RepeatedField_push(VALUE _self, VALUE val) {
240
323
  RepeatedField* self = ruby_to_RepeatedField(_self);
241
- upb_fieldtype_t field_type = self->field_type;
242
- int element_size = native_slot_size(field_type);
243
- void* memory;
244
-
245
- RepeatedField_reserve(self, self->size + 1);
246
- memory = (void *) (((uint8_t *)self->elements) + self->size * element_size);
247
- memcpy(memory, data, element_size);
248
- self->size++;
249
- }
324
+ upb_arena *arena = Arena_get(self->arena);
325
+ upb_array *array = RepeatedField_GetMutable(_self);
250
326
 
251
- void* RepeatedField_index_native(VALUE _self, int index) {
252
- RepeatedField* self = ruby_to_RepeatedField(_self);
253
- upb_fieldtype_t field_type = self->field_type;
254
- int element_size = native_slot_size(field_type);
255
- return RepeatedField_memoryat(self, index, element_size);
256
- }
327
+ upb_msgval msgval = Convert_RubyToUpb(val, "", self->type_info, arena);
328
+ upb_array_append(array, msgval, arena);
257
329
 
258
- int RepeatedField_size(VALUE _self) {
259
- RepeatedField* self = ruby_to_RepeatedField(_self);
260
- return self->size;
330
+ return _self;
261
331
  }
262
332
 
263
333
  /*
264
334
  * Private ruby method, used by RepeatedField.pop
265
335
  */
266
- VALUE RepeatedField_pop_one(VALUE _self) {
336
+ static VALUE RepeatedField_pop_one(VALUE _self) {
267
337
  RepeatedField* self = ruby_to_RepeatedField(_self);
268
- upb_fieldtype_t field_type = self->field_type;
269
- VALUE field_type_class = self->field_type_class;
270
- int element_size = native_slot_size(field_type);
271
- int index;
272
- void* memory;
338
+ size_t size = upb_array_size(self->array);
339
+ upb_array *array = RepeatedField_GetMutable(_self);
340
+ upb_msgval last;
273
341
  VALUE ret;
274
342
 
275
- if (self->size == 0) {
343
+ if (size == 0) {
276
344
  return Qnil;
277
345
  }
278
- index = self->size - 1;
279
- memory = RepeatedField_memoryat(self, index, element_size);
280
- ret = native_slot_get(field_type, field_type_class, memory);
281
- self->size--;
346
+
347
+ last = upb_array_get(self->array, size - 1);
348
+ ret = Convert_UpbToRuby(last, self->type_info, self->arena);
349
+
350
+ upb_array_resize(array, size - 1, Arena_get(self->arena));
282
351
  return ret;
283
352
  }
284
353
 
@@ -288,15 +357,18 @@ VALUE RepeatedField_pop_one(VALUE _self) {
288
357
  *
289
358
  * Replaces the contents of the repeated field with the given list of elements.
290
359
  */
291
- VALUE RepeatedField_replace(VALUE _self, VALUE list) {
360
+ static VALUE RepeatedField_replace(VALUE _self, VALUE list) {
292
361
  RepeatedField* self = ruby_to_RepeatedField(_self);
362
+ upb_array *array = RepeatedField_GetMutable(_self);
293
363
  int i;
294
364
 
295
365
  Check_Type(list, T_ARRAY);
296
- self->size = 0;
366
+ upb_array_resize(array, 0, Arena_get(self->arena));
367
+
297
368
  for (i = 0; i < RARRAY_LEN(list); i++) {
298
369
  RepeatedField_push(_self, rb_ary_entry(list, i));
299
370
  }
371
+
300
372
  return list;
301
373
  }
302
374
 
@@ -306,9 +378,10 @@ VALUE RepeatedField_replace(VALUE _self, VALUE list) {
306
378
  *
307
379
  * Clears (removes all elements from) this repeated field.
308
380
  */
309
- VALUE RepeatedField_clear(VALUE _self) {
381
+ static VALUE RepeatedField_clear(VALUE _self) {
310
382
  RepeatedField* self = ruby_to_RepeatedField(_self);
311
- self->size = 0;
383
+ upb_array *array = RepeatedField_GetMutable(_self);
384
+ upb_array_resize(array, 0, Arena_get(self->arena));
312
385
  return _self;
313
386
  }
314
387
 
@@ -318,23 +391,9 @@ VALUE RepeatedField_clear(VALUE _self) {
318
391
  *
319
392
  * Returns the length of this repeated field.
320
393
  */
321
- VALUE RepeatedField_length(VALUE _self) {
322
- RepeatedField* self = ruby_to_RepeatedField(_self);
323
- return INT2NUM(self->size);
324
- }
325
-
326
- VALUE RepeatedField_new_this_type(VALUE _self) {
394
+ static VALUE RepeatedField_length(VALUE _self) {
327
395
  RepeatedField* self = ruby_to_RepeatedField(_self);
328
- VALUE new_rptfield = Qnil;
329
- VALUE element_type = fieldtype_to_ruby(self->field_type);
330
- if (self->field_type_class != Qnil) {
331
- new_rptfield = rb_funcall(CLASS_OF(_self), rb_intern("new"), 2,
332
- element_type, self->field_type_class);
333
- } else {
334
- new_rptfield = rb_funcall(CLASS_OF(_self), rb_intern("new"), 1,
335
- element_type);
336
- }
337
- return new_rptfield;
396
+ return INT2NUM(upb_array_size(self->array));
338
397
  }
339
398
 
340
399
  /*
@@ -344,42 +403,20 @@ VALUE RepeatedField_new_this_type(VALUE _self) {
344
403
  * Duplicates this repeated field with a shallow copy. References to all
345
404
  * non-primitive element objects (e.g., submessages) are shared.
346
405
  */
347
- VALUE RepeatedField_dup(VALUE _self) {
406
+ static VALUE RepeatedField_dup(VALUE _self) {
348
407
  RepeatedField* self = ruby_to_RepeatedField(_self);
349
- VALUE new_rptfield = RepeatedField_new_this_type(_self);
408
+ VALUE new_rptfield = RepeatedField_new_this_type(self);
350
409
  RepeatedField* new_rptfield_self = ruby_to_RepeatedField(new_rptfield);
351
- upb_fieldtype_t field_type = self->field_type;
352
- size_t elem_size = native_slot_size(field_type);
353
- size_t off = 0;
410
+ upb_array *new_array = RepeatedField_GetMutable(new_rptfield);
411
+ upb_arena* arena = Arena_get(new_rptfield_self->arena);
412
+ int size = upb_array_size(self->array);
354
413
  int i;
355
414
 
356
- RepeatedField_reserve(new_rptfield_self, self->size);
357
- for (i = 0; i < self->size; i++, off += elem_size) {
358
- void* to_mem = (uint8_t *)new_rptfield_self->elements + off;
359
- void* from_mem = (uint8_t *)self->elements + off;
360
- native_slot_dup(field_type, to_mem, from_mem);
361
- new_rptfield_self->size++;
362
- }
363
-
364
- return new_rptfield;
365
- }
415
+ upb_arena_fuse(arena, Arena_get(self->arena));
366
416
 
367
- // Internal only: used by Google::Protobuf.deep_copy.
368
- VALUE RepeatedField_deep_copy(VALUE _self) {
369
- RepeatedField* self = ruby_to_RepeatedField(_self);
370
- VALUE new_rptfield = RepeatedField_new_this_type(_self);
371
- RepeatedField* new_rptfield_self = ruby_to_RepeatedField(new_rptfield);
372
- upb_fieldtype_t field_type = self->field_type;
373
- size_t elem_size = native_slot_size(field_type);
374
- size_t off = 0;
375
- int i;
376
-
377
- RepeatedField_reserve(new_rptfield_self, self->size);
378
- for (i = 0; i < self->size; i++, off += elem_size) {
379
- void* to_mem = (uint8_t *)new_rptfield_self->elements + off;
380
- void* from_mem = (uint8_t *)self->elements + off;
381
- native_slot_deep_copy(field_type, self->field_type_class, to_mem, from_mem);
382
- new_rptfield_self->size++;
417
+ for (i = 0; i < size; i++) {
418
+ upb_msgval msgval = upb_array_get(self->array, i);
419
+ upb_array_append(new_array, msgval, arena);
383
420
  }
384
421
 
385
422
  return new_rptfield;
@@ -394,17 +431,16 @@ VALUE RepeatedField_deep_copy(VALUE _self) {
394
431
  */
395
432
  VALUE RepeatedField_to_ary(VALUE _self) {
396
433
  RepeatedField* self = ruby_to_RepeatedField(_self);
397
- upb_fieldtype_t field_type = self->field_type;
398
- size_t elem_size = native_slot_size(field_type);
399
- size_t off = 0;
400
- VALUE ary = rb_ary_new2(self->size);
434
+ int size = upb_array_size(self->array);
435
+ VALUE ary = rb_ary_new2(size);
401
436
  int i;
402
437
 
403
- for (i = 0; i < self->size; i++, off += elem_size) {
404
- void* mem = ((uint8_t *)self->elements) + off;
405
- VALUE elem = native_slot_get(field_type, self->field_type_class, mem);
406
- rb_ary_push(ary, elem);
438
+ for (i = 0; i < size; i++) {
439
+ upb_msgval msgval = upb_array_get(self->array, i);
440
+ VALUE val = Convert_UpbToRuby(msgval, self->type_info, self->arena);
441
+ rb_ary_push(ary, val);
407
442
  }
443
+
408
444
  return ary;
409
445
  }
410
446
 
@@ -436,28 +472,38 @@ VALUE RepeatedField_eq(VALUE _self, VALUE _other) {
436
472
 
437
473
  self = ruby_to_RepeatedField(_self);
438
474
  other = ruby_to_RepeatedField(_other);
439
- if (self->field_type != other->field_type ||
440
- self->field_type_class != other->field_type_class ||
441
- self->size != other->size) {
475
+ size_t n = upb_array_size(self->array);
476
+
477
+ if (self->type_info.type != other->type_info.type ||
478
+ self->type_class != other->type_class ||
479
+ upb_array_size(other->array) != n) {
442
480
  return Qfalse;
443
481
  }
444
482
 
445
- {
446
- upb_fieldtype_t field_type = self->field_type;
447
- size_t elem_size = native_slot_size(field_type);
448
- size_t off = 0;
449
- int i;
450
-
451
- for (i = 0; i < self->size; i++, off += elem_size) {
452
- void* self_mem = ((uint8_t *)self->elements) + off;
453
- void* other_mem = ((uint8_t *)other->elements) + off;
454
- if (!native_slot_eq(field_type, self->field_type_class, self_mem,
455
- other_mem)) {
456
- return Qfalse;
457
- }
483
+ for (size_t i = 0; i < n; i++) {
484
+ upb_msgval val1 = upb_array_get(self->array, i);
485
+ upb_msgval val2 = upb_array_get(other->array, i);
486
+ if (!Msgval_IsEqual(val1, val2, self->type_info)) {
487
+ return Qfalse;
458
488
  }
459
- return Qtrue;
460
489
  }
490
+
491
+ return Qtrue;
492
+ }
493
+
494
+ /*
495
+ * call-seq:
496
+ * RepeatedField.freeze => self
497
+ *
498
+ * Freezes the repeated field. We have to intercept this so we can pin the Ruby
499
+ * object into memory so we don't forget it's frozen.
500
+ */
501
+ static VALUE RepeatedField_freeze(VALUE _self) {
502
+ RepeatedField* self = ruby_to_RepeatedField(_self);
503
+
504
+ ObjectCache_Pin(self->array, _self, Arena_get(self->arena));
505
+ RB_OBJ_FREEZE(_self);
506
+ return _self;
461
507
  }
462
508
 
463
509
  /*
@@ -468,22 +514,15 @@ VALUE RepeatedField_eq(VALUE _self, VALUE _other) {
468
514
  */
469
515
  VALUE RepeatedField_hash(VALUE _self) {
470
516
  RepeatedField* self = ruby_to_RepeatedField(_self);
471
- st_index_t h = rb_hash_start(0);
472
- VALUE hash_sym = rb_intern("hash");
473
- upb_fieldtype_t field_type = self->field_type;
474
- VALUE field_type_class = self->field_type_class;
475
- size_t elem_size = native_slot_size(field_type);
476
- size_t off = 0;
477
- int i;
517
+ uint64_t hash = 0;
518
+ size_t n = upb_array_size(self->array);
478
519
 
479
- for (i = 0; i < self->size; i++, off += elem_size) {
480
- void* mem = ((uint8_t *)self->elements) + off;
481
- VALUE elem = native_slot_get(field_type, field_type_class, mem);
482
- h = rb_hash_uint(h, NUM2LONG(rb_funcall(elem, hash_sym, 0)));
520
+ for (size_t i = 0; i < n; i++) {
521
+ upb_msgval val = upb_array_get(self->array, i);
522
+ hash = Msgval_GetHash(val, self->type_info, hash);
483
523
  }
484
- h = rb_hash_end(h);
485
524
 
486
- return INT2FIX(h);
525
+ return LL2NUM(hash);
487
526
  }
488
527
 
489
528
  /*
@@ -495,34 +534,39 @@ VALUE RepeatedField_hash(VALUE _self) {
495
534
  * be either another repeated field or a Ruby array.
496
535
  */
497
536
  VALUE RepeatedField_plus(VALUE _self, VALUE list) {
498
- VALUE dupped = RepeatedField_dup(_self);
537
+ VALUE dupped_ = RepeatedField_dup(_self);
499
538
 
500
539
  if (TYPE(list) == T_ARRAY) {
501
540
  int i;
502
541
  for (i = 0; i < RARRAY_LEN(list); i++) {
503
542
  VALUE elem = rb_ary_entry(list, i);
504
- RepeatedField_push(dupped, elem);
543
+ RepeatedField_push(dupped_, elem);
505
544
  }
506
545
  } else if (RB_TYPE_P(list, T_DATA) && RTYPEDDATA_P(list) &&
507
546
  RTYPEDDATA_TYPE(list) == &RepeatedField_type) {
508
547
  RepeatedField* self = ruby_to_RepeatedField(_self);
509
548
  RepeatedField* list_rptfield = ruby_to_RepeatedField(list);
549
+ RepeatedField* dupped = ruby_to_RepeatedField(dupped_);
550
+ upb_array *dupped_array = RepeatedField_GetMutable(dupped_);
551
+ upb_arena* arena = Arena_get(dupped->arena);
552
+ int size = upb_array_size(list_rptfield->array);
510
553
  int i;
511
554
 
512
- if (self->field_type != list_rptfield->field_type ||
513
- self->field_type_class != list_rptfield->field_type_class) {
555
+ if (self->type_info.type != list_rptfield->type_info.type ||
556
+ self->type_class != list_rptfield->type_class) {
514
557
  rb_raise(rb_eArgError,
515
558
  "Attempt to append RepeatedField with different element type.");
516
559
  }
517
- for (i = 0; i < list_rptfield->size; i++) {
518
- void* mem = RepeatedField_index_native(list, i);
519
- RepeatedField_push_native(dupped, mem);
560
+
561
+ for (i = 0; i < size; i++) {
562
+ upb_msgval msgval = upb_array_get(list_rptfield->array, i);
563
+ upb_array_append(dupped_array, msgval, arena);
520
564
  }
521
565
  } else {
522
566
  rb_raise(rb_eArgError, "Unknown type appending to RepeatedField");
523
567
  }
524
568
 
525
- return dupped;
569
+ return dupped_;
526
570
  }
527
571
 
528
572
  /*
@@ -541,116 +585,41 @@ VALUE RepeatedField_concat(VALUE _self, VALUE list) {
541
585
  return _self;
542
586
  }
543
587
 
544
-
545
- void validate_type_class(upb_fieldtype_t type, VALUE klass) {
546
- if (rb_ivar_get(klass, descriptor_instancevar_interned) == Qnil) {
547
- rb_raise(rb_eArgError,
548
- "Type class has no descriptor. Please pass a "
549
- "class or enum as returned by the DescriptorPool.");
550
- }
551
- if (type == UPB_TYPE_MESSAGE) {
552
- VALUE desc = rb_ivar_get(klass, descriptor_instancevar_interned);
553
- if (!RB_TYPE_P(desc, T_DATA) || !RTYPEDDATA_P(desc) ||
554
- RTYPEDDATA_TYPE(desc) != &_Descriptor_type) {
555
- rb_raise(rb_eArgError, "Descriptor has an incorrect type.");
556
- }
557
- if (rb_get_alloc_func(klass) != &Message_alloc) {
558
- rb_raise(rb_eArgError,
559
- "Message class was not returned by the DescriptorPool.");
560
- }
561
- } else if (type == UPB_TYPE_ENUM) {
562
- VALUE enumdesc = rb_ivar_get(klass, descriptor_instancevar_interned);
563
- if (!RB_TYPE_P(enumdesc, T_DATA) || !RTYPEDDATA_P(enumdesc) ||
564
- RTYPEDDATA_TYPE(enumdesc) != &_EnumDescriptor_type) {
565
- rb_raise(rb_eArgError, "Descriptor has an incorrect type.");
566
- }
567
- }
568
- }
569
-
570
- void RepeatedField_init_args(int argc, VALUE* argv,
571
- VALUE _self) {
588
+ /*
589
+ * call-seq:
590
+ * RepeatedField.new(type, type_class = nil, initial_elems = [])
591
+ *
592
+ * Creates a new repeated field. The provided type must be a Ruby symbol, and
593
+ * can take on the same values as those accepted by FieldDescriptor#type=. If
594
+ * the type is :message or :enum, type_class must be non-nil, and must be the
595
+ * Ruby class or module returned by Descriptor#msgclass or
596
+ * EnumDescriptor#enummodule, respectively. An initial list of elements may also
597
+ * be provided.
598
+ */
599
+ VALUE RepeatedField_init(int argc, VALUE* argv, VALUE _self) {
572
600
  RepeatedField* self = ruby_to_RepeatedField(_self);
601
+ upb_arena *arena;
573
602
  VALUE ary = Qnil;
603
+
604
+ self->arena = Arena_new();
605
+ arena = Arena_get(self->arena);
606
+
574
607
  if (argc < 1) {
575
608
  rb_raise(rb_eArgError, "Expected at least 1 argument.");
576
609
  }
577
- self->field_type = ruby_to_fieldtype(argv[0]);
578
610
 
579
- if (self->field_type == UPB_TYPE_MESSAGE ||
580
- self->field_type == UPB_TYPE_ENUM) {
581
- if (argc < 2) {
582
- rb_raise(rb_eArgError, "Expected at least 2 arguments for message/enum.");
583
- }
584
- self->field_type_class = argv[1];
585
- if (argc > 2) {
586
- ary = argv[2];
587
- }
588
- validate_type_class(self->field_type, self->field_type_class);
589
- } else {
590
- if (argc > 2) {
591
- rb_raise(rb_eArgError, "Too many arguments: expected 1 or 2.");
592
- }
593
- if (argc > 1) {
594
- ary = argv[1];
595
- }
596
- }
611
+ self->type_info = TypeInfo_FromClass(argc, argv, 0, &self->type_class, &ary);
612
+ self->array = upb_array_new(arena, self->type_info.type);
613
+ ObjectCache_Add(self->array, _self, arena);
597
614
 
598
615
  if (ary != Qnil) {
599
- int i;
600
-
601
616
  if (!RB_TYPE_P(ary, T_ARRAY)) {
602
617
  rb_raise(rb_eArgError, "Expected array as initialize argument");
603
618
  }
604
- for (i = 0; i < RARRAY_LEN(ary); i++) {
619
+ for (int i = 0; i < RARRAY_LEN(ary); i++) {
605
620
  RepeatedField_push(_self, rb_ary_entry(ary, i));
606
621
  }
607
622
  }
608
- }
609
-
610
- // Mark, free, alloc, init and class setup functions.
611
-
612
- void RepeatedField_mark(void* _self) {
613
- RepeatedField* self = (RepeatedField*)_self;
614
- upb_fieldtype_t field_type = self->field_type;
615
- int element_size = native_slot_size(field_type);
616
- int i;
617
-
618
- rb_gc_mark(self->field_type_class);
619
- for (i = 0; i < self->size; i++) {
620
- void* memory = (((uint8_t *)self->elements) + i * element_size);
621
- native_slot_mark(self->field_type, memory);
622
- }
623
- }
624
-
625
- void RepeatedField_free(void* _self) {
626
- RepeatedField* self = (RepeatedField*)_self;
627
- xfree(self->elements);
628
- xfree(self);
629
- }
630
-
631
- /*
632
- * call-seq:
633
- * RepeatedField.new(type, type_class = nil, initial_elems = [])
634
- *
635
- * Creates a new repeated field. The provided type must be a Ruby symbol, and
636
- * can take on the same values as those accepted by FieldDescriptor#type=. If
637
- * the type is :message or :enum, type_class must be non-nil, and must be the
638
- * Ruby class or module returned by Descriptor#msgclass or
639
- * EnumDescriptor#enummodule, respectively. An initial list of elements may also
640
- * be provided.
641
- */
642
- VALUE RepeatedField_alloc(VALUE klass) {
643
- RepeatedField* self = ALLOC(RepeatedField);
644
- self->elements = NULL;
645
- self->size = 0;
646
- self->capacity = 0;
647
- self->field_type = -1;
648
- self->field_type_class = Qnil;
649
- return TypedData_Wrap_Struct(klass, &RepeatedField_type, self);
650
- }
651
-
652
- VALUE RepeatedField_init(int argc, VALUE* argv, VALUE self) {
653
- RepeatedField_init_args(argc, argv, self);
654
623
  return Qnil;
655
624
  }
656
625
 
@@ -667,7 +636,7 @@ void RepeatedField_register(VALUE module) {
667
636
  rb_define_method(klass, "[]", RepeatedField_index, -1);
668
637
  rb_define_method(klass, "at", RepeatedField_index, -1);
669
638
  rb_define_method(klass, "[]=", RepeatedField_index_set, 2);
670
- rb_define_method(klass, "push", RepeatedField_push_vararg, -2);
639
+ rb_define_method(klass, "push", RepeatedField_push_vararg, -1);
671
640
  rb_define_method(klass, "<<", RepeatedField_push, 1);
672
641
  rb_define_private_method(klass, "pop_one", RepeatedField_pop_one, 0);
673
642
  rb_define_method(klass, "replace", RepeatedField_replace, 1);
@@ -679,6 +648,7 @@ void RepeatedField_register(VALUE module) {
679
648
  rb_define_method(klass, "clone", RepeatedField_dup, 0);
680
649
  rb_define_method(klass, "==", RepeatedField_eq, 1);
681
650
  rb_define_method(klass, "to_ary", RepeatedField_to_ary, 0);
651
+ rb_define_method(klass, "freeze", RepeatedField_freeze, 0);
682
652
  rb_define_method(klass, "hash", RepeatedField_hash, 0);
683
653
  rb_define_method(klass, "+", RepeatedField_plus, 1);
684
654
  rb_define_method(klass, "concat", RepeatedField_concat, 1);