google-protobuf 3.19.1 → 4.30.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/ext/google/protobuf_c/Rakefile +3 -0
  3. data/ext/google/protobuf_c/convert.c +153 -166
  4. data/ext/google/protobuf_c/convert.h +15 -37
  5. data/ext/google/protobuf_c/defs.c +867 -251
  6. data/ext/google/protobuf_c/defs.h +22 -47
  7. data/ext/google/protobuf_c/extconf.rb +25 -5
  8. data/ext/google/protobuf_c/glue.c +135 -0
  9. data/ext/google/protobuf_c/map.c +182 -145
  10. data/ext/google/protobuf_c/map.h +16 -35
  11. data/ext/google/protobuf_c/message.c +534 -437
  12. data/ext/google/protobuf_c/message.h +30 -49
  13. data/ext/google/protobuf_c/protobuf.c +125 -238
  14. data/ext/google/protobuf_c/protobuf.h +40 -49
  15. data/ext/google/protobuf_c/repeated_field.c +152 -120
  16. data/ext/google/protobuf_c/repeated_field.h +16 -34
  17. data/ext/google/protobuf_c/ruby-upb.c +15827 -7228
  18. data/ext/google/protobuf_c/ruby-upb.h +15075 -3866
  19. data/ext/google/protobuf_c/shared_convert.c +69 -0
  20. data/ext/google/protobuf_c/shared_convert.h +26 -0
  21. data/ext/google/protobuf_c/shared_message.c +37 -0
  22. data/ext/google/protobuf_c/shared_message.h +21 -0
  23. data/ext/google/protobuf_c/third_party/utf8_range/LICENSE +22 -0
  24. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.c +207 -0
  25. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.h +22 -0
  26. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range_neon.inc +117 -0
  27. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range_sse.inc +272 -0
  28. data/ext/google/protobuf_c/wrap_memcpy.c +7 -29
  29. data/lib/google/protobuf/any_pb.rb +6 -8
  30. data/lib/google/protobuf/api_pb.rb +6 -26
  31. data/lib/google/protobuf/descriptor_pb.rb +24 -225
  32. data/lib/google/protobuf/duration_pb.rb +6 -8
  33. data/lib/google/protobuf/empty_pb.rb +6 -6
  34. data/lib/google/protobuf/ffi/descriptor.rb +175 -0
  35. data/lib/google/protobuf/ffi/descriptor_pool.rb +77 -0
  36. data/lib/google/protobuf/ffi/enum_descriptor.rb +183 -0
  37. data/lib/google/protobuf/ffi/ffi.rb +214 -0
  38. data/lib/google/protobuf/ffi/field_descriptor.rb +340 -0
  39. data/lib/google/protobuf/ffi/file_descriptor.rb +59 -0
  40. data/lib/google/protobuf/ffi/internal/arena.rb +60 -0
  41. data/lib/google/protobuf/ffi/internal/convert.rb +292 -0
  42. data/lib/google/protobuf/ffi/internal/pointer_helper.rb +35 -0
  43. data/lib/google/protobuf/ffi/internal/type_safety.rb +25 -0
  44. data/lib/google/protobuf/ffi/map.rb +433 -0
  45. data/lib/google/protobuf/ffi/message.rb +783 -0
  46. data/lib/google/protobuf/ffi/method_descriptor.rb +124 -0
  47. data/lib/google/protobuf/ffi/object_cache.rb +30 -0
  48. data/lib/google/protobuf/ffi/oneof_descriptor.rb +107 -0
  49. data/lib/google/protobuf/ffi/repeated_field.rb +411 -0
  50. data/lib/google/protobuf/ffi/service_descriptor.rb +117 -0
  51. data/lib/google/protobuf/field_mask_pb.rb +6 -7
  52. data/lib/google/protobuf/internal/object_cache.rb +99 -0
  53. data/lib/google/protobuf/message_exts.rb +10 -28
  54. data/lib/google/protobuf/plugin_pb.rb +25 -0
  55. data/lib/google/protobuf/repeated_field.rb +22 -33
  56. data/lib/google/protobuf/source_context_pb.rb +6 -7
  57. data/lib/google/protobuf/struct_pb.rb +6 -23
  58. data/lib/google/protobuf/timestamp_pb.rb +6 -8
  59. data/lib/google/protobuf/type_pb.rb +6 -71
  60. data/lib/google/protobuf/well_known_types.rb +16 -40
  61. data/lib/google/protobuf/wrappers_pb.rb +6 -31
  62. data/lib/google/protobuf.rb +32 -50
  63. data/lib/google/protobuf_ffi.rb +52 -0
  64. data/lib/google/protobuf_native.rb +19 -0
  65. data/lib/google/tasks/ffi.rake +100 -0
  66. metadata +97 -20
  67. data/lib/google/protobuf/descriptor_dsl.rb +0 -458
  68. data/tests/basic.rb +0 -640
  69. data/tests/generated_code_test.rb +0 -23
  70. data/tests/stress.rb +0 -38
@@ -1,36 +1,12 @@
1
1
  // Protocol Buffers - Google's data interchange format
2
2
  // Copyright 2014 Google Inc. All rights reserved.
3
- // https://developers.google.com/protocol-buffers/
4
3
  //
5
- // 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.
4
+ // Use of this source code is governed by a BSD-style
5
+ // license that can be found in the LICENSE file or at
6
+ // https://developers.google.com/open-source/licenses/bsd
30
7
 
31
8
  #include <ctype.h>
32
9
  #include <errno.h>
33
- #include <ruby/version.h>
34
10
 
35
11
  #include "convert.h"
36
12
  #include "message.h"
@@ -41,11 +17,14 @@
41
17
  // instances.
42
18
  // -----------------------------------------------------------------------------
43
19
 
44
- static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_msgdef* def);
45
- static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_enumdef* def);
46
- static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_fielddef* def);
47
- static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_filedef* def);
48
- static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_oneofdef* def);
20
+ static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_MessageDef* def);
21
+ static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_EnumDef* def);
22
+ static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_FieldDef* def);
23
+ static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_FileDef* def);
24
+ static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_OneofDef* def);
25
+ static VALUE get_servicedef_obj(VALUE descriptor_pool,
26
+ const upb_ServiceDef* def);
27
+ static VALUE get_methoddef_obj(VALUE descriptor_pool, const upb_MethodDef* def);
49
28
 
50
29
  // A distinct object that is not accessible from Ruby. We use this as a
51
30
  // constructor argument to enforce that certain objects cannot be created from
@@ -67,14 +46,16 @@ static VALUE rb_str_maybe_null(const char* s) {
67
46
  }
68
47
  return rb_str_new2(s);
69
48
  }
70
-
49
+ static ID options_instancevar_interned;
71
50
  // -----------------------------------------------------------------------------
72
51
  // DescriptorPool.
73
52
  // -----------------------------------------------------------------------------
74
53
 
75
54
  typedef struct {
55
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
56
+ // macro to update VALUE references, as to trigger write barriers.
76
57
  VALUE def_to_descriptor; // Hash table of def* -> Ruby descriptor.
77
- upb_symtab* symtab;
58
+ upb_DefPool* symtab;
78
59
  } DescriptorPool;
79
60
 
80
61
  VALUE cDescriptorPool = Qnil;
