repeated_field.c 21.4 KB
Newer Older
Chris Fallin's avatar
Chris Fallin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
// Protocol Buffers - Google's data interchange format
// Copyright 2014 Google Inc.  All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "protobuf.h"

// -----------------------------------------------------------------------------
// Repeated field container type.
// -----------------------------------------------------------------------------

const rb_data_type_t RepeatedField_type = {
  "Google::Protobuf::RepeatedField",
  { RepeatedField_mark, RepeatedField_free, NULL },
};

VALUE cRepeatedField;

RepeatedField* ruby_to_RepeatedField(VALUE _self) {
  RepeatedField* self;
  TypedData_Get_Struct(_self, RepeatedField, &RepeatedField_type, self);
  return self;
}

50 51 52 53
void* RepeatedField_memoryat(RepeatedField* self, int index, int element_size) {
  return ((uint8_t *)self->elements) + index * element_size;
}

54 55 56 57 58 59 60 61
static int index_position(VALUE _index, RepeatedField* repeated_field) {
  int index = NUM2INT(_index);
  if (index < 0 && repeated_field->size > 0) {
    index = repeated_field->size + index;
  }
  return index;
}

62 63 64 65 66 67 68
VALUE RepeatedField_subarray(VALUE _self, long beg, long len) {
  RepeatedField* self = ruby_to_RepeatedField(_self);
  int element_size = native_slot_size(self->field_type);
  upb_fieldtype_t field_type = self->field_type;
  VALUE field_type_class = self->field_type_class;
  size_t off = beg * element_size;
  VALUE ary = rb_ary_new2(len);
69 70 71
  int i;

  for (i = beg; i < beg + len; i++, off += element_size) {
72 73 74 75 76 77
    void* mem = ((uint8_t *)self->elements) + off;
    VALUE elem = native_slot_get(field_type, field_type_class, mem);
    rb_ary_push(ary, elem);
  }
  return ary;
}
78

Chris Fallin's avatar
Chris Fallin committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92
/*
 * call-seq:
 *     RepeatedField.each(&block)
 *
 * Invokes the block once for each element of the repeated field. RepeatedField
 * also includes Enumerable; combined with this method, the repeated field thus
 * acts like an ordinary Ruby sequence.
 */
VALUE RepeatedField_each(VALUE _self) {
  RepeatedField* self = ruby_to_RepeatedField(_self);
  upb_fieldtype_t field_type = self->field_type;
  VALUE field_type_class = self->field_type_class;
  int element_size = native_slot_size(field_type);
  size_t off = 0;
93 94 95
  int i;

  for (i = 0; i < self->size; i++, off += element_size) {
Chris Fallin's avatar
Chris Fallin committed
96 97 98 99
    void* memory = (void *) (((uint8_t *)self->elements) + off);
    VALUE val = native_slot_get(field_type, field_type_class, memory);
    rb_yield(val);
  }
100
  return _self;
Chris Fallin's avatar
Chris Fallin committed
101 102
}

103

Chris Fallin's avatar
Chris Fallin committed
104 105 106 107
/*
 * call-seq:
 *     RepeatedField.[](index) => value
 *
108
 * Accesses the element at the given index. Returns nil on out-of-bounds
Chris Fallin's avatar
Chris Fallin committed
109
 */
110
VALUE RepeatedField_index(int argc, VALUE* argv, VALUE _self) {
Chris Fallin's avatar
Chris Fallin committed
111 112 113 114 115
  RepeatedField* self = ruby_to_RepeatedField(_self);
  int element_size = native_slot_size(self->field_type);
  upb_fieldtype_t field_type = self->field_type;
  VALUE field_type_class = self->field_type_class;

116 117 118 119 120 121
  VALUE arg = argv[0];
  long beg, len;

  if (argc == 1){
    if (FIXNUM_P(arg)) {
      /* standard case */
122
      void* memory;
123 124 125 126
      int index = index_position(argv[0], self);
      if (index < 0 || index >= self->size) {
        return Qnil;
      }
127
      memory = RepeatedField_memoryat(self, index, element_size);
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
      return native_slot_get(field_type, field_type_class, memory);
    }else{
      /* check if idx is Range */
      switch (rb_range_beg_len(arg, &beg, &len, self->size, 0)) {
        case Qfalse:
          break;
        case Qnil:
          return Qnil;
        default:
          return RepeatedField_subarray(_self, beg, len);
      }
    }
  }
  /* assume 2 arguments */
  beg = NUM2LONG(argv[0]);
  len = NUM2LONG(argv[1]);
  if (beg < 0) {
    beg += self->size;
  }
  if (beg >= self->size) {
148
    return Qnil;
Chris Fallin's avatar
Chris Fallin committed
149
  }
150
  return RepeatedField_subarray(_self, beg, len);
Chris Fallin's avatar
Chris Fallin committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164
}

