google-protobuf 3.7.0 → 3.21.0

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 (46) hide show
  1. checksums.yaml +4 -4
  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 +669 -1646
  5. data/ext/google/protobuf_c/defs.h +107 -0
  6. data/ext/google/protobuf_c/extconf.rb +13 -8
  7. data/ext/google/protobuf_c/map.c +330 -477
  8. data/ext/google/protobuf_c/map.h +66 -0
  9. data/ext/google/protobuf_c/message.c +1048 -379
  10. data/ext/google/protobuf_c/message.h +104 -0
  11. data/ext/google/protobuf_c/protobuf.c +413 -54
  12. data/ext/google/protobuf_c/protobuf.h +53 -546
  13. data/ext/google/protobuf_c/repeated_field.c +318 -315
  14. data/ext/google/protobuf_c/repeated_field.h +63 -0
  15. data/ext/google/protobuf_c/ruby-upb.c +11115 -0
  16. data/ext/google/protobuf_c/ruby-upb.h +5612 -0
  17. data/ext/google/protobuf_c/third_party/utf8_range/LICENSE +21 -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 +9 -0
  22. data/ext/google/protobuf_c/wrap_memcpy.c +4 -3
  23. data/lib/google/protobuf/any_pb.rb +1 -1
  24. data/lib/google/protobuf/api_pb.rb +4 -3
  25. data/lib/google/protobuf/descriptor_dsl.rb +465 -0
  26. data/lib/google/protobuf/descriptor_pb.rb +269 -0
  27. data/lib/google/protobuf/duration_pb.rb +1 -1
  28. data/lib/google/protobuf/empty_pb.rb +1 -1
  29. data/lib/google/protobuf/field_mask_pb.rb +1 -1
  30. data/lib/google/protobuf/message_exts.rb +2 -2
  31. data/lib/google/protobuf/repeated_field.rb +15 -2
  32. data/lib/google/protobuf/source_context_pb.rb +1 -1
  33. data/lib/google/protobuf/struct_pb.rb +4 -4
  34. data/lib/google/protobuf/timestamp_pb.rb +1 -1
  35. data/lib/google/protobuf/type_pb.rb +9 -8
  36. data/lib/google/protobuf/well_known_types.rb +20 -4
  37. data/lib/google/protobuf/wrappers_pb.rb +9 -9
  38. data/lib/google/protobuf.rb +6 -4
  39. data/tests/basic.rb +455 -77
  40. data/tests/generated_code_test.rb +0 -0
  41. data/tests/stress.rb +1 -1
  42. metadata +27 -30
  43. data/ext/google/protobuf_c/encode_decode.c +0 -1574
  44. data/ext/google/protobuf_c/storage.c +0 -1019
  45. data/ext/google/protobuf_c/upb.c +0 -17318
  46. data/ext/google/protobuf_c/upb.h +0 -9755
@@ -31,583 +31,90 @@
31
31
  #ifndef __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__
32
32
  #define __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__
33
33
 
34
+ #include <ruby/encoding.h>
34
35
  #include <ruby/ruby.h>
35
36
  #include <ruby/vm.h>
