google-protobuf-z 3.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,654 @@
1
+ // Protocol Buffers - Google's data interchange format
2
+ // Copyright 2014 Google Inc. All rights reserved.
3
+ // https://developers.google.com/protocol-buffers/
4
+ //
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.
30
+
31
+ #include "protobuf.h"
32
+
33
+ // -----------------------------------------------------------------------------
34
+ // Repeated field container type.
35
+ // -----------------------------------------------------------------------------
36
+
37
+ const rb_data_type_t RepeatedField_type = {
38
+ "Google::Protobuf::RepeatedField",
39
+ { RepeatedField_mark, RepeatedField_free, NULL },
40
+ };
41
+
42
+ VALUE cRepeatedField;
43
+
44
+ RepeatedField* ruby_to_RepeatedField(VALUE _self) {
45
+ RepeatedField* self;
46
+ TypedData_Get_Struct(_self, RepeatedField, &RepeatedField_type, self);
47
+ return self;
48
+ }
49
+
50
+ void* RepeatedField_memoryat(RepeatedField* self, int index, int element_size) {
51
+ return ((uint8_t *)self->elements) + index * element_size;
52
+ }
53
+
54
+ static int index_position(VALUE _index, RepeatedField* repeated_field) {
55
+ int index = NUM2INT(_index);
56
+ if (index < 0 && repeated_field->size > 0) {
57
+ index = repeated_field->size + index;
58
+ }
59
+ return index;
60
+ }
61
+
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
+
68
+ size_t off = beg * element_size;
69
+ VALUE ary = rb_ary_new2(len);
70
+ for (int i = beg; i < beg + len; i++, off += element_size) {
71
+ void* mem = ((uint8_t *)self->elements) + off;
72
+ VALUE elem = native_slot_get(field_type, field_type_class, mem);
73
+ rb_ary_push(ary, elem);
74
+ }
75
+ return ary;
76
+ }
77
+
78
+ /*
79
+ * call-seq:
80
+ * RepeatedField.each(&block)
81
+ *
82
+ * Invokes the block once for each element of the repeated field. RepeatedField
83
+ * also includes Enumerable; combined with this method, the repeated field thus
84
+ * acts like an ordinary Ruby sequence.
85
+ */
86
+ VALUE RepeatedField_each(VALUE _self) {
87
+ RepeatedField* self = ruby_to_RepeatedField(_self);
88
+ upb_fieldtype_t field_type = self->field_type;
89
+ VALUE field_type_class = self->field_type_class;
90
+ int element_size = native_slot_size(field_type);
91
+
92
+ size_t off = 0;
93
+ for (int i = 0; i < self->size; i++, off += element_size) {
94
+ void* memory = (void *) (((uint8_t *)self->elements) + off);
95
+ VALUE val = native_slot_get(field_type, field_type_class, memory);
96
+ rb_yield(val);
97
+ }
98
+ return _self;
99
+ }
100
+
101
+
102
+ /*
103
+ * call-seq:
104
+ * RepeatedField.[](index) => value
105
+ *
106
+ * Accesses the element at the given index. Returns nil on out-of-bounds
107
+ */
108
+ VALUE RepeatedField_index(int argc, VALUE* argv, VALUE _self) {
109
+ RepeatedField* self = ruby_to_RepeatedField(_self);
110
+ int element_size = native_slot_size(self->field_type);
111
+ upb_fieldtype_t field_type = self->field_type;
112
+ VALUE field_type_class = self->field_type_class;
113
+
114
+ VALUE arg = argv[0];
115
+ long beg, len;
116
+
117
+ if (argc == 1){
118
+ if (FIXNUM_P(arg)) {
119
+ /* standard case */
120
+ void* memory;
121
+ int index = index_position(argv[0], self);
122
+ if (index < 0 || index >= self->size) {
123
+ return Qnil;
124
+ }
125
+ memory = RepeatedField_memoryat(self, index, element_size);
126
+ return native_slot_get(field_type, field_type_class, memory);
127
+ }else{
128
+ /* check if idx is Range */
129
+ switch (rb_range_beg_len(arg, &beg, &len, self->size, 0)) {
130
+ case Qfalse:
131
+ break;
132
+ case Qnil:
133
+ return Qnil;
134
+ default:
135
+ return RepeatedField_subarray(_self, beg, len);
136
+ }
137
+ }
138
+ }
139
+ /* assume 2 arguments */
140
+ beg = NUM2LONG(argv[0]);
141
+ len = NUM2LONG(argv[1]);
142
+ if (beg < 0) {
143
+ beg += self->size;
144
+ }
145
+ if (beg >= self->size) {
146
+ return Qnil;
147
+ }
148
+ return RepeatedField_subarray(_self, beg, len);
149
+ }
150
+
151
+ /*
152
+ * call-seq:
153
+ * RepeatedField.[]=(index, value)
154
+ *
155
+ * Sets the element at the given index. On out-of-bounds assignments, extends
156
+ * the array and fills the hole (if any) with default values.
157
+ */
158
+ VALUE RepeatedField_index_set(VALUE _self, VALUE _index, VALUE val) {
159
+ RepeatedField* self = ruby_to_RepeatedField(_self);
160
+ upb_fieldtype_t field_type = self->field_type;
161
+ VALUE field_type_class = self->field_type_class;
162
+ int element_size = native_slot_size(field_type);
163
+ void* memory;
164
+
165
+ int index = index_position(_index, self);
166
+ if (index < 0 || index >= (INT_MAX - 1)) {
167
+ return Qnil;
168
+ }
169
+ if (index >= self->size) {
170
+ upb_fieldtype_t field_type = self->field_type;
171
+ int element_size = native_slot_size(field_type);
172
+ RepeatedField_reserve(self, index + 1);
173
+ for (int i = self->size; i <= index; i++) {
174
+ void* elem = RepeatedField_memoryat(self, i, element_size);
175
+ native_slot_init(field_type, elem);
176
+ }
177
+ self->size = index + 1;
178
+ }
179
+
180
+ memory = RepeatedField_memoryat(self, index, element_size);
181
+ native_slot_set(field_type, field_type_class, memory, val);
182
+ return Qnil;
183
+ }
184
+
185
+ static int kInitialSize = 8;
186
+
187
+ void RepeatedField_reserve(RepeatedField* self, int new_size) {
188
+ void* old_elems = self->elements;
189
+ int elem_size = native_slot_size(self->field_type);
190
+ if (new_size <= self->capacity) {
191
+ return;
192
+ }
193
+ if (self->capacity == 0) {
194
+ self->capacity = kInitialSize;
195
+ }
196
+ while (self->capacity < new_size) {
197
+ self->capacity *= 2;
198
+ }
199
+ self->elements = ALLOC_N(uint8_t, elem_size * self->capacity);
200
+ if (old_elems != NULL) {
201
+ memcpy(self->elements, old_elems, self->size * elem_size);
202
+ xfree(old_elems);
203
+ }
204
+ }
205
+
206
+ /*
207
+ * call-seq:
208
+ * RepeatedField.push(value)
209
+ *
210
+ * Adds a new element to the repeated field.
211
+ */
212
+ VALUE RepeatedField_push(VALUE _self, VALUE val) {
213
+ RepeatedField* self = ruby_to_RepeatedField(_self);
214
+ upb_fieldtype_t field_type = self->field_type;
215
+ int element_size = native_slot_size(field_type);
216
+ void* memory;
217
+
218
+ RepeatedField_reserve(self, self->size + 1);
219
+ memory = (void *) (((uint8_t *)self->elements) + self->size * element_size);
220
+ native_slot_set(field_type, self->field_type_class, memory, val);
221
+ // native_slot_set may raise an error; bump size only after set.
222
+ self->size++;
223
+ return _self;
224
+ }
225
+
226
+
227
+ // Used by parsing handlers.
228
+ void RepeatedField_push_native(VALUE _self, void* data) {
229
+ RepeatedField* self = ruby_to_RepeatedField(_self);
230
+ upb_fieldtype_t field_type = self->field_type;
231
+ int element_size = native_slot_size(field_type);
232
+ void* memory;
233
+
234
+ RepeatedField_reserve(self, self->size + 1);
235
+ memory = (void *) (((uint8_t *)self->elements) + self->size * element_size);
236
+ memcpy(memory, data, element_size);
237
+ self->size++;
238
+ }
239
+
240
+ void* RepeatedField_index_native(VALUE _self, int index) {
241
+ RepeatedField* self = ruby_to_RepeatedField(_self);
242
+ upb_fieldtype_t field_type = self->field_type;
243
+ int element_size = native_slot_size(field_type);
244
+ return RepeatedField_memoryat(self, index, element_size);
245
+ }
246
+
247
+ int RepeatedField_size(VALUE _self) {
248
+ RepeatedField* self = ruby_to_RepeatedField(_self);
249
+ return self->size;
250
+ }
251
+
252
+ /*
253
+ * Private ruby method, used by RepeatedField.pop
254
+ */
255
+ VALUE RepeatedField_pop_one(VALUE _self) {
256
+ RepeatedField* self = ruby_to_RepeatedField(_self);
257
+ upb_fieldtype_t field_type = self->field_type;
258
+ VALUE field_type_class = self->field_type_class;
259
+ int element_size = native_slot_size(field_type);
260
+ int index;
261
+ void* memory;
262
+ VALUE ret;
263
+
264
+ if (self->size == 0) {
265
+ return Qnil;
266
+ }
267
+ index = self->size - 1;
268
+ memory = RepeatedField_memoryat(self, index, element_size);
269
+ ret = native_slot_get(field_type, field_type_class, memory);
270
+ self->size--;
271
+ return ret;
272
+ }
273
+
274
+ /*
275
+ * call-seq:
276
+ * RepeatedField.replace(list)
277
+ *
278
+ * Replaces the contents of the repeated field with the given list of elements.
279
+ */
280
+ VALUE RepeatedField_replace(VALUE _self, VALUE list) {
281
+ RepeatedField* self = ruby_to_RepeatedField(_self);
282
+ Check_Type(list, T_ARRAY);
283
+ self->size = 0;
284
+ for (int i = 0; i < RARRAY_LEN(list); i++) {
285
+ RepeatedField_push(_self, rb_ary_entry(list, i));
286
+ }
287
+ return list;
288
+ }
289
+
290
+ /*
291
+ * call-seq:
292
+ * RepeatedField.clear
293
+ *
294
+ * Clears (removes all elements from) this repeated field.
295
+ */
296
+ VALUE RepeatedField_clear(VALUE _self) {
297
+ RepeatedField* self = ruby_to_RepeatedField(_self);
298
+ self->size = 0;
299
+ return _self;
300
+ }
301
+
302
+ /*
303
+ * call-seq:
304
+ * RepeatedField.length
305
+ *
306
+ * Returns the length of this repeated field.
307
+ */
308
+ VALUE RepeatedField_length(VALUE _self) {
309
+ RepeatedField* self = ruby_to_RepeatedField(_self);
310
+ return INT2NUM(self->size);
311
+ }
312
+
313
+ static VALUE RepeatedField_new_this_type(VALUE _self) {
314
+ RepeatedField* self = ruby_to_RepeatedField(_self);
315
+ VALUE new_rptfield = Qnil;
316
+ VALUE element_type = fieldtype_to_ruby(self->field_type);
317
+ if (self->field_type_class != Qnil) {
318
+ new_rptfield = rb_funcall(CLASS_OF(_self), rb_intern("new"), 2,
319
+ element_type, self->field_type_class);
320
+ } else {
321
+ new_rptfield = rb_funcall(CLASS_OF(_self), rb_intern("new"), 1,
322
+ element_type);
323
+ }
324
+ return new_rptfield;
325
+ }
326
+
327
+ /*
328
+ * call-seq:
329
+ * RepeatedField.dup => repeated_field
330
+ *
331
+ * Duplicates this repeated field with a shallow copy. References to all
332
+ * non-primitive element objects (e.g., submessages) are shared.
333
+ */
334
+ VALUE RepeatedField_dup(VALUE _self) {
335
+ RepeatedField* self = ruby_to_RepeatedField(_self);
336
+ VALUE new_rptfield = RepeatedField_new_this_type(_self);
337
+ RepeatedField* new_rptfield_self = ruby_to_RepeatedField(new_rptfield);
338
+ upb_fieldtype_t field_type = self->field_type;
339
+ size_t elem_size = native_slot_size(field_type);
340
+ size_t off = 0;
341
+ RepeatedField_reserve(new_rptfield_self, self->size);
342
+ for (int i = 0; i < self->size; i++, off += elem_size) {
343
+ void* to_mem = (uint8_t *)new_rptfield_self->elements + off;
344
+ void* from_mem = (uint8_t *)self->elements + off;
345
+ native_slot_dup(field_type, to_mem, from_mem);
346
+ new_rptfield_self->size++;
347
+ }
348
+
349
+ return new_rptfield;
350
+ }
351
+
352
+ // Internal only: used by Google::Protobuf.deep_copy.
353
+ VALUE RepeatedField_deep_copy(VALUE _self) {
354
+ RepeatedField* self = ruby_to_RepeatedField(_self);
355
+ VALUE new_rptfield = RepeatedField_new_this_type(_self);
356
+ RepeatedField* new_rptfield_self = ruby_to_RepeatedField(new_rptfield);
357
+ upb_fieldtype_t field_type = self->field_type;
358
+ size_t elem_size = native_slot_size(field_type);
359
+ size_t off = 0;
360
+ RepeatedField_reserve(new_rptfield_self, self->size);
361
+ for (int i = 0; i < self->size; i++, off += elem_size) {
362
+ void* to_mem = (uint8_t *)new_rptfield_self->elements + off;
363
+ void* from_mem = (uint8_t *)self->elements + off;
364
+ native_slot_deep_copy(field_type, to_mem, from_mem);
365
+ new_rptfield_self->size++;
366
+ }
367
+
368
+ return new_rptfield;
369
+ }
370
+
371
+ /*
372
+ * call-seq:
373
+ * RepeatedField.to_ary => array
374
+ *
375
+ * Used when converted implicitly into array, e.g. compared to an Array.
376
+ * Also called as a fallback of Object#to_a
377
+ */
378
+ VALUE RepeatedField_to_ary(VALUE _self) {
379
+ RepeatedField* self = ruby_to_RepeatedField(_self);
380
+ upb_fieldtype_t field_type = self->field_type;
381
+
382
+ size_t elem_size = native_slot_size(field_type);
383
+ size_t off = 0;
384
+ VALUE ary = rb_ary_new2(self->size);
385
+ for (int i = 0; i < self->size; i++, off += elem_size) {
386
+ void* mem = ((uint8_t *)self->elements) + off;
387
+ VALUE elem = native_slot_get(field_type, self->field_type_class, mem);
388
+ rb_ary_push(ary, elem);
389
+ }
390
+ return ary;
391
+ }
392
+
393
+ /*
394
+ * call-seq:
395
+ * RepeatedField.==(other) => boolean
396
+ *
397
+ * Compares this repeated field to another. Repeated fields are equal if their
398
+ * element types are equal, their lengths are equal, and each element is equal.
399
+ * Elements are compared as per normal Ruby semantics, by calling their :==
400
+ * methods (or performing a more efficient comparison for primitive types).
401
+ *
402
+ * Repeated fields with dissimilar element types are never equal, even if value
403
+ * comparison (for example, between integers and floats) would have otherwise
404
+ * indicated that every element has equal value.
405
+ */
406
+ VALUE RepeatedField_eq(VALUE _self, VALUE _other) {
407
+ RepeatedField* self;
408
+ RepeatedField* other;
409
+
410
+ if (_self == _other) {
411
+ return Qtrue;
412
+ }
413
+
414
+ if (TYPE(_other) == T_ARRAY) {
415
+ VALUE self_ary = RepeatedField_to_ary(_self);
416
+ return rb_equal(self_ary, _other);
417
+ }
418
+
419
+ self = ruby_to_RepeatedField(_self);
420
+ other = ruby_to_RepeatedField(_other);
421
+ if (self->field_type != other->field_type ||
422
+ self->field_type_class != other->field_type_class ||
423
+ self->size != other->size) {
424
+ return Qfalse;
425
+ }
426
+
427
+ {
428
+ upb_fieldtype_t field_type = self->field_type;
429
+ size_t elem_size = native_slot_size(field_type);
430
+ size_t off = 0;
431
+ for (int i = 0; i < self->size; i++, off += elem_size) {
432
+ void* self_mem = ((uint8_t *)self->elements) + off;
433
+ void* other_mem = ((uint8_t *)other->elements) + off;
434
+ if (!native_slot_eq(field_type, self_mem, other_mem)) {
435
+ return Qfalse;
436
+ }
437
+ }
438
+ return Qtrue;
439
+ }
440
+ }
441
+
442
+ /*
443
+ * call-seq:
444
+ * RepeatedField.hash => hash_value
445
+ *
446
+ * Returns a hash value computed from this repeated field's elements.
447
+ */
448
+ VALUE RepeatedField_hash(VALUE _self) {
449
+ RepeatedField* self = ruby_to_RepeatedField(_self);
450
+ st_index_t h = rb_hash_start(0);
451
+ VALUE hash_sym = rb_intern("hash");
452
+ upb_fieldtype_t field_type = self->field_type;
453
+ VALUE field_type_class = self->field_type_class;
454
+ size_t elem_size = native_slot_size(field_type);
455
+ size_t off = 0;
456
+ for (int i = 0; i < self->size; i++, off += elem_size) {
457
+ void* mem = ((uint8_t *)self->elements) + off;
458
+ VALUE elem = native_slot_get(field_type, field_type_class, mem);
459
+ h = rb_hash_uint(h, NUM2LONG(rb_funcall(elem, hash_sym, 0)));
460
+ }
461
+ h = rb_hash_end(h);
462
+
463
+ return INT2FIX(h);
464
+ }
465
+
466
+ /*
467
+ * call-seq:
468
+ * RepeatedField.+(other) => repeated field
469
+ *
470
+ * Returns a new repeated field that contains the concatenated list of this
471
+ * repeated field's elements and other's elements. The other (second) list may
472
+ * be either another repeated field or a Ruby array.
473
+ */
474
+ VALUE RepeatedField_plus(VALUE _self, VALUE list) {
475
+ VALUE dupped = RepeatedField_dup(_self);
476
+
477
+ if (TYPE(list) == T_ARRAY) {
478
+ for (int i = 0; i < RARRAY_LEN(list); i++) {
479
+ VALUE elem = rb_ary_entry(list, i);
480
+ RepeatedField_push(dupped, elem);
481
+ }
482
+ } else if (RB_TYPE_P(list, T_DATA) && RTYPEDDATA_P(list) &&
483
+ RTYPEDDATA_TYPE(list) == &RepeatedField_type) {
484
+ RepeatedField* self = ruby_to_RepeatedField(_self);
485
+ RepeatedField* list_rptfield = ruby_to_RepeatedField(list);
486
+ if (self->field_type != list_rptfield->field_type ||
487
+ self->field_type_class != list_rptfield->field_type_class) {
488
+ rb_raise(rb_eArgError,
489
+ "Attempt to append RepeatedField with different element type.");
490
+ }
491
+ for (int i = 0; i < list_rptfield->size; i++) {
492
+ void* mem = RepeatedField_index_native(list, i);
493
+ RepeatedField_push_native(dupped, mem);
494
+ }
495
+ } else {
496
+ rb_raise(rb_eArgError, "Unknown type appending to RepeatedField");
497
+ }
498
+
499
+ return dupped;
500
+ }
501
+
502
+ /*
503
+ * call-seq:
504
+ * RepeatedField.concat(other) => self
505
+ *
506
+ * concats the passed in array to self. Returns a Ruby array.
507
+ */
508
+ VALUE RepeatedField_concat(VALUE _self, VALUE list) {
509
+ Check_Type(list, T_ARRAY);
510
+ for (int i = 0; i < RARRAY_LEN(list); i++) {
511
+ RepeatedField_push(_self, rb_ary_entry(list, i));
512
+ }
513
+ return _self;
514
+ }
515
+
516
+
517
+ void validate_type_class(upb_fieldtype_t type, VALUE klass) {
518
+ if (rb_ivar_get(klass, descriptor_instancevar_interned) == Qnil) {
519
+ rb_raise(rb_eArgError,
520
+ "Type class has no descriptor. Please pass a "
521
+ "class or enum as returned by the DescriptorPool.");
522
+ }
523
+ if (type == UPB_TYPE_MESSAGE) {
524
+ VALUE desc = rb_ivar_get(klass, descriptor_instancevar_interned);
525
+ if (!RB_TYPE_P(desc, T_DATA) || !RTYPEDDATA_P(desc) ||
526
+ RTYPEDDATA_TYPE(desc) != &_Descriptor_type) {
527
+ rb_raise(rb_eArgError, "Descriptor has an incorrect type.");
528
+ }
529
+ if (rb_get_alloc_func(klass) != &Message_alloc) {
530
+ rb_raise(rb_eArgError,
531
+ "Message class was not returned by the DescriptorPool.");
532
+ }
533
+ } else if (type == UPB_TYPE_ENUM) {
534
+ VALUE enumdesc = rb_ivar_get(klass, descriptor_instancevar_interned);
535
+ if (!RB_TYPE_P(enumdesc, T_DATA) || !RTYPEDDATA_P(enumdesc) ||
536
+ RTYPEDDATA_TYPE(enumdesc) != &_EnumDescriptor_type) {
537
+ rb_raise(rb_eArgError, "Descriptor has an incorrect type.");
538
+ }
539
+ }
540
+ }
541
+
542
+ void RepeatedField_init_args(int argc, VALUE* argv,
543
+ VALUE _self) {
544
+ RepeatedField* self = ruby_to_RepeatedField(_self);
545
+ VALUE ary = Qnil;
546
+ if (argc < 1) {
547
+ rb_raise(rb_eArgError, "Expected at least 1 argument.");
548
+ }
549
+ self->field_type = ruby_to_fieldtype(argv[0]);
550
+
551
+ if (self->field_type == UPB_TYPE_MESSAGE ||
552
+ self->field_type == UPB_TYPE_ENUM) {
553
+ if (argc < 2) {
554
+ rb_raise(rb_eArgError, "Expected at least 2 arguments for message/enum.");
555
+ }
556
+ self->field_type_class = argv[1];
557
+ if (argc > 2) {
558
+ ary = argv[2];
559
+ }
560
+ validate_type_class(self->field_type, self->field_type_class);
561
+ } else {
562
+ if (argc > 2) {
563
+ rb_raise(rb_eArgError, "Too many arguments: expected 1 or 2.");
564
+ }
565
+ if (argc > 1) {
566
+ ary = argv[1];
567
+ }
568
+ }
569
+
570
+ if (ary != Qnil) {
571
+ if (!RB_TYPE_P(ary, T_ARRAY)) {
572
+ rb_raise(rb_eArgError, "Expected array as initialize argument");
573
+ }
574
+ for (int i = 0; i < RARRAY_LEN(ary); i++) {
575
+ RepeatedField_push(_self, rb_ary_entry(ary, i));
576
+ }
577
+ }
578
+ }
579
+
580
+ // Mark, free, alloc, init and class setup functions.
581
+
582
+ void RepeatedField_mark(void* _self) {
583
+ RepeatedField* self = (RepeatedField*)_self;
584
+ upb_fieldtype_t field_type = self->field_type;
585
+ int element_size = native_slot_size(field_type);
586
+ rb_gc_mark(self->field_type_class);
587
+ for (int i = 0; i < self->size; i++) {
588
+ void* memory = (((uint8_t *)self->elements) + i * element_size);
589
+ native_slot_mark(self->field_type, memory);
590
+ }
591
+ }
592
+
593
+ void RepeatedField_free(void* _self) {
594
+ RepeatedField* self = (RepeatedField*)_self;
595
+ xfree(self->elements);
596
+ xfree(self);
597
+ }
598
+
599
+ /*
600
+ * call-seq:
601
+ * RepeatedField.new(type, type_class = nil, initial_elems = [])
602
+ *
603
+ * Creates a new repeated field. The provided type must be a Ruby symbol, and
604
+ * can take on the same values as those accepted by FieldDescriptor#type=. If
605
+ * the type is :message or :enum, type_class must be non-nil, and must be the
606
+ * Ruby class or module returned by Descriptor#msgclass or
607
+ * EnumDescriptor#enummodule, respectively. An initial list of elements may also
608
+ * be provided.
609
+ */
610
+ VALUE RepeatedField_alloc(VALUE klass) {
611
+ RepeatedField* self = ALLOC(RepeatedField);
612
+ self->elements = NULL;
613
+ self->size = 0;
614
+ self->capacity = 0;
615
+ self->field_type = -1;
616
+ self->field_type_class = Qnil;
617
+ return TypedData_Wrap_Struct(klass, &RepeatedField_type, self);
618
+ }
619
+
620
+ VALUE RepeatedField_init(int argc, VALUE* argv, VALUE self) {
621
+ RepeatedField_init_args(argc, argv, self);
622
+ return Qnil;
623
+ }
624
+
625
+ void RepeatedField_register(VALUE module) {
626
+ VALUE klass = rb_define_class_under(
627
+ module, "RepeatedField", rb_cObject);
628
+ rb_define_alloc_func(klass, RepeatedField_alloc);
629
+ rb_gc_register_address(&cRepeatedField);
630
+ cRepeatedField = klass;
631
+
632
+ rb_define_method(klass, "initialize",
633
+ RepeatedField_init, -1);
634
+ rb_define_method(klass, "each", RepeatedField_each, 0);
635
+ rb_define_method(klass, "[]", RepeatedField_index, -1);
636
+ rb_define_method(klass, "at", RepeatedField_index, -1);
637
+ rb_define_method(klass, "[]=", RepeatedField_index_set, 2);
638
+ rb_define_method(klass, "push", RepeatedField_push, 1);
639
+ rb_define_method(klass, "<<", RepeatedField_push, 1);
640
+ rb_define_private_method(klass, "pop_one", RepeatedField_pop_one, 0);
641
+ rb_define_method(klass, "replace", RepeatedField_replace, 1);
642
+ rb_define_method(klass, "clear", RepeatedField_clear, 0);
643
+ rb_define_method(klass, "length", RepeatedField_length, 0);
644
+ rb_define_method(klass, "size", RepeatedField_length, 0);
645
+ rb_define_method(klass, "dup", RepeatedField_dup, 0);
646
+ // Also define #clone so that we don't inherit Object#clone.
647
+ rb_define_method(klass, "clone", RepeatedField_dup, 0);
648
+ rb_define_method(klass, "==", RepeatedField_eq, 1);
649
+ rb_define_method(klass, "to_ary", RepeatedField_to_ary, 0);
650
+ rb_define_method(klass, "hash", RepeatedField_hash, 0);
651
+ rb_define_method(klass, "+", RepeatedField_plus, 1);
652
+ rb_define_method(klass, "concat", RepeatedField_concat, 1);
653
+ rb_include_module(klass, rb_mEnumerable);
654
+ }