/*
 * call-seq:
 *     RepeatedField.[]=(index, value)
 *
 * Sets the element at the given index. On out-of-bounds assignments, extends
 * the array and fills the hole (if any) with default values.
 */
VALUE RepeatedField_index_set(VALUE _self, VALUE _index, VALUE val) {
  RepeatedField* self = ruby_to_RepeatedField(_self);
  upb_fieldtype_t field_type = self->field_type;
  VALUE field_type_class = self->field_type_class;
  int element_size = native_slot_size(field_type);
165
  void* memory;
Chris Fallin's avatar
Chris Fallin committed
166

167
  int index = index_position(_index, self);
Chris Fallin's avatar
Chris Fallin committed
168
  if (index < 0 || index >= (INT_MAX - 1)) {
169
    return Qnil;
Chris Fallin's avatar
Chris Fallin committed
170 171 172 173
  }
  if (index >= self->size) {
    upb_fieldtype_t field_type = self->field_type;
    int element_size = native_slot_size(field_type);
174 175
    int i;

176
    RepeatedField_reserve(self, index + 1);
177
    for (i = self->size; i <= index; i++) {
178
      void* elem = RepeatedField_memoryat(self, i, element_size);
Chris Fallin's avatar
Chris Fallin committed
179 180 181 182 183
      native_slot_init(field_type, elem);
    }
    self->size = index + 1;
  }

184
  memory = RepeatedField_memoryat(self, index, element_size);
185
  native_slot_set("", field_type, field_type_class, memory, val);
Chris Fallin's avatar
Chris Fallin committed
186 187 188 189 190 191
  return Qnil;
}

static int kInitialSize = 8;

void RepeatedField_reserve(RepeatedField* self, int new_size) {
192 193
  void* old_elems = self->elements;
  int elem_size = native_slot_size(self->field_type);
Chris Fallin's avatar
Chris Fallin committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
  if (new_size <= self->capacity) {
    return;
  }
  if (self->capacity == 0) {
    self->capacity = kInitialSize;
  }
  while (self->capacity < new_size) {
    self->capacity *= 2;
  }
  self->elements = ALLOC_N(uint8_t, elem_size * self->capacity);
  if (old_elems != NULL) {
    memcpy(self->elements, old_elems, self->size * elem_size);
    xfree(old_elems);
  }
}

/*
 * call-seq:
 *     RepeatedField.push(value)
 *
 * Adds a new element to the repeated field.
 */
VALUE RepeatedField_push(VALUE _self, VALUE val) {
  RepeatedField* self = ruby_to_RepeatedField(_self);
  upb_fieldtype_t field_type = self->field_type;
  int element_size = native_slot_size(field_type);
220 221
  void* memory;

Chris Fallin's avatar
Chris Fallin committed
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.
Chris Fallin's avatar
Chris Fallin committed
226 227 228 229
  self->size++;
  return _self;
}

230
VALUE RepeatedField_push_vararg(VALUE _self, VALUE args) {
231 232
  int i;
  for (i = 0; i < RARRAY_LEN(args); i++) {
233 234 235 236
    RepeatedField_push(_self, rb_ary_entry(args, i));
  }
  return _self;
}
237