36
- #include <ruby/encoding.h>
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 MessageHeader MessageHeader;
63
- typedef struct MessageBuilderContext MessageBuilderContext;
64
- typedef struct OneofBuilderContext OneofBuilderContext;
65
- typedef struct EnumBuilderContext EnumBuilderContext;
66
- typedef struct FileBuilderContext FileBuilderContext;
67
- typedef struct Builder Builder;
68
-
69
- /*
70
- It can be a bit confusing how the C structs defined below and the Ruby
71
- objects interact and hold references to each other. First, a few principles:
72
-
73
- - Ruby's "TypedData" abstraction lets a Ruby VALUE hold a pointer to a C
74
- struct (or arbitrary memory chunk), own it, and free it when collected.
75
- Thus, each struct below will have a corresponding Ruby object
76
- wrapping/owning it.
77
-
78
- - To get back from an underlying upb {msg,enum}def to the Ruby object, we
79
- keep a global hashmap, accessed by get_def_obj/add_def_obj below.
80
-
81
- The in-memory structure is then something like:
82
-
83
- Ruby | upb
84
- |
85
- DescriptorPool ------------|-----------> upb_symtab____________________
86
- | | (message types) \
87
- | v \
88
- Descriptor ---------------|-----------> upb_msgdef (enum types)|
89
- |--> msgclass | | ^ |
90
- | (dynamically built) | | | (submsg fields) |
91
- |--> MessageLayout | | | /
92
- |--------------------------|> decoder method| | /
93
- \--------------------------|> serialize | | /
94
- | handlers v | /
95
- FieldDescriptor -----------|-----------> upb_fielddef /
96
- | | /
97
- | v (enum fields) /
98
- EnumDescriptor ------------|-----------> upb_enumdef <----------'
99
- |
100
- |
101
- ^ | \___/
102
- `---------------|-----------------' (get_def_obj map)
103
- */
104
-
105
- // -----------------------------------------------------------------------------
106
- // Ruby class structure definitions.
107
- // -----------------------------------------------------------------------------
108
-
109
- struct DescriptorPool {
110
- upb_symtab* symtab;
111
- };
112
-
113
- struct Descriptor {
114
- const upb_msgdef* msgdef;
115
- MessageLayout* layout;
116
- VALUE klass; // begins as nil
117
- const upb_handlers* fill_handlers;
118
- const upb_pbdecodermethod* fill_method;
119
- const upb_json_parsermethod* json_fill_method;
120
- const upb_handlers* pb_serialize_handlers;
121
- const upb_handlers* json_serialize_handlers;
122
- const upb_handlers* json_serialize_handlers_preserve;
123
- };
124
-
125
- struct FileDescriptor {
126
- const upb_filedef* filedef;
127
- };
128
-
129
- struct FieldDescriptor {
130
- const upb_fielddef* fielddef;
131
- };
132
-
133
- struct OneofDescriptor {
134
- const upb_oneofdef* oneofdef;
135
- };
136
-
137
- struct EnumDescriptor {
138
- const upb_enumdef* enumdef;
139
- VALUE module; // begins as nil
140
- };
141
-
142
- struct MessageBuilderContext {
143
- VALUE descriptor;
144
- VALUE builder;
145
- };
146
-
147
- struct OneofBuilderContext {
148
- VALUE descriptor;
149
- VALUE builder;
150
- };
151
-
152
- struct EnumBuilderContext {
153
- VALUE enumdesc;
154
- };
155
-
156
- struct FileBuilderContext {
157
- VALUE pending_list;
158
- VALUE file_descriptor;
159
- VALUE builder;
160
- };
161
-
162
- struct Builder {
163
- VALUE pending_list;
164
- VALUE default_file_descriptor;
165
- upb_def** defs; // used only while finalizing
166
- };
167
-
168
- extern VALUE cDescriptorPool;
169
- extern VALUE cDescriptor;
170
- extern VALUE cFileDescriptor;
171
- extern VALUE cFieldDescriptor;
172
- extern VALUE cEnumDescriptor;
173
- extern VALUE cMessageBuilderContext;
174
- extern VALUE cOneofBuilderContext;
175
- extern VALUE cEnumBuilderContext;
176
- extern VALUE cFileBuilderContext;
177
- extern VALUE cBuilder;
178
-
179
- extern VALUE cError;
180
- extern VALUE cParseError;
181
- extern VALUE cTypeError;
182
-
183
- // We forward-declare all of the Ruby method implementations here because we
184
- // sometimes call the methods directly across .c files, rather than going
185
- // through Ruby's method dispatching (e.g. during message parse). It's cleaner
186
- // to keep the list of object methods together than to split them between
187
- // static-in-file definitions and header declarations.
188
-
189
- void DescriptorPool_mark(void* _self);
190
- void DescriptorPool_free(void* _self);
191
- VALUE DescriptorPool_alloc(VALUE klass);
192
- void DescriptorPool_register(VALUE module);
193
- DescriptorPool* ruby_to_DescriptorPool(VALUE value);
194
- VALUE DescriptorPool_add(VALUE _self, VALUE def);
195
- VALUE DescriptorPool_build(int argc, VALUE* argv, VALUE _self);
196
- VALUE DescriptorPool_lookup(VALUE _self, VALUE name);
197
- VALUE DescriptorPool_generated_pool(VALUE _self);
198
-
199
- extern VALUE generated_pool;
200
-
201
- void Descriptor_mark(void* _self);
202
- void Descriptor_free(void* _self);
203
- VALUE Descriptor_alloc(VALUE klass);
204
- void Descriptor_register(VALUE module);
205
- Descriptor* ruby_to_Descriptor(VALUE value);
206
- VALUE Descriptor_initialize(VALUE _self, VALUE file_descriptor_rb);
207
- VALUE Descriptor_name(VALUE _self);
208
- VALUE Descriptor_name_set(VALUE _self, VALUE str);
209
- VALUE Descriptor_each(VALUE _self);
210
- VALUE Descriptor_lookup(VALUE _self, VALUE name);
211
- VALUE Descriptor_add_field(VALUE _self, VALUE obj);
212
- VALUE Descriptor_add_oneof(VALUE _self, VALUE obj);
213
- VALUE Descriptor_each_oneof(VALUE _self);
214
- VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name);
215
- VALUE Descriptor_msgclass(VALUE _self);
216
- VALUE Descriptor_file_descriptor(VALUE _self);
217
- extern const rb_data_type_t _Descriptor_type;
218
-
219
- void FileDescriptor_mark(void* _self);
220
- void FileDescriptor_free(void* _self);
221
- VALUE FileDescriptor_alloc(VALUE klass);
222
- void FileDescriptor_register(VALUE module);
223
- FileDescriptor* ruby_to_FileDescriptor(VALUE value);
224
- VALUE FileDescriptor_initialize(int argc, VALUE* argv, VALUE _self);
225
- VALUE FileDescriptor_name(VALUE _self);
226
- VALUE FileDescriptor_syntax(VALUE _self);
227
- VALUE FileDescriptor_syntax_set(VALUE _self, VALUE syntax);
228
-
229
- void FieldDescriptor_mark(void* _self);
230
- void FieldDescriptor_free(void* _self);
231
- VALUE FieldDescriptor_alloc(VALUE klass);
232
- void FieldDescriptor_register(VALUE module);
233
- FieldDescriptor* ruby_to_FieldDescriptor(VALUE value);
234
- VALUE FieldDescriptor_name(VALUE _self);
235
- VALUE FieldDescriptor_name_set(VALUE _self, VALUE str);
236
- VALUE FieldDescriptor_type(VALUE _self);
237
- VALUE FieldDescriptor_type_set(VALUE _self, VALUE type);
238
- VALUE FieldDescriptor_default(VALUE _self);
239
- VALUE FieldDescriptor_default_set(VALUE _self, VALUE default_value);
240
- VALUE FieldDescriptor_label(VALUE _self);
241
- VALUE FieldDescriptor_label_set(VALUE _self, VALUE label);
242
- VALUE FieldDescriptor_number(VALUE _self);
243
- VALUE FieldDescriptor_number_set(VALUE _self, VALUE number);
244
- VALUE FieldDescriptor_submsg_name(VALUE _self);
245
- VALUE FieldDescriptor_submsg_name_set(VALUE _self, VALUE value);
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_name(VALUE _self);
260
- VALUE OneofDescriptor_name_set(VALUE _self, VALUE value);
261
- VALUE OneofDescriptor_add_field(VALUE _self, VALUE field);
262
- VALUE OneofDescriptor_each(VALUE _self, VALUE field);
263
-
264
- void EnumDescriptor_mark(void* _self);
265
- void EnumDescriptor_free(void* _self);
266
- VALUE EnumDescriptor_alloc(VALUE klass);
267
- void EnumDescriptor_register(VALUE module);
268
- EnumDescriptor* ruby_to_EnumDescriptor(VALUE value);
269
- VALUE EnumDescriptor_initialize(VALUE _self, VALUE file_descriptor_rb);
270
- VALUE EnumDescriptor_file_descriptor(VALUE _self);
271
- VALUE EnumDescriptor_name(VALUE _self);
272
- VALUE EnumDescriptor_name_set(VALUE _self, VALUE str);
273
- VALUE EnumDescriptor_add_value(VALUE _self, VALUE name, VALUE number);
274
- VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name);
275
- VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number);
276
- VALUE EnumDescriptor_each(VALUE _self);
277
- VALUE EnumDescriptor_enummodule(VALUE _self);
278
- extern const rb_data_type_t _EnumDescriptor_type;
279
-
280
- void MessageBuilderContext_mark(void* _self);
281
- void MessageBuilderContext_free(void* _self);
282
- VALUE MessageBuilderContext_alloc(VALUE klass);
283
- void MessageBuilderContext_register(VALUE module);
284
- MessageBuilderContext* ruby_to_MessageBuilderContext(VALUE value);
285
- VALUE MessageBuilderContext_initialize(VALUE _self,
286
- VALUE descriptor,
287
- VALUE builder);
288
- VALUE MessageBuilderContext_optional(int argc, VALUE* argv, VALUE _self);
289
- VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self);
290
- VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self);
291
- VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self);
292
- VALUE MessageBuilderContext_oneof(VALUE _self, VALUE name);
293
-
294
- void OneofBuilderContext_mark(void* _self);
295
- void OneofBuilderContext_free(void* _self);
296
- VALUE OneofBuilderContext_alloc(VALUE klass);
297
- void OneofBuilderContext_register(VALUE module);
298
- OneofBuilderContext* ruby_to_OneofBuilderContext(VALUE value);
299
- VALUE OneofBuilderContext_initialize(VALUE _self,
300
- VALUE descriptor,
301
- VALUE builder);
302
- VALUE OneofBuilderContext_optional(int argc, VALUE* argv, VALUE _self);
303
-
304
- void EnumBuilderContext_mark(void* _self);
305
- void EnumBuilderContext_free(void* _self);
306
- VALUE EnumBuilderContext_alloc(VALUE klass);
307
- void EnumBuilderContext_register(VALUE module);
308
- EnumBuilderContext* ruby_to_EnumBuilderContext(VALUE value);
309
- VALUE EnumBuilderContext_initialize(VALUE _self, VALUE enumdesc);
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
- VALUE FileBuilderContext_initialize(VALUE _self, VALUE file_descriptor,
317
- VALUE builder);
318
- VALUE FileBuilderContext_add_message(VALUE _self, VALUE name);
319
- VALUE FileBuilderContext_add_enum(VALUE _self, VALUE name);
320
- VALUE FileBuilderContext_pending_descriptors(VALUE _self);
321
37
 
