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