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.

@@ -35,632 +35,83 @@
35
35
  #include <ruby/vm.h>
36
36
  #include <ruby/encoding.h>
37
37
 
38
- #include "upb.h"
39
-
40
- // Forward decls.
41
- struct DescriptorPool;
42
- struct Descriptor;
43
- struct FileDescriptor;
44
- struct FieldDescriptor;
45
- struct EnumDescriptor;
46
- struct MessageLayout;
47
- struct MessageField;
48
- struct MessageHeader;
49
- struct MessageBuilderContext;
50
- struct EnumBuilderContext;
51
- struct FileBuilderContext;
52
- struct Builder;
53
-
54
- typedef struct DescriptorPool DescriptorPool;
55
- typedef struct Descriptor Descriptor;
56
- typedef struct FileDescriptor FileDescriptor;
57
- typedef struct FieldDescriptor FieldDescriptor;
58
- typedef struct OneofDescriptor OneofDescriptor;
59
- typedef struct EnumDescriptor EnumDescriptor;
60
- typedef struct MessageLayout MessageLayout;
61
- typedef struct MessageField MessageField;
62
- typedef struct MessageOneof MessageOneof;
63
- typedef struct MessageHeader MessageHeader;
64
- typedef struct MessageBuilderContext MessageBuilderContext;
65
- typedef struct OneofBuilderContext OneofBuilderContext;
66
- typedef struct EnumBuilderContext EnumBuilderContext;
67
- typedef struct FileBuilderContext FileBuilderContext;
68
- typedef struct Builder Builder;
69
-
70
- /*
71
- It can be a bit confusing how the C structs defined below and the Ruby
72
- objects interact and hold references to each other. First, a few principles:
73
-
74
- - Ruby's "TypedData" abstraction lets a Ruby VALUE hold a pointer to a C
75
- struct (or arbitrary memory chunk), own it, and free it when collected.
76
- Thus, each struct below will have a corresponding Ruby object
77
- wrapping/owning it.
78
-
79
- - To get back from an underlying upb {msg,enum}def to the Ruby object, we
80
- keep a global hashmap, accessed by get_def_obj/add_def_obj below.
81
-
82
- The in-memory structure is then something like:
83
-
84
- Ruby | upb
85
- |
86
- DescriptorPool ------------|-----------> upb_symtab____________________
87
- | | (message types) \
88
- | v \
89
- Descriptor ---------------|-----------> upb_msgdef (enum types)|
90
- |--> msgclass | | ^ |
91
- | (dynamically built) | | | (submsg fields) |
92
- |--> MessageLayout | | | /
93
- |--------------------------|> decoder method| | /
94
- \--------------------------|> serialize | | /
95
- | handlers v | /
96
- FieldDescriptor -----------|-----------> upb_fielddef /
97
- | | /
98
- | v (enum fields) /
99
- EnumDescriptor ------------|-----------> upb_enumdef <----------'
100
- |
101
- |
102
- ^ | \___/
103
- `---------------|-----------------' (get_def_obj map)
104
- */
105
-
106
- // -----------------------------------------------------------------------------
107
- // Ruby class structure definitions.
108
- // -----------------------------------------------------------------------------
109
-
110
- struct DescriptorPool {
111
- VALUE def_to_descriptor; // Hash table of def* -> Ruby descriptor.
112
- upb_symtab* symtab;
113
- upb_handlercache* fill_handler_cache;
114
- upb_handlercache* pb_serialize_handler_cache;
115
- upb_handlercache* json_serialize_handler_cache;
116
- upb_handlercache* json_serialize_handler_preserve_cache;
117
- upb_pbcodecache* fill_method_cache;
118
- upb_json_codecache* json_fill_method_cache;
119
- };
120
-
121
- struct Descriptor {
122
- const upb_msgdef* msgdef;
123
- MessageLayout* layout;
124
- VALUE klass;
125
- VALUE descriptor_pool;
126
- };
127
-
128
- struct FileDescriptor {
129
- const upb_filedef* filedef;
130
- VALUE descriptor_pool; // Owns the upb_filedef.
131
- };
132
-
133
- struct FieldDescriptor {
134
- const upb_fielddef* fielddef;
135
- VALUE descriptor_pool; // Owns the upb_fielddef.
136
- };
137
-
138
- struct OneofDescriptor {
139
- const upb_oneofdef* oneofdef;
140
- VALUE descriptor_pool; // Owns the upb_oneofdef.
141
- };
142
-
143
- struct EnumDescriptor {
144
- const upb_enumdef* enumdef;
145
- VALUE module; // begins as nil
146
- VALUE descriptor_pool; // Owns the upb_enumdef.
147
- };
148
-
149
- struct MessageBuilderContext {
150
- google_protobuf_DescriptorProto* msg_proto;
151
- VALUE file_builder;
152
- };
153
-
154
- struct OneofBuilderContext {
155
- int oneof_index;
156
- VALUE message_builder;
157
- };
158
-
159
- struct EnumBuilderContext {
160
- google_protobuf_EnumDescriptorProto* enum_proto;
161
- VALUE file_builder;
162
- };
163
-
164
- struct FileBuilderContext {
165
- upb_arena *arena;
166
- google_protobuf_FileDescriptorProto* file_proto;
167
- VALUE descriptor_pool;
168
- };
169
-
170
- struct Builder {
171
- VALUE descriptor_pool;
172
- VALUE default_file_builder;
173
- };
174
-
175
- extern VALUE cDescriptorPool;
176
- extern VALUE cDescriptor;
177
- extern VALUE cFileDescriptor;
178
- extern VALUE cFieldDescriptor;
179
- extern VALUE cEnumDescriptor;
180
- extern VALUE cMessageBuilderContext;
181
- extern VALUE cOneofBuilderContext;
182
- extern VALUE cEnumBuilderContext;
183
- extern VALUE cFileBuilderContext;
184
- extern VALUE cBuilder;
185
-
186
- extern VALUE cError;
187
- extern VALUE cParseError;
188
- extern VALUE cTypeError;
189
-
190
- // We forward-declare all of the Ruby method implementations here because we
191
- // sometimes call the methods directly across .c files, rather than going
192
- // through Ruby's method dispatching (e.g. during message parse). It's cleaner
193
- // to keep the list of object methods together than to split them between
194
- // static-in-file definitions and header declarations.
195
-
196
- void DescriptorPool_mark(void* _self);
197
- void DescriptorPool_free(void* _self);
198
- VALUE DescriptorPool_alloc(VALUE klass);
199
- void DescriptorPool_register(VALUE module);
200
- DescriptorPool* ruby_to_DescriptorPool(VALUE value);
201
- VALUE DescriptorPool_build(int argc, VALUE* argv, VALUE _self);
202
- VALUE DescriptorPool_lookup(VALUE _self, VALUE name);
203
- VALUE DescriptorPool_generated_pool(VALUE _self);
204
-
205
- extern VALUE generated_pool;
206
-
207
- void Descriptor_mark(void* _self);
208
- void Descriptor_free(void* _self);
209
- VALUE Descriptor_alloc(VALUE klass);
210
- void Descriptor_register(VALUE module);
211
- Descriptor* ruby_to_Descriptor(VALUE value);
212
- VALUE Descriptor_initialize(VALUE _self, VALUE cookie, VALUE descriptor_pool,
213
- VALUE ptr);
214
- VALUE Descriptor_name(VALUE _self);
215
- VALUE Descriptor_each(VALUE _self);
216
- VALUE Descriptor_lookup(VALUE _self, VALUE name);
217
- VALUE Descriptor_each_oneof(VALUE _self);
218
- VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name);
219
- VALUE Descriptor_msgclass(VALUE _self);
220
- VALUE Descriptor_file_descriptor(VALUE _self);
221
- extern const rb_data_type_t _Descriptor_type;
222
-
223
- void FileDescriptor_mark(void* _self);
224
- void FileDescriptor_free(void* _self);
225
- VALUE FileDescriptor_alloc(VALUE klass);
226
- void FileDescriptor_register(VALUE module);
227
- FileDescriptor* ruby_to_FileDescriptor(VALUE value);
228
- VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
229
- VALUE descriptor_pool, VALUE ptr);
230
- VALUE FileDescriptor_name(VALUE _self);
231
- VALUE FileDescriptor_syntax(VALUE _self);
232
-
233
- void FieldDescriptor_mark(void* _self);
234
- void FieldDescriptor_free(void* _self);
235
- VALUE FieldDescriptor_alloc(VALUE klass);
236
- void FieldDescriptor_register(VALUE module);
237
- FieldDescriptor* ruby_to_FieldDescriptor(VALUE value);
238
- VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
239
- VALUE descriptor_pool, VALUE ptr);
240
- VALUE FieldDescriptor_name(VALUE _self);
241
- VALUE FieldDescriptor_type(VALUE _self);
242
- VALUE FieldDescriptor_default(VALUE _self);
243
- VALUE FieldDescriptor_label(VALUE _self);
244
- VALUE FieldDescriptor_number(VALUE _self);
245
- VALUE FieldDescriptor_submsg_name(VALUE _self);
246
- VALUE FieldDescriptor_subtype(VALUE _self);
247
- VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb);
248
- VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb);
249
- VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb);
250
- VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value);
251
- upb_fieldtype_t ruby_to_fieldtype(VALUE type);
252
- VALUE fieldtype_to_ruby(upb_fieldtype_t type);
253
-
254
- void OneofDescriptor_mark(void* _self);
255
- void OneofDescriptor_free(void* _self);
256
- VALUE OneofDescriptor_alloc(VALUE klass);
257
- void OneofDescriptor_register(VALUE module);
258
- OneofDescriptor* ruby_to_OneofDescriptor(VALUE value);
259
- VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
260
- VALUE descriptor_pool, VALUE ptr);
261
- VALUE OneofDescriptor_name(VALUE _self);
262
- VALUE OneofDescriptor_each(VALUE _self);
263
-
264
- void EnumDescriptor_mark(void* _self);
265
- void EnumDescriptor_free(void* _self);
266
- VALUE EnumDescriptor_alloc(VALUE klass);
267
- VALUE EnumDescriptor_initialize(VALUE _self, VALUE cookie,
268
- VALUE descriptor_pool, VALUE ptr);
269
- void EnumDescriptor_register(VALUE module);
270
- EnumDescriptor* ruby_to_EnumDescriptor(VALUE value);
271
- VALUE EnumDescriptor_file_descriptor(VALUE _self);
272
- VALUE EnumDescriptor_name(VALUE _self);
273
- VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name);
274
- VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number);
275
- VALUE EnumDescriptor_each(VALUE _self);
276
- VALUE EnumDescriptor_enummodule(VALUE _self);
277
- extern const rb_data_type_t _EnumDescriptor_type;
278
-
279
- void MessageBuilderContext_mark(void* _self);
280
- void MessageBuilderContext_free(void* _self);
281
- VALUE MessageBuilderContext_alloc(VALUE klass);
282
- void MessageBuilderContext_register(VALUE module);
283
- MessageBuilderContext* ruby_to_MessageBuilderContext(VALUE value);
284
- VALUE MessageBuilderContext_initialize(VALUE _self,
285
- VALUE _file_builder,
286
- VALUE name);
287
- VALUE MessageBuilderContext_optional(int argc, VALUE* argv, VALUE _self);
288
- VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self);
289
- VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self);
290
- VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self);
291
- VALUE MessageBuilderContext_oneof(VALUE _self, VALUE name);
292
-
293
- void OneofBuilderContext_mark(void* _self);
294
- void OneofBuilderContext_free(void* _self);
295
- VALUE OneofBuilderContext_alloc(VALUE klass);
296
- void OneofBuilderContext_register(VALUE module);
297
- OneofBuilderContext* ruby_to_OneofBuilderContext(VALUE value);
298
- VALUE OneofBuilderContext_initialize(VALUE _self,
299
- VALUE descriptor,
300
- VALUE builder);
301
- VALUE OneofBuilderContext_optional(int argc, VALUE* argv, VALUE _self);
302
-
303
- void EnumBuilderContext_mark(void* _self);
304
- void EnumBuilderContext_free(void* _self);
305
- VALUE EnumBuilderContext_alloc(VALUE klass);
306
- void EnumBuilderContext_register(VALUE module);
307
- EnumBuilderContext* ruby_to_EnumBuilderContext(VALUE value);
308
- VALUE EnumBuilderContext_initialize(VALUE _self, VALUE _file_builder,
309
- VALUE name);
310
- VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number);
311
-
312
- void FileBuilderContext_mark(void* _self);
313
- void FileBuilderContext_free(void* _self);
314
- VALUE FileBuilderContext_alloc(VALUE klass);
315
- void FileBuilderContext_register(VALUE module);
316
- FileBuilderContext* ruby_to_FileBuilderContext(VALUE _self);
317
- upb_strview FileBuilderContext_strdup(VALUE _self, VALUE rb_str);
318
- upb_strview FileBuilderContext_strdup_name(VALUE _self, VALUE rb_str);
319
- upb_strview FileBuilderContext_strdup_sym(VALUE _self, VALUE rb_sym);
320
- VALUE FileBuilderContext_initialize(VALUE _self, VALUE descriptor_pool,
321
- VALUE name, VALUE options);
322
- VALUE FileBuilderContext_add_message(VALUE _self, VALUE name);
323
- VALUE FileBuilderContext_add_enum(VALUE _self, VALUE name);
324
- VALUE FileBuilderContext_pending_descriptors(VALUE _self);
325
-
326
- void Builder_mark(void* _self);
327
- void Builder_free(void* _self);
328
- VALUE Builder_alloc(VALUE klass);
329
- void Builder_register(VALUE module);
330
- Builder* ruby_to_Builder(VALUE value);
331
- VALUE Builder_build(VALUE _self);
332
- VALUE Builder_initialize(VALUE _self, VALUE descriptor_pool);
333
- VALUE Builder_add_file(int argc, VALUE *argv, VALUE _self);
334
- VALUE Builder_add_message(VALUE _self, VALUE name);
335
- VALUE Builder_add_enum(VALUE _self, VALUE name);
336
- VALUE Builder_finalize_to_pool(VALUE _self, VALUE pool_rb);
337
-
338
- // -----------------------------------------------------------------------------
339
- // Native slot storage abstraction.
340
- // -----------------------------------------------------------------------------
341
-
342
- #define NATIVE_SLOT_MAX_SIZE sizeof(uint64_t)
343
-
344
- size_t native_slot_size(upb_fieldtype_t type);
345
- void native_slot_set(const char* name,
346
- upb_fieldtype_t type,
347
- VALUE type_class,
348
- void* memory,
349
- VALUE value);
350
- // Atomically (with respect to Ruby VM calls) either update the value and set a
351
- // oneof case, or do neither. If |case_memory| is null, then no case value is
352
- // set.
353
- void native_slot_set_value_and_case(const char* name,
354
- upb_fieldtype_t type,
355
- VALUE type_class,
356
- void* memory,
357
- VALUE value,
358
- uint32_t* case_memory,
359
- uint32_t case_number);
360
- VALUE native_slot_get(upb_fieldtype_t type,
361
- VALUE type_class,
362
- const void* memory);
363
- void native_slot_init(upb_fieldtype_t type, void* memory);
364
- void native_slot_mark(upb_fieldtype_t type, void* memory);
365
- void native_slot_dup(upb_fieldtype_t type, void* to, void* from);
366
- void native_slot_deep_copy(upb_fieldtype_t type, VALUE type_class, void* to,
367
- void* from);
368
- bool native_slot_eq(upb_fieldtype_t type, VALUE type_class, void* mem1,
369
- void* mem2);
370
-
371
- VALUE native_slot_encode_and_freeze_string(upb_fieldtype_t type, VALUE value);
372
- void native_slot_check_int_range_precision(const char* name, upb_fieldtype_t type, VALUE value);
373
- uint32_t slot_read_oneof_case(MessageLayout* layout, const void* storage,
374
- const upb_oneofdef* oneof);
375
- bool is_value_field(const upb_fielddef* f);
376
-
377
- extern rb_encoding* kRubyStringUtf8Encoding;
378
- extern rb_encoding* kRubyStringASCIIEncoding;
379
- extern rb_encoding* kRubyString8bitEncoding;
380
-
381
- VALUE field_type_class(const MessageLayout* layout, const upb_fielddef* field);
382
-
383
- #define MAP_KEY_FIELD 1
384
- #define MAP_VALUE_FIELD 2
385
-
386
- // Oneof case slot value to indicate that no oneof case is set. The value `0` is
387
- // safe because field numbers are used as case identifiers, and no field can
388
- // have a number of 0.
389
- #define ONEOF_CASE_NONE 0
38
+ #include "ruby-upb.h"
39
+ #include "defs.h"
390
40
 
