google-protobuf 3.19.0.rc.1-x86_64-darwin

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 (43) hide show
  1. checksums.yaml +7 -0
  2. data/ext/google/protobuf_c/convert.c +348 -0
  3. data/ext/google/protobuf_c/convert.h +72 -0
  4. data/ext/google/protobuf_c/defs.c +1284 -0
  5. data/ext/google/protobuf_c/defs.h +107 -0
  6. data/ext/google/protobuf_c/extconf.rb +20 -0
  7. data/ext/google/protobuf_c/map.c +694 -0
  8. data/ext/google/protobuf_c/map.h +67 -0
  9. data/ext/google/protobuf_c/message.c +1328 -0
  10. data/ext/google/protobuf_c/message.h +101 -0
  11. data/ext/google/protobuf_c/protobuf.c +470 -0
  12. data/ext/google/protobuf_c/protobuf.h +117 -0
  13. data/ext/google/protobuf_c/repeated_field.c +659 -0
  14. data/ext/google/protobuf_c/repeated_field.h +63 -0
  15. data/ext/google/protobuf_c/ruby-upb.c +9171 -0
  16. data/ext/google/protobuf_c/ruby-upb.h +4704 -0
  17. data/ext/google/protobuf_c/wrap_memcpy.c +51 -0
  18. data/lib/google/2.3/protobuf_c.bundle +0 -0
  19. data/lib/google/2.4/protobuf_c.bundle +0 -0
  20. data/lib/google/2.5/protobuf_c.bundle +0 -0
  21. data/lib/google/2.6/protobuf_c.bundle +0 -0
  22. data/lib/google/2.7/protobuf_c.bundle +0 -0
  23. data/lib/google/3.0/protobuf_c.bundle +0 -0
  24. data/lib/google/protobuf/any_pb.rb +19 -0
  25. data/lib/google/protobuf/api_pb.rb +41 -0
  26. data/lib/google/protobuf/descriptor_dsl.rb +458 -0
  27. data/lib/google/protobuf/descriptor_pb.rb +266 -0
  28. data/lib/google/protobuf/duration_pb.rb +19 -0
  29. data/lib/google/protobuf/empty_pb.rb +17 -0
  30. data/lib/google/protobuf/field_mask_pb.rb +18 -0
  31. data/lib/google/protobuf/message_exts.rb +53 -0
  32. data/lib/google/protobuf/repeated_field.rb +188 -0
  33. data/lib/google/protobuf/source_context_pb.rb +18 -0
  34. data/lib/google/protobuf/struct_pb.rb +37 -0
  35. data/lib/google/protobuf/timestamp_pb.rb +19 -0
  36. data/lib/google/protobuf/type_pb.rb +91 -0
  37. data/lib/google/protobuf/well_known_types.rb +235 -0
  38. data/lib/google/protobuf/wrappers_pb.rb +50 -0
  39. data/lib/google/protobuf.rb +79 -0
  40. data/tests/basic.rb +640 -0
  41. data/tests/generated_code_test.rb +23 -0
  42. data/tests/stress.rb +38 -0
  43. metadata +144 -0