@@ -90,14 +71,14 @@ static void DescriptorPool_mark(void* _self) {
90
71
 
91
72
  static void DescriptorPool_free(void* _self) {
92
73
  DescriptorPool* self = _self;
93
- upb_symtab_free(self->symtab);
74
+ upb_DefPool_Free(self->symtab);
94
75
  xfree(self);
95
76
  }
96
77
 
97
78
  static const rb_data_type_t DescriptorPool_type = {
98
- "Google::Protobuf::DescriptorPool",
99
- {DescriptorPool_mark, DescriptorPool_free, NULL},
100
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
79
+ "Google::Protobuf::DescriptorPool",
80
+ {DescriptorPool_mark, DescriptorPool_free, NULL},
81
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
101
82
  };
102
83
 
103
84
  static DescriptorPool* ruby_to_DescriptorPool(VALUE val) {
@@ -107,8 +88,8 @@ static DescriptorPool* ruby_to_DescriptorPool(VALUE val) {
107
88
  }
108
89
 
109
90
  // Exposed to other modules in defs.h.
110
- const upb_symtab *DescriptorPool_GetSymtab(VALUE desc_pool_rb) {
111
- DescriptorPool *pool = ruby_to_DescriptorPool(desc_pool_rb);
91
+ const upb_DefPool* DescriptorPool_GetSymtab(VALUE desc_pool_rb) {
92
+ DescriptorPool* pool = ruby_to_DescriptorPool(desc_pool_rb);
112
93
  return pool->symtab;
113
94
  }
114
95
 
@@ -125,11 +106,9 @@ static VALUE DescriptorPool_alloc(VALUE klass) {
125
106
  self->def_to_descriptor = Qnil;
126
107
  ret = TypedData_Wrap_Struct(klass, &DescriptorPool_type, self);
127
108
 
128
- self->def_to_descriptor = rb_hash_new();
129
- self->symtab = upb_symtab_new();
130
- ObjectCache_Add(self->symtab, ret);
131
-
132
- return ret;
109
+ RB_OBJ_WRITE(ret, &self->def_to_descriptor, rb_hash_new());
110
+ self->symtab = upb_DefPool_New();
111
+ return ObjectCache_TryAdd(self->symtab, ret);
133
112
  }
134
113
 
135
114
  /*
@@ -143,7 +122,7 @@ VALUE DescriptorPool_add_serialized_file(VALUE _self,
143
122
  DescriptorPool* self = ruby_to_DescriptorPool(_self);
144
123
  Check_Type(serialized_file_proto, T_STRING);
145
124
  VALUE arena_rb = Arena_new();
146
- upb_arena *arena = Arena_get(arena_rb);
125
+ upb_Arena* arena = Arena_get(arena_rb);
147
126
  google_protobuf_FileDescriptorProto* file_proto =
148
127
  google_protobuf_FileDescriptorProto_parse(
149
128
  RSTRING_PTR(serialized_file_proto),
@@ -151,14 +130,15 @@ VALUE DescriptorPool_add_serialized_file(VALUE _self,
151
130
  if (!file_proto) {
152
131
  rb_raise(rb_eArgError, "Unable to parse FileDescriptorProto");
153
132
  }
154
- upb_status status;
155
- upb_status_clear(&status);
156
- const upb_filedef* filedef =
157
- upb_symtab_addfile(self->symtab, file_proto, &status);
133
+ upb_Status status;
134
+ upb_Status_Clear(&status);
135
+ const upb_FileDef* filedef =
136
+ upb_DefPool_AddFile(self->symtab, file_proto, &status);
158
137
  if (!filedef) {
159
138
  rb_raise(cTypeError, "Unable to build file to DescriptorPool: %s",
160
- upb_status_errmsg(&status));
139
+ upb_Status_ErrorMessage(&status));
161
140
  }
141
+ RB_GC_GUARD(arena_rb);
162
142
  return get_filedef_obj(_self, filedef);
163
143
  }
164
144
 
@@ -166,25 +146,37 @@ VALUE DescriptorPool_add_serialized_file(VALUE _self,
166
146
  * call-seq:
167
147
  * DescriptorPool.lookup(name) => descriptor
168
148
  *
169
- * Finds a Descriptor or EnumDescriptor by name and returns it, or nil if none
170
- * exists with the given name.
149
+ * Finds a Descriptor, EnumDescriptor, FieldDescriptor or ServiceDescriptor by
150
+ * name and returns it, or nil if none exists with the given name.
171
151
  */
172
152
  static VALUE DescriptorPool_lookup(VALUE _self, VALUE name) {
173
153
  DescriptorPool* self = ruby_to_DescriptorPool(_self);
174
154
  const char* name_str = get_str(name);
175
- const upb_msgdef* msgdef;
176
- const upb_enumdef* enumdef;
155
+ const upb_MessageDef* msgdef;
156
+ const upb_EnumDef* enumdef;
157
+ const upb_FieldDef* fielddef;
158
+ const upb_ServiceDef* servicedef;
177
159
 
178
- msgdef = upb_symtab_lookupmsg(self->symtab, name_str);
160
+ msgdef = upb_DefPool_FindMessageByName(self->symtab, name_str);
179
161
  if (msgdef) {
180
162
  return get_msgdef_obj(_self, msgdef);
181
163
  }
182
164
 
183
- enumdef = upb_symtab_lookupenum(self->symtab, name_str);
165
+ fielddef = upb_DefPool_FindExtensionByName(self->symtab, name_str);
166
+ if (fielddef) {
167
+ return get_fielddef_obj(_self, fielddef);
168
+ }
169
+
170
+ enumdef = upb_DefPool_FindEnumByName(self->symtab, name_str);
184
171
  if (enumdef) {
185
172
  return get_enumdef_obj(_self, enumdef);
186
173
  }
187
174
 
175
+ servicedef = upb_DefPool_FindServiceByName(self->symtab, name_str);
176
+ if (servicedef) {
177
+ return get_servicedef_obj(_self, servicedef);
178
+ }
179
+
188
180
  return Qnil;
189
181
  }
190
182
 
@@ -202,8 +194,7 @@ static VALUE DescriptorPool_generated_pool(VALUE _self) {
202
194
  }
203
195
 
204
196
  static void DescriptorPool_register(VALUE module) {
205
- VALUE klass = rb_define_class_under(
206
- module, "DescriptorPool", rb_cObject);
197
+ VALUE klass = rb_define_class_under(module, "DescriptorPool", rb_cObject);
207
198
  rb_define_alloc_func(klass, DescriptorPool_alloc);
208
199
  rb_define_method(klass, "add_serialized_file",
209
200
  DescriptorPool_add_serialized_file, 1);
@@ -215,6 +206,7 @@ static void DescriptorPool_register(VALUE module) {
215
206
 
216
207
  rb_gc_register_address(&generated_pool);
217
208
  generated_pool = rb_class_new_instance(0, NULL, klass);
209
+ options_instancevar_interned = rb_intern("options");
218
210
  }
219
211
 
220
212
  // -----------------------------------------------------------------------------
@@ -222,7 +214,9 @@ static void DescriptorPool_register(VALUE module) {
222
214
  // -----------------------------------------------------------------------------
223
215
 
224
216
  typedef struct {
225
- const upb_msgdef* msgdef;
217
+ const upb_MessageDef* msgdef;
218
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
219
+ // macro to update VALUE references, as to trigger write barriers.
226
220
  VALUE klass;
227
221
  VALUE descriptor_pool;
228
222
  } Descriptor;
@@ -236,9 +230,9 @@ static void Descriptor_mark(void* _self) {
236
230
  }
237
231
 
238
232
  static const rb_data_type_t Descriptor_type = {
239
- "Google::Protobuf::Descriptor",
240
- {Descriptor_mark, RUBY_DEFAULT_FREE, NULL},
241
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
233
+ "Google::Protobuf::Descriptor",
234
+ {Descriptor_mark, RUBY_DEFAULT_FREE, NULL},
235
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
242
236
  };
243
237
 
244
238
  static Descriptor* ruby_to_Descriptor(VALUE val) {
@@ -247,6 +241,48 @@ static Descriptor* ruby_to_Descriptor(VALUE val) {
247
241
  return ret;
248
242
  }
249
243
 
244
+ // Decode and return a frozen instance of a Descriptor Option for the given pool
245
+ static VALUE decode_options(VALUE self, const char* option_type, int size,
246
+ const char* bytes, VALUE descriptor_pool) {
247
+ VALUE options_rb = rb_ivar_get(self, options_instancevar_interned);
248
+ if (options_rb != Qnil) {
249
+ return options_rb;
250
+ }
251
+
252
+ static const char* prefix = "google.protobuf.";
253
+ char fullname
254
+ [/*strlen(prefix)*/ 16 +
255
+ /*strln(longest option type supported e.g. "MessageOptions")*/ 14 +
256
+ /*null terminator*/ 1];
257
+
258
+ snprintf(fullname, sizeof(fullname), "%s%s", prefix, option_type);
259
+ const upb_MessageDef* msgdef = upb_DefPool_FindMessageByName(
260
+ ruby_to_DescriptorPool(descriptor_pool)->symtab, fullname);
261
+ if (!msgdef) {
262
+ rb_raise(rb_eRuntimeError, "Cannot find %s in DescriptorPool", option_type);
263
+ }
264
+
265
+ VALUE desc_rb = get_msgdef_obj(descriptor_pool, msgdef);
266
+ const Descriptor* desc = ruby_to_Descriptor(desc_rb);
267
+
268
+ options_rb = Message_decode_bytes(size, bytes, 0, desc->klass, false);
269
+
270
+ // Strip features from the options proto to keep it internal.
271
+ const upb_MessageDef* decoded_desc = NULL;
272
+ upb_Message* options = Message_GetMutable(options_rb, &decoded_desc);
273
+ PBRUBY_ASSERT(options != NULL);
274
+ PBRUBY_ASSERT(decoded_desc == msgdef);
275
+ const upb_FieldDef* field =
276
+ upb_MessageDef_FindFieldByName(decoded_desc, "features");
277
+ PBRUBY_ASSERT(field != NULL);
278
+ upb_Message_ClearFieldByDef(options, field);
279
+
280
+ Message_freeze(options_rb);
281
+
282
+ rb_ivar_set(self, options_instancevar_interned, options_rb);
283
+ return options_rb;
284
+ }
285
+
250
286
  /*
251
287
  * call-seq:
252
288
  * Descriptor.new => descriptor
@@ -280,8 +316,8 @@ static VALUE Descriptor_initialize(VALUE _self, VALUE cookie,
280
316
  "Descriptor objects may not be created from Ruby.");
281
317
  }
282
318
 
283
- self->descriptor_pool = descriptor_pool;
284
- self->msgdef = (const upb_msgdef*)NUM2ULL(ptr);
319
+ RB_OBJ_WRITE(_self, &self->descriptor_pool, descriptor_pool);
320
+ self->msgdef = (const upb_MessageDef*)NUM2ULL(ptr);
285
321
 
286
322
  return Qnil;
287
323
  }
@@ -294,7 +330,8 @@ static VALUE Descriptor_initialize(VALUE _self, VALUE cookie,
294
330
  */
295
331
  static VALUE Descriptor_file_descriptor(VALUE _self) {
296
332
  Descriptor* self = ruby_to_Descriptor(_self);
297
- return get_filedef_obj(self->descriptor_pool, upb_msgdef_file(self->msgdef));
333
+ return get_filedef_obj(self->descriptor_pool,
334
+ upb_MessageDef_File(self->msgdef));
298
335
  }
299
336
 
300
337
  /*
@@ -306,7 +343,7 @@ static VALUE Descriptor_file_descriptor(VALUE _self) {
306
343
  */
307
344
  static VALUE Descriptor_name(VALUE _self) {
308
345
  Descriptor* self = ruby_to_Descriptor(_self);
309
- return rb_str_maybe_null(upb_msgdef_fullname(self->msgdef));
346
+ return rb_str_maybe_null(upb_MessageDef_FullName(self->msgdef));
310
347
  }
311
348
 
312
349
  /*
@@ -318,11 +355,9 @@ static VALUE Descriptor_name(VALUE _self) {
318
355
  static VALUE Descriptor_each(VALUE _self) {
319
356
  Descriptor* self = ruby_to_Descriptor(_self);
320
357
 
321
- upb_msg_field_iter it;
322
- for (upb_msg_field_begin(&it, self->msgdef);
323
- !upb_msg_field_done(&it);
324
- upb_msg_field_next(&it)) {
325
- const upb_fielddef* field = upb_msg_iter_field(&it);
358
+ int n = upb_MessageDef_FieldCount(self->msgdef);
359
+ for (int i = 0; i < n; i++) {
360
+ const upb_FieldDef* field = upb_MessageDef_Field(self->msgdef, i);
326
361
  VALUE obj = get_fielddef_obj(self->descriptor_pool, field);
327
362
  rb_yield(obj);
328
363
  }
@@ -339,7 +374,7 @@ static VALUE Descriptor_each(VALUE _self) {
339
374
  static VALUE Descriptor_lookup(VALUE _self, VALUE name) {
340
375
  Descriptor* self = ruby_to_Descriptor(_self);
341
376
  const char* s = get_str(name);
342
- const upb_fielddef* field = upb_msgdef_ntofz(self->msgdef, s);
377
+ const upb_FieldDef* field = upb_MessageDef_FindFieldByName(self->msgdef, s);
343
378
  if (field == NULL) {
344
379
  return Qnil;
345
380
  }
@@ -356,11 +391,9 @@ static VALUE Descriptor_lookup(VALUE _self, VALUE name) {
356
391
  static VALUE Descriptor_each_oneof(VALUE _self) {
357
392
  Descriptor* self = ruby_to_Descriptor(_self);
358
393
 
359
- upb_msg_oneof_iter it;
360
- for (upb_msg_oneof_begin(&it, self->msgdef);
361
- !upb_msg_oneof_done(&it);
362
- upb_msg_oneof_next(&it)) {
363
- const upb_oneofdef* oneof = upb_msg_iter_oneof(&it);
394
+ int n = upb_MessageDef_OneofCount(self->msgdef);
395
+ for (int i = 0; i < n; i++) {
396
+ const upb_OneofDef* oneof = upb_MessageDef_Oneof(self->msgdef, i);
364
397
  VALUE obj = get_oneofdef_obj(self->descriptor_pool, oneof);
365
398
  rb_yield(obj);
366
399
  }
@@ -377,7 +410,7 @@ static VALUE Descriptor_each_oneof(VALUE _self) {
377
410
  static VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
378
411
  Descriptor* self = ruby_to_Descriptor(_self);
379
412
  const char* s = get_str(name);
380
- const upb_oneofdef* oneof = upb_msgdef_ntooz(self->msgdef, s);
413
+ const upb_OneofDef* oneof = upb_MessageDef_FindOneofByName(self->msgdef, s);
381
414
  if (oneof == NULL) {
382
415
  return Qnil;
383
416
  }
@@ -393,14 +426,54 @@ static VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
393
426
  static VALUE Descriptor_msgclass(VALUE _self) {
394
427
  Descriptor* self = ruby_to_Descriptor(_self);
395
428
  if (self->klass == Qnil) {
396
- self->klass = build_class_from_descriptor(_self);
429
+ RB_OBJ_WRITE(_self, &self->klass, build_class_from_descriptor(_self));
397
430
  }
398
431
  return self->klass;
399
432
  }
400
433
 
434
+ /*
435
+ * call-seq:
436
+ * Descriptor.options => options
437
+ *
438
+ * Returns the `MessageOptions` for this `Descriptor`.
439
+ */
440
+ static VALUE Descriptor_options(VALUE _self) {
441
+ Descriptor* self = ruby_to_Descriptor(_self);
442
+ const google_protobuf_MessageOptions* opts =
443
+ upb_MessageDef_Options(self->msgdef);
444
+ upb_Arena* arena = upb_Arena_New();
445
+ size_t size;
446
+ char* serialized =
447
+ google_protobuf_MessageOptions_serialize(opts, arena, &size);
448
+ VALUE message_options = decode_options(_self, "MessageOptions", size,
449
+ serialized, self->descriptor_pool);
450
+ upb_Arena_Free(arena);
451
+ return message_options;
452
+ }
453
+
454
+ /*
455
+ * call-seq:
456
+ * Descriptor.to_proto => DescriptorProto
457
+ *
458
+ * Returns the `DescriptorProto` of this `Descriptor`.
459
+ */
460
+ static VALUE Descriptor_to_proto(VALUE _self) {
461
+ Descriptor* self = ruby_to_Descriptor(_self);
462
+ upb_Arena* arena = upb_Arena_New();
463
+ google_protobuf_DescriptorProto* proto =
464
+ upb_MessageDef_ToProto(self->msgdef, arena);
465
+ size_t size;
466
+ const char* serialized =
467
+ google_protobuf_DescriptorProto_serialize(proto, arena, &size);
468
+ VALUE proto_class = rb_path2class("Google::Protobuf::DescriptorProto");
469
+ VALUE proto_rb =
470
+ Message_decode_bytes(size, serialized, 0, proto_class, false);
471
+ upb_Arena_Free(arena);
472
+ return proto_rb;
473
+ }
474
+
401
475
  static void Descriptor_register(VALUE module) {
402
- VALUE klass = rb_define_class_under(
403
- module, "Descriptor", rb_cObject);
476
+ VALUE klass = rb_define_class_under(module, "Descriptor", rb_cObject);
404
477
  rb_define_alloc_func(klass, Descriptor_alloc);
405
478
  rb_define_method(klass, "initialize", Descriptor_initialize, 3);
406
479
  rb_define_method(klass, "each", Descriptor_each, 0);
@@ -410,6 +483,8 @@ static void Descriptor_register(VALUE module) {
410
483
  rb_define_method(klass, "msgclass", Descriptor_msgclass, 0);
411
484
  rb_define_method(klass, "name", Descriptor_name, 0);
412
485
  rb_define_method(klass, "file_descriptor", Descriptor_file_descriptor, 0);
486
+ rb_define_method(klass, "options", Descriptor_options, 0);
487
+ rb_define_method(klass, "to_proto", Descriptor_to_proto, 0);
413
488
  rb_include_module(klass, rb_mEnumerable);
414
489
  rb_gc_register_address(&cDescriptor);
415
490
  cDescriptor = klass;
@@ -420,8 +495,10 @@ static void Descriptor_register(VALUE module) {
420
495
  // -----------------------------------------------------------------------------
421
496
 
422
497
  typedef struct {
423
- const upb_filedef* filedef;
424
- VALUE descriptor_pool; // Owns the upb_filedef.
498
+ const upb_FileDef* filedef;
499
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
500
+ // macro to update VALUE references, as to trigger write barriers.
501
+ VALUE descriptor_pool; // Owns the upb_FileDef.
425
502
  } FileDescriptor;
426
503
 
427
504
  static VALUE cFileDescriptor = Qnil;
@@ -432,9 +509,9 @@ static void FileDescriptor_mark(void* _self) {
432
509
  }
433
510
 
434
511
  static const rb_data_type_t FileDescriptor_type = {
435
- "Google::Protobuf::FileDescriptor",
436
- {FileDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
437
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
512
+ "Google::Protobuf::FileDescriptor",
513
+ {FileDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
514
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
438
515
  };
439
516
 
440
517
  static FileDescriptor* ruby_to_FileDescriptor(VALUE val) {
@@ -455,11 +532,11 @@ static VALUE FileDescriptor_alloc(VALUE klass) {
455
532
  * call-seq:
456
533
  * FileDescriptor.new => file
457
534
  *
458
- * Returns a new file descriptor. The syntax must be set before it's passed
535
+ * Returns a new file descriptor. May
459
536
  * to a builder.
460
537
  */
461
538
  static VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
462
- VALUE descriptor_pool, VALUE ptr) {
539
+ VALUE descriptor_pool, VALUE ptr) {
463
540
  FileDescriptor* self = ruby_to_FileDescriptor(_self);
464
541
 
465
542
  if (cookie != c_only_cookie) {
@@ -467,8 +544,8 @@ static VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
467
544
  "Descriptor objects may not be created from Ruby.");
468
545
  }
469
546
 
470
- self->descriptor_pool = descriptor_pool;
471
- self->filedef = (const upb_filedef*)NUM2ULL(ptr);
547
+ RB_OBJ_WRITE(_self, &self->descriptor_pool, descriptor_pool);
548
+ self->filedef = (const upb_FileDef*)NUM2ULL(ptr);
472
549
 
473
550
  return Qnil;
474
551
  }
@@ -481,36 +558,59 @@ static VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
481
558
  */
482
559
  static VALUE FileDescriptor_name(VALUE _self) {
483
560
  FileDescriptor* self = ruby_to_FileDescriptor(_self);
484
- const char* name = upb_filedef_name(self->filedef);
561
+ const char* name = upb_FileDef_Name(self->filedef);
485
562
  return name == NULL ? Qnil : rb_str_new2(name);
486
563
  }
487
564
 
488
565
  /*
489
566
  * call-seq:
490
- * FileDescriptor.syntax => syntax
567
+ * FileDescriptor.options => options
491
568
  *
492
- * Returns this file descriptors syntax.
569
+ * Returns the `FileOptions` for this `FileDescriptor`.
570
+ */
571
+ static VALUE FileDescriptor_options(VALUE _self) {
572
+ FileDescriptor* self = ruby_to_FileDescriptor(_self);
573
+ const google_protobuf_FileOptions* opts = upb_FileDef_Options(self->filedef);
574
+ upb_Arena* arena = upb_Arena_New();
575
+ size_t size;
576
+ char* serialized = google_protobuf_FileOptions_serialize(opts, arena, &size);
577
+ VALUE file_options = decode_options(_self, "FileOptions", size, serialized,
578
+ self->descriptor_pool);
579
+ upb_Arena_Free(arena);
580
+ return file_options;
581
+ }
582
+
583
+ /*
584
+ * call-seq:
585
+ * FileDescriptor.to_proto => FileDescriptorProto
493
586
  *
494
- * Valid syntax versions are:
495
- * :proto2 or :proto3.
587
+ * Returns the `FileDescriptorProto` of this `FileDescriptor`.
496
588
  */
497
- static VALUE FileDescriptor_syntax(VALUE _self) {
589
+ static VALUE FileDescriptor_to_proto(VALUE _self) {
498
590
  FileDescriptor* self = ruby_to_FileDescriptor(_self);
591
+ upb_Arena* arena = upb_Arena_New();
592
+ google_protobuf_FileDescriptorProto* file_proto =
593
+ upb_FileDef_ToProto(self->filedef, arena);
499
594
 
500
- switch (upb_filedef_syntax(self->filedef)) {
501
- case UPB_SYNTAX_PROTO3: return ID2SYM(rb_intern("proto3"));
502
- case UPB_SYNTAX_PROTO2: return ID2SYM(rb_intern("proto2"));
503
- default: return Qnil;
504
- }
595
+ size_t size;
596
+ const char* serialized =
597
+ google_protobuf_FileDescriptorProto_serialize(file_proto, arena, &size);
598
+
599
+ VALUE file_proto_class =
600
+ rb_path2class("Google::Protobuf::FileDescriptorProto");
601
+ VALUE proto_rb =
602
+ Message_decode_bytes(size, serialized, 0, file_proto_class, false);
603
+ upb_Arena_Free(arena);
604
+ return proto_rb;
505
605
  }
506
606
 
507
607
  static void FileDescriptor_register(VALUE module) {
508
- VALUE klass = rb_define_class_under(
509
- module, "FileDescriptor", rb_cObject);
608
+ VALUE klass = rb_define_class_under(module, "FileDescriptor", rb_cObject);
510
609
  rb_define_alloc_func(klass, FileDescriptor_alloc);
511
610
  rb_define_method(klass, "initialize", FileDescriptor_initialize, 3);
512
611
  rb_define_method(klass, "name", FileDescriptor_name, 0);
513
- rb_define_method(klass, "syntax", FileDescriptor_syntax, 0);
612
+ rb_define_method(klass, "options", FileDescriptor_options, 0);
613
+ rb_define_method(klass, "to_proto", FileDescriptor_to_proto, 0);
514
614
  rb_gc_register_address(&cFileDescriptor);
515
615
  cFileDescriptor = klass;
516
616
  }
@@ -520,8 +620,10 @@ static void FileDescriptor_register(VALUE module) {
520
620
  // -----------------------------------------------------------------------------
521
621
 
522
622
  typedef struct {
523
- const upb_fielddef* fielddef;
524
- VALUE descriptor_pool; // Owns the upb_fielddef.
623
+ const upb_FieldDef* fielddef;
624
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
625
+ // macro to update VALUE references, as to trigger write barriers.
626
+ VALUE descriptor_pool; // Owns the upb_FieldDef.
525
627
  } FieldDescriptor;
526
628
 
527
629
  static VALUE cFieldDescriptor = Qnil;
@@ -532,9 +634,9 @@ static void FieldDescriptor_mark(void* _self) {
532
634
  }
533
635
 
534
636
  static const rb_data_type_t FieldDescriptor_type = {
535
- "Google::Protobuf::FieldDescriptor",
536
- {FieldDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
537
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
637
+ "Google::Protobuf::FieldDescriptor",
638
+ {FieldDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
639
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
538
640
  };
539
641
 
540
642
  static FieldDescriptor* ruby_to_FieldDescriptor(VALUE val) {
@@ -559,7 +661,7 @@ static VALUE FieldDescriptor_alloc(VALUE klass) {
559
661
 
560
662
  /*
561
663
  * call-seq:
562
- * EnumDescriptor.new(c_only_cookie, pool, ptr) => EnumDescriptor
664
+ * FieldDescriptor.new(c_only_cookie, pool, ptr) => FieldDescriptor
563
665
  *
564
666
  * Creates a descriptor wrapper object. May only be called from C.
565
667
  */
@@ -572,8 +674,8 @@ static VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
572
674
  "Descriptor objects may not be created from Ruby.");
573
675
  }
574
676
 
575
- self->descriptor_pool = descriptor_pool;
576
- self->fielddef = (const upb_fielddef*)NUM2ULL(ptr);
677
+ RB_OBJ_WRITE(_self, &self->descriptor_pool, descriptor_pool);
678
+ self->fielddef = (const upb_FieldDef*)NUM2ULL(ptr);
577
679
 
578
680
  return Qnil;
579
681
  }
@@ -586,31 +688,31 @@ static VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
586
688
  */
587
689
  static VALUE FieldDescriptor_name(VALUE _self) {
588
690
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
589
- return rb_str_maybe_null(upb_fielddef_name(self->fielddef));
691
+ return rb_str_maybe_null(upb_FieldDef_Name(self->fielddef));
590
692
  }
591
693
 
592
694
  // Non-static, exposed to other .c files.
593
- upb_fieldtype_t ruby_to_fieldtype(VALUE type) {
695
+ upb_CType ruby_to_fieldtype(VALUE type) {
594
696
  if (TYPE(type) != T_SYMBOL) {
595
697
  rb_raise(rb_eArgError, "Expected symbol for field type.");
596
698
  }
597
699
 
598
- #define CONVERT(upb, ruby) \
599
- if (SYM2ID(type) == rb_intern( # ruby )) { \
600
- return UPB_TYPE_ ## upb; \
700
+ #define CONVERT(upb, ruby) \
701
+ if (SYM2ID(type) == rb_intern(#ruby)) { \
702
+ return kUpb_CType_##upb; \
601
703
  }
602
704
 
603
- CONVERT(FLOAT, float);
604
- CONVERT(DOUBLE, double);
605
- CONVERT(BOOL, bool);
606
- CONVERT(STRING, string);
607
- CONVERT(BYTES, bytes);
608
- CONVERT(MESSAGE, message);
609
- CONVERT(ENUM, enum);
610
- CONVERT(INT32, int32);
611
- CONVERT(INT64, int64);
612
- CONVERT(UINT32, uint32);
613
- CONVERT(UINT64, uint64);
705
+ CONVERT(Float, float);
706
+ CONVERT(Double, double);
707
+ CONVERT(Bool, bool);
708
+ CONVERT(String, string);
709
+ CONVERT(Bytes, bytes);
710
+ CONVERT(Message, message);
711
+ CONVERT(Enum, enum);
712
+ CONVERT(Int32, int32);
713
+ CONVERT(Int64, int64);
714
+ CONVERT(UInt32, uint32);
715
+ CONVERT(UInt64, uint64);
614
716
 
615
717
  #undef CONVERT
616
718
 
@@ -618,28 +720,29 @@ upb_fieldtype_t ruby_to_fieldtype(VALUE type) {
618
720
  return 0;
619
721
  }
620
722
 
621
- static VALUE descriptortype_to_ruby(upb_descriptortype_t type) {
723
+ static VALUE descriptortype_to_ruby(upb_FieldType type) {
622
724
  switch (type) {
623
- #define CONVERT(upb, ruby) \
624
- case UPB_DESCRIPTOR_TYPE_ ## upb : return ID2SYM(rb_intern( # ruby ));
625
- CONVERT(FLOAT, float);
626
- CONVERT(DOUBLE, double);
627
- CONVERT(BOOL, bool);
628
- CONVERT(STRING, string);
629
- CONVERT(BYTES, bytes);
630
- CONVERT(MESSAGE, message);
631
- CONVERT(GROUP, group);
632
- CONVERT(ENUM, enum);
633
- CONVERT(INT32, int32);
634
- CONVERT(INT64, int64);
635
- CONVERT(UINT32, uint32);
636
- CONVERT(UINT64, uint64);
637
- CONVERT(SINT32, sint32);
638
- CONVERT(SINT64, sint64);
639
- CONVERT(FIXED32, fixed32);
640
- CONVERT(FIXED64, fixed64);
641
- CONVERT(SFIXED32, sfixed32);
642
- CONVERT(SFIXED64, sfixed64);
725
+ #define CONVERT(upb, ruby) \
726
+ case kUpb_FieldType_##upb: \
727
+ return ID2SYM(rb_intern(#ruby));
728
+ CONVERT(Float, float);
729
+ CONVERT(Double, double);
730
+ CONVERT(Bool, bool);
731
+ CONVERT(String, string);
732
+ CONVERT(Bytes, bytes);
733
+ CONVERT(Message, message);
734
+ CONVERT(Group, group);
735
+ CONVERT(Enum, enum);
736
+ CONVERT(Int32, int32);
737
+ CONVERT(Int64, int64);
738
+ CONVERT(UInt32, uint32);
739
+ CONVERT(UInt64, uint64);
740
+ CONVERT(SInt32, sint32);
741
+ CONVERT(SInt64, sint64);
742
+ CONVERT(Fixed32, fixed32);
743
+ CONVERT(Fixed64, fixed64);
744
+ CONVERT(SFixed32, sfixed32);
745
+ CONVERT(SFixed64, sfixed64);
643
746
  #undef CONVERT
644
747
  }
645
748
  return Qnil;
@@ -657,7 +760,7 @@ static VALUE descriptortype_to_ruby(upb_descriptortype_t type) {
657
760
  */
658
761
  static VALUE FieldDescriptor__type(VALUE _self) {
659
762
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
660
- return descriptortype_to_ruby(upb_fielddef_descriptortype(self->fielddef));
763
+ return descriptortype_to_ruby(upb_FieldDef_Type(self->fielddef));
661
764
  }
662
765
 
663
766
  /*
@@ -668,16 +771,37 @@ static VALUE FieldDescriptor__type(VALUE _self) {
668
771
  */
669
772
  static VALUE FieldDescriptor_default(VALUE _self) {
670
773
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
671
- const upb_fielddef *f = self->fielddef;
672
- upb_msgval default_val = {0};
673
- if (upb_fielddef_issubmsg(f)) {
774
+ const upb_FieldDef* f = self->fielddef;
775
+ upb_MessageValue default_val = upb_MessageValue_Zero();
776
+ if (upb_FieldDef_IsSubMessage(f)) {
674
777
  return Qnil;
675
- } else if (!upb_fielddef_isseq(f)) {
676
- default_val = upb_fielddef_default(f);
778
+ } else if (!upb_FieldDef_IsRepeated(f)) {
779
+ default_val = upb_FieldDef_Default(f);
677
780
  }
678
781
  return Convert_UpbToRuby(default_val, TypeInfo_get(self->fielddef), Qnil);
679
782
  }
680
783
 
784
+ /*
785
+ * call-seq:
786
+ * FieldDescriptor.has_presence? => bool
787
+ *
788
+ * Returns whether this field tracks presence.
789
+ */
790
+ static VALUE FieldDescriptor_has_presence(VALUE _self) {
791
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
792
+ return upb_FieldDef_HasPresence(self->fielddef) ? Qtrue : Qfalse;
793
+ }
794
+
795
+ /*
796
+ * call-seq:
797
+ * FieldDescriptor.is_packed? => bool
798
+ *
799
+ * Returns whether this is a repeated field that uses packed encoding.
800
+ */
801
+ static VALUE FieldDescriptor_is_packed(VALUE _self) {
802
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
803
+ return upb_FieldDef_IsPacked(self->fielddef) ? Qtrue : Qfalse;
804
+ }
681
805
 
682
806
  /*
683
807
  * call-seq:
@@ -687,8 +811,8 @@ static VALUE FieldDescriptor_default(VALUE _self) {
687
811
  */
688
812
  static VALUE FieldDescriptor_json_name(VALUE _self) {
689
813
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
690
- const upb_fielddef *f = self->fielddef;
691
- const char *json_name = upb_fielddef_jsonname(f);
814
+ const upb_FieldDef* f = self->fielddef;
815
+ const char* json_name = upb_FieldDef_JsonName(f);
692
816
  return rb_str_new2(json_name);
693
817
  }
694
818
 
@@ -703,13 +827,14 @@ static VALUE FieldDescriptor_json_name(VALUE _self) {
703
827
  */
704
828
  static VALUE FieldDescriptor_label(VALUE _self) {
705
829
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
706
- switch (upb_fielddef_label(self->fielddef)) {
707
- #define CONVERT(upb, ruby) \
708
- case UPB_LABEL_ ## upb : return ID2SYM(rb_intern( # ruby ));
830
+ switch (upb_FieldDef_Label(self->fielddef)) {
831
+ #define CONVERT(upb, ruby) \
832
+ case kUpb_Label_##upb: \
833
+ return ID2SYM(rb_intern(#ruby));
709
834
 
710
- CONVERT(OPTIONAL, optional);
711
- CONVERT(REQUIRED, required);
712
- CONVERT(REPEATED, repeated);
835
+ CONVERT(Optional, optional);
836
+ CONVERT(Required, required);
837
+ CONVERT(Repeated, repeated);
713
838
 
714
839
  #undef CONVERT
715
840
  }
@@ -725,7 +850,7 @@ static VALUE FieldDescriptor_label(VALUE _self) {
725
850
  */
726
851
  static VALUE FieldDescriptor_number(VALUE _self) {
727
852
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
728
- return INT2NUM(upb_fielddef_number(self->fielddef));
853
+ return INT2NUM(upb_FieldDef_Number(self->fielddef));
729
854
  }
730
855
 
731
856
  /*
@@ -739,13 +864,13 @@ static VALUE FieldDescriptor_number(VALUE _self) {
739
864
  */
740
865
  static VALUE FieldDescriptor_submsg_name(VALUE _self) {
741
866
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
742
- switch (upb_fielddef_type(self->fielddef)) {
743
- case UPB_TYPE_ENUM:
867
+ switch (upb_FieldDef_CType(self->fielddef)) {
868
+ case kUpb_CType_Enum:
744
869
  return rb_str_new2(
745
- upb_enumdef_fullname(upb_fielddef_enumsubdef(self->fielddef)));
746
- case UPB_TYPE_MESSAGE:
870
+ upb_EnumDef_FullName(upb_FieldDef_EnumSubDef(self->fielddef)));
871
+ case kUpb_CType_Message:
747
872
  return rb_str_new2(
748
- upb_msgdef_fullname(upb_fielddef_msgsubdef(self->fielddef)));
873
+ upb_MessageDef_FullName(upb_FieldDef_MessageSubDef(self->fielddef)));
749
874
  default:
750
875
  return Qnil;
751
876
  }
@@ -762,13 +887,13 @@ static VALUE FieldDescriptor_submsg_name(VALUE _self) {
762
887
  */
763
888
  static VALUE FieldDescriptor_subtype(VALUE _self) {
764
889
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
765
- switch (upb_fielddef_type(self->fielddef)) {
766
- case UPB_TYPE_ENUM:
890
+ switch (upb_FieldDef_CType(self->fielddef)) {
891
+ case kUpb_CType_Enum:
767
892
  return get_enumdef_obj(self->descriptor_pool,
768
- upb_fielddef_enumsubdef(self->fielddef));
769
- case UPB_TYPE_MESSAGE:
893
+ upb_FieldDef_EnumSubDef(self->fielddef));
894
+ case kUpb_CType_Message:
770
895
  return get_msgdef_obj(self->descriptor_pool,
771
- upb_fielddef_msgsubdef(self->fielddef));
896
+ upb_FieldDef_MessageSubDef(self->fielddef));
772
897
  default:
773
898
  return Qnil;
774
899
  }
@@ -783,11 +908,11 @@ static VALUE FieldDescriptor_subtype(VALUE _self) {
783
908
  */
784
909
  static VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
785
910
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
786
- const upb_msgdef *m;
911
+ const upb_MessageDef* m;
787
912
 
788
913
  Message_Get(msg_rb, &m);
789
914
 
790
- if (m != upb_fielddef_containingtype(self->fielddef)) {
915
+ if (m != upb_FieldDef_ContainingType(self->fielddef)) {
791
916
  rb_raise(cTypeError, "get method called on wrong message type");
792
917
  }
793
918
 
@@ -803,16 +928,16 @@ static VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
803
928
  */
804
929
  static VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
805
930
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
806
- const upb_msgdef *m;
807
- const upb_msgdef *msg = Message_Get(msg_rb, &m);
931
+ const upb_MessageDef* m;
932
+ const upb_Message* msg = Message_Get(msg_rb, &m);
808
933
 
809
- if (m != upb_fielddef_containingtype(self->fielddef)) {
934
+ if (m != upb_FieldDef_ContainingType(self->fielddef)) {
810
935
  rb_raise(cTypeError, "has method called on wrong message type");
811
- } else if (!upb_fielddef_haspresence(self->fielddef)) {
936
+ } else if (!upb_FieldDef_HasPresence(self->fielddef)) {
812
937
  rb_raise(rb_eArgError, "does not track presence");
813
938
  }
814
939
 
815
- return upb_msg_has(msg, self->fielddef) ? Qtrue : Qfalse;
940
+ return upb_Message_HasFieldByDef(msg, self->fielddef) ? Qtrue : Qfalse;
816
941
  }
817
942
 
818
943
  /*
@@ -823,14 +948,14 @@ static VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
823
948
  */
824
949
  static VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {
825
950
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
826
- const upb_msgdef *m;
827
- upb_msgdef *msg = Message_GetMutable(msg_rb, &m);
951
+ const upb_MessageDef* m;
952
+ upb_Message* msg = Message_GetMutable(msg_rb, &m);
828
953
 
829
- if (m != upb_fielddef_containingtype(self->fielddef)) {
954
+ if (m != upb_FieldDef_ContainingType(self->fielddef)) {
830
955
  rb_raise(cTypeError, "has method called on wrong message type");
831
956
  }
832
957
 
833
- upb_msg_clearfield(msg, self->fielddef);
958
+ upb_Message_ClearFieldByDef(msg, self->fielddef);
834
959
  return Qnil;
835
960
  }
836
961
 
@@ -844,29 +969,70 @@ static VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {
844
969
  */
845
970
  static VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value) {
846
971
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
847
- const upb_msgdef *m;
848
- upb_msgdef *msg = Message_GetMutable(msg_rb, &m);
849
- upb_arena *arena = Arena_get(Message_GetArena(msg_rb));
850
- upb_msgval msgval;
972
+ const upb_MessageDef* m;
973
+ upb_Message* msg = Message_GetMutable(msg_rb, &m);
974
+ upb_Arena* arena = Arena_get(Message_GetArena(msg_rb));
975
+ upb_MessageValue msgval;
851
976
 
852
- if (m != upb_fielddef_containingtype(self->fielddef)) {
977
+ if (m != upb_FieldDef_ContainingType(self->fielddef)) {
853
978
  rb_raise(cTypeError, "set method called on wrong message type");
854
979
  }
855
980
 
856
- msgval = Convert_RubyToUpb(value, upb_fielddef_name(self->fielddef),
981
+ msgval = Convert_RubyToUpb(value, upb_FieldDef_Name(self->fielddef),
857
982
  TypeInfo_get(self->fielddef), arena);
858
- upb_msg_set(msg, self->fielddef, msgval, arena);
983
+ upb_Message_SetFieldByDef(msg, self->fielddef, msgval, arena);
859
984
  return Qnil;
860
985
  }
861
986
 
987
+ /*
988
+ * call-seq:
989
+ * FieldDescriptor.options => options
990
+ *
991
+ * Returns the `FieldOptions` for this `FieldDescriptor`.
992
+ */
993
+ static VALUE FieldDescriptor_options(VALUE _self) {
994
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
995
+ const google_protobuf_FieldOptions* opts =
996
+ upb_FieldDef_Options(self->fielddef);
997
+ upb_Arena* arena = upb_Arena_New();
998
+ size_t size;
999
+ char* serialized = google_protobuf_FieldOptions_serialize(opts, arena, &size);
1000
+ VALUE field_options = decode_options(_self, "FieldOptions", size, serialized,
1001
+ self->descriptor_pool);
1002
+ upb_Arena_Free(arena);
1003
+ return field_options;
1004
+ }
1005
+
1006
+ /*
1007
+ * call-seq:
1008
+ * FieldDescriptor.to_proto => FieldDescriptorProto
1009
+ *
1010
+ * Returns the `FieldDescriptorProto` of this `FieldDescriptor`.
1011
+ */
1012
+ static VALUE FieldDescriptor_to_proto(VALUE _self) {
1013
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
1014
+ upb_Arena* arena = upb_Arena_New();
1015
+ google_protobuf_FieldDescriptorProto* proto =
1016
+ upb_FieldDef_ToProto(self->fielddef, arena);
1017
+ size_t size;
1018
+ const char* serialized =
1019
+ google_protobuf_FieldDescriptorProto_serialize(proto, arena, &size);
1020
+ VALUE proto_class = rb_path2class("Google::Protobuf::FieldDescriptorProto");
1021
+ VALUE proto_rb =
1022
+ Message_decode_bytes(size, serialized, 0, proto_class, false);
1023
+ upb_Arena_Free(arena);
1024
+ return proto_rb;
1025
+ }
1026
+
862
1027
  static void FieldDescriptor_register(VALUE module) {
863
- VALUE klass = rb_define_class_under(
864
- module, "FieldDescriptor", rb_cObject);
1028
+ VALUE klass = rb_define_class_under(module, "FieldDescriptor", rb_cObject);
865
1029
  rb_define_alloc_func(klass, FieldDescriptor_alloc);
866
1030
  rb_define_method(klass, "initialize", FieldDescriptor_initialize, 3);
867
1031
  rb_define_method(klass, "name", FieldDescriptor_name, 0);
868
1032
  rb_define_method(klass, "type", FieldDescriptor__type, 0);
869
1033
  rb_define_method(klass, "default", FieldDescriptor_default, 0);
1034
+ rb_define_method(klass, "has_presence?", FieldDescriptor_has_presence, 0);
1035
+ rb_define_method(klass, "is_packed?", FieldDescriptor_is_packed, 0);
870
1036
  rb_define_method(klass, "json_name", FieldDescriptor_json_name, 0);
871
1037
  rb_define_method(klass, "label", FieldDescriptor_label, 0);
872
1038
  rb_define_method(klass, "number", FieldDescriptor_number, 0);
@@ -876,6 +1042,8 @@ static void FieldDescriptor_register(VALUE module) {
876
1042
  rb_define_method(klass, "clear", FieldDescriptor_clear, 1);
877
1043
  rb_define_method(klass, "get", FieldDescriptor_get, 1);
878
1044
  rb_define_method(klass, "set", FieldDescriptor_set, 2);
1045
+ rb_define_method(klass, "options", FieldDescriptor_options, 0);
1046
+ rb_define_method(klass, "to_proto", FieldDescriptor_to_proto, 0);
879
1047
  rb_gc_register_address(&cFieldDescriptor);
880
1048
  cFieldDescriptor = klass;
881
1049
  }
@@ -885,8 +1053,10 @@ static void FieldDescriptor_register(VALUE module) {
885
1053
  // -----------------------------------------------------------------------------
886
1054
 
887
1055
  typedef struct {
888
- const upb_oneofdef* oneofdef;
889
- VALUE descriptor_pool; // Owns the upb_oneofdef.
1056
+ const upb_OneofDef* oneofdef;
1057
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
1058
+ // macro to update VALUE references, as to trigger write barriers.
1059
+ VALUE descriptor_pool; // Owns the upb_OneofDef.
890
1060
  } OneofDescriptor;
891
1061
 
892
1062
  static VALUE cOneofDescriptor = Qnil;
@@ -899,7 +1069,7 @@ static void OneofDescriptor_mark(void* _self) {
899
1069
  static const rb_data_type_t OneofDescriptor_type = {
900
1070
  "Google::Protobuf::OneofDescriptor",
901
1071
  {OneofDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
902
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
1072
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
903
1073
  };
904
1074
 
905
1075
  static OneofDescriptor* ruby_to_OneofDescriptor(VALUE val) {
@@ -930,7 +1100,7 @@ static VALUE OneofDescriptor_alloc(VALUE klass) {
930
1100
  * Creates a descriptor wrapper object. May only be called from C.
931
1101
  */
932
1102
  static VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
933
- VALUE descriptor_pool, VALUE ptr) {
1103
+ VALUE descriptor_pool, VALUE ptr) {
934
1104
  OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
935
1105
 
936
1106
  if (cookie != c_only_cookie) {
@@ -938,8 +1108,8 @@ static VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
938
1108
  "Descriptor objects may not be created from Ruby.");
939
1109
  }
940
1110
 
941
- self->descriptor_pool = descriptor_pool;
942
- self->oneofdef = (const upb_oneofdef*)NUM2ULL(ptr);
1111
+ RB_OBJ_WRITE(_self, &self->descriptor_pool, descriptor_pool);
1112
+ self->oneofdef = (const upb_OneofDef*)NUM2ULL(ptr);
943
1113
 
944
1114
  return Qnil;
945
1115
  }
@@ -952,7 +1122,7 @@ static VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
952
1122
  */
953
1123
  static VALUE OneofDescriptor_name(VALUE _self) {
954
1124
  OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
955
- return rb_str_maybe_null(upb_oneofdef_name(self->oneofdef));
1125
+ return rb_str_maybe_null(upb_OneofDef_Name(self->oneofdef));
956
1126
  }
957
1127
 
958
1128
  /*
@@ -963,24 +1133,64 @@ static VALUE OneofDescriptor_name(VALUE _self) {
963
1133
  */
964
1134
  static VALUE OneofDescriptor_each(VALUE _self) {
965
1135
  OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
966
- upb_oneof_iter it;
967
- for (upb_oneof_begin(&it, self->oneofdef);
968
- !upb_oneof_done(&it);
969
- upb_oneof_next(&it)) {
970
- const upb_fielddef* f = upb_oneof_iter_field(&it);
1136
+
1137
+ int n = upb_OneofDef_FieldCount(self->oneofdef);
1138
+ for (int i = 0; i < n; i++) {
1139
+ const upb_FieldDef* f = upb_OneofDef_Field(self->oneofdef, i);
971
1140
  VALUE obj = get_fielddef_obj(self->descriptor_pool, f);
972
1141
  rb_yield(obj);
973
1142
  }
974
1143
  return Qnil;
975
1144
  }
976
1145
 
1146
+ /*
1147
+ * call-seq:
1148
+ * OneofDescriptor.options => options
1149
+ *
1150
+ * Returns the `OneofOptions` for this `OneofDescriptor`.
1151
+ */
1152
+ static VALUE OneOfDescriptor_options(VALUE _self) {
1153
+ OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
1154
+ const google_protobuf_OneofOptions* opts =
1155
+ upb_OneofDef_Options(self->oneofdef);
1156
+ upb_Arena* arena = upb_Arena_New();
1157
+ size_t size;
1158
+ char* serialized = google_protobuf_OneofOptions_serialize(opts, arena, &size);
1159
+ VALUE oneof_options = decode_options(_self, "OneofOptions", size, serialized,
1160
+ self->descriptor_pool);
1161
+ upb_Arena_Free(arena);
1162
+ return oneof_options;
1163
+ }
1164
+
1165
+ /*
1166
+ * call-seq:
1167
+ * OneofDescriptor.to_proto => OneofDescriptorProto
1168
+ *
1169
+ * Returns the `OneofDescriptorProto` of this `OneofDescriptor`.
1170
+ */
1171
+ static VALUE OneOfDescriptor_to_proto(VALUE _self) {
1172
+ OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
1173
+ upb_Arena* arena = upb_Arena_New();
1174
+ google_protobuf_OneofDescriptorProto* proto =
1175
+ upb_OneofDef_ToProto(self->oneofdef, arena);
1176
+ size_t size;
1177
+ const char* serialized =
1178
+ google_protobuf_OneofDescriptorProto_serialize(proto, arena, &size);
1179
+ VALUE proto_class = rb_path2class("Google::Protobuf::OneofDescriptorProto");
1180
+ VALUE proto_rb =
1181
+ Message_decode_bytes(size, serialized, 0, proto_class, false);
1182
+ upb_Arena_Free(arena);
1183
+ return proto_rb;
1184
+ }
1185
+
977
1186
  static void OneofDescriptor_register(VALUE module) {
978
- VALUE klass = rb_define_class_under(
979
- module, "OneofDescriptor", rb_cObject);
1187
+ VALUE klass = rb_define_class_under(module, "OneofDescriptor", rb_cObject);
980
1188
  rb_define_alloc_func(klass, OneofDescriptor_alloc);
981
1189
  rb_define_method(klass, "initialize", OneofDescriptor_initialize, 3);
982
1190
  rb_define_method(klass, "name", OneofDescriptor_name, 0);
983
1191
  rb_define_method(klass, "each", OneofDescriptor_each, 0);
1192
+ rb_define_method(klass, "options", OneOfDescriptor_options, 0);
1193
+ rb_define_method(klass, "to_proto", OneOfDescriptor_to_proto, 0);
984
1194
  rb_include_module(klass, rb_mEnumerable);
985
1195
  rb_gc_register_address(&cOneofDescriptor);
986
1196
  cOneofDescriptor = klass;
@@ -991,9 +1201,11 @@ static void OneofDescriptor_register(VALUE module) {
991
1201
  // -----------------------------------------------------------------------------
992
1202
 
993
1203
  typedef struct {
994
- const upb_enumdef* enumdef;
995
- VALUE module; // begins as nil
996
- VALUE descriptor_pool; // Owns the upb_enumdef.
1204
+ const upb_EnumDef* enumdef;
1205
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
1206
+ // macro to update VALUE references, as to trigger write barriers.
1207
+ VALUE module; // begins as nil
1208
+ VALUE descriptor_pool; // Owns the upb_EnumDef.
997
1209
  } EnumDescriptor;
998
1210
 
999
1211
  static VALUE cEnumDescriptor = Qnil;
@@ -1005,9 +1217,9 @@ static void EnumDescriptor_mark(void* _self) {
1005
1217
  }
1006
1218
 
1007
1219
  static const rb_data_type_t EnumDescriptor_type = {
1008
- "Google::Protobuf::EnumDescriptor",
1009
- {EnumDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
1010
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
1220
+ "Google::Protobuf::EnumDescriptor",
1221
+ {EnumDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
1222
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
1011
1223
  };
1012
1224
 
1013
1225
  static EnumDescriptor* ruby_to_EnumDescriptor(VALUE val) {
@@ -1026,8 +1238,8 @@ static VALUE EnumDescriptor_alloc(VALUE klass) {
1026
1238
  }
1027
1239
 
1028
1240
  // Exposed to other modules in defs.h.
1029
- const upb_enumdef *EnumDescriptor_GetEnumDef(VALUE enum_desc_rb) {
1030
- EnumDescriptor *desc = ruby_to_EnumDescriptor(enum_desc_rb);
1241
+ const upb_EnumDef* EnumDescriptor_GetEnumDef(VALUE enum_desc_rb) {
1242
+ EnumDescriptor* desc = ruby_to_EnumDescriptor(enum_desc_rb);
1031
1243
  return desc->enumdef;
1032
1244
  }
1033
1245
 
@@ -1046,8 +1258,8 @@ static VALUE EnumDescriptor_initialize(VALUE _self, VALUE cookie,
1046
1258
  "Descriptor objects may not be created from Ruby.");
1047
1259
  }
1048
1260
 
1049
- self->descriptor_pool = descriptor_pool;
1050
- self->enumdef = (const upb_enumdef*)NUM2ULL(ptr);
1261
+ RB_OBJ_WRITE(_self, &self->descriptor_pool, descriptor_pool);
1262
+ self->enumdef = (const upb_EnumDef*)NUM2ULL(ptr);
1051
1263
 
1052
1264
  return Qnil;
1053
1265
  }
@@ -1061,7 +1273,18 @@ static VALUE EnumDescriptor_initialize(VALUE _self, VALUE cookie,
1061
1273
  static VALUE EnumDescriptor_file_descriptor(VALUE _self) {
1062
1274
  EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1063
1275
  return get_filedef_obj(self->descriptor_pool,
1064
- upb_enumdef_file(self->enumdef));
1276
+ upb_EnumDef_File(self->enumdef));
1277
+ }
1278
+
1279
+ /*
1280
+ * call-seq:
1281
+ * EnumDescriptor.is_closed? => bool
1282
+ *
1283
+ * Returns whether this enum is open or closed.
1284
+ */
1285
+ static VALUE EnumDescriptor_is_closed(VALUE _self) {
1286
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1287
+ return upb_EnumDef_IsClosed(self->enumdef) ? Qtrue : Qfalse;
1065
1288
  }
1066
1289
 
1067
1290
  /*
@@ -1072,7 +1295,7 @@ static VALUE EnumDescriptor_file_descriptor(VALUE _self) {
1072
1295
  */
1073
1296
  static VALUE EnumDescriptor_name(VALUE _self) {
1074
1297
  EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1075
- return rb_str_maybe_null(upb_enumdef_fullname(self->enumdef));
1298
+ return rb_str_maybe_null(upb_EnumDef_FullName(self->enumdef));
1076
1299
  }
1077
1300
 
1078
1301
  /*
@@ -1084,10 +1307,11 @@ static VALUE EnumDescriptor_name(VALUE _self) {
1084
1307
  */
1085
1308
  static VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
1086
1309
  EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1087
- const char* name_str= rb_id2name(SYM2ID(name));
1088
- int32_t val = 0;
1089
- if (upb_enumdef_ntoiz(self->enumdef, name_str, &val)) {
1090
- return INT2NUM(val);
1310
+ const char* name_str = rb_id2name(SYM2ID(name));
1311
+ const upb_EnumValueDef* ev =
1312
+ upb_EnumDef_FindValueByName(self->enumdef, name_str);
1313
+ if (ev) {
1314
+ return INT2NUM(upb_EnumValueDef_Number(ev));
1091
1315
  } else {
1092
1316
  return Qnil;
1093
1317
  }
@@ -1103,9 +1327,10 @@ static VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
1103
1327
  static VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
1104
1328
  EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1105
1329
  int32_t val = NUM2INT(number);
1106
- const char* name = upb_enumdef_iton(self->enumdef, val);
1107
- if (name != NULL) {
1108
- return ID2SYM(rb_intern(name));
1330
+ const upb_EnumValueDef* ev =
1331
+ upb_EnumDef_FindValueByNumber(self->enumdef, val);
1332
+ if (ev) {
1333
+ return ID2SYM(rb_intern(upb_EnumValueDef_Name(ev)));
1109
1334
  } else {
1110
1335
  return Qnil;
1111
1336
  }
@@ -1121,12 +1346,11 @@ static VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
1121
1346
  static VALUE EnumDescriptor_each(VALUE _self) {
1122
1347
  EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1123
1348
 
1124
- upb_enum_iter it;
1125
- for (upb_enum_begin(&it, self->enumdef);
1126
- !upb_enum_done(&it);
1127
- upb_enum_next(&it)) {
1128
- VALUE key = ID2SYM(rb_intern(upb_enum_iter_name(&it)));
1129
- VALUE number = INT2NUM(upb_enum_iter_number(&it));
1349
+ int n = upb_EnumDef_ValueCount(self->enumdef);
1350
+ for (int i = 0; i < n; i++) {
1351
+ const upb_EnumValueDef* ev = upb_EnumDef_Value(self->enumdef, i);
1352
+ VALUE key = ID2SYM(rb_intern(upb_EnumValueDef_Name(ev)));
1353
+ VALUE number = INT2NUM(upb_EnumValueDef_Number(ev));
1130
1354
  rb_yield_values(2, key, number);
1131
1355
  }
1132
1356
 
@@ -1142,14 +1366,54 @@ static VALUE EnumDescriptor_each(VALUE _self) {
1142
1366
  static VALUE EnumDescriptor_enummodule(VALUE _self) {
1143
1367
  EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1144
1368
  if (self->module == Qnil) {
1145
- self->module = build_module_from_enumdesc(_self);
1369
+ RB_OBJ_WRITE(_self, &self->module, build_module_from_enumdesc(_self));
1146
1370
  }
1147
1371
  return self->module;
1148
1372
  }
1149
1373
 
1374
+ /*
1375
+ * call-seq:
1376
+ * EnumDescriptor.options => options
1377
+ *
1378
+ * Returns the `EnumOptions` for this `EnumDescriptor`.
1379
+ */
1380
+ static VALUE EnumDescriptor_options(VALUE _self) {
1381
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1382
+ const google_protobuf_EnumOptions* opts = upb_EnumDef_Options(self->enumdef);
1383
+ upb_Arena* arena = upb_Arena_New();
1384
+ size_t size;
1385
+ char* serialized = google_protobuf_EnumOptions_serialize(opts, arena, &size);
1386
+ VALUE enum_options = decode_options(_self, "EnumOptions", size, serialized,
1387
+ self->descriptor_pool);
1388
+ upb_Arena_Free(arena);
1389
+ return enum_options;
1390
+ }
1391
+
1392
+ /*
1393
+ * call-seq:
1394
+ * EnumDescriptor.to_proto => EnumDescriptorProto
1395
+ *
1396
+ * Returns the `EnumDescriptorProto` of this `EnumDescriptor`.
1397
+ */
1398
+ static VALUE EnumDescriptor_to_proto(VALUE _self) {
1399
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1400
+ upb_Arena* arena = upb_Arena_New();
1401
+ google_protobuf_EnumDescriptorProto* proto =
1402
+ upb_EnumDef_ToProto(self->enumdef, arena);
1403
+
1404
+ size_t size;
1405
+ const char* serialized =
1406
+ google_protobuf_EnumDescriptorProto_serialize(proto, arena, &size);
1407
+
1408
+ VALUE proto_class = rb_path2class("Google::Protobuf::EnumDescriptorProto");
1409
+ VALUE proto_rb =
1410
+ Message_decode_bytes(size, serialized, 0, proto_class, false);
1411
+ upb_Arena_Free(arena);
1412
+ return proto_rb;
1413
+ }
1414
+
1150
1415
  static void EnumDescriptor_register(VALUE module) {
1151
- VALUE klass = rb_define_class_under(
1152
- module, "EnumDescriptor", rb_cObject);
1416
+ VALUE klass = rb_define_class_under(module, "EnumDescriptor", rb_cObject);
1153
1417
  rb_define_alloc_func(klass, EnumDescriptor_alloc);
1154
1418
  rb_define_method(klass, "initialize", EnumDescriptor_initialize, 3);
1155
1419
  rb_define_method(klass, "name", EnumDescriptor_name, 0);
@@ -1158,11 +1422,350 @@ static void EnumDescriptor_register(VALUE module) {
1158
1422
  rb_define_method(klass, "each", EnumDescriptor_each, 0);
1159
1423
  rb_define_method(klass, "enummodule", EnumDescriptor_enummodule, 0);
1160
1424
  rb_define_method(klass, "file_descriptor", EnumDescriptor_file_descriptor, 0);
1425
+ rb_define_method(klass, "is_closed?", EnumDescriptor_is_closed, 0);
1426
+ rb_define_method(klass, "options", EnumDescriptor_options, 0);
1427
+ rb_define_method(klass, "to_proto", EnumDescriptor_to_proto, 0);
1161
1428
  rb_include_module(klass, rb_mEnumerable);
1162
1429
  rb_gc_register_address(&cEnumDescriptor);
1163
1430
  cEnumDescriptor = klass;
1164
1431
  }
1165
1432
 
1433
+ // -----------------------------------------------------------------------------
1434
+ // ServiceDescriptor
1435
+ // -----------------------------------------------------------------------------
1436
+
1437
+ typedef struct {
1438
+ const upb_ServiceDef* servicedef;
1439
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
1440
+ // macro to update VALUE references, as to trigger write barriers.
1441
+ VALUE module; // begins as nil
1442
+ VALUE descriptor_pool; // Owns the upb_ServiceDef.
1443
+ } ServiceDescriptor;
1444
+
1445
+ static VALUE cServiceDescriptor = Qnil;
1446
+
1447
+ static void ServiceDescriptor_mark(void* _self) {
1448
+ ServiceDescriptor* self = _self;
1449
+ rb_gc_mark(self->module);
1450
+ rb_gc_mark(self->descriptor_pool);
1451
+ }
1452
+
1453
+ static const rb_data_type_t ServiceDescriptor_type = {
1454
+ "Google::Protobuf::ServicDescriptor",
1455
+ {ServiceDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
1456
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
1457
+ };
1458
+
1459
+ static ServiceDescriptor* ruby_to_ServiceDescriptor(VALUE val) {
1460
+ ServiceDescriptor* ret;
1461
+ TypedData_Get_Struct(val, ServiceDescriptor, &ServiceDescriptor_type, ret);
1462
+ return ret;
1463
+ }
1464
+
1465
+ static VALUE ServiceDescriptor_alloc(VALUE klass) {
1466
+ ServiceDescriptor* self = ALLOC(ServiceDescriptor);
1467
+ VALUE ret = TypedData_Wrap_Struct(klass, &ServiceDescriptor_type, self);
1468
+ self->servicedef = NULL;
1469
+ self->module = Qnil;
1470
+ self->descriptor_pool = Qnil;
1471
+ return ret;
1472
+ }
1473
+
1474
+ /*
1475
+ * call-seq:
1476
+ * ServiceDescriptor.new(c_only_cookie, ptr) => ServiceDescriptor
1477
+ *
1478
+ * Creates a descriptor wrapper object. May only be called from C.
1479
+ */
1480
+ static VALUE ServiceDescriptor_initialize(VALUE _self, VALUE cookie,
1481
+ VALUE descriptor_pool, VALUE ptr) {
1482
+ ServiceDescriptor* self = ruby_to_ServiceDescriptor(_self);
1483
+
1484
+ if (cookie != c_only_cookie) {
1485
+ rb_raise(rb_eRuntimeError,
1486
+ "Descriptor objects may not be created from Ruby.");
1487
+ }
1488
+
1489
+ RB_OBJ_WRITE(_self, &self->descriptor_pool, descriptor_pool);
1490
+ self->servicedef = (const upb_ServiceDef*)NUM2ULL(ptr);
1491
+
1492
+ return Qnil;
1493
+ }
1494
+
1495
+ /*
1496
+ * call-seq:
1497
+ * ServiceDescriptor.name => name
1498
+ *
1499
+ * Returns the name of this service.
1500
+ */
1501
+ static VALUE ServiceDescriptor_name(VALUE _self) {
1502
+ ServiceDescriptor* self = ruby_to_ServiceDescriptor(_self);
1503
+ return rb_str_maybe_null(upb_ServiceDef_FullName(self->servicedef));
1504
+ }
1505
+
1506
+ /*
1507
+ * call-seq:
1508
+ * ServiceDescriptor.file_descriptor
1509
+ *
1510
+ * Returns the FileDescriptor object this service belongs to.
1511
+ */
1512
+ static VALUE ServiceDescriptor_file_descriptor(VALUE _self) {
1513
+ ServiceDescriptor* self = ruby_to_ServiceDescriptor(_self);
1514
+ return get_filedef_obj(self->descriptor_pool,
1515
+ upb_ServiceDef_File(self->servicedef));
1516
+ }
1517
+
1518
+ /*
1519
+ * call-seq:
1520
+ * ServiceDescriptor.each(&block)
1521
+ *
1522
+ * Iterates over methods in this service, yielding to the block on each one.
1523
+ */
1524
+ static VALUE ServiceDescriptor_each(VALUE _self) {
1525
+ ServiceDescriptor* self = ruby_to_ServiceDescriptor(_self);
1526
+
1527
+ int n = upb_ServiceDef_MethodCount(self->servicedef);
1528
+ for (int i = 0; i < n; i++) {
1529
+ const upb_MethodDef* method = upb_ServiceDef_Method(self->servicedef, i);
1530
+ VALUE obj = get_methoddef_obj(self->descriptor_pool, method);
1531
+ rb_yield(obj);
1532
+ }
1533
+ return Qnil;
1534
+ }
1535
+
1536
+ /*
1537
+ * call-seq:
1538
+ * ServiceDescriptor.options => options
1539
+ *
1540
+ * Returns the `ServiceOptions` for this `ServiceDescriptor`.
1541
+ */
1542
+ static VALUE ServiceDescriptor_options(VALUE _self) {
1543
+ ServiceDescriptor* self = ruby_to_ServiceDescriptor(_self);
1544
+ const google_protobuf_ServiceOptions* opts =
1545
+ upb_ServiceDef_Options(self->servicedef);
1546
+ upb_Arena* arena = upb_Arena_New();
1547
+ size_t size;
1548
+ char* serialized =
1549
+ google_protobuf_ServiceOptions_serialize(opts, arena, &size);
1550
+ VALUE service_options = decode_options(_self, "ServiceOptions", size,
1551
+ serialized, self->descriptor_pool);
1552
+ upb_Arena_Free(arena);
1553
+ return service_options;
1554
+ }
1555
+
1556
+ /*
1557
+ * call-seq:
1558
+ * ServiceDescriptor.to_proto => ServiceDescriptorProto
1559
+ *
1560
+ * Returns the `ServiceDescriptorProto` of this `ServiceDescriptor`.
1561
+ */
1562
+ static VALUE ServiceDescriptor_to_proto(VALUE _self) {
1563
+ ServiceDescriptor* self = ruby_to_ServiceDescriptor(_self);
1564
+ upb_Arena* arena = upb_Arena_New();
1565
+ google_protobuf_ServiceDescriptorProto* proto =
1566
+ upb_ServiceDef_ToProto(self->servicedef, arena);
1567
+ size_t size;
1568
+ const char* serialized =
1569
+ google_protobuf_ServiceDescriptorProto_serialize(proto, arena, &size);
1570
+ VALUE proto_class = rb_path2class("Google::Protobuf::ServiceDescriptorProto");
1571
+ VALUE proto_rb =
1572
+ Message_decode_bytes(size, serialized, 0, proto_class, false);
1573
+ upb_Arena_Free(arena);
1574
+ return proto_rb;
1575
+ }
1576
+
1577
+ static void ServiceDescriptor_register(VALUE module) {
1578
+ VALUE klass = rb_define_class_under(module, "ServiceDescriptor", rb_cObject);
1579
+ rb_define_alloc_func(klass, ServiceDescriptor_alloc);
1580
+ rb_define_method(klass, "initialize", ServiceDescriptor_initialize, 3);
1581
+ rb_define_method(klass, "name", ServiceDescriptor_name, 0);
1582
+ rb_define_method(klass, "each", ServiceDescriptor_each, 0);
1583
+ rb_define_method(klass, "file_descriptor", ServiceDescriptor_file_descriptor,
1584
+ 0);
1585
+ rb_define_method(klass, "options", ServiceDescriptor_options, 0);
1586
+ rb_define_method(klass, "to_proto", ServiceDescriptor_to_proto, 0);
1587
+ rb_include_module(klass, rb_mEnumerable);
1588
+ rb_gc_register_address(&cServiceDescriptor);
1589
+ cServiceDescriptor = klass;
1590
+ }
1591
+
1592
+ // -----------------------------------------------------------------------------
1593
+ // MethodDescriptor
1594
+ // -----------------------------------------------------------------------------
1595
+
1596
+ typedef struct {
1597
+ const upb_MethodDef* methoddef;
1598
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
1599
+ // macro to update VALUE references, as to trigger write barriers.
1600
+ VALUE module; // begins as nil
1601
+ VALUE descriptor_pool; // Owns the upb_MethodDef.
1602
+ } MethodDescriptor;
1603
+
1604
+ static VALUE cMethodDescriptor = Qnil;
1605
+
1606
+ static void MethodDescriptor_mark(void* _self) {
1607
+ MethodDescriptor* self = _self;
1608
+ rb_gc_mark(self->module);
1609
+ rb_gc_mark(self->descriptor_pool);
1610
+ }
1611
+
1612
+ static const rb_data_type_t MethodDescriptor_type = {
1613
+ "Google::Protobuf::MethodDescriptor",
1614
+ {MethodDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
1615
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
1616
+ };
1617
+
1618
+ static MethodDescriptor* ruby_to_MethodDescriptor(VALUE val) {
1619
+ MethodDescriptor* ret;
1620
+ TypedData_Get_Struct(val, MethodDescriptor, &MethodDescriptor_type, ret);
1621
+ return ret;
1622
+ }
1623
+
1624
+ static VALUE MethodDescriptor_alloc(VALUE klass) {
1625
+ MethodDescriptor* self = ALLOC(MethodDescriptor);
1626
+ VALUE ret = TypedData_Wrap_Struct(klass, &MethodDescriptor_type, self);
1627
+ self->methoddef = NULL;
1628
+ self->module = Qnil;
1629
+ self->descriptor_pool = Qnil;
1630
+ return ret;
1631
+ }
1632
+
1633
+ /*
1634
+ * call-seq:
1635
+ * MethodDescriptor.new(c_only_cookie, ptr) => MethodDescriptor
1636
+ *
1637
+ * Creates a descriptor wrapper object. May only be called from C.
1638
+ */
1639
+ static VALUE MethodDescriptor_initialize(VALUE _self, VALUE cookie,
1640
+ VALUE descriptor_pool, VALUE ptr) {
1641
+ MethodDescriptor* self = ruby_to_MethodDescriptor(_self);
1642
+
1643
+ if (cookie != c_only_cookie) {
1644
+ rb_raise(rb_eRuntimeError,
1645
+ "Descriptor objects may not be created from Ruby.");
1646
+ }
1647
+
1648
+ RB_OBJ_WRITE(_self, &self->descriptor_pool, descriptor_pool);
1649
+ self->methoddef = (const upb_MethodDef*)NUM2ULL(ptr);
1650
+
1651
+ return Qnil;
1652
+ }
1653
+
1654
+ /*
1655
+ * call-seq:
1656
+ * MethodDescriptor.name => name
1657
+ *
1658
+ * Returns the name of this method
1659
+ */
1660
+ static VALUE MethodDescriptor_name(VALUE _self) {
1661
+ MethodDescriptor* self = ruby_to_MethodDescriptor(_self);
1662
+ return rb_str_maybe_null(upb_MethodDef_Name(self->methoddef));
1663
+ }
1664
+
1665
+ /*
1666
+ * call-seq:
1667
+ * MethodDescriptor.options => options
1668
+ *
1669
+ * Returns the `MethodOptions` for this `MethodDescriptor`.
1670
+ */
1671
+ static VALUE MethodDescriptor_options(VALUE _self) {
1672
+ MethodDescriptor* self = ruby_to_MethodDescriptor(_self);
1673
+ const google_protobuf_MethodOptions* opts =
1674
+ upb_MethodDef_Options(self->methoddef);
1675
+ upb_Arena* arena = upb_Arena_New();
1676
+ size_t size;
1677
+ char* serialized =
1678
+ google_protobuf_MethodOptions_serialize(opts, arena, &size);
1679
+ VALUE method_options = decode_options(_self, "MethodOptions", size,
1680
+ serialized, self->descriptor_pool);
1681
+ upb_Arena_Free(arena);
1682
+ return method_options;
1683
+ }
1684
+
1685
+ /*
1686
+ * call-seq:
1687
+ * MethodDescriptor.input_type => Descriptor
1688
+ *
1689
+ * Returns the `Descriptor` for the request message type of this method
1690
+ */
1691
+ static VALUE MethodDescriptor_input_type(VALUE _self) {
1692
+ MethodDescriptor* self = ruby_to_MethodDescriptor(_self);
1693
+ const upb_MessageDef* type = upb_MethodDef_InputType(self->methoddef);
1694
+ return get_msgdef_obj(self->descriptor_pool, type);
1695
+ }
1696
+
1697
+ /*
1698
+ * call-seq:
1699
+ * MethodDescriptor.output_type => Descriptor
1700
+ *
1701
+ * Returns the `Descriptor` for the response message type of this method
1702
+ */
1703
+ static VALUE MethodDescriptor_output_type(VALUE _self) {
1704
+ MethodDescriptor* self = ruby_to_MethodDescriptor(_self);
1705
+ const upb_MessageDef* type = upb_MethodDef_OutputType(self->methoddef);
1706
+ return get_msgdef_obj(self->descriptor_pool, type);
1707
+ }
1708
+
1709
+ /*
1710
+ * call-seq:
1711
+ * MethodDescriptor.client_streaming => bool
1712
+ *
1713
+ * Returns whether or not this is a streaming request method
1714
+ */
1715
+ static VALUE MethodDescriptor_client_streaming(VALUE _self) {
1716
+ MethodDescriptor* self = ruby_to_MethodDescriptor(_self);
1717
+ return upb_MethodDef_ClientStreaming(self->methoddef) ? Qtrue : Qfalse;
1718
+ }
1719
+
1720
+ /*
1721
+ * call-seq:
1722
+ * MethodDescriptor.to_proto => MethodDescriptorProto
1723
+ *
1724
+ * Returns the `MethodDescriptorProto` of this `MethodDescriptor`.
1725
+ */
1726
+ static VALUE MethodDescriptor_to_proto(VALUE _self) {
1727
+ MethodDescriptor* self = ruby_to_MethodDescriptor(_self);
1728
+ upb_Arena* arena = upb_Arena_New();
1729
+ google_protobuf_MethodDescriptorProto* proto =
1730
+ upb_MethodDef_ToProto(self->methoddef, arena);
1731
+ size_t size;
1732
+ const char* serialized =
1733
+ google_protobuf_MethodDescriptorProto_serialize(proto, arena, &size);
1734
+ VALUE proto_class = rb_path2class("Google::Protobuf::MethodDescriptorProto");
1735
+ VALUE proto_rb =
1736
+ Message_decode_bytes(size, serialized, 0, proto_class, false);
1737
+ upb_Arena_Free(arena);
1738
+ return proto_rb;
1739
+ }
1740
+
1741
+ /*
1742
+ * call-seq:
1743
+ * MethodDescriptor.server_streaming => bool
1744
+ *
1745
+ * Returns whether or not this is a streaming response method
1746
+ */
1747
+ static VALUE MethodDescriptor_server_streaming(VALUE _self) {
1748
+ MethodDescriptor* self = ruby_to_MethodDescriptor(_self);
1749
+ return upb_MethodDef_ServerStreaming(self->methoddef) ? Qtrue : Qfalse;
1750
+ }
1751
+
1752
+ static void MethodDescriptor_register(VALUE module) {
1753
+ VALUE klass = rb_define_class_under(module, "MethodDescriptor", rb_cObject);
1754
+ rb_define_alloc_func(klass, MethodDescriptor_alloc);
1755
+ rb_define_method(klass, "initialize", MethodDescriptor_initialize, 3);
1756
+ rb_define_method(klass, "name", MethodDescriptor_name, 0);
1757
+ rb_define_method(klass, "options", MethodDescriptor_options, 0);
1758
+ rb_define_method(klass, "input_type", MethodDescriptor_input_type, 0);
1759
+ rb_define_method(klass, "output_type", MethodDescriptor_output_type, 0);
1760
+ rb_define_method(klass, "client_streaming", MethodDescriptor_client_streaming,
1761
+ 0);
1762
+ rb_define_method(klass, "server_streaming", MethodDescriptor_server_streaming,
1763
+ 0);
1764
+ rb_define_method(klass, "to_proto", MethodDescriptor_to_proto, 0);
1765
+ rb_gc_register_address(&cMethodDescriptor);
1766
+ cMethodDescriptor = klass;
1767
+ }
1768
+
1166
1769
  static VALUE get_def_obj(VALUE _descriptor_pool, const void* ptr, VALUE klass) {
1167
1770
  DescriptorPool* descriptor_pool = ruby_to_DescriptorPool(_descriptor_pool);
1168
1771
  VALUE key = ULL2NUM((intptr_t)ptr);
@@ -1176,7 +1779,7 @@ static VALUE get_def_obj(VALUE _descriptor_pool, const void* ptr, VALUE klass) {
1176
1779
 
1177
1780
  if (def == Qnil) {
1178
1781
  // Lazily create wrapper object.
1179
- VALUE args[3] = { c_only_cookie, _descriptor_pool, key };
1782
+ VALUE args[3] = {c_only_cookie, _descriptor_pool, key};
1180
1783
  def = rb_class_new_instance(3, args, klass);
1181
1784
  rb_hash_aset(descriptor_pool->def_to_descriptor, key, def);
1182
1785
  }
@@ -1184,34 +1787,44 @@ static VALUE get_def_obj(VALUE _descriptor_pool, const void* ptr, VALUE klass) {
1184
1787
  return def;
1185
1788
  }
1186
1789
 
1187
- static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_msgdef* def) {
1790
+ static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_MessageDef* def) {
1188
1791
  return get_def_obj(descriptor_pool, def, cDescriptor);
1189
1792
  }
1190
1793
 
1191
- static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_enumdef* def) {
1794
+ static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_EnumDef* def) {
1192
1795
  return get_def_obj(descriptor_pool, def, cEnumDescriptor);
1193
1796
  }
1194
1797
 
1195
- static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_fielddef* def) {
1798
+ static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_FieldDef* def) {
1196
1799
  return get_def_obj(descriptor_pool, def, cFieldDescriptor);
1197
1800
  }
1198
1801
 
1199
- static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_filedef* def) {
1802
+ static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_FileDef* def) {
1200
1803
  return get_def_obj(descriptor_pool, def, cFileDescriptor);
1201
1804
  }
1202
1805
 
1203
- static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_oneofdef* def) {
1806
+ static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_OneofDef* def) {
1204
1807
  return get_def_obj(descriptor_pool, def, cOneofDescriptor);
1205
1808
  }
1206
1809
 
1810
+ static VALUE get_servicedef_obj(VALUE descriptor_pool,
1811
+ const upb_ServiceDef* def) {
1812
+ return get_def_obj(descriptor_pool, def, cServiceDescriptor);
1813
+ }
1814
+
1815
+ static VALUE get_methoddef_obj(VALUE descriptor_pool,
1816
+ const upb_MethodDef* def) {
1817
+ return get_def_obj(descriptor_pool, def, cMethodDescriptor);
1818
+ }
1819
+
1207
1820
  // -----------------------------------------------------------------------------
1208
1821
  // Shared functions
1209
1822
  // -----------------------------------------------------------------------------
1210
1823
 
1211
1824
  // Functions exposed to other modules in defs.h.
1212
1825
 
1213
- VALUE Descriptor_DefToClass(const upb_msgdef *m) {
1214
- const upb_symtab *symtab = upb_filedef_symtab(upb_msgdef_file(m));
1826
+ VALUE Descriptor_DefToClass(const upb_MessageDef* m) {
1827
+ const upb_DefPool* symtab = upb_FileDef_Pool(upb_MessageDef_File(m));
1215
1828
  VALUE pool = ObjectCache_Get(symtab);
1216
1829
  PBRUBY_ASSERT(pool != Qnil);
1217
1830
  VALUE desc_rb = get_msgdef_obj(pool, m);
@@ -1219,15 +1832,16 @@ VALUE Descriptor_DefToClass(const upb_msgdef *m) {
1219
1832
  return desc->klass;
1220
1833
  }
1221
1834
 
1222
- const upb_msgdef *Descriptor_GetMsgDef(VALUE desc_rb) {
1835
+ const upb_MessageDef* Descriptor_GetMsgDef(VALUE desc_rb) {
1223
1836
  const Descriptor* desc = ruby_to_Descriptor(desc_rb);
1224
1837
  return desc->msgdef;
1225
1838
  }
1226
1839
 
1227
- VALUE TypeInfo_InitArg(int argc, VALUE *argv, int skip_arg) {
1840
+ VALUE TypeInfo_InitArg(int argc, VALUE* argv, int skip_arg) {
1228
1841
  if (argc > skip_arg) {
1229
1842
  if (argc > 1 + skip_arg) {
1230
- rb_raise(rb_eArgError, "Expected a maximum of %d arguments.", skip_arg + 1);
1843
+ rb_raise(rb_eArgError, "Expected a maximum of %d arguments.",
1844
+ skip_arg + 1);
1231
1845
  }
1232
1846
  return argv[skip_arg];
1233
1847
  } else {
@@ -1239,7 +1853,7 @@ TypeInfo TypeInfo_FromClass(int argc, VALUE* argv, int skip_arg,
1239
1853
  VALUE* type_class, VALUE* init_arg) {
1240
1854
  TypeInfo ret = {ruby_to_fieldtype(argv[skip_arg])};
1241
1855
 
1242
- if (ret.type == UPB_TYPE_MESSAGE || ret.type == UPB_TYPE_ENUM) {
1856
+ if (ret.type == kUpb_CType_Message || ret.type == kUpb_CType_Enum) {
1243
1857
  *init_arg = TypeInfo_InitArg(argc, argv, skip_arg + 2);
1244
1858
 
1245
1859
  if (argc < 2 + skip_arg) {
@@ -1257,11 +1871,11 @@ TypeInfo TypeInfo_FromClass(int argc, VALUE* argv, int skip_arg,
1257
1871
  "class or enum as returned by the DescriptorPool.");
1258
1872
  }
1259
1873
 
1260
- if (ret.type == UPB_TYPE_MESSAGE) {
1874
+ if (ret.type == kUpb_CType_Message) {
1261
1875
  ret.def.msgdef = ruby_to_Descriptor(desc)->msgdef;
1262
1876
  Message_CheckClass(klass);
1263
1877
  } else {
1264
- PBRUBY_ASSERT(ret.type == UPB_TYPE_ENUM);
1878
+ PBRUBY_ASSERT(ret.type == kUpb_CType_Enum);
1265
1879
  ret.def.enumdef = ruby_to_EnumDescriptor(desc)->enumdef;
1266
1880
  }
1267
1881
  } else {
@@ -1278,6 +1892,8 @@ void Defs_register(VALUE module) {
1278
1892
  FieldDescriptor_register(module);
1279
1893
  OneofDescriptor_register(module);
1280
1894
  EnumDescriptor_register(module);
1895
+ ServiceDescriptor_register(module);
1896
+ MethodDescriptor_register(module);
1281
1897
 
1282
1898
  rb_gc_register_address(&c_only_cookie);
1283
1899
  c_only_cookie = rb_class_new_instance(0, NULL, rb_cObject);