391
41
  // These operate on a map field (i.e., a repeated field of submessages whose
392
42
  // submessage type is a map-entry msgdef).
393
- bool is_map_field(const upb_fielddef* field);
394
43
  const upb_fielddef* map_field_key(const upb_fielddef* field);
395
44
  const upb_fielddef* map_field_value(const upb_fielddef* field);
396
45
 
397
- // These operate on a map-entry msgdef.
398
- const upb_fielddef* map_entry_key(const upb_msgdef* msgdef);
399
- const upb_fielddef* map_entry_value(const upb_msgdef* msgdef);
400
-
401
46
  // -----------------------------------------------------------------------------
402
- // Repeated field container type.
47
+ // Arena
403
48
  // -----------------------------------------------------------------------------
404
49
 
405
- typedef struct {
406
- upb_fieldtype_t field_type;
407
- VALUE field_type_class;
408
- void* elements;
409
- int size;
410
- int capacity;
411
- } RepeatedField;
50
+ // A Ruby object that wraps an underlying upb_arena. Any objects that are
51
+ // allocated from this arena should reference the Arena in rb_gc_mark(), to
52
+ // ensure that the object's underlying memory outlives any Ruby object that can
53
+ // reach it.
412
54
 
413
- void RepeatedField_mark(void* self);
414
- void RepeatedField_free(void* self);
415
- VALUE RepeatedField_alloc(VALUE klass);
416
- VALUE RepeatedField_init(int argc, VALUE* argv, VALUE self);
417
- void RepeatedField_register(VALUE module);
55
+ VALUE Arena_new();
56
+ upb_arena *Arena_get(VALUE arena);
418
57
 
