google-protobuf 3.14.0 → 3.17.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of google-protobuf might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4efa381bee3e947252b6df3d7e26ab4163892b419131e6995d599b91da9eb069
4
- data.tar.gz: 45267244b3e9c99d5cb27aac3727f8739c6e7ad1e0097a6f0c9204aaebddb609
3
+ metadata.gz: b1ded185284a923b15342a8f5600e74debdd40f99faf40c35c1b8675636406ff
4
+ data.tar.gz: '018d94176b65ef8ddcb7b27b785dfaac029ada709e999e58bcc194bff018f984'
5
5
  SHA512:
6
- metadata.gz: ff369fc09844a202a30aaf129bcd141d5e3c576c7870f8fc202c3bf4792cd427b4515ebacd9557c2056a613832bcd425156f18d17fe40887df1b09c39146e5ec
7
- data.tar.gz: 6743af82db871d76712e06fb8b6571b61ce4e4c520191243eed922f382b9a50d7938dfbd074a0faa9369a8ed8362b89c8fe3d80e1595166f63e01fe297ae7e24
6
+ metadata.gz: f0c47bf01f20c9327b089e60d2bb36d1eee98483eb862e7434b9409a5b18dc24ab38808660b997fbbb66ed21fa012417aaeb1164019ea715c7547d5928ca1d37
7
+ data.tar.gz: 01e37346bed78463df8a19d1b60315494065094b13aa4f76bbffb872be51f163297542d8a6db2a8d7902c2987312e06f23d3b55e2ed6833f988733c90f11e97c
@@ -0,0 +1,349 @@
1
+ // Protocol Buffers - Google's data interchange format
2
+ // Copyright 2008 Google Inc. All rights reserved.
3
+ // https://developers.google.com/protocol-buffers/
4
+ //
5
+ // Redistribution and use in source and binary forms, with or without
6
+ // modification, are permitted provided that the following conditions are
7
+ // met:
8
+ //
9
+ // * Redistributions of source code must retain the above copyright
10
+ // notice, this list of conditions and the following disclaimer.
11
+ // * Redistributions in binary form must reproduce the above
12
+ // copyright notice, this list of conditions and the following disclaimer
13
+ // in the documentation and/or other materials provided with the
14
+ // distribution.
15
+ // * Neither the name of Google Inc. nor the names of its
16
+ // contributors may be used to endorse or promote products derived from
17
+ // this software without specific prior written permission.
18
+ //
19
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
+ // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
+ // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
+ // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
+ // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
+ // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
+ // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
+ // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
+ // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
+ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+
31
+ // -----------------------------------------------------------------------------
32
+ // Ruby <-> upb data conversion functions.
33
+ //
34
+ // This file Also contains a few other assorted algorithms on upb_msgval.
35
+ //
36
+ // None of the algorithms in this file require any access to the internal
37
+ // representation of Ruby or upb objects.
38
+ // -----------------------------------------------------------------------------
39
+
40
+ #include "convert.h"
41
+
42
+ #include "message.h"
43
+ #include "protobuf.h"
44
+ #include "third_party/wyhash/wyhash.h"
45
+
46
+ static upb_strview Convert_StringData(VALUE str, upb_arena *arena) {
47
+ upb_strview ret;
48
+ if (arena) {
49
+ char *ptr = upb_arena_malloc(arena, RSTRING_LEN(str));
50
+ memcpy(ptr, RSTRING_PTR(str), RSTRING_LEN(str));
51
+ ret.data = ptr;
52
+ } else {
53
+ // Data is only needed temporarily (within map lookup).
54
+ ret.data = RSTRING_PTR(str);
55
+ }
56
+ ret.size = RSTRING_LEN(str);
57
+ return ret;
58
+ }
59
+
60
+ static bool is_ruby_num(VALUE value) {
61
+ return (TYPE(value) == T_FLOAT ||
62
+ TYPE(value) == T_FIXNUM ||
63
+ TYPE(value) == T_BIGNUM);
64
+ }
65
+
66
+ static void Convert_CheckInt(const char* name, upb_fieldtype_t type,
67
+ VALUE val) {
68
+ if (!is_ruby_num(val)) {
69
+ rb_raise(cTypeError,
70
+ "Expected number type for integral field '%s' (given %s).", name,
71
+ rb_class2name(CLASS_OF(val)));
72
+ }
73
+
74
+ // NUM2{INT,UINT,LL,ULL} macros do the appropriate range checks on upper
75
+ // bound; we just need to do precision checks (i.e., disallow rounding) and
76
+ // check for < 0 on unsigned types.
77
+ if (TYPE(val) == T_FLOAT) {
78
+ double dbl_val = NUM2DBL(val);
79
+ if (floor(dbl_val) != dbl_val) {
80
+ rb_raise(rb_eRangeError,
81
+ "Non-integral floating point value assigned to integer field "
82
+ "'%s' (given %s).",
83
+ name, rb_class2name(CLASS_OF(val)));
84
+ }
85
+ }
86
+ if (type == UPB_TYPE_UINT32 || type == UPB_TYPE_UINT64) {
87
+ if (NUM2DBL(val) < 0) {
88
+ rb_raise(
89
+ rb_eRangeError,
90
+ "Assigning negative value to unsigned integer field '%s' (given %s).",
91
+ name, rb_class2name(CLASS_OF(val)));
92
+ }
93
+ }
94
+ }
95
+
96
+ static int32_t Convert_ToEnum(VALUE value, const char* name,
97
+ const upb_enumdef* e) {
98
+ int32_t val;
99
+
100
+ switch (TYPE(value)) {
101
+ case T_FLOAT:
102
+ case T_FIXNUM:
103
+ case T_BIGNUM:
104
+ Convert_CheckInt(name, UPB_TYPE_INT32, value);
105
+ val = NUM2INT(value);
106
+ break;
107
+ case T_STRING:
108
+ if (!upb_enumdef_ntoi(e, RSTRING_PTR(value), RSTRING_LEN(value), &val)) {
109
+ goto unknownval;
110
+ }
111
+ break;
112
+ case T_SYMBOL:
113
+ if (!upb_enumdef_ntoiz(e, rb_id2name(SYM2ID(value)), &val)) {
114
+ goto unknownval;
115
+ }
116
+ break;
117
+ default:
118
+ rb_raise(cTypeError,
119
+ "Expected number or symbol type for enum field '%s'.", name);
120
+ }
121
+
122
+ return val;
123
+
124
+ unknownval:
125
+ rb_raise(rb_eRangeError, "Unknown symbol value for enum field '%s'.", name);
126
+ }
127
+
128
+ upb_msgval Convert_RubyToUpb(VALUE value, const char* name, TypeInfo type_info,
129
+ upb_arena* arena) {
130
+ upb_msgval ret;
131
+
132
+ switch (type_info.type) {
133
+ case UPB_TYPE_FLOAT:
134
+ if (!is_ruby_num(value)) {
135
+ rb_raise(cTypeError, "Expected number type for float field '%s' (given %s).",
136
+ name, rb_class2name(CLASS_OF(value)));
137
+ }
138
+ ret.float_val = NUM2DBL(value);
139
+ break;
140
+ case UPB_TYPE_DOUBLE:
141
+ if (!is_ruby_num(value)) {
142
+ rb_raise(cTypeError, "Expected number type for double field '%s' (given %s).",
143
+ name, rb_class2name(CLASS_OF(value)));
144
+ }
145
+ ret.double_val = NUM2DBL(value);
146
+ break;
147
+ case UPB_TYPE_BOOL: {
148
+ if (value == Qtrue) {
149
+ ret.bool_val = 1;
150
+ } else if (value == Qfalse) {
151
+ ret.bool_val = 0;
152
+ } else {
153
+ rb_raise(cTypeError, "Invalid argument for boolean field '%s' (given %s).",
154
+ name, rb_class2name(CLASS_OF(value)));
155
+ }
156
+ break;
157
+ }
158
+ case UPB_TYPE_STRING: {
159
+ VALUE utf8 = rb_enc_from_encoding(rb_utf8_encoding());
160
+ if (CLASS_OF(value) == rb_cSymbol) {
161
+ value = rb_funcall(value, rb_intern("to_s"), 0);
162
+ } else if (CLASS_OF(value) != rb_cString) {
163
+ rb_raise(cTypeError, "Invalid argument for string field '%s' (given %s).",
164
+ name, rb_class2name(CLASS_OF(value)));
165
+ }
166
+
167
+ if (rb_obj_encoding(value) != utf8) {
168
+ // Note: this will not duplicate underlying string data unless necessary.
169
+ value = rb_str_encode(value, utf8, 0, Qnil);
170
+
171
+ if (rb_enc_str_coderange(value) == ENC_CODERANGE_BROKEN) {
172
+ rb_raise(rb_eEncodingError, "String is invalid UTF-8");
173
+ }
174
+ }
175
+
176
+ ret.str_val = Convert_StringData(value, arena);
177
+ break;
178
+ }
179
+ case UPB_TYPE_BYTES: {
180
+ VALUE bytes = rb_enc_from_encoding(rb_ascii8bit_encoding());
181
+ if (CLASS_OF(value) != rb_cString) {
182
+ rb_raise(cTypeError, "Invalid argument for bytes field '%s' (given %s).",
183
+ name, rb_class2name(CLASS_OF(value)));
184
+ }
185
+
186
+ if (rb_obj_encoding(value) != bytes) {
187
+ // Note: this will not duplicate underlying string data unless necessary.
188
+ // TODO(haberman): is this really necessary to get raw bytes?
189
+ value = rb_str_encode(value, bytes, 0, Qnil);
190
+ }
191
+
192
+ ret.str_val = Convert_StringData(value, arena);
193
+ break;
194
+ }
195
+ case UPB_TYPE_MESSAGE:
196
+ ret.msg_val =
197
+ Message_GetUpbMessage(value, type_info.def.msgdef, name, arena);
198
+ break;
199
+ case UPB_TYPE_ENUM:
200
+ ret.int32_val = Convert_ToEnum(value, name, type_info.def.enumdef);
201
+ break;
202
+ case UPB_TYPE_INT32:
203
+ case UPB_TYPE_INT64:
204
+ case UPB_TYPE_UINT32:
205
+ case UPB_TYPE_UINT64:
206
+ Convert_CheckInt(name, type_info.type, value);
207
+ switch (type_info.type) {
208
+ case UPB_TYPE_INT32:
209
+ ret.int32_val = NUM2INT(value);
210
+ break;
211
+ case UPB_TYPE_INT64:
212
+ ret.int64_val = NUM2LL(value);
213
+ break;
214
+ case UPB_TYPE_UINT32:
215
+ ret.uint32_val = NUM2UINT(value);
216
+ break;
217
+ case UPB_TYPE_UINT64:
218
+ ret.uint64_val = NUM2ULL(value);
219
+ break;
220
+ default:
221
+ break;
222
+ }
223
+ break;
224
+ default:
225
+ break;
226
+ }
227
+
228
+ return ret;
229
+ }
230
+
231
+ VALUE Convert_UpbToRuby(upb_msgval upb_val, TypeInfo type_info, VALUE arena) {
232
+ switch (type_info.type) {
233
+ case UPB_TYPE_FLOAT:
234
+ return DBL2NUM(upb_val.float_val);
235
+ case UPB_TYPE_DOUBLE:
236
+ return DBL2NUM(upb_val.double_val);
237
+ case UPB_TYPE_BOOL:
238
+ return upb_val.bool_val ? Qtrue : Qfalse;
239
+ case UPB_TYPE_INT32:
240
+ return INT2NUM(upb_val.int32_val);
241
+ case UPB_TYPE_INT64:
242
+ return LL2NUM(upb_val.int64_val);
243
+ case UPB_TYPE_UINT32:
244
+ return UINT2NUM(upb_val.uint32_val);
245
+ case UPB_TYPE_UINT64:
246
+ return ULL2NUM(upb_val.int64_val);
247
+ case UPB_TYPE_ENUM: {
248
+ const char* name =
249
+ upb_enumdef_iton(type_info.def.enumdef, upb_val.int32_val);
250
+ if (name) {
251
+ return ID2SYM(rb_intern(name));
252
+ } else {
253
+ return INT2NUM(upb_val.int32_val);
254
+ }
255
+ }
256
+ case UPB_TYPE_STRING: {
257
+ VALUE str_rb = rb_str_new(upb_val.str_val.data, upb_val.str_val.size);
258
+ rb_enc_associate(str_rb, rb_utf8_encoding());
259
+ rb_obj_freeze(str_rb);
260
+ return str_rb;
261
+ }
262
+ case UPB_TYPE_BYTES: {
263
+ VALUE str_rb = rb_str_new(upb_val.str_val.data, upb_val.str_val.size);
264
+ rb_enc_associate(str_rb, rb_ascii8bit_encoding());
265
+ rb_obj_freeze(str_rb);
266
+ return str_rb;
267
+ }
268
+ case UPB_TYPE_MESSAGE:
269
+ return Message_GetRubyWrapper((upb_msg*)upb_val.msg_val,
270
+ type_info.def.msgdef, arena);
271
+ default:
272
+ rb_raise(rb_eRuntimeError, "Convert_UpbToRuby(): Unexpected type %d",
273
+ (int)type_info.type);
274
+ }
275
+ }
276
+
277
+ upb_msgval Msgval_DeepCopy(upb_msgval msgval, TypeInfo type_info,
278
+ upb_arena* arena) {
279
+ upb_msgval new_msgval;
280
+
281
+ switch (type_info.type) {
282
+ default:
283
+ memcpy(&new_msgval, &msgval, sizeof(msgval));
284
+ break;
285
+ case UPB_TYPE_STRING:
286
+ case UPB_TYPE_BYTES: {
287
+ size_t n = msgval.str_val.size;
288
+ char *mem = upb_arena_malloc(arena, n);
289
+ new_msgval.str_val.data = mem;
290
+ new_msgval.str_val.size = n;
291
+ memcpy(mem, msgval.str_val.data, n);
292
+ break;
293
+ }
294
+ case UPB_TYPE_MESSAGE:
295
+ new_msgval.msg_val =
296
+ Message_deep_copy(msgval.msg_val, type_info.def.msgdef, arena);
297
+ break;
298
+ }
299
+
300
+ return new_msgval;
301
+ }
302
+
303
+ bool Msgval_IsEqual(upb_msgval val1, upb_msgval val2, TypeInfo type_info) {
304
+ switch (type_info.type) {
305
+ case UPB_TYPE_BOOL:
306
+ return memcmp(&val1, &val2, 1) == 0;
307
+ case UPB_TYPE_FLOAT:
308
+ case UPB_TYPE_INT32:
309
+ case UPB_TYPE_UINT32:
310
+ case UPB_TYPE_ENUM:
311
+ return memcmp(&val1, &val2, 4) == 0;
312
+ case UPB_TYPE_DOUBLE:
313
+ case UPB_TYPE_INT64:
314
+ case UPB_TYPE_UINT64:
315
+ return memcmp(&val1, &val2, 8) == 0;
316
+ case UPB_TYPE_STRING:
317
+ case UPB_TYPE_BYTES:
318
+ return val1.str_val.size == val2.str_val.size &&
319
+ memcmp(val1.str_val.data, val2.str_val.data,
320
+ val1.str_val.size) == 0;
321
+ case UPB_TYPE_MESSAGE:
322
+ return Message_Equal(val1.msg_val, val2.msg_val, type_info.def.msgdef);
323
+ default:
324
+ rb_raise(rb_eRuntimeError, "Internal error, unexpected type");
325
+ }
326
+ }
327
+
328
+ uint64_t Msgval_GetHash(upb_msgval val, TypeInfo type_info, uint64_t seed) {
329
+ switch (type_info.type) {
330
+ case UPB_TYPE_BOOL:
331
+ return wyhash(&val, 1, seed, _wyp);
332
+ case UPB_TYPE_FLOAT:
333
+ case UPB_TYPE_INT32:
334
+ case UPB_TYPE_UINT32:
335
+ case UPB_TYPE_ENUM:
336
+ return wyhash(&val, 4, seed, _wyp);
337
+ case UPB_TYPE_DOUBLE:
338
+ case UPB_TYPE_INT64:
339
+ case UPB_TYPE_UINT64:
340
+ return wyhash(&val, 8, seed, _wyp);
341
+ case UPB_TYPE_STRING:
342
+ case UPB_TYPE_BYTES:
343
+ return wyhash(val.str_val.data, val.str_val.size, seed, _wyp);
344
+ case UPB_TYPE_MESSAGE:
345
+ return Message_Hash(val.msg_val, type_info.def.msgdef, seed);
346
+ default:
347
+ rb_raise(rb_eRuntimeError, "Internal error, unexpected type");
348
+ }
349
+ }
@@ -0,0 +1,72 @@
1
+ // Protocol Buffers - Google's data interchange format
2
+ // Copyright 2008 Google Inc. All rights reserved.
3
+ // https://developers.google.com/protocol-buffers/
4
+ //
5
+ // Redistribution and use in source and binary forms, with or without
6
+ // modification, are permitted provided that the following conditions are
7
+ // met:
8
+ //
9
+ // * Redistributions of source code must retain the above copyright
10
+ // notice, this list of conditions and the following disclaimer.
11
+ // * Redistributions in binary form must reproduce the above
12
+ // copyright notice, this list of conditions and the following disclaimer
13
+ // in the documentation and/or other materials provided with the
14
+ // distribution.
15
+ // * Neither the name of Google Inc. nor the names of its
16
+ // contributors may be used to endorse or promote products derived from
17
+ // this software without specific prior written permission.
18
+ //
19
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
+ // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
+ // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
+ // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
+ // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
+ // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
+ // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
+ // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
+ // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
+ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+
31
+ #ifndef RUBY_PROTOBUF_CONVERT_H_
32
+ #define RUBY_PROTOBUF_CONVERT_H_
33
+
34
+ #include <ruby/ruby.h>
35
+
36
+ #include "protobuf.h"
37
+ #include "ruby-upb.h"
38
+
39
+ // Converts |ruby_val| to a upb_msgval according to |type_info|.
40
+ //
41
+ // The |arena| parameter indicates the lifetime of the container where this
42
+ // value will be assigned. It is used as follows:
43
+ // - If type is string or bytes, the string data will be copied into |arena|.
44
+ // - If type is message, and we need to auto-construct a message due to implicit
45
+ // conversions (eg. Time -> Google::Protobuf::Timestamp), the new message
46
+ // will be created in |arena|.
47
+ // - If type is message and the Ruby value is a message instance, we will fuse
48
+ // the message's arena into |arena|, to ensure that this message outlives the
49
+ // container.
50
+ upb_msgval Convert_RubyToUpb(VALUE ruby_val, const char *name,
51
+ TypeInfo type_info, upb_arena *arena);
52
+
53
+ // Converts |upb_val| to a Ruby VALUE according to |type_info|. This may involve
54
+ // creating a Ruby wrapper object.
55
+ //
56
+ // The |arena| parameter indicates the arena that owns the lifetime of
57
+ // |upb_val|. Any Ruby wrapper object that is created will reference |arena|
58
+ // and ensure it outlives the wrapper.
59
+ VALUE Convert_UpbToRuby(upb_msgval upb_val, TypeInfo type_info, VALUE arena);
60
+
61
+ // Creates a deep copy of |msgval| in |arena|.
62
+ upb_msgval Msgval_DeepCopy(upb_msgval msgval, TypeInfo type_info,
63
+ upb_arena *arena);
64
+
65
+ // Returns true if |val1| and |val2| are equal. Their type is given by
66
+ // |type_info|.
67
+ bool Msgval_IsEqual(upb_msgval val1, upb_msgval val2, TypeInfo type_info);
68
+
69
+ // Returns a hash value for the given upb_msgval.
70
+ uint64_t Msgval_GetHash(upb_msgval val, TypeInfo type_info, uint64_t seed);
71
+
72
+ #endif // RUBY_PROTOBUF_CONVERT_H_
@@ -30,8 +30,35 @@
30
30
 
