google-protobuf 3.0.0.alpha.3 → 3.0.0.alpha.3.1.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of google-protobuf might be problematic. Click here for more details.

@@ -47,30 +47,6 @@ RepeatedField* ruby_to_RepeatedField(VALUE _self) {
47
47
  return self;
48
48
  }
49
49
 
50
- static int index_position(VALUE _index, RepeatedField* repeated_field) {
51
- int index = NUM2INT(_index);
52
- if (index < 0 && repeated_field->size > 0) {
53
- index = repeated_field->size + index;
54
- }
55
- return index;
56
- }
57
-
58
- VALUE RepeatedField_subarray(VALUE _self, long beg, long len) {
59
- RepeatedField* self = ruby_to_RepeatedField(_self);
60
- int element_size = native_slot_size(self->field_type);
61
- upb_fieldtype_t field_type = self->field_type;
62
- VALUE field_type_class = self->field_type_class;
63
-
64
- size_t off = beg * element_size;
65
- VALUE ary = rb_ary_new2(len);
66
- for (int i = beg; i < beg + len; i++, off += element_size) {
67
- void* mem = ((uint8_t *)self->elements) + off;
68
- VALUE elem = native_slot_get(field_type, field_type_class, mem);
69
- rb_ary_push(ary, elem);
70
- }
71
- return ary;
72
- }
73
-
74
50
  /*
75
51
  * call-seq:
76
52
  * RepeatedField.each(&block)
@@ -91,58 +67,29 @@ VALUE RepeatedField_each(VALUE _self) {
91
67
  VALUE val = native_slot_get(field_type, field_type_class, memory);
92
68
  rb_yield(val);
93
69
  }
94
- return _self;
70
+ return Qnil;
95
71
  }
96
72
 
97
-
98
73
  /*
99
74
  * call-seq:
100
75
  * RepeatedField.[](index) => value
101
76
  *
102
- * Accesses the element at the given index. Returns nil on out-of-bounds
77
+ * Accesses the element at the given index. Throws an exception on out-of-bounds
78
+ * errors.
103
79
  */