419
- extern const rb_data_type_t RepeatedField_type;
420
- extern VALUE cRepeatedField;
58
+ // Fuses this arena to another, throwing a Ruby exception if this is not
59
+ // possible.
60
+ void Arena_fuse(VALUE arena, upb_arena *other);
421
61
 
422
- RepeatedField* ruby_to_RepeatedField(VALUE value);
423
-
424
- VALUE RepeatedField_new_this_type(VALUE _self);
425
- VALUE RepeatedField_each(VALUE _self);
426
- VALUE RepeatedField_index(int argc, VALUE* argv, VALUE _self);
427
- void* RepeatedField_index_native(VALUE _self, int index);
428
- int RepeatedField_size(VALUE _self);
429
- VALUE RepeatedField_index_set(VALUE _self, VALUE _index, VALUE val);
430
- void RepeatedField_reserve(RepeatedField* self, int new_size);
431
- VALUE RepeatedField_push(VALUE _self, VALUE val);
432
- void RepeatedField_push_native(VALUE _self, void* data);
433
- VALUE RepeatedField_pop_one(VALUE _self);
434
- VALUE RepeatedField_insert(int argc, VALUE* argv, VALUE _self);
435
- VALUE RepeatedField_replace(VALUE _self, VALUE list);
436
- VALUE RepeatedField_clear(VALUE _self);
437
- VALUE RepeatedField_length(VALUE _self);
438
- VALUE RepeatedField_dup(VALUE _self);
439
- VALUE RepeatedField_deep_copy(VALUE _self);
440
- VALUE RepeatedField_to_ary(VALUE _self);
441
- VALUE RepeatedField_eq(VALUE _self, VALUE _other);
442
- VALUE RepeatedField_hash(VALUE _self);
443
- VALUE RepeatedField_inspect(VALUE _self);
444
- VALUE RepeatedField_plus(VALUE _self, VALUE list);
445
-
446
- // Defined in repeated_field.c; also used by Map.
447
- void validate_type_class(upb_fieldtype_t type, VALUE klass);
448
-
449
- // -----------------------------------------------------------------------------
450
- // Map container type.
451
- // -----------------------------------------------------------------------------
452
-
453
- typedef struct {
454
- upb_fieldtype_t key_type;
455
- upb_fieldtype_t value_type;
456
- VALUE value_type_class;
457
- VALUE parse_frame;
458
- upb_strtable table;
459
- } Map;
460
-
461
- void Map_mark(void* self);
462
- void Map_free(void* self);
463
- VALUE Map_alloc(VALUE klass);
464
- VALUE Map_init(int argc, VALUE* argv, VALUE self);
465
- void Map_register(VALUE module);
466
- VALUE Map_set_frame(VALUE self, VALUE val);
467
-
468
- extern const rb_data_type_t Map_type;
469
- extern VALUE cMap;
470
-
471
- Map* ruby_to_Map(VALUE value);
472
-
473
- VALUE Map_new_this_type(VALUE _self);
474
- VALUE Map_each(VALUE _self);
475
- VALUE Map_keys(VALUE _self);
476
- VALUE Map_values(VALUE _self);
477
- VALUE Map_index(VALUE _self, VALUE key);
478
- VALUE Map_index_set(VALUE _self, VALUE key, VALUE value);
479
- VALUE Map_has_key(VALUE _self, VALUE key);
480
- VALUE Map_delete(VALUE _self, VALUE key);
481
- VALUE Map_clear(VALUE _self);
482
- VALUE Map_length(VALUE _self);
483
- VALUE Map_dup(VALUE _self);
484
- VALUE Map_deep_copy(VALUE _self);
485
- VALUE Map_eq(VALUE _self, VALUE _other);
486
- VALUE Map_hash(VALUE _self);
487
- VALUE Map_to_h(VALUE _self);
488
- VALUE Map_inspect(VALUE _self);
489
- VALUE Map_merge(VALUE _self, VALUE hashmap);
490
- VALUE Map_merge_into_self(VALUE _self, VALUE hashmap);
491
-
492
- typedef struct {
493
- Map* self;
494
- upb_strtable_iter it;
495
- } Map_iter;
496
-
497
- void Map_begin(VALUE _self, Map_iter* iter);
498
- void Map_next(Map_iter* iter);
499
- bool Map_done(Map_iter* iter);
500
- VALUE Map_iter_key(Map_iter* iter);
501
- VALUE Map_iter_value(Map_iter* iter);
62
+ // Pins this Ruby object to the lifetime of this arena, so that as long as the
63
+ // arena is alive this object will not be collected.
64
+ //
65
+ // We use this to guarantee that the "frozen" bit on the object will be
66
+ // remembered, even if the user drops their reference to this precise object.
67
+ void Arena_Pin(VALUE arena, VALUE obj);
502
68
 
