google-protobuf 4.31.0.rc.1-aarch64-linux-gnu

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. checksums.yaml +7 -0
  2. data/ext/google/protobuf_c/Rakefile +3 -0
  3. data/ext/google/protobuf_c/convert.c +335 -0
  4. data/ext/google/protobuf_c/convert.h +50 -0
  5. data/ext/google/protobuf_c/defs.c +1932 -0
  6. data/ext/google/protobuf_c/defs.h +82 -0
  7. data/ext/google/protobuf_c/extconf.rb +44 -0
  8. data/ext/google/protobuf_c/glue.c +135 -0
  9. data/ext/google/protobuf_c/map.c +731 -0
  10. data/ext/google/protobuf_c/map.h +48 -0
  11. data/ext/google/protobuf_c/message.c +1426 -0
  12. data/ext/google/protobuf_c/message.h +82 -0
  13. data/ext/google/protobuf_c/protobuf.c +357 -0
  14. data/ext/google/protobuf_c/protobuf.h +102 -0
  15. data/ext/google/protobuf_c/repeated_field.c +691 -0
  16. data/ext/google/protobuf_c/repeated_field.h +45 -0
  17. data/ext/google/protobuf_c/ruby-upb.c +18305 -0
  18. data/ext/google/protobuf_c/ruby-upb.h +16315 -0
  19. data/ext/google/protobuf_c/shared_convert.c +69 -0
  20. data/ext/google/protobuf_c/shared_convert.h +26 -0
  21. data/ext/google/protobuf_c/shared_message.c +37 -0
  22. data/ext/google/protobuf_c/shared_message.h +21 -0
  23. data/ext/google/protobuf_c/third_party/utf8_range/LICENSE +22 -0
  24. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.c +207 -0
  25. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.h +22 -0
  26. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range_neon.inc +117 -0
  27. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range_sse.inc +272 -0
  28. data/ext/google/protobuf_c/wrap_memcpy.c +29 -0
  29. data/lib/google/3.1/protobuf_c.so +0 -0
  30. data/lib/google/3.2/protobuf_c.so +0 -0
  31. data/lib/google/3.3/protobuf_c.so +0 -0
  32. data/lib/google/3.4/protobuf_c.so +0 -0
  33. data/lib/google/protobuf/any_pb.rb +17 -0
  34. data/lib/google/protobuf/api_pb.rb +22 -0
  35. data/lib/google/protobuf/descriptor_pb.rb +70 -0
  36. data/lib/google/protobuf/duration_pb.rb +17 -0
  37. data/lib/google/protobuf/empty_pb.rb +17 -0
  38. data/lib/google/protobuf/ffi/descriptor.rb +175 -0
  39. data/lib/google/protobuf/ffi/descriptor_pool.rb +79 -0
  40. data/lib/google/protobuf/ffi/enum_descriptor.rb +183 -0
  41. data/lib/google/protobuf/ffi/ffi.rb +214 -0
  42. data/lib/google/protobuf/ffi/field_descriptor.rb +346 -0
  43. data/lib/google/protobuf/ffi/file_descriptor.rb +85 -0
  44. data/lib/google/protobuf/ffi/internal/arena.rb +60 -0
  45. data/lib/google/protobuf/ffi/internal/convert.rb +292 -0
  46. data/lib/google/protobuf/ffi/internal/pointer_helper.rb +36 -0
  47. data/lib/google/protobuf/ffi/internal/type_safety.rb +25 -0
  48. data/lib/google/protobuf/ffi/map.rb +433 -0
  49. data/lib/google/protobuf/ffi/message.rb +783 -0
  50. data/lib/google/protobuf/ffi/method_descriptor.rb +124 -0
  51. data/lib/google/protobuf/ffi/object_cache.rb +30 -0
  52. data/lib/google/protobuf/ffi/oneof_descriptor.rb +107 -0
  53. data/lib/google/protobuf/ffi/repeated_field.rb +411 -0
  54. data/lib/google/protobuf/ffi/service_descriptor.rb +117 -0
  55. data/lib/google/protobuf/field_mask_pb.rb +17 -0
  56. data/lib/google/protobuf/internal/object_cache.rb +99 -0
  57. data/lib/google/protobuf/message_exts.rb +39 -0
  58. data/lib/google/protobuf/plugin_pb.rb +25 -0
  59. data/lib/google/protobuf/repeated_field.rb +177 -0
  60. data/lib/google/protobuf/source_context_pb.rb +17 -0
  61. data/lib/google/protobuf/struct_pb.rb +20 -0
  62. data/lib/google/protobuf/timestamp_pb.rb +17 -0
  63. data/lib/google/protobuf/type_pb.rb +27 -0
  64. data/lib/google/protobuf/well_known_types.rb +211 -0
  65. data/lib/google/protobuf/wrappers_pb.rb +25 -0
  66. data/lib/google/protobuf.rb +61 -0
  67. data/lib/google/protobuf_ffi.rb +52 -0
  68. data/lib/google/protobuf_native.rb +19 -0
  69. data/lib/google/tasks/ffi.rake +100 -0
  70. metadata +215 -0