Chris Fallin's avatar
Chris Fallin committed
238 239 240 241 242
// Used by parsing handlers.
void RepeatedField_push_native(VALUE _self, void* data) {
  RepeatedField* self = ruby_to_RepeatedField(_self);
  upb_fieldtype_t field_type = self->field_type;
  int element_size = native_slot_size(field_type);
243 244
  void* memory;

Chris Fallin's avatar
Chris Fallin committed
245
  RepeatedField_reserve(self, self->size + 1);
246
  memory = (void *) (((uint8_t *)self->elements) + self->size * element_size);
Chris Fallin's avatar
Chris Fallin committed
247 248 249 250 251 252 253 254
  memcpy(memory, data, element_size);
  self->size++;
}

void* RepeatedField_index_native(VALUE _self, int index) {
  RepeatedField* self = ruby_to_RepeatedField(_self);
  upb_fieldtype_t field_type = self->field_type;
  int element_size = native_slot_size(field_type);
255
  return RepeatedField_memoryat(self, index, element_size);
Chris Fallin's avatar
Chris Fallin committed
256 257
}

258 259 260 261 262
int RepeatedField_size(VALUE _self) {
  RepeatedField* self = ruby_to_RepeatedField(_self);
  return self->size;
}

Chris Fallin's avatar
Chris Fallin committed
263
/*
264
 * Private ruby method, used by RepeatedField.pop
Chris Fallin's avatar
Chris Fallin committed
265
 */
266
VALUE RepeatedField_pop_one(VALUE _self) {
Chris Fallin's avatar
Chris Fallin committed
267 268 269 270
  RepeatedField* self = ruby_to_RepeatedField(_self);
  upb_fieldtype_t field_type = self->field_type;
  VALUE field_type_class = self->field_type_class;
  int element_size = native_slot_size(field_type);
271 272 273 274
  int index;
  void* memory;
  VALUE ret;

Chris Fallin's avatar
Chris Fallin committed
275
  if (self->size == 0) {
276
    return Qnil;
Chris Fallin's avatar
Chris Fallin committed
277
  }
278 279 280
  index = self->size - 1;
  memory = RepeatedField_memoryat(self, index, element_size);
  ret = native_slot_get(field_type, field_type_class, memory);
Chris Fallin's avatar
Chris Fallin committed
281 282 283 284 285 286 287 288 289 290 291 292
  self->size--;
  return ret;
}

/*
 * call-seq:
 *     RepeatedField.replace(list)
 *
 * Replaces the contents of the repeated field with the given list of elements.
 */
VALUE RepeatedField_replace(VALUE _self, VALUE list) {
  RepeatedField* self = ruby_to_RepeatedField(_self);
293 294
  int i;

Chris Fallin's avatar
Chris Fallin committed
295 296
  Check_Type(list, T_ARRAY);
  self->size = 0;
297
  for (i = 0; i < RARRAY_LEN(list); i++) {
Chris Fallin's avatar
Chris Fallin committed
298 299
    RepeatedField_push(_self, rb_ary_entry(list, i));
  }
300
  return list;
Chris Fallin's avatar
Chris Fallin committed
301 302 303 304 305 306 307 308 309 310 311
}

/*
 * call-seq:
 *     RepeatedField.clear
 *
 * Clears (removes all elements from) this repeated field.
 */
VALUE RepeatedField_clear(VALUE _self) {
  RepeatedField* self = ruby_to_RepeatedField(_self);
  self->size = 0;
312
  return _self;
Chris Fallin's avatar
Chris Fallin committed
313 314 315 316 317 318 319 320 321 322 323 324 325
}

/*
 * call-seq:
 *     RepeatedField.length
 *
 * Returns the length of this repeated field.
 */
VALUE RepeatedField_length(VALUE _self) {
  RepeatedField* self = ruby_to_RepeatedField(_self);
  return INT2NUM(self->size);
}

326
VALUE RepeatedField_new_this_type(VALUE _self) {
Chris Fallin's avatar
Chris Fallin committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
  RepeatedField* self = ruby_to_RepeatedField(_self);
  VALUE new_rptfield = Qnil;
  VALUE element_type = fieldtype_to_ruby(self->field_type);
  if (self->field_type_class != Qnil) {
    new_rptfield = rb_funcall(CLASS_OF(_self), rb_intern("new"), 2,
                              element_type, self->field_type_class);
  } else {
    new_rptfield = rb_funcall(CLASS_OF(_self), rb_intern("new"), 1,
                              element_type);
  }
  return new_rptfield;
}

