google-protobuf 3.14.0 → 3.21.0
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.
Potentially problematic release.
This version of google-protobuf might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/ext/google/protobuf_c/convert.c +361 -0
- data/ext/google/protobuf_c/convert.h +75 -0
- data/ext/google/protobuf_c/defs.c +576 -1662
- data/ext/google/protobuf_c/defs.h +107 -0
- data/ext/google/protobuf_c/extconf.rb +13 -6
- data/ext/google/protobuf_c/map.c +329 -463
- data/ext/google/protobuf_c/map.h +66 -0
- data/ext/google/protobuf_c/message.c +1010 -467
- data/ext/google/protobuf_c/message.h +104 -0
- data/ext/google/protobuf_c/protobuf.c +411 -67
- data/ext/google/protobuf_c/protobuf.h +49 -596
- data/ext/google/protobuf_c/repeated_field.c +299 -328
- data/ext/google/protobuf_c/repeated_field.h +63 -0
- data/ext/google/protobuf_c/ruby-upb.c +11115 -0
- data/ext/google/protobuf_c/ruby-upb.h +5612 -0
- data/ext/google/protobuf_c/third_party/utf8_range/LICENSE +21 -0
- data/ext/google/protobuf_c/third_party/utf8_range/naive.c +92 -0
- data/ext/google/protobuf_c/third_party/utf8_range/range2-neon.c +157 -0
- data/ext/google/protobuf_c/third_party/utf8_range/range2-sse.c +170 -0
- data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.h +9 -0
- data/ext/google/protobuf_c/wrap_memcpy.c +4 -3
- data/lib/google/protobuf/api_pb.rb +1 -0
- data/lib/google/protobuf/descriptor_dsl.rb +465 -0
- data/lib/google/protobuf/descriptor_pb.rb +269 -0
- data/lib/google/protobuf/message_exts.rb +2 -2
- data/lib/google/protobuf/repeated_field.rb +15 -2
- data/lib/google/protobuf/type_pb.rb +1 -0
- data/lib/google/protobuf/well_known_types.rb +12 -2
- data/lib/google/protobuf.rb +5 -73
- data/tests/basic.rb +209 -13
- data/tests/stress.rb +1 -1
- metadata +23 -32
- data/ext/google/protobuf_c/encode_decode.c +0 -1795
- data/ext/google/protobuf_c/storage.c +0 -1198
- data/ext/google/protobuf_c/upb.c +0 -13817
- data/ext/google/protobuf_c/upb.h +0 -6777
@@ -30,8 +30,28 @@
|
|
30
30
|
|
31
31
|
#include <ctype.h>
|
32
32
|
#include <errno.h>
|
33
|
+
#include <ruby/version.h>
|
34
|
+
|
35
|
+
#include "convert.h"
|
36
|
+
#include "message.h"
|
33
37
|
#include "protobuf.h"
|
34
38
|
|
39
|
+
// -----------------------------------------------------------------------------
|
40
|
+
// Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
|
41
|
+
// instances.
|
42
|
+
// -----------------------------------------------------------------------------
|
43
|
+
|
44
|
+
static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_MessageDef* def);
|
45
|
+
static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_EnumDef* def);
|
46
|
+
static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_FieldDef* def);
|
47
|
+
static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_FileDef* def);
|
48
|
+
static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_OneofDef* def);
|
49
|
+
|
50
|
+
// A distinct object that is not accessible from Ruby. We use this as a
|
51
|
+
// constructor argument to enforce that certain objects cannot be created from
|
52
|
+
// Ruby.
|
53
|
+
VALUE c_only_cookie = Qnil;
|
54
|
+
|
35
55
|
// -----------------------------------------------------------------------------
|
36
56
|
// Common utilities.
|
37
57
|
// -----------------------------------------------------------------------------
|
@@ -48,334 +68,48 @@ static VALUE rb_str_maybe_null(const char* s) {
|
|
48
68
|
return rb_str_new2(s);
|
49
69
|
}
|
50
70
|
|
51
|
-
static void rewrite_enum_default(const upb_symtab* symtab,
|
52
|
-
google_protobuf_FileDescriptorProto* file,
|
53
|
-
google_protobuf_FieldDescriptorProto* field) {
|
54
|
-
upb_strview defaultval;
|
55
|
-
const char *type_name_str;
|
56
|
-
char *end;
|
57
|
-
long val;
|
58
|
-
const upb_enumdef *e;
|
59
|
-
upb_strview type_name;
|
60
|
-
|
61
|
-
/* Look for TYPE_ENUM fields that have a default. */
|
62
|
-
if (google_protobuf_FieldDescriptorProto_type(field) !=
|
63
|
-
google_protobuf_FieldDescriptorProto_TYPE_ENUM ||
|
64
|
-
!google_protobuf_FieldDescriptorProto_has_default_value(field) ||
|
65
|
-
!google_protobuf_FieldDescriptorProto_has_type_name(field)) {
|
66
|
-
return;
|
67
|
-
}
|
68
|
-
|
69
|
-
defaultval = google_protobuf_FieldDescriptorProto_default_value(field);
|
70
|
-
type_name = google_protobuf_FieldDescriptorProto_type_name(field);
|
71
|
-
|
72
|
-
if (defaultval.size == 0 || !isdigit(defaultval.data[0])) {
|
73
|
-
return;
|
74
|
-
}
|
75
|
-
|
76
|
-
if (type_name.size == 0 || type_name.data[0] != '.') {
|
77
|
-
return;
|
78
|
-
}
|
79
|
-
|
80
|
-
type_name_str = type_name.data + 1;
|
81
|
-
|
82
|
-
errno = 0;
|
83
|
-
val = strtol(defaultval.data, &end, 10);
|
84
|
-
|
85
|
-
if (errno != 0 || *end != 0 || val < INT32_MIN || val > INT32_MAX) {
|
86
|
-
return;
|
87
|
-
}
|
88
|
-
|
89
|
-
/* Now find the corresponding enum definition. */
|
90
|
-
e = upb_symtab_lookupenum(symtab, type_name_str);
|
91
|
-
if (e) {
|
92
|
-
/* Look in previously loaded files. */
|
93
|
-
const char *label = upb_enumdef_iton(e, val);
|
94
|
-
if (!label) {
|
95
|
-
return;
|
96
|
-
}
|
97
|
-
google_protobuf_FieldDescriptorProto_set_default_value(
|
98
|
-
field, upb_strview_makez(label));
|
99
|
-
} else {
|
100
|
-
/* Look in enums defined in this file. */
|
101
|
-
const google_protobuf_EnumDescriptorProto* matching_enum = NULL;
|
102
|
-
size_t i, n;
|
103
|
-
const google_protobuf_EnumDescriptorProto* const* enums =
|
104
|
-
google_protobuf_FileDescriptorProto_enum_type(file, &n);
|
105
|
-
const google_protobuf_EnumValueDescriptorProto* const* values;
|
106
|
-
|
107
|
-
for (i = 0; i < n; i++) {
|
108
|
-
if (upb_strview_eql(google_protobuf_EnumDescriptorProto_name(enums[i]),
|
109
|
-
upb_strview_makez(type_name_str))) {
|
110
|
-
matching_enum = enums[i];
|
111
|
-
break;
|
112
|
-
}
|
113
|
-
}
|
114
|
-
|
115
|
-
if (!matching_enum) {
|
116
|
-
return;
|
117
|
-
}
|
118
|
-
|
119
|
-
values = google_protobuf_EnumDescriptorProto_value(matching_enum, &n);
|
120
|
-
for (i = 0; i < n; i++) {
|
121
|
-
if (google_protobuf_EnumValueDescriptorProto_number(values[i]) == val) {
|
122
|
-
google_protobuf_FieldDescriptorProto_set_default_value(
|
123
|
-
field, google_protobuf_EnumValueDescriptorProto_name(values[i]));
|
124
|
-
return;
|
125
|
-
}
|
126
|
-
}
|
127
|
-
|
128
|
-
/* We failed to find an enum default. But we'll just leave the enum
|
129
|
-
* untouched and let the normal def-building code catch it. */
|
130
|
-
}
|
131
|
-
}
|
132
|
-
|
133
|
-
/* Historically we allowed enum defaults to be specified as a number. In
|
134
|
-
* retrospect this was a mistake as descriptors require defaults to be
|
135
|
-
* specified as a label. This can make a difference if multiple labels have the
|
136
|
-
* same number.
|
137
|
-
*
|
138
|
-
* Here we do a pass over all enum defaults and rewrite numeric defaults by
|
139
|
-
* looking up their labels. This is complicated by the fact that the enum
|
140
|
-
* definition can live in either the symtab or the file_proto.
|
141
|
-
* */
|
142
|
-
static void rewrite_enum_defaults(
|
143
|
-
const upb_symtab* symtab, google_protobuf_FileDescriptorProto* file_proto) {
|
144
|
-
size_t i, n;
|
145
|
-
google_protobuf_DescriptorProto** msgs =
|
146
|
-
google_protobuf_FileDescriptorProto_mutable_message_type(file_proto, &n);
|
147
|
-
|
148
|
-
for (i = 0; i < n; i++) {
|
149
|
-
size_t j, m;
|
150
|
-
google_protobuf_FieldDescriptorProto** fields =
|
151
|
-
google_protobuf_DescriptorProto_mutable_field(msgs[i], &m);
|
152
|
-
for (j = 0; j < m; j++) {
|
153
|
-
rewrite_enum_default(symtab, file_proto, fields[j]);
|
154
|
-
}
|
155
|
-
}
|
156
|
-
}
|
157
|
-
|
158
|
-
static void remove_path(upb_strview *name) {
|
159
|
-
const char* last = strrchr(name->data, '.');
|
160
|
-
if (last) {
|
161
|
-
size_t remove = last - name->data + 1;
|
162
|
-
name->data += remove;
|
163
|
-
name->size -= remove;
|
164
|
-
}
|
165
|
-
}
|
166
|
-
|
167
|
-
static void rewrite_nesting(VALUE msg_ent, google_protobuf_DescriptorProto* msg,
|
168
|
-
google_protobuf_DescriptorProto* const* msgs,
|
169
|
-
google_protobuf_EnumDescriptorProto* const* enums,
|
170
|
-
upb_arena *arena) {
|
171
|
-
VALUE submsgs = rb_hash_aref(msg_ent, ID2SYM(rb_intern("msgs")));
|
172
|
-
VALUE enum_pos = rb_hash_aref(msg_ent, ID2SYM(rb_intern("enums")));
|
173
|
-
int submsg_count;
|
174
|
-
int enum_count;
|
175
|
-
int i;
|
176
|
-
google_protobuf_DescriptorProto** msg_msgs;
|
177
|
-
google_protobuf_EnumDescriptorProto** msg_enums;
|
178
|
-
|
179
|
-
Check_Type(submsgs, T_ARRAY);
|
180
|
-
Check_Type(enum_pos, T_ARRAY);
|
181
|
-
|
182
|
-
submsg_count = RARRAY_LEN(submsgs);
|
183
|
-
enum_count = RARRAY_LEN(enum_pos);
|
184
|
-
|
185
|
-
msg_msgs = google_protobuf_DescriptorProto_resize_nested_type(
|
186
|
-
msg, submsg_count, arena);
|
187
|
-
msg_enums =
|
188
|
-
google_protobuf_DescriptorProto_resize_enum_type(msg, enum_count, arena);
|
189
|
-
|
190
|
-
for (i = 0; i < submsg_count; i++) {
|
191
|
-
VALUE submsg_ent = RARRAY_PTR(submsgs)[i];
|
192
|
-
VALUE pos = rb_hash_aref(submsg_ent, ID2SYM(rb_intern("pos")));
|
193
|
-
upb_strview name;
|
194
|
-
|
195
|
-
msg_msgs[i] = msgs[NUM2INT(pos)];
|
196
|
-
name = google_protobuf_DescriptorProto_name(msg_msgs[i]);
|
197
|
-
remove_path(&name);
|
198
|
-
google_protobuf_DescriptorProto_set_name(msg_msgs[i], name);
|
199
|
-
rewrite_nesting(submsg_ent, msg_msgs[i], msgs, enums, arena);
|
200
|
-
}
|
201
|
-
|
202
|
-
for (i = 0; i < enum_count; i++) {
|
203
|
-
VALUE pos = RARRAY_PTR(enum_pos)[i];
|
204
|
-
msg_enums[i] = enums[NUM2INT(pos)];
|
205
|
-
}
|
206
|
-
}
|
207
|
-
|
208
|
-
/* We have to do some relatively complicated logic here for backward
|
209
|
-
* compatibility.
|
210
|
-
*
|
211
|
-
* In descriptor.proto, messages are nested inside other messages if that is
|
212
|
-
* what the original .proto file looks like. For example, suppose we have this
|
213
|
-
* foo.proto:
|
214
|
-
*
|
215
|
-
* package foo;
|
216
|
-
* message Bar {
|
217
|
-
* message Baz {}
|
218
|
-
* }
|
219
|
-
*
|
220
|
-
* The descriptor for this must look like this:
|
221
|
-
*
|
222
|
-
* file {
|
223
|
-
* name: "test.proto"
|
224
|
-
* package: "foo"
|
225
|
-
* message_type {
|
226
|
-
* name: "Bar"
|
227
|
-
* nested_type {
|
228
|
-
* name: "Baz"
|
229
|
-
* }
|
230
|
-
* }
|
231
|
-
* }
|
232
|
-
*
|
233
|
-
* However, the Ruby generated code has always generated messages in a flat,
|
234
|
-
* non-nested way:
|
235
|
-
*
|
236
|
-
* Google::Protobuf::DescriptorPool.generated_pool.build do
|
237
|
-
* add_message "foo.Bar" do
|
238
|
-
* end
|
239
|
-
* add_message "foo.Bar.Baz" do
|
240
|
-
* end
|
241
|
-
* end
|
242
|
-
*
|
243
|
-
* Here we need to do a translation where we turn this generated code into the
|
244
|
-
* above descriptor. We need to infer that "foo" is the package name, and not
|
245
|
-
* a message itself.
|
246
|
-
*
|
247
|
-
* We delegate to Ruby to compute the transformation, for more concice and
|
248
|
-
* readable code than we can do in C */
|
249
|
-
static void rewrite_names(VALUE _file_builder,
|
250
|
-
google_protobuf_FileDescriptorProto* file_proto) {
|
251
|
-
FileBuilderContext* file_builder = ruby_to_FileBuilderContext(_file_builder);
|
252
|
-
upb_arena *arena = file_builder->arena;
|
253
|
-
// Build params (package, msg_names, enum_names).
|
254
|
-
VALUE package = Qnil;
|
255
|
-
VALUE msg_names = rb_ary_new();
|
256
|
-
VALUE enum_names = rb_ary_new();
|
257
|
-
size_t msg_count, enum_count, i;
|
258
|
-
VALUE new_package, nesting, msg_ents, enum_ents;
|
259
|
-
google_protobuf_DescriptorProto** msgs;
|
260
|
-
google_protobuf_EnumDescriptorProto** enums;
|
261
|
-
|
262
|
-
if (google_protobuf_FileDescriptorProto_has_package(file_proto)) {
|
263
|
-
upb_strview package_str =
|
264
|
-
google_protobuf_FileDescriptorProto_package(file_proto);
|
265
|
-
package = rb_str_new(package_str.data, package_str.size);
|
266
|
-
}
|
267
|
-
|
268
|
-
msgs = google_protobuf_FileDescriptorProto_mutable_message_type(file_proto,
|
269
|
-
&msg_count);
|
270
|
-
for (i = 0; i < msg_count; i++) {
|
271
|
-
upb_strview name = google_protobuf_DescriptorProto_name(msgs[i]);
|
272
|
-
rb_ary_push(msg_names, rb_str_new(name.data, name.size));
|
273
|
-
}
|
274
|
-
|
275
|
-
enums = google_protobuf_FileDescriptorProto_mutable_enum_type(file_proto,
|
276
|
-
&enum_count);
|
277
|
-
for (i = 0; i < enum_count; i++) {
|
278
|
-
upb_strview name = google_protobuf_EnumDescriptorProto_name(enums[i]);
|
279
|
-
rb_ary_push(enum_names, rb_str_new(name.data, name.size));
|
280
|
-
}
|
281
|
-
|
282
|
-
{
|
283
|
-
// Call Ruby code to calculate package name and nesting.
|
284
|
-
VALUE args[3] = { package, msg_names, enum_names };
|
285
|
-
VALUE internal = rb_eval_string("Google::Protobuf::Internal");
|
286
|
-
VALUE ret = rb_funcallv(internal, rb_intern("fixup_descriptor"), 3, args);
|
287
|
-
|
288
|
-
new_package = rb_ary_entry(ret, 0);
|
289
|
-
nesting = rb_ary_entry(ret, 1);
|
290
|
-
}
|
291
|
-
|
292
|
-
// Rewrite package and names.
|
293
|
-
if (new_package != Qnil) {
|
294
|
-
upb_strview new_package_str =
|
295
|
-
FileBuilderContext_strdup(_file_builder, new_package);
|
296
|
-
google_protobuf_FileDescriptorProto_set_package(file_proto,
|
297
|
-
new_package_str);
|
298
|
-
}
|
299
|
-
|
300
|
-
for (i = 0; i < msg_count; i++) {
|
301
|
-
upb_strview name = google_protobuf_DescriptorProto_name(msgs[i]);
|
302
|
-
remove_path(&name);
|
303
|
-
google_protobuf_DescriptorProto_set_name(msgs[i], name);
|
304
|
-
}
|
305
|
-
|
306
|
-
for (i = 0; i < enum_count; i++) {
|
307
|
-
upb_strview name = google_protobuf_EnumDescriptorProto_name(enums[i]);
|
308
|
-
remove_path(&name);
|
309
|
-
google_protobuf_EnumDescriptorProto_set_name(enums[i], name);
|
310
|
-
}
|
311
|
-
|
312
|
-
// Rewrite nesting.
|
313
|
-
msg_ents = rb_hash_aref(nesting, ID2SYM(rb_intern("msgs")));
|
314
|
-
enum_ents = rb_hash_aref(nesting, ID2SYM(rb_intern("enums")));
|
315
|
-
|
316
|
-
Check_Type(msg_ents, T_ARRAY);
|
317
|
-
Check_Type(enum_ents, T_ARRAY);
|
318
|
-
|
319
|
-
for (i = 0; i < (size_t)RARRAY_LEN(msg_ents); i++) {
|
320
|
-
VALUE msg_ent = rb_ary_entry(msg_ents, i);
|
321
|
-
VALUE pos = rb_hash_aref(msg_ent, ID2SYM(rb_intern("pos")));
|
322
|
-
msgs[i] = msgs[NUM2INT(pos)];
|
323
|
-
rewrite_nesting(msg_ent, msgs[i], msgs, enums, arena);
|
324
|
-
}
|
325
|
-
|
326
|
-
for (i = 0; i < (size_t)RARRAY_LEN(enum_ents); i++) {
|
327
|
-
VALUE enum_pos = rb_ary_entry(enum_ents, i);
|
328
|
-
enums[i] = enums[NUM2INT(enum_pos)];
|
329
|
-
}
|
330
|
-
|
331
|
-
google_protobuf_FileDescriptorProto_resize_message_type(
|
332
|
-
file_proto, RARRAY_LEN(msg_ents), arena);
|
333
|
-
google_protobuf_FileDescriptorProto_resize_enum_type(
|
334
|
-
file_proto, RARRAY_LEN(enum_ents), arena);
|
335
|
-
}
|
336
|
-
|
337
71
|
// -----------------------------------------------------------------------------
|
338
72
|
// DescriptorPool.
|
339
73
|
// -----------------------------------------------------------------------------
|
340
74
|
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
name* ruby_to_ ## name(VALUE val) { \
|
348
|
-
name* ret; \
|
349
|
-
TypedData_Get_Struct(val, name, &_ ## name ## _type, ret); \
|
350
|
-
return ret; \
|
351
|
-
} \
|
352
|
-
|
353
|
-
#define DEFINE_SELF(type, var, rb_var) \
|
354
|
-
type* var = ruby_to_ ## type(rb_var)
|
75
|
+
typedef struct {
|
76
|
+
VALUE def_to_descriptor; // Hash table of def* -> Ruby descriptor.
|
77
|
+
upb_DefPool* symtab;
|
78
|
+
} DescriptorPool;
|
79
|
+
|
80
|
+
VALUE cDescriptorPool = Qnil;
|
355
81
|
|
356
82
|
// Global singleton DescriptorPool. The user is free to create others, but this
|
357
83
|
// is used by generated code.
|
358
84
|
VALUE generated_pool = Qnil;
|
359
85
|
|
360
|
-
|
361
|
-
|
362
|
-
void DescriptorPool_mark(void* _self) {
|
86
|
+
static void DescriptorPool_mark(void* _self) {
|
363
87
|
DescriptorPool* self = _self;
|
364
88
|
rb_gc_mark(self->def_to_descriptor);
|
365
89
|
}
|
366
90
|
|
367
|
-
void DescriptorPool_free(void* _self) {
|
91
|
+
static void DescriptorPool_free(void* _self) {
|
368
92
|
DescriptorPool* self = _self;
|
93
|
+
upb_DefPool_Free(self->symtab);
|
94
|
+
xfree(self);
|
95
|
+
}
|
369
96
|
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
upb_pbcodecache_free(self->fill_method_cache);
|
376
|
-
upb_json_codecache_free(self->json_fill_method_cache);
|
97
|
+
static const rb_data_type_t DescriptorPool_type = {
|
98
|
+
"Google::Protobuf::DescriptorPool",
|
99
|
+
{DescriptorPool_mark, DescriptorPool_free, NULL},
|
100
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
|
101
|
+
};
|
377
102
|
|
378
|
-
|
103
|
+
static DescriptorPool* ruby_to_DescriptorPool(VALUE val) {
|
104
|
+
DescriptorPool* ret;
|
105
|
+
TypedData_Get_Struct(val, DescriptorPool, &DescriptorPool_type, ret);
|
106
|
+
return ret;
|
107
|
+
}
|
108
|
+
|
109
|
+
// Exposed to other modules in defs.h.
|
110
|
+
const upb_DefPool* DescriptorPool_GetSymtab(VALUE desc_pool_rb) {
|
111
|
+
DescriptorPool* pool = ruby_to_DescriptorPool(desc_pool_rb);
|
112
|
+
return pool->symtab;
|
379
113
|
}
|
380
114
|
|
381
115
|
/*
|
@@ -384,58 +118,49 @@ void DescriptorPool_free(void* _self) {
|
|
384
118
|
*
|
385
119
|
* Creates a new, empty, descriptor pool.
|
386
120
|
*/
|
387
|
-
VALUE DescriptorPool_alloc(VALUE klass) {
|
121
|
+
static VALUE DescriptorPool_alloc(VALUE klass) {
|
388
122
|
DescriptorPool* self = ALLOC(DescriptorPool);
|
389
123
|
VALUE ret;
|
390
124
|
|
391
125
|
self->def_to_descriptor = Qnil;
|
392
|
-
ret = TypedData_Wrap_Struct(klass, &
|
126
|
+
ret = TypedData_Wrap_Struct(klass, &DescriptorPool_type, self);
|
393
127
|
|
394
128
|
self->def_to_descriptor = rb_hash_new();
|
395
|
-
self->symtab =
|
396
|
-
self->
|
397
|
-
upb_handlercache_new(add_handlers_for_message, (void*)ret);
|
398
|
-
self->pb_serialize_handler_cache = upb_pb_encoder_newcache();
|
399
|
-
self->json_serialize_handler_cache = upb_json_printer_newcache(false);
|
400
|
-
self->json_serialize_handler_preserve_cache =
|
401
|
-
upb_json_printer_newcache(true);
|
402
|
-
self->fill_method_cache = upb_pbcodecache_new(self->fill_handler_cache);
|
403
|
-
self->json_fill_method_cache = upb_json_codecache_new();
|
129
|
+
self->symtab = upb_DefPool_New();
|
130
|
+
ObjectCache_Add(self->symtab, ret);
|
404
131
|
|
405
132
|
return ret;
|
406
133
|
}
|
407
134
|
|
408
|
-
void DescriptorPool_register(VALUE module) {
|
409
|
-
VALUE klass = rb_define_class_under(
|
410
|
-
module, "DescriptorPool", rb_cObject);
|
411
|
-
rb_define_alloc_func(klass, DescriptorPool_alloc);
|
412
|
-
rb_define_method(klass, "build", DescriptorPool_build, -1);
|
413
|
-
rb_define_method(klass, "lookup", DescriptorPool_lookup, 1);
|
414
|
-
rb_define_singleton_method(klass, "generated_pool",
|
415
|
-
DescriptorPool_generated_pool, 0);
|
416
|
-
rb_gc_register_address(&cDescriptorPool);
|
417
|
-
cDescriptorPool = klass;
|
418
|
-
|
419
|
-
rb_gc_register_address(&generated_pool);
|
420
|
-
generated_pool = rb_class_new_instance(0, NULL, klass);
|
421
|
-
}
|
422
|
-
|
423
135
|
/*
|
424
136
|
* call-seq:
|
425
|
-
* DescriptorPool.
|
137
|
+
* DescriptorPool.add_serialized_file(serialized_file_proto)
|
426
138
|
*
|
427
|
-
*
|
428
|
-
* added within the block are committed to the pool atomically, and may refer
|
429
|
-
* (co)recursively to each other. The user should call Builder#add_message and
|
430
|
-
* Builder#add_enum within the block as appropriate. This is the recommended,
|
431
|
-
* idiomatic way to define new message and enum types.
|
139
|
+
* Adds the given serialized FileDescriptorProto to the pool.
|
432
140
|
*/
|
433
|
-
VALUE
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
141
|
+
VALUE DescriptorPool_add_serialized_file(VALUE _self,
|
142
|
+
VALUE serialized_file_proto) {
|
143
|
+
DescriptorPool* self = ruby_to_DescriptorPool(_self);
|
144
|
+
Check_Type(serialized_file_proto, T_STRING);
|
145
|
+
VALUE arena_rb = Arena_new();
|
146
|
+
upb_Arena* arena = Arena_get(arena_rb);
|
147
|
+
google_protobuf_FileDescriptorProto* file_proto =
|
148
|
+
google_protobuf_FileDescriptorProto_parse(
|
149
|
+
RSTRING_PTR(serialized_file_proto),
|
150
|
+
RSTRING_LEN(serialized_file_proto), arena);
|
151
|
+
if (!file_proto) {
|
152
|
+
rb_raise(rb_eArgError, "Unable to parse FileDescriptorProto");
|
153
|
+
}
|
154
|
+
upb_Status status;
|
155
|
+
upb_Status_Clear(&status);
|
156
|
+
const upb_FileDef* filedef =
|
157
|
+
upb_DefPool_AddFile(self->symtab, file_proto, &status);
|
158
|
+
if (!filedef) {
|
159
|
+
rb_raise(cTypeError, "Unable to build file to DescriptorPool: %s",
|
160
|
+
upb_Status_ErrorMessage(&status));
|
161
|
+
}
|
162
|
+
RB_GC_GUARD(arena_rb);
|
163
|
+
return get_filedef_obj(_self, filedef);
|
439
164
|
}
|
440
165
|
|
441
166
|
/*
|
@@ -445,18 +170,18 @@ VALUE DescriptorPool_build(int argc, VALUE* argv, VALUE _self) {
|
|
445
170
|
* Finds a Descriptor or EnumDescriptor by name and returns it, or nil if none
|
446
171
|
* exists with the given name.
|
447
172
|
*/
|
448
|
-
VALUE DescriptorPool_lookup(VALUE _self, VALUE name) {
|
449
|
-
|
173
|
+
static VALUE DescriptorPool_lookup(VALUE _self, VALUE name) {
|
174
|
+
DescriptorPool* self = ruby_to_DescriptorPool(_self);
|
450
175
|
const char* name_str = get_str(name);
|
451
|
-
const
|
452
|
-
const
|
176
|
+
const upb_MessageDef* msgdef;
|
177
|
+
const upb_EnumDef* enumdef;
|
453
178
|
|
454
|
-
msgdef =
|
179
|
+
msgdef = upb_DefPool_FindMessageByName(self->symtab, name_str);
|
455
180
|
if (msgdef) {
|
456
181
|
return get_msgdef_obj(_self, msgdef);
|
457
182
|
}
|
458
183
|
|
459
|
-
enumdef =
|
184
|
+
enumdef = upb_DefPool_FindEnumByName(self->symtab, name_str);
|
460
185
|
if (enumdef) {
|
461
186
|
return get_enumdef_obj(_self, enumdef);
|
462
187
|
}
|
@@ -473,31 +198,53 @@ VALUE DescriptorPool_lookup(VALUE _self, VALUE name) {
|
|
473
198
|
* register types in this pool for convenience so that they do not have to hold
|
474
199
|
* a reference to a private pool instance.
|
475
200
|
*/
|
476
|
-
VALUE DescriptorPool_generated_pool(VALUE _self) {
|
201
|
+
static VALUE DescriptorPool_generated_pool(VALUE _self) {
|
477
202
|
return generated_pool;
|
478
203
|
}
|
479
204
|
|
205
|
+
static void DescriptorPool_register(VALUE module) {
|
206
|
+
VALUE klass = rb_define_class_under(module, "DescriptorPool", rb_cObject);
|
207
|
+
rb_define_alloc_func(klass, DescriptorPool_alloc);
|
208
|
+
rb_define_method(klass, "add_serialized_file",
|
209
|
+
DescriptorPool_add_serialized_file, 1);
|
210
|
+
rb_define_method(klass, "lookup", DescriptorPool_lookup, 1);
|
211
|
+
rb_define_singleton_method(klass, "generated_pool",
|
212
|
+
DescriptorPool_generated_pool, 0);
|
213
|
+
rb_gc_register_address(&cDescriptorPool);
|
214
|
+
cDescriptorPool = klass;
|
215
|
+
|
216
|
+
rb_gc_register_address(&generated_pool);
|
217
|
+
generated_pool = rb_class_new_instance(0, NULL, klass);
|
218
|
+
}
|
219
|
+
|
480
220
|
// -----------------------------------------------------------------------------
|
481
221
|
// Descriptor.
|
482
222
|
// -----------------------------------------------------------------------------
|
483
223
|
|
484
|
-
|
224
|
+
typedef struct {
|
225
|
+
const upb_MessageDef* msgdef;
|
226
|
+
VALUE klass;
|
227
|
+
VALUE descriptor_pool;
|
228
|
+
} Descriptor;
|
229
|
+
|
230
|
+
VALUE cDescriptor = Qnil;
|
485
231
|
|
486
|
-
void Descriptor_mark(void* _self) {
|
232
|
+
static void Descriptor_mark(void* _self) {
|
487
233
|
Descriptor* self = _self;
|
488
234
|
rb_gc_mark(self->klass);
|
489
235
|
rb_gc_mark(self->descriptor_pool);
|
490
|
-
if (self->layout && self->layout->empty_template) {
|
491
|
-
layout_mark(self->layout, self->layout->empty_template);
|
492
|
-
}
|
493
236
|
}
|
494
237
|
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
238
|
+
static const rb_data_type_t Descriptor_type = {
|
239
|
+
"Google::Protobuf::Descriptor",
|
240
|
+
{Descriptor_mark, RUBY_DEFAULT_FREE, NULL},
|
241
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
|
242
|
+
};
|
243
|
+
|
244
|
+
static Descriptor* ruby_to_Descriptor(VALUE val) {
|
245
|
+
Descriptor* ret;
|
246
|
+
TypedData_Get_Struct(val, Descriptor, &Descriptor_type, ret);
|
247
|
+
return ret;
|
501
248
|
}
|
502
249
|
|
503
250
|
/*
|
@@ -509,42 +256,24 @@ void Descriptor_free(void* _self) {
|
|
509
256
|
* it is added to a pool, after which it becomes immutable (as part of a
|
510
257
|
* finalization process).
|
511
258
|
*/
|
512
|
-
VALUE Descriptor_alloc(VALUE klass) {
|
259
|
+
static VALUE Descriptor_alloc(VALUE klass) {
|
513
260
|
Descriptor* self = ALLOC(Descriptor);
|
514
|
-
VALUE ret = TypedData_Wrap_Struct(klass, &
|
261
|
+
VALUE ret = TypedData_Wrap_Struct(klass, &Descriptor_type, self);
|
515
262
|
self->msgdef = NULL;
|
516
263
|
self->klass = Qnil;
|
517
264
|
self->descriptor_pool = Qnil;
|
518
|
-
self->layout = NULL;
|
519
265
|
return ret;
|
520
266
|
}
|
521
267
|
|
522
|
-
void Descriptor_register(VALUE module) {
|
523
|
-
VALUE klass = rb_define_class_under(
|
524
|
-
module, "Descriptor", rb_cObject);
|
525
|
-
rb_define_alloc_func(klass, Descriptor_alloc);
|
526
|
-
rb_define_method(klass, "initialize", Descriptor_initialize, 3);
|
527
|
-
rb_define_method(klass, "each", Descriptor_each, 0);
|
528
|
-
rb_define_method(klass, "lookup", Descriptor_lookup, 1);
|
529
|
-
rb_define_method(klass, "each_oneof", Descriptor_each_oneof, 0);
|
530
|
-
rb_define_method(klass, "lookup_oneof", Descriptor_lookup_oneof, 1);
|
531
|
-
rb_define_method(klass, "msgclass", Descriptor_msgclass, 0);
|
532
|
-
rb_define_method(klass, "name", Descriptor_name, 0);
|
533
|
-
rb_define_method(klass, "file_descriptor", Descriptor_file_descriptor, 0);
|
534
|
-
rb_include_module(klass, rb_mEnumerable);
|
535
|
-
rb_gc_register_address(&cDescriptor);
|
536
|
-
cDescriptor = klass;
|
537
|
-
}
|
538
|
-
|
539
268
|
/*
|
540
269
|
* call-seq:
|
541
270
|
* Descriptor.new(c_only_cookie, ptr) => Descriptor
|
542
271
|
*
|
543
272
|
* Creates a descriptor wrapper object. May only be called from C.
|
544
273
|
*/
|
545
|
-
VALUE Descriptor_initialize(VALUE _self, VALUE cookie,
|
546
|
-
|
547
|
-
|
274
|
+
static VALUE Descriptor_initialize(VALUE _self, VALUE cookie,
|
275
|
+
VALUE descriptor_pool, VALUE ptr) {
|
276
|
+
Descriptor* self = ruby_to_Descriptor(_self);
|
548
277
|
|
549
278
|
if (cookie != c_only_cookie) {
|
550
279
|
rb_raise(rb_eRuntimeError,
|
@@ -552,7 +281,7 @@ VALUE Descriptor_initialize(VALUE _self, VALUE cookie,
|
|
552
281
|
}
|
553
282
|
|
554
283
|
self->descriptor_pool = descriptor_pool;
|
555
|
-
self->msgdef = (const
|
284
|
+
self->msgdef = (const upb_MessageDef*)NUM2ULL(ptr);
|
556
285
|
|
557
286
|
return Qnil;
|
558
287
|
}
|
@@ -563,9 +292,10 @@ VALUE Descriptor_initialize(VALUE _self, VALUE cookie,
|
|
563
292
|
*
|
564
293
|
* Returns the FileDescriptor object this message belongs to.
|
565
294
|
*/
|
566
|
-
VALUE Descriptor_file_descriptor(VALUE _self) {
|
567
|
-
|
568
|
-
return get_filedef_obj(self->descriptor_pool,
|
295
|
+
static VALUE Descriptor_file_descriptor(VALUE _self) {
|
296
|
+
Descriptor* self = ruby_to_Descriptor(_self);
|
297
|
+
return get_filedef_obj(self->descriptor_pool,
|
298
|
+
upb_MessageDef_File(self->msgdef));
|
569
299
|
}
|
570
300
|
|
571
301
|
/*
|
@@ -575,9 +305,9 @@ VALUE Descriptor_file_descriptor(VALUE _self) {
|
|
575
305
|
* Returns the name of this message type as a fully-qualified string (e.g.,
|
576
306
|
* My.Package.MessageType).
|
577
307
|
*/
|
578
|
-
VALUE Descriptor_name(VALUE _self) {
|
579
|
-
|
580
|
-
return rb_str_maybe_null(
|
308
|
+
static VALUE Descriptor_name(VALUE _self) {
|
309
|
+
Descriptor* self = ruby_to_Descriptor(_self);
|
310
|
+
return rb_str_maybe_null(upb_MessageDef_FullName(self->msgdef));
|
581
311
|
}
|
582
312
|
|
583
313
|
/*
|
@@ -586,14 +316,12 @@ VALUE Descriptor_name(VALUE _self) {
|
|
586
316
|
*
|
587
317
|
* Iterates over fields in this message type, yielding to the block on each one.
|
588
318
|
*/
|
589
|
-
VALUE Descriptor_each(VALUE _self) {
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
for (
|
594
|
-
|
595
|
-
upb_msg_field_next(&it)) {
|
596
|
-
const upb_fielddef* field = upb_msg_iter_field(&it);
|
319
|
+
static VALUE Descriptor_each(VALUE _self) {
|
320
|
+
Descriptor* self = ruby_to_Descriptor(_self);
|
321
|
+
|
322
|
+
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);
|
597
325
|
VALUE obj = get_fielddef_obj(self->descriptor_pool, field);
|
598
326
|
rb_yield(obj);
|
599
327
|
}
|
@@ -607,10 +335,10 @@ VALUE Descriptor_each(VALUE _self) {
|
|
607
335
|
* Returns the field descriptor for the field with the given name, if present,
|
608
336
|
* or nil if none.
|
609
337
|
*/
|
610
|
-
VALUE Descriptor_lookup(VALUE _self, VALUE name) {
|
611
|
-
|
338
|
+
static VALUE Descriptor_lookup(VALUE _self, VALUE name) {
|
339
|
+
Descriptor* self = ruby_to_Descriptor(_self);
|
612
340
|
const char* s = get_str(name);
|
613
|
-
const
|
341
|
+
const upb_FieldDef* field = upb_MessageDef_FindFieldByName(self->msgdef, s);
|
614
342
|
if (field == NULL) {
|
615
343
|
return Qnil;
|
616
344
|
}
|
@@ -624,14 +352,12 @@ VALUE Descriptor_lookup(VALUE _self, VALUE name) {
|
|
624
352
|
* Invokes the given block for each oneof in this message type, passing the
|
625
353
|
* corresponding OneofDescriptor.
|
626
354
|
*/
|
627
|
-
VALUE Descriptor_each_oneof(VALUE _self) {
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
for (
|
632
|
-
|
633
|
-
upb_msg_oneof_next(&it)) {
|
634
|
-
const upb_oneofdef* oneof = upb_msg_iter_oneof(&it);
|
355
|
+
static VALUE Descriptor_each_oneof(VALUE _self) {
|
356
|
+
Descriptor* self = ruby_to_Descriptor(_self);
|
357
|
+
|
358
|
+
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);
|
635
361
|
VALUE obj = get_oneofdef_obj(self->descriptor_pool, oneof);
|
636
362
|
rb_yield(obj);
|
637
363
|
}
|
@@ -645,10 +371,10 @@ VALUE Descriptor_each_oneof(VALUE _self) {
|
|
645
371
|
* Returns the oneof descriptor for the oneof with the given name, if present,
|
646
372
|
* or nil if none.
|
647
373
|
*/
|
648
|
-
VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
|
649
|
-
|
374
|
+
static VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
|
375
|
+
Descriptor* self = ruby_to_Descriptor(_self);
|
650
376
|
const char* s = get_str(name);
|
651
|
-
const
|
377
|
+
const upb_OneofDef* oneof = upb_MessageDef_FindOneofByName(self->msgdef, s);
|
652
378
|
if (oneof == NULL) {
|
653
379
|
return Qnil;
|
654
380
|
}
|
@@ -661,32 +387,61 @@ VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
|
|
661
387
|
*
|
662
388
|
* Returns the Ruby class created for this message type.
|
663
389
|
*/
|
664
|
-
VALUE Descriptor_msgclass(VALUE _self) {
|
665
|
-
|
390
|
+
static VALUE Descriptor_msgclass(VALUE _self) {
|
391
|
+
Descriptor* self = ruby_to_Descriptor(_self);
|
666
392
|
if (self->klass == Qnil) {
|
667
393
|
self->klass = build_class_from_descriptor(_self);
|
668
394
|
}
|
669
395
|
return self->klass;
|
670
396
|
}
|
671
397
|
|
398
|
+
static void Descriptor_register(VALUE module) {
|
399
|
+
VALUE klass = rb_define_class_under(module, "Descriptor", rb_cObject);
|
400
|
+
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
|
+
|
672
414
|
// -----------------------------------------------------------------------------
|
673
415
|
// FileDescriptor.
|
674
416
|
// -----------------------------------------------------------------------------
|
675
417
|
|
676
|
-
|
418
|
+
typedef struct {
|
419
|
+
const upb_FileDef* filedef;
|
420
|
+
VALUE descriptor_pool; // Owns the upb_FileDef.
|
421
|
+
} FileDescriptor;
|
422
|
+
|
423
|
+
static VALUE cFileDescriptor = Qnil;
|
677
424
|
|
678
|
-
void FileDescriptor_mark(void* _self) {
|
425
|
+
static void FileDescriptor_mark(void* _self) {
|
679
426
|
FileDescriptor* self = _self;
|
680
427
|
rb_gc_mark(self->descriptor_pool);
|
681
428
|
}
|
682
429
|
|
683
|
-
|
684
|
-
|
430
|
+
static const rb_data_type_t FileDescriptor_type = {
|
431
|
+
"Google::Protobuf::FileDescriptor",
|
432
|
+
{FileDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
|
433
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
|
434
|
+
};
|
435
|
+
|
436
|
+
static FileDescriptor* ruby_to_FileDescriptor(VALUE val) {
|
437
|
+
FileDescriptor* ret;
|
438
|
+
TypedData_Get_Struct(val, FileDescriptor, &FileDescriptor_type, ret);
|
439
|
+
return ret;
|
685
440
|
}
|
686
441
|
|
687
|
-
VALUE FileDescriptor_alloc(VALUE klass) {
|
442
|
+
static VALUE FileDescriptor_alloc(VALUE klass) {
|
688
443
|
FileDescriptor* self = ALLOC(FileDescriptor);
|
689
|
-
VALUE ret = TypedData_Wrap_Struct(klass, &
|
444
|
+
VALUE ret = TypedData_Wrap_Struct(klass, &FileDescriptor_type, self);
|
690
445
|
self->descriptor_pool = Qnil;
|
691
446
|
self->filedef = NULL;
|
692
447
|
return ret;
|
@@ -699,9 +454,9 @@ VALUE FileDescriptor_alloc(VALUE klass) {
|
|
699
454
|
* Returns a new file descriptor. The syntax must be set before it's passed
|
700
455
|
* to a builder.
|
701
456
|
*/
|
702
|
-
VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
|
703
|
-
|
704
|
-
|
457
|
+
static VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
|
458
|
+
VALUE descriptor_pool, VALUE ptr) {
|
459
|
+
FileDescriptor* self = ruby_to_FileDescriptor(_self);
|
705
460
|
|
706
461
|
if (cookie != c_only_cookie) {
|
707
462
|
rb_raise(rb_eRuntimeError,
|
@@ -709,31 +464,20 @@ VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
|
|
709
464
|
}
|
710
465
|
|
711
466
|
self->descriptor_pool = descriptor_pool;
|
712
|
-
self->filedef = (const
|
467
|
+
self->filedef = (const upb_FileDef*)NUM2ULL(ptr);
|
713
468
|
|
714
469
|
return Qnil;
|
715
470
|
}
|
716
471
|
|
717
|
-
void FileDescriptor_register(VALUE module) {
|
718
|
-
VALUE klass = rb_define_class_under(
|
719
|
-
module, "FileDescriptor", rb_cObject);
|
720
|
-
rb_define_alloc_func(klass, FileDescriptor_alloc);
|
721
|
-
rb_define_method(klass, "initialize", FileDescriptor_initialize, 3);
|
722
|
-
rb_define_method(klass, "name", FileDescriptor_name, 0);
|
723
|
-
rb_define_method(klass, "syntax", FileDescriptor_syntax, 0);
|
724
|
-
rb_gc_register_address(&cFileDescriptor);
|
725
|
-
cFileDescriptor = klass;
|
726
|
-
}
|
727
|
-
|
728
472
|
/*
|
729
473
|
* call-seq:
|
730
474
|
* FileDescriptor.name => name
|
731
475
|
*
|
732
476
|
* Returns the name of the file.
|
733
477
|
*/
|
734
|
-
VALUE FileDescriptor_name(VALUE _self) {
|
735
|
-
|
736
|
-
const char* name =
|
478
|
+
static VALUE FileDescriptor_name(VALUE _self) {
|
479
|
+
FileDescriptor* self = ruby_to_FileDescriptor(_self);
|
480
|
+
const char* name = upb_FileDef_Name(self->filedef);
|
737
481
|
return name == NULL ? Qnil : rb_str_new2(name);
|
738
482
|
}
|
739
483
|
|
@@ -746,29 +490,55 @@ VALUE FileDescriptor_name(VALUE _self) {
|
|
746
490
|
* Valid syntax versions are:
|
747
491
|
* :proto2 or :proto3.
|
748
492
|
*/
|
749
|
-
VALUE FileDescriptor_syntax(VALUE _self) {
|
750
|
-
|
751
|
-
|
752
|
-
switch (
|
753
|
-
case
|
754
|
-
|
755
|
-
|
493
|
+
static VALUE FileDescriptor_syntax(VALUE _self) {
|
494
|
+
FileDescriptor* self = ruby_to_FileDescriptor(_self);
|
495
|
+
|
496
|
+
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;
|
756
503
|
}
|
757
504
|
}
|
758
505
|
|
506
|
+
static void FileDescriptor_register(VALUE module) {
|
507
|
+
VALUE klass = rb_define_class_under(module, "FileDescriptor", rb_cObject);
|
508
|
+
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
|
+
|
759
516
|
// -----------------------------------------------------------------------------
|
760
517
|
// FieldDescriptor.
|
761
518
|
// -----------------------------------------------------------------------------
|
762
519
|
|
763
|
-
|
520
|
+
typedef struct {
|
521
|
+
const upb_FieldDef* fielddef;
|
522
|
+
VALUE descriptor_pool; // Owns the upb_FieldDef.
|
523
|
+
} FieldDescriptor;
|
524
|
+
|
525
|
+
static VALUE cFieldDescriptor = Qnil;
|
764
526
|
|
765
|
-
void FieldDescriptor_mark(void* _self) {
|
527
|
+
static void FieldDescriptor_mark(void* _self) {
|
766
528
|
FieldDescriptor* self = _self;
|
767
529
|
rb_gc_mark(self->descriptor_pool);
|
768
530
|
}
|
769
531
|
|
770
|
-
|
771
|
-
|
532
|
+
static const rb_data_type_t FieldDescriptor_type = {
|
533
|
+
"Google::Protobuf::FieldDescriptor",
|
534
|
+
{FieldDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
|
535
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
|
536
|
+
};
|
537
|
+
|
538
|
+
static FieldDescriptor* ruby_to_FieldDescriptor(VALUE val) {
|
539
|
+
FieldDescriptor* ret;
|
540
|
+
TypedData_Get_Struct(val, FieldDescriptor, &FieldDescriptor_type, ret);
|
541
|
+
return ret;
|
772
542
|
}
|
773
543
|
|
774
544
|
/*
|
@@ -778,42 +548,22 @@ void FieldDescriptor_free(void* _self) {
|
|
778
548
|
* Returns a new field descriptor. Its name, type, etc. must be set before it is
|
779
549
|
* added to a message type.
|
780
550
|
*/
|
781
|
-
VALUE FieldDescriptor_alloc(VALUE klass) {
|
551
|
+
static VALUE FieldDescriptor_alloc(VALUE klass) {
|
782
552
|
FieldDescriptor* self = ALLOC(FieldDescriptor);
|
783
|
-
VALUE ret = TypedData_Wrap_Struct(klass, &
|
553
|
+
VALUE ret = TypedData_Wrap_Struct(klass, &FieldDescriptor_type, self);
|
784
554
|
self->fielddef = NULL;
|
785
555
|
return ret;
|
786
556
|
}
|
787
557
|
|
788
|
-
void FieldDescriptor_register(VALUE module) {
|
789
|
-
VALUE klass = rb_define_class_under(
|
790
|
-
module, "FieldDescriptor", rb_cObject);
|
791
|
-
rb_define_alloc_func(klass, FieldDescriptor_alloc);
|
792
|
-
rb_define_method(klass, "initialize", FieldDescriptor_initialize, 3);
|
793
|
-
rb_define_method(klass, "name", FieldDescriptor_name, 0);
|
794
|
-
rb_define_method(klass, "type", FieldDescriptor_type, 0);
|
795
|
-
rb_define_method(klass, "default", FieldDescriptor_default, 0);
|
796
|
-
rb_define_method(klass, "label", FieldDescriptor_label, 0);
|
797
|
-
rb_define_method(klass, "number", FieldDescriptor_number, 0);
|
798
|
-
rb_define_method(klass, "submsg_name", FieldDescriptor_submsg_name, 0);
|
799
|
-
rb_define_method(klass, "subtype", FieldDescriptor_subtype, 0);
|
800
|
-
rb_define_method(klass, "has?", FieldDescriptor_has, 1);
|
801
|
-
rb_define_method(klass, "clear", FieldDescriptor_clear, 1);
|
802
|
-
rb_define_method(klass, "get", FieldDescriptor_get, 1);
|
803
|
-
rb_define_method(klass, "set", FieldDescriptor_set, 2);
|
804
|
-
rb_gc_register_address(&cFieldDescriptor);
|
805
|
-
cFieldDescriptor = klass;
|
806
|
-
}
|
807
|
-
|
808
558
|
/*
|
809
559
|
* call-seq:
|
810
560
|
* EnumDescriptor.new(c_only_cookie, pool, ptr) => EnumDescriptor
|
811
561
|
*
|
812
562
|
* Creates a descriptor wrapper object. May only be called from C.
|
813
563
|
*/
|
814
|
-
VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
|
815
|
-
|
816
|
-
|
564
|
+
static VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
|
565
|
+
VALUE descriptor_pool, VALUE ptr) {
|
566
|
+
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
|
817
567
|
|
818
568
|
if (cookie != c_only_cookie) {
|
819
569
|
rb_raise(rb_eRuntimeError,
|
@@ -821,7 +571,7 @@ VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
|
|
821
571
|
}
|
822
572
|
|
823
573
|
self->descriptor_pool = descriptor_pool;
|
824
|
-
self->fielddef = (const
|
574
|
+
self->fielddef = (const upb_FieldDef*)NUM2ULL(ptr);
|
825
575
|
|
826
576
|
return Qnil;
|
827
577
|
}
|
@@ -832,87 +582,33 @@ VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
|
|
832
582
|
*
|
833
583
|
* Returns the name of this field.
|
834
584
|
*/
|
835
|
-
VALUE FieldDescriptor_name(VALUE _self) {
|
836
|
-
|
837
|
-
return rb_str_maybe_null(
|
838
|
-
}
|
839
|
-
|
840
|
-
upb_fieldtype_t ruby_to_fieldtype(VALUE type) {
|
841
|
-
if (TYPE(type) != T_SYMBOL) {
|
842
|
-
rb_raise(rb_eArgError, "Expected symbol for field type.");
|
843
|
-
}
|
844
|
-
|
845
|
-
#define CONVERT(upb, ruby) \
|
846
|
-
if (SYM2ID(type) == rb_intern( # ruby )) { \
|
847
|
-
return UPB_TYPE_ ## upb; \
|
848
|
-
}
|
849
|
-
|
850
|
-
CONVERT(FLOAT, float);
|
851
|
-
CONVERT(DOUBLE, double);
|
852
|
-
CONVERT(BOOL, bool);
|
853
|
-
CONVERT(STRING, string);
|
854
|
-
CONVERT(BYTES, bytes);
|
855
|
-
CONVERT(MESSAGE, message);
|
856
|
-
CONVERT(ENUM, enum);
|
857
|
-
CONVERT(INT32, int32);
|
858
|
-
CONVERT(INT64, int64);
|
859
|
-
CONVERT(UINT32, uint32);
|
860
|
-
CONVERT(UINT64, uint64);
|
861
|
-
|
862
|
-
#undef CONVERT
|
863
|
-
|
864
|
-
rb_raise(rb_eArgError, "Unknown field type.");
|
865
|
-
return 0;
|
866
|
-
}
|
867
|
-
|
868
|
-
VALUE fieldtype_to_ruby(upb_fieldtype_t type) {
|
869
|
-
switch (type) {
|
870
|
-
#define CONVERT(upb, ruby) \
|
871
|
-
case UPB_TYPE_ ## upb : return ID2SYM(rb_intern( # ruby ));
|
872
|
-
CONVERT(FLOAT, float);
|
873
|
-
CONVERT(DOUBLE, double);
|
874
|
-
CONVERT(BOOL, bool);
|
875
|
-
CONVERT(STRING, string);
|
876
|
-
CONVERT(BYTES, bytes);
|
877
|
-
CONVERT(MESSAGE, message);
|
878
|
-
CONVERT(ENUM, enum);
|
879
|
-
CONVERT(INT32, int32);
|
880
|
-
CONVERT(INT64, int64);
|
881
|
-
CONVERT(UINT32, uint32);
|
882
|
-
CONVERT(UINT64, uint64);
|
883
|
-
#undef CONVERT
|
884
|
-
}
|
885
|
-
return Qnil;
|
585
|
+
static VALUE FieldDescriptor_name(VALUE _self) {
|
586
|
+
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
|
587
|
+
return rb_str_maybe_null(upb_FieldDef_Name(self->fielddef));
|
886
588
|
}
|
887
589
|
|
888
|
-
|
590
|
+
// Non-static, exposed to other .c files.
|
591
|
+
upb_CType ruby_to_fieldtype(VALUE type) {
|
889
592
|
if (TYPE(type) != T_SYMBOL) {
|
890
593
|
rb_raise(rb_eArgError, "Expected symbol for field type.");
|
891
594
|
}
|
892
595
|
|
893
|
-
#define CONVERT(upb, ruby)
|
894
|
-
if (SYM2ID(type) == rb_intern(
|
895
|
-
return
|
596
|
+
#define CONVERT(upb, ruby) \
|
597
|
+
if (SYM2ID(type) == rb_intern(#ruby)) { \
|
598
|
+
return kUpb_CType_##upb; \
|
896
599
|
}
|
897
600
|
|
898
|
-
CONVERT(
|
899
|
-
CONVERT(
|
900
|
-
CONVERT(
|
901
|
-
CONVERT(
|
902
|
-
CONVERT(
|
903
|
-
CONVERT(
|
904
|
-
CONVERT(
|
905
|
-
CONVERT(
|
906
|
-
CONVERT(
|
907
|
-
CONVERT(
|
908
|
-
CONVERT(
|
909
|
-
CONVERT(UINT64, uint64);
|
910
|
-
CONVERT(SINT32, sint32);
|
911
|
-
CONVERT(SINT64, sint64);
|
912
|
-
CONVERT(FIXED32, fixed32);
|
913
|
-
CONVERT(FIXED64, fixed64);
|
914
|
-
CONVERT(SFIXED32, sfixed32);
|
915
|
-
CONVERT(SFIXED64, sfixed64);
|
601
|
+
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);
|
916
612
|
|
917
613
|
#undef CONVERT
|
918
614
|
|
@@ -920,56 +616,34 @@ upb_descriptortype_t ruby_to_descriptortype(VALUE type) {
|
|
920
616
|
return 0;
|
921
617
|
}
|
922
618
|
|
923
|
-
VALUE descriptortype_to_ruby(
|
619
|
+
static VALUE descriptortype_to_ruby(upb_FieldType type) {
|
924
620
|
switch (type) {
|
925
|
-
#define CONVERT(upb, ruby)
|
926
|
-
|
927
|
-
|
928
|
-
CONVERT(
|
929
|
-
CONVERT(
|
930
|
-
CONVERT(
|
931
|
-
CONVERT(
|
932
|
-
CONVERT(
|
933
|
-
CONVERT(
|
934
|
-
CONVERT(
|
935
|
-
CONVERT(
|
936
|
-
CONVERT(
|
937
|
-
CONVERT(
|
938
|
-
CONVERT(
|
939
|
-
CONVERT(
|
940
|
-
CONVERT(
|
941
|
-
CONVERT(
|
942
|
-
CONVERT(
|
943
|
-
CONVERT(
|
944
|
-
CONVERT(
|
621
|
+
#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);
|
945
642
|
#undef CONVERT
|
946
643
|
}
|
947
644
|
return Qnil;
|
948
645
|
}
|
949
646
|
|
950
|
-
VALUE ruby_to_label(VALUE label) {
|
951
|
-
upb_label_t upb_label;
|
952
|
-
bool converted = false;
|
953
|
-
|
954
|
-
#define CONVERT(upb, ruby) \
|
955
|
-
if (SYM2ID(label) == rb_intern( # ruby )) { \
|
956
|
-
upb_label = UPB_LABEL_ ## upb; \
|
957
|
-
converted = true; \
|
958
|
-
}
|
959
|
-
|
960
|
-
CONVERT(OPTIONAL, optional);
|
961
|
-
CONVERT(REQUIRED, required);
|
962
|
-
CONVERT(REPEATED, repeated);
|
963
|
-
|
964
|
-
#undef CONVERT
|
965
|
-
|
966
|
-
if (!converted) {
|
967
|
-
rb_raise(rb_eArgError, "Unknown field label.");
|
968
|
-
}
|
969
|
-
|
970
|
-
return upb_label;
|
971
|
-
}
|
972
|
-
|
973
647
|
/*
|
974
648
|
* call-seq:
|
975
649
|
* FieldDescriptor.type => type
|
@@ -980,9 +654,9 @@ VALUE ruby_to_label(VALUE label) {
|
|
980
654
|
* :int32, :int64, :uint32, :uint64, :float, :double, :bool, :string,
|
981
655
|
* :bytes, :message.
|
982
656
|
*/
|
983
|
-
VALUE
|
984
|
-
|
985
|
-
return descriptortype_to_ruby(
|
657
|
+
static VALUE FieldDescriptor__type(VALUE _self) {
|
658
|
+
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
|
659
|
+
return descriptortype_to_ruby(upb_FieldDef_Type(self->fielddef));
|
986
660
|
}
|
987
661
|
|
988
662
|
/*
|
@@ -991,9 +665,29 @@ VALUE FieldDescriptor_type(VALUE _self) {
|
|
991
665
|
*
|
992
666
|
* Returns this field's default, as a Ruby object, or nil if not yet set.
|
993
667
|
*/
|
994
|
-
VALUE FieldDescriptor_default(VALUE _self) {
|
995
|
-
|
996
|
-
|
668
|
+
static VALUE FieldDescriptor_default(VALUE _self) {
|
669
|
+
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
|
670
|
+
const upb_FieldDef* f = self->fielddef;
|
671
|
+
upb_MessageValue default_val = {0};
|
672
|
+
if (upb_FieldDef_IsSubMessage(f)) {
|
673
|
+
return Qnil;
|
674
|
+
} else if (!upb_FieldDef_IsRepeated(f)) {
|
675
|
+
default_val = upb_FieldDef_Default(f);
|
676
|
+
}
|
677
|
+
return Convert_UpbToRuby(default_val, TypeInfo_get(self->fielddef), Qnil);
|
678
|
+
}
|
679
|
+
|
680
|
+
/*
|
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
|
+
*/
|
686
|
+
static VALUE FieldDescriptor_json_name(VALUE _self) {
|
687
|
+
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
|
688
|
+
const upb_FieldDef* f = self->fielddef;
|
689
|
+
const char* json_name = upb_FieldDef_JsonName(f);
|
690
|
+
return rb_str_new2(json_name);
|
997
691
|
}
|
998
692
|
|
999
693
|
/*
|
@@ -1005,15 +699,16 @@ VALUE FieldDescriptor_default(VALUE _self) {
|
|
1005
699
|
* Valid field labels are:
|
1006
700
|
* :optional, :repeated
|
1007
701
|
*/
|
1008
|
-
VALUE FieldDescriptor_label(VALUE _self) {
|
1009
|
-
|
1010
|
-
switch (
|
1011
|
-
#define CONVERT(upb, ruby)
|
1012
|
-
|
702
|
+
static VALUE FieldDescriptor_label(VALUE _self) {
|
703
|
+
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
|
704
|
+
switch (upb_FieldDef_Label(self->fielddef)) {
|
705
|
+
#define CONVERT(upb, ruby) \
|
706
|
+
case kUpb_Label_##upb: \
|
707
|
+
return ID2SYM(rb_intern(#ruby));
|
1013
708
|
|
1014
|
-
CONVERT(
|
1015
|
-
CONVERT(
|
1016
|
-
CONVERT(
|
709
|
+
CONVERT(Optional, optional);
|
710
|
+
CONVERT(Required, required);
|
711
|
+
CONVERT(Repeated, repeated);
|
1017
712
|
|
1018
713
|
#undef CONVERT
|
1019
714
|
}
|
@@ -1027,9 +722,9 @@ VALUE FieldDescriptor_label(VALUE _self) {
|
|
1027
722
|
*
|
1028
723
|
* Returns the tag number for this field.
|
1029
724
|
*/
|
1030
|
-
VALUE FieldDescriptor_number(VALUE _self) {
|
1031
|
-
|
1032
|
-
return INT2NUM(
|
725
|
+
static VALUE FieldDescriptor_number(VALUE _self) {
|
726
|
+
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
|
727
|
+
return INT2NUM(upb_FieldDef_Number(self->fielddef));
|
1033
728
|
}
|
1034
729
|
|
1035
730
|
/*
|
@@ -1041,15 +736,15 @@ VALUE FieldDescriptor_number(VALUE _self) {
|
|
1041
736
|
* name will be resolved within the context of the pool to which the containing
|
1042
737
|
* message type is added.
|
1043
738
|
*/
|
1044
|
-
VALUE FieldDescriptor_submsg_name(VALUE _self) {
|
1045
|
-
|
1046
|
-
switch (
|
1047
|
-
case
|
739
|
+
static VALUE FieldDescriptor_submsg_name(VALUE _self) {
|
740
|
+
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
|
741
|
+
switch (upb_FieldDef_CType(self->fielddef)) {
|
742
|
+
case kUpb_CType_Enum:
|
1048
743
|
return rb_str_new2(
|
1049
|
-
|
1050
|
-
case
|
744
|
+
upb_EnumDef_FullName(upb_FieldDef_EnumSubDef(self->fielddef)));
|
745
|
+
case kUpb_CType_Message:
|
1051
746
|
return rb_str_new2(
|
1052
|
-
|
747
|
+
upb_MessageDef_FullName(upb_FieldDef_MessageSubDef(self->fielddef)));
|
1053
748
|
default:
|
1054
749
|
return Qnil;
|
1055
750
|
}
|
@@ -1064,15 +759,15 @@ VALUE FieldDescriptor_submsg_name(VALUE _self) {
|
|
1064
759
|
* called *until* the containing message type is added to a pool (and thus
|
1065
760
|
* resolved).
|
1066
761
|
*/
|
1067
|
-
VALUE FieldDescriptor_subtype(VALUE _self) {
|
1068
|
-
|
1069
|
-
switch (
|
1070
|
-
case
|
762
|
+
static VALUE FieldDescriptor_subtype(VALUE _self) {
|
763
|
+
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
|
764
|
+
switch (upb_FieldDef_CType(self->fielddef)) {
|
765
|
+
case kUpb_CType_Enum:
|
1071
766
|
return get_enumdef_obj(self->descriptor_pool,
|
1072
|
-
|
1073
|
-
case
|
767
|
+
upb_FieldDef_EnumSubDef(self->fielddef));
|
768
|
+
case kUpb_CType_Message:
|
1074
769
|
return get_msgdef_obj(self->descriptor_pool,
|
1075
|
-
|
770
|
+
upb_FieldDef_MessageSubDef(self->fielddef));
|
1076
771
|
default:
|
1077
772
|
return Qnil;
|
1078
773
|
}
|
@@ -1085,14 +780,17 @@ VALUE FieldDescriptor_subtype(VALUE _self) {
|
|
1085
780
|
* Returns the value set for this field on the given message. Raises an
|
1086
781
|
* exception if message is of the wrong type.
|
1087
782
|
*/
|
1088
|
-
VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
|
1089
|
-
|
1090
|
-
|
1091
|
-
|
1092
|
-
|
783
|
+
static VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
|
784
|
+
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
|
785
|
+
const upb_MessageDef* m;
|
786
|
+
|
787
|
+
Message_Get(msg_rb, &m);
|
788
|
+
|
789
|
+
if (m != upb_FieldDef_ContainingType(self->fielddef)) {
|
1093
790
|
rb_raise(cTypeError, "get method called on wrong message type");
|
1094
791
|
}
|
1095
|
-
|
792
|
+
|
793
|
+
return Message_getfield(msg_rb, self->fielddef);
|
1096
794
|
}
|
1097
795
|
|
1098
796
|
/*
|
@@ -1102,17 +800,18 @@ VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
|
|
1102
800
|
* Returns whether the value is set on the given message. Raises an
|
1103
801
|
* exception when calling for fields that do not have presence.
|
1104
802
|
*/
|
1105
|
-
VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
|
1106
|
-
|
1107
|
-
|
1108
|
-
|
1109
|
-
|
803
|
+
static VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
|
804
|
+
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
|
805
|
+
const upb_MessageDef* m;
|
806
|
+
const upb_MessageDef* msg = Message_Get(msg_rb, &m);
|
807
|
+
|
808
|
+
if (m != upb_FieldDef_ContainingType(self->fielddef)) {
|
1110
809
|
rb_raise(cTypeError, "has method called on wrong message type");
|
1111
|
-
} else if (!
|
810
|
+
} else if (!upb_FieldDef_HasPresence(self->fielddef)) {
|
1112
811
|
rb_raise(rb_eArgError, "does not track presence");
|
1113
812
|
}
|
1114
813
|
|
1115
|
-
return
|
814
|
+
return upb_Message_Has(msg, self->fielddef) ? Qtrue : Qfalse;
|
1116
815
|
}
|
1117
816
|
|
1118
817
|
/*
|
@@ -1121,15 +820,16 @@ VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
|
|
1121
820
|
*
|
1122
821
|
* Clears the field from the message if it's set.
|
1123
822
|
*/
|
1124
|
-
VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {
|
1125
|
-
|
1126
|
-
|
1127
|
-
|
1128
|
-
|
823
|
+
static VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {
|
824
|
+
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
|
825
|
+
const upb_MessageDef* m;
|
826
|
+
upb_MessageDef* msg = Message_GetMutable(msg_rb, &m);
|
827
|
+
|
828
|
+
if (m != upb_FieldDef_ContainingType(self->fielddef)) {
|
1129
829
|
rb_raise(cTypeError, "has method called on wrong message type");
|
1130
830
|
}
|
1131
831
|
|
1132
|
-
|
832
|
+
upb_Message_ClearField(msg, self->fielddef);
|
1133
833
|
return Qnil;
|
1134
834
|
}
|
1135
835
|
|
@@ -1141,30 +841,69 @@ VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {
|
|
1141
841
|
* message. Raises an exception if message is of the wrong type. Performs the
|
1142
842
|
* ordinary type-checks for field setting.
|
1143
843
|
*/
|
1144
|
-
VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value) {
|
1145
|
-
|
1146
|
-
|
1147
|
-
|
1148
|
-
|
844
|
+
static VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value) {
|
845
|
+
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
|
846
|
+
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;
|
850
|
+
|
851
|
+
if (m != upb_FieldDef_ContainingType(self->fielddef)) {
|
1149
852
|
rb_raise(cTypeError, "set method called on wrong message type");
|
1150
853
|
}
|
1151
|
-
|
854
|
+
|
855
|
+
msgval = Convert_RubyToUpb(value, upb_FieldDef_Name(self->fielddef),
|
856
|
+
TypeInfo_get(self->fielddef), arena);
|
857
|
+
upb_Message_Set(msg, self->fielddef, msgval, arena);
|
1152
858
|
return Qnil;
|
1153
859
|
}
|
1154
860
|
|
861
|
+
static void FieldDescriptor_register(VALUE module) {
|
862
|
+
VALUE klass = rb_define_class_under(module, "FieldDescriptor", rb_cObject);
|
863
|
+
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);
|
868
|
+
rb_define_method(klass, "json_name", FieldDescriptor_json_name, 0);
|
869
|
+
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
|
+
|
1155
881
|
// -----------------------------------------------------------------------------
|
1156
882
|
// OneofDescriptor.
|
1157
883
|
// -----------------------------------------------------------------------------
|
1158
884
|
|
1159
|
-
|
885
|
+
typedef struct {
|
886
|
+
const upb_OneofDef* oneofdef;
|
887
|
+
VALUE descriptor_pool; // Owns the upb_OneofDef.
|
888
|
+
} OneofDescriptor;
|
889
|
+
|
890
|
+
static VALUE cOneofDescriptor = Qnil;
|
1160
891
|
|
1161
|
-
void OneofDescriptor_mark(void* _self) {
|
892
|
+
static void OneofDescriptor_mark(void* _self) {
|
1162
893
|
OneofDescriptor* self = _self;
|
1163
894
|
rb_gc_mark(self->descriptor_pool);
|
1164
895
|
}
|
1165
896
|
|
1166
|
-
|
1167
|
-
|
897
|
+
static 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
|
+
|
903
|
+
static OneofDescriptor* ruby_to_OneofDescriptor(VALUE val) {
|
904
|
+
OneofDescriptor* ret;
|
905
|
+
TypedData_Get_Struct(val, OneofDescriptor, &OneofDescriptor_type, ret);
|
906
|
+
return ret;
|
1168
907
|
}
|
1169
908
|
|
1170
909
|
/*
|
@@ -1174,35 +913,23 @@ void OneofDescriptor_free(void* _self) {
|
|
1174
913
|
* Creates a new, empty, oneof descriptor. The oneof may only be modified prior
|
1175
914
|
* to being added to a message descriptor which is subsequently added to a pool.
|
1176
915
|
*/
|
1177
|
-
VALUE OneofDescriptor_alloc(VALUE klass) {
|
916
|
+
static VALUE OneofDescriptor_alloc(VALUE klass) {
|
1178
917
|
OneofDescriptor* self = ALLOC(OneofDescriptor);
|
1179
|
-
VALUE ret = TypedData_Wrap_Struct(klass, &
|
918
|
+
VALUE ret = TypedData_Wrap_Struct(klass, &OneofDescriptor_type, self);
|
1180
919
|
self->oneofdef = NULL;
|
1181
920
|
self->descriptor_pool = Qnil;
|
1182
921
|
return ret;
|
1183
922
|
}
|
1184
923
|
|
1185
|
-
void OneofDescriptor_register(VALUE module) {
|
1186
|
-
VALUE klass = rb_define_class_under(
|
1187
|
-
module, "OneofDescriptor", rb_cObject);
|
1188
|
-
rb_define_alloc_func(klass, OneofDescriptor_alloc);
|
1189
|
-
rb_define_method(klass, "initialize", OneofDescriptor_initialize, 3);
|
1190
|
-
rb_define_method(klass, "name", OneofDescriptor_name, 0);
|
1191
|
-
rb_define_method(klass, "each", OneofDescriptor_each, 0);
|
1192
|
-
rb_include_module(klass, rb_mEnumerable);
|
1193
|
-
rb_gc_register_address(&cOneofDescriptor);
|
1194
|
-
cOneofDescriptor = klass;
|
1195
|
-
}
|
1196
|
-
|
1197
924
|
/*
|
1198
925
|
* call-seq:
|
1199
926
|
* OneofDescriptor.new(c_only_cookie, pool, ptr) => OneofDescriptor
|
1200
927
|
*
|
1201
928
|
* Creates a descriptor wrapper object. May only be called from C.
|
1202
929
|
*/
|
1203
|
-
VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
|
1204
|
-
|
1205
|
-
|
930
|
+
static VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
|
931
|
+
VALUE descriptor_pool, VALUE ptr) {
|
932
|
+
OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
|
1206
933
|
|
1207
934
|
if (cookie != c_only_cookie) {
|
1208
935
|
rb_raise(rb_eRuntimeError,
|
@@ -1210,7 +937,7 @@ VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
|
|
1210
937
|
}
|
1211
938
|
|
1212
939
|
self->descriptor_pool = descriptor_pool;
|
1213
|
-
self->oneofdef = (const
|
940
|
+
self->oneofdef = (const upb_OneofDef*)NUM2ULL(ptr);
|
1214
941
|
|
1215
942
|
return Qnil;
|
1216
943
|
}
|
@@ -1221,9 +948,9 @@ VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
|
|
1221
948
|
*
|
1222
949
|
* Returns the name of this oneof.
|
1223
950
|
*/
|
1224
|
-
VALUE OneofDescriptor_name(VALUE _self) {
|
1225
|
-
|
1226
|
-
return rb_str_maybe_null(
|
951
|
+
static VALUE OneofDescriptor_name(VALUE _self) {
|
952
|
+
OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
|
953
|
+
return rb_str_maybe_null(upb_OneofDef_Name(self->oneofdef));
|
1227
954
|
}
|
1228
955
|
|
1229
956
|
/*
|
@@ -1232,53 +959,83 @@ VALUE OneofDescriptor_name(VALUE _self) {
|
|
1232
959
|
*
|
1233
960
|
* Iterates through fields in this oneof, yielding to the block on each one.
|
1234
961
|
*/
|
1235
|
-
VALUE OneofDescriptor_each(VALUE _self) {
|
1236
|
-
|
1237
|
-
|
1238
|
-
|
1239
|
-
|
1240
|
-
|
1241
|
-
const upb_fielddef* f = upb_oneof_iter_field(&it);
|
962
|
+
static VALUE OneofDescriptor_each(VALUE _self) {
|
963
|
+
OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
|
964
|
+
|
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);
|
1242
968
|
VALUE obj = get_fielddef_obj(self->descriptor_pool, f);
|
1243
969
|
rb_yield(obj);
|
1244
970
|
}
|
1245
971
|
return Qnil;
|
1246
972
|
}
|
1247
973
|
|
974
|
+
static void OneofDescriptor_register(VALUE module) {
|
975
|
+
VALUE klass = rb_define_class_under(module, "OneofDescriptor", rb_cObject);
|
976
|
+
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
|
+
|
1248
985
|
// -----------------------------------------------------------------------------
|
1249
|
-
// EnumDescriptor.
|
1250
|
-
// -----------------------------------------------------------------------------
|
986
|
+
// EnumDescriptor.
|
987
|
+
// -----------------------------------------------------------------------------
|
988
|
+
|
989
|
+
typedef struct {
|
990
|
+
const upb_EnumDef* enumdef;
|
991
|
+
VALUE module; // begins as nil
|
992
|
+
VALUE descriptor_pool; // Owns the upb_EnumDef.
|
993
|
+
} EnumDescriptor;
|
1251
994
|
|
1252
|
-
|
995
|
+
static VALUE cEnumDescriptor = Qnil;
|
1253
996
|
|
1254
|
-
void EnumDescriptor_mark(void* _self) {
|
997
|
+
static void EnumDescriptor_mark(void* _self) {
|
1255
998
|
EnumDescriptor* self = _self;
|
1256
999
|
rb_gc_mark(self->module);
|
1257
1000
|
rb_gc_mark(self->descriptor_pool);
|
1258
1001
|
}
|
1259
1002
|
|
1260
|
-
|
1261
|
-
|
1003
|
+
static const rb_data_type_t EnumDescriptor_type = {
|
1004
|
+
"Google::Protobuf::EnumDescriptor",
|
1005
|
+
{EnumDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
|
1006
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
|
1007
|
+
};
|
1008
|
+
|
1009
|
+
static EnumDescriptor* ruby_to_EnumDescriptor(VALUE val) {
|
1010
|
+
EnumDescriptor* ret;
|
1011
|
+
TypedData_Get_Struct(val, EnumDescriptor, &EnumDescriptor_type, ret);
|
1012
|
+
return ret;
|
1262
1013
|
}
|
1263
1014
|
|
1264
|
-
VALUE EnumDescriptor_alloc(VALUE klass) {
|
1015
|
+
static VALUE EnumDescriptor_alloc(VALUE klass) {
|
1265
1016
|
EnumDescriptor* self = ALLOC(EnumDescriptor);
|
1266
|
-
VALUE ret = TypedData_Wrap_Struct(klass, &
|
1017
|
+
VALUE ret = TypedData_Wrap_Struct(klass, &EnumDescriptor_type, self);
|
1267
1018
|
self->enumdef = NULL;
|
1268
1019
|
self->module = Qnil;
|
1269
1020
|
self->descriptor_pool = Qnil;
|
1270
1021
|
return ret;
|
1271
1022
|
}
|
1272
1023
|
|
1024
|
+
// Exposed to other modules in defs.h.
|
1025
|
+
const upb_EnumDef* EnumDescriptor_GetEnumDef(VALUE enum_desc_rb) {
|
1026
|
+
EnumDescriptor* desc = ruby_to_EnumDescriptor(enum_desc_rb);
|
1027
|
+
return desc->enumdef;
|
1028
|
+
}
|
1029
|
+
|
1273
1030
|
/*
|
1274
1031
|
* call-seq:
|
1275
1032
|
* EnumDescriptor.new(c_only_cookie, ptr) => EnumDescriptor
|
1276
1033
|
*
|
1277
1034
|
* Creates a descriptor wrapper object. May only be called from C.
|
1278
1035
|
*/
|
1279
|
-
VALUE EnumDescriptor_initialize(VALUE _self, VALUE cookie,
|
1280
|
-
|
1281
|
-
|
1036
|
+
static VALUE EnumDescriptor_initialize(VALUE _self, VALUE cookie,
|
1037
|
+
VALUE descriptor_pool, VALUE ptr) {
|
1038
|
+
EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
|
1282
1039
|
|
1283
1040
|
if (cookie != c_only_cookie) {
|
1284
1041
|
rb_raise(rb_eRuntimeError,
|
@@ -1286,37 +1043,21 @@ VALUE EnumDescriptor_initialize(VALUE _self, VALUE cookie,
|
|
1286
1043
|
}
|
1287
1044
|
|
1288
1045
|
self->descriptor_pool = descriptor_pool;
|
1289
|
-
self->enumdef = (const
|
1046
|
+
self->enumdef = (const upb_EnumDef*)NUM2ULL(ptr);
|
1290
1047
|
|
1291
1048
|
return Qnil;
|
1292
1049
|
}
|
1293
1050
|
|
1294
|
-
void EnumDescriptor_register(VALUE module) {
|
1295
|
-
VALUE klass = rb_define_class_under(
|
1296
|
-
module, "EnumDescriptor", rb_cObject);
|
1297
|
-
rb_define_alloc_func(klass, EnumDescriptor_alloc);
|
1298
|
-
rb_define_method(klass, "initialize", EnumDescriptor_initialize, 3);
|
1299
|
-
rb_define_method(klass, "name", EnumDescriptor_name, 0);
|
1300
|
-
rb_define_method(klass, "lookup_name", EnumDescriptor_lookup_name, 1);
|
1301
|
-
rb_define_method(klass, "lookup_value", EnumDescriptor_lookup_value, 1);
|
1302
|
-
rb_define_method(klass, "each", EnumDescriptor_each, 0);
|
1303
|
-
rb_define_method(klass, "enummodule", EnumDescriptor_enummodule, 0);
|
1304
|
-
rb_define_method(klass, "file_descriptor", EnumDescriptor_file_descriptor, 0);
|
1305
|
-
rb_include_module(klass, rb_mEnumerable);
|
1306
|
-
rb_gc_register_address(&cEnumDescriptor);
|
1307
|
-
cEnumDescriptor = klass;
|
1308
|
-
}
|
1309
|
-
|
1310
1051
|
/*
|
1311
1052
|
* call-seq:
|
1312
1053
|
* EnumDescriptor.file_descriptor
|
1313
1054
|
*
|
1314
1055
|
* Returns the FileDescriptor object this enum belongs to.
|
1315
1056
|
*/
|
1316
|
-
VALUE EnumDescriptor_file_descriptor(VALUE _self) {
|
1317
|
-
|
1057
|
+
static VALUE EnumDescriptor_file_descriptor(VALUE _self) {
|
1058
|
+
EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
|
1318
1059
|
return get_filedef_obj(self->descriptor_pool,
|
1319
|
-
|
1060
|
+
upb_EnumDef_File(self->enumdef));
|
1320
1061
|
}
|
1321
1062
|
|
1322
1063
|
/*
|
@@ -1325,9 +1066,9 @@ VALUE EnumDescriptor_file_descriptor(VALUE _self) {
|
|
1325
1066
|
*
|
1326
1067
|
* Returns the name of this enum type.
|
1327
1068
|
*/
|
1328
|
-
VALUE EnumDescriptor_name(VALUE _self) {
|
1329
|
-
|
1330
|
-
return rb_str_maybe_null(
|
1069
|
+
static VALUE EnumDescriptor_name(VALUE _self) {
|
1070
|
+
EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
|
1071
|
+
return rb_str_maybe_null(upb_EnumDef_FullName(self->enumdef));
|
1331
1072
|
}
|
1332
1073
|
|
1333
1074
|
/*
|
@@ -1337,12 +1078,13 @@ VALUE EnumDescriptor_name(VALUE _self) {
|
|
1337
1078
|
* Returns the numeric value corresponding to the given key name (as a Ruby
|
1338
1079
|
* symbol), or nil if none.
|
1339
1080
|
*/
|
1340
|
-
VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
|
1341
|
-
|
1342
|
-
const char* name_str= rb_id2name(SYM2ID(name));
|
1343
|
-
|
1344
|
-
|
1345
|
-
|
1081
|
+
static VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
|
1082
|
+
EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
|
1083
|
+
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));
|
1346
1088
|
} else {
|
1347
1089
|
return Qnil;
|
1348
1090
|
}
|
@@ -1355,12 +1097,12 @@ VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
|
|
1355
1097
|
* Returns the key name (as a Ruby symbol) corresponding to the integer value,
|
1356
1098
|
* or nil if none.
|
1357
1099
|
*/
|
1358
|
-
VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
|
1359
|
-
|
1100
|
+
static VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
|
1101
|
+
EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
|
1360
1102
|
int32_t val = NUM2INT(number);
|
1361
|
-
const
|
1362
|
-
if (
|
1363
|
-
return ID2SYM(rb_intern(
|
1103
|
+
const upb_EnumValueDef* ev = upb_EnumDef_FindValueByNumber(self->enumdef, val);
|
1104
|
+
if (ev) {
|
1105
|
+
return ID2SYM(rb_intern(upb_EnumValueDef_Name(ev)));
|
1364
1106
|
} else {
|
1365
1107
|
return Qnil;
|
1366
1108
|
}
|
@@ -1373,15 +1115,14 @@ VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
|
|
1373
1115
|
* Iterates over key => value mappings in this enum's definition, yielding to
|
1374
1116
|
* the block with (key, value) arguments for each one.
|
1375
1117
|
*/
|
1376
|
-
VALUE EnumDescriptor_each(VALUE _self) {
|
1377
|
-
|
1378
|
-
|
1379
|
-
|
1380
|
-
for (
|
1381
|
-
|
1382
|
-
|
1383
|
-
VALUE
|
1384
|
-
VALUE number = INT2NUM(upb_enum_iter_number(&it));
|
1118
|
+
static VALUE EnumDescriptor_each(VALUE _self) {
|
1119
|
+
EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
|
1120
|
+
|
1121
|
+
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));
|
1385
1126
|
rb_yield_values(2, key, number);
|
1386
1127
|
}
|
1387
1128
|
|
@@ -1394,973 +1135,146 @@ VALUE EnumDescriptor_each(VALUE _self) {
|
|
1394
1135
|
*
|
1395
1136
|
* Returns the Ruby module corresponding to this enum type.
|
1396
1137
|
*/
|
1397
|
-
VALUE EnumDescriptor_enummodule(VALUE _self) {
|
1398
|
-
|
1138
|
+
static VALUE EnumDescriptor_enummodule(VALUE _self) {
|
1139
|
+
EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
|
1399
1140
|
if (self->module == Qnil) {
|
1400
1141
|
self->module = build_module_from_enumdesc(_self);
|
1401
1142
|
}
|
1402
1143
|
return self->module;
|
1403
1144
|
}
|
1404
1145
|
|
1405
|
-
|
1406
|
-
|
1407
|
-
|
1408
|
-
|
1409
|
-
|
1410
|
-
|
1411
|
-
|
1412
|
-
|
1413
|
-
|
1414
|
-
|
1415
|
-
|
1416
|
-
|
1417
|
-
|
1418
|
-
MessageBuilderContext* self = _self;
|
1419
|
-
xfree(self);
|
1420
|
-
}
|
1421
|
-
|
1422
|
-
VALUE MessageBuilderContext_alloc(VALUE klass) {
|
1423
|
-
MessageBuilderContext* self = ALLOC(MessageBuilderContext);
|
1424
|
-
VALUE ret = TypedData_Wrap_Struct(
|
1425
|
-
klass, &_MessageBuilderContext_type, self);
|
1426
|
-
self->file_builder = Qnil;
|
1427
|
-
return ret;
|
1428
|
-
}
|
1429
|
-
|
1430
|
-
void MessageBuilderContext_register(VALUE module) {
|
1431
|
-
VALUE klass = rb_define_class_under(
|
1432
|
-
module, "MessageBuilderContext", rb_cObject);
|
1433
|
-
rb_define_alloc_func(klass, MessageBuilderContext_alloc);
|
1434
|
-
rb_define_method(klass, "initialize",
|
1435
|
-
MessageBuilderContext_initialize, 2);
|
1436
|
-
rb_define_method(klass, "optional", MessageBuilderContext_optional, -1);
|
1437
|
-
rb_define_method(klass, "proto3_optional", MessageBuilderContext_proto3_optional, -1);
|
1438
|
-
rb_define_method(klass, "required", MessageBuilderContext_required, -1);
|
1439
|
-
rb_define_method(klass, "repeated", MessageBuilderContext_repeated, -1);
|
1440
|
-
rb_define_method(klass, "map", MessageBuilderContext_map, -1);
|
1441
|
-
rb_define_method(klass, "oneof", MessageBuilderContext_oneof, 1);
|
1442
|
-
rb_gc_register_address(&cMessageBuilderContext);
|
1443
|
-
cMessageBuilderContext = klass;
|
1444
|
-
}
|
1445
|
-
|
1446
|
-
/*
|
1447
|
-
* call-seq:
|
1448
|
-
* MessageBuilderContext.new(file_builder, name) => context
|
1449
|
-
*
|
1450
|
-
* Create a new message builder context around the given message descriptor and
|
1451
|
-
* builder context. This class is intended to serve as a DSL context to be used
|
1452
|
-
* with #instance_eval.
|
1453
|
-
*/
|
1454
|
-
VALUE MessageBuilderContext_initialize(VALUE _self,
|
1455
|
-
VALUE _file_builder,
|
1456
|
-
VALUE name) {
|
1457
|
-
DEFINE_SELF(MessageBuilderContext, self, _self);
|
1458
|
-
FileBuilderContext* file_builder = ruby_to_FileBuilderContext(_file_builder);
|
1459
|
-
google_protobuf_FileDescriptorProto* file_proto = file_builder->file_proto;
|
1460
|
-
|
1461
|
-
self->file_builder = _file_builder;
|
1462
|
-
self->msg_proto = google_protobuf_FileDescriptorProto_add_message_type(
|
1463
|
-
file_proto, file_builder->arena);
|
1464
|
-
|
1465
|
-
google_protobuf_DescriptorProto_set_name(
|
1466
|
-
self->msg_proto, FileBuilderContext_strdup(_file_builder, name));
|
1467
|
-
|
1468
|
-
return Qnil;
|
1469
|
-
}
|
1470
|
-
|
1471
|
-
static void msgdef_add_field(VALUE msgbuilder_rb, upb_label_t label, VALUE name,
|
1472
|
-
VALUE type, VALUE number, VALUE type_class,
|
1473
|
-
VALUE options, int oneof_index,
|
1474
|
-
bool proto3_optional) {
|
1475
|
-
DEFINE_SELF(MessageBuilderContext, self, msgbuilder_rb);
|
1476
|
-
FileBuilderContext* file_context =
|
1477
|
-
ruby_to_FileBuilderContext(self->file_builder);
|
1478
|
-
google_protobuf_FieldDescriptorProto* field_proto;
|
1479
|
-
VALUE name_str;
|
1480
|
-
|
1481
|
-
field_proto = google_protobuf_DescriptorProto_add_field(self->msg_proto,
|
1482
|
-
file_context->arena);
|
1483
|
-
|
1484
|
-
Check_Type(name, T_SYMBOL);
|
1485
|
-
name_str = rb_id2str(SYM2ID(name));
|
1486
|
-
|
1487
|
-
google_protobuf_FieldDescriptorProto_set_name(
|
1488
|
-
field_proto, FileBuilderContext_strdup(self->file_builder, name_str));
|
1489
|
-
google_protobuf_FieldDescriptorProto_set_number(field_proto, NUM2INT(number));
|
1490
|
-
google_protobuf_FieldDescriptorProto_set_label(field_proto, (int)label);
|
1491
|
-
google_protobuf_FieldDescriptorProto_set_type(
|
1492
|
-
field_proto, (int)ruby_to_descriptortype(type));
|
1493
|
-
|
1494
|
-
if (proto3_optional) {
|
1495
|
-
google_protobuf_FieldDescriptorProto_set_proto3_optional(field_proto, true);
|
1496
|
-
}
|
1497
|
-
|
1498
|
-
if (type_class != Qnil) {
|
1499
|
-
Check_Type(type_class, T_STRING);
|
1500
|
-
|
1501
|
-
// Make it an absolute type name by prepending a dot.
|
1502
|
-
type_class = rb_str_append(rb_str_new2("."), type_class);
|
1503
|
-
google_protobuf_FieldDescriptorProto_set_type_name(
|
1504
|
-
field_proto, FileBuilderContext_strdup(self->file_builder, type_class));
|
1505
|
-
}
|
1506
|
-
|
1507
|
-
if (options != Qnil) {
|
1508
|
-
Check_Type(options, T_HASH);
|
1509
|
-
|
1510
|
-
if (rb_funcall(options, rb_intern("key?"), 1,
|
1511
|
-
ID2SYM(rb_intern("default"))) == Qtrue) {
|
1512
|
-
VALUE default_value =
|
1513
|
-
rb_hash_lookup(options, ID2SYM(rb_intern("default")));
|
1514
|
-
|
1515
|
-
/* Call #to_s since all defaults are strings in the descriptor. */
|
1516
|
-
default_value = rb_funcall(default_value, rb_intern("to_s"), 0);
|
1517
|
-
|
1518
|
-
google_protobuf_FieldDescriptorProto_set_default_value(
|
1519
|
-
field_proto,
|
1520
|
-
FileBuilderContext_strdup(self->file_builder, default_value));
|
1521
|
-
}
|
1522
|
-
}
|
1523
|
-
|
1524
|
-
if (oneof_index >= 0) {
|
1525
|
-
google_protobuf_FieldDescriptorProto_set_oneof_index(field_proto,
|
1526
|
-
oneof_index);
|
1527
|
-
}
|
1528
|
-
}
|
1529
|
-
|
1530
|
-
static VALUE make_mapentry(VALUE _message_builder, VALUE types, int argc,
|
1531
|
-
VALUE* argv) {
|
1532
|
-
DEFINE_SELF(MessageBuilderContext, message_builder, _message_builder);
|
1533
|
-
VALUE type_class = rb_ary_entry(types, 2);
|
1534
|
-
FileBuilderContext* file_context =
|
1535
|
-
ruby_to_FileBuilderContext(message_builder->file_builder);
|
1536
|
-
google_protobuf_MessageOptions* options =
|
1537
|
-
google_protobuf_DescriptorProto_mutable_options(
|
1538
|
-
message_builder->msg_proto, file_context->arena);
|
1539
|
-
|
1540
|
-
google_protobuf_MessageOptions_set_map_entry(options, true);
|
1541
|
-
|
1542
|
-
// optional <type> key = 1;
|
1543
|
-
rb_funcall(_message_builder, rb_intern("optional"), 3,
|
1544
|
-
ID2SYM(rb_intern("key")), rb_ary_entry(types, 0), INT2NUM(1));
|
1545
|
-
|
1546
|
-
// optional <type> value = 2;
|
1547
|
-
if (type_class == Qnil) {
|
1548
|
-
rb_funcall(_message_builder, rb_intern("optional"), 3,
|
1549
|
-
ID2SYM(rb_intern("value")), rb_ary_entry(types, 1), INT2NUM(2));
|
1550
|
-
} else {
|
1551
|
-
rb_funcall(_message_builder, rb_intern("optional"), 4,
|
1552
|
-
ID2SYM(rb_intern("value")), rb_ary_entry(types, 1), INT2NUM(2),
|
1553
|
-
type_class);
|
1554
|
-
}
|
1555
|
-
|
1556
|
-
return Qnil;
|
1557
|
-
}
|
1558
|
-
|
1559
|
-
/*
|
1560
|
-
* call-seq:
|
1561
|
-
* MessageBuilderContext.optional(name, type, number, type_class = nil,
|
1562
|
-
* options = nil)
|
1563
|
-
*
|
1564
|
-
* Defines a new optional field on this message type with the given type, tag
|
1565
|
-
* number, and type class (for message and enum fields). The type must be a Ruby
|
1566
|
-
* symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
|
1567
|
-
* string, if present (as accepted by FieldDescriptor#submsg_name=).
|
1568
|
-
*/
|
1569
|
-
VALUE MessageBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
|
1570
|
-
VALUE name, type, number;
|
1571
|
-
VALUE type_class, options = Qnil;
|
1572
|
-
|
1573
|
-
rb_scan_args(argc, argv, "32", &name, &type, &number, &type_class, &options);
|
1574
|
-
|
1575
|
-
// Allow passing (name, type, number, options) or
|
1576
|
-
// (name, type, number, type_class, options)
|
1577
|
-
if (argc == 4 && RB_TYPE_P(type_class, T_HASH)) {
|
1578
|
-
options = type_class;
|
1579
|
-
type_class = Qnil;
|
1580
|
-
}
|
1581
|
-
|
1582
|
-
msgdef_add_field(_self, UPB_LABEL_OPTIONAL, name, type, number, type_class,
|
1583
|
-
options, -1, false);
|
1584
|
-
|
1585
|
-
return Qnil;
|
1586
|
-
}
|
1587
|
-
|
1588
|
-
/*
|
1589
|
-
* call-seq:
|
1590
|
-
* MessageBuilderContext.proto3_optional(name, type, number,
|
1591
|
-
* type_class = nil, options = nil)
|
1592
|
-
*
|
1593
|
-
* Defines a true proto3 optional field (that tracks presence) on this message
|
1594
|
-
* type with the given type, tag number, and type class (for message and enum
|
1595
|
-
* fields). The type must be a Ruby symbol (as accepted by
|
1596
|
-
* FieldDescriptor#type=) and the type_class must be a string, if present (as
|
1597
|
-
* accepted by FieldDescriptor#submsg_name=).
|
1598
|
-
*/
|
1599
|
-
VALUE MessageBuilderContext_proto3_optional(int argc, VALUE* argv,
|
1600
|
-
VALUE _self) {
|
1601
|
-
VALUE name, type, number;
|
1602
|
-
VALUE type_class, options = Qnil;
|
1603
|
-
|
1604
|
-
rb_scan_args(argc, argv, "32", &name, &type, &number, &type_class, &options);
|
1605
|
-
|
1606
|
-
// Allow passing (name, type, number, options) or
|
1607
|
-
// (name, type, number, type_class, options)
|
1608
|
-
if (argc == 4 && RB_TYPE_P(type_class, T_HASH)) {
|
1609
|
-
options = type_class;
|
1610
|
-
type_class = Qnil;
|
1611
|
-
}
|
1612
|
-
|
1613
|
-
msgdef_add_field(_self, UPB_LABEL_OPTIONAL, name, type, number, type_class,
|
1614
|
-
options, -1, true);
|
1615
|
-
|
1616
|
-
return Qnil;
|
1617
|
-
}
|
1618
|
-
|
1619
|
-
/*
|
1620
|
-
* call-seq:
|
1621
|
-
* MessageBuilderContext.required(name, type, number, type_class = nil,
|
1622
|
-
* options = nil)
|
1623
|
-
*
|
1624
|
-
* Defines a new required field on this message type with the given type, tag
|
1625
|
-
* number, and type class (for message and enum fields). The type must be a Ruby
|
1626
|
-
* symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
|
1627
|
-
* string, if present (as accepted by FieldDescriptor#submsg_name=).
|
1628
|
-
*
|
1629
|
-
* Proto3 does not have required fields, but this method exists for
|
1630
|
-
* completeness. Any attempt to add a message type with required fields to a
|
1631
|
-
* pool will currently result in an error.
|
1632
|
-
*/
|
1633
|
-
VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self) {
|
1634
|
-
VALUE name, type, number;
|
1635
|
-
VALUE type_class, options = Qnil;
|
1636
|
-
|
1637
|
-
rb_scan_args(argc, argv, "32", &name, &type, &number, &type_class, &options);
|
1638
|
-
|
1639
|
-
// Allow passing (name, type, number, options) or
|
1640
|
-
// (name, type, number, type_class, options)
|
1641
|
-
if (argc == 4 && RB_TYPE_P(type_class, T_HASH)) {
|
1642
|
-
options = type_class;
|
1643
|
-
type_class = Qnil;
|
1644
|
-
}
|
1645
|
-
|
1646
|
-
msgdef_add_field(_self, UPB_LABEL_REQUIRED, name, type, number, type_class,
|
1647
|
-
options, -1, false);
|
1648
|
-
|
1649
|
-
return Qnil;
|
1650
|
-
}
|
1651
|
-
|
1652
|
-
/*
|
1653
|
-
* call-seq:
|
1654
|
-
* MessageBuilderContext.repeated(name, type, number, type_class = nil)
|
1655
|
-
*
|
1656
|
-
* Defines a new repeated field on this message type with the given type, tag
|
1657
|
-
* number, and type class (for message and enum fields). The type must be a Ruby
|
1658
|
-
* symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
|
1659
|
-
* string, if present (as accepted by FieldDescriptor#submsg_name=).
|
1660
|
-
*/
|
1661
|
-
VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self) {
|
1662
|
-
VALUE name, type, number, type_class;
|
1663
|
-
|
1664
|
-
if (argc < 3) {
|
1665
|
-
rb_raise(rb_eArgError, "Expected at least 3 arguments.");
|
1666
|
-
}
|
1667
|
-
name = argv[0];
|
1668
|
-
type = argv[1];
|
1669
|
-
number = argv[2];
|
1670
|
-
type_class = (argc > 3) ? argv[3] : Qnil;
|
1671
|
-
|
1672
|
-
msgdef_add_field(_self, UPB_LABEL_REPEATED, name, type, number, type_class,
|
1673
|
-
Qnil, -1, false);
|
1674
|
-
|
1675
|
-
return Qnil;
|
1676
|
-
}
|
1677
|
-
|
1678
|
-
/*
|
1679
|
-
* call-seq:
|
1680
|
-
* MessageBuilderContext.map(name, key_type, value_type, number,
|
1681
|
-
* value_type_class = nil)
|
1682
|
-
*
|
1683
|
-
* Defines a new map field on this message type with the given key and value
|
1684
|
-
* types, tag number, and type class (for message and enum value types). The key
|
1685
|
-
* type must be :int32/:uint32/:int64/:uint64, :bool, or :string. The value type
|
1686
|
-
* type must be a Ruby symbol (as accepted by FieldDescriptor#type=) and the
|
1687
|
-
* type_class must be a string, if present (as accepted by
|
1688
|
-
* FieldDescriptor#submsg_name=).
|
1689
|
-
*/
|
1690
|
-
VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self) {
|
1691
|
-
DEFINE_SELF(MessageBuilderContext, self, _self);
|
1692
|
-
VALUE name, key_type, value_type, number, type_class;
|
1693
|
-
VALUE mapentry_desc_name;
|
1694
|
-
FileBuilderContext* file_builder;
|
1695
|
-
upb_strview msg_name;
|
1696
|
-
|
1697
|
-
if (argc < 4) {
|
1698
|
-
rb_raise(rb_eArgError, "Expected at least 4 arguments.");
|
1699
|
-
}
|
1700
|
-
name = argv[0];
|
1701
|
-
key_type = argv[1];
|
1702
|
-
value_type = argv[2];
|
1703
|
-
number = argv[3];
|
1704
|
-
type_class = (argc > 4) ? argv[4] : Qnil;
|
1705
|
-
|
1706
|
-
// Validate the key type. We can't accept enums, messages, or floats/doubles
|
1707
|
-
// as map keys. (We exclude these explicitly, and the field-descriptor setter
|
1708
|
-
// below then ensures that the type is one of the remaining valid options.)
|
1709
|
-
if (SYM2ID(key_type) == rb_intern("float") ||
|
1710
|
-
SYM2ID(key_type) == rb_intern("double") ||
|
1711
|
-
SYM2ID(key_type) == rb_intern("enum") ||
|
1712
|
-
SYM2ID(key_type) == rb_intern("message")) {
|
1713
|
-
rb_raise(rb_eArgError,
|
1714
|
-
"Cannot add a map field with a float, double, enum, or message "
|
1715
|
-
"type.");
|
1716
|
-
}
|
1717
|
-
|
1718
|
-
file_builder = ruby_to_FileBuilderContext(self->file_builder);
|
1719
|
-
|
1720
|
-
// TODO(haberman): remove this restriction, maps are supported in proto2.
|
1721
|
-
if (upb_strview_eql(
|
1722
|
-
google_protobuf_FileDescriptorProto_syntax(file_builder->file_proto),
|
1723
|
-
upb_strview_makez("proto2"))) {
|
1724
|
-
rb_raise(rb_eArgError,
|
1725
|
-
"Cannot add a native map field using proto2 syntax.");
|
1726
|
-
}
|
1727
|
-
|
1728
|
-
// Create a new message descriptor for the map entry message, and create a
|
1729
|
-
// repeated submessage field here with that type.
|
1730
|
-
msg_name = google_protobuf_DescriptorProto_name(self->msg_proto);
|
1731
|
-
mapentry_desc_name = rb_str_new(msg_name.data, msg_name.size);
|
1732
|
-
mapentry_desc_name = rb_str_cat2(mapentry_desc_name, "_MapEntry_");
|
1733
|
-
mapentry_desc_name =
|
1734
|
-
rb_str_cat2(mapentry_desc_name, rb_id2name(SYM2ID(name)));
|
1735
|
-
|
1736
|
-
{
|
1737
|
-
// message <msgname>_MapEntry_ { /* ... */ }
|
1738
|
-
VALUE args[1] = {mapentry_desc_name};
|
1739
|
-
VALUE types = rb_ary_new3(3, key_type, value_type, type_class);
|
1740
|
-
rb_block_call(self->file_builder, rb_intern("add_message"), 1, args,
|
1741
|
-
make_mapentry, types);
|
1742
|
-
}
|
1743
|
-
|
1744
|
-
// If this file is in a package, we need to qualify the map entry type.
|
1745
|
-
if (google_protobuf_FileDescriptorProto_has_package(file_builder->file_proto)) {
|
1746
|
-
upb_strview package_view =
|
1747
|
-
google_protobuf_FileDescriptorProto_package(file_builder->file_proto);
|
1748
|
-
VALUE package = rb_str_new(package_view.data, package_view.size);
|
1749
|
-
package = rb_str_cat2(package, ".");
|
1750
|
-
mapentry_desc_name = rb_str_concat(package, mapentry_desc_name);
|
1751
|
-
}
|
1752
|
-
|
1753
|
-
// repeated MapEntry <name> = <number>;
|
1754
|
-
rb_funcall(_self, rb_intern("repeated"), 4, name,
|
1755
|
-
ID2SYM(rb_intern("message")), number, mapentry_desc_name);
|
1756
|
-
|
1757
|
-
return Qnil;
|
1146
|
+
static void EnumDescriptor_register(VALUE module) {
|
1147
|
+
VALUE klass = rb_define_class_under(module, "EnumDescriptor", rb_cObject);
|
1148
|
+
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;
|
1758
1159
|
}
|
1759
1160
|
|
1760
|
-
|
1761
|
-
|
1762
|
-
|
1763
|
-
|
1764
|
-
* Creates a new OneofDescriptor with the given name, creates a
|
1765
|
-
* OneofBuilderContext attached to that OneofDescriptor, evaluates the given
|
1766
|
-
* block in the context of that OneofBuilderContext with #instance_eval, and
|
1767
|
-
* then adds the oneof to the message.
|
1768
|
-
*
|
1769
|
-
* This is the recommended, idiomatic way to build oneof definitions.
|
1770
|
-
*/
|
1771
|
-
VALUE MessageBuilderContext_oneof(VALUE _self, VALUE name) {
|
1772
|
-
DEFINE_SELF(MessageBuilderContext, self, _self);
|
1773
|
-
size_t oneof_count;
|
1774
|
-
FileBuilderContext* file_context =
|
1775
|
-
ruby_to_FileBuilderContext(self->file_builder);
|
1776
|
-
google_protobuf_OneofDescriptorProto* oneof_proto;
|
1777
|
-
|
1778
|
-
// Existing oneof_count becomes oneof_index.
|
1779
|
-
google_protobuf_DescriptorProto_oneof_decl(self->msg_proto, &oneof_count);
|
1780
|
-
|
1781
|
-
// Create oneof_proto and set its name.
|
1782
|
-
oneof_proto = google_protobuf_DescriptorProto_add_oneof_decl(
|
1783
|
-
self->msg_proto, file_context->arena);
|
1784
|
-
google_protobuf_OneofDescriptorProto_set_name(
|
1785
|
-
oneof_proto, FileBuilderContext_strdup_sym(self->file_builder, name));
|
1786
|
-
|
1787
|
-
// Evaluate the block with the builder as argument.
|
1788
|
-
{
|
1789
|
-
VALUE args[2] = { INT2NUM(oneof_count), _self };
|
1790
|
-
VALUE ctx = rb_class_new_instance(2, args, cOneofBuilderContext);
|
1791
|
-
VALUE block = rb_block_proc();
|
1792
|
-
rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
|
1793
|
-
}
|
1161
|
+
static VALUE get_def_obj(VALUE _descriptor_pool, const void* ptr, VALUE klass) {
|
1162
|
+
DescriptorPool* descriptor_pool = ruby_to_DescriptorPool(_descriptor_pool);
|
1163
|
+
VALUE key = ULL2NUM((intptr_t)ptr);
|
1164
|
+
VALUE def;
|
1794
1165
|
|
1795
|
-
|
1796
|
-
}
|
1166
|
+
def = rb_hash_aref(descriptor_pool->def_to_descriptor, key);
|
1797
1167
|
|
1798
|
-
|
1799
|
-
|
1800
|
-
FileBuilderContext* file_context =
|
1801
|
-
ruby_to_FileBuilderContext(self->file_builder);
|
1802
|
-
size_t field_count, oneof_count;
|
1803
|
-
google_protobuf_FieldDescriptorProto** fields =
|
1804
|
-
google_protobuf_DescriptorProto_mutable_field(self->msg_proto, &field_count);
|
1805
|
-
const google_protobuf_OneofDescriptorProto*const* oneofs =
|
1806
|
-
google_protobuf_DescriptorProto_oneof_decl(self->msg_proto, &oneof_count);
|
1807
|
-
VALUE names = rb_hash_new();
|
1808
|
-
VALUE underscore = rb_str_new2("_");
|
1809
|
-
size_t i;
|
1810
|
-
|
1811
|
-
// We have to build a set of all names, to ensure that synthetic oneofs are
|
1812
|
-
// not creating conflicts.
|
1813
|
-
for (i = 0; i < field_count; i++) {
|
1814
|
-
upb_strview name = google_protobuf_FieldDescriptorProto_name(fields[i]);
|
1815
|
-
rb_hash_aset(names, rb_str_new(name.data, name.size), Qtrue);
|
1816
|
-
}
|
1817
|
-
for (i = 0; i < oneof_count; i++) {
|
1818
|
-
upb_strview name = google_protobuf_OneofDescriptorProto_name(oneofs[i]);
|
1819
|
-
rb_hash_aset(names, rb_str_new(name.data, name.size), Qtrue);
|
1168
|
+
if (ptr == NULL) {
|
1169
|
+
return Qnil;
|
1820
1170
|
}
|
1821
1171
|
|
1822
|
-
|
1823
|
-
|
1824
|
-
VALUE
|
1825
|
-
|
1826
|
-
|
1827
|
-
if (!google_protobuf_FieldDescriptorProto_proto3_optional(fields[i])) {
|
1828
|
-
continue;
|
1829
|
-
}
|
1830
|
-
|
1831
|
-
// Prepend '_' until we are no longer conflicting.
|
1832
|
-
field_name = google_protobuf_FieldDescriptorProto_name(fields[i]);
|
1833
|
-
oneof_name = rb_str_new(field_name.data, field_name.size);
|
1834
|
-
while (rb_hash_lookup(names, oneof_name) != Qnil) {
|
1835
|
-
oneof_name = rb_str_plus(underscore, oneof_name);
|
1836
|
-
}
|
1837
|
-
|
1838
|
-
rb_hash_aset(names, oneof_name, Qtrue);
|
1839
|
-
google_protobuf_FieldDescriptorProto_set_oneof_index(fields[i],
|
1840
|
-
oneof_count++);
|
1841
|
-
oneof_proto = google_protobuf_DescriptorProto_add_oneof_decl(
|
1842
|
-
self->msg_proto, file_context->arena);
|
1843
|
-
google_protobuf_OneofDescriptorProto_set_name(
|
1844
|
-
oneof_proto, FileBuilderContext_strdup(self->file_builder, oneof_name));
|
1172
|
+
if (def == Qnil) {
|
1173
|
+
// Lazily create wrapper object.
|
1174
|
+
VALUE args[3] = {c_only_cookie, _descriptor_pool, key};
|
1175
|
+
def = rb_class_new_instance(3, args, klass);
|
1176
|
+
rb_hash_aset(descriptor_pool->def_to_descriptor, key, def);
|
1845
1177
|
}
|
1846
|
-
}
|
1847
|
-
|
1848
|
-
// -----------------------------------------------------------------------------
|
1849
|
-
// OneofBuilderContext.
|
1850
|
-
// -----------------------------------------------------------------------------
|
1851
|
-
|
1852
|
-
DEFINE_CLASS(OneofBuilderContext,
|
1853
|
-
"Google::Protobuf::Internal::OneofBuilderContext");
|
1854
|
-
|
1855
|
-
void OneofBuilderContext_mark(void* _self) {
|
1856
|
-
OneofBuilderContext* self = _self;
|
1857
|
-
rb_gc_mark(self->message_builder);
|
1858
|
-
}
|
1859
|
-
|
1860
|
-
void OneofBuilderContext_free(void* _self) {
|
1861
|
-
xfree(_self);
|
1862
|
-
}
|
1863
|
-
|
1864
|
-
VALUE OneofBuilderContext_alloc(VALUE klass) {
|
1865
|
-
OneofBuilderContext* self = ALLOC(OneofBuilderContext);
|
1866
|
-
VALUE ret = TypedData_Wrap_Struct(
|
1867
|
-
klass, &_OneofBuilderContext_type, self);
|
1868
|
-
self->oneof_index = 0;
|
1869
|
-
self->message_builder = Qnil;
|
1870
|
-
return ret;
|
1871
|
-
}
|
1872
1178
|
|
1873
|
-
|
1874
|
-
VALUE klass = rb_define_class_under(
|
1875
|
-
module, "OneofBuilderContext", rb_cObject);
|
1876
|
-
rb_define_alloc_func(klass, OneofBuilderContext_alloc);
|
1877
|
-
rb_define_method(klass, "initialize",
|
1878
|
-
OneofBuilderContext_initialize, 2);
|
1879
|
-
rb_define_method(klass, "optional", OneofBuilderContext_optional, -1);
|
1880
|
-
rb_gc_register_address(&cOneofBuilderContext);
|
1881
|
-
cOneofBuilderContext = klass;
|
1882
|
-
}
|
1883
|
-
|
1884
|
-
/*
|
1885
|
-
* call-seq:
|
1886
|
-
* OneofBuilderContext.new(oneof_index, message_builder) => context
|
1887
|
-
*
|
1888
|
-
* Create a new oneof builder context around the given oneof descriptor and
|
1889
|
-
* builder context. This class is intended to serve as a DSL context to be used
|
1890
|
-
* with #instance_eval.
|
1891
|
-
*/
|
1892
|
-
VALUE OneofBuilderContext_initialize(VALUE _self,
|
1893
|
-
VALUE oneof_index,
|
1894
|
-
VALUE message_builder) {
|
1895
|
-
DEFINE_SELF(OneofBuilderContext, self, _self);
|
1896
|
-
self->oneof_index = NUM2INT(oneof_index);
|
1897
|
-
self->message_builder = message_builder;
|
1898
|
-
return Qnil;
|
1899
|
-
}
|
1900
|
-
|
1901
|
-
/*
|
1902
|
-
* call-seq:
|
1903
|
-
* OneofBuilderContext.optional(name, type, number, type_class = nil,
|
1904
|
-
* default_value = nil)
|
1905
|
-
*
|
1906
|
-
* Defines a new optional field in this oneof with the given type, tag number,
|
1907
|
-
* and type class (for message and enum fields). The type must be a Ruby symbol
|
1908
|
-
* (as accepted by FieldDescriptor#type=) and the type_class must be a string,
|
1909
|
-
* if present (as accepted by FieldDescriptor#submsg_name=).
|
1910
|
-
*/
|
1911
|
-
VALUE OneofBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
|
1912
|
-
DEFINE_SELF(OneofBuilderContext, self, _self);
|
1913
|
-
VALUE name, type, number;
|
1914
|
-
VALUE type_class, options = Qnil;
|
1915
|
-
|
1916
|
-
rb_scan_args(argc, argv, "32", &name, &type, &number, &type_class, &options);
|
1917
|
-
|
1918
|
-
msgdef_add_field(self->message_builder, UPB_LABEL_OPTIONAL, name, type,
|
1919
|
-
number, type_class, options, self->oneof_index, false);
|
1920
|
-
|
1921
|
-
return Qnil;
|
1922
|
-
}
|
1923
|
-
|
1924
|
-
// -----------------------------------------------------------------------------
|
1925
|
-
// EnumBuilderContext.
|
1926
|
-
// -----------------------------------------------------------------------------
|
1927
|
-
|
1928
|
-
DEFINE_CLASS(EnumBuilderContext,
|
1929
|
-
"Google::Protobuf::Internal::EnumBuilderContext");
|
1930
|
-
|
1931
|
-
void EnumBuilderContext_mark(void* _self) {
|
1932
|
-
EnumBuilderContext* self = _self;
|
1933
|
-
rb_gc_mark(self->file_builder);
|
1179
|
+
return def;
|
1934
1180
|
}
|
1935
1181
|
|
1936
|
-
|
1937
|
-
|
1182
|
+
static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_MessageDef* def) {
|
1183
|
+
return get_def_obj(descriptor_pool, def, cDescriptor);
|
1938
1184
|
}
|
1939
1185
|
|
1940
|
-
VALUE
|
1941
|
-
|
1942
|
-
VALUE ret = TypedData_Wrap_Struct(
|
1943
|
-
klass, &_EnumBuilderContext_type, self);
|
1944
|
-
self->enum_proto = NULL;
|
1945
|
-
self->file_builder = Qnil;
|
1946
|
-
return ret;
|
1186
|
+
static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_EnumDef* def) {
|
1187
|
+
return get_def_obj(descriptor_pool, def, cEnumDescriptor);
|
1947
1188
|
}
|
1948
1189
|
|
1949
|
-
|
1950
|
-
|
1951
|
-
module, "EnumBuilderContext", rb_cObject);
|
1952
|
-
rb_define_alloc_func(klass, EnumBuilderContext_alloc);
|
1953
|
-
rb_define_method(klass, "initialize", EnumBuilderContext_initialize, 2);
|
1954
|
-
rb_define_method(klass, "value", EnumBuilderContext_value, 2);
|
1955
|
-
rb_gc_register_address(&cEnumBuilderContext);
|
1956
|
-
cEnumBuilderContext = klass;
|
1190
|
+
static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_FieldDef* def) {
|
1191
|
+
return get_def_obj(descriptor_pool, def, cFieldDescriptor);
|
1957
1192
|
}
|
1958
1193
|
|
1959
|
-
|
1960
|
-
|
1961
|
-
* EnumBuilderContext.new(file_builder) => context
|
1962
|
-
*
|
1963
|
-
* Create a new builder context around the given enum descriptor. This class is
|
1964
|
-
* intended to serve as a DSL context to be used with #instance_eval.
|
1965
|
-
*/
|
1966
|
-
VALUE EnumBuilderContext_initialize(VALUE _self, VALUE _file_builder,
|
1967
|
-
VALUE name) {
|
1968
|
-
DEFINE_SELF(EnumBuilderContext, self, _self);
|
1969
|
-
FileBuilderContext* file_builder = ruby_to_FileBuilderContext(_file_builder);
|
1970
|
-
google_protobuf_FileDescriptorProto* file_proto = file_builder->file_proto;
|
1971
|
-
|
1972
|
-
self->file_builder = _file_builder;
|
1973
|
-
self->enum_proto = google_protobuf_FileDescriptorProto_add_enum_type(
|
1974
|
-
file_proto, file_builder->arena);
|
1975
|
-
|
1976
|
-
google_protobuf_EnumDescriptorProto_set_name(
|
1977
|
-
self->enum_proto, FileBuilderContext_strdup(_file_builder, name));
|
1978
|
-
|
1979
|
-
return Qnil;
|
1194
|
+
static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_FileDef* def) {
|
1195
|
+
return get_def_obj(descriptor_pool, def, cFileDescriptor);
|
1980
1196
|
}
|
1981
1197
|
|
1982
|
-
|
1983
|
-
|
1984
|
-
* EnumBuilder.add_value(name, number)
|
1985
|
-
*
|
1986
|
-
* Adds the given name => number mapping to the enum type. Name must be a Ruby
|
1987
|
-
* symbol.
|
1988
|
-
*/
|
1989
|
-
VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number) {
|
1990
|
-
DEFINE_SELF(EnumBuilderContext, self, _self);
|
1991
|
-
FileBuilderContext* file_builder =
|
1992
|
-
ruby_to_FileBuilderContext(self->file_builder);
|
1993
|
-
google_protobuf_EnumValueDescriptorProto* enum_value;
|
1994
|
-
|
1995
|
-
enum_value = google_protobuf_EnumDescriptorProto_add_value(
|
1996
|
-
self->enum_proto, file_builder->arena);
|
1997
|
-
|
1998
|
-
google_protobuf_EnumValueDescriptorProto_set_name(
|
1999
|
-
enum_value, FileBuilderContext_strdup_sym(self->file_builder, name));
|
2000
|
-
google_protobuf_EnumValueDescriptorProto_set_number(enum_value,
|
2001
|
-
NUM2INT(number));
|
2002
|
-
|
2003
|
-
return Qnil;
|
1198
|
+
static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_OneofDef* def) {
|
1199
|
+
return get_def_obj(descriptor_pool, def, cOneofDescriptor);
|
2004
1200
|
}
|
2005
1201
|
|
2006
|
-
|
2007
1202
|
// -----------------------------------------------------------------------------
|
2008
|
-
//
|
1203
|
+
// Shared functions
|
2009
1204
|
// -----------------------------------------------------------------------------
|
2010
1205
|
|
2011
|
-
|
2012
|
-
"Google::Protobuf::Internal::FileBuilderContext");
|
1206
|
+
// Functions exposed to other modules in defs.h.
|
2013
1207
|
|
2014
|
-
|
2015
|
-
|
2016
|
-
|
2017
|
-
|
2018
|
-
|
2019
|
-
|
2020
|
-
|
2021
|
-
upb_arena_free(self->arena);
|
2022
|
-
xfree(self);
|
2023
|
-
}
|
2024
|
-
|
2025
|
-
upb_strview FileBuilderContext_strdup2(VALUE _self, const char *str) {
|
2026
|
-
DEFINE_SELF(FileBuilderContext, self, _self);
|
2027
|
-
upb_strview ret;
|
2028
|
-
char *data;
|
2029
|
-
|
2030
|
-
ret.size = strlen(str);
|
2031
|
-
data = upb_malloc(upb_arena_alloc(self->arena), ret.size + 1);
|
2032
|
-
ret.data = data;
|
2033
|
-
memcpy(data, str, ret.size);
|
2034
|
-
/* Null-terminate required by rewrite_enum_defaults() above. */
|
2035
|
-
data[ret.size] = '\0';
|
2036
|
-
return ret;
|
2037
|
-
}
|
2038
|
-
|
2039
|
-
upb_strview FileBuilderContext_strdup(VALUE _self, VALUE rb_str) {
|
2040
|
-
return FileBuilderContext_strdup2(_self, get_str(rb_str));
|
2041
|
-
}
|
2042
|
-
|
2043
|
-
upb_strview FileBuilderContext_strdup_sym(VALUE _self, VALUE rb_sym) {
|
2044
|
-
Check_Type(rb_sym, T_SYMBOL);
|
2045
|
-
return FileBuilderContext_strdup(_self, rb_id2str(SYM2ID(rb_sym)));
|
1208
|
+
VALUE Descriptor_DefToClass(const upb_MessageDef* m) {
|
1209
|
+
const upb_DefPool* symtab = upb_FileDef_Pool(upb_MessageDef_File(m));
|
1210
|
+
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;
|
2046
1215
|
}
|
2047
1216
|
|
2048
|
-
|
2049
|
-
|
2050
|
-
|
2051
|
-
self->arena = upb_arena_new();
|
2052
|
-
self->file_proto = google_protobuf_FileDescriptorProto_new(self->arena);
|
2053
|
-
self->descriptor_pool = Qnil;
|
2054
|
-
return ret;
|
2055
|
-
}
|
2056
|
-
|
2057
|
-
void FileBuilderContext_register(VALUE module) {
|
2058
|
-
VALUE klass = rb_define_class_under(module, "FileBuilderContext", rb_cObject);
|
2059
|
-
rb_define_alloc_func(klass, FileBuilderContext_alloc);
|
2060
|
-
rb_define_method(klass, "initialize", FileBuilderContext_initialize, 3);
|
2061
|
-
rb_define_method(klass, "add_message", FileBuilderContext_add_message, 1);
|
2062
|
-
rb_define_method(klass, "add_enum", FileBuilderContext_add_enum, 1);
|
2063
|
-
rb_gc_register_address(&cFileBuilderContext);
|
2064
|
-
cFileBuilderContext = klass;
|
1217
|
+
const upb_MessageDef* Descriptor_GetMsgDef(VALUE desc_rb) {
|
1218
|
+
const Descriptor* desc = ruby_to_Descriptor(desc_rb);
|
1219
|
+
return desc->msgdef;
|
2065
1220
|
}
|
2066
1221
|
|
2067
|
-
|
2068
|
-
|
2069
|
-
|
2070
|
-
|
2071
|
-
|
2072
|
-
* builder context. This class is intended to serve as a DSL context to be used
|
2073
|
-
* with #instance_eval.
|
2074
|
-
*/
|
2075
|
-
VALUE FileBuilderContext_initialize(VALUE _self, VALUE descriptor_pool,
|
2076
|
-
VALUE name, VALUE options) {
|
2077
|
-
DEFINE_SELF(FileBuilderContext, self, _self);
|
2078
|
-
self->descriptor_pool = descriptor_pool;
|
2079
|
-
|
2080
|
-
google_protobuf_FileDescriptorProto_set_name(
|
2081
|
-
self->file_proto, FileBuilderContext_strdup(_self, name));
|
2082
|
-
|
2083
|
-
// Default syntax for Ruby is proto3.
|
2084
|
-
google_protobuf_FileDescriptorProto_set_syntax(
|
2085
|
-
self->file_proto,
|
2086
|
-
FileBuilderContext_strdup(_self, rb_str_new2("proto3")));
|
2087
|
-
|
2088
|
-
if (options != Qnil) {
|
2089
|
-
VALUE syntax;
|
2090
|
-
|
2091
|
-
Check_Type(options, T_HASH);
|
2092
|
-
syntax = rb_hash_lookup2(options, ID2SYM(rb_intern("syntax")), Qnil);
|
2093
|
-
|
2094
|
-
if (syntax != Qnil) {
|
2095
|
-
VALUE syntax_str;
|
2096
|
-
|
2097
|
-
Check_Type(syntax, T_SYMBOL);
|
2098
|
-
syntax_str = rb_id2str(SYM2ID(syntax));
|
2099
|
-
google_protobuf_FileDescriptorProto_set_syntax(
|
2100
|
-
self->file_proto, FileBuilderContext_strdup(_self, syntax_str));
|
1222
|
+
VALUE TypeInfo_InitArg(int argc, VALUE* argv, int skip_arg) {
|
1223
|
+
if (argc > skip_arg) {
|
1224
|
+
if (argc > 1 + skip_arg) {
|
1225
|
+
rb_raise(rb_eArgError, "Expected a maximum of %d arguments.",
|
1226
|
+
skip_arg + 1);
|
2101
1227
|
}
|
1228
|
+
return argv[skip_arg];
|
1229
|
+
} else {
|
1230
|
+
return Qnil;
|
2102
1231
|
}
|
2103
|
-
|
2104
|
-
return Qnil;
|
2105
|
-
}
|
2106
|
-
|
2107
|
-
/*
|
2108
|
-
* call-seq:
|
2109
|
-
* FileBuilderContext.add_message(name, &block)
|
2110
|
-
*
|
2111
|
-
* Creates a new, empty descriptor with the given name, and invokes the block in
|
2112
|
-
* the context of a MessageBuilderContext on that descriptor. The block can then
|
2113
|
-
* call, e.g., MessageBuilderContext#optional and MessageBuilderContext#repeated
|
2114
|
-
* methods to define the message fields.
|
2115
|
-
*
|
2116
|
-
* This is the recommended, idiomatic way to build message definitions.
|
2117
|
-
*/
|
2118
|
-
VALUE FileBuilderContext_add_message(VALUE _self, VALUE name) {
|
2119
|
-
VALUE args[2] = { _self, name };
|
2120
|
-
VALUE ctx = rb_class_new_instance(2, args, cMessageBuilderContext);
|
2121
|
-
VALUE block = rb_block_proc();
|
2122
|
-
rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
|
2123
|
-
MessageBuilderContext_add_synthetic_oneofs(ctx);
|
2124
|
-
return Qnil;
|
2125
|
-
}
|
2126
|
-
|
2127
|
-
/*
|
2128
|
-
* call-seq:
|
2129
|
-
* FileBuilderContext.add_enum(name, &block)
|
2130
|
-
*
|
2131
|
-
* Creates a new, empty enum descriptor with the given name, and invokes the
|
2132
|
-
* block in the context of an EnumBuilderContext on that descriptor. The block
|
2133
|
-
* can then call EnumBuilderContext#add_value to define the enum values.
|
2134
|
-
*
|
2135
|
-
* This is the recommended, idiomatic way to build enum definitions.
|
2136
|
-
*/
|
2137
|
-
VALUE FileBuilderContext_add_enum(VALUE _self, VALUE name) {
|
2138
|
-
VALUE args[2] = { _self, name };
|
2139
|
-
VALUE ctx = rb_class_new_instance(2, args, cEnumBuilderContext);
|
2140
|
-
VALUE block = rb_block_proc();
|
2141
|
-
rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
|
2142
|
-
return Qnil;
|
2143
|
-
}
|
2144
|
-
|
2145
|
-
void FileBuilderContext_build(VALUE _self) {
|
2146
|
-
DEFINE_SELF(FileBuilderContext, self, _self);
|
2147
|
-
DescriptorPool* pool = ruby_to_DescriptorPool(self->descriptor_pool);
|
2148
|
-
upb_status status;
|
2149
|
-
|
2150
|
-
rewrite_enum_defaults(pool->symtab, self->file_proto);
|
2151
|
-
rewrite_names(_self, self->file_proto);
|
2152
|
-
|
2153
|
-
upb_status_clear(&status);
|
2154
|
-
if (!upb_symtab_addfile(pool->symtab, self->file_proto, &status)) {
|
2155
|
-
rb_raise(cTypeError, "Unable to add defs to DescriptorPool: %s",
|
2156
|
-
upb_status_errmsg(&status));
|
2157
|
-
}
|
2158
|
-
}
|
2159
|
-
|
2160
|
-
// -----------------------------------------------------------------------------
|
2161
|
-
// Builder.
|
2162
|
-
// -----------------------------------------------------------------------------
|
2163
|
-
|
2164
|
-
DEFINE_CLASS(Builder, "Google::Protobuf::Internal::Builder");
|
2165
|
-
|
2166
|
-
void Builder_mark(void* _self) {
|
2167
|
-
Builder* self = _self;
|
2168
|
-
rb_gc_mark(self->descriptor_pool);
|
2169
|
-
rb_gc_mark(self->default_file_builder);
|
2170
|
-
}
|
2171
|
-
|
2172
|
-
void Builder_free(void* _self) {
|
2173
|
-
xfree(_self);
|
2174
|
-
}
|
2175
|
-
|
2176
|
-
VALUE Builder_alloc(VALUE klass) {
|
2177
|
-
Builder* self = ALLOC(Builder);
|
2178
|
-
VALUE ret = TypedData_Wrap_Struct(
|
2179
|
-
klass, &_Builder_type, self);
|
2180
|
-
self->descriptor_pool = Qnil;
|
2181
|
-
self->default_file_builder = Qnil;
|
2182
|
-
return ret;
|
2183
|
-
}
|
2184
|
-
|
2185
|
-
void Builder_register(VALUE module) {
|
2186
|
-
VALUE klass = rb_define_class_under(module, "Builder", rb_cObject);
|
2187
|
-
rb_define_alloc_func(klass, Builder_alloc);
|
2188
|
-
rb_define_method(klass, "initialize", Builder_initialize, 1);
|
2189
|
-
rb_define_method(klass, "add_file", Builder_add_file, -1);
|
2190
|
-
rb_define_method(klass, "add_message", Builder_add_message, 1);
|
2191
|
-
rb_define_method(klass, "add_enum", Builder_add_enum, 1);
|
2192
|
-
rb_gc_register_address(&cBuilder);
|
2193
|
-
cBuilder = klass;
|
2194
|
-
}
|
2195
|
-
|
2196
|
-
/*
|
2197
|
-
* call-seq:
|
2198
|
-
* Builder.new(descriptor_pool) => builder
|
2199
|
-
*
|
2200
|
-
* Creates a new Builder. A Builder can accumulate a set of new message and enum
|
2201
|
-
* descriptors and atomically register them into a pool in a way that allows for
|
2202
|
-
* (co)recursive type references.
|
2203
|
-
*/
|
2204
|
-
VALUE Builder_initialize(VALUE _self, VALUE pool) {
|
2205
|
-
DEFINE_SELF(Builder, self, _self);
|
2206
|
-
self->descriptor_pool = pool;
|
2207
|
-
self->default_file_builder = Qnil; // Created lazily if needed.
|
2208
|
-
return Qnil;
|
2209
|
-
}
|
2210
|
-
|
2211
|
-
/*
|
2212
|
-
* call-seq:
|
2213
|
-
* Builder.add_file(name, options = nil, &block)
|
2214
|
-
*
|
2215
|
-
* Creates a new, file descriptor with the given name and options and invokes
|
2216
|
-
* the block in the context of a FileBuilderContext on that descriptor. The
|
2217
|
-
* block can then call FileBuilderContext#add_message or
|
2218
|
-
* FileBuilderContext#add_enum to define new messages or enums, respectively.
|
2219
|
-
*
|
2220
|
-
* This is the recommended, idiomatic way to build file descriptors.
|
2221
|
-
*/
|
2222
|
-
VALUE Builder_add_file(int argc, VALUE* argv, VALUE _self) {
|
2223
|
-
DEFINE_SELF(Builder, self, _self);
|
2224
|
-
VALUE name, options;
|
2225
|
-
VALUE ctx;
|
2226
|
-
VALUE block;
|
2227
|
-
|
2228
|
-
rb_scan_args(argc, argv, "11", &name, &options);
|
2229
|
-
|
2230
|
-
{
|
2231
|
-
VALUE args[3] = { self->descriptor_pool, name, options };
|
2232
|
-
ctx = rb_class_new_instance(3, args, cFileBuilderContext);
|
2233
|
-
}
|
2234
|
-
|
2235
|
-
block = rb_block_proc();
|
2236
|
-
rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
|
2237
|
-
FileBuilderContext_build(ctx);
|
2238
|
-
|
2239
|
-
return Qnil;
|
2240
|
-
}
|
2241
|
-
|
2242
|
-
static VALUE Builder_get_default_file(VALUE _self) {
|
2243
|
-
DEFINE_SELF(Builder, self, _self);
|
2244
|
-
|
2245
|
-
/* Lazily create only if legacy builder-level methods are called. */
|
2246
|
-
if (self->default_file_builder == Qnil) {
|
2247
|
-
VALUE name = rb_str_new2("ruby_default_file.proto");
|
2248
|
-
VALUE args [3] = { self->descriptor_pool, name, rb_hash_new() };
|
2249
|
-
self->default_file_builder =
|
2250
|
-
rb_class_new_instance(3, args, cFileBuilderContext);
|
2251
|
-
}
|
2252
|
-
|
2253
|
-
return self->default_file_builder;
|
2254
|
-
}
|
2255
|
-
|
2256
|
-
/*
|
2257
|
-
* call-seq:
|
2258
|
-
* Builder.add_message(name, &block)
|
2259
|
-
*
|
2260
|
-
* Old and deprecated way to create a new descriptor.
|
2261
|
-
* See FileBuilderContext.add_message for the recommended way.
|
2262
|
-
*
|
2263
|
-
* Exists for backwards compatibility to allow building descriptor pool for
|
2264
|
-
* files generated by protoc which don't add messages within "add_file" block.
|
2265
|
-
* Descriptors created this way get assigned to a default empty FileDescriptor.
|
2266
|
-
*/
|
2267
|
-
VALUE Builder_add_message(VALUE _self, VALUE name) {
|
2268
|
-
VALUE file_builder = Builder_get_default_file(_self);
|
2269
|
-
rb_funcall_with_block(file_builder, rb_intern("add_message"), 1, &name,
|
2270
|
-
rb_block_proc());
|
2271
|
-
return Qnil;
|
2272
|
-
}
|
2273
|
-
|
2274
|
-
/*
|
2275
|
-
* call-seq:
|
2276
|
-
* Builder.add_enum(name, &block)
|
2277
|
-
*
|
2278
|
-
* Old and deprecated way to create a new enum descriptor.
|
2279
|
-
* See FileBuilderContext.add_enum for the recommended way.
|
2280
|
-
*
|
2281
|
-
* Exists for backwards compatibility to allow building descriptor pool for
|
2282
|
-
* files generated by protoc which don't add enums within "add_file" block.
|
2283
|
-
* Enum descriptors created this way get assigned to a default empty
|
2284
|
-
* FileDescriptor.
|
2285
|
-
*/
|
2286
|
-
VALUE Builder_add_enum(VALUE _self, VALUE name) {
|
2287
|
-
VALUE file_builder = Builder_get_default_file(_self);
|
2288
|
-
rb_funcall_with_block(file_builder, rb_intern("add_enum"), 1, &name,
|
2289
|
-
rb_block_proc());
|
2290
|
-
return Qnil;
|
2291
1232
|
}
|
2292
1233
|
|
2293
|
-
|
2294
|
-
*
|
2295
|
-
|
2296
|
-
DEFINE_SELF(Builder, self, _self);
|
2297
|
-
|
2298
|
-
if (self->default_file_builder != Qnil) {
|
2299
|
-
FileBuilderContext_build(self->default_file_builder);
|
2300
|
-
self->default_file_builder = Qnil;
|
2301
|
-
}
|
1234
|
+
TypeInfo 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])};
|
2302
1237
|
|
2303
|
-
|
2304
|
-
|
1238
|
+
if (ret.type == kUpb_CType_Message || ret.type == kUpb_CType_Enum) {
|
1239
|
+
*init_arg = TypeInfo_InitArg(argc, argv, skip_arg + 2);
|
2305
1240
|
|
2306
|
-
|
2307
|
-
|
2308
|
-
|
2309
|
-
|
1241
|
+
if (argc < 2 + skip_arg) {
|
1242
|
+
rb_raise(rb_eArgError, "Expected at least %d arguments for message/enum.",
|
1243
|
+
2 + skip_arg);
|
1244
|
+
}
|
2310
1245
|
|
2311
|
-
|
1246
|
+
VALUE klass = argv[1 + skip_arg];
|
1247
|
+
VALUE desc = MessageOrEnum_GetDescriptor(klass);
|
1248
|
+
*type_class = klass;
|
2312
1249
|
|
2313
|
-
|
2314
|
-
|
2315
|
-
|
2316
|
-
|
2317
|
-
|
2318
|
-
// Lazily create wrapper object.
|
2319
|
-
VALUE args[3] = { c_only_cookie, _descriptor_pool, key };
|
2320
|
-
def = rb_class_new_instance(3, args, klass);
|
2321
|
-
rb_hash_aset(descriptor_pool->def_to_descriptor, key, def);
|
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
|
+
}
|
2322
1255
|
|
2323
|
-
|
2324
|
-
|
2325
|
-
|
2326
|
-
|
2327
|
-
|
2328
|
-
|
2329
|
-
// C extension from a different thread. This wreaks havoc on our state
|
2330
|
-
// if we were in the middle of building handlers.
|
2331
|
-
if (klass == cDescriptor) {
|
2332
|
-
const upb_msgdef *m = ptr;
|
2333
|
-
upb_msg_field_iter it;
|
2334
|
-
for (upb_msg_field_begin(&it, m);
|
2335
|
-
!upb_msg_field_done(&it);
|
2336
|
-
upb_msg_field_next(&it)) {
|
2337
|
-
const upb_fielddef* f = upb_msg_iter_field(&it);
|
2338
|
-
if (upb_fielddef_issubmsg(f)) {
|
2339
|
-
get_msgdef_obj(_descriptor_pool, upb_fielddef_msgsubdef(f));
|
2340
|
-
}
|
2341
|
-
}
|
1256
|
+
if (ret.type == kUpb_CType_Message) {
|
1257
|
+
ret.def.msgdef = ruby_to_Descriptor(desc)->msgdef;
|
1258
|
+
Message_CheckClass(klass);
|
1259
|
+
} else {
|
1260
|
+
PBRUBY_ASSERT(ret.type == kUpb_CType_Enum);
|
1261
|
+
ret.def.enumdef = ruby_to_EnumDescriptor(desc)->enumdef;
|
2342
1262
|
}
|
1263
|
+
} else {
|
1264
|
+
*init_arg = TypeInfo_InitArg(argc, argv, skip_arg + 1);
|
2343
1265
|
}
|
2344
1266
|
|
2345
|
-
return
|
2346
|
-
}
|
2347
|
-
|
2348
|
-
VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_msgdef* def) {
|
2349
|
-
return get_def_obj(descriptor_pool, def, cDescriptor);
|
2350
|
-
}
|
2351
|
-
|
2352
|
-
VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_enumdef* def) {
|
2353
|
-
return get_def_obj(descriptor_pool, def, cEnumDescriptor);
|
2354
|
-
}
|
2355
|
-
|
2356
|
-
VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_fielddef* def) {
|
2357
|
-
return get_def_obj(descriptor_pool, def, cFieldDescriptor);
|
1267
|
+
return ret;
|
2358
1268
|
}
|
2359
1269
|
|
2360
|
-
|
2361
|
-
|
2362
|
-
|
1270
|
+
void 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);
|
2363
1277
|
|
2364
|
-
|
2365
|
-
|
1278
|
+
rb_gc_register_address(&c_only_cookie);
|
1279
|
+
c_only_cookie = rb_class_new_instance(0, NULL, rb_cObject);
|
2366
1280
|
}
|