322
- void Builder_mark(void* _self);
323
- void Builder_free(void* _self);
324
- VALUE Builder_alloc(VALUE klass);
325
- void Builder_register(VALUE module);
326
- Builder* ruby_to_Builder(VALUE value);
327
- VALUE Builder_initialize(VALUE _self);
328
- VALUE Builder_add_file(int argc, VALUE *argv, VALUE _self);
329
- VALUE Builder_add_message(VALUE _self, VALUE name);
330
- VALUE Builder_add_enum(VALUE _self, VALUE name);
331
- VALUE Builder_finalize_to_pool(VALUE _self, VALUE pool_rb);
332
-
333
- // -----------------------------------------------------------------------------
334
- // Native slot storage abstraction.
335
- // -----------------------------------------------------------------------------
336
-
337
- #define NATIVE_SLOT_MAX_SIZE sizeof(uint64_t)
338
-
339
- size_t native_slot_size(upb_fieldtype_t type);
340
- void native_slot_set(upb_fieldtype_t type,
341
- VALUE type_class,
342
- void* memory,
343
- VALUE value);
344
- // Atomically (with respect to Ruby VM calls) either update the value and set a
345
- // oneof case, or do neither. If |case_memory| is null, then no case value is
346
- // set.
347
- void native_slot_set_value_and_case(upb_fieldtype_t type,
348
- VALUE type_class,
349
- void* memory,
350
- VALUE value,
351
- uint32_t* case_memory,
352
- uint32_t case_number);
353
- VALUE native_slot_get(upb_fieldtype_t type,
354
- VALUE type_class,
355
- const void* memory);
356
- void native_slot_init(upb_fieldtype_t type, void* memory);
357
- void native_slot_mark(upb_fieldtype_t type, void* memory);
358
- void native_slot_dup(upb_fieldtype_t type, void* to, void* from);
359
- void native_slot_deep_copy(upb_fieldtype_t type, void* to, void* from);
360
- bool native_slot_eq(upb_fieldtype_t type, void* mem1, void* mem2);
361
-
362
- VALUE native_slot_encode_and_freeze_string(upb_fieldtype_t type, VALUE value);
363
- void native_slot_check_int_range_precision(upb_fieldtype_t type, VALUE value);
364
-
365
- extern rb_encoding* kRubyStringUtf8Encoding;
366
- extern rb_encoding* kRubyStringASCIIEncoding;
367
- extern rb_encoding* kRubyString8bitEncoding;
368
-
369
- VALUE field_type_class(const upb_fielddef* field);
370
-
371
- #define MAP_KEY_FIELD 1
372
- #define MAP_VALUE_FIELD 2
373
-
374
- // Oneof case slot value to indicate that no oneof case is set. The value `0` is
375
- // safe because field numbers are used as case identifiers, and no field can
376
- // have a number of 0.
377
- #define ONEOF_CASE_NONE 0
38
+ #include "defs.h"
39
+ #include "ruby-upb.h"
378
40
 