/*
 * call-seq:
 *     RepeatedField.dup => repeated_field
 *
 * Duplicates this repeated field with a shallow copy. References to all
 * non-primitive element objects (e.g., submessages) are shared.
 */
VALUE RepeatedField_dup(VALUE _self) {
  RepeatedField* self = ruby_to_RepeatedField(_self);
  VALUE new_rptfield = RepeatedField_new_this_type(_self);
  RepeatedField* new_rptfield_self = ruby_to_RepeatedField(new_rptfield);
  upb_fieldtype_t field_type = self->field_type;
  size_t elem_size = native_slot_size(field_type);
  size_t off = 0;
354 355
  int i;

356
  RepeatedField_reserve(new_rptfield_self, self->size);
357
  for (i = 0; i < self->size; i++, off += elem_size) {
Chris Fallin's avatar
Chris Fallin committed
358 359 360 361 362 363 364 365 366
    void* to_mem = (uint8_t *)new_rptfield_self->elements + off;
    void* from_mem = (uint8_t *)self->elements + off;
    native_slot_dup(field_type, to_mem, from_mem);
    new_rptfield_self->size++;
  }

  return new_rptfield;
}

367 368
// Internal only: used by Google::Protobuf.deep_copy.
VALUE RepeatedField_deep_copy(VALUE _self) {
Chris Fallin's avatar
Chris Fallin committed
369 370 371 372 373 374
  RepeatedField* self = ruby_to_RepeatedField(_self);
  VALUE new_rptfield = RepeatedField_new_this_type(_self);
  RepeatedField* new_rptfield_self = ruby_to_RepeatedField(new_rptfield);
  upb_fieldtype_t field_type = self->field_type;
  size_t elem_size = native_slot_size(field_type);
  size_t off = 0;
375 376
  int i;

377
  RepeatedField_reserve(new_rptfield_self, self->size);
378
  for (i = 0; i < self->size; i++, off += elem_size) {
Chris Fallin's avatar
Chris Fallin committed
379 380 381 382 383 384 385 386 387
    void* to_mem = (uint8_t *)new_rptfield_self->elements + off;
    void* from_mem = (uint8_t *)self->elements + off;
    native_slot_deep_copy(field_type, to_mem, from_mem);
    new_rptfield_self->size++;
  }

  return new_rptfield;
}

388 389 390 391 392 393 394 395 396 397 398 399 400
/*
 * call-seq:
 *     RepeatedField.to_ary => array
 *
 * Used when converted implicitly into array, e.g. compared to an Array.
 * Also called as a fallback of Object#to_a
 */
VALUE RepeatedField_to_ary(VALUE _self) {
  RepeatedField* self = ruby_to_RepeatedField(_self);
  upb_fieldtype_t field_type = self->field_type;
  size_t elem_size = native_slot_size(field_type);
  size_t off = 0;
  VALUE ary = rb_ary_new2(self->size);
401 402 403
  int i;

  for (i = 0; i < self->size; i++, off += elem_size) {
404 405 406 407 408 409 410
    void* mem = ((uint8_t *)self->elements) + off;
    VALUE elem = native_slot_get(field_type, self->field_type_class, mem);
    rb_ary_push(ary, elem);
  }
  return ary;
}

Chris Fallin's avatar
Chris Fallin committed
411 412 413 414 415 416 417 418
/*
 * call-seq:
 *     RepeatedField.==(other) => boolean
 *
 * Compares this repeated field to another. Repeated fields are equal if their
 * element types are equal, their lengths are equal, and each element is equal.
 * Elements are compared as per normal Ruby semantics, by calling their :==
 * methods (or performing a more efficient comparison for primitive types).
419 420 421 422
 *
 * Repeated fields with dissimilar element types are never equal, even if value
 * comparison (for example, between integers and floats) would have otherwise
 * indicated that every element has equal value.
Chris Fallin's avatar
Chris Fallin committed
423 424
 */
