google-protobuf 3.5.0 → 3.23.4

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.

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