379
41
  // These operate on a map field (i.e., a repeated field of submessages whose
380
42
  // submessage type is a map-entry msgdef).
381
- bool is_map_field(const upb_fielddef* field);
382
- const upb_fielddef* map_field_key(const upb_fielddef* field);
383
- const upb_fielddef* map_field_value(const upb_fielddef* field);
384
-
385
- // These operate on a map-entry msgdef.
386
- const upb_fielddef* map_entry_key(const upb_msgdef* msgdef);
387
- const upb_fielddef* map_entry_value(const upb_msgdef* msgdef);
388
-
389
- // -----------------------------------------------------------------------------
390
- // Repeated field container type.
391
- // -----------------------------------------------------------------------------
392
-
393
- typedef struct {
394
- upb_fieldtype_t field_type;
395
- VALUE field_type_class;
396
- void* elements;
397
- int size;
398
- int capacity;
399
- } RepeatedField;
400
-
401
- void RepeatedField_mark(void* self);
402
- void RepeatedField_free(void* self);
403
- VALUE RepeatedField_alloc(VALUE klass);
404
- VALUE RepeatedField_init(int argc, VALUE* argv, VALUE self);
405
- void RepeatedField_register(VALUE module);
406
-
407
- extern const rb_data_type_t RepeatedField_type;
408
- extern VALUE cRepeatedField;
409
-
410
- RepeatedField* ruby_to_RepeatedField(VALUE value);
411
-
412
- VALUE RepeatedField_each(VALUE _self);
413
- VALUE RepeatedField_index(int argc, VALUE* argv, VALUE _self);
414
- void* RepeatedField_index_native(VALUE _self, int index);
415
- int RepeatedField_size(VALUE _self);
416
- VALUE RepeatedField_index_set(VALUE _self, VALUE _index, VALUE val);
417
- void RepeatedField_reserve(RepeatedField* self, int new_size);
418
- VALUE RepeatedField_push(VALUE _self, VALUE val);
419
- void RepeatedField_push_native(VALUE _self, void* data);
420
- VALUE RepeatedField_pop_one(VALUE _self);
421
- VALUE RepeatedField_insert(int argc, VALUE* argv, VALUE _self);
422
- VALUE RepeatedField_replace(VALUE _self, VALUE list);
423
- VALUE RepeatedField_clear(VALUE _self);
424
- VALUE RepeatedField_length(VALUE _self);
425
- VALUE RepeatedField_dup(VALUE _self);
426
- VALUE RepeatedField_deep_copy(VALUE _self);
427
- VALUE RepeatedField_to_ary(VALUE _self);
428
- VALUE RepeatedField_eq(VALUE _self, VALUE _other);
429
- VALUE RepeatedField_hash(VALUE _self);
430
- VALUE RepeatedField_inspect(VALUE _self);
431
- VALUE RepeatedField_plus(VALUE _self, VALUE list);
432
-
433
- // Defined in repeated_field.c; also used by Map.
434
- void validate_type_class(upb_fieldtype_t type, VALUE klass);
43
+ const upb_FieldDef* map_field_key(const upb_FieldDef* field);
44
+ const upb_FieldDef* map_field_value(const upb_FieldDef* field);
435
45
 
