google-protobuf 3.8.0 → 3.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


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

Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/ext/google/protobuf_c/convert.c +349 -0
  3. data/ext/google/protobuf_c/convert.h +72 -0
  4. data/ext/google/protobuf_c/defs.c +1525 -1223
  5. data/ext/google/protobuf_c/defs.h +107 -0
  6. data/ext/google/protobuf_c/extconf.rb +4 -7
  7. data/ext/google/protobuf_c/map.c +309 -476
  8. data/ext/google/protobuf_c/map.h +66 -0
  9. data/ext/google/protobuf_c/message.c +912 -446
  10. data/ext/google/protobuf_c/message.h +98 -0
  11. data/ext/google/protobuf_c/protobuf.c +326 -51
  12. data/ext/google/protobuf_c/protobuf.h +46 -549
  13. data/ext/google/protobuf_c/repeated_field.c +308 -312
  14. data/ext/google/protobuf_c/repeated_field.h +62 -0
  15. data/ext/google/protobuf_c/ruby-upb.c +8915 -0
  16. data/ext/google/protobuf_c/ruby-upb.h +4452 -0
  17. data/ext/google/protobuf_c/third_party/wyhash/wyhash.h +145 -0
  18. data/lib/google/protobuf/any_pb.rb +1 -1
  19. data/lib/google/protobuf/api_pb.rb +3 -3
  20. data/lib/google/protobuf/duration_pb.rb +1 -1
  21. data/lib/google/protobuf/empty_pb.rb +1 -1
  22. data/lib/google/protobuf/field_mask_pb.rb +1 -1
  23. data/lib/google/protobuf/source_context_pb.rb +1 -1
  24. data/lib/google/protobuf/struct_pb.rb +4 -4
  25. data/lib/google/protobuf/timestamp_pb.rb +1 -1
  26. data/lib/google/protobuf/type_pb.rb +8 -8
  27. data/lib/google/protobuf/well_known_types.rb +8 -2
  28. data/lib/google/protobuf/wrappers_pb.rb +9 -9
  29. data/lib/google/protobuf.rb +70 -0
  30. data/tests/basic.rb +262 -71
  31. data/tests/generated_code_test.rb +0 -0
  32. data/tests/stress.rb +0 -0
  33. metadata +26 -14
  34. data/ext/google/protobuf_c/encode_decode.c +0 -1614
  35. data/ext/google/protobuf_c/storage.c +0 -1062
  36. data/ext/google/protobuf_c/upb.c +0 -17480
  37. data/ext/google/protobuf_c/upb.h +0 -10641
@@ -28,8 +28,37 @@
28
28
  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
29
  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
30
 
31
+ #include <ctype.h>
32
+ #include <errno.h>
33
+ #include <ruby/version.h>
34
+
35
+ #include "convert.h"
36
+ #include "message.h"
31
37
  #include "protobuf.h"
32
38
 
39
+ static VALUE Builder_build(VALUE _self);
40
+
41
+ static VALUE cMessageBuilderContext;
42
+ static VALUE cOneofBuilderContext;
43
+ static VALUE cEnumBuilderContext;
44
+ static VALUE cBuilder;
45
+
46
+ // -----------------------------------------------------------------------------
47
+ // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
48
+ // instances.
49
+ // -----------------------------------------------------------------------------
50
+
51
+ static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_msgdef* def);
52
+ static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_enumdef* def);
53
+ static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_fielddef* def);
54
+ static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_filedef* def);
55
+ static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_oneofdef* def);
56
+
57
+ // A distinct object that is not accessible from Ruby. We use this as a
58
+ // constructor argument to enforce that certain objects cannot be created from
59
+ // Ruby.
60
+ VALUE c_only_cookie = Qnil;
61
+
33
62
  // -----------------------------------------------------------------------------
34
63
  // Common utilities.
35
64
  // -----------------------------------------------------------------------------
@@ -46,129 +75,229 @@ static VALUE rb_str_maybe_null(const char* s) {
46
75
  return rb_str_new2(s);
47
76
  }
48
77
 
49
- static upb_def* check_notfrozen(const upb_def* def) {
50
- if (upb_def_isfrozen(def)) {
51
- rb_raise(rb_eRuntimeError,
52
- "Attempt to modify a frozen descriptor. Once descriptors are "
53
- "added to the descriptor pool, they may not be modified.");
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;
54
98
  }
55
- return (upb_def*)def;
56
- }
57
99
 
58
- static upb_msgdef* check_msg_notfrozen(const upb_msgdef* def) {
59
- return upb_downcast_msgdef_mutable(check_notfrozen((const upb_def*)def));
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
+ }
60
162
  }
61
163
 
62
- static upb_fielddef* check_field_notfrozen(const upb_fielddef* def) {
63
- return upb_downcast_fielddef_mutable(check_notfrozen((const upb_def*)def));
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
+ }
64
187
  }
65
188
 
66
- static upb_oneofdef* check_oneof_notfrozen(const upb_oneofdef* def) {
67
- return (upb_oneofdef*)check_notfrozen((const upb_def*)def);
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
+ }
68
196
  }
69
197
 
70
- static upb_enumdef* check_enum_notfrozen(const upb_enumdef* def) {
71
- return (upb_enumdef*)check_notfrozen((const upb_def*)def);
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
+ }
72
237
  }
73
238
 
74
239
  // -----------------------------------------------------------------------------
75
240
  // DescriptorPool.
76
241
  // -----------------------------------------------------------------------------
77
242
 
78
- #define DEFINE_CLASS(name, string_name) \
79
- VALUE c ## name = Qnil; \
80
- const rb_data_type_t _ ## name ## _type = { \
81
- string_name, \
82
- { name ## _mark, name ## _free, NULL }, \
83
- }; \
84
- name* ruby_to_ ## name(VALUE val) { \
85
- name* ret; \
86
- TypedData_Get_Struct(val, name, &_ ## name ## _type, ret); \
87
- return ret; \
88
- } \
89
-
90
- #define DEFINE_SELF(type, var, rb_var) \
91
- type* var = ruby_to_ ## type(rb_var)
243
+ typedef struct {
244
+ VALUE def_to_descriptor; // Hash table of def* -> Ruby descriptor.
245
+ upb_symtab* symtab;
246
+ } DescriptorPool;
247
+
248
+ VALUE cDescriptorPool = Qnil;
92
249
 
93
250
  // Global singleton DescriptorPool. The user is free to create others, but this
94
251
  // is used by generated code.
95
- VALUE generated_pool;
252
+ VALUE generated_pool = Qnil;
96
253
 