VALUE RepeatedField_eq(VALUE _self, VALUE _other) {
425 426 427
  RepeatedField* self;
  RepeatedField* other;

Chris Fallin's avatar
Chris Fallin committed
428 429 430 431 432
  if (_self == _other) {
    return Qtrue;
  }

  if (TYPE(_other) == T_ARRAY) {
433 434
    VALUE self_ary = RepeatedField_to_ary(_self);
    return rb_equal(self_ary, _other);
Chris Fallin's avatar
Chris Fallin committed
435 436
  }

437 438
  self = ruby_to_RepeatedField(_self);
  other = ruby_to_RepeatedField(_other);
Chris Fallin's avatar
Chris Fallin committed
439 440 441 442 443 444
  if (self->field_type != other->field_type ||
      self->field_type_class != other->field_type_class ||
      self->size != other->size) {
    return Qfalse;
  }

445 446 447 448
  {
    upb_fieldtype_t field_type = self->field_type;
    size_t elem_size = native_slot_size(field_type);
    size_t off = 0;
449 450 451
    int i;

    for (i = 0; i < self->size; i++, off += elem_size) {
452 453 454 455 456
      void* self_mem = ((uint8_t *)self->elements) + off;
      void* other_mem = ((uint8_t *)other->elements) + off;
      if (!native_slot_eq(field_type, self_mem, other_mem)) {
        return Qfalse;
      }
Chris Fallin's avatar
Chris Fallin committed
457
    }
458
    return Qtrue;
Chris Fallin's avatar
Chris Fallin committed
459 460 461 462 463 464 465 466 467 468 469
  }
}

/*
 * call-seq:
 *     RepeatedField.hash => hash_value
 *
 * Returns a hash value computed from this repeated field's elements.
 */
VALUE RepeatedField_hash(VALUE _self) {
  RepeatedField* self = ruby_to_RepeatedField(_self);
470 471
  st_index_t h = rb_hash_start(0);
  VALUE hash_sym = rb_intern("hash");
Chris Fallin's avatar
Chris Fallin committed
472 473 474 475
  upb_fieldtype_t field_type = self->field_type;
  VALUE field_type_class = self->field_type_class;
  size_t elem_size = native_slot_size(field_type);
  size_t off = 0;
476 477 478
  int i;

  for (i = 0; i < self->size; i++, off += elem_size) {
Chris Fallin's avatar
Chris Fallin committed
479 480
    void* mem = ((uint8_t *)self->elements) + off;
    VALUE elem = native_slot_get(field_type, field_type_class, mem);
481
    h = rb_hash_uint(h, NUM2LONG(rb_funcall(elem, hash_sym, 0)));
Chris Fallin's avatar
Chris Fallin committed
482
  }
483
  h = rb_hash_end(h);
Chris Fallin's avatar
Chris Fallin committed
484

485
  return INT2FIX(h);
Chris Fallin's avatar
Chris Fallin committed
486 487 488 489 490 491 492 493 494 495 496 497 498 499
}

/*
 * call-seq:
 *     RepeatedField.+(other) => repeated field
 *
 * Returns a new repeated field that contains the concatenated list of this
 * repeated field's elements and other's elements. The other (second) list may
 * be either another repeated field or a Ruby array.
 */
VALUE RepeatedField_plus(VALUE _self, VALUE list) {
  VALUE dupped = RepeatedField_dup(_self);

  if (TYPE(list) == T_ARRAY) {
500 501
    int i;
    for (i = 0; i < RARRAY_LEN(list); i++) {
Chris Fallin's avatar
Chris Fallin committed
502 503 504 505 506 507 508
      VALUE elem = rb_ary_entry(list, i);
      RepeatedField_push(dupped, elem);
    }
  } else if (RB_TYPE_P(list, T_DATA) && RTYPEDDATA_P(list) &&
             RTYPEDDATA_TYPE(list) == &RepeatedField_type) {
    RepeatedField* self = ruby_to_RepeatedField(_self);
    RepeatedField* list_rptfield = ruby_to_RepeatedField(list);
509 510
    int i;

Chris Fallin's avatar
Chris Fallin committed
511 512 513 514 515
    if (self->field_type != list_rptfield->field_type ||
        self->field_type_class != list_rptfield->field_type_class) {
      rb_raise(rb_eArgError,
               "Attempt to append RepeatedField with different element type.");
    }
516
    for (i = 0; i < list_rptfield->size; i++) {
Chris Fallin's avatar
Chris Fallin committed
517 518 519 520 521 522 523 524 525 526
      void* mem = RepeatedField_index_native(list, i);
      RepeatedField_push_native(dupped, mem);
    }
  } else {
    rb_raise(rb_eArgError, "Unknown type appending to RepeatedField");
  }

  return dupped;
}

