google-protobuf 3.15.8 → 3.22.3

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

Potentially problematic release.


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

Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/ext/google/protobuf_c/convert.c +128 -116
  3. data/ext/google/protobuf_c/convert.h +12 -9
  4. data/ext/google/protobuf_c/defs.c +229 -1506
  5. data/ext/google/protobuf_c/defs.h +19 -19
  6. data/ext/google/protobuf_c/extconf.rb +12 -5
  7. data/ext/google/protobuf_c/map.c +108 -110
  8. data/ext/google/protobuf_c/map.h +7 -7
  9. data/ext/google/protobuf_c/message.c +451 -343
  10. data/ext/google/protobuf_c/message.h +22 -19
  11. data/ext/google/protobuf_c/protobuf.c +93 -65
  12. data/ext/google/protobuf_c/protobuf.h +16 -9
  13. data/ext/google/protobuf_c/repeated_field.c +85 -85
  14. data/ext/google/protobuf_c/repeated_field.h +6 -5
  15. data/ext/google/protobuf_c/ruby-upb.c +11656 -6644
  16. data/ext/google/protobuf_c/ruby-upb.h +9772 -3551
  17. data/ext/google/protobuf_c/third_party/utf8_range/LICENSE +22 -0
  18. data/ext/google/protobuf_c/third_party/utf8_range/naive.c +92 -0
  19. data/ext/google/protobuf_c/third_party/utf8_range/range2-neon.c +157 -0
  20. data/ext/google/protobuf_c/third_party/utf8_range/range2-sse.c +170 -0
  21. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.h +21 -0
  22. data/ext/google/protobuf_c/wrap_memcpy.c +4 -3
  23. data/lib/google/protobuf/any_pb.rb +0 -0
  24. data/lib/google/protobuf/api_pb.rb +1 -0
  25. data/lib/google/protobuf/descriptor_dsl.rb +465 -0
  26. data/lib/google/protobuf/descriptor_pb.rb +298 -0
  27. data/lib/google/protobuf/duration_pb.rb +0 -0
  28. data/lib/google/protobuf/empty_pb.rb +0 -0
  29. data/lib/google/protobuf/field_mask_pb.rb +0 -0
  30. data/lib/google/protobuf/message_exts.rb +7 -2
  31. data/lib/google/protobuf/plugin_pb.rb +50 -0
  32. data/lib/google/protobuf/repeated_field.rb +15 -2
  33. data/lib/google/protobuf/source_context_pb.rb +0 -0
  34. data/lib/google/protobuf/struct_pb.rb +0 -0
  35. data/lib/google/protobuf/timestamp_pb.rb +0 -0
  36. data/lib/google/protobuf/type_pb.rb +1 -0
  37. data/lib/google/protobuf/well_known_types.rb +12 -2
  38. data/lib/google/protobuf/wrappers_pb.rb +0 -0
  39. data/lib/google/protobuf.rb +5 -73
  40. metadata +17 -36
  41. data/ext/google/protobuf_c/third_party/wyhash/wyhash.h +0 -145
  42. data/tests/basic.rb +0 -604
  43. data/tests/generated_code_test.rb +0 -23
  44. data/tests/stress.rb +0 -38
@@ -36,23 +36,16 @@
36
36
  #include "message.h"
37
37
  #include "protobuf.h"
38
38
 
39
- static VALUE Builder_build(VALUE _self);
40
-
41
- static VALUE cMessageBuilderContext;
42
- static VALUE cOneofBuilderContext;
43
- static VALUE cEnumBuilderContext;
44
- static VALUE cBuilder;
45
-
46
39
  // -----------------------------------------------------------------------------
47
40
  // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
48
41
  // instances.
49
42
  // -----------------------------------------------------------------------------
50
43
 
51
- static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_msgdef* def);
52
- static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_enumdef* def);
53
- static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_fielddef* def);
54
- static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_filedef* def);
55
- static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_oneofdef* def);
44
+ static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_MessageDef* 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);
56
49
 
57
50
  // A distinct object that is not accessible from Ruby. We use this as a
58
51
  // constructor argument to enforce that certain objects cannot be created from
@@ -75,174 +68,13 @@ static VALUE rb_str_maybe_null(const char* s) {
75
68
  return rb_str_new2(s);
76
69
  }
77
70
 
78
- // -----------------------------------------------------------------------------
79
- // Backward compatibility code.
80
- // -----------------------------------------------------------------------------
81
-
82
- static void rewrite_enum_default(const upb_symtab* symtab,
83
- google_protobuf_FileDescriptorProto* file,
84
- google_protobuf_FieldDescriptorProto* field) {
85
- upb_strview defaultval;
86
- const char *type_name_str;
87
- char *end;
88
- long val;
89
- const upb_enumdef *e;
90
- upb_strview type_name;
91
-
92
- /* Look for TYPE_ENUM fields that have a default. */
93
- if (google_protobuf_FieldDescriptorProto_type(field) !=
94
- google_protobuf_FieldDescriptorProto_TYPE_ENUM ||
95
- !google_protobuf_FieldDescriptorProto_has_default_value(field) ||
96
- !google_protobuf_FieldDescriptorProto_has_type_name(field)) {
97
- return;
98
- }
99
-
100
- defaultval = google_protobuf_FieldDescriptorProto_default_value(field);
101
- type_name = google_protobuf_FieldDescriptorProto_type_name(field);
102
-
103
- if (defaultval.size == 0 || !isdigit(defaultval.data[0])) {
104
- return;
105
- }
106
-
107
- if (type_name.size == 0 || type_name.data[0] != '.') {
108
- return;
109
- }
110
-
111
- type_name_str = type_name.data + 1;
112
-
113
- errno = 0;
114
- val = strtol(defaultval.data, &end, 10);
115
-
116
- if (errno != 0 || *end != 0 || val < INT32_MIN || val > INT32_MAX) {
117
- return;
118
- }
119
-
120
- /* Now find the corresponding enum definition. */
121
- e = upb_symtab_lookupenum(symtab, type_name_str);
122
- if (e) {
123
- /* Look in previously loaded files. */
124
- const char *label = upb_enumdef_iton(e, val);
125
- if (!label) {
126
- return;
127
- }
128
- google_protobuf_FieldDescriptorProto_set_default_value(
129
- field, upb_strview_makez(label));
130
- } else {
131
- /* Look in enums defined in this file. */
132
- const google_protobuf_EnumDescriptorProto* matching_enum = NULL;
133
- size_t i, n;
134
- const google_protobuf_EnumDescriptorProto* const* enums =
135
- google_protobuf_FileDescriptorProto_enum_type(file, &n);
136
- const google_protobuf_EnumValueDescriptorProto* const* values;
137
-
138
- for (i = 0; i < n; i++) {
139
- if (upb_strview_eql(google_protobuf_EnumDescriptorProto_name(enums[i]),
140
- upb_strview_makez(type_name_str))) {
141
- matching_enum = enums[i];
142
- break;
143
- }
144
- }
145
-
146
- if (!matching_enum) {
147
- return;
148
- }
149
-
150
- values = google_protobuf_EnumDescriptorProto_value(matching_enum, &n);
151
- for (i = 0; i < n; i++) {
152
- if (google_protobuf_EnumValueDescriptorProto_number(values[i]) == val) {
153
- google_protobuf_FieldDescriptorProto_set_default_value(
154
- field, google_protobuf_EnumValueDescriptorProto_name(values[i]));
155
- return;
156
- }
157
- }
158
-
159
- /* We failed to find an enum default. But we'll just leave the enum
160
- * untouched and let the normal def-building code catch it. */
161
- }
162
- }
163
-
164
- /* Historically we allowed enum defaults to be specified as a number. In
165
- * retrospect this was a mistake as descriptors require defaults to be
166
- * specified as a label. This can make a difference if multiple labels have the
167
- * same number.
168
- *
169
- * Here we do a pass over all enum defaults and rewrite numeric defaults by
170
- * looking up their labels. This is complicated by the fact that the enum
171
- * definition can live in either the symtab or the file_proto.
172
- * */
173
- static void rewrite_enum_defaults(
174
- const upb_symtab* symtab, google_protobuf_FileDescriptorProto* file_proto) {
175
- size_t i, n;
176
- google_protobuf_DescriptorProto** msgs =
177
- google_protobuf_FileDescriptorProto_mutable_message_type(file_proto, &n);
178
-
179
- for (i = 0; i < n; i++) {
180
- size_t j, m;
181
- google_protobuf_FieldDescriptorProto** fields =
182
- google_protobuf_DescriptorProto_mutable_field(msgs[i], &m);
183
- for (j = 0; j < m; j++) {
184
- rewrite_enum_default(symtab, file_proto, fields[j]);
185
- }
186
- }
187
- }
188
-
189
- static void remove_path(upb_strview *name) {
190
- const char* last = strrchr(name->data, '.');
191
- if (last) {
192
- size_t remove = last - name->data + 1;
193
- name->data += remove;
194
- name->size -= remove;
195
- }
196
- }
197
-
198
- static void rewrite_nesting(VALUE msg_ent, google_protobuf_DescriptorProto* msg,
199
- google_protobuf_DescriptorProto* const* msgs,
200
- google_protobuf_EnumDescriptorProto* const* enums,
201
- upb_arena *arena) {
202
- VALUE submsgs = rb_hash_aref(msg_ent, ID2SYM(rb_intern("msgs")));
203
- VALUE enum_pos = rb_hash_aref(msg_ent, ID2SYM(rb_intern("enums")));
204
- int submsg_count;
205
- int enum_count;
206
- int i;
207
- google_protobuf_DescriptorProto** msg_msgs;
208
- google_protobuf_EnumDescriptorProto** msg_enums;
209
-
210
- Check_Type(submsgs, T_ARRAY);
211
- Check_Type(enum_pos, T_ARRAY);
212
-
213
- submsg_count = RARRAY_LEN(submsgs);
214
- enum_count = RARRAY_LEN(enum_pos);
215
-
216
- msg_msgs = google_protobuf_DescriptorProto_resize_nested_type(
217
- msg, submsg_count, arena);
218
- msg_enums =
219
- google_protobuf_DescriptorProto_resize_enum_type(msg, enum_count, arena);
220
-
221
- for (i = 0; i < submsg_count; i++) {
222
- VALUE submsg_ent = RARRAY_PTR(submsgs)[i];
223
- VALUE pos = rb_hash_aref(submsg_ent, ID2SYM(rb_intern("pos")));
224
- upb_strview name;
225
-
226
- msg_msgs[i] = msgs[NUM2INT(pos)];
227
- name = google_protobuf_DescriptorProto_name(msg_msgs[i]);
228
- remove_path(&name);
229
- google_protobuf_DescriptorProto_set_name(msg_msgs[i], name);
230
- rewrite_nesting(submsg_ent, msg_msgs[i], msgs, enums, arena);
231
- }
232
-
233
- for (i = 0; i < enum_count; i++) {
234
- VALUE pos = RARRAY_PTR(enum_pos)[i];
235
- msg_enums[i] = enums[NUM2INT(pos)];
236
- }
237
- }
238
-
239
71
  // -----------------------------------------------------------------------------
240
72
  // DescriptorPool.
241
73
  // -----------------------------------------------------------------------------
242
74
 
243
75
  typedef struct {
244
76
  VALUE def_to_descriptor; // Hash table of def* -> Ruby descriptor.
245
- upb_symtab* symtab;
77
+ upb_DefPool* symtab;
246
78
  } DescriptorPool;
247
79
 
248
80
  VALUE cDescriptorPool = Qnil;
