google-protobuf 3.11.3 → 3.19.3

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

Potentially problematic release.


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

@@ -30,8 +30,28 @@
30
30
 
31
31
  #include <ctype.h>
32
32
  #include <errno.h>
33
+ #include <ruby/version.h>
34
+
35
+ #include "convert.h"
36
+ #include "message.h"
33
37
  #include "protobuf.h"
34
38
 
39
+ // -----------------------------------------------------------------------------
40
+ // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
41
+ // instances.
42
+ // -----------------------------------------------------------------------------
43
+
44
+ static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_msgdef* def);
45
+ static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_enumdef* def);
46
+ static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_fielddef* def);
47
+ static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_filedef* def);
48
+ static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_oneofdef* def);
49
+
50
+ // A distinct object that is not accessible from Ruby. We use this as a
51
+ // constructor argument to enforce that certain objects cannot be created from
52
+ // Ruby.
53
+ VALUE c_only_cookie = Qnil;
54
+
35
55
  // -----------------------------------------------------------------------------
36
56
  // Common utilities.
37
57
  // -----------------------------------------------------------------------------
@@ -48,394 +68,98 @@ static VALUE rb_str_maybe_null(const char* s) {
48
68
  return rb_str_new2(s);
49
69
  }
50
70
 
51
- static void rewrite_enum_default(const upb_symtab* symtab,
52
- google_protobuf_FileDescriptorProto* file,
53
- google_protobuf_FieldDescriptorProto* field) {
54
- upb_strview defaultval;
55
- const char *type_name_str;
56
- char *end;
57
- long val;
58
- const upb_enumdef *e;
59
- upb_strview type_name;
60
-
61
- /* Look for TYPE_ENUM fields that have a default. */
62
- if (google_protobuf_FieldDescriptorProto_type(field) !=
63
- google_protobuf_FieldDescriptorProto_TYPE_ENUM ||
64
- !google_protobuf_FieldDescriptorProto_has_default_value(field) ||
65
- !google_protobuf_FieldDescriptorProto_has_type_name(field)) {
66
- return;
67
- }
68
-
69
- defaultval = google_protobuf_FieldDescriptorProto_default_value(field);
70
- type_name = google_protobuf_FieldDescriptorProto_type_name(field);
71
-
72
- if (defaultval.size == 0 || !isdigit(defaultval.data[0])) {
73
- return;
74
- }
75
-
76
- if (type_name.size == 0 || type_name.data[0] != '.') {
77
- return;
78
- }
79
-
80
- type_name_str = type_name.data + 1;
81
-
82
- errno = 0;
83
- val = strtol(defaultval.data, &end, 10);
84
-
85
- if (errno != 0 || *end != 0 || val < INT32_MIN || val > INT32_MAX) {
86
- return;
87
- }
88
-
89
- /* Now find the corresponding enum definition. */
90
- e = upb_symtab_lookupenum(symtab, type_name_str);
91
- if (e) {
92
- /* Look in previously loaded files. */
93
- const char *label = upb_enumdef_iton(e, val);
94
- if (!label) {
95
- return;
96
- }
97
- google_protobuf_FieldDescriptorProto_set_default_value(
98
- field, upb_strview_makez(label));
99
- } else {
100
- /* Look in enums defined in this file. */
101
- const google_protobuf_EnumDescriptorProto* matching_enum = NULL;
102
- size_t i, n;
103
- const google_protobuf_EnumDescriptorProto* const* enums =
104
- google_protobuf_FileDescriptorProto_enum_type(file, &n);
105
- const google_protobuf_EnumValueDescriptorProto* const* values;
106
-
107
- for (i = 0; i < n; i++) {
108
- if (upb_strview_eql(google_protobuf_EnumDescriptorProto_name(enums[i]),
109
- upb_strview_makez(type_name_str))) {
110
- matching_enum = enums[i];
111
- break;
112
- }
113
- }
114
-
115
- if (!matching_enum) {
116
- return;
117
- }
118
-
119
- values = google_protobuf_EnumDescriptorProto_value(matching_enum, &n);
120
- for (i = 0; i < n; i++) {
121
- if (google_protobuf_EnumValueDescriptorProto_number(values[i]) == val) {
122
- google_protobuf_FieldDescriptorProto_set_default_value(
123
- field, google_protobuf_EnumValueDescriptorProto_name(values[i]));
124
- return;
125
- }
126
- }
127
-
128
- /* We failed to find an enum default. But we'll just leave the enum
129
- * untouched and let the normal def-building code catch it. */
130
- }
131
- }
132
-
133
- /* Historically we allowed enum defaults to be specified as a number. In
134
- * retrospect this was a mistake as descriptors require defaults to be
135
- * specified as a label. This can make a difference if multiple labels have the
136
- * same number.
137
- *
138
- * Here we do a pass over all enum defaults and rewrite numeric defaults by
139
- * looking up their labels. This is compilcated by the fact that the enum
140
- * definition can live in either the symtab or the file_proto.
141
- * */
142
- static void rewrite_enum_defaults(
143
- const upb_symtab* symtab, google_protobuf_FileDescriptorProto* file_proto) {
144
- size_t i, n;
145
- google_protobuf_DescriptorProto** msgs =
146
- google_protobuf_FileDescriptorProto_mutable_message_type(file_proto, &n);
147
-
148
- for (i = 0; i < n; i++) {
149
- size_t j, m;
150
- google_protobuf_FieldDescriptorProto** fields =
151
- google_protobuf_DescriptorProto_mutable_field(msgs[i], &m);
152
- for (j = 0; j < m; j++) {
153
- rewrite_enum_default(symtab, file_proto, fields[j]);
154
- }
155
- }
156
- }
157
-
158
- static void remove_path(upb_strview *name) {
159
- const char* last = strrchr(name->data, '.');
160
- if (last) {
161
- size_t remove = last - name->data + 1;
162
- name->data += remove;
163
- name->size -= remove;
164
- }
165
- }
166
-
167
- static void rewrite_nesting(VALUE msg_ent, google_protobuf_DescriptorProto* msg,
168
- google_protobuf_DescriptorProto* const* msgs,
169
- google_protobuf_EnumDescriptorProto* const* enums,
170
- upb_arena *arena) {
171
- VALUE submsgs = rb_hash_aref(msg_ent, ID2SYM(rb_intern("msgs")));
172
- VALUE enum_pos = rb_hash_aref(msg_ent, ID2SYM(rb_intern("enums")));
173
- int submsg_count;
174
- int enum_count;
175
- int i;
176
- google_protobuf_DescriptorProto** msg_msgs;
177
- google_protobuf_EnumDescriptorProto** msg_enums;
178
-
179
- Check_Type(submsgs, T_ARRAY);
180
- Check_Type(enum_pos, T_ARRAY);
181
-
182
- submsg_count = RARRAY_LEN(submsgs);
183
- enum_count = RARRAY_LEN(enum_pos);
184
-
185
- msg_msgs = google_protobuf_DescriptorProto_resize_nested_type(
186
- msg, submsg_count, arena);
187
- msg_enums =
188
- google_protobuf_DescriptorProto_resize_enum_type(msg, enum_count, arena);
189
-
190
- for (i = 0; i < submsg_count; i++) {
191
- VALUE submsg_ent = RARRAY_PTR(submsgs)[i];
192
- VALUE pos = rb_hash_aref(submsg_ent, ID2SYM(rb_intern("pos")));
193
- upb_strview name;
194
-
195
- msg_msgs[i] = msgs[NUM2INT(pos)];
196
- name = google_protobuf_DescriptorProto_name(msg_msgs[i]);
197
- remove_path(&name);
198
- google_protobuf_DescriptorProto_set_name(msg_msgs[i], name);
199
- rewrite_nesting(submsg_ent, msg_msgs[i], msgs, enums, arena);
200
- }
201
-
202
- for (i = 0; i < enum_count; i++) {
203
- VALUE pos = RARRAY_PTR(enum_pos)[i];
204
- msg_enums[i] = enums[NUM2INT(pos)];
205
- }
206
- }
207
-
208
- /* We have to do some relatively complicated logic here for backward
209
- * compatibility.
210
- *
211
- * In descriptor.proto, messages are nested inside other messages if that is
212
- * what the original .proto file looks like. For example, suppose we have this
213
- * foo.proto:
214
- *
215
- * package foo;
216
- * message Bar {
217
- * message Baz {}
218
- * }
219
- *
220
- * The descriptor for this must look like this:
221
- *
222
- * file {
223
- * name: "test.proto"
224
- * package: "foo"
225
- * message_type {
226
- * name: "Bar"
227
- * nested_type {
228
- * name: "Baz"
229
- * }
230
- * }
231
- * }
232
- *
233
- * However, the Ruby generated code has always generated messages in a flat,
234
- * non-nested way:
235
- *
236
- * Google::Protobuf::DescriptorPool.generated_pool.build do
237
- * add_message "foo.Bar" do
238
- * end
239
- * add_message "foo.Bar.Baz" do
240
- * end
241
- * end
242
- *
243
- * Here we need to do a translation where we turn this generated code into the
244
- * above descriptor. We need to infer that "foo" is the package name, and not
245
- * a message itself.
246
- *
247
- * We delegate to Ruby to compute the transformation, for more concice and
248
- * readable code than we can do in C */
249
- static void rewrite_names(VALUE _file_builder,
250
- google_protobuf_FileDescriptorProto* file_proto) {
251
- FileBuilderContext* file_builder = ruby_to_FileBuilderContext(_file_builder);
252
- upb_arena *arena = file_builder->arena;
253
- // Build params (package, msg_names, enum_names).
254
- VALUE package = Qnil;
255
- VALUE msg_names = rb_ary_new();
256
- VALUE enum_names = rb_ary_new();
257
- size_t msg_count, enum_count, i;
258
- VALUE new_package, nesting, msg_ents, enum_ents;
259
- google_protobuf_DescriptorProto** msgs;
260
- google_protobuf_EnumDescriptorProto** enums;
261
-
262
- if (google_protobuf_FileDescriptorProto_has_package(file_proto)) {
263
- upb_strview package_str =
264
- google_protobuf_FileDescriptorProto_package(file_proto);
265
- package = rb_str_new(package_str.data, package_str.size);
266
- }
267
-
268
- msgs = google_protobuf_FileDescriptorProto_mutable_message_type(file_proto,
269
- &msg_count);
270
- for (i = 0; i < msg_count; i++) {
271
- upb_strview name = google_protobuf_DescriptorProto_name(msgs[i]);
272
- rb_ary_push(msg_names, rb_str_new(name.data, name.size));
273
- }
274
-
275
- enums = google_protobuf_FileDescriptorProto_mutable_enum_type(file_proto,
276
- &enum_count);
277
- for (i = 0; i < enum_count; i++) {
278
- upb_strview name = google_protobuf_EnumDescriptorProto_name(enums[i]);
279
- rb_ary_push(enum_names, rb_str_new(name.data, name.size));
280
- }
281
-
282
- {
283
- // Call Ruby code to calculate package name and nesting.
284
- VALUE args[3] = { package, msg_names, enum_names };
285
- VALUE internal = rb_eval_string("Google::Protobuf::Internal");
286
- VALUE ret = rb_funcallv(internal, rb_intern("fixup_descriptor"), 3, args);
287
-
288
- new_package = rb_ary_entry(ret, 0);
289
- nesting = rb_ary_entry(ret, 1);
290
- }
291
-
292
- // Rewrite package and names.
293
- if (new_package != Qnil) {
294
- upb_strview new_package_str =
295
- FileBuilderContext_strdup(_file_builder, new_package);
296
- google_protobuf_FileDescriptorProto_set_package(file_proto,
297
- new_package_str);
298
- }
299
-
300
- for (i = 0; i < msg_count; i++) {
301
- upb_strview name = google_protobuf_DescriptorProto_name(msgs[i]);
302
- remove_path(&name);
303
- google_protobuf_DescriptorProto_set_name(msgs[i], name);
304
- }
305
-
306
- for (i = 0; i < enum_count; i++) {
307
- upb_strview name = google_protobuf_EnumDescriptorProto_name(enums[i]);
308
- remove_path(&name);
309
- google_protobuf_EnumDescriptorProto_set_name(enums[i], name);
310
- }
311
-
312
- // Rewrite nesting.
313
- msg_ents = rb_hash_aref(nesting, ID2SYM(rb_intern("msgs")));
314
- enum_ents = rb_hash_aref(nesting, ID2SYM(rb_intern("enums")));
315
-
316
- Check_Type(msg_ents, T_ARRAY);
317
- Check_Type(enum_ents, T_ARRAY);
318
-
319
- for (i = 0; i < (size_t)RARRAY_LEN(msg_ents); i++) {
320
- VALUE msg_ent = rb_ary_entry(msg_ents, i);
321
- VALUE pos = rb_hash_aref(msg_ent, ID2SYM(rb_intern("pos")));
322
- msgs[i] = msgs[NUM2INT(pos)];
323
- rewrite_nesting(msg_ent, msgs[i], msgs, enums, arena);
324
- }
325
-
326
- for (i = 0; i < (size_t)RARRAY_LEN(enum_ents); i++) {
327
- VALUE enum_pos = rb_ary_entry(enum_ents, i);
328
- enums[i] = enums[NUM2INT(enum_pos)];
329
- }
330
-
331
- google_protobuf_FileDescriptorProto_resize_message_type(
332
- file_proto, RARRAY_LEN(msg_ents), arena);
333
- google_protobuf_FileDescriptorProto_resize_enum_type(
334
- file_proto, RARRAY_LEN(enum_ents), arena);
335
- }
336
-
337
71
  // -----------------------------------------------------------------------------
338
72
  // DescriptorPool.
339
73
  // -----------------------------------------------------------------------------
340
74
 
341
- #define DEFINE_CLASS(name, string_name) \
342
- VALUE c ## name = Qnil; \
343
- const rb_data_type_t _ ## name ## _type = { \
344
- string_name, \
345
- { name ## _mark, name ## _free, NULL }, \
346
- }; \
347
- name* ruby_to_ ## name(VALUE val) { \
348
- name* ret; \
349
- TypedData_Get_Struct(val, name, &_ ## name ## _type, ret); \
350
- return ret; \
351
- } \
352
-
353
- #define DEFINE_SELF(type, var, rb_var) \
354
- type* var = ruby_to_ ## type(rb_var)
75
+ typedef struct {
76
+ VALUE def_to_descriptor; // Hash table of def* -> Ruby descriptor.
77
+ upb_symtab* symtab;
78
+ } DescriptorPool;
79
+
80
+ VALUE cDescriptorPool = Qnil;
355
81
 