@@ -0,0 +1,1284 @@
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 <ctype.h>
32
+ #include <errno.h>
33
+ #include <ruby/version.h>
34
+
35
+ #include "convert.h"
36
+ #include "message.h"
37
+ #include "protobuf.h"
38
+
39
+ // -----------------------------------------------------------------------------
40
+ // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
41
+ // instances.
42
+ // -----------------------------------------------------------------------------
43
+
44
+ static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_msgdef* def);
45
+ static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_enumdef* def);
46
+ static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_fielddef* def);
47
+ static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_filedef* def);
48
+ static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_oneofdef* def);
49
+
50
+ // A distinct object that is not accessible from Ruby. We use this as a
51
+ // constructor argument to enforce that certain objects cannot be created from
52
+ // Ruby.
53
+ VALUE c_only_cookie = Qnil;
54
+
55
+ // -----------------------------------------------------------------------------
56
+ // Common utilities.
57
+ // -----------------------------------------------------------------------------
58
+
59
+ static const char* get_str(VALUE str) {
60
+ Check_Type(str, T_STRING);
61
+ return RSTRING_PTR(str);
62
+ }
63
+
64
+ static VALUE rb_str_maybe_null(const char* s) {
65
+ if (s == NULL) {
66
+ s = "";
67
+ }
68
+ return rb_str_new2(s);
69
+ }
70
+
71
+ // -----------------------------------------------------------------------------
72
+ // DescriptorPool.
73
+ // -----------------------------------------------------------------------------
74
+
75
+ typedef struct {
76
+ VALUE def_to_descriptor; // Hash table of def* -> Ruby descriptor.
77
+ upb_symtab* symtab;
78
+ } DescriptorPool;
79
+
80
+ VALUE cDescriptorPool = Qnil;
81
+
82
+ // Global singleton DescriptorPool. The user is free to create others, but this
83
+ // is used by generated code.
84
+ VALUE generated_pool = Qnil;
85
+
86
+ static void DescriptorPool_mark(void* _self) {
87
+ DescriptorPool* self = _self;
88
+ rb_gc_mark(self->def_to_descriptor);
89
+ }
90
+
91
+ static void DescriptorPool_free(void* _self) {
92
+ DescriptorPool* self = _self;
93
+ upb_symtab_free(self->symtab);
94
+ xfree(self);
95
+ }
96
+
97
+ static const rb_data_type_t DescriptorPool_type = {
98
+ "Google::Protobuf::DescriptorPool",
99
+ {DescriptorPool_mark, DescriptorPool_free, NULL},
100
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
101
+ };
102
+
103
+ static DescriptorPool* ruby_to_DescriptorPool(VALUE val) {
104
+ DescriptorPool* ret;
105
+ TypedData_Get_Struct(val, DescriptorPool, &DescriptorPool_type, ret);
106
+ return ret;
107
+ }
108
+
109
+ // Exposed to other modules in defs.h.
110
+ const upb_symtab *DescriptorPool_GetSymtab(VALUE desc_pool_rb) {
111
+ DescriptorPool *pool = ruby_to_DescriptorPool(desc_pool_rb);
112
+ return pool->symtab;
113
+ }
114
+
115
+ /*
116
+ * call-seq:
117
+ * DescriptorPool.new => pool
118
+ *
119
+ * Creates a new, empty, descriptor pool.
120
+ */
121
+ static VALUE DescriptorPool_alloc(VALUE klass) {
122
+ DescriptorPool* self = ALLOC(DescriptorPool);
123
+ VALUE ret;
124
+
125
+ self->def_to_descriptor = Qnil;
126
+ ret = TypedData_Wrap_Struct(klass, &DescriptorPool_type, self);
127
+
128
+ self->def_to_descriptor = rb_hash_new();
129
+ self->symtab = upb_symtab_new();
130
+ ObjectCache_Add(self->symtab, ret);
131
+
132
+ return ret;
133
+ }
134
+
135
+ /*
136
+ * call-seq:
137
+ * DescriptorPool.add_serialized_file(serialized_file_proto)
138
+ *
139
+ * Adds the given serialized FileDescriptorProto to the pool.
140
+ */
141
+ VALUE DescriptorPool_add_serialized_file(VALUE _self,
142
+ VALUE serialized_file_proto) {
143
+ DescriptorPool* self = ruby_to_DescriptorPool(_self);
144
+ Check_Type(serialized_file_proto, T_STRING);
145
+ VALUE arena_rb = Arena_new();
146
+ upb_arena *arena = Arena_get(arena_rb);
147
+ google_protobuf_FileDescriptorProto* file_proto =
148
+ google_protobuf_FileDescriptorProto_parse(
149
+ RSTRING_PTR(serialized_file_proto),
150
+ RSTRING_LEN(serialized_file_proto), arena);
151
+ if (!file_proto) {
152
+ rb_raise(rb_eArgError, "Unable to parse FileDescriptorProto");
153
+ }
154
+ upb_status status;
155
+ upb_status_clear(&status);
156
+ const upb_filedef* filedef =
157
+ upb_symtab_addfile(self->symtab, file_proto, &status);
158
+ if (!filedef) {
159
+ rb_raise(cTypeError, "Unable to build file to DescriptorPool: %s",
160
+ upb_status_errmsg(&status));
161
+ }
162
+ return get_filedef_obj(_self, filedef);
163
+ }
164
+
165
+ /*
166
+ * call-seq:
167
+ * DescriptorPool.lookup(name) => descriptor
168
+ *
169
+ * Finds a Descriptor or EnumDescriptor by name and returns it, or nil if none
170
+ * exists with the given name.
171
+ */
172
+ static VALUE DescriptorPool_lookup(VALUE _self, VALUE name) {
173
+ DescriptorPool* self = ruby_to_DescriptorPool(_self);
174
+ const char* name_str = get_str(name);
175
+ const upb_msgdef* msgdef;
176
+ const upb_enumdef* enumdef;
177
+
178
+ msgdef = upb_symtab_lookupmsg(self->symtab, name_str);
179
+ if (msgdef) {
180
+ return get_msgdef_obj(_self, msgdef);
181
+ }
182
+
183
+ enumdef = upb_symtab_lookupenum(self->symtab, name_str);
184
+ if (enumdef) {
185
+ return get_enumdef_obj(_self, enumdef);
186
+ }
187
+
188
+ return Qnil;
189
+ }
190
+
191
+ /*
192
+ * call-seq:
193
+ * DescriptorPool.generated_pool => descriptor_pool
194
+ *
195
+ * Class method that returns the global DescriptorPool. This is a singleton into
196
+ * which generated-code message and enum types are registered. The user may also
197
+ * register types in this pool for convenience so that they do not have to hold
198
+ * a reference to a private pool instance.
199
+ */
200
+ static VALUE DescriptorPool_generated_pool(VALUE _self) {
201
+ return generated_pool;
202
+ }
203
+
204
+ static void DescriptorPool_register(VALUE module) {
205
+ VALUE klass = rb_define_class_under(
206
+ module, "DescriptorPool", rb_cObject);
207
+ rb_define_alloc_func(klass, DescriptorPool_alloc);
208
+ rb_define_method(klass, "add_serialized_file",
209
+ DescriptorPool_add_serialized_file, 1);
210
+ rb_define_method(klass, "lookup", DescriptorPool_lookup, 1);
211
+ rb_define_singleton_method(klass, "generated_pool",
212
+ DescriptorPool_generated_pool, 0);
213
+ rb_gc_register_address(&cDescriptorPool);
214
+ cDescriptorPool = klass;
215
+
216
+ rb_gc_register_address(&generated_pool);
217
+ generated_pool = rb_class_new_instance(0, NULL, klass);
218
+ }
219
+
220
+ // -----------------------------------------------------------------------------
221
+ // Descriptor.
222
+ // -----------------------------------------------------------------------------
223
+
224
+ typedef struct {
225
+ const upb_msgdef* msgdef;
226
+ VALUE klass;
227
+ VALUE descriptor_pool;
228
+ } Descriptor;
229
+
230
+ VALUE cDescriptor = Qnil;
231
+
232
+ static void Descriptor_mark(void* _self) {
233
+ Descriptor* self = _self;
234
+ rb_gc_mark(self->klass);
235
+ rb_gc_mark(self->descriptor_pool);
236
+ }
237
+
238
+ static const rb_data_type_t Descriptor_type = {
239
+ "Google::Protobuf::Descriptor",
240
+ {Descriptor_mark, RUBY_DEFAULT_FREE, NULL},
241
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
242
+ };
243
+
244
+ static Descriptor* ruby_to_Descriptor(VALUE val) {
245
+ Descriptor* ret;
246
+ TypedData_Get_Struct(val, Descriptor, &Descriptor_type, ret);
247
+ return ret;
248
+ }
249
+
250
+ /*
251
+ * call-seq:
252
+ * Descriptor.new => descriptor
253
+ *
254
+ * Creates a new, empty, message type descriptor. At a minimum, its name must be
255
+ * set before it is added to a pool. It cannot be used to create messages until
256
+ * it is added to a pool, after which it becomes immutable (as part of a
257
+ * finalization process).
258
+ */
259
+ static VALUE Descriptor_alloc(VALUE klass) {
260
+ Descriptor* self = ALLOC(Descriptor);
261
+ VALUE ret = TypedData_Wrap_Struct(klass, &Descriptor_type, self);
262
+ self->msgdef = NULL;
263
+ self->klass = Qnil;
264
+ self->descriptor_pool = Qnil;
265
+ return ret;
266
+ }
267
+
268
+ /*
269
+ * call-seq:
270
+ * Descriptor.new(c_only_cookie, ptr) => Descriptor
271
+ *
272
+ * Creates a descriptor wrapper object. May only be called from C.
273
+ */
274
+ static VALUE Descriptor_initialize(VALUE _self, VALUE cookie,
275
+ VALUE descriptor_pool, VALUE ptr) {
276
+ Descriptor* self = ruby_to_Descriptor(_self);
277
+
278
+ if (cookie != c_only_cookie) {
279
+ rb_raise(rb_eRuntimeError,
280
+ "Descriptor objects may not be created from Ruby.");
281
+ }
282
+
283
+ self->descriptor_pool = descriptor_pool;
284
+ self->msgdef = (const upb_msgdef*)NUM2ULL(ptr);
285
+
286
+ return Qnil;
287
+ }
288
+
289
+ /*
290
+ * call-seq:
291
+ * Descriptor.file_descriptor
292
+ *
293
+ * Returns the FileDescriptor object this message belongs to.
294
+ */
295
+ static VALUE Descriptor_file_descriptor(VALUE _self) {
296
+ Descriptor* self = ruby_to_Descriptor(_self);
297
+ return get_filedef_obj(self->descriptor_pool, upb_msgdef_file(self->msgdef));
298
+ }
299
+
300
+ /*
301
+ * call-seq:
302
+ * Descriptor.name => name
303
+ *
304
+ * Returns the name of this message type as a fully-qualified string (e.g.,
305
+ * My.Package.MessageType).
306
+ */
307
+ static VALUE Descriptor_name(VALUE _self) {
308
+ Descriptor* self = ruby_to_Descriptor(_self);
309
+ return rb_str_maybe_null(upb_msgdef_fullname(self->msgdef));
310
+ }
311
+
312
+ /*
313
+ * call-seq:
314
+ * Descriptor.each(&block)
315
+ *
316
+ * Iterates over fields in this message type, yielding to the block on each one.
317
+ */
318
+ static VALUE Descriptor_each(VALUE _self) {
319
+ Descriptor* self = ruby_to_Descriptor(_self);
320
+
321
+ upb_msg_field_iter it;
322
+ for (upb_msg_field_begin(&it, self->msgdef);
323
+ !upb_msg_field_done(&it);
324
+ upb_msg_field_next(&it)) {
325
+ const upb_fielddef* field = upb_msg_iter_field(&it);
326
+ VALUE obj = get_fielddef_obj(self->descriptor_pool, field);
327
+ rb_yield(obj);
328
+ }
329
+ return Qnil;
330
+ }
331
+
332
+ /*
333
+ * call-seq:
334
+ * Descriptor.lookup(name) => FieldDescriptor
335
+ *
336
+ * Returns the field descriptor for the field with the given name, if present,
337
+ * or nil if none.
338
+ */
339
+ static VALUE Descriptor_lookup(VALUE _self, VALUE name) {
340
+ Descriptor* self = ruby_to_Descriptor(_self);
341
+ const char* s = get_str(name);
342
+ const upb_fielddef* field = upb_msgdef_ntofz(self->msgdef, s);
343
+ if (field == NULL) {
344
+ return Qnil;
345
+ }
346
+ return get_fielddef_obj(self->descriptor_pool, field);
347
+ }
348
+
349
+ /*
350
+ * call-seq:
351
+ * Descriptor.each_oneof(&block) => nil
352
+ *
353
+ * Invokes the given block for each oneof in this message type, passing the
354
+ * corresponding OneofDescriptor.
355
+ */
356
+ static VALUE Descriptor_each_oneof(VALUE _self) {
357
+ Descriptor* self = ruby_to_Descriptor(_self);
358
+
359
+ upb_msg_oneof_iter it;
360
+ for (upb_msg_oneof_begin(&it, self->msgdef);
361
+ !upb_msg_oneof_done(&it);
362
+ upb_msg_oneof_next(&it)) {
363
+ const upb_oneofdef* oneof = upb_msg_iter_oneof(&it);
364
+ VALUE obj = get_oneofdef_obj(self->descriptor_pool, oneof);
365
+ rb_yield(obj);
366
+ }
367
+ return Qnil;
368
+ }
369
+
370
+ /*
371
+ * call-seq:
372
+ * Descriptor.lookup_oneof(name) => OneofDescriptor
373
+ *
374
+ * Returns the oneof descriptor for the oneof with the given name, if present,
375
+ * or nil if none.
376
+ */
377
+ static VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
378
+ Descriptor* self = ruby_to_Descriptor(_self);
379
+ const char* s = get_str(name);
380
+ const upb_oneofdef* oneof = upb_msgdef_ntooz(self->msgdef, s);
381
+ if (oneof == NULL) {
382
+ return Qnil;
383
+ }
384
+ return get_oneofdef_obj(self->descriptor_pool, oneof);
385
+ }
386
+
387
+ /*
388
+ * call-seq:
389
+ * Descriptor.msgclass => message_klass
390
+ *
391
+ * Returns the Ruby class created for this message type.
392
+ */
393
+ static VALUE Descriptor_msgclass(VALUE _self) {
394
+ Descriptor* self = ruby_to_Descriptor(_self);
395
+ if (self->klass == Qnil) {
396
+ self->klass = build_class_from_descriptor(_self);
397
+ }
398
+ return self->klass;
399
+ }
400
+
401
+ static void Descriptor_register(VALUE module) {
402
+ VALUE klass = rb_define_class_under(
403
+ module, "Descriptor", rb_cObject);
404
+ rb_define_alloc_func(klass, Descriptor_alloc);
405
+ rb_define_method(klass, "initialize", Descriptor_initialize, 3);
406
+ rb_define_method(klass, "each", Descriptor_each, 0);
407
+ rb_define_method(klass, "lookup", Descriptor_lookup, 1);
408
+ rb_define_method(klass, "each_oneof", Descriptor_each_oneof, 0);
409
+ rb_define_method(klass, "lookup_oneof", Descriptor_lookup_oneof, 1);
410
+ rb_define_method(klass, "msgclass", Descriptor_msgclass, 0);
411
+ rb_define_method(klass, "name", Descriptor_name, 0);
412
+ rb_define_method(klass, "file_descriptor", Descriptor_file_descriptor, 0);
413
+ rb_include_module(klass, rb_mEnumerable);
414
+ rb_gc_register_address(&cDescriptor);
415
+ cDescriptor = klass;
416
+ }
417
+
418
+ // -----------------------------------------------------------------------------
419
+ // FileDescriptor.
420
+ // -----------------------------------------------------------------------------
421
+
422
+ typedef struct {
423
+ const upb_filedef* filedef;
424
+ VALUE descriptor_pool; // Owns the upb_filedef.
425
+ } FileDescriptor;
426
+
427
+ static VALUE cFileDescriptor = Qnil;
428
+
429
+ static void FileDescriptor_mark(void* _self) {
430
+ FileDescriptor* self = _self;
431
+ rb_gc_mark(self->descriptor_pool);
432
+ }
433
+
434
+ static const rb_data_type_t FileDescriptor_type = {
435
+ "Google::Protobuf::FileDescriptor",
436
+ {FileDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
437
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
438
+ };
439
+
440
+ static FileDescriptor* ruby_to_FileDescriptor(VALUE val) {
441
+ FileDescriptor* ret;
442
+ TypedData_Get_Struct(val, FileDescriptor, &FileDescriptor_type, ret);
443
+ return ret;
444
+ }
445
+
446
+ static VALUE FileDescriptor_alloc(VALUE klass) {
447
+ FileDescriptor* self = ALLOC(FileDescriptor);
448
+ VALUE ret = TypedData_Wrap_Struct(klass, &FileDescriptor_type, self);
449
+ self->descriptor_pool = Qnil;
450
+ self->filedef = NULL;
451
+ return ret;
452
+ }
453
+
454
+ /*
455
+ * call-seq:
456
+ * FileDescriptor.new => file
457
+ *
458
+ * Returns a new file descriptor. The syntax must be set before it's passed
459
+ * to a builder.
460
+ */
461
+ static VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
462
+ VALUE descriptor_pool, VALUE ptr) {
463
+ FileDescriptor* self = ruby_to_FileDescriptor(_self);
464
+
465
+ if (cookie != c_only_cookie) {
466
+ rb_raise(rb_eRuntimeError,
467
+ "Descriptor objects may not be created from Ruby.");
468
+ }
469
+
470
+ self->descriptor_pool = descriptor_pool;
471
+ self->filedef = (const upb_filedef*)NUM2ULL(ptr);
472
+
473
+ return Qnil;
474
+ }
475
+
476
+ /*
477
+ * call-seq:
478
+ * FileDescriptor.name => name
479
+ *
480
+ * Returns the name of the file.
481
+ */
482
+ static VALUE FileDescriptor_name(VALUE _self) {
483
+ FileDescriptor* self = ruby_to_FileDescriptor(_self);
484
+ const char* name = upb_filedef_name(self->filedef);
485
+ return name == NULL ? Qnil : rb_str_new2(name);
486
+ }
487
+
488
+ /*
489
+ * call-seq:
490
+ * FileDescriptor.syntax => syntax
491
+ *
492
+ * Returns this file descriptors syntax.
493
+ *
494
+ * Valid syntax versions are:
495
+ * :proto2 or :proto3.
496
+ */
497
+ static VALUE FileDescriptor_syntax(VALUE _self) {
498
+ FileDescriptor* self = ruby_to_FileDescriptor(_self);
499
+
500
+ switch (upb_filedef_syntax(self->filedef)) {
501
+ case UPB_SYNTAX_PROTO3: return ID2SYM(rb_intern("proto3"));
502
+ case UPB_SYNTAX_PROTO2: return ID2SYM(rb_intern("proto2"));
503
+ default: return Qnil;
504
+ }
505
+ }
506
+
507
+ static void FileDescriptor_register(VALUE module) {
508
+ VALUE klass = rb_define_class_under(
509
+ module, "FileDescriptor", rb_cObject);
510
+ rb_define_alloc_func(klass, FileDescriptor_alloc);
511
+ rb_define_method(klass, "initialize", FileDescriptor_initialize, 3);
512
+ rb_define_method(klass, "name", FileDescriptor_name, 0);
513
+ rb_define_method(klass, "syntax", FileDescriptor_syntax, 0);
514
+ rb_gc_register_address(&cFileDescriptor);
515
+ cFileDescriptor = klass;
516
+ }
517
+
518
+ // -----------------------------------------------------------------------------
519
+ // FieldDescriptor.
520
+ // -----------------------------------------------------------------------------
521
+
522
+ typedef struct {
523
+ const upb_fielddef* fielddef;
524
+ VALUE descriptor_pool; // Owns the upb_fielddef.
525
+ } FieldDescriptor;
526
+
527
+ static VALUE cFieldDescriptor = Qnil;
528
+
529
+ static void FieldDescriptor_mark(void* _self) {
530
+ FieldDescriptor* self = _self;
531
+ rb_gc_mark(self->descriptor_pool);
532
+ }
533
+
534
+ static const rb_data_type_t FieldDescriptor_type = {
535
+ "Google::Protobuf::FieldDescriptor",
536
+ {FieldDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
537
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
538
+ };
539
+
540
+ static FieldDescriptor* ruby_to_FieldDescriptor(VALUE val) {
541
+ FieldDescriptor* ret;
542
+ TypedData_Get_Struct(val, FieldDescriptor, &FieldDescriptor_type, ret);
543
+ return ret;
544
+ }
545
+
546
+ /*
547
+ * call-seq:
548
+ * FieldDescriptor.new => field
549
+ *
550
+ * Returns a new field descriptor. Its name, type, etc. must be set before it is
551
+ * added to a message type.
552
+ */
553
+ static VALUE FieldDescriptor_alloc(VALUE klass) {
554
+ FieldDescriptor* self = ALLOC(FieldDescriptor);
555
+ VALUE ret = TypedData_Wrap_Struct(klass, &FieldDescriptor_type, self);
556
+ self->fielddef = NULL;
557
+ return ret;
558
+ }
559
+
560
+ /*
561
+ * call-seq:
562
+ * EnumDescriptor.new(c_only_cookie, pool, ptr) => EnumDescriptor
563
+ *
564
+ * Creates a descriptor wrapper object. May only be called from C.
565
+ */
566
+ static VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
567
+ VALUE descriptor_pool, VALUE ptr) {
568
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
569
+
570
+ if (cookie != c_only_cookie) {
571
+ rb_raise(rb_eRuntimeError,
572
+ "Descriptor objects may not be created from Ruby.");
573
+ }
574
+
575
+ self->descriptor_pool = descriptor_pool;
576
+ self->fielddef = (const upb_fielddef*)NUM2ULL(ptr);
577
+
578
+ return Qnil;
579
+ }
580
+
581
+ /*
582
+ * call-seq:
583
+ * FieldDescriptor.name => name
584
+ *
585
+ * Returns the name of this field.
586
+ */
587
+ static VALUE FieldDescriptor_name(VALUE _self) {
588
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
589
+ return rb_str_maybe_null(upb_fielddef_name(self->fielddef));
590
+ }
591
+
592
+ // Non-static, exposed to other .c files.
593
+ upb_fieldtype_t ruby_to_fieldtype(VALUE type) {
594
+ if (TYPE(type) != T_SYMBOL) {
595
+ rb_raise(rb_eArgError, "Expected symbol for field type.");
596
+ }
597
+
598
+ #define CONVERT(upb, ruby) \
599
+ if (SYM2ID(type) == rb_intern( # ruby )) { \
600
+ return UPB_TYPE_ ## upb; \
601
+ }
602
+
603
+ CONVERT(FLOAT, float);
604
+ CONVERT(DOUBLE, double);
605
+ CONVERT(BOOL, bool);
606
+ CONVERT(STRING, string);
607
+ CONVERT(BYTES, bytes);
608
+ CONVERT(MESSAGE, message);
609
+ CONVERT(ENUM, enum);
610
+ CONVERT(INT32, int32);
611
+ CONVERT(INT64, int64);
612
+ CONVERT(UINT32, uint32);
613
+ CONVERT(UINT64, uint64);
614
+
615
+ #undef CONVERT
616
+
617
+ rb_raise(rb_eArgError, "Unknown field type.");
618
+ return 0;
619
+ }
620
+
621
+ static VALUE descriptortype_to_ruby(upb_descriptortype_t type) {
622
+ switch (type) {
623
+ #define CONVERT(upb, ruby) \
624
+ case UPB_DESCRIPTOR_TYPE_ ## upb : return ID2SYM(rb_intern( # ruby ));
625
+ CONVERT(FLOAT, float);
626
+ CONVERT(DOUBLE, double);
627
+ CONVERT(BOOL, bool);
628
+ CONVERT(STRING, string);
629
+ CONVERT(BYTES, bytes);
630
+ CONVERT(MESSAGE, message);
631
+ CONVERT(GROUP, group);
632
+ CONVERT(ENUM, enum);
633
+ CONVERT(INT32, int32);
634
+ CONVERT(INT64, int64);
635
+ CONVERT(UINT32, uint32);
636
+ CONVERT(UINT64, uint64);
637
+ CONVERT(SINT32, sint32);
638
+ CONVERT(SINT64, sint64);
639
+ CONVERT(FIXED32, fixed32);
640
+ CONVERT(FIXED64, fixed64);
641
+ CONVERT(SFIXED32, sfixed32);
642
+ CONVERT(SFIXED64, sfixed64);
643
+ #undef CONVERT
644
+ }
645
+ return Qnil;
646
+ }
647
+
648
+ /*
649
+ * call-seq:
650
+ * FieldDescriptor.type => type
651
+ *
652
+ * Returns this field's type, as a Ruby symbol, or nil if not yet set.
653
+ *
654
+ * Valid field types are:
655
+ * :int32, :int64, :uint32, :uint64, :float, :double, :bool, :string,
656
+ * :bytes, :message.
657
+ */
658
+ static VALUE FieldDescriptor__type(VALUE _self) {
659
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
660
+ return descriptortype_to_ruby(upb_fielddef_descriptortype(self->fielddef));
661
+ }
662
+
663
+ /*
664
+ * call-seq:
665
+ * FieldDescriptor.default => default
666
+ *
667
+ * Returns this field's default, as a Ruby object, or nil if not yet set.
668
+ */
669
+ static VALUE FieldDescriptor_default(VALUE _self) {
670
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
671
+ const upb_fielddef *f = self->fielddef;
672
+ upb_msgval default_val = {0};
673
+ if (upb_fielddef_issubmsg(f)) {
674
+ return Qnil;
675
+ } else if (!upb_fielddef_isseq(f)) {
676
+ default_val = upb_fielddef_default(f);
677
+ }
678
+ return Convert_UpbToRuby(default_val, TypeInfo_get(self->fielddef), Qnil);
679
+ }
680
+
681
+
682
+ /*
683
+ * call-seq:
684
+ * FieldDescriptor.json_name => json_name
685
+ *
686
+ * Returns this field's json_name, as a Ruby string, or nil if not yet set.
687
+ */
688
+ static VALUE FieldDescriptor_json_name(VALUE _self) {
689
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
690
+ const upb_fielddef *f = self->fielddef;
691
+ const char *json_name = upb_fielddef_jsonname(f);
692
+ return rb_str_new2(json_name);
693
+ }
694
+
695
+ /*
696
+ * call-seq:
697
+ * FieldDescriptor.label => label
698
+ *
699
+ * Returns this field's label (i.e., plurality), as a Ruby symbol.
700
+ *
701
+ * Valid field labels are:
702
+ * :optional, :repeated
703
+ */
704
+ static VALUE FieldDescriptor_label(VALUE _self) {
705
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
706
+ switch (upb_fielddef_label(self->fielddef)) {
707
+ #define CONVERT(upb, ruby) \
708
+ case UPB_LABEL_ ## upb : return ID2SYM(rb_intern( # ruby ));
709
+
710
+ CONVERT(OPTIONAL, optional);
711
+ CONVERT(REQUIRED, required);
712
+ CONVERT(REPEATED, repeated);
713
+
714
+ #undef CONVERT
715
+ }
716
+
717
+ return Qnil;
718
+ }
719
+
720
+ /*
721
+ * call-seq:
722
+ * FieldDescriptor.number => number
723
+ *
724
+ * Returns the tag number for this field.
725
+ */
726
+ static VALUE FieldDescriptor_number(VALUE _self) {
727
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
728
+ return INT2NUM(upb_fielddef_number(self->fielddef));
729
+ }
730
+
731
+ /*
732
+ * call-seq:
733
+ * FieldDescriptor.submsg_name => submsg_name
734
+ *
735
+ * Returns the name of the message or enum type corresponding to this field, if
736
+ * it is a message or enum field (respectively), or nil otherwise. This type
737
+ * name will be resolved within the context of the pool to which the containing
738
+ * message type is added.
739
+ */
740
+ static VALUE FieldDescriptor_submsg_name(VALUE _self) {
741
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
742
+ switch (upb_fielddef_type(self->fielddef)) {
743
+ case UPB_TYPE_ENUM:
744
+ return rb_str_new2(
745
+ upb_enumdef_fullname(upb_fielddef_enumsubdef(self->fielddef)));
746
+ case UPB_TYPE_MESSAGE:
747
+ return rb_str_new2(
748
+ upb_msgdef_fullname(upb_fielddef_msgsubdef(self->fielddef)));
749
+ default:
750
+ return Qnil;
751
+ }
752
+ }
753
+
754
+ /*
755
+ * call-seq:
756
+ * FieldDescriptor.subtype => message_or_enum_descriptor
757
+ *
758
+ * Returns the message or enum descriptor corresponding to this field's type if
759
+ * it is a message or enum field, respectively, or nil otherwise. Cannot be
760
+ * called *until* the containing message type is added to a pool (and thus
761
+ * resolved).
762
+ */
763
+ static VALUE FieldDescriptor_subtype(VALUE _self) {
764
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
765
+ switch (upb_fielddef_type(self->fielddef)) {
766
+ case UPB_TYPE_ENUM:
767
+ return get_enumdef_obj(self->descriptor_pool,
768
+ upb_fielddef_enumsubdef(self->fielddef));
769
+ case UPB_TYPE_MESSAGE:
770
+ return get_msgdef_obj(self->descriptor_pool,
771
+ upb_fielddef_msgsubdef(self->fielddef));
772
+ default:
773
+ return Qnil;
774
+ }
775
+ }
776
+
777
+ /*
778
+ * call-seq:
779
+ * FieldDescriptor.get(message) => value
780
+ *
781
+ * Returns the value set for this field on the given message. Raises an
782
+ * exception if message is of the wrong type.
783
+ */
784
+ static VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
785
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
786
+ const upb_msgdef *m;
787
+
788
+ Message_Get(msg_rb, &m);
789
+
790
+ if (m != upb_fielddef_containingtype(self->fielddef)) {
791
+ rb_raise(cTypeError, "get method called on wrong message type");
792
+ }
793
+
794
+ return Message_getfield(msg_rb, self->fielddef);
795
+ }
796
+
797
+ /*
798
+ * call-seq:
799
+ * FieldDescriptor.has?(message) => boolean
800
+ *
801
+ * Returns whether the value is set on the given message. Raises an
802
+ * exception when calling for fields that do not have presence.
803
+ */
804
+ static VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
805
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
806
+ const upb_msgdef *m;
807
+ const upb_msgdef *msg = Message_Get(msg_rb, &m);
808
+
809
+ if (m != upb_fielddef_containingtype(self->fielddef)) {
810
+ rb_raise(cTypeError, "has method called on wrong message type");
811
+ } else if (!upb_fielddef_haspresence(self->fielddef)) {
812
+ rb_raise(rb_eArgError, "does not track presence");
813
+ }
814
+
815
+ return upb_msg_has(msg, self->fielddef) ? Qtrue : Qfalse;
816
+ }
817
+
818
+ /*
819
+ * call-seq:
820
+ * FieldDescriptor.clear(message)
821
+ *
822
+ * Clears the field from the message if it's set.
823
+ */
824
+ static VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {
825
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
826
+ const upb_msgdef *m;
827
+ upb_msgdef *msg = Message_GetMutable(msg_rb, &m);
828
+
829
+ if (m != upb_fielddef_containingtype(self->fielddef)) {
830
+ rb_raise(cTypeError, "has method called on wrong message type");
831
+ }
832
+
833
+ upb_msg_clearfield(msg, self->fielddef);
834
+ return Qnil;
835
+ }
836
+
837
+ /*
838
+ * call-seq:
839
+ * FieldDescriptor.set(message, value)
840
+ *
841
+ * Sets the value corresponding to this field to the given value on the given
842
+ * message. Raises an exception if message is of the wrong type. Performs the
843
+ * ordinary type-checks for field setting.
844
+ */
845
+ static VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value) {
846
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
847
+ const upb_msgdef *m;
848
+ upb_msgdef *msg = Message_GetMutable(msg_rb, &m);
849
+ upb_arena *arena = Arena_get(Message_GetArena(msg_rb));
850
+ upb_msgval msgval;
851
+
852
+ if (m != upb_fielddef_containingtype(self->fielddef)) {
853
+ rb_raise(cTypeError, "set method called on wrong message type");
854
+ }
855
+
856
+ msgval = Convert_RubyToUpb(value, upb_fielddef_name(self->fielddef),
857
+ TypeInfo_get(self->fielddef), arena);
858
+ upb_msg_set(msg, self->fielddef, msgval, arena);
859
+ return Qnil;
860
+ }
861
+
862
+ static void FieldDescriptor_register(VALUE module) {
863
+ VALUE klass = rb_define_class_under(
864
+ module, "FieldDescriptor", rb_cObject);
865
+ rb_define_alloc_func(klass, FieldDescriptor_alloc);
866
+ rb_define_method(klass, "initialize", FieldDescriptor_initialize, 3);
867
+ rb_define_method(klass, "name", FieldDescriptor_name, 0);
868
+ rb_define_method(klass, "type", FieldDescriptor__type, 0);
869
+ rb_define_method(klass, "default", FieldDescriptor_default, 0);
870
+ rb_define_method(klass, "json_name", FieldDescriptor_json_name, 0);
871
+ rb_define_method(klass, "label", FieldDescriptor_label, 0);
872
+ rb_define_method(klass, "number", FieldDescriptor_number, 0);
873
+ rb_define_method(klass, "submsg_name", FieldDescriptor_submsg_name, 0);
874
+ rb_define_method(klass, "subtype", FieldDescriptor_subtype, 0);
875
+ rb_define_method(klass, "has?", FieldDescriptor_has, 1);
876
+ rb_define_method(klass, "clear", FieldDescriptor_clear, 1);
877
+ rb_define_method(klass, "get", FieldDescriptor_get, 1);
878
+ rb_define_method(klass, "set", FieldDescriptor_set, 2);
879
+ rb_gc_register_address(&cFieldDescriptor);
880
+ cFieldDescriptor = klass;
881
+ }
882
+
883
+ // -----------------------------------------------------------------------------
884
+ // OneofDescriptor.
885
+ // -----------------------------------------------------------------------------
886
+
887
+ typedef struct {
888
+ const upb_oneofdef* oneofdef;
889
+ VALUE descriptor_pool; // Owns the upb_oneofdef.
890
+ } OneofDescriptor;
891
+
892
+ static VALUE cOneofDescriptor = Qnil;
893
+
894
+ static void OneofDescriptor_mark(void* _self) {
895
+ OneofDescriptor* self = _self;
896
+ rb_gc_mark(self->descriptor_pool);
897
+ }
898
+
899
+ static const rb_data_type_t OneofDescriptor_type = {
900
+ "Google::Protobuf::OneofDescriptor",
901
+ {OneofDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
902
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
903
+ };
904
+
905
+ static OneofDescriptor* ruby_to_OneofDescriptor(VALUE val) {
906
+ OneofDescriptor* ret;
907
+ TypedData_Get_Struct(val, OneofDescriptor, &OneofDescriptor_type, ret);
908
+ return ret;
909
+ }
910
+
911
+ /*
912
+ * call-seq:
913
+ * OneofDescriptor.new => oneof_descriptor
914
+ *
915
+ * Creates a new, empty, oneof descriptor. The oneof may only be modified prior
916
+ * to being added to a message descriptor which is subsequently added to a pool.
917
+ */
918
+ static VALUE OneofDescriptor_alloc(VALUE klass) {
919
+ OneofDescriptor* self = ALLOC(OneofDescriptor);
920
+ VALUE ret = TypedData_Wrap_Struct(klass, &OneofDescriptor_type, self);
921
+ self->oneofdef = NULL;
922
+ self->descriptor_pool = Qnil;
923
+ return ret;
924
+ }
925
+
926
+ /*
927
+ * call-seq:
928
+ * OneofDescriptor.new(c_only_cookie, pool, ptr) => OneofDescriptor
929
+ *
930
+ * Creates a descriptor wrapper object. May only be called from C.
931
+ */
932
+ static VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
933
+ VALUE descriptor_pool, VALUE ptr) {
934
+ OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
935
+
936
+ if (cookie != c_only_cookie) {
937
+ rb_raise(rb_eRuntimeError,
938
+ "Descriptor objects may not be created from Ruby.");
939
+ }
940
+
941
+ self->descriptor_pool = descriptor_pool;
942
+ self->oneofdef = (const upb_oneofdef*)NUM2ULL(ptr);
943
+
944
+ return Qnil;
945
+ }
946
+
947
+ /*
948
+ * call-seq:
949
+ * OneofDescriptor.name => name
950
+ *
951
+ * Returns the name of this oneof.
952
+ */
953
+ static VALUE OneofDescriptor_name(VALUE _self) {
954
+ OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
955
+ return rb_str_maybe_null(upb_oneofdef_name(self->oneofdef));
956
+ }
957
+
958
+ /*
959
+ * call-seq:
960
+ * OneofDescriptor.each(&block) => nil
961
+ *
962
+ * Iterates through fields in this oneof, yielding to the block on each one.
963
+ */
964
+ static VALUE OneofDescriptor_each(VALUE _self) {
965
+ OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
966
+ upb_oneof_iter it;
967
+ for (upb_oneof_begin(&it, self->oneofdef);
968
+ !upb_oneof_done(&it);
969
+ upb_oneof_next(&it)) {
970
+ const upb_fielddef* f = upb_oneof_iter_field(&it);
971
+ VALUE obj = get_fielddef_obj(self->descriptor_pool, f);
972
+ rb_yield(obj);
973
+ }
974
+ return Qnil;
975
+ }
976
+
977
+ static void OneofDescriptor_register(VALUE module) {
978
+ VALUE klass = rb_define_class_under(
979
+ module, "OneofDescriptor", rb_cObject);
980
+ rb_define_alloc_func(klass, OneofDescriptor_alloc);
981
+ rb_define_method(klass, "initialize", OneofDescriptor_initialize, 3);
982
+ rb_define_method(klass, "name", OneofDescriptor_name, 0);
983
+ rb_define_method(klass, "each", OneofDescriptor_each, 0);
984
+ rb_include_module(klass, rb_mEnumerable);
985
+ rb_gc_register_address(&cOneofDescriptor);
986
+ cOneofDescriptor = klass;
987
+ }
988
+
989
+ // -----------------------------------------------------------------------------
990
+ // EnumDescriptor.
991
+ // -----------------------------------------------------------------------------
992
+
993
+ typedef struct {
994
+ const upb_enumdef* enumdef;
995
+ VALUE module; // begins as nil
996
+ VALUE descriptor_pool; // Owns the upb_enumdef.
997
+ } EnumDescriptor;
998
+
999
+ static VALUE cEnumDescriptor = Qnil;
1000
+
1001
+ static void EnumDescriptor_mark(void* _self) {
1002
+ EnumDescriptor* self = _self;
1003
+ rb_gc_mark(self->module);
1004
+ rb_gc_mark(self->descriptor_pool);
1005
+ }
1006
+
1007
+ static const rb_data_type_t EnumDescriptor_type = {
1008
+ "Google::Protobuf::EnumDescriptor",
1009
+ {EnumDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
1010
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
1011
+ };
1012
+
1013
+ static EnumDescriptor* ruby_to_EnumDescriptor(VALUE val) {
1014
+ EnumDescriptor* ret;
1015
+ TypedData_Get_Struct(val, EnumDescriptor, &EnumDescriptor_type, ret);
1016
+ return ret;
1017
+ }
1018
+
1019
+ static VALUE EnumDescriptor_alloc(VALUE klass) {
1020
+ EnumDescriptor* self = ALLOC(EnumDescriptor);
1021
+ VALUE ret = TypedData_Wrap_Struct(klass, &EnumDescriptor_type, self);
1022
+ self->enumdef = NULL;
1023
+ self->module = Qnil;
1024
+ self->descriptor_pool = Qnil;
1025
+ return ret;
1026
+ }
1027
+
1028
+ // Exposed to other modules in defs.h.
1029
+ const upb_enumdef *EnumDescriptor_GetEnumDef(VALUE enum_desc_rb) {
1030
+ EnumDescriptor *desc = ruby_to_EnumDescriptor(enum_desc_rb);
1031
+ return desc->enumdef;
1032
+ }
1033
+
1034
+ /*
1035
+ * call-seq:
1036
+ * EnumDescriptor.new(c_only_cookie, ptr) => EnumDescriptor
1037
+ *
1038
+ * Creates a descriptor wrapper object. May only be called from C.
1039
+ */
1040
+ static VALUE EnumDescriptor_initialize(VALUE _self, VALUE cookie,
1041
+ VALUE descriptor_pool, VALUE ptr) {
1042
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1043
+
1044
+ if (cookie != c_only_cookie) {
1045
+ rb_raise(rb_eRuntimeError,
1046
+ "Descriptor objects may not be created from Ruby.");
1047
+ }
1048
+
1049
+ self->descriptor_pool = descriptor_pool;
1050
+ self->enumdef = (const upb_enumdef*)NUM2ULL(ptr);
1051
+
1052
+ return Qnil;
1053
+ }
1054
+
1055
+ /*
1056
+ * call-seq:
1057
+ * EnumDescriptor.file_descriptor
1058
+ *
1059
+ * Returns the FileDescriptor object this enum belongs to.
1060
+ */
1061
+ static VALUE EnumDescriptor_file_descriptor(VALUE _self) {
1062
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1063
+ return get_filedef_obj(self->descriptor_pool,
1064
+ upb_enumdef_file(self->enumdef));
1065
+ }
1066
+
1067
+ /*
1068
+ * call-seq:
1069
+ * EnumDescriptor.name => name
1070
+ *
1071
+ * Returns the name of this enum type.
1072
+ */
1073
+ static VALUE EnumDescriptor_name(VALUE _self) {
1074
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1075
+ return rb_str_maybe_null(upb_enumdef_fullname(self->enumdef));
1076
+ }
1077
+
1078
+ /*
1079
+ * call-seq:
1080
+ * EnumDescriptor.lookup_name(name) => value
1081
+ *
1082
+ * Returns the numeric value corresponding to the given key name (as a Ruby
1083
+ * symbol), or nil if none.
1084
+ */
1085
+ static VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
1086
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1087
+ const char* name_str= rb_id2name(SYM2ID(name));
1088
+ int32_t val = 0;
1089
+ if (upb_enumdef_ntoiz(self->enumdef, name_str, &val)) {
1090
+ return INT2NUM(val);
1091
+ } else {
1092
+ return Qnil;
1093
+ }
1094
+ }
1095
+
1096
+ /*
1097
+ * call-seq:
1098
+ * EnumDescriptor.lookup_value(name) => value
1099
+ *
1100
+ * Returns the key name (as a Ruby symbol) corresponding to the integer value,
1101
+ * or nil if none.
1102
+ */
1103
+ static VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
1104
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1105
+ int32_t val = NUM2INT(number);
1106
+ const char* name = upb_enumdef_iton(self->enumdef, val);
1107
+ if (name != NULL) {
1108
+ return ID2SYM(rb_intern(name));
1109
+ } else {
1110
+ return Qnil;
1111
+ }
1112
+ }
1113
+
1114
+ /*
1115
+ * call-seq:
1116
+ * EnumDescriptor.each(&block)
1117
+ *
1118
+ * Iterates over key => value mappings in this enum's definition, yielding to
1119
+ * the block with (key, value) arguments for each one.
1120
+ */
1121
+ static VALUE EnumDescriptor_each(VALUE _self) {
1122
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1123
+
1124
+ upb_enum_iter it;
1125
+ for (upb_enum_begin(&it, self->enumdef);
1126
+ !upb_enum_done(&it);
1127
+ upb_enum_next(&it)) {
1128
+ VALUE key = ID2SYM(rb_intern(upb_enum_iter_name(&it)));
1129
+ VALUE number = INT2NUM(upb_enum_iter_number(&it));
1130
+ rb_yield_values(2, key, number);
1131
+ }
1132
+
1133
+ return Qnil;
1134
+ }
1135
+
1136
+ /*
1137
+ * call-seq:
1138
+ * EnumDescriptor.enummodule => module
1139
+ *
1140
+ * Returns the Ruby module corresponding to this enum type.
1141
+ */
1142
+ static VALUE EnumDescriptor_enummodule(VALUE _self) {
1143
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1144
+ if (self->module == Qnil) {
1145
+ self->module = build_module_from_enumdesc(_self);
1146
+ }
1147
+ return self->module;
1148
+ }
1149
+
1150
+ static void EnumDescriptor_register(VALUE module) {
1151
+ VALUE klass = rb_define_class_under(
1152
+ module, "EnumDescriptor", rb_cObject);
1153
+ rb_define_alloc_func(klass, EnumDescriptor_alloc);
1154
+ rb_define_method(klass, "initialize", EnumDescriptor_initialize, 3);
1155
+ rb_define_method(klass, "name", EnumDescriptor_name, 0);
1156
+ rb_define_method(klass, "lookup_name", EnumDescriptor_lookup_name, 1);
1157
+ rb_define_method(klass, "lookup_value", EnumDescriptor_lookup_value, 1);
1158
+ rb_define_method(klass, "each", EnumDescriptor_each, 0);
1159
+ rb_define_method(klass, "enummodule", EnumDescriptor_enummodule, 0);
1160
+ rb_define_method(klass, "file_descriptor", EnumDescriptor_file_descriptor, 0);
1161
+ rb_include_module(klass, rb_mEnumerable);
1162
+ rb_gc_register_address(&cEnumDescriptor);
1163
+ cEnumDescriptor = klass;
1164
+ }
1165
+
1166
+ static VALUE get_def_obj(VALUE _descriptor_pool, const void* ptr, VALUE klass) {
1167
+ DescriptorPool* descriptor_pool = ruby_to_DescriptorPool(_descriptor_pool);
1168
+ VALUE key = ULL2NUM((intptr_t)ptr);
1169
+ VALUE def;
1170
+
1171
+ def = rb_hash_aref(descriptor_pool->def_to_descriptor, key);
1172
+
1173
+ if (ptr == NULL) {
1174
+ return Qnil;
1175
+ }
1176
+
1177
+ if (def == Qnil) {
1178
+ // Lazily create wrapper object.
1179
+ VALUE args[3] = { c_only_cookie, _descriptor_pool, key };
1180
+ def = rb_class_new_instance(3, args, klass);
1181
+ rb_hash_aset(descriptor_pool->def_to_descriptor, key, def);
1182
+ }
1183
+
1184
+ return def;
1185
+ }
1186
+
1187
+ static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_msgdef* def) {
1188
+ return get_def_obj(descriptor_pool, def, cDescriptor);
1189
+ }
1190
+
1191
+ static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_enumdef* def) {
1192
+ return get_def_obj(descriptor_pool, def, cEnumDescriptor);
1193
+ }
1194
+
1195
+ static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_fielddef* def) {
1196
+ return get_def_obj(descriptor_pool, def, cFieldDescriptor);
1197
+ }
1198
+
1199
+ static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_filedef* def) {
1200
+ return get_def_obj(descriptor_pool, def, cFileDescriptor);
1201
+ }
1202
+
1203
+ static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_oneofdef* def) {
1204
+ return get_def_obj(descriptor_pool, def, cOneofDescriptor);
1205
+ }
1206
+
1207
+ // -----------------------------------------------------------------------------
1208
+ // Shared functions
1209
+ // -----------------------------------------------------------------------------
1210
+
1211
+ // Functions exposed to other modules in defs.h.
1212
+
1213
+ VALUE Descriptor_DefToClass(const upb_msgdef *m) {
1214
+ const upb_symtab *symtab = upb_filedef_symtab(upb_msgdef_file(m));
1215
+ VALUE pool = ObjectCache_Get(symtab);
1216
+ PBRUBY_ASSERT(pool != Qnil);
1217
+ VALUE desc_rb = get_msgdef_obj(pool, m);
1218
+ const Descriptor* desc = ruby_to_Descriptor(desc_rb);
1219
+ return desc->klass;
1220
+ }
1221
+
1222
+ const upb_msgdef *Descriptor_GetMsgDef(VALUE desc_rb) {
1223
+ const Descriptor* desc = ruby_to_Descriptor(desc_rb);
1224
+ return desc->msgdef;
1225
+ }
1226
+
1227
+ VALUE TypeInfo_InitArg(int argc, VALUE *argv, int skip_arg) {
1228
+ if (argc > skip_arg) {
1229
+ if (argc > 1 + skip_arg) {
1230
+ rb_raise(rb_eArgError, "Expected a maximum of %d arguments.", skip_arg + 1);
1231
+ }
1232
+ return argv[skip_arg];
1233
+ } else {
1234
+ return Qnil;
1235
+ }
1236
+ }
1237
+
1238
+ TypeInfo TypeInfo_FromClass(int argc, VALUE* argv, int skip_arg,
1239
+ VALUE* type_class, VALUE* init_arg) {
1240
+ TypeInfo ret = {ruby_to_fieldtype(argv[skip_arg])};
1241
+
1242
+ if (ret.type == UPB_TYPE_MESSAGE || ret.type == UPB_TYPE_ENUM) {
1243
+ *init_arg = TypeInfo_InitArg(argc, argv, skip_arg + 2);
1244
+
1245
+ if (argc < 2 + skip_arg) {
1246
+ rb_raise(rb_eArgError, "Expected at least %d arguments for message/enum.",
1247
+ 2 + skip_arg);
1248
+ }
1249
+
1250
+ VALUE klass = argv[1 + skip_arg];
1251
+ VALUE desc = MessageOrEnum_GetDescriptor(klass);
1252
+ *type_class = klass;
1253
+
1254
+ if (desc == Qnil) {
1255
+ rb_raise(rb_eArgError,
1256
+ "Type class has no descriptor. Please pass a "
1257
+ "class or enum as returned by the DescriptorPool.");
1258
+ }
1259
+
1260
+ if (ret.type == UPB_TYPE_MESSAGE) {
1261
+ ret.def.msgdef = ruby_to_Descriptor(desc)->msgdef;
1262
+ Message_CheckClass(klass);
1263
+ } else {
1264
+ PBRUBY_ASSERT(ret.type == UPB_TYPE_ENUM);
1265
+ ret.def.enumdef = ruby_to_EnumDescriptor(desc)->enumdef;
1266
+ }
1267
+ } else {
1268
+ *init_arg = TypeInfo_InitArg(argc, argv, skip_arg + 1);
1269
+ }
1270
+
1271
+ return ret;
1272
+ }
1273
+
1274
+ void Defs_register(VALUE module) {
1275
+ DescriptorPool_register(module);
1276
+ Descriptor_register(module);
1277
+ FileDescriptor_register(module);
1278
+ FieldDescriptor_register(module);
1279
+ OneofDescriptor_register(module);
1280
+ EnumDescriptor_register(module);
1281
+
1282
+ rb_gc_register_address(&c_only_cookie);
1283
+ c_only_cookie = rb_class_new_instance(0, NULL, rb_cObject);
1284
+ }