google-protobuf 3.25.3 → 4.26.1-java

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