31
31
  #include <ctype.h>
32
32
  #include <errno.h>
33
+ #include <ruby/version.h>
34
+
35
+ #include "convert.h"
36
+ #include "message.h"
33
37
  #include "protobuf.h"
34
38
 
39
+ static VALUE Builder_build(VALUE _self);
40
+
41
+ static VALUE cMessageBuilderContext;
42
+ static VALUE cOneofBuilderContext;
43
+ static VALUE cEnumBuilderContext;
44
+ static VALUE cBuilder;
45
+
46
+ // -----------------------------------------------------------------------------
47
+ // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
48
+ // instances.
49
+ // -----------------------------------------------------------------------------
50
+
51
+ static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_msgdef* def);
52
+ static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_enumdef* def);
53
+ static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_fielddef* def);
54
+ static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_filedef* def);
55
+ static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_oneofdef* def);
56
+
57
+ // A distinct object that is not accessible from Ruby. We use this as a
58
+ // constructor argument to enforce that certain objects cannot be created from
59
+ // Ruby.
60
+ VALUE c_only_cookie = Qnil;
61
+
35
62
  // -----------------------------------------------------------------------------
36
63
  // Common utilities.
37
64
  // -----------------------------------------------------------------------------
@@ -48,6 +75,10 @@ static VALUE rb_str_maybe_null(const char* s) {
48
75
  return rb_str_new2(s);
49
76
  }
50
77
 
78
+ // -----------------------------------------------------------------------------
79
+ // Backward compatibility code.
80
+ // -----------------------------------------------------------------------------
81
+
51
82
  static void rewrite_enum_default(const upb_symtab* symtab,
52
83
  google_protobuf_FileDescriptorProto* file,
53
84
  google_protobuf_FieldDescriptorProto* field) {
@@ -205,221 +236,70 @@ static void rewrite_nesting(VALUE msg_ent, google_protobuf_DescriptorProto* msg,
205
236
  }
206
237
  }
207
238
 
208
- /* We have to do some relatively complicated logic here for backward
209
- * compatibility.
210
- *
211
- * In descriptor.proto, messages are nested inside other messages if that is
212
- * what the original .proto file looks like. For example, suppose we have this
213
- * foo.proto:
214
- *
215
- * package foo;
216
- * message Bar {
217
- * message Baz {}
218
- * }
219
- *
220
- * The descriptor for this must look like this:
221
- *
222
- * file {
223
- * name: "test.proto"
224
- * package: "foo"
225
- * message_type {
226
- * name: "Bar"
227
- * nested_type {
228
- * name: "Baz"
229
- * }
230
- * }
231
- * }
232
- *
233
- * However, the Ruby generated code has always generated messages in a flat,
234
- * non-nested way:
235
- *
236
- * Google::Protobuf::DescriptorPool.generated_pool.build do
237
- * add_message "foo.Bar" do
238
- * end
239
- * add_message "foo.Bar.Baz" do
240
- * end
241
- * end
242
- *
243
- * Here we need to do a translation where we turn this generated code into the
244
- * above descriptor. We need to infer that "foo" is the package name, and not
245
- * a message itself.
246
- *
247
- * We delegate to Ruby to compute the transformation, for more concice and
248
- * readable code than we can do in C */
249
- static void rewrite_names(VALUE _file_builder,
250
- google_protobuf_FileDescriptorProto* file_proto) {
251
- FileBuilderContext* file_builder = ruby_to_FileBuilderContext(_file_builder);
252
- upb_arena *arena = file_builder->arena;
253
- // Build params (package, msg_names, enum_names).
254
- VALUE package = Qnil;
255
- VALUE msg_names = rb_ary_new();
256
- VALUE enum_names = rb_ary_new();
257
- size_t msg_count, enum_count, i;
258
- VALUE new_package, nesting, msg_ents, enum_ents;
259
- google_protobuf_DescriptorProto** msgs;
260
- google_protobuf_EnumDescriptorProto** enums;
261
-
262
- if (google_protobuf_FileDescriptorProto_has_package(file_proto)) {
263
- upb_strview package_str =
264
- google_protobuf_FileDescriptorProto_package(file_proto);
265
- package = rb_str_new(package_str.data, package_str.size);
266
- }
267
-
268
- msgs = google_protobuf_FileDescriptorProto_mutable_message_type(file_proto,
269
- &msg_count);
270
- for (i = 0; i < msg_count; i++) {
271
- upb_strview name = google_protobuf_DescriptorProto_name(msgs[i]);
272
- rb_ary_push(msg_names, rb_str_new(name.data, name.size));
273
- }
274
-
275
- enums = google_protobuf_FileDescriptorProto_mutable_enum_type(file_proto,
276
- &enum_count);
277
- for (i = 0; i < enum_count; i++) {
278
- upb_strview name = google_protobuf_EnumDescriptorProto_name(enums[i]);
279
- rb_ary_push(enum_names, rb_str_new(name.data, name.size));
280
- }
281
-
282
- {
283
- // Call Ruby code to calculate package name and nesting.
284
- VALUE args[3] = { package, msg_names, enum_names };
285
- VALUE internal = rb_eval_string("Google::Protobuf::Internal");
286
- VALUE ret = rb_funcallv(internal, rb_intern("fixup_descriptor"), 3, args);
287
-
288
- new_package = rb_ary_entry(ret, 0);
289
- nesting = rb_ary_entry(ret, 1);
290
- }
291
-
292
- // Rewrite package and names.
293
- if (new_package != Qnil) {
294
- upb_strview new_package_str =
295
- FileBuilderContext_strdup(_file_builder, new_package);
296
- google_protobuf_FileDescriptorProto_set_package(file_proto,
297
- new_package_str);
298
- }
299
-
300
- for (i = 0; i < msg_count; i++) {
301
- upb_strview name = google_protobuf_DescriptorProto_name(msgs[i]);
302
- remove_path(&name);
303
- google_protobuf_DescriptorProto_set_name(msgs[i], name);
304
- }
305
-
306
- for (i = 0; i < enum_count; i++) {
307
- upb_strview name = google_protobuf_EnumDescriptorProto_name(enums[i]);
308
- remove_path(&name);
309
- google_protobuf_EnumDescriptorProto_set_name(enums[i], name);
310
- }
311
-
312
- // Rewrite nesting.
313
- msg_ents = rb_hash_aref(nesting, ID2SYM(rb_intern("msgs")));
314
- enum_ents = rb_hash_aref(nesting, ID2SYM(rb_intern("enums")));
315
-
316
- Check_Type(msg_ents, T_ARRAY);
317
- Check_Type(enum_ents, T_ARRAY);
318
-
319
- for (i = 0; i < (size_t)RARRAY_LEN(msg_ents); i++) {
320
- VALUE msg_ent = rb_ary_entry(msg_ents, i);
321
- VALUE pos = rb_hash_aref(msg_ent, ID2SYM(rb_intern("pos")));
322
- msgs[i] = msgs[NUM2INT(pos)];
323
- rewrite_nesting(msg_ent, msgs[i], msgs, enums, arena);
324
- }
325
-
326
- for (i = 0; i < (size_t)RARRAY_LEN(enum_ents); i++) {
327
- VALUE enum_pos = rb_ary_entry(enum_ents, i);
328
- enums[i] = enums[NUM2INT(enum_pos)];
329
- }
330
-
331
- google_protobuf_FileDescriptorProto_resize_message_type(
332
- file_proto, RARRAY_LEN(msg_ents), arena);
333
- google_protobuf_FileDescriptorProto_resize_enum_type(
334
- file_proto, RARRAY_LEN(enum_ents), arena);
335
- }
336
-
337
239
  // -----------------------------------------------------------------------------
338
240
  // DescriptorPool.
339
241
  // -----------------------------------------------------------------------------
340
242
 
341
- #define DEFINE_CLASS(name, string_name) \
342
- VALUE c ## name = Qnil; \
343
- const rb_data_type_t _ ## name ## _type = { \
344
- string_name, \
345
- { name ## _mark, name ## _free, NULL }, \
346
- }; \
347
- name* ruby_to_ ## name(VALUE val) { \
348
- name* ret; \
349
- TypedData_Get_Struct(val, name, &_ ## name ## _type, ret); \
350
- return ret; \
351
- } \
352
-
353
- #define DEFINE_SELF(type, var, rb_var) \
354
- type* var = ruby_to_ ## type(rb_var)
243
+ typedef struct {
244
+ VALUE def_to_descriptor; // Hash table of def* -> Ruby descriptor.
245
+ upb_symtab* symtab;
246
+ } DescriptorPool;
247
+
248
+ VALUE cDescriptorPool = Qnil;
355
249
 
356
250
  // Global singleton DescriptorPool. The user is free to create others, but this
357
251
  // is used by generated code.
358
252
  VALUE generated_pool = Qnil;
359
253
 