527 528 529 530 531 532 533
/*
 * call-seq:
 *     RepeatedField.concat(other) => self
 *
 * concats the passed in array to self.  Returns a Ruby array.
 */
VALUE RepeatedField_concat(VALUE _self, VALUE list) {
534 535
  int i;

536
  Check_Type(list, T_ARRAY);
537
  for (i = 0; i < RARRAY_LEN(list); i++) {
538 539 540 541 542 543
    RepeatedField_push(_self, rb_ary_entry(list, i));
  }
  return _self;
}


544
void validate_type_class(upb_fieldtype_t type, VALUE klass) {
545
  if (rb_ivar_get(klass, descriptor_instancevar_interned) == Qnil) {
Chris Fallin's avatar
Chris Fallin committed
546 547 548 549 550
    rb_raise(rb_eArgError,
             "Type class has no descriptor. Please pass a "
             "class or enum as returned by the DescriptorPool.");
  }
  if (type == UPB_TYPE_MESSAGE) {
551
    VALUE desc = rb_ivar_get(klass, descriptor_instancevar_interned);
Chris Fallin's avatar
Chris Fallin committed
552 553 554 555 556 557 558 559 560
    if (!RB_TYPE_P(desc, T_DATA) || !RTYPEDDATA_P(desc) ||
        RTYPEDDATA_TYPE(desc) != &_Descriptor_type) {
      rb_raise(rb_eArgError, "Descriptor has an incorrect type.");
    }
    if (rb_get_alloc_func(klass) != &Message_alloc) {
      rb_raise(rb_eArgError,
               "Message class was not returned by the DescriptorPool.");
    }
  } else if (type == UPB_TYPE_ENUM) {
561
    VALUE enumdesc = rb_ivar_get(klass, descriptor_instancevar_interned);
Chris Fallin's avatar
Chris Fallin committed
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
    if (!RB_TYPE_P(enumdesc, T_DATA) || !RTYPEDDATA_P(enumdesc) ||
        RTYPEDDATA_TYPE(enumdesc) != &_EnumDescriptor_type) {
      rb_raise(rb_eArgError, "Descriptor has an incorrect type.");
    }
  }
}

void RepeatedField_init_args(int argc, VALUE* argv,
                             VALUE _self) {
  RepeatedField* self = ruby_to_RepeatedField(_self);
  VALUE ary = Qnil;
  if (argc < 1) {
    rb_raise(rb_eArgError, "Expected at least 1 argument.");
  }
  self->field_type = ruby_to_fieldtype(argv[0]);

  if (self->field_type == UPB_TYPE_MESSAGE ||
      self->field_type == UPB_TYPE_ENUM) {
    if (argc < 2) {
      rb_raise(rb_eArgError, "Expected at least 2 arguments for message/enum.");
    }
    self->field_type_class = argv[1];
    if (argc > 2) {
      ary = argv[2];
    }
    validate_type_class(self->field_type, self->field_type_class);
  } else {
    if (argc > 2) {
      rb_raise(rb_eArgError, "Too many arguments: expected 1 or 2.");
    }
    if (argc > 1) {
      ary = argv[1];
    }
  }

  if (ary != Qnil) {
598 599
    int i;

Chris Fallin's avatar
Chris Fallin committed
600 601 602
    if (!RB_TYPE_P(ary, T_ARRAY)) {
      rb_raise(rb_eArgError, "Expected array as initialize argument");
    }
603
    for (i = 0; i < RARRAY_LEN(ary); i++) {
Chris Fallin's avatar
Chris Fallin committed
604 605 606 607 608 609 610 611 612 613 614
      RepeatedField_push(_self, rb_ary_entry(ary, i));
    }
  }
}