@@ -258,14 +90,14 @@ static void DescriptorPool_mark(void* _self) {
258
90
 
259
91
  static void DescriptorPool_free(void* _self) {
260
92
  DescriptorPool* self = _self;
261
- upb_symtab_free(self->symtab);
93
+ upb_DefPool_Free(self->symtab);
262
94
  xfree(self);
263
95
  }
264
96
 
265
97
  static const rb_data_type_t DescriptorPool_type = {
266
- "Google::Protobuf::DescriptorPool",
267
- {DescriptorPool_mark, DescriptorPool_free, NULL},
268
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
98
+ "Google::Protobuf::DescriptorPool",
99
+ {DescriptorPool_mark, DescriptorPool_free, NULL},
100
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
269
101
  };
270
102
 
271
103
  static DescriptorPool* ruby_to_DescriptorPool(VALUE val) {
@@ -275,8 +107,8 @@ static DescriptorPool* ruby_to_DescriptorPool(VALUE val) {
275
107
  }
276
108
 
277
109
  // Exposed to other modules in defs.h.
278
- const upb_symtab *DescriptorPool_GetSymtab(VALUE desc_pool_rb) {
279
- DescriptorPool *pool = ruby_to_DescriptorPool(desc_pool_rb);
110
+ const upb_DefPool* DescriptorPool_GetSymtab(VALUE desc_pool_rb) {
111
+ DescriptorPool* pool = ruby_to_DescriptorPool(desc_pool_rb);
280
112
  return pool->symtab;
281
113
  }
282
114
 
@@ -294,7 +126,7 @@ static VALUE DescriptorPool_alloc(VALUE klass) {
294
126
  ret = TypedData_Wrap_Struct(klass, &DescriptorPool_type, self);
295
127
 
296
128
  self->def_to_descriptor = rb_hash_new();
297
- self->symtab = upb_symtab_new();
129
+ self->symtab = upb_DefPool_New();
298
130
  ObjectCache_Add(self->symtab, ret);
299
131
 
300
132
  return ret;
@@ -302,20 +134,33 @@ static VALUE DescriptorPool_alloc(VALUE klass) {
302
134
 
303
135
  /*
304
136
  * call-seq:
305
- * DescriptorPool.build(&block)
137
+ * DescriptorPool.add_serialized_file(serialized_file_proto)
306
138
  *
307
- * Invokes the block with a Builder instance as self. All message and enum types
308
- * added within the block are committed to the pool atomically, and may refer
309
- * (co)recursively to each other. The user should call Builder#add_message and
310
- * Builder#add_enum within the block as appropriate. This is the recommended,
311
- * idiomatic way to define new message and enum types.
139
+ * Adds the given serialized FileDescriptorProto to the pool.
312
140
  */
313
- static VALUE DescriptorPool_build(int argc, VALUE* argv, VALUE _self) {
314
- VALUE ctx = rb_class_new_instance(1, &_self, cBuilder);
315
- VALUE block = rb_block_proc();
316
- rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
317
- Builder_build(ctx);
318
- return Qnil;
141
+ VALUE DescriptorPool_add_serialized_file(VALUE _self,
142
+ VALUE serialized_file_proto) {
143
+ DescriptorPool* self = ruby_to_DescriptorPool(_self);
144
+ Check_Type(serialized_file_proto, T_STRING);
145
+ VALUE arena_rb = Arena_new();
146
+ upb_Arena* arena = Arena_get(arena_rb);
147
+ google_protobuf_FileDescriptorProto* file_proto =
148
+ google_protobuf_FileDescriptorProto_parse(
149
+ RSTRING_PTR(serialized_file_proto),
150
+ RSTRING_LEN(serialized_file_proto), arena);
151
+ if (!file_proto) {
152
+ rb_raise(rb_eArgError, "Unable to parse FileDescriptorProto");
153
+ }
154
+ upb_Status status;
155
+ upb_Status_Clear(&status);
156
+ const upb_FileDef* filedef =
157
+ upb_DefPool_AddFile(self->symtab, file_proto, &status);
158
+ if (!filedef) {
159
+ rb_raise(cTypeError, "Unable to build file to DescriptorPool: %s",
160
+ upb_Status_ErrorMessage(&status));
161
+ }
162
+ RB_GC_GUARD(arena_rb);
163
+ return get_filedef_obj(_self, filedef);
319
164
  }
320
165
 
321
166
  /*
@@ -328,15 +173,15 @@ static VALUE DescriptorPool_build(int argc, VALUE* argv, VALUE _self) {
328
173
  static VALUE DescriptorPool_lookup(VALUE _self, VALUE name) {
329
174
  DescriptorPool* self = ruby_to_DescriptorPool(_self);
330
175
  const char* name_str = get_str(name);
331
- const upb_msgdef* msgdef;
332
- const upb_enumdef* enumdef;
176
+ const upb_MessageDef* msgdef;
177
+ const upb_EnumDef* enumdef;
333
178
 
334
- msgdef = upb_symtab_lookupmsg(self->symtab, name_str);
179
+ msgdef = upb_DefPool_FindMessageByName(self->symtab, name_str);
335
180
  if (msgdef) {
336
181
  return get_msgdef_obj(_self, msgdef);
337
182
  }
338
183
 
339
- enumdef = upb_symtab_lookupenum(self->symtab, name_str);
184
+ enumdef = upb_DefPool_FindEnumByName(self->symtab, name_str);
340
185
  if (enumdef) {
341
186
  return get_enumdef_obj(_self, enumdef);
342
187
  }
@@ -358,10 +203,10 @@ static VALUE DescriptorPool_generated_pool(VALUE _self) {
358
203
  }
359
204
 
360
205
  static void DescriptorPool_register(VALUE module) {
361
- VALUE klass = rb_define_class_under(
362
- module, "DescriptorPool", rb_cObject);
206
+ VALUE klass = rb_define_class_under(module, "DescriptorPool", rb_cObject);
363
207
  rb_define_alloc_func(klass, DescriptorPool_alloc);
364
- rb_define_method(klass, "build", DescriptorPool_build, -1);
208
+ rb_define_method(klass, "add_serialized_file",
209
+ DescriptorPool_add_serialized_file, 1);
365
210
  rb_define_method(klass, "lookup", DescriptorPool_lookup, 1);
366
211
  rb_define_singleton_method(klass, "generated_pool",
367
212
  DescriptorPool_generated_pool, 0);
@@ -377,7 +222,7 @@ static void DescriptorPool_register(VALUE module) {
377
222
  // -----------------------------------------------------------------------------
378
223
 
379
224
  typedef struct {
380
- const upb_msgdef* msgdef;
225
+ const upb_MessageDef* msgdef;
381
226
  VALUE klass;
382
227
  VALUE descriptor_pool;
383
228
  } Descriptor;
@@ -391,9 +236,9 @@ static void Descriptor_mark(void* _self) {
391
236
  }
392
237
 
393
238
  static const rb_data_type_t Descriptor_type = {
394
- "Google::Protobuf::Descriptor",
395
- {Descriptor_mark, RUBY_DEFAULT_FREE, NULL},
396
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
239
+ "Google::Protobuf::Descriptor",
240
+ {Descriptor_mark, RUBY_DEFAULT_FREE, NULL},
241
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
397
242
  };
398
243
 
399
244
  static Descriptor* ruby_to_Descriptor(VALUE val) {
@@ -436,7 +281,7 @@ static VALUE Descriptor_initialize(VALUE _self, VALUE cookie,
436
281
  }
437
282
 
438
283
  self->descriptor_pool = descriptor_pool;
439
- self->msgdef = (const upb_msgdef*)NUM2ULL(ptr);
284
+ self->msgdef = (const upb_MessageDef*)NUM2ULL(ptr);
440
285
 
441
286
  return Qnil;
442
287
  }
@@ -449,7 +294,8 @@ static VALUE Descriptor_initialize(VALUE _self, VALUE cookie,
449
294
  */
450
295
  static VALUE Descriptor_file_descriptor(VALUE _self) {
451
296
  Descriptor* self = ruby_to_Descriptor(_self);
452
- return get_filedef_obj(self->descriptor_pool, upb_msgdef_file(self->msgdef));
297
+ return get_filedef_obj(self->descriptor_pool,
298
+ upb_MessageDef_File(self->msgdef));
453
299
  }
454
300
 
455
301
  /*
@@ -461,7 +307,7 @@ static VALUE Descriptor_file_descriptor(VALUE _self) {
461
307
  */
462
308
  static VALUE Descriptor_name(VALUE _self) {
463
309
  Descriptor* self = ruby_to_Descriptor(_self);
464
- return rb_str_maybe_null(upb_msgdef_fullname(self->msgdef));
310
+ return rb_str_maybe_null(upb_MessageDef_FullName(self->msgdef));
465
311
  }
466
312
 
467
313
  /*
@@ -473,11 +319,9 @@ static VALUE Descriptor_name(VALUE _self) {
473
319
  static VALUE Descriptor_each(VALUE _self) {
474
320
  Descriptor* self = ruby_to_Descriptor(_self);
475
321
 
476
- upb_msg_field_iter it;
477
- for (upb_msg_field_begin(&it, self->msgdef);
478
- !upb_msg_field_done(&it);
479
- upb_msg_field_next(&it)) {
480
- const upb_fielddef* field = upb_msg_iter_field(&it);
322
+ int n = upb_MessageDef_FieldCount(self->msgdef);
323
+ for (int i = 0; i < n; i++) {
324
+ const upb_FieldDef* field = upb_MessageDef_Field(self->msgdef, i);
481
325
  VALUE obj = get_fielddef_obj(self->descriptor_pool, field);
482
326
  rb_yield(obj);
483
327
  }
@@ -494,7 +338,7 @@ static VALUE Descriptor_each(VALUE _self) {
494
338
  static VALUE Descriptor_lookup(VALUE _self, VALUE name) {
495
339
  Descriptor* self = ruby_to_Descriptor(_self);
496
340
  const char* s = get_str(name);
497
- const upb_fielddef* field = upb_msgdef_ntofz(self->msgdef, s);
341
+ const upb_FieldDef* field = upb_MessageDef_FindFieldByName(self->msgdef, s);
498
342
  if (field == NULL) {
499
343
  return Qnil;
500
344
  }
@@ -511,11 +355,9 @@ static VALUE Descriptor_lookup(VALUE _self, VALUE name) {
511
355
  static VALUE Descriptor_each_oneof(VALUE _self) {
512
356
  Descriptor* self = ruby_to_Descriptor(_self);
513
357
 
514
- upb_msg_oneof_iter it;
515
- for (upb_msg_oneof_begin(&it, self->msgdef);
516
- !upb_msg_oneof_done(&it);
517
- upb_msg_oneof_next(&it)) {
518
- const upb_oneofdef* oneof = upb_msg_iter_oneof(&it);
358
+ int n = upb_MessageDef_OneofCount(self->msgdef);
359
+ for (int i = 0; i < n; i++) {
360
+ const upb_OneofDef* oneof = upb_MessageDef_Oneof(self->msgdef, i);
519
361
  VALUE obj = get_oneofdef_obj(self->descriptor_pool, oneof);
520
362
  rb_yield(obj);
521
363
  }
@@ -532,7 +374,7 @@ static VALUE Descriptor_each_oneof(VALUE _self) {
532
374
  static VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
533
375
  Descriptor* self = ruby_to_Descriptor(_self);
534
376
  const char* s = get_str(name);
535
- const upb_oneofdef* oneof = upb_msgdef_ntooz(self->msgdef, s);
377
+ const upb_OneofDef* oneof = upb_MessageDef_FindOneofByName(self->msgdef, s);
536
378
  if (oneof == NULL) {
537
379
  return Qnil;
538
380
  }
@@ -554,8 +396,7 @@ static VALUE Descriptor_msgclass(VALUE _self) {
554
396
  }
555
397
 
556
398
  static void Descriptor_register(VALUE module) {
557
- VALUE klass = rb_define_class_under(
558
- module, "Descriptor", rb_cObject);
399
+ VALUE klass = rb_define_class_under(module, "Descriptor", rb_cObject);
559
400
  rb_define_alloc_func(klass, Descriptor_alloc);
560
401
  rb_define_method(klass, "initialize", Descriptor_initialize, 3);
561
402
  rb_define_method(klass, "each", Descriptor_each, 0);
@@ -575,8 +416,8 @@ static void Descriptor_register(VALUE module) {
575
416
  // -----------------------------------------------------------------------------
576
417
 
577
418
  typedef struct {
578
- const upb_filedef* filedef;
579
- VALUE descriptor_pool; // Owns the upb_filedef.
419
+ const upb_FileDef* filedef;
420
+ VALUE descriptor_pool; // Owns the upb_FileDef.
580
421
  } FileDescriptor;
581
422
 
582
423
  static VALUE cFileDescriptor = Qnil;
@@ -587,9 +428,9 @@ static void FileDescriptor_mark(void* _self) {
587
428
  }
588
429
 
589
430
  static const rb_data_type_t FileDescriptor_type = {
590
- "Google::Protobuf::FileDescriptor",
591
- {FileDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
592
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
431
+ "Google::Protobuf::FileDescriptor",
432
+ {FileDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
433
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
593
434
  };
594
435
 
595
436
  static FileDescriptor* ruby_to_FileDescriptor(VALUE val) {
@@ -614,7 +455,7 @@ static VALUE FileDescriptor_alloc(VALUE klass) {
614
455
  * to a builder.
615
456
  */
616
457
  static VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
617
- VALUE descriptor_pool, VALUE ptr) {
458
+ VALUE descriptor_pool, VALUE ptr) {
618
459
  FileDescriptor* self = ruby_to_FileDescriptor(_self);
619
460
 
620
461
  if (cookie != c_only_cookie) {
@@ -623,7 +464,7 @@ static VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
623
464
  }
624
465
 
625
466
  self->descriptor_pool = descriptor_pool;
626
- self->filedef = (const upb_filedef*)NUM2ULL(ptr);
467
+ self->filedef = (const upb_FileDef*)NUM2ULL(ptr);
627
468
 
628
469
  return Qnil;
629
470
  }
@@ -636,7 +477,7 @@ static VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
636
477
  */
637
478
  static VALUE FileDescriptor_name(VALUE _self) {
638
479
  FileDescriptor* self = ruby_to_FileDescriptor(_self);
639
- const char* name = upb_filedef_name(self->filedef);
480
+ const char* name = upb_FileDef_Name(self->filedef);
640
481
  return name == NULL ? Qnil : rb_str_new2(name);
641
482
  }
642
483
 
@@ -652,16 +493,18 @@ static VALUE FileDescriptor_name(VALUE _self) {
652
493
  static VALUE FileDescriptor_syntax(VALUE _self) {
653
494
  FileDescriptor* self = ruby_to_FileDescriptor(_self);
654
495
 
655
- switch (upb_filedef_syntax(self->filedef)) {
656
- case UPB_SYNTAX_PROTO3: return ID2SYM(rb_intern("proto3"));
657
- case UPB_SYNTAX_PROTO2: return ID2SYM(rb_intern("proto2"));
658
- default: return Qnil;
496
+ switch (upb_FileDef_Syntax(self->filedef)) {
497
+ case kUpb_Syntax_Proto3:
498
+ return ID2SYM(rb_intern("proto3"));
499
+ case kUpb_Syntax_Proto2:
500
+ return ID2SYM(rb_intern("proto2"));
501
+ default:
502
+ return Qnil;
659
503
  }
660
504
  }
661
505
 
662
506
  static void FileDescriptor_register(VALUE module) {
663
- VALUE klass = rb_define_class_under(
664
- module, "FileDescriptor", rb_cObject);
507
+ VALUE klass = rb_define_class_under(module, "FileDescriptor", rb_cObject);
665
508
  rb_define_alloc_func(klass, FileDescriptor_alloc);
666
509
  rb_define_method(klass, "initialize", FileDescriptor_initialize, 3);
667
510
  rb_define_method(klass, "name", FileDescriptor_name, 0);
@@ -675,8 +518,8 @@ static void FileDescriptor_register(VALUE module) {
675
518
  // -----------------------------------------------------------------------------
676
519
 
677
520
  typedef struct {
678
- const upb_fielddef* fielddef;
679
- VALUE descriptor_pool; // Owns the upb_fielddef.
521
+ const upb_FieldDef* fielddef;
522
+ VALUE descriptor_pool; // Owns the upb_FieldDef.
680
523
  } FieldDescriptor;
681
524
 
682
525
  static VALUE cFieldDescriptor = Qnil;
@@ -687,9 +530,9 @@ static void FieldDescriptor_mark(void* _self) {
687
530
  }
688
531
 
689
532
  static const rb_data_type_t FieldDescriptor_type = {
690
- "Google::Protobuf::FieldDescriptor",
691
- {FieldDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
692
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
533
+ "Google::Protobuf::FieldDescriptor",
534
+ {FieldDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
535
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
693
536
  };
694
537
 
695
538
  static FieldDescriptor* ruby_to_FieldDescriptor(VALUE val) {
@@ -728,7 +571,7 @@ static VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
728
571
  }
729
572
 
730
573
  self->descriptor_pool = descriptor_pool;
731
- self->fielddef = (const upb_fielddef*)NUM2ULL(ptr);
574
+ self->fielddef = (const upb_FieldDef*)NUM2ULL(ptr);
732
575
 
733
576
  return Qnil;
734
577
  }
@@ -741,66 +584,31 @@ static VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
741
584
  */
742
585
  static VALUE FieldDescriptor_name(VALUE _self) {
743
586
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
744
- return rb_str_maybe_null(upb_fielddef_name(self->fielddef));
587
+ return rb_str_maybe_null(upb_FieldDef_Name(self->fielddef));
745
588
  }
746
589
 
747
590
  // Non-static, exposed to other .c files.
748
- upb_fieldtype_t ruby_to_fieldtype(VALUE type) {
749
- if (TYPE(type) != T_SYMBOL) {
750
- rb_raise(rb_eArgError, "Expected symbol for field type.");
751
- }
752
-
753
- #define CONVERT(upb, ruby) \
754
- if (SYM2ID(type) == rb_intern( # ruby )) { \
755
- return UPB_TYPE_ ## upb; \
756
- }
757
-
758
- CONVERT(FLOAT, float);
759
- CONVERT(DOUBLE, double);
760
- CONVERT(BOOL, bool);
761
- CONVERT(STRING, string);
762
- CONVERT(BYTES, bytes);
763
- CONVERT(MESSAGE, message);
764
- CONVERT(ENUM, enum);
765
- CONVERT(INT32, int32);
766
- CONVERT(INT64, int64);
767
- CONVERT(UINT32, uint32);
768
- CONVERT(UINT64, uint64);
769
-
770
- #undef CONVERT
771
-
772
- rb_raise(rb_eArgError, "Unknown field type.");
773
- return 0;
774
- }
775
-
776
- static upb_descriptortype_t ruby_to_descriptortype(VALUE type) {
591
+ upb_CType ruby_to_fieldtype(VALUE type) {
777
592
  if (TYPE(type) != T_SYMBOL) {
778
593
  rb_raise(rb_eArgError, "Expected symbol for field type.");
779
594
  }
780
595
 
781
- #define CONVERT(upb, ruby) \
782
- if (SYM2ID(type) == rb_intern( # ruby )) { \
783
- return UPB_DESCRIPTOR_TYPE_ ## upb; \
596
+ #define CONVERT(upb, ruby) \
597
+ if (SYM2ID(type) == rb_intern(#ruby)) { \
598
+ return kUpb_CType_##upb; \
784
599
  }
785
600
 
786
- CONVERT(FLOAT, float);
787
- CONVERT(DOUBLE, double);
788
- CONVERT(BOOL, bool);
789
- CONVERT(STRING, string);
790
- CONVERT(BYTES, bytes);
791
- CONVERT(MESSAGE, message);
792
- CONVERT(GROUP, group);
793
- CONVERT(ENUM, enum);
794
- CONVERT(INT32, int32);
795
- CONVERT(INT64, int64);
796
- CONVERT(UINT32, uint32);
797
- CONVERT(UINT64, uint64);
798
- CONVERT(SINT32, sint32);
799
- CONVERT(SINT64, sint64);
800
- CONVERT(FIXED32, fixed32);
801
- CONVERT(FIXED64, fixed64);
802
- CONVERT(SFIXED32, sfixed32);
803
- CONVERT(SFIXED64, sfixed64);
601
+ CONVERT(Float, float);
602
+ CONVERT(Double, double);
603
+ CONVERT(Bool, bool);
604
+ CONVERT(String, string);
605
+ CONVERT(Bytes, bytes);
606
+ CONVERT(Message, message);
607
+ CONVERT(Enum, enum);
608
+ CONVERT(Int32, int32);
609
+ CONVERT(Int64, int64);
610
+ CONVERT(UInt32, uint32);
611
+ CONVERT(UInt64, uint64);
804
612
 
805
613
  #undef CONVERT
806
614
 
@@ -808,28 +616,29 @@ static upb_descriptortype_t ruby_to_descriptortype(VALUE type) {
808
616
  return 0;
809
617
  }
810
618
 
811
- static VALUE descriptortype_to_ruby(upb_descriptortype_t type) {
619
+ static VALUE descriptortype_to_ruby(upb_FieldType type) {
812
620
  switch (type) {
813
- #define CONVERT(upb, ruby) \
814
- case UPB_DESCRIPTOR_TYPE_ ## upb : return ID2SYM(rb_intern( # ruby ));
815
- CONVERT(FLOAT, float);
816
- CONVERT(DOUBLE, double);
817
- CONVERT(BOOL, bool);
818
- CONVERT(STRING, string);
819
- CONVERT(BYTES, bytes);
820
- CONVERT(MESSAGE, message);
821
- CONVERT(GROUP, group);
822
- CONVERT(ENUM, enum);
823
- CONVERT(INT32, int32);
824
- CONVERT(INT64, int64);
825
- CONVERT(UINT32, uint32);
826
- CONVERT(UINT64, uint64);
827
- CONVERT(SINT32, sint32);
828
- CONVERT(SINT64, sint64);
829
- CONVERT(FIXED32, fixed32);
830
- CONVERT(FIXED64, fixed64);
831
- CONVERT(SFIXED32, sfixed32);
832
- CONVERT(SFIXED64, sfixed64);
621
+ #define CONVERT(upb, ruby) \
622
+ case kUpb_FieldType_##upb: \
623
+ return ID2SYM(rb_intern(#ruby));
624
+ CONVERT(Float, float);
625
+ CONVERT(Double, double);
626
+ CONVERT(Bool, bool);
627
+ CONVERT(String, string);
628
+ CONVERT(Bytes, bytes);
629
+ CONVERT(Message, message);
630
+ CONVERT(Group, group);
631
+ CONVERT(Enum, enum);
632
+ CONVERT(Int32, int32);
633
+ CONVERT(Int64, int64);
634
+ CONVERT(UInt32, uint32);
635
+ CONVERT(UInt64, uint64);
636
+ CONVERT(SInt32, sint32);
637
+ CONVERT(SInt64, sint64);
638
+ CONVERT(Fixed32, fixed32);
639
+ CONVERT(Fixed64, fixed64);
640
+ CONVERT(SFixed32, sfixed32);
641
+ CONVERT(SFixed64, sfixed64);
833
642
  #undef CONVERT
834
643
  }
835
644
  return Qnil;
@@ -847,7 +656,7 @@ static VALUE descriptortype_to_ruby(upb_descriptortype_t type) {
847
656
  */
848
657
  static VALUE FieldDescriptor__type(VALUE _self) {
849
658
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
850
- return descriptortype_to_ruby(upb_fielddef_descriptortype(self->fielddef));
659
+ return descriptortype_to_ruby(upb_FieldDef_Type(self->fielddef));
851
660
  }
852
661
 
853
662
  /*
@@ -858,16 +667,29 @@ static VALUE FieldDescriptor__type(VALUE _self) {
858
667
  */
859
668
  static VALUE FieldDescriptor_default(VALUE _self) {
860
669
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
861
- const upb_fielddef *f = self->fielddef;
862
- upb_msgval default_val = {0};
863
- if (upb_fielddef_issubmsg(f)) {
670
+ const upb_FieldDef* f = self->fielddef;
671
+ upb_MessageValue default_val = {0};
672
+ if (upb_FieldDef_IsSubMessage(f)) {
864
673
  return Qnil;
865
- } else if (!upb_fielddef_isseq(f)) {
866
- default_val = upb_fielddef_default(f);
674
+ } else if (!upb_FieldDef_IsRepeated(f)) {
675
+ default_val = upb_FieldDef_Default(f);
867
676
  }
868
677
  return Convert_UpbToRuby(default_val, TypeInfo_get(self->fielddef), Qnil);
869
678
  }
870
679
 
680
+ /*
681
+ * call-seq:
682
+ * FieldDescriptor.json_name => json_name
683
+ *
684
+ * Returns this field's json_name, as a Ruby string, or nil if not yet set.
685
+ */
686
+ static VALUE FieldDescriptor_json_name(VALUE _self) {
687
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
688
+ const upb_FieldDef* f = self->fielddef;
689
+ const char* json_name = upb_FieldDef_JsonName(f);
690
+ return rb_str_new2(json_name);
691
+ }
692
+
871
693
  /*
872
694
  * call-seq:
873
695
  * FieldDescriptor.label => label
@@ -879,13 +701,14 @@ static VALUE FieldDescriptor_default(VALUE _self) {
879
701
  */
880
702
  static VALUE FieldDescriptor_label(VALUE _self) {
881
703
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
882
- switch (upb_fielddef_label(self->fielddef)) {
883
- #define CONVERT(upb, ruby) \
884
- case UPB_LABEL_ ## upb : return ID2SYM(rb_intern( # ruby ));
704
+ switch (upb_FieldDef_Label(self->fielddef)) {
705
+ #define CONVERT(upb, ruby) \
706
+ case kUpb_Label_##upb: \
707
+ return ID2SYM(rb_intern(#ruby));
885
708
 
886
- CONVERT(OPTIONAL, optional);
887
- CONVERT(REQUIRED, required);
888
- CONVERT(REPEATED, repeated);
709
+ CONVERT(Optional, optional);
710
+ CONVERT(Required, required);
711
+ CONVERT(Repeated, repeated);
889
712
 
890
713
  #undef CONVERT
891
714
  }
@@ -901,7 +724,7 @@ static VALUE FieldDescriptor_label(VALUE _self) {
901
724
  */
902
725
  static VALUE FieldDescriptor_number(VALUE _self) {
903
726
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
904
- return INT2NUM(upb_fielddef_number(self->fielddef));
727
+ return INT2NUM(upb_FieldDef_Number(self->fielddef));
905
728
  }
906
729
 
907
730
  /*
@@ -915,13 +738,13 @@ static VALUE FieldDescriptor_number(VALUE _self) {
915
738
  */
916
739
  static VALUE FieldDescriptor_submsg_name(VALUE _self) {
917
740
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
918
- switch (upb_fielddef_type(self->fielddef)) {
919
- case UPB_TYPE_ENUM:
741
+ switch (upb_FieldDef_CType(self->fielddef)) {
742
+ case kUpb_CType_Enum:
920
743
  return rb_str_new2(
921
- upb_enumdef_fullname(upb_fielddef_enumsubdef(self->fielddef)));
922
- case UPB_TYPE_MESSAGE:
744
+ upb_EnumDef_FullName(upb_FieldDef_EnumSubDef(self->fielddef)));
745
+ case kUpb_CType_Message:
923
746
  return rb_str_new2(
924
- upb_msgdef_fullname(upb_fielddef_msgsubdef(self->fielddef)));
747
+ upb_MessageDef_FullName(upb_FieldDef_MessageSubDef(self->fielddef)));
925
748
  default:
926
749
  return Qnil;
927
750
  }
@@ -938,13 +761,13 @@ static VALUE FieldDescriptor_submsg_name(VALUE _self) {
938
761
  */
939
762
  static VALUE FieldDescriptor_subtype(VALUE _self) {
940
763
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
941
- switch (upb_fielddef_type(self->fielddef)) {
942
- case UPB_TYPE_ENUM:
764
+ switch (upb_FieldDef_CType(self->fielddef)) {
765
+ case kUpb_CType_Enum:
943
766
  return get_enumdef_obj(self->descriptor_pool,
944
- upb_fielddef_enumsubdef(self->fielddef));
945
- case UPB_TYPE_MESSAGE:
767
+ upb_FieldDef_EnumSubDef(self->fielddef));
768
+ case kUpb_CType_Message:
946
769
  return get_msgdef_obj(self->descriptor_pool,
947
- upb_fielddef_msgsubdef(self->fielddef));
770
+ upb_FieldDef_MessageSubDef(self->fielddef));
948
771
  default:
949
772
  return Qnil;
950
773
  }
@@ -959,11 +782,11 @@ static VALUE FieldDescriptor_subtype(VALUE _self) {
959
782
  */
960
783
  static VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
961
784
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
962
- const upb_msgdef *m;
785
+ const upb_MessageDef* m;
963
786
 
964
787
  Message_Get(msg_rb, &m);
965
788
 
966
- if (m != upb_fielddef_containingtype(self->fielddef)) {
789
+ if (m != upb_FieldDef_ContainingType(self->fielddef)) {
967
790
  rb_raise(cTypeError, "get method called on wrong message type");
968
791
  }
969
792
 
@@ -979,16 +802,16 @@ static VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
979
802
  */
980
803
  static VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
981
804
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
982
- const upb_msgdef *m;
983
- const upb_msgdef *msg = Message_Get(msg_rb, &m);
805
+ const upb_MessageDef* m;
806
+ const upb_MessageDef* msg = Message_Get(msg_rb, &m);
984
807
 
985
- if (m != upb_fielddef_containingtype(self->fielddef)) {
808
+ if (m != upb_FieldDef_ContainingType(self->fielddef)) {
986
809
  rb_raise(cTypeError, "has method called on wrong message type");
987
- } else if (!upb_fielddef_haspresence(self->fielddef)) {
810
+ } else if (!upb_FieldDef_HasPresence(self->fielddef)) {
988
811
  rb_raise(rb_eArgError, "does not track presence");
989
812
  }
990
813
 
991
- return upb_msg_has(msg, self->fielddef) ? Qtrue : Qfalse;
814
+ return upb_Message_HasFieldByDef(msg, self->fielddef) ? Qtrue : Qfalse;
992
815
  }
993
816
 
994
817
  /*
@@ -999,14 +822,14 @@ static VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
999
822
  */
1000
823
  static VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {
1001
824
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
1002
- const upb_msgdef *m;
1003
- upb_msgdef *msg = Message_GetMutable(msg_rb, &m);
825
+ const upb_MessageDef* m;
826
+ upb_MessageDef* msg = Message_GetMutable(msg_rb, &m);
1004
827
 
1005
- if (m != upb_fielddef_containingtype(self->fielddef)) {
828
+ if (m != upb_FieldDef_ContainingType(self->fielddef)) {
1006
829
  rb_raise(cTypeError, "has method called on wrong message type");
1007
830
  }
1008
831
 
1009
- upb_msg_clearfield(msg, self->fielddef);
832
+ upb_Message_ClearFieldByDef(msg, self->fielddef);
1010
833
  return Qnil;
1011
834
  }
1012
835
 
@@ -1020,29 +843,29 @@ static VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {
1020
843
  */
1021
844
  static VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value) {
1022
845
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
1023
- const upb_msgdef *m;
1024
- upb_msgdef *msg = Message_GetMutable(msg_rb, &m);
1025
- upb_arena *arena = Arena_get(Message_GetArena(msg_rb));
1026
- upb_msgval msgval;
846
+ const upb_MessageDef* m;
847
+ upb_MessageDef* msg = Message_GetMutable(msg_rb, &m);
848
+ upb_Arena* arena = Arena_get(Message_GetArena(msg_rb));
849
+ upb_MessageValue msgval;
1027
850
 
1028
- if (m != upb_fielddef_containingtype(self->fielddef)) {
851
+ if (m != upb_FieldDef_ContainingType(self->fielddef)) {
1029
852
  rb_raise(cTypeError, "set method called on wrong message type");
1030
853
  }
1031
854
 
1032
- msgval = Convert_RubyToUpb(value, upb_fielddef_name(self->fielddef),
855
+ msgval = Convert_RubyToUpb(value, upb_FieldDef_Name(self->fielddef),
1033
856
  TypeInfo_get(self->fielddef), arena);
1034
- upb_msg_set(msg, self->fielddef, msgval, arena);
857
+ upb_Message_SetFieldByDef(msg, self->fielddef, msgval, arena);
1035
858
  return Qnil;
1036
859
  }
1037
860
 
1038
861
  static void FieldDescriptor_register(VALUE module) {
1039
- VALUE klass = rb_define_class_under(
1040
- module, "FieldDescriptor", rb_cObject);
862
+ VALUE klass = rb_define_class_under(module, "FieldDescriptor", rb_cObject);
1041
863
  rb_define_alloc_func(klass, FieldDescriptor_alloc);
1042
864
  rb_define_method(klass, "initialize", FieldDescriptor_initialize, 3);
1043
865
  rb_define_method(klass, "name", FieldDescriptor_name, 0);
1044
866
  rb_define_method(klass, "type", FieldDescriptor__type, 0);
1045
867
  rb_define_method(klass, "default", FieldDescriptor_default, 0);
868
+ rb_define_method(klass, "json_name", FieldDescriptor_json_name, 0);
1046
869
  rb_define_method(klass, "label", FieldDescriptor_label, 0);
1047
870
  rb_define_method(klass, "number", FieldDescriptor_number, 0);
1048
871
  rb_define_method(klass, "submsg_name", FieldDescriptor_submsg_name, 0);
@@ -1060,8 +883,8 @@ static void FieldDescriptor_register(VALUE module) {
1060
883
  // -----------------------------------------------------------------------------
1061
884
 
1062
885
  typedef struct {
1063
- const upb_oneofdef* oneofdef;
1064
- VALUE descriptor_pool; // Owns the upb_oneofdef.
886
+ const upb_OneofDef* oneofdef;
887
+ VALUE descriptor_pool; // Owns the upb_OneofDef.
1065
888
  } OneofDescriptor;
1066
889
 
1067
890
  static VALUE cOneofDescriptor = Qnil;
@@ -1105,7 +928,7 @@ static VALUE OneofDescriptor_alloc(VALUE klass) {
1105
928
  * Creates a descriptor wrapper object. May only be called from C.
1106
929
  */
1107
930
  static VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
1108
- VALUE descriptor_pool, VALUE ptr) {
931
+ VALUE descriptor_pool, VALUE ptr) {
1109
932
  OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
1110
933
 
1111
934
  if (cookie != c_only_cookie) {
@@ -1114,7 +937,7 @@ static VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
1114
937
  }
1115
938
 
1116
939
  self->descriptor_pool = descriptor_pool;
1117
- self->oneofdef = (const upb_oneofdef*)NUM2ULL(ptr);
940
+ self->oneofdef = (const upb_OneofDef*)NUM2ULL(ptr);
1118
941
 
1119
942
  return Qnil;
1120
943
  }
@@ -1127,7 +950,7 @@ static VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
1127
950
  */
1128
951
  static VALUE OneofDescriptor_name(VALUE _self) {
1129
952
  OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
1130
- return rb_str_maybe_null(upb_oneofdef_name(self->oneofdef));
953
+ return rb_str_maybe_null(upb_OneofDef_Name(self->oneofdef));
1131
954
  }
1132
955
 
1133
956
  /*
@@ -1138,11 +961,10 @@ static VALUE OneofDescriptor_name(VALUE _self) {
1138
961
  */
1139
962
  static VALUE OneofDescriptor_each(VALUE _self) {
1140
963
  OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
1141
- upb_oneof_iter it;
1142
- for (upb_oneof_begin(&it, self->oneofdef);
1143
- !upb_oneof_done(&it);
1144
- upb_oneof_next(&it)) {
1145
- const upb_fielddef* f = upb_oneof_iter_field(&it);
964
+
965
+ int n = upb_OneofDef_FieldCount(self->oneofdef);
966
+ for (int i = 0; i < n; i++) {
967
+ const upb_FieldDef* f = upb_OneofDef_Field(self->oneofdef, i);
1146
968
  VALUE obj = get_fielddef_obj(self->descriptor_pool, f);
1147
969
  rb_yield(obj);
1148
970
  }
@@ -1150,8 +972,7 @@ static VALUE OneofDescriptor_each(VALUE _self) {
1150
972
  }
1151
973
 
1152
974
  static void OneofDescriptor_register(VALUE module) {
1153
- VALUE klass = rb_define_class_under(
1154
- module, "OneofDescriptor", rb_cObject);
975
+ VALUE klass = rb_define_class_under(module, "OneofDescriptor", rb_cObject);
1155
976
  rb_define_alloc_func(klass, OneofDescriptor_alloc);
1156
977
  rb_define_method(klass, "initialize", OneofDescriptor_initialize, 3);
1157
978
  rb_define_method(klass, "name", OneofDescriptor_name, 0);
@@ -1166,9 +987,9 @@ static void OneofDescriptor_register(VALUE module) {
1166
987
  // -----------------------------------------------------------------------------
1167
988
 
1168
989
  typedef struct {
1169
- const upb_enumdef* enumdef;
1170
- VALUE module; // begins as nil
1171
- VALUE descriptor_pool; // Owns the upb_enumdef.
990
+ const upb_EnumDef* enumdef;
991
+ VALUE module; // begins as nil
992
+ VALUE descriptor_pool; // Owns the upb_EnumDef.
1172
993
  } EnumDescriptor;
1173
994
 
1174
995
  static VALUE cEnumDescriptor = Qnil;
@@ -1180,9 +1001,9 @@ static void EnumDescriptor_mark(void* _self) {
1180
1001
  }
1181
1002
 
1182
1003
  static const rb_data_type_t EnumDescriptor_type = {
1183
- "Google::Protobuf::EnumDescriptor",
1184
- {EnumDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
1185
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
1004
+ "Google::Protobuf::EnumDescriptor",
1005
+ {EnumDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
1006
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
1186
1007
  };
1187
1008
 
1188
1009
  static EnumDescriptor* ruby_to_EnumDescriptor(VALUE val) {
@@ -1201,8 +1022,8 @@ static VALUE EnumDescriptor_alloc(VALUE klass) {
1201
1022
  }
1202
1023
 
1203
1024
  // Exposed to other modules in defs.h.
1204
- const upb_enumdef *EnumDescriptor_GetEnumDef(VALUE enum_desc_rb) {
1205
- EnumDescriptor *desc = ruby_to_EnumDescriptor(enum_desc_rb);
1025
+ const upb_EnumDef* EnumDescriptor_GetEnumDef(VALUE enum_desc_rb) {
1026
+ EnumDescriptor* desc = ruby_to_EnumDescriptor(enum_desc_rb);
1206
1027
  return desc->enumdef;
1207
1028
  }
1208
1029
 
@@ -1222,7 +1043,7 @@ static VALUE EnumDescriptor_initialize(VALUE _self, VALUE cookie,
1222
1043
  }
1223
1044
 
1224
1045
  self->descriptor_pool = descriptor_pool;
1225
- self->enumdef = (const upb_enumdef*)NUM2ULL(ptr);
1046
+ self->enumdef = (const upb_EnumDef*)NUM2ULL(ptr);
1226
1047
 
1227
1048
  return Qnil;
1228
1049
  }
@@ -1236,7 +1057,7 @@ static VALUE EnumDescriptor_initialize(VALUE _self, VALUE cookie,
1236
1057
  static VALUE EnumDescriptor_file_descriptor(VALUE _self) {
1237
1058
  EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1238
1059
  return get_filedef_obj(self->descriptor_pool,
1239
- upb_enumdef_file(self->enumdef));
1060
+ upb_EnumDef_File(self->enumdef));
1240
1061
  }
1241
1062
 
1242
1063
  /*
@@ -1247,7 +1068,7 @@ static VALUE EnumDescriptor_file_descriptor(VALUE _self) {
1247
1068
  */
1248
1069
  static VALUE EnumDescriptor_name(VALUE _self) {
1249
1070
  EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1250
- return rb_str_maybe_null(upb_enumdef_fullname(self->enumdef));
1071
+ return rb_str_maybe_null(upb_EnumDef_FullName(self->enumdef));
1251
1072
  }
1252
1073
 
1253
1074
  /*
@@ -1259,10 +1080,11 @@ static VALUE EnumDescriptor_name(VALUE _self) {
1259
1080
  */
1260
1081
  static VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
1261
1082
  EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1262
- const char* name_str= rb_id2name(SYM2ID(name));
1263
- int32_t val = 0;
1264
- if (upb_enumdef_ntoiz(self->enumdef, name_str, &val)) {
1265
- return INT2NUM(val);
1083
+ const char* name_str = rb_id2name(SYM2ID(name));
1084
+ const upb_EnumValueDef *ev =
1085
+ upb_EnumDef_FindValueByName(self->enumdef, name_str);
1086
+ if (ev) {
1087
+ return INT2NUM(upb_EnumValueDef_Number(ev));
1266
1088
  } else {
1267
1089
  return Qnil;
1268
1090
  }
@@ -1278,9 +1100,9 @@ static VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
1278
1100
  static VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
1279
1101
  EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1280
1102
  int32_t val = NUM2INT(number);
1281
- const char* name = upb_enumdef_iton(self->enumdef, val);
1282
- if (name != NULL) {
1283
- return ID2SYM(rb_intern(name));
1103
+ const upb_EnumValueDef* ev = upb_EnumDef_FindValueByNumber(self->enumdef, val);
1104
+ if (ev) {
1105
+ return ID2SYM(rb_intern(upb_EnumValueDef_Name(ev)));
1284
1106
  } else {
1285
1107
  return Qnil;
1286
1108
  }
@@ -1296,12 +1118,11 @@ static VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
1296
1118
  static VALUE EnumDescriptor_each(VALUE _self) {
1297
1119
  EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1298
1120
 
1299
- upb_enum_iter it;
1300
- for (upb_enum_begin(&it, self->enumdef);
1301
- !upb_enum_done(&it);
1302
- upb_enum_next(&it)) {
1303
- VALUE key = ID2SYM(rb_intern(upb_enum_iter_name(&it)));
1304
- VALUE number = INT2NUM(upb_enum_iter_number(&it));
1121
+ int n = upb_EnumDef_ValueCount(self->enumdef);
1122
+ for (int i = 0; i < n; i++) {
1123
+ const upb_EnumValueDef* ev = upb_EnumDef_Value(self->enumdef, i);
1124
+ VALUE key = ID2SYM(rb_intern(upb_EnumValueDef_Name(ev)));
1125
+ VALUE number = INT2NUM(upb_EnumValueDef_Number(ev));
1305
1126
  rb_yield_values(2, key, number);
1306
1127
  }
1307
1128
 
@@ -1323,8 +1144,7 @@ static VALUE EnumDescriptor_enummodule(VALUE _self) {
1323
1144
  }
1324
1145
 
1325
1146
  static void EnumDescriptor_register(VALUE module) {
1326
- VALUE klass = rb_define_class_under(
1327
- module, "EnumDescriptor", rb_cObject);
1147
+ VALUE klass = rb_define_class_under(module, "EnumDescriptor", rb_cObject);
1328
1148
  rb_define_alloc_func(klass, EnumDescriptor_alloc);
1329
1149
  rb_define_method(klass, "initialize", EnumDescriptor_initialize, 3);
1330
1150
  rb_define_method(klass, "name", EnumDescriptor_name, 0);
@@ -1338,1099 +1158,6 @@ static void EnumDescriptor_register(VALUE module) {
1338
1158
  cEnumDescriptor = klass;
1339
1159
  }
1340
1160
 
1341
- // -----------------------------------------------------------------------------
1342
- // FileBuilderContext.
1343
- // -----------------------------------------------------------------------------
1344
-
1345
- typedef struct {
1346
- upb_arena *arena;
1347
- google_protobuf_FileDescriptorProto* file_proto;
1348
- VALUE descriptor_pool;
1349
- } FileBuilderContext;
1350
-
1351
- static VALUE cFileBuilderContext = Qnil;
1352
-
1353
- static void FileBuilderContext_mark(void* _self) {
1354
- FileBuilderContext* self = _self;
1355
- rb_gc_mark(self->descriptor_pool);
1356
- }
1357
-
1358
- static void FileBuilderContext_free(void* _self) {
1359
- FileBuilderContext* self = _self;
1360
- upb_arena_free(self->arena);
1361
- xfree(self);
1362
- }
1363
-
1364
- static const rb_data_type_t FileBuilderContext_type = {
1365
- "Google::Protobuf::Internal::FileBuilderContext",
1366
- {FileBuilderContext_mark, FileBuilderContext_free, NULL},
1367
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
1368
- };
1369
-
1370
- static FileBuilderContext* ruby_to_FileBuilderContext(VALUE val) {
1371
- FileBuilderContext* ret;
1372
- TypedData_Get_Struct(val, FileBuilderContext, &FileBuilderContext_type, ret);
1373
- return ret;
1374
- }
1375
-
1376
- static upb_strview FileBuilderContext_strdup2(VALUE _self, const char *str) {
1377
- FileBuilderContext* self = ruby_to_FileBuilderContext(_self);
1378
- upb_strview ret;
1379
- char *data;
1380
-
1381
- ret.size = strlen(str);
1382
- data = upb_malloc(upb_arena_alloc(self->arena), ret.size + 1);
1383
- ret.data = data;
1384
- memcpy(data, str, ret.size);
1385
- /* Null-terminate required by rewrite_enum_defaults() above. */
1386
- data[ret.size] = '\0';
1387
- return ret;
1388
- }
1389
-
1390
- static upb_strview FileBuilderContext_strdup(VALUE _self, VALUE rb_str) {
1391
- return FileBuilderContext_strdup2(_self, get_str(rb_str));
1392
- }
1393
-
1394
- static upb_strview FileBuilderContext_strdup_sym(VALUE _self, VALUE rb_sym) {
1395
- Check_Type(rb_sym, T_SYMBOL);
1396
- return FileBuilderContext_strdup(_self, rb_id2str(SYM2ID(rb_sym)));
1397
- }
1398
-
1399
- static VALUE FileBuilderContext_alloc(VALUE klass) {
1400
- FileBuilderContext* self = ALLOC(FileBuilderContext);
1401
- VALUE ret = TypedData_Wrap_Struct(klass, &FileBuilderContext_type, self);
1402
- self->arena = upb_arena_new();
1403
- self->file_proto = google_protobuf_FileDescriptorProto_new(self->arena);
1404
- self->descriptor_pool = Qnil;
1405
- return ret;
1406
- }
1407
-
1408
- /*
1409
- * call-seq:
1410
- * FileBuilderContext.new(descriptor_pool) => context
1411
- *
1412
- * Create a new file builder context for the given file descriptor and
1413
- * builder context. This class is intended to serve as a DSL context to be used
1414
- * with #instance_eval.
1415
- */
1416
- static VALUE FileBuilderContext_initialize(VALUE _self, VALUE descriptor_pool,
1417
- VALUE name, VALUE options) {
1418
- FileBuilderContext* self = ruby_to_FileBuilderContext(_self);
1419
- self->descriptor_pool = descriptor_pool;
1420
-
1421
- google_protobuf_FileDescriptorProto_set_name(
1422
- self->file_proto, FileBuilderContext_strdup(_self, name));
1423
-
1424
- // Default syntax for Ruby is proto3.
1425
- google_protobuf_FileDescriptorProto_set_syntax(
1426
- self->file_proto,
1427
- FileBuilderContext_strdup(_self, rb_str_new2("proto3")));
1428
-
1429
- if (options != Qnil) {
1430
- VALUE syntax;
1431
-
1432
- Check_Type(options, T_HASH);
1433
- syntax = rb_hash_lookup2(options, ID2SYM(rb_intern("syntax")), Qnil);
1434
-
1435
- if (syntax != Qnil) {
1436
- VALUE syntax_str;
1437
-
1438
- Check_Type(syntax, T_SYMBOL);
1439
- syntax_str = rb_id2str(SYM2ID(syntax));
1440
- google_protobuf_FileDescriptorProto_set_syntax(
1441
- self->file_proto, FileBuilderContext_strdup(_self, syntax_str));
1442
- }
1443
- }
1444
-
1445
- return Qnil;
1446
- }
1447
-
1448
- static void MessageBuilderContext_add_synthetic_oneofs(VALUE _self);
1449
-
1450
- /*
1451
- * call-seq:
1452
- * FileBuilderContext.add_message(name, &block)
1453
- *
1454
- * Creates a new, empty descriptor with the given name, and invokes the block in
1455
- * the context of a MessageBuilderContext on that descriptor. The block can then
1456
- * call, e.g., MessageBuilderContext#optional and MessageBuilderContext#repeated
1457
- * methods to define the message fields.
1458
- *
1459
- * This is the recommended, idiomatic way to build message definitions.
1460
- */
1461
- static VALUE FileBuilderContext_add_message(VALUE _self, VALUE name) {
1462
- VALUE args[2] = { _self, name };
1463
- VALUE ctx = rb_class_new_instance(2, args, cMessageBuilderContext);
1464
- VALUE block = rb_block_proc();
1465
- rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
1466
- MessageBuilderContext_add_synthetic_oneofs(ctx);
1467
- return Qnil;
1468
- }
1469
-
1470
- /* We have to do some relatively complicated logic here for backward
1471
- * compatibility.
1472
- *
1473
- * In descriptor.proto, messages are nested inside other messages if that is
1474
- * what the original .proto file looks like. For example, suppose we have this
1475
- * foo.proto:
1476
- *
1477
- * package foo;
1478
- * message Bar {
1479
- * message Baz {}
1480
- * }
1481
- *
1482
- * The descriptor for this must look like this:
1483
- *
1484
- * file {
1485
- * name: "test.proto"
1486
- * package: "foo"
1487
- * message_type {
1488
- * name: "Bar"
1489
- * nested_type {
1490
- * name: "Baz"
1491
- * }
1492
- * }
1493
- * }
1494
- *
1495
- * However, the Ruby generated code has always generated messages in a flat,
1496
- * non-nested way:
1497
- *
1498
- * Google::Protobuf::DescriptorPool.generated_pool.build do
1499
- * add_message "foo.Bar" do
1500
- * end
1501
- * add_message "foo.Bar.Baz" do
1502
- * end
1503
- * end
1504
- *
1505
- * Here we need to do a translation where we turn this generated code into the
1506
- * above descriptor. We need to infer that "foo" is the package name, and not
1507
- * a message itself.
1508
- *
1509
- * We delegate to Ruby to compute the transformation, for more concice and
1510
- * readable code than we can do in C */
1511
- static void rewrite_names(VALUE _file_builder,
1512
- google_protobuf_FileDescriptorProto* file_proto) {
1513
- FileBuilderContext* file_builder = ruby_to_FileBuilderContext(_file_builder);
1514
- upb_arena *arena = file_builder->arena;
1515
- // Build params (package, msg_names, enum_names).
1516
- VALUE package = Qnil;
1517
- VALUE msg_names = rb_ary_new();
1518
- VALUE enum_names = rb_ary_new();
1519
- size_t msg_count, enum_count, i;
1520
- VALUE new_package, nesting, msg_ents, enum_ents;
1521
- google_protobuf_DescriptorProto** msgs;
1522
- google_protobuf_EnumDescriptorProto** enums;
1523
-
1524
- if (google_protobuf_FileDescriptorProto_has_package(file_proto)) {
1525
- upb_strview package_str =
1526
- google_protobuf_FileDescriptorProto_package(file_proto);
1527
- package = rb_str_new(package_str.data, package_str.size);
1528
- }
1529
-
1530
- msgs = google_protobuf_FileDescriptorProto_mutable_message_type(file_proto,
1531
- &msg_count);
1532
- for (i = 0; i < msg_count; i++) {
1533
- upb_strview name = google_protobuf_DescriptorProto_name(msgs[i]);
1534
- rb_ary_push(msg_names, rb_str_new(name.data, name.size));
1535
- }
1536
-
1537
- enums = google_protobuf_FileDescriptorProto_mutable_enum_type(file_proto,
1538
- &enum_count);
1539
- for (i = 0; i < enum_count; i++) {
1540
- upb_strview name = google_protobuf_EnumDescriptorProto_name(enums[i]);
1541
- rb_ary_push(enum_names, rb_str_new(name.data, name.size));
1542
- }
1543
-
1544
- {
1545
- // Call Ruby code to calculate package name and nesting.
1546
- VALUE args[3] = { package, msg_names, enum_names };
1547
- VALUE internal = rb_eval_string("Google::Protobuf::Internal");
1548
- VALUE ret = rb_funcallv(internal, rb_intern("fixup_descriptor"), 3, args);
1549
-
1550
- new_package = rb_ary_entry(ret, 0);
1551
- nesting = rb_ary_entry(ret, 1);
1552
- }
1553
-
1554
- // Rewrite package and names.
1555
- if (new_package != Qnil) {
1556
- upb_strview new_package_str =
1557
- FileBuilderContext_strdup(_file_builder, new_package);
1558
- google_protobuf_FileDescriptorProto_set_package(file_proto,
1559
- new_package_str);
1560
- }
1561
-
1562
- for (i = 0; i < msg_count; i++) {
1563
- upb_strview name = google_protobuf_DescriptorProto_name(msgs[i]);
1564
- remove_path(&name);
1565
- google_protobuf_DescriptorProto_set_name(msgs[i], name);
1566
- }
1567
-
1568
- for (i = 0; i < enum_count; i++) {
1569
- upb_strview name = google_protobuf_EnumDescriptorProto_name(enums[i]);
1570
- remove_path(&name);
1571
- google_protobuf_EnumDescriptorProto_set_name(enums[i], name);
1572
- }
1573
-
1574
- // Rewrite nesting.
1575
- msg_ents = rb_hash_aref(nesting, ID2SYM(rb_intern("msgs")));
1576
- enum_ents = rb_hash_aref(nesting, ID2SYM(rb_intern("enums")));
1577
-
1578
- Check_Type(msg_ents, T_ARRAY);
1579
- Check_Type(enum_ents, T_ARRAY);
1580
-
1581
- for (i = 0; i < (size_t)RARRAY_LEN(msg_ents); i++) {
1582
- VALUE msg_ent = rb_ary_entry(msg_ents, i);
1583
- VALUE pos = rb_hash_aref(msg_ent, ID2SYM(rb_intern("pos")));
1584
- msgs[i] = msgs[NUM2INT(pos)];
1585
- rewrite_nesting(msg_ent, msgs[i], msgs, enums, arena);
1586
- }
1587
-
1588
- for (i = 0; i < (size_t)RARRAY_LEN(enum_ents); i++) {
1589
- VALUE enum_pos = rb_ary_entry(enum_ents, i);
1590
- enums[i] = enums[NUM2INT(enum_pos)];
1591
- }
1592
-
1593
- google_protobuf_FileDescriptorProto_resize_message_type(
1594
- file_proto, RARRAY_LEN(msg_ents), arena);
1595
- google_protobuf_FileDescriptorProto_resize_enum_type(
1596
- file_proto, RARRAY_LEN(enum_ents), arena);
1597
- }
1598
-
1599
- /*
1600
- * call-seq:
1601
- * FileBuilderContext.add_enum(name, &block)
1602
- *
1603
- * Creates a new, empty enum descriptor with the given name, and invokes the
1604
- * block in the context of an EnumBuilderContext on that descriptor. The block
1605
- * can then call EnumBuilderContext#add_value to define the enum values.
1606
- *
1607
- * This is the recommended, idiomatic way to build enum definitions.
1608
- */
1609
- static VALUE FileBuilderContext_add_enum(VALUE _self, VALUE name) {
1610
- VALUE args[2] = { _self, name };
1611
- VALUE ctx = rb_class_new_instance(2, args, cEnumBuilderContext);
1612
- VALUE block = rb_block_proc();
1613
- rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
1614
- return Qnil;
1615
- }
1616
-
1617
- static void FileBuilderContext_build(VALUE _self) {
1618
- FileBuilderContext* self = ruby_to_FileBuilderContext(_self);
1619
- DescriptorPool* pool = ruby_to_DescriptorPool(self->descriptor_pool);
1620
- upb_status status;
1621
-
1622
- rewrite_enum_defaults(pool->symtab, self->file_proto);
1623
- rewrite_names(_self, self->file_proto);
1624
-
1625
- upb_status_clear(&status);
1626
- if (!upb_symtab_addfile(pool->symtab, self->file_proto, &status)) {
1627
- rb_raise(cTypeError, "Unable to add defs to DescriptorPool: %s",
1628
- upb_status_errmsg(&status));
1629
- }
1630
- }
1631
-
1632
- static void FileBuilderContext_register(VALUE module) {
1633
- VALUE klass = rb_define_class_under(module, "FileBuilderContext", rb_cObject);
1634
- rb_define_alloc_func(klass, FileBuilderContext_alloc);
1635
- rb_define_method(klass, "initialize", FileBuilderContext_initialize, 3);
1636
- rb_define_method(klass, "add_message", FileBuilderContext_add_message, 1);
1637
- rb_define_method(klass, "add_enum", FileBuilderContext_add_enum, 1);
1638
- rb_gc_register_address(&cFileBuilderContext);
1639
- cFileBuilderContext = klass;
1640
- }
1641
-
1642
- // -----------------------------------------------------------------------------
1643
- // MessageBuilderContext.
1644
- // -----------------------------------------------------------------------------
1645
-
1646
- typedef struct {
1647
- google_protobuf_DescriptorProto* msg_proto;
1648
- VALUE file_builder;
1649
- } MessageBuilderContext;
1650
-
1651
- static VALUE cMessageBuilderContext = Qnil;
1652
-
1653
- static void MessageBuilderContext_mark(void* _self) {
1654
- MessageBuilderContext* self = _self;
1655
- rb_gc_mark(self->file_builder);
1656
- }
1657
-
1658
- static const rb_data_type_t MessageBuilderContext_type = {
1659
- "Google::Protobuf::Internal::MessageBuilderContext",
1660
- {MessageBuilderContext_mark, RUBY_DEFAULT_FREE, NULL},
1661
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
1662
- };
1663
-
1664
- static MessageBuilderContext* ruby_to_MessageBuilderContext(VALUE val) {
1665
- MessageBuilderContext* ret;
1666
- TypedData_Get_Struct(val, MessageBuilderContext, &MessageBuilderContext_type,
1667
- ret);
1668
- return ret;
1669
- }
1670
-
1671
- static VALUE MessageBuilderContext_alloc(VALUE klass) {
1672
- MessageBuilderContext* self = ALLOC(MessageBuilderContext);
1673
- VALUE ret = TypedData_Wrap_Struct(klass, &MessageBuilderContext_type, self);
1674
- self->file_builder = Qnil;
1675
- return ret;
1676
- }
1677
-
1678
- /*
1679
- * call-seq:
1680
- * MessageBuilderContext.new(file_builder, name) => context
1681
- *
1682
- * Create a new message builder context around the given message descriptor and
1683
- * builder context. This class is intended to serve as a DSL context to be used
1684
- * with #instance_eval.
1685
- */
1686
- static VALUE MessageBuilderContext_initialize(VALUE _self, VALUE _file_builder,
1687
- VALUE name) {
1688
- MessageBuilderContext* self = ruby_to_MessageBuilderContext(_self);
1689
- FileBuilderContext* file_builder = ruby_to_FileBuilderContext(_file_builder);
1690
- google_protobuf_FileDescriptorProto* file_proto = file_builder->file_proto;
1691
-
1692
- self->file_builder = _file_builder;
1693
- self->msg_proto = google_protobuf_FileDescriptorProto_add_message_type(
1694
- file_proto, file_builder->arena);
1695
-
1696
- google_protobuf_DescriptorProto_set_name(
1697
- self->msg_proto, FileBuilderContext_strdup(_file_builder, name));
1698
-
1699
- return Qnil;
1700
- }
1701
-
1702
- static void msgdef_add_field(VALUE msgbuilder_rb, upb_label_t label, VALUE name,
1703
- VALUE type, VALUE number, VALUE type_class,
1704
- VALUE options, int oneof_index,
1705
- bool proto3_optional) {
1706
- MessageBuilderContext* self = ruby_to_MessageBuilderContext(msgbuilder_rb);
1707
- FileBuilderContext* file_context =
1708
- ruby_to_FileBuilderContext(self->file_builder);
1709
- google_protobuf_FieldDescriptorProto* field_proto;
1710
- VALUE name_str;
1711
-
1712
- field_proto = google_protobuf_DescriptorProto_add_field(self->msg_proto,
1713
- file_context->arena);
1714
-
1715
- Check_Type(name, T_SYMBOL);
1716
- name_str = rb_id2str(SYM2ID(name));
1717
-
1718
- google_protobuf_FieldDescriptorProto_set_name(
1719
- field_proto, FileBuilderContext_strdup(self->file_builder, name_str));
1720
- google_protobuf_FieldDescriptorProto_set_number(field_proto, NUM2INT(number));
1721
- google_protobuf_FieldDescriptorProto_set_label(field_proto, (int)label);
1722
- google_protobuf_FieldDescriptorProto_set_type(
1723
- field_proto, (int)ruby_to_descriptortype(type));
1724
-
1725
- if (proto3_optional) {
1726
- google_protobuf_FieldDescriptorProto_set_proto3_optional(field_proto, true);
1727
- }
1728
-
1729
- if (type_class != Qnil) {
1730
- Check_Type(type_class, T_STRING);
1731
-
1732
- // Make it an absolute type name by prepending a dot.
1733
- type_class = rb_str_append(rb_str_new2("."), type_class);
1734
- google_protobuf_FieldDescriptorProto_set_type_name(
1735
- field_proto, FileBuilderContext_strdup(self->file_builder, type_class));
1736
- }
1737
-
1738
- if (options != Qnil) {
1739
- Check_Type(options, T_HASH);
1740
-
1741
- if (rb_funcall(options, rb_intern("key?"), 1,
1742
- ID2SYM(rb_intern("default"))) == Qtrue) {
1743
- VALUE default_value =
1744
- rb_hash_lookup(options, ID2SYM(rb_intern("default")));
1745
-
1746
- /* Call #to_s since all defaults are strings in the descriptor. */
1747
- default_value = rb_funcall(default_value, rb_intern("to_s"), 0);
1748
-
1749
- google_protobuf_FieldDescriptorProto_set_default_value(
1750
- field_proto,
1751
- FileBuilderContext_strdup(self->file_builder, default_value));
1752
- }
1753
- }
1754
-
1755
- if (oneof_index >= 0) {
1756
- google_protobuf_FieldDescriptorProto_set_oneof_index(field_proto,
1757
- oneof_index);
1758
- }
1759
- }
1760
-
1761
- #if RUBY_API_VERSION_CODE >= 20700
1762
- static VALUE make_mapentry(VALUE _message_builder, VALUE types, int argc,
1763
- const VALUE* argv, VALUE blockarg) {
1764
- (void)blockarg;
1765
- #else
1766
- static VALUE make_mapentry(VALUE _message_builder, VALUE types, int argc,
1767
- VALUE* argv) {
1768
- #endif
1769
- MessageBuilderContext* message_builder =
1770
- ruby_to_MessageBuilderContext(_message_builder);
1771
- VALUE type_class = rb_ary_entry(types, 2);
1772
- FileBuilderContext* file_context =
1773
- ruby_to_FileBuilderContext(message_builder->file_builder);
1774
- google_protobuf_MessageOptions* options =
1775
- google_protobuf_DescriptorProto_mutable_options(
1776
- message_builder->msg_proto, file_context->arena);
1777
-
1778
- google_protobuf_MessageOptions_set_map_entry(options, true);
1779
-
1780
- // optional <type> key = 1;
1781
- rb_funcall(_message_builder, rb_intern("optional"), 3,
1782
- ID2SYM(rb_intern("key")), rb_ary_entry(types, 0), INT2NUM(1));
1783
-
1784
- // optional <type> value = 2;
1785
- if (type_class == Qnil) {
1786
- rb_funcall(_message_builder, rb_intern("optional"), 3,
1787
- ID2SYM(rb_intern("value")), rb_ary_entry(types, 1), INT2NUM(2));
1788
- } else {
1789
- rb_funcall(_message_builder, rb_intern("optional"), 4,
1790
- ID2SYM(rb_intern("value")), rb_ary_entry(types, 1), INT2NUM(2),
1791
- type_class);
1792
- }
1793
-
1794
- return Qnil;
1795
- }
1796
-
1797
- /*
1798
- * call-seq:
1799
- * MessageBuilderContext.optional(name, type, number, type_class = nil,
1800
- * options = nil)
1801
- *
1802
- * Defines a new optional field on this message type with the given type, tag
1803
- * number, and type class (for message and enum fields). The type must be a Ruby
1804
- * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
1805
- * string, if present (as accepted by FieldDescriptor#submsg_name=).
1806
- */
1807
- VALUE MessageBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
1808
- VALUE name, type, number;
1809
- VALUE type_class, options = Qnil;
1810
-
1811
- rb_scan_args(argc, argv, "32", &name, &type, &number, &type_class, &options);
1812
-
1813
- // Allow passing (name, type, number, options) or
1814
- // (name, type, number, type_class, options)
1815
- if (argc == 4 && RB_TYPE_P(type_class, T_HASH)) {
1816
- options = type_class;
1817
- type_class = Qnil;
1818
- }
1819
-
1820
- msgdef_add_field(_self, UPB_LABEL_OPTIONAL, name, type, number, type_class,
1821
- options, -1, false);
1822
-
1823
- return Qnil;
1824
- }
1825
-
1826
- /*
1827
- * call-seq:
1828
- * MessageBuilderContext.proto3_optional(name, type, number,
1829
- * type_class = nil, options = nil)
1830
- *
1831
- * Defines a true proto3 optional field (that tracks presence) on this message
1832
- * type with the given type, tag number, and type class (for message and enum
1833
- * fields). The type must be a Ruby symbol (as accepted by
1834
- * FieldDescriptor#type=) and the type_class must be a string, if present (as
1835
- * accepted by FieldDescriptor#submsg_name=).
1836
- */
1837
- static VALUE MessageBuilderContext_proto3_optional(int argc, VALUE* argv,
1838
- VALUE _self) {
1839
- VALUE name, type, number;
1840
- VALUE type_class, options = Qnil;
1841
-
1842
- rb_scan_args(argc, argv, "32", &name, &type, &number, &type_class, &options);
1843
-
1844
- // Allow passing (name, type, number, options) or
1845
- // (name, type, number, type_class, options)
1846
- if (argc == 4 && RB_TYPE_P(type_class, T_HASH)) {
1847
- options = type_class;
1848
- type_class = Qnil;
1849
- }
1850
-
1851
- msgdef_add_field(_self, UPB_LABEL_OPTIONAL, name, type, number, type_class,
1852
- options, -1, true);
1853
-
1854
- return Qnil;
1855
- }
1856
-
1857
- /*
1858
- * call-seq:
1859
- * MessageBuilderContext.required(name, type, number, type_class = nil,
1860
- * options = nil)
1861
- *
1862
- * Defines a new required field on this message type with the given type, tag
1863
- * number, and type class (for message and enum fields). The type must be a Ruby
1864
- * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
1865
- * string, if present (as accepted by FieldDescriptor#submsg_name=).
1866
- *
1867
- * Proto3 does not have required fields, but this method exists for
1868
- * completeness. Any attempt to add a message type with required fields to a
1869
- * pool will currently result in an error.
1870
- */
1871
- static VALUE MessageBuilderContext_required(int argc, VALUE* argv,
1872
- VALUE _self) {
1873
- VALUE name, type, number;
1874
- VALUE type_class, options = Qnil;
1875
-
1876
- rb_scan_args(argc, argv, "32", &name, &type, &number, &type_class, &options);
1877
-
1878
- // Allow passing (name, type, number, options) or
1879
- // (name, type, number, type_class, options)
1880
- if (argc == 4 && RB_TYPE_P(type_class, T_HASH)) {
1881
- options = type_class;
1882
- type_class = Qnil;
1883
- }
1884
-
1885
- msgdef_add_field(_self, UPB_LABEL_REQUIRED, name, type, number, type_class,
1886
- options, -1, false);
1887
-
1888
- return Qnil;
1889
- }
1890
-
1891
- /*
1892
- * call-seq:
1893
- * MessageBuilderContext.repeated(name, type, number, type_class = nil)
1894
- *
1895
- * Defines a new repeated field on this message type with the given type, tag
1896
- * number, and type class (for message and enum fields). The type must be a Ruby
1897
- * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
1898
- * string, if present (as accepted by FieldDescriptor#submsg_name=).
1899
- */
1900
- static VALUE MessageBuilderContext_repeated(int argc, VALUE* argv,
1901
- VALUE _self) {
1902
- VALUE name, type, number, type_class;
1903
-
1904
- if (argc < 3) {
1905
- rb_raise(rb_eArgError, "Expected at least 3 arguments.");
1906
- }
1907
- name = argv[0];
1908
- type = argv[1];
1909
- number = argv[2];
1910
- type_class = (argc > 3) ? argv[3] : Qnil;
1911
-
1912
- msgdef_add_field(_self, UPB_LABEL_REPEATED, name, type, number, type_class,
1913
- Qnil, -1, false);
1914
-
1915
- return Qnil;
1916
- }
1917
-
1918
- /*
1919
- * call-seq:
1920
- * MessageBuilderContext.map(name, key_type, value_type, number,
1921
- * value_type_class = nil)
1922
- *
1923
- * Defines a new map field on this message type with the given key and value
1924
- * types, tag number, and type class (for message and enum value types). The key
1925
- * type must be :int32/:uint32/:int64/:uint64, :bool, or :string. The value type
1926
- * type must be a Ruby symbol (as accepted by FieldDescriptor#type=) and the
1927
- * type_class must be a string, if present (as accepted by
1928
- * FieldDescriptor#submsg_name=).
1929
- */
1930
- static VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self) {
1931
- MessageBuilderContext* self = ruby_to_MessageBuilderContext(_self);
1932
- VALUE name, key_type, value_type, number, type_class;
1933
- VALUE mapentry_desc_name;
1934
- FileBuilderContext* file_builder;
1935
- upb_strview msg_name;
1936
-
1937
- if (argc < 4) {
1938
- rb_raise(rb_eArgError, "Expected at least 4 arguments.");
1939
- }
1940
- name = argv[0];
1941
- key_type = argv[1];
1942
- value_type = argv[2];
1943
- number = argv[3];
1944
- type_class = (argc > 4) ? argv[4] : Qnil;
1945
-
1946
- // Validate the key type. We can't accept enums, messages, or floats/doubles
1947
- // as map keys. (We exclude these explicitly, and the field-descriptor setter
1948
- // below then ensures that the type is one of the remaining valid options.)
1949
- if (SYM2ID(key_type) == rb_intern("float") ||
1950
- SYM2ID(key_type) == rb_intern("double") ||
1951
- SYM2ID(key_type) == rb_intern("enum") ||
1952
- SYM2ID(key_type) == rb_intern("message")) {
1953
- rb_raise(rb_eArgError,
1954
- "Cannot add a map field with a float, double, enum, or message "
1955
- "type.");
1956
- }
1957
-
1958
- file_builder = ruby_to_FileBuilderContext(self->file_builder);
1959
-
1960
- // Create a new message descriptor for the map entry message, and create a
1961
- // repeated submessage field here with that type.
1962
- msg_name = google_protobuf_DescriptorProto_name(self->msg_proto);
1963
- mapentry_desc_name = rb_str_new(msg_name.data, msg_name.size);
1964
- mapentry_desc_name = rb_str_cat2(mapentry_desc_name, "_MapEntry_");
1965
- mapentry_desc_name =
1966
- rb_str_cat2(mapentry_desc_name, rb_id2name(SYM2ID(name)));
1967
-
1968
- {
1969
- // message <msgname>_MapEntry_ { /* ... */ }
1970
- VALUE args[1] = {mapentry_desc_name};
1971
- VALUE types = rb_ary_new3(3, key_type, value_type, type_class);
1972
- rb_block_call(self->file_builder, rb_intern("add_message"), 1, args,
1973
- make_mapentry, types);
1974
- }
1975
-
1976
- // If this file is in a package, we need to qualify the map entry type.
1977
- if (google_protobuf_FileDescriptorProto_has_package(file_builder->file_proto)) {
1978
- upb_strview package_view =
1979
- google_protobuf_FileDescriptorProto_package(file_builder->file_proto);
1980
- VALUE package = rb_str_new(package_view.data, package_view.size);
1981
- package = rb_str_cat2(package, ".");
1982
- mapentry_desc_name = rb_str_concat(package, mapentry_desc_name);
1983
- }
1984
-
1985
- // repeated MapEntry <name> = <number>;
1986
- rb_funcall(_self, rb_intern("repeated"), 4, name,
1987
- ID2SYM(rb_intern("message")), number, mapentry_desc_name);
1988
-
1989
- return Qnil;
1990
- }
1991
-
1992
- /*
1993
- * call-seq:
1994
- * MessageBuilderContext.oneof(name, &block) => nil
1995
- *
1996
- * Creates a new OneofDescriptor with the given name, creates a
1997
- * OneofBuilderContext attached to that OneofDescriptor, evaluates the given
1998
- * block in the context of that OneofBuilderContext with #instance_eval, and
1999
- * then adds the oneof to the message.
2000
- *
2001
- * This is the recommended, idiomatic way to build oneof definitions.
2002
- */
2003
- static VALUE MessageBuilderContext_oneof(VALUE _self, VALUE name) {
2004
- MessageBuilderContext* self = ruby_to_MessageBuilderContext(_self);
2005
- size_t oneof_count;
2006
- FileBuilderContext* file_context =
2007
- ruby_to_FileBuilderContext(self->file_builder);
2008
- google_protobuf_OneofDescriptorProto* oneof_proto;
2009
-
2010
- // Existing oneof_count becomes oneof_index.
2011
- google_protobuf_DescriptorProto_oneof_decl(self->msg_proto, &oneof_count);
2012
-
2013
- // Create oneof_proto and set its name.
2014
- oneof_proto = google_protobuf_DescriptorProto_add_oneof_decl(
2015
- self->msg_proto, file_context->arena);
2016
- google_protobuf_OneofDescriptorProto_set_name(
2017
- oneof_proto, FileBuilderContext_strdup_sym(self->file_builder, name));
2018
-
2019
- // Evaluate the block with the builder as argument.
2020
- {
2021
- VALUE args[2] = { INT2NUM(oneof_count), _self };
2022
- VALUE ctx = rb_class_new_instance(2, args, cOneofBuilderContext);
2023
- VALUE block = rb_block_proc();
2024
- rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
2025
- }
2026
-
2027
- return Qnil;
2028
- }
2029
-
2030
- static void MessageBuilderContext_add_synthetic_oneofs(VALUE _self) {
2031
- MessageBuilderContext* self = ruby_to_MessageBuilderContext(_self);
2032
- FileBuilderContext* file_context =
2033
- ruby_to_FileBuilderContext(self->file_builder);
2034
- size_t field_count, oneof_count;
2035
- google_protobuf_FieldDescriptorProto** fields =
2036
- google_protobuf_DescriptorProto_mutable_field(self->msg_proto, &field_count);
2037
- const google_protobuf_OneofDescriptorProto*const* oneofs =
2038
- google_protobuf_DescriptorProto_oneof_decl(self->msg_proto, &oneof_count);
2039
- VALUE names = rb_hash_new();
2040
- VALUE underscore = rb_str_new2("_");
2041
- size_t i;
2042
-
2043
- // We have to build a set of all names, to ensure that synthetic oneofs are
2044
- // not creating conflicts.
2045
- for (i = 0; i < field_count; i++) {
2046
- upb_strview name = google_protobuf_FieldDescriptorProto_name(fields[i]);
2047
- rb_hash_aset(names, rb_str_new(name.data, name.size), Qtrue);
2048
- }
2049
- for (i = 0; i < oneof_count; i++) {
2050
- upb_strview name = google_protobuf_OneofDescriptorProto_name(oneofs[i]);
2051
- rb_hash_aset(names, rb_str_new(name.data, name.size), Qtrue);
2052
- }
2053
-
2054
- for (i = 0; i < field_count; i++) {
2055
- google_protobuf_OneofDescriptorProto* oneof_proto;
2056
- VALUE oneof_name;
2057
- upb_strview field_name;
2058
-
2059
- if (!google_protobuf_FieldDescriptorProto_proto3_optional(fields[i])) {
2060
- continue;
2061
- }
2062
-
2063
- // Prepend '_' until we are no longer conflicting.
2064
- field_name = google_protobuf_FieldDescriptorProto_name(fields[i]);
2065
- oneof_name = rb_str_new(field_name.data, field_name.size);
2066
- while (rb_hash_lookup(names, oneof_name) != Qnil) {
2067
- oneof_name = rb_str_plus(underscore, oneof_name);
2068
- }
2069
-
2070
- rb_hash_aset(names, oneof_name, Qtrue);
2071
- google_protobuf_FieldDescriptorProto_set_oneof_index(fields[i],
2072
- oneof_count++);
2073
- oneof_proto = google_protobuf_DescriptorProto_add_oneof_decl(
2074
- self->msg_proto, file_context->arena);
2075
- google_protobuf_OneofDescriptorProto_set_name(
2076
- oneof_proto, FileBuilderContext_strdup(self->file_builder, oneof_name));
2077
- }
2078
- }
2079
-
2080
- static void MessageBuilderContext_register(VALUE module) {
2081
- VALUE klass = rb_define_class_under(
2082
- module, "MessageBuilderContext", rb_cObject);
2083
- rb_define_alloc_func(klass, MessageBuilderContext_alloc);
2084
- rb_define_method(klass, "initialize",
2085
- MessageBuilderContext_initialize, 2);
2086
- rb_define_method(klass, "optional", MessageBuilderContext_optional, -1);
2087
- rb_define_method(klass, "proto3_optional", MessageBuilderContext_proto3_optional, -1);
2088
- rb_define_method(klass, "required", MessageBuilderContext_required, -1);
2089
- rb_define_method(klass, "repeated", MessageBuilderContext_repeated, -1);
2090
- rb_define_method(klass, "map", MessageBuilderContext_map, -1);
2091
- rb_define_method(klass, "oneof", MessageBuilderContext_oneof, 1);
2092
- rb_gc_register_address(&cMessageBuilderContext);
2093
- cMessageBuilderContext = klass;
2094
- }
2095
-
2096
- // -----------------------------------------------------------------------------
2097
- // OneofBuilderContext.
2098
- // -----------------------------------------------------------------------------
2099
-
2100
- typedef struct {
2101
- int oneof_index;
2102
- VALUE message_builder;
2103
- } OneofBuilderContext;
2104
-
2105
- static VALUE cOneofBuilderContext = Qnil;
2106
-
2107
- void OneofBuilderContext_mark(void* _self) {
2108
- OneofBuilderContext* self = _self;
2109
- rb_gc_mark(self->message_builder);
2110
- }
2111
-
2112
- static const rb_data_type_t OneofBuilderContext_type = {
2113
- "Google::Protobuf::Internal::OneofBuilderContext",
2114
- {OneofBuilderContext_mark, RUBY_DEFAULT_FREE, NULL},
2115
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
2116
- };
2117
-
2118
- static OneofBuilderContext* ruby_to_OneofBuilderContext(VALUE val) {
2119
- OneofBuilderContext* ret;
2120
- TypedData_Get_Struct(val, OneofBuilderContext, &OneofBuilderContext_type,
2121
- ret);
2122
- return ret;
2123
- }
2124
-
2125
- static VALUE OneofBuilderContext_alloc(VALUE klass) {
2126
- OneofBuilderContext* self = ALLOC(OneofBuilderContext);
2127
- VALUE ret = TypedData_Wrap_Struct(klass, &OneofBuilderContext_type, self);
2128
- self->oneof_index = 0;
2129
- self->message_builder = Qnil;
2130
- return ret;
2131
- }
2132
-
2133
- /*
2134
- * call-seq:
2135
- * OneofBuilderContext.new(oneof_index, message_builder) => context
2136
- *
2137
- * Create a new oneof builder context around the given oneof descriptor and
2138
- * builder context. This class is intended to serve as a DSL context to be used
2139
- * with #instance_eval.
2140
- */
2141
- static VALUE OneofBuilderContext_initialize(VALUE _self, VALUE oneof_index,
2142
- VALUE message_builder) {
2143
- OneofBuilderContext* self = ruby_to_OneofBuilderContext(_self);
2144
- self->oneof_index = NUM2INT(oneof_index);
2145
- self->message_builder = message_builder;
2146
- return Qnil;
2147
- }
2148
-
2149
- /*
2150
- * call-seq:
2151
- * OneofBuilderContext.optional(name, type, number, type_class = nil,
2152
- * default_value = nil)
2153
- *
2154
- * Defines a new optional field in this oneof with the given type, tag number,
2155
- * and type class (for message and enum fields). The type must be a Ruby symbol
2156
- * (as accepted by FieldDescriptor#type=) and the type_class must be a string,
2157
- * if present (as accepted by FieldDescriptor#submsg_name=).
2158
- */
2159
- static VALUE OneofBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
2160
- OneofBuilderContext* self = ruby_to_OneofBuilderContext(_self);
2161
- VALUE name, type, number;
2162
- VALUE type_class, options = Qnil;
2163
-
2164
- rb_scan_args(argc, argv, "32", &name, &type, &number, &type_class, &options);
2165
-
2166
- msgdef_add_field(self->message_builder, UPB_LABEL_OPTIONAL, name, type,
2167
- number, type_class, options, self->oneof_index, false);
2168
-
2169
- return Qnil;
2170
- }
2171
-
2172
- static void OneofBuilderContext_register(VALUE module) {
2173
- VALUE klass = rb_define_class_under(
2174
- module, "OneofBuilderContext", rb_cObject);
2175
- rb_define_alloc_func(klass, OneofBuilderContext_alloc);
2176
- rb_define_method(klass, "initialize",
2177
- OneofBuilderContext_initialize, 2);
2178
- rb_define_method(klass, "optional", OneofBuilderContext_optional, -1);
2179
- rb_gc_register_address(&cOneofBuilderContext);
2180
- cOneofBuilderContext = klass;
2181
- }
2182
-
2183
- // -----------------------------------------------------------------------------
2184
- // EnumBuilderContext.
2185
- // -----------------------------------------------------------------------------
2186
-
2187
- typedef struct {
2188
- google_protobuf_EnumDescriptorProto* enum_proto;
2189
- VALUE file_builder;
2190
- } EnumBuilderContext;
2191
-
2192
- static VALUE cEnumBuilderContext = Qnil;
2193
-
2194
- void EnumBuilderContext_mark(void* _self) {
2195
- EnumBuilderContext* self = _self;
2196
- rb_gc_mark(self->file_builder);
2197
- }
2198
-
2199
- static const rb_data_type_t EnumBuilderContext_type = {
2200
- "Google::Protobuf::Internal::EnumBuilderContext",
2201
- {EnumBuilderContext_mark, RUBY_DEFAULT_FREE, NULL},
2202
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
2203
- };
2204
-
2205
- static EnumBuilderContext* ruby_to_EnumBuilderContext(VALUE val) {
2206
- EnumBuilderContext* ret;
2207
- TypedData_Get_Struct(val, EnumBuilderContext, &EnumBuilderContext_type, ret);
2208
- return ret;
2209
- }
2210
-
2211
- static VALUE EnumBuilderContext_alloc(VALUE klass) {
2212
- EnumBuilderContext* self = ALLOC(EnumBuilderContext);
2213
- VALUE ret = TypedData_Wrap_Struct(klass, &EnumBuilderContext_type, self);
2214
- self->enum_proto = NULL;
2215
- self->file_builder = Qnil;
2216
- return ret;
2217
- }
2218
-
2219
- /*
2220
- * call-seq:
2221
- * EnumBuilderContext.new(file_builder) => context
2222
- *
2223
- * Create a new builder context around the given enum descriptor. This class is
2224
- * intended to serve as a DSL context to be used with #instance_eval.
2225
- */
2226
- static VALUE EnumBuilderContext_initialize(VALUE _self, VALUE _file_builder,
2227
- VALUE name) {
2228
- EnumBuilderContext* self = ruby_to_EnumBuilderContext(_self);
2229
- FileBuilderContext* file_builder = ruby_to_FileBuilderContext(_file_builder);
2230
- google_protobuf_FileDescriptorProto* file_proto = file_builder->file_proto;
2231
-
2232
- self->file_builder = _file_builder;
2233
- self->enum_proto = google_protobuf_FileDescriptorProto_add_enum_type(
2234
- file_proto, file_builder->arena);
2235
-
2236
- google_protobuf_EnumDescriptorProto_set_name(
2237
- self->enum_proto, FileBuilderContext_strdup(_file_builder, name));
2238
-
2239
- return Qnil;
2240
- }
2241
-
2242
- /*
2243
- * call-seq:
2244
- * EnumBuilder.add_value(name, number)
2245
- *
2246
- * Adds the given name => number mapping to the enum type. Name must be a Ruby
2247
- * symbol.
2248
- */
2249
- static VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number) {
2250
- EnumBuilderContext* self = ruby_to_EnumBuilderContext(_self);
2251
- FileBuilderContext* file_builder =
2252
- ruby_to_FileBuilderContext(self->file_builder);
2253
- google_protobuf_EnumValueDescriptorProto* enum_value;
2254
-
2255
- enum_value = google_protobuf_EnumDescriptorProto_add_value(
2256
- self->enum_proto, file_builder->arena);
2257
-
2258
- google_protobuf_EnumValueDescriptorProto_set_name(
2259
- enum_value, FileBuilderContext_strdup_sym(self->file_builder, name));
2260
- google_protobuf_EnumValueDescriptorProto_set_number(enum_value,
2261
- NUM2INT(number));
2262
-
2263
- return Qnil;
2264
- }
2265
-
2266
- static void EnumBuilderContext_register(VALUE module) {
2267
- VALUE klass = rb_define_class_under(
2268
- module, "EnumBuilderContext", rb_cObject);
2269
- rb_define_alloc_func(klass, EnumBuilderContext_alloc);
2270
- rb_define_method(klass, "initialize", EnumBuilderContext_initialize, 2);
2271
- rb_define_method(klass, "value", EnumBuilderContext_value, 2);
2272
- rb_gc_register_address(&cEnumBuilderContext);
2273
- cEnumBuilderContext = klass;
2274
- }
2275
-
2276
- // -----------------------------------------------------------------------------
2277
- // Builder.
2278
- // -----------------------------------------------------------------------------
2279
-
2280
- typedef struct {
2281
- VALUE descriptor_pool;
2282
- VALUE default_file_builder;
2283
- } Builder;
2284
-
2285
- static VALUE cBuilder = Qnil;
2286
-
2287
- static void Builder_mark(void* _self) {
2288
- Builder* self = _self;
2289
- rb_gc_mark(self->descriptor_pool);
2290
- rb_gc_mark(self->default_file_builder);
2291
- }
2292
-
2293
- static const rb_data_type_t Builder_type = {
2294
- "Google::Protobuf::Internal::Builder",
2295
- {Builder_mark, RUBY_DEFAULT_FREE, NULL},
2296
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
2297
- };
2298
-
2299
- static Builder* ruby_to_Builder(VALUE val) {
2300
- Builder* ret;
2301
- TypedData_Get_Struct(val, Builder, &Builder_type, ret);
2302
- return ret;
2303
- }
2304
-
2305
- static VALUE Builder_alloc(VALUE klass) {
2306
- Builder* self = ALLOC(Builder);
2307
- VALUE ret = TypedData_Wrap_Struct(klass, &Builder_type, self);
2308
- self->descriptor_pool = Qnil;
2309
- self->default_file_builder = Qnil;
2310
- return ret;
2311
- }
2312
-
2313
- /*
2314
- * call-seq:
2315
- * Builder.new(descriptor_pool) => builder
2316
- *
2317
- * Creates a new Builder. A Builder can accumulate a set of new message and enum
2318
- * descriptors and atomically register them into a pool in a way that allows for
2319
- * (co)recursive type references.
2320
- */
2321
- static VALUE Builder_initialize(VALUE _self, VALUE pool) {
2322
- Builder* self = ruby_to_Builder(_self);
2323
- self->descriptor_pool = pool;
2324
- self->default_file_builder = Qnil; // Created lazily if needed.
2325
- return Qnil;
2326
- }
2327
-
2328
- /*
2329
- * call-seq:
2330
- * Builder.add_file(name, options = nil, &block)
2331
- *
2332
- * Creates a new, file descriptor with the given name and options and invokes
2333
- * the block in the context of a FileBuilderContext on that descriptor. The
2334
- * block can then call FileBuilderContext#add_message or
2335
- * FileBuilderContext#add_enum to define new messages or enums, respectively.
2336
- *
2337
- * This is the recommended, idiomatic way to build file descriptors.
2338
- */
2339
- static VALUE Builder_add_file(int argc, VALUE* argv, VALUE _self) {
2340
- Builder* self = ruby_to_Builder(_self);
2341
- VALUE name, options;
2342
- VALUE ctx;
2343
- VALUE block;
2344
-
2345
- rb_scan_args(argc, argv, "11", &name, &options);
2346
-
2347
- {
2348
- VALUE args[3] = { self->descriptor_pool, name, options };
2349
- ctx = rb_class_new_instance(3, args, cFileBuilderContext);
2350
- }
2351
-
2352
- block = rb_block_proc();
2353
- rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
2354
- FileBuilderContext_build(ctx);
2355
-
2356
- return Qnil;
2357
- }
2358
-
2359
- static VALUE Builder_get_default_file(VALUE _self) {
2360
- Builder* self = ruby_to_Builder(_self);
2361
-
2362
- /* Lazily create only if legacy builder-level methods are called. */
2363
- if (self->default_file_builder == Qnil) {
2364
- VALUE name = rb_str_new2("ruby_default_file.proto");
2365
- VALUE args [3] = { self->descriptor_pool, name, rb_hash_new() };
2366
- self->default_file_builder =
2367
- rb_class_new_instance(3, args, cFileBuilderContext);
2368
- }
2369
-
2370
- return self->default_file_builder;
2371
- }
2372
-
2373
- /*
2374
- * call-seq:
2375
- * Builder.add_message(name, &block)
2376
- *
2377
- * Old and deprecated way to create a new descriptor.
2378
- * See FileBuilderContext.add_message for the recommended way.
2379
- *
2380
- * Exists for backwards compatibility to allow building descriptor pool for
2381
- * files generated by protoc which don't add messages within "add_file" block.
2382
- * Descriptors created this way get assigned to a default empty FileDescriptor.
2383
- */
2384
- static VALUE Builder_add_message(VALUE _self, VALUE name) {
2385
- VALUE file_builder = Builder_get_default_file(_self);
2386
- rb_funcall_with_block(file_builder, rb_intern("add_message"), 1, &name,
2387
- rb_block_proc());
2388
- return Qnil;
2389
- }
2390
-
2391
- /*
2392
- * call-seq:
2393
- * Builder.add_enum(name, &block)
2394
- *
2395
- * Old and deprecated way to create a new enum descriptor.
2396
- * See FileBuilderContext.add_enum for the recommended way.
2397
- *
2398
- * Exists for backwards compatibility to allow building descriptor pool for
2399
- * files generated by protoc which don't add enums within "add_file" block.
2400
- * Enum descriptors created this way get assigned to a default empty
2401
- * FileDescriptor.
2402
- */
2403
- static VALUE Builder_add_enum(VALUE _self, VALUE name) {
2404
- VALUE file_builder = Builder_get_default_file(_self);
2405
- rb_funcall_with_block(file_builder, rb_intern("add_enum"), 1, &name,
2406
- rb_block_proc());
2407
- return Qnil;
2408
- }
2409
-
2410
- /* This method is hidden from Ruby, and only called directly from
2411
- * DescriptorPool_build(). */
2412
- static VALUE Builder_build(VALUE _self) {
2413
- Builder* self = ruby_to_Builder(_self);
2414
-
2415
- if (self->default_file_builder != Qnil) {
2416
- FileBuilderContext_build(self->default_file_builder);
2417
- self->default_file_builder = Qnil;
2418
- }
2419
-
2420
- return Qnil;
2421
- }
2422
-
2423
- static void Builder_register(VALUE module) {
2424
- VALUE klass = rb_define_class_under(module, "Builder", rb_cObject);
2425
- rb_define_alloc_func(klass, Builder_alloc);
2426
- rb_define_method(klass, "initialize", Builder_initialize, 1);
2427
- rb_define_method(klass, "add_file", Builder_add_file, -1);
2428
- rb_define_method(klass, "add_message", Builder_add_message, 1);
2429
- rb_define_method(klass, "add_enum", Builder_add_enum, 1);
2430
- rb_gc_register_address(&cBuilder);
2431
- cBuilder = klass;
2432
- }
2433
-
2434
1161
  static VALUE get_def_obj(VALUE _descriptor_pool, const void* ptr, VALUE klass) {
2435
1162
  DescriptorPool* descriptor_pool = ruby_to_DescriptorPool(_descriptor_pool);
2436
1163
  VALUE key = ULL2NUM((intptr_t)ptr);
@@ -2444,7 +1171,7 @@ static VALUE get_def_obj(VALUE _descriptor_pool, const void* ptr, VALUE klass) {
2444
1171
 
2445
1172
  if (def == Qnil) {
2446
1173
  // Lazily create wrapper object.
2447
- VALUE args[3] = { c_only_cookie, _descriptor_pool, key };
1174
+ VALUE args[3] = {c_only_cookie, _descriptor_pool, key};
2448
1175
  def = rb_class_new_instance(3, args, klass);
2449
1176
  rb_hash_aset(descriptor_pool->def_to_descriptor, key, def);
2450
1177
  }
@@ -2452,23 +1179,23 @@ static VALUE get_def_obj(VALUE _descriptor_pool, const void* ptr, VALUE klass) {
2452
1179
  return def;
2453
1180
  }
2454
1181
 
2455
- static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_msgdef* def) {
1182
+ static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_MessageDef* def) {
2456
1183
  return get_def_obj(descriptor_pool, def, cDescriptor);
2457
1184
  }
2458
1185
 
2459
- static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_enumdef* def) {
1186
+ static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_EnumDef* def) {
2460
1187
  return get_def_obj(descriptor_pool, def, cEnumDescriptor);
2461
1188
  }
2462
1189
 
2463
- static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_fielddef* def) {
1190
+ static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_FieldDef* def) {
2464
1191
  return get_def_obj(descriptor_pool, def, cFieldDescriptor);
2465
1192
  }
2466
1193
 
2467
- static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_filedef* def) {
1194
+ static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_FileDef* def) {
2468
1195
  return get_def_obj(descriptor_pool, def, cFileDescriptor);
2469
1196
  }
2470
1197
 
2471
- static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_oneofdef* def) {
1198
+ static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_OneofDef* def) {
2472
1199
  return get_def_obj(descriptor_pool, def, cOneofDescriptor);
2473
1200
  }
2474
1201
 
@@ -2478,8 +1205,8 @@ static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_oneofdef* def) {
2478
1205
 
2479
1206
  // Functions exposed to other modules in defs.h.
2480
1207
 
2481
- VALUE Descriptor_DefToClass(const upb_msgdef *m) {
2482
- const upb_symtab *symtab = upb_filedef_symtab(upb_msgdef_file(m));
1208
+ VALUE Descriptor_DefToClass(const upb_MessageDef* m) {
1209
+ const upb_DefPool* symtab = upb_FileDef_Pool(upb_MessageDef_File(m));
2483
1210
  VALUE pool = ObjectCache_Get(symtab);
2484
1211
  PBRUBY_ASSERT(pool != Qnil);
2485
1212
  VALUE desc_rb = get_msgdef_obj(pool, m);
@@ -2487,15 +1214,16 @@ VALUE Descriptor_DefToClass(const upb_msgdef *m) {
2487
1214
  return desc->klass;
2488
1215
  }
2489
1216
 
2490
- const upb_msgdef *Descriptor_GetMsgDef(VALUE desc_rb) {
1217
+ const upb_MessageDef* Descriptor_GetMsgDef(VALUE desc_rb) {
2491
1218
  const Descriptor* desc = ruby_to_Descriptor(desc_rb);
2492
1219
  return desc->msgdef;
2493
1220
  }
2494
1221
 
2495
- VALUE TypeInfo_InitArg(int argc, VALUE *argv, int skip_arg) {
1222
+ VALUE TypeInfo_InitArg(int argc, VALUE* argv, int skip_arg) {
2496
1223
  if (argc > skip_arg) {
2497
1224
  if (argc > 1 + skip_arg) {
2498
- rb_raise(rb_eArgError, "Expected a maximum of %d arguments.", skip_arg + 1);
1225
+ rb_raise(rb_eArgError, "Expected a maximum of %d arguments.",
1226
+ skip_arg + 1);
2499
1227
  }
2500
1228
  return argv[skip_arg];
2501
1229
  } else {
@@ -2507,7 +1235,7 @@ TypeInfo TypeInfo_FromClass(int argc, VALUE* argv, int skip_arg,
2507
1235
  VALUE* type_class, VALUE* init_arg) {
2508
1236
  TypeInfo ret = {ruby_to_fieldtype(argv[skip_arg])};
2509
1237
 
2510
- if (ret.type == UPB_TYPE_MESSAGE || ret.type == UPB_TYPE_ENUM) {
1238
+ if (ret.type == kUpb_CType_Message || ret.type == kUpb_CType_Enum) {
2511
1239
  *init_arg = TypeInfo_InitArg(argc, argv, skip_arg + 2);
2512
1240
 
2513
1241
  if (argc < 2 + skip_arg) {
@@ -2525,11 +1253,11 @@ TypeInfo TypeInfo_FromClass(int argc, VALUE* argv, int skip_arg,
2525
1253
  "class or enum as returned by the DescriptorPool.");
2526
1254
  }
2527
1255
 
2528
- if (ret.type == UPB_TYPE_MESSAGE) {
1256
+ if (ret.type == kUpb_CType_Message) {
2529
1257
  ret.def.msgdef = ruby_to_Descriptor(desc)->msgdef;
2530
1258
  Message_CheckClass(klass);
2531
1259
  } else {
2532
- PBRUBY_ASSERT(ret.type == UPB_TYPE_ENUM);
1260
+ PBRUBY_ASSERT(ret.type == kUpb_CType_Enum);
2533
1261
  ret.def.enumdef = ruby_to_EnumDescriptor(desc)->enumdef;
2534
1262
  }
2535
1263
  } else {
@@ -2546,11 +1274,6 @@ void Defs_register(VALUE module) {
2546
1274
  FieldDescriptor_register(module);
2547
1275
  OneofDescriptor_register(module);
2548
1276
  EnumDescriptor_register(module);
2549
- FileBuilderContext_register(module);
2550
- MessageBuilderContext_register(module);
2551
- OneofBuilderContext_register(module);
2552
- EnumBuilderContext_register(module);
2553
- Builder_register(module);
2554
1277
 
2555
1278
  rb_gc_register_address(&c_only_cookie);
2556
1279
  c_only_cookie = rb_class_new_instance(0, NULL, rb_cObject);