blob: 3bd18e840028a093300ae241bd751f6e1735c612 [file] [log] [blame]
Chris Fallin973f4252014-11-18 14:19:58 -08001// Protocol Buffers - Google's data interchange format
2// Copyright 2014 Google Inc. All rights reserved.
3// https://developers.google.com/protocol-buffers/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
Joshua Haberman63f324a2019-08-14 22:41:37 +010031#include <ctype.h>
32#include <errno.h>
Joshua Haberman9abf6e22021-01-13 12:16:25 -080033#include <ruby/version.h>
34
35#include "convert.h"
36#include "message.h"
Chris Fallin973f4252014-11-18 14:19:58 -080037#include "protobuf.h"
38
Joshua Haberman9abf6e22021-01-13 12:16:25 -080039// -----------------------------------------------------------------------------
40// Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
41// instances.
42// -----------------------------------------------------------------------------
43
Joshua Habermanc153dd92022-01-21 15:46:56 -080044static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_MessageDef* def);
45static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_EnumDef* def);
46static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_FieldDef* def);
47static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_FileDef* def);
48static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_OneofDef* def);
Joshua Haberman9abf6e22021-01-13 12:16:25 -080049
50// A distinct object that is not accessible from Ruby. We use this as a
51// constructor argument to enforce that certain objects cannot be created from
52// Ruby.
53VALUE c_only_cookie = Qnil;
54
Chris Fallin973f4252014-11-18 14:19:58 -080055// -----------------------------------------------------------------------------
56// Common utilities.
57// -----------------------------------------------------------------------------
58
Chris Fallin973f4252014-11-18 14:19:58 -080059static const char* get_str(VALUE str) {
60 Check_Type(str, T_STRING);
61 return RSTRING_PTR(str);
62}
63
64static VALUE rb_str_maybe_null(const char* s) {
65 if (s == NULL) {
66 s = "";
67 }
68 return rb_str_new2(s);
69}
70
Joshua Haberman9abf6e22021-01-13 12:16:25 -080071// -----------------------------------------------------------------------------
Joshua Haberman9abf6e22021-01-13 12:16:25 -080072// DescriptorPool.
73// -----------------------------------------------------------------------------
74
75typedef struct {
76 VALUE def_to_descriptor; // Hash table of def* -> Ruby descriptor.
Joshua Habermanc153dd92022-01-21 15:46:56 -080077 upb_DefPool* symtab;
Joshua Haberman9abf6e22021-01-13 12:16:25 -080078} DescriptorPool;
79
80VALUE cDescriptorPool = Qnil;
81
82// Global singleton DescriptorPool. The user is free to create others, but this
83// is used by generated code.
84VALUE generated_pool = Qnil;
85
86static void DescriptorPool_mark(void* _self) {
87 DescriptorPool* self = _self;
88 rb_gc_mark(self->def_to_descriptor);
89}
90
91static void DescriptorPool_free(void* _self) {
92 DescriptorPool* self = _self;
Joshua Habermanc153dd92022-01-21 15:46:56 -080093 upb_DefPool_Free(self->symtab);
Joshua Haberman9abf6e22021-01-13 12:16:25 -080094 xfree(self);
95}
96
97static const rb_data_type_t DescriptorPool_type = {
Joshua Habermanc153dd92022-01-21 15:46:56 -080098 "Google::Protobuf::DescriptorPool",
99 {DescriptorPool_mark, DescriptorPool_free, NULL},
100 .flags = RUBY_TYPED_FREE_IMMEDIATELY,
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800101};
102
103static DescriptorPool* ruby_to_DescriptorPool(VALUE val) {
104 DescriptorPool* ret;
105 TypedData_Get_Struct(val, DescriptorPool, &DescriptorPool_type, ret);
106 return ret;
107}
108
109// Exposed to other modules in defs.h.
Joshua Habermanc153dd92022-01-21 15:46:56 -0800110const upb_DefPool* DescriptorPool_GetSymtab(VALUE desc_pool_rb) {
111 DescriptorPool* pool = ruby_to_DescriptorPool(desc_pool_rb);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800112 return pool->symtab;
113}
114
115/*
116 * call-seq:
117 * DescriptorPool.new => pool
118 *
119 * Creates a new, empty, descriptor pool.
120 */
121static VALUE DescriptorPool_alloc(VALUE klass) {
122 DescriptorPool* self = ALLOC(DescriptorPool);
123 VALUE ret;
124
125 self->def_to_descriptor = Qnil;
126 ret = TypedData_Wrap_Struct(klass, &DescriptorPool_type, self);
127
128 self->def_to_descriptor = rb_hash_new();
Joshua Habermanc153dd92022-01-21 15:46:56 -0800129 self->symtab = upb_DefPool_New();
Joshua Haberman9879f422021-02-24 16:41:35 -0800130 ObjectCache_Add(self->symtab, ret);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800131
132 return ret;
133}
134
135/*
136 * call-seq:
Joshua Haberman16e16eb2021-07-31 11:13:08 -0700137 * DescriptorPool.add_serialized_file(serialized_file_proto)
Joshua Habermanb4b5c892021-07-30 15:04:53 -0700138 *
139 * Adds the given serialized FileDescriptorProto to the pool.
140 */
Joshua Haberman16e16eb2021-07-31 11:13:08 -0700141VALUE DescriptorPool_add_serialized_file(VALUE _self,
142 VALUE serialized_file_proto) {
143 DescriptorPool* self = ruby_to_DescriptorPool(_self);
Joshua Habermanb4b5c892021-07-30 15:04:53 -0700144 Check_Type(serialized_file_proto, T_STRING);
Joshua Haberman16e16eb2021-07-31 11:13:08 -0700145 VALUE arena_rb = Arena_new();
Joshua Habermanc153dd92022-01-21 15:46:56 -0800146 upb_Arena* arena = Arena_get(arena_rb);
Joshua Habermanb4b5c892021-07-30 15:04:53 -0700147 google_protobuf_FileDescriptorProto* file_proto =
148 google_protobuf_FileDescriptorProto_parse(
149 RSTRING_PTR(serialized_file_proto),
150 RSTRING_LEN(serialized_file_proto), arena);
Joshua Haberman16e16eb2021-07-31 11:13:08 -0700151 if (!file_proto) {
152 rb_raise(rb_eArgError, "Unable to parse FileDescriptorProto");
153 }
Joshua Habermanc153dd92022-01-21 15:46:56 -0800154 upb_Status status;
155 upb_Status_Clear(&status);
156 const upb_FileDef* filedef =
157 upb_DefPool_AddFile(self->symtab, file_proto, &status);
Joshua Haberman16e16eb2021-07-31 11:13:08 -0700158 if (!filedef) {
Joshua Habermanb4b5c892021-07-30 15:04:53 -0700159 rb_raise(cTypeError, "Unable to build file to DescriptorPool: %s",
Joshua Habermanc153dd92022-01-21 15:46:56 -0800160 upb_Status_ErrorMessage(&status));
Joshua Habermanb4b5c892021-07-30 15:04:53 -0700161 }
zhangskz276add02022-03-08 12:05:34 -0500162 RB_GC_GUARD(arena_rb);
Joshua Haberman16e16eb2021-07-31 11:13:08 -0700163 return get_filedef_obj(_self, filedef);
Joshua Habermanb4b5c892021-07-30 15:04:53 -0700164}
165
166/*
167 * call-seq:
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800168 * DescriptorPool.lookup(name) => descriptor
169 *
170 * Finds a Descriptor or EnumDescriptor by name and returns it, or nil if none
171 * exists with the given name.
172 */
173static VALUE DescriptorPool_lookup(VALUE _self, VALUE name) {
174 DescriptorPool* self = ruby_to_DescriptorPool(_self);
175 const char* name_str = get_str(name);
Joshua Habermanc153dd92022-01-21 15:46:56 -0800176 const upb_MessageDef* msgdef;
177 const upb_EnumDef* enumdef;
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800178
Joshua Habermanc153dd92022-01-21 15:46:56 -0800179 msgdef = upb_DefPool_FindMessageByName(self->symtab, name_str);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800180 if (msgdef) {
181 return get_msgdef_obj(_self, msgdef);
182 }
183
Joshua Habermanc153dd92022-01-21 15:46:56 -0800184 enumdef = upb_DefPool_FindEnumByName(self->symtab, name_str);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800185 if (enumdef) {
186 return get_enumdef_obj(_self, enumdef);
187 }
188
189 return Qnil;
190}
191
192/*
193 * call-seq:
194 * DescriptorPool.generated_pool => descriptor_pool
195 *
196 * Class method that returns the global DescriptorPool. This is a singleton into
197 * which generated-code message and enum types are registered. The user may also
198 * register types in this pool for convenience so that they do not have to hold
199 * a reference to a private pool instance.
200 */
201static VALUE DescriptorPool_generated_pool(VALUE _self) {
202 return generated_pool;
203}
204
205static void DescriptorPool_register(VALUE module) {
Joshua Habermanc153dd92022-01-21 15:46:56 -0800206 VALUE klass = rb_define_class_under(module, "DescriptorPool", rb_cObject);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800207 rb_define_alloc_func(klass, DescriptorPool_alloc);
Joshua Haberman16e16eb2021-07-31 11:13:08 -0700208 rb_define_method(klass, "add_serialized_file",
209 DescriptorPool_add_serialized_file, 1);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800210 rb_define_method(klass, "lookup", DescriptorPool_lookup, 1);
211 rb_define_singleton_method(klass, "generated_pool",
212 DescriptorPool_generated_pool, 0);
213 rb_gc_register_address(&cDescriptorPool);
214 cDescriptorPool = klass;
215
216 rb_gc_register_address(&generated_pool);
217 generated_pool = rb_class_new_instance(0, NULL, klass);
218}
219
220// -----------------------------------------------------------------------------
221// Descriptor.
222// -----------------------------------------------------------------------------
223
224typedef struct {
Joshua Habermanc153dd92022-01-21 15:46:56 -0800225 const upb_MessageDef* msgdef;
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800226 VALUE klass;
227 VALUE descriptor_pool;
228} Descriptor;
229
230VALUE cDescriptor = Qnil;
231
232static void Descriptor_mark(void* _self) {
233 Descriptor* self = _self;
234 rb_gc_mark(self->klass);
235 rb_gc_mark(self->descriptor_pool);
236}
237
238static const rb_data_type_t Descriptor_type = {
Joshua Habermanc153dd92022-01-21 15:46:56 -0800239 "Google::Protobuf::Descriptor",
240 {Descriptor_mark, RUBY_DEFAULT_FREE, NULL},
241 .flags = RUBY_TYPED_FREE_IMMEDIATELY,
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800242};
243
244static Descriptor* ruby_to_Descriptor(VALUE val) {
245 Descriptor* ret;
246 TypedData_Get_Struct(val, Descriptor, &Descriptor_type, ret);
247 return ret;
248}
249
250/*
251 * call-seq:
252 * Descriptor.new => descriptor
253 *
254 * Creates a new, empty, message type descriptor. At a minimum, its name must be
255 * set before it is added to a pool. It cannot be used to create messages until
256 * it is added to a pool, after which it becomes immutable (as part of a
257 * finalization process).
258 */
259static VALUE Descriptor_alloc(VALUE klass) {
260 Descriptor* self = ALLOC(Descriptor);
261 VALUE ret = TypedData_Wrap_Struct(klass, &Descriptor_type, self);
262 self->msgdef = NULL;
263 self->klass = Qnil;
264 self->descriptor_pool = Qnil;
265 return ret;
266}
267
268/*
269 * call-seq:
270 * Descriptor.new(c_only_cookie, ptr) => Descriptor
271 *
272 * Creates a descriptor wrapper object. May only be called from C.
273 */
274static VALUE Descriptor_initialize(VALUE _self, VALUE cookie,
275 VALUE descriptor_pool, VALUE ptr) {
276 Descriptor* self = ruby_to_Descriptor(_self);
277
278 if (cookie != c_only_cookie) {
279 rb_raise(rb_eRuntimeError,
280 "Descriptor objects may not be created from Ruby.");
281 }
282
283 self->descriptor_pool = descriptor_pool;
Joshua Habermanc153dd92022-01-21 15:46:56 -0800284 self->msgdef = (const upb_MessageDef*)NUM2ULL(ptr);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800285
286 return Qnil;
287}
288
289/*
290 * call-seq:
291 * Descriptor.file_descriptor
292 *
293 * Returns the FileDescriptor object this message belongs to.
294 */
295static VALUE Descriptor_file_descriptor(VALUE _self) {
296 Descriptor* self = ruby_to_Descriptor(_self);
Joshua Habermanc153dd92022-01-21 15:46:56 -0800297 return get_filedef_obj(self->descriptor_pool,
298 upb_MessageDef_File(self->msgdef));
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800299}
300
301/*
302 * call-seq:
303 * Descriptor.name => name
304 *
305 * Returns the name of this message type as a fully-qualified string (e.g.,
306 * My.Package.MessageType).
307 */
308static VALUE Descriptor_name(VALUE _self) {
309 Descriptor* self = ruby_to_Descriptor(_self);
Joshua Habermanc153dd92022-01-21 15:46:56 -0800310 return rb_str_maybe_null(upb_MessageDef_FullName(self->msgdef));
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800311}
312
313/*
314 * call-seq:
315 * Descriptor.each(&block)
316 *
317 * Iterates over fields in this message type, yielding to the block on each one.
318 */
319static VALUE Descriptor_each(VALUE _self) {
320 Descriptor* self = ruby_to_Descriptor(_self);
321
Joshua Habermanc153dd92022-01-21 15:46:56 -0800322 int n = upb_MessageDef_FieldCount(self->msgdef);
323 for (int i = 0; i < n; i++) {
324 const upb_FieldDef* field = upb_MessageDef_Field(self->msgdef, i);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800325 VALUE obj = get_fielddef_obj(self->descriptor_pool, field);
326 rb_yield(obj);
327 }
328 return Qnil;
329}
330
331/*
332 * call-seq:
333 * Descriptor.lookup(name) => FieldDescriptor
334 *
335 * Returns the field descriptor for the field with the given name, if present,
336 * or nil if none.
337 */
338static VALUE Descriptor_lookup(VALUE _self, VALUE name) {
339 Descriptor* self = ruby_to_Descriptor(_self);
340 const char* s = get_str(name);
Joshua Habermanc153dd92022-01-21 15:46:56 -0800341 const upb_FieldDef* field = upb_MessageDef_FindFieldByName(self->msgdef, s);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800342 if (field == NULL) {
343 return Qnil;
344 }
345 return get_fielddef_obj(self->descriptor_pool, field);
346}
347
348/*
349 * call-seq:
350 * Descriptor.each_oneof(&block) => nil
351 *
352 * Invokes the given block for each oneof in this message type, passing the
353 * corresponding OneofDescriptor.
354 */
355static VALUE Descriptor_each_oneof(VALUE _self) {
356 Descriptor* self = ruby_to_Descriptor(_self);
357
Joshua Habermanc153dd92022-01-21 15:46:56 -0800358 int n = upb_MessageDef_OneofCount(self->msgdef);
359 for (int i = 0; i < n; i++) {
360 const upb_OneofDef* oneof = upb_MessageDef_Oneof(self->msgdef, i);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800361 VALUE obj = get_oneofdef_obj(self->descriptor_pool, oneof);
362 rb_yield(obj);
363 }
364 return Qnil;
365}
366
367/*
368 * call-seq:
369 * Descriptor.lookup_oneof(name) => OneofDescriptor
370 *
371 * Returns the oneof descriptor for the oneof with the given name, if present,
372 * or nil if none.
373 */
374static VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
375 Descriptor* self = ruby_to_Descriptor(_self);
376 const char* s = get_str(name);
Joshua Habermanc153dd92022-01-21 15:46:56 -0800377 const upb_OneofDef* oneof = upb_MessageDef_FindOneofByName(self->msgdef, s);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800378 if (oneof == NULL) {
379 return Qnil;
380 }
381 return get_oneofdef_obj(self->descriptor_pool, oneof);
382}
383
384/*
385 * call-seq:
386 * Descriptor.msgclass => message_klass
387 *
388 * Returns the Ruby class created for this message type.
389 */
390static VALUE Descriptor_msgclass(VALUE _self) {
391 Descriptor* self = ruby_to_Descriptor(_self);
392 if (self->klass == Qnil) {
393 self->klass = build_class_from_descriptor(_self);
394 }
395 return self->klass;
396}
397
398static void Descriptor_register(VALUE module) {
Joshua Habermanc153dd92022-01-21 15:46:56 -0800399 VALUE klass = rb_define_class_under(module, "Descriptor", rb_cObject);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800400 rb_define_alloc_func(klass, Descriptor_alloc);
401 rb_define_method(klass, "initialize", Descriptor_initialize, 3);
402 rb_define_method(klass, "each", Descriptor_each, 0);
403 rb_define_method(klass, "lookup", Descriptor_lookup, 1);
404 rb_define_method(klass, "each_oneof", Descriptor_each_oneof, 0);
405 rb_define_method(klass, "lookup_oneof", Descriptor_lookup_oneof, 1);
406 rb_define_method(klass, "msgclass", Descriptor_msgclass, 0);
407 rb_define_method(klass, "name", Descriptor_name, 0);
408 rb_define_method(klass, "file_descriptor", Descriptor_file_descriptor, 0);
409 rb_include_module(klass, rb_mEnumerable);
410 rb_gc_register_address(&cDescriptor);
411 cDescriptor = klass;
412}
413
414// -----------------------------------------------------------------------------
415// FileDescriptor.
416// -----------------------------------------------------------------------------
417
418typedef struct {
Joshua Habermanc153dd92022-01-21 15:46:56 -0800419 const upb_FileDef* filedef;
420 VALUE descriptor_pool; // Owns the upb_FileDef.
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800421} FileDescriptor;
422
423static VALUE cFileDescriptor = Qnil;
424
425static void FileDescriptor_mark(void* _self) {
426 FileDescriptor* self = _self;
427 rb_gc_mark(self->descriptor_pool);
428}
429
430static const rb_data_type_t FileDescriptor_type = {
Joshua Habermanc153dd92022-01-21 15:46:56 -0800431 "Google::Protobuf::FileDescriptor",
432 {FileDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
433 .flags = RUBY_TYPED_FREE_IMMEDIATELY,
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800434};
435
436static FileDescriptor* ruby_to_FileDescriptor(VALUE val) {
437 FileDescriptor* ret;
438 TypedData_Get_Struct(val, FileDescriptor, &FileDescriptor_type, ret);
439 return ret;
440}
441
442static VALUE FileDescriptor_alloc(VALUE klass) {
443 FileDescriptor* self = ALLOC(FileDescriptor);
444 VALUE ret = TypedData_Wrap_Struct(klass, &FileDescriptor_type, self);
445 self->descriptor_pool = Qnil;
446 self->filedef = NULL;
447 return ret;
448}
449
450/*
451 * call-seq:
452 * FileDescriptor.new => file
453 *
454 * Returns a new file descriptor. The syntax must be set before it's passed
455 * to a builder.
456 */
457static VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
Joshua Habermanc153dd92022-01-21 15:46:56 -0800458 VALUE descriptor_pool, VALUE ptr) {
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800459 FileDescriptor* self = ruby_to_FileDescriptor(_self);
460
461 if (cookie != c_only_cookie) {
462 rb_raise(rb_eRuntimeError,
463 "Descriptor objects may not be created from Ruby.");
464 }
465
466 self->descriptor_pool = descriptor_pool;
Joshua Habermanc153dd92022-01-21 15:46:56 -0800467 self->filedef = (const upb_FileDef*)NUM2ULL(ptr);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800468
469 return Qnil;
470}
471
472/*
473 * call-seq:
474 * FileDescriptor.name => name
475 *
476 * Returns the name of the file.
477 */
478static VALUE FileDescriptor_name(VALUE _self) {
479 FileDescriptor* self = ruby_to_FileDescriptor(_self);
Joshua Habermanc153dd92022-01-21 15:46:56 -0800480 const char* name = upb_FileDef_Name(self->filedef);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800481 return name == NULL ? Qnil : rb_str_new2(name);
482}
483
484/*
485 * call-seq:
486 * FileDescriptor.syntax => syntax
487 *
488 * Returns this file descriptors syntax.
489 *
490 * Valid syntax versions are:
491 * :proto2 or :proto3.
492 */
493static VALUE FileDescriptor_syntax(VALUE _self) {
494 FileDescriptor* self = ruby_to_FileDescriptor(_self);
495
Joshua Habermanc153dd92022-01-21 15:46:56 -0800496 switch (upb_FileDef_Syntax(self->filedef)) {
497 case kUpb_Syntax_Proto3:
498 return ID2SYM(rb_intern("proto3"));
499 case kUpb_Syntax_Proto2:
500 return ID2SYM(rb_intern("proto2"));
501 default:
502 return Qnil;
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800503 }
504}
505
506static void FileDescriptor_register(VALUE module) {
Joshua Habermanc153dd92022-01-21 15:46:56 -0800507 VALUE klass = rb_define_class_under(module, "FileDescriptor", rb_cObject);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800508 rb_define_alloc_func(klass, FileDescriptor_alloc);
509 rb_define_method(klass, "initialize", FileDescriptor_initialize, 3);
510 rb_define_method(klass, "name", FileDescriptor_name, 0);
511 rb_define_method(klass, "syntax", FileDescriptor_syntax, 0);
512 rb_gc_register_address(&cFileDescriptor);
513 cFileDescriptor = klass;
514}
515
516// -----------------------------------------------------------------------------
517// FieldDescriptor.
518// -----------------------------------------------------------------------------
519
520typedef struct {
Joshua Habermanc153dd92022-01-21 15:46:56 -0800521 const upb_FieldDef* fielddef;
522 VALUE descriptor_pool; // Owns the upb_FieldDef.
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800523} FieldDescriptor;
524
525static VALUE cFieldDescriptor = Qnil;
526
527static void FieldDescriptor_mark(void* _self) {
528 FieldDescriptor* self = _self;
529 rb_gc_mark(self->descriptor_pool);
530}
531
532static const rb_data_type_t FieldDescriptor_type = {
Joshua Habermanc153dd92022-01-21 15:46:56 -0800533 "Google::Protobuf::FieldDescriptor",
534 {FieldDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
535 .flags = RUBY_TYPED_FREE_IMMEDIATELY,
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800536};
537
538static FieldDescriptor* ruby_to_FieldDescriptor(VALUE val) {
539 FieldDescriptor* ret;
540 TypedData_Get_Struct(val, FieldDescriptor, &FieldDescriptor_type, ret);
541 return ret;
542}
543
544/*
545 * call-seq:
546 * FieldDescriptor.new => field
547 *
548 * Returns a new field descriptor. Its name, type, etc. must be set before it is
549 * added to a message type.
550 */
551static VALUE FieldDescriptor_alloc(VALUE klass) {
552 FieldDescriptor* self = ALLOC(FieldDescriptor);
553 VALUE ret = TypedData_Wrap_Struct(klass, &FieldDescriptor_type, self);
554 self->fielddef = NULL;
555 return ret;
556}
557
558/*
559 * call-seq:
560 * EnumDescriptor.new(c_only_cookie, pool, ptr) => EnumDescriptor
561 *
562 * Creates a descriptor wrapper object. May only be called from C.
563 */
564static VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
565 VALUE descriptor_pool, VALUE ptr) {
566 FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
567
568 if (cookie != c_only_cookie) {
569 rb_raise(rb_eRuntimeError,
570 "Descriptor objects may not be created from Ruby.");
571 }
572
573 self->descriptor_pool = descriptor_pool;
Joshua Habermanc153dd92022-01-21 15:46:56 -0800574 self->fielddef = (const upb_FieldDef*)NUM2ULL(ptr);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800575
576 return Qnil;
577}
578
579/*
580 * call-seq:
581 * FieldDescriptor.name => name
582 *
583 * Returns the name of this field.
584 */
585static VALUE FieldDescriptor_name(VALUE _self) {
586 FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
Joshua Habermanc153dd92022-01-21 15:46:56 -0800587 return rb_str_maybe_null(upb_FieldDef_Name(self->fielddef));
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800588}
589
590// Non-static, exposed to other .c files.
Joshua Habermanc153dd92022-01-21 15:46:56 -0800591upb_CType ruby_to_fieldtype(VALUE type) {
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800592 if (TYPE(type) != T_SYMBOL) {
593 rb_raise(rb_eArgError, "Expected symbol for field type.");
594 }
595
Joshua Habermanc153dd92022-01-21 15:46:56 -0800596#define CONVERT(upb, ruby) \
597 if (SYM2ID(type) == rb_intern(#ruby)) { \
598 return kUpb_CType_##upb; \
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800599 }
600
Joshua Habermanc153dd92022-01-21 15:46:56 -0800601 CONVERT(Float, float);
602 CONVERT(Double, double);
603 CONVERT(Bool, bool);
604 CONVERT(String, string);
605 CONVERT(Bytes, bytes);
606 CONVERT(Message, message);
607 CONVERT(Enum, enum);
608 CONVERT(Int32, int32);
609 CONVERT(Int64, int64);
610 CONVERT(UInt32, uint32);
611 CONVERT(UInt64, uint64);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800612
613#undef CONVERT
614
615 rb_raise(rb_eArgError, "Unknown field type.");
616 return 0;
617}
618
Joshua Habermanc153dd92022-01-21 15:46:56 -0800619static VALUE descriptortype_to_ruby(upb_FieldType type) {
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800620 switch (type) {
Joshua Habermanc153dd92022-01-21 15:46:56 -0800621#define CONVERT(upb, ruby) \
622 case kUpb_FieldType_##upb: \
623 return ID2SYM(rb_intern(#ruby));
624 CONVERT(Float, float);
625 CONVERT(Double, double);
626 CONVERT(Bool, bool);
627 CONVERT(String, string);
628 CONVERT(Bytes, bytes);
629 CONVERT(Message, message);
630 CONVERT(Group, group);
631 CONVERT(Enum, enum);
632 CONVERT(Int32, int32);
633 CONVERT(Int64, int64);
634 CONVERT(UInt32, uint32);
635 CONVERT(UInt64, uint64);
636 CONVERT(SInt32, sint32);
637 CONVERT(SInt64, sint64);
638 CONVERT(Fixed32, fixed32);
639 CONVERT(Fixed64, fixed64);
640 CONVERT(SFixed32, sfixed32);
641 CONVERT(SFixed64, sfixed64);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800642#undef CONVERT
643 }
644 return Qnil;
645}
646
647/*
648 * call-seq:
649 * FieldDescriptor.type => type
650 *
651 * Returns this field's type, as a Ruby symbol, or nil if not yet set.
652 *
653 * Valid field types are:
654 * :int32, :int64, :uint32, :uint64, :float, :double, :bool, :string,
655 * :bytes, :message.
656 */
657static VALUE FieldDescriptor__type(VALUE _self) {
658 FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
Joshua Habermanc153dd92022-01-21 15:46:56 -0800659 return descriptortype_to_ruby(upb_FieldDef_Type(self->fielddef));
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800660}
661
662/*
663 * call-seq:
664 * FieldDescriptor.default => default
665 *
666 * Returns this field's default, as a Ruby object, or nil if not yet set.
667 */
668static VALUE FieldDescriptor_default(VALUE _self) {
669 FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
Joshua Habermanc153dd92022-01-21 15:46:56 -0800670 const upb_FieldDef* f = self->fielddef;
671 upb_MessageValue default_val = {0};
672 if (upb_FieldDef_IsSubMessage(f)) {
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800673 return Qnil;
Joshua Habermanc153dd92022-01-21 15:46:56 -0800674 } else if (!upb_FieldDef_IsRepeated(f)) {
675 default_val = upb_FieldDef_Default(f);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800676 }
677 return Convert_UpbToRuby(default_val, TypeInfo_get(self->fielddef), Qnil);
678}
679
Lukas Fittl2c354c62021-03-01 00:15:58 -0800680/*
681 * call-seq:
682 * FieldDescriptor.json_name => json_name
683 *
684 * Returns this field's json_name, as a Ruby string, or nil if not yet set.
685 */
686static VALUE FieldDescriptor_json_name(VALUE _self) {
687 FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
Joshua Habermanc153dd92022-01-21 15:46:56 -0800688 const upb_FieldDef* f = self->fielddef;
689 const char* json_name = upb_FieldDef_JsonName(f);
Lukas Fittl07f263c2021-04-03 18:42:43 -0700690 return rb_str_new2(json_name);
Lukas Fittl2c354c62021-03-01 00:15:58 -0800691}
692
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800693/*
694 * call-seq:
695 * FieldDescriptor.label => label
696 *
697 * Returns this field's label (i.e., plurality), as a Ruby symbol.
698 *
699 * Valid field labels are:
700 * :optional, :repeated
701 */
702static VALUE FieldDescriptor_label(VALUE _self) {
703 FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
Joshua Habermanc153dd92022-01-21 15:46:56 -0800704 switch (upb_FieldDef_Label(self->fielddef)) {
705#define CONVERT(upb, ruby) \
706 case kUpb_Label_##upb: \
707 return ID2SYM(rb_intern(#ruby));
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800708
Joshua Habermanc153dd92022-01-21 15:46:56 -0800709 CONVERT(Optional, optional);
710 CONVERT(Required, required);
711 CONVERT(Repeated, repeated);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800712
713#undef CONVERT
714 }
715
716 return Qnil;
717}
718
719/*
720 * call-seq:
721 * FieldDescriptor.number => number
722 *
723 * Returns the tag number for this field.
724 */
725static VALUE FieldDescriptor_number(VALUE _self) {
726 FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
Joshua Habermanc153dd92022-01-21 15:46:56 -0800727 return INT2NUM(upb_FieldDef_Number(self->fielddef));
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800728}
729
730/*
731 * call-seq:
732 * FieldDescriptor.submsg_name => submsg_name
733 *
734 * Returns the name of the message or enum type corresponding to this field, if
735 * it is a message or enum field (respectively), or nil otherwise. This type
736 * name will be resolved within the context of the pool to which the containing
737 * message type is added.
738 */
739static VALUE FieldDescriptor_submsg_name(VALUE _self) {
740 FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
Joshua Habermanc153dd92022-01-21 15:46:56 -0800741 switch (upb_FieldDef_CType(self->fielddef)) {
742 case kUpb_CType_Enum:
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800743 return rb_str_new2(
Joshua Habermanc153dd92022-01-21 15:46:56 -0800744 upb_EnumDef_FullName(upb_FieldDef_EnumSubDef(self->fielddef)));
745 case kUpb_CType_Message:
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800746 return rb_str_new2(
Joshua Habermanc153dd92022-01-21 15:46:56 -0800747 upb_MessageDef_FullName(upb_FieldDef_MessageSubDef(self->fielddef)));
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800748 default:
749 return Qnil;
750 }
751}
752
753/*
754 * call-seq:
755 * FieldDescriptor.subtype => message_or_enum_descriptor
756 *
757 * Returns the message or enum descriptor corresponding to this field's type if
758 * it is a message or enum field, respectively, or nil otherwise. Cannot be
759 * called *until* the containing message type is added to a pool (and thus
760 * resolved).
761 */
762static VALUE FieldDescriptor_subtype(VALUE _self) {
763 FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
Joshua Habermanc153dd92022-01-21 15:46:56 -0800764 switch (upb_FieldDef_CType(self->fielddef)) {
765 case kUpb_CType_Enum:
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800766 return get_enumdef_obj(self->descriptor_pool,
Joshua Habermanc153dd92022-01-21 15:46:56 -0800767 upb_FieldDef_EnumSubDef(self->fielddef));
768 case kUpb_CType_Message:
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800769 return get_msgdef_obj(self->descriptor_pool,
Joshua Habermanc153dd92022-01-21 15:46:56 -0800770 upb_FieldDef_MessageSubDef(self->fielddef));
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800771 default:
772 return Qnil;
773 }
774}
775
776/*
777 * call-seq:
778 * FieldDescriptor.get(message) => value
779 *
780 * Returns the value set for this field on the given message. Raises an
781 * exception if message is of the wrong type.
782 */
783static VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
784 FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
Joshua Habermanc153dd92022-01-21 15:46:56 -0800785 const upb_MessageDef* m;
Joshua Haberman4e3ea742021-02-22 17:14:46 -0800786
787 Message_Get(msg_rb, &m);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800788
Joshua Habermanc153dd92022-01-21 15:46:56 -0800789 if (m != upb_FieldDef_ContainingType(self->fielddef)) {
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800790 rb_raise(cTypeError, "get method called on wrong message type");
791 }
792
Joshua Haberman4e3ea742021-02-22 17:14:46 -0800793 return Message_getfield(msg_rb, self->fielddef);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800794}
795
796/*
797 * call-seq:
798 * FieldDescriptor.has?(message) => boolean
799 *
800 * Returns whether the value is set on the given message. Raises an
801 * exception when calling for fields that do not have presence.
802 */
803static VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
804 FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
Joshua Habermanc153dd92022-01-21 15:46:56 -0800805 const upb_MessageDef* m;
806 const upb_MessageDef* msg = Message_Get(msg_rb, &m);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800807
Joshua Habermanc153dd92022-01-21 15:46:56 -0800808 if (m != upb_FieldDef_ContainingType(self->fielddef)) {
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800809 rb_raise(cTypeError, "has method called on wrong message type");
Joshua Habermanc153dd92022-01-21 15:46:56 -0800810 } else if (!upb_FieldDef_HasPresence(self->fielddef)) {
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800811 rb_raise(rb_eArgError, "does not track presence");
812 }
813
Joshua Habermanc153dd92022-01-21 15:46:56 -0800814 return upb_Message_Has(msg, self->fielddef) ? Qtrue : Qfalse;
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800815}
816
817/*
818 * call-seq:
819 * FieldDescriptor.clear(message)
820 *
821 * Clears the field from the message if it's set.
822 */
823static VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {
824 FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
Joshua Habermanc153dd92022-01-21 15:46:56 -0800825 const upb_MessageDef* m;
826 upb_MessageDef* msg = Message_GetMutable(msg_rb, &m);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800827
Joshua Habermanc153dd92022-01-21 15:46:56 -0800828 if (m != upb_FieldDef_ContainingType(self->fielddef)) {
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800829 rb_raise(cTypeError, "has method called on wrong message type");
830 }
831
Joshua Habermanc153dd92022-01-21 15:46:56 -0800832 upb_Message_ClearField(msg, self->fielddef);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800833 return Qnil;
834}
835
836/*
837 * call-seq:
838 * FieldDescriptor.set(message, value)
839 *
840 * Sets the value corresponding to this field to the given value on the given
841 * message. Raises an exception if message is of the wrong type. Performs the
842 * ordinary type-checks for field setting.
843 */
844static VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value) {
845 FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
Joshua Habermanc153dd92022-01-21 15:46:56 -0800846 const upb_MessageDef* m;
847 upb_MessageDef* msg = Message_GetMutable(msg_rb, &m);
848 upb_Arena* arena = Arena_get(Message_GetArena(msg_rb));
849 upb_MessageValue msgval;
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800850
Joshua Habermanc153dd92022-01-21 15:46:56 -0800851 if (m != upb_FieldDef_ContainingType(self->fielddef)) {
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800852 rb_raise(cTypeError, "set method called on wrong message type");
853 }
854
Joshua Habermanc153dd92022-01-21 15:46:56 -0800855 msgval = Convert_RubyToUpb(value, upb_FieldDef_Name(self->fielddef),
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800856 TypeInfo_get(self->fielddef), arena);
Joshua Habermanc153dd92022-01-21 15:46:56 -0800857 upb_Message_Set(msg, self->fielddef, msgval, arena);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800858 return Qnil;
859}
860
861static void FieldDescriptor_register(VALUE module) {
Joshua Habermanc153dd92022-01-21 15:46:56 -0800862 VALUE klass = rb_define_class_under(module, "FieldDescriptor", rb_cObject);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800863 rb_define_alloc_func(klass, FieldDescriptor_alloc);
864 rb_define_method(klass, "initialize", FieldDescriptor_initialize, 3);
865 rb_define_method(klass, "name", FieldDescriptor_name, 0);
866 rb_define_method(klass, "type", FieldDescriptor__type, 0);
867 rb_define_method(klass, "default", FieldDescriptor_default, 0);
Lukas Fittl2c354c62021-03-01 00:15:58 -0800868 rb_define_method(klass, "json_name", FieldDescriptor_json_name, 0);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800869 rb_define_method(klass, "label", FieldDescriptor_label, 0);
870 rb_define_method(klass, "number", FieldDescriptor_number, 0);
871 rb_define_method(klass, "submsg_name", FieldDescriptor_submsg_name, 0);
872 rb_define_method(klass, "subtype", FieldDescriptor_subtype, 0);
873 rb_define_method(klass, "has?", FieldDescriptor_has, 1);
874 rb_define_method(klass, "clear", FieldDescriptor_clear, 1);
875 rb_define_method(klass, "get", FieldDescriptor_get, 1);
876 rb_define_method(klass, "set", FieldDescriptor_set, 2);
877 rb_gc_register_address(&cFieldDescriptor);
878 cFieldDescriptor = klass;
879}
880
881// -----------------------------------------------------------------------------
882// OneofDescriptor.
883// -----------------------------------------------------------------------------
884
885typedef struct {
Joshua Habermanc153dd92022-01-21 15:46:56 -0800886 const upb_OneofDef* oneofdef;
887 VALUE descriptor_pool; // Owns the upb_OneofDef.
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800888} OneofDescriptor;
889
890static VALUE cOneofDescriptor = Qnil;
891
892static void OneofDescriptor_mark(void* _self) {
893 OneofDescriptor* self = _self;
894 rb_gc_mark(self->descriptor_pool);
895}
896
897static const rb_data_type_t OneofDescriptor_type = {
898 "Google::Protobuf::OneofDescriptor",
899 {OneofDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
900 .flags = RUBY_TYPED_FREE_IMMEDIATELY,
901};
902
903static OneofDescriptor* ruby_to_OneofDescriptor(VALUE val) {
904 OneofDescriptor* ret;
905 TypedData_Get_Struct(val, OneofDescriptor, &OneofDescriptor_type, ret);
906 return ret;
907}
908
909/*
910 * call-seq:
911 * OneofDescriptor.new => oneof_descriptor
912 *
913 * Creates a new, empty, oneof descriptor. The oneof may only be modified prior
914 * to being added to a message descriptor which is subsequently added to a pool.
915 */
916static VALUE OneofDescriptor_alloc(VALUE klass) {
917 OneofDescriptor* self = ALLOC(OneofDescriptor);
918 VALUE ret = TypedData_Wrap_Struct(klass, &OneofDescriptor_type, self);
919 self->oneofdef = NULL;
920 self->descriptor_pool = Qnil;
921 return ret;
922}
923
924/*
925 * call-seq:
926 * OneofDescriptor.new(c_only_cookie, pool, ptr) => OneofDescriptor
927 *
928 * Creates a descriptor wrapper object. May only be called from C.
929 */
930static VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
Joshua Habermanc153dd92022-01-21 15:46:56 -0800931 VALUE descriptor_pool, VALUE ptr) {
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800932 OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
933
934 if (cookie != c_only_cookie) {
935 rb_raise(rb_eRuntimeError,
936 "Descriptor objects may not be created from Ruby.");
937 }
938
939 self->descriptor_pool = descriptor_pool;
Joshua Habermanc153dd92022-01-21 15:46:56 -0800940 self->oneofdef = (const upb_OneofDef*)NUM2ULL(ptr);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800941
942 return Qnil;
943}
944
945/*
946 * call-seq:
947 * OneofDescriptor.name => name
948 *
949 * Returns the name of this oneof.
950 */
951static VALUE OneofDescriptor_name(VALUE _self) {
952 OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
Joshua Habermanc153dd92022-01-21 15:46:56 -0800953 return rb_str_maybe_null(upb_OneofDef_Name(self->oneofdef));
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800954}
955
956/*
957 * call-seq:
958 * OneofDescriptor.each(&block) => nil
959 *
960 * Iterates through fields in this oneof, yielding to the block on each one.
961 */
962static VALUE OneofDescriptor_each(VALUE _self) {
963 OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
Joshua Habermanc153dd92022-01-21 15:46:56 -0800964
965 int n = upb_OneofDef_FieldCount(self->oneofdef);
966 for (int i = 0; i < n; i++) {
967 const upb_FieldDef* f = upb_OneofDef_Field(self->oneofdef, i);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800968 VALUE obj = get_fielddef_obj(self->descriptor_pool, f);
969 rb_yield(obj);
970 }
971 return Qnil;
972}
973
974static void OneofDescriptor_register(VALUE module) {
Joshua Habermanc153dd92022-01-21 15:46:56 -0800975 VALUE klass = rb_define_class_under(module, "OneofDescriptor", rb_cObject);
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800976 rb_define_alloc_func(klass, OneofDescriptor_alloc);
977 rb_define_method(klass, "initialize", OneofDescriptor_initialize, 3);
978 rb_define_method(klass, "name", OneofDescriptor_name, 0);
979 rb_define_method(klass, "each", OneofDescriptor_each, 0);
980 rb_include_module(klass, rb_mEnumerable);
981 rb_gc_register_address(&cOneofDescriptor);
982 cOneofDescriptor = klass;
983}
984
985// -----------------------------------------------------------------------------
986// EnumDescriptor.
987// -----------------------------------------------------------------------------
988
989typedef struct {
Joshua Habermanc153dd92022-01-21 15:46:56 -0800990 const upb_EnumDef* enumdef;
991 VALUE module; // begins as nil
992 VALUE descriptor_pool; // Owns the upb_EnumDef.
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800993} EnumDescriptor;
994
995static VALUE cEnumDescriptor = Qnil;
996
997static void EnumDescriptor_mark(void* _self) {
998 EnumDescriptor* self = _self;
999 rb_gc_mark(self->module);
1000 rb_gc_mark(self->descriptor_pool);
1001}
1002
1003static const rb_data_type_t EnumDescriptor_type = {
Joshua Habermanc153dd92022-01-21 15:46:56 -08001004 "Google::Protobuf::EnumDescriptor",
1005 {EnumDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
1006 .flags = RUBY_TYPED_FREE_IMMEDIATELY,
Joshua Haberman9abf6e22021-01-13 12:16:25 -08001007};
1008
1009static EnumDescriptor* ruby_to_EnumDescriptor(VALUE val) {
1010 EnumDescriptor* ret;
1011 TypedData_Get_Struct(val, EnumDescriptor, &EnumDescriptor_type, ret);
1012 return ret;
1013}
1014
1015static VALUE EnumDescriptor_alloc(VALUE klass) {
1016 EnumDescriptor* self = ALLOC(EnumDescriptor);
1017 VALUE ret = TypedData_Wrap_Struct(klass, &EnumDescriptor_type, self);
1018 self->enumdef = NULL;
1019 self->module = Qnil;
1020 self->descriptor_pool = Qnil;
1021 return ret;
1022}
1023
1024// Exposed to other modules in defs.h.
Joshua Habermanc153dd92022-01-21 15:46:56 -08001025const upb_EnumDef* EnumDescriptor_GetEnumDef(VALUE enum_desc_rb) {
1026 EnumDescriptor* desc = ruby_to_EnumDescriptor(enum_desc_rb);
Joshua Haberman9abf6e22021-01-13 12:16:25 -08001027 return desc->enumdef;
1028}
1029
1030/*
1031 * call-seq:
1032 * EnumDescriptor.new(c_only_cookie, ptr) => EnumDescriptor
1033 *
1034 * Creates a descriptor wrapper object. May only be called from C.
1035 */
1036static VALUE EnumDescriptor_initialize(VALUE _self, VALUE cookie,
1037 VALUE descriptor_pool, VALUE ptr) {
1038 EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1039
1040 if (cookie != c_only_cookie) {
1041 rb_raise(rb_eRuntimeError,
1042 "Descriptor objects may not be created from Ruby.");
1043 }
1044
1045 self->descriptor_pool = descriptor_pool;
Joshua Habermanc153dd92022-01-21 15:46:56 -08001046 self->enumdef = (const upb_EnumDef*)NUM2ULL(ptr);
Joshua Haberman9abf6e22021-01-13 12:16:25 -08001047
1048 return Qnil;
1049}
1050
1051/*
1052 * call-seq:
1053 * EnumDescriptor.file_descriptor
1054 *
1055 * Returns the FileDescriptor object this enum belongs to.
1056 */
1057static VALUE EnumDescriptor_file_descriptor(VALUE _self) {
1058 EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1059 return get_filedef_obj(self->descriptor_pool,
Joshua Habermanc153dd92022-01-21 15:46:56 -08001060 upb_EnumDef_File(self->enumdef));
Joshua Haberman9abf6e22021-01-13 12:16:25 -08001061}
1062
1063/*
1064 * call-seq:
1065 * EnumDescriptor.name => name
1066 *
1067 * Returns the name of this enum type.
1068 */
1069static VALUE EnumDescriptor_name(VALUE _self) {
1070 EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
Joshua Habermanc153dd92022-01-21 15:46:56 -08001071 return rb_str_maybe_null(upb_EnumDef_FullName(self->enumdef));
Joshua Haberman9abf6e22021-01-13 12:16:25 -08001072}
1073
1074/*
1075 * call-seq:
1076 * EnumDescriptor.lookup_name(name) => value
1077 *
1078 * Returns the numeric value corresponding to the given key name (as a Ruby
1079 * symbol), or nil if none.
1080 */
1081static VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
1082 EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
Joshua Habermanc153dd92022-01-21 15:46:56 -08001083 const char* name_str = rb_id2name(SYM2ID(name));
1084 const upb_EnumValueDef *ev =
1085 upb_EnumDef_FindValueByName(self->enumdef, name_str);
1086 if (ev) {
1087 return INT2NUM(upb_EnumValueDef_Number(ev));
Joshua Haberman9abf6e22021-01-13 12:16:25 -08001088 } else {
1089 return Qnil;
1090 }
1091}
1092
1093/*
1094 * call-seq:
1095 * EnumDescriptor.lookup_value(name) => value
1096 *
1097 * Returns the key name (as a Ruby symbol) corresponding to the integer value,
1098 * or nil if none.
1099 */
1100static VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
1101 EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1102 int32_t val = NUM2INT(number);
Joshua Habermanc153dd92022-01-21 15:46:56 -08001103 const upb_EnumValueDef* ev = upb_EnumDef_FindValueByNumber(self->enumdef, val);
1104 if (ev) {
1105 return ID2SYM(rb_intern(upb_EnumValueDef_Name(ev)));
Joshua Haberman9abf6e22021-01-13 12:16:25 -08001106 } else {
1107 return Qnil;
1108 }
1109}
1110
1111/*
1112 * call-seq:
1113 * EnumDescriptor.each(&block)
1114 *
1115 * Iterates over key => value mappings in this enum's definition, yielding to
1116 * the block with (key, value) arguments for each one.
1117 */
1118static VALUE EnumDescriptor_each(VALUE _self) {
1119 EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1120
Joshua Habermanc153dd92022-01-21 15:46:56 -08001121 int n = upb_EnumDef_ValueCount(self->enumdef);
1122 for (int i = 0; i < n; i++) {
1123 const upb_EnumValueDef* ev = upb_EnumDef_Value(self->enumdef, i);
1124 VALUE key = ID2SYM(rb_intern(upb_EnumValueDef_Name(ev)));
1125 VALUE number = INT2NUM(upb_EnumValueDef_Number(ev));
Joshua Haberman9abf6e22021-01-13 12:16:25 -08001126 rb_yield_values(2, key, number);
1127 }
1128
1129 return Qnil;
1130}
1131
1132/*
1133 * call-seq:
1134 * EnumDescriptor.enummodule => module
1135 *
1136 * Returns the Ruby module corresponding to this enum type.
1137 */
1138static VALUE EnumDescriptor_enummodule(VALUE _self) {
1139 EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1140 if (self->module == Qnil) {
1141 self->module = build_module_from_enumdesc(_self);
1142 }
1143 return self->module;
1144}
1145
1146static void EnumDescriptor_register(VALUE module) {
Joshua Habermanc153dd92022-01-21 15:46:56 -08001147 VALUE klass = rb_define_class_under(module, "EnumDescriptor", rb_cObject);
Joshua Haberman9abf6e22021-01-13 12:16:25 -08001148 rb_define_alloc_func(klass, EnumDescriptor_alloc);
1149 rb_define_method(klass, "initialize", EnumDescriptor_initialize, 3);
1150 rb_define_method(klass, "name", EnumDescriptor_name, 0);
1151 rb_define_method(klass, "lookup_name", EnumDescriptor_lookup_name, 1);
1152 rb_define_method(klass, "lookup_value", EnumDescriptor_lookup_value, 1);
1153 rb_define_method(klass, "each", EnumDescriptor_each, 0);
1154 rb_define_method(klass, "enummodule", EnumDescriptor_enummodule, 0);
1155 rb_define_method(klass, "file_descriptor", EnumDescriptor_file_descriptor, 0);
1156 rb_include_module(klass, rb_mEnumerable);
1157 rb_gc_register_address(&cEnumDescriptor);
1158 cEnumDescriptor = klass;
1159}
1160
Joshua Haberman63f324a2019-08-14 22:41:37 +01001161static VALUE get_def_obj(VALUE _descriptor_pool, const void* ptr, VALUE klass) {
Joshua Haberman9abf6e22021-01-13 12:16:25 -08001162 DescriptorPool* descriptor_pool = ruby_to_DescriptorPool(_descriptor_pool);
Joshua Haberman63f324a2019-08-14 22:41:37 +01001163 VALUE key = ULL2NUM((intptr_t)ptr);
1164 VALUE def;
1165
1166 def = rb_hash_aref(descriptor_pool->def_to_descriptor, key);
1167
1168 if (ptr == NULL) {
1169 return Qnil;
1170 }
1171
1172 if (def == Qnil) {
1173 // Lazily create wrapper object.
Joshua Habermanc153dd92022-01-21 15:46:56 -08001174 VALUE args[3] = {c_only_cookie, _descriptor_pool, key};
Joshua Haberman63f324a2019-08-14 22:41:37 +01001175 def = rb_class_new_instance(3, args, klass);
1176 rb_hash_aset(descriptor_pool->def_to_descriptor, key, def);
1177 }
1178
1179 return def;
1180}
1181
Joshua Habermanc153dd92022-01-21 15:46:56 -08001182static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_MessageDef* def) {
Joshua Haberman63f324a2019-08-14 22:41:37 +01001183 return get_def_obj(descriptor_pool, def, cDescriptor);
1184}
1185
Joshua Habermanc153dd92022-01-21 15:46:56 -08001186static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_EnumDef* def) {
Joshua Haberman63f324a2019-08-14 22:41:37 +01001187 return get_def_obj(descriptor_pool, def, cEnumDescriptor);
1188}
1189
Joshua Habermanc153dd92022-01-21 15:46:56 -08001190static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_FieldDef* def) {
Joshua Haberman63f324a2019-08-14 22:41:37 +01001191 return get_def_obj(descriptor_pool, def, cFieldDescriptor);
1192}
1193
Joshua Habermanc153dd92022-01-21 15:46:56 -08001194static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_FileDef* def) {
Joshua Haberman63f324a2019-08-14 22:41:37 +01001195 return get_def_obj(descriptor_pool, def, cFileDescriptor);
1196}
1197
Joshua Habermanc153dd92022-01-21 15:46:56 -08001198static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_OneofDef* def) {
Joshua Haberman63f324a2019-08-14 22:41:37 +01001199 return get_def_obj(descriptor_pool, def, cOneofDescriptor);
1200}
Joshua Haberman9abf6e22021-01-13 12:16:25 -08001201
1202// -----------------------------------------------------------------------------
1203// Shared functions
1204// -----------------------------------------------------------------------------
1205
1206// Functions exposed to other modules in defs.h.
1207
Joshua Habermanc153dd92022-01-21 15:46:56 -08001208VALUE Descriptor_DefToClass(const upb_MessageDef* m) {
1209 const upb_DefPool* symtab = upb_FileDef_Pool(upb_MessageDef_File(m));
Joshua Haberman9abf6e22021-01-13 12:16:25 -08001210 VALUE pool = ObjectCache_Get(symtab);
1211 PBRUBY_ASSERT(pool != Qnil);
1212 VALUE desc_rb = get_msgdef_obj(pool, m);
1213 const Descriptor* desc = ruby_to_Descriptor(desc_rb);
1214 return desc->klass;
1215}
1216
Joshua Habermanc153dd92022-01-21 15:46:56 -08001217const upb_MessageDef* Descriptor_GetMsgDef(VALUE desc_rb) {
Joshua Haberman9abf6e22021-01-13 12:16:25 -08001218 const Descriptor* desc = ruby_to_Descriptor(desc_rb);
1219 return desc->msgdef;
1220}
1221
Joshua Habermanc153dd92022-01-21 15:46:56 -08001222VALUE TypeInfo_InitArg(int argc, VALUE* argv, int skip_arg) {
Joshua Haberman9abf6e22021-01-13 12:16:25 -08001223 if (argc > skip_arg) {
1224 if (argc > 1 + skip_arg) {
Joshua Habermanc153dd92022-01-21 15:46:56 -08001225 rb_raise(rb_eArgError, "Expected a maximum of %d arguments.",
1226 skip_arg + 1);
Joshua Haberman9abf6e22021-01-13 12:16:25 -08001227 }
1228 return argv[skip_arg];
1229 } else {
1230 return Qnil;
1231 }
1232}
1233
1234TypeInfo TypeInfo_FromClass(int argc, VALUE* argv, int skip_arg,
1235 VALUE* type_class, VALUE* init_arg) {
1236 TypeInfo ret = {ruby_to_fieldtype(argv[skip_arg])};
1237
Joshua Habermanc153dd92022-01-21 15:46:56 -08001238 if (ret.type == kUpb_CType_Message || ret.type == kUpb_CType_Enum) {
Joshua Haberman9abf6e22021-01-13 12:16:25 -08001239 *init_arg = TypeInfo_InitArg(argc, argv, skip_arg + 2);
1240
1241 if (argc < 2 + skip_arg) {
1242 rb_raise(rb_eArgError, "Expected at least %d arguments for message/enum.",
1243 2 + skip_arg);
1244 }
1245
1246 VALUE klass = argv[1 + skip_arg];
1247 VALUE desc = MessageOrEnum_GetDescriptor(klass);
1248 *type_class = klass;
1249
1250 if (desc == Qnil) {
1251 rb_raise(rb_eArgError,
1252 "Type class has no descriptor. Please pass a "
1253 "class or enum as returned by the DescriptorPool.");
1254 }
1255
Joshua Habermanc153dd92022-01-21 15:46:56 -08001256 if (ret.type == kUpb_CType_Message) {
Joshua Haberman9abf6e22021-01-13 12:16:25 -08001257 ret.def.msgdef = ruby_to_Descriptor(desc)->msgdef;
1258 Message_CheckClass(klass);
1259 } else {
Joshua Habermanc153dd92022-01-21 15:46:56 -08001260 PBRUBY_ASSERT(ret.type == kUpb_CType_Enum);
Joshua Haberman9abf6e22021-01-13 12:16:25 -08001261 ret.def.enumdef = ruby_to_EnumDescriptor(desc)->enumdef;
1262 }
1263 } else {
1264 *init_arg = TypeInfo_InitArg(argc, argv, skip_arg + 1);
1265 }
1266
1267 return ret;
1268}
1269
1270void Defs_register(VALUE module) {
1271 DescriptorPool_register(module);
1272 Descriptor_register(module);
1273 FileDescriptor_register(module);
1274 FieldDescriptor_register(module);
1275 OneofDescriptor_register(module);
1276 EnumDescriptor_register(module);
Joshua Haberman9abf6e22021-01-13 12:16:25 -08001277
1278 rb_gc_register_address(&c_only_cookie);
1279 c_only_cookie = rb_class_new_instance(0, NULL, rb_cObject);
1280}