503
69
  // -----------------------------------------------------------------------------
504
- // Message layout / storage.
70
+ // ObjectCache
505
71
  // -----------------------------------------------------------------------------
506
72
 
507
- #define MESSAGE_FIELD_NO_HASBIT ((uint32_t)-1)
508
-
509
- struct MessageField {
510
- uint32_t offset;
511
- uint32_t hasbit;
512
- };
513
-
514
- struct MessageOneof {
515
- uint32_t offset;
516
- uint32_t case_offset;
517
- };
518
-
519
- // MessageLayout is owned by the enclosing Descriptor, which must outlive us.
520
- struct MessageLayout {
521
- const Descriptor* desc;
522
- const upb_msgdef* msgdef;
523
- void* empty_template; // Can memcpy() onto a layout to clear it.
524
- MessageField* fields;
525
- MessageOneof* oneofs;
526
- uint32_t size;
527
- uint32_t value_offset;
528
- int value_count;
529
- int repeated_count;
530
- int map_count;
531
- };
532
-
533
- #define ONEOF_CASE_MASK 0x80000000
73
+ // Global object cache from upb array/map/message/symtab to wrapper object.
74
+ //
75
+ // This is a conceptually "weak" cache, in that it does not prevent "val" from
76
+ // being collected (though in Ruby <2.7 is it effectively strong, due to
77
+ // implementation limitations).
534
78
 
