google-protobuf 3.8.0 → 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.

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