360
- DEFINE_CLASS(DescriptorPool, "Google::Protobuf::DescriptorPool");
361
-
362
- void DescriptorPool_mark(void* _self) {
254
+ static void DescriptorPool_mark(void* _self) {
363
255
  DescriptorPool* self = _self;
364
256
  rb_gc_mark(self->def_to_descriptor);
365
257
  }
366
258
 
367
- void DescriptorPool_free(void* _self) {
259
+ static void DescriptorPool_free(void* _self) {
368
260
  DescriptorPool* self = _self;
369
-
370
261
  upb_symtab_free(self->symtab);
371
- upb_handlercache_free(self->fill_handler_cache);
372
- upb_handlercache_free(self->pb_serialize_handler_cache);
373
- upb_handlercache_free(self->json_serialize_handler_cache);
374
- upb_handlercache_free(self->json_serialize_handler_preserve_cache);
375
- upb_pbcodecache_free(self->fill_method_cache);
376
- upb_json_codecache_free(self->json_fill_method_cache);
377
-
378
262
  xfree(self);
379
263
  }
380
264
 
265
+ static const rb_data_type_t DescriptorPool_type = {
266
+ "Google::Protobuf::DescriptorPool",
267
+ {DescriptorPool_mark, DescriptorPool_free, NULL},
268
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
269
+ };
270
+
271
+ static DescriptorPool* ruby_to_DescriptorPool(VALUE val) {
272
+ DescriptorPool* ret;
273
+ TypedData_Get_Struct(val, DescriptorPool, &DescriptorPool_type, ret);
274
+ return ret;
275
+ }
276
+
277
+ // Exposed to other modules in defs.h.
278
+ const upb_symtab *DescriptorPool_GetSymtab(VALUE desc_pool_rb) {
279
+ DescriptorPool *pool = ruby_to_DescriptorPool(desc_pool_rb);
280
+ return pool->symtab;
281
+ }
282
+
381
283
  /*
382
284
  * call-seq:
383
285
  * DescriptorPool.new => pool
384
286
  *
385
287
  * Creates a new, empty, descriptor pool.
386
288
  */
387
- VALUE DescriptorPool_alloc(VALUE klass) {
289
+ static VALUE DescriptorPool_alloc(VALUE klass) {
388
290
  DescriptorPool* self = ALLOC(DescriptorPool);
389
291
  VALUE ret;
390
292
 
391
293
  self->def_to_descriptor = Qnil;
392
- ret = TypedData_Wrap_Struct(klass, &_DescriptorPool_type, self);
294
+ ret = TypedData_Wrap_Struct(klass, &DescriptorPool_type, self);
393
295
 
394
296
  self->def_to_descriptor = rb_hash_new();
395
297
  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();
298
+ ObjectCache_Add(self->symtab, ret);
404
299
 
405
300
  return ret;
406
301
  }
407
302
 
408
- void DescriptorPool_register(VALUE module) {
409
- VALUE klass = rb_define_class_under(
410
- module, "DescriptorPool", rb_cObject);
411
- rb_define_alloc_func(klass, DescriptorPool_alloc);
412
- rb_define_method(klass, "build", DescriptorPool_build, -1);
413
- rb_define_method(klass, "lookup", DescriptorPool_lookup, 1);
414
- rb_define_singleton_method(klass, "generated_pool",
415
- DescriptorPool_generated_pool, 0);
416
- rb_gc_register_address(&cDescriptorPool);
417
- cDescriptorPool = klass;
418
-
419
- rb_gc_register_address(&generated_pool);
420
- generated_pool = rb_class_new_instance(0, NULL, klass);
421
- }
422
-
423
303
  /*
424
304
  * call-seq:
425
305
  * DescriptorPool.build(&block)
@@ -430,7 +310,7 @@ void DescriptorPool_register(VALUE module) {
430
310
  * Builder#add_enum within the block as appropriate. This is the recommended,
431
311
  * idiomatic way to define new message and enum types.
432
312
  */
433
- VALUE DescriptorPool_build(int argc, VALUE* argv, VALUE _self) {
313
+ static VALUE DescriptorPool_build(int argc, VALUE* argv, VALUE _self) {
434
314
  VALUE ctx = rb_class_new_instance(1, &_self, cBuilder);
435
315
  VALUE block = rb_block_proc();
436
316
  rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
@@ -445,8 +325,8 @@ VALUE DescriptorPool_build(int argc, VALUE* argv, VALUE _self) {
445
325
  * Finds a Descriptor or EnumDescriptor by name and returns it, or nil if none
446
326
  * exists with the given name.
447
327
  */
448
- VALUE DescriptorPool_lookup(VALUE _self, VALUE name) {
449
- DEFINE_SELF(DescriptorPool, self, _self);
328
+ static VALUE DescriptorPool_lookup(VALUE _self, VALUE name) {
329
+ DescriptorPool* self = ruby_to_DescriptorPool(_self);
450
330
  const char* name_str = get_str(name);
451
331
  const upb_msgdef* msgdef;
452
332
  const upb_enumdef* enumdef;
@@ -473,31 +353,53 @@ VALUE DescriptorPool_lookup(VALUE _self, VALUE name) {
473
353
  * register types in this pool for convenience so that they do not have to hold
474
354
  * a reference to a private pool instance.
475
355
  */
476
- VALUE DescriptorPool_generated_pool(VALUE _self) {
356
+ static VALUE DescriptorPool_generated_pool(VALUE _self) {
477
357
  return generated_pool;
478
358
  }
479
359
 
360
+ static void DescriptorPool_register(VALUE module) {
361
+ VALUE klass = rb_define_class_under(
362
+ module, "DescriptorPool", rb_cObject);
363
+ rb_define_alloc_func(klass, DescriptorPool_alloc);
364
+ rb_define_method(klass, "build", DescriptorPool_build, -1);
365
+ rb_define_method(klass, "lookup", DescriptorPool_lookup, 1);
366
+ rb_define_singleton_method(klass, "generated_pool",
367
+ DescriptorPool_generated_pool, 0);
368
+ rb_gc_register_address(&cDescriptorPool);
369
+ cDescriptorPool = klass;
370
+
371
+ rb_gc_register_address(&generated_pool);
372
+ generated_pool = rb_class_new_instance(0, NULL, klass);
373
+ }
374
+
480
375
  // -----------------------------------------------------------------------------
481
376
  // Descriptor.
482
377
  // -----------------------------------------------------------------------------
483
378
 
484
- DEFINE_CLASS(Descriptor, "Google::Protobuf::Descriptor");
379
+ typedef struct {
380
+ const upb_msgdef* msgdef;
381
+ VALUE klass;
382
+ VALUE descriptor_pool;
383
+ } Descriptor;
384
+
385
+ VALUE cDescriptor = Qnil;
485
386
 
486
- void Descriptor_mark(void* _self) {
387
+ static void Descriptor_mark(void* _self) {
487
388
  Descriptor* self = _self;
488
389
  rb_gc_mark(self->klass);
489
390
  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
391
  }
494
392
 
495
- void Descriptor_free(void* _self) {
496
- Descriptor* self = _self;
497
- if (self->layout) {
498
- free_layout(self->layout);
499
- }
500
- xfree(self);
393
+ static const rb_data_type_t Descriptor_type = {
394
+ "Google::Protobuf::Descriptor",
395
+ {Descriptor_mark, RUBY_DEFAULT_FREE, NULL},
396
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
397
+ };
398
+
399
+ static Descriptor* ruby_to_Descriptor(VALUE val) {
400
+ Descriptor* ret;
401
+ TypedData_Get_Struct(val, Descriptor, &Descriptor_type, ret);
402
+ return ret;
501
403
  }
502
404
 
503
405
  /*
@@ -509,42 +411,24 @@ void Descriptor_free(void* _self) {
509
411
  * it is added to a pool, after which it becomes immutable (as part of a
510
412
  * finalization process).
511
413
  */
512
- VALUE Descriptor_alloc(VALUE klass) {
414
+ static VALUE Descriptor_alloc(VALUE klass) {
513
415
  Descriptor* self = ALLOC(Descriptor);
514
- VALUE ret = TypedData_Wrap_Struct(klass, &_Descriptor_type, self);
416
+ VALUE ret = TypedData_Wrap_Struct(klass, &Descriptor_type, self);
515
417
  self->msgdef = NULL;
516
418
  self->klass = Qnil;
517
419
  self->descriptor_pool = Qnil;
518
- self->layout = NULL;
519
420
  return ret;
520
421
  }
521
422
 
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
423
  /*
540
424
  * call-seq:
541
425
  * Descriptor.new(c_only_cookie, ptr) => Descriptor
542
426
  *
543
427
  * Creates a descriptor wrapper object. May only be called from C.
544
428
  */
545
- VALUE Descriptor_initialize(VALUE _self, VALUE cookie,
546
- VALUE descriptor_pool, VALUE ptr) {
547
- DEFINE_SELF(Descriptor, self, _self);
429
+ static VALUE Descriptor_initialize(VALUE _self, VALUE cookie,
430
+ VALUE descriptor_pool, VALUE ptr) {
431
+ Descriptor* self = ruby_to_Descriptor(_self);
548
432
 
549
433
  if (cookie != c_only_cookie) {
550
434
  rb_raise(rb_eRuntimeError,
@@ -563,8 +447,8 @@ VALUE Descriptor_initialize(VALUE _self, VALUE cookie,
563
447
  *
564
448
  * Returns the FileDescriptor object this message belongs to.
565
449
  */
566
- VALUE Descriptor_file_descriptor(VALUE _self) {
567
- DEFINE_SELF(Descriptor, self, _self);
450
+ static VALUE Descriptor_file_descriptor(VALUE _self) {
451
+ Descriptor* self = ruby_to_Descriptor(_self);
568
452
  return get_filedef_obj(self->descriptor_pool, upb_msgdef_file(self->msgdef));
569
453
  }
570
454
 
@@ -575,8 +459,8 @@ VALUE Descriptor_file_descriptor(VALUE _self) {
575
459
  * Returns the name of this message type as a fully-qualified string (e.g.,
576
460
  * My.Package.MessageType).
577
461
  */
578
- VALUE Descriptor_name(VALUE _self) {
579
- DEFINE_SELF(Descriptor, self, _self);
462
+ static VALUE Descriptor_name(VALUE _self) {
463
+ Descriptor* self = ruby_to_Descriptor(_self);
580
464
  return rb_str_maybe_null(upb_msgdef_fullname(self->msgdef));
581
465
  }
582
466
 
@@ -586,8 +470,8 @@ VALUE Descriptor_name(VALUE _self) {
586
470
  *
587
471
  * Iterates over fields in this message type, yielding to the block on each one.
588
472
  */
589
- VALUE Descriptor_each(VALUE _self) {
590
- DEFINE_SELF(Descriptor, self, _self);
473
+ static VALUE Descriptor_each(VALUE _self) {
474
+ Descriptor* self = ruby_to_Descriptor(_self);
591
475
 
592
476
  upb_msg_field_iter it;
593
477
  for (upb_msg_field_begin(&it, self->msgdef);
@@ -607,8 +491,8 @@ VALUE Descriptor_each(VALUE _self) {
607
491
  * Returns the field descriptor for the field with the given name, if present,
608
492
  * or nil if none.
609
493
  */
610
- VALUE Descriptor_lookup(VALUE _self, VALUE name) {
611
- DEFINE_SELF(Descriptor, self, _self);
494
+ static VALUE Descriptor_lookup(VALUE _self, VALUE name) {
495
+ Descriptor* self = ruby_to_Descriptor(_self);
612
496
  const char* s = get_str(name);
613
497
  const upb_fielddef* field = upb_msgdef_ntofz(self->msgdef, s);
614
498
  if (field == NULL) {
@@ -624,8 +508,8 @@ VALUE Descriptor_lookup(VALUE _self, VALUE name) {
624
508
  * Invokes the given block for each oneof in this message type, passing the
625
509
  * corresponding OneofDescriptor.
626
510
  */
627
- VALUE Descriptor_each_oneof(VALUE _self) {
628
- DEFINE_SELF(Descriptor, self, _self);
511
+ static VALUE Descriptor_each_oneof(VALUE _self) {
512
+ Descriptor* self = ruby_to_Descriptor(_self);
629
513
 
630
514
  upb_msg_oneof_iter it;
631
515
  for (upb_msg_oneof_begin(&it, self->msgdef);
@@ -645,8 +529,8 @@ VALUE Descriptor_each_oneof(VALUE _self) {
645
529
  * Returns the oneof descriptor for the oneof with the given name, if present,
646
530
  * or nil if none.
647
531
  */
648
- VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
649
- DEFINE_SELF(Descriptor, self, _self);
532
+ static VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
533
+ Descriptor* self = ruby_to_Descriptor(_self);
650
534
  const char* s = get_str(name);
651
535
  const upb_oneofdef* oneof = upb_msgdef_ntooz(self->msgdef, s);
652
536
  if (oneof == NULL) {
@@ -661,32 +545,62 @@ VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
661
545
  *
662
546
  * Returns the Ruby class created for this message type.
663
547
  */
664
- VALUE Descriptor_msgclass(VALUE _self) {
665
- DEFINE_SELF(Descriptor, self, _self);
548
+ static VALUE Descriptor_msgclass(VALUE _self) {
549
+ Descriptor* self = ruby_to_Descriptor(_self);
666
550
  if (self->klass == Qnil) {
667
551
  self->klass = build_class_from_descriptor(_self);
668
552
  }
669
553
  return self->klass;
670
554
  }
671
555
 
556
+ static void Descriptor_register(VALUE module) {
557
+ VALUE klass = rb_define_class_under(
558
+ module, "Descriptor", rb_cObject);
559
+ rb_define_alloc_func(klass, Descriptor_alloc);
560
+ rb_define_method(klass, "initialize", Descriptor_initialize, 3);
561
+ rb_define_method(klass, "each", Descriptor_each, 0);
562
+ rb_define_method(klass, "lookup", Descriptor_lookup, 1);
563
+ rb_define_method(klass, "each_oneof", Descriptor_each_oneof, 0);
564
+ rb_define_method(klass, "lookup_oneof", Descriptor_lookup_oneof, 1);
565
+ rb_define_method(klass, "msgclass", Descriptor_msgclass, 0);
566
+ rb_define_method(klass, "name", Descriptor_name, 0);
567
+ rb_define_method(klass, "file_descriptor", Descriptor_file_descriptor, 0);
568
+ rb_include_module(klass, rb_mEnumerable);
569
+ rb_gc_register_address(&cDescriptor);
570
+ cDescriptor = klass;
571
+ }
572
+
672
573
  // -----------------------------------------------------------------------------
673
574
  // FileDescriptor.
674
575
  // -----------------------------------------------------------------------------
675
576
 
676
- DEFINE_CLASS(FileDescriptor, "Google::Protobuf::FileDescriptor");
577
+ typedef struct {
578
+ const upb_filedef* filedef;
579
+ VALUE descriptor_pool; // Owns the upb_filedef.
580
+ } FileDescriptor;
677
581
 
678
- void FileDescriptor_mark(void* _self) {
582
+ static VALUE cFileDescriptor = Qnil;
583
+
584
+ static void FileDescriptor_mark(void* _self) {
679
585
  FileDescriptor* self = _self;
680
586
  rb_gc_mark(self->descriptor_pool);
681
587
  }
682
588
 
683
- void FileDescriptor_free(void* _self) {
684
- xfree(_self);
589
+ static const rb_data_type_t FileDescriptor_type = {
590
+ "Google::Protobuf::FileDescriptor",
591
+ {FileDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
592
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
593
+ };
594
+
595
+ static FileDescriptor* ruby_to_FileDescriptor(VALUE val) {
596
+ FileDescriptor* ret;
597
+ TypedData_Get_Struct(val, FileDescriptor, &FileDescriptor_type, ret);
598
+ return ret;
685
599
  }
686
600
 
687
- VALUE FileDescriptor_alloc(VALUE klass) {
601
+ static VALUE FileDescriptor_alloc(VALUE klass) {
688
602
  FileDescriptor* self = ALLOC(FileDescriptor);
689
- VALUE ret = TypedData_Wrap_Struct(klass, &_FileDescriptor_type, self);
603
+ VALUE ret = TypedData_Wrap_Struct(klass, &FileDescriptor_type, self);
690
604
  self->descriptor_pool = Qnil;
691
605
  self->filedef = NULL;
692
606
  return ret;
@@ -699,9 +613,9 @@ VALUE FileDescriptor_alloc(VALUE klass) {
699
613
  * Returns a new file descriptor. The syntax must be set before it's passed
700
614
  * to a builder.
701
615
  */
702
- VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
616
+ static VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
703
617
  VALUE descriptor_pool, VALUE ptr) {
704
- DEFINE_SELF(FileDescriptor, self, _self);
618
+ FileDescriptor* self = ruby_to_FileDescriptor(_self);
705
619
 
706
620
  if (cookie != c_only_cookie) {
707
621
  rb_raise(rb_eRuntimeError,
@@ -714,25 +628,14 @@ VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
714
628
  return Qnil;
715
629
  }
716
630
 
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
631
  /*
729
632
  * call-seq:
730
633
  * FileDescriptor.name => name
731
634
  *
732
635
  * Returns the name of the file.
733
636
  */
734
- VALUE FileDescriptor_name(VALUE _self) {
735
- DEFINE_SELF(FileDescriptor, self, _self);
637
+ static VALUE FileDescriptor_name(VALUE _self) {
638
+ FileDescriptor* self = ruby_to_FileDescriptor(_self);
736
639
  const char* name = upb_filedef_name(self->filedef);
737
640
  return name == NULL ? Qnil : rb_str_new2(name);
738
641
  }
@@ -746,8 +649,8 @@ VALUE FileDescriptor_name(VALUE _self) {
746
649
  * Valid syntax versions are:
747
650
  * :proto2 or :proto3.
748
651
  */
749
- VALUE FileDescriptor_syntax(VALUE _self) {
750
- DEFINE_SELF(FileDescriptor, self, _self);
652
+ static VALUE FileDescriptor_syntax(VALUE _self) {
653
+ FileDescriptor* self = ruby_to_FileDescriptor(_self);
751
654
 
752
655
  switch (upb_filedef_syntax(self->filedef)) {
753
656
  case UPB_SYNTAX_PROTO3: return ID2SYM(rb_intern("proto3"));
@@ -756,19 +659,43 @@ VALUE FileDescriptor_syntax(VALUE _self) {
756
659
  }
757
660
  }
758
661
 
662
+ static void FileDescriptor_register(VALUE module) {
663
+ VALUE klass = rb_define_class_under(
664
+ module, "FileDescriptor", rb_cObject);
665
+ rb_define_alloc_func(klass, FileDescriptor_alloc);
666
+ rb_define_method(klass, "initialize", FileDescriptor_initialize, 3);
667
+ rb_define_method(klass, "name", FileDescriptor_name, 0);
668
+ rb_define_method(klass, "syntax", FileDescriptor_syntax, 0);
669
+ rb_gc_register_address(&cFileDescriptor);
670
+ cFileDescriptor = klass;
671
+ }
672
+
759
673
  // -----------------------------------------------------------------------------
760
674
  // FieldDescriptor.
761
675
  // -----------------------------------------------------------------------------
762
676
 
763
- DEFINE_CLASS(FieldDescriptor, "Google::Protobuf::FieldDescriptor");
677
+ typedef struct {
678
+ const upb_fielddef* fielddef;
679
+ VALUE descriptor_pool; // Owns the upb_fielddef.
680
+ } FieldDescriptor;
681
+
682
+ static VALUE cFieldDescriptor = Qnil;
764
683
 
765
- void FieldDescriptor_mark(void* _self) {
684
+ static void FieldDescriptor_mark(void* _self) {
766
685
  FieldDescriptor* self = _self;
767
686
  rb_gc_mark(self->descriptor_pool);
768
687
  }
769
688
 
770
- void FieldDescriptor_free(void* _self) {
771
- xfree(_self);
689
+ static const rb_data_type_t FieldDescriptor_type = {
690
+ "Google::Protobuf::FieldDescriptor",
691
+ {FieldDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
692
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
693
+ };
694
+
695
+ static FieldDescriptor* ruby_to_FieldDescriptor(VALUE val) {
696
+ FieldDescriptor* ret;
697
+ TypedData_Get_Struct(val, FieldDescriptor, &FieldDescriptor_type, ret);
698
+ return ret;
772
699
  }
773
700
 
774
701
  /*
@@ -778,42 +705,22 @@ void FieldDescriptor_free(void* _self) {
778
705
  * Returns a new field descriptor. Its name, type, etc. must be set before it is
779
706
  * added to a message type.
780
707
  */
781
- VALUE FieldDescriptor_alloc(VALUE klass) {
708
+ static VALUE FieldDescriptor_alloc(VALUE klass) {
782
709
  FieldDescriptor* self = ALLOC(FieldDescriptor);
783
- VALUE ret = TypedData_Wrap_Struct(klass, &_FieldDescriptor_type, self);
710
+ VALUE ret = TypedData_Wrap_Struct(klass, &FieldDescriptor_type, self);
784
711
  self->fielddef = NULL;
785
712
  return ret;
786
713
  }
787
714
 
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
715
  /*
809
716
  * call-seq:
810
717
  * EnumDescriptor.new(c_only_cookie, pool, ptr) => EnumDescriptor
811
718
  *
812
719
  * Creates a descriptor wrapper object. May only be called from C.
813
720
  */
814
- VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
815
- VALUE descriptor_pool, VALUE ptr) {
816
- DEFINE_SELF(FieldDescriptor, self, _self);
721
+ static VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
722
+ VALUE descriptor_pool, VALUE ptr) {
723
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
817
724
 
818
725
  if (cookie != c_only_cookie) {
819
726
  rb_raise(rb_eRuntimeError,
@@ -832,11 +739,12 @@ VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
832
739
  *
833
740
  * Returns the name of this field.
834
741
  */
835
- VALUE FieldDescriptor_name(VALUE _self) {
836
- DEFINE_SELF(FieldDescriptor, self, _self);
742
+ static VALUE FieldDescriptor_name(VALUE _self) {
743
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
837
744
  return rb_str_maybe_null(upb_fielddef_name(self->fielddef));
838
745
  }
839
746
 
747
+ // Non-static, exposed to other .c files.
840
748
  upb_fieldtype_t ruby_to_fieldtype(VALUE type) {
841
749
  if (TYPE(type) != T_SYMBOL) {
842
750
  rb_raise(rb_eArgError, "Expected symbol for field type.");
@@ -865,27 +773,7 @@ upb_fieldtype_t ruby_to_fieldtype(VALUE type) {
865
773
  return 0;
866
774
  }
867
775
 
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;
886
- }
887
-
888
- upb_descriptortype_t ruby_to_descriptortype(VALUE type) {
776
+ static upb_descriptortype_t ruby_to_descriptortype(VALUE type) {
889
777
  if (TYPE(type) != T_SYMBOL) {
890
778
  rb_raise(rb_eArgError, "Expected symbol for field type.");
891
779
  }
@@ -920,7 +808,7 @@ upb_descriptortype_t ruby_to_descriptortype(VALUE type) {
920
808
  return 0;
921
809
  }
922
810
 
923
- VALUE descriptortype_to_ruby(upb_descriptortype_t type) {
811
+ static VALUE descriptortype_to_ruby(upb_descriptortype_t type) {
924
812
  switch (type) {
925
813
  #define CONVERT(upb, ruby) \
926
814
  case UPB_DESCRIPTOR_TYPE_ ## upb : return ID2SYM(rb_intern( # ruby ));
@@ -947,29 +835,6 @@ VALUE descriptortype_to_ruby(upb_descriptortype_t type) {
947
835
  return Qnil;
948
836
  }
949
837
 
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
838
  /*
974
839
  * call-seq:
975
840
  * FieldDescriptor.type => type
@@ -980,8 +845,8 @@ VALUE ruby_to_label(VALUE label) {
980
845
  * :int32, :int64, :uint32, :uint64, :float, :double, :bool, :string,
981
846
  * :bytes, :message.
982
847
  */
983
- VALUE FieldDescriptor_type(VALUE _self) {
984
- DEFINE_SELF(FieldDescriptor, self, _self);
848
+ static VALUE FieldDescriptor__type(VALUE _self) {
849
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
985
850
  return descriptortype_to_ruby(upb_fielddef_descriptortype(self->fielddef));
986
851
  }
987
852
 
@@ -991,9 +856,30 @@ VALUE FieldDescriptor_type(VALUE _self) {
991
856
  *
992
857
  * Returns this field's default, as a Ruby object, or nil if not yet set.
993
858
  */
994
- VALUE FieldDescriptor_default(VALUE _self) {
995
- DEFINE_SELF(FieldDescriptor, self, _self);
996
- return layout_get_default(self->fielddef);
859
+ static VALUE FieldDescriptor_default(VALUE _self) {
860
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
861
+ const upb_fielddef *f = self->fielddef;
862
+ upb_msgval default_val = {0};
863
+ if (upb_fielddef_issubmsg(f)) {
864
+ return Qnil;
865
+ } else if (!upb_fielddef_isseq(f)) {
866
+ default_val = upb_fielddef_default(f);
867
+ }
868
+ return Convert_UpbToRuby(default_val, TypeInfo_get(self->fielddef), Qnil);
869
+ }
870
+
871
+
872
+ /*
873
+ * call-seq:
874
+ * FieldDescriptor.json_name => json_name
875
+ *
876
+ * Returns this field's json_name, as a Ruby string, or nil if not yet set.
877
+ */
878
+ static VALUE FieldDescriptor_json_name(VALUE _self) {
879
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
880
+ const upb_fielddef *f = self->fielddef;
881
+ const char *json_name = upb_fielddef_jsonname(f);
882
+ return rb_str_new2(json_name);
997
883
  }
998
884
 
999
885
  /*
@@ -1005,8 +891,8 @@ VALUE FieldDescriptor_default(VALUE _self) {
1005
891
  * Valid field labels are:
1006
892
  * :optional, :repeated
1007
893
  */
1008
- VALUE FieldDescriptor_label(VALUE _self) {
1009
- DEFINE_SELF(FieldDescriptor, self, _self);
894
+ static VALUE FieldDescriptor_label(VALUE _self) {
895
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
1010
896
  switch (upb_fielddef_label(self->fielddef)) {
1011
897
  #define CONVERT(upb, ruby) \
1012
898
  case UPB_LABEL_ ## upb : return ID2SYM(rb_intern( # ruby ));
@@ -1027,8 +913,8 @@ VALUE FieldDescriptor_label(VALUE _self) {
1027
913
  *
1028
914
  * Returns the tag number for this field.
1029
915
  */
1030
- VALUE FieldDescriptor_number(VALUE _self) {
1031
- DEFINE_SELF(FieldDescriptor, self, _self);
916
+ static VALUE FieldDescriptor_number(VALUE _self) {
917
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
1032
918
  return INT2NUM(upb_fielddef_number(self->fielddef));
1033
919
  }
1034
920
 
@@ -1041,8 +927,8 @@ VALUE FieldDescriptor_number(VALUE _self) {
1041
927
  * name will be resolved within the context of the pool to which the containing
1042
928
  * message type is added.
1043
929
  */
1044
- VALUE FieldDescriptor_submsg_name(VALUE _self) {
1045
- DEFINE_SELF(FieldDescriptor, self, _self);
930
+ static VALUE FieldDescriptor_submsg_name(VALUE _self) {
931
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
1046
932
  switch (upb_fielddef_type(self->fielddef)) {
1047
933
  case UPB_TYPE_ENUM:
1048
934
  return rb_str_new2(
@@ -1064,8 +950,8 @@ VALUE FieldDescriptor_submsg_name(VALUE _self) {
1064
950
  * called *until* the containing message type is added to a pool (and thus
1065
951
  * resolved).
1066
952
  */
1067
- VALUE FieldDescriptor_subtype(VALUE _self) {
1068
- DEFINE_SELF(FieldDescriptor, self, _self);
953
+ static VALUE FieldDescriptor_subtype(VALUE _self) {
954
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
1069
955
  switch (upb_fielddef_type(self->fielddef)) {
1070
956
  case UPB_TYPE_ENUM:
1071
957
  return get_enumdef_obj(self->descriptor_pool,
@@ -1085,14 +971,17 @@ VALUE FieldDescriptor_subtype(VALUE _self) {
1085
971
  * Returns the value set for this field on the given message. Raises an
1086
972
  * exception if message is of the wrong type.
1087
973
  */
1088
- VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
1089
- DEFINE_SELF(FieldDescriptor, self, _self);
1090
- MessageHeader* msg;
1091
- TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
1092
- if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
974
+ static VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
975
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
976
+ const upb_msgdef *m;
977
+
978
+ Message_Get(msg_rb, &m);
979
+
980
+ if (m != upb_fielddef_containingtype(self->fielddef)) {
1093
981
  rb_raise(cTypeError, "get method called on wrong message type");
1094
982
  }
1095
- return layout_get(msg->descriptor->layout, Message_data(msg), self->fielddef);
983
+
984
+ return Message_getfield(msg_rb, self->fielddef);
1096
985
  }
1097
986
 
1098
987
  /*
@@ -1102,17 +991,18 @@ VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
1102
991
  * Returns whether the value is set on the given message. Raises an
1103
992
  * exception when calling for fields that do not have presence.
1104
993
  */
1105
- VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
1106
- DEFINE_SELF(FieldDescriptor, self, _self);
1107
- MessageHeader* msg;
1108
- TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
1109
- if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
994
+ static VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
995
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
996
+ const upb_msgdef *m;
997
+ const upb_msgdef *msg = Message_Get(msg_rb, &m);
998
+
999
+ if (m != upb_fielddef_containingtype(self->fielddef)) {
1110
1000
  rb_raise(cTypeError, "has method called on wrong message type");
1111
1001
  } else if (!upb_fielddef_haspresence(self->fielddef)) {
1112
1002
  rb_raise(rb_eArgError, "does not track presence");
1113
1003
  }
1114
1004
 
1115
- return layout_has(msg->descriptor->layout, Message_data(msg), self->fielddef);
1005
+ return upb_msg_has(msg, self->fielddef) ? Qtrue : Qfalse;
1116
1006
  }
1117
1007
 
1118
1008
  /*
@@ -1121,15 +1011,16 @@ VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
1121
1011
  *
1122
1012
  * Clears the field from the message if it's set.
1123
1013
  */
1124
- VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {
1125
- DEFINE_SELF(FieldDescriptor, self, _self);
1126
- MessageHeader* msg;
1127
- TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
1128
- if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
1014
+ static VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {
1015
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
1016
+ const upb_msgdef *m;
1017
+ upb_msgdef *msg = Message_GetMutable(msg_rb, &m);
1018
+
1019
+ if (m != upb_fielddef_containingtype(self->fielddef)) {
1129
1020
  rb_raise(cTypeError, "has method called on wrong message type");
1130
1021
  }
1131
1022
 
1132
- layout_clear(msg->descriptor->layout, Message_data(msg), self->fielddef);
1023
+ upb_msg_clearfield(msg, self->fielddef);
1133
1024
  return Qnil;
1134
1025
  }
1135
1026
 
@@ -1141,30 +1032,70 @@ VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {
1141
1032
  * message. Raises an exception if message is of the wrong type. Performs the
1142
1033
  * ordinary type-checks for field setting.
1143
1034
  */
1144
- VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value) {
1145
- DEFINE_SELF(FieldDescriptor, self, _self);
1146
- MessageHeader* msg;
1147
- TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
1148
- if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
1035
+ static VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value) {
1036
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
1037
+ const upb_msgdef *m;
1038
+ upb_msgdef *msg = Message_GetMutable(msg_rb, &m);
1039
+ upb_arena *arena = Arena_get(Message_GetArena(msg_rb));
1040
+ upb_msgval msgval;
1041
+
1042
+ if (m != upb_fielddef_containingtype(self->fielddef)) {
1149
1043
  rb_raise(cTypeError, "set method called on wrong message type");
1150
1044
  }
1151
- layout_set(msg->descriptor->layout, Message_data(msg), self->fielddef, value);
1045
+
1046
+ msgval = Convert_RubyToUpb(value, upb_fielddef_name(self->fielddef),
1047
+ TypeInfo_get(self->fielddef), arena);
1048
+ upb_msg_set(msg, self->fielddef, msgval, arena);
1152
1049
  return Qnil;
1153
1050
  }
1154
1051
 
1052
+ static void FieldDescriptor_register(VALUE module) {
1053
+ VALUE klass = rb_define_class_under(
1054
+ module, "FieldDescriptor", rb_cObject);
1055
+ rb_define_alloc_func(klass, FieldDescriptor_alloc);
1056
+ rb_define_method(klass, "initialize", FieldDescriptor_initialize, 3);
1057
+ rb_define_method(klass, "name", FieldDescriptor_name, 0);
1058
+ rb_define_method(klass, "type", FieldDescriptor__type, 0);
1059
+ rb_define_method(klass, "default", FieldDescriptor_default, 0);
1060
+ rb_define_method(klass, "json_name", FieldDescriptor_json_name, 0);
1061
+ rb_define_method(klass, "label", FieldDescriptor_label, 0);
1062
+ rb_define_method(klass, "number", FieldDescriptor_number, 0);
1063
+ rb_define_method(klass, "submsg_name", FieldDescriptor_submsg_name, 0);
1064
+ rb_define_method(klass, "subtype", FieldDescriptor_subtype, 0);
1065
+ rb_define_method(klass, "has?", FieldDescriptor_has, 1);
1066
+ rb_define_method(klass, "clear", FieldDescriptor_clear, 1);
1067
+ rb_define_method(klass, "get", FieldDescriptor_get, 1);
1068
+ rb_define_method(klass, "set", FieldDescriptor_set, 2);
1069
+ rb_gc_register_address(&cFieldDescriptor);
1070
+ cFieldDescriptor = klass;
1071
+ }
1072
+
1155
1073
  // -----------------------------------------------------------------------------
1156
1074
  // OneofDescriptor.
1157
1075
  // -----------------------------------------------------------------------------
1158
1076
 
1159
- DEFINE_CLASS(OneofDescriptor, "Google::Protobuf::OneofDescriptor");
1077
+ typedef struct {
1078
+ const upb_oneofdef* oneofdef;
1079
+ VALUE descriptor_pool; // Owns the upb_oneofdef.
1080
+ } OneofDescriptor;
1081
+
1082
+ static VALUE cOneofDescriptor = Qnil;
1160
1083
 
1161
- void OneofDescriptor_mark(void* _self) {
1084
+ static void OneofDescriptor_mark(void* _self) {
1162
1085
  OneofDescriptor* self = _self;
1163
1086
  rb_gc_mark(self->descriptor_pool);
1164
1087
  }
1165
1088
 
1166
- void OneofDescriptor_free(void* _self) {
1167
- xfree(_self);
1089
+ static const rb_data_type_t OneofDescriptor_type = {
1090
+ "Google::Protobuf::OneofDescriptor",
1091
+ {OneofDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
1092
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
1093
+ };
1094
+
1095
+ static OneofDescriptor* ruby_to_OneofDescriptor(VALUE val) {
1096
+ OneofDescriptor* ret;
1097
+ TypedData_Get_Struct(val, OneofDescriptor, &OneofDescriptor_type, ret);
1098
+ return ret;
1168
1099
  }
1169
1100
 
1170
1101
  /*
@@ -1174,35 +1105,23 @@ void OneofDescriptor_free(void* _self) {
1174
1105
  * Creates a new, empty, oneof descriptor. The oneof may only be modified prior
1175
1106
  * to being added to a message descriptor which is subsequently added to a pool.
1176
1107
  */
1177
- VALUE OneofDescriptor_alloc(VALUE klass) {
1108
+ static VALUE OneofDescriptor_alloc(VALUE klass) {
1178
1109
  OneofDescriptor* self = ALLOC(OneofDescriptor);
1179
- VALUE ret = TypedData_Wrap_Struct(klass, &_OneofDescriptor_type, self);
1110
+ VALUE ret = TypedData_Wrap_Struct(klass, &OneofDescriptor_type, self);
1180
1111
  self->oneofdef = NULL;
1181
1112
  self->descriptor_pool = Qnil;
1182
1113
  return ret;
1183
1114
  }
1184
1115
 
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
1116
  /*
1198
1117
  * call-seq:
1199
1118
  * OneofDescriptor.new(c_only_cookie, pool, ptr) => OneofDescriptor
1200
1119
  *
1201
1120
  * Creates a descriptor wrapper object. May only be called from C.
1202
1121
  */
1203
- VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
1122
+ static VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
1204
1123
  VALUE descriptor_pool, VALUE ptr) {
1205
- DEFINE_SELF(OneofDescriptor, self, _self);
1124
+ OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
1206
1125
 
1207
1126
  if (cookie != c_only_cookie) {
1208
1127
  rb_raise(rb_eRuntimeError,
@@ -1221,8 +1140,8 @@ VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
1221
1140
  *
1222
1141
  * Returns the name of this oneof.
1223
1142
  */
1224
- VALUE OneofDescriptor_name(VALUE _self) {
1225
- DEFINE_SELF(OneofDescriptor, self, _self);
1143
+ static VALUE OneofDescriptor_name(VALUE _self) {
1144
+ OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
1226
1145
  return rb_str_maybe_null(upb_oneofdef_name(self->oneofdef));
1227
1146
  }
1228
1147
 
@@ -1232,8 +1151,8 @@ VALUE OneofDescriptor_name(VALUE _self) {
1232
1151
  *
1233
1152
  * Iterates through fields in this oneof, yielding to the block on each one.
1234
1153
  */
1235
- VALUE OneofDescriptor_each(VALUE _self) {
1236
- DEFINE_SELF(OneofDescriptor, self, _self);
1154
+ static VALUE OneofDescriptor_each(VALUE _self) {
1155
+ OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
1237
1156
  upb_oneof_iter it;
1238
1157
  for (upb_oneof_begin(&it, self->oneofdef);
1239
1158
  !upb_oneof_done(&it);
@@ -1245,40 +1164,72 @@ VALUE OneofDescriptor_each(VALUE _self) {
1245
1164
  return Qnil;
1246
1165
  }
1247
1166
 
1167
+ static void OneofDescriptor_register(VALUE module) {
1168
+ VALUE klass = rb_define_class_under(
1169
+ module, "OneofDescriptor", rb_cObject);
1170
+ rb_define_alloc_func(klass, OneofDescriptor_alloc);
1171
+ rb_define_method(klass, "initialize", OneofDescriptor_initialize, 3);
1172
+ rb_define_method(klass, "name", OneofDescriptor_name, 0);
1173
+ rb_define_method(klass, "each", OneofDescriptor_each, 0);
1174
+ rb_include_module(klass, rb_mEnumerable);
1175
+ rb_gc_register_address(&cOneofDescriptor);
1176
+ cOneofDescriptor = klass;
1177
+ }
1178
+
1248
1179
  // -----------------------------------------------------------------------------
1249
1180
  // EnumDescriptor.
1250
1181
  // -----------------------------------------------------------------------------
1251
1182
 
1252
- DEFINE_CLASS(EnumDescriptor, "Google::Protobuf::EnumDescriptor");
1183
+ typedef struct {
1184
+ const upb_enumdef* enumdef;
1185
+ VALUE module; // begins as nil
1186
+ VALUE descriptor_pool; // Owns the upb_enumdef.
1187
+ } EnumDescriptor;
1188
+
1189
+ static VALUE cEnumDescriptor = Qnil;
1253
1190
 
1254
- void EnumDescriptor_mark(void* _self) {
1191
+ static void EnumDescriptor_mark(void* _self) {
1255
1192
  EnumDescriptor* self = _self;
1256
1193
  rb_gc_mark(self->module);
1257
1194
  rb_gc_mark(self->descriptor_pool);
1258
1195
  }
1259
1196
 
1260
- void EnumDescriptor_free(void* _self) {
1261
- xfree(_self);
1197
+ static const rb_data_type_t EnumDescriptor_type = {
1198
+ "Google::Protobuf::EnumDescriptor",
1199
+ {EnumDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
1200
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
1201
+ };
1202
+
1203
+ static EnumDescriptor* ruby_to_EnumDescriptor(VALUE val) {
1204
+ EnumDescriptor* ret;
1205
+ TypedData_Get_Struct(val, EnumDescriptor, &EnumDescriptor_type, ret);
1206
+ return ret;
1262
1207
  }
1263
1208
 
1264
- VALUE EnumDescriptor_alloc(VALUE klass) {
1209
+ static VALUE EnumDescriptor_alloc(VALUE klass) {
1265
1210
  EnumDescriptor* self = ALLOC(EnumDescriptor);
1266
- VALUE ret = TypedData_Wrap_Struct(klass, &_EnumDescriptor_type, self);
1211
+ VALUE ret = TypedData_Wrap_Struct(klass, &EnumDescriptor_type, self);
1267
1212
  self->enumdef = NULL;
1268
1213
  self->module = Qnil;
1269
1214
  self->descriptor_pool = Qnil;
1270
1215
  return ret;
1271
1216
  }
1272
1217
 
1218
+ // Exposed to other modules in defs.h.
1219
+ const upb_enumdef *EnumDescriptor_GetEnumDef(VALUE enum_desc_rb) {
1220
+ EnumDescriptor *desc = ruby_to_EnumDescriptor(enum_desc_rb);
1221
+ return desc->enumdef;
1222
+ }
1223
+
1273
1224
  /*
1274
1225
  * call-seq:
1275
1226
  * EnumDescriptor.new(c_only_cookie, ptr) => EnumDescriptor
1276
1227
  *
1277
1228
  * Creates a descriptor wrapper object. May only be called from C.
1278
1229
  */
1279
- VALUE EnumDescriptor_initialize(VALUE _self, VALUE cookie,
1280
- VALUE descriptor_pool, VALUE ptr) {
1281
- DEFINE_SELF(EnumDescriptor, self, _self);
1230
+ static VALUE EnumDescriptor_initialize(VALUE _self, VALUE cookie,
1231
+ VALUE descriptor_pool, VALUE ptr) {
1232
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1282
1233
 
1283
1234
  if (cookie != c_only_cookie) {
1284
1235
  rb_raise(rb_eRuntimeError,
@@ -1291,30 +1242,14 @@ VALUE EnumDescriptor_initialize(VALUE _self, VALUE cookie,
1291
1242
  return Qnil;
1292
1243
  }
1293
1244
 
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
1245
  /*
1311
1246
  * call-seq:
1312
1247
  * EnumDescriptor.file_descriptor
1313
1248
  *
1314
1249
  * Returns the FileDescriptor object this enum belongs to.
1315
1250
  */
1316
- VALUE EnumDescriptor_file_descriptor(VALUE _self) {
1317
- DEFINE_SELF(EnumDescriptor, self, _self);
1251
+ static VALUE EnumDescriptor_file_descriptor(VALUE _self) {
1252
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1318
1253
  return get_filedef_obj(self->descriptor_pool,
1319
1254
  upb_enumdef_file(self->enumdef));
1320
1255
  }
@@ -1325,8 +1260,8 @@ VALUE EnumDescriptor_file_descriptor(VALUE _self) {
1325
1260
  *
1326
1261
  * Returns the name of this enum type.
1327
1262
  */
1328
- VALUE EnumDescriptor_name(VALUE _self) {
1329
- DEFINE_SELF(EnumDescriptor, self, _self);
1263
+ static VALUE EnumDescriptor_name(VALUE _self) {
1264
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1330
1265
  return rb_str_maybe_null(upb_enumdef_fullname(self->enumdef));
1331
1266
  }
1332
1267
 
@@ -1337,8 +1272,8 @@ VALUE EnumDescriptor_name(VALUE _self) {
1337
1272
  * Returns the numeric value corresponding to the given key name (as a Ruby
1338
1273
  * symbol), or nil if none.
1339
1274
  */
1340
- VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
1341
- DEFINE_SELF(EnumDescriptor, self, _self);
1275
+ static VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
1276
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1342
1277
  const char* name_str= rb_id2name(SYM2ID(name));
1343
1278
  int32_t val = 0;
1344
1279
  if (upb_enumdef_ntoiz(self->enumdef, name_str, &val)) {
@@ -1355,8 +1290,8 @@ VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
1355
1290
  * Returns the key name (as a Ruby symbol) corresponding to the integer value,
1356
1291
  * or nil if none.
1357
1292
  */
1358
- VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
1359
- DEFINE_SELF(EnumDescriptor, self, _self);
1293
+ static VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
1294
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1360
1295
  int32_t val = NUM2INT(number);
1361
1296
  const char* name = upb_enumdef_iton(self->enumdef, val);
1362
1297
  if (name != NULL) {
@@ -1373,8 +1308,8 @@ VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
1373
1308
  * Iterates over key => value mappings in this enum's definition, yielding to
1374
1309
  * the block with (key, value) arguments for each one.
1375
1310
  */
1376
- VALUE EnumDescriptor_each(VALUE _self) {
1377
- DEFINE_SELF(EnumDescriptor, self, _self);
1311
+ static VALUE EnumDescriptor_each(VALUE _self) {
1312
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1378
1313
 
1379
1314
  upb_enum_iter it;
1380
1315
  for (upb_enum_begin(&it, self->enumdef);
@@ -1394,130 +1329,451 @@ VALUE EnumDescriptor_each(VALUE _self) {
1394
1329
  *
1395
1330
  * Returns the Ruby module corresponding to this enum type.
1396
1331
  */
1397
- VALUE EnumDescriptor_enummodule(VALUE _self) {
1398
- DEFINE_SELF(EnumDescriptor, self, _self);
1332
+ static VALUE EnumDescriptor_enummodule(VALUE _self) {
1333
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1399
1334
  if (self->module == Qnil) {
1400
1335
  self->module = build_module_from_enumdesc(_self);
1401
1336
  }
1402
1337
  return self->module;
1403
1338
  }
1404
1339
 
1340
+ static void EnumDescriptor_register(VALUE module) {
1341
+ VALUE klass = rb_define_class_under(
1342
+ module, "EnumDescriptor", rb_cObject);
1343
+ rb_define_alloc_func(klass, EnumDescriptor_alloc);
1344
+ rb_define_method(klass, "initialize", EnumDescriptor_initialize, 3);
1345
+ rb_define_method(klass, "name", EnumDescriptor_name, 0);
1346
+ rb_define_method(klass, "lookup_name", EnumDescriptor_lookup_name, 1);
1347
+ rb_define_method(klass, "lookup_value", EnumDescriptor_lookup_value, 1);
1348
+ rb_define_method(klass, "each", EnumDescriptor_each, 0);
1349
+ rb_define_method(klass, "enummodule", EnumDescriptor_enummodule, 0);
1350
+ rb_define_method(klass, "file_descriptor", EnumDescriptor_file_descriptor, 0);
1351
+ rb_include_module(klass, rb_mEnumerable);
1352
+ rb_gc_register_address(&cEnumDescriptor);
1353
+ cEnumDescriptor = klass;
1354
+ }
1355
+
1405
1356
  // -----------------------------------------------------------------------------
1406
- // MessageBuilderContext.
1357
+ // FileBuilderContext.
1407
1358
  // -----------------------------------------------------------------------------
1408
1359
 
1409
- DEFINE_CLASS(MessageBuilderContext,
1410
- "Google::Protobuf::Internal::MessageBuilderContext");
1360
+ typedef struct {
1361
+ upb_arena *arena;
1362
+ google_protobuf_FileDescriptorProto* file_proto;
1363
+ VALUE descriptor_pool;
1364
+ } FileBuilderContext;
1411
1365
 
1412
- void MessageBuilderContext_mark(void* _self) {
1413
- MessageBuilderContext* self = _self;
1414
- rb_gc_mark(self->file_builder);
1366
+ static VALUE cFileBuilderContext = Qnil;
1367
+
1368
+ static void FileBuilderContext_mark(void* _self) {
1369
+ FileBuilderContext* self = _self;
1370
+ rb_gc_mark(self->descriptor_pool);
1415
1371
  }
1416
1372
 
1417
- void MessageBuilderContext_free(void* _self) {
1418
- MessageBuilderContext* self = _self;
1373
+ static void FileBuilderContext_free(void* _self) {
1374
+ FileBuilderContext* self = _self;
1375
+ upb_arena_free(self->arena);
1419
1376
  xfree(self);
1420
1377
  }
1421
1378
 
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;
1379
+ static const rb_data_type_t FileBuilderContext_type = {
1380
+ "Google::Protobuf::Internal::FileBuilderContext",
1381
+ {FileBuilderContext_mark, FileBuilderContext_free, NULL},
1382
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
1383
+ };
1384
+
1385
+ static FileBuilderContext* ruby_to_FileBuilderContext(VALUE val) {
1386
+ FileBuilderContext* ret;
1387
+ TypedData_Get_Struct(val, FileBuilderContext, &FileBuilderContext_type, ret);
1427
1388
  return ret;
1428
1389
  }
1429
1390
 
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;
1391
+ static upb_strview FileBuilderContext_strdup2(VALUE _self, const char *str) {
1392
+ FileBuilderContext* self = ruby_to_FileBuilderContext(_self);
1393
+ upb_strview ret;
1394
+ char *data;
1395
+
1396
+ ret.size = strlen(str);
1397
+ data = upb_malloc(upb_arena_alloc(self->arena), ret.size + 1);
1398
+ ret.data = data;
1399
+ memcpy(data, str, ret.size);
1400
+ /* Null-terminate required by rewrite_enum_defaults() above. */
1401
+ data[ret.size] = '\0';
1402
+ return ret;
1403
+ }
1404
+
1405
+ static upb_strview FileBuilderContext_strdup(VALUE _self, VALUE rb_str) {
1406
+ return FileBuilderContext_strdup2(_self, get_str(rb_str));
1407
+ }
1408
+
1409
+ static upb_strview FileBuilderContext_strdup_sym(VALUE _self, VALUE rb_sym) {
1410
+ Check_Type(rb_sym, T_SYMBOL);
1411
+ return FileBuilderContext_strdup(_self, rb_id2str(SYM2ID(rb_sym)));
1412
+ }
1413
+
1414
+ static VALUE FileBuilderContext_alloc(VALUE klass) {
1415
+ FileBuilderContext* self = ALLOC(FileBuilderContext);
1416
+ VALUE ret = TypedData_Wrap_Struct(klass, &FileBuilderContext_type, self);
1417
+ self->arena = upb_arena_new();
1418
+ self->file_proto = google_protobuf_FileDescriptorProto_new(self->arena);
1419
+ self->descriptor_pool = Qnil;
1420
+ return ret;
1444
1421
  }
1445
1422
 
1446
1423
  /*
1447
1424
  * call-seq:
1448
- * MessageBuilderContext.new(file_builder, name) => context
1425
+ * FileBuilderContext.new(descriptor_pool) => context
1449
1426
  *
1450
- * Create a new message builder context around the given message descriptor and
1427
+ * Create a new file builder context for the given file descriptor and
1451
1428
  * builder context. This class is intended to serve as a DSL context to be used
1452
1429
  * with #instance_eval.
1453
1430
  */
1454
- VALUE MessageBuilderContext_initialize(VALUE _self,
1455
- VALUE _file_builder,
1456
- VALUE name) {
1457
- DEFINE_SELF(MessageBuilderContext, self, _self);
1458
- FileBuilderContext* file_builder = ruby_to_FileBuilderContext(_file_builder);
1459
- google_protobuf_FileDescriptorProto* file_proto = file_builder->file_proto;
1460
-
1461
- self->file_builder = _file_builder;
1462
- self->msg_proto = google_protobuf_FileDescriptorProto_add_message_type(
1463
- file_proto, file_builder->arena);
1464
-
1465
- google_protobuf_DescriptorProto_set_name(
1466
- self->msg_proto, FileBuilderContext_strdup(_file_builder, name));
1467
-
1468
- return Qnil;
1469
- }
1470
-
1471
- static void msgdef_add_field(VALUE msgbuilder_rb, upb_label_t label, VALUE name,
1472
- VALUE type, VALUE number, VALUE type_class,
1473
- VALUE options, int oneof_index,
1474
- bool proto3_optional) {
1475
- DEFINE_SELF(MessageBuilderContext, self, msgbuilder_rb);
1476
- FileBuilderContext* file_context =
1477
- ruby_to_FileBuilderContext(self->file_builder);
1478
- google_protobuf_FieldDescriptorProto* field_proto;
1479
- VALUE name_str;
1480
-
1481
- field_proto = google_protobuf_DescriptorProto_add_field(self->msg_proto,
1482
- file_context->arena);
1483
-
1484
- Check_Type(name, T_SYMBOL);
1485
- name_str = rb_id2str(SYM2ID(name));
1486
-
1487
- google_protobuf_FieldDescriptorProto_set_name(
1488
- field_proto, FileBuilderContext_strdup(self->file_builder, name_str));
1489
- google_protobuf_FieldDescriptorProto_set_number(field_proto, NUM2INT(number));
1490
- google_protobuf_FieldDescriptorProto_set_label(field_proto, (int)label);
1491
- google_protobuf_FieldDescriptorProto_set_type(
1492
- field_proto, (int)ruby_to_descriptortype(type));
1493
-
1494
- if (proto3_optional) {
1495
- google_protobuf_FieldDescriptorProto_set_proto3_optional(field_proto, true);
1496
- }
1431
+ static VALUE FileBuilderContext_initialize(VALUE _self, VALUE descriptor_pool,
1432
+ VALUE name, VALUE options) {
1433
+ FileBuilderContext* self = ruby_to_FileBuilderContext(_self);
1434
+ self->descriptor_pool = descriptor_pool;
1497
1435
 
1498
- if (type_class != Qnil) {
1499
- Check_Type(type_class, T_STRING);
1436
+ google_protobuf_FileDescriptorProto_set_name(
1437
+ self->file_proto, FileBuilderContext_strdup(_self, name));
1500
1438
 
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
- }
1439
+ // Default syntax for Ruby is proto3.
1440
+ google_protobuf_FileDescriptorProto_set_syntax(
1441
+ self->file_proto,
1442
+ FileBuilderContext_strdup(_self, rb_str_new2("proto3")));
1506
1443
 
1507
1444
  if (options != Qnil) {
1508
- Check_Type(options, T_HASH);
1445
+ VALUE syntax;
1509
1446
 
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")));
1447
+ Check_Type(options, T_HASH);
1448
+ syntax = rb_hash_lookup2(options, ID2SYM(rb_intern("syntax")), Qnil);
1514
1449
 
1515
- /* Call #to_s since all defaults are strings in the descriptor. */
1516
- default_value = rb_funcall(default_value, rb_intern("to_s"), 0);
1450
+ if (syntax != Qnil) {
1451
+ VALUE syntax_str;
1517
1452
 
1518
- google_protobuf_FieldDescriptorProto_set_default_value(
1519
- field_proto,
1520
- FileBuilderContext_strdup(self->file_builder, default_value));
1453
+ Check_Type(syntax, T_SYMBOL);
1454
+ syntax_str = rb_id2str(SYM2ID(syntax));
1455
+ google_protobuf_FileDescriptorProto_set_syntax(
1456
+ self->file_proto, FileBuilderContext_strdup(_self, syntax_str));
1457
+ }
1458
+ }
1459
+
1460
+ return Qnil;
1461
+ }
1462
+
1463
+ static void MessageBuilderContext_add_synthetic_oneofs(VALUE _self);
1464
+
1465
+ /*
1466
+ * call-seq:
1467
+ * FileBuilderContext.add_message(name, &block)
1468
+ *
1469
+ * Creates a new, empty descriptor with the given name, and invokes the block in
1470
+ * the context of a MessageBuilderContext on that descriptor. The block can then
1471
+ * call, e.g., MessageBuilderContext#optional and MessageBuilderContext#repeated
1472
+ * methods to define the message fields.
1473
+ *
1474
+ * This is the recommended, idiomatic way to build message definitions.
1475
+ */
1476
+ static VALUE FileBuilderContext_add_message(VALUE _self, VALUE name) {
1477
+ VALUE args[2] = { _self, name };
1478
+ VALUE ctx = rb_class_new_instance(2, args, cMessageBuilderContext);
1479
+ VALUE block = rb_block_proc();
1480
+ rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
1481
+ MessageBuilderContext_add_synthetic_oneofs(ctx);
1482
+ return Qnil;
1483
+ }
1484
+
1485
+ /* We have to do some relatively complicated logic here for backward
1486
+ * compatibility.
1487
+ *
1488
+ * In descriptor.proto, messages are nested inside other messages if that is
1489
+ * what the original .proto file looks like. For example, suppose we have this
1490
+ * foo.proto:
1491
+ *
1492
+ * package foo;
1493
+ * message Bar {
1494
+ * message Baz {}
1495
+ * }
1496
+ *
1497
+ * The descriptor for this must look like this:
1498
+ *
1499
+ * file {
1500
+ * name: "test.proto"
1501
+ * package: "foo"
1502
+ * message_type {
1503
+ * name: "Bar"
1504
+ * nested_type {
1505
+ * name: "Baz"
1506
+ * }
1507
+ * }
1508
+ * }
1509
+ *
1510
+ * However, the Ruby generated code has always generated messages in a flat,
1511
+ * non-nested way:
1512
+ *
1513
+ * Google::Protobuf::DescriptorPool.generated_pool.build do
1514
+ * add_message "foo.Bar" do
1515
+ * end
1516
+ * add_message "foo.Bar.Baz" do
1517
+ * end
1518
+ * end
1519
+ *
1520
+ * Here we need to do a translation where we turn this generated code into the
1521
+ * above descriptor. We need to infer that "foo" is the package name, and not
1522
+ * a message itself.
1523
+ *
1524
+ * We delegate to Ruby to compute the transformation, for more concice and
1525
+ * readable code than we can do in C */
1526
+ static void rewrite_names(VALUE _file_builder,
1527
+ google_protobuf_FileDescriptorProto* file_proto) {
1528
+ FileBuilderContext* file_builder = ruby_to_FileBuilderContext(_file_builder);
1529
+ upb_arena *arena = file_builder->arena;
1530
+ // Build params (package, msg_names, enum_names).
1531
+ VALUE package = Qnil;
1532
+ VALUE msg_names = rb_ary_new();
1533
+ VALUE enum_names = rb_ary_new();
1534
+ size_t msg_count, enum_count, i;
1535
+ VALUE new_package, nesting, msg_ents, enum_ents;
1536
+ google_protobuf_DescriptorProto** msgs;
1537
+ google_protobuf_EnumDescriptorProto** enums;
1538
+
1539
+ if (google_protobuf_FileDescriptorProto_has_package(file_proto)) {
1540
+ upb_strview package_str =
1541
+ google_protobuf_FileDescriptorProto_package(file_proto);
1542
+ package = rb_str_new(package_str.data, package_str.size);
1543
+ }
1544
+
1545
+ msgs = google_protobuf_FileDescriptorProto_mutable_message_type(file_proto,
1546
+ &msg_count);
1547
+ for (i = 0; i < msg_count; i++) {
1548
+ upb_strview name = google_protobuf_DescriptorProto_name(msgs[i]);
1549
+ rb_ary_push(msg_names, rb_str_new(name.data, name.size));
1550
+ }
1551
+
1552
+ enums = google_protobuf_FileDescriptorProto_mutable_enum_type(file_proto,
1553
+ &enum_count);
1554
+ for (i = 0; i < enum_count; i++) {
1555
+ upb_strview name = google_protobuf_EnumDescriptorProto_name(enums[i]);
1556
+ rb_ary_push(enum_names, rb_str_new(name.data, name.size));
1557
+ }
1558
+
1559
+ {
1560
+ // Call Ruby code to calculate package name and nesting.
1561
+ VALUE args[3] = { package, msg_names, enum_names };
1562
+ VALUE internal = rb_eval_string("Google::Protobuf::Internal");
1563
+ VALUE ret = rb_funcallv(internal, rb_intern("fixup_descriptor"), 3, args);
1564
+
1565
+ new_package = rb_ary_entry(ret, 0);
1566
+ nesting = rb_ary_entry(ret, 1);
1567
+ }
1568
+
1569
+ // Rewrite package and names.
1570
+ if (new_package != Qnil) {
1571
+ upb_strview new_package_str =
1572
+ FileBuilderContext_strdup(_file_builder, new_package);
1573
+ google_protobuf_FileDescriptorProto_set_package(file_proto,
1574
+ new_package_str);
1575
+ }
1576
+
1577
+ for (i = 0; i < msg_count; i++) {
1578
+ upb_strview name = google_protobuf_DescriptorProto_name(msgs[i]);
1579
+ remove_path(&name);
1580
+ google_protobuf_DescriptorProto_set_name(msgs[i], name);
1581
+ }
1582
+
1583
+ for (i = 0; i < enum_count; i++) {
1584
+ upb_strview name = google_protobuf_EnumDescriptorProto_name(enums[i]);
1585
+ remove_path(&name);
1586
+ google_protobuf_EnumDescriptorProto_set_name(enums[i], name);
1587
+ }
1588
+
1589
+ // Rewrite nesting.
1590
+ msg_ents = rb_hash_aref(nesting, ID2SYM(rb_intern("msgs")));
1591
+ enum_ents = rb_hash_aref(nesting, ID2SYM(rb_intern("enums")));
1592
+
1593
+ Check_Type(msg_ents, T_ARRAY);
1594
+ Check_Type(enum_ents, T_ARRAY);
1595
+
1596
+ for (i = 0; i < (size_t)RARRAY_LEN(msg_ents); i++) {
1597
+ VALUE msg_ent = rb_ary_entry(msg_ents, i);
1598
+ VALUE pos = rb_hash_aref(msg_ent, ID2SYM(rb_intern("pos")));
1599
+ msgs[i] = msgs[NUM2INT(pos)];
1600
+ rewrite_nesting(msg_ent, msgs[i], msgs, enums, arena);
1601
+ }
1602
+
1603
+ for (i = 0; i < (size_t)RARRAY_LEN(enum_ents); i++) {
1604
+ VALUE enum_pos = rb_ary_entry(enum_ents, i);
1605
+ enums[i] = enums[NUM2INT(enum_pos)];
1606
+ }
1607
+
1608
+ google_protobuf_FileDescriptorProto_resize_message_type(
1609
+ file_proto, RARRAY_LEN(msg_ents), arena);
1610
+ google_protobuf_FileDescriptorProto_resize_enum_type(
1611
+ file_proto, RARRAY_LEN(enum_ents), arena);
1612
+ }
1613
+
1614
+ /*
1615
+ * call-seq:
1616
+ * FileBuilderContext.add_enum(name, &block)
1617
+ *
1618
+ * Creates a new, empty enum descriptor with the given name, and invokes the
1619
+ * block in the context of an EnumBuilderContext on that descriptor. The block
1620
+ * can then call EnumBuilderContext#add_value to define the enum values.
1621
+ *
1622
+ * This is the recommended, idiomatic way to build enum definitions.
1623
+ */
1624
+ static VALUE FileBuilderContext_add_enum(VALUE _self, VALUE name) {
1625
+ VALUE args[2] = { _self, name };
1626
+ VALUE ctx = rb_class_new_instance(2, args, cEnumBuilderContext);
1627
+ VALUE block = rb_block_proc();
1628
+ rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
1629
+ return Qnil;
1630
+ }
1631
+
1632
+ static void FileBuilderContext_build(VALUE _self) {
1633
+ FileBuilderContext* self = ruby_to_FileBuilderContext(_self);
1634
+ DescriptorPool* pool = ruby_to_DescriptorPool(self->descriptor_pool);
1635
+ upb_status status;
1636
+
1637
+ rewrite_enum_defaults(pool->symtab, self->file_proto);
1638
+ rewrite_names(_self, self->file_proto);
1639
+
1640
+ upb_status_clear(&status);
1641
+ if (!upb_symtab_addfile(pool->symtab, self->file_proto, &status)) {
1642
+ rb_raise(cTypeError, "Unable to add defs to DescriptorPool: %s",
1643
+ upb_status_errmsg(&status));
1644
+ }
1645
+ }
1646
+
1647
+ static void FileBuilderContext_register(VALUE module) {
1648
+ VALUE klass = rb_define_class_under(module, "FileBuilderContext", rb_cObject);
1649
+ rb_define_alloc_func(klass, FileBuilderContext_alloc);
1650
+ rb_define_method(klass, "initialize", FileBuilderContext_initialize, 3);
1651
+ rb_define_method(klass, "add_message", FileBuilderContext_add_message, 1);
1652
+ rb_define_method(klass, "add_enum", FileBuilderContext_add_enum, 1);
1653
+ rb_gc_register_address(&cFileBuilderContext);
1654
+ cFileBuilderContext = klass;
1655
+ }
1656
+
1657
+ // -----------------------------------------------------------------------------
1658
+ // MessageBuilderContext.
1659
+ // -----------------------------------------------------------------------------
1660
+
1661
+ typedef struct {
1662
+ google_protobuf_DescriptorProto* msg_proto;
1663
+ VALUE file_builder;
1664
+ } MessageBuilderContext;
1665
+
1666
+ static VALUE cMessageBuilderContext = Qnil;
1667
+
1668
+ static void MessageBuilderContext_mark(void* _self) {
1669
+ MessageBuilderContext* self = _self;
1670
+ rb_gc_mark(self->file_builder);
1671
+ }
1672
+
1673
+ static const rb_data_type_t MessageBuilderContext_type = {
1674
+ "Google::Protobuf::Internal::MessageBuilderContext",
1675
+ {MessageBuilderContext_mark, RUBY_DEFAULT_FREE, NULL},
1676
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
1677
+ };
1678
+
1679
+ static MessageBuilderContext* ruby_to_MessageBuilderContext(VALUE val) {
1680
+ MessageBuilderContext* ret;
1681
+ TypedData_Get_Struct(val, MessageBuilderContext, &MessageBuilderContext_type,
1682
+ ret);
1683
+ return ret;
1684
+ }
1685
+
1686
+ static VALUE MessageBuilderContext_alloc(VALUE klass) {
1687
+ MessageBuilderContext* self = ALLOC(MessageBuilderContext);
1688
+ VALUE ret = TypedData_Wrap_Struct(klass, &MessageBuilderContext_type, self);
1689
+ self->file_builder = Qnil;
1690
+ return ret;
1691
+ }
1692
+
1693
+ /*
1694
+ * call-seq:
1695
+ * MessageBuilderContext.new(file_builder, name) => context
1696
+ *
1697
+ * Create a new message builder context around the given message descriptor and
1698
+ * builder context. This class is intended to serve as a DSL context to be used
1699
+ * with #instance_eval.
1700
+ */
1701
+ static VALUE MessageBuilderContext_initialize(VALUE _self, VALUE _file_builder,
1702
+ VALUE name) {
1703
+ MessageBuilderContext* self = ruby_to_MessageBuilderContext(_self);
1704
+ FileBuilderContext* file_builder = ruby_to_FileBuilderContext(_file_builder);
1705
+ google_protobuf_FileDescriptorProto* file_proto = file_builder->file_proto;
1706
+
1707
+ self->file_builder = _file_builder;
1708
+ self->msg_proto = google_protobuf_FileDescriptorProto_add_message_type(
1709
+ file_proto, file_builder->arena);
1710
+
1711
+ google_protobuf_DescriptorProto_set_name(
1712
+ self->msg_proto, FileBuilderContext_strdup(_file_builder, name));
1713
+
1714
+ return Qnil;
1715
+ }
1716
+
1717
+ static void msgdef_add_field(VALUE msgbuilder_rb, upb_label_t label, VALUE name,
1718
+ VALUE type, VALUE number, VALUE type_class,
1719
+ VALUE options, int oneof_index,
1720
+ bool proto3_optional) {
1721
+ MessageBuilderContext* self = ruby_to_MessageBuilderContext(msgbuilder_rb);
1722
+ FileBuilderContext* file_context =
1723
+ ruby_to_FileBuilderContext(self->file_builder);
1724
+ google_protobuf_FieldDescriptorProto* field_proto;
1725
+ VALUE name_str;
1726
+
1727
+ field_proto = google_protobuf_DescriptorProto_add_field(self->msg_proto,
1728
+ file_context->arena);
1729
+
1730
+ Check_Type(name, T_SYMBOL);
1731
+ name_str = rb_id2str(SYM2ID(name));
1732
+
1733
+ google_protobuf_FieldDescriptorProto_set_name(
1734
+ field_proto, FileBuilderContext_strdup(self->file_builder, name_str));
1735
+ google_protobuf_FieldDescriptorProto_set_number(field_proto, NUM2INT(number));
1736
+ google_protobuf_FieldDescriptorProto_set_label(field_proto, (int)label);
1737
+ google_protobuf_FieldDescriptorProto_set_type(
1738
+ field_proto, (int)ruby_to_descriptortype(type));
1739
+
1740
+ if (proto3_optional) {
1741
+ google_protobuf_FieldDescriptorProto_set_proto3_optional(field_proto, true);
1742
+ }
1743
+
1744
+ if (type_class != Qnil) {
1745
+ Check_Type(type_class, T_STRING);
1746
+
1747
+ // Make it an absolute type name by prepending a dot.
1748
+ type_class = rb_str_append(rb_str_new2("."), type_class);
1749
+ google_protobuf_FieldDescriptorProto_set_type_name(
1750
+ field_proto, FileBuilderContext_strdup(self->file_builder, type_class));
1751
+ }
1752
+
1753
+ if (options != Qnil) {
1754
+ Check_Type(options, T_HASH);
1755
+
1756
+ if (rb_funcall(options, rb_intern("key?"), 1,
1757
+ ID2SYM(rb_intern("default"))) == Qtrue) {
1758
+ VALUE default_value =
1759
+ rb_hash_lookup(options, ID2SYM(rb_intern("default")));
1760
+
1761
+ /* Call #to_s since all defaults are strings in the descriptor. */
1762
+ default_value = rb_funcall(default_value, rb_intern("to_s"), 0);
1763
+
1764
+ google_protobuf_FieldDescriptorProto_set_default_value(
1765
+ field_proto,
1766
+ FileBuilderContext_strdup(self->file_builder, default_value));
1767
+ }
1768
+
1769
+ if (rb_funcall(options, rb_intern("key?"), 1,
1770
+ ID2SYM(rb_intern("json_name"))) == Qtrue) {
1771
+ VALUE json_name =
1772
+ rb_hash_lookup(options, ID2SYM(rb_intern("json_name")));
1773
+
1774
+ google_protobuf_FieldDescriptorProto_set_json_name(
1775
+ field_proto,
1776
+ FileBuilderContext_strdup(self->file_builder, json_name));
1521
1777
  }
1522
1778
  }
1523
1779
 
@@ -1527,9 +1783,16 @@ static void msgdef_add_field(VALUE msgbuilder_rb, upb_label_t label, VALUE name,
1527
1783
  }
1528
1784
  }
1529
1785
 
1786
+ #if RUBY_API_VERSION_CODE >= 20700
1787
+ static VALUE make_mapentry(VALUE _message_builder, VALUE types, int argc,
1788
+ const VALUE* argv, VALUE blockarg) {
1789
+ (void)blockarg;
1790
+ #else
1530
1791
  static VALUE make_mapentry(VALUE _message_builder, VALUE types, int argc,
1531
1792
  VALUE* argv) {
1532
- DEFINE_SELF(MessageBuilderContext, message_builder, _message_builder);
1793
+ #endif
1794
+ MessageBuilderContext* message_builder =
1795
+ ruby_to_MessageBuilderContext(_message_builder);
1533
1796
  VALUE type_class = rb_ary_entry(types, 2);
1534
1797
  FileBuilderContext* file_context =
1535
1798
  ruby_to_FileBuilderContext(message_builder->file_builder);
@@ -1596,8 +1859,8 @@ VALUE MessageBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
1596
1859
  * FieldDescriptor#type=) and the type_class must be a string, if present (as
1597
1860
  * accepted by FieldDescriptor#submsg_name=).
1598
1861
  */
1599
- VALUE MessageBuilderContext_proto3_optional(int argc, VALUE* argv,
1600
- VALUE _self) {
1862
+ static VALUE MessageBuilderContext_proto3_optional(int argc, VALUE* argv,
1863
+ VALUE _self) {
1601
1864
  VALUE name, type, number;
1602
1865
  VALUE type_class, options = Qnil;
1603
1866
 
@@ -1630,7 +1893,8 @@ VALUE MessageBuilderContext_proto3_optional(int argc, VALUE* argv,
1630
1893
  * completeness. Any attempt to add a message type with required fields to a
1631
1894
  * pool will currently result in an error.
1632
1895
  */
1633
- VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self) {
1896
+ static VALUE MessageBuilderContext_required(int argc, VALUE* argv,
1897
+ VALUE _self) {
1634
1898
  VALUE name, type, number;
1635
1899
  VALUE type_class, options = Qnil;
1636
1900
 
@@ -1658,19 +1922,22 @@ VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self) {
1658
1922
  * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
1659
1923
  * string, if present (as accepted by FieldDescriptor#submsg_name=).
1660
1924
  */
1661
- VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self) {
1662
- VALUE name, type, number, type_class;
1925
+ static VALUE MessageBuilderContext_repeated(int argc, VALUE* argv,
1926
+ VALUE _self) {
1927
+ VALUE name, type, number;
1928
+ VALUE type_class, options = Qnil;
1929
+
1930
+ rb_scan_args(argc, argv, "32", &name, &type, &number, &type_class, &options);
1663
1931
 
1664
- if (argc < 3) {
1665
- rb_raise(rb_eArgError, "Expected at least 3 arguments.");
1932
+ // Allow passing (name, type, number, options) or
1933
+ // (name, type, number, type_class, options)
1934
+ if (argc == 4 && RB_TYPE_P(type_class, T_HASH)) {
1935
+ options = type_class;
1936
+ type_class = Qnil;
1666
1937
  }
1667
- name = argv[0];
1668
- type = argv[1];
1669
- number = argv[2];
1670
- type_class = (argc > 3) ? argv[3] : Qnil;
1671
1938
 
1672
1939
  msgdef_add_field(_self, UPB_LABEL_REPEATED, name, type, number, type_class,
1673
- Qnil, -1, false);
1940
+ options, -1, false);
1674
1941
 
1675
1942
  return Qnil;
1676
1943
  }
@@ -1687,8 +1954,8 @@ VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self) {
1687
1954
  * type_class must be a string, if present (as accepted by
1688
1955
  * FieldDescriptor#submsg_name=).
1689
1956
  */
1690
- VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self) {
1691
- DEFINE_SELF(MessageBuilderContext, self, _self);
1957
+ static VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self) {
1958
+ MessageBuilderContext* self = ruby_to_MessageBuilderContext(_self);
1692
1959
  VALUE name, key_type, value_type, number, type_class;
1693
1960
  VALUE mapentry_desc_name;
1694
1961
  FileBuilderContext* file_builder;
@@ -1717,14 +1984,6 @@ VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self) {
1717
1984
 
1718
1985
  file_builder = ruby_to_FileBuilderContext(self->file_builder);
1719
1986
 
1720
- // TODO(haberman): remove this restriction, maps are supported in proto2.
1721
- if (upb_strview_eql(
1722
- google_protobuf_FileDescriptorProto_syntax(file_builder->file_proto),
1723
- upb_strview_makez("proto2"))) {
1724
- rb_raise(rb_eArgError,
1725
- "Cannot add a native map field using proto2 syntax.");
1726
- }
1727
-
1728
1987
  // Create a new message descriptor for the map entry message, and create a
1729
1988
  // repeated submessage field here with that type.
1730
1989
  msg_name = google_protobuf_DescriptorProto_name(self->msg_proto);
@@ -1768,8 +2027,8 @@ VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self) {
1768
2027
  *
1769
2028
  * This is the recommended, idiomatic way to build oneof definitions.
1770
2029
  */
1771
- VALUE MessageBuilderContext_oneof(VALUE _self, VALUE name) {
1772
- DEFINE_SELF(MessageBuilderContext, self, _self);
2030
+ static VALUE MessageBuilderContext_oneof(VALUE _self, VALUE name) {
2031
+ MessageBuilderContext* self = ruby_to_MessageBuilderContext(_self);
1773
2032
  size_t oneof_count;
1774
2033
  FileBuilderContext* file_context =
1775
2034
  ruby_to_FileBuilderContext(self->file_builder);
@@ -1795,8 +2054,8 @@ VALUE MessageBuilderContext_oneof(VALUE _self, VALUE name) {
1795
2054
  return Qnil;
1796
2055
  }
1797
2056
 
1798
- void MessageBuilderContext_add_synthetic_oneofs(VALUE _self) {
1799
- DEFINE_SELF(MessageBuilderContext, self, _self);
2057
+ static void MessageBuilderContext_add_synthetic_oneofs(VALUE _self) {
2058
+ MessageBuilderContext* self = ruby_to_MessageBuilderContext(_self);
1800
2059
  FileBuilderContext* file_context =
1801
2060
  ruby_to_FileBuilderContext(self->file_builder);
1802
2061
  size_t field_count, oneof_count;
@@ -1845,42 +2104,59 @@ void MessageBuilderContext_add_synthetic_oneofs(VALUE _self) {
1845
2104
  }
1846
2105
  }
1847
2106
 
2107
+ static void MessageBuilderContext_register(VALUE module) {
2108
+ VALUE klass = rb_define_class_under(
2109
+ module, "MessageBuilderContext", rb_cObject);
2110
+ rb_define_alloc_func(klass, MessageBuilderContext_alloc);
2111
+ rb_define_method(klass, "initialize",
2112
+ MessageBuilderContext_initialize, 2);
2113
+ rb_define_method(klass, "optional", MessageBuilderContext_optional, -1);
2114
+ rb_define_method(klass, "proto3_optional", MessageBuilderContext_proto3_optional, -1);
2115
+ rb_define_method(klass, "required", MessageBuilderContext_required, -1);
2116
+ rb_define_method(klass, "repeated", MessageBuilderContext_repeated, -1);
2117
+ rb_define_method(klass, "map", MessageBuilderContext_map, -1);
2118
+ rb_define_method(klass, "oneof", MessageBuilderContext_oneof, 1);
2119
+ rb_gc_register_address(&cMessageBuilderContext);
2120
+ cMessageBuilderContext = klass;
2121
+ }
2122
+
1848
2123
  // -----------------------------------------------------------------------------
1849
2124
  // OneofBuilderContext.
1850
2125
  // -----------------------------------------------------------------------------
1851
2126
 
1852
- DEFINE_CLASS(OneofBuilderContext,
1853
- "Google::Protobuf::Internal::OneofBuilderContext");
2127
+ typedef struct {
2128
+ int oneof_index;
2129
+ VALUE message_builder;
2130
+ } OneofBuilderContext;
2131
+
2132
+ static VALUE cOneofBuilderContext = Qnil;
1854
2133
 
1855
2134
  void OneofBuilderContext_mark(void* _self) {
1856
2135
  OneofBuilderContext* self = _self;
1857
2136
  rb_gc_mark(self->message_builder);
1858
2137
  }
1859
2138
 
1860
- void OneofBuilderContext_free(void* _self) {
1861
- xfree(_self);
2139
+ static const rb_data_type_t OneofBuilderContext_type = {
2140
+ "Google::Protobuf::Internal::OneofBuilderContext",
2141
+ {OneofBuilderContext_mark, RUBY_DEFAULT_FREE, NULL},
2142
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
2143
+ };
2144
+
2145
+ static OneofBuilderContext* ruby_to_OneofBuilderContext(VALUE val) {
2146
+ OneofBuilderContext* ret;
2147
+ TypedData_Get_Struct(val, OneofBuilderContext, &OneofBuilderContext_type,
2148
+ ret);
2149
+ return ret;
1862
2150
  }
1863
2151
 
1864
- VALUE OneofBuilderContext_alloc(VALUE klass) {
2152
+ static VALUE OneofBuilderContext_alloc(VALUE klass) {
1865
2153
  OneofBuilderContext* self = ALLOC(OneofBuilderContext);
1866
- VALUE ret = TypedData_Wrap_Struct(
1867
- klass, &_OneofBuilderContext_type, self);
2154
+ VALUE ret = TypedData_Wrap_Struct(klass, &OneofBuilderContext_type, self);
1868
2155
  self->oneof_index = 0;
1869
2156
  self->message_builder = Qnil;
1870
2157
  return ret;
1871
2158
  }
1872
2159
 
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;
1882
- }
1883
-
1884
2160
  /*
1885
2161
  * call-seq:
1886
2162
  * OneofBuilderContext.new(oneof_index, message_builder) => context
@@ -1889,10 +2165,9 @@ void OneofBuilderContext_register(VALUE module) {
1889
2165
  * builder context. This class is intended to serve as a DSL context to be used
1890
2166
  * with #instance_eval.
1891
2167
  */
1892
- VALUE OneofBuilderContext_initialize(VALUE _self,
1893
- VALUE oneof_index,
1894
- VALUE message_builder) {
1895
- DEFINE_SELF(OneofBuilderContext, self, _self);
2168
+ static VALUE OneofBuilderContext_initialize(VALUE _self, VALUE oneof_index,
2169
+ VALUE message_builder) {
2170
+ OneofBuilderContext* self = ruby_to_OneofBuilderContext(_self);
1896
2171
  self->oneof_index = NUM2INT(oneof_index);
1897
2172
  self->message_builder = message_builder;
1898
2173
  return Qnil;
@@ -1908,8 +2183,8 @@ VALUE OneofBuilderContext_initialize(VALUE _self,
1908
2183
  * (as accepted by FieldDescriptor#type=) and the type_class must be a string,
1909
2184
  * if present (as accepted by FieldDescriptor#submsg_name=).
1910
2185
  */
1911
- VALUE OneofBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
1912
- DEFINE_SELF(OneofBuilderContext, self, _self);
2186
+ static VALUE OneofBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
2187
+ OneofBuilderContext* self = ruby_to_OneofBuilderContext(_self);
1913
2188
  VALUE name, type, number;
1914
2189
  VALUE type_class, options = Qnil;
1915
2190
 
@@ -1921,41 +2196,53 @@ VALUE OneofBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
1921
2196
  return Qnil;
1922
2197
  }
1923
2198
 
2199
+ static void OneofBuilderContext_register(VALUE module) {
2200
+ VALUE klass = rb_define_class_under(
2201
+ module, "OneofBuilderContext", rb_cObject);
2202
+ rb_define_alloc_func(klass, OneofBuilderContext_alloc);
2203
+ rb_define_method(klass, "initialize",
2204
+ OneofBuilderContext_initialize, 2);
2205
+ rb_define_method(klass, "optional", OneofBuilderContext_optional, -1);
2206
+ rb_gc_register_address(&cOneofBuilderContext);
2207
+ cOneofBuilderContext = klass;
2208
+ }
2209
+
1924
2210
  // -----------------------------------------------------------------------------
1925
2211
  // EnumBuilderContext.
1926
2212
  // -----------------------------------------------------------------------------
1927
2213
 
1928
- DEFINE_CLASS(EnumBuilderContext,
1929
- "Google::Protobuf::Internal::EnumBuilderContext");
2214
+ typedef struct {
2215
+ google_protobuf_EnumDescriptorProto* enum_proto;
2216
+ VALUE file_builder;
2217
+ } EnumBuilderContext;
2218
+
2219
+ static VALUE cEnumBuilderContext = Qnil;
1930
2220
 
1931
2221
  void EnumBuilderContext_mark(void* _self) {
1932
2222
  EnumBuilderContext* self = _self;
1933
2223
  rb_gc_mark(self->file_builder);
1934
2224
  }
1935
2225
 
1936
- void EnumBuilderContext_free(void* _self) {
1937
- xfree(_self);
2226
+ static const rb_data_type_t EnumBuilderContext_type = {
2227
+ "Google::Protobuf::Internal::EnumBuilderContext",
2228
+ {EnumBuilderContext_mark, RUBY_DEFAULT_FREE, NULL},
2229
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
2230
+ };
2231
+
2232
+ static EnumBuilderContext* ruby_to_EnumBuilderContext(VALUE val) {
2233
+ EnumBuilderContext* ret;
2234
+ TypedData_Get_Struct(val, EnumBuilderContext, &EnumBuilderContext_type, ret);
2235
+ return ret;
1938
2236
  }
1939
2237
 
1940
- VALUE EnumBuilderContext_alloc(VALUE klass) {
2238
+ static VALUE EnumBuilderContext_alloc(VALUE klass) {
1941
2239
  EnumBuilderContext* self = ALLOC(EnumBuilderContext);
1942
- VALUE ret = TypedData_Wrap_Struct(
1943
- klass, &_EnumBuilderContext_type, self);
2240
+ VALUE ret = TypedData_Wrap_Struct(klass, &EnumBuilderContext_type, self);
1944
2241
  self->enum_proto = NULL;
1945
2242
  self->file_builder = Qnil;
1946
2243
  return ret;
1947
2244
  }
1948
2245
 
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
2246
  /*
1960
2247
  * call-seq:
1961
2248
  * EnumBuilderContext.new(file_builder) => context
@@ -1963,9 +2250,9 @@ void EnumBuilderContext_register(VALUE module) {
1963
2250
  * Create a new builder context around the given enum descriptor. This class is
1964
2251
  * intended to serve as a DSL context to be used with #instance_eval.
1965
2252
  */
1966
- VALUE EnumBuilderContext_initialize(VALUE _self, VALUE _file_builder,
1967
- VALUE name) {
1968
- DEFINE_SELF(EnumBuilderContext, self, _self);
2253
+ static VALUE EnumBuilderContext_initialize(VALUE _self, VALUE _file_builder,
2254
+ VALUE name) {
2255
+ EnumBuilderContext* self = ruby_to_EnumBuilderContext(_self);
1969
2256
  FileBuilderContext* file_builder = ruby_to_FileBuilderContext(_file_builder);
1970
2257
  google_protobuf_FileDescriptorProto* file_proto = file_builder->file_proto;
1971
2258
 
@@ -1986,8 +2273,8 @@ VALUE EnumBuilderContext_initialize(VALUE _self, VALUE _file_builder,
1986
2273
  * Adds the given name => number mapping to the enum type. Name must be a Ruby
1987
2274
  * symbol.
1988
2275
  */
1989
- VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number) {
1990
- DEFINE_SELF(EnumBuilderContext, self, _self);
2276
+ static VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number) {
2277
+ EnumBuilderContext* self = ruby_to_EnumBuilderContext(_self);
1991
2278
  FileBuilderContext* file_builder =
1992
2279
  ruby_to_FileBuilderContext(self->file_builder);
1993
2280
  google_protobuf_EnumValueDescriptorProto* enum_value;
@@ -2003,196 +2290,53 @@ VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number) {
2003
2290
  return Qnil;
2004
2291
  }
2005
2292
 
2006
-
2007
- // -----------------------------------------------------------------------------
2008
- // FileBuilderContext.
2009
- // -----------------------------------------------------------------------------
2010
-
2011
- DEFINE_CLASS(FileBuilderContext,
2012
- "Google::Protobuf::Internal::FileBuilderContext");
2013
-
2014
- void FileBuilderContext_mark(void* _self) {
2015
- FileBuilderContext* self = _self;
2016
- rb_gc_mark(self->descriptor_pool);
2017
- }
2018
-
2019
- void FileBuilderContext_free(void* _self) {
2020
- FileBuilderContext* self = _self;
2021
- upb_arena_free(self->arena);
2022
- xfree(self);
2023
- }
2024
-
2025
- upb_strview FileBuilderContext_strdup2(VALUE _self, const char *str) {
2026
- DEFINE_SELF(FileBuilderContext, self, _self);
2027
- upb_strview ret;
2028
- char *data;
2029
-
2030
- ret.size = strlen(str);
2031
- data = upb_malloc(upb_arena_alloc(self->arena), ret.size + 1);
2032
- ret.data = data;
2033
- memcpy(data, str, ret.size);
2034
- /* Null-terminate required by rewrite_enum_defaults() above. */
2035
- data[ret.size] = '\0';
2036
- return ret;
2037
- }
2038
-
2039
- upb_strview FileBuilderContext_strdup(VALUE _self, VALUE rb_str) {
2040
- return FileBuilderContext_strdup2(_self, get_str(rb_str));
2041
- }
2042
-
2043
- upb_strview FileBuilderContext_strdup_sym(VALUE _self, VALUE rb_sym) {
2044
- Check_Type(rb_sym, T_SYMBOL);
2045
- return FileBuilderContext_strdup(_self, rb_id2str(SYM2ID(rb_sym)));
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);
2053
- self->descriptor_pool = Qnil;
2054
- return ret;
2055
- }
2056
-
2057
- void FileBuilderContext_register(VALUE module) {
2058
- VALUE klass = rb_define_class_under(module, "FileBuilderContext", rb_cObject);
2059
- rb_define_alloc_func(klass, FileBuilderContext_alloc);
2060
- rb_define_method(klass, "initialize", FileBuilderContext_initialize, 3);
2061
- rb_define_method(klass, "add_message", FileBuilderContext_add_message, 1);
2062
- rb_define_method(klass, "add_enum", FileBuilderContext_add_enum, 1);
2063
- rb_gc_register_address(&cFileBuilderContext);
2064
- cFileBuilderContext = klass;
2065
- }
2066
-
2067
- /*
2068
- * call-seq:
2069
- * FileBuilderContext.new(descriptor_pool) => context
2070
- *
2071
- * Create a new file builder context for the given file descriptor and
2072
- * builder context. This class is intended to serve as a DSL context to be used
2073
- * with #instance_eval.
2074
- */
2075
- VALUE FileBuilderContext_initialize(VALUE _self, VALUE descriptor_pool,
2076
- VALUE name, VALUE options) {
2077
- DEFINE_SELF(FileBuilderContext, self, _self);
2078
- self->descriptor_pool = descriptor_pool;
2079
-
2080
- google_protobuf_FileDescriptorProto_set_name(
2081
- self->file_proto, FileBuilderContext_strdup(_self, name));
2082
-
2083
- // Default syntax for Ruby is proto3.
2084
- google_protobuf_FileDescriptorProto_set_syntax(
2085
- self->file_proto,
2086
- FileBuilderContext_strdup(_self, rb_str_new2("proto3")));
2087
-
2088
- if (options != Qnil) {
2089
- VALUE syntax;
2090
-
2091
- Check_Type(options, T_HASH);
2092
- syntax = rb_hash_lookup2(options, ID2SYM(rb_intern("syntax")), Qnil);
2093
-
2094
- if (syntax != Qnil) {
2095
- VALUE syntax_str;
2096
-
2097
- Check_Type(syntax, T_SYMBOL);
2098
- syntax_str = rb_id2str(SYM2ID(syntax));
2099
- google_protobuf_FileDescriptorProto_set_syntax(
2100
- self->file_proto, FileBuilderContext_strdup(_self, syntax_str));
2101
- }
2102
- }
2103
-
2104
- return Qnil;
2105
- }
2106
-
2107
- /*
2108
- * call-seq:
2109
- * FileBuilderContext.add_message(name, &block)
2110
- *
2111
- * Creates a new, empty descriptor with the given name, and invokes the block in
2112
- * the context of a MessageBuilderContext on that descriptor. The block can then
2113
- * call, e.g., MessageBuilderContext#optional and MessageBuilderContext#repeated
2114
- * methods to define the message fields.
2115
- *
2116
- * This is the recommended, idiomatic way to build message definitions.
2117
- */
2118
- VALUE FileBuilderContext_add_message(VALUE _self, VALUE name) {
2119
- VALUE args[2] = { _self, name };
2120
- VALUE ctx = rb_class_new_instance(2, args, cMessageBuilderContext);
2121
- VALUE block = rb_block_proc();
2122
- rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
2123
- MessageBuilderContext_add_synthetic_oneofs(ctx);
2124
- return Qnil;
2125
- }
2126
-
2127
- /*
2128
- * call-seq:
2129
- * FileBuilderContext.add_enum(name, &block)
2130
- *
2131
- * Creates a new, empty enum descriptor with the given name, and invokes the
2132
- * block in the context of an EnumBuilderContext on that descriptor. The block
2133
- * can then call EnumBuilderContext#add_value to define the enum values.
2134
- *
2135
- * This is the recommended, idiomatic way to build enum definitions.
2136
- */
2137
- VALUE FileBuilderContext_add_enum(VALUE _self, VALUE name) {
2138
- VALUE args[2] = { _self, name };
2139
- VALUE ctx = rb_class_new_instance(2, args, cEnumBuilderContext);
2140
- VALUE block = rb_block_proc();
2141
- rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
2142
- return Qnil;
2143
- }
2144
-
2145
- void FileBuilderContext_build(VALUE _self) {
2146
- DEFINE_SELF(FileBuilderContext, self, _self);
2147
- DescriptorPool* pool = ruby_to_DescriptorPool(self->descriptor_pool);
2148
- upb_status status;
2149
-
2150
- rewrite_enum_defaults(pool->symtab, self->file_proto);
2151
- rewrite_names(_self, self->file_proto);
2152
-
2153
- upb_status_clear(&status);
2154
- if (!upb_symtab_addfile(pool->symtab, self->file_proto, &status)) {
2155
- rb_raise(cTypeError, "Unable to add defs to DescriptorPool: %s",
2156
- upb_status_errmsg(&status));
2157
- }
2293
+ static void EnumBuilderContext_register(VALUE module) {
2294
+ VALUE klass = rb_define_class_under(
2295
+ module, "EnumBuilderContext", rb_cObject);
2296
+ rb_define_alloc_func(klass, EnumBuilderContext_alloc);
2297
+ rb_define_method(klass, "initialize", EnumBuilderContext_initialize, 2);
2298
+ rb_define_method(klass, "value", EnumBuilderContext_value, 2);
2299
+ rb_gc_register_address(&cEnumBuilderContext);
2300
+ cEnumBuilderContext = klass;
2158
2301
  }
2159
2302
 
2160
2303
  // -----------------------------------------------------------------------------
2161
2304
  // Builder.
2162
2305
  // -----------------------------------------------------------------------------
2163
2306
 
2164
- DEFINE_CLASS(Builder, "Google::Protobuf::Internal::Builder");
2307
+ typedef struct {
2308
+ VALUE descriptor_pool;
2309
+ VALUE default_file_builder;
2310
+ } Builder;
2165
2311
 
2166
- void Builder_mark(void* _self) {
2312
+ static VALUE cBuilder = Qnil;
2313
+
2314
+ static void Builder_mark(void* _self) {
2167
2315
  Builder* self = _self;
2168
2316
  rb_gc_mark(self->descriptor_pool);
2169
2317
  rb_gc_mark(self->default_file_builder);
2170
2318
  }
2171
2319
 
2172
- void Builder_free(void* _self) {
2173
- xfree(_self);
2320
+ static const rb_data_type_t Builder_type = {
2321
+ "Google::Protobuf::Internal::Builder",
2322
+ {Builder_mark, RUBY_DEFAULT_FREE, NULL},
2323
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
2324
+ };
2325
+
2326
+ static Builder* ruby_to_Builder(VALUE val) {
2327
+ Builder* ret;
2328
+ TypedData_Get_Struct(val, Builder, &Builder_type, ret);
2329
+ return ret;
2174
2330
  }
2175
2331
 
2176
- VALUE Builder_alloc(VALUE klass) {
2332
+ static VALUE Builder_alloc(VALUE klass) {
2177
2333
  Builder* self = ALLOC(Builder);
2178
- VALUE ret = TypedData_Wrap_Struct(
2179
- klass, &_Builder_type, self);
2334
+ VALUE ret = TypedData_Wrap_Struct(klass, &Builder_type, self);
2180
2335
  self->descriptor_pool = Qnil;
2181
2336
  self->default_file_builder = Qnil;
2182
2337
  return ret;
2183
2338
  }
2184
2339
 
2185
- void Builder_register(VALUE module) {
2186
- VALUE klass = rb_define_class_under(module, "Builder", rb_cObject);
2187
- rb_define_alloc_func(klass, Builder_alloc);
2188
- rb_define_method(klass, "initialize", Builder_initialize, 1);
2189
- rb_define_method(klass, "add_file", Builder_add_file, -1);
2190
- rb_define_method(klass, "add_message", Builder_add_message, 1);
2191
- rb_define_method(klass, "add_enum", Builder_add_enum, 1);
2192
- rb_gc_register_address(&cBuilder);
2193
- cBuilder = klass;
2194
- }
2195
-
2196
2340
  /*
2197
2341
  * call-seq:
2198
2342
  * Builder.new(descriptor_pool) => builder
@@ -2201,8 +2345,8 @@ void Builder_register(VALUE module) {
2201
2345
  * descriptors and atomically register them into a pool in a way that allows for
2202
2346
  * (co)recursive type references.
2203
2347
  */
2204
- VALUE Builder_initialize(VALUE _self, VALUE pool) {
2205
- DEFINE_SELF(Builder, self, _self);
2348
+ static VALUE Builder_initialize(VALUE _self, VALUE pool) {
2349
+ Builder* self = ruby_to_Builder(_self);
2206
2350
  self->descriptor_pool = pool;
2207
2351
  self->default_file_builder = Qnil; // Created lazily if needed.
2208
2352
  return Qnil;
@@ -2219,8 +2363,8 @@ VALUE Builder_initialize(VALUE _self, VALUE pool) {
2219
2363
  *
2220
2364
  * This is the recommended, idiomatic way to build file descriptors.
2221
2365
  */
2222
- VALUE Builder_add_file(int argc, VALUE* argv, VALUE _self) {
2223
- DEFINE_SELF(Builder, self, _self);
2366
+ static VALUE Builder_add_file(int argc, VALUE* argv, VALUE _self) {
2367
+ Builder* self = ruby_to_Builder(_self);
2224
2368
  VALUE name, options;
2225
2369
  VALUE ctx;
2226
2370
  VALUE block;
@@ -2240,7 +2384,7 @@ VALUE Builder_add_file(int argc, VALUE* argv, VALUE _self) {
2240
2384
  }
2241
2385
 
2242
2386
  static VALUE Builder_get_default_file(VALUE _self) {
2243
- DEFINE_SELF(Builder, self, _self);
2387
+ Builder* self = ruby_to_Builder(_self);
2244
2388
 
2245
2389
  /* Lazily create only if legacy builder-level methods are called. */
2246
2390
  if (self->default_file_builder == Qnil) {
@@ -2264,7 +2408,7 @@ static VALUE Builder_get_default_file(VALUE _self) {
2264
2408
  * files generated by protoc which don't add messages within "add_file" block.
2265
2409
  * Descriptors created this way get assigned to a default empty FileDescriptor.
2266
2410
  */
2267
- VALUE Builder_add_message(VALUE _self, VALUE name) {
2411
+ static VALUE Builder_add_message(VALUE _self, VALUE name) {
2268
2412
  VALUE file_builder = Builder_get_default_file(_self);
2269
2413
  rb_funcall_with_block(file_builder, rb_intern("add_message"), 1, &name,
2270
2414
  rb_block_proc());
@@ -2283,7 +2427,7 @@ VALUE Builder_add_message(VALUE _self, VALUE name) {
2283
2427
  * Enum descriptors created this way get assigned to a default empty
2284
2428
  * FileDescriptor.
2285
2429
  */
2286
- VALUE Builder_add_enum(VALUE _self, VALUE name) {
2430
+ static VALUE Builder_add_enum(VALUE _self, VALUE name) {
2287
2431
  VALUE file_builder = Builder_get_default_file(_self);
2288
2432
  rb_funcall_with_block(file_builder, rb_intern("add_enum"), 1, &name,
2289
2433
  rb_block_proc());
@@ -2292,8 +2436,8 @@ VALUE Builder_add_enum(VALUE _self, VALUE name) {
2292
2436
 
2293
2437
  /* This method is hidden from Ruby, and only called directly from
2294
2438
  * DescriptorPool_build(). */
2295
- VALUE Builder_build(VALUE _self) {
2296
- DEFINE_SELF(Builder, self, _self);
2439
+ static VALUE Builder_build(VALUE _self) {
2440
+ Builder* self = ruby_to_Builder(_self);
2297
2441
 
2298
2442
  if (self->default_file_builder != Qnil) {
2299
2443
  FileBuilderContext_build(self->default_file_builder);
@@ -2303,8 +2447,19 @@ VALUE Builder_build(VALUE _self) {
2303
2447
  return Qnil;
2304
2448
  }
2305
2449
 
2450
+ static void Builder_register(VALUE module) {
2451
+ VALUE klass = rb_define_class_under(module, "Builder", rb_cObject);
2452
+ rb_define_alloc_func(klass, Builder_alloc);
2453
+ rb_define_method(klass, "initialize", Builder_initialize, 1);
2454
+ rb_define_method(klass, "add_file", Builder_add_file, -1);
2455
+ rb_define_method(klass, "add_message", Builder_add_message, 1);
2456
+ rb_define_method(klass, "add_enum", Builder_add_enum, 1);
2457
+ rb_gc_register_address(&cBuilder);
2458
+ cBuilder = klass;
2459
+ }
2460
+
2306
2461
  static VALUE get_def_obj(VALUE _descriptor_pool, const void* ptr, VALUE klass) {
2307
- DEFINE_SELF(DescriptorPool, descriptor_pool, _descriptor_pool);
2462
+ DescriptorPool* descriptor_pool = ruby_to_DescriptorPool(_descriptor_pool);
2308
2463
  VALUE key = ULL2NUM((intptr_t)ptr);
2309
2464
  VALUE def;
2310
2465
 
@@ -2319,48 +2474,111 @@ static VALUE get_def_obj(VALUE _descriptor_pool, const void* ptr, VALUE klass) {
2319
2474
  VALUE args[3] = { c_only_cookie, _descriptor_pool, key };
2320
2475
  def = rb_class_new_instance(3, args, klass);
2321
2476
  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
2477
  }
2344
2478
 
2345
2479
  return def;
2346
2480
  }
2347
2481
 
2348
- VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_msgdef* def) {
2482
+ static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_msgdef* def) {
2349
2483
  return get_def_obj(descriptor_pool, def, cDescriptor);
2350
2484
  }
2351
2485
 
2352
- VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_enumdef* def) {
2486
+ static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_enumdef* def) {
2353
2487
  return get_def_obj(descriptor_pool, def, cEnumDescriptor);
2354
2488
  }
2355
2489
 
2356
- VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_fielddef* def) {
2490
+ static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_fielddef* def) {
2357
2491
  return get_def_obj(descriptor_pool, def, cFieldDescriptor);
2358
2492
  }
2359
2493
 
2360
- VALUE get_filedef_obj(VALUE descriptor_pool, const upb_filedef* def) {
2494
+ static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_filedef* def) {
2361
2495
  return get_def_obj(descriptor_pool, def, cFileDescriptor);
2362
2496
  }
2363
2497
 
2364
- VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_oneofdef* def) {
2498
+ static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_oneofdef* def) {
2365
2499
  return get_def_obj(descriptor_pool, def, cOneofDescriptor);
2366
2500
  }
2501
+
2502
+ // -----------------------------------------------------------------------------
2503
+ // Shared functions
2504
+ // -----------------------------------------------------------------------------
2505
+
2506
+ // Functions exposed to other modules in defs.h.
2507
+
2508
+ VALUE Descriptor_DefToClass(const upb_msgdef *m) {
2509
+ const upb_symtab *symtab = upb_filedef_symtab(upb_msgdef_file(m));
2510
+ VALUE pool = ObjectCache_Get(symtab);
2511
+ PBRUBY_ASSERT(pool != Qnil);
2512
+ VALUE desc_rb = get_msgdef_obj(pool, m);
2513
+ const Descriptor* desc = ruby_to_Descriptor(desc_rb);
2514
+ return desc->klass;
2515
+ }
2516
+
2517
+ const upb_msgdef *Descriptor_GetMsgDef(VALUE desc_rb) {
2518
+ const Descriptor* desc = ruby_to_Descriptor(desc_rb);
2519
+ return desc->msgdef;
2520
+ }
2521
+
2522
+ VALUE TypeInfo_InitArg(int argc, VALUE *argv, int skip_arg) {
2523
+ if (argc > skip_arg) {
2524
+ if (argc > 1 + skip_arg) {
2525
+ rb_raise(rb_eArgError, "Expected a maximum of %d arguments.", skip_arg + 1);
2526
+ }
2527
+ return argv[skip_arg];
2528
+ } else {
2529
+ return Qnil;
2530
+ }
2531
+ }
2532
+
2533
+ TypeInfo TypeInfo_FromClass(int argc, VALUE* argv, int skip_arg,
2534
+ VALUE* type_class, VALUE* init_arg) {
2535
+ TypeInfo ret = {ruby_to_fieldtype(argv[skip_arg])};
2536
+
2537
+ if (ret.type == UPB_TYPE_MESSAGE || ret.type == UPB_TYPE_ENUM) {
2538
+ *init_arg = TypeInfo_InitArg(argc, argv, skip_arg + 2);
2539
+
2540
+ if (argc < 2 + skip_arg) {
2541
+ rb_raise(rb_eArgError, "Expected at least %d arguments for message/enum.",
2542
+ 2 + skip_arg);
2543
+ }
2544
+
2545
+ VALUE klass = argv[1 + skip_arg];
2546
+ VALUE desc = MessageOrEnum_GetDescriptor(klass);
2547
+ *type_class = klass;
2548
+
2549
+ if (desc == Qnil) {
2550
+ rb_raise(rb_eArgError,
2551
+ "Type class has no descriptor. Please pass a "
2552
+ "class or enum as returned by the DescriptorPool.");
2553
+ }
2554
+
2555
+ if (ret.type == UPB_TYPE_MESSAGE) {
2556
+ ret.def.msgdef = ruby_to_Descriptor(desc)->msgdef;
2557
+ Message_CheckClass(klass);
2558
+ } else {
2559
+ PBRUBY_ASSERT(ret.type == UPB_TYPE_ENUM);
2560
+ ret.def.enumdef = ruby_to_EnumDescriptor(desc)->enumdef;
2561
+ }
2562
+ } else {
2563
+ *init_arg = TypeInfo_InitArg(argc, argv, skip_arg + 1);
2564
+ }
2565
+
2566
+ return ret;
2567
+ }
2568
+
2569
+ void Defs_register(VALUE module) {
2570
+ DescriptorPool_register(module);
2571
+ Descriptor_register(module);
2572
+ FileDescriptor_register(module);
2573
+ FieldDescriptor_register(module);
2574
+ OneofDescriptor_register(module);
2575
+ EnumDescriptor_register(module);
2576
+ FileBuilderContext_register(module);
2577
+ MessageBuilderContext_register(module);
2578
+ OneofBuilderContext_register(module);
2579
+ EnumBuilderContext_register(module);
2580
+ Builder_register(module);
2581
+
2582
+ rb_gc_register_address(&c_only_cookie);
2583
+ c_only_cookie = rb_class_new_instance(0, NULL, rb_cObject);
2584
+ }