535
- void create_layout(Descriptor* desc);
536
- void free_layout(MessageLayout* layout);
537
- bool field_contains_hasbit(MessageLayout* layout,
538
- const upb_fielddef* field);
539
- VALUE layout_get_default(const upb_fielddef* field);
540
- VALUE layout_get(MessageLayout* layout,
541
- const void* storage,
542
- const upb_fielddef* field);
543
- void layout_set(MessageLayout* layout,
544
- void* storage,
545
- const upb_fielddef* field,
546
- VALUE val);
547
- VALUE layout_has(MessageLayout* layout,
548
- const void* storage,
549
- const upb_fielddef* field);
550
- void layout_clear(MessageLayout* layout,
551
- const void* storage,
552
- const upb_fielddef* field);
553
- void layout_init(MessageLayout* layout, void* storage);
554
- void layout_mark(MessageLayout* layout, void* storage);
555
- void layout_dup(MessageLayout* layout, void* to, void* from);
556
- void layout_deep_copy(MessageLayout* layout, void* to, void* from);
557
- VALUE layout_eq(MessageLayout* layout, void* msg1, void* msg2);
558
- VALUE layout_hash(MessageLayout* layout, void* storage);
559
- VALUE layout_inspect(MessageLayout* layout, void* storage);
79
+ // Adds an entry to the cache. The "arena" parameter must give the arena that
80
+ // "key" was allocated from. In Ruby <2.7.0, it will be used to remove the key
81
+ // from the cache when the arena is destroyed.
82
+ void ObjectCache_Add(const void* key, VALUE val);
560
83
 