436
46
  // -----------------------------------------------------------------------------
437
- // Map container type.
47
+ // Arena
438
48
  // -----------------------------------------------------------------------------
439
49
 
440
- typedef struct {
441
- upb_fieldtype_t key_type;
442
- upb_fieldtype_t value_type;
443
- VALUE value_type_class;
444
- VALUE parse_frame;
445
- upb_strtable table;
446
- } Map;
447
-
448
- void Map_mark(void* self);
449
- void Map_free(void* self);
450
- VALUE Map_alloc(VALUE klass);
451
- VALUE Map_init(int argc, VALUE* argv, VALUE self);
452
- void Map_register(VALUE module);
453
- VALUE Map_set_frame(VALUE self, VALUE val);
454
-
455
- extern const rb_data_type_t Map_type;
456
- extern VALUE cMap;
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.
457
54
 
458
- Map* ruby_to_Map(VALUE value);
55
+ VALUE Arena_new();
56
+ upb_Arena* Arena_get(VALUE arena);
459
57
 
460
- VALUE Map_each(VALUE _self);
461
- VALUE Map_keys(VALUE _self);
462
- VALUE Map_values(VALUE _self);
463
- VALUE Map_index(VALUE _self, VALUE key);
464
- VALUE Map_index_set(VALUE _self, VALUE key, VALUE value);
465
- VALUE Map_has_key(VALUE _self, VALUE key);
466
- VALUE Map_delete(VALUE _self, VALUE key);
467
- VALUE Map_clear(VALUE _self);
468
- VALUE Map_length(VALUE _self);
469
- VALUE Map_dup(VALUE _self);
470
- VALUE Map_deep_copy(VALUE _self);
471
- VALUE Map_eq(VALUE _self, VALUE _other);
472
- VALUE Map_hash(VALUE _self);
473
- VALUE Map_to_h(VALUE _self);
474
- VALUE Map_inspect(VALUE _self);
475
- VALUE Map_merge(VALUE _self, VALUE hashmap);
476
- VALUE Map_merge_into_self(VALUE _self, VALUE hashmap);
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);
477
61
 
