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