97
- DEFINE_CLASS(DescriptorPool, "Google::Protobuf::DescriptorPool");
98
-
99
- void DescriptorPool_mark(void* _self) {
254
+ static void DescriptorPool_mark(void* _self) {
255
+ DescriptorPool* self = _self;
256
+ rb_gc_mark(self->def_to_descriptor);
100
257
  }
101
258
 
102
- void DescriptorPool_free(void* _self) {
259
+ static void DescriptorPool_free(void* _self) {
103
260
  DescriptorPool* self = _self;
104
261
  upb_symtab_free(self->symtab);
105
262
  xfree(self);
106
263
  }
107
264
 
265
+ static const rb_data_type_t DescriptorPool_type = {
266
+ "Google::Protobuf::DescriptorPool",
267
+ {DescriptorPool_mark, DescriptorPool_free, NULL},
268
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
269
+ };
270
+
271
+ static DescriptorPool* ruby_to_DescriptorPool(VALUE val) {
272
+ DescriptorPool* ret;
273
+ TypedData_Get_Struct(val, DescriptorPool, &DescriptorPool_type, ret);
274
+ return ret;
275
+ }
276
+
277
+ // Exposed to other modules in defs.h.
278
+ const upb_symtab *DescriptorPool_GetSymtab(VALUE desc_pool_rb) {
279
+ DescriptorPool *pool = ruby_to_DescriptorPool(desc_pool_rb);
280
+ return pool->symtab;
281
+ }
282
+
108
283
  /*
109
284
  * call-seq:
110
285
  * DescriptorPool.new => pool
111
286
  *
112
287
  * Creates a new, empty, descriptor pool.
113
288
  */
114
- VALUE DescriptorPool_alloc(VALUE klass) {
289
+ static VALUE DescriptorPool_alloc(VALUE klass) {
115
290
  DescriptorPool* self = ALLOC(DescriptorPool);
116
- self->symtab = upb_symtab_new();
117
- return TypedData_Wrap_Struct(klass, &_DescriptorPool_type, self);
118
- }
119
-
120
- void DescriptorPool_register(VALUE module) {
121
- VALUE klass = rb_define_class_under(
122
- module, "DescriptorPool", rb_cObject);
123
- rb_define_alloc_func(klass, DescriptorPool_alloc);
124
- rb_define_method(klass, "add", DescriptorPool_add, 1);
125
- rb_define_method(klass, "build", DescriptorPool_build, -1);
126
- rb_define_method(klass, "lookup", DescriptorPool_lookup, 1);
127
- rb_define_singleton_method(klass, "generated_pool",
128
- DescriptorPool_generated_pool, 0);
129
- rb_gc_register_address(&cDescriptorPool);
130
- cDescriptorPool = klass;
131
-
132
- rb_gc_register_address(&generated_pool);
133
- generated_pool = rb_class_new_instance(0, NULL, klass);
134
- }
291
+ VALUE ret;
135
292
 
136
- static void add_descriptor_to_pool(DescriptorPool* self,
137
- Descriptor* descriptor) {
138
- CHECK_UPB(
139
- upb_symtab_add(self->symtab, (upb_def**)&descriptor->msgdef, 1,
140
- NULL, &status),
141
- "Adding Descriptor to DescriptorPool failed");
142
- }
293
+ self->def_to_descriptor = Qnil;
294
+ ret = TypedData_Wrap_Struct(klass, &DescriptorPool_type, self);
143
295
 
144
- static void add_enumdesc_to_pool(DescriptorPool* self,
145
- EnumDescriptor* enumdesc) {
146
- CHECK_UPB(
147
- upb_symtab_add(self->symtab, (upb_def**)&enumdesc->enumdef, 1,
148
- NULL, &status),
149
- "Adding EnumDescriptor to DescriptorPool failed");
150
- }
296
+ self->def_to_descriptor = rb_hash_new();
297
+ self->symtab = upb_symtab_new();
298
+ ObjectCache_Add(self->symtab, ret, _upb_symtab_arena(self->symtab));
151
299
 
152
- /*
153
- * call-seq:
154
- * DescriptorPool.add(descriptor)
155
- *
156
- * Adds the given Descriptor or EnumDescriptor to this pool. All references to
157
- * other types in a Descriptor's fields must be resolvable within this pool or
158
- * an exception will be raised.
159
- */
160
- VALUE DescriptorPool_add(VALUE _self, VALUE def) {
161
- DEFINE_SELF(DescriptorPool, self, _self);
162
- VALUE def_klass = rb_obj_class(def);
163
- if (def_klass == cDescriptor) {
164
- add_descriptor_to_pool(self, ruby_to_Descriptor(def));
165
- } else if (def_klass == cEnumDescriptor) {
166
- add_enumdesc_to_pool(self, ruby_to_EnumDescriptor(def));
167
- } else {
168
- rb_raise(rb_eArgError,
169
- "Second argument must be a Descriptor or EnumDescriptor.");
170
- }
171
- return Qnil;
300
+ return ret;
172
301
  }
173
302
 
174
303
  /*
@@ -181,11 +310,11 @@ VALUE DescriptorPool_add(VALUE _self, VALUE def) {
181
310
  * Builder#add_enum within the block as appropriate. This is the recommended,
182
311
  * idiomatic way to define new message and enum types.
183
312
  */
184
- VALUE DescriptorPool_build(int argc, VALUE* argv, VALUE _self) {
185
- VALUE ctx = rb_class_new_instance(0, NULL, cBuilder);
313
+ static VALUE DescriptorPool_build(int argc, VALUE* argv, VALUE _self) {
314
+ VALUE ctx = rb_class_new_instance(1, &_self, cBuilder);
186
315
  VALUE block = rb_block_proc();
187
316
  rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
188
- rb_funcall(ctx, rb_intern("finalize_to_pool"), 1, _self);
317
+ Builder_build(ctx);
189
318
  return Qnil;
190
319
  }
191
320
 
@@ -196,14 +325,23 @@ VALUE DescriptorPool_build(int argc, VALUE* argv, VALUE _self) {
196
325
  * Finds a Descriptor or EnumDescriptor by name and returns it, or nil if none
197
326
  * exists with the given name.
198
327
  */
199
- VALUE DescriptorPool_lookup(VALUE _self, VALUE name) {
200
- DEFINE_SELF(DescriptorPool, self, _self);
328
+ static VALUE DescriptorPool_lookup(VALUE _self, VALUE name) {
329
+ DescriptorPool* self = ruby_to_DescriptorPool(_self);
201
330
  const char* name_str = get_str(name);
202
- const upb_def* def = upb_symtab_lookup(self->symtab, name_str);
203
- if (!def) {
204
- return Qnil;
331
+ const upb_msgdef* msgdef;
332
+ const upb_enumdef* enumdef;
333
+
334
+ msgdef = upb_symtab_lookupmsg(self->symtab, name_str);
335
+ if (msgdef) {
336
+ return get_msgdef_obj(_self, msgdef);
337
+ }
338
+
339
+ enumdef = upb_symtab_lookupenum(self->symtab, name_str);
340
+ if (enumdef) {
341
+ return get_enumdef_obj(_self, enumdef);
205
342
  }
206
- return get_def_obj(def);
343
+
344
+ return Qnil;
207
345
  }
208
346
 
209
347
  /*
@@ -215,50 +353,53 @@ VALUE DescriptorPool_lookup(VALUE _self, VALUE name) {
215
353
  * register types in this pool for convenience so that they do not have to hold
216
354
  * a reference to a private pool instance.
217
355
  */
218
- VALUE DescriptorPool_generated_pool(VALUE _self) {
356
+ static VALUE DescriptorPool_generated_pool(VALUE _self) {
219
357
  return generated_pool;
220
358
  }
221
359
 
360
+ static void DescriptorPool_register(VALUE module) {
361
+ VALUE klass = rb_define_class_under(
362
+ module, "DescriptorPool", rb_cObject);
363
+ rb_define_alloc_func(klass, DescriptorPool_alloc);
364
+ rb_define_method(klass, "build", DescriptorPool_build, -1);
365
+ rb_define_method(klass, "lookup", DescriptorPool_lookup, 1);
366
+ rb_define_singleton_method(klass, "generated_pool",
367
+ DescriptorPool_generated_pool, 0);
368
+ rb_gc_register_address(&cDescriptorPool);
369
+ cDescriptorPool = klass;
370
+
371
+ rb_gc_register_address(&generated_pool);
372
+ generated_pool = rb_class_new_instance(0, NULL, klass);
373
+ }
374
+
222
375
  // -----------------------------------------------------------------------------
223
376
  // Descriptor.
224
377
  // -----------------------------------------------------------------------------
225
378
 
226
- DEFINE_CLASS(Descriptor, "Google::Protobuf::Descriptor");
379
+ typedef struct {
380
+ const upb_msgdef* msgdef;
381
+ VALUE klass;
382
+ VALUE descriptor_pool;
383
+ } Descriptor;
227
384
 
228
- void Descriptor_mark(void* _self) {
385
+ VALUE cDescriptor = Qnil;
386
+
387
+ static void Descriptor_mark(void* _self) {
229
388
  Descriptor* self = _self;
230
389
  rb_gc_mark(self->klass);
390
+ rb_gc_mark(self->descriptor_pool);
231
391
  }
232
392
 
233
- void Descriptor_free(void* _self) {
234
- Descriptor* self = _self;
235
- upb_msgdef_unref(self->msgdef, &self->msgdef);
236
- if (self->layout) {
237
- free_layout(self->layout);
238
- }
239
- if (self->fill_handlers) {
240
- upb_handlers_unref(self->fill_handlers, &self->fill_handlers);
241
- }
242
- if (self->fill_method) {
243
- upb_pbdecodermethod_unref(self->fill_method, &self->fill_method);
244
- }
245
- if (self->json_fill_method) {
246
- upb_json_parsermethod_unref(self->json_fill_method,
247
- &self->json_fill_method);
248
- }
249
- if (self->pb_serialize_handlers) {
250
- upb_handlers_unref(self->pb_serialize_handlers,
251
- &self->pb_serialize_handlers);
252
- }
253
- if (self->json_serialize_handlers) {
254
- upb_handlers_unref(self->json_serialize_handlers,
255
- &self->json_serialize_handlers);
256
- }
257
- if (self->json_serialize_handlers_preserve) {
258
- upb_handlers_unref(self->json_serialize_handlers_preserve,
259
- &self->json_serialize_handlers_preserve);
260
- }
261
- xfree(self);
393
+ static const rb_data_type_t Descriptor_type = {
394
+ "Google::Protobuf::Descriptor",
395
+ {Descriptor_mark, RUBY_DEFAULT_FREE, NULL},
396
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
397
+ };
398
+
399
+ static Descriptor* ruby_to_Descriptor(VALUE val) {
400
+ Descriptor* ret;
401
+ TypedData_Get_Struct(val, Descriptor, &Descriptor_type, ret);
402
+ return ret;
262
403
  }
263
404
 
264
405
  /*
@@ -270,56 +411,32 @@ void Descriptor_free(void* _self) {
270
411
  * it is added to a pool, after which it becomes immutable (as part of a
271
412
  * finalization process).
272
413
  */
273
- VALUE Descriptor_alloc(VALUE klass) {
414
+ static VALUE Descriptor_alloc(VALUE klass) {
274
415
  Descriptor* self = ALLOC(Descriptor);
275
- VALUE ret = TypedData_Wrap_Struct(klass, &_Descriptor_type, self);
276
- self->msgdef = upb_msgdef_new(&self->msgdef);
416
+ VALUE ret = TypedData_Wrap_Struct(klass, &Descriptor_type, self);
417
+ self->msgdef = NULL;
277
418
  self->klass = Qnil;
278
- self->layout = NULL;
279
- self->fill_handlers = NULL;
280
- self->fill_method = NULL;
281
- self->json_fill_method = NULL;
282
- self->pb_serialize_handlers = NULL;
283
- self->json_serialize_handlers = NULL;
284
- self->json_serialize_handlers_preserve = NULL;
419
+ self->descriptor_pool = Qnil;
285
420
  return ret;
286
421
  }
287
422
 
288
- void Descriptor_register(VALUE module) {
289
- VALUE klass = rb_define_class_under(
290
- module, "Descriptor", rb_cObject);
291
- rb_define_alloc_func(klass, Descriptor_alloc);
292
- rb_define_method(klass, "initialize", Descriptor_initialize, 1);
293
- rb_define_method(klass, "each", Descriptor_each, 0);
294
- rb_define_method(klass, "lookup", Descriptor_lookup, 1);
295
- rb_define_method(klass, "add_field", Descriptor_add_field, 1);
296
- rb_define_method(klass, "add_oneof", Descriptor_add_oneof, 1);
297
- rb_define_method(klass, "each_oneof", Descriptor_each_oneof, 0);
298
- rb_define_method(klass, "lookup_oneof", Descriptor_lookup_oneof, 1);
299
- rb_define_method(klass, "msgclass", Descriptor_msgclass, 0);
300
- rb_define_method(klass, "name", Descriptor_name, 0);
301
- rb_define_method(klass, "name=", Descriptor_name_set, 1);
302
- rb_define_method(klass, "file_descriptor", Descriptor_file_descriptor, 0);
303
- rb_include_module(klass, rb_mEnumerable);
304
- rb_gc_register_address(&cDescriptor);
305
- cDescriptor = klass;
306
- }
307
-
308
423
  /*
309
424
  * call-seq:
310
- * Descriptor.new(file_descriptor)
425
+ * Descriptor.new(c_only_cookie, ptr) => Descriptor
311
426
  *
312
- * Initializes a new descriptor and assigns a file descriptor to it.
427
+ * Creates a descriptor wrapper object. May only be called from C.
313
428
  */
314
- VALUE Descriptor_initialize(VALUE _self, VALUE file_descriptor_rb) {
315
- DEFINE_SELF(Descriptor, self, _self);
429
+ static VALUE Descriptor_initialize(VALUE _self, VALUE cookie,
430
+ VALUE descriptor_pool, VALUE ptr) {
431
+ Descriptor* self = ruby_to_Descriptor(_self);
316
432
 
317
- FileDescriptor* file_descriptor = ruby_to_FileDescriptor(file_descriptor_rb);
433
+ if (cookie != c_only_cookie) {
434
+ rb_raise(rb_eRuntimeError,
435
+ "Descriptor objects may not be created from Ruby.");
436
+ }
318
437
 
319
- CHECK_UPB(
320
- upb_filedef_addmsg(file_descriptor->filedef, self->msgdef, NULL, &status),
321
- "Failed to associate message to file descriptor.");
322
- add_def_obj(file_descriptor->filedef, file_descriptor_rb);
438
+ self->descriptor_pool = descriptor_pool;
439
+ self->msgdef = (const upb_msgdef*)NUM2ULL(ptr);
323
440
 
324
441
  return Qnil;
325
442
  }
@@ -330,55 +447,38 @@ VALUE Descriptor_initialize(VALUE _self, VALUE file_descriptor_rb) {
330
447
  *
331
448
  * Returns the FileDescriptor object this message belongs to.
332
449
  */
333
- VALUE Descriptor_file_descriptor(VALUE _self) {
334
- DEFINE_SELF(Descriptor, self, _self);
335
- return get_def_obj(upb_def_file(self->msgdef));
450
+ static VALUE Descriptor_file_descriptor(VALUE _self) {
451
+ Descriptor* self = ruby_to_Descriptor(_self);
452
+ return get_filedef_obj(self->descriptor_pool, upb_msgdef_file(self->msgdef));
336
453
  }
337
454
 
338
455
  /*
339
456
  * call-seq:
340
457
  * Descriptor.name => name
341
458
  *
342
- * Returns the name of this message type as a fully-qualfied string (e.g.,
459
+ * Returns the name of this message type as a fully-qualified string (e.g.,
343
460
  * My.Package.MessageType).
344
461
  */
345
- VALUE Descriptor_name(VALUE _self) {
346
- DEFINE_SELF(Descriptor, self, _self);
462
+ static VALUE Descriptor_name(VALUE _self) {
463
+ Descriptor* self = ruby_to_Descriptor(_self);
347
464
  return rb_str_maybe_null(upb_msgdef_fullname(self->msgdef));
348
465
  }
349
466
 
350
- /*
351
- * call-seq:
352
- * Descriptor.name = name
353
- *
354
- * Assigns a name to this message type. The descriptor must not have been added
355
- * to a pool yet.
356
- */
357
- VALUE Descriptor_name_set(VALUE _self, VALUE str) {
358
- DEFINE_SELF(Descriptor, self, _self);
359
- upb_msgdef* mut_def = check_msg_notfrozen(self->msgdef);
360
- const char* name = get_str(str);
361
- CHECK_UPB(
362
- upb_msgdef_setfullname(mut_def, name, &status),
363
- "Error setting Descriptor name");
364
- return Qnil;
365
- }
366
-
367
467
  /*
368
468
  * call-seq:
369
469
  * Descriptor.each(&block)
370
470
  *
371
471
  * Iterates over fields in this message type, yielding to the block on each one.
372
472
  */
373
- VALUE Descriptor_each(VALUE _self) {
374
- DEFINE_SELF(Descriptor, self, _self);
473
+ static VALUE Descriptor_each(VALUE _self) {
474
+ Descriptor* self = ruby_to_Descriptor(_self);
375
475
 
376
476
  upb_msg_field_iter it;
377
477
  for (upb_msg_field_begin(&it, self->msgdef);
378
478
  !upb_msg_field_done(&it);
379
479
  upb_msg_field_next(&it)) {
380
480
  const upb_fielddef* field = upb_msg_iter_field(&it);
381
- VALUE obj = get_def_obj(field);
481
+ VALUE obj = get_fielddef_obj(self->descriptor_pool, field);
382
482
  rb_yield(obj);
383
483
  }
384
484
  return Qnil;
@@ -391,58 +491,14 @@ VALUE Descriptor_each(VALUE _self) {
391
491
  * Returns the field descriptor for the field with the given name, if present,
392
492
  * or nil if none.
393
493
  */
394
- VALUE Descriptor_lookup(VALUE _self, VALUE name) {
395
- DEFINE_SELF(Descriptor, self, _self);
494
+ static VALUE Descriptor_lookup(VALUE _self, VALUE name) {
495
+ Descriptor* self = ruby_to_Descriptor(_self);
396
496
  const char* s = get_str(name);
397
497
  const upb_fielddef* field = upb_msgdef_ntofz(self->msgdef, s);
398
498
  if (field == NULL) {
399
499
  return Qnil;
400
500
  }
401
- return get_def_obj(field);
402
- }
403
-
404
- /*
405
- * call-seq:
406
- * Descriptor.add_field(field) => nil
407
- *
408
- * Adds the given FieldDescriptor to this message type. This descriptor must not
409
- * have been added to a pool yet. Raises an exception if a field with the same
410
- * name or number already exists. Sub-type references (e.g. for fields of type
411
- * message) are not resolved at this point.
412
- */
413
- VALUE Descriptor_add_field(VALUE _self, VALUE obj) {
414
- DEFINE_SELF(Descriptor, self, _self);
415
- upb_msgdef* mut_def = check_msg_notfrozen(self->msgdef);
416
- FieldDescriptor* def = ruby_to_FieldDescriptor(obj);
417
- upb_fielddef* mut_field_def = check_field_notfrozen(def->fielddef);
418
- CHECK_UPB(
419
- upb_msgdef_addfield(mut_def, mut_field_def, NULL, &status),
420
- "Adding field to Descriptor failed");
421
- add_def_obj(def->fielddef, obj);
422
- return Qnil;
423
- }
424
-
425
- /*
426
- * call-seq:
427
- * Descriptor.add_oneof(oneof) => nil
428
- *
429
- * Adds the given OneofDescriptor to this message type. This descriptor must not
430
- * have been added to a pool yet. Raises an exception if a oneof with the same
431
- * name already exists, or if any of the oneof's fields' names or numbers
432
- * conflict with an existing field in this message type. All fields in the oneof
433
- * are added to the message descriptor. Sub-type references (e.g. for fields of
434
- * type message) are not resolved at this point.
435
- */
436
- VALUE Descriptor_add_oneof(VALUE _self, VALUE obj) {
437
- DEFINE_SELF(Descriptor, self, _self);
438
- upb_msgdef* mut_def = check_msg_notfrozen(self->msgdef);
439
- OneofDescriptor* def = ruby_to_OneofDescriptor(obj);
440
- upb_oneofdef* mut_oneof_def = check_oneof_notfrozen(def->oneofdef);
441
- CHECK_UPB(
442
- upb_msgdef_addoneof(mut_def, mut_oneof_def, NULL, &status),
443
- "Adding oneof to Descriptor failed");
444
- add_def_obj(def->oneofdef, obj);
445
- return Qnil;
501
+ return get_fielddef_obj(self->descriptor_pool, field);
446
502
  }
447
503
 
448
504
  /*
@@ -452,15 +508,15 @@ VALUE Descriptor_add_oneof(VALUE _self, VALUE obj) {
452
508
  * Invokes the given block for each oneof in this message type, passing the
453
509
  * corresponding OneofDescriptor.
454
510
  */
455
- VALUE Descriptor_each_oneof(VALUE _self) {
456
- DEFINE_SELF(Descriptor, self, _self);
511
+ static VALUE Descriptor_each_oneof(VALUE _self) {
512
+ Descriptor* self = ruby_to_Descriptor(_self);
457
513
 
458
514
  upb_msg_oneof_iter it;
459
515
  for (upb_msg_oneof_begin(&it, self->msgdef);
460
516
  !upb_msg_oneof_done(&it);
461
517
  upb_msg_oneof_next(&it)) {
462
518
  const upb_oneofdef* oneof = upb_msg_iter_oneof(&it);
463
- VALUE obj = get_def_obj(oneof);
519
+ VALUE obj = get_oneofdef_obj(self->descriptor_pool, oneof);
464
520
  rb_yield(obj);
465
521
  }
466
522
  return Qnil;
@@ -473,111 +529,101 @@ VALUE Descriptor_each_oneof(VALUE _self) {
473
529
  * Returns the oneof descriptor for the oneof with the given name, if present,
474
530
  * or nil if none.
475
531
  */
476
- VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
477
- DEFINE_SELF(Descriptor, self, _self);
532
+ static VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
533
+ Descriptor* self = ruby_to_Descriptor(_self);
478
534
  const char* s = get_str(name);
479
535
  const upb_oneofdef* oneof = upb_msgdef_ntooz(self->msgdef, s);
480
536
  if (oneof == NULL) {
481
537
  return Qnil;
482
538
  }
483
- return get_def_obj(oneof);
539
+ return get_oneofdef_obj(self->descriptor_pool, oneof);
484
540
  }
485
541
 
486
542
  /*
487
543
  * call-seq:
488
544
  * Descriptor.msgclass => message_klass
489
545
  *
490
- * Returns the Ruby class created for this message type. Valid only once the
491
- * message type has been added to a pool.
546
+ * Returns the Ruby class created for this message type.
492
547
  */
493
- VALUE Descriptor_msgclass(VALUE _self) {
494
- DEFINE_SELF(Descriptor, self, _self);
495
- if (!upb_def_isfrozen((const upb_def*)self->msgdef)) {
496
- rb_raise(rb_eRuntimeError,
497
- "Cannot fetch message class from a Descriptor not yet in a pool.");
498
- }
548
+ static VALUE Descriptor_msgclass(VALUE _self) {
549
+ Descriptor* self = ruby_to_Descriptor(_self);
499
550
  if (self->klass == Qnil) {
500
- self->klass = build_class_from_descriptor(self);
551
+ self->klass = build_class_from_descriptor(_self);
501
552
  }
502
553
  return self->klass;
503
554
  }
504
555
 
556
+ static void Descriptor_register(VALUE module) {
557
+ VALUE klass = rb_define_class_under(
558
+ module, "Descriptor", rb_cObject);
559
+ rb_define_alloc_func(klass, Descriptor_alloc);
560
+ rb_define_method(klass, "initialize", Descriptor_initialize, 3);
561
+ rb_define_method(klass, "each", Descriptor_each, 0);
562
+ rb_define_method(klass, "lookup", Descriptor_lookup, 1);
563
+ rb_define_method(klass, "each_oneof", Descriptor_each_oneof, 0);
564
+ rb_define_method(klass, "lookup_oneof", Descriptor_lookup_oneof, 1);
565
+ rb_define_method(klass, "msgclass", Descriptor_msgclass, 0);
566
+ rb_define_method(klass, "name", Descriptor_name, 0);
567
+ rb_define_method(klass, "file_descriptor", Descriptor_file_descriptor, 0);
568
+ rb_include_module(klass, rb_mEnumerable);
569
+ rb_gc_register_address(&cDescriptor);
570
+ cDescriptor = klass;
571
+ }
572
+
505
573
  // -----------------------------------------------------------------------------
506
574
  // FileDescriptor.
507
575
  // -----------------------------------------------------------------------------
508
576
 
509
- DEFINE_CLASS(FileDescriptor, "Google::Protobuf::FileDescriptor");
577
+ typedef struct {
578
+ const upb_filedef* filedef;
579
+ VALUE descriptor_pool; // Owns the upb_filedef.
580
+ } FileDescriptor;
510
581
 
511
- void FileDescriptor_mark(void* _self) {
512
- }
582
+ static VALUE cFileDescriptor = Qnil;
513
583
 
514
- void FileDescriptor_free(void* _self) {
584
+ static void FileDescriptor_mark(void* _self) {
515
585
  FileDescriptor* self = _self;
516
- upb_filedef_unref(self->filedef, &self->filedef);
517
- xfree(self);
586
+ rb_gc_mark(self->descriptor_pool);
518
587
  }
519
588
 
520
- /*
521
- * call-seq:
522
- * FileDescriptor.new => file
523
- *
524
- * Returns a new file descriptor. The syntax must be set before it's passed
525
- * to a builder.
526
- */
527
- VALUE FileDescriptor_alloc(VALUE klass) {
528
- FileDescriptor* self = ALLOC(FileDescriptor);
529
- VALUE ret = TypedData_Wrap_Struct(klass, &_FileDescriptor_type, self);
530
- upb_filedef* filedef = upb_filedef_new(&self->filedef);
531
- self->filedef = filedef;
589
+ static const rb_data_type_t FileDescriptor_type = {
590
+ "Google::Protobuf::FileDescriptor",
591
+ {FileDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
592
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
593
+ };
594
+
595
+ static FileDescriptor* ruby_to_FileDescriptor(VALUE val) {
596
+ FileDescriptor* ret;
597
+ TypedData_Get_Struct(val, FileDescriptor, &FileDescriptor_type, ret);
532
598
  return ret;
533
599
  }
534
600
 
535
- void FileDescriptor_register(VALUE module) {
536
- VALUE klass = rb_define_class_under(
537
- module, "FileDescriptor", rb_cObject);
538
- rb_define_alloc_func(klass, FileDescriptor_alloc);
539
- rb_define_method(klass, "initialize", FileDescriptor_initialize, -1);
540
- rb_define_method(klass, "name", FileDescriptor_name, 0);
541
- rb_define_method(klass, "syntax", FileDescriptor_syntax, 0);
542
- rb_define_method(klass, "syntax=", FileDescriptor_syntax_set, 1);
543
- rb_gc_register_address(&cFileDescriptor);
544
- cFileDescriptor = klass;
601
+ static VALUE FileDescriptor_alloc(VALUE klass) {
602
+ FileDescriptor* self = ALLOC(FileDescriptor);
603
+ VALUE ret = TypedData_Wrap_Struct(klass, &FileDescriptor_type, self);
604
+ self->descriptor_pool = Qnil;
605
+ self->filedef = NULL;
606
+ return ret;
545
607
  }
546
608
 
547
609
  /*
548
610
  * call-seq:
549
- * FileDescriptor.new(name, options = nil) => file
611
+ * FileDescriptor.new => file
550
612
  *
551
- * Initializes a new file descriptor with the given file name.
552
- * Also accepts an optional "options" hash, specifying other optional
553
- * metadata about the file. The options hash currently accepts the following
554
- * * "syntax": :proto2 or :proto3 (default: :proto3)
613
+ * Returns a new file descriptor. The syntax must be set before it's passed
614
+ * to a builder.
555
615
  */
556
- VALUE FileDescriptor_initialize(int argc, VALUE* argv, VALUE _self) {
557
- DEFINE_SELF(FileDescriptor, self, _self);
558
-
559
- VALUE name_rb;
560
- VALUE options = Qnil;
561
- rb_scan_args(argc, argv, "11", &name_rb, &options);
616
+ static VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
617
+ VALUE descriptor_pool, VALUE ptr) {
618
+ FileDescriptor* self = ruby_to_FileDescriptor(_self);
562
619
 
563
- if (name_rb != Qnil) {
564
- Check_Type(name_rb, T_STRING);
565
- const char* name = get_str(name_rb);
566
- CHECK_UPB(upb_filedef_setname(self->filedef, name, &status),
567
- "Error setting file name");
620
+ if (cookie != c_only_cookie) {
621
+ rb_raise(rb_eRuntimeError,
622
+ "Descriptor objects may not be created from Ruby.");
568
623
  }
569
624
 
570
- // Default syntax is proto3.
571
- VALUE syntax = ID2SYM(rb_intern("proto3"));
572
- if (options != Qnil) {
573
- Check_Type(options, T_HASH);
574
-
575
- if (rb_funcall(options, rb_intern("key?"), 1,
576
- ID2SYM(rb_intern("syntax"))) == Qtrue) {
577
- syntax = rb_hash_lookup(options, ID2SYM(rb_intern("syntax")));
578
- }
579
- }
580
- FileDescriptor_syntax_set(_self, syntax);
625
+ self->descriptor_pool = descriptor_pool;
626
+ self->filedef = (const upb_filedef*)NUM2ULL(ptr);
581
627
 
582
628
  return Qnil;
583
629
  }
@@ -588,8 +634,8 @@ VALUE FileDescriptor_initialize(int argc, VALUE* argv, VALUE _self) {
588
634
  *
589
635
  * Returns the name of the file.
590
636
  */
591
- VALUE FileDescriptor_name(VALUE _self) {
592
- DEFINE_SELF(FileDescriptor, self, _self);
637
+ static VALUE FileDescriptor_name(VALUE _self) {
638
+ FileDescriptor* self = ruby_to_FileDescriptor(_self);
593
639
  const char* name = upb_filedef_name(self->filedef);
594
640
  return name == NULL ? Qnil : rb_str_new2(name);
595
641
  }
@@ -603,8 +649,8 @@ VALUE FileDescriptor_name(VALUE _self) {
603
649
  * Valid syntax versions are:
604
650
  * :proto2 or :proto3.
605
651
  */
606
- VALUE FileDescriptor_syntax(VALUE _self) {
607
- DEFINE_SELF(FileDescriptor, self, _self);
652
+ static VALUE FileDescriptor_syntax(VALUE _self) {
653
+ FileDescriptor* self = ruby_to_FileDescriptor(_self);
608
654
 
609
655
  switch (upb_filedef_syntax(self->filedef)) {
610
656
  case UPB_SYNTAX_PROTO3: return ID2SYM(rb_intern("proto3"));
@@ -613,44 +659,43 @@ VALUE FileDescriptor_syntax(VALUE _self) {
613
659
  }
614
660
  }
615
661
 
616
- /*
617
- * call-seq:
618
- * FileDescriptor.syntax = version
619
- *
620
- * Sets this file descriptor's syntax, can be :proto3 or :proto2.
621
- */
622
- VALUE FileDescriptor_syntax_set(VALUE _self, VALUE syntax_rb) {
623
- DEFINE_SELF(FileDescriptor, self, _self);
624
- Check_Type(syntax_rb, T_SYMBOL);
625
-
626
- upb_syntax_t syntax;
627
- if (SYM2ID(syntax_rb) == rb_intern("proto3")) {
628
- syntax = UPB_SYNTAX_PROTO3;
629
- } else if (SYM2ID(syntax_rb) == rb_intern("proto2")) {
630
- syntax = UPB_SYNTAX_PROTO2;
631
- } else {
632
- rb_raise(rb_eArgError, "Expected :proto3 or :proto3, received '%s'",
633
- rb_id2name(SYM2ID(syntax_rb)));
634
- }
635
-
636
- CHECK_UPB(upb_filedef_setsyntax(self->filedef, syntax, &status),
637
- "Error setting file syntax for proto");
638
- return Qnil;
662
+ static void FileDescriptor_register(VALUE module) {
663
+ VALUE klass = rb_define_class_under(
664
+ module, "FileDescriptor", rb_cObject);
665
+ rb_define_alloc_func(klass, FileDescriptor_alloc);
666
+ rb_define_method(klass, "initialize", FileDescriptor_initialize, 3);
667
+ rb_define_method(klass, "name", FileDescriptor_name, 0);
668
+ rb_define_method(klass, "syntax", FileDescriptor_syntax, 0);
669
+ rb_gc_register_address(&cFileDescriptor);
670
+ cFileDescriptor = klass;
639
671
  }
640
672
 
641
673
  // -----------------------------------------------------------------------------
642
674
  // FieldDescriptor.
643
675
  // -----------------------------------------------------------------------------
644
676
 
645
- DEFINE_CLASS(FieldDescriptor, "Google::Protobuf::FieldDescriptor");
677
+ typedef struct {
678
+ const upb_fielddef* fielddef;
679
+ VALUE descriptor_pool; // Owns the upb_fielddef.
680
+ } FieldDescriptor;
646
681
 
647
- void FieldDescriptor_mark(void* _self) {
648
- }
682
+ static VALUE cFieldDescriptor = Qnil;
649
683
 
650
- void FieldDescriptor_free(void* _self) {
684
+ static void FieldDescriptor_mark(void* _self) {
651
685
  FieldDescriptor* self = _self;
652
- upb_fielddef_unref(self->fielddef, &self->fielddef);
653
- xfree(self);
686
+ rb_gc_mark(self->descriptor_pool);
687
+ }
688
+
689
+ static const rb_data_type_t FieldDescriptor_type = {
690
+ "Google::Protobuf::FieldDescriptor",
691
+ {FieldDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
692
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
693
+ };
694
+
695
+ static FieldDescriptor* ruby_to_FieldDescriptor(VALUE val) {
696
+ FieldDescriptor* ret;
697
+ TypedData_Get_Struct(val, FieldDescriptor, &FieldDescriptor_type, ret);
698
+ return ret;
654
699
  }
655
700
 
656
701
  /*
@@ -660,67 +705,46 @@ void FieldDescriptor_free(void* _self) {
660
705
  * Returns a new field descriptor. Its name, type, etc. must be set before it is
661
706
  * added to a message type.
662
707
  */
663
- VALUE FieldDescriptor_alloc(VALUE klass) {
708
+ static VALUE FieldDescriptor_alloc(VALUE klass) {
664
709
  FieldDescriptor* self = ALLOC(FieldDescriptor);
665
- VALUE ret = TypedData_Wrap_Struct(klass, &_FieldDescriptor_type, self);
666
- upb_fielddef* fielddef = upb_fielddef_new(&self->fielddef);
667
- upb_fielddef_setpacked(fielddef, false);
668
- self->fielddef = fielddef;
710
+ VALUE ret = TypedData_Wrap_Struct(klass, &FieldDescriptor_type, self);
711
+ self->fielddef = NULL;
669
712
  return ret;
670
713
  }
671
714
 
672
- void FieldDescriptor_register(VALUE module) {
673
- VALUE klass = rb_define_class_under(
674
- module, "FieldDescriptor", rb_cObject);
675
- rb_define_alloc_func(klass, FieldDescriptor_alloc);
676
- rb_define_method(klass, "name", FieldDescriptor_name, 0);
677
- rb_define_method(klass, "name=", FieldDescriptor_name_set, 1);
678
- rb_define_method(klass, "type", FieldDescriptor_type, 0);
679
- rb_define_method(klass, "type=", FieldDescriptor_type_set, 1);
680
- rb_define_method(klass, "default", FieldDescriptor_default, 0);
681
- rb_define_method(klass, "default=", FieldDescriptor_default_set, 1);
682
- rb_define_method(klass, "label", FieldDescriptor_label, 0);
683
- rb_define_method(klass, "label=", FieldDescriptor_label_set, 1);
684
- rb_define_method(klass, "number", FieldDescriptor_number, 0);
685
- rb_define_method(klass, "number=", FieldDescriptor_number_set, 1);
686
- rb_define_method(klass, "submsg_name", FieldDescriptor_submsg_name, 0);
687
- rb_define_method(klass, "submsg_name=", FieldDescriptor_submsg_name_set, 1);
688
- rb_define_method(klass, "subtype", FieldDescriptor_subtype, 0);
689
- rb_define_method(klass, "has?", FieldDescriptor_has, 1);
690
- rb_define_method(klass, "clear", FieldDescriptor_clear, 1);
691
- rb_define_method(klass, "get", FieldDescriptor_get, 1);
692
- rb_define_method(klass, "set", FieldDescriptor_set, 2);
693
- rb_gc_register_address(&cFieldDescriptor);
694
- cFieldDescriptor = klass;
695
- }
696
-
697
715
  /*
698
716
  * call-seq:
699
- * FieldDescriptor.name => name
717
+ * EnumDescriptor.new(c_only_cookie, pool, ptr) => EnumDescriptor
700
718
  *
701
- * Returns the name of this field.
719
+ * Creates a descriptor wrapper object. May only be called from C.
702
720
  */
703
- VALUE FieldDescriptor_name(VALUE _self) {
704
- DEFINE_SELF(FieldDescriptor, self, _self);
705
- return rb_str_maybe_null(upb_fielddef_name(self->fielddef));
721
+ static VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
722
+ VALUE descriptor_pool, VALUE ptr) {
723
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
724
+
725
+ if (cookie != c_only_cookie) {
726
+ rb_raise(rb_eRuntimeError,
727
+ "Descriptor objects may not be created from Ruby.");
728
+ }
729
+
730
+ self->descriptor_pool = descriptor_pool;
731
+ self->fielddef = (const upb_fielddef*)NUM2ULL(ptr);
732
+
733
+ return Qnil;
706
734
  }
707
735
 
708
736
  /*
709
737
  * call-seq:
710
- * FieldDescriptor.name = name
738
+ * FieldDescriptor.name => name
711
739
  *
712
- * Sets the name of this field. Cannot be called once the containing message
713
- * type, if any, is added to a pool.
740
+ * Returns the name of this field.
714
741
  */
715
- VALUE FieldDescriptor_name_set(VALUE _self, VALUE str) {
716
- DEFINE_SELF(FieldDescriptor, self, _self);
717
- upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
718
- const char* name = get_str(str);
719
- CHECK_UPB(upb_fielddef_setname(mut_def, name, &status),
720
- "Error setting FieldDescriptor name");
721
- return Qnil;
742
+ static VALUE FieldDescriptor_name(VALUE _self) {
743
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
744
+ return rb_str_maybe_null(upb_fielddef_name(self->fielddef));
722
745
  }
723
746
 
747
+ // Non-static, exposed to other .c files.
724
748
  upb_fieldtype_t ruby_to_fieldtype(VALUE type) {
725
749
  if (TYPE(type) != T_SYMBOL) {
726
750
  rb_raise(rb_eArgError, "Expected symbol for field type.");
@@ -749,27 +773,7 @@ upb_fieldtype_t ruby_to_fieldtype(VALUE type) {
749
773
  return 0;
750
774
  }
751
775
 
752
- VALUE fieldtype_to_ruby(upb_fieldtype_t type) {
753
- switch (type) {
754
- #define CONVERT(upb, ruby) \
755
- case UPB_TYPE_ ## upb : return ID2SYM(rb_intern( # ruby ));
756
- CONVERT(FLOAT, float);
757
- CONVERT(DOUBLE, double);
758
- CONVERT(BOOL, bool);
759
- CONVERT(STRING, string);
760
- CONVERT(BYTES, bytes);
761
- CONVERT(MESSAGE, message);
762
- CONVERT(ENUM, enum);
763
- CONVERT(INT32, int32);
764
- CONVERT(INT64, int64);
765
- CONVERT(UINT32, uint32);
766
- CONVERT(UINT64, uint64);
767
- #undef CONVERT
768
- }
769
- return Qnil;
770
- }
771
-
772
- upb_descriptortype_t ruby_to_descriptortype(VALUE type) {
776
+ static upb_descriptortype_t ruby_to_descriptortype(VALUE type) {
773
777
  if (TYPE(type) != T_SYMBOL) {
774
778
  rb_raise(rb_eArgError, "Expected symbol for field type.");
775
779
  }
@@ -804,7 +808,7 @@ upb_descriptortype_t ruby_to_descriptortype(VALUE type) {
804
808
  return 0;
805
809
  }
806
810
 
807
- VALUE descriptortype_to_ruby(upb_descriptortype_t type) {
811
+ static VALUE descriptortype_to_ruby(upb_descriptortype_t type) {
808
812
  switch (type) {
809
813
  #define CONVERT(upb, ruby) \
810
814
  case UPB_DESCRIPTOR_TYPE_ ## upb : return ID2SYM(rb_intern( # ruby ));
@@ -841,91 +845,27 @@ VALUE descriptortype_to_ruby(upb_descriptortype_t type) {
841
845
  * :int32, :int64, :uint32, :uint64, :float, :double, :bool, :string,
842
846
  * :bytes, :message.
843
847
  */
844
- VALUE FieldDescriptor_type(VALUE _self) {
845
- DEFINE_SELF(FieldDescriptor, self, _self);
846
- if (!upb_fielddef_typeisset(self->fielddef)) {
847
- return Qnil;
848
- }
848
+ static VALUE FieldDescriptor__type(VALUE _self) {
849
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
849
850
  return descriptortype_to_ruby(upb_fielddef_descriptortype(self->fielddef));
850
851
  }
851
852
 
852
- /*
853
- * call-seq:
854
- * FieldDescriptor.type = type
855
- *
856
- * Sets this field's type. Cannot be called if field is part of a message type
857
- * already in a pool.
858
- */
859
- VALUE FieldDescriptor_type_set(VALUE _self, VALUE type) {
860
- DEFINE_SELF(FieldDescriptor, self, _self);
861
- upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
862
- upb_fielddef_setdescriptortype(mut_def, ruby_to_descriptortype(type));
863
- return Qnil;
864
- }
865
-
866
853
  /*
867
854
  * call-seq:
868
855
  * FieldDescriptor.default => default
869
856
  *
870
857
  * Returns this field's default, as a Ruby object, or nil if not yet set.
871
858
  */
872
- VALUE FieldDescriptor_default(VALUE _self) {
873
- DEFINE_SELF(FieldDescriptor, self, _self);
874
- return layout_get_default(self->fielddef);
875
- }
876
-
877
- /*
878
- * call-seq:
879
- * FieldDescriptor.default = default
880
- *
881
- * Sets this field's default value. Raises an exception when calling with
882
- * proto syntax 3.
883
- */
884
- VALUE FieldDescriptor_default_set(VALUE _self, VALUE default_value) {
885
- DEFINE_SELF(FieldDescriptor, self, _self);
886
- upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
887
-
888
- switch (upb_fielddef_type(mut_def)) {
889
- case UPB_TYPE_FLOAT:
890
- upb_fielddef_setdefaultfloat(mut_def, NUM2DBL(default_value));
891
- break;
892
- case UPB_TYPE_DOUBLE:
893
- upb_fielddef_setdefaultdouble(mut_def, NUM2DBL(default_value));
894
- break;
895
- case UPB_TYPE_BOOL:
896
- if (!RB_TYPE_P(default_value, T_TRUE) &&
897
- !RB_TYPE_P(default_value, T_FALSE) &&
898
- !RB_TYPE_P(default_value, T_NIL)) {
899
- rb_raise(cTypeError, "Expected boolean for default value.");
900
- }
901
-
902
- upb_fielddef_setdefaultbool(mut_def, RTEST(default_value));
903
- break;
904
- case UPB_TYPE_ENUM:
905
- case UPB_TYPE_INT32:
906
- upb_fielddef_setdefaultint32(mut_def, NUM2INT(default_value));
907
- break;
908
- case UPB_TYPE_INT64:
909
- upb_fielddef_setdefaultint64(mut_def, NUM2INT(default_value));
910
- break;
911
- case UPB_TYPE_UINT32:
912
- upb_fielddef_setdefaultuint32(mut_def, NUM2UINT(default_value));
913
- break;
914
- case UPB_TYPE_UINT64:
915
- upb_fielddef_setdefaultuint64(mut_def, NUM2UINT(default_value));
916
- break;
917
- case UPB_TYPE_STRING:
918
- case UPB_TYPE_BYTES:
919
- CHECK_UPB(upb_fielddef_setdefaultcstr(mut_def, StringValuePtr(default_value),
920
- &status),
921
- "Error setting default string");
922
- break;
923
- default:
924
- rb_raise(rb_eArgError, "Defaults not supported on field %s.%s",
925
- upb_fielddef_fullname(mut_def), upb_fielddef_name(mut_def));
859
+ static VALUE FieldDescriptor_default(VALUE _self) {
860
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
861
+ const upb_fielddef *f = self->fielddef;
862
+ upb_msgval default_val = {0};
863
+ if (upb_fielddef_issubmsg(f)) {
864
+ return Qnil;
865
+ } else if (!upb_fielddef_isseq(f)) {
866
+ default_val = upb_fielddef_default(f);
926
867
  }
927
-
928
- return Qnil;
868
+ return Convert_UpbToRuby(default_val, TypeInfo_get(self->fielddef), Qnil);
929
869
  }
930
870
 
931
871
  /*
@@ -937,8 +877,8 @@ VALUE FieldDescriptor_default_set(VALUE _self, VALUE default_value) {
937
877
  * Valid field labels are:
938
878
  * :optional, :repeated
939
879
  */
940
- VALUE FieldDescriptor_label(VALUE _self) {
941
- DEFINE_SELF(FieldDescriptor, self, _self);
880
+ static VALUE FieldDescriptor_label(VALUE _self) {
881
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
942
882
  switch (upb_fielddef_label(self->fielddef)) {
943
883
  #define CONVERT(upb, ruby) \
944
884
  case UPB_LABEL_ ## upb : return ID2SYM(rb_intern( # ruby ));
@@ -953,70 +893,17 @@ VALUE FieldDescriptor_label(VALUE _self) {
953
893
  return Qnil;
954
894
  }
955
895
 
956
- /*
957
- * call-seq:
958
- * FieldDescriptor.label = label
959
- *
960
- * Sets the label on this field. Cannot be called if field is part of a message
961
- * type already in a pool.
962
- */
963
- VALUE FieldDescriptor_label_set(VALUE _self, VALUE label) {
964
- DEFINE_SELF(FieldDescriptor, self, _self);
965
- upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
966
- upb_label_t upb_label = -1;
967
- bool converted = false;
968
-
969
- if (TYPE(label) != T_SYMBOL) {
970
- rb_raise(rb_eArgError, "Expected symbol for field label.");
971
- }
972
-
973
- #define CONVERT(upb, ruby) \
974
- if (SYM2ID(label) == rb_intern( # ruby )) { \
975
- upb_label = UPB_LABEL_ ## upb; \
976
- converted = true; \
977
- }
978
-
979
- CONVERT(OPTIONAL, optional);
980
- CONVERT(REQUIRED, required);
981
- CONVERT(REPEATED, repeated);
982
-
983
- #undef CONVERT
984
-
985
- if (!converted) {
986
- rb_raise(rb_eArgError, "Unknown field label.");
987
- }
988
-
989
- upb_fielddef_setlabel(mut_def, upb_label);
990
-
991
- return Qnil;
992
- }
993
-
994
896
  /*
995
897
  * call-seq:
996
898
  * FieldDescriptor.number => number
997
899
  *
998
900
  * Returns the tag number for this field.
999
901
  */
1000
- VALUE FieldDescriptor_number(VALUE _self) {
1001
- DEFINE_SELF(FieldDescriptor, self, _self);
902
+ static VALUE FieldDescriptor_number(VALUE _self) {
903
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
1002
904
  return INT2NUM(upb_fielddef_number(self->fielddef));
1003
905
  }
1004
906
 
1005
- /*
1006
- * call-seq:
1007
- * FieldDescriptor.number = number
1008
- *
1009
- * Sets the tag number for this field. Cannot be called if field is part of a
1010
- * message type already in a pool.
1011
- */
1012
- VALUE FieldDescriptor_number_set(VALUE _self, VALUE number) {
1013
- DEFINE_SELF(FieldDescriptor, self, _self);
1014
- upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
1015
- CHECK_UPB(upb_fielddef_setnumber(mut_def, NUM2INT(number), &status),
1016
- "Error setting field number");
1017
- return Qnil;
1018
- }
1019
-
1020
907
  /*
1021
908
  * call-seq:
1022
909
  * FieldDescriptor.submsg_name => submsg_name
@@ -1026,34 +913,18 @@ VALUE FieldDescriptor_number_set(VALUE _self, VALUE number) {
1026
913
  * name will be resolved within the context of the pool to which the containing
1027
914
  * message type is added.
1028
915
  */
1029
- VALUE FieldDescriptor_submsg_name(VALUE _self) {
1030
- DEFINE_SELF(FieldDescriptor, self, _self);
1031
- if (!upb_fielddef_hassubdef(self->fielddef)) {
1032
- return Qnil;
916
+ static VALUE FieldDescriptor_submsg_name(VALUE _self) {
917
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
918
+ switch (upb_fielddef_type(self->fielddef)) {
919
+ case UPB_TYPE_ENUM:
920
+ return rb_str_new2(
921
+ upb_enumdef_fullname(upb_fielddef_enumsubdef(self->fielddef)));
922
+ case UPB_TYPE_MESSAGE:
923
+ return rb_str_new2(
924
+ upb_msgdef_fullname(upb_fielddef_msgsubdef(self->fielddef)));
925
+ default:
926
+ return Qnil;
1033
927
  }
1034
- return rb_str_maybe_null(upb_fielddef_subdefname(self->fielddef));
1035
- }
1036
-
1037
- /*
1038
- * call-seq:
1039
- * FieldDescriptor.submsg_name = submsg_name
1040
- *
1041
- * Sets the name of the message or enum type corresponding to this field, if it
1042
- * is a message or enum field (respectively). This type name will be resolved
1043
- * within the context of the pool to which the containing message type is added.
1044
- * Cannot be called on field that are not of message or enum type, or on fields
1045
- * that are part of a message type already added to a pool.
1046
- */
1047
- VALUE FieldDescriptor_submsg_name_set(VALUE _self, VALUE value) {
1048
- DEFINE_SELF(FieldDescriptor, self, _self);
1049
- upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
1050
- const char* str = get_str(value);
1051
- if (!upb_fielddef_hassubdef(self->fielddef)) {
1052
- rb_raise(cTypeError, "FieldDescriptor does not have subdef.");
1053
- }
1054
- CHECK_UPB(upb_fielddef_setsubdefname(mut_def, str, &status),
1055
- "Error setting submessage name");
1056
- return Qnil;
1057
928
  }
1058
929
 
1059
930
  /*
@@ -1065,18 +936,18 @@ VALUE FieldDescriptor_submsg_name_set(VALUE _self, VALUE value) {
1065
936
  * called *until* the containing message type is added to a pool (and thus
1066
937
  * resolved).
1067
938
  */
1068
- VALUE FieldDescriptor_subtype(VALUE _self) {
1069
- DEFINE_SELF(FieldDescriptor, self, _self);
1070
- const upb_def* def;
1071
-
1072
- if (!upb_fielddef_hassubdef(self->fielddef)) {
1073
- return Qnil;
1074
- }
1075
- def = upb_fielddef_subdef(self->fielddef);
1076
- if (def == NULL) {
1077
- return Qnil;
939
+ static VALUE FieldDescriptor_subtype(VALUE _self) {
940
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
941
+ switch (upb_fielddef_type(self->fielddef)) {
942
+ case UPB_TYPE_ENUM:
943
+ return get_enumdef_obj(self->descriptor_pool,
944
+ upb_fielddef_enumsubdef(self->fielddef));
945
+ case UPB_TYPE_MESSAGE:
946
+ return get_msgdef_obj(self->descriptor_pool,
947
+ upb_fielddef_msgsubdef(self->fielddef));
948
+ default:
949
+ return Qnil;
1078
950
  }
1079
- return get_def_obj(def);
1080
951
  }
1081
952
 
1082
953
  /*
@@ -1086,14 +957,19 @@ VALUE FieldDescriptor_subtype(VALUE _self) {
1086
957
  * Returns the value set for this field on the given message. Raises an
1087
958
  * exception if message is of the wrong type.
1088
959
  */
1089
- VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
1090
- DEFINE_SELF(FieldDescriptor, self, _self);
1091
- MessageHeader* msg;
1092
- TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
1093
- if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
960
+ static VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
961
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
962
+ const upb_msgdef *m;
963
+ const upb_msgdef *msg = Message_Get(msg_rb, &m);
964
+ VALUE arena = Message_GetArena(msg_rb);
965
+ upb_msgval msgval;
966
+
967
+ if (m != upb_fielddef_containingtype(self->fielddef)) {
1094
968
  rb_raise(cTypeError, "get method called on wrong message type");
1095
969
  }
1096
- return layout_get(msg->descriptor->layout, Message_data(msg), self->fielddef);
970
+
971
+ msgval = upb_msg_get(msg, self->fielddef);
972
+ return Convert_UpbToRuby(msgval, TypeInfo_get(self->fielddef), arena);
1097
973
  }
1098
974
 
1099
975
  /*
@@ -1101,19 +977,20 @@ VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
1101
977
  * FieldDescriptor.has?(message) => boolean
1102
978
  *
1103
979
  * Returns whether the value is set on the given message. Raises an
1104
- * exception when calling with proto syntax 3.
980
+ * exception when calling for fields that do not have presence.
1105
981
  */
1106
- VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
1107
- DEFINE_SELF(FieldDescriptor, self, _self);
1108
- MessageHeader* msg;
1109
- TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
1110
- if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
982
+ static VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
983
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
984
+ const upb_msgdef *m;
985
+ const upb_msgdef *msg = Message_Get(msg_rb, &m);
986
+
987
+ if (m != upb_fielddef_containingtype(self->fielddef)) {
1111
988
  rb_raise(cTypeError, "has method called on wrong message type");
1112
989
  } else if (!upb_fielddef_haspresence(self->fielddef)) {
1113
990
  rb_raise(rb_eArgError, "does not track presence");
1114
991
  }
1115
992
 
1116
- return layout_has(msg->descriptor->layout, Message_data(msg), self->fielddef);
993
+ return upb_msg_has(msg, self->fielddef) ? Qtrue : Qfalse;
1117
994
  }
1118
995
 
1119
996
  /*
@@ -1122,15 +999,16 @@ VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
1122
999
  *
1123
1000
  * Clears the field from the message if it's set.
1124
1001
  */
1125
- VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {
1126
- DEFINE_SELF(FieldDescriptor, self, _self);
1127
- MessageHeader* msg;
1128
- TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
1129
- if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
1002
+ static VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {
1003
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
1004
+ const upb_msgdef *m;
1005
+ upb_msgdef *msg = Message_GetMutable(msg_rb, &m);
1006
+
1007
+ if (m != upb_fielddef_containingtype(self->fielddef)) {
1130
1008
  rb_raise(cTypeError, "has method called on wrong message type");
1131
1009
  }
1132
1010
 
1133
- layout_clear(msg->descriptor->layout, Message_data(msg), self->fielddef);
1011
+ upb_msg_clearfield(msg, self->fielddef);
1134
1012
  return Qnil;
1135
1013
  }
1136
1014
 
@@ -1142,30 +1020,69 @@ VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {
1142
1020
  * message. Raises an exception if message is of the wrong type. Performs the
1143
1021
  * ordinary type-checks for field setting.
1144
1022
  */
1145
- VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value) {
1146
- DEFINE_SELF(FieldDescriptor, self, _self);
1147
- MessageHeader* msg;
1148
- TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
1149
- if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
1023
+ static VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value) {
1024
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
1025
+ const upb_msgdef *m;
1026
+ upb_msgdef *msg = Message_GetMutable(msg_rb, &m);
1027
+ upb_arena *arena = Arena_get(Message_GetArena(msg_rb));
1028
+ upb_msgval msgval;
1029
+
1030
+ if (m != upb_fielddef_containingtype(self->fielddef)) {
1150
1031
  rb_raise(cTypeError, "set method called on wrong message type");
1151
1032
  }
1152
- layout_set(msg->descriptor->layout, Message_data(msg), self->fielddef, value);
1033
+
1034
+ msgval = Convert_RubyToUpb(value, upb_fielddef_name(self->fielddef),
1035
+ TypeInfo_get(self->fielddef), arena);
1036
+ upb_msg_set(msg, self->fielddef, msgval, arena);
1153
1037
  return Qnil;
1154
1038
  }
1155
1039
 
1040
+ static void FieldDescriptor_register(VALUE module) {
1041
+ VALUE klass = rb_define_class_under(
1042
+ module, "FieldDescriptor", rb_cObject);
1043
+ rb_define_alloc_func(klass, FieldDescriptor_alloc);
1044
+ rb_define_method(klass, "initialize", FieldDescriptor_initialize, 3);
1045
+ rb_define_method(klass, "name", FieldDescriptor_name, 0);
1046
+ rb_define_method(klass, "type", FieldDescriptor__type, 0);
1047
+ rb_define_method(klass, "default", FieldDescriptor_default, 0);
1048
+ rb_define_method(klass, "label", FieldDescriptor_label, 0);
1049
+ rb_define_method(klass, "number", FieldDescriptor_number, 0);
1050
+ rb_define_method(klass, "submsg_name", FieldDescriptor_submsg_name, 0);
1051
+ rb_define_method(klass, "subtype", FieldDescriptor_subtype, 0);
1052
+ rb_define_method(klass, "has?", FieldDescriptor_has, 1);
1053
+ rb_define_method(klass, "clear", FieldDescriptor_clear, 1);
1054
+ rb_define_method(klass, "get", FieldDescriptor_get, 1);
1055
+ rb_define_method(klass, "set", FieldDescriptor_set, 2);
1056
+ rb_gc_register_address(&cFieldDescriptor);
1057
+ cFieldDescriptor = klass;
1058
+ }
1059
+
1156
1060
  // -----------------------------------------------------------------------------
1157
1061
  // OneofDescriptor.
1158
1062
  // -----------------------------------------------------------------------------
1159
1063
 
1160
- DEFINE_CLASS(OneofDescriptor, "Google::Protobuf::OneofDescriptor");
1064
+ typedef struct {
1065
+ const upb_oneofdef* oneofdef;
1066
+ VALUE descriptor_pool; // Owns the upb_oneofdef.
1067
+ } OneofDescriptor;
1161
1068
 
1162
- void OneofDescriptor_mark(void* _self) {
1163
- }
1069
+ static VALUE cOneofDescriptor = Qnil;
1164
1070
 
1165
- void OneofDescriptor_free(void* _self) {
1071
+ static void OneofDescriptor_mark(void* _self) {
1166
1072
  OneofDescriptor* self = _self;
1167
- upb_oneofdef_unref(self->oneofdef, &self->oneofdef);
1168
- xfree(self);
1073
+ rb_gc_mark(self->descriptor_pool);
1074
+ }
1075
+
1076
+ static const rb_data_type_t OneofDescriptor_type = {
1077
+ "Google::Protobuf::OneofDescriptor",
1078
+ {OneofDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
1079
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
1080
+ };
1081
+
1082
+ static OneofDescriptor* ruby_to_OneofDescriptor(VALUE val) {
1083
+ OneofDescriptor* ret;
1084
+ TypedData_Get_Struct(val, OneofDescriptor, &OneofDescriptor_type, ret);
1085
+ return ret;
1169
1086
  }
1170
1087
 
1171
1088
  /*
@@ -1175,77 +1092,44 @@ void OneofDescriptor_free(void* _self) {
1175
1092
  * Creates a new, empty, oneof descriptor. The oneof may only be modified prior
1176
1093
  * to being added to a message descriptor which is subsequently added to a pool.
1177
1094
  */
1178
- VALUE OneofDescriptor_alloc(VALUE klass) {
1095
+ static VALUE OneofDescriptor_alloc(VALUE klass) {
1179
1096
  OneofDescriptor* self = ALLOC(OneofDescriptor);
1180
- VALUE ret = TypedData_Wrap_Struct(klass, &_OneofDescriptor_type, self);
1181
- self->oneofdef = upb_oneofdef_new(&self->oneofdef);
1097
+ VALUE ret = TypedData_Wrap_Struct(klass, &OneofDescriptor_type, self);
1098
+ self->oneofdef = NULL;
1099
+ self->descriptor_pool = Qnil;
1182
1100
  return ret;
1183
1101
  }
1184
1102
 
1185
- void OneofDescriptor_register(VALUE module) {
1186
- VALUE klass = rb_define_class_under(
1187
- module, "OneofDescriptor", rb_cObject);
1188
- rb_define_alloc_func(klass, OneofDescriptor_alloc);
1189
- rb_define_method(klass, "name", OneofDescriptor_name, 0);
1190
- rb_define_method(klass, "name=", OneofDescriptor_name_set, 1);
1191
- rb_define_method(klass, "add_field", OneofDescriptor_add_field, 1);
1192
- rb_define_method(klass, "each", OneofDescriptor_each, 0);
1193
- rb_include_module(klass, rb_mEnumerable);
1194
- rb_gc_register_address(&cOneofDescriptor);
1195
- cOneofDescriptor = klass;
1196
- }
1197
-
1198
1103
  /*
1199
1104
  * call-seq:
1200
- * OneofDescriptor.name => name
1105
+ * OneofDescriptor.new(c_only_cookie, pool, ptr) => OneofDescriptor
1201
1106
  *
1202
- * Returns the name of this oneof.
1107
+ * Creates a descriptor wrapper object. May only be called from C.
1203
1108
  */
1204
- VALUE OneofDescriptor_name(VALUE _self) {
1205
- DEFINE_SELF(OneofDescriptor, self, _self);
1206
- return rb_str_maybe_null(upb_oneofdef_name(self->oneofdef));
1207
- }
1109
+ static VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
1110
+ VALUE descriptor_pool, VALUE ptr) {
1111
+ OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
1112
+
1113
+ if (cookie != c_only_cookie) {
1114
+ rb_raise(rb_eRuntimeError,
1115
+ "Descriptor objects may not be created from Ruby.");
1116
+ }
1117
+
1118
+ self->descriptor_pool = descriptor_pool;
1119
+ self->oneofdef = (const upb_oneofdef*)NUM2ULL(ptr);
1208
1120
 
1209
- /*
1210
- * call-seq:
1211
- * OneofDescriptor.name = name
1212
- *
1213
- * Sets a new name for this oneof. The oneof must not have been added to a
1214
- * message descriptor yet.
1215
- */
1216
- VALUE OneofDescriptor_name_set(VALUE _self, VALUE value) {
1217
- DEFINE_SELF(OneofDescriptor, self, _self);
1218
- upb_oneofdef* mut_def = check_oneof_notfrozen(self->oneofdef);
1219
- const char* str = get_str(value);
1220
- CHECK_UPB(upb_oneofdef_setname(mut_def, str, &status),
1221
- "Error setting oneof name");
1222
1121
  return Qnil;
1223
1122
  }
1224
1123
 
1225
1124
  /*
1226
1125
  * call-seq:
1227
- * OneofDescriptor.add_field(field) => nil
1228
- *
1229
- * Adds a field to this oneof. The field may have been added to this oneof in
1230
- * the past, or the message to which this oneof belongs (if any), but may not
1231
- * have already been added to any other oneof or message. Otherwise, an
1232
- * exception is raised.
1126
+ * OneofDescriptor.name => name
1233
1127
  *
1234
- * All fields added to the oneof via this method will be automatically added to
1235
- * the message to which this oneof belongs, if it belongs to one currently, or
1236
- * else will be added to any message to which the oneof is later added at the
1237
- * time that it is added.
1128
+ * Returns the name of this oneof.
1238
1129
  */
1239
- VALUE OneofDescriptor_add_field(VALUE _self, VALUE obj) {
1240
- DEFINE_SELF(OneofDescriptor, self, _self);
1241
- upb_oneofdef* mut_def = check_oneof_notfrozen(self->oneofdef);
1242
- FieldDescriptor* def = ruby_to_FieldDescriptor(obj);
1243
- upb_fielddef* mut_field_def = check_field_notfrozen(def->fielddef);
1244
- CHECK_UPB(
1245
- upb_oneofdef_addfield(mut_def, mut_field_def, NULL, &status),
1246
- "Adding field to OneofDescriptor failed");
1247
- add_def_obj(def->fielddef, obj);
1248
- return Qnil;
1130
+ static VALUE OneofDescriptor_name(VALUE _self) {
1131
+ OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
1132
+ return rb_str_maybe_null(upb_oneofdef_name(self->oneofdef));
1249
1133
  }
1250
1134
 
1251
1135
  /*
@@ -1254,97 +1138,107 @@ VALUE OneofDescriptor_add_field(VALUE _self, VALUE obj) {
1254
1138
  *
1255
1139
  * Iterates through fields in this oneof, yielding to the block on each one.
1256
1140
  */
1257
- VALUE OneofDescriptor_each(VALUE _self, VALUE field) {
1258
- DEFINE_SELF(OneofDescriptor, self, _self);
1141
+ static VALUE OneofDescriptor_each(VALUE _self) {
1142
+ OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
1259
1143
  upb_oneof_iter it;
1260
1144
  for (upb_oneof_begin(&it, self->oneofdef);
1261
1145
  !upb_oneof_done(&it);
1262
1146
  upb_oneof_next(&it)) {
1263
1147
  const upb_fielddef* f = upb_oneof_iter_field(&it);
1264
- VALUE obj = get_def_obj(f);
1148
+ VALUE obj = get_fielddef_obj(self->descriptor_pool, f);
1265
1149
  rb_yield(obj);
1266
1150
  }
1267
1151
  return Qnil;
1268
1152
  }
1269
1153
 
1154
+ static void OneofDescriptor_register(VALUE module) {
1155
+ VALUE klass = rb_define_class_under(
1156
+ module, "OneofDescriptor", rb_cObject);
1157
+ rb_define_alloc_func(klass, OneofDescriptor_alloc);
1158
+ rb_define_method(klass, "initialize", OneofDescriptor_initialize, 3);
1159
+ rb_define_method(klass, "name", OneofDescriptor_name, 0);
1160
+ rb_define_method(klass, "each", OneofDescriptor_each, 0);
1161
+ rb_include_module(klass, rb_mEnumerable);
1162
+ rb_gc_register_address(&cOneofDescriptor);
1163
+ cOneofDescriptor = klass;
1164
+ }
1165
+
1270
1166
  // -----------------------------------------------------------------------------
1271
1167
  // EnumDescriptor.
1272
1168
  // -----------------------------------------------------------------------------
1273
1169
 
1274
- DEFINE_CLASS(EnumDescriptor, "Google::Protobuf::EnumDescriptor");
1170
+ typedef struct {
1171
+ const upb_enumdef* enumdef;
1172
+ VALUE module; // begins as nil
1173
+ VALUE descriptor_pool; // Owns the upb_enumdef.
1174
+ } EnumDescriptor;
1275
1175
 
1276
- void EnumDescriptor_mark(void* _self) {
1176
+ static VALUE cEnumDescriptor = Qnil;
1177
+
1178
+ static void EnumDescriptor_mark(void* _self) {
1277
1179
  EnumDescriptor* self = _self;
1278
1180
  rb_gc_mark(self->module);
1181
+ rb_gc_mark(self->descriptor_pool);
1279
1182
  }
1280
1183
 
1281
- void EnumDescriptor_free(void* _self) {
1282
- EnumDescriptor* self = _self;
1283
- upb_enumdef_unref(self->enumdef, &self->enumdef);
1284
- xfree(self);
1184
+ static const rb_data_type_t EnumDescriptor_type = {
1185
+ "Google::Protobuf::EnumDescriptor",
1186
+ {EnumDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
1187
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
1188
+ };
1189
+
1190
+ static EnumDescriptor* ruby_to_EnumDescriptor(VALUE val) {
1191
+ EnumDescriptor* ret;
1192
+ TypedData_Get_Struct(val, EnumDescriptor, &EnumDescriptor_type, ret);
1193
+ return ret;
1285
1194
  }
1286
1195
 
1287
- /*
1288
- * call-seq:
1289
- * EnumDescriptor.new => enum_descriptor
1290
- *
1291
- * Creates a new, empty, enum descriptor. Must be added to a pool before the
1292
- * enum type can be used. The enum type may only be modified prior to adding to
1293
- * a pool.
1294
- */
1295
- VALUE EnumDescriptor_alloc(VALUE klass) {
1196
+ static VALUE EnumDescriptor_alloc(VALUE klass) {
1296
1197
  EnumDescriptor* self = ALLOC(EnumDescriptor);
1297
- VALUE ret = TypedData_Wrap_Struct(klass, &_EnumDescriptor_type, self);
1298
- self->enumdef = upb_enumdef_new(&self->enumdef);
1198
+ VALUE ret = TypedData_Wrap_Struct(klass, &EnumDescriptor_type, self);
1199
+ self->enumdef = NULL;
1299
1200
  self->module = Qnil;
1201
+ self->descriptor_pool = Qnil;
1300
1202
  return ret;
1301
1203
  }
1302
1204
 
1303
- void EnumDescriptor_register(VALUE module) {
1304
- VALUE klass = rb_define_class_under(
1305
- module, "EnumDescriptor", rb_cObject);
1306
- rb_define_alloc_func(klass, EnumDescriptor_alloc);
1307
- rb_define_method(klass, "initialize", EnumDescriptor_initialize, 1);
1308
- rb_define_method(klass, "name", EnumDescriptor_name, 0);
1309
- rb_define_method(klass, "name=", EnumDescriptor_name_set, 1);
1310
- rb_define_method(klass, "add_value", EnumDescriptor_add_value, 2);
1311
- rb_define_method(klass, "lookup_name", EnumDescriptor_lookup_name, 1);
1312
- rb_define_method(klass, "lookup_value", EnumDescriptor_lookup_value, 1);
1313
- rb_define_method(klass, "each", EnumDescriptor_each, 0);
1314
- rb_define_method(klass, "enummodule", EnumDescriptor_enummodule, 0);
1315
- rb_define_method(klass, "file_descriptor", EnumDescriptor_file_descriptor, 0);
1316
- rb_include_module(klass, rb_mEnumerable);
1317
- rb_gc_register_address(&cEnumDescriptor);
1318
- cEnumDescriptor = klass;
1205
+ // Exposed to other modules in defs.h.
1206
+ const upb_enumdef *EnumDescriptor_GetEnumDef(VALUE enum_desc_rb) {
1207
+ EnumDescriptor *desc = ruby_to_EnumDescriptor(enum_desc_rb);
1208
+ return desc->enumdef;
1319
1209
  }
1320
1210
 
1321
1211
  /*
1322
1212
  * call-seq:
1323
- * Descriptor.new(file_descriptor)
1213
+ * EnumDescriptor.new(c_only_cookie, ptr) => EnumDescriptor
1324
1214
  *
1325
- * Initializes a new descriptor and assigns a file descriptor to it.
1215
+ * Creates a descriptor wrapper object. May only be called from C.
1326
1216
  */
1327
- VALUE EnumDescriptor_initialize(VALUE _self, VALUE file_descriptor_rb) {
1328
- DEFINE_SELF(EnumDescriptor, self, _self);
1329
- FileDescriptor* file_descriptor = ruby_to_FileDescriptor(file_descriptor_rb);
1330
- CHECK_UPB(
1331
- upb_filedef_addenum(file_descriptor->filedef, self->enumdef,
1332
- NULL, &status),
1333
- "Failed to associate enum to file descriptor.");
1334
- add_def_obj(file_descriptor->filedef, file_descriptor_rb);
1217
+ static VALUE EnumDescriptor_initialize(VALUE _self, VALUE cookie,
1218
+ VALUE descriptor_pool, VALUE ptr) {
1219
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1220
+
1221
+ if (cookie != c_only_cookie) {
1222
+ rb_raise(rb_eRuntimeError,
1223
+ "Descriptor objects may not be created from Ruby.");
1224
+ }
1225
+
1226
+ self->descriptor_pool = descriptor_pool;
1227
+ self->enumdef = (const upb_enumdef*)NUM2ULL(ptr);
1335
1228
 
1336
1229
  return Qnil;
1337
1230
  }
1338
1231
 
1339
1232
  /*
1340
1233
  * call-seq:
1341
- * Descriptor.file_descriptor
1234
+ * EnumDescriptor.file_descriptor
1342
1235
  *
1343
1236
  * Returns the FileDescriptor object this enum belongs to.
1344
1237
  */
1345
- VALUE EnumDescriptor_file_descriptor(VALUE _self) {
1346
- DEFINE_SELF(EnumDescriptor, self, _self);
1347
- return get_def_obj(upb_def_file(self->enumdef));
1238
+ static VALUE EnumDescriptor_file_descriptor(VALUE _self) {
1239
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1240
+ return get_filedef_obj(self->descriptor_pool,
1241
+ upb_enumdef_file(self->enumdef));
1348
1242
  }
1349
1243
 
1350
1244
  /*
@@ -1353,45 +1247,11 @@ VALUE EnumDescriptor_file_descriptor(VALUE _self) {
1353
1247
  *
1354
1248
  * Returns the name of this enum type.
1355
1249
  */
1356
- VALUE EnumDescriptor_name(VALUE _self) {
1357
- DEFINE_SELF(EnumDescriptor, self, _self);
1250
+ static VALUE EnumDescriptor_name(VALUE _self) {
1251
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1358
1252
  return rb_str_maybe_null(upb_enumdef_fullname(self->enumdef));
1359
1253
  }
1360
1254
 
1361
- /*
1362
- * call-seq:
1363
- * EnumDescriptor.name = name
1364
- *
1365
- * Sets the name of this enum type. Cannot be called if the enum type has
1366
- * already been added to a pool.
1367
- */
1368
- VALUE EnumDescriptor_name_set(VALUE _self, VALUE str) {
1369
- DEFINE_SELF(EnumDescriptor, self, _self);
1370
- upb_enumdef* mut_def = check_enum_notfrozen(self->enumdef);
1371
- const char* name = get_str(str);
1372
- CHECK_UPB(upb_enumdef_setfullname(mut_def, name, &status),
1373
- "Error setting EnumDescriptor name");
1374
- return Qnil;
1375
- }
1376
-
1377
- /*
1378
- * call-seq:
1379
- * EnumDescriptor.add_value(key, value)
1380
- *
1381
- * Adds a new key => value mapping to this enum type. Key must be given as a
1382
- * Ruby symbol. Cannot be called if the enum type has already been added to a
1383
- * pool. Will raise an exception if the key or value is already in use.
1384
- */
1385
- VALUE EnumDescriptor_add_value(VALUE _self, VALUE name, VALUE number) {
1386
- DEFINE_SELF(EnumDescriptor, self, _self);
1387
- upb_enumdef* mut_def = check_enum_notfrozen(self->enumdef);
1388
- const char* name_str = rb_id2name(SYM2ID(name));
1389
- int32_t val = NUM2INT(number);
1390
- CHECK_UPB(upb_enumdef_addval(mut_def, name_str, val, &status),
1391
- "Error adding value to enum");
1392
- return Qnil;
1393
- }
1394
-
1395
1255
  /*
1396
1256
  * call-seq:
1397
1257
  * EnumDescriptor.lookup_name(name) => value
@@ -1399,8 +1259,8 @@ VALUE EnumDescriptor_add_value(VALUE _self, VALUE name, VALUE number) {
1399
1259
  * Returns the numeric value corresponding to the given key name (as a Ruby
1400
1260
  * symbol), or nil if none.
1401
1261
  */
1402
- VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
1403
- DEFINE_SELF(EnumDescriptor, self, _self);
1262
+ static VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
1263
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1404
1264
  const char* name_str= rb_id2name(SYM2ID(name));
1405
1265
  int32_t val = 0;
1406
1266
  if (upb_enumdef_ntoiz(self->enumdef, name_str, &val)) {
@@ -1417,8 +1277,8 @@ VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
1417
1277
  * Returns the key name (as a Ruby symbol) corresponding to the integer value,
1418
1278
  * or nil if none.
1419
1279
  */
1420
- VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
1421
- DEFINE_SELF(EnumDescriptor, self, _self);
1280
+ static VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
1281
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1422
1282
  int32_t val = NUM2INT(number);
1423
1283
  const char* name = upb_enumdef_iton(self->enumdef, val);
1424
1284
  if (name != NULL) {
@@ -1435,8 +1295,8 @@ VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
1435
1295
  * Iterates over key => value mappings in this enum's definition, yielding to
1436
1296
  * the block with (key, value) arguments for each one.
1437
1297
  */
1438
- VALUE EnumDescriptor_each(VALUE _self) {
1439
- DEFINE_SELF(EnumDescriptor, self, _self);
1298
+ static VALUE EnumDescriptor_each(VALUE _self) {
1299
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1440
1300
 
1441
1301
  upb_enum_iter it;
1442
1302
  for (upb_enum_begin(&it, self->enumdef);
@@ -1454,139 +1314,499 @@ VALUE EnumDescriptor_each(VALUE _self) {
1454
1314
  * call-seq:
1455
1315
  * EnumDescriptor.enummodule => module
1456
1316
  *
1457
- * Returns the Ruby module corresponding to this enum type. Cannot be called
1458
- * until the enum descriptor has been added to a pool.
1317
+ * Returns the Ruby module corresponding to this enum type.
1459
1318
  */
1460
- VALUE EnumDescriptor_enummodule(VALUE _self) {
1461
- DEFINE_SELF(EnumDescriptor, self, _self);
1462
- if (!upb_def_isfrozen((const upb_def*)self->enumdef)) {
1463
- rb_raise(rb_eRuntimeError,
1464
- "Cannot fetch enum module from an EnumDescriptor not yet "
1465
- "in a pool.");
1466
- }
1319
+ static VALUE EnumDescriptor_enummodule(VALUE _self) {
1320
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1467
1321
  if (self->module == Qnil) {
1468
- self->module = build_module_from_enumdesc(self);
1322
+ self->module = build_module_from_enumdesc(_self);
1469
1323
  }
1470
1324
  return self->module;
1471
1325
  }
1472
1326
 
1327
+ static void EnumDescriptor_register(VALUE module) {
1328
+ VALUE klass = rb_define_class_under(
1329
+ module, "EnumDescriptor", rb_cObject);
1330
+ rb_define_alloc_func(klass, EnumDescriptor_alloc);
1331
+ rb_define_method(klass, "initialize", EnumDescriptor_initialize, 3);
1332
+ rb_define_method(klass, "name", EnumDescriptor_name, 0);
1333
+ rb_define_method(klass, "lookup_name", EnumDescriptor_lookup_name, 1);
1334
+ rb_define_method(klass, "lookup_value", EnumDescriptor_lookup_value, 1);
1335
+ rb_define_method(klass, "each", EnumDescriptor_each, 0);
1336
+ rb_define_method(klass, "enummodule", EnumDescriptor_enummodule, 0);
1337
+ rb_define_method(klass, "file_descriptor", EnumDescriptor_file_descriptor, 0);
1338
+ rb_include_module(klass, rb_mEnumerable);
1339
+ rb_gc_register_address(&cEnumDescriptor);
1340
+ cEnumDescriptor = klass;
1341
+ }
1342
+
1473
1343
  // -----------------------------------------------------------------------------
1474
- // MessageBuilderContext.
1344
+ // FileBuilderContext.
1475
1345
  // -----------------------------------------------------------------------------
1476
1346
 
1477
- DEFINE_CLASS(MessageBuilderContext,
1478
- "Google::Protobuf::Internal::MessageBuilderContext");
1347
+ typedef struct {
1348
+ upb_arena *arena;
1349
+ google_protobuf_FileDescriptorProto* file_proto;
1350
+ VALUE descriptor_pool;
1351
+ } FileBuilderContext;
1479
1352
 
1480
- void MessageBuilderContext_mark(void* _self) {
1481
- MessageBuilderContext* self = _self;
1482
- rb_gc_mark(self->descriptor);
1483
- rb_gc_mark(self->builder);
1353
+ static VALUE cFileBuilderContext = Qnil;
1354
+
1355
+ static void FileBuilderContext_mark(void* _self) {
1356
+ FileBuilderContext* self = _self;
1357
+ rb_gc_mark(self->descriptor_pool);
1484
1358
  }
1485
1359
 
1486
- void MessageBuilderContext_free(void* _self) {
1487
- MessageBuilderContext* self = _self;
1360
+ static void FileBuilderContext_free(void* _self) {
1361
+ FileBuilderContext* self = _self;
1362
+ upb_arena_free(self->arena);
1488
1363
  xfree(self);
1489
1364
  }
1490
1365
 
1491
- VALUE MessageBuilderContext_alloc(VALUE klass) {
1492
- MessageBuilderContext* self = ALLOC(MessageBuilderContext);
1493
- VALUE ret = TypedData_Wrap_Struct(
1494
- klass, &_MessageBuilderContext_type, self);
1495
- self->descriptor = Qnil;
1496
- self->builder = Qnil;
1366
+ static const rb_data_type_t FileBuilderContext_type = {
1367
+ "Google::Protobuf::Internal::FileBuilderContext",
1368
+ {FileBuilderContext_mark, FileBuilderContext_free, NULL},
1369
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
1370
+ };
1371
+
1372
+ static FileBuilderContext* ruby_to_FileBuilderContext(VALUE val) {
1373
+ FileBuilderContext* ret;
1374
+ TypedData_Get_Struct(val, FileBuilderContext, &FileBuilderContext_type, ret);
1497
1375
  return ret;
1498
1376
  }
1499
1377
 
1500
- void MessageBuilderContext_register(VALUE module) {
1501
- VALUE klass = rb_define_class_under(
1502
- module, "MessageBuilderContext", rb_cObject);
1503
- rb_define_alloc_func(klass, MessageBuilderContext_alloc);
1504
- rb_define_method(klass, "initialize",
1505
- MessageBuilderContext_initialize, 2);
1506
- rb_define_method(klass, "optional", MessageBuilderContext_optional, -1);
1507
- rb_define_method(klass, "required", MessageBuilderContext_required, -1);
1508
- rb_define_method(klass, "repeated", MessageBuilderContext_repeated, -1);
1509
- rb_define_method(klass, "map", MessageBuilderContext_map, -1);
1510
- rb_define_method(klass, "oneof", MessageBuilderContext_oneof, 1);
1511
- rb_gc_register_address(&cMessageBuilderContext);
1512
- cMessageBuilderContext = klass;
1378
+ static upb_strview FileBuilderContext_strdup2(VALUE _self, const char *str) {
1379
+ FileBuilderContext* self = ruby_to_FileBuilderContext(_self);
1380
+ upb_strview ret;
1381
+ char *data;
1382
+
1383
+ ret.size = strlen(str);
1384
+ data = upb_malloc(upb_arena_alloc(self->arena), ret.size + 1);
1385
+ ret.data = data;
1386
+ memcpy(data, str, ret.size);
1387
+ /* Null-terminate required by rewrite_enum_defaults() above. */
1388
+ data[ret.size] = '\0';
1389
+ return ret;
1390
+ }
1391
+
1392
+ static upb_strview FileBuilderContext_strdup(VALUE _self, VALUE rb_str) {
1393
+ return FileBuilderContext_strdup2(_self, get_str(rb_str));
1394
+ }
1395
+
1396
+ static upb_strview FileBuilderContext_strdup_sym(VALUE _self, VALUE rb_sym) {
1397
+ Check_Type(rb_sym, T_SYMBOL);
1398
+ return FileBuilderContext_strdup(_self, rb_id2str(SYM2ID(rb_sym)));
1399
+ }
1400
+
1401
+ static VALUE FileBuilderContext_alloc(VALUE klass) {
1402
+ FileBuilderContext* self = ALLOC(FileBuilderContext);
1403
+ VALUE ret = TypedData_Wrap_Struct(klass, &FileBuilderContext_type, self);
1404
+ self->arena = upb_arena_new();
1405
+ self->file_proto = google_protobuf_FileDescriptorProto_new(self->arena);
1406
+ self->descriptor_pool = Qnil;
1407
+ return ret;
1513
1408
  }
1514
1409
 
1515
1410
  /*
1516
1411
  * call-seq:
1517
- * MessageBuilderContext.new(desc, builder) => context
1412
+ * FileBuilderContext.new(descriptor_pool) => context
1518
1413
  *
1519
- * Create a new message builder context around the given message descriptor and
1414
+ * Create a new file builder context for the given file descriptor and
1520
1415
  * builder context. This class is intended to serve as a DSL context to be used
1521
1416
  * with #instance_eval.
1522
1417
  */
1523
- VALUE MessageBuilderContext_initialize(VALUE _self,
1524
- VALUE msgdef,
1525
- VALUE builder) {
1526
- DEFINE_SELF(MessageBuilderContext, self, _self);
1527
- self->descriptor = msgdef;
1528
- self->builder = builder;
1529
- return Qnil;
1530
- }
1418
+ static VALUE FileBuilderContext_initialize(VALUE _self, VALUE descriptor_pool,
1419
+ VALUE name, VALUE options) {
1420
+ FileBuilderContext* self = ruby_to_FileBuilderContext(_self);
1421
+ self->descriptor_pool = descriptor_pool;
1531
1422
 
1532
- static VALUE msgdef_add_field(VALUE msgdef_rb,
1533
- const char* label, VALUE name,
1534
- VALUE type, VALUE number,
1535
- VALUE type_class,
1536
- VALUE options) {
1537
- VALUE fielddef_rb = rb_class_new_instance(0, NULL, cFieldDescriptor);
1538
- VALUE name_str = rb_str_new2(rb_id2name(SYM2ID(name)));
1539
-
1540
- rb_funcall(fielddef_rb, rb_intern("label="), 1, ID2SYM(rb_intern(label)));
1541
- rb_funcall(fielddef_rb, rb_intern("name="), 1, name_str);
1542
- rb_funcall(fielddef_rb, rb_intern("type="), 1, type);
1543
- rb_funcall(fielddef_rb, rb_intern("number="), 1, number);
1544
-
1545
- if (type_class != Qnil) {
1546
- Check_Type(type_class, T_STRING);
1423
+ google_protobuf_FileDescriptorProto_set_name(
1424
+ self->file_proto, FileBuilderContext_strdup(_self, name));
1547
1425
 
1548
- // Make it an absolute type name by prepending a dot.
1549
- type_class = rb_str_append(rb_str_new2("."), type_class);
1550
- rb_funcall(fielddef_rb, rb_intern("submsg_name="), 1, type_class);
1551
- }
1426
+ // Default syntax for Ruby is proto3.
1427
+ google_protobuf_FileDescriptorProto_set_syntax(
1428
+ self->file_proto,
1429
+ FileBuilderContext_strdup(_self, rb_str_new2("proto3")));
1552
1430
 
1553
1431
  if (options != Qnil) {
1554
- Check_Type(options, T_HASH);
1432
+ VALUE syntax;
1555
1433
 
1556
- if (rb_funcall(options, rb_intern("key?"), 1,
1557
- ID2SYM(rb_intern("default"))) == Qtrue) {
1558
- Descriptor* msgdef = ruby_to_Descriptor(msgdef_rb);
1559
- if (upb_msgdef_syntax((upb_msgdef*)msgdef->msgdef) == UPB_SYNTAX_PROTO3) {
1560
- rb_raise(rb_eArgError, "Cannot set :default when using proto3 syntax.");
1561
- }
1434
+ Check_Type(options, T_HASH);
1435
+ syntax = rb_hash_lookup2(options, ID2SYM(rb_intern("syntax")), Qnil);
1562
1436
 
1563
- FieldDescriptor* fielddef = ruby_to_FieldDescriptor(fielddef_rb);
1564
- if (!upb_fielddef_haspresence((upb_fielddef*)fielddef->fielddef) ||
1565
- upb_fielddef_issubmsg((upb_fielddef*)fielddef->fielddef)) {
1566
- rb_raise(rb_eArgError, "Cannot set :default on this kind of field.");
1567
- }
1437
+ if (syntax != Qnil) {
1438
+ VALUE syntax_str;
1568
1439
 
1569
- rb_funcall(fielddef_rb, rb_intern("default="), 1,
1570
- rb_hash_lookup(options, ID2SYM(rb_intern("default"))));
1440
+ Check_Type(syntax, T_SYMBOL);
1441
+ syntax_str = rb_id2str(SYM2ID(syntax));
1442
+ google_protobuf_FileDescriptorProto_set_syntax(
1443
+ self->file_proto, FileBuilderContext_strdup(_self, syntax_str));
1571
1444
  }
1572
1445
  }
1573
1446
 
1574
- rb_funcall(msgdef_rb, rb_intern("add_field"), 1, fielddef_rb);
1575
- return fielddef_rb;
1447
+ return Qnil;
1576
1448
  }
1577
1449
 
1450
+ static void MessageBuilderContext_add_synthetic_oneofs(VALUE _self);
1451
+
1578
1452
  /*
1579
1453
  * call-seq:
1580
- * MessageBuilderContext.optional(name, type, number, type_class = nil,
1581
- * options = nil)
1454
+ * FileBuilderContext.add_message(name, &block)
1582
1455
  *
1583
- * Defines a new optional field on this message type with the given type, tag
1584
- * number, and type class (for message and enum fields). The type must be a Ruby
1585
- * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
1456
+ * Creates a new, empty descriptor with the given name, and invokes the block in
1457
+ * the context of a MessageBuilderContext on that descriptor. The block can then
1458
+ * call, e.g., MessageBuilderContext#optional and MessageBuilderContext#repeated
1459
+ * methods to define the message fields.
1460
+ *
1461
+ * This is the recommended, idiomatic way to build message definitions.
1462
+ */
1463
+ static VALUE FileBuilderContext_add_message(VALUE _self, VALUE name) {
1464
+ VALUE args[2] = { _self, name };
1465
+ VALUE ctx = rb_class_new_instance(2, args, cMessageBuilderContext);
1466
+ VALUE block = rb_block_proc();
1467
+ rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
1468
+ MessageBuilderContext_add_synthetic_oneofs(ctx);
1469
+ return Qnil;
1470
+ }
1471
+
1472
+ /* We have to do some relatively complicated logic here for backward
1473
+ * compatibility.
1474
+ *
1475
+ * In descriptor.proto, messages are nested inside other messages if that is
1476
+ * what the original .proto file looks like. For example, suppose we have this
1477
+ * foo.proto:
1478
+ *
1479
+ * package foo;
1480
+ * message Bar {
1481
+ * message Baz {}
1482
+ * }
1483
+ *
1484
+ * The descriptor for this must look like this:
1485
+ *
1486
+ * file {
1487
+ * name: "test.proto"
1488
+ * package: "foo"
1489
+ * message_type {
1490
+ * name: "Bar"
1491
+ * nested_type {
1492
+ * name: "Baz"
1493
+ * }
1494
+ * }
1495
+ * }
1496
+ *
1497
+ * However, the Ruby generated code has always generated messages in a flat,
1498
+ * non-nested way:
1499
+ *
1500
+ * Google::Protobuf::DescriptorPool.generated_pool.build do
1501
+ * add_message "foo.Bar" do
1502
+ * end
1503
+ * add_message "foo.Bar.Baz" do
1504
+ * end
1505
+ * end
1506
+ *
1507
+ * Here we need to do a translation where we turn this generated code into the
1508
+ * above descriptor. We need to infer that "foo" is the package name, and not
1509
+ * a message itself.
1510
+ *
1511
+ * We delegate to Ruby to compute the transformation, for more concice and
1512
+ * readable code than we can do in C */
1513
+ static void rewrite_names(VALUE _file_builder,
1514
+ google_protobuf_FileDescriptorProto* file_proto) {
1515
+ FileBuilderContext* file_builder = ruby_to_FileBuilderContext(_file_builder);
1516
+ upb_arena *arena = file_builder->arena;
1517
+ // Build params (package, msg_names, enum_names).
1518
+ VALUE package = Qnil;
1519
+ VALUE msg_names = rb_ary_new();
1520
+ VALUE enum_names = rb_ary_new();
1521
+ size_t msg_count, enum_count, i;
1522
+ VALUE new_package, nesting, msg_ents, enum_ents;
1523
+ google_protobuf_DescriptorProto** msgs;
1524
+ google_protobuf_EnumDescriptorProto** enums;
1525
+
1526
+ if (google_protobuf_FileDescriptorProto_has_package(file_proto)) {
1527
+ upb_strview package_str =
1528
+ google_protobuf_FileDescriptorProto_package(file_proto);
1529
+ package = rb_str_new(package_str.data, package_str.size);
1530
+ }
1531
+
1532
+ msgs = google_protobuf_FileDescriptorProto_mutable_message_type(file_proto,
1533
+ &msg_count);
1534
+ for (i = 0; i < msg_count; i++) {
1535
+ upb_strview name = google_protobuf_DescriptorProto_name(msgs[i]);
1536
+ rb_ary_push(msg_names, rb_str_new(name.data, name.size));
1537
+ }
1538
+
1539
+ enums = google_protobuf_FileDescriptorProto_mutable_enum_type(file_proto,
1540
+ &enum_count);
1541
+ for (i = 0; i < enum_count; i++) {
1542
+ upb_strview name = google_protobuf_EnumDescriptorProto_name(enums[i]);
1543
+ rb_ary_push(enum_names, rb_str_new(name.data, name.size));
1544
+ }
1545
+
1546
+ {
1547
+ // Call Ruby code to calculate package name and nesting.
1548
+ VALUE args[3] = { package, msg_names, enum_names };
1549
+ VALUE internal = rb_eval_string("Google::Protobuf::Internal");
1550
+ VALUE ret = rb_funcallv(internal, rb_intern("fixup_descriptor"), 3, args);
1551
+
1552
+ new_package = rb_ary_entry(ret, 0);
1553
+ nesting = rb_ary_entry(ret, 1);
1554
+ }
1555
+
1556
+ // Rewrite package and names.
1557
+ if (new_package != Qnil) {
1558
+ upb_strview new_package_str =
1559
+ FileBuilderContext_strdup(_file_builder, new_package);
1560
+ google_protobuf_FileDescriptorProto_set_package(file_proto,
1561
+ new_package_str);
1562
+ }
1563
+
1564
+ for (i = 0; i < msg_count; i++) {
1565
+ upb_strview name = google_protobuf_DescriptorProto_name(msgs[i]);
1566
+ remove_path(&name);
1567
+ google_protobuf_DescriptorProto_set_name(msgs[i], name);
1568
+ }
1569
+
1570
+ for (i = 0; i < enum_count; i++) {
1571
+ upb_strview name = google_protobuf_EnumDescriptorProto_name(enums[i]);
1572
+ remove_path(&name);
1573
+ google_protobuf_EnumDescriptorProto_set_name(enums[i], name);
1574
+ }
1575
+
1576
+ // Rewrite nesting.
1577
+ msg_ents = rb_hash_aref(nesting, ID2SYM(rb_intern("msgs")));
1578
+ enum_ents = rb_hash_aref(nesting, ID2SYM(rb_intern("enums")));
1579
+
1580
+ Check_Type(msg_ents, T_ARRAY);
1581
+ Check_Type(enum_ents, T_ARRAY);
1582
+
1583
+ for (i = 0; i < (size_t)RARRAY_LEN(msg_ents); i++) {
1584
+ VALUE msg_ent = rb_ary_entry(msg_ents, i);
1585
+ VALUE pos = rb_hash_aref(msg_ent, ID2SYM(rb_intern("pos")));
1586
+ msgs[i] = msgs[NUM2INT(pos)];
1587
+ rewrite_nesting(msg_ent, msgs[i], msgs, enums, arena);
1588
+ }
1589
+
1590
+ for (i = 0; i < (size_t)RARRAY_LEN(enum_ents); i++) {
1591
+ VALUE enum_pos = rb_ary_entry(enum_ents, i);
1592
+ enums[i] = enums[NUM2INT(enum_pos)];
1593
+ }
1594
+
1595
+ google_protobuf_FileDescriptorProto_resize_message_type(
1596
+ file_proto, RARRAY_LEN(msg_ents), arena);
1597
+ google_protobuf_FileDescriptorProto_resize_enum_type(
1598
+ file_proto, RARRAY_LEN(enum_ents), arena);
1599
+ }
1600
+
1601
+ /*
1602
+ * call-seq:
1603
+ * FileBuilderContext.add_enum(name, &block)
1604
+ *
1605
+ * Creates a new, empty enum descriptor with the given name, and invokes the
1606
+ * block in the context of an EnumBuilderContext on that descriptor. The block
1607
+ * can then call EnumBuilderContext#add_value to define the enum values.
1608
+ *
1609
+ * This is the recommended, idiomatic way to build enum definitions.
1610
+ */
1611
+ static VALUE FileBuilderContext_add_enum(VALUE _self, VALUE name) {
1612
+ VALUE args[2] = { _self, name };
1613
+ VALUE ctx = rb_class_new_instance(2, args, cEnumBuilderContext);
1614
+ VALUE block = rb_block_proc();
1615
+ rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
1616
+ return Qnil;
1617
+ }
1618
+
1619
+ static void FileBuilderContext_build(VALUE _self) {
1620
+ FileBuilderContext* self = ruby_to_FileBuilderContext(_self);
1621
+ DescriptorPool* pool = ruby_to_DescriptorPool(self->descriptor_pool);
1622
+ upb_status status;
1623
+
1624
+ rewrite_enum_defaults(pool->symtab, self->file_proto);
1625
+ rewrite_names(_self, self->file_proto);
1626
+
1627
+ upb_status_clear(&status);
1628
+ if (!upb_symtab_addfile(pool->symtab, self->file_proto, &status)) {
1629
+ rb_raise(cTypeError, "Unable to add defs to DescriptorPool: %s",
1630
+ upb_status_errmsg(&status));
1631
+ }
1632
+ }
1633
+
1634
+ static void FileBuilderContext_register(VALUE module) {
1635
+ VALUE klass = rb_define_class_under(module, "FileBuilderContext", rb_cObject);
1636
+ rb_define_alloc_func(klass, FileBuilderContext_alloc);
1637
+ rb_define_method(klass, "initialize", FileBuilderContext_initialize, 3);
1638
+ rb_define_method(klass, "add_message", FileBuilderContext_add_message, 1);
1639
+ rb_define_method(klass, "add_enum", FileBuilderContext_add_enum, 1);
1640
+ rb_gc_register_address(&cFileBuilderContext);
1641
+ cFileBuilderContext = klass;
1642
+ }
1643
+
1644
+ // -----------------------------------------------------------------------------
1645
+ // MessageBuilderContext.
1646
+ // -----------------------------------------------------------------------------
1647
+
1648
+ typedef struct {
1649
+ google_protobuf_DescriptorProto* msg_proto;
1650
+ VALUE file_builder;
1651
+ } MessageBuilderContext;
1652
+
1653
+ static VALUE cMessageBuilderContext = Qnil;
1654
+
1655
+ static void MessageBuilderContext_mark(void* _self) {
1656
+ MessageBuilderContext* self = _self;
1657
+ rb_gc_mark(self->file_builder);
1658
+ }
1659
+
1660
+ static const rb_data_type_t MessageBuilderContext_type = {
1661
+ "Google::Protobuf::Internal::MessageBuilderContext",
1662
+ {MessageBuilderContext_mark, RUBY_DEFAULT_FREE, NULL},
1663
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
1664
+ };
1665
+
1666
+ static MessageBuilderContext* ruby_to_MessageBuilderContext(VALUE val) {
1667
+ MessageBuilderContext* ret;
1668
+ TypedData_Get_Struct(val, MessageBuilderContext, &MessageBuilderContext_type,
1669
+ ret);
1670
+ return ret;
1671
+ }
1672
+
1673
+ static VALUE MessageBuilderContext_alloc(VALUE klass) {
1674
+ MessageBuilderContext* self = ALLOC(MessageBuilderContext);
1675
+ VALUE ret = TypedData_Wrap_Struct(klass, &MessageBuilderContext_type, self);
1676
+ self->file_builder = Qnil;
1677
+ return ret;
1678
+ }
1679
+
1680
+ /*
1681
+ * call-seq:
1682
+ * MessageBuilderContext.new(file_builder, name) => context
1683
+ *
1684
+ * Create a new message builder context around the given message descriptor and
1685
+ * builder context. This class is intended to serve as a DSL context to be used
1686
+ * with #instance_eval.
1687
+ */
1688
+ static VALUE MessageBuilderContext_initialize(VALUE _self, VALUE _file_builder,
1689
+ VALUE name) {
1690
+ MessageBuilderContext* self = ruby_to_MessageBuilderContext(_self);
1691
+ FileBuilderContext* file_builder = ruby_to_FileBuilderContext(_file_builder);
1692
+ google_protobuf_FileDescriptorProto* file_proto = file_builder->file_proto;
1693
+
1694
+ self->file_builder = _file_builder;
1695
+ self->msg_proto = google_protobuf_FileDescriptorProto_add_message_type(
1696
+ file_proto, file_builder->arena);
1697
+
1698
+ google_protobuf_DescriptorProto_set_name(
1699
+ self->msg_proto, FileBuilderContext_strdup(_file_builder, name));
1700
+
1701
+ return Qnil;
1702
+ }
1703
+
1704
+ static void msgdef_add_field(VALUE msgbuilder_rb, upb_label_t label, VALUE name,
1705
+ VALUE type, VALUE number, VALUE type_class,
1706
+ VALUE options, int oneof_index,
1707
+ bool proto3_optional) {
1708
+ MessageBuilderContext* self = ruby_to_MessageBuilderContext(msgbuilder_rb);
1709
+ FileBuilderContext* file_context =
1710
+ ruby_to_FileBuilderContext(self->file_builder);
1711
+ google_protobuf_FieldDescriptorProto* field_proto;
1712
+ VALUE name_str;
1713
+
1714
+ field_proto = google_protobuf_DescriptorProto_add_field(self->msg_proto,
1715
+ file_context->arena);
1716
+
1717
+ Check_Type(name, T_SYMBOL);
1718
+ name_str = rb_id2str(SYM2ID(name));
1719
+
1720
+ google_protobuf_FieldDescriptorProto_set_name(
1721
+ field_proto, FileBuilderContext_strdup(self->file_builder, name_str));
1722
+ google_protobuf_FieldDescriptorProto_set_number(field_proto, NUM2INT(number));
1723
+ google_protobuf_FieldDescriptorProto_set_label(field_proto, (int)label);
1724
+ google_protobuf_FieldDescriptorProto_set_type(
1725
+ field_proto, (int)ruby_to_descriptortype(type));
1726
+
1727
+ if (proto3_optional) {
1728
+ google_protobuf_FieldDescriptorProto_set_proto3_optional(field_proto, true);
1729
+ }
1730
+
1731
+ if (type_class != Qnil) {
1732
+ Check_Type(type_class, T_STRING);
1733
+
1734
+ // Make it an absolute type name by prepending a dot.
1735
+ type_class = rb_str_append(rb_str_new2("."), type_class);
1736
+ google_protobuf_FieldDescriptorProto_set_type_name(
1737
+ field_proto, FileBuilderContext_strdup(self->file_builder, type_class));
1738
+ }
1739
+
1740
+ if (options != Qnil) {
1741
+ Check_Type(options, T_HASH);
1742
+
1743
+ if (rb_funcall(options, rb_intern("key?"), 1,
1744
+ ID2SYM(rb_intern("default"))) == Qtrue) {
1745
+ VALUE default_value =
1746
+ rb_hash_lookup(options, ID2SYM(rb_intern("default")));
1747
+
1748
+ /* Call #to_s since all defaults are strings in the descriptor. */
1749
+ default_value = rb_funcall(default_value, rb_intern("to_s"), 0);
1750
+
1751
+ google_protobuf_FieldDescriptorProto_set_default_value(
1752
+ field_proto,
1753
+ FileBuilderContext_strdup(self->file_builder, default_value));
1754
+ }
1755
+ }
1756
+
1757
+ if (oneof_index >= 0) {
1758
+ google_protobuf_FieldDescriptorProto_set_oneof_index(field_proto,
1759
+ oneof_index);
1760
+ }
1761
+ }
1762
+
1763
+ #if RUBY_API_VERSION_CODE >= 20700
1764
+ static VALUE make_mapentry(VALUE _message_builder, VALUE types, int argc,
1765
+ const VALUE* argv, VALUE blockarg) {
1766
+ (void)blockarg;
1767
+ #else
1768
+ static VALUE make_mapentry(VALUE _message_builder, VALUE types, int argc,
1769
+ VALUE* argv) {
1770
+ #endif
1771
+ MessageBuilderContext* message_builder =
1772
+ ruby_to_MessageBuilderContext(_message_builder);
1773
+ VALUE type_class = rb_ary_entry(types, 2);
1774
+ FileBuilderContext* file_context =
1775
+ ruby_to_FileBuilderContext(message_builder->file_builder);
1776
+ google_protobuf_MessageOptions* options =
1777
+ google_protobuf_DescriptorProto_mutable_options(
1778
+ message_builder->msg_proto, file_context->arena);
1779
+
1780
+ google_protobuf_MessageOptions_set_map_entry(options, true);
1781
+
1782
+ // optional <type> key = 1;
1783
+ rb_funcall(_message_builder, rb_intern("optional"), 3,
1784
+ ID2SYM(rb_intern("key")), rb_ary_entry(types, 0), INT2NUM(1));
1785
+
1786
+ // optional <type> value = 2;
1787
+ if (type_class == Qnil) {
1788
+ rb_funcall(_message_builder, rb_intern("optional"), 3,
1789
+ ID2SYM(rb_intern("value")), rb_ary_entry(types, 1), INT2NUM(2));
1790
+ } else {
1791
+ rb_funcall(_message_builder, rb_intern("optional"), 4,
1792
+ ID2SYM(rb_intern("value")), rb_ary_entry(types, 1), INT2NUM(2),
1793
+ type_class);
1794
+ }
1795
+
1796
+ return Qnil;
1797
+ }
1798
+
1799
+ /*
1800
+ * call-seq:
1801
+ * MessageBuilderContext.optional(name, type, number, type_class = nil,
1802
+ * options = nil)
1803
+ *
1804
+ * Defines a new optional field on this message type with the given type, tag
1805
+ * number, and type class (for message and enum fields). The type must be a Ruby
1806
+ * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
1586
1807
  * string, if present (as accepted by FieldDescriptor#submsg_name=).
1587
1808
  */
1588
1809
  VALUE MessageBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
1589
- DEFINE_SELF(MessageBuilderContext, self, _self);
1590
1810
  VALUE name, type, number;
1591
1811
  VALUE type_class, options = Qnil;
1592
1812
 
@@ -1599,8 +1819,41 @@ VALUE MessageBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
1599
1819
  type_class = Qnil;
1600
1820
  }
1601
1821
 
1602
- return msgdef_add_field(self->descriptor, "optional",
1603
- name, type, number, type_class, options);
1822
+ msgdef_add_field(_self, UPB_LABEL_OPTIONAL, name, type, number, type_class,
1823
+ options, -1, false);
1824
+
1825
+ return Qnil;
1826
+ }
1827
+
1828
+ /*
1829
+ * call-seq:
1830
+ * MessageBuilderContext.proto3_optional(name, type, number,
1831
+ * type_class = nil, options = nil)
1832
+ *
1833
+ * Defines a true proto3 optional field (that tracks presence) on this message
1834
+ * type with the given type, tag number, and type class (for message and enum
1835
+ * fields). The type must be a Ruby symbol (as accepted by
1836
+ * FieldDescriptor#type=) and the type_class must be a string, if present (as
1837
+ * accepted by FieldDescriptor#submsg_name=).
1838
+ */
1839
+ static VALUE MessageBuilderContext_proto3_optional(int argc, VALUE* argv,
1840
+ VALUE _self) {
1841
+ VALUE name, type, number;
1842
+ VALUE type_class, options = Qnil;
1843
+
1844
+ rb_scan_args(argc, argv, "32", &name, &type, &number, &type_class, &options);
1845
+
1846
+ // Allow passing (name, type, number, options) or
1847
+ // (name, type, number, type_class, options)
1848
+ if (argc == 4 && RB_TYPE_P(type_class, T_HASH)) {
1849
+ options = type_class;
1850
+ type_class = Qnil;
1851
+ }
1852
+
1853
+ msgdef_add_field(_self, UPB_LABEL_OPTIONAL, name, type, number, type_class,
1854
+ options, -1, true);
1855
+
1856
+ return Qnil;
1604
1857
  }
1605
1858
 
1606
1859
  /*
@@ -1617,8 +1870,8 @@ VALUE MessageBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
1617
1870
  * completeness. Any attempt to add a message type with required fields to a
1618
1871
  * pool will currently result in an error.
1619
1872
  */
1620
- VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self) {
1621
- DEFINE_SELF(MessageBuilderContext, self, _self);
1873
+ static VALUE MessageBuilderContext_required(int argc, VALUE* argv,
1874
+ VALUE _self) {
1622
1875
  VALUE name, type, number;
1623
1876
  VALUE type_class, options = Qnil;
1624
1877
 
@@ -1631,8 +1884,10 @@ VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self) {
1631
1884
  type_class = Qnil;
1632
1885
  }
1633
1886
 
1634
- return msgdef_add_field(self->descriptor, "required",
1635
- name, type, number, type_class, options);
1887
+ msgdef_add_field(_self, UPB_LABEL_REQUIRED, name, type, number, type_class,
1888
+ options, -1, false);
1889
+
1890
+ return Qnil;
1636
1891
  }
1637
1892
 
1638
1893
  /*
@@ -1644,8 +1899,8 @@ VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self) {
1644
1899
  * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
1645
1900
  * string, if present (as accepted by FieldDescriptor#submsg_name=).
1646
1901
  */
1647
- VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self) {
1648
- DEFINE_SELF(MessageBuilderContext, self, _self);
1902
+ static VALUE MessageBuilderContext_repeated(int argc, VALUE* argv,
1903
+ VALUE _self) {
1649
1904
  VALUE name, type, number, type_class;
1650
1905
 
1651
1906
  if (argc < 3) {
@@ -1656,8 +1911,10 @@ VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self) {
1656
1911
  number = argv[2];
1657
1912
  type_class = (argc > 3) ? argv[3] : Qnil;
1658
1913
 
1659
- return msgdef_add_field(self->descriptor, "repeated",
1660
- name, type, number, type_class, Qnil);
1914
+ msgdef_add_field(_self, UPB_LABEL_REPEATED, name, type, number, type_class,
1915
+ Qnil, -1, false);
1916
+
1917
+ return Qnil;
1661
1918
  }
1662
1919
 
1663
1920
  /*
@@ -1672,10 +1929,12 @@ VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self) {
1672
1929
  * type_class must be a string, if present (as accepted by
1673
1930
  * FieldDescriptor#submsg_name=).
1674
1931
  */
1675
- VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self) {
1676
- DEFINE_SELF(MessageBuilderContext, self, _self);
1932
+ static VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self) {
1933
+ MessageBuilderContext* self = ruby_to_MessageBuilderContext(_self);
1677
1934
  VALUE name, key_type, value_type, number, type_class;
1678
- VALUE mapentry_desc, mapentry_desc_name;
1935
+ VALUE mapentry_desc_name;
1936
+ FileBuilderContext* file_builder;
1937
+ upb_strview msg_name;
1679
1938
 
1680
1939
  if (argc < 4) {
1681
1940
  rb_raise(rb_eArgError, "Expected at least 4 arguments.");
@@ -1698,77 +1957,36 @@ VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self) {
1698
1957
  "type.");
1699
1958
  }
1700
1959
 
1701
- Descriptor* descriptor = ruby_to_Descriptor(self->descriptor);
1702
- if (upb_msgdef_syntax(descriptor->msgdef) == UPB_SYNTAX_PROTO2) {
1703
- rb_raise(rb_eArgError,
1704
- "Cannot add a native map field using proto2 syntax.");
1705
- }
1960
+ file_builder = ruby_to_FileBuilderContext(self->file_builder);
1706
1961
 
1707
1962
  // Create a new message descriptor for the map entry message, and create a
1708
1963
  // repeated submessage field here with that type.
1709
- VALUE file_descriptor_rb =
1710
- rb_funcall(self->descriptor, rb_intern("file_descriptor"), 0);
1711
- mapentry_desc = rb_class_new_instance(1, &file_descriptor_rb, cDescriptor);
1712
- mapentry_desc_name = rb_funcall(self->descriptor, rb_intern("name"), 0);
1964
+ msg_name = google_protobuf_DescriptorProto_name(self->msg_proto);
1965
+ mapentry_desc_name = rb_str_new(msg_name.data, msg_name.size);
1713
1966
  mapentry_desc_name = rb_str_cat2(mapentry_desc_name, "_MapEntry_");
1714
- mapentry_desc_name = rb_str_cat2(mapentry_desc_name,
1715
- rb_id2name(SYM2ID(name)));
1716
- Descriptor_name_set(mapentry_desc, mapentry_desc_name);
1717
-
1718
- {
1719
- // The 'mapentry' attribute has no Ruby setter because we do not want the
1720
- // user attempting to DIY the setup below; we want to ensure that the fields
1721
- // are correct. So we reach into the msgdef here to set the bit manually.
1722
- Descriptor* mapentry_desc_self = ruby_to_Descriptor(mapentry_desc);
1723
- upb_msgdef_setmapentry((upb_msgdef*)mapentry_desc_self->msgdef, true);
1724
- }
1725
-
1726
- {
1727
- // optional <type> key = 1;
1728
- VALUE key_field = rb_class_new_instance(0, NULL, cFieldDescriptor);
1729
- FieldDescriptor_name_set(key_field, rb_str_new2("key"));
1730
- FieldDescriptor_label_set(key_field, ID2SYM(rb_intern("optional")));
1731
- FieldDescriptor_number_set(key_field, INT2NUM(1));
1732
- FieldDescriptor_type_set(key_field, key_type);
1733
- Descriptor_add_field(mapentry_desc, key_field);
1734
- }
1967
+ mapentry_desc_name =
1968
+ rb_str_cat2(mapentry_desc_name, rb_id2name(SYM2ID(name)));
1735
1969
 
1736
1970
  {
1737
- // optional <type> value = 2;
1738
- VALUE value_field = rb_class_new_instance(0, NULL, cFieldDescriptor);
1739
- FieldDescriptor_name_set(value_field, rb_str_new2("value"));
1740
- FieldDescriptor_label_set(value_field, ID2SYM(rb_intern("optional")));
1741
- FieldDescriptor_number_set(value_field, INT2NUM(2));
1742
- FieldDescriptor_type_set(value_field, value_type);
1743
- if (type_class != Qnil) {
1744
- VALUE submsg_name = rb_str_new2("."); // prepend '.' to make absolute.
1745
- submsg_name = rb_str_append(submsg_name, type_class);
1746
- FieldDescriptor_submsg_name_set(value_field, submsg_name);
1747
- }
1748
- Descriptor_add_field(mapentry_desc, value_field);
1971
+ // message <msgname>_MapEntry_ { /* ... */ }
1972
+ VALUE args[1] = {mapentry_desc_name};
1973
+ VALUE types = rb_ary_new3(3, key_type, value_type, type_class);
1974
+ rb_block_call(self->file_builder, rb_intern("add_message"), 1, args,
1975
+ make_mapentry, types);
1749
1976
  }
1750
1977
 
1751
- {
1752
- // Add the map-entry message type to the current builder, and use the type
1753
- // to create the map field itself.
1754
- Builder* builder = ruby_to_Builder(self->builder);
1755
- rb_ary_push(builder->pending_list, mapentry_desc);
1978
+ // If this file is in a package, we need to qualify the map entry type.
1979
+ if (google_protobuf_FileDescriptorProto_has_package(file_builder->file_proto)) {
1980
+ upb_strview package_view =
1981
+ google_protobuf_FileDescriptorProto_package(file_builder->file_proto);
1982
+ VALUE package = rb_str_new(package_view.data, package_view.size);
1983
+ package = rb_str_cat2(package, ".");
1984
+ mapentry_desc_name = rb_str_concat(package, mapentry_desc_name);
1756
1985
  }
1757
1986
 
1758
- {
1759
- VALUE map_field = rb_class_new_instance(0, NULL, cFieldDescriptor);
1760
- VALUE name_str = rb_str_new2(rb_id2name(SYM2ID(name)));
1761
- VALUE submsg_name;
1762
-
1763
- FieldDescriptor_name_set(map_field, name_str);
1764
- FieldDescriptor_number_set(map_field, number);
1765
- FieldDescriptor_label_set(map_field, ID2SYM(rb_intern("repeated")));
1766
- FieldDescriptor_type_set(map_field, ID2SYM(rb_intern("message")));
1767
- submsg_name = rb_str_new2("."); // prepend '.' to make name absolute.
1768
- submsg_name = rb_str_append(submsg_name, mapentry_desc_name);
1769
- FieldDescriptor_submsg_name_set(map_field, submsg_name);
1770
- Descriptor_add_field(self->descriptor, map_field);
1771
- }
1987
+ // repeated MapEntry <name> = <number>;
1988
+ rb_funcall(_self, rb_intern("repeated"), 4, name,
1989
+ ID2SYM(rb_intern("message")), number, mapentry_desc_name);
1772
1990
 
1773
1991
  return Qnil;
1774
1992
  }
@@ -1784,72 +2002,149 @@ VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self) {
1784
2002
  *
1785
2003
  * This is the recommended, idiomatic way to build oneof definitions.
1786
2004
  */
1787
- VALUE MessageBuilderContext_oneof(VALUE _self, VALUE name) {
1788
- DEFINE_SELF(MessageBuilderContext, self, _self);
1789
- VALUE oneofdef = rb_class_new_instance(0, NULL, cOneofDescriptor);
1790
- VALUE args[2] = { oneofdef, self->builder };
1791
- VALUE ctx = rb_class_new_instance(2, args, cOneofBuilderContext);
1792
- VALUE block = rb_block_proc();
1793
- VALUE name_str = rb_str_new2(rb_id2name(SYM2ID(name)));
1794
- rb_funcall(oneofdef, rb_intern("name="), 1, name_str);
1795
- rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
1796
- Descriptor_add_oneof(self->descriptor, oneofdef);
2005
+ static VALUE MessageBuilderContext_oneof(VALUE _self, VALUE name) {
2006
+ MessageBuilderContext* self = ruby_to_MessageBuilderContext(_self);
2007
+ size_t oneof_count;
2008
+ FileBuilderContext* file_context =
2009
+ ruby_to_FileBuilderContext(self->file_builder);
2010
+ google_protobuf_OneofDescriptorProto* oneof_proto;
2011
+
2012
+ // Existing oneof_count becomes oneof_index.
2013
+ google_protobuf_DescriptorProto_oneof_decl(self->msg_proto, &oneof_count);
2014
+
2015
+ // Create oneof_proto and set its name.
2016
+ oneof_proto = google_protobuf_DescriptorProto_add_oneof_decl(
2017
+ self->msg_proto, file_context->arena);
2018
+ google_protobuf_OneofDescriptorProto_set_name(
2019
+ oneof_proto, FileBuilderContext_strdup_sym(self->file_builder, name));
2020
+
2021
+ // Evaluate the block with the builder as argument.
2022
+ {
2023
+ VALUE args[2] = { INT2NUM(oneof_count), _self };
2024
+ VALUE ctx = rb_class_new_instance(2, args, cOneofBuilderContext);
2025
+ VALUE block = rb_block_proc();
2026
+ rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
2027
+ }
1797
2028
 
1798
2029
  return Qnil;
1799
2030
  }
1800
2031
 
2032
+ static void MessageBuilderContext_add_synthetic_oneofs(VALUE _self) {
2033
+ MessageBuilderContext* self = ruby_to_MessageBuilderContext(_self);
2034
+ FileBuilderContext* file_context =
2035
+ ruby_to_FileBuilderContext(self->file_builder);
2036
+ size_t field_count, oneof_count;
2037
+ google_protobuf_FieldDescriptorProto** fields =
2038
+ google_protobuf_DescriptorProto_mutable_field(self->msg_proto, &field_count);
2039
+ const google_protobuf_OneofDescriptorProto*const* oneofs =
2040
+ google_protobuf_DescriptorProto_oneof_decl(self->msg_proto, &oneof_count);
2041
+ VALUE names = rb_hash_new();
2042
+ VALUE underscore = rb_str_new2("_");
2043
+ size_t i;
2044
+
2045
+ // We have to build a set of all names, to ensure that synthetic oneofs are
2046
+ // not creating conflicts.
2047
+ for (i = 0; i < field_count; i++) {
2048
+ upb_strview name = google_protobuf_FieldDescriptorProto_name(fields[i]);
2049
+ rb_hash_aset(names, rb_str_new(name.data, name.size), Qtrue);
2050
+ }
2051
+ for (i = 0; i < oneof_count; i++) {
2052
+ upb_strview name = google_protobuf_OneofDescriptorProto_name(oneofs[i]);
2053
+ rb_hash_aset(names, rb_str_new(name.data, name.size), Qtrue);
2054
+ }
2055
+
2056
+ for (i = 0; i < field_count; i++) {
2057
+ google_protobuf_OneofDescriptorProto* oneof_proto;
2058
+ VALUE oneof_name;
2059
+ upb_strview field_name;
2060
+
2061
+ if (!google_protobuf_FieldDescriptorProto_proto3_optional(fields[i])) {
2062
+ continue;
2063
+ }
2064
+
2065
+ // Prepend '_' until we are no longer conflicting.
2066
+ field_name = google_protobuf_FieldDescriptorProto_name(fields[i]);
2067
+ oneof_name = rb_str_new(field_name.data, field_name.size);
2068
+ while (rb_hash_lookup(names, oneof_name) != Qnil) {
2069
+ oneof_name = rb_str_plus(underscore, oneof_name);
2070
+ }
2071
+
2072
+ rb_hash_aset(names, oneof_name, Qtrue);
2073
+ google_protobuf_FieldDescriptorProto_set_oneof_index(fields[i],
2074
+ oneof_count++);
2075
+ oneof_proto = google_protobuf_DescriptorProto_add_oneof_decl(
2076
+ self->msg_proto, file_context->arena);
2077
+ google_protobuf_OneofDescriptorProto_set_name(
2078
+ oneof_proto, FileBuilderContext_strdup(self->file_builder, oneof_name));
2079
+ }
2080
+ }
2081
+
2082
+ static void MessageBuilderContext_register(VALUE module) {
2083
+ VALUE klass = rb_define_class_under(
2084
+ module, "MessageBuilderContext", rb_cObject);
2085
+ rb_define_alloc_func(klass, MessageBuilderContext_alloc);
2086
+ rb_define_method(klass, "initialize",
2087
+ MessageBuilderContext_initialize, 2);
2088
+ rb_define_method(klass, "optional", MessageBuilderContext_optional, -1);
2089
+ rb_define_method(klass, "proto3_optional", MessageBuilderContext_proto3_optional, -1);
2090
+ rb_define_method(klass, "required", MessageBuilderContext_required, -1);
2091
+ rb_define_method(klass, "repeated", MessageBuilderContext_repeated, -1);
2092
+ rb_define_method(klass, "map", MessageBuilderContext_map, -1);
2093
+ rb_define_method(klass, "oneof", MessageBuilderContext_oneof, 1);
2094
+ rb_gc_register_address(&cMessageBuilderContext);
2095
+ cMessageBuilderContext = klass;
2096
+ }
2097
+
1801
2098
  // -----------------------------------------------------------------------------
1802
2099
  // OneofBuilderContext.
1803
2100
  // -----------------------------------------------------------------------------
1804
2101
 
1805
- DEFINE_CLASS(OneofBuilderContext,
1806
- "Google::Protobuf::Internal::OneofBuilderContext");
2102
+ typedef struct {
2103
+ int oneof_index;
2104
+ VALUE message_builder;
2105
+ } OneofBuilderContext;
2106
+
2107
+ static VALUE cOneofBuilderContext = Qnil;
1807
2108
 
1808
2109
  void OneofBuilderContext_mark(void* _self) {
1809
2110
  OneofBuilderContext* self = _self;
1810
- rb_gc_mark(self->descriptor);
1811
- rb_gc_mark(self->builder);
2111
+ rb_gc_mark(self->message_builder);
1812
2112
  }
1813
2113
 
1814
- void OneofBuilderContext_free(void* _self) {
1815
- OneofBuilderContext* self = _self;
1816
- xfree(self);
1817
- }
2114
+ static const rb_data_type_t OneofBuilderContext_type = {
2115
+ "Google::Protobuf::Internal::OneofBuilderContext",
2116
+ {OneofBuilderContext_mark, RUBY_DEFAULT_FREE, NULL},
2117
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
2118
+ };
1818
2119
 
1819
- VALUE OneofBuilderContext_alloc(VALUE klass) {
1820
- OneofBuilderContext* self = ALLOC(OneofBuilderContext);
1821
- VALUE ret = TypedData_Wrap_Struct(
1822
- klass, &_OneofBuilderContext_type, self);
1823
- self->descriptor = Qnil;
1824
- self->builder = Qnil;
2120
+ static OneofBuilderContext* ruby_to_OneofBuilderContext(VALUE val) {
2121
+ OneofBuilderContext* ret;
2122
+ TypedData_Get_Struct(val, OneofBuilderContext, &OneofBuilderContext_type,
2123
+ ret);
1825
2124
  return ret;
1826
2125
  }
1827
2126
 
1828
- void OneofBuilderContext_register(VALUE module) {
1829
- VALUE klass = rb_define_class_under(
1830
- module, "OneofBuilderContext", rb_cObject);
1831
- rb_define_alloc_func(klass, OneofBuilderContext_alloc);
1832
- rb_define_method(klass, "initialize",
1833
- OneofBuilderContext_initialize, 2);
1834
- rb_define_method(klass, "optional", OneofBuilderContext_optional, -1);
1835
- rb_gc_register_address(&cOneofBuilderContext);
1836
- cOneofBuilderContext = klass;
2127
+ static VALUE OneofBuilderContext_alloc(VALUE klass) {
2128
+ OneofBuilderContext* self = ALLOC(OneofBuilderContext);
2129
+ VALUE ret = TypedData_Wrap_Struct(klass, &OneofBuilderContext_type, self);
2130
+ self->oneof_index = 0;
2131
+ self->message_builder = Qnil;
2132
+ return ret;
1837
2133
  }
1838
2134
 
1839
2135
  /*
1840
2136
  * call-seq:
1841
- * OneofBuilderContext.new(desc, builder) => context
2137
+ * OneofBuilderContext.new(oneof_index, message_builder) => context
1842
2138
  *
1843
2139
  * Create a new oneof builder context around the given oneof descriptor and
1844
2140
  * builder context. This class is intended to serve as a DSL context to be used
1845
2141
  * with #instance_eval.
1846
2142
  */
1847
- VALUE OneofBuilderContext_initialize(VALUE _self,
1848
- VALUE oneofdef,
1849
- VALUE builder) {
1850
- DEFINE_SELF(OneofBuilderContext, self, _self);
1851
- self->descriptor = oneofdef;
1852
- self->builder = builder;
2143
+ static VALUE OneofBuilderContext_initialize(VALUE _self, VALUE oneof_index,
2144
+ VALUE message_builder) {
2145
+ OneofBuilderContext* self = ruby_to_OneofBuilderContext(_self);
2146
+ self->oneof_index = NUM2INT(oneof_index);
2147
+ self->message_builder = message_builder;
1853
2148
  return Qnil;
1854
2149
  }
1855
2150
 
@@ -1863,69 +2158,86 @@ VALUE OneofBuilderContext_initialize(VALUE _self,
1863
2158
  * (as accepted by FieldDescriptor#type=) and the type_class must be a string,
1864
2159
  * if present (as accepted by FieldDescriptor#submsg_name=).
1865
2160
  */
1866
- VALUE OneofBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
1867
- DEFINE_SELF(OneofBuilderContext, self, _self);
2161
+ static VALUE OneofBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
2162
+ OneofBuilderContext* self = ruby_to_OneofBuilderContext(_self);
1868
2163
  VALUE name, type, number;
1869
2164
  VALUE type_class, options = Qnil;
1870
2165
 
1871
2166
  rb_scan_args(argc, argv, "32", &name, &type, &number, &type_class, &options);
1872
2167
 
1873
- return msgdef_add_field(self->descriptor, "optional",
1874
- name, type, number, type_class, options);
2168
+ msgdef_add_field(self->message_builder, UPB_LABEL_OPTIONAL, name, type,
2169
+ number, type_class, options, self->oneof_index, false);
2170
+
2171
+ return Qnil;
2172
+ }
2173
+
2174
+ static void OneofBuilderContext_register(VALUE module) {
2175
+ VALUE klass = rb_define_class_under(
2176
+ module, "OneofBuilderContext", rb_cObject);
2177
+ rb_define_alloc_func(klass, OneofBuilderContext_alloc);
2178
+ rb_define_method(klass, "initialize",
2179
+ OneofBuilderContext_initialize, 2);
2180
+ rb_define_method(klass, "optional", OneofBuilderContext_optional, -1);
2181
+ rb_gc_register_address(&cOneofBuilderContext);
2182
+ cOneofBuilderContext = klass;
1875
2183
  }
1876
2184
 
1877
2185
  // -----------------------------------------------------------------------------
1878
2186
  // EnumBuilderContext.
1879
2187
  // -----------------------------------------------------------------------------
1880
2188
 
1881
- DEFINE_CLASS(EnumBuilderContext,
1882
- "Google::Protobuf::Internal::EnumBuilderContext");
2189
+ typedef struct {
2190
+ google_protobuf_EnumDescriptorProto* enum_proto;
2191
+ VALUE file_builder;
2192
+ } EnumBuilderContext;
2193
+
2194
+ static VALUE cEnumBuilderContext = Qnil;
1883
2195
 
1884
2196
  void EnumBuilderContext_mark(void* _self) {
1885
2197
  EnumBuilderContext* self = _self;
1886
- rb_gc_mark(self->enumdesc);
2198
+ rb_gc_mark(self->file_builder);
1887
2199
  }
1888
2200
 
1889
- void EnumBuilderContext_free(void* _self) {
1890
- EnumBuilderContext* self = _self;
1891
- xfree(self);
1892
- }
2201
+ static const rb_data_type_t EnumBuilderContext_type = {
2202
+ "Google::Protobuf::Internal::EnumBuilderContext",
2203
+ {EnumBuilderContext_mark, RUBY_DEFAULT_FREE, NULL},
2204
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
2205
+ };
1893
2206
 
1894
- VALUE EnumBuilderContext_alloc(VALUE klass) {
1895
- EnumBuilderContext* self = ALLOC(EnumBuilderContext);
1896
- VALUE ret = TypedData_Wrap_Struct(
1897
- klass, &_EnumBuilderContext_type, self);
1898
- self->enumdesc = Qnil;
2207
+ static EnumBuilderContext* ruby_to_EnumBuilderContext(VALUE val) {
2208
+ EnumBuilderContext* ret;
2209
+ TypedData_Get_Struct(val, EnumBuilderContext, &EnumBuilderContext_type, ret);
1899
2210
  return ret;
1900
2211
  }
1901
2212
 
1902
- void EnumBuilderContext_register(VALUE module) {
1903
- VALUE klass = rb_define_class_under(
1904
- module, "EnumBuilderContext", rb_cObject);
1905
- rb_define_alloc_func(klass, EnumBuilderContext_alloc);
1906
- rb_define_method(klass, "initialize",
1907
- EnumBuilderContext_initialize, 1);
1908
- rb_define_method(klass, "value", EnumBuilderContext_value, 2);
1909
- rb_gc_register_address(&cEnumBuilderContext);
1910
- cEnumBuilderContext = klass;
2213
+ static VALUE EnumBuilderContext_alloc(VALUE klass) {
2214
+ EnumBuilderContext* self = ALLOC(EnumBuilderContext);
2215
+ VALUE ret = TypedData_Wrap_Struct(klass, &EnumBuilderContext_type, self);
2216
+ self->enum_proto = NULL;
2217
+ self->file_builder = Qnil;
2218
+ return ret;
1911
2219
  }
1912
2220
 
1913
2221
  /*
1914
2222
  * call-seq:
1915
- * EnumBuilderContext.new(enumdesc) => context
2223
+ * EnumBuilderContext.new(file_builder) => context
1916
2224
  *
1917
2225
  * Create a new builder context around the given enum descriptor. This class is
1918
2226
  * intended to serve as a DSL context to be used with #instance_eval.
1919
2227
  */
1920
- VALUE EnumBuilderContext_initialize(VALUE _self, VALUE enumdef) {
1921
- DEFINE_SELF(EnumBuilderContext, self, _self);
1922
- self->enumdesc = enumdef;
1923
- return Qnil;
1924
- }
2228
+ static VALUE EnumBuilderContext_initialize(VALUE _self, VALUE _file_builder,
2229
+ VALUE name) {
2230
+ EnumBuilderContext* self = ruby_to_EnumBuilderContext(_self);
2231
+ FileBuilderContext* file_builder = ruby_to_FileBuilderContext(_file_builder);
2232
+ google_protobuf_FileDescriptorProto* file_proto = file_builder->file_proto;
2233
+
2234
+ self->file_builder = _file_builder;
2235
+ self->enum_proto = google_protobuf_FileDescriptorProto_add_enum_type(
2236
+ file_proto, file_builder->arena);
2237
+
2238
+ google_protobuf_EnumDescriptorProto_set_name(
2239
+ self->enum_proto, FileBuilderContext_strdup(_file_builder, name));
1925
2240
 
1926
- static VALUE enumdef_add_value(VALUE enumdef,
1927
- VALUE name, VALUE number) {
1928
- rb_funcall(enumdef, rb_intern("add_value"), 2, name, number);
1929
2241
  return Qnil;
1930
2242
  }
1931
2243
 
@@ -1936,177 +2248,82 @@ static VALUE enumdef_add_value(VALUE enumdef,
1936
2248
  * Adds the given name => number mapping to the enum type. Name must be a Ruby
1937
2249
  * symbol.
1938
2250
  */
1939
- VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number) {
1940
- DEFINE_SELF(EnumBuilderContext, self, _self);
1941
- return enumdef_add_value(self->enumdesc, name, number);
1942
- }
1943
-
1944
-
1945
- // -----------------------------------------------------------------------------
1946
- // FileBuilderContext.
1947
- // -----------------------------------------------------------------------------
2251
+ static VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number) {
2252
+ EnumBuilderContext* self = ruby_to_EnumBuilderContext(_self);
2253
+ FileBuilderContext* file_builder =
2254
+ ruby_to_FileBuilderContext(self->file_builder);
2255
+ google_protobuf_EnumValueDescriptorProto* enum_value;
1948
2256
 
1949
- DEFINE_CLASS(FileBuilderContext,
1950
- "Google::Protobuf::Internal::FileBuilderContext");
2257
+ enum_value = google_protobuf_EnumDescriptorProto_add_value(
2258
+ self->enum_proto, file_builder->arena);
1951
2259
 
1952
- void FileBuilderContext_mark(void* _self) {
1953
- FileBuilderContext* self = _self;
1954
- rb_gc_mark(self->pending_list);
1955
- rb_gc_mark(self->file_descriptor);
1956
- rb_gc_mark(self->builder);
1957
- }
1958
-
1959
- void FileBuilderContext_free(void* _self) {
1960
- FileBuilderContext* self = _self;
1961
- xfree(self);
1962
- }
2260
+ google_protobuf_EnumValueDescriptorProto_set_name(
2261
+ enum_value, FileBuilderContext_strdup_sym(self->file_builder, name));
2262
+ google_protobuf_EnumValueDescriptorProto_set_number(enum_value,
2263
+ NUM2INT(number));
1963
2264
 
1964
- VALUE FileBuilderContext_alloc(VALUE klass) {
1965
- FileBuilderContext* self = ALLOC(FileBuilderContext);
1966
- VALUE ret = TypedData_Wrap_Struct(klass, &_FileBuilderContext_type, self);
1967
- self->pending_list = Qnil;
1968
- self->file_descriptor = Qnil;
1969
- self->builder = Qnil;
1970
- return ret;
1971
- }
1972
-
1973
- void FileBuilderContext_register(VALUE module) {
1974
- VALUE klass = rb_define_class_under(module, "FileBuilderContext", rb_cObject);
1975
- rb_define_alloc_func(klass, FileBuilderContext_alloc);
1976
- rb_define_method(klass, "initialize", FileBuilderContext_initialize, 2);
1977
- rb_define_method(klass, "add_message", FileBuilderContext_add_message, 1);
1978
- rb_define_method(klass, "add_enum", FileBuilderContext_add_enum, 1);
1979
- rb_gc_register_address(&cFileBuilderContext);
1980
- cFileBuilderContext = klass;
1981
- }
1982
-
1983
- /*
1984
- * call-seq:
1985
- * FileBuilderContext.new(file_descriptor, builder) => context
1986
- *
1987
- * Create a new file builder context for the given file descriptor and
1988
- * builder context. This class is intended to serve as a DSL context to be used
1989
- * with #instance_eval.
1990
- */
1991
- VALUE FileBuilderContext_initialize(VALUE _self, VALUE file_descriptor,
1992
- VALUE builder) {
1993
- DEFINE_SELF(FileBuilderContext, self, _self);
1994
- self->pending_list = rb_ary_new();
1995
- self->file_descriptor = file_descriptor;
1996
- self->builder = builder;
1997
- return Qnil;
1998
- }
1999
-
2000
- /*
2001
- * call-seq:
2002
- * FileBuilderContext.add_message(name, &block)
2003
- *
2004
- * Creates a new, empty descriptor with the given name, and invokes the block in
2005
- * the context of a MessageBuilderContext on that descriptor. The block can then
2006
- * call, e.g., MessageBuilderContext#optional and MessageBuilderContext#repeated
2007
- * methods to define the message fields.
2008
- *
2009
- * This is the recommended, idiomatic way to build message definitions.
2010
- */
2011
- VALUE FileBuilderContext_add_message(VALUE _self, VALUE name) {
2012
- DEFINE_SELF(FileBuilderContext, self, _self);
2013
- VALUE msgdef = rb_class_new_instance(1, &self->file_descriptor, cDescriptor);
2014
- VALUE args[2] = { msgdef, self->builder };
2015
- VALUE ctx = rb_class_new_instance(2, args, cMessageBuilderContext);
2016
- VALUE block = rb_block_proc();
2017
- rb_funcall(msgdef, rb_intern("name="), 1, name);
2018
- rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
2019
- rb_ary_push(self->pending_list, msgdef);
2020
- return Qnil;
2021
- }
2022
-
2023
- /*
2024
- * call-seq:
2025
- * FileBuilderContext.add_enum(name, &block)
2026
- *
2027
- * Creates a new, empty enum descriptor with the given name, and invokes the
2028
- * block in the context of an EnumBuilderContext on that descriptor. The block
2029
- * can then call EnumBuilderContext#add_value to define the enum values.
2030
- *
2031
- * This is the recommended, idiomatic way to build enum definitions.
2032
- */
2033
- VALUE FileBuilderContext_add_enum(VALUE _self, VALUE name) {
2034
- DEFINE_SELF(FileBuilderContext, self, _self);
2035
- VALUE enumdef =
2036
- rb_class_new_instance(1, &self->file_descriptor, cEnumDescriptor);
2037
- VALUE ctx = rb_class_new_instance(1, &enumdef, cEnumBuilderContext);
2038
- VALUE block = rb_block_proc();
2039
- rb_funcall(enumdef, rb_intern("name="), 1, name);
2040
- rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
2041
- rb_ary_push(self->pending_list, enumdef);
2042
2265
  return Qnil;
2043
2266
  }
2044
2267
 
2045
- VALUE FileBuilderContext_pending_descriptors(VALUE _self) {
2046
- DEFINE_SELF(FileBuilderContext, self, _self);
2047
- return self->pending_list;
2268
+ static void EnumBuilderContext_register(VALUE module) {
2269
+ VALUE klass = rb_define_class_under(
2270
+ module, "EnumBuilderContext", rb_cObject);
2271
+ rb_define_alloc_func(klass, EnumBuilderContext_alloc);
2272
+ rb_define_method(klass, "initialize", EnumBuilderContext_initialize, 2);
2273
+ rb_define_method(klass, "value", EnumBuilderContext_value, 2);
2274
+ rb_gc_register_address(&cEnumBuilderContext);
2275
+ cEnumBuilderContext = klass;
2048
2276
  }
2049
2277
 
2050
2278
  // -----------------------------------------------------------------------------
2051
2279
  // Builder.
2052
2280
  // -----------------------------------------------------------------------------
2053
2281
 
2054
- DEFINE_CLASS(Builder, "Google::Protobuf::Internal::Builder");
2282
+ typedef struct {
2283
+ VALUE descriptor_pool;
2284
+ VALUE default_file_builder;
2285
+ } Builder;
2055
2286
 
2056
- void Builder_mark(void* _self) {
2057
- Builder* self = _self;
2058
- rb_gc_mark(self->pending_list);
2059
- rb_gc_mark(self->default_file_descriptor);
2060
- }
2287
+ static VALUE cBuilder = Qnil;
2061
2288
 
2062
- void Builder_free(void* _self) {
2289
+ static void Builder_mark(void* _self) {
2063
2290
  Builder* self = _self;
2064
- xfree(self->defs);
2065
- xfree(self);
2291
+ rb_gc_mark(self->descriptor_pool);
2292
+ rb_gc_mark(self->default_file_builder);
2066
2293
  }
2067
2294
 
2068
- /*
2069
- * call-seq:
2070
- * Builder.new => builder
2071
- *
2072
- * Creates a new Builder. A Builder can accumulate a set of new message and enum
2073
- * descriptors and atomically register them into a pool in a way that allows for
2074
- * (co)recursive type references.
2075
- */
2076
- VALUE Builder_alloc(VALUE klass) {
2077
- Builder* self = ALLOC(Builder);
2078
- VALUE ret = TypedData_Wrap_Struct(
2079
- klass, &_Builder_type, self);
2080
- self->pending_list = Qnil;
2081
- self->defs = NULL;
2082
- self->default_file_descriptor = Qnil;
2295
+ static const rb_data_type_t Builder_type = {
2296
+ "Google::Protobuf::Internal::Builder",
2297
+ {Builder_mark, RUBY_DEFAULT_FREE, NULL},
2298
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
2299
+ };
2300
+
2301
+ static Builder* ruby_to_Builder(VALUE val) {
2302
+ Builder* ret;
2303
+ TypedData_Get_Struct(val, Builder, &Builder_type, ret);
2083
2304
  return ret;
2084
2305
  }
2085
2306
 
2086
- void Builder_register(VALUE module) {
2087
- VALUE klass = rb_define_class_under(module, "Builder", rb_cObject);
2088
- rb_define_alloc_func(klass, Builder_alloc);
2089
- rb_define_method(klass, "initialize", Builder_initialize, 0);
2090
- rb_define_method(klass, "add_file", Builder_add_file, -1);
2091
- rb_define_method(klass, "add_message", Builder_add_message, 1);
2092
- rb_define_method(klass, "add_enum", Builder_add_enum, 1);
2093
- rb_define_method(klass, "finalize_to_pool", Builder_finalize_to_pool, 1);
2094
- rb_gc_register_address(&cBuilder);
2095
- cBuilder = klass;
2307
+ static VALUE Builder_alloc(VALUE klass) {
2308
+ Builder* self = ALLOC(Builder);
2309
+ VALUE ret = TypedData_Wrap_Struct(klass, &Builder_type, self);
2310
+ self->descriptor_pool = Qnil;
2311
+ self->default_file_builder = Qnil;
2312
+ return ret;
2096
2313
  }
2097
2314
 
2098
2315
  /*
2099
2316
  * call-seq:
2100
- * Builder.new
2317
+ * Builder.new(descriptor_pool) => builder
2101
2318
  *
2102
- * Initializes a new builder.
2319
+ * Creates a new Builder. A Builder can accumulate a set of new message and enum
2320
+ * descriptors and atomically register them into a pool in a way that allows for
2321
+ * (co)recursive type references.
2103
2322
  */
2104
- VALUE Builder_initialize(VALUE _self) {
2105
- DEFINE_SELF(Builder, self, _self);
2106
- self->pending_list = rb_ary_new();
2107
- VALUE file_name = Qnil;
2108
- self->default_file_descriptor =
2109
- rb_class_new_instance(1, &file_name, cFileDescriptor);
2323
+ static VALUE Builder_initialize(VALUE _self, VALUE pool) {
2324
+ Builder* self = ruby_to_Builder(_self);
2325
+ self->descriptor_pool = pool;
2326
+ self->default_file_builder = Qnil; // Created lazily if needed.
2110
2327
  return Qnil;
2111
2328
  }
2112
2329
 
@@ -2121,19 +2338,40 @@ VALUE Builder_initialize(VALUE _self) {
2121
2338
  *
2122
2339
  * This is the recommended, idiomatic way to build file descriptors.
2123
2340
  */
2124
- VALUE Builder_add_file(int argc, VALUE* argv, VALUE _self) {
2125
- DEFINE_SELF(Builder, self, _self);
2126
- VALUE file_descriptor = rb_class_new_instance(argc, argv, cFileDescriptor);
2127
- VALUE args[2] = { file_descriptor, _self };
2128
- VALUE ctx = rb_class_new_instance(2, args, cFileBuilderContext);
2129
- VALUE block = rb_block_proc();
2341
+ static VALUE Builder_add_file(int argc, VALUE* argv, VALUE _self) {
2342
+ Builder* self = ruby_to_Builder(_self);
2343
+ VALUE name, options;
2344
+ VALUE ctx;
2345
+ VALUE block;
2346
+
2347
+ rb_scan_args(argc, argv, "11", &name, &options);
2348
+
2349
+ {
2350
+ VALUE args[3] = { self->descriptor_pool, name, options };
2351
+ ctx = rb_class_new_instance(3, args, cFileBuilderContext);
2352
+ }
2353
+
2354
+ block = rb_block_proc();
2130
2355
  rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
2356
+ FileBuilderContext_build(ctx);
2131
2357
 
2132
- rb_ary_concat(self->pending_list,
2133
- FileBuilderContext_pending_descriptors(ctx));
2134
2358
  return Qnil;
2135
2359
  }
2136
2360
 
2361
+ static VALUE Builder_get_default_file(VALUE _self) {
2362
+ Builder* self = ruby_to_Builder(_self);
2363
+
2364
+ /* Lazily create only if legacy builder-level methods are called. */
2365
+ if (self->default_file_builder == Qnil) {
2366
+ VALUE name = rb_str_new2("ruby_default_file.proto");
2367
+ VALUE args [3] = { self->descriptor_pool, name, rb_hash_new() };
2368
+ self->default_file_builder =
2369
+ rb_class_new_instance(3, args, cFileBuilderContext);
2370
+ }
2371
+
2372
+ return self->default_file_builder;
2373
+ }
2374
+
2137
2375
  /*
2138
2376
  * call-seq:
2139
2377
  * Builder.add_message(name, &block)
@@ -2145,16 +2383,10 @@ VALUE Builder_add_file(int argc, VALUE* argv, VALUE _self) {
2145
2383
  * files generated by protoc which don't add messages within "add_file" block.
2146
2384
  * Descriptors created this way get assigned to a default empty FileDescriptor.
2147
2385
  */
2148
- VALUE Builder_add_message(VALUE _self, VALUE name) {
2149
- DEFINE_SELF(Builder, self, _self);
2150
- VALUE msgdef =
2151
- rb_class_new_instance(1, &self->default_file_descriptor, cDescriptor);
2152
- VALUE args[2] = { msgdef, _self };
2153
- VALUE ctx = rb_class_new_instance(2, args, cMessageBuilderContext);
2154
- VALUE block = rb_block_proc();
2155
- rb_funcall(msgdef, rb_intern("name="), 1, name);
2156
- rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
2157
- rb_ary_push(self->pending_list, msgdef);
2386
+ static VALUE Builder_add_message(VALUE _self, VALUE name) {
2387
+ VALUE file_builder = Builder_get_default_file(_self);
2388
+ rb_funcall_with_block(file_builder, rb_intern("add_message"), 1, &name,
2389
+ rb_block_proc());
2158
2390
  return Qnil;
2159
2391
  }
2160
2392
 
@@ -2170,88 +2402,158 @@ VALUE Builder_add_message(VALUE _self, VALUE name) {
2170
2402
  * Enum descriptors created this way get assigned to a default empty
2171
2403
  * FileDescriptor.
2172
2404
  */
2173
- VALUE Builder_add_enum(VALUE _self, VALUE name) {
2174
- DEFINE_SELF(Builder, self, _self);
2175
- VALUE enumdef =
2176
- rb_class_new_instance(1, &self->default_file_descriptor, cEnumDescriptor);
2177
- VALUE ctx = rb_class_new_instance(1, &enumdef, cEnumBuilderContext);
2178
- VALUE block = rb_block_proc();
2179
- rb_funcall(enumdef, rb_intern("name="), 1, name);
2180
- rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
2181
- rb_ary_push(self->pending_list, enumdef);
2405
+ static VALUE Builder_add_enum(VALUE _self, VALUE name) {
2406
+ VALUE file_builder = Builder_get_default_file(_self);
2407
+ rb_funcall_with_block(file_builder, rb_intern("add_enum"), 1, &name,
2408
+ rb_block_proc());
2182
2409
  return Qnil;
2183
2410
  }
2184
2411
 
2185
- static void proto3_validate_msgdef(const upb_msgdef* msgdef) {
2186
- // Verify that no required fields exist. proto3 does not support these.
2187
- upb_msg_field_iter it;
2188
- for (upb_msg_field_begin(&it, msgdef);
2189
- !upb_msg_field_done(&it);
2190
- upb_msg_field_next(&it)) {
2191
- const upb_fielddef* field = upb_msg_iter_field(&it);
2192
- if (upb_fielddef_label(field) == UPB_LABEL_REQUIRED) {
2193
- rb_raise(cTypeError, "Required fields are unsupported in proto3.");
2194
- }
2412
+ /* This method is hidden from Ruby, and only called directly from
2413
+ * DescriptorPool_build(). */
2414
+ static VALUE Builder_build(VALUE _self) {
2415
+ Builder* self = ruby_to_Builder(_self);
2416
+
2417
+ if (self->default_file_builder != Qnil) {
2418
+ FileBuilderContext_build(self->default_file_builder);
2419
+ self->default_file_builder = Qnil;
2195
2420
  }
2421
+
2422
+ return Qnil;
2196
2423
  }
2197
2424
 
2198
- static void proto3_validate_enumdef(const upb_enumdef* enumdef) {
2199
- // Verify that an entry exists with integer value 0. (This is the default
2200
- // value.)
2201
- const char* lookup = upb_enumdef_iton(enumdef, 0);
2202
- if (lookup == NULL) {
2203
- rb_raise(cTypeError,
2204
- "Enum definition does not contain a value for '0'.");
2425
+ static void Builder_register(VALUE module) {
2426
+ VALUE klass = rb_define_class_under(module, "Builder", rb_cObject);
2427
+ rb_define_alloc_func(klass, Builder_alloc);
2428
+ rb_define_method(klass, "initialize", Builder_initialize, 1);
2429
+ rb_define_method(klass, "add_file", Builder_add_file, -1);
2430
+ rb_define_method(klass, "add_message", Builder_add_message, 1);
2431
+ rb_define_method(klass, "add_enum", Builder_add_enum, 1);
2432
+ rb_gc_register_address(&cBuilder);
2433
+ cBuilder = klass;
2434
+ }
2435
+
2436
+ static VALUE get_def_obj(VALUE _descriptor_pool, const void* ptr, VALUE klass) {
2437
+ DescriptorPool* descriptor_pool = ruby_to_DescriptorPool(_descriptor_pool);
2438
+ VALUE key = ULL2NUM((intptr_t)ptr);
2439
+ VALUE def;
2440
+
2441
+ def = rb_hash_aref(descriptor_pool->def_to_descriptor, key);
2442
+
2443
+ if (ptr == NULL) {
2444
+ return Qnil;
2445
+ }
2446
+
2447
+ if (def == Qnil) {
2448
+ // Lazily create wrapper object.
2449
+ VALUE args[3] = { c_only_cookie, _descriptor_pool, key };
2450
+ def = rb_class_new_instance(3, args, klass);
2451
+ rb_hash_aset(descriptor_pool->def_to_descriptor, key, def);
2205
2452
  }
2453
+
2454
+ return def;
2206
2455
  }
2207
2456
 
2208
- /*
2209
- * call-seq:
2210
- * Builder.finalize_to_pool(pool)
2211
- *
2212
- * Adds all accumulated message and enum descriptors created in this builder
2213
- * context to the given pool. The operation occurs atomically, and all
2214
- * descriptors can refer to each other (including in cycles). This is the only
2215
- * way to build (co)recursive message definitions.
2216
- *
2217
- * This method is usually called automatically by DescriptorPool#build after it
2218
- * invokes the given user block in the context of the builder. The user should
2219
- * not normally need to call this manually because a Builder is not normally
2220
- * created manually.
2221
- */
2222
- VALUE Builder_finalize_to_pool(VALUE _self, VALUE pool_rb) {
2223
- DEFINE_SELF(Builder, self, _self);
2457
+ static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_msgdef* def) {
2458
+ return get_def_obj(descriptor_pool, def, cDescriptor);
2459
+ }
2224
2460
 
2225
- DescriptorPool* pool = ruby_to_DescriptorPool(pool_rb);
2461
+ static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_enumdef* def) {
2462
+ return get_def_obj(descriptor_pool, def, cEnumDescriptor);
2463
+ }
2226
2464
 
2227
- REALLOC_N(self->defs, upb_def*, RARRAY_LEN(self->pending_list));
2465
+ static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_fielddef* def) {
2466
+ return get_def_obj(descriptor_pool, def, cFieldDescriptor);
2467
+ }
2228
2468
 
2229
- for (int i = 0; i < RARRAY_LEN(self->pending_list); i++) {
2230
- VALUE def_rb = rb_ary_entry(self->pending_list, i);
2231
- if (CLASS_OF(def_rb) == cDescriptor) {
2232
- self->defs[i] = (upb_def*)ruby_to_Descriptor(def_rb)->msgdef;
2469
+ static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_filedef* def) {
2470
+ return get_def_obj(descriptor_pool, def, cFileDescriptor);
2471
+ }
2233
2472
 
2234
- if (upb_filedef_syntax(upb_def_file(self->defs[i])) == UPB_SYNTAX_PROTO3) {
2235
- proto3_validate_msgdef((const upb_msgdef*)self->defs[i]);
2236
- }
2237
- } else if (CLASS_OF(def_rb) == cEnumDescriptor) {
2238
- self->defs[i] = (upb_def*)ruby_to_EnumDescriptor(def_rb)->enumdef;
2473
+ static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_oneofdef* def) {
2474
+ return get_def_obj(descriptor_pool, def, cOneofDescriptor);
2475
+ }
2239
2476
 
2240
- if (upb_filedef_syntax(upb_def_file(self->defs[i])) == UPB_SYNTAX_PROTO3) {
2241
- proto3_validate_enumdef((const upb_enumdef*)self->defs[i]);
2242
- }
2477
+ // -----------------------------------------------------------------------------
2478
+ // Shared functions
2479
+ // -----------------------------------------------------------------------------
2480
+
2481
+ // Functions exposed to other modules in defs.h.
2482
+
2483
+ VALUE Descriptor_DefToClass(const upb_msgdef *m) {
2484
+ const upb_symtab *symtab = upb_filedef_symtab(upb_msgdef_file(m));
2485
+ VALUE pool = ObjectCache_Get(symtab);
2486
+ PBRUBY_ASSERT(pool != Qnil);
2487
+ VALUE desc_rb = get_msgdef_obj(pool, m);
2488
+ const Descriptor* desc = ruby_to_Descriptor(desc_rb);
2489
+ return desc->klass;
2490
+ }
2491
+
2492
+ const upb_msgdef *Descriptor_GetMsgDef(VALUE desc_rb) {
2493
+ const Descriptor* desc = ruby_to_Descriptor(desc_rb);
2494
+ return desc->msgdef;
2495
+ }
2496
+
2497
+ VALUE TypeInfo_InitArg(int argc, VALUE *argv, int skip_arg) {
2498
+ if (argc > skip_arg) {
2499
+ if (argc > 1 + skip_arg) {
2500
+ rb_raise(rb_eArgError, "Expected a maximum of %d arguments.", skip_arg + 1);
2243
2501
  }
2502
+ return argv[skip_arg];
2503
+ } else {
2504
+ return Qnil;
2244
2505
  }
2506
+ }
2245
2507
 
2246
- CHECK_UPB(upb_symtab_add(pool->symtab, (upb_def**)self->defs,
2247
- RARRAY_LEN(self->pending_list), NULL, &status),
2248
- "Unable to add defs to DescriptorPool");
2508
+ TypeInfo TypeInfo_FromClass(int argc, VALUE* argv, int skip_arg,
2509
+ VALUE* type_class, VALUE* init_arg) {
2510
+ TypeInfo ret = {ruby_to_fieldtype(argv[skip_arg])};
2249
2511
 
2250
- for (int i = 0; i < RARRAY_LEN(self->pending_list); i++) {
2251
- VALUE def_rb = rb_ary_entry(self->pending_list, i);
2252
- add_def_obj(self->defs[i], def_rb);
2512
+ if (ret.type == UPB_TYPE_MESSAGE || ret.type == UPB_TYPE_ENUM) {
2513
+ *init_arg = TypeInfo_InitArg(argc, argv, skip_arg + 2);
2514
+
2515
+ if (argc < 2 + skip_arg) {
2516
+ rb_raise(rb_eArgError, "Expected at least %d arguments for message/enum.",
2517
+ 2 + skip_arg);
2518
+ }
2519
+
2520
+ VALUE klass = argv[1 + skip_arg];
2521
+ VALUE desc = MessageOrEnum_GetDescriptor(klass);
2522
+ *type_class = klass;
2523
+
2524
+ if (desc == Qnil) {
2525
+ rb_raise(rb_eArgError,
2526
+ "Type class has no descriptor. Please pass a "
2527
+ "class or enum as returned by the DescriptorPool.");
2528
+ }
2529
+
2530
+ if (ret.type == UPB_TYPE_MESSAGE) {
2531
+ ret.def.msgdef = ruby_to_Descriptor(desc)->msgdef;
2532
+ Message_CheckClass(klass);
2533
+ } else {
2534
+ PBRUBY_ASSERT(ret.type == UPB_TYPE_ENUM);
2535
+ ret.def.enumdef = ruby_to_EnumDescriptor(desc)->enumdef;
2536
+ }
2537
+ } else {
2538
+ *init_arg = TypeInfo_InitArg(argc, argv, skip_arg + 1);
2253
2539
  }
2254
2540
 
2255
- self->pending_list = rb_ary_new();
2256
- return Qnil;
2541
+ return ret;
2542
+ }
2543
+
2544
+ void Defs_register(VALUE module) {
2545
+ DescriptorPool_register(module);
2546
+ Descriptor_register(module);
2547
+ FileDescriptor_register(module);
2548
+ FieldDescriptor_register(module);
2549
+ OneofDescriptor_register(module);
2550
+ EnumDescriptor_register(module);
2551
+ FileBuilderContext_register(module);
2552
+ MessageBuilderContext_register(module);
2553
+ OneofBuilderContext_register(module);
2554
+ EnumBuilderContext_register(module);
2555
+ Builder_register(module);
2556
+
2557
+ rb_gc_register_address(&c_only_cookie);
2558
+ c_only_cookie = rb_class_new_instance(0, NULL, rb_cObject);
2257
2559
  }