google-protobuf 3.19.4 → 3.25.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/ext/google/protobuf_c/Rakefile +3 -0
  3. data/ext/google/protobuf_c/convert.c +121 -155
  4. data/ext/google/protobuf_c/convert.h +15 -37
  5. data/ext/google/protobuf_c/defs.c +223 -239
  6. data/ext/google/protobuf_c/defs.h +22 -47
  7. data/ext/google/protobuf_c/extconf.rb +13 -5
  8. data/ext/google/protobuf_c/glue.c +21 -0
  9. data/ext/google/protobuf_c/map.c +109 -137
  10. data/ext/google/protobuf_c/map.h +10 -36
  11. data/ext/google/protobuf_c/message.c +445 -386
  12. data/ext/google/protobuf_c/message.h +25 -47
  13. data/ext/google/protobuf_c/protobuf.c +101 -228
  14. data/ext/google/protobuf_c/protobuf.h +37 -42
  15. data/ext/google/protobuf_c/repeated_field.c +91 -113
  16. data/ext/google/protobuf_c/repeated_field.h +9 -34
  17. data/ext/google/protobuf_c/ruby-upb.c +12236 -6993
  18. data/ext/google/protobuf_c/ruby-upb.h +12127 -3787
  19. data/ext/google/protobuf_c/shared_convert.c +64 -0
  20. data/ext/google/protobuf_c/shared_convert.h +26 -0
  21. data/ext/google/protobuf_c/shared_message.c +65 -0
  22. data/ext/google/protobuf_c/shared_message.h +25 -0
  23. data/ext/google/protobuf_c/third_party/utf8_range/LICENSE +22 -0
  24. data/ext/google/protobuf_c/third_party/utf8_range/naive.c +92 -0
  25. data/ext/google/protobuf_c/third_party/utf8_range/range2-neon.c +157 -0
  26. data/ext/google/protobuf_c/third_party/utf8_range/range2-sse.c +170 -0
  27. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.h +21 -0
  28. data/ext/google/protobuf_c/wrap_memcpy.c +7 -29
  29. data/lib/google/protobuf/any_pb.rb +24 -5
  30. data/lib/google/protobuf/api_pb.rb +26 -23
  31. data/lib/google/protobuf/descriptor_dsl.rb +8 -1
  32. data/lib/google/protobuf/descriptor_pb.rb +43 -225
  33. data/lib/google/protobuf/duration_pb.rb +24 -5
  34. data/lib/google/protobuf/empty_pb.rb +24 -3
  35. data/lib/google/protobuf/ffi/descriptor.rb +154 -0
  36. data/lib/google/protobuf/ffi/descriptor_pool.rb +70 -0
  37. data/lib/google/protobuf/ffi/enum_descriptor.rb +161 -0
  38. data/lib/google/protobuf/ffi/ffi.rb +213 -0
  39. data/lib/google/protobuf/ffi/field_descriptor.rb +309 -0
  40. data/lib/google/protobuf/ffi/file_descriptor.rb +48 -0
  41. data/lib/google/protobuf/ffi/internal/arena.rb +66 -0
  42. data/lib/google/protobuf/ffi/internal/convert.rb +305 -0
  43. data/lib/google/protobuf/ffi/internal/pointer_helper.rb +35 -0
  44. data/lib/google/protobuf/ffi/internal/type_safety.rb +25 -0
  45. data/lib/google/protobuf/ffi/map.rb +396 -0
  46. data/lib/google/protobuf/ffi/message.rb +641 -0
  47. data/lib/google/protobuf/ffi/object_cache.rb +30 -0
  48. data/lib/google/protobuf/ffi/oneof_descriptor.rb +88 -0
  49. data/lib/google/protobuf/ffi/repeated_field.rb +503 -0
  50. data/lib/google/protobuf/field_mask_pb.rb +24 -4
  51. data/lib/google/protobuf/message_exts.rb +10 -28
  52. data/lib/google/protobuf/object_cache.rb +97 -0
  53. data/lib/google/protobuf/plugin_pb.rb +47 -0
  54. data/lib/google/protobuf/repeated_field.rb +18 -28
  55. data/lib/google/protobuf/source_context_pb.rb +24 -4
  56. data/lib/google/protobuf/struct_pb.rb +24 -20
  57. data/lib/google/protobuf/timestamp_pb.rb +24 -5
  58. data/lib/google/protobuf/type_pb.rb +26 -68
  59. data/lib/google/protobuf/well_known_types.rb +16 -40
  60. data/lib/google/protobuf/wrappers_pb.rb +24 -28
  61. data/lib/google/protobuf.rb +32 -50
  62. data/lib/google/protobuf_ffi.rb +50 -0
  63. data/lib/google/protobuf_native.rb +20 -0
  64. data/lib/google/tasks/ffi.rake +102 -0
  65. metadata +82 -14
  66. data/tests/basic.rb +0 -648
  67. data/tests/generated_code_test.rb +0 -23
  68. data/tests/stress.rb +0 -38
@@ -1,32 +1,9 @@
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>
@@ -41,11 +18,11 @@
41
18
  // instances.
42
19
  // -----------------------------------------------------------------------------
43
20
 
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);
21
+ static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_MessageDef* def);
22
+ static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_EnumDef* def);
23
+ static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_FieldDef* def);
24
+ static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_FileDef* def);
25
+ static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_OneofDef* def);
49
26
 
50
27
  // A distinct object that is not accessible from Ruby. We use this as a
51
28
  // constructor argument to enforce that certain objects cannot be created from