@@ -0,0 +1,1932 @@
1
+ // Protocol Buffers - Google's data interchange format
2
+ // Copyright 2014 Google Inc. All rights reserved.
3
+ //
4
+ // Use of this source code is governed by a BSD-style
5
+ // license that can be found in the LICENSE file or at
6
+ // https://developers.google.com/open-source/licenses/bsd
7
+
8
+ #include <ctype.h>
9
+ #include <errno.h>
10
+
11
+ #include "convert.h"
12
+ #include "message.h"
13
+ #include "protobuf.h"
14
+
15
+ // -----------------------------------------------------------------------------
16
+ // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
17
+ // instances.
18
+ // -----------------------------------------------------------------------------
19
+
20
+ static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_MessageDef* def);
21
+ static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_EnumDef* def);
22
+ static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_FieldDef* def);
23
+ static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_FileDef* def);
24
+ static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_OneofDef* def);
25
+ static VALUE get_servicedef_obj(VALUE descriptor_pool,
26
+ const upb_ServiceDef* def);
27
+ static VALUE get_methoddef_obj(VALUE descriptor_pool, const upb_MethodDef* def);
28
+
29
+ // A distinct object that is not accessible from Ruby. We use this as a
30
+ // constructor argument to enforce that certain objects cannot be created from
31
+ // Ruby.
32
+ VALUE c_only_cookie = Qnil;
33
+
34
+ // -----------------------------------------------------------------------------
35
+ // Common utilities.
36
+ // -----------------------------------------------------------------------------
37
+
38
+ static const char* get_str(VALUE str) {
39
+ Check_Type(str, T_STRING);
40
+ return RSTRING_PTR(str);
41
+ }
42
+
43
+ static VALUE rb_str_maybe_null(const char* s) {
44
+ if (s == NULL) {
45
+ s = "";
46
+ }
47
+ return rb_str_new2(s);
48
+ }
49
+ static ID options_instancevar_interned;
50
+ // -----------------------------------------------------------------------------
51
+ // DescriptorPool.
52
+ // -----------------------------------------------------------------------------
53
+
54
+ typedef struct {
55
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
56
+ // macro to update VALUE references, as to trigger write barriers.
57
+ VALUE def_to_descriptor; // Hash table of def* -> Ruby descriptor.
58
+ upb_DefPool* symtab;
59
+ } DescriptorPool;
60
+
61
+ VALUE cDescriptorPool = Qnil;
62
+
63
+ // Global singleton DescriptorPool. The user is free to create others, but this
64
+ // is used by generated code.
65
+ VALUE generated_pool = Qnil;
66
+
67
+ static void DescriptorPool_mark(void* _self) {
68
+ DescriptorPool* self = _self;
69
+ rb_gc_mark(self->def_to_descriptor);
70
+ }
71
+
72
+ static void DescriptorPool_free(void* _self) {
73
+ DescriptorPool* self = _self;
74
+ upb_DefPool_Free(self->symtab);
75
+ xfree(self);
76
+ }
77
+
78
+ static const rb_data_type_t DescriptorPool_type = {
79
+ "Google::Protobuf::DescriptorPool",
80
+ {DescriptorPool_mark, DescriptorPool_free, NULL},
81
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
82
+ };
83
+
84
+ static DescriptorPool* ruby_to_DescriptorPool(VALUE val) {
85
+ DescriptorPool* ret;
86
+ TypedData_Get_Struct(val, DescriptorPool, &DescriptorPool_type, ret);
87
+ return ret;
88
+ }
89
+
90
+ // Exposed to other modules in defs.h.
91
+ const upb_DefPool* DescriptorPool_GetSymtab(VALUE desc_pool_rb) {
92
+ DescriptorPool* pool = ruby_to_DescriptorPool(desc_pool_rb);
93
+ return pool->symtab;
94
+ }
95
+
96
+ /*
97
+ * call-seq:
98
+ * DescriptorPool.new => pool
99
+ *
100
+ * Creates a new, empty, descriptor pool.
101
+ */
102
+ static VALUE DescriptorPool_alloc(VALUE klass) {
103
+ DescriptorPool* self = ALLOC(DescriptorPool);
104
+ VALUE ret;
105
+
106
+ self->def_to_descriptor = Qnil;
107
+ ret = TypedData_Wrap_Struct(klass, &DescriptorPool_type, self);
108
+
109
+ RB_OBJ_WRITE(ret, &self->def_to_descriptor, rb_hash_new());
110
+ self->symtab = upb_DefPool_New();
111
+ return ObjectCache_TryAdd(self->symtab, ret);
112
+ }
113
+
114
+ /*
115
+ * call-seq:
116
+ * DescriptorPool.add_serialized_file(serialized_file_proto)
117
+ *
118
+ * Adds the given serialized FileDescriptorProto to the pool.
119
+ */
120
+ VALUE DescriptorPool_add_serialized_file(VALUE _self,
121
+ VALUE serialized_file_proto) {
122
+ DescriptorPool* self = ruby_to_DescriptorPool(_self);
123
+ Check_Type(serialized_file_proto, T_STRING);
124
+ VALUE arena_rb = Arena_new();
125
+ upb_Arena* arena = Arena_get(arena_rb);
126
+ google_protobuf_FileDescriptorProto* file_proto =
127
+ google_protobuf_FileDescriptorProto_parse(
128
+ RSTRING_PTR(serialized_file_proto),
129
+ RSTRING_LEN(serialized_file_proto), arena);
130
+ if (!file_proto) {
131
+ rb_raise(rb_eArgError, "Unable to parse FileDescriptorProto");
132
+ }
133
+ upb_Status status;
134
+ upb_Status_Clear(&status);
135
+ const upb_FileDef* filedef =
136
+ upb_DefPool_AddFile(self->symtab, file_proto, &status);
137
+ if (!filedef) {
138
+ rb_raise(cTypeError, "Unable to build file to DescriptorPool: %s",
139
+ upb_Status_ErrorMessage(&status));
140
+ }
141
+ RB_GC_GUARD(arena_rb);
142
+ return get_filedef_obj(_self, filedef);
143
+ }
144
+
145
+ /*
146
+ * call-seq:
147
+ * DescriptorPool.lookup(name) => descriptor
148
+ *
149
+ * Finds a Descriptor, EnumDescriptor, FieldDescriptor or ServiceDescriptor by
150
+ * name and returns it, or nil if none exists with the given name.
151
+ */
152
+ static VALUE DescriptorPool_lookup(VALUE _self, VALUE name) {
153
+ DescriptorPool* self = ruby_to_DescriptorPool(_self);
154
+ const char* name_str = get_str(name);
155
+ const upb_MessageDef* msgdef;
156
+ const upb_EnumDef* enumdef;
157
+ const upb_FieldDef* fielddef;
158
+ const upb_ServiceDef* servicedef;
159
+ const upb_FileDef* filedef;
160
+
161
+ msgdef = upb_DefPool_FindMessageByName(self->symtab, name_str);
162
+ if (msgdef) {
163
+ return get_msgdef_obj(_self, msgdef);
164
+ }
165
+
166
+ fielddef = upb_DefPool_FindExtensionByName(self->symtab, name_str);
167
+ if (fielddef) {
168
+ return get_fielddef_obj(_self, fielddef);
169
+ }
170
+
171
+ enumdef = upb_DefPool_FindEnumByName(self->symtab, name_str);
172
+ if (enumdef) {
173
+ return get_enumdef_obj(_self, enumdef);
174
+ }
175
+
176
+ servicedef = upb_DefPool_FindServiceByName(self->symtab, name_str);
177
+ if (servicedef) {
178
+ return get_servicedef_obj(_self, servicedef);
179
+ }
180
+
181
+ filedef = upb_DefPool_FindFileByName(self->symtab, name_str);
182
+ if (filedef) {
183
+ return get_filedef_obj(_self, filedef);
184
+ }
185
+
186
+ return Qnil;
187
+ }
188
+
189
+ /*
190
+ * call-seq:
191
+ * DescriptorPool.generated_pool => descriptor_pool
192
+ *
193
+ * Class method that returns the global DescriptorPool. This is a singleton into
194
+ * which generated-code message and enum types are registered. The user may also
195
+ * register types in this pool for convenience so that they do not have to hold
196
+ * a reference to a private pool instance.
197
+ */
198
+ static VALUE DescriptorPool_generated_pool(VALUE _self) {
199
+ return generated_pool;
200
+ }
201
+
202
+ static void DescriptorPool_register(VALUE module) {
203
+ VALUE klass = rb_define_class_under(module, "DescriptorPool", rb_cObject);
204
+ rb_define_alloc_func(klass, DescriptorPool_alloc);
205
+ rb_define_method(klass, "add_serialized_file",
206
+ DescriptorPool_add_serialized_file, 1);
207
+ rb_define_method(klass, "lookup", DescriptorPool_lookup, 1);
208
+ rb_define_singleton_method(klass, "generated_pool",
209
+ DescriptorPool_generated_pool, 0);
210
+ rb_gc_register_address(&cDescriptorPool);
211
+ cDescriptorPool = klass;
212
+
213
+ rb_gc_register_address(&generated_pool);
214
+ generated_pool = rb_class_new_instance(0, NULL, klass);
215
+ options_instancevar_interned = rb_intern("options");
216
+ }
217
+
218
+ // -----------------------------------------------------------------------------
219
+ // Descriptor.
220
+ // -----------------------------------------------------------------------------
221
+
222
+ typedef struct {
223
+ const upb_MessageDef* msgdef;
224
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
225
+ // macro to update VALUE references, as to trigger write barriers.
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 | RUBY_TYPED_WB_PROTECTED,
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
+ // Decode and return a frozen instance of a Descriptor Option for the given pool
251
+ static VALUE decode_options(VALUE self, const char* option_type, int size,
252
+ const char* bytes, VALUE descriptor_pool) {
253
+ VALUE options_rb = rb_ivar_get(self, options_instancevar_interned);
254
+ if (options_rb != Qnil) {
255
+ return options_rb;
256
+ }
257
+
258
+ static const char* prefix = "google.protobuf.";
259
+ char fullname
260
+ [/*strlen(prefix)*/ 16 +
261
+ /*strln(longest option type supported e.g. "MessageOptions")*/ 14 +
262
+ /*null terminator*/ 1];
263
+
264
+ snprintf(fullname, sizeof(fullname), "%s%s", prefix, option_type);
265
+ const upb_MessageDef* msgdef = upb_DefPool_FindMessageByName(
266
+ ruby_to_DescriptorPool(descriptor_pool)->symtab, fullname);
267
+ if (!msgdef) {
268
+ rb_raise(rb_eRuntimeError, "Cannot find %s in DescriptorPool", option_type);
269
+ }
270
+
271
+ VALUE desc_rb = get_msgdef_obj(descriptor_pool, msgdef);
272
+ const Descriptor* desc = ruby_to_Descriptor(desc_rb);
273
+
274
+ options_rb = Message_decode_bytes(size, bytes, 0, desc->klass, false);
275
+
276
+ // Strip features from the options proto to keep it internal.
277
+ const upb_MessageDef* decoded_desc = NULL;
278
+ upb_Message* options = Message_GetMutable(options_rb, &decoded_desc);
279
+ PBRUBY_ASSERT(options != NULL);
280
+ PBRUBY_ASSERT(decoded_desc == msgdef);
281
+ const upb_FieldDef* field =
282
+ upb_MessageDef_FindFieldByName(decoded_desc, "features");
283
+ PBRUBY_ASSERT(field != NULL);
284
+ upb_Message_ClearFieldByDef(options, field);
285
+
286
+ Message_freeze(options_rb);
287
+
288
+ rb_ivar_set(self, options_instancevar_interned, options_rb);
289
+ return options_rb;
290
+ }
291
+
292
+ /*
293
+ * call-seq:
294
+ * Descriptor.new => descriptor
295
+ *
296
+ * Creates a new, empty, message type descriptor. At a minimum, its name must be
297
+ * set before it is added to a pool. It cannot be used to create messages until
298
+ * it is added to a pool, after which it becomes immutable (as part of a
299
+ * finalization process).
300
+ */
301
+ static VALUE Descriptor_alloc(VALUE klass) {
302
+ Descriptor* self = ALLOC(Descriptor);
303
+ VALUE ret = TypedData_Wrap_Struct(klass, &Descriptor_type, self);
304
+ self->msgdef = NULL;
305
+ self->klass = Qnil;
306
+ self->descriptor_pool = Qnil;
307
+ return ret;
308
+ }
309
+
310
+ /*
311
+ * call-seq:
312
+ * Descriptor.new(c_only_cookie, ptr) => Descriptor
313
+ *
314
+ * Creates a descriptor wrapper object. May only be called from C.
315
+ */
316
+ static VALUE Descriptor_initialize(VALUE _self, VALUE cookie,
317
+ VALUE descriptor_pool, VALUE ptr) {
318
+ Descriptor* self = ruby_to_Descriptor(_self);
319
+
320
+ if (cookie != c_only_cookie) {
321
+ rb_raise(rb_eRuntimeError,
322
+ "Descriptor objects may not be created from Ruby.");
323
+ }
324
+
325
+ RB_OBJ_WRITE(_self, &self->descriptor_pool, descriptor_pool);
326
+ self->msgdef = (const upb_MessageDef*)NUM2ULL(ptr);
327
+
328
+ return Qnil;
329
+ }
330
+
331
+ /*
332
+ * call-seq:
333
+ * Descriptor.file_descriptor
334
+ *
335
+ * Returns the FileDescriptor object this message belongs to.
336
+ */
337
+ static VALUE Descriptor_file_descriptor(VALUE _self) {
338
+ Descriptor* self = ruby_to_Descriptor(_self);
339
+ return get_filedef_obj(self->descriptor_pool,
340
+ upb_MessageDef_File(self->msgdef));
341
+ }
342
+
343
+ /*
344
+ * call-seq:
345
+ * Descriptor.name => name
346
+ *
347
+ * Returns the name of this message type as a fully-qualified string (e.g.,
348
+ * My.Package.MessageType).
349
+ */
350
+ static VALUE Descriptor_name(VALUE _self) {
351
+ Descriptor* self = ruby_to_Descriptor(_self);
352
+ return rb_str_maybe_null(upb_MessageDef_FullName(self->msgdef));
353
+ }
354
+
355
+ /*
356
+ * call-seq:
357
+ * Descriptor.each(&block)
358
+ *
359
+ * Iterates over fields in this message type, yielding to the block on each one.
360
+ */
361
+ static VALUE Descriptor_each(VALUE _self) {
362
+ Descriptor* self = ruby_to_Descriptor(_self);
363
+
364
+ int n = upb_MessageDef_FieldCount(self->msgdef);
365
+ for (int i = 0; i < n; i++) {
366
+ const upb_FieldDef* field = upb_MessageDef_Field(self->msgdef, i);
367
+ VALUE obj = get_fielddef_obj(self->descriptor_pool, field);
368
+ rb_yield(obj);
369
+ }
370
+ return Qnil;
371
+ }
372
+
373
+ /*
374
+ * call-seq:
375
+ * Descriptor.lookup(name) => FieldDescriptor
376
+ *
377
+ * Returns the field descriptor for the field with the given name, if present,
378
+ * or nil if none.
379
+ */
380
+ static VALUE Descriptor_lookup(VALUE _self, VALUE name) {
381
+ Descriptor* self = ruby_to_Descriptor(_self);
382
+ const char* s = get_str(name);
383
+ const upb_FieldDef* field = upb_MessageDef_FindFieldByName(self->msgdef, s);
384
+ if (field == NULL) {
385
+ return Qnil;
386
+ }
387
+ return get_fielddef_obj(self->descriptor_pool, field);
388
+ }
389
+
390
+ /*
391
+ * call-seq:
392
+ * Descriptor.each_oneof(&block) => nil
393
+ *
394
+ * Invokes the given block for each oneof in this message type, passing the
395
+ * corresponding OneofDescriptor.
396
+ */
397
+ static VALUE Descriptor_each_oneof(VALUE _self) {
398
+ Descriptor* self = ruby_to_Descriptor(_self);
399
+
400
+ int n = upb_MessageDef_OneofCount(self->msgdef);
401
+ for (int i = 0; i < n; i++) {
402
+ const upb_OneofDef* oneof = upb_MessageDef_Oneof(self->msgdef, i);
403
+ VALUE obj = get_oneofdef_obj(self->descriptor_pool, oneof);
404
+ rb_yield(obj);
405
+ }
406
+ return Qnil;
407
+ }
408
+
409
+ /*
410
+ * call-seq:
411
+ * Descriptor.lookup_oneof(name) => OneofDescriptor
412
+ *
413
+ * Returns the oneof descriptor for the oneof with the given name, if present,
414
+ * or nil if none.
415
+ */
416
+ static VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
417
+ Descriptor* self = ruby_to_Descriptor(_self);
418
+ const char* s = get_str(name);
419
+ const upb_OneofDef* oneof = upb_MessageDef_FindOneofByName(self->msgdef, s);
420
+ if (oneof == NULL) {
421
+ return Qnil;
422
+ }
423
+ return get_oneofdef_obj(self->descriptor_pool, oneof);
424
+ }
425
+
426
+ /*
427
+ * call-seq:
428
+ * Descriptor.msgclass => message_klass
429
+ *
430
+ * Returns the Ruby class created for this message type.
431
+ */
432
+ static VALUE Descriptor_msgclass(VALUE _self) {
433
+ Descriptor* self = ruby_to_Descriptor(_self);
434
+ if (self->klass == Qnil) {
435
+ RB_OBJ_WRITE(_self, &self->klass, build_class_from_descriptor(_self));
436
+ }
437
+ return self->klass;
438
+ }
439
+
440
+ /*
441
+ * call-seq:
442
+ * Descriptor.options => options
443
+ *
444
+ * Returns the `MessageOptions` for this `Descriptor`.
445
+ */
446
+ static VALUE Descriptor_options(VALUE _self) {
447
+ Descriptor* self = ruby_to_Descriptor(_self);
448
+ const google_protobuf_MessageOptions* opts =
449
+ upb_MessageDef_Options(self->msgdef);
450
+ upb_Arena* arena = upb_Arena_New();
451
+ size_t size;
452
+ char* serialized =
453
+ google_protobuf_MessageOptions_serialize(opts, arena, &size);
454
+ VALUE message_options = decode_options(_self, "MessageOptions", size,
455
+ serialized, self->descriptor_pool);
456
+ upb_Arena_Free(arena);
457
+ return message_options;
458
+ }
459
+
460
+ /*
461
+ * call-seq:
462
+ * Descriptor.to_proto => DescriptorProto
463
+ *
464
+ * Returns the `DescriptorProto` of this `Descriptor`.
465
+ */
466
+ static VALUE Descriptor_to_proto(VALUE _self) {
467
+ Descriptor* self = ruby_to_Descriptor(_self);
468
+ upb_Arena* arena = upb_Arena_New();
469
+ google_protobuf_DescriptorProto* proto =
470
+ upb_MessageDef_ToProto(self->msgdef, arena);
471
+ size_t size;
472
+ const char* serialized =
473
+ google_protobuf_DescriptorProto_serialize(proto, arena, &size);
474
+ VALUE proto_class = rb_path2class("Google::Protobuf::DescriptorProto");
475
+ VALUE proto_rb =
476
+ Message_decode_bytes(size, serialized, 0, proto_class, false);
477
+ upb_Arena_Free(arena);
478
+ return proto_rb;
479
+ }
480
+
481
+ static void Descriptor_register(VALUE module) {
482
+ VALUE klass = rb_define_class_under(module, "Descriptor", rb_cObject);
483
+ rb_define_alloc_func(klass, Descriptor_alloc);
484
+ rb_define_method(klass, "initialize", Descriptor_initialize, 3);
485
+ rb_define_method(klass, "each", Descriptor_each, 0);
486
+ rb_define_method(klass, "lookup", Descriptor_lookup, 1);
487
+ rb_define_method(klass, "each_oneof", Descriptor_each_oneof, 0);
488
+ rb_define_method(klass, "lookup_oneof", Descriptor_lookup_oneof, 1);
489
+ rb_define_method(klass, "msgclass", Descriptor_msgclass, 0);
490
+ rb_define_method(klass, "name", Descriptor_name, 0);
491
+ rb_define_method(klass, "file_descriptor", Descriptor_file_descriptor, 0);
492
+ rb_define_method(klass, "options", Descriptor_options, 0);
493
+ rb_define_method(klass, "to_proto", Descriptor_to_proto, 0);
494
+ rb_include_module(klass, rb_mEnumerable);
495
+ rb_gc_register_address(&cDescriptor);
496
+ cDescriptor = klass;
497
+ }
498
+
499
+ // -----------------------------------------------------------------------------
500
+ // FileDescriptor.
501
+ // -----------------------------------------------------------------------------
502
+
503
+ typedef struct {
504
+ const upb_FileDef* filedef;
505
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
506
+ // macro to update VALUE references, as to trigger write barriers.
507
+ VALUE descriptor_pool; // Owns the upb_FileDef.
508
+ } FileDescriptor;
509
+
510
+ static VALUE cFileDescriptor = Qnil;
511
+
512
+ static void FileDescriptor_mark(void* _self) {
513
+ FileDescriptor* self = _self;
514
+ rb_gc_mark(self->descriptor_pool);
515
+ }
516
+
517
+ static const rb_data_type_t FileDescriptor_type = {
518
+ "Google::Protobuf::FileDescriptor",
519
+ {FileDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
520
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
521
+ };
522
+
523
+ static FileDescriptor* ruby_to_FileDescriptor(VALUE val) {
524
+ FileDescriptor* ret;
525
+ TypedData_Get_Struct(val, FileDescriptor, &FileDescriptor_type, ret);
526
+ return ret;
527
+ }
528
+
529
+ static VALUE FileDescriptor_alloc(VALUE klass) {
530
+ FileDescriptor* self = ALLOC(FileDescriptor);
531
+ VALUE ret = TypedData_Wrap_Struct(klass, &FileDescriptor_type, self);
532
+ self->descriptor_pool = Qnil;
533
+ self->filedef = NULL;
534
+ return ret;
535
+ }
536
+
537
+ /*
538
+ * call-seq:
539
+ * FileDescriptor.new => file
540
+ *
541
+ * Returns a new file descriptor. May
542
+ * to a builder.
543
+ */
544
+ static VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
545
+ VALUE descriptor_pool, VALUE ptr) {
546
+ FileDescriptor* self = ruby_to_FileDescriptor(_self);
547
+
548
+ if (cookie != c_only_cookie) {
549
+ rb_raise(rb_eRuntimeError,
550
+ "Descriptor objects may not be created from Ruby.");
551
+ }
552
+
553
+ RB_OBJ_WRITE(_self, &self->descriptor_pool, descriptor_pool);
554
+ self->filedef = (const upb_FileDef*)NUM2ULL(ptr);
555
+
556
+ return Qnil;
557
+ }
558
+
559
+ /*
560
+ * call-seq:
561
+ * FileDescriptor.name => name
562
+ *
563
+ * Returns the name of the file.
564
+ */
565
+ static VALUE FileDescriptor_name(VALUE _self) {
566
+ FileDescriptor* self = ruby_to_FileDescriptor(_self);
567
+ const char* name = upb_FileDef_Name(self->filedef);
568
+ return name == NULL ? Qnil : rb_str_new2(name);
569
+ }
570
+
571
+ /*
572
+ * call-seq:
573
+ * FileDescriptor.options => options
574
+ *
575
+ * Returns the `FileOptions` for this `FileDescriptor`.
576
+ */
577
+ static VALUE FileDescriptor_options(VALUE _self) {
578
+ FileDescriptor* self = ruby_to_FileDescriptor(_self);
579
+ const google_protobuf_FileOptions* opts = upb_FileDef_Options(self->filedef);
580
+ upb_Arena* arena = upb_Arena_New();
581
+ size_t size;
582
+ char* serialized = google_protobuf_FileOptions_serialize(opts, arena, &size);
583
+ VALUE file_options = decode_options(_self, "FileOptions", size, serialized,
584
+ self->descriptor_pool);
585
+ upb_Arena_Free(arena);
586
+ return file_options;
587
+ }
588
+
589
+ /*
590
+ * call-seq:
591
+ * FileDescriptor.to_proto => FileDescriptorProto
592
+ *
593
+ * Returns the `FileDescriptorProto` of this `FileDescriptor`.
594
+ */
595
+ static VALUE FileDescriptor_to_proto(VALUE _self) {
596
+ FileDescriptor* self = ruby_to_FileDescriptor(_self);
597
+ upb_Arena* arena = upb_Arena_New();
598
+ google_protobuf_FileDescriptorProto* file_proto =
599
+ upb_FileDef_ToProto(self->filedef, arena);
600
+
601
+ size_t size;
602
+ const char* serialized =
603
+ google_protobuf_FileDescriptorProto_serialize(file_proto, arena, &size);
604
+
605
+ VALUE file_proto_class =
606
+ rb_path2class("Google::Protobuf::FileDescriptorProto");
607
+ VALUE proto_rb =
608
+ Message_decode_bytes(size, serialized, 0, file_proto_class, false);
609
+ upb_Arena_Free(arena);
610
+ return proto_rb;
611
+ }
612
+
613
+ static void FileDescriptor_register(VALUE module) {
614
+ VALUE klass = rb_define_class_under(module, "FileDescriptor", rb_cObject);
615
+ rb_define_alloc_func(klass, FileDescriptor_alloc);
616
+ rb_define_method(klass, "initialize", FileDescriptor_initialize, 3);
617
+ rb_define_method(klass, "name", FileDescriptor_name, 0);
618
+ rb_define_method(klass, "options", FileDescriptor_options, 0);
619
+ rb_define_method(klass, "to_proto", FileDescriptor_to_proto, 0);
620
+ rb_gc_register_address(&cFileDescriptor);
621
+ cFileDescriptor = klass;
622
+ }
623
+
624
+ // -----------------------------------------------------------------------------
625
+ // FieldDescriptor.
626
+ // -----------------------------------------------------------------------------
627
+
628
+ typedef struct {
629
+ const upb_FieldDef* fielddef;
630
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
631
+ // macro to update VALUE references, as to trigger write barriers.
632
+ VALUE descriptor_pool; // Owns the upb_FieldDef.
633
+ } FieldDescriptor;
634
+
635
+ static VALUE cFieldDescriptor = Qnil;
636
+
637
+ static void FieldDescriptor_mark(void* _self) {
638
+ FieldDescriptor* self = _self;
639
+ rb_gc_mark(self->descriptor_pool);
640
+ }
641
+
642
+ static const rb_data_type_t FieldDescriptor_type = {
643
+ "Google::Protobuf::FieldDescriptor",
644
+ {FieldDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
645
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
646
+ };
647
+
648
+ static FieldDescriptor* ruby_to_FieldDescriptor(VALUE val) {
649
+ FieldDescriptor* ret;
650
+ TypedData_Get_Struct(val, FieldDescriptor, &FieldDescriptor_type, ret);
651
+ return ret;
652
+ }
653
+
654
+ /*
655
+ * call-seq:
656
+ * FieldDescriptor.new => field
657
+ *
658
+ * Returns a new field descriptor. Its name, type, etc. must be set before it is
659
+ * added to a message type.
660
+ */
661
+ static VALUE FieldDescriptor_alloc(VALUE klass) {
662
+ FieldDescriptor* self = ALLOC(FieldDescriptor);
663
+ VALUE ret = TypedData_Wrap_Struct(klass, &FieldDescriptor_type, self);
664
+ self->fielddef = NULL;
665
+ return ret;
666
+ }
667
+
668
+ /*
669
+ * call-seq:
670
+ * FieldDescriptor.new(c_only_cookie, pool, ptr) => FieldDescriptor
671
+ *
672
+ * Creates a descriptor wrapper object. May only be called from C.
673
+ */
674
+ static VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
675
+ VALUE descriptor_pool, VALUE ptr) {
676
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
677
+
678
+ if (cookie != c_only_cookie) {
679
+ rb_raise(rb_eRuntimeError,
680
+ "Descriptor objects may not be created from Ruby.");
681
+ }
682
+
683
+ RB_OBJ_WRITE(_self, &self->descriptor_pool, descriptor_pool);
684
+ self->fielddef = (const upb_FieldDef*)NUM2ULL(ptr);
685
+
686
+ return Qnil;
687
+ }
688
+
689
+ /*
690
+ * call-seq:
691
+ * FieldDescriptor.name => name
692
+ *
693
+ * Returns the name of this field.
694
+ */
695
+ static VALUE FieldDescriptor_name(VALUE _self) {
696
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
697
+ return rb_str_maybe_null(upb_FieldDef_Name(self->fielddef));
698
+ }
699
+
700
+ // Non-static, exposed to other .c files.
701
+ upb_CType ruby_to_fieldtype(VALUE type) {
702
+ if (TYPE(type) != T_SYMBOL) {
703
+ rb_raise(rb_eArgError, "Expected symbol for field type.");
704
+ }
705
+
706
+ #define CONVERT(upb, ruby) \
707
+ if (SYM2ID(type) == rb_intern(#ruby)) { \
708
+ return kUpb_CType_##upb; \
709
+ }
710
+
711
+ CONVERT(Float, float);
712
+ CONVERT(Double, double);
713
+ CONVERT(Bool, bool);
714
+ CONVERT(String, string);
715
+ CONVERT(Bytes, bytes);
716
+ CONVERT(Message, message);
717
+ CONVERT(Enum, enum);
718
+ CONVERT(Int32, int32);
719
+ CONVERT(Int64, int64);
720
+ CONVERT(UInt32, uint32);
721
+ CONVERT(UInt64, uint64);
722
+
723
+ #undef CONVERT
724
+
725
+ rb_raise(rb_eArgError, "Unknown field type.");
726
+ return 0;
727
+ }
728
+
729
+ static VALUE descriptortype_to_ruby(upb_FieldType type) {
730
+ switch (type) {
731
+ #define CONVERT(upb, ruby) \
732
+ case kUpb_FieldType_##upb: \
733
+ return ID2SYM(rb_intern(#ruby));
734
+ CONVERT(Float, float);
735
+ CONVERT(Double, double);
736
+ CONVERT(Bool, bool);
737
+ CONVERT(String, string);
738
+ CONVERT(Bytes, bytes);
739
+ CONVERT(Message, message);
740
+ CONVERT(Group, group);
741
+ CONVERT(Enum, enum);
742
+ CONVERT(Int32, int32);
743
+ CONVERT(Int64, int64);
744
+ CONVERT(UInt32, uint32);
745
+ CONVERT(UInt64, uint64);
746
+ CONVERT(SInt32, sint32);
747
+ CONVERT(SInt64, sint64);
748
+ CONVERT(Fixed32, fixed32);
749
+ CONVERT(Fixed64, fixed64);
750
+ CONVERT(SFixed32, sfixed32);
751
+ CONVERT(SFixed64, sfixed64);
752
+ #undef CONVERT
753
+ }
754
+ return Qnil;
755
+ }
756
+
757
+ /*
758
+ * call-seq:
759
+ * FieldDescriptor.type => type
760
+ *
761
+ * Returns this field's type, as a Ruby symbol, or nil if not yet set.
762
+ *
763
+ * Valid field types are:
764
+ * :int32, :int64, :uint32, :uint64, :float, :double, :bool, :string,
765
+ * :bytes, :message.
766
+ */
767
+ static VALUE FieldDescriptor__type(VALUE _self) {
768
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
769
+ return descriptortype_to_ruby(upb_FieldDef_Type(self->fielddef));
770
+ }
771
+
772
+ /*
773
+ * call-seq:
774
+ * FieldDescriptor.default => default
775
+ *
776
+ * Returns this field's default, as a Ruby object, or nil if not yet set.
777
+ */
778
+ static VALUE FieldDescriptor_default(VALUE _self) {
779
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
780
+ const upb_FieldDef* f = self->fielddef;
781
+ upb_MessageValue default_val = upb_MessageValue_Zero();
782
+ if (upb_FieldDef_IsSubMessage(f)) {
783
+ return Qnil;
784
+ } else if (!upb_FieldDef_IsRepeated(f)) {
785
+ default_val = upb_FieldDef_Default(f);
786
+ }
787
+ return Convert_UpbToRuby(default_val, TypeInfo_get(self->fielddef), Qnil);
788
+ }
789
+
790
+ /*
791
+ * call-seq:
792
+ * FieldDescriptor.has_presence? => bool
793
+ *
794
+ * Returns whether this field tracks presence.
795
+ */
796
+ static VALUE FieldDescriptor_has_presence(VALUE _self) {
797
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
798
+ return upb_FieldDef_HasPresence(self->fielddef) ? Qtrue : Qfalse;
799
+ }
800
+
801
+ /*
802
+ * call-seq:
803
+ * FieldDescriptor.required? => bool
804
+ *
805
+ * Returns whether this is a required field.
806
+ */
807
+ static VALUE FieldDescriptor_is_required(VALUE _self) {
808
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
809
+ return upb_FieldDef_IsRequired(self->fielddef) ? Qtrue : Qfalse;
810
+ }
811
+
812
+ /*
813
+ * call-seq:
814
+ * FieldDescriptor.repeated? => bool
815
+ *
816
+ * Returns whether this is a repeated field.
817
+ */
818
+ static VALUE FieldDescriptor_is_repeated(VALUE _self) {
819
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
820
+ return upb_FieldDef_IsRepeated(self->fielddef) ? Qtrue : Qfalse;
821
+ }
822
+
823
+ /*
824
+ * call-seq:
825
+ * FieldDescriptor.is_packed? => bool
826
+ *
827
+ * Returns whether this is a repeated field that uses packed encoding.
828
+ */
829
+ static VALUE FieldDescriptor_is_packed(VALUE _self) {
830
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
831
+ return upb_FieldDef_IsPacked(self->fielddef) ? Qtrue : Qfalse;
832
+ }
833
+
834
+ /*
835
+ * call-seq:
836
+ * FieldDescriptor.json_name => json_name
837
+ *
838
+ * Returns this field's json_name, as a Ruby string, or nil if not yet set.
839
+ */
840
+ static VALUE FieldDescriptor_json_name(VALUE _self) {
841
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
842
+ const upb_FieldDef* f = self->fielddef;
843
+ const char* json_name = upb_FieldDef_JsonName(f);
844
+ return rb_str_new2(json_name);
845
+ }
846
+
847
+ /*
848
+ * DEPRECATED: Use repeated? or required? instead.
849
+ *
850
+ * call-seq:
851
+ * FieldDescriptor.label => label
852
+ *
853
+ * Returns this field's label (i.e., plurality), as a Ruby symbol.
854
+ *
855
+ * Valid field labels are:
856
+ * :optional, :repeated
857
+ */
858
+ static VALUE FieldDescriptor_label(VALUE _self) {
859
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
860
+ switch (upb_FieldDef_Label(self->fielddef)) {
861
+ #define CONVERT(upb, ruby) \
862
+ case kUpb_Label_##upb: \
863
+ return ID2SYM(rb_intern(#ruby));
864
+
865
+ CONVERT(Optional, optional);
866
+ CONVERT(Required, required);
867
+ CONVERT(Repeated, repeated);
868
+
869
+ #undef CONVERT
870
+ }
871
+
872
+ return Qnil;
873
+ }
874
+
875
+ /*
876
+ * call-seq:
877
+ * FieldDescriptor.number => number
878
+ *
879
+ * Returns the tag number for this field.
880
+ */
881
+ static VALUE FieldDescriptor_number(VALUE _self) {
882
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
883
+ return INT2NUM(upb_FieldDef_Number(self->fielddef));
884
+ }
885
+
886
+ /*
887
+ * call-seq:
888
+ * FieldDescriptor.submsg_name => submsg_name
889
+ *
890
+ * Returns the name of the message or enum type corresponding to this field, if
891
+ * it is a message or enum field (respectively), or nil otherwise. This type
892
+ * name will be resolved within the context of the pool to which the containing
893
+ * message type is added.
894
+ */
895
+ static VALUE FieldDescriptor_submsg_name(VALUE _self) {
896
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
897
+ switch (upb_FieldDef_CType(self->fielddef)) {
898
+ case kUpb_CType_Enum:
899
+ return rb_str_new2(
900
+ upb_EnumDef_FullName(upb_FieldDef_EnumSubDef(self->fielddef)));
901
+ case kUpb_CType_Message:
902
+ return rb_str_new2(
903
+ upb_MessageDef_FullName(upb_FieldDef_MessageSubDef(self->fielddef)));
904
+ default:
905
+ return Qnil;
906
+ }
907
+ }
908
+
909
+ /*
910
+ * call-seq:
911
+ * FieldDescriptor.subtype => message_or_enum_descriptor
912
+ *
913
+ * Returns the message or enum descriptor corresponding to this field's type if
914
+ * it is a message or enum field, respectively, or nil otherwise. Cannot be
915
+ * called *until* the containing message type is added to a pool (and thus
916
+ * resolved).
917
+ */
918
+ static VALUE FieldDescriptor_subtype(VALUE _self) {
919
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
920
+ switch (upb_FieldDef_CType(self->fielddef)) {
921
+ case kUpb_CType_Enum:
922
+ return get_enumdef_obj(self->descriptor_pool,
923
+ upb_FieldDef_EnumSubDef(self->fielddef));
924
+ case kUpb_CType_Message:
925
+ return get_msgdef_obj(self->descriptor_pool,
926
+ upb_FieldDef_MessageSubDef(self->fielddef));
927
+ default:
928
+ return Qnil;
929
+ }
930
+ }
931
+
932
+ /*
933
+ * call-seq:
934
+ * FieldDescriptor.get(message) => value
935
+ *
936
+ * Returns the value set for this field on the given message. Raises an
937
+ * exception if message is of the wrong type.
938
+ */
939
+ static VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
940
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
941
+ const upb_MessageDef* m;
942
+
943
+ Message_Get(msg_rb, &m);
944
+
945
+ if (m != upb_FieldDef_ContainingType(self->fielddef)) {
946
+ rb_raise(cTypeError, "get method called on wrong message type");
947
+ }
948
+
949
+ return Message_getfield(msg_rb, self->fielddef);
950
+ }
951
+
952
+ /*
953
+ * call-seq:
954
+ * FieldDescriptor.has?(message) => boolean
955
+ *
956
+ * Returns whether the value is set on the given message. Raises an
957
+ * exception when calling for fields that do not have presence.
958
+ */
959
+ static VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
960
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
961
+ const upb_MessageDef* m;
962
+ const upb_Message* msg = Message_Get(msg_rb, &m);
963
+
964
+ if (m != upb_FieldDef_ContainingType(self->fielddef)) {
965
+ rb_raise(cTypeError, "has method called on wrong message type");
966
+ } else if (!upb_FieldDef_HasPresence(self->fielddef)) {
967
+ rb_raise(rb_eArgError, "does not track presence");
968
+ }
969
+
970
+ return upb_Message_HasFieldByDef(msg, self->fielddef) ? Qtrue : Qfalse;
971
+ }
972
+
973
+ /*
974
+ * call-seq:
975
+ * FieldDescriptor.clear(message)
976
+ *
977
+ * Clears the field from the message if it's set.
978
+ */
979
+ static VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {
980
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
981
+ const upb_MessageDef* m;
982
+ upb_Message* msg = Message_GetMutable(msg_rb, &m);
983
+
984
+ if (m != upb_FieldDef_ContainingType(self->fielddef)) {
985
+ rb_raise(cTypeError, "has method called on wrong message type");
986
+ }
987
+
988
+ upb_Message_ClearFieldByDef(msg, self->fielddef);
989
+ return Qnil;
990
+ }
991
+
992
+ /*
993
+ * call-seq:
994
+ * FieldDescriptor.set(message, value)
995
+ *
996
+ * Sets the value corresponding to this field to the given value on the given
997
+ * message. Raises an exception if message is of the wrong type. Performs the
998
+ * ordinary type-checks for field setting.
999
+ */
1000
+ static VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value) {
1001
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
1002
+ const upb_MessageDef* m;
1003
+ upb_Message* msg = Message_GetMutable(msg_rb, &m);
1004
+ upb_Arena* arena = Arena_get(Message_GetArena(msg_rb));
1005
+ upb_MessageValue msgval;
1006
+
1007
+ if (m != upb_FieldDef_ContainingType(self->fielddef)) {
1008
+ rb_raise(cTypeError, "set method called on wrong message type");
1009
+ }
1010
+
1011
+ msgval = Convert_RubyToUpb(value, upb_FieldDef_Name(self->fielddef),
1012
+ TypeInfo_get(self->fielddef), arena);
1013
+ upb_Message_SetFieldByDef(msg, self->fielddef, msgval, arena);
1014
+ return Qnil;
1015
+ }
1016
+
1017
+ /*
1018
+ * call-seq:
1019
+ * FieldDescriptor.options => options
1020
+ *
1021
+ * Returns the `FieldOptions` for this `FieldDescriptor`.
1022
+ */
1023
+ static VALUE FieldDescriptor_options(VALUE _self) {
1024
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
1025
+ const google_protobuf_FieldOptions* opts =
1026
+ upb_FieldDef_Options(self->fielddef);
1027
+ upb_Arena* arena = upb_Arena_New();
1028
+ size_t size;
1029
+ char* serialized = google_protobuf_FieldOptions_serialize(opts, arena, &size);
1030
+ VALUE field_options = decode_options(_self, "FieldOptions", size, serialized,
1031
+ self->descriptor_pool);
1032
+ upb_Arena_Free(arena);
1033
+ return field_options;
1034
+ }
1035
+
1036
+ /*
1037
+ * call-seq:
1038
+ * FieldDescriptor.to_proto => FieldDescriptorProto
1039
+ *
1040
+ * Returns the `FieldDescriptorProto` of this `FieldDescriptor`.
1041
+ */
1042
+ static VALUE FieldDescriptor_to_proto(VALUE _self) {
1043
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
1044
+ upb_Arena* arena = upb_Arena_New();
1045
+ google_protobuf_FieldDescriptorProto* proto =
1046
+ upb_FieldDef_ToProto(self->fielddef, arena);
1047
+ size_t size;
1048
+ const char* serialized =
1049
+ google_protobuf_FieldDescriptorProto_serialize(proto, arena, &size);
1050
+ VALUE proto_class = rb_path2class("Google::Protobuf::FieldDescriptorProto");
1051
+ VALUE proto_rb =
1052
+ Message_decode_bytes(size, serialized, 0, proto_class, false);
1053
+ upb_Arena_Free(arena);
1054
+ return proto_rb;
1055
+ }
1056
+
1057
+ static void FieldDescriptor_register(VALUE module) {
1058
+ VALUE klass = rb_define_class_under(module, "FieldDescriptor", rb_cObject);
1059
+ rb_define_alloc_func(klass, FieldDescriptor_alloc);
1060
+ rb_define_method(klass, "initialize", FieldDescriptor_initialize, 3);
1061
+ rb_define_method(klass, "name", FieldDescriptor_name, 0);
1062
+ rb_define_method(klass, "type", FieldDescriptor__type, 0);
1063
+ rb_define_method(klass, "default", FieldDescriptor_default, 0);
1064
+ rb_define_method(klass, "has_presence?", FieldDescriptor_has_presence, 0);
1065
+ rb_define_method(klass, "required?", FieldDescriptor_is_required, 0);
1066
+ rb_define_method(klass, "repeated?", FieldDescriptor_is_repeated, 0);
1067
+ rb_define_method(klass, "is_packed?", FieldDescriptor_is_packed, 0);
1068
+ rb_define_method(klass, "json_name", FieldDescriptor_json_name, 0);
1069
+ rb_define_method(klass, "label", FieldDescriptor_label, 0);
1070
+ rb_define_method(klass, "number", FieldDescriptor_number, 0);
1071
+ rb_define_method(klass, "submsg_name", FieldDescriptor_submsg_name, 0);
1072
+ rb_define_method(klass, "subtype", FieldDescriptor_subtype, 0);
1073
+ rb_define_method(klass, "has?", FieldDescriptor_has, 1);
1074
+ rb_define_method(klass, "clear", FieldDescriptor_clear, 1);
1075
+ rb_define_method(klass, "get", FieldDescriptor_get, 1);
1076
+ rb_define_method(klass, "set", FieldDescriptor_set, 2);
1077
+ rb_define_method(klass, "options", FieldDescriptor_options, 0);
1078
+ rb_define_method(klass, "to_proto", FieldDescriptor_to_proto, 0);
1079
+ rb_gc_register_address(&cFieldDescriptor);
1080
+ cFieldDescriptor = klass;
1081
+ }
1082
+
1083
+ // -----------------------------------------------------------------------------
1084
+ // OneofDescriptor.
1085
+ // -----------------------------------------------------------------------------
1086
+
1087
+ typedef struct {
1088
+ const upb_OneofDef* oneofdef;
1089
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
1090
+ // macro to update VALUE references, as to trigger write barriers.
1091
+ VALUE descriptor_pool; // Owns the upb_OneofDef.
1092
+ } OneofDescriptor;
1093
+
1094
+ static VALUE cOneofDescriptor = Qnil;
1095
+
1096
+ static void OneofDescriptor_mark(void* _self) {
1097
+ OneofDescriptor* self = _self;
1098
+ rb_gc_mark(self->descriptor_pool);
1099
+ }
1100
+
1101
+ static const rb_data_type_t OneofDescriptor_type = {
1102
+ "Google::Protobuf::OneofDescriptor",
1103
+ {OneofDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
1104
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
1105
+ };
1106
+
1107
+ static OneofDescriptor* ruby_to_OneofDescriptor(VALUE val) {
1108
+ OneofDescriptor* ret;
1109
+ TypedData_Get_Struct(val, OneofDescriptor, &OneofDescriptor_type, ret);
1110
+ return ret;
1111
+ }
1112
+
1113
+ /*
1114
+ * call-seq:
1115
+ * OneofDescriptor.new => oneof_descriptor
1116
+ *
1117
+ * Creates a new, empty, oneof descriptor. The oneof may only be modified prior
1118
+ * to being added to a message descriptor which is subsequently added to a pool.
1119
+ */
1120
+ static VALUE OneofDescriptor_alloc(VALUE klass) {
1121
+ OneofDescriptor* self = ALLOC(OneofDescriptor);
1122
+ VALUE ret = TypedData_Wrap_Struct(klass, &OneofDescriptor_type, self);
1123
+ self->oneofdef = NULL;
1124
+ self->descriptor_pool = Qnil;
1125
+ return ret;
1126
+ }
1127
+
1128
+ /*
1129
+ * call-seq:
1130
+ * OneofDescriptor.new(c_only_cookie, pool, ptr) => OneofDescriptor
1131
+ *
1132
+ * Creates a descriptor wrapper object. May only be called from C.
1133
+ */
1134
+ static VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
1135
+ VALUE descriptor_pool, VALUE ptr) {
1136
+ OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
1137
+
1138
+ if (cookie != c_only_cookie) {
1139
+ rb_raise(rb_eRuntimeError,
1140
+ "Descriptor objects may not be created from Ruby.");
1141
+ }
1142
+
1143
+ RB_OBJ_WRITE(_self, &self->descriptor_pool, descriptor_pool);
1144
+ self->oneofdef = (const upb_OneofDef*)NUM2ULL(ptr);
1145
+
1146
+ return Qnil;
1147
+ }
1148
+
1149
+ /*
1150
+ * call-seq:
1151
+ * OneofDescriptor.name => name
1152
+ *
1153
+ * Returns the name of this oneof.
1154
+ */
1155
+ static VALUE OneofDescriptor_name(VALUE _self) {
1156
+ OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
1157
+ return rb_str_maybe_null(upb_OneofDef_Name(self->oneofdef));
1158
+ }
1159
+
1160
+ /*
1161
+ * call-seq:
1162
+ * OneofDescriptor.each(&block) => nil
1163
+ *
1164
+ * Iterates through fields in this oneof, yielding to the block on each one.
1165
+ */
1166
+ static VALUE OneofDescriptor_each(VALUE _self) {
1167
+ OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
1168
+
1169
+ int n = upb_OneofDef_FieldCount(self->oneofdef);
1170
+ for (int i = 0; i < n; i++) {
1171
+ const upb_FieldDef* f = upb_OneofDef_Field(self->oneofdef, i);
1172
+ VALUE obj = get_fielddef_obj(self->descriptor_pool, f);
1173
+ rb_yield(obj);
1174
+ }
1175
+ return Qnil;
1176
+ }
1177
+
1178
+ /*
1179
+ * call-seq:
1180
+ * OneofDescriptor.options => options
1181
+ *
1182
+ * Returns the `OneofOptions` for this `OneofDescriptor`.
1183
+ */
1184
+ static VALUE OneOfDescriptor_options(VALUE _self) {
1185
+ OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
1186
+ const google_protobuf_OneofOptions* opts =
1187
+ upb_OneofDef_Options(self->oneofdef);
1188
+ upb_Arena* arena = upb_Arena_New();
1189
+ size_t size;
1190
+ char* serialized = google_protobuf_OneofOptions_serialize(opts, arena, &size);
1191
+ VALUE oneof_options = decode_options(_self, "OneofOptions", size, serialized,
1192
+ self->descriptor_pool);
1193
+ upb_Arena_Free(arena);
1194
+ return oneof_options;
1195
+ }
1196
+
1197
+ /*
1198
+ * call-seq:
1199
+ * OneofDescriptor.to_proto => OneofDescriptorProto
1200
+ *
1201
+ * Returns the `OneofDescriptorProto` of this `OneofDescriptor`.
1202
+ */
1203
+ static VALUE OneOfDescriptor_to_proto(VALUE _self) {
1204
+ OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
1205
+ upb_Arena* arena = upb_Arena_New();
1206
+ google_protobuf_OneofDescriptorProto* proto =
1207
+ upb_OneofDef_ToProto(self->oneofdef, arena);
1208
+ size_t size;
1209
+ const char* serialized =
1210
+ google_protobuf_OneofDescriptorProto_serialize(proto, arena, &size);
1211
+ VALUE proto_class = rb_path2class("Google::Protobuf::OneofDescriptorProto");
1212
+ VALUE proto_rb =
1213
+ Message_decode_bytes(size, serialized, 0, proto_class, false);
1214
+ upb_Arena_Free(arena);
1215
+ return proto_rb;
1216
+ }
1217
+
1218
+ static void OneofDescriptor_register(VALUE module) {
1219
+ VALUE klass = rb_define_class_under(module, "OneofDescriptor", rb_cObject);
1220
+ rb_define_alloc_func(klass, OneofDescriptor_alloc);
1221
+ rb_define_method(klass, "initialize", OneofDescriptor_initialize, 3);
1222
+ rb_define_method(klass, "name", OneofDescriptor_name, 0);
1223
+ rb_define_method(klass, "each", OneofDescriptor_each, 0);
1224
+ rb_define_method(klass, "options", OneOfDescriptor_options, 0);
1225
+ rb_define_method(klass, "to_proto", OneOfDescriptor_to_proto, 0);
1226
+ rb_include_module(klass, rb_mEnumerable);
1227
+ rb_gc_register_address(&cOneofDescriptor);
1228
+ cOneofDescriptor = klass;
1229
+ }
1230
+
1231
+ // -----------------------------------------------------------------------------
1232
+ // EnumDescriptor.
1233
+ // -----------------------------------------------------------------------------
1234
+
1235
+ typedef struct {
1236
+ const upb_EnumDef* enumdef;
1237
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
1238
+ // macro to update VALUE references, as to trigger write barriers.
1239
+ VALUE module; // begins as nil
1240
+ VALUE descriptor_pool; // Owns the upb_EnumDef.
1241
+ } EnumDescriptor;
1242
+
1243
+ static VALUE cEnumDescriptor = Qnil;
1244
+
1245
+ static void EnumDescriptor_mark(void* _self) {
1246
+ EnumDescriptor* self = _self;
1247
+ rb_gc_mark(self->module);
1248
+ rb_gc_mark(self->descriptor_pool);
1249
+ }
1250
+
1251
+ static const rb_data_type_t EnumDescriptor_type = {
1252
+ "Google::Protobuf::EnumDescriptor",
1253
+ {EnumDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
1254
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
1255
+ };
1256
+
1257
+ static EnumDescriptor* ruby_to_EnumDescriptor(VALUE val) {
1258
+ EnumDescriptor* ret;
1259
+ TypedData_Get_Struct(val, EnumDescriptor, &EnumDescriptor_type, ret);
1260
+ return ret;
1261
+ }
1262
+
1263
+ static VALUE EnumDescriptor_alloc(VALUE klass) {
1264
+ EnumDescriptor* self = ALLOC(EnumDescriptor);
1265
+ VALUE ret = TypedData_Wrap_Struct(klass, &EnumDescriptor_type, self);
1266
+ self->enumdef = NULL;
1267
+ self->module = Qnil;
1268
+ self->descriptor_pool = Qnil;
1269
+ return ret;
1270
+ }
1271
+
1272
+ // Exposed to other modules in defs.h.
1273
+ const upb_EnumDef* EnumDescriptor_GetEnumDef(VALUE enum_desc_rb) {
1274
+ EnumDescriptor* desc = ruby_to_EnumDescriptor(enum_desc_rb);
1275
+ return desc->enumdef;
1276
+ }
1277
+
1278
+ /*
1279
+ * call-seq:
1280
+ * EnumDescriptor.new(c_only_cookie, ptr) => EnumDescriptor
1281
+ *
1282
+ * Creates a descriptor wrapper object. May only be called from C.
1283
+ */
1284
+ static VALUE EnumDescriptor_initialize(VALUE _self, VALUE cookie,
1285
+ VALUE descriptor_pool, VALUE ptr) {
1286
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1287
+
1288
+ if (cookie != c_only_cookie) {
1289
+ rb_raise(rb_eRuntimeError,
1290
+ "Descriptor objects may not be created from Ruby.");
1291
+ }
1292
+
1293
+ RB_OBJ_WRITE(_self, &self->descriptor_pool, descriptor_pool);
1294
+ self->enumdef = (const upb_EnumDef*)NUM2ULL(ptr);
1295
+
1296
+ return Qnil;
1297
+ }
1298
+
1299
+ /*
1300
+ * call-seq:
1301
+ * EnumDescriptor.file_descriptor
1302
+ *
1303
+ * Returns the FileDescriptor object this enum belongs to.
1304
+ */
1305
+ static VALUE EnumDescriptor_file_descriptor(VALUE _self) {
1306
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1307
+ return get_filedef_obj(self->descriptor_pool,
1308
+ upb_EnumDef_File(self->enumdef));
1309
+ }
1310
+
1311
+ /*
1312
+ * call-seq:
1313
+ * EnumDescriptor.is_closed? => bool
1314
+ *
1315
+ * Returns whether this enum is open or closed.
1316
+ */
1317
+ static VALUE EnumDescriptor_is_closed(VALUE _self) {
1318
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1319
+ return upb_EnumDef_IsClosed(self->enumdef) ? Qtrue : Qfalse;
1320
+ }
1321
+
1322
+ /*
1323
+ * call-seq:
1324
+ * EnumDescriptor.name => name
1325
+ *
1326
+ * Returns the name of this enum type.
1327
+ */
1328
+ static VALUE EnumDescriptor_name(VALUE _self) {
1329
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1330
+ return rb_str_maybe_null(upb_EnumDef_FullName(self->enumdef));
1331
+ }
1332
+
1333
+ /*
1334
+ * call-seq:
1335
+ * EnumDescriptor.lookup_name(name) => value
1336
+ *
1337
+ * Returns the numeric value corresponding to the given key name (as a Ruby
1338
+ * symbol), or nil if none.
1339
+ */
1340
+ static VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
1341
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1342
+ const char* name_str = rb_id2name(SYM2ID(name));
1343
+ const upb_EnumValueDef* ev =
1344
+ upb_EnumDef_FindValueByName(self->enumdef, name_str);
1345
+ if (ev) {
1346
+ return INT2NUM(upb_EnumValueDef_Number(ev));
1347
+ } else {
1348
+ return Qnil;
1349
+ }
1350
+ }
1351
+
1352
+ /*
1353
+ * call-seq:
1354
+ * EnumDescriptor.lookup_value(name) => value
1355
+ *
1356
+ * Returns the key name (as a Ruby symbol) corresponding to the integer value,
1357
+ * or nil if none.
1358
+ */
1359
+ static VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
1360
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1361
+ int32_t val = NUM2INT(number);
1362
+ const upb_EnumValueDef* ev =
1363
+ upb_EnumDef_FindValueByNumber(self->enumdef, val);
1364
+ if (ev) {
1365
+ return ID2SYM(rb_intern(upb_EnumValueDef_Name(ev)));
1366
+ } else {
1367
+ return Qnil;
1368
+ }
1369
+ }
1370
+
1371
+ /*
1372
+ * call-seq:
1373
+ * EnumDescriptor.each(&block)
1374
+ *
1375
+ * Iterates over key => value mappings in this enum's definition, yielding to
1376
+ * the block with (key, value) arguments for each one.
1377
+ */
1378
+ static VALUE EnumDescriptor_each(VALUE _self) {
1379
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1380
+
1381
+ int n = upb_EnumDef_ValueCount(self->enumdef);
1382
+ for (int i = 0; i < n; i++) {
1383
+ const upb_EnumValueDef* ev = upb_EnumDef_Value(self->enumdef, i);
1384
+ VALUE key = ID2SYM(rb_intern(upb_EnumValueDef_Name(ev)));
1385
+ VALUE number = INT2NUM(upb_EnumValueDef_Number(ev));
1386
+ rb_yield_values(2, key, number);
1387
+ }
1388
+
1389
+ return Qnil;
1390
+ }
1391
+
1392
+ /*
1393
+ * call-seq:
1394
+ * EnumDescriptor.enummodule => module
1395
+ *
1396
+ * Returns the Ruby module corresponding to this enum type.
1397
+ */
1398
+ static VALUE EnumDescriptor_enummodule(VALUE _self) {
1399
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1400
+ if (self->module == Qnil) {
1401
+ RB_OBJ_WRITE(_self, &self->module, build_module_from_enumdesc(_self));
1402
+ }
1403
+ return self->module;
1404
+ }
1405
+
1406
+ /*
1407
+ * call-seq:
1408
+ * EnumDescriptor.options => options
1409
+ *
1410
+ * Returns the `EnumOptions` for this `EnumDescriptor`.
1411
+ */
1412
+ static VALUE EnumDescriptor_options(VALUE _self) {
1413
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1414
+ const google_protobuf_EnumOptions* opts = upb_EnumDef_Options(self->enumdef);
1415
+ upb_Arena* arena = upb_Arena_New();
1416
+ size_t size;
1417
+ char* serialized = google_protobuf_EnumOptions_serialize(opts, arena, &size);
1418
+ VALUE enum_options = decode_options(_self, "EnumOptions", size, serialized,
1419
+ self->descriptor_pool);
1420
+ upb_Arena_Free(arena);
1421
+ return enum_options;
1422
+ }
1423
+
1424
+ /*
1425
+ * call-seq:
1426
+ * EnumDescriptor.to_proto => EnumDescriptorProto
1427
+ *
1428
+ * Returns the `EnumDescriptorProto` of this `EnumDescriptor`.
1429
+ */
1430
+ static VALUE EnumDescriptor_to_proto(VALUE _self) {
1431
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1432
+ upb_Arena* arena = upb_Arena_New();
1433
+ google_protobuf_EnumDescriptorProto* proto =
1434
+ upb_EnumDef_ToProto(self->enumdef, arena);
1435
+
1436
+ size_t size;
1437
+ const char* serialized =
1438
+ google_protobuf_EnumDescriptorProto_serialize(proto, arena, &size);
1439
+
1440
+ VALUE proto_class = rb_path2class("Google::Protobuf::EnumDescriptorProto");
1441
+ VALUE proto_rb =
1442
+ Message_decode_bytes(size, serialized, 0, proto_class, false);
1443
+ upb_Arena_Free(arena);
1444
+ return proto_rb;
1445
+ }
1446
+
1447
+ static void EnumDescriptor_register(VALUE module) {
1448
+ VALUE klass = rb_define_class_under(module, "EnumDescriptor", rb_cObject);
1449
+ rb_define_alloc_func(klass, EnumDescriptor_alloc);
1450
+ rb_define_method(klass, "initialize", EnumDescriptor_initialize, 3);
1451
+ rb_define_method(klass, "name", EnumDescriptor_name, 0);
1452
+ rb_define_method(klass, "lookup_name", EnumDescriptor_lookup_name, 1);
1453
+ rb_define_method(klass, "lookup_value", EnumDescriptor_lookup_value, 1);
1454
+ rb_define_method(klass, "each", EnumDescriptor_each, 0);
1455
+ rb_define_method(klass, "enummodule", EnumDescriptor_enummodule, 0);
1456
+ rb_define_method(klass, "file_descriptor", EnumDescriptor_file_descriptor, 0);
1457
+ rb_define_method(klass, "is_closed?", EnumDescriptor_is_closed, 0);
1458
+ rb_define_method(klass, "options", EnumDescriptor_options, 0);
1459
+ rb_define_method(klass, "to_proto", EnumDescriptor_to_proto, 0);
1460
+ rb_include_module(klass, rb_mEnumerable);
1461
+ rb_gc_register_address(&cEnumDescriptor);
1462
+ cEnumDescriptor = klass;
1463
+ }
1464
+
1465
+ // -----------------------------------------------------------------------------
1466
+ // ServiceDescriptor
1467
+ // -----------------------------------------------------------------------------
1468
+
1469
+ typedef struct {
1470
+ const upb_ServiceDef* servicedef;
1471
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
1472
+ // macro to update VALUE references, as to trigger write barriers.
1473
+ VALUE module; // begins as nil
1474
+ VALUE descriptor_pool; // Owns the upb_ServiceDef.
1475
+ } ServiceDescriptor;
1476
+
1477
+ static VALUE cServiceDescriptor = Qnil;
1478
+
1479
+ static void ServiceDescriptor_mark(void* _self) {
1480
+ ServiceDescriptor* self = _self;
1481
+ rb_gc_mark(self->module);
1482
+ rb_gc_mark(self->descriptor_pool);
1483
+ }
1484
+
1485
+ static const rb_data_type_t ServiceDescriptor_type = {
1486
+ "Google::Protobuf::ServicDescriptor",
1487
+ {ServiceDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
1488
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
1489
+ };
1490
+
1491
+ static ServiceDescriptor* ruby_to_ServiceDescriptor(VALUE val) {
1492
+ ServiceDescriptor* ret;
1493
+ TypedData_Get_Struct(val, ServiceDescriptor, &ServiceDescriptor_type, ret);
1494
+ return ret;
1495
+ }
1496
+
1497
+ static VALUE ServiceDescriptor_alloc(VALUE klass) {
1498
+ ServiceDescriptor* self = ALLOC(ServiceDescriptor);
1499
+ VALUE ret = TypedData_Wrap_Struct(klass, &ServiceDescriptor_type, self);
1500
+ self->servicedef = NULL;
1501
+ self->module = Qnil;
1502
+ self->descriptor_pool = Qnil;
1503
+ return ret;
1504
+ }
1505
+
1506
+ /*
1507
+ * call-seq:
1508
+ * ServiceDescriptor.new(c_only_cookie, ptr) => ServiceDescriptor
1509
+ *
1510
+ * Creates a descriptor wrapper object. May only be called from C.
1511
+ */
1512
+ static VALUE ServiceDescriptor_initialize(VALUE _self, VALUE cookie,
1513
+ VALUE descriptor_pool, VALUE ptr) {
1514
+ ServiceDescriptor* self = ruby_to_ServiceDescriptor(_self);
1515
+
1516
+ if (cookie != c_only_cookie) {
1517
+ rb_raise(rb_eRuntimeError,
1518
+ "Descriptor objects may not be created from Ruby.");
1519
+ }
1520
+
1521
+ RB_OBJ_WRITE(_self, &self->descriptor_pool, descriptor_pool);
1522
+ self->servicedef = (const upb_ServiceDef*)NUM2ULL(ptr);
1523
+
1524
+ return Qnil;
1525
+ }
1526
+
1527
+ /*
1528
+ * call-seq:
1529
+ * ServiceDescriptor.name => name
1530
+ *
1531
+ * Returns the name of this service.
1532
+ */
1533
+ static VALUE ServiceDescriptor_name(VALUE _self) {
1534
+ ServiceDescriptor* self = ruby_to_ServiceDescriptor(_self);
1535
+ return rb_str_maybe_null(upb_ServiceDef_FullName(self->servicedef));
1536
+ }
1537
+
1538
+ /*
1539
+ * call-seq:
1540
+ * ServiceDescriptor.file_descriptor
1541
+ *
1542
+ * Returns the FileDescriptor object this service belongs to.
1543
+ */
1544
+ static VALUE ServiceDescriptor_file_descriptor(VALUE _self) {
1545
+ ServiceDescriptor* self = ruby_to_ServiceDescriptor(_self);
1546
+ return get_filedef_obj(self->descriptor_pool,
1547
+ upb_ServiceDef_File(self->servicedef));
1548
+ }
1549
+
1550
+ /*
1551
+ * call-seq:
1552
+ * ServiceDescriptor.each(&block)
1553
+ *
1554
+ * Iterates over methods in this service, yielding to the block on each one.
1555
+ */
1556
+ static VALUE ServiceDescriptor_each(VALUE _self) {
1557
+ ServiceDescriptor* self = ruby_to_ServiceDescriptor(_self);
1558
+
1559
+ int n = upb_ServiceDef_MethodCount(self->servicedef);
1560
+ for (int i = 0; i < n; i++) {
1561
+ const upb_MethodDef* method = upb_ServiceDef_Method(self->servicedef, i);
1562
+ VALUE obj = get_methoddef_obj(self->descriptor_pool, method);
1563
+ rb_yield(obj);
1564
+ }
1565
+ return Qnil;
1566
+ }
1567
+
1568
+ /*
1569
+ * call-seq:
1570
+ * ServiceDescriptor.options => options
1571
+ *
1572
+ * Returns the `ServiceOptions` for this `ServiceDescriptor`.
1573
+ */
1574
+ static VALUE ServiceDescriptor_options(VALUE _self) {
1575
+ ServiceDescriptor* self = ruby_to_ServiceDescriptor(_self);
1576
+ const google_protobuf_ServiceOptions* opts =
1577
+ upb_ServiceDef_Options(self->servicedef);
1578
+ upb_Arena* arena = upb_Arena_New();
1579
+ size_t size;
1580
+ char* serialized =
1581
+ google_protobuf_ServiceOptions_serialize(opts, arena, &size);
1582
+ VALUE service_options = decode_options(_self, "ServiceOptions", size,
1583
+ serialized, self->descriptor_pool);
1584
+ upb_Arena_Free(arena);
1585
+ return service_options;
1586
+ }
1587
+
1588
+ /*
1589
+ * call-seq:
1590
+ * ServiceDescriptor.to_proto => ServiceDescriptorProto
1591
+ *
1592
+ * Returns the `ServiceDescriptorProto` of this `ServiceDescriptor`.
1593
+ */
1594
+ static VALUE ServiceDescriptor_to_proto(VALUE _self) {
1595
+ ServiceDescriptor* self = ruby_to_ServiceDescriptor(_self);
1596
+ upb_Arena* arena = upb_Arena_New();
1597
+ google_protobuf_ServiceDescriptorProto* proto =
1598
+ upb_ServiceDef_ToProto(self->servicedef, arena);
1599
+ size_t size;
1600
+ const char* serialized =
1601
+ google_protobuf_ServiceDescriptorProto_serialize(proto, arena, &size);
1602
+ VALUE proto_class = rb_path2class("Google::Protobuf::ServiceDescriptorProto");
1603
+ VALUE proto_rb =
1604
+ Message_decode_bytes(size, serialized, 0, proto_class, false);
1605
+ upb_Arena_Free(arena);
1606
+ return proto_rb;
1607
+ }
1608
+
1609
+ static void ServiceDescriptor_register(VALUE module) {
1610
+ VALUE klass = rb_define_class_under(module, "ServiceDescriptor", rb_cObject);
1611
+ rb_define_alloc_func(klass, ServiceDescriptor_alloc);
1612
+ rb_define_method(klass, "initialize", ServiceDescriptor_initialize, 3);
1613
+ rb_define_method(klass, "name", ServiceDescriptor_name, 0);
1614
+ rb_define_method(klass, "each", ServiceDescriptor_each, 0);
1615
+ rb_define_method(klass, "file_descriptor", ServiceDescriptor_file_descriptor,
1616
+ 0);
1617
+ rb_define_method(klass, "options", ServiceDescriptor_options, 0);
1618
+ rb_define_method(klass, "to_proto", ServiceDescriptor_to_proto, 0);
1619
+ rb_include_module(klass, rb_mEnumerable);
1620
+ rb_gc_register_address(&cServiceDescriptor);
1621
+ cServiceDescriptor = klass;
1622
+ }
1623
+
1624
+ // -----------------------------------------------------------------------------
1625
+ // MethodDescriptor
1626
+ // -----------------------------------------------------------------------------
1627
+
1628
+ typedef struct {
1629
+ const upb_MethodDef* methoddef;
1630
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
1631
+ // macro to update VALUE references, as to trigger write barriers.
1632
+ VALUE module; // begins as nil
1633
+ VALUE descriptor_pool; // Owns the upb_MethodDef.
1634
+ } MethodDescriptor;
1635
+
1636
+ static VALUE cMethodDescriptor = Qnil;
1637
+
1638
+ static void MethodDescriptor_mark(void* _self) {
1639
+ MethodDescriptor* self = _self;
1640
+ rb_gc_mark(self->module);
1641
+ rb_gc_mark(self->descriptor_pool);
1642
+ }
1643
+
1644
+ static const rb_data_type_t MethodDescriptor_type = {
1645
+ "Google::Protobuf::MethodDescriptor",
1646
+ {MethodDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
1647
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
1648
+ };
1649
+
1650
+ static MethodDescriptor* ruby_to_MethodDescriptor(VALUE val) {
1651
+ MethodDescriptor* ret;
1652
+ TypedData_Get_Struct(val, MethodDescriptor, &MethodDescriptor_type, ret);
1653
+ return ret;
1654
+ }
1655
+
1656
+ static VALUE MethodDescriptor_alloc(VALUE klass) {
1657
+ MethodDescriptor* self = ALLOC(MethodDescriptor);
1658
+ VALUE ret = TypedData_Wrap_Struct(klass, &MethodDescriptor_type, self);
1659
+ self->methoddef = NULL;
1660
+ self->module = Qnil;
1661
+ self->descriptor_pool = Qnil;
1662
+ return ret;
1663
+ }
1664
+
1665
+ /*
1666
+ * call-seq:
1667
+ * MethodDescriptor.new(c_only_cookie, ptr) => MethodDescriptor
1668
+ *
1669
+ * Creates a descriptor wrapper object. May only be called from C.
1670
+ */
1671
+ static VALUE MethodDescriptor_initialize(VALUE _self, VALUE cookie,
1672
+ VALUE descriptor_pool, VALUE ptr) {
1673
+ MethodDescriptor* self = ruby_to_MethodDescriptor(_self);
1674
+
1675
+ if (cookie != c_only_cookie) {
1676
+ rb_raise(rb_eRuntimeError,
1677
+ "Descriptor objects may not be created from Ruby.");
1678
+ }
1679
+
1680
+ RB_OBJ_WRITE(_self, &self->descriptor_pool, descriptor_pool);
1681
+ self->methoddef = (const upb_MethodDef*)NUM2ULL(ptr);
1682
+
1683
+ return Qnil;
1684
+ }
1685
+
1686
+ /*
1687
+ * call-seq:
1688
+ * MethodDescriptor.name => name
1689
+ *
1690
+ * Returns the name of this method
1691
+ */
1692
+ static VALUE MethodDescriptor_name(VALUE _self) {
1693
+ MethodDescriptor* self = ruby_to_MethodDescriptor(_self);
1694
+ return rb_str_maybe_null(upb_MethodDef_Name(self->methoddef));
1695
+ }
1696
+
1697
+ /*
1698
+ * call-seq:
1699
+ * MethodDescriptor.options => options
1700
+ *
1701
+ * Returns the `MethodOptions` for this `MethodDescriptor`.
1702
+ */
1703
+ static VALUE MethodDescriptor_options(VALUE _self) {
1704
+ MethodDescriptor* self = ruby_to_MethodDescriptor(_self);
1705
+ const google_protobuf_MethodOptions* opts =
1706
+ upb_MethodDef_Options(self->methoddef);
1707
+ upb_Arena* arena = upb_Arena_New();
1708
+ size_t size;
1709
+ char* serialized =
1710
+ google_protobuf_MethodOptions_serialize(opts, arena, &size);
1711
+ VALUE method_options = decode_options(_self, "MethodOptions", size,
1712
+ serialized, self->descriptor_pool);
1713
+ upb_Arena_Free(arena);
1714
+ return method_options;
1715
+ }
1716
+
1717
+ /*
1718
+ * call-seq:
1719
+ * MethodDescriptor.input_type => Descriptor
1720
+ *
1721
+ * Returns the `Descriptor` for the request message type of this method
1722
+ */
1723
+ static VALUE MethodDescriptor_input_type(VALUE _self) {
1724
+ MethodDescriptor* self = ruby_to_MethodDescriptor(_self);
1725
+ const upb_MessageDef* type = upb_MethodDef_InputType(self->methoddef);
1726
+ return get_msgdef_obj(self->descriptor_pool, type);
1727
+ }
1728
+
1729
+ /*
1730
+ * call-seq:
1731
+ * MethodDescriptor.output_type => Descriptor
1732
+ *
1733
+ * Returns the `Descriptor` for the response message type of this method
1734
+ */
1735
+ static VALUE MethodDescriptor_output_type(VALUE _self) {
1736
+ MethodDescriptor* self = ruby_to_MethodDescriptor(_self);
1737
+ const upb_MessageDef* type = upb_MethodDef_OutputType(self->methoddef);
1738
+ return get_msgdef_obj(self->descriptor_pool, type);
1739
+ }
1740
+
1741
+ /*
1742
+ * call-seq:
1743
+ * MethodDescriptor.client_streaming => bool
1744
+ *
1745
+ * Returns whether or not this is a streaming request method
1746
+ */
1747
+ static VALUE MethodDescriptor_client_streaming(VALUE _self) {
1748
+ MethodDescriptor* self = ruby_to_MethodDescriptor(_self);
1749
+ return upb_MethodDef_ClientStreaming(self->methoddef) ? Qtrue : Qfalse;
1750
+ }
1751
+
1752
+ /*
1753
+ * call-seq:
1754
+ * MethodDescriptor.to_proto => MethodDescriptorProto
1755
+ *
1756
+ * Returns the `MethodDescriptorProto` of this `MethodDescriptor`.
1757
+ */
1758
+ static VALUE MethodDescriptor_to_proto(VALUE _self) {
1759
+ MethodDescriptor* self = ruby_to_MethodDescriptor(_self);
1760
+ upb_Arena* arena = upb_Arena_New();
1761
+ google_protobuf_MethodDescriptorProto* proto =
1762
+ upb_MethodDef_ToProto(self->methoddef, arena);
1763
+ size_t size;
1764
+ const char* serialized =
1765
+ google_protobuf_MethodDescriptorProto_serialize(proto, arena, &size);
1766
+ VALUE proto_class = rb_path2class("Google::Protobuf::MethodDescriptorProto");
1767
+ VALUE proto_rb =
1768
+ Message_decode_bytes(size, serialized, 0, proto_class, false);
1769
+ upb_Arena_Free(arena);
1770
+ return proto_rb;
1771
+ }
1772
+
1773
+ /*
1774
+ * call-seq:
1775
+ * MethodDescriptor.server_streaming => bool
1776
+ *
1777
+ * Returns whether or not this is a streaming response method
1778
+ */
1779
+ static VALUE MethodDescriptor_server_streaming(VALUE _self) {
1780
+ MethodDescriptor* self = ruby_to_MethodDescriptor(_self);
1781
+ return upb_MethodDef_ServerStreaming(self->methoddef) ? Qtrue : Qfalse;
1782
+ }
1783
+
1784
+ static void MethodDescriptor_register(VALUE module) {
1785
+ VALUE klass = rb_define_class_under(module, "MethodDescriptor", rb_cObject);
1786
+ rb_define_alloc_func(klass, MethodDescriptor_alloc);
1787
+ rb_define_method(klass, "initialize", MethodDescriptor_initialize, 3);
1788
+ rb_define_method(klass, "name", MethodDescriptor_name, 0);
1789
+ rb_define_method(klass, "options", MethodDescriptor_options, 0);
1790
+ rb_define_method(klass, "input_type", MethodDescriptor_input_type, 0);
1791
+ rb_define_method(klass, "output_type", MethodDescriptor_output_type, 0);
1792
+ rb_define_method(klass, "client_streaming", MethodDescriptor_client_streaming,
1793
+ 0);
1794
+ rb_define_method(klass, "server_streaming", MethodDescriptor_server_streaming,
1795
+ 0);
1796
+ rb_define_method(klass, "to_proto", MethodDescriptor_to_proto, 0);
1797
+ rb_gc_register_address(&cMethodDescriptor);
1798
+ cMethodDescriptor = klass;
1799
+ }
1800
+
1801
+ static VALUE get_def_obj(VALUE _descriptor_pool, const void* ptr, VALUE klass) {
1802
+ DescriptorPool* descriptor_pool = ruby_to_DescriptorPool(_descriptor_pool);
1803
+ VALUE key = ULL2NUM((intptr_t)ptr);
1804
+ VALUE def;
1805
+
1806
+ def = rb_hash_aref(descriptor_pool->def_to_descriptor, key);
1807
+
1808
+ if (ptr == NULL) {
1809
+ return Qnil;
1810
+ }
1811
+
1812
+ if (def == Qnil) {
1813
+ // Lazily create wrapper object.
1814
+ VALUE args[3] = {c_only_cookie, _descriptor_pool, key};
1815
+ def = rb_class_new_instance(3, args, klass);
1816
+ rb_hash_aset(descriptor_pool->def_to_descriptor, key, def);
1817
+ }
1818
+
1819
+ return def;
1820
+ }
1821
+
1822
+ static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_MessageDef* def) {
1823
+ return get_def_obj(descriptor_pool, def, cDescriptor);
1824
+ }
1825
+
1826
+ static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_EnumDef* def) {
1827
+ return get_def_obj(descriptor_pool, def, cEnumDescriptor);
1828
+ }
1829
+
1830
+ static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_FieldDef* def) {
1831
+ return get_def_obj(descriptor_pool, def, cFieldDescriptor);
1832
+ }
1833
+
1834
+ static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_FileDef* def) {
1835
+ return get_def_obj(descriptor_pool, def, cFileDescriptor);
1836
+ }
1837
+
1838
+ static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_OneofDef* def) {
1839
+ return get_def_obj(descriptor_pool, def, cOneofDescriptor);
1840
+ }
1841
+
1842
+ static VALUE get_servicedef_obj(VALUE descriptor_pool,
1843
+ const upb_ServiceDef* def) {
1844
+ return get_def_obj(descriptor_pool, def, cServiceDescriptor);
1845
+ }
1846
+
1847
+ static VALUE get_methoddef_obj(VALUE descriptor_pool,
1848
+ const upb_MethodDef* def) {
1849
+ return get_def_obj(descriptor_pool, def, cMethodDescriptor);
1850
+ }
1851
+
1852
+ // -----------------------------------------------------------------------------
1853
+ // Shared functions
1854
+ // -----------------------------------------------------------------------------
1855
+
1856
+ // Functions exposed to other modules in defs.h.
1857
+
1858
+ VALUE Descriptor_DefToClass(const upb_MessageDef* m) {
1859
+ const upb_DefPool* symtab = upb_FileDef_Pool(upb_MessageDef_File(m));
1860
+ VALUE pool = ObjectCache_Get(symtab);
1861
+ PBRUBY_ASSERT(pool != Qnil);
1862
+ VALUE desc_rb = get_msgdef_obj(pool, m);
1863
+ const Descriptor* desc = ruby_to_Descriptor(desc_rb);
1864
+ return desc->klass;
1865
+ }
1866
+
1867
+ const upb_MessageDef* Descriptor_GetMsgDef(VALUE desc_rb) {
1868
+ const Descriptor* desc = ruby_to_Descriptor(desc_rb);
1869
+ return desc->msgdef;
1870
+ }
1871
+
1872
+ VALUE TypeInfo_InitArg(int argc, VALUE* argv, int skip_arg) {
1873
+ if (argc > skip_arg) {
1874
+ if (argc > 1 + skip_arg) {
1875
+ rb_raise(rb_eArgError, "Expected a maximum of %d arguments.",
1876
+ skip_arg + 1);
1877
+ }
1878
+ return argv[skip_arg];
1879
+ } else {
1880
+ return Qnil;
1881
+ }
1882
+ }
1883
+
1884
+ TypeInfo TypeInfo_FromClass(int argc, VALUE* argv, int skip_arg,
1885
+ VALUE* type_class, VALUE* init_arg) {
1886
+ TypeInfo ret = {ruby_to_fieldtype(argv[skip_arg])};
1887
+
1888
+ if (ret.type == kUpb_CType_Message || ret.type == kUpb_CType_Enum) {
1889
+ *init_arg = TypeInfo_InitArg(argc, argv, skip_arg + 2);
1890
+
1891
+ if (argc < 2 + skip_arg) {
1892
+ rb_raise(rb_eArgError, "Expected at least %d arguments for message/enum.",
1893
+ 2 + skip_arg);
1894
+ }
1895
+
1896
+ VALUE klass = argv[1 + skip_arg];
1897
+ VALUE desc = MessageOrEnum_GetDescriptor(klass);
1898
+ *type_class = klass;
1899
+
1900
+ if (desc == Qnil) {
1901
+ rb_raise(rb_eArgError,
1902
+ "Type class has no descriptor. Please pass a "
1903
+ "class or enum as returned by the DescriptorPool.");
1904
+ }
1905
+
1906
+ if (ret.type == kUpb_CType_Message) {
1907
+ ret.def.msgdef = ruby_to_Descriptor(desc)->msgdef;
1908
+ Message_CheckClass(klass);
1909
+ } else {
1910
+ PBRUBY_ASSERT(ret.type == kUpb_CType_Enum);
1911
+ ret.def.enumdef = ruby_to_EnumDescriptor(desc)->enumdef;
1912
+ }
1913
+ } else {
1914
+ *init_arg = TypeInfo_InitArg(argc, argv, skip_arg + 1);
1915
+ }
1916
+
1917
+ return ret;
1918
+ }
1919
+
1920
+ void Defs_register(VALUE module) {
1921
+ DescriptorPool_register(module);
1922
+ Descriptor_register(module);
1923
+ FileDescriptor_register(module);
1924
+ FieldDescriptor_register(module);
1925
+ OneofDescriptor_register(module);
1926
+ EnumDescriptor_register(module);
1927
+ ServiceDescriptor_register(module);
1928
+ MethodDescriptor_register(module);
1929
+
1930
+ rb_gc_register_address(&c_only_cookie);
1931
+ c_only_cookie = rb_class_new_instance(0, NULL, rb_cObject);
1932
+ }