478
- typedef struct {
479
- Map* self;
480
- upb_strtable_iter it;
481
- } Map_iter;
482
-
483
- void Map_begin(VALUE _self, Map_iter* iter);
484
- void Map_next(Map_iter* iter);
485
- bool Map_done(Map_iter* iter);
486
- VALUE Map_iter_key(Map_iter* iter);
487
- 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);
488
68
 
489
69
  // -----------------------------------------------------------------------------
490
- // Message layout / storage.
70
+ // ObjectCache
491
71
  // -----------------------------------------------------------------------------
492
72
 
493
- #define MESSAGE_FIELD_NO_CASE ((size_t)-1)
494
- #define MESSAGE_FIELD_NO_HASBIT ((size_t)-1)
495
-
496
- struct MessageField {
497
- size_t offset;
498
- size_t case_offset; // for oneofs, a uint32. Else, MESSAGE_FIELD_NO_CASE.
499
- size_t hasbit;
500
- };
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).
501
78
 
502
- struct MessageLayout {
503
- const upb_msgdef* msgdef;
504
- MessageField* fields;
505
- size_t size;
506
- };
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);
507
83
 
508
- MessageLayout* create_layout(const upb_msgdef* msgdef);
509
- void free_layout(MessageLayout* layout);
510
- bool field_contains_hasbit(MessageLayout* layout,
511
- const upb_fielddef* field);
512
- VALUE layout_get_default(const upb_fielddef* field);
513
- VALUE layout_get(MessageLayout* layout,
514
- const void* storage,
515
- const upb_fielddef* field);
516
- void layout_set(MessageLayout* layout,
517
- void* storage,
518
- const upb_fielddef* field,
519
- VALUE val);
520
- VALUE layout_has(MessageLayout* layout,
521
- const void* storage,
522
- const upb_fielddef* field);
523
- void layout_clear(MessageLayout* layout,
524
- const void* storage,
525
- const upb_fielddef* field);
526
- void layout_init(MessageLayout* layout, void* storage);
527
- void layout_mark(MessageLayout* layout, void* storage);
528
- void layout_dup(MessageLayout* layout, void* to, void* from);
529
- void layout_deep_copy(MessageLayout* layout, void* to, void* from);
530
- VALUE layout_eq(MessageLayout* layout, void* msg1, void* msg2);
531
- VALUE layout_hash(MessageLayout* layout, void* storage);
532
- VALUE layout_inspect(MessageLayout* layout, void* storage);
84
+ // Returns the cached object for this key, if any. Otherwise returns Qnil.
85
+ VALUE ObjectCache_Get(const void* key);
533
86
 