356
82
  // Global singleton DescriptorPool. The user is free to create others, but this
357
83
  // is used by generated code.
358
84
  VALUE generated_pool = Qnil;
359
85
 
360
- DEFINE_CLASS(DescriptorPool, "Google::Protobuf::DescriptorPool");
361
-
362
- void DescriptorPool_mark(void* _self) {
86
+ static void DescriptorPool_mark(void* _self) {
363
87
  DescriptorPool* self = _self;
364
88
  rb_gc_mark(self->def_to_descriptor);
365
89
  }
366
90
 
367
- void DescriptorPool_free(void* _self) {
91
+ static void DescriptorPool_free(void* _self) {
368
92
  DescriptorPool* self = _self;
369
-
370
93
  upb_symtab_free(self->symtab);
371
- upb_handlercache_free(self->fill_handler_cache);
372
- upb_handlercache_free(self->pb_serialize_handler_cache);
373
- upb_handlercache_free(self->json_serialize_handler_cache);
374
- upb_handlercache_free(self->json_serialize_handler_preserve_cache);
375
- upb_pbcodecache_free(self->fill_method_cache);
376
- upb_json_codecache_free(self->json_fill_method_cache);
377
-
378
94
  xfree(self);
379
95
  }
380
96
 
97
+ static const rb_data_type_t DescriptorPool_type = {
98
+ "Google::Protobuf::DescriptorPool",
99
+ {DescriptorPool_mark, DescriptorPool_free, NULL},
100
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
101
+ };
102
+
103
+ static DescriptorPool* ruby_to_DescriptorPool(VALUE val) {
104
+ DescriptorPool* ret;
105
+ TypedData_Get_Struct(val, DescriptorPool, &DescriptorPool_type, ret);
106
+ return ret;
107
+ }
108
+
109
+ // Exposed to other modules in defs.h.
110
+ const upb_symtab *DescriptorPool_GetSymtab(VALUE desc_pool_rb) {
111
+ DescriptorPool *pool = ruby_to_DescriptorPool(desc_pool_rb);
112
+ return pool->symtab;
113
+ }
114
+
381
115
  /*
382
116
  * call-seq:
383
117
  * DescriptorPool.new => pool
384
118
  *
385
119
  * Creates a new, empty, descriptor pool.
386
120
  */
387
- VALUE DescriptorPool_alloc(VALUE klass) {
121
+ static VALUE DescriptorPool_alloc(VALUE klass) {
388
122
  DescriptorPool* self = ALLOC(DescriptorPool);
389
123
  VALUE ret;
390
124
 
391
125
  self->def_to_descriptor = Qnil;
392
- ret = TypedData_Wrap_Struct(klass, &_DescriptorPool_type, self);
126
+ ret = TypedData_Wrap_Struct(klass, &DescriptorPool_type, self);
393
127
 
394
128
  self->def_to_descriptor = rb_hash_new();
395
129
  self->symtab = upb_symtab_new();
396
- self->fill_handler_cache =
397
- upb_handlercache_new(add_handlers_for_message, (void*)ret);
398
- self->pb_serialize_handler_cache = upb_pb_encoder_newcache();
399
- self->json_serialize_handler_cache = upb_json_printer_newcache(false);
400
- self->json_serialize_handler_preserve_cache =
401
- upb_json_printer_newcache(true);
402
- self->fill_method_cache = upb_pbcodecache_new(self->fill_handler_cache);
403
- self->json_fill_method_cache = upb_json_codecache_new();
130
+ ObjectCache_Add(self->symtab, ret);
404
131
 
405
132
  return ret;
406
133
  }
407
134
 
408
- void DescriptorPool_register(VALUE module) {
409
- VALUE klass = rb_define_class_under(
410
- module, "DescriptorPool", rb_cObject);
411
- rb_define_alloc_func(klass, DescriptorPool_alloc);
412
- rb_define_method(klass, "build", DescriptorPool_build, -1);
413
- rb_define_method(klass, "lookup", DescriptorPool_lookup, 1);
414
- rb_define_singleton_method(klass, "generated_pool",
415
- DescriptorPool_generated_pool, 0);
416
- rb_gc_register_address(&cDescriptorPool);
417
- cDescriptorPool = klass;
418
-
419
- rb_gc_register_address(&generated_pool);
420
- generated_pool = rb_class_new_instance(0, NULL, klass);
421
- }
422
-
423
135
  /*
424
136
  * call-seq:
425
- * DescriptorPool.build(&block)
137
+ * DescriptorPool.add_serialized_file(serialized_file_proto)
426
138
  *
427
- * Invokes the block with a Builder instance as self. All message and enum types
428
- * added within the block are committed to the pool atomically, and may refer
429
- * (co)recursively to each other. The user should call Builder#add_message and
430
- * Builder#add_enum within the block as appropriate. This is the recommended,
431
- * idiomatic way to define new message and enum types.
139
+ * Adds the given serialized FileDescriptorProto to the pool.
432
140
  */
433
- VALUE DescriptorPool_build(int argc, VALUE* argv, VALUE _self) {
434
- VALUE ctx = rb_class_new_instance(1, &_self, cBuilder);
435
- VALUE block = rb_block_proc();
436
- rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
437
- Builder_build(ctx);
438
- return Qnil;
141
+ VALUE DescriptorPool_add_serialized_file(VALUE _self,
142
+ VALUE serialized_file_proto) {
143
+ DescriptorPool* self = ruby_to_DescriptorPool(_self);
144
+ Check_Type(serialized_file_proto, T_STRING);
145
+ VALUE arena_rb = Arena_new();
146
+ upb_arena *arena = Arena_get(arena_rb);
147
+ google_protobuf_FileDescriptorProto* file_proto =
148
+ google_protobuf_FileDescriptorProto_parse(
149
+ RSTRING_PTR(serialized_file_proto),
150
+ RSTRING_LEN(serialized_file_proto), arena);
151
+ if (!file_proto) {
152
+ rb_raise(rb_eArgError, "Unable to parse FileDescriptorProto");
153
+ }
154
+ upb_status status;
155
+ upb_status_clear(&status);
156
+ const upb_filedef* filedef =
157
+ upb_symtab_addfile(self->symtab, file_proto, &status);
158
+ if (!filedef) {
159
+ rb_raise(cTypeError, "Unable to build file to DescriptorPool: %s",
160
+ upb_status_errmsg(&status));
161
+ }
162
+ return get_filedef_obj(_self, filedef);
439
163
  }
440
164
 
441
165
  /*
@@ -445,8 +169,8 @@ VALUE DescriptorPool_build(int argc, VALUE* argv, VALUE _self) {
445
169
  * Finds a Descriptor or EnumDescriptor by name and returns it, or nil if none
446
170
  * exists with the given name.
447
171
  */
448
- VALUE DescriptorPool_lookup(VALUE _self, VALUE name) {
449
- DEFINE_SELF(DescriptorPool, self, _self);
172
+ static VALUE DescriptorPool_lookup(VALUE _self, VALUE name) {
173
+ DescriptorPool* self = ruby_to_DescriptorPool(_self);
450
174
  const char* name_str = get_str(name);
451
175
  const upb_msgdef* msgdef;
452
176
  const upb_enumdef* enumdef;
@@ -473,31 +197,54 @@ VALUE DescriptorPool_lookup(VALUE _self, VALUE name) {
473
197
  * register types in this pool for convenience so that they do not have to hold
474
198
  * a reference to a private pool instance.
475
199
  */
476
- VALUE DescriptorPool_generated_pool(VALUE _self) {
200
+ static VALUE DescriptorPool_generated_pool(VALUE _self) {
477
201
  return generated_pool;
478
202
  }
479
203
 
204
+ static void DescriptorPool_register(VALUE module) {
205
+ VALUE klass = rb_define_class_under(
206
+ module, "DescriptorPool", rb_cObject);
207
+ rb_define_alloc_func(klass, DescriptorPool_alloc);
208
+ rb_define_method(klass, "add_serialized_file",
209
+ DescriptorPool_add_serialized_file, 1);
210
+ rb_define_method(klass, "lookup", DescriptorPool_lookup, 1);
211
+ rb_define_singleton_method(klass, "generated_pool",
212
+ DescriptorPool_generated_pool, 0);
213
+ rb_gc_register_address(&cDescriptorPool);
214
+ cDescriptorPool = klass;
215
+
216
+ rb_gc_register_address(&generated_pool);
217
+ generated_pool = rb_class_new_instance(0, NULL, klass);
218
+ }
219
+
480
220
  // -----------------------------------------------------------------------------
481
221
  // Descriptor.
482
222
  // -----------------------------------------------------------------------------
483
223
 
484
- DEFINE_CLASS(Descriptor, "Google::Protobuf::Descriptor");
224
+ typedef struct {
225
+ const upb_msgdef* msgdef;
226
+ VALUE klass;
227
+ VALUE descriptor_pool;
228
+ } Descriptor;
229
+
230
+ VALUE cDescriptor = Qnil;
485
231
 
486
- void Descriptor_mark(void* _self) {
232
+ static void Descriptor_mark(void* _self) {
487
233
  Descriptor* self = _self;
488
234
  rb_gc_mark(self->klass);
489
235
  rb_gc_mark(self->descriptor_pool);
490
- if (self->layout && self->layout->empty_template) {
491
- layout_mark(self->layout, self->layout->empty_template);
492
- }
493
236
  }
494
237
 
495
- void Descriptor_free(void* _self) {
496
- Descriptor* self = _self;
497
- if (self->layout) {
498
- free_layout(self->layout);
499
- }
500
- xfree(self);
238
+ static const rb_data_type_t Descriptor_type = {
239
+ "Google::Protobuf::Descriptor",
240
+ {Descriptor_mark, RUBY_DEFAULT_FREE, NULL},
241
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
242
+ };
243
+
244
+ static Descriptor* ruby_to_Descriptor(VALUE val) {
245
+ Descriptor* ret;
246
+ TypedData_Get_Struct(val, Descriptor, &Descriptor_type, ret);
247
+ return ret;
501
248
  }
502
249
 
503
250
  /*
@@ -509,42 +256,24 @@ void Descriptor_free(void* _self) {
509
256
  * it is added to a pool, after which it becomes immutable (as part of a
510
257
  * finalization process).
511
258
  */
512
- VALUE Descriptor_alloc(VALUE klass) {
259
+ static VALUE Descriptor_alloc(VALUE klass) {
513
260
  Descriptor* self = ALLOC(Descriptor);
514
- VALUE ret = TypedData_Wrap_Struct(klass, &_Descriptor_type, self);
261
+ VALUE ret = TypedData_Wrap_Struct(klass, &Descriptor_type, self);
515
262
  self->msgdef = NULL;
516
263
  self->klass = Qnil;
517
264
  self->descriptor_pool = Qnil;
518
- self->layout = NULL;
519
265
  return ret;
520
266
  }
521
267
 
522
- void Descriptor_register(VALUE module) {
523
- VALUE klass = rb_define_class_under(
524
- module, "Descriptor", rb_cObject);
525
- rb_define_alloc_func(klass, Descriptor_alloc);
526
- rb_define_method(klass, "initialize", Descriptor_initialize, 3);
527
- rb_define_method(klass, "each", Descriptor_each, 0);
528
- rb_define_method(klass, "lookup", Descriptor_lookup, 1);
529
- rb_define_method(klass, "each_oneof", Descriptor_each_oneof, 0);
530
- rb_define_method(klass, "lookup_oneof", Descriptor_lookup_oneof, 1);
531
- rb_define_method(klass, "msgclass", Descriptor_msgclass, 0);
532
- rb_define_method(klass, "name", Descriptor_name, 0);
533
- rb_define_method(klass, "file_descriptor", Descriptor_file_descriptor, 0);
534
- rb_include_module(klass, rb_mEnumerable);
535
- rb_gc_register_address(&cDescriptor);
536
- cDescriptor = klass;
537
- }
538
-
539
268
  /*
540
269
  * call-seq:
541
270
  * Descriptor.new(c_only_cookie, ptr) => Descriptor
542
271
  *
543
272
  * Creates a descriptor wrapper object. May only be called from C.
544
273
  */
545
- VALUE Descriptor_initialize(VALUE _self, VALUE cookie,
546
- VALUE descriptor_pool, VALUE ptr) {
547
- DEFINE_SELF(Descriptor, self, _self);
274
+ static VALUE Descriptor_initialize(VALUE _self, VALUE cookie,
275
+ VALUE descriptor_pool, VALUE ptr) {
276
+ Descriptor* self = ruby_to_Descriptor(_self);
548
277
 
549
278
  if (cookie != c_only_cookie) {
550
279
  rb_raise(rb_eRuntimeError,
@@ -563,8 +292,8 @@ VALUE Descriptor_initialize(VALUE _self, VALUE cookie,
563
292
  *
564
293
  * Returns the FileDescriptor object this message belongs to.
565
294
  */
566
- VALUE Descriptor_file_descriptor(VALUE _self) {
567
- DEFINE_SELF(Descriptor, self, _self);
295
+ static VALUE Descriptor_file_descriptor(VALUE _self) {
296
+ Descriptor* self = ruby_to_Descriptor(_self);
568
297
  return get_filedef_obj(self->descriptor_pool, upb_msgdef_file(self->msgdef));
569
298
  }
570
299
 
@@ -572,11 +301,11 @@ VALUE Descriptor_file_descriptor(VALUE _self) {
572
301
  * call-seq:
573
302
  * Descriptor.name => name
574
303
  *
575
- * Returns the name of this message type as a fully-qualfied string (e.g.,
304
+ * Returns the name of this message type as a fully-qualified string (e.g.,
576
305
  * My.Package.MessageType).
577
306
  */
578
- VALUE Descriptor_name(VALUE _self) {
579
- DEFINE_SELF(Descriptor, self, _self);
307
+ static VALUE Descriptor_name(VALUE _self) {
308
+ Descriptor* self = ruby_to_Descriptor(_self);
580
309
  return rb_str_maybe_null(upb_msgdef_fullname(self->msgdef));
581
310
  }
582
311
 
@@ -586,8 +315,8 @@ VALUE Descriptor_name(VALUE _self) {
586
315
  *
587
316
  * Iterates over fields in this message type, yielding to the block on each one.
588
317
  */
589
- VALUE Descriptor_each(VALUE _self) {
590
- DEFINE_SELF(Descriptor, self, _self);
318
+ static VALUE Descriptor_each(VALUE _self) {
319
+ Descriptor* self = ruby_to_Descriptor(_self);
591
320
 
592
321
  upb_msg_field_iter it;
593
322
  for (upb_msg_field_begin(&it, self->msgdef);
@@ -607,8 +336,8 @@ VALUE Descriptor_each(VALUE _self) {
607
336
  * Returns the field descriptor for the field with the given name, if present,
608
337
  * or nil if none.
609
338
  */
610
- VALUE Descriptor_lookup(VALUE _self, VALUE name) {
611
- DEFINE_SELF(Descriptor, self, _self);
339
+ static VALUE Descriptor_lookup(VALUE _self, VALUE name) {
340
+ Descriptor* self = ruby_to_Descriptor(_self);
612
341
  const char* s = get_str(name);
613
342
  const upb_fielddef* field = upb_msgdef_ntofz(self->msgdef, s);
614
343
  if (field == NULL) {
@@ -624,8 +353,8 @@ VALUE Descriptor_lookup(VALUE _self, VALUE name) {
624
353
  * Invokes the given block for each oneof in this message type, passing the
625
354
  * corresponding OneofDescriptor.
626
355
  */
627
- VALUE Descriptor_each_oneof(VALUE _self) {
628
- DEFINE_SELF(Descriptor, self, _self);
356
+ static VALUE Descriptor_each_oneof(VALUE _self) {
357
+ Descriptor* self = ruby_to_Descriptor(_self);
629
358
 
630
359
  upb_msg_oneof_iter it;
631
360
  for (upb_msg_oneof_begin(&it, self->msgdef);
@@ -645,8 +374,8 @@ VALUE Descriptor_each_oneof(VALUE _self) {
645
374
  * Returns the oneof descriptor for the oneof with the given name, if present,
646
375
  * or nil if none.
647
376
  */
648
- VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
649
- DEFINE_SELF(Descriptor, self, _self);
377
+ static VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
378
+ Descriptor* self = ruby_to_Descriptor(_self);
650
379
  const char* s = get_str(name);
651
380
  const upb_oneofdef* oneof = upb_msgdef_ntooz(self->msgdef, s);
652
381
  if (oneof == NULL) {
@@ -661,32 +390,62 @@ VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
661
390
  *
662
391
  * Returns the Ruby class created for this message type.
663
392
  */
664
- VALUE Descriptor_msgclass(VALUE _self) {
665
- DEFINE_SELF(Descriptor, self, _self);
393
+ static VALUE Descriptor_msgclass(VALUE _self) {
394
+ Descriptor* self = ruby_to_Descriptor(_self);
666
395
  if (self->klass == Qnil) {
667
396
  self->klass = build_class_from_descriptor(_self);
668
397
  }
669
398
  return self->klass;
670
399
  }
671
400
 
401
+ static void Descriptor_register(VALUE module) {
402
+ VALUE klass = rb_define_class_under(
403
+ module, "Descriptor", rb_cObject);
404
+ rb_define_alloc_func(klass, Descriptor_alloc);
405
+ rb_define_method(klass, "initialize", Descriptor_initialize, 3);
406
+ rb_define_method(klass, "each", Descriptor_each, 0);
407
+ rb_define_method(klass, "lookup", Descriptor_lookup, 1);
408
+ rb_define_method(klass, "each_oneof", Descriptor_each_oneof, 0);
409
+ rb_define_method(klass, "lookup_oneof", Descriptor_lookup_oneof, 1);
410
+ rb_define_method(klass, "msgclass", Descriptor_msgclass, 0);
411
+ rb_define_method(klass, "name", Descriptor_name, 0);
412
+ rb_define_method(klass, "file_descriptor", Descriptor_file_descriptor, 0);
413
+ rb_include_module(klass, rb_mEnumerable);
414
+ rb_gc_register_address(&cDescriptor);
415
+ cDescriptor = klass;
416
+ }
417
+
672
418
  // -----------------------------------------------------------------------------
673
419
  // FileDescriptor.
674
420
  // -----------------------------------------------------------------------------
675
421
 
676
- DEFINE_CLASS(FileDescriptor, "Google::Protobuf::FileDescriptor");
422
+ typedef struct {
423
+ const upb_filedef* filedef;
424
+ VALUE descriptor_pool; // Owns the upb_filedef.
425
+ } FileDescriptor;
677
426
 
678
- void FileDescriptor_mark(void* _self) {
427
+ static VALUE cFileDescriptor = Qnil;
428
+
429
+ static void FileDescriptor_mark(void* _self) {
679
430
  FileDescriptor* self = _self;
680
431
  rb_gc_mark(self->descriptor_pool);
681
432
  }
682
433
 
683
- void FileDescriptor_free(void* _self) {
684
- xfree(_self);
434
+ static const rb_data_type_t FileDescriptor_type = {
435
+ "Google::Protobuf::FileDescriptor",
436
+ {FileDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
437
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
438
+ };
439
+
440
+ static FileDescriptor* ruby_to_FileDescriptor(VALUE val) {
441
+ FileDescriptor* ret;
442
+ TypedData_Get_Struct(val, FileDescriptor, &FileDescriptor_type, ret);
443
+ return ret;
685
444
  }
686
445
 
687
- VALUE FileDescriptor_alloc(VALUE klass) {
446
+ static VALUE FileDescriptor_alloc(VALUE klass) {
688
447
  FileDescriptor* self = ALLOC(FileDescriptor);
689
- VALUE ret = TypedData_Wrap_Struct(klass, &_FileDescriptor_type, self);
448
+ VALUE ret = TypedData_Wrap_Struct(klass, &FileDescriptor_type, self);
690
449
  self->descriptor_pool = Qnil;
691
450
  self->filedef = NULL;
692
451
  return ret;
@@ -699,9 +458,9 @@ VALUE FileDescriptor_alloc(VALUE klass) {
699
458
  * Returns a new file descriptor. The syntax must be set before it's passed
700
459
  * to a builder.
701
460
  */
702
- VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
461
+ static VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
703
462
  VALUE descriptor_pool, VALUE ptr) {
704
- DEFINE_SELF(FileDescriptor, self, _self);
463
+ FileDescriptor* self = ruby_to_FileDescriptor(_self);
705
464
 
706
465
  if (cookie != c_only_cookie) {
707
466
  rb_raise(rb_eRuntimeError,
@@ -714,25 +473,14 @@ VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
714
473
  return Qnil;
715
474
  }
716
475
 
717
- void FileDescriptor_register(VALUE module) {
718
- VALUE klass = rb_define_class_under(
719
- module, "FileDescriptor", rb_cObject);
720
- rb_define_alloc_func(klass, FileDescriptor_alloc);
721
- rb_define_method(klass, "initialize", FileDescriptor_initialize, 3);
722
- rb_define_method(klass, "name", FileDescriptor_name, 0);
723
- rb_define_method(klass, "syntax", FileDescriptor_syntax, 0);
724
- rb_gc_register_address(&cFileDescriptor);
725
- cFileDescriptor = klass;
726
- }
727
-
728
476
  /*
729
477
  * call-seq:
730
478
  * FileDescriptor.name => name
731
479
  *
732
480
  * Returns the name of the file.
733
481
  */
734
- VALUE FileDescriptor_name(VALUE _self) {
735
- DEFINE_SELF(FileDescriptor, self, _self);
482
+ static VALUE FileDescriptor_name(VALUE _self) {
483
+ FileDescriptor* self = ruby_to_FileDescriptor(_self);
736
484
  const char* name = upb_filedef_name(self->filedef);
737
485
  return name == NULL ? Qnil : rb_str_new2(name);
738
486
  }
@@ -746,8 +494,8 @@ VALUE FileDescriptor_name(VALUE _self) {
746
494
  * Valid syntax versions are:
747
495
  * :proto2 or :proto3.
748
496
  */
749
- VALUE FileDescriptor_syntax(VALUE _self) {
750
- DEFINE_SELF(FileDescriptor, self, _self);
497
+ static VALUE FileDescriptor_syntax(VALUE _self) {
498
+ FileDescriptor* self = ruby_to_FileDescriptor(_self);
751
499
 
752
500
  switch (upb_filedef_syntax(self->filedef)) {
753
501
  case UPB_SYNTAX_PROTO3: return ID2SYM(rb_intern("proto3"));
@@ -756,19 +504,43 @@ VALUE FileDescriptor_syntax(VALUE _self) {
756
504
  }
757
505
  }
758
506
 
507
+ static void FileDescriptor_register(VALUE module) {
508
+ VALUE klass = rb_define_class_under(
509
+ module, "FileDescriptor", rb_cObject);
510
+ rb_define_alloc_func(klass, FileDescriptor_alloc);
511
+ rb_define_method(klass, "initialize", FileDescriptor_initialize, 3);
512
+ rb_define_method(klass, "name", FileDescriptor_name, 0);
513
+ rb_define_method(klass, "syntax", FileDescriptor_syntax, 0);
514
+ rb_gc_register_address(&cFileDescriptor);
515
+ cFileDescriptor = klass;
516
+ }
517
+
759
518
  // -----------------------------------------------------------------------------
760
519
  // FieldDescriptor.
761
520
  // -----------------------------------------------------------------------------
762
521
 
763
- DEFINE_CLASS(FieldDescriptor, "Google::Protobuf::FieldDescriptor");
522
+ typedef struct {
523
+ const upb_fielddef* fielddef;
524
+ VALUE descriptor_pool; // Owns the upb_fielddef.
525
+ } FieldDescriptor;
526
+
527
+ static VALUE cFieldDescriptor = Qnil;
764
528
 
765
- void FieldDescriptor_mark(void* _self) {
529
+ static void FieldDescriptor_mark(void* _self) {
766
530
  FieldDescriptor* self = _self;
767
531
  rb_gc_mark(self->descriptor_pool);
768
532
  }
769
533
 
770
- void FieldDescriptor_free(void* _self) {
771
- xfree(_self);
534
+ static const rb_data_type_t FieldDescriptor_type = {
535
+ "Google::Protobuf::FieldDescriptor",
536
+ {FieldDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
537
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
538
+ };
539
+
540
+ static FieldDescriptor* ruby_to_FieldDescriptor(VALUE val) {
541
+ FieldDescriptor* ret;
542
+ TypedData_Get_Struct(val, FieldDescriptor, &FieldDescriptor_type, ret);
543
+ return ret;
772
544
  }
773
545
 
774
546
  /*
@@ -778,42 +550,22 @@ void FieldDescriptor_free(void* _self) {
778
550
  * Returns a new field descriptor. Its name, type, etc. must be set before it is
779
551
  * added to a message type.
780
552
  */
781
- VALUE FieldDescriptor_alloc(VALUE klass) {
553
+ static VALUE FieldDescriptor_alloc(VALUE klass) {
782
554
  FieldDescriptor* self = ALLOC(FieldDescriptor);
783
- VALUE ret = TypedData_Wrap_Struct(klass, &_FieldDescriptor_type, self);
555
+ VALUE ret = TypedData_Wrap_Struct(klass, &FieldDescriptor_type, self);
784
556
  self->fielddef = NULL;
785
557
  return ret;
786
558
  }
787
559
 
788
- void FieldDescriptor_register(VALUE module) {
789
- VALUE klass = rb_define_class_under(
790
- module, "FieldDescriptor", rb_cObject);
791
- rb_define_alloc_func(klass, FieldDescriptor_alloc);
792
- rb_define_method(klass, "initialize", FieldDescriptor_initialize, 3);
793
- rb_define_method(klass, "name", FieldDescriptor_name, 0);
794
- rb_define_method(klass, "type", FieldDescriptor_type, 0);
795
- rb_define_method(klass, "default", FieldDescriptor_default, 0);
796
- rb_define_method(klass, "label", FieldDescriptor_label, 0);
797
- rb_define_method(klass, "number", FieldDescriptor_number, 0);
798
- rb_define_method(klass, "submsg_name", FieldDescriptor_submsg_name, 0);
799
- rb_define_method(klass, "subtype", FieldDescriptor_subtype, 0);
800
- rb_define_method(klass, "has?", FieldDescriptor_has, 1);
801
- rb_define_method(klass, "clear", FieldDescriptor_clear, 1);
802
- rb_define_method(klass, "get", FieldDescriptor_get, 1);
803
- rb_define_method(klass, "set", FieldDescriptor_set, 2);
804
- rb_gc_register_address(&cFieldDescriptor);
805
- cFieldDescriptor = klass;
806
- }
807
-
808
560
  /*
809
561
  * call-seq:
810
562
  * EnumDescriptor.new(c_only_cookie, pool, ptr) => EnumDescriptor
811
563
  *
812
564
  * Creates a descriptor wrapper object. May only be called from C.
813
565
  */
814
- VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
815
- VALUE descriptor_pool, VALUE ptr) {
816
- DEFINE_SELF(FieldDescriptor, self, _self);
566
+ static VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
567
+ VALUE descriptor_pool, VALUE ptr) {
568
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
817
569
 
818
570
  if (cookie != c_only_cookie) {
819
571
  rb_raise(rb_eRuntimeError,
@@ -832,11 +584,12 @@ VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
832
584
  *
833
585
  * Returns the name of this field.
834
586
  */
835
- VALUE FieldDescriptor_name(VALUE _self) {
836
- DEFINE_SELF(FieldDescriptor, self, _self);
587
+ static VALUE FieldDescriptor_name(VALUE _self) {
588
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
837
589
  return rb_str_maybe_null(upb_fielddef_name(self->fielddef));
838
590
  }
839
591
 
592
+ // Non-static, exposed to other .c files.
840
593
  upb_fieldtype_t ruby_to_fieldtype(VALUE type) {
841
594
  if (TYPE(type) != T_SYMBOL) {
842
595
  rb_raise(rb_eArgError, "Expected symbol for field type.");
@@ -865,62 +618,7 @@ upb_fieldtype_t ruby_to_fieldtype(VALUE type) {
865
618
  return 0;
866
619
  }
867
620
 
868
- VALUE fieldtype_to_ruby(upb_fieldtype_t type) {
869
- switch (type) {
870
- #define CONVERT(upb, ruby) \
871
- case UPB_TYPE_ ## upb : return ID2SYM(rb_intern( # ruby ));
872
- CONVERT(FLOAT, float);
873
- CONVERT(DOUBLE, double);
874
- CONVERT(BOOL, bool);
875
- CONVERT(STRING, string);
876
- CONVERT(BYTES, bytes);
877
- CONVERT(MESSAGE, message);
878
- CONVERT(ENUM, enum);
879
- CONVERT(INT32, int32);
880
- CONVERT(INT64, int64);
881
- CONVERT(UINT32, uint32);
882
- CONVERT(UINT64, uint64);
883
- #undef CONVERT
884
- }
885
- return Qnil;
886
- }
887
-
888
- upb_descriptortype_t ruby_to_descriptortype(VALUE type) {
889
- if (TYPE(type) != T_SYMBOL) {
890
- rb_raise(rb_eArgError, "Expected symbol for field type.");
891
- }
892
-
893
- #define CONVERT(upb, ruby) \
894
- if (SYM2ID(type) == rb_intern( # ruby )) { \
895
- return UPB_DESCRIPTOR_TYPE_ ## upb; \
896
- }
897
-
898
- CONVERT(FLOAT, float);
899
- CONVERT(DOUBLE, double);
900
- CONVERT(BOOL, bool);
901
- CONVERT(STRING, string);
902
- CONVERT(BYTES, bytes);
903
- CONVERT(MESSAGE, message);
904
- CONVERT(GROUP, group);
905
- CONVERT(ENUM, enum);
906
- CONVERT(INT32, int32);
907
- CONVERT(INT64, int64);
908
- CONVERT(UINT32, uint32);
909
- CONVERT(UINT64, uint64);
910
- CONVERT(SINT32, sint32);
911
- CONVERT(SINT64, sint64);
912
- CONVERT(FIXED32, fixed32);
913
- CONVERT(FIXED64, fixed64);
914
- CONVERT(SFIXED32, sfixed32);
915
- CONVERT(SFIXED64, sfixed64);
916
-
917
- #undef CONVERT
918
-
919
- rb_raise(rb_eArgError, "Unknown field type.");
920
- return 0;
921
- }
922
-
923
- VALUE descriptortype_to_ruby(upb_descriptortype_t type) {
621
+ static VALUE descriptortype_to_ruby(upb_descriptortype_t type) {
924
622
  switch (type) {
925
623
  #define CONVERT(upb, ruby) \
926
624
  case UPB_DESCRIPTOR_TYPE_ ## upb : return ID2SYM(rb_intern( # ruby ));
@@ -947,29 +645,6 @@ VALUE descriptortype_to_ruby(upb_descriptortype_t type) {
947
645
  return Qnil;
948
646
  }
949
647
 
950
- VALUE ruby_to_label(VALUE label) {
951
- upb_label_t upb_label;
952
- bool converted = false;
953
-
954
- #define CONVERT(upb, ruby) \
955
- if (SYM2ID(label) == rb_intern( # ruby )) { \
956
- upb_label = UPB_LABEL_ ## upb; \
957
- converted = true; \
958
- }
959
-
960
- CONVERT(OPTIONAL, optional);
961
- CONVERT(REQUIRED, required);
962
- CONVERT(REPEATED, repeated);
963
-
964
- #undef CONVERT
965
-
966
- if (!converted) {
967
- rb_raise(rb_eArgError, "Unknown field label.");
968
- }
969
-
970
- return upb_label;
971
- }
972
-
973
648
  /*
974
649
  * call-seq:
975
650
  * FieldDescriptor.type => type
@@ -980,8 +655,8 @@ VALUE ruby_to_label(VALUE label) {
980
655
  * :int32, :int64, :uint32, :uint64, :float, :double, :bool, :string,
981
656
  * :bytes, :message.
982
657
  */
983
- VALUE FieldDescriptor_type(VALUE _self) {
984
- DEFINE_SELF(FieldDescriptor, self, _self);
658
+ static VALUE FieldDescriptor__type(VALUE _self) {
659
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
985
660
  return descriptortype_to_ruby(upb_fielddef_descriptortype(self->fielddef));
986
661
  }
987
662
 
@@ -991,9 +666,30 @@ VALUE FieldDescriptor_type(VALUE _self) {
991
666
  *
992
667
  * Returns this field's default, as a Ruby object, or nil if not yet set.
993
668
  */
994
- VALUE FieldDescriptor_default(VALUE _self) {
995
- DEFINE_SELF(FieldDescriptor, self, _self);
996
- return layout_get_default(self->fielddef);
669
+ static VALUE FieldDescriptor_default(VALUE _self) {
670
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
671
+ const upb_fielddef *f = self->fielddef;
672
+ upb_msgval default_val = {0};
673
+ if (upb_fielddef_issubmsg(f)) {
674
+ return Qnil;
675
+ } else if (!upb_fielddef_isseq(f)) {
676
+ default_val = upb_fielddef_default(f);
677
+ }
678
+ return Convert_UpbToRuby(default_val, TypeInfo_get(self->fielddef), Qnil);
679
+ }
680
+
681
+
682
+ /*
683
+ * call-seq:
684
+ * FieldDescriptor.json_name => json_name
685
+ *
686
+ * Returns this field's json_name, as a Ruby string, or nil if not yet set.
687
+ */
688
+ static VALUE FieldDescriptor_json_name(VALUE _self) {
689
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
690
+ const upb_fielddef *f = self->fielddef;
691
+ const char *json_name = upb_fielddef_jsonname(f);
692
+ return rb_str_new2(json_name);
997
693
  }
998
694
 
999
695
  /*
@@ -1005,8 +701,8 @@ VALUE FieldDescriptor_default(VALUE _self) {
1005
701
  * Valid field labels are:
1006
702
  * :optional, :repeated
1007
703
  */
1008
- VALUE FieldDescriptor_label(VALUE _self) {
1009
- DEFINE_SELF(FieldDescriptor, self, _self);
704
+ static VALUE FieldDescriptor_label(VALUE _self) {
705
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
1010
706
  switch (upb_fielddef_label(self->fielddef)) {
1011
707
  #define CONVERT(upb, ruby) \
1012
708
  case UPB_LABEL_ ## upb : return ID2SYM(rb_intern( # ruby ));
@@ -1027,8 +723,8 @@ VALUE FieldDescriptor_label(VALUE _self) {
1027
723
  *
1028
724
  * Returns the tag number for this field.
1029
725
  */
1030
- VALUE FieldDescriptor_number(VALUE _self) {
1031
- DEFINE_SELF(FieldDescriptor, self, _self);
726
+ static VALUE FieldDescriptor_number(VALUE _self) {
727
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
1032
728
  return INT2NUM(upb_fielddef_number(self->fielddef));
1033
729
  }
1034
730
 
@@ -1041,8 +737,8 @@ VALUE FieldDescriptor_number(VALUE _self) {
1041
737
  * name will be resolved within the context of the pool to which the containing
1042
738
  * message type is added.
1043
739
  */
1044
- VALUE FieldDescriptor_submsg_name(VALUE _self) {
1045
- DEFINE_SELF(FieldDescriptor, self, _self);
740
+ static VALUE FieldDescriptor_submsg_name(VALUE _self) {
741
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
1046
742
  switch (upb_fielddef_type(self->fielddef)) {
1047
743
  case UPB_TYPE_ENUM:
1048
744
  return rb_str_new2(
@@ -1064,8 +760,8 @@ VALUE FieldDescriptor_submsg_name(VALUE _self) {
1064
760
  * called *until* the containing message type is added to a pool (and thus
1065
761
  * resolved).
1066
762
  */
1067
- VALUE FieldDescriptor_subtype(VALUE _self) {
1068
- DEFINE_SELF(FieldDescriptor, self, _self);
763
+ static VALUE FieldDescriptor_subtype(VALUE _self) {
764
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
1069
765
  switch (upb_fielddef_type(self->fielddef)) {
1070
766
  case UPB_TYPE_ENUM:
1071
767
  return get_enumdef_obj(self->descriptor_pool,
@@ -1085,14 +781,17 @@ VALUE FieldDescriptor_subtype(VALUE _self) {
1085
781
  * Returns the value set for this field on the given message. Raises an
1086
782
  * exception if message is of the wrong type.
1087
783
  */
1088
- VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
1089
- DEFINE_SELF(FieldDescriptor, self, _self);
1090
- MessageHeader* msg;
1091
- TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
1092
- if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
784
+ static VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
785
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
786
+ const upb_msgdef *m;
787
+
788
+ Message_Get(msg_rb, &m);
789
+
790
+ if (m != upb_fielddef_containingtype(self->fielddef)) {
1093
791
  rb_raise(cTypeError, "get method called on wrong message type");
1094
792
  }
1095
- return layout_get(msg->descriptor->layout, Message_data(msg), self->fielddef);
793
+
794
+ return Message_getfield(msg_rb, self->fielddef);
1096
795
  }
1097
796
 
1098
797
  /*
@@ -1100,19 +799,20 @@ VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
1100
799
  * FieldDescriptor.has?(message) => boolean
1101
800
  *
1102
801
  * Returns whether the value is set on the given message. Raises an
1103
- * exception when calling with proto syntax 3.
802
+ * exception when calling for fields that do not have presence.
1104
803
  */
1105
- VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
1106
- DEFINE_SELF(FieldDescriptor, self, _self);
1107
- MessageHeader* msg;
1108
- TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
1109
- if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
804
+ static VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
805
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
806
+ const upb_msgdef *m;
807
+ const upb_msgdef *msg = Message_Get(msg_rb, &m);
808
+
809
+ if (m != upb_fielddef_containingtype(self->fielddef)) {
1110
810
  rb_raise(cTypeError, "has method called on wrong message type");
1111
811
  } else if (!upb_fielddef_haspresence(self->fielddef)) {
1112
812
  rb_raise(rb_eArgError, "does not track presence");
1113
813
  }
1114
814
 
1115
- return layout_has(msg->descriptor->layout, Message_data(msg), self->fielddef);
815
+ return upb_msg_has(msg, self->fielddef) ? Qtrue : Qfalse;
1116
816
  }
1117
817
 
1118
818
  /*
@@ -1121,15 +821,16 @@ VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
1121
821
  *
1122
822
  * Clears the field from the message if it's set.
1123
823
  */
1124
- VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {
1125
- DEFINE_SELF(FieldDescriptor, self, _self);
1126
- MessageHeader* msg;
1127
- TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
1128
- if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
824
+ static VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {
825
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
826
+ const upb_msgdef *m;
827
+ upb_msgdef *msg = Message_GetMutable(msg_rb, &m);
828
+
829
+ if (m != upb_fielddef_containingtype(self->fielddef)) {
1129
830
  rb_raise(cTypeError, "has method called on wrong message type");
1130
831
  }
1131
832
 
1132
- layout_clear(msg->descriptor->layout, Message_data(msg), self->fielddef);
833
+ upb_msg_clearfield(msg, self->fielddef);
1133
834
  return Qnil;
1134
835
  }
1135
836
 
@@ -1141,30 +842,70 @@ VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {
1141
842
  * message. Raises an exception if message is of the wrong type. Performs the
1142
843
  * ordinary type-checks for field setting.
1143
844
  */
1144
- VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value) {
1145
- DEFINE_SELF(FieldDescriptor, self, _self);
1146
- MessageHeader* msg;
1147
- TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
1148
- if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
845
+ static VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value) {
846
+ FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
847
+ const upb_msgdef *m;
848
+ upb_msgdef *msg = Message_GetMutable(msg_rb, &m);
849
+ upb_arena *arena = Arena_get(Message_GetArena(msg_rb));
850
+ upb_msgval msgval;
851
+
852
+ if (m != upb_fielddef_containingtype(self->fielddef)) {
1149
853
  rb_raise(cTypeError, "set method called on wrong message type");
1150
854
  }
1151
- layout_set(msg->descriptor->layout, Message_data(msg), self->fielddef, value);
855
+
856
+ msgval = Convert_RubyToUpb(value, upb_fielddef_name(self->fielddef),
857
+ TypeInfo_get(self->fielddef), arena);
858
+ upb_msg_set(msg, self->fielddef, msgval, arena);
1152
859
  return Qnil;
1153
860
  }
1154
861
 
862
+ static void FieldDescriptor_register(VALUE module) {
863
+ VALUE klass = rb_define_class_under(
864
+ module, "FieldDescriptor", rb_cObject);
865
+ rb_define_alloc_func(klass, FieldDescriptor_alloc);
866
+ rb_define_method(klass, "initialize", FieldDescriptor_initialize, 3);
867
+ rb_define_method(klass, "name", FieldDescriptor_name, 0);
868
+ rb_define_method(klass, "type", FieldDescriptor__type, 0);
869
+ rb_define_method(klass, "default", FieldDescriptor_default, 0);
870
+ rb_define_method(klass, "json_name", FieldDescriptor_json_name, 0);
871
+ rb_define_method(klass, "label", FieldDescriptor_label, 0);
872
+ rb_define_method(klass, "number", FieldDescriptor_number, 0);
873
+ rb_define_method(klass, "submsg_name", FieldDescriptor_submsg_name, 0);
874
+ rb_define_method(klass, "subtype", FieldDescriptor_subtype, 0);
875
+ rb_define_method(klass, "has?", FieldDescriptor_has, 1);
876
+ rb_define_method(klass, "clear", FieldDescriptor_clear, 1);
877
+ rb_define_method(klass, "get", FieldDescriptor_get, 1);
878
+ rb_define_method(klass, "set", FieldDescriptor_set, 2);
879
+ rb_gc_register_address(&cFieldDescriptor);
880
+ cFieldDescriptor = klass;
881
+ }
882
+
1155
883
  // -----------------------------------------------------------------------------
1156
884
  // OneofDescriptor.
1157
885
  // -----------------------------------------------------------------------------
1158
886
 
1159
- DEFINE_CLASS(OneofDescriptor, "Google::Protobuf::OneofDescriptor");
887
+ typedef struct {
888
+ const upb_oneofdef* oneofdef;
889
+ VALUE descriptor_pool; // Owns the upb_oneofdef.
890
+ } OneofDescriptor;
891
+
892
+ static VALUE cOneofDescriptor = Qnil;
1160
893
 
1161
- void OneofDescriptor_mark(void* _self) {
894
+ static void OneofDescriptor_mark(void* _self) {
1162
895
  OneofDescriptor* self = _self;
1163
896
  rb_gc_mark(self->descriptor_pool);
1164
897
  }
1165
898
 
1166
- void OneofDescriptor_free(void* _self) {
1167
- xfree(_self);
899
+ static const rb_data_type_t OneofDescriptor_type = {
900
+ "Google::Protobuf::OneofDescriptor",
901
+ {OneofDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
902
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
903
+ };
904
+
905
+ static OneofDescriptor* ruby_to_OneofDescriptor(VALUE val) {
906
+ OneofDescriptor* ret;
907
+ TypedData_Get_Struct(val, OneofDescriptor, &OneofDescriptor_type, ret);
908
+ return ret;
1168
909
  }
1169
910
 
1170
911
  /*
@@ -1174,35 +915,23 @@ void OneofDescriptor_free(void* _self) {
1174
915
  * Creates a new, empty, oneof descriptor. The oneof may only be modified prior
1175
916
  * to being added to a message descriptor which is subsequently added to a pool.
1176
917
  */
1177
- VALUE OneofDescriptor_alloc(VALUE klass) {
918
+ static VALUE OneofDescriptor_alloc(VALUE klass) {
1178
919
  OneofDescriptor* self = ALLOC(OneofDescriptor);
1179
- VALUE ret = TypedData_Wrap_Struct(klass, &_OneofDescriptor_type, self);
920
+ VALUE ret = TypedData_Wrap_Struct(klass, &OneofDescriptor_type, self);
1180
921
  self->oneofdef = NULL;
1181
922
  self->descriptor_pool = Qnil;
1182
923
  return ret;
1183
924
  }
1184
925
 
1185
- void OneofDescriptor_register(VALUE module) {
1186
- VALUE klass = rb_define_class_under(
1187
- module, "OneofDescriptor", rb_cObject);
1188
- rb_define_alloc_func(klass, OneofDescriptor_alloc);
1189
- rb_define_method(klass, "initialize", OneofDescriptor_initialize, 3);
1190
- rb_define_method(klass, "name", OneofDescriptor_name, 0);
1191
- rb_define_method(klass, "each", OneofDescriptor_each, 0);
1192
- rb_include_module(klass, rb_mEnumerable);
1193
- rb_gc_register_address(&cOneofDescriptor);
1194
- cOneofDescriptor = klass;
1195
- }
1196
-
1197
926
  /*
1198
927
  * call-seq:
1199
928
  * OneofDescriptor.new(c_only_cookie, pool, ptr) => OneofDescriptor
1200
929
  *
1201
930
  * Creates a descriptor wrapper object. May only be called from C.
1202
931
  */
1203
- VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
932
+ static VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
1204
933
  VALUE descriptor_pool, VALUE ptr) {
1205
- DEFINE_SELF(OneofDescriptor, self, _self);
934
+ OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
1206
935
 
1207
936
  if (cookie != c_only_cookie) {
1208
937
  rb_raise(rb_eRuntimeError,
@@ -1221,8 +950,8 @@ VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
1221
950
  *
1222
951
  * Returns the name of this oneof.
1223
952
  */
1224
- VALUE OneofDescriptor_name(VALUE _self) {
1225
- DEFINE_SELF(OneofDescriptor, self, _self);
953
+ static VALUE OneofDescriptor_name(VALUE _self) {
954
+ OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
1226
955
  return rb_str_maybe_null(upb_oneofdef_name(self->oneofdef));
1227
956
  }
1228
957
 
@@ -1232,8 +961,8 @@ VALUE OneofDescriptor_name(VALUE _self) {
1232
961
  *
1233
962
  * Iterates through fields in this oneof, yielding to the block on each one.
1234
963
  */
1235
- VALUE OneofDescriptor_each(VALUE _self) {
1236
- DEFINE_SELF(OneofDescriptor, self, _self);
964
+ static VALUE OneofDescriptor_each(VALUE _self) {
965
+ OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
1237
966
  upb_oneof_iter it;
1238
967
  for (upb_oneof_begin(&it, self->oneofdef);
1239
968
  !upb_oneof_done(&it);
@@ -1245,40 +974,72 @@ VALUE OneofDescriptor_each(VALUE _self) {
1245
974
  return Qnil;
1246
975
  }
1247
976
 
977
+ static void OneofDescriptor_register(VALUE module) {
978
+ VALUE klass = rb_define_class_under(
979
+ module, "OneofDescriptor", rb_cObject);
980
+ rb_define_alloc_func(klass, OneofDescriptor_alloc);
981
+ rb_define_method(klass, "initialize", OneofDescriptor_initialize, 3);
982
+ rb_define_method(klass, "name", OneofDescriptor_name, 0);
983
+ rb_define_method(klass, "each", OneofDescriptor_each, 0);
984
+ rb_include_module(klass, rb_mEnumerable);
985
+ rb_gc_register_address(&cOneofDescriptor);
986
+ cOneofDescriptor = klass;
987
+ }
988
+
1248
989
  // -----------------------------------------------------------------------------
1249
990
  // EnumDescriptor.
1250
991
  // -----------------------------------------------------------------------------
1251
992
 
1252
- DEFINE_CLASS(EnumDescriptor, "Google::Protobuf::EnumDescriptor");
993
+ typedef struct {
994
+ const upb_enumdef* enumdef;
995
+ VALUE module; // begins as nil
996
+ VALUE descriptor_pool; // Owns the upb_enumdef.
997
+ } EnumDescriptor;
998
+
999
+ static VALUE cEnumDescriptor = Qnil;
1253
1000
 
1254
- void EnumDescriptor_mark(void* _self) {
1001
+ static void EnumDescriptor_mark(void* _self) {
1255
1002
  EnumDescriptor* self = _self;
1256
1003
  rb_gc_mark(self->module);
1257
1004
  rb_gc_mark(self->descriptor_pool);
1258
1005
  }
1259
1006
 
1260
- void EnumDescriptor_free(void* _self) {
1261
- xfree(_self);
1007
+ static const rb_data_type_t EnumDescriptor_type = {
1008
+ "Google::Protobuf::EnumDescriptor",
1009
+ {EnumDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
1010
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
1011
+ };
1012
+
1013
+ static EnumDescriptor* ruby_to_EnumDescriptor(VALUE val) {
1014
+ EnumDescriptor* ret;
1015
+ TypedData_Get_Struct(val, EnumDescriptor, &EnumDescriptor_type, ret);
1016
+ return ret;
1262
1017
  }
1263
1018
 
1264
- VALUE EnumDescriptor_alloc(VALUE klass) {
1019
+ static VALUE EnumDescriptor_alloc(VALUE klass) {
1265
1020
  EnumDescriptor* self = ALLOC(EnumDescriptor);
1266
- VALUE ret = TypedData_Wrap_Struct(klass, &_EnumDescriptor_type, self);
1021
+ VALUE ret = TypedData_Wrap_Struct(klass, &EnumDescriptor_type, self);
1267
1022
  self->enumdef = NULL;
1268
1023
  self->module = Qnil;
1269
1024
  self->descriptor_pool = Qnil;
1270
1025
  return ret;
1271
1026
  }
1272
1027
 
1028
+ // Exposed to other modules in defs.h.
1029
+ const upb_enumdef *EnumDescriptor_GetEnumDef(VALUE enum_desc_rb) {
1030
+ EnumDescriptor *desc = ruby_to_EnumDescriptor(enum_desc_rb);
1031
+ return desc->enumdef;
1032
+ }
1033
+
1273
1034
  /*
1274
1035
  * call-seq:
1275
1036
  * EnumDescriptor.new(c_only_cookie, ptr) => EnumDescriptor
1276
1037
  *
1277
1038
  * Creates a descriptor wrapper object. May only be called from C.
1278
1039
  */
1279
- VALUE EnumDescriptor_initialize(VALUE _self, VALUE cookie,
1280
- VALUE descriptor_pool, VALUE ptr) {
1281
- DEFINE_SELF(EnumDescriptor, self, _self);
1040
+ static VALUE EnumDescriptor_initialize(VALUE _self, VALUE cookie,
1041
+ VALUE descriptor_pool, VALUE ptr) {
1042
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1282
1043
 
1283
1044
  if (cookie != c_only_cookie) {
1284
1045
  rb_raise(rb_eRuntimeError,
@@ -1291,30 +1052,14 @@ VALUE EnumDescriptor_initialize(VALUE _self, VALUE cookie,
1291
1052
  return Qnil;
1292
1053
  }
1293
1054
 
1294
- void EnumDescriptor_register(VALUE module) {
1295
- VALUE klass = rb_define_class_under(
1296
- module, "EnumDescriptor", rb_cObject);
1297
- rb_define_alloc_func(klass, EnumDescriptor_alloc);
1298
- rb_define_method(klass, "initialize", EnumDescriptor_initialize, 3);
1299
- rb_define_method(klass, "name", EnumDescriptor_name, 0);
1300
- rb_define_method(klass, "lookup_name", EnumDescriptor_lookup_name, 1);
1301
- rb_define_method(klass, "lookup_value", EnumDescriptor_lookup_value, 1);
1302
- rb_define_method(klass, "each", EnumDescriptor_each, 0);
1303
- rb_define_method(klass, "enummodule", EnumDescriptor_enummodule, 0);
1304
- rb_define_method(klass, "file_descriptor", EnumDescriptor_file_descriptor, 0);
1305
- rb_include_module(klass, rb_mEnumerable);
1306
- rb_gc_register_address(&cEnumDescriptor);
1307
- cEnumDescriptor = klass;
1308
- }
1309
-
1310
1055
  /*
1311
1056
  * call-seq:
1312
1057
  * EnumDescriptor.file_descriptor
1313
1058
  *
1314
1059
  * Returns the FileDescriptor object this enum belongs to.
1315
1060
  */
1316
- VALUE EnumDescriptor_file_descriptor(VALUE _self) {
1317
- DEFINE_SELF(EnumDescriptor, self, _self);
1061
+ static VALUE EnumDescriptor_file_descriptor(VALUE _self) {
1062
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1318
1063
  return get_filedef_obj(self->descriptor_pool,
1319
1064
  upb_enumdef_file(self->enumdef));
1320
1065
  }
@@ -1325,8 +1070,8 @@ VALUE EnumDescriptor_file_descriptor(VALUE _self) {
1325
1070
  *
1326
1071
  * Returns the name of this enum type.
1327
1072
  */
1328
- VALUE EnumDescriptor_name(VALUE _self) {
1329
- DEFINE_SELF(EnumDescriptor, self, _self);
1073
+ static VALUE EnumDescriptor_name(VALUE _self) {
1074
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1330
1075
  return rb_str_maybe_null(upb_enumdef_fullname(self->enumdef));
1331
1076
  }
1332
1077
 
@@ -1337,8 +1082,8 @@ VALUE EnumDescriptor_name(VALUE _self) {
1337
1082
  * Returns the numeric value corresponding to the given key name (as a Ruby
1338
1083
  * symbol), or nil if none.
1339
1084
  */
1340
- VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
1341
- DEFINE_SELF(EnumDescriptor, self, _self);
1085
+ static VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
1086
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1342
1087
  const char* name_str= rb_id2name(SYM2ID(name));
1343
1088
  int32_t val = 0;
1344
1089
  if (upb_enumdef_ntoiz(self->enumdef, name_str, &val)) {
@@ -1355,8 +1100,8 @@ VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
1355
1100
  * Returns the key name (as a Ruby symbol) corresponding to the integer value,
1356
1101
  * or nil if none.
1357
1102
  */
1358
- VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
1359
- DEFINE_SELF(EnumDescriptor, self, _self);
1103
+ static VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
1104
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1360
1105
  int32_t val = NUM2INT(number);
1361
1106
  const char* name = upb_enumdef_iton(self->enumdef, val);
1362
1107
  if (name != NULL) {
@@ -1373,8 +1118,8 @@ VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
1373
1118
  * Iterates over key => value mappings in this enum's definition, yielding to
1374
1119
  * the block with (key, value) arguments for each one.
1375
1120
  */
1376
- VALUE EnumDescriptor_each(VALUE _self) {
1377
- DEFINE_SELF(EnumDescriptor, self, _self);
1121
+ static VALUE EnumDescriptor_each(VALUE _self) {
1122
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1378
1123
 
1379
1124
  upb_enum_iter it;
1380
1125
  for (upb_enum_begin(&it, self->enumdef);
@@ -1394,885 +1139,146 @@ VALUE EnumDescriptor_each(VALUE _self) {
1394
1139
  *
1395
1140
  * Returns the Ruby module corresponding to this enum type.
1396
1141
  */
1397
- VALUE EnumDescriptor_enummodule(VALUE _self) {
1398
- DEFINE_SELF(EnumDescriptor, self, _self);
1142
+ static VALUE EnumDescriptor_enummodule(VALUE _self) {
1143
+ EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1399
1144
  if (self->module == Qnil) {
1400
1145
  self->module = build_module_from_enumdesc(_self);
1401
1146
  }
1402
1147
  return self->module;
1403
1148
  }
1404
1149
 
1405
- // -----------------------------------------------------------------------------
1406
- // MessageBuilderContext.
1407
- // -----------------------------------------------------------------------------
1408
-
1409
- DEFINE_CLASS(MessageBuilderContext,
1410
- "Google::Protobuf::Internal::MessageBuilderContext");
1411
-
1412
- void MessageBuilderContext_mark(void* _self) {
1413
- MessageBuilderContext* self = _self;
1414
- rb_gc_mark(self->file_builder);
1415
- }
1416
-
1417
- void MessageBuilderContext_free(void* _self) {
1418
- MessageBuilderContext* self = _self;
1419
- xfree(self);
1420
- }
1421
-
1422
- VALUE MessageBuilderContext_alloc(VALUE klass) {
1423
- MessageBuilderContext* self = ALLOC(MessageBuilderContext);
1424
- VALUE ret = TypedData_Wrap_Struct(
1425
- klass, &_MessageBuilderContext_type, self);
1426
- self->file_builder = Qnil;
1427
- return ret;
1428
- }
1429
-
1430
- void MessageBuilderContext_register(VALUE module) {
1150
+ static void EnumDescriptor_register(VALUE module) {
1431
1151
  VALUE klass = rb_define_class_under(
1432
- module, "MessageBuilderContext", rb_cObject);
1433
- rb_define_alloc_func(klass, MessageBuilderContext_alloc);
1434
- rb_define_method(klass, "initialize",
1435
- MessageBuilderContext_initialize, 2);
1436
- rb_define_method(klass, "optional", MessageBuilderContext_optional, -1);
1437
- rb_define_method(klass, "required", MessageBuilderContext_required, -1);
1438
- rb_define_method(klass, "repeated", MessageBuilderContext_repeated, -1);
1439
- rb_define_method(klass, "map", MessageBuilderContext_map, -1);
1440
- rb_define_method(klass, "oneof", MessageBuilderContext_oneof, 1);
1441
- rb_gc_register_address(&cMessageBuilderContext);
1442
- cMessageBuilderContext = klass;
1443
- }
1444
-
1445
- /*
1446
- * call-seq:
1447
- * MessageBuilderContext.new(file_builder, name) => context
1448
- *
1449
- * Create a new message builder context around the given message descriptor and
1450
- * builder context. This class is intended to serve as a DSL context to be used
1451
- * with #instance_eval.
1452
- */
1453
- VALUE MessageBuilderContext_initialize(VALUE _self,
1454
- VALUE _file_builder,
1455
- VALUE name) {
1456
- DEFINE_SELF(MessageBuilderContext, self, _self);
1457
- FileBuilderContext* file_builder = ruby_to_FileBuilderContext(_file_builder);
1458
- google_protobuf_FileDescriptorProto* file_proto = file_builder->file_proto;
1459
-
1460
- self->file_builder = _file_builder;
1461
- self->msg_proto = google_protobuf_FileDescriptorProto_add_message_type(
1462
- file_proto, file_builder->arena);
1463
-
1464
- google_protobuf_DescriptorProto_set_name(
1465
- self->msg_proto, FileBuilderContext_strdup(_file_builder, name));
1466
-
1467
- return Qnil;
1468
- }
1469
-
1470
- static void msgdef_add_field(VALUE msgbuilder_rb, upb_label_t label, VALUE name,
1471
- VALUE type, VALUE number, VALUE type_class,
1472
- VALUE options, int oneof_index) {
1473
- DEFINE_SELF(MessageBuilderContext, self, msgbuilder_rb);
1474
- FileBuilderContext* file_context =
1475
- ruby_to_FileBuilderContext(self->file_builder);
1476
- google_protobuf_FieldDescriptorProto* field_proto;
1477
- VALUE name_str;
1478
-
1479
- field_proto = google_protobuf_DescriptorProto_add_field(self->msg_proto,
1480
- file_context->arena);
1481
-
1482
- Check_Type(name, T_SYMBOL);
1483
- name_str = rb_id2str(SYM2ID(name));
1484
-
1485
- google_protobuf_FieldDescriptorProto_set_name(
1486
- field_proto, FileBuilderContext_strdup(self->file_builder, name_str));
1487
- google_protobuf_FieldDescriptorProto_set_number(field_proto, NUM2INT(number));
1488
- google_protobuf_FieldDescriptorProto_set_label(field_proto, (int)label);
1489
- google_protobuf_FieldDescriptorProto_set_type(
1490
- field_proto, (int)ruby_to_descriptortype(type));
1491
-
1492
- if (type_class != Qnil) {
1493
- Check_Type(type_class, T_STRING);
1494
-
1495
- // Make it an absolute type name by prepending a dot.
1496
- type_class = rb_str_append(rb_str_new2("."), type_class);
1497
- google_protobuf_FieldDescriptorProto_set_type_name(
1498
- field_proto, FileBuilderContext_strdup(self->file_builder, type_class));
1499
- }
1500
-
1501
- if (options != Qnil) {
1502
- Check_Type(options, T_HASH);
1503
-
1504
- if (rb_funcall(options, rb_intern("key?"), 1,
1505
- ID2SYM(rb_intern("default"))) == Qtrue) {
1506
- VALUE default_value =
1507
- rb_hash_lookup(options, ID2SYM(rb_intern("default")));
1508
-
1509
- /* Call #to_s since all defaults are strings in the descriptor. */
1510
- default_value = rb_funcall(default_value, rb_intern("to_s"), 0);
1511
-
1512
- google_protobuf_FieldDescriptorProto_set_default_value(
1513
- field_proto,
1514
- FileBuilderContext_strdup(self->file_builder, default_value));
1515
- }
1516
- }
1517
-
1518
- if (oneof_index >= 0) {
1519
- google_protobuf_FieldDescriptorProto_set_oneof_index(field_proto,
1520
- oneof_index);
1521
- }
1522
- }
1523
-
1524
- static VALUE make_mapentry(VALUE _message_builder, VALUE types, int argc,
1525
- VALUE* argv) {
1526
- DEFINE_SELF(MessageBuilderContext, message_builder, _message_builder);
1527
- VALUE type_class = rb_ary_entry(types, 2);
1528
- FileBuilderContext* file_context =
1529
- ruby_to_FileBuilderContext(message_builder->file_builder);
1530
- google_protobuf_MessageOptions* options =
1531
- google_protobuf_DescriptorProto_mutable_options(
1532
- message_builder->msg_proto, file_context->arena);
1533
-
1534
- google_protobuf_MessageOptions_set_map_entry(options, true);
1535
-
1536
- // optional <type> key = 1;
1537
- rb_funcall(_message_builder, rb_intern("optional"), 3,
1538
- ID2SYM(rb_intern("key")), rb_ary_entry(types, 0), INT2NUM(1));
1539
-
1540
- // optional <type> value = 2;
1541
- if (type_class == Qnil) {
1542
- rb_funcall(_message_builder, rb_intern("optional"), 3,
1543
- ID2SYM(rb_intern("value")), rb_ary_entry(types, 1), INT2NUM(2));
1544
- } else {
1545
- rb_funcall(_message_builder, rb_intern("optional"), 4,
1546
- ID2SYM(rb_intern("value")), rb_ary_entry(types, 1), INT2NUM(2),
1547
- type_class);
1548
- }
1549
-
1550
- return Qnil;
1551
- }
1552
-
1553
- /*
1554
- * call-seq:
1555
- * MessageBuilderContext.optional(name, type, number, type_class = nil,
1556
- * options = nil)
1557
- *
1558
- * Defines a new optional field on this message type with the given type, tag
1559
- * number, and type class (for message and enum fields). The type must be a Ruby
1560
- * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
1561
- * string, if present (as accepted by FieldDescriptor#submsg_name=).
1562
- */
1563
- VALUE MessageBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
1564
- VALUE name, type, number;
1565
- VALUE type_class, options = Qnil;
1566
-
1567
- rb_scan_args(argc, argv, "32", &name, &type, &number, &type_class, &options);
1568
-
1569
- // Allow passing (name, type, number, options) or
1570
- // (name, type, number, type_class, options)
1571
- if (argc == 4 && RB_TYPE_P(type_class, T_HASH)) {
1572
- options = type_class;
1573
- type_class = Qnil;
1574
- }
1575
-
1576
- msgdef_add_field(_self, UPB_LABEL_OPTIONAL, name, type, number, type_class,
1577
- options, -1);
1578
-
1579
- return Qnil;
1580
- }
1581
-
1582
- /*
1583
- * call-seq:
1584
- * MessageBuilderContext.required(name, type, number, type_class = nil,
1585
- * options = nil)
1586
- *
1587
- * Defines a new required field on this message type with the given type, tag
1588
- * number, and type class (for message and enum fields). The type must be a Ruby
1589
- * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
1590
- * string, if present (as accepted by FieldDescriptor#submsg_name=).
1591
- *
1592
- * Proto3 does not have required fields, but this method exists for
1593
- * completeness. Any attempt to add a message type with required fields to a
1594
- * pool will currently result in an error.
1595
- */
1596
- VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self) {
1597
- VALUE name, type, number;
1598
- VALUE type_class, options = Qnil;
1599
-
1600
- rb_scan_args(argc, argv, "32", &name, &type, &number, &type_class, &options);
1601
-
1602
- // Allow passing (name, type, number, options) or
1603
- // (name, type, number, type_class, options)
1604
- if (argc == 4 && RB_TYPE_P(type_class, T_HASH)) {
1605
- options = type_class;
1606
- type_class = Qnil;
1607
- }
1608
-
1609
- msgdef_add_field(_self, UPB_LABEL_REQUIRED, name, type, number, type_class,
1610
- options, -1);
1611
-
1612
- return Qnil;
1613
- }
1614
-
1615
- /*
1616
- * call-seq:
1617
- * MessageBuilderContext.repeated(name, type, number, type_class = nil)
1618
- *
1619
- * Defines a new repeated field on this message type with the given type, tag
1620
- * number, and type class (for message and enum fields). The type must be a Ruby
1621
- * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
1622
- * string, if present (as accepted by FieldDescriptor#submsg_name=).
1623
- */
1624
- VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self) {
1625
- VALUE name, type, number, type_class;
1626
-
1627
- if (argc < 3) {
1628
- rb_raise(rb_eArgError, "Expected at least 3 arguments.");
1629
- }
1630
- name = argv[0];
1631
- type = argv[1];
1632
- number = argv[2];
1633
- type_class = (argc > 3) ? argv[3] : Qnil;
1634
-
1635
- msgdef_add_field(_self, UPB_LABEL_REPEATED, name, type, number, type_class,
1636
- Qnil, -1);
1637
-
1638
- return Qnil;
1152
+ module, "EnumDescriptor", rb_cObject);
1153
+ rb_define_alloc_func(klass, EnumDescriptor_alloc);
1154
+ rb_define_method(klass, "initialize", EnumDescriptor_initialize, 3);
1155
+ rb_define_method(klass, "name", EnumDescriptor_name, 0);
1156
+ rb_define_method(klass, "lookup_name", EnumDescriptor_lookup_name, 1);
1157
+ rb_define_method(klass, "lookup_value", EnumDescriptor_lookup_value, 1);
1158
+ rb_define_method(klass, "each", EnumDescriptor_each, 0);
1159
+ rb_define_method(klass, "enummodule", EnumDescriptor_enummodule, 0);
1160
+ rb_define_method(klass, "file_descriptor", EnumDescriptor_file_descriptor, 0);
1161
+ rb_include_module(klass, rb_mEnumerable);
1162
+ rb_gc_register_address(&cEnumDescriptor);
1163
+ cEnumDescriptor = klass;
1639
1164
  }
1640
1165
 
1641
- /*
1642
- * call-seq:
1643
- * MessageBuilderContext.map(name, key_type, value_type, number,
1644
- * value_type_class = nil)
1645
- *
1646
- * Defines a new map field on this message type with the given key and value
1647
- * types, tag number, and type class (for message and enum value types). The key
1648
- * type must be :int32/:uint32/:int64/:uint64, :bool, or :string. The value type
1649
- * type must be a Ruby symbol (as accepted by FieldDescriptor#type=) and the
1650
- * type_class must be a string, if present (as accepted by
1651
- * FieldDescriptor#submsg_name=).
1652
- */
1653
- VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self) {
1654
- DEFINE_SELF(MessageBuilderContext, self, _self);
1655
- VALUE name, key_type, value_type, number, type_class;
1656
- VALUE mapentry_desc_name;
1657
- FileBuilderContext* file_builder;
1658
- upb_strview msg_name;
1659
-
1660
- if (argc < 4) {
1661
- rb_raise(rb_eArgError, "Expected at least 4 arguments.");
1662
- }
1663
- name = argv[0];
1664
- key_type = argv[1];
1665
- value_type = argv[2];
1666
- number = argv[3];
1667
- type_class = (argc > 4) ? argv[4] : Qnil;
1668
-
1669
- // Validate the key type. We can't accept enums, messages, or floats/doubles
1670
- // as map keys. (We exclude these explicitly, and the field-descriptor setter
1671
- // below then ensures that the type is one of the remaining valid options.)
1672
- if (SYM2ID(key_type) == rb_intern("float") ||
1673
- SYM2ID(key_type) == rb_intern("double") ||
1674
- SYM2ID(key_type) == rb_intern("enum") ||
1675
- SYM2ID(key_type) == rb_intern("message")) {
1676
- rb_raise(rb_eArgError,
1677
- "Cannot add a map field with a float, double, enum, or message "
1678
- "type.");
1679
- }
1680
-
1681
- file_builder = ruby_to_FileBuilderContext(self->file_builder);
1682
-
1683
- // TODO(haberman): remove this restriction, maps are supported in proto2.
1684
- if (upb_strview_eql(
1685
- google_protobuf_FileDescriptorProto_syntax(file_builder->file_proto),
1686
- upb_strview_makez("proto2"))) {
1687
- rb_raise(rb_eArgError,
1688
- "Cannot add a native map field using proto2 syntax.");
1689
- }
1166
+ static VALUE get_def_obj(VALUE _descriptor_pool, const void* ptr, VALUE klass) {
1167
+ DescriptorPool* descriptor_pool = ruby_to_DescriptorPool(_descriptor_pool);
1168
+ VALUE key = ULL2NUM((intptr_t)ptr);
1169
+ VALUE def;
1690
1170
 
1691
- // Create a new message descriptor for the map entry message, and create a
1692
- // repeated submessage field here with that type.
1693
- msg_name = google_protobuf_DescriptorProto_name(self->msg_proto);
1694
- mapentry_desc_name = rb_str_new(msg_name.data, msg_name.size);
1695
- mapentry_desc_name = rb_str_cat2(mapentry_desc_name, "_MapEntry_");
1696
- mapentry_desc_name =
1697
- rb_str_cat2(mapentry_desc_name, rb_id2name(SYM2ID(name)));
1698
-
1699
- {
1700
- // message <msgname>_MapEntry_ { /* ... */ }
1701
- VALUE args[1] = {mapentry_desc_name};
1702
- VALUE types = rb_ary_new3(3, key_type, value_type, type_class);
1703
- rb_block_call(self->file_builder, rb_intern("add_message"), 1, args,
1704
- make_mapentry, types);
1705
- }
1171
+ def = rb_hash_aref(descriptor_pool->def_to_descriptor, key);
1706
1172
 
1707
- // If this file is in a package, we need to qualify the map entry type.
1708
- if (google_protobuf_FileDescriptorProto_has_package(file_builder->file_proto)) {
1709
- upb_strview package_view =
1710
- google_protobuf_FileDescriptorProto_package(file_builder->file_proto);
1711
- VALUE package = rb_str_new(package_view.data, package_view.size);
1712
- package = rb_str_cat2(package, ".");
1713
- mapentry_desc_name = rb_str_concat(package, mapentry_desc_name);
1173
+ if (ptr == NULL) {
1174
+ return Qnil;
1714
1175
  }
1715
1176
 
1716
- // repeated MapEntry <name> = <number>;
1717
- rb_funcall(_self, rb_intern("repeated"), 4, name,
1718
- ID2SYM(rb_intern("message")), number, mapentry_desc_name);
1719
-
1720
- return Qnil;
1721
- }
1722
-
1723
- /*
1724
- * call-seq:
1725
- * MessageBuilderContext.oneof(name, &block) => nil
1726
- *
1727
- * Creates a new OneofDescriptor with the given name, creates a
1728
- * OneofBuilderContext attached to that OneofDescriptor, evaluates the given
1729
- * block in the context of that OneofBuilderContext with #instance_eval, and
1730
- * then adds the oneof to the message.
1731
- *
1732
- * This is the recommended, idiomatic way to build oneof definitions.
1733
- */
1734
- VALUE MessageBuilderContext_oneof(VALUE _self, VALUE name) {
1735
- DEFINE_SELF(MessageBuilderContext, self, _self);
1736
- size_t oneof_count;
1737
- FileBuilderContext* file_context =
1738
- ruby_to_FileBuilderContext(self->file_builder);
1739
- google_protobuf_OneofDescriptorProto* oneof_proto;
1740
-
1741
- // Existing oneof_count becomes oneof_index.
1742
- google_protobuf_DescriptorProto_oneof_decl(self->msg_proto, &oneof_count);
1743
-
1744
- // Create oneof_proto and set its name.
1745
- oneof_proto = google_protobuf_DescriptorProto_add_oneof_decl(
1746
- self->msg_proto, file_context->arena);
1747
- google_protobuf_OneofDescriptorProto_set_name(
1748
- oneof_proto, FileBuilderContext_strdup_sym(self->file_builder, name));
1749
-
1750
- // Evaluate the block with the builder as argument.
1751
- {
1752
- VALUE args[2] = { INT2NUM(oneof_count), _self };
1753
- VALUE ctx = rb_class_new_instance(2, args, cOneofBuilderContext);
1754
- VALUE block = rb_block_proc();
1755
- rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
1177
+ if (def == Qnil) {
1178
+ // Lazily create wrapper object.
1179
+ VALUE args[3] = { c_only_cookie, _descriptor_pool, key };
1180
+ def = rb_class_new_instance(3, args, klass);
1181
+ rb_hash_aset(descriptor_pool->def_to_descriptor, key, def);
1756
1182
  }
1757
1183
 
1758
- return Qnil;
1759
- }
1760
-
1761
- // -----------------------------------------------------------------------------
1762
- // OneofBuilderContext.
1763
- // -----------------------------------------------------------------------------
1764
-
1765
- DEFINE_CLASS(OneofBuilderContext,
1766
- "Google::Protobuf::Internal::OneofBuilderContext");
1767
-
1768
- void OneofBuilderContext_mark(void* _self) {
1769
- OneofBuilderContext* self = _self;
1770
- rb_gc_mark(self->message_builder);
1771
- }
1772
-
1773
- void OneofBuilderContext_free(void* _self) {
1774
- xfree(_self);
1775
- }
1776
-
1777
- VALUE OneofBuilderContext_alloc(VALUE klass) {
1778
- OneofBuilderContext* self = ALLOC(OneofBuilderContext);
1779
- VALUE ret = TypedData_Wrap_Struct(
1780
- klass, &_OneofBuilderContext_type, self);
1781
- self->oneof_index = 0;
1782
- self->message_builder = Qnil;
1783
- return ret;
1784
- }
1785
-
1786
- void OneofBuilderContext_register(VALUE module) {
1787
- VALUE klass = rb_define_class_under(
1788
- module, "OneofBuilderContext", rb_cObject);
1789
- rb_define_alloc_func(klass, OneofBuilderContext_alloc);
1790
- rb_define_method(klass, "initialize",
1791
- OneofBuilderContext_initialize, 2);
1792
- rb_define_method(klass, "optional", OneofBuilderContext_optional, -1);
1793
- rb_gc_register_address(&cOneofBuilderContext);
1794
- cOneofBuilderContext = klass;
1795
- }
1796
-
1797
- /*
1798
- * call-seq:
1799
- * OneofBuilderContext.new(oneof_index, message_builder) => context
1800
- *
1801
- * Create a new oneof builder context around the given oneof descriptor and
1802
- * builder context. This class is intended to serve as a DSL context to be used
1803
- * with #instance_eval.
1804
- */
1805
- VALUE OneofBuilderContext_initialize(VALUE _self,
1806
- VALUE oneof_index,
1807
- VALUE message_builder) {
1808
- DEFINE_SELF(OneofBuilderContext, self, _self);
1809
- self->oneof_index = NUM2INT(oneof_index);
1810
- self->message_builder = message_builder;
1811
- return Qnil;
1812
- }
1813
-
1814
- /*
1815
- * call-seq:
1816
- * OneofBuilderContext.optional(name, type, number, type_class = nil,
1817
- * default_value = nil)
1818
- *
1819
- * Defines a new optional field in this oneof with the given type, tag number,
1820
- * and type class (for message and enum fields). The type must be a Ruby symbol
1821
- * (as accepted by FieldDescriptor#type=) and the type_class must be a string,
1822
- * if present (as accepted by FieldDescriptor#submsg_name=).
1823
- */
1824
- VALUE OneofBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
1825
- DEFINE_SELF(OneofBuilderContext, self, _self);
1826
- VALUE name, type, number;
1827
- VALUE type_class, options = Qnil;
1828
-
1829
- rb_scan_args(argc, argv, "32", &name, &type, &number, &type_class, &options);
1830
-
1831
- msgdef_add_field(self->message_builder, UPB_LABEL_OPTIONAL, name, type,
1832
- number, type_class, options, self->oneof_index);
1833
-
1834
- return Qnil;
1835
- }
1836
-
1837
- // -----------------------------------------------------------------------------
1838
- // EnumBuilderContext.
1839
- // -----------------------------------------------------------------------------
1840
-
1841
- DEFINE_CLASS(EnumBuilderContext,
1842
- "Google::Protobuf::Internal::EnumBuilderContext");
1843
-
1844
- void EnumBuilderContext_mark(void* _self) {
1845
- EnumBuilderContext* self = _self;
1846
- rb_gc_mark(self->file_builder);
1184
+ return def;
1847
1185
  }
1848
1186
 
1849
- void EnumBuilderContext_free(void* _self) {
1850
- xfree(_self);
1187
+ static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_msgdef* def) {
1188
+ return get_def_obj(descriptor_pool, def, cDescriptor);
1851
1189
  }
1852
1190
 
1853
- VALUE EnumBuilderContext_alloc(VALUE klass) {
1854
- EnumBuilderContext* self = ALLOC(EnumBuilderContext);
1855
- VALUE ret = TypedData_Wrap_Struct(
1856
- klass, &_EnumBuilderContext_type, self);
1857
- self->enum_proto = NULL;
1858
- self->file_builder = Qnil;
1859
- return ret;
1191
+ static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_enumdef* def) {
1192
+ return get_def_obj(descriptor_pool, def, cEnumDescriptor);
1860
1193
  }
1861
1194
 
1862
- void EnumBuilderContext_register(VALUE module) {
1863
- VALUE klass = rb_define_class_under(
1864
- module, "EnumBuilderContext", rb_cObject);
1865
- rb_define_alloc_func(klass, EnumBuilderContext_alloc);
1866
- rb_define_method(klass, "initialize", EnumBuilderContext_initialize, 2);
1867
- rb_define_method(klass, "value", EnumBuilderContext_value, 2);
1868
- rb_gc_register_address(&cEnumBuilderContext);
1869
- cEnumBuilderContext = klass;
1195
+ static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_fielddef* def) {
1196
+ return get_def_obj(descriptor_pool, def, cFieldDescriptor);
1870
1197
  }
1871
1198
 
1872
- /*
1873
- * call-seq:
1874
- * EnumBuilderContext.new(file_builder) => context
1875
- *
1876
- * Create a new builder context around the given enum descriptor. This class is
1877
- * intended to serve as a DSL context to be used with #instance_eval.
1878
- */
1879
- VALUE EnumBuilderContext_initialize(VALUE _self, VALUE _file_builder,
1880
- VALUE name) {
1881
- DEFINE_SELF(EnumBuilderContext, self, _self);
1882
- FileBuilderContext* file_builder = ruby_to_FileBuilderContext(_file_builder);
1883
- google_protobuf_FileDescriptorProto* file_proto = file_builder->file_proto;
1884
-
1885
- self->file_builder = _file_builder;
1886
- self->enum_proto = google_protobuf_FileDescriptorProto_add_enum_type(
1887
- file_proto, file_builder->arena);
1888
-
1889
- google_protobuf_EnumDescriptorProto_set_name(
1890
- self->enum_proto, FileBuilderContext_strdup(_file_builder, name));
1891
-
1892
- return Qnil;
1199
+ static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_filedef* def) {
1200
+ return get_def_obj(descriptor_pool, def, cFileDescriptor);
1893
1201
  }
1894
1202
 
1895
- /*
1896
- * call-seq:
1897
- * EnumBuilder.add_value(name, number)
1898
- *
1899
- * Adds the given name => number mapping to the enum type. Name must be a Ruby
1900
- * symbol.
1901
- */
1902
- VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number) {
1903
- DEFINE_SELF(EnumBuilderContext, self, _self);
1904
- FileBuilderContext* file_builder =
1905
- ruby_to_FileBuilderContext(self->file_builder);
1906
- google_protobuf_EnumValueDescriptorProto* enum_value;
1907
-
1908
- enum_value = google_protobuf_EnumDescriptorProto_add_value(
1909
- self->enum_proto, file_builder->arena);
1910
-
1911
- google_protobuf_EnumValueDescriptorProto_set_name(
1912
- enum_value, FileBuilderContext_strdup_sym(self->file_builder, name));
1913
- google_protobuf_EnumValueDescriptorProto_set_number(enum_value,
1914
- NUM2INT(number));
1915
-
1916
- return Qnil;
1203
+ static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_oneofdef* def) {
1204
+ return get_def_obj(descriptor_pool, def, cOneofDescriptor);
1917
1205
  }
1918
1206
 
1919
-
1920
1207
  // -----------------------------------------------------------------------------
1921
- // FileBuilderContext.
1208
+ // Shared functions
1922
1209
  // -----------------------------------------------------------------------------
1923
1210
 
1924
- DEFINE_CLASS(FileBuilderContext,
1925
- "Google::Protobuf::Internal::FileBuilderContext");
1926
-
1927
- void FileBuilderContext_mark(void* _self) {
1928
- FileBuilderContext* self = _self;
1929
- rb_gc_mark(self->descriptor_pool);
1930
- }
1931
-
1932
- void FileBuilderContext_free(void* _self) {
1933
- FileBuilderContext* self = _self;
1934
- upb_arena_free(self->arena);
1935
- xfree(self);
1936
- }
1937
-
1938
- upb_strview FileBuilderContext_strdup2(VALUE _self, const char *str) {
1939
- DEFINE_SELF(FileBuilderContext, self, _self);
1940
- upb_strview ret;
1941
- char *data;
1211
+ // Functions exposed to other modules in defs.h.
1942
1212
 
1943
- ret.size = strlen(str);
1944
- data = upb_malloc(upb_arena_alloc(self->arena), ret.size + 1);
1945
- ret.data = data;
1946
- memcpy(data, str, ret.size);
1947
- /* Null-terminate required by rewrite_enum_defaults() above. */
1948
- data[ret.size] = '\0';
1949
- return ret;
1213
+ VALUE Descriptor_DefToClass(const upb_msgdef *m) {
1214
+ const upb_symtab *symtab = upb_filedef_symtab(upb_msgdef_file(m));
1215
+ VALUE pool = ObjectCache_Get(symtab);
1216
+ PBRUBY_ASSERT(pool != Qnil);
1217
+ VALUE desc_rb = get_msgdef_obj(pool, m);
1218
+ const Descriptor* desc = ruby_to_Descriptor(desc_rb);
1219
+ return desc->klass;
1950
1220
  }
1951
1221
 
1952
- upb_strview FileBuilderContext_strdup(VALUE _self, VALUE rb_str) {
1953
- return FileBuilderContext_strdup2(_self, get_str(rb_str));
1222
+ const upb_msgdef *Descriptor_GetMsgDef(VALUE desc_rb) {
1223
+ const Descriptor* desc = ruby_to_Descriptor(desc_rb);
1224
+ return desc->msgdef;
1954
1225
  }
1955
1226
 
1956
- upb_strview FileBuilderContext_strdup_sym(VALUE _self, VALUE rb_sym) {
1957
- Check_Type(rb_sym, T_SYMBOL);
1958
- return FileBuilderContext_strdup(_self, rb_id2str(SYM2ID(rb_sym)));
1959
- }
1960
-
1961
- VALUE FileBuilderContext_alloc(VALUE klass) {
1962
- FileBuilderContext* self = ALLOC(FileBuilderContext);
1963
- VALUE ret = TypedData_Wrap_Struct(klass, &_FileBuilderContext_type, self);
1964
- self->arena = upb_arena_new();
1965
- self->file_proto = google_protobuf_FileDescriptorProto_new(self->arena);
1966
- self->descriptor_pool = Qnil;
1967
- return ret;
1968
- }
1969
-
1970
- void FileBuilderContext_register(VALUE module) {
1971
- VALUE klass = rb_define_class_under(module, "FileBuilderContext", rb_cObject);
1972
- rb_define_alloc_func(klass, FileBuilderContext_alloc);
1973
- rb_define_method(klass, "initialize", FileBuilderContext_initialize, 3);
1974
- rb_define_method(klass, "add_message", FileBuilderContext_add_message, 1);
1975
- rb_define_method(klass, "add_enum", FileBuilderContext_add_enum, 1);
1976
- rb_gc_register_address(&cFileBuilderContext);
1977
- cFileBuilderContext = klass;
1978
- }
1979
-
1980
- /*
1981
- * call-seq:
1982
- * FileBuilderContext.new(descriptor_pool) => context
1983
- *
1984
- * Create a new file builder context for the given file descriptor and
1985
- * builder context. This class is intended to serve as a DSL context to be used
1986
- * with #instance_eval.
1987
- */
1988
- VALUE FileBuilderContext_initialize(VALUE _self, VALUE descriptor_pool,
1989
- VALUE name, VALUE options) {
1990
- DEFINE_SELF(FileBuilderContext, self, _self);
1991
- self->descriptor_pool = descriptor_pool;
1992
-
1993
- google_protobuf_FileDescriptorProto_set_name(
1994
- self->file_proto, FileBuilderContext_strdup(_self, name));
1995
-
1996
- // Default syntax for Ruby is proto3.
1997
- google_protobuf_FileDescriptorProto_set_syntax(
1998
- self->file_proto,
1999
- FileBuilderContext_strdup(_self, rb_str_new2("proto3")));
2000
-
2001
- if (options != Qnil) {
2002
- VALUE syntax;
2003
-
2004
- Check_Type(options, T_HASH);
2005
- syntax = rb_hash_lookup2(options, ID2SYM(rb_intern("syntax")), Qnil);
2006
-
2007
- if (syntax != Qnil) {
2008
- VALUE syntax_str;
2009
-
2010
- Check_Type(syntax, T_SYMBOL);
2011
- syntax_str = rb_id2str(SYM2ID(syntax));
2012
- google_protobuf_FileDescriptorProto_set_syntax(
2013
- self->file_proto, FileBuilderContext_strdup(_self, syntax_str));
1227
+ VALUE TypeInfo_InitArg(int argc, VALUE *argv, int skip_arg) {
1228
+ if (argc > skip_arg) {
1229
+ if (argc > 1 + skip_arg) {
1230
+ rb_raise(rb_eArgError, "Expected a maximum of %d arguments.", skip_arg + 1);
2014
1231
  }
1232
+ return argv[skip_arg];
1233
+ } else {
1234
+ return Qnil;
2015
1235
  }
2016
-
2017
- return Qnil;
2018
- }
2019
-
2020
- /*
2021
- * call-seq:
2022
- * FileBuilderContext.add_message(name, &block)
2023
- *
2024
- * Creates a new, empty descriptor with the given name, and invokes the block in
2025
- * the context of a MessageBuilderContext on that descriptor. The block can then
2026
- * call, e.g., MessageBuilderContext#optional and MessageBuilderContext#repeated
2027
- * methods to define the message fields.
2028
- *
2029
- * This is the recommended, idiomatic way to build message definitions.
2030
- */
2031
- VALUE FileBuilderContext_add_message(VALUE _self, VALUE name) {
2032
- VALUE args[2] = { _self, name };
2033
- VALUE ctx = rb_class_new_instance(2, args, cMessageBuilderContext);
2034
- VALUE block = rb_block_proc();
2035
- rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
2036
- return Qnil;
2037
- }
2038
-
2039
- /*
2040
- * call-seq:
2041
- * FileBuilderContext.add_enum(name, &block)
2042
- *
2043
- * Creates a new, empty enum descriptor with the given name, and invokes the
2044
- * block in the context of an EnumBuilderContext on that descriptor. The block
2045
- * can then call EnumBuilderContext#add_value to define the enum values.
2046
- *
2047
- * This is the recommended, idiomatic way to build enum definitions.
2048
- */
2049
- VALUE FileBuilderContext_add_enum(VALUE _self, VALUE name) {
2050
- VALUE args[2] = { _self, name };
2051
- VALUE ctx = rb_class_new_instance(2, args, cEnumBuilderContext);
2052
- VALUE block = rb_block_proc();
2053
- rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
2054
- return Qnil;
2055
- }
2056
-
2057
- void FileBuilderContext_build(VALUE _self) {
2058
- DEFINE_SELF(FileBuilderContext, self, _self);
2059
- DescriptorPool* pool = ruby_to_DescriptorPool(self->descriptor_pool);
2060
- upb_status status;
2061
-
2062
- rewrite_enum_defaults(pool->symtab, self->file_proto);
2063
- rewrite_names(_self, self->file_proto);
2064
-
2065
- upb_status_clear(&status);
2066
- if (!upb_symtab_addfile(pool->symtab, self->file_proto, &status)) {
2067
- rb_raise(cTypeError, "Unable to add defs to DescriptorPool: %s",
2068
- upb_status_errmsg(&status));
2069
- }
2070
- }
2071
-
2072
- // -----------------------------------------------------------------------------
2073
- // Builder.
2074
- // -----------------------------------------------------------------------------
2075
-
2076
- DEFINE_CLASS(Builder, "Google::Protobuf::Internal::Builder");
2077
-
2078
- void Builder_mark(void* _self) {
2079
- Builder* self = _self;
2080
- rb_gc_mark(self->descriptor_pool);
2081
- rb_gc_mark(self->default_file_builder);
2082
- }
2083
-
2084
- void Builder_free(void* _self) {
2085
- xfree(_self);
2086
- }
2087
-
2088
- VALUE Builder_alloc(VALUE klass) {
2089
- Builder* self = ALLOC(Builder);
2090
- VALUE ret = TypedData_Wrap_Struct(
2091
- klass, &_Builder_type, self);
2092
- self->descriptor_pool = Qnil;
2093
- self->default_file_builder = Qnil;
2094
- return ret;
2095
- }
2096
-
2097
- void Builder_register(VALUE module) {
2098
- VALUE klass = rb_define_class_under(module, "Builder", rb_cObject);
2099
- rb_define_alloc_func(klass, Builder_alloc);
2100
- rb_define_method(klass, "initialize", Builder_initialize, 1);
2101
- rb_define_method(klass, "add_file", Builder_add_file, -1);
2102
- rb_define_method(klass, "add_message", Builder_add_message, 1);
2103
- rb_define_method(klass, "add_enum", Builder_add_enum, 1);
2104
- rb_gc_register_address(&cBuilder);
2105
- cBuilder = klass;
2106
- }
2107
-
2108
- /*
2109
- * call-seq:
2110
- * Builder.new(descriptor_pool) => builder
2111
- *
2112
- * Creates a new Builder. A Builder can accumulate a set of new message and enum
2113
- * descriptors and atomically register them into a pool in a way that allows for
2114
- * (co)recursive type references.
2115
- */
2116
- VALUE Builder_initialize(VALUE _self, VALUE pool) {
2117
- DEFINE_SELF(Builder, self, _self);
2118
- self->descriptor_pool = pool;
2119
- self->default_file_builder = Qnil; // Created lazily if needed.
2120
- return Qnil;
2121
- }
2122
-
2123
- /*
2124
- * call-seq:
2125
- * Builder.add_file(name, options = nil, &block)
2126
- *
2127
- * Creates a new, file descriptor with the given name and options and invokes
2128
- * the block in the context of a FileBuilderContext on that descriptor. The
2129
- * block can then call FileBuilderContext#add_message or
2130
- * FileBuilderContext#add_enum to define new messages or enums, respectively.
2131
- *
2132
- * This is the recommended, idiomatic way to build file descriptors.
2133
- */
2134
- VALUE Builder_add_file(int argc, VALUE* argv, VALUE _self) {
2135
- DEFINE_SELF(Builder, self, _self);
2136
- VALUE name, options;
2137
- VALUE ctx;
2138
- VALUE block;
2139
-
2140
- rb_scan_args(argc, argv, "11", &name, &options);
2141
-
2142
- {
2143
- VALUE args[3] = { self->descriptor_pool, name, options };
2144
- ctx = rb_class_new_instance(3, args, cFileBuilderContext);
2145
- }
2146
-
2147
- block = rb_block_proc();
2148
- rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
2149
- FileBuilderContext_build(ctx);
2150
-
2151
- return Qnil;
2152
- }
2153
-
2154
- static VALUE Builder_get_default_file(VALUE _self) {
2155
- DEFINE_SELF(Builder, self, _self);
2156
-
2157
- /* Lazily create only if legacy builder-level methods are called. */
2158
- if (self->default_file_builder == Qnil) {
2159
- VALUE name = rb_str_new2("ruby_default_file.proto");
2160
- VALUE args [3] = { self->descriptor_pool, name, rb_hash_new() };
2161
- self->default_file_builder =
2162
- rb_class_new_instance(3, args, cFileBuilderContext);
2163
- }
2164
-
2165
- return self->default_file_builder;
2166
- }
2167
-
2168
- /*
2169
- * call-seq:
2170
- * Builder.add_message(name, &block)
2171
- *
2172
- * Old and deprecated way to create a new descriptor.
2173
- * See FileBuilderContext.add_message for the recommended way.
2174
- *
2175
- * Exists for backwards compatibility to allow building descriptor pool for
2176
- * files generated by protoc which don't add messages within "add_file" block.
2177
- * Descriptors created this way get assigned to a default empty FileDescriptor.
2178
- */
2179
- VALUE Builder_add_message(VALUE _self, VALUE name) {
2180
- VALUE file_builder = Builder_get_default_file(_self);
2181
- rb_funcall_with_block(file_builder, rb_intern("add_message"), 1, &name,
2182
- rb_block_proc());
2183
- return Qnil;
2184
- }
2185
-
2186
- /*
2187
- * call-seq:
2188
- * Builder.add_enum(name, &block)
2189
- *
2190
- * Old and deprecated way to create a new enum descriptor.
2191
- * See FileBuilderContext.add_enum for the recommended way.
2192
- *
2193
- * Exists for backwards compatibility to allow building descriptor pool for
2194
- * files generated by protoc which don't add enums within "add_file" block.
2195
- * Enum descriptors created this way get assigned to a default empty
2196
- * FileDescriptor.
2197
- */
2198
- VALUE Builder_add_enum(VALUE _self, VALUE name) {
2199
- VALUE file_builder = Builder_get_default_file(_self);
2200
- rb_funcall_with_block(file_builder, rb_intern("add_enum"), 1, &name,
2201
- rb_block_proc());
2202
- return Qnil;
2203
1236
  }
2204
1237
 
2205
- /* This method is hidden from Ruby, and only called directly from
2206
- * DescriptorPool_build(). */
2207
- VALUE Builder_build(VALUE _self) {
2208
- DEFINE_SELF(Builder, self, _self);
1238
+ TypeInfo TypeInfo_FromClass(int argc, VALUE* argv, int skip_arg,
1239
+ VALUE* type_class, VALUE* init_arg) {
1240
+ TypeInfo ret = {ruby_to_fieldtype(argv[skip_arg])};
2209
1241
 
2210
- if (self->default_file_builder != Qnil) {
2211
- FileBuilderContext_build(self->default_file_builder);
2212
- self->default_file_builder = Qnil;
2213
- }
1242
+ if (ret.type == UPB_TYPE_MESSAGE || ret.type == UPB_TYPE_ENUM) {
1243
+ *init_arg = TypeInfo_InitArg(argc, argv, skip_arg + 2);
2214
1244
 
2215
- return Qnil;
2216
- }
2217
-
2218
- static VALUE get_def_obj(VALUE _descriptor_pool, const void* ptr, VALUE klass) {
2219
- DEFINE_SELF(DescriptorPool, descriptor_pool, _descriptor_pool);
2220
- VALUE key = ULL2NUM((intptr_t)ptr);
2221
- VALUE def;
2222
-
2223
- def = rb_hash_aref(descriptor_pool->def_to_descriptor, key);
1245
+ if (argc < 2 + skip_arg) {
1246
+ rb_raise(rb_eArgError, "Expected at least %d arguments for message/enum.",
1247
+ 2 + skip_arg);
1248
+ }
2224
1249
 
2225
- if (ptr == NULL) {
2226
- return Qnil;
2227
- }
1250
+ VALUE klass = argv[1 + skip_arg];
1251
+ VALUE desc = MessageOrEnum_GetDescriptor(klass);
1252
+ *type_class = klass;
2228
1253
 
2229
- if (def == Qnil) {
2230
- // Lazily create wrapper object.
2231
- VALUE args[3] = { c_only_cookie, _descriptor_pool, key };
2232
- def = rb_class_new_instance(3, args, klass);
2233
- rb_hash_aset(descriptor_pool->def_to_descriptor, key, def);
1254
+ if (desc == Qnil) {
1255
+ rb_raise(rb_eArgError,
1256
+ "Type class has no descriptor. Please pass a "
1257
+ "class or enum as returned by the DescriptorPool.");
1258
+ }
2234
1259
 
2235
- // For message defs, we now eagerly get/create descriptors for all
2236
- // submessages. We will need these anyway to parse or serialize this
2237
- // message type. But more importantly, we must do this now so that
2238
- // add_handlers_for_message() (which calls get_msgdef_obj()) does *not*
2239
- // need to create a Ruby object or insert into a Ruby Hash. We need to
2240
- // avoid triggering GC, which can switch Ruby threads and re-enter our
2241
- // C extension from a different thread. This wreaks havoc on our state
2242
- // if we were in the middle of building handlers.
2243
- if (klass == cDescriptor) {
2244
- const upb_msgdef *m = ptr;
2245
- upb_msg_field_iter it;
2246
- for (upb_msg_field_begin(&it, m);
2247
- !upb_msg_field_done(&it);
2248
- upb_msg_field_next(&it)) {
2249
- const upb_fielddef* f = upb_msg_iter_field(&it);
2250
- if (upb_fielddef_issubmsg(f)) {
2251
- get_msgdef_obj(_descriptor_pool, upb_fielddef_msgsubdef(f));
2252
- }
2253
- }
1260
+ if (ret.type == UPB_TYPE_MESSAGE) {
1261
+ ret.def.msgdef = ruby_to_Descriptor(desc)->msgdef;
1262
+ Message_CheckClass(klass);
1263
+ } else {
1264
+ PBRUBY_ASSERT(ret.type == UPB_TYPE_ENUM);
1265
+ ret.def.enumdef = ruby_to_EnumDescriptor(desc)->enumdef;
2254
1266
  }
1267
+ } else {
1268
+ *init_arg = TypeInfo_InitArg(argc, argv, skip_arg + 1);
2255
1269
  }
2256
1270
 
2257
- return def;
2258
- }
2259
-
2260
- VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_msgdef* def) {
2261
- return get_def_obj(descriptor_pool, def, cDescriptor);
2262
- }
2263
-
2264
- VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_enumdef* def) {
2265
- return get_def_obj(descriptor_pool, def, cEnumDescriptor);
2266
- }
2267
-
2268
- VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_fielddef* def) {
2269
- return get_def_obj(descriptor_pool, def, cFieldDescriptor);
1271
+ return ret;
2270
1272
  }
2271
1273
 
2272
- VALUE get_filedef_obj(VALUE descriptor_pool, const upb_filedef* def) {
2273
- return get_def_obj(descriptor_pool, def, cFileDescriptor);
2274
- }
1274
+ void Defs_register(VALUE module) {
1275
+ DescriptorPool_register(module);
1276
+ Descriptor_register(module);
1277
+ FileDescriptor_register(module);
1278
+ FieldDescriptor_register(module);
1279
+ OneofDescriptor_register(module);
1280
+ EnumDescriptor_register(module);
2275
1281
 
2276
- VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_oneofdef* def) {
2277
- return get_def_obj(descriptor_pool, def, cOneofDescriptor);
1282
+ rb_gc_register_address(&c_only_cookie);
1283
+ c_only_cookie = rb_class_new_instance(0, NULL, rb_cObject);
2278
1284
  }