561
- bool is_wrapper_type_field(const upb_fielddef* field);
562
- VALUE ruby_wrapper_type(VALUE type_class, VALUE value);
84
+ // Returns the cached object for this key, if any. Otherwise returns Qnil.
85
+ VALUE ObjectCache_Get(const void* key);
563
86
 
564
87
  // -----------------------------------------------------------------------------
565
- // Message class creation.
88
+ // StringBuilder, for inspect
566
89
  // -----------------------------------------------------------------------------
567
90
 
568
- // This should probably be factored into a common upb component.
569
-
570
- typedef struct {
571
- upb_byteshandler handler;
572
- upb_bytessink sink;
573
- char *ptr;
574
- size_t len, size;
575
- } stringsink;
576
-
577
- void stringsink_uninit(stringsink *sink);
578
-
579
- struct MessageHeader {
580
- Descriptor* descriptor; // kept alive by self.class.descriptor reference.
581
- stringsink* unknown_fields; // store unknown fields in decoding.
582
- // Data comes after this.
583
- };
91
+ struct StringBuilder;
92
+ typedef struct StringBuilder StringBuilder;
584
93
 
585
- extern rb_data_type_t Message_type;
94
+ StringBuilder* StringBuilder_New();
95
+ void StringBuilder_Free(StringBuilder* b);
96
+ void StringBuilder_Printf(StringBuilder* b, const char *fmt, ...);
97
+ VALUE StringBuilder_ToRubyString(StringBuilder* b);
586
98
 
