repeated_field.c 22 KB

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