google-protobuf 3.11.3 → 3.19.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


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

@@ -1,1149 +0,0 @@
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 CHARPTR_AT(msg, ofs) ((char*)msg + ofs)
42
- #define DEREF_OFFSET(msg, ofs, type) *(type*)CHARPTR_AT(msg, ofs)
43
- #define DEREF(memory, type) *(type*)(memory)
44
-
45
- size_t native_slot_size(upb_fieldtype_t type) {
46
- switch (type) {
47
- case UPB_TYPE_FLOAT: return 4;
48
- case UPB_TYPE_DOUBLE: return 8;
49
- case UPB_TYPE_BOOL: return 1;
50
- case UPB_TYPE_STRING: return sizeof(VALUE);
51
- case UPB_TYPE_BYTES: return sizeof(VALUE);
52
- case UPB_TYPE_MESSAGE: return sizeof(VALUE);
53
- case UPB_TYPE_ENUM: return 4;
54
- case UPB_TYPE_INT32: return 4;
55
- case UPB_TYPE_INT64: return 8;
56
- case UPB_TYPE_UINT32: return 4;
57
- case UPB_TYPE_UINT64: return 8;
58
- default: return 0;
59
- }
60
- }
61
-
62
- static bool is_ruby_num(VALUE value) {
63
- return (TYPE(value) == T_FLOAT ||
64
- TYPE(value) == T_FIXNUM ||
65
- TYPE(value) == T_BIGNUM);
66
- }
67
-
68
- void native_slot_check_int_range_precision(const char* name, upb_fieldtype_t type, VALUE val) {
69
- if (!is_ruby_num(val)) {
70
- rb_raise(cTypeError, "Expected number type for integral field '%s' (given %s).",
71
- name, rb_class2name(CLASS_OF(val)));
72
- }
73
-
74
- // NUM2{INT,UINT,LL,ULL} macros do the appropriate range checks on upper
75
- // bound; we just need to do precision checks (i.e., disallow rounding) and
76
- // check for < 0 on unsigned types.
77
- if (TYPE(val) == T_FLOAT) {
78
- double dbl_val = NUM2DBL(val);
79
- if (floor(dbl_val) != dbl_val) {
80
- rb_raise(rb_eRangeError,
81
- "Non-integral floating point value assigned to integer field '%s' (given %s).",
82
- name, rb_class2name(CLASS_OF(val)));
83
- }
84
- }
85
- if (type == UPB_TYPE_UINT32 || type == UPB_TYPE_UINT64) {
86
- if (NUM2DBL(val) < 0) {
87
- rb_raise(rb_eRangeError,
88
- "Assigning negative value to unsigned integer field '%s' (given %s).",
89
- name, rb_class2name(CLASS_OF(val)));
90
- }
91
- }
92
- }
93
-
94
- VALUE native_slot_encode_and_freeze_string(upb_fieldtype_t type, VALUE value) {
95
- rb_encoding* desired_encoding = (type == UPB_TYPE_STRING) ?
96
- kRubyStringUtf8Encoding : kRubyString8bitEncoding;
97
- VALUE desired_encoding_value = rb_enc_from_encoding(desired_encoding);
98
-
99
- if (rb_obj_encoding(value) != desired_encoding_value || !OBJ_FROZEN(value)) {
100
- // Note: this will not duplicate underlying string data unless necessary.
101
- value = rb_str_encode(value, desired_encoding_value, 0, Qnil);
102
-
103
- if (type == UPB_TYPE_STRING &&
104
- rb_enc_str_coderange(value) == ENC_CODERANGE_BROKEN) {
105
- rb_raise(rb_eEncodingError, "String is invalid UTF-8");
106
- }
107
-
108
- // Ensure the data remains valid. Since we called #encode a moment ago,
109
- // this does not freeze the string the user assigned.
110
- rb_obj_freeze(value);
111
- }
112
-
113
- return value;
114
- }
115
-
116
- void native_slot_set(const char* name,
117
- upb_fieldtype_t type, VALUE type_class,
118
- void* memory, VALUE value) {
119
- native_slot_set_value_and_case(name, type, type_class, memory, value, NULL, 0);
120
- }
121
-
122
- void native_slot_set_value_and_case(const char* name,
123
- upb_fieldtype_t type, VALUE type_class,
124
- void* memory, VALUE value,
125
- uint32_t* case_memory,
126
- uint32_t case_number) {
127
- // Note that in order to atomically change the value in memory and the case
128
- // value (w.r.t. Ruby VM calls), we must set the value at |memory| only after
129
- // all Ruby VM calls are complete. The case is then set at the bottom of this
130
- // function.
131
- switch (type) {
132
- case UPB_TYPE_FLOAT:
133
- if (!is_ruby_num(value)) {
134
- rb_raise(cTypeError, "Expected number type for float field '%s' (given %s).",
135
- name, rb_class2name(CLASS_OF(value)));
136
- }
137
- DEREF(memory, float) = NUM2DBL(value);
138
- break;
139
- case UPB_TYPE_DOUBLE:
140
- if (!is_ruby_num(value)) {
141
- rb_raise(cTypeError, "Expected number type for double field '%s' (given %s).",
142
- name, rb_class2name(CLASS_OF(value)));
143
- }
144
- DEREF(memory, double) = NUM2DBL(value);
145
- break;
146
- case UPB_TYPE_BOOL: {
147
- int8_t val = -1;
148
- if (value == Qtrue) {
149
- val = 1;
150
- } else if (value == Qfalse) {
151
- val = 0;
152
- } else {
153
- rb_raise(cTypeError, "Invalid argument for boolean field '%s' (given %s).",
154
- name, rb_class2name(CLASS_OF(value)));
155
- }
156
- DEREF(memory, int8_t) = val;
157
- break;
158
- }
159
- case UPB_TYPE_STRING:
160
- if (CLASS_OF(value) == rb_cSymbol) {
161
- value = rb_funcall(value, rb_intern("to_s"), 0);
162
- } else if (CLASS_OF(value) != rb_cString) {
163
- rb_raise(cTypeError, "Invalid argument for string field '%s' (given %s).",
164
- name, rb_class2name(CLASS_OF(value)));
165
- }
166
-
167
- DEREF(memory, VALUE) = native_slot_encode_and_freeze_string(type, value);
168
- break;
169
-
170
- case UPB_TYPE_BYTES: {
171
- if (CLASS_OF(value) != rb_cString) {
172
- rb_raise(cTypeError, "Invalid argument for bytes field '%s' (given %s).",
173
- name, rb_class2name(CLASS_OF(value)));
174
- }
175
-
176
- DEREF(memory, VALUE) = native_slot_encode_and_freeze_string(type, value);
177
- break;
178
- }
179
- case UPB_TYPE_MESSAGE: {
180
- if (CLASS_OF(value) == CLASS_OF(Qnil)) {
181
- value = Qnil;
182
- } else if (CLASS_OF(value) != type_class) {
183
- // check for possible implicit conversions
184
- VALUE converted_value = Qnil;
185
- const char* field_type_name = rb_class2name(type_class);
186
-
187
- if (strcmp(field_type_name, "Google::Protobuf::Timestamp") == 0 &&
188
- rb_obj_is_kind_of(value, rb_cTime)) {
189
- // Time -> Google::Protobuf::Timestamp
190
- VALUE hash = rb_hash_new();
191
- rb_hash_aset(hash, rb_str_new2("seconds"),
192
- rb_funcall(value, rb_intern("to_i"), 0));
193
- rb_hash_aset(hash, rb_str_new2("nanos"),
194
- rb_funcall(value, rb_intern("nsec"), 0));
195
- {
196
- VALUE args[1] = {hash};
197
- converted_value = rb_class_new_instance(1, args, type_class);
198
- }
199
- } else if (strcmp(field_type_name, "Google::Protobuf::Duration") == 0 &&
200
- rb_obj_is_kind_of(value, rb_cNumeric)) {
201
- // Numeric -> Google::Protobuf::Duration
202
- VALUE hash = rb_hash_new();
203
- rb_hash_aset(hash, rb_str_new2("seconds"),
204
- rb_funcall(value, rb_intern("to_i"), 0));
205
- {
206
- VALUE n_value =
207
- rb_funcall(value, rb_intern("remainder"), 1, INT2NUM(1));
208
- n_value =
209
- rb_funcall(n_value, rb_intern("*"), 1, INT2NUM(1000000000));
210
- n_value = rb_funcall(n_value, rb_intern("round"), 0);
211
- rb_hash_aset(hash, rb_str_new2("nanos"), n_value);
212
- }
213
- {
214
- VALUE args[1] = { hash };
215
- converted_value = rb_class_new_instance(1, args, type_class);
216
- }
217
- }
218
-
219
- // raise if no suitable conversaion could be found
220
- if (converted_value == Qnil) {
221
- rb_raise(cTypeError,
222
- "Invalid type %s to assign to submessage field '%s'.",
223
- rb_class2name(CLASS_OF(value)), name);
224
- } else {
225
- value = converted_value;
226
- }
227
- }
228
- DEREF(memory, VALUE) = value;
229
- break;
230
- }
231
- case UPB_TYPE_ENUM: {
232
- int32_t int_val = 0;
233
- if (TYPE(value) == T_STRING) {
234
- value = rb_funcall(value, rb_intern("to_sym"), 0);
235
- } else if (!is_ruby_num(value) && TYPE(value) != T_SYMBOL) {
236
- rb_raise(cTypeError,
237
- "Expected number or symbol type for enum field '%s'.", name);
238
- }
239
- if (TYPE(value) == T_SYMBOL) {
240
- // Ensure that the given symbol exists in the enum module.
241
- VALUE lookup = rb_funcall(type_class, rb_intern("resolve"), 1, value);
242
- if (lookup == Qnil) {
243
- rb_raise(rb_eRangeError, "Unknown symbol value for enum field '%s'.", name);
244
- } else {
245
- int_val = NUM2INT(lookup);
246
- }
247
- } else {
248
- native_slot_check_int_range_precision(name, UPB_TYPE_INT32, value);
249
- int_val = NUM2INT(value);
250
- }
251
- DEREF(memory, int32_t) = int_val;
252
- break;
253
- }
254
- case UPB_TYPE_INT32:
255
- case UPB_TYPE_INT64:
256
- case UPB_TYPE_UINT32:
257
- case UPB_TYPE_UINT64:
258
- native_slot_check_int_range_precision(name, type, value);
259
- switch (type) {
260
- case UPB_TYPE_INT32:
261
- DEREF(memory, int32_t) = NUM2INT(value);
262
- break;
263
- case UPB_TYPE_INT64:
264
- DEREF(memory, int64_t) = NUM2LL(value);
265
- break;
266
- case UPB_TYPE_UINT32:
267
- DEREF(memory, uint32_t) = NUM2UINT(value);
268
- break;
269
- case UPB_TYPE_UINT64:
270
- DEREF(memory, uint64_t) = NUM2ULL(value);
271
- break;
272
- default:
273
- break;
274
- }
275
- break;
276
- default:
277
- break;
278
- }
279
-
280
- if (case_memory != NULL) {
281
- *case_memory = case_number;
282
- }
283
- }
284
-
285
- VALUE native_slot_get(upb_fieldtype_t type,
286
- VALUE type_class,
287
- const void* memory) {
288
- switch (type) {
289
- case UPB_TYPE_FLOAT:
290
- return DBL2NUM(DEREF(memory, float));
291
- case UPB_TYPE_DOUBLE:
292
- return DBL2NUM(DEREF(memory, double));
293
- case UPB_TYPE_BOOL:
294
- return DEREF(memory, int8_t) ? Qtrue : Qfalse;
295
- case UPB_TYPE_STRING:
296
- case UPB_TYPE_BYTES:
297
- return DEREF(memory, VALUE);
298
- case UPB_TYPE_MESSAGE: {
299
- VALUE val = DEREF(memory, VALUE);
300
-
301
- // Lazily expand wrapper type if necessary.
302
- int type = TYPE(val);
303
- if (type != T_DATA && type != T_NIL) {
304
- // This must be a wrapper type.
305
- val = ruby_wrapper_type(type_class, val);
306
- DEREF(memory, VALUE) = val;
307
- }
308
-
309
- return val;
310
- }
311
- case UPB_TYPE_ENUM: {
312
- int32_t val = DEREF(memory, int32_t);
313
- VALUE symbol = enum_lookup(type_class, INT2NUM(val));
314
- if (symbol == Qnil) {
315
- return INT2NUM(val);
316
- } else {
317
- return symbol;
318
- }
319
- }
320
- case UPB_TYPE_INT32:
321
- return INT2NUM(DEREF(memory, int32_t));
322
- case UPB_TYPE_INT64:
323
- return LL2NUM(DEREF(memory, int64_t));
324
- case UPB_TYPE_UINT32:
325
- return UINT2NUM(DEREF(memory, uint32_t));
326
- case UPB_TYPE_UINT64:
327
- return ULL2NUM(DEREF(memory, uint64_t));
328
- default:
329
- return Qnil;
330
- }
331
- }
332
-
333
- void native_slot_init(upb_fieldtype_t type, void* memory) {
334
- switch (type) {
335
- case UPB_TYPE_FLOAT:
336
- DEREF(memory, float) = 0.0;
337
- break;
338
- case UPB_TYPE_DOUBLE:
339
- DEREF(memory, double) = 0.0;
340
- break;
341
- case UPB_TYPE_BOOL:
342
- DEREF(memory, int8_t) = 0;
343
- break;
344
- case UPB_TYPE_STRING:
345
- case UPB_TYPE_BYTES:
346
- DEREF(memory, VALUE) = rb_str_new2("");
347
- rb_enc_associate(DEREF(memory, VALUE), (type == UPB_TYPE_BYTES) ?
348
- kRubyString8bitEncoding : kRubyStringUtf8Encoding);
349
- break;
350
- case UPB_TYPE_MESSAGE:
351
- DEREF(memory, VALUE) = Qnil;
352
- break;
353
- case UPB_TYPE_ENUM:
354
- case UPB_TYPE_INT32:
355
- DEREF(memory, int32_t) = 0;
356
- break;
357
- case UPB_TYPE_INT64:
358
- DEREF(memory, int64_t) = 0;
359
- break;
360
- case UPB_TYPE_UINT32:
361
- DEREF(memory, uint32_t) = 0;
362
- break;
363
- case UPB_TYPE_UINT64:
364
- DEREF(memory, uint64_t) = 0;
365
- break;
366
- default:
367
- break;
368
- }
369
- }
370
-
371
- void native_slot_mark(upb_fieldtype_t type, void* memory) {
372
- switch (type) {
373
- case UPB_TYPE_STRING:
374
- case UPB_TYPE_BYTES:
375
- case UPB_TYPE_MESSAGE:
376
- rb_gc_mark(DEREF(memory, VALUE));
377
- break;
378
- default:
379
- break;
380
- }
381
- }
382
-
383
- void native_slot_dup(upb_fieldtype_t type, void* to, void* from) {
384
- memcpy(to, from, native_slot_size(type));
385
- }
386
-
387
- void native_slot_deep_copy(upb_fieldtype_t type, VALUE type_class, void* to,
388
- void* from) {
389
- switch (type) {
390
- case UPB_TYPE_STRING:
391
- case UPB_TYPE_BYTES: {
392
- VALUE from_val = DEREF(from, VALUE);
393
- DEREF(to, VALUE) = (from_val != Qnil) ?
394
- rb_funcall(from_val, rb_intern("dup"), 0) : Qnil;
395
- break;
396
- }
397
- case UPB_TYPE_MESSAGE: {
398
- VALUE from_val = native_slot_get(type, type_class, from);
399
- DEREF(to, VALUE) = (from_val != Qnil) ?
400
- Message_deep_copy(from_val) : Qnil;
401
- break;
402
- }
403
- default:
404
- memcpy(to, from, native_slot_size(type));
405
- }
406
- }
407
-
408
- bool native_slot_eq(upb_fieldtype_t type, VALUE type_class, void* mem1,
409
- void* mem2) {
410
- switch (type) {
411
- case UPB_TYPE_STRING:
412
- case UPB_TYPE_BYTES:
413
- case UPB_TYPE_MESSAGE: {
414
- VALUE val1 = native_slot_get(type, type_class, mem1);
415
- VALUE val2 = native_slot_get(type, type_class, mem2);
416
- VALUE ret = rb_funcall(val1, rb_intern("=="), 1, val2);
417
- return ret == Qtrue;
418
- }
419
- default:
420
- return !memcmp(mem1, mem2, native_slot_size(type));
421
- }
422
- }
423
-
424
- // -----------------------------------------------------------------------------
425
- // Map field utilities.
426
- // -----------------------------------------------------------------------------
427
-
428
- const upb_msgdef* tryget_map_entry_msgdef(const upb_fielddef* field) {
429
- const upb_msgdef* subdef;
430
- if (upb_fielddef_label(field) != UPB_LABEL_REPEATED ||
431
- upb_fielddef_type(field) != UPB_TYPE_MESSAGE) {
432
- return NULL;
433
- }
434
- subdef = upb_fielddef_msgsubdef(field);
435
- return upb_msgdef_mapentry(subdef) ? subdef : NULL;
436
- }
437
-
438
- const upb_msgdef *map_entry_msgdef(const upb_fielddef* field) {
439
- const upb_msgdef* subdef = tryget_map_entry_msgdef(field);
440
- assert(subdef);
441
- return subdef;
442
- }
443
-
444
- bool is_map_field(const upb_fielddef *field) {
445
- const upb_msgdef* subdef = tryget_map_entry_msgdef(field);
446
- if (subdef == NULL) return false;
447
-
448
- // Map fields are a proto3 feature.
449
- // If we're using proto2 syntax we need to fallback to the repeated field.
450
- return upb_msgdef_syntax(subdef) == UPB_SYNTAX_PROTO3;
451
- }
452
-
453
- const upb_fielddef* map_field_key(const upb_fielddef* field) {
454
- const upb_msgdef* subdef = map_entry_msgdef(field);
455
- return map_entry_key(subdef);
456
- }
457
-
458
- const upb_fielddef* map_field_value(const upb_fielddef* field) {
459
- const upb_msgdef* subdef = map_entry_msgdef(field);
460
- return map_entry_value(subdef);
461
- }
462
-
463
- const upb_fielddef* map_entry_key(const upb_msgdef* msgdef) {
464
- const upb_fielddef* key_field = upb_msgdef_itof(msgdef, MAP_KEY_FIELD);
465
- assert(key_field != NULL);
466
- return key_field;
467
- }
468
-
469
- const upb_fielddef* map_entry_value(const upb_msgdef* msgdef) {
470
- const upb_fielddef* value_field = upb_msgdef_itof(msgdef, MAP_VALUE_FIELD);
471
- assert(value_field != NULL);
472
- return value_field;
473
- }
474
-
475
- // -----------------------------------------------------------------------------
476
- // Memory layout management.
477
- // -----------------------------------------------------------------------------
478
-
479
- bool field_contains_hasbit(MessageLayout* layout,
480
- const upb_fielddef* field) {
481
- return layout->fields[upb_fielddef_index(field)].hasbit !=
482
- MESSAGE_FIELD_NO_HASBIT;
483
- }
484
-
485
- static size_t align_up_to(size_t offset, size_t granularity) {
486
- // Granularity must be a power of two.
487
- return (offset + granularity - 1) & ~(granularity - 1);
488
- }
489
-
490
- bool is_value_field(const upb_fielddef* f) {
491
- return upb_fielddef_isseq(f) || upb_fielddef_issubmsg(f) ||
492
- upb_fielddef_isstring(f);
493
- }
494
-
495
- void create_layout(Descriptor* desc) {
496
- const upb_msgdef *msgdef = desc->msgdef;
497
- MessageLayout* layout = ALLOC(MessageLayout);
498
- int nfields = upb_msgdef_numfields(msgdef);
499
- int noneofs = upb_msgdef_numoneofs(msgdef);
500
- upb_msg_field_iter it;
501
- upb_msg_oneof_iter oit;
502
- size_t off = 0;
503
- size_t hasbit = 0;
504
-
505
- layout->empty_template = NULL;
506
- layout->desc = desc;
507
- desc->layout = layout;
508
-
509
- layout->fields = ALLOC_N(MessageField, nfields);
510
- layout->oneofs = NULL;
511
-
512
- if (noneofs > 0) {
513
- layout->oneofs = ALLOC_N(MessageOneof, noneofs);
514
- }
515
-
516
- for (upb_msg_field_begin(&it, msgdef);
517
- !upb_msg_field_done(&it);
518
- upb_msg_field_next(&it)) {
519
- const upb_fielddef* field = upb_msg_iter_field(&it);
520
- if (upb_fielddef_haspresence(field)) {
521
- layout->fields[upb_fielddef_index(field)].hasbit = hasbit++;
522
- } else {
523
- layout->fields[upb_fielddef_index(field)].hasbit =
524
- MESSAGE_FIELD_NO_HASBIT;
525
- }
526
- }
527
-
528
- if (hasbit != 0) {
529
- off += (hasbit + 8 - 1) / 8;
530
- }
531
-
532
- off = align_up_to(off, sizeof(VALUE));
533
- layout->value_offset = off;
534
- layout->repeated_count = 0;
535
- layout->map_count = 0;
536
- layout->value_count = 0;
537
-
538
- // Place all VALUE fields for repeated fields.
539
- for (upb_msg_field_begin(&it, msgdef);
540
- !upb_msg_field_done(&it);
541
- upb_msg_field_next(&it)) {
542
- const upb_fielddef* field = upb_msg_iter_field(&it);
543
- if (upb_fielddef_containingoneof(field) || !upb_fielddef_isseq(field) ||
544
- upb_fielddef_ismap(field)) {
545
- continue;
546
- }
547
-
548
- layout->fields[upb_fielddef_index(field)].offset = off;
549
- off += sizeof(VALUE);
550
- layout->repeated_count++;
551
- }
552
-
553
- // Place all VALUE fields for map fields.
554
- for (upb_msg_field_begin(&it, msgdef);
555
- !upb_msg_field_done(&it);
556
- upb_msg_field_next(&it)) {
557
- const upb_fielddef* field = upb_msg_iter_field(&it);
558
- if (upb_fielddef_containingoneof(field) || !upb_fielddef_isseq(field) ||
559
- !upb_fielddef_ismap(field)) {
560
- continue;
561
- }
562
-
563
- layout->fields[upb_fielddef_index(field)].offset = off;
564
- off += sizeof(VALUE);
565
- layout->map_count++;
566
- }
567
-
568
- layout->value_count = layout->repeated_count + layout->map_count;
569
-
570
- // Next place all other (non-oneof) VALUE fields.
571
- for (upb_msg_field_begin(&it, msgdef);
572
- !upb_msg_field_done(&it);
573
- upb_msg_field_next(&it)) {
574
- const upb_fielddef* field = upb_msg_iter_field(&it);
575
- if (upb_fielddef_containingoneof(field) || !is_value_field(field) ||
576
- upb_fielddef_isseq(field)) {
577
- continue;
578
- }
579
-
580
- layout->fields[upb_fielddef_index(field)].offset = off;
581
- off += sizeof(VALUE);
582
- layout->value_count++;
583
- }
584
-
585
- // Now place all other (non-oneof) fields.
586
- for (upb_msg_field_begin(&it, msgdef);
587
- !upb_msg_field_done(&it);
588
- upb_msg_field_next(&it)) {
589
- const upb_fielddef* field = upb_msg_iter_field(&it);
590
- size_t field_size;
591
-
592
- if (upb_fielddef_containingoneof(field) || is_value_field(field)) {
593
- continue;
594
- }
595
-
596
- // Allocate |field_size| bytes for this field in the layout.
597
- field_size = native_slot_size(upb_fielddef_type(field));
598
-
599
- // Align current offset up to |size| granularity.
600
- off = align_up_to(off, field_size);
601
- layout->fields[upb_fielddef_index(field)].offset = off;
602
- off += field_size;
603
- }
604
-
605
- // Handle oneofs now -- we iterate over oneofs specifically and allocate only
606
- // one slot per oneof.
607
- //
608
- // We assign all value slots first, then pack the 'case' fields at the end,
609
- // since in the common case (modern 64-bit platform) these are 8 bytes and 4
610
- // bytes respectively and we want to avoid alignment overhead.
611
- //
612
- // Note that we reserve 4 bytes (a uint32) per 'case' slot because the value
613
- // space for oneof cases is conceptually as wide as field tag numbers. In
614
- // practice, it's unlikely that a oneof would have more than e.g. 256 or 64K
615
- // members (8 or 16 bits respectively), so conceivably we could assign
616
- // consecutive case numbers and then pick a smaller oneof case slot size, but
617
- // the complexity to implement this indirection is probably not worthwhile.
618
- for (upb_msg_oneof_begin(&oit, msgdef);
619
- !upb_msg_oneof_done(&oit);
620
- upb_msg_oneof_next(&oit)) {
621
- const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit);
622
- upb_oneof_iter fit;
623
-
624
- // Always allocate NATIVE_SLOT_MAX_SIZE bytes, but share the slot between
625
- // all fields.
626
- size_t field_size = NATIVE_SLOT_MAX_SIZE;
627
- // Align the offset.
628
- off = align_up_to(off, field_size);
629
- // Assign all fields in the oneof this same offset.
630
- for (upb_oneof_begin(&fit, oneof);
631
- !upb_oneof_done(&fit);
632
- upb_oneof_next(&fit)) {
633
- const upb_fielddef* field = upb_oneof_iter_field(&fit);
634
- layout->fields[upb_fielddef_index(field)].offset = off;
635
- layout->oneofs[upb_oneofdef_index(oneof)].offset = off;
636
- }
637
- off += field_size;
638
- }
639
-
640
- // Now the case fields.
641
- for (upb_msg_oneof_begin(&oit, msgdef);
642
- !upb_msg_oneof_done(&oit);
643
- upb_msg_oneof_next(&oit)) {
644
- const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit);
645
- size_t field_size = sizeof(uint32_t);
646
- // Align the offset.
647
- off = (off + field_size - 1) & ~(field_size - 1);
648
- layout->oneofs[upb_oneofdef_index(oneof)].case_offset = off;
649
- off += field_size;
650
- }
651
-
652
- layout->size = off;
653
- layout->msgdef = msgdef;
654
-
655
- // Create the empty message template.
656
- layout->empty_template = ALLOC_N(char, layout->size);
657
- memset(layout->empty_template, 0, layout->size);
658
-
659
- for (upb_msg_field_begin(&it, layout->msgdef);
660
- !upb_msg_field_done(&it);
661
- upb_msg_field_next(&it)) {
662
- layout_clear(layout, layout->empty_template, upb_msg_iter_field(&it));
663
- }
664
- }
665
-
666
- void free_layout(MessageLayout* layout) {
667
- xfree(layout->empty_template);
668
- xfree(layout->fields);
669
- xfree(layout->oneofs);
670
- xfree(layout);
671
- }
672
-
673
- VALUE field_type_class(const MessageLayout* layout, const upb_fielddef* field) {
674
- VALUE type_class = Qnil;
675
- if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) {
676
- VALUE submsgdesc = get_msgdef_obj(layout->desc->descriptor_pool,
677
- upb_fielddef_msgsubdef(field));
678
- type_class = Descriptor_msgclass(submsgdesc);
679
- } else if (upb_fielddef_type(field) == UPB_TYPE_ENUM) {
680
- VALUE subenumdesc = get_enumdef_obj(layout->desc->descriptor_pool,
681
- upb_fielddef_enumsubdef(field));
682
- type_class = EnumDescriptor_enummodule(subenumdesc);
683
- }
684
- return type_class;
685
- }
686
-
687
- static void* slot_memory(MessageLayout* layout,
688
- const void* storage,
689
- const upb_fielddef* field) {
690
- return ((uint8_t *)storage) +
691
- layout->fields[upb_fielddef_index(field)].offset;
692
- }
693
-
694
- static uint32_t* slot_oneof_case(MessageLayout* layout,
695
- const void* storage,
696
- const upb_oneofdef* oneof) {
697
- return (uint32_t*)(((uint8_t*)storage) +
698
- layout->oneofs[upb_oneofdef_index(oneof)].case_offset);
699
- }
700
-
701
- uint32_t slot_read_oneof_case(MessageLayout* layout, const void* storage,
702
- const upb_oneofdef* oneof) {
703
- uint32_t* ptr = slot_oneof_case(layout, storage, oneof);
704
- return *ptr & ~ONEOF_CASE_MASK;
705
- }
706
-
707
- static void slot_set_hasbit(MessageLayout* layout,
708
- const void* storage,
709
- const upb_fielddef* field) {
710
- size_t hasbit = layout->fields[upb_fielddef_index(field)].hasbit;
711
- assert(hasbit != MESSAGE_FIELD_NO_HASBIT);
712
-
713
- ((uint8_t*)storage)[hasbit / 8] |= 1 << (hasbit % 8);
714
- }
715
-
716
- static void slot_clear_hasbit(MessageLayout* layout,
717
- const void* storage,
718
- const upb_fielddef* field) {
719
- size_t hasbit = layout->fields[upb_fielddef_index(field)].hasbit;
720
- assert(hasbit != MESSAGE_FIELD_NO_HASBIT);
721
- ((uint8_t*)storage)[hasbit / 8] &= ~(1 << (hasbit % 8));
722
- }
723
-
724
- static bool slot_is_hasbit_set(MessageLayout* layout,
725
- const void* storage,
726
- const upb_fielddef* field) {
727
- size_t hasbit = layout->fields[upb_fielddef_index(field)].hasbit;
728
- if (hasbit == MESSAGE_FIELD_NO_HASBIT) {
729
- return false;
730
- }
731
-
732
- return DEREF_OFFSET(
733
- (uint8_t*)storage, hasbit / 8, char) & (1 << (hasbit % 8));
734
- }
735
-
736
- VALUE layout_has(MessageLayout* layout,
737
- const void* storage,
738
- const upb_fielddef* field) {
739
- assert(field_contains_hasbit(layout, field));
740
- return slot_is_hasbit_set(layout, storage, field) ? Qtrue : Qfalse;
741
- }
742
-
743
- void layout_clear(MessageLayout* layout,
744
- const void* storage,
745
- const upb_fielddef* field) {
746
- void* memory = slot_memory(layout, storage, field);
747
- const upb_oneofdef* oneof = upb_fielddef_containingoneof(field);
748
-
749
- if (field_contains_hasbit(layout, field)) {
750
- slot_clear_hasbit(layout, storage, field);
751
- }
752
-
753
- if (oneof) {
754
- uint32_t* oneof_case = slot_oneof_case(layout, storage, oneof);
755
- memset(memory, 0, NATIVE_SLOT_MAX_SIZE);
756
- *oneof_case = ONEOF_CASE_NONE;
757
- } else if (is_map_field(field)) {
758
- VALUE map = Qnil;
759
-
760
- const upb_fielddef* key_field = map_field_key(field);
761
- const upb_fielddef* value_field = map_field_value(field);
762
- VALUE type_class = field_type_class(layout, value_field);
763
-
764
- if (type_class != Qnil) {
765
- VALUE args[3] = {
766
- fieldtype_to_ruby(upb_fielddef_type(key_field)),
767
- fieldtype_to_ruby(upb_fielddef_type(value_field)),
768
- type_class,
769
- };
770
- map = rb_class_new_instance(3, args, cMap);
771
- } else {
772
- VALUE args[2] = {
773
- fieldtype_to_ruby(upb_fielddef_type(key_field)),
774
- fieldtype_to_ruby(upb_fielddef_type(value_field)),
775
- };
776
- map = rb_class_new_instance(2, args, cMap);
777
- }
778
-
779
- DEREF(memory, VALUE) = map;
780
- } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
781
- VALUE ary = Qnil;
782
-
783
- VALUE type_class = field_type_class(layout, field);
784
-
785
- if (type_class != Qnil) {
786
- VALUE args[2] = {
787
- fieldtype_to_ruby(upb_fielddef_type(field)),
788
- type_class,
789
- };
790
- ary = rb_class_new_instance(2, args, cRepeatedField);
791
- } else {
792
- VALUE args[1] = { fieldtype_to_ruby(upb_fielddef_type(field)) };
793
- ary = rb_class_new_instance(1, args, cRepeatedField);
794
- }
795
-
796
- DEREF(memory, VALUE) = ary;
797
- } else {
798
- native_slot_set(upb_fielddef_name(field), upb_fielddef_type(field),
799
- field_type_class(layout, field), memory,
800
- layout_get_default(field));
801
- }
802
- }
803
-
804
- VALUE layout_get_default(const upb_fielddef *field) {
805
- switch (upb_fielddef_type(field)) {
806
- case UPB_TYPE_FLOAT: return DBL2NUM(upb_fielddef_defaultfloat(field));
807
- case UPB_TYPE_DOUBLE: return DBL2NUM(upb_fielddef_defaultdouble(field));
808
- case UPB_TYPE_BOOL:
809
- return upb_fielddef_defaultbool(field) ? Qtrue : Qfalse;
810
- case UPB_TYPE_MESSAGE: return Qnil;
811
- case UPB_TYPE_ENUM: {
812
- const upb_enumdef *enumdef = upb_fielddef_enumsubdef(field);
813
- int32_t num = upb_fielddef_defaultint32(field);
814
- const char *label = upb_enumdef_iton(enumdef, num);
815
- if (label) {
816
- return ID2SYM(rb_intern(label));
817
- } else {
818
- return INT2NUM(num);
819
- }
820
- }
821
- case UPB_TYPE_INT32: return INT2NUM(upb_fielddef_defaultint32(field));
822
- case UPB_TYPE_INT64: return LL2NUM(upb_fielddef_defaultint64(field));;
823
- case UPB_TYPE_UINT32: return UINT2NUM(upb_fielddef_defaultuint32(field));
824
- case UPB_TYPE_UINT64: return ULL2NUM(upb_fielddef_defaultuint64(field));
825
- case UPB_TYPE_STRING:
826
- case UPB_TYPE_BYTES: {
827
- size_t size;
828
- const char *str = upb_fielddef_defaultstr(field, &size);
829
- return get_frozen_string(str, size,
830
- upb_fielddef_type(field) == UPB_TYPE_BYTES);
831
- }
832
- default: return Qnil;
833
- }
834
- }
835
-
836
- VALUE layout_get(MessageLayout* layout,
837
- const void* storage,
838
- const upb_fielddef* field) {
839
- void* memory = slot_memory(layout, storage, field);
840
- const upb_oneofdef* oneof = upb_fielddef_containingoneof(field);
841
- bool field_set;
842
- if (field_contains_hasbit(layout, field)) {
843
- field_set = slot_is_hasbit_set(layout, storage, field);
844
- } else {
845
- field_set = true;
846
- }
847
-
848
- if (oneof) {
849
- uint32_t oneof_case = slot_read_oneof_case(layout, storage, oneof);
850
- if (oneof_case != upb_fielddef_number(field)) {
851
- return layout_get_default(field);
852
- }
853
- return native_slot_get(upb_fielddef_type(field),
854
- field_type_class(layout, field), memory);
855
- } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
856
- return *((VALUE *)memory);
857
- } else if (!field_set) {
858
- return layout_get_default(field);
859
- } else {
860
- return native_slot_get(upb_fielddef_type(field),
861
- field_type_class(layout, field), memory);
862
- }
863
- }
864
-
865
- static void check_repeated_field_type(const MessageLayout* layout, VALUE val,
866
- const upb_fielddef* field) {
867
- RepeatedField* self;
868
- assert(upb_fielddef_label(field) == UPB_LABEL_REPEATED);
869
-
870
- if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
871
- RTYPEDDATA_TYPE(val) != &RepeatedField_type) {
872
- rb_raise(cTypeError, "Expected repeated field array");
873
- }
874
-
875
- self = ruby_to_RepeatedField(val);
876
- if (self->field_type != upb_fielddef_type(field)) {
877
- rb_raise(cTypeError, "Repeated field array has wrong element type");
878
- }
879
-
880
- if (self->field_type_class != field_type_class(layout, field)) {
881
- rb_raise(cTypeError, "Repeated field array has wrong message/enum class");
882
- }
883
- }
884
-
885
- static void check_map_field_type(const MessageLayout* layout, VALUE val,
886
- const upb_fielddef* field) {
887
- const upb_fielddef* key_field = map_field_key(field);
888
- const upb_fielddef* value_field = map_field_value(field);
889
- Map* self;
890
-
891
- if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
892
- RTYPEDDATA_TYPE(val) != &Map_type) {
893
- rb_raise(cTypeError, "Expected Map instance");
894
- }
895
-
896
- self = ruby_to_Map(val);
897
- if (self->key_type != upb_fielddef_type(key_field)) {
898
- rb_raise(cTypeError, "Map key type does not match field's key type");
899
- }
900
- if (self->value_type != upb_fielddef_type(value_field)) {
901
- rb_raise(cTypeError, "Map value type does not match field's value type");
902
- }
903
- if (self->value_type_class != field_type_class(layout, value_field)) {
904
- rb_raise(cTypeError, "Map value type has wrong message/enum class");
905
- }
906
- }
907
-
908
- void layout_set(MessageLayout* layout,
909
- void* storage,
910
- const upb_fielddef* field,
911
- VALUE val) {
912
- void* memory = slot_memory(layout, storage, field);
913
- const upb_oneofdef* oneof = upb_fielddef_containingoneof(field);
914
-
915
- if (oneof) {
916
- uint32_t* oneof_case = slot_oneof_case(layout, storage, oneof);
917
- if (val == Qnil) {
918
- // Assigning nil to a oneof field clears the oneof completely.
919
- *oneof_case = ONEOF_CASE_NONE;
920
- memset(memory, 0, NATIVE_SLOT_MAX_SIZE);
921
- } else {
922
- // The transition between field types for a single oneof (union) slot is
923
- // somewhat complex because we need to ensure that a GC triggered at any
924
- // point by a call into the Ruby VM sees a valid state for this field and
925
- // does not either go off into the weeds (following what it thinks is a
926
- // VALUE but is actually a different field type) or miss an object (seeing
927
- // what it thinks is a primitive field but is actually a VALUE for the new
928
- // field type).
929
- //
930
- // In order for the transition to be safe, the oneof case slot must be in
931
- // sync with the value slot whenever the Ruby VM has been called. Thus, we
932
- // use native_slot_set_value_and_case(), which ensures that both the value
933
- // and case number are altered atomically (w.r.t. the Ruby VM).
934
- uint32_t case_value = upb_fielddef_number(field);
935
- if (upb_fielddef_issubmsg(field) || upb_fielddef_isstring(field)) {
936
- case_value |= ONEOF_CASE_MASK;
937
- }
938
-
939
- native_slot_set_value_and_case(
940
- upb_fielddef_name(field), upb_fielddef_type(field),
941
- field_type_class(layout, field), memory, val, oneof_case, case_value);
942
- }
943
- } else if (is_map_field(field)) {
944
- check_map_field_type(layout, val, field);
945
- DEREF(memory, VALUE) = val;
946
- } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
947
- check_repeated_field_type(layout, val, field);
948
- DEREF(memory, VALUE) = val;
949
- } else {
950
- native_slot_set(upb_fielddef_name(field), upb_fielddef_type(field),
951
- field_type_class(layout, field), memory, val);
952
- }
953
-
954
- if (layout->fields[upb_fielddef_index(field)].hasbit !=
955
- MESSAGE_FIELD_NO_HASBIT) {
956
- slot_set_hasbit(layout, storage, field);
957
- }
958
- }
959
-
960
- void layout_init(MessageLayout* layout, void* storage) {
961
- VALUE* value = (VALUE*)CHARPTR_AT(storage, layout->value_offset);
962
- int i;
963
-
964
- for (i = 0; i < layout->repeated_count; i++, value++) {
965
- *value = RepeatedField_new_this_type(*value);
966
- }
967
-
968
- for (i = 0; i < layout->map_count; i++, value++) {
969
- *value = Map_new_this_type(*value);
970
- }
971
- }
972
-
973
- void layout_mark(MessageLayout* layout, void* storage) {
974
- VALUE* values = (VALUE*)CHARPTR_AT(storage, layout->value_offset);
975
- int noneofs = upb_msgdef_numoneofs(layout->msgdef);
976
- int i;
977
-
978
- for (i = 0; i < layout->value_count; i++) {
979
- rb_gc_mark(values[i]);
980
- }
981
-
982
- for (i = 0; i < noneofs; i++) {
983
- MessageOneof* oneof = &layout->oneofs[i];
984
- uint32_t* case_ptr = (uint32_t*)CHARPTR_AT(storage, oneof->case_offset);
985
- if (*case_ptr & ONEOF_CASE_MASK) {
986
- rb_gc_mark(DEREF_OFFSET(storage, oneof->offset, VALUE));
987
- }
988
- }
989
- }
990
-
991
- void layout_dup(MessageLayout* layout, void* to, void* from) {
992
- upb_msg_field_iter it;
993
- for (upb_msg_field_begin(&it, layout->msgdef);
994
- !upb_msg_field_done(&it);
995
- upb_msg_field_next(&it)) {
996
- const upb_fielddef* field = upb_msg_iter_field(&it);
997
- const upb_oneofdef* oneof = upb_fielddef_containingoneof(field);
998
-
999
- void* to_memory = slot_memory(layout, to, field);
1000
- void* from_memory = slot_memory(layout, from, field);
1001
-
1002
- if (oneof) {
1003
- uint32_t* to_oneof_case = slot_oneof_case(layout, to, oneof);
1004
- uint32_t* from_oneof_case = slot_oneof_case(layout, from, oneof);
1005
- if (slot_read_oneof_case(layout, from, oneof) ==
1006
- upb_fielddef_number(field)) {
1007
- *to_oneof_case = *from_oneof_case;
1008
- native_slot_dup(upb_fielddef_type(field), to_memory, from_memory);
1009
- }
1010
- } else if (is_map_field(field)) {
1011
- DEREF(to_memory, VALUE) = Map_dup(DEREF(from_memory, VALUE));
1012
- } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
1013
- DEREF(to_memory, VALUE) = RepeatedField_dup(DEREF(from_memory, VALUE));
1014
- } else {
1015
- if (field_contains_hasbit(layout, field)) {
1016
- if (!slot_is_hasbit_set(layout, from, field)) continue;
1017
- slot_set_hasbit(layout, to, field);
1018
- }
1019
-
1020
- native_slot_dup(upb_fielddef_type(field), to_memory, from_memory);
1021
- }
1022
- }
1023
- }
1024
-
1025
- void layout_deep_copy(MessageLayout* layout, void* to, void* from) {
1026
- upb_msg_field_iter it;
1027
- for (upb_msg_field_begin(&it, layout->msgdef);
1028
- !upb_msg_field_done(&it);
1029
- upb_msg_field_next(&it)) {
1030
- const upb_fielddef* field = upb_msg_iter_field(&it);
1031
- const upb_oneofdef* oneof = upb_fielddef_containingoneof(field);
1032
-
1033
- void* to_memory = slot_memory(layout, to, field);
1034
- void* from_memory = slot_memory(layout, from, field);
1035
-
1036
- if (oneof) {
1037
- uint32_t* to_oneof_case = slot_oneof_case(layout, to, oneof);
1038
- uint32_t* from_oneof_case = slot_oneof_case(layout, from, oneof);
1039
- if (slot_read_oneof_case(layout, from, oneof) ==
1040
- upb_fielddef_number(field)) {
1041
- *to_oneof_case = *from_oneof_case;
1042
- native_slot_deep_copy(upb_fielddef_type(field),
1043
- field_type_class(layout, field), to_memory,
1044
- from_memory);
1045
- }
1046
- } else if (is_map_field(field)) {
1047
- DEREF(to_memory, VALUE) =
1048
- Map_deep_copy(DEREF(from_memory, VALUE));
1049
- } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
1050
- DEREF(to_memory, VALUE) =
1051
- RepeatedField_deep_copy(DEREF(from_memory, VALUE));
1052
- } else {
1053
- if (field_contains_hasbit(layout, field)) {
1054
- if (!slot_is_hasbit_set(layout, from, field)) continue;
1055
- slot_set_hasbit(layout, to, field);
1056
- }
1057
-
1058
- native_slot_deep_copy(upb_fielddef_type(field),
1059
- field_type_class(layout, field), to_memory,
1060
- from_memory);
1061
- }
1062
- }
1063
- }
1064
-
1065
- VALUE layout_eq(MessageLayout* layout, void* msg1, void* msg2) {
1066
- upb_msg_field_iter it;
1067
- for (upb_msg_field_begin(&it, layout->msgdef);
1068
- !upb_msg_field_done(&it);
1069
- upb_msg_field_next(&it)) {
1070
- const upb_fielddef* field = upb_msg_iter_field(&it);
1071
- const upb_oneofdef* oneof = upb_fielddef_containingoneof(field);
1072
-
1073
- void* msg1_memory = slot_memory(layout, msg1, field);
1074
- void* msg2_memory = slot_memory(layout, msg2, field);
1075
-
1076
- if (oneof) {
1077
- uint32_t* msg1_oneof_case = slot_oneof_case(layout, msg1, oneof);
1078
- uint32_t* msg2_oneof_case = slot_oneof_case(layout, msg2, oneof);
1079
- if (*msg1_oneof_case != *msg2_oneof_case ||
1080
- (slot_read_oneof_case(layout, msg1, oneof) ==
1081
- upb_fielddef_number(field) &&
1082
- !native_slot_eq(upb_fielddef_type(field),
1083
- field_type_class(layout, field), msg1_memory,
1084
- msg2_memory))) {
1085
- return Qfalse;
1086
- }
1087
- } else if (is_map_field(field)) {
1088
- if (!Map_eq(DEREF(msg1_memory, VALUE),
1089
- DEREF(msg2_memory, VALUE))) {
1090
- return Qfalse;
1091
- }
1092
- } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
1093
- if (!RepeatedField_eq(DEREF(msg1_memory, VALUE),
1094
- DEREF(msg2_memory, VALUE))) {
1095
- return Qfalse;
1096
- }
1097
- } else {
1098
- if (slot_is_hasbit_set(layout, msg1, field) !=
1099
- slot_is_hasbit_set(layout, msg2, field) ||
1100
- !native_slot_eq(upb_fielddef_type(field),
1101
- field_type_class(layout, field), msg1_memory,
1102
- msg2_memory)) {
1103
- return Qfalse;
1104
- }
1105
- }
1106
- }
1107
- return Qtrue;
1108
- }
1109
-
1110
- VALUE layout_hash(MessageLayout* layout, void* storage) {
1111
- upb_msg_field_iter it;
1112
- st_index_t h = rb_hash_start(0);
1113
- VALUE hash_sym = rb_intern("hash");
1114
- for (upb_msg_field_begin(&it, layout->msgdef);
1115
- !upb_msg_field_done(&it);
1116
- upb_msg_field_next(&it)) {
1117
- const upb_fielddef* field = upb_msg_iter_field(&it);
1118
- VALUE field_val = layout_get(layout, storage, field);
1119
- h = rb_hash_uint(h, NUM2LONG(rb_funcall(field_val, hash_sym, 0)));
1120
- }
1121
- h = rb_hash_end(h);
1122
-
1123
- return INT2FIX(h);
1124
- }
1125
-
1126
- VALUE layout_inspect(MessageLayout* layout, void* storage) {
1127
- VALUE str = rb_str_new2("");
1128
-
1129
- upb_msg_field_iter it;
1130
- bool first = true;
1131
- for (upb_msg_field_begin(&it, layout->msgdef);
1132
- !upb_msg_field_done(&it);
1133
- upb_msg_field_next(&it)) {
1134
- const upb_fielddef* field = upb_msg_iter_field(&it);
1135
- VALUE field_val = layout_get(layout, storage, field);
1136
-
1137
- if (!first) {
1138
- str = rb_str_cat2(str, ", ");
1139
- } else {
1140
- first = false;
1141
- }
1142
- str = rb_str_cat2(str, upb_fielddef_name(field));
1143
- str = rb_str_cat2(str, ": ");
1144
-
1145
- str = rb_str_append(str, rb_funcall(field_val, rb_intern("inspect"), 0));
1146
- }
1147
-
1148
- return str;
1149
- }