google-protobuf-z 3.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,121 @@
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
+ // -----------------------------------------------------------------------------
34
+ // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
35
+ // instances.
36
+ // -----------------------------------------------------------------------------
37
+
38
+ // This is a hash table from def objects (encoded by converting pointers to
39
+ // Ruby integers) to MessageDef/EnumDef instances (as Ruby values).
40
+ VALUE upb_def_to_ruby_obj_map;
41
+
42
+ VALUE cError;
43
+ VALUE cParseError;
44
+ VALUE cTypeError;
45
+
46
+ void add_def_obj(const void* def, VALUE value) {
47
+ rb_hash_aset(upb_def_to_ruby_obj_map, ULL2NUM((intptr_t)def), value);
48
+ }
49
+
50
+ VALUE get_def_obj(const void* def) {
51
+ return rb_hash_aref(upb_def_to_ruby_obj_map, ULL2NUM((intptr_t)def));
52
+ }
53
+
54
+ // -----------------------------------------------------------------------------
55
+ // Utilities.
56
+ // -----------------------------------------------------------------------------
57
+
58
+ // Raises a Ruby error if |status| is not OK, using its error message.
59
+ void check_upb_status(const upb_status* status, const char* msg) {
60
+ if (!upb_ok(status)) {
61
+ rb_raise(rb_eRuntimeError, "%s: %s\n", msg, upb_status_errmsg(status));
62
+ }
63
+ }
64
+
65
+ // String encodings: we look these up once, at load time, and then cache them
66
+ // here.
67
+ rb_encoding* kRubyStringUtf8Encoding;
68
+ rb_encoding* kRubyStringASCIIEncoding;
69
+ rb_encoding* kRubyString8bitEncoding;
70
+
71
+ // Ruby-interned string: "descriptor". We use this identifier to store an
72
+ // instance variable on message classes we create in order to link them back to
73
+ // their descriptors.
74
+ //
75
+ // We intern this once at module load time then use the interned identifier at
76
+ // runtime in order to avoid the cost of repeatedly interning in hot paths.
77
+ const char* kDescriptorInstanceVar = "descriptor";
78
+ ID descriptor_instancevar_interned;
79
+
80
+ // -----------------------------------------------------------------------------
81
+ // Initialization/entry point.
82
+ // -----------------------------------------------------------------------------
83
+
84
+ // This must be named "Init_protobuf_c" because the Ruby module is named
85
+ // "protobuf_c" -- the VM looks for this symbol in our .so.
86
+ void Init_protobuf_c() {
87
+ VALUE google = rb_define_module("Google");
88
+ VALUE protobuf = rb_define_module_under(google, "Protobuf");
89
+ VALUE internal = rb_define_module_under(protobuf, "Internal");
90
+
91
+ descriptor_instancevar_interned = rb_intern(kDescriptorInstanceVar);
92
+ DescriptorPool_register(protobuf);
93
+ Descriptor_register(protobuf);
94
+ FileDescriptor_register(protobuf);
95
+ FieldDescriptor_register(protobuf);
96
+ OneofDescriptor_register(protobuf);
97
+ EnumDescriptor_register(protobuf);
98
+ MessageBuilderContext_register(internal);
99
+ OneofBuilderContext_register(internal);
100
+ EnumBuilderContext_register(internal);
101
+ FileBuilderContext_register(internal);
102
+ Builder_register(internal);
103
+ RepeatedField_register(protobuf);
104
+ Map_register(protobuf);
105
+
106
+ cError = rb_const_get(protobuf, rb_intern("Error"));
107
+ cParseError = rb_const_get(protobuf, rb_intern("ParseError"));
108
+ cTypeError = rb_const_get(protobuf, rb_intern("TypeError"));
109
+
110
+ rb_define_singleton_method(protobuf, "discard_unknown",
111
+ Google_Protobuf_discard_unknown, 1);
112
+ rb_define_singleton_method(protobuf, "deep_copy",
113
+ Google_Protobuf_deep_copy, 1);
114
+
115
+ kRubyStringUtf8Encoding = rb_utf8_encoding();
116
+ kRubyStringASCIIEncoding = rb_usascii_encoding();
117
+ kRubyString8bitEncoding = rb_ascii8bit_encoding();
118
+
119
+ rb_gc_register_address(&upb_def_to_ruby_obj_map);
120
+ upb_def_to_ruby_obj_map = rb_hash_new();
121
+ }
@@ -0,0 +1,611 @@
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
+ #ifndef __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__
32
+ #define __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__
33
+
34
+ #include <ruby/ruby.h>
35
+ #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
+ void Descriptor_mark(void* _self);
200
+ void Descriptor_free(void* _self);
201
+ VALUE Descriptor_alloc(VALUE klass);
202
+ void Descriptor_register(VALUE module);
203
+ Descriptor* ruby_to_Descriptor(VALUE value);
204
+ VALUE Descriptor_initialize(VALUE _self, VALUE file_descriptor_rb);
205
+ VALUE Descriptor_name(VALUE _self);
206
+ VALUE Descriptor_name_set(VALUE _self, VALUE str);
207
+ VALUE Descriptor_each(VALUE _self);
208
+ VALUE Descriptor_lookup(VALUE _self, VALUE name);
209
+ VALUE Descriptor_add_field(VALUE _self, VALUE obj);
210
+ VALUE Descriptor_add_oneof(VALUE _self, VALUE obj);
211
+ VALUE Descriptor_each_oneof(VALUE _self);
212
+ VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name);
213
+ VALUE Descriptor_msgclass(VALUE _self);
214
+ VALUE Descriptor_file_descriptor(VALUE _self);
215
+ extern const rb_data_type_t _Descriptor_type;
216
+
217
+ void FileDescriptor_mark(void* _self);
218
+ void FileDescriptor_free(void* _self);
219
+ VALUE FileDescriptor_alloc(VALUE klass);
220
+ void FileDescriptor_register(VALUE module);
221
+ FileDescriptor* ruby_to_FileDescriptor(VALUE value);
222
+ VALUE FileDescriptor_initialize(int argc, VALUE* argv, VALUE _self);
223
+ VALUE FileDescriptor_name(VALUE _self);
224
+ VALUE FileDescriptor_syntax(VALUE _self);
225
+ VALUE FileDescriptor_syntax_set(VALUE _self, VALUE syntax);
226
+
227
+ void FieldDescriptor_mark(void* _self);
228
+ void FieldDescriptor_free(void* _self);
229
+ VALUE FieldDescriptor_alloc(VALUE klass);
230
+ void FieldDescriptor_register(VALUE module);
231
+ FieldDescriptor* ruby_to_FieldDescriptor(VALUE value);
232
+ VALUE FieldDescriptor_name(VALUE _self);
233
+ VALUE FieldDescriptor_name_set(VALUE _self, VALUE str);
234
+ VALUE FieldDescriptor_type(VALUE _self);
235
+ VALUE FieldDescriptor_type_set(VALUE _self, VALUE type);
236
+ VALUE FieldDescriptor_default(VALUE _self);
237
+ VALUE FieldDescriptor_default_set(VALUE _self, VALUE default_value);
238
+ VALUE FieldDescriptor_label(VALUE _self);
239
+ VALUE FieldDescriptor_label_set(VALUE _self, VALUE label);
240
+ VALUE FieldDescriptor_number(VALUE _self);
241
+ VALUE FieldDescriptor_number_set(VALUE _self, VALUE number);
242
+ VALUE FieldDescriptor_submsg_name(VALUE _self);
243
+ VALUE FieldDescriptor_submsg_name_set(VALUE _self, VALUE value);
244
+ VALUE FieldDescriptor_subtype(VALUE _self);
245
+ VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb);
246
+ VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb);
247
+ VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb);
248
+ VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value);
249
+ upb_fieldtype_t ruby_to_fieldtype(VALUE type);
250
+ VALUE fieldtype_to_ruby(upb_fieldtype_t type);
251
+
252
+ void OneofDescriptor_mark(void* _self);
253
+ void OneofDescriptor_free(void* _self);
254
+ VALUE OneofDescriptor_alloc(VALUE klass);
255
+ void OneofDescriptor_register(VALUE module);
256
+ OneofDescriptor* ruby_to_OneofDescriptor(VALUE value);
257
+ VALUE OneofDescriptor_name(VALUE _self);
258
+ VALUE OneofDescriptor_name_set(VALUE _self, VALUE value);
259
+ VALUE OneofDescriptor_add_field(VALUE _self, VALUE field);
260
+ VALUE OneofDescriptor_each(VALUE _self, VALUE field);
261
+
262
+ void EnumDescriptor_mark(void* _self);
263
+ void EnumDescriptor_free(void* _self);
264
+ VALUE EnumDescriptor_alloc(VALUE klass);
265
+ void EnumDescriptor_register(VALUE module);
266
+ EnumDescriptor* ruby_to_EnumDescriptor(VALUE value);
267
+ VALUE EnumDescriptor_initialize(VALUE _self, VALUE file_descriptor_rb);
268
+ VALUE EnumDescriptor_file_descriptor(VALUE _self);
269
+ VALUE EnumDescriptor_name(VALUE _self);
270
+ VALUE EnumDescriptor_name_set(VALUE _self, VALUE str);
271
+ VALUE EnumDescriptor_add_value(VALUE _self, VALUE name, VALUE number);
272
+ VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name);
273
+ VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number);
274
+ VALUE EnumDescriptor_each(VALUE _self);
275
+ VALUE EnumDescriptor_enummodule(VALUE _self);
276
+ extern const rb_data_type_t _EnumDescriptor_type;
277
+
278
+ void MessageBuilderContext_mark(void* _self);
279
+ void MessageBuilderContext_free(void* _self);
280
+ VALUE MessageBuilderContext_alloc(VALUE klass);
281
+ void MessageBuilderContext_register(VALUE module);
282
+ MessageBuilderContext* ruby_to_MessageBuilderContext(VALUE value);
283
+ VALUE MessageBuilderContext_initialize(VALUE _self,
284
+ VALUE descriptor,
285
+ VALUE builder);
286
+ VALUE MessageBuilderContext_optional(int argc, VALUE* argv, VALUE _self);
287
+ VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self);
288
+ VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self);
289
+ VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self);
290
+ VALUE MessageBuilderContext_oneof(VALUE _self, VALUE name);
291
+
292
+ void OneofBuilderContext_mark(void* _self);
293
+ void OneofBuilderContext_free(void* _self);
294
+ VALUE OneofBuilderContext_alloc(VALUE klass);
295
+ void OneofBuilderContext_register(VALUE module);
296
+ OneofBuilderContext* ruby_to_OneofBuilderContext(VALUE value);
297
+ VALUE OneofBuilderContext_initialize(VALUE _self,
298
+ VALUE descriptor,
299
+ VALUE builder);
300
+ VALUE OneofBuilderContext_optional(int argc, VALUE* argv, VALUE _self);
301
+
302
+ void EnumBuilderContext_mark(void* _self);
303
+ void EnumBuilderContext_free(void* _self);
304
+ VALUE EnumBuilderContext_alloc(VALUE klass);
305
+ void EnumBuilderContext_register(VALUE module);
306
+ EnumBuilderContext* ruby_to_EnumBuilderContext(VALUE value);
307
+ VALUE EnumBuilderContext_initialize(VALUE _self, VALUE enumdesc);
308
+ VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number);
309
+
310
+ void FileBuilderContext_mark(void* _self);
311
+ void FileBuilderContext_free(void* _self);
312
+ VALUE FileBuilderContext_alloc(VALUE klass);
313
+ void FileBuilderContext_register(VALUE module);
314
+ VALUE FileBuilderContext_initialize(VALUE _self, VALUE file_descriptor,
315
+ VALUE builder);
316
+ VALUE FileBuilderContext_add_message(VALUE _self, VALUE name);
317
+ VALUE FileBuilderContext_add_enum(VALUE _self, VALUE name);
318
+ VALUE FileBuilderContext_pending_descriptors(VALUE _self);
319
+
320
+ void Builder_mark(void* _self);
321
+ void Builder_free(void* _self);
322
+ VALUE Builder_alloc(VALUE klass);
323
+ void Builder_register(VALUE module);
324
+ Builder* ruby_to_Builder(VALUE value);
325
+ VALUE Builder_initialize(VALUE _self);
326
+ VALUE Builder_add_file(int argc, VALUE *argv, VALUE _self);
327
+ VALUE Builder_add_message(VALUE _self, VALUE name);
328
+ VALUE Builder_add_enum(VALUE _self, VALUE name);
329
+ VALUE Builder_finalize_to_pool(VALUE _self, VALUE pool_rb);
330
+
331
+ // -----------------------------------------------------------------------------
332
+ // Native slot storage abstraction.
333
+ // -----------------------------------------------------------------------------
334
+
335
+ #define NATIVE_SLOT_MAX_SIZE sizeof(uint64_t)
336
+
337
+ size_t native_slot_size(upb_fieldtype_t type);
338
+ void native_slot_set(upb_fieldtype_t type,
339
+ VALUE type_class,
340
+ void* memory,
341
+ VALUE value);
342
+ // Atomically (with respect to Ruby VM calls) either update the value and set a
343
+ // oneof case, or do neither. If |case_memory| is null, then no case value is
344
+ // set.
345
+ void native_slot_set_value_and_case(upb_fieldtype_t type,
346
+ VALUE type_class,
347
+ void* memory,
348
+ VALUE value,
349
+ uint32_t* case_memory,
350
+ uint32_t case_number);
351
+ VALUE native_slot_get(upb_fieldtype_t type,
352
+ VALUE type_class,
353
+ const void* memory);
354
+ void native_slot_init(upb_fieldtype_t type, void* memory);
355
+ void native_slot_mark(upb_fieldtype_t type, void* memory);
356
+ void native_slot_dup(upb_fieldtype_t type, void* to, void* from);
357
+ void native_slot_deep_copy(upb_fieldtype_t type, void* to, void* from);
358
+ bool native_slot_eq(upb_fieldtype_t type, void* mem1, void* mem2);
359
+
360
+ VALUE native_slot_encode_and_freeze_string(upb_fieldtype_t type, VALUE value);
361
+ void native_slot_check_int_range_precision(upb_fieldtype_t type, VALUE value);
362
+
363
+ extern rb_encoding* kRubyStringUtf8Encoding;
364
+ extern rb_encoding* kRubyStringASCIIEncoding;
365
+ extern rb_encoding* kRubyString8bitEncoding;
366
+
367
+ VALUE field_type_class(const upb_fielddef* field);
368
+
369
+ #define MAP_KEY_FIELD 1
370
+ #define MAP_VALUE_FIELD 2
371
+
372
+ // Oneof case slot value to indicate that no oneof case is set. The value `0` is
373
+ // safe because field numbers are used as case identifiers, and no field can
374
+ // have a number of 0.
375
+ #define ONEOF_CASE_NONE 0
376
+
377
+ // These operate on a map field (i.e., a repeated field of submessages whose
378
+ // submessage type is a map-entry msgdef).
379
+ bool is_map_field(const upb_fielddef* field);
380
+ const upb_fielddef* map_field_key(const upb_fielddef* field);
381
+ const upb_fielddef* map_field_value(const upb_fielddef* field);
382
+
383
+ // These operate on a map-entry msgdef.
384
+ const upb_fielddef* map_entry_key(const upb_msgdef* msgdef);
385
+ const upb_fielddef* map_entry_value(const upb_msgdef* msgdef);
386
+
387
+ // -----------------------------------------------------------------------------
388
+ // Repeated field container type.
389
+ // -----------------------------------------------------------------------------
390
+
391
+ typedef struct {
392
+ upb_fieldtype_t field_type;
393
+ VALUE field_type_class;
394
+ void* elements;
395
+ int size;
396
+ int capacity;
397
+ } RepeatedField;
398
+
399
+ void RepeatedField_mark(void* self);
400
+ void RepeatedField_free(void* self);
401
+ VALUE RepeatedField_alloc(VALUE klass);
402
+ VALUE RepeatedField_init(int argc, VALUE* argv, VALUE self);
403
+ void RepeatedField_register(VALUE module);
404
+
405
+ extern const rb_data_type_t RepeatedField_type;
406
+ extern VALUE cRepeatedField;
407
+
408
+ RepeatedField* ruby_to_RepeatedField(VALUE value);
409
+
410
+ VALUE RepeatedField_each(VALUE _self);
411
+ VALUE RepeatedField_index(int argc, VALUE* argv, VALUE _self);
412
+ void* RepeatedField_index_native(VALUE _self, int index);
413
+ int RepeatedField_size(VALUE _self);
414
+ VALUE RepeatedField_index_set(VALUE _self, VALUE _index, VALUE val);
415
+ void RepeatedField_reserve(RepeatedField* self, int new_size);
416
+ VALUE RepeatedField_push(VALUE _self, VALUE val);
417
+ void RepeatedField_push_native(VALUE _self, void* data);
418
+ VALUE RepeatedField_pop_one(VALUE _self);
419
+ VALUE RepeatedField_insert(int argc, VALUE* argv, VALUE _self);
420
+ VALUE RepeatedField_replace(VALUE _self, VALUE list);
421
+ VALUE RepeatedField_clear(VALUE _self);
422
+ VALUE RepeatedField_length(VALUE _self);
423
+ VALUE RepeatedField_dup(VALUE _self);
424
+ VALUE RepeatedField_deep_copy(VALUE _self);
425
+ VALUE RepeatedField_to_ary(VALUE _self);
426
+ VALUE RepeatedField_eq(VALUE _self, VALUE _other);
427
+ VALUE RepeatedField_hash(VALUE _self);
428
+ VALUE RepeatedField_inspect(VALUE _self);
429
+ VALUE RepeatedField_plus(VALUE _self, VALUE list);
430
+
431
+ // Defined in repeated_field.c; also used by Map.
432
+ void validate_type_class(upb_fieldtype_t type, VALUE klass);
433
+
434
+ // -----------------------------------------------------------------------------
435
+ // Map container type.
436
+ // -----------------------------------------------------------------------------
437
+
438
+ typedef struct {
439
+ upb_fieldtype_t key_type;
440
+ upb_fieldtype_t value_type;
441
+ VALUE value_type_class;
442
+ VALUE parse_frame;
443
+ upb_strtable table;
444
+ } Map;
445
+
446
+ void Map_mark(void* self);
447
+ void Map_free(void* self);
448
+ VALUE Map_alloc(VALUE klass);
449
+ VALUE Map_init(int argc, VALUE* argv, VALUE self);
450
+ void Map_register(VALUE module);
451
+ VALUE Map_set_frame(VALUE self, VALUE val);
452
+
453
+ extern const rb_data_type_t Map_type;
454
+ extern VALUE cMap;
455
+
456
+ Map* ruby_to_Map(VALUE value);
457
+
458
+ VALUE Map_each(VALUE _self);
459
+ VALUE Map_keys(VALUE _self);
460
+ VALUE Map_values(VALUE _self);
461
+ VALUE Map_index(VALUE _self, VALUE key);
462
+ VALUE Map_index_set(VALUE _self, VALUE key, VALUE value);
463
+ VALUE Map_has_key(VALUE _self, VALUE key);
464
+ VALUE Map_delete(VALUE _self, VALUE key);
465
+ VALUE Map_clear(VALUE _self);
466
+ VALUE Map_length(VALUE _self);
467
+ VALUE Map_dup(VALUE _self);
468
+ VALUE Map_deep_copy(VALUE _self);
469
+ VALUE Map_eq(VALUE _self, VALUE _other);
470
+ VALUE Map_hash(VALUE _self);
471
+ VALUE Map_to_h(VALUE _self);
472
+ VALUE Map_inspect(VALUE _self);
473
+ VALUE Map_merge(VALUE _self, VALUE hashmap);
474
+ VALUE Map_merge_into_self(VALUE _self, VALUE hashmap);
475
+
476
+ typedef struct {
477
+ Map* self;
478
+ upb_strtable_iter it;
479
+ } Map_iter;
480
+
481
+ void Map_begin(VALUE _self, Map_iter* iter);
482
+ void Map_next(Map_iter* iter);
483
+ bool Map_done(Map_iter* iter);
484
+ VALUE Map_iter_key(Map_iter* iter);
485
+ VALUE Map_iter_value(Map_iter* iter);
486
+
487
+ // -----------------------------------------------------------------------------
488
+ // Message layout / storage.
489
+ // -----------------------------------------------------------------------------
490
+
491
+ #define MESSAGE_FIELD_NO_CASE ((size_t)-1)
492
+ #define MESSAGE_FIELD_NO_HASBIT ((size_t)-1)
493
+
494
+ struct MessageField {
495
+ size_t offset;
496
+ size_t case_offset; // for oneofs, a uint32. Else, MESSAGE_FIELD_NO_CASE.
497
+ size_t hasbit;
498
+ };
499
+
500
+ struct MessageLayout {
501
+ const upb_msgdef* msgdef;
502
+ MessageField* fields;
503
+ size_t size;
504
+ };
505
+
506
+ MessageLayout* create_layout(const upb_msgdef* msgdef);
507
+ void free_layout(MessageLayout* layout);
508
+ bool field_contains_hasbit(MessageLayout* layout,
509
+ const upb_fielddef* field);
510
+ VALUE layout_get_default(const upb_fielddef* field);
511
+ VALUE layout_get(MessageLayout* layout,
512
+ const void* storage,
513
+ const upb_fielddef* field);
514
+ void layout_set(MessageLayout* layout,
515
+ void* storage,
516
+ const upb_fielddef* field,
517
+ VALUE val);
518
+ VALUE layout_has(MessageLayout* layout,
519
+ const void* storage,
520
+ const upb_fielddef* field);
521
+ void layout_clear(MessageLayout* layout,
522
+ const void* storage,
523
+ const upb_fielddef* field);
524
+ void layout_init(MessageLayout* layout, void* storage);
525
+ void layout_mark(MessageLayout* layout, void* storage);
526
+ void layout_dup(MessageLayout* layout, void* to, void* from);
527
+ void layout_deep_copy(MessageLayout* layout, void* to, void* from);
528
+ VALUE layout_eq(MessageLayout* layout, void* msg1, void* msg2);
529
+ VALUE layout_hash(MessageLayout* layout, void* storage);
530
+ VALUE layout_inspect(MessageLayout* layout, void* storage);
531
+
532
+ // -----------------------------------------------------------------------------
533
+ // Message class creation.
534
+ // -----------------------------------------------------------------------------
535
+
536
+ // This should probably be factored into a common upb component.
537
+
538
+ typedef struct {
539
+ upb_byteshandler handler;
540
+ upb_bytessink sink;
541
+ char *ptr;
542
+ size_t len, size;
543
+ } stringsink;
544
+
545
+ void stringsink_uninit(stringsink *sink);
546
+
547
+ struct MessageHeader {
548
+ Descriptor* descriptor; // kept alive by self.class.descriptor reference.
549
+ stringsink* unknown_fields; // store unknown fields in decoding.
550
+ // Data comes after this.
551
+ };
552
+
553
+ extern rb_data_type_t Message_type;
554
+
555
+ VALUE build_class_from_descriptor(Descriptor* descriptor);
556
+ void* Message_data(void* msg);
557
+ void Message_mark(void* self);
558
+ void Message_free(void* self);
559
+ VALUE Message_alloc(VALUE klass);
560
+ VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self);
561
+ VALUE Message_initialize(int argc, VALUE* argv, VALUE _self);
562
+ VALUE Message_dup(VALUE _self);
563
+ VALUE Message_deep_copy(VALUE _self);
564
+ VALUE Message_eq(VALUE _self, VALUE _other);
565
+ VALUE Message_hash(VALUE _self);
566
+ VALUE Message_inspect(VALUE _self);
567
+ VALUE Message_to_h(VALUE _self);
568
+ VALUE Message_index(VALUE _self, VALUE field_name);
569
+ VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value);
570
+ VALUE Message_descriptor(VALUE klass);
571
+ VALUE Message_decode(VALUE klass, VALUE data);
572
+ VALUE Message_encode(VALUE klass, VALUE msg_rb);
573
+ VALUE Message_decode_json(VALUE klass, VALUE data);
574
+ VALUE Message_encode_json(int argc, VALUE* argv, VALUE klass);
575
+
576
+ VALUE Google_Protobuf_discard_unknown(VALUE self, VALUE msg_rb);
577
+ VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj);
578
+
579
+ VALUE build_module_from_enumdesc(EnumDescriptor* enumdef);
580
+ VALUE enum_lookup(VALUE self, VALUE number);
581
+ VALUE enum_resolve(VALUE self, VALUE sym);
582
+
583
+ const upb_pbdecodermethod *new_fillmsg_decodermethod(
584
+ Descriptor* descriptor, const void *owner);
585
+
586
+ // Maximum depth allowed during encoding, to avoid stack overflows due to
587
+ // cycles.
588
+ #define ENCODE_MAX_NESTING 63
589
+
590
+ // -----------------------------------------------------------------------------
591
+ // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
592
+ // instances.
593
+ // -----------------------------------------------------------------------------
594
+ void add_def_obj(const void* def, VALUE value);
595
+ VALUE get_def_obj(const void* def);
596
+
597
+ // -----------------------------------------------------------------------------
598
+ // Utilities.
599
+ // -----------------------------------------------------------------------------
600
+
601
+ void check_upb_status(const upb_status* status, const char* msg);
602
+
603
+ #define CHECK_UPB(code, msg) do { \
604
+ upb_status status = UPB_STATUS_INIT; \
605
+ code; \
606
+ check_upb_status(&status, msg); \
607
+ } while (0)
608
+
609
+ extern ID descriptor_instancevar_interned;
610
+
611
+ #endif // __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__