534
87
  // -----------------------------------------------------------------------------
535
- // Message class creation.
88
+ // StringBuilder, for inspect
536
89
  // -----------------------------------------------------------------------------
537
90
 
538
- // This should probably be factored into a common upb component.
539
-
540
- typedef struct {
541
- upb_byteshandler handler;
542
- upb_bytessink sink;
543
- char *ptr;
544
- size_t len, size;
545
- } stringsink;
546
-
547
- void stringsink_uninit(stringsink *sink);
548
-
549
- struct MessageHeader {
550
- Descriptor* descriptor; // kept alive by self.class.descriptor reference.
551
- stringsink* unknown_fields; // store unknown fields in decoding.
552
- // Data comes after this.
553
- };
91
+ struct StringBuilder;
92
+ typedef struct StringBuilder StringBuilder;
554
93
 
555
- 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);
556
98
 
557
- VALUE build_class_from_descriptor(Descriptor* descriptor);
558
- void* Message_data(void* msg);
559
- void Message_mark(void* self);
560
- void Message_free(void* self);
561
- VALUE Message_alloc(VALUE klass);
562
- VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self);
563
- VALUE Message_initialize(int argc, VALUE* argv, VALUE _self);
564
- VALUE Message_dup(VALUE _self);
565
- VALUE Message_deep_copy(VALUE _self);
566
- VALUE Message_eq(VALUE _self, VALUE _other);
567
- VALUE Message_hash(VALUE _self);
568
- VALUE Message_inspect(VALUE _self);
569
- VALUE Message_to_h(VALUE _self);
570
- VALUE Message_index(VALUE _self, VALUE field_name);
571
- VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value);
572
- VALUE Message_descriptor(VALUE klass);
573
- VALUE Message_decode(VALUE klass, VALUE data);
574
- VALUE Message_encode(VALUE klass, VALUE msg_rb);
575
- VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass);
576
- VALUE Message_encode_json(int argc, VALUE* argv, VALUE klass);
577
-
578
- VALUE Google_Protobuf_discard_unknown(VALUE self, VALUE msg_rb);
579
- VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj);
580
-
581
- VALUE build_module_from_enumdesc(EnumDescriptor* enumdef);
582
- VALUE enum_lookup(VALUE self, VALUE number);
583
- VALUE enum_resolve(VALUE self, VALUE sym);
584
-
585
- const upb_pbdecodermethod *new_fillmsg_decodermethod(
586
- Descriptor* descriptor, const void *owner);
587
-
588
- // Maximum depth allowed during encoding, to avoid stack overflows due to
589
- // cycles.
590
- #define ENCODE_MAX_NESTING 63
591
-
592
- // -----------------------------------------------------------------------------
593
- // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
594
- // instances.
595
- // -----------------------------------------------------------------------------
596
- void add_def_obj(const void* def, VALUE value);
597
- VALUE get_def_obj(const void* def);
99
+ void StringBuilder_PrintMsgval(StringBuilder* b, upb_MessageValue val,
100
+ TypeInfo info);
598
101
 
599
102
  // -----------------------------------------------------------------------------
600
103
  // Utilities.
601
104
  // -----------------------------------------------------------------------------
602
105
 
603
- void check_upb_status(const upb_status* status, const char* msg);
106
+ extern VALUE cTypeError;
107
+
108
+ #ifdef NDEBUG
109
+ #define PBRUBY_ASSERT(expr) \
110
+ do { \
111
+ } while (false && (expr))
112
+ #else
113
+ #define PBRUBY_ASSERT(expr) assert(expr)
114
+ #endif
604
115
 
605
- #define CHECK_UPB(code, msg) do { \
606
- upb_status status = UPB_STATUS_INIT; \
607
- code; \
608
- check_upb_status(&status, msg); \
609
- } while (0)
116
+ #define PBRUBY_MAX(x, y) (((x) > (y)) ? (x) : (y))
610
117
 
611
- extern ID descriptor_instancevar_interned;
118
+ #define UPB_UNUSED(var) (void)var
612
119
 
613
120
  #endif // __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__