587
- VALUE build_class_from_descriptor(VALUE descriptor);
588
- void* Message_data(void* msg);
589
- void Message_mark(void* self);
590
- void Message_free(void* self);
591
- VALUE Message_alloc(VALUE klass);
592
- VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self);
593
- VALUE Message_initialize(int argc, VALUE* argv, VALUE _self);
594
- VALUE Message_dup(VALUE _self);
595
- VALUE Message_deep_copy(VALUE _self);
596
- VALUE Message_eq(VALUE _self, VALUE _other);
597
- VALUE Message_hash(VALUE _self);
598
- VALUE Message_inspect(VALUE _self);
599
- VALUE Message_to_h(VALUE _self);
600
- VALUE Message_index(VALUE _self, VALUE field_name);
601
- VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value);
602
- VALUE Message_descriptor(VALUE klass);
603
- VALUE Message_decode(VALUE klass, VALUE data);
604
- VALUE Message_encode(VALUE klass, VALUE msg_rb);
605
- VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass);
606
- VALUE Message_encode_json(int argc, VALUE* argv, VALUE klass);
607
-
608
- VALUE Google_Protobuf_discard_unknown(VALUE self, VALUE msg_rb);
609
- VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj);
610
-
611
- VALUE build_module_from_enumdesc(VALUE _enumdesc);
612
- VALUE enum_lookup(VALUE self, VALUE number);
613
- VALUE enum_resolve(VALUE self, VALUE sym);
614
- VALUE enum_descriptor(VALUE self);
615
-
616
- const upb_pbdecodermethod *new_fillmsg_decodermethod(
617
- Descriptor* descriptor, const void *owner);
618
- void add_handlers_for_message(const void *closure, upb_handlers *h);
619
-
620
- // Maximum depth allowed during encoding, to avoid stack overflows due to
621
- // cycles.
622
- #define ENCODE_MAX_NESTING 63
623
-
624
- // -----------------------------------------------------------------------------
625
- // A cache of frozen string objects to use as field defaults.
626
- // -----------------------------------------------------------------------------
627
- VALUE get_frozen_string(const char* data, size_t size, bool binary);
628
-
629
- // -----------------------------------------------------------------------------
630
- // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
631
- // instances.
632
- // -----------------------------------------------------------------------------
633
- VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_msgdef* def);
634
- VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_enumdef* def);
635
- VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_fielddef* def);
636
- VALUE get_filedef_obj(VALUE descriptor_pool, const upb_filedef* def);
637
- VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_oneofdef* def);
99
+ void StringBuilder_PrintMsgval(StringBuilder* b, upb_msgval val, TypeInfo info);
638
100
 