@@ -73,8 +50,10 @@ static VALUE rb_str_maybe_null(const char* s) {
73
50
  // -----------------------------------------------------------------------------
74
51
 
75
52
  typedef struct {
53
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
54
+ // macro to update VALUE references, as to trigger write barriers.
76
55
  VALUE def_to_descriptor; // Hash table of def* -> Ruby descriptor.
77
- upb_symtab* symtab;
56
+ upb_DefPool* symtab;
78
57
  } DescriptorPool;
79
58
 
80
59
  VALUE cDescriptorPool = Qnil;
@@ -90,14 +69,14 @@ static void DescriptorPool_mark(void* _self) {
90
69
 
91
70
  static void DescriptorPool_free(void* _self) {
92
71
  DescriptorPool* self = _self;
93
- upb_symtab_free(self->symtab);
72
+ upb_DefPool_Free(self->symtab);
94
73
  xfree(self);
95
74
  }
96
75
 
97
76
  static const rb_data_type_t DescriptorPool_type = {
98
- "Google::Protobuf::DescriptorPool",
99
- {DescriptorPool_mark, DescriptorPool_free, NULL},
100
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
77
+ "Google::Protobuf::DescriptorPool",
78
+ {DescriptorPool_mark, DescriptorPool_free, NULL},
79
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
101
80
  };
102
81
 
103
82
  static DescriptorPool* ruby_to_DescriptorPool(VALUE val) {
@@ -107,8 +86,8 @@ static DescriptorPool* ruby_to_DescriptorPool(VALUE val) {
107
86
  }
108
87
 
109
88
  // 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);
89
+ const upb_DefPool* DescriptorPool_GetSymtab(VALUE desc_pool_rb) {
90
+ DescriptorPool* pool = ruby_to_DescriptorPool(desc_pool_rb);
112
91
  return pool->symtab;
113
92
  }
114
93
 
@@ -125,11 +104,9 @@ static VALUE DescriptorPool_alloc(VALUE klass) {
125
104
  self->def_to_descriptor = Qnil;
126
105
  ret = TypedData_Wrap_Struct(klass, &DescriptorPool_type, self);
127
106
 
128
- self->def_to_descriptor = rb_hash_new();
129
- self->symtab = upb_symtab_new();
130
- ObjectCache_Add(self->symtab, ret);
131
-
132
- return ret;
107
+ RB_OBJ_WRITE(ret, &self->def_to_descriptor, rb_hash_new());
108
+ self->symtab = upb_DefPool_New();
109
+ return ObjectCache_TryAdd(self->symtab, ret);
133
110
  }
134
111
 
135
112
  /*
@@ -143,7 +120,7 @@ VALUE DescriptorPool_add_serialized_file(VALUE _self,
143
120
  DescriptorPool* self = ruby_to_DescriptorPool(_self);
144
121
  Check_Type(serialized_file_proto, T_STRING);
145
122
  VALUE arena_rb = Arena_new();
146
- upb_arena *arena = Arena_get(arena_rb);
123
+ upb_Arena* arena = Arena_get(arena_rb);
147
124
  google_protobuf_FileDescriptorProto* file_proto =
148
125
  google_protobuf_FileDescriptorProto_parse(
149
126
  RSTRING_PTR(serialized_file_proto),
@@ -151,14 +128,15 @@ VALUE DescriptorPool_add_serialized_file(VALUE _self,
151
128
  if (!file_proto) {
152
129
  rb_raise(rb_eArgError, "Unable to parse FileDescriptorProto");
153
130
  }
154
- upb_status status;
155
- upb_status_clear(&status);
156
- const upb_filedef* filedef =
157
- upb_symtab_addfile(self->symtab, file_proto, &status);
131
+ upb_Status status;
132
+ upb_Status_Clear(&status);
133
+ const upb_FileDef* filedef =
134
+ upb_DefPool_AddFile(self->symtab, file_proto, &status);
158
135
  if (!filedef) {
159
136
  rb_raise(cTypeError, "Unable to build file to DescriptorPool: %s",
160
- upb_status_errmsg(&status));
137
+ upb_Status_ErrorMessage(&status));
161
138
  }
139
+ RB_GC_GUARD(arena_rb);
162
140
  return get_filedef_obj(_self, filedef);
163
141
  }
164
142
 
@@ -172,15 +150,15 @@ VALUE DescriptorPool_add_serialized_file(VALUE _self,
172
150
  static VALUE DescriptorPool_lookup(VALUE _self, VALUE name) {
173
151
  DescriptorPool* self = ruby_to_DescriptorPool(_self);
174
152
  const char* name_str = get_str(name);
175
- const upb_msgdef* msgdef;
176
- const upb_enumdef* enumdef;
153
+ const upb_MessageDef* msgdef;
154
+ const upb_EnumDef* enumdef;
177
155
 
178
- msgdef = upb_symtab_lookupmsg(self->symtab, name_str);
156
+ msgdef = upb_DefPool_FindMessageByName(self->symtab, name_str);
179
157
  if (msgdef) {
180
158
  return get_msgdef_obj(_self, msgdef);
181
159
  }
182
160
 
183
- enumdef = upb_symtab_lookupenum(self->symtab, name_str);
161
+ enumdef = upb_DefPool_FindEnumByName(self->symtab, name_str);
184
162
  if (enumdef) {
185
163
  return get_enumdef_obj(_self, enumdef);
186
164
  }
@@ -202,8 +180,7 @@ static VALUE DescriptorPool_generated_pool(VALUE _self) {
202
180
  }
203
181
 
204
182
  static void DescriptorPool_register(VALUE module) {
205
- VALUE klass = rb_define_class_under(
206
- module, "DescriptorPool", rb_cObject);
183
+ VALUE klass = rb_define_class_under(module, "DescriptorPool", rb_cObject);
207
184
  rb_define_alloc_func(klass, DescriptorPool_alloc);
208
185
  rb_define_method(klass, "add_serialized_file",
209
186
  DescriptorPool_add_serialized_file, 1);
@@ -222,7 +199,9 @@ static void DescriptorPool_register(VALUE module) {
222
199
  // -----------------------------------------------------------------------------
223
200
 
224
201
  typedef struct {
225
- const upb_msgdef* msgdef;
202
+ const upb_MessageDef* msgdef;
203
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
204
+ // macro to update VALUE references, as to trigger write barriers.
226
205
  VALUE klass;
227
206
  VALUE descriptor_pool;
228
207
  } Descriptor;
@@ -236,9 +215,9 @@ static void Descriptor_mark(void* _self) {
236
215
  }
237
216
 
238
217
  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,
218
+ "Google::Protobuf::Descriptor",
219
+ {Descriptor_mark, RUBY_DEFAULT_FREE, NULL},
220
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
242
221
  };
243
222
 
244
223
  static Descriptor* ruby_to_Descriptor(VALUE val) {
@@ -280,8 +259,8 @@ static VALUE Descriptor_initialize(VALUE _self, VALUE cookie,
280
259
  "Descriptor objects may not be created from Ruby.");
281
260
  }
282
261
 
283
- self->descriptor_pool = descriptor_pool;
284
- self->msgdef = (const upb_msgdef*)NUM2ULL(ptr);
262
+ RB_OBJ_WRITE(_self, &self->descriptor_pool, descriptor_pool);
263
+ self->msgdef = (const upb_MessageDef*)NUM2ULL(ptr);
285
264
 
286
265
  return Qnil;
287
266
  }
@@ -294,7 +273,8 @@ static VALUE Descriptor_initialize(VALUE _self, VALUE cookie,
294
273
  */
295
274
  static VALUE Descriptor_file_descriptor(VALUE _self) {
296
275
  Descriptor* self = ruby_to_Descriptor(_self);
297
- return get_filedef_obj(self->descriptor_pool, upb_msgdef_file(self->msgdef));
276
+ return get_filedef_obj(self->descriptor_pool,
277
+ upb_MessageDef_File(self->msgdef));
298
278
  }
299
279
 
300
280
  /*
@@ -306,7 +286,7 @@ static VALUE Descriptor_file_descriptor(VALUE _self) {
306
286
  */
307
287
  static VALUE Descriptor_name(VALUE _self) {
308
288
  Descriptor* self = ruby_to_Descriptor(_self);
309
- return rb_str_maybe_null(upb_msgdef_fullname(self->msgdef));
289
+ return rb_str_maybe_null(upb_MessageDef_FullName(self->msgdef));
310
290
  }
311
291
 
312
292
  /*
@@ -318,11 +298,9 @@ static VALUE Descriptor_name(VALUE _self) {
318
298
  static VALUE Descriptor_each(VALUE _self) {
319
299
  Descriptor* self = ruby_to_Descriptor(_self);
320
300
 
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);
301
+ int n = upb_MessageDef_FieldCount(self->msgdef);
302
+ for (int i = 0; i < n; i++) {
303
+ const upb_FieldDef* field = upb_MessageDef_Field(self->msgdef, i);
326
304
  VALUE obj = get_fielddef_obj(self->descriptor_pool, field);
327
305
  rb_yield(obj);
328
306
  }
@@ -339,7 +317,7 @@ static VALUE Descriptor_each(VALUE _self) {
339
317
  static VALUE Descriptor_lookup(VALUE _self, VALUE name) {
340
318
  Descriptor* self = ruby_to_Descriptor(_self);
341
319
  const char* s = get_str(name);
342
- const upb_fielddef* field = upb_msgdef_ntofz(self->msgdef, s);
320
+ const upb_FieldDef* field = upb_MessageDef_FindFieldByName(self->msgdef, s);
343
321
  if (field == NULL) {
344
322
  return Qnil;
345
323
  }
@@ -356,11 +334,9 @@ static VALUE Descriptor_lookup(VALUE _self, VALUE name) {
356
334
  static VALUE Descriptor_each_oneof(VALUE _self) {
357
335
  Descriptor* self = ruby_to_Descriptor(_self);
358
336
 
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);
337
+ int n = upb_MessageDef_OneofCount(self->msgdef);
338
+ for (int i = 0; i < n; i++) {
339
+ const upb_OneofDef* oneof = upb_MessageDef_Oneof(self->msgdef, i);
364
340
  VALUE obj = get_oneofdef_obj(self->descriptor_pool, oneof);
365
341
  rb_yield(obj);
366
342
  }
@@ -377,7 +353,7 @@ static VALUE Descriptor_each_oneof(VALUE _self) {
377
353
  static VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
378
354
  Descriptor* self = ruby_to_Descriptor(_self);
379
355
  const char* s = get_str(name);
380
- const upb_oneofdef* oneof = upb_msgdef_ntooz(self->msgdef, s);
356
+ const upb_OneofDef* oneof = upb_MessageDef_FindOneofByName(self->msgdef, s);
381
357
  if (oneof == NULL) {
382
358
  return Qnil;
383
359
  }
@@ -393,14 +369,13 @@ static VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
393
369
  static VALUE Descriptor_msgclass(VALUE _self) {
394
370
  Descriptor* self = ruby_to_Descriptor(_self);
395
371
  if (self->klass == Qnil) {
396
- self->klass = build_class_from_descriptor(_self);
372
+ RB_OBJ_WRITE(_self, &self->klass, build_class_from_descriptor(_self));
397
373
  }
398
374
  return self->klass;
399
375
  }
400
376
 
401
377
  static void Descriptor_register(VALUE module) {
402
- VALUE klass = rb_define_class_under(
403
- module, "Descriptor", rb_cObject);
378
+ VALUE klass = rb_define_class_under(module, "Descriptor", rb_cObject);
404
379
  rb_define_alloc_func(klass, Descriptor_alloc);
405
380
  rb_define_method(klass, "initialize", Descriptor_initialize, 3);
406
381
  rb_define_method(klass, "each", Descriptor_each, 0);
@@ -420,8 +395,10 @@ static void Descriptor_register(VALUE module) {
420
395
  // -----------------------------------------------------------------------------
421
396
 
422
397
  typedef struct {
423
- const upb_filedef* filedef;
424
- VALUE descriptor_pool; // Owns the upb_filedef.
398
+ const upb_FileDef* filedef;
399
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
400
+ // macro to update VALUE references, as to trigger write barriers.
401
+ VALUE descriptor_pool; // Owns the upb_FileDef.
425
402
  } FileDescriptor;
426
403
 
427
404
  static VALUE cFileDescriptor = Qnil;
@@ -432,9 +409,9 @@ static void FileDescriptor_mark(void* _self) {
432
409
  }
433
410
 
434
411
  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,
412
+ "Google::Protobuf::FileDescriptor",
413
+ {FileDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
414
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
438
415
  };
439
416
 
440
417
  static FileDescriptor* ruby_to_FileDescriptor(VALUE val) {
@@ -459,7 +436,7 @@ static VALUE FileDescriptor_alloc(VALUE klass) {
459
436
  * to a builder.
460
437
  */
461
438
  static VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
462
- VALUE descriptor_pool, VALUE ptr) {
439
+ VALUE descriptor_pool, VALUE ptr) {
463
440
  FileDescriptor* self = ruby_to_FileDescriptor(_self);
464
441
 
465
442
  if (cookie != c_only_cookie) {
@@ -467,8 +444,8 @@ static VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
467
444
  "Descriptor objects may not be created from Ruby.");
468
445
  }
469
446
 
470
- self->descriptor_pool = descriptor_pool;
471
- self->filedef = (const upb_filedef*)NUM2ULL(ptr);
447
+ RB_OBJ_WRITE(_self, &self->descriptor_pool, descriptor_pool);
448
+ self->filedef = (const upb_FileDef*)NUM2ULL(ptr);
472
449
 
473
450
  return Qnil;
474
451
  }
@@ -481,7 +458,7 @@ static VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
481
458
  */
482
459
  static VALUE FileDescriptor_name(VALUE _self) {
483
460
  FileDescriptor* self = ruby_to_FileDescriptor(_self);
484
- const char* name = upb_filedef_name(self->filedef);
461
+ const char* name = upb_FileDef_Name(self->filedef);
485
462
  return name == NULL ? Qnil : rb_str_new2(name);
486
463
  }
487
464
 
@@ -497,16 +474,18 @@ static VALUE FileDescriptor_name(VALUE _self) {
497
474
  static VALUE FileDescriptor_syntax(VALUE _self) {
498
475
  FileDescriptor* self = ruby_to_FileDescriptor(_self);
499
476
 
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;
477
+ switch (upb_FileDef_Syntax(self->filedef)) {
478
+ case kUpb_Syntax_Proto3:
479
+ return ID2SYM(rb_intern("proto3"));
480
+ case kUpb_Syntax_Proto2:
481
+ return ID2SYM(rb_intern("proto2"));
482
+ default:
483
+ return Qnil;
504
484
  }
505
485
  }
506
486
 
507
487
  static void FileDescriptor_register(VALUE module) {
508
- VALUE klass = rb_define_class_under(
509
- module, "FileDescriptor", rb_cObject);
488
+ VALUE klass = rb_define_class_under(module, "FileDescriptor", rb_cObject);
510
489
  rb_define_alloc_func(klass, FileDescriptor_alloc);
511
490
  rb_define_method(klass, "initialize", FileDescriptor_initialize, 3);
512
491
  rb_define_method(klass, "name", FileDescriptor_name, 0);
@@ -520,8 +499,10 @@ static void FileDescriptor_register(VALUE module) {
520
499
  // -----------------------------------------------------------------------------
521
500
 
522
501
  typedef struct {
523
- const upb_fielddef* fielddef;
524
- VALUE descriptor_pool; // Owns the upb_fielddef.
502
+ const upb_FieldDef* fielddef;
503
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
504
+ // macro to update VALUE references, as to trigger write barriers.
505
+ VALUE descriptor_pool; // Owns the upb_FieldDef.
525
506
  } FieldDescriptor;
526
507
 
527
508
  static VALUE cFieldDescriptor = Qnil;
@@ -532,9 +513,9 @@ static void FieldDescriptor_mark(void* _self) {
532
513
  }
533
514
 
534
515
  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,
516
+ "Google::Protobuf::FieldDescriptor",
517
+ {FieldDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
518
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
538
519
  };
539
520
 
540
521
  static FieldDescriptor* ruby_to_FieldDescriptor(VALUE val) {
@@ -572,8 +553,8 @@ static VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
572
553
  "Descriptor objects may not be created from Ruby.");
573
554
  }
574
555
 
575
- self->descriptor_pool = descriptor_pool;
576
- self->fielddef = (const upb_fielddef*)NUM2ULL(ptr);
556
+ RB_OBJ_WRITE(_self, &self->descriptor_pool, descriptor_pool);
557
+ self->fielddef = (const upb_FieldDef*)NUM2ULL(ptr);
577
558
 
578
559
  return Qnil;
579
560
  }
@@ -586,31 +567,31 @@ static VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
586
567
  */
587
568
  static VALUE FieldDescriptor_name(VALUE _self) {
588
569
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
589
- return rb_str_maybe_null(upb_fielddef_name(self->fielddef));
570
+ return rb_str_maybe_null(upb_FieldDef_Name(self->fielddef));
590
571
  }
591
572
 
592
573
  // Non-static, exposed to other .c files.
593
- upb_fieldtype_t ruby_to_fieldtype(VALUE type) {
574
+ upb_CType ruby_to_fieldtype(VALUE type) {
594
575
  if (TYPE(type) != T_SYMBOL) {
595
576
  rb_raise(rb_eArgError, "Expected symbol for field type.");
596
577
  }
597
578
 
598
- #define CONVERT(upb, ruby) \
599
- if (SYM2ID(type) == rb_intern( # ruby )) { \
600
- return UPB_TYPE_ ## upb; \
579
+ #define CONVERT(upb, ruby) \
580
+ if (SYM2ID(type) == rb_intern(#ruby)) { \
581
+ return kUpb_CType_##upb; \
601
582
  }
602
583
 
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);
584
+ CONVERT(Float, float);
585
+ CONVERT(Double, double);
586
+ CONVERT(Bool, bool);
587
+ CONVERT(String, string);
588
+ CONVERT(Bytes, bytes);
589
+ CONVERT(Message, message);
590
+ CONVERT(Enum, enum);
591
+ CONVERT(Int32, int32);
592
+ CONVERT(Int64, int64);
593
+ CONVERT(UInt32, uint32);
594
+ CONVERT(UInt64, uint64);
614
595
 
615
596
  #undef CONVERT
616
597
 
@@ -618,28 +599,29 @@ upb_fieldtype_t ruby_to_fieldtype(VALUE type) {
618
599
  return 0;
619
600
  }
620
601
 
621
- static VALUE descriptortype_to_ruby(upb_descriptortype_t type) {
602
+ static VALUE descriptortype_to_ruby(upb_FieldType type) {
622
603
  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);
604
+ #define CONVERT(upb, ruby) \
605
+ case kUpb_FieldType_##upb: \
606
+ return ID2SYM(rb_intern(#ruby));
607
+ CONVERT(Float, float);
608
+ CONVERT(Double, double);
609
+ CONVERT(Bool, bool);
610
+ CONVERT(String, string);
611
+ CONVERT(Bytes, bytes);
612
+ CONVERT(Message, message);
613
+ CONVERT(Group, group);
614
+ CONVERT(Enum, enum);
615
+ CONVERT(Int32, int32);
616
+ CONVERT(Int64, int64);
617
+ CONVERT(UInt32, uint32);
618
+ CONVERT(UInt64, uint64);
619
+ CONVERT(SInt32, sint32);
620
+ CONVERT(SInt64, sint64);
621
+ CONVERT(Fixed32, fixed32);
622
+ CONVERT(Fixed64, fixed64);
623
+ CONVERT(SFixed32, sfixed32);
624
+ CONVERT(SFixed64, sfixed64);
643
625
  #undef CONVERT
644
626
  }
645
627
  return Qnil;
@@ -657,7 +639,7 @@ static VALUE descriptortype_to_ruby(upb_descriptortype_t type) {
657
639
  */
658
640
  static VALUE FieldDescriptor__type(VALUE _self) {
659
641
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
660
- return descriptortype_to_ruby(upb_fielddef_descriptortype(self->fielddef));
642
+ return descriptortype_to_ruby(upb_FieldDef_Type(self->fielddef));
661
643
  }
662
644
 
663
645
  /*
@@ -668,17 +650,16 @@ static VALUE FieldDescriptor__type(VALUE _self) {
668
650
  */
669
651
  static VALUE FieldDescriptor_default(VALUE _self) {
670
652
  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)) {
653
+ const upb_FieldDef* f = self->fielddef;
654
+ upb_MessageValue default_val = {0};
655
+ if (upb_FieldDef_IsSubMessage(f)) {
674
656
  return Qnil;
675
- } else if (!upb_fielddef_isseq(f)) {
676
- default_val = upb_fielddef_default(f);
657
+ } else if (!upb_FieldDef_IsRepeated(f)) {
658
+ default_val = upb_FieldDef_Default(f);
677
659
  }
678
660
  return Convert_UpbToRuby(default_val, TypeInfo_get(self->fielddef), Qnil);
679
661
  }
680
662
 
681
-
682
663
  /*
683
664
  * call-seq:
684
665
  * FieldDescriptor.json_name => json_name
@@ -687,8 +668,8 @@ static VALUE FieldDescriptor_default(VALUE _self) {
687
668
  */
688
669
  static VALUE FieldDescriptor_json_name(VALUE _self) {
689
670
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
690
- const upb_fielddef *f = self->fielddef;
691
- const char *json_name = upb_fielddef_jsonname(f);
671
+ const upb_FieldDef* f = self->fielddef;
672
+ const char* json_name = upb_FieldDef_JsonName(f);
692
673
  return rb_str_new2(json_name);
693
674
  }
694
675
 
@@ -703,13 +684,14 @@ static VALUE FieldDescriptor_json_name(VALUE _self) {
703
684
  */
704
685
  static VALUE FieldDescriptor_label(VALUE _self) {
705
686
  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 ));
687
+ switch (upb_FieldDef_Label(self->fielddef)) {
688
+ #define CONVERT(upb, ruby) \
689
+ case kUpb_Label_##upb: \
690
+ return ID2SYM(rb_intern(#ruby));
709
691
 
710
- CONVERT(OPTIONAL, optional);
711
- CONVERT(REQUIRED, required);
712
- CONVERT(REPEATED, repeated);
692
+ CONVERT(Optional, optional);
693
+ CONVERT(Required, required);
694
+ CONVERT(Repeated, repeated);
713
695
 
714
696
  #undef CONVERT
715
697
  }
@@ -725,7 +707,7 @@ static VALUE FieldDescriptor_label(VALUE _self) {
725
707
  */
726
708
  static VALUE FieldDescriptor_number(VALUE _self) {
727
709
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
728
- return INT2NUM(upb_fielddef_number(self->fielddef));
710
+ return INT2NUM(upb_FieldDef_Number(self->fielddef));
729
711
  }
730
712
 
731
713
  /*
@@ -739,13 +721,13 @@ static VALUE FieldDescriptor_number(VALUE _self) {
739
721
  */
740
722
  static VALUE FieldDescriptor_submsg_name(VALUE _self) {
741
723
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
742
- switch (upb_fielddef_type(self->fielddef)) {
743
- case UPB_TYPE_ENUM:
724
+ switch (upb_FieldDef_CType(self->fielddef)) {
725
+ case kUpb_CType_Enum:
744
726
  return rb_str_new2(
745
- upb_enumdef_fullname(upb_fielddef_enumsubdef(self->fielddef)));
746
- case UPB_TYPE_MESSAGE:
727
+ upb_EnumDef_FullName(upb_FieldDef_EnumSubDef(self->fielddef)));
728
+ case kUpb_CType_Message:
747
729
  return rb_str_new2(
748
- upb_msgdef_fullname(upb_fielddef_msgsubdef(self->fielddef)));
730
+ upb_MessageDef_FullName(upb_FieldDef_MessageSubDef(self->fielddef)));
749
731
  default:
750
732
  return Qnil;
751
733
  }
@@ -762,13 +744,13 @@ static VALUE FieldDescriptor_submsg_name(VALUE _self) {
762
744
  */
763
745
  static VALUE FieldDescriptor_subtype(VALUE _self) {
764
746
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
765
- switch (upb_fielddef_type(self->fielddef)) {
766
- case UPB_TYPE_ENUM:
747
+ switch (upb_FieldDef_CType(self->fielddef)) {
748
+ case kUpb_CType_Enum:
767
749
  return get_enumdef_obj(self->descriptor_pool,
768
- upb_fielddef_enumsubdef(self->fielddef));
769
- case UPB_TYPE_MESSAGE:
750
+ upb_FieldDef_EnumSubDef(self->fielddef));
751
+ case kUpb_CType_Message:
770
752
  return get_msgdef_obj(self->descriptor_pool,
771
- upb_fielddef_msgsubdef(self->fielddef));
753
+ upb_FieldDef_MessageSubDef(self->fielddef));
772
754
  default:
773
755
  return Qnil;
774
756
  }
@@ -783,11 +765,11 @@ static VALUE FieldDescriptor_subtype(VALUE _self) {
783
765
  */
784
766
  static VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
785
767
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
786
- const upb_msgdef *m;
768
+ const upb_MessageDef* m;
787
769
 
788
770
  Message_Get(msg_rb, &m);
789
771
 
790
- if (m != upb_fielddef_containingtype(self->fielddef)) {
772
+ if (m != upb_FieldDef_ContainingType(self->fielddef)) {
791
773
  rb_raise(cTypeError, "get method called on wrong message type");
792
774
  }
793
775
 
@@ -803,16 +785,16 @@ static VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
803
785
  */
804
786
  static VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
805
787
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
806
- const upb_msgdef *m;
807
- const upb_msgdef *msg = Message_Get(msg_rb, &m);
788
+ const upb_MessageDef* m;
789
+ const upb_MessageDef* msg = Message_Get(msg_rb, &m);
808
790
 
809
- if (m != upb_fielddef_containingtype(self->fielddef)) {
791
+ if (m != upb_FieldDef_ContainingType(self->fielddef)) {
810
792
  rb_raise(cTypeError, "has method called on wrong message type");
811
- } else if (!upb_fielddef_haspresence(self->fielddef)) {
793
+ } else if (!upb_FieldDef_HasPresence(self->fielddef)) {
812
794
  rb_raise(rb_eArgError, "does not track presence");
813
795
  }
814
796
 
815
- return upb_msg_has(msg, self->fielddef) ? Qtrue : Qfalse;
797
+ return upb_Message_HasFieldByDef(msg, self->fielddef) ? Qtrue : Qfalse;
816
798
  }
817
799
 
818
800
  /*
@@ -823,14 +805,14 @@ static VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
823
805
  */
824
806
  static VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {
825
807
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
826
- const upb_msgdef *m;
827
- upb_msgdef *msg = Message_GetMutable(msg_rb, &m);
808
+ const upb_MessageDef* m;
809
+ upb_MessageDef* msg = Message_GetMutable(msg_rb, &m);
828
810
 
829
- if (m != upb_fielddef_containingtype(self->fielddef)) {
811
+ if (m != upb_FieldDef_ContainingType(self->fielddef)) {
830
812
  rb_raise(cTypeError, "has method called on wrong message type");
831
813
  }
832
814
 
833
- upb_msg_clearfield(msg, self->fielddef);
815
+ upb_Message_ClearFieldByDef(msg, self->fielddef);
834
816
  return Qnil;
835
817
  }
836
818
 
@@ -844,24 +826,23 @@ static VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {
844
826
  */
845
827
  static VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value) {
846
828
  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;
829
+ const upb_MessageDef* m;
830
+ upb_MessageDef* msg = Message_GetMutable(msg_rb, &m);
831
+ upb_Arena* arena = Arena_get(Message_GetArena(msg_rb));
832
+ upb_MessageValue msgval;
851
833
 
852
- if (m != upb_fielddef_containingtype(self->fielddef)) {
834
+ if (m != upb_FieldDef_ContainingType(self->fielddef)) {
853
835
  rb_raise(cTypeError, "set method called on wrong message type");
854
836
  }
855
837
 
856
- msgval = Convert_RubyToUpb(value, upb_fielddef_name(self->fielddef),
838
+ msgval = Convert_RubyToUpb(value, upb_FieldDef_Name(self->fielddef),
857
839
  TypeInfo_get(self->fielddef), arena);
858
- upb_msg_set(msg, self->fielddef, msgval, arena);
840
+ upb_Message_SetFieldByDef(msg, self->fielddef, msgval, arena);
859
841
  return Qnil;
860
842
  }
861
843
 
862
844
  static void FieldDescriptor_register(VALUE module) {
863
- VALUE klass = rb_define_class_under(
864
- module, "FieldDescriptor", rb_cObject);
845
+ VALUE klass = rb_define_class_under(module, "FieldDescriptor", rb_cObject);
865
846
  rb_define_alloc_func(klass, FieldDescriptor_alloc);
866
847
  rb_define_method(klass, "initialize", FieldDescriptor_initialize, 3);
867
848
  rb_define_method(klass, "name", FieldDescriptor_name, 0);
@@ -885,8 +866,10 @@ static void FieldDescriptor_register(VALUE module) {
885
866
  // -----------------------------------------------------------------------------
886
867
 
887
868
  typedef struct {
888
- const upb_oneofdef* oneofdef;
889
- VALUE descriptor_pool; // Owns the upb_oneofdef.
869
+ const upb_OneofDef* oneofdef;
870
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
871
+ // macro to update VALUE references, as to trigger write barriers.
872
+ VALUE descriptor_pool; // Owns the upb_OneofDef.
890
873
  } OneofDescriptor;
891
874
 
892
875
  static VALUE cOneofDescriptor = Qnil;
@@ -899,7 +882,7 @@ static void OneofDescriptor_mark(void* _self) {
899
882
  static const rb_data_type_t OneofDescriptor_type = {
900
883
  "Google::Protobuf::OneofDescriptor",
901
884
  {OneofDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
902
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
885
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
903
886
  };
904
887
 
905
888
  static OneofDescriptor* ruby_to_OneofDescriptor(VALUE val) {
@@ -930,7 +913,7 @@ static VALUE OneofDescriptor_alloc(VALUE klass) {
930
913
  * Creates a descriptor wrapper object. May only be called from C.
931
914
  */
932
915
  static VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
933
- VALUE descriptor_pool, VALUE ptr) {
916
+ VALUE descriptor_pool, VALUE ptr) {
934
917
  OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
935
918
 
936
919
  if (cookie != c_only_cookie) {
@@ -938,8 +921,8 @@ static VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
938
921
  "Descriptor objects may not be created from Ruby.");
939
922
  }
940
923
 
941
- self->descriptor_pool = descriptor_pool;
942
- self->oneofdef = (const upb_oneofdef*)NUM2ULL(ptr);
924
+ RB_OBJ_WRITE(_self, &self->descriptor_pool, descriptor_pool);
925
+ self->oneofdef = (const upb_OneofDef*)NUM2ULL(ptr);
943
926
 
944
927
  return Qnil;
945
928
  }
@@ -952,7 +935,7 @@ static VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
952
935
  */
953
936
  static VALUE OneofDescriptor_name(VALUE _self) {
954
937
  OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
955
- return rb_str_maybe_null(upb_oneofdef_name(self->oneofdef));
938
+ return rb_str_maybe_null(upb_OneofDef_Name(self->oneofdef));
956
939
  }
957
940
 
958
941
  /*
@@ -963,11 +946,10 @@ static VALUE OneofDescriptor_name(VALUE _self) {
963
946
  */
964
947
  static VALUE OneofDescriptor_each(VALUE _self) {
965
948
  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);
949
+
950
+ int n = upb_OneofDef_FieldCount(self->oneofdef);
951
+ for (int i = 0; i < n; i++) {
952
+ const upb_FieldDef* f = upb_OneofDef_Field(self->oneofdef, i);
971
953
  VALUE obj = get_fielddef_obj(self->descriptor_pool, f);
972
954
  rb_yield(obj);
973
955
  }
@@ -975,8 +957,7 @@ static VALUE OneofDescriptor_each(VALUE _self) {
975
957
  }
976
958
 
977
959
  static void OneofDescriptor_register(VALUE module) {
978
- VALUE klass = rb_define_class_under(
979
- module, "OneofDescriptor", rb_cObject);
960
+ VALUE klass = rb_define_class_under(module, "OneofDescriptor", rb_cObject);
980
961
  rb_define_alloc_func(klass, OneofDescriptor_alloc);
981
962
  rb_define_method(klass, "initialize", OneofDescriptor_initialize, 3);
982
963
  rb_define_method(klass, "name", OneofDescriptor_name, 0);
@@ -991,9 +972,11 @@ static void OneofDescriptor_register(VALUE module) {
991
972
  // -----------------------------------------------------------------------------
992
973
 
993
974
  typedef struct {
994
- const upb_enumdef* enumdef;
995
- VALUE module; // begins as nil
996
- VALUE descriptor_pool; // Owns the upb_enumdef.
975
+ const upb_EnumDef* enumdef;
976
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
977
+ // macro to update VALUE references, as to trigger write barriers.
978
+ VALUE module; // begins as nil
979
+ VALUE descriptor_pool; // Owns the upb_EnumDef.
997
980
  } EnumDescriptor;
998
981
 
999
982
  static VALUE cEnumDescriptor = Qnil;
@@ -1005,9 +988,9 @@ static void EnumDescriptor_mark(void* _self) {
1005
988
  }
1006
989
 
1007
990
  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,
991
+ "Google::Protobuf::EnumDescriptor",
992
+ {EnumDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
993
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
1011
994
  };
1012
995
 
1013
996
  static EnumDescriptor* ruby_to_EnumDescriptor(VALUE val) {
@@ -1026,8 +1009,8 @@ static VALUE EnumDescriptor_alloc(VALUE klass) {
1026
1009
  }
1027
1010
 
1028
1011
  // 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);
1012
+ const upb_EnumDef* EnumDescriptor_GetEnumDef(VALUE enum_desc_rb) {
1013
+ EnumDescriptor* desc = ruby_to_EnumDescriptor(enum_desc_rb);
1031
1014
  return desc->enumdef;
1032
1015
  }
1033
1016
 
@@ -1046,8 +1029,8 @@ static VALUE EnumDescriptor_initialize(VALUE _self, VALUE cookie,
1046
1029
  "Descriptor objects may not be created from Ruby.");
1047
1030
  }
1048
1031
 
1049
- self->descriptor_pool = descriptor_pool;
1050
- self->enumdef = (const upb_enumdef*)NUM2ULL(ptr);
1032
+ RB_OBJ_WRITE(_self, &self->descriptor_pool, descriptor_pool);
1033
+ self->enumdef = (const upb_EnumDef*)NUM2ULL(ptr);
1051
1034
 
1052
1035
  return Qnil;
1053
1036
  }
@@ -1061,7 +1044,7 @@ static VALUE EnumDescriptor_initialize(VALUE _self, VALUE cookie,
1061
1044
  static VALUE EnumDescriptor_file_descriptor(VALUE _self) {
1062
1045
  EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1063
1046
  return get_filedef_obj(self->descriptor_pool,
1064
- upb_enumdef_file(self->enumdef));
1047
+ upb_EnumDef_File(self->enumdef));
1065
1048
  }
1066
1049
 
1067
1050
  /*
@@ -1072,7 +1055,7 @@ static VALUE EnumDescriptor_file_descriptor(VALUE _self) {
1072
1055
  */
1073
1056
  static VALUE EnumDescriptor_name(VALUE _self) {
1074
1057
  EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1075
- return rb_str_maybe_null(upb_enumdef_fullname(self->enumdef));
1058
+ return rb_str_maybe_null(upb_EnumDef_FullName(self->enumdef));
1076
1059
  }
1077
1060
 
1078
1061
  /*
@@ -1084,10 +1067,11 @@ static VALUE EnumDescriptor_name(VALUE _self) {
1084
1067
  */
1085
1068
  static VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
1086
1069
  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);
1070
+ const char* name_str = rb_id2name(SYM2ID(name));
1071
+ const upb_EnumValueDef* ev =
1072
+ upb_EnumDef_FindValueByName(self->enumdef, name_str);
1073
+ if (ev) {
1074
+ return INT2NUM(upb_EnumValueDef_Number(ev));
1091
1075
  } else {
1092
1076
  return Qnil;
1093
1077
  }
@@ -1103,9 +1087,10 @@ static VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
1103
1087
  static VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
1104
1088
  EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1105
1089
  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));
1090
+ const upb_EnumValueDef* ev =
1091
+ upb_EnumDef_FindValueByNumber(self->enumdef, val);
1092
+ if (ev) {
1093
+ return ID2SYM(rb_intern(upb_EnumValueDef_Name(ev)));
1109
1094
  } else {
1110
1095
  return Qnil;
1111
1096
  }
@@ -1121,12 +1106,11 @@ static VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
1121
1106
  static VALUE EnumDescriptor_each(VALUE _self) {
1122
1107
  EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1123
1108
 
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));
1109
+ int n = upb_EnumDef_ValueCount(self->enumdef);
1110
+ for (int i = 0; i < n; i++) {
1111
+ const upb_EnumValueDef* ev = upb_EnumDef_Value(self->enumdef, i);
1112
+ VALUE key = ID2SYM(rb_intern(upb_EnumValueDef_Name(ev)));
1113
+ VALUE number = INT2NUM(upb_EnumValueDef_Number(ev));
1130
1114
  rb_yield_values(2, key, number);
1131
1115
  }
1132
1116
 
@@ -1142,14 +1126,13 @@ static VALUE EnumDescriptor_each(VALUE _self) {
1142
1126
  static VALUE EnumDescriptor_enummodule(VALUE _self) {
1143
1127
  EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1144
1128
  if (self->module == Qnil) {
1145
- self->module = build_module_from_enumdesc(_self);
1129
+ RB_OBJ_WRITE(_self, &self->module, build_module_from_enumdesc(_self));
1146
1130
  }
1147
1131
  return self->module;
1148
1132
  }
1149
1133
 
1150
1134
  static void EnumDescriptor_register(VALUE module) {
1151
- VALUE klass = rb_define_class_under(
1152
- module, "EnumDescriptor", rb_cObject);
1135
+ VALUE klass = rb_define_class_under(module, "EnumDescriptor", rb_cObject);
1153
1136
  rb_define_alloc_func(klass, EnumDescriptor_alloc);
1154
1137
  rb_define_method(klass, "initialize", EnumDescriptor_initialize, 3);
1155
1138
  rb_define_method(klass, "name", EnumDescriptor_name, 0);
@@ -1176,7 +1159,7 @@ static VALUE get_def_obj(VALUE _descriptor_pool, const void* ptr, VALUE klass) {
1176
1159
 
1177
1160
  if (def == Qnil) {
1178
1161
  // Lazily create wrapper object.
1179
- VALUE args[3] = { c_only_cookie, _descriptor_pool, key };
1162
+ VALUE args[3] = {c_only_cookie, _descriptor_pool, key};
1180
1163
  def = rb_class_new_instance(3, args, klass);
1181
1164
  rb_hash_aset(descriptor_pool->def_to_descriptor, key, def);
1182
1165
  }
@@ -1184,23 +1167,23 @@ static VALUE get_def_obj(VALUE _descriptor_pool, const void* ptr, VALUE klass) {
1184
1167
  return def;
1185
1168
  }
1186
1169
 
1187
- static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_msgdef* def) {
1170
+ static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_MessageDef* def) {
1188
1171
  return get_def_obj(descriptor_pool, def, cDescriptor);
1189
1172
  }
1190
1173
 
1191
- static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_enumdef* def) {
1174
+ static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_EnumDef* def) {
1192
1175
  return get_def_obj(descriptor_pool, def, cEnumDescriptor);
1193
1176
  }
1194
1177
 
1195
- static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_fielddef* def) {
1178
+ static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_FieldDef* def) {
1196
1179
  return get_def_obj(descriptor_pool, def, cFieldDescriptor);
1197
1180
  }
1198
1181
 
1199
- static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_filedef* def) {
1182
+ static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_FileDef* def) {
1200
1183
  return get_def_obj(descriptor_pool, def, cFileDescriptor);
1201
1184
  }
1202
1185
 
1203
- static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_oneofdef* def) {
1186
+ static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_OneofDef* def) {
1204
1187
  return get_def_obj(descriptor_pool, def, cOneofDescriptor);
1205
1188
  }
1206
1189
 
@@ -1210,8 +1193,8 @@ static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_oneofdef* def) {
1210
1193
 
1211
1194
  // Functions exposed to other modules in defs.h.
1212
1195
 
1213
- VALUE Descriptor_DefToClass(const upb_msgdef *m) {
1214
- const upb_symtab *symtab = upb_filedef_symtab(upb_msgdef_file(m));
1196
+ VALUE Descriptor_DefToClass(const upb_MessageDef* m) {
1197
+ const upb_DefPool* symtab = upb_FileDef_Pool(upb_MessageDef_File(m));
1215
1198
  VALUE pool = ObjectCache_Get(symtab);
1216
1199
  PBRUBY_ASSERT(pool != Qnil);
1217
1200
  VALUE desc_rb = get_msgdef_obj(pool, m);
@@ -1219,15 +1202,16 @@ VALUE Descriptor_DefToClass(const upb_msgdef *m) {
1219
1202
  return desc->klass;
1220
1203
  }
1221
1204
 
1222
- const upb_msgdef *Descriptor_GetMsgDef(VALUE desc_rb) {
1205
+ const upb_MessageDef* Descriptor_GetMsgDef(VALUE desc_rb) {
1223
1206
  const Descriptor* desc = ruby_to_Descriptor(desc_rb);
1224
1207
  return desc->msgdef;
1225
1208
  }
1226
1209
 
1227
- VALUE TypeInfo_InitArg(int argc, VALUE *argv, int skip_arg) {
1210
+ VALUE TypeInfo_InitArg(int argc, VALUE* argv, int skip_arg) {
1228
1211
  if (argc > skip_arg) {
1229
1212
  if (argc > 1 + skip_arg) {
1230
- rb_raise(rb_eArgError, "Expected a maximum of %d arguments.", skip_arg + 1);
1213
+ rb_raise(rb_eArgError, "Expected a maximum of %d arguments.",
1214
+ skip_arg + 1);
1231
1215
  }
1232
1216
  return argv[skip_arg];
1233
1217
  } else {
@@ -1239,7 +1223,7 @@ TypeInfo TypeInfo_FromClass(int argc, VALUE* argv, int skip_arg,
1239
1223
  VALUE* type_class, VALUE* init_arg) {
1240
1224
  TypeInfo ret = {ruby_to_fieldtype(argv[skip_arg])};
1241
1225
 
1242
- if (ret.type == UPB_TYPE_MESSAGE || ret.type == UPB_TYPE_ENUM) {
1226
+ if (ret.type == kUpb_CType_Message || ret.type == kUpb_CType_Enum) {
1243
1227
  *init_arg = TypeInfo_InitArg(argc, argv, skip_arg + 2);
1244
1228
 
1245
1229
  if (argc < 2 + skip_arg) {
@@ -1257,11 +1241,11 @@ TypeInfo TypeInfo_FromClass(int argc, VALUE* argv, int skip_arg,
1257
1241
  "class or enum as returned by the DescriptorPool.");
1258
1242
  }
1259
1243
 
1260
- if (ret.type == UPB_TYPE_MESSAGE) {
1244
+ if (ret.type == kUpb_CType_Message) {
1261
1245
  ret.def.msgdef = ruby_to_Descriptor(desc)->msgdef;
1262
1246
  Message_CheckClass(klass);
1263
1247
  } else {
1264
- PBRUBY_ASSERT(ret.type == UPB_TYPE_ENUM);
1248
+ PBRUBY_ASSERT(ret.type == kUpb_CType_Enum);
1265
1249
  ret.def.enumdef = ruby_to_EnumDescriptor(desc)->enumdef;
1266
1250
  }
1267
1251
  } else {