104
- VALUE RepeatedField_index(int argc, VALUE* argv, VALUE _self) {
80
+ VALUE RepeatedField_index(VALUE _self, VALUE _index) {
105
81
  RepeatedField* self = ruby_to_RepeatedField(_self);
106
82
  int element_size = native_slot_size(self->field_type);
107
83
  upb_fieldtype_t field_type = self->field_type;
108
84
  VALUE field_type_class = self->field_type_class;
109
85
 
110
- VALUE arg = argv[0];
111
- long beg, len;
112
-
113
- if (argc == 1){
114
- if (FIXNUM_P(arg)) {
115
- /* standard case */
116
- int index = index_position(argv[0], self);
117
- if (index < 0 || index >= self->size) {
118
- return Qnil;
119
- }
120
- void* memory = (void *) (((uint8_t *)self->elements) +
121
- index * element_size);
122
- return native_slot_get(field_type, field_type_class, memory);
123
- }else{
124
- /* check if idx is Range */
125
- size_t off;
126
- switch (rb_range_beg_len(arg, &beg, &len, self->size, 0)) {
127
- case Qfalse:
128
- break;
129
- case Qnil:
130
- return Qnil;
131
- default:
132
- return RepeatedField_subarray(_self, beg, len);
133
- }
134
- }
135
- }
136
- /* assume 2 arguments */
137
- beg = NUM2LONG(argv[0]);
138
- len = NUM2LONG(argv[1]);
139
- if (beg < 0) {
140
- beg += self->size;
141
- }
142
- if (beg >= self->size) {
143
- return Qnil;
86
+ int index = NUM2INT(_index);
87
+ if (index < 0 || index >= self->size) {
88
+ rb_raise(rb_eRangeError, "Index out of range");
144
89
  }
145
- return RepeatedField_subarray(_self, beg, len);
90
+
91
+ void* memory = (void *) (((uint8_t *)self->elements) + index * element_size);
92
+ return native_slot_get(field_type, field_type_class, memory);
146
93
  }
147
94
 
148
95
  /*
@@ -158,9 +105,9 @@ VALUE RepeatedField_index_set(VALUE _self, VALUE _index, VALUE val) {
158
105
  VALUE field_type_class = self->field_type_class;
159
106
  int element_size = native_slot_size(field_type);
160
107
 
161
- int index = index_position(_index, self);
108
+ int index = NUM2INT(_index);
162
109
  if (index < 0 || index >= (INT_MAX - 1)) {
163
- return Qnil;
110
+ rb_raise(rb_eRangeError, "Index out of range");
164
111
  }
165
112
  if (index >= self->size) {
166
113
  RepeatedField_reserve(self, index + 1);
@@ -218,7 +165,6 @@ VALUE RepeatedField_push(VALUE _self, VALUE val) {
218
165
  return _self;
219
166
  }
220
167
 
221
-
222
168
  // Used by parsing handlers.
223
169
  void RepeatedField_push_native(VALUE _self, void* data) {
224
170
  RepeatedField* self = ruby_to_RepeatedField(_self);
@@ -239,15 +185,19 @@ void* RepeatedField_index_native(VALUE _self, int index) {
239
185
  }
240
186
 
241
187
  /*
242
- * Private ruby method, used by RepeatedField.pop
188
+ * call-seq:
189
+ * RepeatedField.pop => value
190
+ *
191
+ * Removes the last element and returns it. Throws an exception if the repeated
192
+ * field is empty.
243
193
  */
244
- VALUE RepeatedField_pop_one(VALUE _self) {
194
+ VALUE RepeatedField_pop(VALUE _self) {
245
195
  RepeatedField* self = ruby_to_RepeatedField(_self);
246
196
  upb_fieldtype_t field_type = self->field_type;
247
197
  VALUE field_type_class = self->field_type_class;
248
198
  int element_size = native_slot_size(field_type);
249
199
  if (self->size == 0) {
250
- return Qnil;
200
+ rb_raise(rb_eRangeError, "Pop from empty repeated field is not allowed.");
251
201
  }
252
202
  int index = self->size - 1;
253
203
  void* memory = (void *) (((uint8_t *)self->elements) + index * element_size);
@@ -256,6 +206,19 @@ VALUE RepeatedField_pop_one(VALUE _self) {
256
206
  return ret;
257
207
  }
258
208
 
209
+ /*
210
+ * call-seq:
211
+ * RepeatedField.insert(*args)
212
+ *
213
+ * Pushes each arg in turn onto the end of the repeated field.
214
+ */
215
+ VALUE RepeatedField_insert(int argc, VALUE* argv, VALUE _self) {
216
+ for (int i = 0; i < argc; i++) {
217
+ RepeatedField_push(_self, argv[i]);
218
+ }
219
+ return Qnil;
220
+ }
221
+
259
222
  /*
260
223
  * call-seq:
261
224
  * RepeatedField.replace(list)
@@ -269,7 +232,7 @@ VALUE RepeatedField_replace(VALUE _self, VALUE list) {
269
232
  for (int i = 0; i < RARRAY_LEN(list); i++) {
270
233
  RepeatedField_push(_self, rb_ary_entry(list, i));
271
234
  }
272
- return list;
235
+ return Qnil;
273
236
  }
274
237
 
275
238
  /*
@@ -281,7 +244,7 @@ VALUE RepeatedField_replace(VALUE _self, VALUE list) {
281
244
  VALUE RepeatedField_clear(VALUE _self) {
282
245
  RepeatedField* self = ruby_to_RepeatedField(_self);
283
246
  self->size = 0;
284
- return _self;
247
+ return Qnil;
285
248
  }
286
249
 
287
250
  /*
@@ -370,6 +333,7 @@ VALUE RepeatedField_to_ary(VALUE _self) {
370
333
  for (int i = 0; i < self->size; i++, off += elem_size) {
371
334
  void* mem = ((uint8_t *)self->elements) + off;
372
335
  VALUE elem = native_slot_get(field_type, self->field_type_class, mem);
336
+
373
337
  rb_ary_push(ary, elem);
374
338
  }
375
339
  return ary;
@@ -445,6 +409,19 @@ VALUE RepeatedField_hash(VALUE _self) {
445
409
  return hash;
446
410
  }
447
411
 
412
+ /*
413
+ * call-seq:
414
+ * RepeatedField.inspect => string
415
+ *
416
+ * Returns a string representing this repeated field's elements. It will be
417
+ * formated as "[<element>, <element>, ...]", with each element's string
418
+ * representation computed by its own #inspect method.
419
+ */
420
+ VALUE RepeatedField_inspect(VALUE _self) {
421
+ VALUE self_ary = RepeatedField_to_ary(_self);
422
+ return rb_funcall(self_ary, rb_intern("inspect"), 0);
423
+ }
424
+
448
425
  /*
449
426
  * call-seq:
450
427
  * RepeatedField.+(other) => repeated field
@@ -481,30 +458,14 @@ VALUE RepeatedField_plus(VALUE _self, VALUE list) {
481
458
  return dupped;
482
459
  }
483
460
 
484
- /*
485
- * call-seq:
486
- * RepeatedField.concat(other) => self
487
- *
488
- * concats the passed in array to self. Returns a Ruby array.
489
- */
490
- VALUE RepeatedField_concat(VALUE _self, VALUE list) {
491
- RepeatedField* self = ruby_to_RepeatedField(_self);
492
- Check_Type(list, T_ARRAY);
493
- for (int i = 0; i < RARRAY_LEN(list); i++) {
494
- RepeatedField_push(_self, rb_ary_entry(list, i));
495
- }
496
- return _self;
497
- }
498
-
499
-
500
461
  void validate_type_class(upb_fieldtype_t type, VALUE klass) {
501
- if (rb_ivar_get(klass, descriptor_instancevar_interned) == Qnil) {
462
+ if (rb_iv_get(klass, kDescriptorInstanceVar) == Qnil) {
502
463
  rb_raise(rb_eArgError,
503
464
  "Type class has no descriptor. Please pass a "
504
465
  "class or enum as returned by the DescriptorPool.");
505
466
  }
506
467
  if (type == UPB_TYPE_MESSAGE) {
507
- VALUE desc = rb_ivar_get(klass, descriptor_instancevar_interned);
468
+ VALUE desc = rb_iv_get(klass, kDescriptorInstanceVar);
508
469
  if (!RB_TYPE_P(desc, T_DATA) || !RTYPEDDATA_P(desc) ||
509
470
  RTYPEDDATA_TYPE(desc) != &_Descriptor_type) {
510
471
  rb_raise(rb_eArgError, "Descriptor has an incorrect type.");
@@ -514,7 +475,7 @@ void validate_type_class(upb_fieldtype_t type, VALUE klass) {
514
475
  "Message class was not returned by the DescriptorPool.");
515
476
  }
516
477
  } else if (type == UPB_TYPE_ENUM) {
517
- VALUE enumdesc = rb_ivar_get(klass, descriptor_instancevar_interned);
478
+ VALUE enumdesc = rb_iv_get(klass, kDescriptorInstanceVar);
518
479
  if (!RB_TYPE_P(enumdesc, T_DATA) || !RTYPEDDATA_P(enumdesc) ||
519
480
  RTYPEDDATA_TYPE(enumdesc) != &_EnumDescriptor_type) {
520
481
  rb_raise(rb_eArgError, "Descriptor has an incorrect type.");
@@ -616,23 +577,22 @@ void RepeatedField_register(VALUE module) {
616
577
  rb_define_method(klass, "initialize",
617
578
  RepeatedField_init, -1);
618
579
  rb_define_method(klass, "each", RepeatedField_each, 0);
619
- rb_define_method(klass, "[]", RepeatedField_index, -1);
620
- rb_define_method(klass, "at", RepeatedField_index, -1);
580
+ rb_define_method(klass, "[]", RepeatedField_index, 1);
621
581
  rb_define_method(klass, "[]=", RepeatedField_index_set, 2);
622
582
  rb_define_method(klass, "push", RepeatedField_push, 1);
623
583
  rb_define_method(klass, "<<", RepeatedField_push, 1);
624
- rb_define_private_method(klass, "pop_one", RepeatedField_pop_one, 0);
584
+ rb_define_method(klass, "pop", RepeatedField_pop, 0);
585
+ rb_define_method(klass, "insert", RepeatedField_insert, -1);
625
586
  rb_define_method(klass, "replace", RepeatedField_replace, 1);
626
587
  rb_define_method(klass, "clear", RepeatedField_clear, 0);
627
588
  rb_define_method(klass, "length", RepeatedField_length, 0);
628
- rb_define_method(klass, "size", RepeatedField_length, 0);
629
589
  rb_define_method(klass, "dup", RepeatedField_dup, 0);
630
590
  // Also define #clone so that we don't inherit Object#clone.
631
591
  rb_define_method(klass, "clone", RepeatedField_dup, 0);
632
592
  rb_define_method(klass, "==", RepeatedField_eq, 1);
633
593
  rb_define_method(klass, "to_ary", RepeatedField_to_ary, 0);
634
594
  rb_define_method(klass, "hash", RepeatedField_hash, 0);
595
+ rb_define_method(klass, "inspect", RepeatedField_inspect, 0);
635
596
  rb_define_method(klass, "+", RepeatedField_plus, 1);
636
- rb_define_method(klass, "concat", RepeatedField_concat, 1);
637
597
  rb_include_module(klass, rb_mEnumerable);
638
598
  }
@@ -155,9 +155,7 @@ void native_slot_set_value_and_case(upb_fieldtype_t type, VALUE type_class,
155
155
  break;
156
156
  }
157
157
  case UPB_TYPE_MESSAGE: {
158
- if (CLASS_OF(value) == CLASS_OF(Qnil)) {
159
- value = Qnil;
160
- } else if (CLASS_OF(value) != type_class) {
158
+ if (CLASS_OF(value) != type_class) {
161
159
  rb_raise(rb_eTypeError,
162
160
  "Invalid type %s to assign to submessage field.",
163
161
  rb_class2name(CLASS_OF(value)));
@@ -415,8 +413,7 @@ MessageLayout* create_layout(const upb_msgdef* msgdef) {
415
413
  // Align current offset up to |size| granularity.
416
414
  off = align_up_to(off, field_size);
417
415
  layout->fields[upb_fielddef_index(field)].offset = off;
418
- layout->fields[upb_fielddef_index(field)].case_offset =
419
- MESSAGE_FIELD_NO_CASE;
416
+ layout->fields[upb_fielddef_index(field)].case_offset = MESSAGE_FIELD_NO_CASE;
420
417
  off += field_size;
421
418
  }
422
419