639
101
  // -----------------------------------------------------------------------------
640
102
  // Utilities.
641
103
  // -----------------------------------------------------------------------------
642
104
 
643
- void check_upb_status(const upb_status* status, const char* msg);
644
-
645
- #define CHECK_UPB(code, msg) do { \
646
- upb_status status = UPB_STATUS_INIT; \
647
- code; \
648
- check_upb_status(&status, msg); \
649
- } while (0)
650
-
651
- extern ID descriptor_instancevar_interned;
652
-
653
- // A distinct object that is not accessible from Ruby. We use this as a
654
- // constructor argument to enforce that certain objects cannot be created from
655
- // Ruby.
656
- extern VALUE c_only_cookie;
105
+ extern VALUE cTypeError;
657
106
 
658
107
  #ifdef NDEBUG
659
- #define UPB_ASSERT(expr) do {} while (false && (expr))
108
+ #define PBRUBY_ASSERT(expr) do {} while (false && (expr))
660
109
  #else
661
- #define UPB_ASSERT(expr) assert(expr)
110
+ #define PBRUBY_ASSERT(expr) assert(expr)
662
111
  #endif
663
112
 
113
+ #define PBRUBY_MAX(x, y) (((x) > (y)) ? (x) : (y))
114
+
664
115
  #define UPB_UNUSED(var) (void)var
665
116
 
666
117
  #endif // __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__