-
Notifications
You must be signed in to change notification settings - Fork 15.3k
/
defs.c
2557 lines (2201 loc) · 82.8 KB
/
defs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Protocol Buffers - Google's data interchange format
// Copyright 2014 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <ctype.h>
#include <errno.h>
#include <ruby/version.h>
#include "convert.h"
#include "message.h"
#include "protobuf.h"
static VALUE Builder_build(VALUE _self);
static VALUE cMessageBuilderContext;
static VALUE cOneofBuilderContext;
static VALUE cEnumBuilderContext;
static VALUE cBuilder;
// -----------------------------------------------------------------------------
// Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
// instances.
// -----------------------------------------------------------------------------
static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_msgdef* def);
static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_enumdef* def);
static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_fielddef* def);
static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_filedef* def);
static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_oneofdef* def);
// A distinct object that is not accessible from Ruby. We use this as a
// constructor argument to enforce that certain objects cannot be created from
// Ruby.
VALUE c_only_cookie = Qnil;
// -----------------------------------------------------------------------------
// Common utilities.
// -----------------------------------------------------------------------------
static const char* get_str(VALUE str) {
Check_Type(str, T_STRING);
return RSTRING_PTR(str);
}
static VALUE rb_str_maybe_null(const char* s) {
if (s == NULL) {
s = "";
}
return rb_str_new2(s);
}
// -----------------------------------------------------------------------------
// Backward compatibility code.
// -----------------------------------------------------------------------------
static void rewrite_enum_default(const upb_symtab* symtab,
google_protobuf_FileDescriptorProto* file,
google_protobuf_FieldDescriptorProto* field) {
upb_strview defaultval;
const char *type_name_str;
char *end;
long val;
const upb_enumdef *e;
upb_strview type_name;
/* Look for TYPE_ENUM fields that have a default. */
if (google_protobuf_FieldDescriptorProto_type(field) !=
google_protobuf_FieldDescriptorProto_TYPE_ENUM ||
!google_protobuf_FieldDescriptorProto_has_default_value(field) ||
!google_protobuf_FieldDescriptorProto_has_type_name(field)) {
return;
}
defaultval = google_protobuf_FieldDescriptorProto_default_value(field);
type_name = google_protobuf_FieldDescriptorProto_type_name(field);
if (defaultval.size == 0 || !isdigit(defaultval.data[0])) {
return;
}
if (type_name.size == 0 || type_name.data[0] != '.') {
return;
}
type_name_str = type_name.data + 1;
errno = 0;
val = strtol(defaultval.data, &end, 10);
if (errno != 0 || *end != 0 || val < INT32_MIN || val > INT32_MAX) {
return;
}
/* Now find the corresponding enum definition. */
e = upb_symtab_lookupenum(symtab, type_name_str);
if (e) {
/* Look in previously loaded files. */
const char *label = upb_enumdef_iton(e, val);
if (!label) {
return;
}
google_protobuf_FieldDescriptorProto_set_default_value(
field, upb_strview_makez(label));
} else {
/* Look in enums defined in this file. */
const google_protobuf_EnumDescriptorProto* matching_enum = NULL;
size_t i, n;
const google_protobuf_EnumDescriptorProto* const* enums =
google_protobuf_FileDescriptorProto_enum_type(file, &n);
const google_protobuf_EnumValueDescriptorProto* const* values;
for (i = 0; i < n; i++) {
if (upb_strview_eql(google_protobuf_EnumDescriptorProto_name(enums[i]),
upb_strview_makez(type_name_str))) {
matching_enum = enums[i];
break;
}
}
if (!matching_enum) {
return;
}
values = google_protobuf_EnumDescriptorProto_value(matching_enum, &n);
for (i = 0; i < n; i++) {
if (google_protobuf_EnumValueDescriptorProto_number(values[i]) == val) {
google_protobuf_FieldDescriptorProto_set_default_value(
field, google_protobuf_EnumValueDescriptorProto_name(values[i]));
return;
}
}
/* We failed to find an enum default. But we'll just leave the enum
* untouched and let the normal def-building code catch it. */
}
}
/* Historically we allowed enum defaults to be specified as a number. In
* retrospect this was a mistake as descriptors require defaults to be
* specified as a label. This can make a difference if multiple labels have the
* same number.
*
* Here we do a pass over all enum defaults and rewrite numeric defaults by
* looking up their labels. This is complicated by the fact that the enum
* definition can live in either the symtab or the file_proto.
* */
static void rewrite_enum_defaults(
const upb_symtab* symtab, google_protobuf_FileDescriptorProto* file_proto) {
size_t i, n;
google_protobuf_DescriptorProto** msgs =
google_protobuf_FileDescriptorProto_mutable_message_type(file_proto, &n);
for (i = 0; i < n; i++) {
size_t j, m;
google_protobuf_FieldDescriptorProto** fields =
google_protobuf_DescriptorProto_mutable_field(msgs[i], &m);
for (j = 0; j < m; j++) {
rewrite_enum_default(symtab, file_proto, fields[j]);
}
}
}
static void remove_path(upb_strview *name) {
const char* last = strrchr(name->data, '.');
if (last) {
size_t remove = last - name->data + 1;
name->data += remove;
name->size -= remove;
}
}
static void rewrite_nesting(VALUE msg_ent, google_protobuf_DescriptorProto* msg,
google_protobuf_DescriptorProto* const* msgs,
google_protobuf_EnumDescriptorProto* const* enums,
upb_arena *arena) {
VALUE submsgs = rb_hash_aref(msg_ent, ID2SYM(rb_intern("msgs")));
VALUE enum_pos = rb_hash_aref(msg_ent, ID2SYM(rb_intern("enums")));
int submsg_count;
int enum_count;
int i;
google_protobuf_DescriptorProto** msg_msgs;
google_protobuf_EnumDescriptorProto** msg_enums;
Check_Type(submsgs, T_ARRAY);
Check_Type(enum_pos, T_ARRAY);
submsg_count = RARRAY_LEN(submsgs);
enum_count = RARRAY_LEN(enum_pos);
msg_msgs = google_protobuf_DescriptorProto_resize_nested_type(
msg, submsg_count, arena);
msg_enums =
google_protobuf_DescriptorProto_resize_enum_type(msg, enum_count, arena);
for (i = 0; i < submsg_count; i++) {
VALUE submsg_ent = RARRAY_PTR(submsgs)[i];
VALUE pos = rb_hash_aref(submsg_ent, ID2SYM(rb_intern("pos")));
upb_strview name;
msg_msgs[i] = msgs[NUM2INT(pos)];
name = google_protobuf_DescriptorProto_name(msg_msgs[i]);
remove_path(&name);
google_protobuf_DescriptorProto_set_name(msg_msgs[i], name);
rewrite_nesting(submsg_ent, msg_msgs[i], msgs, enums, arena);
}
for (i = 0; i < enum_count; i++) {
VALUE pos = RARRAY_PTR(enum_pos)[i];
msg_enums[i] = enums[NUM2INT(pos)];
}
}
// -----------------------------------------------------------------------------
// DescriptorPool.
// -----------------------------------------------------------------------------
typedef struct {
VALUE def_to_descriptor; // Hash table of def* -> Ruby descriptor.
upb_symtab* symtab;
} DescriptorPool;
VALUE cDescriptorPool = Qnil;
// Global singleton DescriptorPool. The user is free to create others, but this
// is used by generated code.
VALUE generated_pool = Qnil;
static void DescriptorPool_mark(void* _self) {
DescriptorPool* self = _self;
rb_gc_mark(self->def_to_descriptor);
}
static void DescriptorPool_free(void* _self) {
DescriptorPool* self = _self;
upb_symtab_free(self->symtab);
xfree(self);
}
static const rb_data_type_t DescriptorPool_type = {
"Google::Protobuf::DescriptorPool",
{DescriptorPool_mark, DescriptorPool_free, NULL},
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
};
static DescriptorPool* ruby_to_DescriptorPool(VALUE val) {
DescriptorPool* ret;
TypedData_Get_Struct(val, DescriptorPool, &DescriptorPool_type, ret);
return ret;
}
// Exposed to other modules in defs.h.
const upb_symtab *DescriptorPool_GetSymtab(VALUE desc_pool_rb) {
DescriptorPool *pool = ruby_to_DescriptorPool(desc_pool_rb);
return pool->symtab;
}
/*
* call-seq:
* DescriptorPool.new => pool
*
* Creates a new, empty, descriptor pool.
*/
static VALUE DescriptorPool_alloc(VALUE klass) {
DescriptorPool* self = ALLOC(DescriptorPool);
VALUE ret;
self->def_to_descriptor = Qnil;
ret = TypedData_Wrap_Struct(klass, &DescriptorPool_type, self);
self->def_to_descriptor = rb_hash_new();
self->symtab = upb_symtab_new();
ObjectCache_Add(self->symtab, ret);
return ret;
}
/*
* call-seq:
* DescriptorPool.build(&block)
*
* Invokes the block with a Builder instance as self. All message and enum types
* added within the block are committed to the pool atomically, and may refer
* (co)recursively to each other. The user should call Builder#add_message and
* Builder#add_enum within the block as appropriate. This is the recommended,
* idiomatic way to define new message and enum types.
*/
static VALUE DescriptorPool_build(int argc, VALUE* argv, VALUE _self) {
VALUE ctx = rb_class_new_instance(1, &_self, cBuilder);
VALUE block = rb_block_proc();
rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
Builder_build(ctx);
return Qnil;
}
/*
* call-seq:
* DescriptorPool.lookup(name) => descriptor
*
* Finds a Descriptor or EnumDescriptor by name and returns it, or nil if none
* exists with the given name.
*/
static VALUE DescriptorPool_lookup(VALUE _self, VALUE name) {
DescriptorPool* self = ruby_to_DescriptorPool(_self);
const char* name_str = get_str(name);
const upb_msgdef* msgdef;
const upb_enumdef* enumdef;
msgdef = upb_symtab_lookupmsg(self->symtab, name_str);
if (msgdef) {
return get_msgdef_obj(_self, msgdef);
}
enumdef = upb_symtab_lookupenum(self->symtab, name_str);
if (enumdef) {
return get_enumdef_obj(_self, enumdef);
}
return Qnil;
}
/*
* call-seq:
* DescriptorPool.generated_pool => descriptor_pool
*
* Class method that returns the global DescriptorPool. This is a singleton into
* which generated-code message and enum types are registered. The user may also
* register types in this pool for convenience so that they do not have to hold
* a reference to a private pool instance.
*/
static VALUE DescriptorPool_generated_pool(VALUE _self) {
return generated_pool;
}
static void DescriptorPool_register(VALUE module) {
VALUE klass = rb_define_class_under(
module, "DescriptorPool", rb_cObject);
rb_define_alloc_func(klass, DescriptorPool_alloc);
rb_define_method(klass, "build", DescriptorPool_build, -1);
rb_define_method(klass, "lookup", DescriptorPool_lookup, 1);
rb_define_singleton_method(klass, "generated_pool",
DescriptorPool_generated_pool, 0);
rb_gc_register_address(&cDescriptorPool);
cDescriptorPool = klass;
rb_gc_register_address(&generated_pool);
generated_pool = rb_class_new_instance(0, NULL, klass);
}
// -----------------------------------------------------------------------------
// Descriptor.
// -----------------------------------------------------------------------------
typedef struct {
const upb_msgdef* msgdef;
VALUE klass;
VALUE descriptor_pool;
} Descriptor;
VALUE cDescriptor = Qnil;
static void Descriptor_mark(void* _self) {
Descriptor* self = _self;
rb_gc_mark(self->klass);
rb_gc_mark(self->descriptor_pool);
}
static const rb_data_type_t Descriptor_type = {
"Google::Protobuf::Descriptor",
{Descriptor_mark, RUBY_DEFAULT_FREE, NULL},
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
};
static Descriptor* ruby_to_Descriptor(VALUE val) {
Descriptor* ret;
TypedData_Get_Struct(val, Descriptor, &Descriptor_type, ret);
return ret;
}
/*
* call-seq:
* Descriptor.new => descriptor
*
* Creates a new, empty, message type descriptor. At a minimum, its name must be
* set before it is added to a pool. It cannot be used to create messages until
* it is added to a pool, after which it becomes immutable (as part of a
* finalization process).
*/
static VALUE Descriptor_alloc(VALUE klass) {
Descriptor* self = ALLOC(Descriptor);
VALUE ret = TypedData_Wrap_Struct(klass, &Descriptor_type, self);
self->msgdef = NULL;
self->klass = Qnil;
self->descriptor_pool = Qnil;
return ret;
}
/*
* call-seq:
* Descriptor.new(c_only_cookie, ptr) => Descriptor
*
* Creates a descriptor wrapper object. May only be called from C.
*/
static VALUE Descriptor_initialize(VALUE _self, VALUE cookie,
VALUE descriptor_pool, VALUE ptr) {
Descriptor* self = ruby_to_Descriptor(_self);
if (cookie != c_only_cookie) {
rb_raise(rb_eRuntimeError,
"Descriptor objects may not be created from Ruby.");
}
self->descriptor_pool = descriptor_pool;
self->msgdef = (const upb_msgdef*)NUM2ULL(ptr);
return Qnil;
}
/*
* call-seq:
* Descriptor.file_descriptor
*
* Returns the FileDescriptor object this message belongs to.
*/
static VALUE Descriptor_file_descriptor(VALUE _self) {
Descriptor* self = ruby_to_Descriptor(_self);
return get_filedef_obj(self->descriptor_pool, upb_msgdef_file(self->msgdef));
}
/*
* call-seq:
* Descriptor.name => name
*
* Returns the name of this message type as a fully-qualified string (e.g.,
* My.Package.MessageType).
*/
static VALUE Descriptor_name(VALUE _self) {
Descriptor* self = ruby_to_Descriptor(_self);
return rb_str_maybe_null(upb_msgdef_fullname(self->msgdef));
}
/*
* call-seq:
* Descriptor.each(&block)
*
* Iterates over fields in this message type, yielding to the block on each one.
*/
static VALUE Descriptor_each(VALUE _self) {
Descriptor* self = ruby_to_Descriptor(_self);
upb_msg_field_iter it;
for (upb_msg_field_begin(&it, self->msgdef);
!upb_msg_field_done(&it);
upb_msg_field_next(&it)) {
const upb_fielddef* field = upb_msg_iter_field(&it);
VALUE obj = get_fielddef_obj(self->descriptor_pool, field);
rb_yield(obj);
}
return Qnil;
}
/*
* call-seq:
* Descriptor.lookup(name) => FieldDescriptor
*
* Returns the field descriptor for the field with the given name, if present,
* or nil if none.
*/
static VALUE Descriptor_lookup(VALUE _self, VALUE name) {
Descriptor* self = ruby_to_Descriptor(_self);
const char* s = get_str(name);
const upb_fielddef* field = upb_msgdef_ntofz(self->msgdef, s);
if (field == NULL) {
return Qnil;
}
return get_fielddef_obj(self->descriptor_pool, field);
}
/*
* call-seq:
* Descriptor.each_oneof(&block) => nil
*
* Invokes the given block for each oneof in this message type, passing the
* corresponding OneofDescriptor.
*/
static VALUE Descriptor_each_oneof(VALUE _self) {
Descriptor* self = ruby_to_Descriptor(_self);
upb_msg_oneof_iter it;
for (upb_msg_oneof_begin(&it, self->msgdef);
!upb_msg_oneof_done(&it);
upb_msg_oneof_next(&it)) {
const upb_oneofdef* oneof = upb_msg_iter_oneof(&it);
VALUE obj = get_oneofdef_obj(self->descriptor_pool, oneof);
rb_yield(obj);
}
return Qnil;
}
/*
* call-seq:
* Descriptor.lookup_oneof(name) => OneofDescriptor
*
* Returns the oneof descriptor for the oneof with the given name, if present,
* or nil if none.
*/
static VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
Descriptor* self = ruby_to_Descriptor(_self);
const char* s = get_str(name);
const upb_oneofdef* oneof = upb_msgdef_ntooz(self->msgdef, s);
if (oneof == NULL) {
return Qnil;
}
return get_oneofdef_obj(self->descriptor_pool, oneof);
}
/*
* call-seq:
* Descriptor.msgclass => message_klass
*
* Returns the Ruby class created for this message type.
*/
static VALUE Descriptor_msgclass(VALUE _self) {
Descriptor* self = ruby_to_Descriptor(_self);
if (self->klass == Qnil) {
self->klass = build_class_from_descriptor(_self);
}
return self->klass;
}
static void Descriptor_register(VALUE module) {
VALUE klass = rb_define_class_under(
module, "Descriptor", rb_cObject);
rb_define_alloc_func(klass, Descriptor_alloc);
rb_define_method(klass, "initialize", Descriptor_initialize, 3);
rb_define_method(klass, "each", Descriptor_each, 0);
rb_define_method(klass, "lookup", Descriptor_lookup, 1);
rb_define_method(klass, "each_oneof", Descriptor_each_oneof, 0);
rb_define_method(klass, "lookup_oneof", Descriptor_lookup_oneof, 1);
rb_define_method(klass, "msgclass", Descriptor_msgclass, 0);
rb_define_method(klass, "name", Descriptor_name, 0);
rb_define_method(klass, "file_descriptor", Descriptor_file_descriptor, 0);
rb_include_module(klass, rb_mEnumerable);
rb_gc_register_address(&cDescriptor);
cDescriptor = klass;
}
// -----------------------------------------------------------------------------
// FileDescriptor.
// -----------------------------------------------------------------------------
typedef struct {
const upb_filedef* filedef;
VALUE descriptor_pool; // Owns the upb_filedef.
} FileDescriptor;
static VALUE cFileDescriptor = Qnil;
static void FileDescriptor_mark(void* _self) {
FileDescriptor* self = _self;
rb_gc_mark(self->descriptor_pool);
}
static const rb_data_type_t FileDescriptor_type = {
"Google::Protobuf::FileDescriptor",
{FileDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
};
static FileDescriptor* ruby_to_FileDescriptor(VALUE val) {
FileDescriptor* ret;
TypedData_Get_Struct(val, FileDescriptor, &FileDescriptor_type, ret);
return ret;
}
static VALUE FileDescriptor_alloc(VALUE klass) {
FileDescriptor* self = ALLOC(FileDescriptor);
VALUE ret = TypedData_Wrap_Struct(klass, &FileDescriptor_type, self);
self->descriptor_pool = Qnil;
self->filedef = NULL;
return ret;
}
/*
* call-seq:
* FileDescriptor.new => file
*
* Returns a new file descriptor. The syntax must be set before it's passed
* to a builder.
*/
static VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
VALUE descriptor_pool, VALUE ptr) {
FileDescriptor* self = ruby_to_FileDescriptor(_self);
if (cookie != c_only_cookie) {
rb_raise(rb_eRuntimeError,
"Descriptor objects may not be created from Ruby.");
}
self->descriptor_pool = descriptor_pool;
self->filedef = (const upb_filedef*)NUM2ULL(ptr);
return Qnil;
}
/*
* call-seq:
* FileDescriptor.name => name
*
* Returns the name of the file.
*/
static VALUE FileDescriptor_name(VALUE _self) {
FileDescriptor* self = ruby_to_FileDescriptor(_self);
const char* name = upb_filedef_name(self->filedef);
return name == NULL ? Qnil : rb_str_new2(name);
}
/*
* call-seq:
* FileDescriptor.syntax => syntax
*
* Returns this file descriptors syntax.
*
* Valid syntax versions are:
* :proto2 or :proto3.
*/
static VALUE FileDescriptor_syntax(VALUE _self) {
FileDescriptor* self = ruby_to_FileDescriptor(_self);
switch (upb_filedef_syntax(self->filedef)) {
case UPB_SYNTAX_PROTO3: return ID2SYM(rb_intern("proto3"));
case UPB_SYNTAX_PROTO2: return ID2SYM(rb_intern("proto2"));
default: return Qnil;
}
}
static void FileDescriptor_register(VALUE module) {
VALUE klass = rb_define_class_under(
module, "FileDescriptor", rb_cObject);
rb_define_alloc_func(klass, FileDescriptor_alloc);
rb_define_method(klass, "initialize", FileDescriptor_initialize, 3);
rb_define_method(klass, "name", FileDescriptor_name, 0);
rb_define_method(klass, "syntax", FileDescriptor_syntax, 0);
rb_gc_register_address(&cFileDescriptor);
cFileDescriptor = klass;
}
// -----------------------------------------------------------------------------
// FieldDescriptor.
// -----------------------------------------------------------------------------
typedef struct {
const upb_fielddef* fielddef;
VALUE descriptor_pool; // Owns the upb_fielddef.
} FieldDescriptor;
static VALUE cFieldDescriptor = Qnil;
static void FieldDescriptor_mark(void* _self) {
FieldDescriptor* self = _self;
rb_gc_mark(self->descriptor_pool);
}
static const rb_data_type_t FieldDescriptor_type = {
"Google::Protobuf::FieldDescriptor",
{FieldDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
};
static FieldDescriptor* ruby_to_FieldDescriptor(VALUE val) {
FieldDescriptor* ret;
TypedData_Get_Struct(val, FieldDescriptor, &FieldDescriptor_type, ret);
return ret;
}
/*
* call-seq:
* FieldDescriptor.new => field
*
* Returns a new field descriptor. Its name, type, etc. must be set before it is
* added to a message type.
*/
static VALUE FieldDescriptor_alloc(VALUE klass) {
FieldDescriptor* self = ALLOC(FieldDescriptor);
VALUE ret = TypedData_Wrap_Struct(klass, &FieldDescriptor_type, self);
self->fielddef = NULL;
return ret;
}
/*
* call-seq:
* EnumDescriptor.new(c_only_cookie, pool, ptr) => EnumDescriptor
*
* Creates a descriptor wrapper object. May only be called from C.
*/
static VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
VALUE descriptor_pool, VALUE ptr) {
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
if (cookie != c_only_cookie) {
rb_raise(rb_eRuntimeError,
"Descriptor objects may not be created from Ruby.");
}
self->descriptor_pool = descriptor_pool;
self->fielddef = (const upb_fielddef*)NUM2ULL(ptr);
return Qnil;
}
/*
* call-seq:
* FieldDescriptor.name => name
*
* Returns the name of this field.
*/
static VALUE FieldDescriptor_name(VALUE _self) {
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
return rb_str_maybe_null(upb_fielddef_name(self->fielddef));
}
// Non-static, exposed to other .c files.
upb_fieldtype_t ruby_to_fieldtype(VALUE type) {
if (TYPE(type) != T_SYMBOL) {
rb_raise(rb_eArgError, "Expected symbol for field type.");
}
#define CONVERT(upb, ruby) \
if (SYM2ID(type) == rb_intern( # ruby )) { \
return UPB_TYPE_ ## upb; \
}
CONVERT(FLOAT, float);
CONVERT(DOUBLE, double);
CONVERT(BOOL, bool);
CONVERT(STRING, string);
CONVERT(BYTES, bytes);
CONVERT(MESSAGE, message);
CONVERT(ENUM, enum);
CONVERT(INT32, int32);
CONVERT(INT64, int64);
CONVERT(UINT32, uint32);
CONVERT(UINT64, uint64);
#undef CONVERT
rb_raise(rb_eArgError, "Unknown field type.");
return 0;
}
static upb_descriptortype_t ruby_to_descriptortype(VALUE type) {
if (TYPE(type) != T_SYMBOL) {
rb_raise(rb_eArgError, "Expected symbol for field type.");
}
#define CONVERT(upb, ruby) \
if (SYM2ID(type) == rb_intern( # ruby )) { \
return UPB_DESCRIPTOR_TYPE_ ## upb; \
}
CONVERT(FLOAT, float);
CONVERT(DOUBLE, double);
CONVERT(BOOL, bool);
CONVERT(STRING, string);
CONVERT(BYTES, bytes);
CONVERT(MESSAGE, message);
CONVERT(GROUP, group);
CONVERT(ENUM, enum);
CONVERT(INT32, int32);
CONVERT(INT64, int64);
CONVERT(UINT32, uint32);
CONVERT(UINT64, uint64);
CONVERT(SINT32, sint32);
CONVERT(SINT64, sint64);
CONVERT(FIXED32, fixed32);
CONVERT(FIXED64, fixed64);
CONVERT(SFIXED32, sfixed32);
CONVERT(SFIXED64, sfixed64);
#undef CONVERT
rb_raise(rb_eArgError, "Unknown field type.");
return 0;
}
static VALUE descriptortype_to_ruby(upb_descriptortype_t type) {
switch (type) {
#define CONVERT(upb, ruby) \
case UPB_DESCRIPTOR_TYPE_ ## upb : return ID2SYM(rb_intern( # ruby ));
CONVERT(FLOAT, float);
CONVERT(DOUBLE, double);
CONVERT(BOOL, bool);
CONVERT(STRING, string);
CONVERT(BYTES, bytes);
CONVERT(MESSAGE, message);
CONVERT(GROUP, group);
CONVERT(ENUM, enum);
CONVERT(INT32, int32);
CONVERT(INT64, int64);
CONVERT(UINT32, uint32);
CONVERT(UINT64, uint64);
CONVERT(SINT32, sint32);
CONVERT(SINT64, sint64);
CONVERT(FIXED32, fixed32);
CONVERT(FIXED64, fixed64);
CONVERT(SFIXED32, sfixed32);
CONVERT(SFIXED64, sfixed64);
#undef CONVERT
}
return Qnil;
}
/*
* call-seq:
* FieldDescriptor.type => type
*
* Returns this field's type, as a Ruby symbol, or nil if not yet set.
*
* Valid field types are:
* :int32, :int64, :uint32, :uint64, :float, :double, :bool, :string,
* :bytes, :message.
*/
static VALUE FieldDescriptor__type(VALUE _self) {
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
return descriptortype_to_ruby(upb_fielddef_descriptortype(self->fielddef));
}
/*
* call-seq:
* FieldDescriptor.default => default
*
* Returns this field's default, as a Ruby object, or nil if not yet set.
*/
static VALUE FieldDescriptor_default(VALUE _self) {
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
const upb_fielddef *f = self->fielddef;
upb_msgval default_val = {0};
if (upb_fielddef_issubmsg(f)) {
return Qnil;
} else if (!upb_fielddef_isseq(f)) {
default_val = upb_fielddef_default(f);
}
return Convert_UpbToRuby(default_val, TypeInfo_get(self->fielddef), Qnil);
}
/*
* call-seq:
* FieldDescriptor.label => label
*
* Returns this field's label (i.e., plurality), as a Ruby symbol.
*
* Valid field labels are:
* :optional, :repeated
*/
static VALUE FieldDescriptor_label(VALUE _self) {
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
switch (upb_fielddef_label(self->fielddef)) {
#define CONVERT(upb, ruby) \
case UPB_LABEL_ ## upb : return ID2SYM(rb_intern( # ruby ));
CONVERT(OPTIONAL, optional);
CONVERT(REQUIRED, required);
CONVERT(REPEATED, repeated);
#undef CONVERT
}
return Qnil;
}
/*
* call-seq:
* FieldDescriptor.number => number
*
* Returns the tag number for this field.
*/
static VALUE FieldDescriptor_number(VALUE _self) {
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
return INT2NUM(upb_fielddef_number(self->fielddef));
}
/*
* call-seq:
* FieldDescriptor.submsg_name => submsg_name
*
* Returns the name of the message or enum type corresponding to this field, if
* it is a message or enum field (respectively), or nil otherwise. This type
* name will be resolved within the context of the pool to which the containing
* message type is added.
*/
static VALUE FieldDescriptor_submsg_name(VALUE _self) {
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
switch (upb_fielddef_type(self->fielddef)) {
case UPB_TYPE_ENUM:
return rb_str_new2(
upb_enumdef_fullname(upb_fielddef_enumsubdef(self->fielddef)));
case UPB_TYPE_MESSAGE:
return rb_str_new2(
upb_msgdef_fullname(upb_fielddef_msgsubdef(self->fielddef)));
default:
return Qnil;
}
}
/*
* call-seq:
* FieldDescriptor.subtype => message_or_enum_descriptor
*
* Returns the message or enum descriptor corresponding to this field's type if
* it is a message or enum field, respectively, or nil otherwise. Cannot be
* called *until* the containing message type is added to a pool (and thus
* resolved).
*/
static VALUE FieldDescriptor_subtype(VALUE _self) {
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
switch (upb_fielddef_type(self->fielddef)) {
case UPB_TYPE_ENUM:
return get_enumdef_obj(self->descriptor_pool,
upb_fielddef_enumsubdef(self->fielddef));
case UPB_TYPE_MESSAGE:
return get_msgdef_obj(self->descriptor_pool,
upb_fielddef_msgsubdef(self->fielddef));
default:
return Qnil;
}
}
/*
* call-seq:
* FieldDescriptor.get(message) => value
*
* Returns the value set for this field on the given message. Raises an
* exception if message is of the wrong type.
*/
static VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
const upb_msgdef *m;
Message_Get(msg_rb, &m);
if (m != upb_fielddef_containingtype(self->fielddef)) {
rb_raise(cTypeError, "get method called on wrong message type");
}
return Message_getfield(msg_rb, self->fielddef);
}
/*
* call-seq:
* FieldDescriptor.has?(message) => boolean
*
* Returns whether the value is set on the given message. Raises an
* exception when calling for fields that do not have presence.
*/
static VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
const upb_msgdef *m;
const upb_msgdef *msg = Message_Get(msg_rb, &m);
if (m != upb_fielddef_containingtype(self->fielddef)) {
rb_raise(cTypeError, "has method called on wrong message type");
} else if (!upb_fielddef_haspresence(self->fielddef)) {
rb_raise(rb_eArgError, "does not track presence");
}
return upb_msg_has(msg, self->fielddef) ? Qtrue : Qfalse;
}
/*
* call-seq:
* FieldDescriptor.clear(message)
*
* Clears the field from the message if it's set.
*/
static VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {