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 #include <math.h>
34
35 #include <ruby/encoding.h>
36
37 // -----------------------------------------------------------------------------
38 // Ruby <-> native slot management.
39 // -----------------------------------------------------------------------------
40
41 #define DEREF(memory, type) *(type*)(memory)
42
native_slot_size(upb_fieldtype_t type)43 size_t native_slot_size(upb_fieldtype_t type) {
44 switch (type) {
45 case UPB_TYPE_FLOAT: return 4;
46 case UPB_TYPE_DOUBLE: return 8;
47 case UPB_TYPE_BOOL: return 1;
48 case UPB_TYPE_STRING: return sizeof(VALUE);
49 case UPB_TYPE_BYTES: return sizeof(VALUE);
50 case UPB_TYPE_MESSAGE: return sizeof(VALUE);
51 case UPB_TYPE_ENUM: return 4;
52 case UPB_TYPE_INT32: return 4;
53 case UPB_TYPE_INT64: return 8;
54 case UPB_TYPE_UINT32: return 4;
55 case UPB_TYPE_UINT64: return 8;
56 default: return 0;
57 }
58 }
59
value_from_default(const upb_fielddef * field)60 static VALUE value_from_default(const upb_fielddef *field) {
61 switch (upb_fielddef_type(field)) {
62 case UPB_TYPE_FLOAT: return DBL2NUM(upb_fielddef_defaultfloat(field));
63 case UPB_TYPE_DOUBLE: return DBL2NUM(upb_fielddef_defaultdouble(field));
64 case UPB_TYPE_BOOL:
65 return upb_fielddef_defaultbool(field) ? Qtrue : Qfalse;
66 case UPB_TYPE_MESSAGE: return Qnil;
67 case UPB_TYPE_ENUM: {
68 const upb_enumdef *enumdef = upb_fielddef_enumsubdef(field);
69 int32_t num = upb_fielddef_defaultint32(field);
70 const char *label = upb_enumdef_iton(enumdef, num);
71 if (label) {
72 return ID2SYM(rb_intern(label));
73 } else {
74 return INT2NUM(num);
75 }
76 }
77 case UPB_TYPE_INT32: return INT2NUM(upb_fielddef_defaultint32(field));
78 case UPB_TYPE_INT64: return LL2NUM(upb_fielddef_defaultint64(field));;
79 case UPB_TYPE_UINT32: return UINT2NUM(upb_fielddef_defaultuint32(field));
80 case UPB_TYPE_UINT64: return ULL2NUM(upb_fielddef_defaultuint64(field));
81 case UPB_TYPE_STRING:
82 case UPB_TYPE_BYTES: {
83 size_t size;
84 const char *str = upb_fielddef_defaultstr(field, &size);
85 return rb_str_new(str, size);
86 }
87 default: return Qnil;
88 }
89 }
90
is_ruby_num(VALUE value)91 static bool is_ruby_num(VALUE value) {
92 return (TYPE(value) == T_FLOAT ||
93 TYPE(value) == T_FIXNUM ||
94 TYPE(value) == T_BIGNUM);
95 }
96
native_slot_check_int_range_precision(upb_fieldtype_t type,VALUE val)97 void native_slot_check_int_range_precision(upb_fieldtype_t type, VALUE val) {
98 if (!is_ruby_num(val)) {
99 rb_raise(rb_eTypeError, "Expected number type for integral field.");
100 }
101
102 // NUM2{INT,UINT,LL,ULL} macros do the appropriate range checks on upper
103 // bound; we just need to do precision checks (i.e., disallow rounding) and
104 // check for < 0 on unsigned types.
105 if (TYPE(val) == T_FLOAT) {
106 double dbl_val = NUM2DBL(val);
107 if (floor(dbl_val) != dbl_val) {
108 rb_raise(rb_eRangeError,
109 "Non-integral floating point value assigned to integer field.");
110 }
111 }
112 if (type == UPB_TYPE_UINT32 || type == UPB_TYPE_UINT64) {
113 if (NUM2DBL(val) < 0) {
114 rb_raise(rb_eRangeError,
115 "Assigning negative value to unsigned integer field.");
116 }
117 }
118 }
119
native_slot_encode_and_freeze_string(upb_fieldtype_t type,VALUE value)120 VALUE native_slot_encode_and_freeze_string(upb_fieldtype_t type, VALUE value) {
121 rb_encoding* desired_encoding = (type == UPB_TYPE_STRING) ?
122 kRubyStringUtf8Encoding : kRubyString8bitEncoding;
123 VALUE desired_encoding_value = rb_enc_from_encoding(desired_encoding);
124
125 // Note: this will not duplicate underlying string data unless necessary.
126 value = rb_str_encode(value, desired_encoding_value, 0, Qnil);
127
128 if (type == UPB_TYPE_STRING &&
129 rb_enc_str_coderange(value) == ENC_CODERANGE_BROKEN) {
130 rb_raise(rb_eEncodingError, "String is invalid UTF-8");
131 }
132
133 // Ensure the data remains valid. Since we called #encode a moment ago,
134 // this does not freeze the string the user assigned.
135 rb_obj_freeze(value);
136
137 return value;
138 }
139
native_slot_set(upb_fieldtype_t type,VALUE type_class,void * memory,VALUE value)140 void native_slot_set(upb_fieldtype_t type, VALUE type_class,
141 void* memory, VALUE value) {
142 native_slot_set_value_and_case(type, type_class, memory, value, NULL, 0);
143 }
144
native_slot_set_value_and_case(upb_fieldtype_t type,VALUE type_class,void * memory,VALUE value,uint32_t * case_memory,uint32_t case_number)145 void native_slot_set_value_and_case(upb_fieldtype_t type, VALUE type_class,
146 void* memory, VALUE value,
147 uint32_t* case_memory,
148 uint32_t case_number) {
149 // Note that in order to atomically change the value in memory and the case
150 // value (w.r.t. Ruby VM calls), we must set the value at |memory| only after
151 // all Ruby VM calls are complete. The case is then set at the bottom of this
152 // function.
153 switch (type) {
154 case UPB_TYPE_FLOAT:
155 if (!is_ruby_num(value)) {
156 rb_raise(rb_eTypeError, "Expected number type for float field.");
157 }
158 DEREF(memory, float) = NUM2DBL(value);
159 break;
160 case UPB_TYPE_DOUBLE:
161 if (!is_ruby_num(value)) {
162 rb_raise(rb_eTypeError, "Expected number type for double field.");
163 }
164 DEREF(memory, double) = NUM2DBL(value);
165 break;
166 case UPB_TYPE_BOOL: {
167 int8_t val = -1;
168 if (value == Qtrue) {
169 val = 1;
170 } else if (value == Qfalse) {
171 val = 0;
172 } else {
173 rb_raise(rb_eTypeError, "Invalid argument for boolean field.");
174 }
175 DEREF(memory, int8_t) = val;
176 break;
177 }
178 case UPB_TYPE_STRING:
179 case UPB_TYPE_BYTES: {
180 if (CLASS_OF(value) != rb_cString) {
181 rb_raise(rb_eTypeError, "Invalid argument for string field.");
182 }
183
184 DEREF(memory, VALUE) = native_slot_encode_and_freeze_string(type, value);
185 break;
186 }
187 case UPB_TYPE_MESSAGE: {
188 if (CLASS_OF(value) == CLASS_OF(Qnil)) {
189 value = Qnil;
190 } else if (CLASS_OF(value) != type_class) {
191 rb_raise(rb_eTypeError,
192 "Invalid type %s to assign to submessage field.",
193 rb_class2name(CLASS_OF(value)));
194 }
195 DEREF(memory, VALUE) = value;
196 break;
197 }
198 case UPB_TYPE_ENUM: {
199 int32_t int_val = 0;
200 if (!is_ruby_num(value) && TYPE(value) != T_SYMBOL) {
201 rb_raise(rb_eTypeError,
202 "Expected number or symbol type for enum field.");
203 }
204 if (TYPE(value) == T_SYMBOL) {
205 // Ensure that the given symbol exists in the enum module.
206 VALUE lookup = rb_funcall(type_class, rb_intern("resolve"), 1, value);
207 if (lookup == Qnil) {
208 rb_raise(rb_eRangeError, "Unknown symbol value for enum field.");
209 } else {
210 int_val = NUM2INT(lookup);
211 }
212 } else {
213 native_slot_check_int_range_precision(UPB_TYPE_INT32, value);
214 int_val = NUM2INT(value);
215 }
216 DEREF(memory, int32_t) = int_val;
217 break;
218 }
219 case UPB_TYPE_INT32:
220 case UPB_TYPE_INT64:
221 case UPB_TYPE_UINT32:
222 case UPB_TYPE_UINT64:
223 native_slot_check_int_range_precision(type, value);
224 switch (type) {
225 case UPB_TYPE_INT32:
226 DEREF(memory, int32_t) = NUM2INT(value);
227 break;
228 case UPB_TYPE_INT64:
229 DEREF(memory, int64_t) = NUM2LL(value);
230 break;
231 case UPB_TYPE_UINT32:
232 DEREF(memory, uint32_t) = NUM2UINT(value);
233 break;
234 case UPB_TYPE_UINT64:
235 DEREF(memory, uint64_t) = NUM2ULL(value);
236 break;
237 default:
238 break;
239 }
240 break;
241 default:
242 break;
243 }
244
245 if (case_memory != NULL) {
246 *case_memory = case_number;
247 }
248 }
249
native_slot_get(upb_fieldtype_t type,VALUE type_class,const void * memory)250 VALUE native_slot_get(upb_fieldtype_t type,
251 VALUE type_class,
252 const void* memory) {
253 switch (type) {
254 case UPB_TYPE_FLOAT:
255 return DBL2NUM(DEREF(memory, float));
256 case UPB_TYPE_DOUBLE:
257 return DBL2NUM(DEREF(memory, double));
258 case UPB_TYPE_BOOL:
259 return DEREF(memory, int8_t) ? Qtrue : Qfalse;
260 case UPB_TYPE_STRING:
261 case UPB_TYPE_BYTES:
262 case UPB_TYPE_MESSAGE:
263 return DEREF(memory, VALUE);
264 case UPB_TYPE_ENUM: {
265 int32_t val = DEREF(memory, int32_t);
266 VALUE symbol = enum_lookup(type_class, INT2NUM(val));
267 if (symbol == Qnil) {
268 return INT2NUM(val);
269 } else {
270 return symbol;
271 }
272 }
273 case UPB_TYPE_INT32:
274 return INT2NUM(DEREF(memory, int32_t));
275 case UPB_TYPE_INT64:
276 return LL2NUM(DEREF(memory, int64_t));
277 case UPB_TYPE_UINT32:
278 return UINT2NUM(DEREF(memory, uint32_t));
279 case UPB_TYPE_UINT64:
280 return ULL2NUM(DEREF(memory, uint64_t));
281 default:
282 return Qnil;
283 }
284 }
285
native_slot_init(upb_fieldtype_t type,void * memory)286 void native_slot_init(upb_fieldtype_t type, void* memory) {
287 switch (type) {
288 case UPB_TYPE_FLOAT:
289 DEREF(memory, float) = 0.0;
290 break;
291 case UPB_TYPE_DOUBLE:
292 DEREF(memory, double) = 0.0;
293 break;
294 case UPB_TYPE_BOOL:
295 DEREF(memory, int8_t) = 0;
296 break;
297 case UPB_TYPE_STRING:
298 case UPB_TYPE_BYTES:
299 DEREF(memory, VALUE) = rb_str_new2("");
300 rb_enc_associate(DEREF(memory, VALUE), (type == UPB_TYPE_BYTES) ?
301 kRubyString8bitEncoding : kRubyStringUtf8Encoding);
302 break;
303 case UPB_TYPE_MESSAGE:
304 DEREF(memory, VALUE) = Qnil;
305 break;
306 case UPB_TYPE_ENUM:
307 case UPB_TYPE_INT32:
308 DEREF(memory, int32_t) = 0;
309 break;
310 case UPB_TYPE_INT64:
311 DEREF(memory, int64_t) = 0;
312 break;
313 case UPB_TYPE_UINT32:
314 DEREF(memory, uint32_t) = 0;
315 break;
316 case UPB_TYPE_UINT64:
317 DEREF(memory, uint64_t) = 0;
318 break;
319 default:
320 break;
321 }
322 }
323
native_slot_mark(upb_fieldtype_t type,void * memory)324 void native_slot_mark(upb_fieldtype_t type, void* memory) {
325 switch (type) {
326 case UPB_TYPE_STRING:
327 case UPB_TYPE_BYTES:
328 case UPB_TYPE_MESSAGE:
329 rb_gc_mark(DEREF(memory, VALUE));
330 break;
331 default:
332 break;
333 }
334 }
335
native_slot_dup(upb_fieldtype_t type,void * to,void * from)336 void native_slot_dup(upb_fieldtype_t type, void* to, void* from) {
337 memcpy(to, from, native_slot_size(type));
338 }
339
native_slot_deep_copy(upb_fieldtype_t type,void * to,void * from)340 void native_slot_deep_copy(upb_fieldtype_t type, void* to, void* from) {
341 switch (type) {
342 case UPB_TYPE_STRING:
343 case UPB_TYPE_BYTES: {
344 VALUE from_val = DEREF(from, VALUE);
345 DEREF(to, VALUE) = (from_val != Qnil) ?
346 rb_funcall(from_val, rb_intern("dup"), 0) : Qnil;
347 break;
348 }
349 case UPB_TYPE_MESSAGE: {
350 VALUE from_val = DEREF(from, VALUE);
351 DEREF(to, VALUE) = (from_val != Qnil) ?
352 Message_deep_copy(from_val) : Qnil;
353 break;
354 }
355 default:
356 memcpy(to, from, native_slot_size(type));
357 }
358 }
359
native_slot_eq(upb_fieldtype_t type,void * mem1,void * mem2)360 bool native_slot_eq(upb_fieldtype_t type, void* mem1, void* mem2) {
361 switch (type) {
362 case UPB_TYPE_STRING:
363 case UPB_TYPE_BYTES:
364 case UPB_TYPE_MESSAGE: {
365 VALUE val1 = DEREF(mem1, VALUE);
366 VALUE val2 = DEREF(mem2, VALUE);
367 VALUE ret = rb_funcall(val1, rb_intern("=="), 1, val2);
368 return ret == Qtrue;
369 }
370 default:
371 return !memcmp(mem1, mem2, native_slot_size(type));
372 }
373 }
374
375 // -----------------------------------------------------------------------------
376 // Map field utilities.
377 // -----------------------------------------------------------------------------
378
tryget_map_entry_msgdef(const upb_fielddef * field)379 const upb_msgdef* tryget_map_entry_msgdef(const upb_fielddef* field) {
380 const upb_msgdef* subdef;
381 if (upb_fielddef_label(field) != UPB_LABEL_REPEATED ||
382 upb_fielddef_type(field) != UPB_TYPE_MESSAGE) {
383 return NULL;
384 }
385 subdef = upb_fielddef_msgsubdef(field);
386 return upb_msgdef_mapentry(subdef) ? subdef : NULL;
387 }
388
map_entry_msgdef(const upb_fielddef * field)389 const upb_msgdef *map_entry_msgdef(const upb_fielddef* field) {
390 const upb_msgdef* subdef = tryget_map_entry_msgdef(field);
391 assert(subdef);
392 return subdef;
393 }
394
is_map_field(const upb_fielddef * field)395 bool is_map_field(const upb_fielddef *field) {
396 return tryget_map_entry_msgdef(field) != NULL;
397 }
398
map_field_key(const upb_fielddef * field)399 const upb_fielddef* map_field_key(const upb_fielddef* field) {
400 const upb_msgdef* subdef = map_entry_msgdef(field);
401 return map_entry_key(subdef);
402 }
403
map_field_value(const upb_fielddef * field)404 const upb_fielddef* map_field_value(const upb_fielddef* field) {
405 const upb_msgdef* subdef = map_entry_msgdef(field);
406 return map_entry_value(subdef);
407 }
408
map_entry_key(const upb_msgdef * msgdef)409 const upb_fielddef* map_entry_key(const upb_msgdef* msgdef) {
410 const upb_fielddef* key_field = upb_msgdef_itof(msgdef, MAP_KEY_FIELD);
411 assert(key_field != NULL);
412 return key_field;
413 }
414
map_entry_value(const upb_msgdef * msgdef)415 const upb_fielddef* map_entry_value(const upb_msgdef* msgdef) {
416 const upb_fielddef* value_field = upb_msgdef_itof(msgdef, MAP_VALUE_FIELD);
417 assert(value_field != NULL);
418 return value_field;
419 }
420
421 // -----------------------------------------------------------------------------
422 // Memory layout management.
423 // -----------------------------------------------------------------------------
424
align_up_to(size_t offset,size_t granularity)425 static size_t align_up_to(size_t offset, size_t granularity) {
426 // Granularity must be a power of two.
427 return (offset + granularity - 1) & ~(granularity - 1);
428 }
429
create_layout(const upb_msgdef * msgdef)430 MessageLayout* create_layout(const upb_msgdef* msgdef) {
431 MessageLayout* layout = ALLOC(MessageLayout);
432 int nfields = upb_msgdef_numfields(msgdef);
433 upb_msg_field_iter it;
434 upb_msg_oneof_iter oit;
435 size_t off = 0;
436
437 layout->fields = ALLOC_N(MessageField, nfields);
438
439 for (upb_msg_field_begin(&it, msgdef);
440 !upb_msg_field_done(&it);
441 upb_msg_field_next(&it)) {
442 const upb_fielddef* field = upb_msg_iter_field(&it);
443 size_t field_size;
444
445 if (upb_fielddef_containingoneof(field)) {
446 // Oneofs are handled separately below.
447 continue;
448 }
449
450 // Allocate |field_size| bytes for this field in the layout.
451 field_size = 0;
452 if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
453 field_size = sizeof(VALUE);
454 } else {
455 field_size = native_slot_size(upb_fielddef_type(field));
456 }
457 // Align current offset up to |size| granularity.
458 off = align_up_to(off, field_size);
459 layout->fields[upb_fielddef_index(field)].offset = off;
460 layout->fields[upb_fielddef_index(field)].case_offset =
461 MESSAGE_FIELD_NO_CASE;
462 off += field_size;
463 }
464
465 // Handle oneofs now -- we iterate over oneofs specifically and allocate only
466 // one slot per oneof.
467 //
468 // We assign all value slots first, then pack the 'case' fields at the end,
469 // since in the common case (modern 64-bit platform) these are 8 bytes and 4
470 // bytes respectively and we want to avoid alignment overhead.
471 //
472 // Note that we reserve 4 bytes (a uint32) per 'case' slot because the value
473 // space for oneof cases is conceptually as wide as field tag numbers. In
474 // practice, it's unlikely that a oneof would have more than e.g. 256 or 64K
475 // members (8 or 16 bits respectively), so conceivably we could assign
476 // consecutive case numbers and then pick a smaller oneof case slot size, but
477 // the complexity to implement this indirection is probably not worthwhile.
478 for (upb_msg_oneof_begin(&oit, msgdef);
479 !upb_msg_oneof_done(&oit);
480 upb_msg_oneof_next(&oit)) {
481 const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit);
482 upb_oneof_iter fit;
483
484 // Always allocate NATIVE_SLOT_MAX_SIZE bytes, but share the slot between
485 // all fields.
486 size_t field_size = NATIVE_SLOT_MAX_SIZE;
487 // Align the offset.
488 off = align_up_to(off, field_size);
489 // Assign all fields in the oneof this same offset.
490 for (upb_oneof_begin(&fit, oneof);
491 !upb_oneof_done(&fit);
492 upb_oneof_next(&fit)) {
493 const upb_fielddef* field = upb_oneof_iter_field(&fit);
494 layout->fields[upb_fielddef_index(field)].offset = off;
495 }
496 off += field_size;
497 }
498
499 // Now the case fields.
500 for (upb_msg_oneof_begin(&oit, msgdef);
501 !upb_msg_oneof_done(&oit);
502 upb_msg_oneof_next(&oit)) {
503 const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit);
504 upb_oneof_iter fit;
505
506 size_t field_size = sizeof(uint32_t);
507 // Align the offset.
508 off = (off + field_size - 1) & ~(field_size - 1);
509 // Assign all fields in the oneof this same offset.
510 for (upb_oneof_begin(&fit, oneof);
511 !upb_oneof_done(&fit);
512 upb_oneof_next(&fit)) {
513 const upb_fielddef* field = upb_oneof_iter_field(&fit);
514 layout->fields[upb_fielddef_index(field)].case_offset = off;
515 }
516 off += field_size;
517 }
518
519 layout->size = off;
520
521 layout->msgdef = msgdef;
522 upb_msgdef_ref(layout->msgdef, &layout->msgdef);
523
524 return layout;
525 }
526
free_layout(MessageLayout * layout)527 void free_layout(MessageLayout* layout) {
528 xfree(layout->fields);
529 upb_msgdef_unref(layout->msgdef, &layout->msgdef);
530 xfree(layout);
531 }
532
field_type_class(const upb_fielddef * field)533 VALUE field_type_class(const upb_fielddef* field) {
534 VALUE type_class = Qnil;
535 if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) {
536 VALUE submsgdesc =
537 get_def_obj(upb_fielddef_subdef(field));
538 type_class = Descriptor_msgclass(submsgdesc);
539 } else if (upb_fielddef_type(field) == UPB_TYPE_ENUM) {
540 VALUE subenumdesc =
541 get_def_obj(upb_fielddef_subdef(field));
542 type_class = EnumDescriptor_enummodule(subenumdesc);
543 }
544 return type_class;
545 }
546
slot_memory(MessageLayout * layout,const void * storage,const upb_fielddef * field)547 static void* slot_memory(MessageLayout* layout,
548 const void* storage,
549 const upb_fielddef* field) {
550 return ((uint8_t *)storage) +
551 layout->fields[upb_fielddef_index(field)].offset;
552 }
553
slot_oneof_case(MessageLayout * layout,const void * storage,const upb_fielddef * field)554 static uint32_t* slot_oneof_case(MessageLayout* layout,
555 const void* storage,
556 const upb_fielddef* field) {
557 return (uint32_t *)(((uint8_t *)storage) +
558 layout->fields[upb_fielddef_index(field)].case_offset);
559 }
560
561
layout_get(MessageLayout * layout,const void * storage,const upb_fielddef * field)562 VALUE layout_get(MessageLayout* layout,
563 const void* storage,
564 const upb_fielddef* field) {
565 void* memory = slot_memory(layout, storage, field);
566 uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
567
568 if (upb_fielddef_containingoneof(field)) {
569 if (*oneof_case != upb_fielddef_number(field)) {
570 return value_from_default(field);
571 }
572 return native_slot_get(upb_fielddef_type(field),
573 field_type_class(field),
574 memory);
575 } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
576 return *((VALUE *)memory);
577 } else {
578 return native_slot_get(upb_fielddef_type(field),
579 field_type_class(field),
580 memory);
581 }
582 }
583
check_repeated_field_type(VALUE val,const upb_fielddef * field)584 static void check_repeated_field_type(VALUE val, const upb_fielddef* field) {
585 RepeatedField* self;
586 assert(upb_fielddef_label(field) == UPB_LABEL_REPEATED);
587
588 if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
589 RTYPEDDATA_TYPE(val) != &RepeatedField_type) {
590 rb_raise(rb_eTypeError, "Expected repeated field array");
591 }
592
593 self = ruby_to_RepeatedField(val);
594 if (self->field_type != upb_fielddef_type(field)) {
595 rb_raise(rb_eTypeError, "Repeated field array has wrong element type");
596 }
597
598 if (self->field_type == UPB_TYPE_MESSAGE ||
599 self->field_type == UPB_TYPE_ENUM) {
600 if (self->field_type_class !=
601 get_def_obj(upb_fielddef_subdef(field))) {
602 rb_raise(rb_eTypeError,
603 "Repeated field array has wrong message/enum class");
604 }
605 }
606 }
607
check_map_field_type(VALUE val,const upb_fielddef * field)608 static void check_map_field_type(VALUE val, const upb_fielddef* field) {
609 const upb_fielddef* key_field = map_field_key(field);
610 const upb_fielddef* value_field = map_field_value(field);
611 Map* self;
612
613 if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
614 RTYPEDDATA_TYPE(val) != &Map_type) {
615 rb_raise(rb_eTypeError, "Expected Map instance");
616 }
617
618 self = ruby_to_Map(val);
619 if (self->key_type != upb_fielddef_type(key_field)) {
620 rb_raise(rb_eTypeError, "Map key type does not match field's key type");
621 }
622 if (self->value_type != upb_fielddef_type(value_field)) {
623 rb_raise(rb_eTypeError, "Map value type does not match field's value type");
624 }
625 if (upb_fielddef_type(value_field) == UPB_TYPE_MESSAGE ||
626 upb_fielddef_type(value_field) == UPB_TYPE_ENUM) {
627 if (self->value_type_class !=
628 get_def_obj(upb_fielddef_subdef(value_field))) {
629 rb_raise(rb_eTypeError,
630 "Map value type has wrong message/enum class");
631 }
632 }
633 }
634
635
layout_set(MessageLayout * layout,void * storage,const upb_fielddef * field,VALUE val)636 void layout_set(MessageLayout* layout,
637 void* storage,
638 const upb_fielddef* field,
639 VALUE val) {
640 void* memory = slot_memory(layout, storage, field);
641 uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
642
643 if (upb_fielddef_containingoneof(field)) {
644 if (val == Qnil) {
645 // Assigning nil to a oneof field clears the oneof completely.
646 *oneof_case = ONEOF_CASE_NONE;
647 memset(memory, 0, NATIVE_SLOT_MAX_SIZE);
648 } else {
649 // The transition between field types for a single oneof (union) slot is
650 // somewhat complex because we need to ensure that a GC triggered at any
651 // point by a call into the Ruby VM sees a valid state for this field and
652 // does not either go off into the weeds (following what it thinks is a
653 // VALUE but is actually a different field type) or miss an object (seeing
654 // what it thinks is a primitive field but is actually a VALUE for the new
655 // field type).
656 //
657 // In order for the transition to be safe, the oneof case slot must be in
658 // sync with the value slot whenever the Ruby VM has been called. Thus, we
659 // use native_slot_set_value_and_case(), which ensures that both the value
660 // and case number are altered atomically (w.r.t. the Ruby VM).
661 native_slot_set_value_and_case(
662 upb_fielddef_type(field), field_type_class(field),
663 memory, val,
664 oneof_case, upb_fielddef_number(field));
665 }
666 } else if (is_map_field(field)) {
667 check_map_field_type(val, field);
668 DEREF(memory, VALUE) = val;
669 } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
670 check_repeated_field_type(val, field);
671 DEREF(memory, VALUE) = val;
672 } else {
673 native_slot_set(upb_fielddef_type(field), field_type_class(field),
674 memory, val);
675 }
676 }
677
layout_init(MessageLayout * layout,void * storage)678 void layout_init(MessageLayout* layout,
679 void* storage) {
680 upb_msg_field_iter it;
681 for (upb_msg_field_begin(&it, layout->msgdef);
682 !upb_msg_field_done(&it);
683 upb_msg_field_next(&it)) {
684 const upb_fielddef* field = upb_msg_iter_field(&it);
685 void* memory = slot_memory(layout, storage, field);
686 uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
687
688 if (upb_fielddef_containingoneof(field)) {
689 memset(memory, 0, NATIVE_SLOT_MAX_SIZE);
690 *oneof_case = ONEOF_CASE_NONE;
691 } else if (is_map_field(field)) {
692 VALUE map = Qnil;
693
694 const upb_fielddef* key_field = map_field_key(field);
695 const upb_fielddef* value_field = map_field_value(field);
696 VALUE type_class = field_type_class(value_field);
697
698 if (type_class != Qnil) {
699 VALUE args[3] = {
700 fieldtype_to_ruby(upb_fielddef_type(key_field)),
701 fieldtype_to_ruby(upb_fielddef_type(value_field)),
702 type_class,
703 };
704 map = rb_class_new_instance(3, args, cMap);
705 } else {
706 VALUE args[2] = {
707 fieldtype_to_ruby(upb_fielddef_type(key_field)),
708 fieldtype_to_ruby(upb_fielddef_type(value_field)),
709 };
710 map = rb_class_new_instance(2, args, cMap);
711 }
712
713 DEREF(memory, VALUE) = map;
714 } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
715 VALUE ary = Qnil;
716
717 VALUE type_class = field_type_class(field);
718
719 if (type_class != Qnil) {
720 VALUE args[2] = {
721 fieldtype_to_ruby(upb_fielddef_type(field)),
722 type_class,
723 };
724 ary = rb_class_new_instance(2, args, cRepeatedField);
725 } else {
726 VALUE args[1] = { fieldtype_to_ruby(upb_fielddef_type(field)) };
727 ary = rb_class_new_instance(1, args, cRepeatedField);
728 }
729
730 DEREF(memory, VALUE) = ary;
731 } else {
732 native_slot_init(upb_fielddef_type(field), memory);
733 }
734 }
735 }
736
layout_mark(MessageLayout * layout,void * storage)737 void layout_mark(MessageLayout* layout, void* storage) {
738 upb_msg_field_iter it;
739 for (upb_msg_field_begin(&it, layout->msgdef);
740 !upb_msg_field_done(&it);
741 upb_msg_field_next(&it)) {
742 const upb_fielddef* field = upb_msg_iter_field(&it);
743 void* memory = slot_memory(layout, storage, field);
744 uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
745
746 if (upb_fielddef_containingoneof(field)) {
747 if (*oneof_case == upb_fielddef_number(field)) {
748 native_slot_mark(upb_fielddef_type(field), memory);
749 }
750 } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
751 rb_gc_mark(DEREF(memory, VALUE));
752 } else {
753 native_slot_mark(upb_fielddef_type(field), memory);
754 }
755 }
756 }
757
layout_dup(MessageLayout * layout,void * to,void * from)758 void layout_dup(MessageLayout* layout, void* to, void* from) {
759 upb_msg_field_iter it;
760 for (upb_msg_field_begin(&it, layout->msgdef);
761 !upb_msg_field_done(&it);
762 upb_msg_field_next(&it)) {
763 const upb_fielddef* field = upb_msg_iter_field(&it);
764
765 void* to_memory = slot_memory(layout, to, field);
766 uint32_t* to_oneof_case = slot_oneof_case(layout, to, field);
767 void* from_memory = slot_memory(layout, from, field);
768 uint32_t* from_oneof_case = slot_oneof_case(layout, from, field);
769
770 if (upb_fielddef_containingoneof(field)) {
771 if (*from_oneof_case == upb_fielddef_number(field)) {
772 *to_oneof_case = *from_oneof_case;
773 native_slot_dup(upb_fielddef_type(field), to_memory, from_memory);
774 }
775 } else if (is_map_field(field)) {
776 DEREF(to_memory, VALUE) = Map_dup(DEREF(from_memory, VALUE));
777 } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
778 DEREF(to_memory, VALUE) = RepeatedField_dup(DEREF(from_memory, VALUE));
779 } else {
780 native_slot_dup(upb_fielddef_type(field), to_memory, from_memory);
781 }
782 }
783 }
784
layout_deep_copy(MessageLayout * layout,void * to,void * from)785 void layout_deep_copy(MessageLayout* layout, void* to, void* from) {
786 upb_msg_field_iter it;
787 for (upb_msg_field_begin(&it, layout->msgdef);
788 !upb_msg_field_done(&it);
789 upb_msg_field_next(&it)) {
790 const upb_fielddef* field = upb_msg_iter_field(&it);
791
792 void* to_memory = slot_memory(layout, to, field);
793 uint32_t* to_oneof_case = slot_oneof_case(layout, to, field);
794 void* from_memory = slot_memory(layout, from, field);
795 uint32_t* from_oneof_case = slot_oneof_case(layout, from, field);
796
797 if (upb_fielddef_containingoneof(field)) {
798 if (*from_oneof_case == upb_fielddef_number(field)) {
799 *to_oneof_case = *from_oneof_case;
800 native_slot_deep_copy(upb_fielddef_type(field), to_memory, from_memory);
801 }
802 } else if (is_map_field(field)) {
803 DEREF(to_memory, VALUE) =
804 Map_deep_copy(DEREF(from_memory, VALUE));
805 } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
806 DEREF(to_memory, VALUE) =
807 RepeatedField_deep_copy(DEREF(from_memory, VALUE));
808 } else {
809 native_slot_deep_copy(upb_fielddef_type(field), to_memory, from_memory);
810 }
811 }
812 }
813
layout_eq(MessageLayout * layout,void * msg1,void * msg2)814 VALUE layout_eq(MessageLayout* layout, void* msg1, void* msg2) {
815 upb_msg_field_iter it;
816 for (upb_msg_field_begin(&it, layout->msgdef);
817 !upb_msg_field_done(&it);
818 upb_msg_field_next(&it)) {
819 const upb_fielddef* field = upb_msg_iter_field(&it);
820
821 void* msg1_memory = slot_memory(layout, msg1, field);
822 uint32_t* msg1_oneof_case = slot_oneof_case(layout, msg1, field);
823 void* msg2_memory = slot_memory(layout, msg2, field);
824 uint32_t* msg2_oneof_case = slot_oneof_case(layout, msg2, field);
825
826 if (upb_fielddef_containingoneof(field)) {
827 if (*msg1_oneof_case != *msg2_oneof_case ||
828 (*msg1_oneof_case == upb_fielddef_number(field) &&
829 !native_slot_eq(upb_fielddef_type(field),
830 msg1_memory,
831 msg2_memory))) {
832 return Qfalse;
833 }
834 } else if (is_map_field(field)) {
835 if (!Map_eq(DEREF(msg1_memory, VALUE),
836 DEREF(msg2_memory, VALUE))) {
837 return Qfalse;
838 }
839 } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
840 if (!RepeatedField_eq(DEREF(msg1_memory, VALUE),
841 DEREF(msg2_memory, VALUE))) {
842 return Qfalse;
843 }
844 } else {
845 if (!native_slot_eq(upb_fielddef_type(field),
846 msg1_memory, msg2_memory)) {
847 return Qfalse;
848 }
849 }
850 }
851 return Qtrue;
852 }
853
layout_hash(MessageLayout * layout,void * storage)854 VALUE layout_hash(MessageLayout* layout, void* storage) {
855 upb_msg_field_iter it;
856 st_index_t h = rb_hash_start(0);
857 VALUE hash_sym = rb_intern("hash");
858 for (upb_msg_field_begin(&it, layout->msgdef);
859 !upb_msg_field_done(&it);
860 upb_msg_field_next(&it)) {
861 const upb_fielddef* field = upb_msg_iter_field(&it);
862 VALUE field_val = layout_get(layout, storage, field);
863 h = rb_hash_uint(h, NUM2LONG(rb_funcall(field_val, hash_sym, 0)));
864 }
865 h = rb_hash_end(h);
866
867 return INT2FIX(h);
868 }
869
layout_inspect(MessageLayout * layout,void * storage)870 VALUE layout_inspect(MessageLayout* layout, void* storage) {
871 VALUE str = rb_str_new2("");
872
873 upb_msg_field_iter it;
874 bool first = true;
875 for (upb_msg_field_begin(&it, layout->msgdef);
876 !upb_msg_field_done(&it);
877 upb_msg_field_next(&it)) {
878 const upb_fielddef* field = upb_msg_iter_field(&it);
879 VALUE field_val = layout_get(layout, storage, field);
880
881 if (!first) {
882 str = rb_str_cat2(str, ", ");
883 } else {
884 first = false;
885 }
886 str = rb_str_cat2(str, upb_fielddef_name(field));
887 str = rb_str_cat2(str, ": ");
888
889 str = rb_str_append(str, rb_funcall(field_val, rb_intern("inspect"), 0));
890 }
891
892 return str;
893 }
894