// Mark, free, alloc, init and class setup functions.

void RepeatedField_mark(void* _self) {
  RepeatedField* self = (RepeatedField*)_self;
  upb_fieldtype_t field_type = self->field_type;
  int element_size = native_slot_size(field_type);
615 616
  int i;

617
  rb_gc_mark(self->field_type_class);
618
  for (i = 0; i < self->size; i++) {
Chris Fallin's avatar
Chris Fallin committed
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
    void* memory = (((uint8_t *)self->elements) + i * element_size);
    native_slot_mark(self->field_type, memory);
  }
}

void RepeatedField_free(void* _self) {
  RepeatedField* self = (RepeatedField*)_self;
  xfree(self->elements);
  xfree(self);
}

/*
 * call-seq:
 *     RepeatedField.new(type, type_class = nil, initial_elems = [])
 *
 * Creates a new repeated field. The provided type must be a Ruby symbol, and
 * can take on the same values as those accepted by FieldDescriptor#type=. If
 * the type is :message or :enum, type_class must be non-nil, and must be the
 * Ruby class or module returned by Descriptor#msgclass or
 * EnumDescriptor#enummodule, respectively. An initial list of elements may also
 * be provided.
 */
VALUE RepeatedField_alloc(VALUE klass) {
  RepeatedField* self = ALLOC(RepeatedField);
  self->elements = NULL;
  self->size = 0;
  self->capacity = 0;
  self->field_type = -1;
  self->field_type_class = Qnil;
648
  return TypedData_Wrap_Struct(klass, &RepeatedField_type, self);
Chris Fallin's avatar
Chris Fallin committed
649 650 651 652 653 654 655 656 657 658 659 660
}

VALUE RepeatedField_init(int argc, VALUE* argv, VALUE self) {
  RepeatedField_init_args(argc, argv, self);
  return Qnil;
}

void RepeatedField_register(VALUE module) {
  VALUE klass = rb_define_class_under(
      module, "RepeatedField", rb_cObject);
  rb_define_alloc_func(klass, RepeatedField_alloc);
  rb_gc_register_address(&cRepeatedField);
661
  cRepeatedField = klass;
Chris Fallin's avatar
Chris Fallin committed
662 663 664 665

  rb_define_method(klass, "initialize",
                   RepeatedField_init, -1);
  rb_define_method(klass, "each", RepeatedField_each, 0);
666 667
  rb_define_method(klass, "[]", RepeatedField_index, -1);
  rb_define_method(klass, "at", RepeatedField_index, -1);
Chris Fallin's avatar
Chris Fallin committed
668
  rb_define_method(klass, "[]=", RepeatedField_index_set, 2);
669
  rb_define_method(klass, "push", RepeatedField_push_vararg, -2);
Chris Fallin's avatar
Chris Fallin committed
670
  rb_define_method(klass, "<<", RepeatedField_push, 1);
671
  rb_define_private_method(klass, "pop_one", RepeatedField_pop_one, 0);
Chris Fallin's avatar
Chris Fallin committed
672 673 674
  rb_define_method(klass, "replace", RepeatedField_replace, 1);
  rb_define_method(klass, "clear", RepeatedField_clear, 0);
  rb_define_method(klass, "length", RepeatedField_length, 0);
675
  rb_define_method(klass, "size", RepeatedField_length, 0);
Chris Fallin's avatar
Chris Fallin committed
676 677 678 679
  rb_define_method(klass, "dup", RepeatedField_dup, 0);
  // Also define #clone so that we don't inherit Object#clone.
  rb_define_method(klass, "clone", RepeatedField_dup, 0);
  rb_define_method(klass, "==", RepeatedField_eq, 1);
680
  rb_define_method(klass, "to_ary", RepeatedField_to_ary, 0);
Chris Fallin's avatar
Chris Fallin committed
681 682
  rb_define_method(klass, "hash", RepeatedField_hash, 0);
  rb_define_method(klass, "+", RepeatedField_plus, 1);
683
  rb_define_method(klass, "concat", RepeatedField_concat, 1);
Chris Fallin's avatar
Chris Fallin committed
684 685
  rb_include_module(klass, rb_mEnumerable);
}