google-protobuf 3.4.0.2 → 3.19.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of google-protobuf might be problematic. Click here for more details.
- checksums.yaml +5 -5
- data/ext/google/protobuf_c/convert.c +348 -0
- data/ext/google/protobuf_c/convert.h +72 -0
- data/ext/google/protobuf_c/defs.c +709 -1188
- data/ext/google/protobuf_c/defs.h +107 -0
- data/ext/google/protobuf_c/extconf.rb +7 -4
- data/ext/google/protobuf_c/map.c +316 -463
- data/ext/google/protobuf_c/map.h +67 -0
- data/ext/google/protobuf_c/message.c +993 -296
- data/ext/google/protobuf_c/message.h +101 -0
- data/ext/google/protobuf_c/protobuf.c +403 -50
- data/ext/google/protobuf_c/protobuf.h +47 -473
- data/ext/google/protobuf_c/repeated_field.c +314 -309
- data/ext/google/protobuf_c/repeated_field.h +63 -0
- data/ext/google/protobuf_c/ruby-upb.c +9171 -0
- data/ext/google/protobuf_c/ruby-upb.h +4704 -0
- data/ext/google/protobuf_c/wrap_memcpy.c +1 -1
- data/lib/google/protobuf/any_pb.rb +6 -4
- data/lib/google/protobuf/api_pb.rb +27 -24
- data/lib/google/protobuf/descriptor_dsl.rb +458 -0
- data/lib/google/protobuf/descriptor_pb.rb +268 -0
- data/lib/google/protobuf/duration_pb.rb +6 -4
- data/lib/google/protobuf/empty_pb.rb +4 -2
- data/lib/google/protobuf/field_mask_pb.rb +5 -3
- data/lib/google/protobuf/message_exts.rb +2 -2
- data/lib/google/protobuf/repeated_field.rb +3 -3
- data/lib/google/protobuf/source_context_pb.rb +5 -3
- data/lib/google/protobuf/struct_pb.rb +23 -21
- data/lib/google/protobuf/timestamp_pb.rb +6 -4
- data/lib/google/protobuf/type_pb.rb +77 -74
- data/lib/google/protobuf/well_known_types.rb +25 -2
- data/lib/google/protobuf/wrappers_pb.rb +37 -35
- data/lib/google/protobuf.rb +7 -4
- data/tests/basic.rb +432 -1115
- data/tests/generated_code_test.rb +6 -2
- data/tests/stress.rb +1 -1
- metadata +22 -30
- data/ext/google/protobuf_c/encode_decode.c +0 -1311
- data/ext/google/protobuf_c/storage.c +0 -893
- data/ext/google/protobuf_c/upb.c +0 -13911
- data/ext/google/protobuf_c/upb.h +0 -8872
@@ -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
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
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
|
-
|
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, 0);
|
126
|
-
rb_define_method(klass, "lookup", DescriptorPool_lookup, 1);
|
127
|
-
rb_define_singleton_method(klass, "generated_pool",
|
128
|
-
DescriptorPool_generated_pool, 0);
|
129
|
-
cDescriptorPool = klass;
|
130
|
-
rb_gc_register_address(&cDescriptorPool);
|
123
|
+
VALUE ret;
|
131
124
|
|
132
|
-
|
133
|
-
|
134
|
-
}
|
125
|
+
self->def_to_descriptor = Qnil;
|
126
|
+
ret = TypedData_Wrap_Struct(klass, &DescriptorPool_type, self);
|
135
127
|
|
136
|
-
|
137
|
-
|
138
|
-
|
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
|
-
|
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.
|
137
|
+
* DescriptorPool.add_serialized_file(serialized_file_proto)
|
155
138
|
*
|
156
|
-
* Adds the given
|
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
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
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
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
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(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(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
|
-
|
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
|
203
|
-
|
204
|
-
|
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
|
-
|
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,51 +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
|
-
|
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);
|
231
|
-
rb_gc_mark(self->
|
235
|
+
rb_gc_mark(self->descriptor_pool);
|
232
236
|
}
|
233
237
|
|
234
|
-
|
235
|
-
Descriptor
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
upb_pbdecodermethod_unref(self->fill_method, &self->fill_method);
|
245
|
-
}
|
246
|
-
if (self->json_fill_method) {
|
247
|
-
upb_json_parsermethod_unref(self->json_fill_method,
|
248
|
-
&self->json_fill_method);
|
249
|
-
}
|
250
|
-
if (self->pb_serialize_handlers) {
|
251
|
-
upb_handlers_unref(self->pb_serialize_handlers,
|
252
|
-
&self->pb_serialize_handlers);
|
253
|
-
}
|
254
|
-
if (self->json_serialize_handlers) {
|
255
|
-
upb_handlers_unref(self->json_serialize_handlers,
|
256
|
-
&self->json_serialize_handlers);
|
257
|
-
}
|
258
|
-
if (self->json_serialize_handlers_preserve) {
|
259
|
-
upb_handlers_unref(self->json_serialize_handlers_preserve,
|
260
|
-
&self->json_serialize_handlers_preserve);
|
261
|
-
}
|
262
|
-
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;
|
263
248
|
}
|
264
249
|
|
265
250
|
/*
|
@@ -271,67 +256,57 @@ void Descriptor_free(void* _self) {
|
|
271
256
|
* it is added to a pool, after which it becomes immutable (as part of a
|
272
257
|
* finalization process).
|
273
258
|
*/
|
274
|
-
VALUE Descriptor_alloc(VALUE klass) {
|
259
|
+
static VALUE Descriptor_alloc(VALUE klass) {
|
275
260
|
Descriptor* self = ALLOC(Descriptor);
|
276
|
-
VALUE ret = TypedData_Wrap_Struct(klass, &
|
277
|
-
self->msgdef =
|
261
|
+
VALUE ret = TypedData_Wrap_Struct(klass, &Descriptor_type, self);
|
262
|
+
self->msgdef = NULL;
|
278
263
|
self->klass = Qnil;
|
279
|
-
self->
|
280
|
-
self->fill_handlers = NULL;
|
281
|
-
self->fill_method = NULL;
|
282
|
-
self->json_fill_method = NULL;
|
283
|
-
self->pb_serialize_handlers = NULL;
|
284
|
-
self->json_serialize_handlers = NULL;
|
285
|
-
self->json_serialize_handlers_preserve = NULL;
|
286
|
-
self->typeclass_references = rb_ary_new();
|
264
|
+
self->descriptor_pool = Qnil;
|
287
265
|
return ret;
|
288
266
|
}
|
289
267
|
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
268
|
+
/*
|
269
|
+
* call-seq:
|
270
|
+
* Descriptor.new(c_only_cookie, ptr) => Descriptor
|
271
|
+
*
|
272
|
+
* Creates a descriptor wrapper object. May only be called from C.
|
273
|
+
*/
|
274
|
+
static VALUE Descriptor_initialize(VALUE _self, VALUE cookie,
|
275
|
+
VALUE descriptor_pool, VALUE ptr) {
|
276
|
+
Descriptor* self = ruby_to_Descriptor(_self);
|
277
|
+
|
278
|
+
if (cookie != c_only_cookie) {
|
279
|
+
rb_raise(rb_eRuntimeError,
|
280
|
+
"Descriptor objects may not be created from Ruby.");
|
281
|
+
}
|
282
|
+
|
283
|
+
self->descriptor_pool = descriptor_pool;
|
284
|
+
self->msgdef = (const upb_msgdef*)NUM2ULL(ptr);
|
285
|
+
|
286
|
+
return Qnil;
|
306
287
|
}
|
307
288
|
|
308
289
|
/*
|
309
290
|
* call-seq:
|
310
|
-
*
|
291
|
+
* Descriptor.file_descriptor
|
311
292
|
*
|
312
|
-
* Returns the
|
313
|
-
* My.Package.MessageType).
|
293
|
+
* Returns the FileDescriptor object this message belongs to.
|
314
294
|
*/
|
315
|
-
VALUE
|
316
|
-
|
317
|
-
return
|
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));
|
318
298
|
}
|
319
299
|
|
320
300
|
/*
|
321
301
|
* call-seq:
|
322
|
-
*
|
302
|
+
* Descriptor.name => name
|
323
303
|
*
|
324
|
-
*
|
325
|
-
*
|
304
|
+
* Returns the name of this message type as a fully-qualified string (e.g.,
|
305
|
+
* My.Package.MessageType).
|
326
306
|
*/
|
327
|
-
VALUE
|
328
|
-
|
329
|
-
|
330
|
-
const char* name = get_str(str);
|
331
|
-
CHECK_UPB(
|
332
|
-
upb_msgdef_setfullname(mut_def, name, &status),
|
333
|
-
"Error setting Descriptor name");
|
334
|
-
return Qnil;
|
307
|
+
static VALUE Descriptor_name(VALUE _self) {
|
308
|
+
Descriptor* self = ruby_to_Descriptor(_self);
|
309
|
+
return rb_str_maybe_null(upb_msgdef_fullname(self->msgdef));
|
335
310
|
}
|
336
311
|
|
337
312
|
/*
|
@@ -340,15 +315,15 @@ VALUE Descriptor_name_set(VALUE _self, VALUE str) {
|
|
340
315
|
*
|
341
316
|
* Iterates over fields in this message type, yielding to the block on each one.
|
342
317
|
*/
|
343
|
-
VALUE Descriptor_each(VALUE _self) {
|
344
|
-
|
318
|
+
static VALUE Descriptor_each(VALUE _self) {
|
319
|
+
Descriptor* self = ruby_to_Descriptor(_self);
|
345
320
|
|
346
321
|
upb_msg_field_iter it;
|
347
322
|
for (upb_msg_field_begin(&it, self->msgdef);
|
348
323
|
!upb_msg_field_done(&it);
|
349
324
|
upb_msg_field_next(&it)) {
|
350
325
|
const upb_fielddef* field = upb_msg_iter_field(&it);
|
351
|
-
VALUE obj =
|
326
|
+
VALUE obj = get_fielddef_obj(self->descriptor_pool, field);
|
352
327
|
rb_yield(obj);
|
353
328
|
}
|
354
329
|
return Qnil;
|
@@ -361,58 +336,14 @@ VALUE Descriptor_each(VALUE _self) {
|
|
361
336
|
* Returns the field descriptor for the field with the given name, if present,
|
362
337
|
* or nil if none.
|
363
338
|
*/
|
364
|
-
VALUE Descriptor_lookup(VALUE _self, VALUE name) {
|
365
|
-
|
339
|
+
static VALUE Descriptor_lookup(VALUE _self, VALUE name) {
|
340
|
+
Descriptor* self = ruby_to_Descriptor(_self);
|
366
341
|
const char* s = get_str(name);
|
367
342
|
const upb_fielddef* field = upb_msgdef_ntofz(self->msgdef, s);
|
368
343
|
if (field == NULL) {
|
369
344
|
return Qnil;
|
370
345
|
}
|
371
|
-
return
|
372
|
-
}
|
373
|
-
|
374
|
-
/*
|
375
|
-
* call-seq:
|
376
|
-
* Descriptor.add_field(field) => nil
|
377
|
-
*
|
378
|
-
* Adds the given FieldDescriptor to this message type. This descriptor must not
|
379
|
-
* have been added to a pool yet. Raises an exception if a field with the same
|
380
|
-
* name or number already exists. Sub-type references (e.g. for fields of type
|
381
|
-
* message) are not resolved at this point.
|
382
|
-
*/
|
383
|
-
VALUE Descriptor_add_field(VALUE _self, VALUE obj) {
|
384
|
-
DEFINE_SELF(Descriptor, self, _self);
|
385
|
-
upb_msgdef* mut_def = check_msg_notfrozen(self->msgdef);
|
386
|
-
FieldDescriptor* def = ruby_to_FieldDescriptor(obj);
|
387
|
-
upb_fielddef* mut_field_def = check_field_notfrozen(def->fielddef);
|
388
|
-
CHECK_UPB(
|
389
|
-
upb_msgdef_addfield(mut_def, mut_field_def, NULL, &status),
|
390
|
-
"Adding field to Descriptor failed");
|
391
|
-
add_def_obj(def->fielddef, obj);
|
392
|
-
return Qnil;
|
393
|
-
}
|
394
|
-
|
395
|
-
/*
|
396
|
-
* call-seq:
|
397
|
-
* Descriptor.add_oneof(oneof) => nil
|
398
|
-
*
|
399
|
-
* Adds the given OneofDescriptor to this message type. This descriptor must not
|
400
|
-
* have been added to a pool yet. Raises an exception if a oneof with the same
|
401
|
-
* name already exists, or if any of the oneof's fields' names or numbers
|
402
|
-
* conflict with an existing field in this message type. All fields in the oneof
|
403
|
-
* are added to the message descriptor. Sub-type references (e.g. for fields of
|
404
|
-
* type message) are not resolved at this point.
|
405
|
-
*/
|
406
|
-
VALUE Descriptor_add_oneof(VALUE _self, VALUE obj) {
|
407
|
-
DEFINE_SELF(Descriptor, self, _self);
|
408
|
-
upb_msgdef* mut_def = check_msg_notfrozen(self->msgdef);
|
409
|
-
OneofDescriptor* def = ruby_to_OneofDescriptor(obj);
|
410
|
-
upb_oneofdef* mut_oneof_def = check_oneof_notfrozen(def->oneofdef);
|
411
|
-
CHECK_UPB(
|
412
|
-
upb_msgdef_addoneof(mut_def, mut_oneof_def, NULL, &status),
|
413
|
-
"Adding oneof to Descriptor failed");
|
414
|
-
add_def_obj(def->oneofdef, obj);
|
415
|
-
return Qnil;
|
346
|
+
return get_fielddef_obj(self->descriptor_pool, field);
|
416
347
|
}
|
417
348
|
|
418
349
|
/*
|
@@ -422,15 +353,15 @@ VALUE Descriptor_add_oneof(VALUE _self, VALUE obj) {
|
|
422
353
|
* Invokes the given block for each oneof in this message type, passing the
|
423
354
|
* corresponding OneofDescriptor.
|
424
355
|
*/
|
425
|
-
VALUE Descriptor_each_oneof(VALUE _self) {
|
426
|
-
|
356
|
+
static VALUE Descriptor_each_oneof(VALUE _self) {
|
357
|
+
Descriptor* self = ruby_to_Descriptor(_self);
|
427
358
|
|
428
359
|
upb_msg_oneof_iter it;
|
429
360
|
for (upb_msg_oneof_begin(&it, self->msgdef);
|
430
361
|
!upb_msg_oneof_done(&it);
|
431
362
|
upb_msg_oneof_next(&it)) {
|
432
363
|
const upb_oneofdef* oneof = upb_msg_iter_oneof(&it);
|
433
|
-
VALUE obj =
|
364
|
+
VALUE obj = get_oneofdef_obj(self->descriptor_pool, oneof);
|
434
365
|
rb_yield(obj);
|
435
366
|
}
|
436
367
|
return Qnil;
|
@@ -443,48 +374,173 @@ VALUE Descriptor_each_oneof(VALUE _self) {
|
|
443
374
|
* Returns the oneof descriptor for the oneof with the given name, if present,
|
444
375
|
* or nil if none.
|
445
376
|
*/
|
446
|
-
VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
|
447
|
-
|
377
|
+
static VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
|
378
|
+
Descriptor* self = ruby_to_Descriptor(_self);
|
448
379
|
const char* s = get_str(name);
|
449
380
|
const upb_oneofdef* oneof = upb_msgdef_ntooz(self->msgdef, s);
|
450
381
|
if (oneof == NULL) {
|
451
382
|
return Qnil;
|
452
383
|
}
|
453
|
-
return
|
384
|
+
return get_oneofdef_obj(self->descriptor_pool, oneof);
|
454
385
|
}
|
455
386
|
|
456
387
|
/*
|
457
388
|
* call-seq:
|
458
389
|
* Descriptor.msgclass => message_klass
|
459
390
|
*
|
460
|
-
* Returns the Ruby class created for this message type.
|
461
|
-
* message type has been added to a pool.
|
391
|
+
* Returns the Ruby class created for this message type.
|
462
392
|
*/
|
463
|
-
VALUE Descriptor_msgclass(VALUE _self) {
|
464
|
-
|
465
|
-
if (!upb_def_isfrozen((const upb_def*)self->msgdef)) {
|
466
|
-
rb_raise(rb_eRuntimeError,
|
467
|
-
"Cannot fetch message class from a Descriptor not yet in a pool.");
|
468
|
-
}
|
393
|
+
static VALUE Descriptor_msgclass(VALUE _self) {
|
394
|
+
Descriptor* self = ruby_to_Descriptor(_self);
|
469
395
|
if (self->klass == Qnil) {
|
470
|
-
self->klass = build_class_from_descriptor(
|
396
|
+
self->klass = build_class_from_descriptor(_self);
|
471
397
|
}
|
472
398
|
return self->klass;
|
473
399
|
}
|
474
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
|
+
|
475
418
|
// -----------------------------------------------------------------------------
|
476
|
-
//
|
419
|
+
// FileDescriptor.
|
477
420
|
// -----------------------------------------------------------------------------
|
478
421
|
|
479
|
-
|
422
|
+
typedef struct {
|
423
|
+
const upb_filedef* filedef;
|
424
|
+
VALUE descriptor_pool; // Owns the upb_filedef.
|
425
|
+
} FileDescriptor;
|
426
|
+
|
427
|
+
static VALUE cFileDescriptor = Qnil;
|
428
|
+
|
429
|
+
static void FileDescriptor_mark(void* _self) {
|
430
|
+
FileDescriptor* self = _self;
|
431
|
+
rb_gc_mark(self->descriptor_pool);
|
432
|
+
}
|
433
|
+
|
434
|
+
static const rb_data_type_t FileDescriptor_type = {
|
435
|
+
"Google::Protobuf::FileDescriptor",
|
436
|
+
{FileDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
|
437
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
|
438
|
+
};
|
439
|
+
|
440
|
+
static FileDescriptor* ruby_to_FileDescriptor(VALUE val) {
|
441
|
+
FileDescriptor* ret;
|
442
|
+
TypedData_Get_Struct(val, FileDescriptor, &FileDescriptor_type, ret);
|
443
|
+
return ret;
|
444
|
+
}
|
445
|
+
|
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;
|
452
|
+
}
|
453
|
+
|
454
|
+
/*
|
455
|
+
* call-seq:
|
456
|
+
* FileDescriptor.new => file
|
457
|
+
*
|
458
|
+
* Returns a new file descriptor. The syntax must be set before it's passed
|
459
|
+
* to a builder.
|
460
|
+
*/
|
461
|
+
static VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
|
462
|
+
VALUE descriptor_pool, VALUE ptr) {
|
463
|
+
FileDescriptor* self = ruby_to_FileDescriptor(_self);
|
464
|
+
|
465
|
+
if (cookie != c_only_cookie) {
|
466
|
+
rb_raise(rb_eRuntimeError,
|
467
|
+
"Descriptor objects may not be created from Ruby.");
|
468
|
+
}
|
469
|
+
|
470
|
+
self->descriptor_pool = descriptor_pool;
|
471
|
+
self->filedef = (const upb_filedef*)NUM2ULL(ptr);
|
472
|
+
|
473
|
+
return Qnil;
|
474
|
+
}
|
475
|
+
|
476
|
+
/*
|
477
|
+
* call-seq:
|
478
|
+
* FileDescriptor.name => name
|
479
|
+
*
|
480
|
+
* Returns the name of the file.
|
481
|
+
*/
|
482
|
+
static VALUE FileDescriptor_name(VALUE _self) {
|
483
|
+
FileDescriptor* self = ruby_to_FileDescriptor(_self);
|
484
|
+
const char* name = upb_filedef_name(self->filedef);
|
485
|
+
return name == NULL ? Qnil : rb_str_new2(name);
|
486
|
+
}
|
487
|
+
|
488
|
+
/*
|
489
|
+
* call-seq:
|
490
|
+
* FileDescriptor.syntax => syntax
|
491
|
+
*
|
492
|
+
* Returns this file descriptors syntax.
|
493
|
+
*
|
494
|
+
* Valid syntax versions are:
|
495
|
+
* :proto2 or :proto3.
|
496
|
+
*/
|
497
|
+
static VALUE FileDescriptor_syntax(VALUE _self) {
|
498
|
+
FileDescriptor* self = ruby_to_FileDescriptor(_self);
|
499
|
+
|
500
|
+
switch (upb_filedef_syntax(self->filedef)) {
|
501
|
+
case UPB_SYNTAX_PROTO3: return ID2SYM(rb_intern("proto3"));
|
502
|
+
case UPB_SYNTAX_PROTO2: return ID2SYM(rb_intern("proto2"));
|
503
|
+
default: return Qnil;
|
504
|
+
}
|
505
|
+
}
|
480
506
|
|
481
|
-
void
|
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;
|
482
516
|
}
|
483
517
|
|
484
|
-
|
518
|
+
// -----------------------------------------------------------------------------
|
519
|
+
// FieldDescriptor.
|
520
|
+
// -----------------------------------------------------------------------------
|
521
|
+
|
522
|
+
typedef struct {
|
523
|
+
const upb_fielddef* fielddef;
|
524
|
+
VALUE descriptor_pool; // Owns the upb_fielddef.
|
525
|
+
} FieldDescriptor;
|
526
|
+
|
527
|
+
static VALUE cFieldDescriptor = Qnil;
|
528
|
+
|
529
|
+
static void FieldDescriptor_mark(void* _self) {
|
485
530
|
FieldDescriptor* self = _self;
|
486
|
-
|
487
|
-
|
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;
|
488
544
|
}
|
489
545
|
|
490
546
|
/*
|
@@ -494,63 +550,46 @@ void FieldDescriptor_free(void* _self) {
|
|
494
550
|
* Returns a new field descriptor. Its name, type, etc. must be set before it is
|
495
551
|
* added to a message type.
|
496
552
|
*/
|
497
|
-
VALUE FieldDescriptor_alloc(VALUE klass) {
|
553
|
+
static VALUE FieldDescriptor_alloc(VALUE klass) {
|
498
554
|
FieldDescriptor* self = ALLOC(FieldDescriptor);
|
499
|
-
VALUE ret = TypedData_Wrap_Struct(klass, &
|
500
|
-
|
501
|
-
upb_fielddef_setpacked(fielddef, false);
|
502
|
-
self->fielddef = fielddef;
|
555
|
+
VALUE ret = TypedData_Wrap_Struct(klass, &FieldDescriptor_type, self);
|
556
|
+
self->fielddef = NULL;
|
503
557
|
return ret;
|
504
558
|
}
|
505
559
|
|
506
|
-
void FieldDescriptor_register(VALUE module) {
|
507
|
-
VALUE klass = rb_define_class_under(
|
508
|
-
module, "FieldDescriptor", rb_cObject);
|
509
|
-
rb_define_alloc_func(klass, FieldDescriptor_alloc);
|
510
|
-
rb_define_method(klass, "name", FieldDescriptor_name, 0);
|
511
|
-
rb_define_method(klass, "name=", FieldDescriptor_name_set, 1);
|
512
|
-
rb_define_method(klass, "type", FieldDescriptor_type, 0);
|
513
|
-
rb_define_method(klass, "type=", FieldDescriptor_type_set, 1);
|
514
|
-
rb_define_method(klass, "label", FieldDescriptor_label, 0);
|
515
|
-
rb_define_method(klass, "label=", FieldDescriptor_label_set, 1);
|
516
|
-
rb_define_method(klass, "number", FieldDescriptor_number, 0);
|
517
|
-
rb_define_method(klass, "number=", FieldDescriptor_number_set, 1);
|
518
|
-
rb_define_method(klass, "submsg_name", FieldDescriptor_submsg_name, 0);
|
519
|
-
rb_define_method(klass, "submsg_name=", FieldDescriptor_submsg_name_set, 1);
|
520
|
-
rb_define_method(klass, "subtype", FieldDescriptor_subtype, 0);
|
521
|
-
rb_define_method(klass, "get", FieldDescriptor_get, 1);
|
522
|
-
rb_define_method(klass, "set", FieldDescriptor_set, 2);
|
523
|
-
cFieldDescriptor = klass;
|
524
|
-
rb_gc_register_address(&cFieldDescriptor);
|
525
|
-
}
|
526
|
-
|
527
560
|
/*
|
528
561
|
* call-seq:
|
529
|
-
*
|
562
|
+
* EnumDescriptor.new(c_only_cookie, pool, ptr) => EnumDescriptor
|
530
563
|
*
|
531
|
-
*
|
564
|
+
* Creates a descriptor wrapper object. May only be called from C.
|
532
565
|
*/
|
533
|
-
VALUE
|
534
|
-
|
535
|
-
|
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;
|
536
579
|
}
|
537
580
|
|
538
581
|
/*
|
539
582
|
* call-seq:
|
540
|
-
* FieldDescriptor.name
|
583
|
+
* FieldDescriptor.name => name
|
541
584
|
*
|
542
|
-
*
|
543
|
-
* type, if any, is added to a pool.
|
585
|
+
* Returns the name of this field.
|
544
586
|
*/
|
545
|
-
VALUE
|
546
|
-
|
547
|
-
|
548
|
-
const char* name = get_str(str);
|
549
|
-
CHECK_UPB(upb_fielddef_setname(mut_def, name, &status),
|
550
|
-
"Error setting FieldDescriptor name");
|
551
|
-
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));
|
552
590
|
}
|
553
591
|
|
592
|
+
// Non-static, exposed to other .c files.
|
554
593
|
upb_fieldtype_t ruby_to_fieldtype(VALUE type) {
|
555
594
|
if (TYPE(type) != T_SYMBOL) {
|
556
595
|
rb_raise(rb_eArgError, "Expected symbol for field type.");
|
@@ -579,118 +618,78 @@ upb_fieldtype_t ruby_to_fieldtype(VALUE type) {
|
|
579
618
|
return 0;
|
580
619
|
}
|
581
620
|
|
582
|
-
VALUE
|
621
|
+
static VALUE descriptortype_to_ruby(upb_descriptortype_t type) {
|
583
622
|
switch (type) {
|
584
623
|
#define CONVERT(upb, ruby) \
|
585
|
-
case
|
624
|
+
case UPB_DESCRIPTOR_TYPE_ ## upb : return ID2SYM(rb_intern( # ruby ));
|
586
625
|
CONVERT(FLOAT, float);
|
587
626
|
CONVERT(DOUBLE, double);
|
588
627
|
CONVERT(BOOL, bool);
|
589
628
|
CONVERT(STRING, string);
|
590
629
|
CONVERT(BYTES, bytes);
|
591
630
|
CONVERT(MESSAGE, message);
|
631
|
+
CONVERT(GROUP, group);
|
592
632
|
CONVERT(ENUM, enum);
|
593
633
|
CONVERT(INT32, int32);
|
594
634
|
CONVERT(INT64, int64);
|
595
635
|
CONVERT(UINT32, uint32);
|
596
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);
|
597
643
|
#undef CONVERT
|
598
644
|
}
|
599
645
|
return Qnil;
|
600
646
|
}
|
601
647
|
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
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
|
+
}
|
606
662
|
|
607
|
-
|
608
|
-
|
609
|
-
|
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);
|
610
677
|
}
|
678
|
+
return Convert_UpbToRuby(default_val, TypeInfo_get(self->fielddef), Qnil);
|
679
|
+
}
|
611
680
|
|
612
|
-
CONVERT(FLOAT, float);
|
613
|
-
CONVERT(DOUBLE, double);
|
614
|
-
CONVERT(BOOL, bool);
|
615
|
-
CONVERT(STRING, string);
|
616
|
-
CONVERT(BYTES, bytes);
|
617
|
-
CONVERT(MESSAGE, message);
|
618
|
-
CONVERT(GROUP, group);
|
619
|
-
CONVERT(ENUM, enum);
|
620
|
-
CONVERT(INT32, int32);
|
621
|
-
CONVERT(INT64, int64);
|
622
|
-
CONVERT(UINT32, uint32);
|
623
|
-
CONVERT(UINT64, uint64);
|
624
|
-
CONVERT(SINT32, sint32);
|
625
|
-
CONVERT(SINT64, sint64);
|
626
|
-
CONVERT(FIXED32, fixed32);
|
627
|
-
CONVERT(FIXED64, fixed64);
|
628
|
-
CONVERT(SFIXED32, sfixed32);
|
629
|
-
CONVERT(SFIXED64, sfixed64);
|
630
|
-
|
631
|
-
#undef CONVERT
|
632
|
-
|
633
|
-
rb_raise(rb_eArgError, "Unknown field type.");
|
634
|
-
return 0;
|
635
|
-
}
|
636
|
-
|
637
|
-
VALUE descriptortype_to_ruby(upb_descriptortype_t type) {
|
638
|
-
switch (type) {
|
639
|
-
#define CONVERT(upb, ruby) \
|
640
|
-
case UPB_DESCRIPTOR_TYPE_ ## upb : return ID2SYM(rb_intern( # ruby ));
|
641
|
-
CONVERT(FLOAT, float);
|
642
|
-
CONVERT(DOUBLE, double);
|
643
|
-
CONVERT(BOOL, bool);
|
644
|
-
CONVERT(STRING, string);
|
645
|
-
CONVERT(BYTES, bytes);
|
646
|
-
CONVERT(MESSAGE, message);
|
647
|
-
CONVERT(GROUP, group);
|
648
|
-
CONVERT(ENUM, enum);
|
649
|
-
CONVERT(INT32, int32);
|
650
|
-
CONVERT(INT64, int64);
|
651
|
-
CONVERT(UINT32, uint32);
|
652
|
-
CONVERT(UINT64, uint64);
|
653
|
-
CONVERT(SINT32, sint32);
|
654
|
-
CONVERT(SINT64, sint64);
|
655
|
-
CONVERT(FIXED32, fixed32);
|
656
|
-
CONVERT(FIXED64, fixed64);
|
657
|
-
CONVERT(SFIXED32, sfixed32);
|
658
|
-
CONVERT(SFIXED64, sfixed64);
|
659
|
-
#undef CONVERT
|
660
|
-
}
|
661
|
-
return Qnil;
|
662
|
-
}
|
663
681
|
|
664
682
|
/*
|
665
683
|
* call-seq:
|
666
|
-
* FieldDescriptor.
|
667
|
-
*
|
668
|
-
* Returns this field's type, as a Ruby symbol, or nil if not yet set.
|
684
|
+
* FieldDescriptor.json_name => json_name
|
669
685
|
*
|
670
|
-
*
|
671
|
-
* :int32, :int64, :uint32, :uint64, :float, :double, :bool, :string,
|
672
|
-
* :bytes, :message.
|
686
|
+
* Returns this field's json_name, as a Ruby string, or nil if not yet set.
|
673
687
|
*/
|
674
|
-
VALUE
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
return descriptortype_to_ruby(upb_fielddef_descriptortype(self->fielddef));
|
680
|
-
}
|
681
|
-
|
682
|
-
/*
|
683
|
-
* call-seq:
|
684
|
-
* FieldDescriptor.type = type
|
685
|
-
*
|
686
|
-
* Sets this field's type. Cannot be called if field is part of a message type
|
687
|
-
* already in a pool.
|
688
|
-
*/
|
689
|
-
VALUE FieldDescriptor_type_set(VALUE _self, VALUE type) {
|
690
|
-
DEFINE_SELF(FieldDescriptor, self, _self);
|
691
|
-
upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
|
692
|
-
upb_fielddef_setdescriptortype(mut_def, ruby_to_descriptortype(type));
|
693
|
-
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);
|
694
693
|
}
|
695
694
|
|
696
695
|
/*
|
@@ -702,8 +701,8 @@ VALUE FieldDescriptor_type_set(VALUE _self, VALUE type) {
|
|
702
701
|
* Valid field labels are:
|
703
702
|
* :optional, :repeated
|
704
703
|
*/
|
705
|
-
VALUE FieldDescriptor_label(VALUE _self) {
|
706
|
-
|
704
|
+
static VALUE FieldDescriptor_label(VALUE _self) {
|
705
|
+
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
|
707
706
|
switch (upb_fielddef_label(self->fielddef)) {
|
708
707
|
#define CONVERT(upb, ruby) \
|
709
708
|
case UPB_LABEL_ ## upb : return ID2SYM(rb_intern( # ruby ));
|
@@ -718,70 +717,17 @@ VALUE FieldDescriptor_label(VALUE _self) {
|
|
718
717
|
return Qnil;
|
719
718
|
}
|
720
719
|
|
721
|
-
/*
|
722
|
-
* call-seq:
|
723
|
-
* FieldDescriptor.label = label
|
724
|
-
*
|
725
|
-
* Sets the label on this field. Cannot be called if field is part of a message
|
726
|
-
* type already in a pool.
|
727
|
-
*/
|
728
|
-
VALUE FieldDescriptor_label_set(VALUE _self, VALUE label) {
|
729
|
-
DEFINE_SELF(FieldDescriptor, self, _self);
|
730
|
-
upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
|
731
|
-
upb_label_t upb_label = -1;
|
732
|
-
bool converted = false;
|
733
|
-
|
734
|
-
if (TYPE(label) != T_SYMBOL) {
|
735
|
-
rb_raise(rb_eArgError, "Expected symbol for field label.");
|
736
|
-
}
|
737
|
-
|
738
|
-
#define CONVERT(upb, ruby) \
|
739
|
-
if (SYM2ID(label) == rb_intern( # ruby )) { \
|
740
|
-
upb_label = UPB_LABEL_ ## upb; \
|
741
|
-
converted = true; \
|
742
|
-
}
|
743
|
-
|
744
|
-
CONVERT(OPTIONAL, optional);
|
745
|
-
CONVERT(REQUIRED, required);
|
746
|
-
CONVERT(REPEATED, repeated);
|
747
|
-
|
748
|
-
#undef CONVERT
|
749
|
-
|
750
|
-
if (!converted) {
|
751
|
-
rb_raise(rb_eArgError, "Unknown field label.");
|
752
|
-
}
|
753
|
-
|
754
|
-
upb_fielddef_setlabel(mut_def, upb_label);
|
755
|
-
|
756
|
-
return Qnil;
|
757
|
-
}
|
758
|
-
|
759
720
|
/*
|
760
721
|
* call-seq:
|
761
722
|
* FieldDescriptor.number => number
|
762
723
|
*
|
763
724
|
* Returns the tag number for this field.
|
764
725
|
*/
|
765
|
-
VALUE FieldDescriptor_number(VALUE _self) {
|
766
|
-
|
726
|
+
static VALUE FieldDescriptor_number(VALUE _self) {
|
727
|
+
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
|
767
728
|
return INT2NUM(upb_fielddef_number(self->fielddef));
|
768
729
|
}
|
769
730
|
|
770
|
-
/*
|
771
|
-
* call-seq:
|
772
|
-
* FieldDescriptor.number = number
|
773
|
-
*
|
774
|
-
* Sets the tag number for this field. Cannot be called if field is part of a
|
775
|
-
* message type already in a pool.
|
776
|
-
*/
|
777
|
-
VALUE FieldDescriptor_number_set(VALUE _self, VALUE number) {
|
778
|
-
DEFINE_SELF(FieldDescriptor, self, _self);
|
779
|
-
upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
|
780
|
-
CHECK_UPB(upb_fielddef_setnumber(mut_def, NUM2INT(number), &status),
|
781
|
-
"Error setting field number");
|
782
|
-
return Qnil;
|
783
|
-
}
|
784
|
-
|
785
731
|
/*
|
786
732
|
* call-seq:
|
787
733
|
* FieldDescriptor.submsg_name => submsg_name
|
@@ -791,74 +737,101 @@ VALUE FieldDescriptor_number_set(VALUE _self, VALUE number) {
|
|
791
737
|
* name will be resolved within the context of the pool to which the containing
|
792
738
|
* message type is added.
|
793
739
|
*/
|
794
|
-
VALUE FieldDescriptor_submsg_name(VALUE _self) {
|
795
|
-
|
796
|
-
|
797
|
-
|
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;
|
798
751
|
}
|
799
|
-
return rb_str_maybe_null(upb_fielddef_subdefname(self->fielddef));
|
800
752
|
}
|
801
753
|
|
802
754
|
/*
|
803
755
|
* call-seq:
|
804
|
-
* FieldDescriptor.
|
756
|
+
* FieldDescriptor.subtype => message_or_enum_descriptor
|
805
757
|
*
|
806
|
-
*
|
807
|
-
* is a message or enum field
|
808
|
-
*
|
809
|
-
*
|
810
|
-
* that are part of a message type already added to a pool.
|
758
|
+
* Returns the message or enum descriptor corresponding to this field's type if
|
759
|
+
* it is a message or enum field, respectively, or nil otherwise. Cannot be
|
760
|
+
* called *until* the containing message type is added to a pool (and thus
|
761
|
+
* resolved).
|
811
762
|
*/
|
812
|
-
VALUE
|
813
|
-
|
814
|
-
|
815
|
-
|
816
|
-
|
817
|
-
|
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;
|
818
774
|
}
|
819
|
-
CHECK_UPB(upb_fielddef_setsubdefname(mut_def, str, &status),
|
820
|
-
"Error setting submessage name");
|
821
|
-
return Qnil;
|
822
775
|
}
|
823
776
|
|
824
777
|
/*
|
825
778
|
* call-seq:
|
826
|
-
* FieldDescriptor.
|
779
|
+
* FieldDescriptor.get(message) => value
|
827
780
|
*
|
828
|
-
* Returns the
|
829
|
-
*
|
830
|
-
* called *until* the containing message type is added to a pool (and thus
|
831
|
-
* resolved).
|
781
|
+
* Returns the value set for this field on the given message. Raises an
|
782
|
+
* exception if message is of the wrong type.
|
832
783
|
*/
|
833
|
-
VALUE
|
834
|
-
|
835
|
-
const
|
784
|
+
static VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
|
785
|
+
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
|
786
|
+
const upb_msgdef *m;
|
836
787
|
|
837
|
-
|
838
|
-
|
788
|
+
Message_Get(msg_rb, &m);
|
789
|
+
|
790
|
+
if (m != upb_fielddef_containingtype(self->fielddef)) {
|
791
|
+
rb_raise(cTypeError, "get method called on wrong message type");
|
839
792
|
}
|
840
|
-
|
841
|
-
|
842
|
-
|
793
|
+
|
794
|
+
return Message_getfield(msg_rb, self->fielddef);
|
795
|
+
}
|
796
|
+
|
797
|
+
/*
|
798
|
+
* call-seq:
|
799
|
+
* FieldDescriptor.has?(message) => boolean
|
800
|
+
*
|
801
|
+
* Returns whether the value is set on the given message. Raises an
|
802
|
+
* exception when calling for fields that do not have presence.
|
803
|
+
*/
|
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)) {
|
810
|
+
rb_raise(cTypeError, "has method called on wrong message type");
|
811
|
+
} else if (!upb_fielddef_haspresence(self->fielddef)) {
|
812
|
+
rb_raise(rb_eArgError, "does not track presence");
|
843
813
|
}
|
844
|
-
|
814
|
+
|
815
|
+
return upb_msg_has(msg, self->fielddef) ? Qtrue : Qfalse;
|
845
816
|
}
|
846
817
|
|
847
818
|
/*
|
848
819
|
* call-seq:
|
849
|
-
* FieldDescriptor.
|
820
|
+
* FieldDescriptor.clear(message)
|
850
821
|
*
|
851
|
-
*
|
852
|
-
* exception if message is of the wrong type.
|
822
|
+
* Clears the field from the message if it's set.
|
853
823
|
*/
|
854
|
-
VALUE
|
855
|
-
|
856
|
-
|
857
|
-
|
858
|
-
|
859
|
-
|
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)) {
|
830
|
+
rb_raise(cTypeError, "has method called on wrong message type");
|
860
831
|
}
|
861
|
-
|
832
|
+
|
833
|
+
upb_msg_clearfield(msg, self->fielddef);
|
834
|
+
return Qnil;
|
862
835
|
}
|
863
836
|
|
864
837
|
/*
|
@@ -869,30 +842,70 @@ VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
|
|
869
842
|
* message. Raises an exception if message is of the wrong type. Performs the
|
870
843
|
* ordinary type-checks for field setting.
|
871
844
|
*/
|
872
|
-
VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value) {
|
873
|
-
|
874
|
-
|
875
|
-
|
876
|
-
|
877
|
-
|
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)) {
|
853
|
+
rb_raise(cTypeError, "set method called on wrong message type");
|
878
854
|
}
|
879
|
-
|
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);
|
880
859
|
return Qnil;
|
881
860
|
}
|
882
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
|
+
|
883
883
|
// -----------------------------------------------------------------------------
|
884
884
|
// OneofDescriptor.
|
885
885
|
// -----------------------------------------------------------------------------
|
886
886
|
|
887
|
-
|
887
|
+
typedef struct {
|
888
|
+
const upb_oneofdef* oneofdef;
|
889
|
+
VALUE descriptor_pool; // Owns the upb_oneofdef.
|
890
|
+
} OneofDescriptor;
|
888
891
|
|
889
|
-
|
890
|
-
}
|
892
|
+
static VALUE cOneofDescriptor = Qnil;
|
891
893
|
|
892
|
-
void
|
894
|
+
static void OneofDescriptor_mark(void* _self) {
|
893
895
|
OneofDescriptor* self = _self;
|
894
|
-
|
895
|
-
|
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;
|
896
909
|
}
|
897
910
|
|
898
911
|
/*
|
@@ -902,77 +915,44 @@ void OneofDescriptor_free(void* _self) {
|
|
902
915
|
* Creates a new, empty, oneof descriptor. The oneof may only be modified prior
|
903
916
|
* to being added to a message descriptor which is subsequently added to a pool.
|
904
917
|
*/
|
905
|
-
VALUE OneofDescriptor_alloc(VALUE klass) {
|
918
|
+
static VALUE OneofDescriptor_alloc(VALUE klass) {
|
906
919
|
OneofDescriptor* self = ALLOC(OneofDescriptor);
|
907
|
-
VALUE ret = TypedData_Wrap_Struct(klass, &
|
908
|
-
self->oneofdef =
|
920
|
+
VALUE ret = TypedData_Wrap_Struct(klass, &OneofDescriptor_type, self);
|
921
|
+
self->oneofdef = NULL;
|
922
|
+
self->descriptor_pool = Qnil;
|
909
923
|
return ret;
|
910
924
|
}
|
911
925
|
|
912
|
-
void OneofDescriptor_register(VALUE module) {
|
913
|
-
VALUE klass = rb_define_class_under(
|
914
|
-
module, "OneofDescriptor", rb_cObject);
|
915
|
-
rb_define_alloc_func(klass, OneofDescriptor_alloc);
|
916
|
-
rb_define_method(klass, "name", OneofDescriptor_name, 0);
|
917
|
-
rb_define_method(klass, "name=", OneofDescriptor_name_set, 1);
|
918
|
-
rb_define_method(klass, "add_field", OneofDescriptor_add_field, 1);
|
919
|
-
rb_define_method(klass, "each", OneofDescriptor_each, 0);
|
920
|
-
rb_include_module(klass, rb_mEnumerable);
|
921
|
-
cOneofDescriptor = klass;
|
922
|
-
rb_gc_register_address(&cOneofDescriptor);
|
923
|
-
}
|
924
|
-
|
925
926
|
/*
|
926
927
|
* call-seq:
|
927
|
-
*
|
928
|
+
* OneofDescriptor.new(c_only_cookie, pool, ptr) => OneofDescriptor
|
928
929
|
*
|
929
|
-
*
|
930
|
+
* Creates a descriptor wrapper object. May only be called from C.
|
930
931
|
*/
|
931
|
-
VALUE
|
932
|
-
|
933
|
-
|
934
|
-
|
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);
|
935
943
|
|
936
|
-
/*
|
937
|
-
* call-seq:
|
938
|
-
* OneofDescriptor.name = name
|
939
|
-
*
|
940
|
-
* Sets a new name for this oneof. The oneof must not have been added to a
|
941
|
-
* message descriptor yet.
|
942
|
-
*/
|
943
|
-
VALUE OneofDescriptor_name_set(VALUE _self, VALUE value) {
|
944
|
-
DEFINE_SELF(OneofDescriptor, self, _self);
|
945
|
-
upb_oneofdef* mut_def = check_oneof_notfrozen(self->oneofdef);
|
946
|
-
const char* str = get_str(value);
|
947
|
-
CHECK_UPB(upb_oneofdef_setname(mut_def, str, &status),
|
948
|
-
"Error setting oneof name");
|
949
944
|
return Qnil;
|
950
945
|
}
|
951
946
|
|
952
947
|
/*
|
953
948
|
* call-seq:
|
954
|
-
* OneofDescriptor.
|
955
|
-
*
|
956
|
-
* Adds a field to this oneof. The field may have been added to this oneof in
|
957
|
-
* the past, or the message to which this oneof belongs (if any), but may not
|
958
|
-
* have already been added to any other oneof or message. Otherwise, an
|
959
|
-
* exception is raised.
|
949
|
+
* OneofDescriptor.name => name
|
960
950
|
*
|
961
|
-
*
|
962
|
-
* the message to which this oneof belongs, if it belongs to one currently, or
|
963
|
-
* else will be added to any message to which the oneof is later added at the
|
964
|
-
* time that it is added.
|
951
|
+
* Returns the name of this oneof.
|
965
952
|
*/
|
966
|
-
VALUE
|
967
|
-
|
968
|
-
|
969
|
-
FieldDescriptor* def = ruby_to_FieldDescriptor(obj);
|
970
|
-
upb_fielddef* mut_field_def = check_field_notfrozen(def->fielddef);
|
971
|
-
CHECK_UPB(
|
972
|
-
upb_oneofdef_addfield(mut_def, mut_field_def, NULL, &status),
|
973
|
-
"Adding field to OneofDescriptor failed");
|
974
|
-
add_def_obj(def->fielddef, obj);
|
975
|
-
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));
|
976
956
|
}
|
977
957
|
|
978
958
|
/*
|
@@ -981,111 +961,118 @@ VALUE OneofDescriptor_add_field(VALUE _self, VALUE obj) {
|
|
981
961
|
*
|
982
962
|
* Iterates through fields in this oneof, yielding to the block on each one.
|
983
963
|
*/
|
984
|
-
VALUE OneofDescriptor_each(VALUE _self
|
985
|
-
|
964
|
+
static VALUE OneofDescriptor_each(VALUE _self) {
|
965
|
+
OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
|
986
966
|
upb_oneof_iter it;
|
987
967
|
for (upb_oneof_begin(&it, self->oneofdef);
|
988
968
|
!upb_oneof_done(&it);
|
989
969
|
upb_oneof_next(&it)) {
|
990
970
|
const upb_fielddef* f = upb_oneof_iter_field(&it);
|
991
|
-
VALUE obj =
|
971
|
+
VALUE obj = get_fielddef_obj(self->descriptor_pool, f);
|
992
972
|
rb_yield(obj);
|
993
973
|
}
|
994
974
|
return Qnil;
|
995
975
|
}
|
996
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
|
+
|
997
989
|
// -----------------------------------------------------------------------------
|
998
990
|
// EnumDescriptor.
|
999
991
|
// -----------------------------------------------------------------------------
|
1000
992
|
|
1001
|
-
|
993
|
+
typedef struct {
|
994
|
+
const upb_enumdef* enumdef;
|
995
|
+
VALUE module; // begins as nil
|
996
|
+
VALUE descriptor_pool; // Owns the upb_enumdef.
|
997
|
+
} EnumDescriptor;
|
1002
998
|
|
1003
|
-
|
999
|
+
static VALUE cEnumDescriptor = Qnil;
|
1000
|
+
|
1001
|
+
static void EnumDescriptor_mark(void* _self) {
|
1004
1002
|
EnumDescriptor* self = _self;
|
1005
1003
|
rb_gc_mark(self->module);
|
1004
|
+
rb_gc_mark(self->descriptor_pool);
|
1006
1005
|
}
|
1007
1006
|
|
1008
|
-
|
1009
|
-
EnumDescriptor
|
1010
|
-
|
1011
|
-
|
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;
|
1012
1017
|
}
|
1013
1018
|
|
1014
|
-
|
1015
|
-
* call-seq:
|
1016
|
-
* EnumDescriptor.new => enum_descriptor
|
1017
|
-
*
|
1018
|
-
* Creates a new, empty, enum descriptor. Must be added to a pool before the
|
1019
|
-
* enum type can be used. The enum type may only be modified prior to adding to
|
1020
|
-
* a pool.
|
1021
|
-
*/
|
1022
|
-
VALUE EnumDescriptor_alloc(VALUE klass) {
|
1019
|
+
static VALUE EnumDescriptor_alloc(VALUE klass) {
|
1023
1020
|
EnumDescriptor* self = ALLOC(EnumDescriptor);
|
1024
|
-
VALUE ret = TypedData_Wrap_Struct(klass, &
|
1025
|
-
self->enumdef =
|
1021
|
+
VALUE ret = TypedData_Wrap_Struct(klass, &EnumDescriptor_type, self);
|
1022
|
+
self->enumdef = NULL;
|
1026
1023
|
self->module = Qnil;
|
1024
|
+
self->descriptor_pool = Qnil;
|
1027
1025
|
return ret;
|
1028
1026
|
}
|
1029
1027
|
|
1030
|
-
|
1031
|
-
|
1032
|
-
|
1033
|
-
|
1034
|
-
rb_define_method(klass, "name", EnumDescriptor_name, 0);
|
1035
|
-
rb_define_method(klass, "name=", EnumDescriptor_name_set, 1);
|
1036
|
-
rb_define_method(klass, "add_value", EnumDescriptor_add_value, 2);
|
1037
|
-
rb_define_method(klass, "lookup_name", EnumDescriptor_lookup_name, 1);
|
1038
|
-
rb_define_method(klass, "lookup_value", EnumDescriptor_lookup_value, 1);
|
1039
|
-
rb_define_method(klass, "each", EnumDescriptor_each, 0);
|
1040
|
-
rb_define_method(klass, "enummodule", EnumDescriptor_enummodule, 0);
|
1041
|
-
rb_include_module(klass, rb_mEnumerable);
|
1042
|
-
cEnumDescriptor = klass;
|
1043
|
-
rb_gc_register_address(&cEnumDescriptor);
|
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;
|
1044
1032
|
}
|
1045
1033
|
|
1046
1034
|
/*
|
1047
1035
|
* call-seq:
|
1048
|
-
*
|
1036
|
+
* EnumDescriptor.new(c_only_cookie, ptr) => EnumDescriptor
|
1049
1037
|
*
|
1050
|
-
*
|
1038
|
+
* Creates a descriptor wrapper object. May only be called from C.
|
1051
1039
|
*/
|
1052
|
-
VALUE
|
1053
|
-
|
1054
|
-
|
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);
|
1051
|
+
|
1052
|
+
return Qnil;
|
1055
1053
|
}
|
1056
1054
|
|
1057
1055
|
/*
|
1058
1056
|
* call-seq:
|
1059
|
-
*
|
1057
|
+
* EnumDescriptor.file_descriptor
|
1060
1058
|
*
|
1061
|
-
*
|
1062
|
-
* already been added to a pool.
|
1059
|
+
* Returns the FileDescriptor object this enum belongs to.
|
1063
1060
|
*/
|
1064
|
-
VALUE
|
1065
|
-
|
1066
|
-
|
1067
|
-
|
1068
|
-
CHECK_UPB(upb_enumdef_setfullname(mut_def, name, &status),
|
1069
|
-
"Error setting EnumDescriptor name");
|
1070
|
-
return Qnil;
|
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));
|
1071
1065
|
}
|
1072
1066
|
|
1073
1067
|
/*
|
1074
1068
|
* call-seq:
|
1075
|
-
* EnumDescriptor.
|
1069
|
+
* EnumDescriptor.name => name
|
1076
1070
|
*
|
1077
|
-
*
|
1078
|
-
* Ruby symbol. Cannot be called if the enum type has already been added to a
|
1079
|
-
* pool. Will raise an exception if the key or value is already in use.
|
1071
|
+
* Returns the name of this enum type.
|
1080
1072
|
*/
|
1081
|
-
VALUE
|
1082
|
-
|
1083
|
-
|
1084
|
-
const char* name_str = rb_id2name(SYM2ID(name));
|
1085
|
-
int32_t val = NUM2INT(number);
|
1086
|
-
CHECK_UPB(upb_enumdef_addval(mut_def, name_str, val, &status),
|
1087
|
-
"Error adding value to enum");
|
1088
|
-
return Qnil;
|
1073
|
+
static VALUE EnumDescriptor_name(VALUE _self) {
|
1074
|
+
EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
|
1075
|
+
return rb_str_maybe_null(upb_enumdef_fullname(self->enumdef));
|
1089
1076
|
}
|
1090
1077
|
|
1091
1078
|
/*
|
@@ -1095,8 +1082,8 @@ VALUE EnumDescriptor_add_value(VALUE _self, VALUE name, VALUE number) {
|
|
1095
1082
|
* Returns the numeric value corresponding to the given key name (as a Ruby
|
1096
1083
|
* symbol), or nil if none.
|
1097
1084
|
*/
|
1098
|
-
VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
|
1099
|
-
|
1085
|
+
static VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
|
1086
|
+
EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
|
1100
1087
|
const char* name_str= rb_id2name(SYM2ID(name));
|
1101
1088
|
int32_t val = 0;
|
1102
1089
|
if (upb_enumdef_ntoiz(self->enumdef, name_str, &val)) {
|
@@ -1113,8 +1100,8 @@ VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
|
|
1113
1100
|
* Returns the key name (as a Ruby symbol) corresponding to the integer value,
|
1114
1101
|
* or nil if none.
|
1115
1102
|
*/
|
1116
|
-
VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
|
1117
|
-
|
1103
|
+
static VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
|
1104
|
+
EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
|
1118
1105
|
int32_t val = NUM2INT(number);
|
1119
1106
|
const char* name = upb_enumdef_iton(self->enumdef, val);
|
1120
1107
|
if (name != NULL) {
|
@@ -1131,8 +1118,8 @@ VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
|
|
1131
1118
|
* Iterates over key => value mappings in this enum's definition, yielding to
|
1132
1119
|
* the block with (key, value) arguments for each one.
|
1133
1120
|
*/
|
1134
|
-
VALUE EnumDescriptor_each(VALUE _self) {
|
1135
|
-
|
1121
|
+
static VALUE EnumDescriptor_each(VALUE _self) {
|
1122
|
+
EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
|
1136
1123
|
|
1137
1124
|
upb_enum_iter it;
|
1138
1125
|
for (upb_enum_begin(&it, self->enumdef);
|
@@ -1150,614 +1137,148 @@ VALUE EnumDescriptor_each(VALUE _self) {
|
|
1150
1137
|
* call-seq:
|
1151
1138
|
* EnumDescriptor.enummodule => module
|
1152
1139
|
*
|
1153
|
-
* Returns the Ruby module corresponding to this enum type.
|
1154
|
-
* until the enum descriptor has been added to a pool.
|
1140
|
+
* Returns the Ruby module corresponding to this enum type.
|
1155
1141
|
*/
|
1156
|
-
VALUE EnumDescriptor_enummodule(VALUE _self) {
|
1157
|
-
|
1158
|
-
if (!upb_def_isfrozen((const upb_def*)self->enumdef)) {
|
1159
|
-
rb_raise(rb_eRuntimeError,
|
1160
|
-
"Cannot fetch enum module from an EnumDescriptor not yet "
|
1161
|
-
"in a pool.");
|
1162
|
-
}
|
1142
|
+
static VALUE EnumDescriptor_enummodule(VALUE _self) {
|
1143
|
+
EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
|
1163
1144
|
if (self->module == Qnil) {
|
1164
|
-
self->module = build_module_from_enumdesc(
|
1145
|
+
self->module = build_module_from_enumdesc(_self);
|
1165
1146
|
}
|
1166
1147
|
return self->module;
|
1167
1148
|
}
|
1168
1149
|
|
1169
|
-
|
1170
|
-
// MessageBuilderContext.
|
1171
|
-
// -----------------------------------------------------------------------------
|
1172
|
-
|
1173
|
-
DEFINE_CLASS(MessageBuilderContext,
|
1174
|
-
"Google::Protobuf::Internal::MessageBuilderContext");
|
1175
|
-
|
1176
|
-
void MessageBuilderContext_mark(void* _self) {
|
1177
|
-
MessageBuilderContext* self = _self;
|
1178
|
-
rb_gc_mark(self->descriptor);
|
1179
|
-
rb_gc_mark(self->builder);
|
1180
|
-
}
|
1181
|
-
|
1182
|
-
void MessageBuilderContext_free(void* _self) {
|
1183
|
-
MessageBuilderContext* self = _self;
|
1184
|
-
xfree(self);
|
1185
|
-
}
|
1186
|
-
|
1187
|
-
VALUE MessageBuilderContext_alloc(VALUE klass) {
|
1188
|
-
MessageBuilderContext* self = ALLOC(MessageBuilderContext);
|
1189
|
-
VALUE ret = TypedData_Wrap_Struct(
|
1190
|
-
klass, &_MessageBuilderContext_type, self);
|
1191
|
-
self->descriptor = Qnil;
|
1192
|
-
self->builder = Qnil;
|
1193
|
-
return ret;
|
1194
|
-
}
|
1195
|
-
|
1196
|
-
void MessageBuilderContext_register(VALUE module) {
|
1150
|
+
static void EnumDescriptor_register(VALUE module) {
|
1197
1151
|
VALUE klass = rb_define_class_under(
|
1198
|
-
module, "
|
1199
|
-
rb_define_alloc_func(klass,
|
1200
|
-
rb_define_method(klass, "initialize",
|
1201
|
-
|
1202
|
-
rb_define_method(klass, "
|
1203
|
-
rb_define_method(klass, "
|
1204
|
-
rb_define_method(klass, "
|
1205
|
-
rb_define_method(klass, "
|
1206
|
-
rb_define_method(klass, "
|
1207
|
-
|
1208
|
-
rb_gc_register_address(&
|
1209
|
-
|
1210
|
-
|
1211
|
-
/*
|
1212
|
-
* call-seq:
|
1213
|
-
* MessageBuilderContext.new(desc, builder) => context
|
1214
|
-
*
|
1215
|
-
* Create a new message builder context around the given message descriptor and
|
1216
|
-
* builder context. This class is intended to serve as a DSL context to be used
|
1217
|
-
* with #instance_eval.
|
1218
|
-
*/
|
1219
|
-
VALUE MessageBuilderContext_initialize(VALUE _self,
|
1220
|
-
VALUE msgdef,
|
1221
|
-
VALUE builder) {
|
1222
|
-
DEFINE_SELF(MessageBuilderContext, self, _self);
|
1223
|
-
self->descriptor = msgdef;
|
1224
|
-
self->builder = builder;
|
1225
|
-
return Qnil;
|
1152
|
+
module, "EnumDescriptor", rb_cObject);
|
1153
|
+
rb_define_alloc_func(klass, EnumDescriptor_alloc);
|
1154
|
+
rb_define_method(klass, "initialize", EnumDescriptor_initialize, 3);
|
1155
|
+
rb_define_method(klass, "name", EnumDescriptor_name, 0);
|
1156
|
+
rb_define_method(klass, "lookup_name", EnumDescriptor_lookup_name, 1);
|
1157
|
+
rb_define_method(klass, "lookup_value", EnumDescriptor_lookup_value, 1);
|
1158
|
+
rb_define_method(klass, "each", EnumDescriptor_each, 0);
|
1159
|
+
rb_define_method(klass, "enummodule", EnumDescriptor_enummodule, 0);
|
1160
|
+
rb_define_method(klass, "file_descriptor", EnumDescriptor_file_descriptor, 0);
|
1161
|
+
rb_include_module(klass, rb_mEnumerable);
|
1162
|
+
rb_gc_register_address(&cEnumDescriptor);
|
1163
|
+
cEnumDescriptor = klass;
|
1226
1164
|
}
|
1227
1165
|
|
1228
|
-
static VALUE
|
1229
|
-
|
1230
|
-
|
1231
|
-
|
1232
|
-
VALUE fielddef = rb_class_new_instance(0, NULL, cFieldDescriptor);
|
1233
|
-
VALUE name_str = rb_str_new2(rb_id2name(SYM2ID(name)));
|
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;
|
1234
1170
|
|
1235
|
-
|
1236
|
-
rb_funcall(fielddef, rb_intern("name="), 1, name_str);
|
1237
|
-
rb_funcall(fielddef, rb_intern("type="), 1, type);
|
1238
|
-
rb_funcall(fielddef, rb_intern("number="), 1, number);
|
1171
|
+
def = rb_hash_aref(descriptor_pool->def_to_descriptor, key);
|
1239
1172
|
|
1240
|
-
if (
|
1241
|
-
|
1242
|
-
rb_raise(rb_eArgError, "Expected string for type class");
|
1243
|
-
}
|
1244
|
-
// Make it an absolute type name by prepending a dot.
|
1245
|
-
type_class = rb_str_append(rb_str_new2("."), type_class);
|
1246
|
-
rb_funcall(fielddef, rb_intern("submsg_name="), 1, type_class);
|
1173
|
+
if (ptr == NULL) {
|
1174
|
+
return Qnil;
|
1247
1175
|
}
|
1248
1176
|
|
1249
|
-
|
1250
|
-
|
1251
|
-
}
|
1252
|
-
|
1253
|
-
|
1254
|
-
* call-seq:
|
1255
|
-
* MessageBuilderContext.optional(name, type, number, type_class = nil)
|
1256
|
-
*
|
1257
|
-
* Defines a new optional field on this message type with the given type, tag
|
1258
|
-
* number, and type class (for message and enum fields). The type must be a Ruby
|
1259
|
-
* symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
|
1260
|
-
* string, if present (as accepted by FieldDescriptor#submsg_name=).
|
1261
|
-
*/
|
1262
|
-
VALUE MessageBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
|
1263
|
-
DEFINE_SELF(MessageBuilderContext, self, _self);
|
1264
|
-
VALUE name, type, number, type_class;
|
1265
|
-
|
1266
|
-
if (argc < 3) {
|
1267
|
-
rb_raise(rb_eArgError, "Expected at least 3 arguments.");
|
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);
|
1268
1182
|
}
|
1269
|
-
name = argv[0];
|
1270
|
-
type = argv[1];
|
1271
|
-
number = argv[2];
|
1272
|
-
type_class = (argc > 3) ? argv[3] : Qnil;
|
1273
1183
|
|
1274
|
-
return
|
1275
|
-
name, type, number, type_class);
|
1184
|
+
return def;
|
1276
1185
|
}
|
1277
1186
|
|
1278
|
-
|
1279
|
-
|
1280
|
-
* MessageBuilderContext.required(name, type, number, type_class = nil)
|
1281
|
-
*
|
1282
|
-
* Defines a new required field on this message type with the given type, tag
|
1283
|
-
* number, and type class (for message and enum fields). The type must be a Ruby
|
1284
|
-
* symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
|
1285
|
-
* string, if present (as accepted by FieldDescriptor#submsg_name=).
|
1286
|
-
*
|
1287
|
-
* Proto3 does not have required fields, but this method exists for
|
1288
|
-
* completeness. Any attempt to add a message type with required fields to a
|
1289
|
-
* pool will currently result in an error.
|
1290
|
-
*/
|
1291
|
-
VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self) {
|
1292
|
-
DEFINE_SELF(MessageBuilderContext, self, _self);
|
1293
|
-
VALUE name, type, number, type_class;
|
1294
|
-
|
1295
|
-
if (argc < 3) {
|
1296
|
-
rb_raise(rb_eArgError, "Expected at least 3 arguments.");
|
1297
|
-
}
|
1298
|
-
name = argv[0];
|
1299
|
-
type = argv[1];
|
1300
|
-
number = argv[2];
|
1301
|
-
type_class = (argc > 3) ? argv[3] : Qnil;
|
1302
|
-
|
1303
|
-
return msgdef_add_field(self->descriptor, "required",
|
1304
|
-
name, type, number, type_class);
|
1187
|
+
static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_msgdef* def) {
|
1188
|
+
return get_def_obj(descriptor_pool, def, cDescriptor);
|
1305
1189
|
}
|
1306
1190
|
|
1307
|
-
|
1308
|
-
|
1309
|
-
* MessageBuilderContext.repeated(name, type, number, type_class = nil)
|
1310
|
-
*
|
1311
|
-
* Defines a new repeated field on this message type with the given type, tag
|
1312
|
-
* number, and type class (for message and enum fields). The type must be a Ruby
|
1313
|
-
* symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
|
1314
|
-
* string, if present (as accepted by FieldDescriptor#submsg_name=).
|
1315
|
-
*/
|
1316
|
-
VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self) {
|
1317
|
-
DEFINE_SELF(MessageBuilderContext, self, _self);
|
1318
|
-
VALUE name, type, number, type_class;
|
1319
|
-
|
1320
|
-
if (argc < 3) {
|
1321
|
-
rb_raise(rb_eArgError, "Expected at least 3 arguments.");
|
1322
|
-
}
|
1323
|
-
name = argv[0];
|
1324
|
-
type = argv[1];
|
1325
|
-
number = argv[2];
|
1326
|
-
type_class = (argc > 3) ? argv[3] : Qnil;
|
1327
|
-
|
1328
|
-
return msgdef_add_field(self->descriptor, "repeated",
|
1329
|
-
name, type, number, type_class);
|
1191
|
+
static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_enumdef* def) {
|
1192
|
+
return get_def_obj(descriptor_pool, def, cEnumDescriptor);
|
1330
1193
|
}
|
1331
1194
|
|
1332
|
-
|
1333
|
-
|
1334
|
-
* MessageBuilderContext.map(name, key_type, value_type, number,
|
1335
|
-
* value_type_class = nil)
|
1336
|
-
*
|
1337
|
-
* Defines a new map field on this message type with the given key and value
|
1338
|
-
* types, tag number, and type class (for message and enum value types). The key
|
1339
|
-
* type must be :int32/:uint32/:int64/:uint64, :bool, or :string. The value type
|
1340
|
-
* type must be a Ruby symbol (as accepted by FieldDescriptor#type=) and the
|
1341
|
-
* type_class must be a string, if present (as accepted by
|
1342
|
-
* FieldDescriptor#submsg_name=).
|
1343
|
-
*/
|
1344
|
-
VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self) {
|
1345
|
-
DEFINE_SELF(MessageBuilderContext, self, _self);
|
1346
|
-
VALUE name, key_type, value_type, number, type_class;
|
1347
|
-
VALUE mapentry_desc, mapentry_desc_name;
|
1348
|
-
|
1349
|
-
if (argc < 4) {
|
1350
|
-
rb_raise(rb_eArgError, "Expected at least 4 arguments.");
|
1351
|
-
}
|
1352
|
-
name = argv[0];
|
1353
|
-
key_type = argv[1];
|
1354
|
-
value_type = argv[2];
|
1355
|
-
number = argv[3];
|
1356
|
-
type_class = (argc > 4) ? argv[4] : Qnil;
|
1357
|
-
|
1358
|
-
// Validate the key type. We can't accept enums, messages, or floats/doubles
|
1359
|
-
// as map keys. (We exclude these explicitly, and the field-descriptor setter
|
1360
|
-
// below then ensures that the type is one of the remaining valid options.)
|
1361
|
-
if (SYM2ID(key_type) == rb_intern("float") ||
|
1362
|
-
SYM2ID(key_type) == rb_intern("double") ||
|
1363
|
-
SYM2ID(key_type) == rb_intern("enum") ||
|
1364
|
-
SYM2ID(key_type) == rb_intern("message")) {
|
1365
|
-
rb_raise(rb_eArgError,
|
1366
|
-
"Cannot add a map field with a float, double, enum, or message "
|
1367
|
-
"type.");
|
1368
|
-
}
|
1369
|
-
|
1370
|
-
// Create a new message descriptor for the map entry message, and create a
|
1371
|
-
// repeated submessage field here with that type.
|
1372
|
-
mapentry_desc = rb_class_new_instance(0, NULL, cDescriptor);
|
1373
|
-
mapentry_desc_name = rb_funcall(self->descriptor, rb_intern("name"), 0);
|
1374
|
-
mapentry_desc_name = rb_str_cat2(mapentry_desc_name, "_MapEntry_");
|
1375
|
-
mapentry_desc_name = rb_str_cat2(mapentry_desc_name,
|
1376
|
-
rb_id2name(SYM2ID(name)));
|
1377
|
-
Descriptor_name_set(mapentry_desc, mapentry_desc_name);
|
1378
|
-
|
1379
|
-
{
|
1380
|
-
// The 'mapentry' attribute has no Ruby setter because we do not want the
|
1381
|
-
// user attempting to DIY the setup below; we want to ensure that the fields
|
1382
|
-
// are correct. So we reach into the msgdef here to set the bit manually.
|
1383
|
-
Descriptor* mapentry_desc_self = ruby_to_Descriptor(mapentry_desc);
|
1384
|
-
upb_msgdef_setmapentry((upb_msgdef*)mapentry_desc_self->msgdef, true);
|
1385
|
-
}
|
1386
|
-
|
1387
|
-
{
|
1388
|
-
// optional <type> key = 1;
|
1389
|
-
VALUE key_field = rb_class_new_instance(0, NULL, cFieldDescriptor);
|
1390
|
-
FieldDescriptor_name_set(key_field, rb_str_new2("key"));
|
1391
|
-
FieldDescriptor_label_set(key_field, ID2SYM(rb_intern("optional")));
|
1392
|
-
FieldDescriptor_number_set(key_field, INT2NUM(1));
|
1393
|
-
FieldDescriptor_type_set(key_field, key_type);
|
1394
|
-
Descriptor_add_field(mapentry_desc, key_field);
|
1395
|
-
}
|
1396
|
-
|
1397
|
-
{
|
1398
|
-
// optional <type> value = 2;
|
1399
|
-
VALUE value_field = rb_class_new_instance(0, NULL, cFieldDescriptor);
|
1400
|
-
FieldDescriptor_name_set(value_field, rb_str_new2("value"));
|
1401
|
-
FieldDescriptor_label_set(value_field, ID2SYM(rb_intern("optional")));
|
1402
|
-
FieldDescriptor_number_set(value_field, INT2NUM(2));
|
1403
|
-
FieldDescriptor_type_set(value_field, value_type);
|
1404
|
-
if (type_class != Qnil) {
|
1405
|
-
VALUE submsg_name = rb_str_new2("."); // prepend '.' to make absolute.
|
1406
|
-
submsg_name = rb_str_append(submsg_name, type_class);
|
1407
|
-
FieldDescriptor_submsg_name_set(value_field, submsg_name);
|
1408
|
-
}
|
1409
|
-
Descriptor_add_field(mapentry_desc, value_field);
|
1410
|
-
}
|
1411
|
-
|
1412
|
-
{
|
1413
|
-
// Add the map-entry message type to the current builder, and use the type
|
1414
|
-
// to create the map field itself.
|
1415
|
-
Builder* builder_self = ruby_to_Builder(self->builder);
|
1416
|
-
rb_ary_push(builder_self->pending_list, mapentry_desc);
|
1417
|
-
}
|
1418
|
-
|
1419
|
-
{
|
1420
|
-
VALUE map_field = rb_class_new_instance(0, NULL, cFieldDescriptor);
|
1421
|
-
VALUE name_str = rb_str_new2(rb_id2name(SYM2ID(name)));
|
1422
|
-
VALUE submsg_name;
|
1423
|
-
|
1424
|
-
FieldDescriptor_name_set(map_field, name_str);
|
1425
|
-
FieldDescriptor_number_set(map_field, number);
|
1426
|
-
FieldDescriptor_label_set(map_field, ID2SYM(rb_intern("repeated")));
|
1427
|
-
FieldDescriptor_type_set(map_field, ID2SYM(rb_intern("message")));
|
1428
|
-
submsg_name = rb_str_new2("."); // prepend '.' to make name absolute.
|
1429
|
-
submsg_name = rb_str_append(submsg_name, mapentry_desc_name);
|
1430
|
-
FieldDescriptor_submsg_name_set(map_field, submsg_name);
|
1431
|
-
Descriptor_add_field(self->descriptor, map_field);
|
1432
|
-
}
|
1433
|
-
|
1434
|
-
return Qnil;
|
1435
|
-
}
|
1436
|
-
|
1437
|
-
/*
|
1438
|
-
* call-seq:
|
1439
|
-
* MessageBuilderContext.oneof(name, &block) => nil
|
1440
|
-
*
|
1441
|
-
* Creates a new OneofDescriptor with the given name, creates a
|
1442
|
-
* OneofBuilderContext attached to that OneofDescriptor, evaluates the given
|
1443
|
-
* block in the context of that OneofBuilderContext with #instance_eval, and
|
1444
|
-
* then adds the oneof to the message.
|
1445
|
-
*
|
1446
|
-
* This is the recommended, idiomatic way to build oneof definitions.
|
1447
|
-
*/
|
1448
|
-
VALUE MessageBuilderContext_oneof(VALUE _self, VALUE name) {
|
1449
|
-
DEFINE_SELF(MessageBuilderContext, self, _self);
|
1450
|
-
VALUE oneofdef = rb_class_new_instance(0, NULL, cOneofDescriptor);
|
1451
|
-
VALUE args[2] = { oneofdef, self->builder };
|
1452
|
-
VALUE ctx = rb_class_new_instance(2, args, cOneofBuilderContext);
|
1453
|
-
VALUE block = rb_block_proc();
|
1454
|
-
VALUE name_str = rb_str_new2(rb_id2name(SYM2ID(name)));
|
1455
|
-
rb_funcall(oneofdef, rb_intern("name="), 1, name_str);
|
1456
|
-
rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
|
1457
|
-
Descriptor_add_oneof(self->descriptor, oneofdef);
|
1458
|
-
|
1459
|
-
return Qnil;
|
1195
|
+
static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_fielddef* def) {
|
1196
|
+
return get_def_obj(descriptor_pool, def, cFieldDescriptor);
|
1460
1197
|
}
|
1461
1198
|
|
1462
|
-
|
1463
|
-
|
1464
|
-
// -----------------------------------------------------------------------------
|
1465
|
-
|
1466
|
-
DEFINE_CLASS(OneofBuilderContext,
|
1467
|
-
"Google::Protobuf::Internal::OneofBuilderContext");
|
1468
|
-
|
1469
|
-
void OneofBuilderContext_mark(void* _self) {
|
1470
|
-
OneofBuilderContext* self = _self;
|
1471
|
-
rb_gc_mark(self->descriptor);
|
1472
|
-
rb_gc_mark(self->builder);
|
1199
|
+
static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_filedef* def) {
|
1200
|
+
return get_def_obj(descriptor_pool, def, cFileDescriptor);
|
1473
1201
|
}
|
1474
1202
|
|
1475
|
-
|
1476
|
-
|
1477
|
-
xfree(self);
|
1478
|
-
}
|
1479
|
-
|
1480
|
-
VALUE OneofBuilderContext_alloc(VALUE klass) {
|
1481
|
-
OneofBuilderContext* self = ALLOC(OneofBuilderContext);
|
1482
|
-
VALUE ret = TypedData_Wrap_Struct(
|
1483
|
-
klass, &_OneofBuilderContext_type, self);
|
1484
|
-
self->descriptor = Qnil;
|
1485
|
-
self->builder = Qnil;
|
1486
|
-
return ret;
|
1487
|
-
}
|
1488
|
-
|
1489
|
-
void OneofBuilderContext_register(VALUE module) {
|
1490
|
-
VALUE klass = rb_define_class_under(
|
1491
|
-
module, "OneofBuilderContext", rb_cObject);
|
1492
|
-
rb_define_alloc_func(klass, OneofBuilderContext_alloc);
|
1493
|
-
rb_define_method(klass, "initialize",
|
1494
|
-
OneofBuilderContext_initialize, 2);
|
1495
|
-
rb_define_method(klass, "optional", OneofBuilderContext_optional, -1);
|
1496
|
-
cOneofBuilderContext = klass;
|
1497
|
-
rb_gc_register_address(&cOneofBuilderContext);
|
1498
|
-
}
|
1499
|
-
|
1500
|
-
/*
|
1501
|
-
* call-seq:
|
1502
|
-
* OneofBuilderContext.new(desc, builder) => context
|
1503
|
-
*
|
1504
|
-
* Create a new oneof builder context around the given oneof descriptor and
|
1505
|
-
* builder context. This class is intended to serve as a DSL context to be used
|
1506
|
-
* with #instance_eval.
|
1507
|
-
*/
|
1508
|
-
VALUE OneofBuilderContext_initialize(VALUE _self,
|
1509
|
-
VALUE oneofdef,
|
1510
|
-
VALUE builder) {
|
1511
|
-
DEFINE_SELF(OneofBuilderContext, self, _self);
|
1512
|
-
self->descriptor = oneofdef;
|
1513
|
-
self->builder = builder;
|
1514
|
-
return Qnil;
|
1515
|
-
}
|
1516
|
-
|
1517
|
-
/*
|
1518
|
-
* call-seq:
|
1519
|
-
* OneofBuilderContext.optional(name, type, number, type_class = nil)
|
1520
|
-
*
|
1521
|
-
* Defines a new optional field in this oneof with the given type, tag number,
|
1522
|
-
* and type class (for message and enum fields). The type must be a Ruby symbol
|
1523
|
-
* (as accepted by FieldDescriptor#type=) and the type_class must be a string,
|
1524
|
-
* if present (as accepted by FieldDescriptor#submsg_name=).
|
1525
|
-
*/
|
1526
|
-
VALUE OneofBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
|
1527
|
-
DEFINE_SELF(OneofBuilderContext, self, _self);
|
1528
|
-
VALUE name, type, number, type_class;
|
1529
|
-
|
1530
|
-
if (argc < 3) {
|
1531
|
-
rb_raise(rb_eArgError, "Expected at least 3 arguments.");
|
1532
|
-
}
|
1533
|
-
name = argv[0];
|
1534
|
-
type = argv[1];
|
1535
|
-
number = argv[2];
|
1536
|
-
type_class = (argc > 3) ? argv[3] : Qnil;
|
1537
|
-
|
1538
|
-
return msgdef_add_field(self->descriptor, "optional",
|
1539
|
-
name, type, number, type_class);
|
1540
|
-
}
|
1541
|
-
|
1542
|
-
// -----------------------------------------------------------------------------
|
1543
|
-
// EnumBuilderContext.
|
1544
|
-
// -----------------------------------------------------------------------------
|
1545
|
-
|
1546
|
-
DEFINE_CLASS(EnumBuilderContext,
|
1547
|
-
"Google::Protobuf::Internal::EnumBuilderContext");
|
1548
|
-
|
1549
|
-
void EnumBuilderContext_mark(void* _self) {
|
1550
|
-
EnumBuilderContext* self = _self;
|
1551
|
-
rb_gc_mark(self->enumdesc);
|
1552
|
-
}
|
1553
|
-
|
1554
|
-
void EnumBuilderContext_free(void* _self) {
|
1555
|
-
EnumBuilderContext* self = _self;
|
1556
|
-
xfree(self);
|
1557
|
-
}
|
1558
|
-
|
1559
|
-
VALUE EnumBuilderContext_alloc(VALUE klass) {
|
1560
|
-
EnumBuilderContext* self = ALLOC(EnumBuilderContext);
|
1561
|
-
VALUE ret = TypedData_Wrap_Struct(
|
1562
|
-
klass, &_EnumBuilderContext_type, self);
|
1563
|
-
self->enumdesc = Qnil;
|
1564
|
-
return ret;
|
1565
|
-
}
|
1566
|
-
|
1567
|
-
void EnumBuilderContext_register(VALUE module) {
|
1568
|
-
VALUE klass = rb_define_class_under(
|
1569
|
-
module, "EnumBuilderContext", rb_cObject);
|
1570
|
-
rb_define_alloc_func(klass, EnumBuilderContext_alloc);
|
1571
|
-
rb_define_method(klass, "initialize",
|
1572
|
-
EnumBuilderContext_initialize, 1);
|
1573
|
-
rb_define_method(klass, "value", EnumBuilderContext_value, 2);
|
1574
|
-
cEnumBuilderContext = klass;
|
1575
|
-
rb_gc_register_address(&cEnumBuilderContext);
|
1576
|
-
}
|
1577
|
-
|
1578
|
-
/*
|
1579
|
-
* call-seq:
|
1580
|
-
* EnumBuilderContext.new(enumdesc) => context
|
1581
|
-
*
|
1582
|
-
* Create a new builder context around the given enum descriptor. This class is
|
1583
|
-
* intended to serve as a DSL context to be used with #instance_eval.
|
1584
|
-
*/
|
1585
|
-
VALUE EnumBuilderContext_initialize(VALUE _self, VALUE enumdef) {
|
1586
|
-
DEFINE_SELF(EnumBuilderContext, self, _self);
|
1587
|
-
self->enumdesc = enumdef;
|
1588
|
-
return Qnil;
|
1589
|
-
}
|
1590
|
-
|
1591
|
-
static VALUE enumdef_add_value(VALUE enumdef,
|
1592
|
-
VALUE name, VALUE number) {
|
1593
|
-
rb_funcall(enumdef, rb_intern("add_value"), 2, name, number);
|
1594
|
-
return Qnil;
|
1595
|
-
}
|
1596
|
-
|
1597
|
-
/*
|
1598
|
-
* call-seq:
|
1599
|
-
* EnumBuilder.add_value(name, number)
|
1600
|
-
*
|
1601
|
-
* Adds the given name => number mapping to the enum type. Name must be a Ruby
|
1602
|
-
* symbol.
|
1603
|
-
*/
|
1604
|
-
VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number) {
|
1605
|
-
DEFINE_SELF(EnumBuilderContext, self, _self);
|
1606
|
-
return enumdef_add_value(self->enumdesc, name, number);
|
1203
|
+
static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_oneofdef* def) {
|
1204
|
+
return get_def_obj(descriptor_pool, def, cOneofDescriptor);
|
1607
1205
|
}
|
1608
1206
|
|
1609
1207
|
// -----------------------------------------------------------------------------
|
1610
|
-
//
|
1208
|
+
// Shared functions
|
1611
1209
|
// -----------------------------------------------------------------------------
|
1612
1210
|
|
1613
|
-
|
1211
|
+
// Functions exposed to other modules in defs.h.
|
1614
1212
|
|
1615
|
-
|
1616
|
-
|
1617
|
-
|
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;
|
1618
1220
|
}
|
1619
1221
|
|
1620
|
-
|
1621
|
-
|
1622
|
-
|
1623
|
-
xfree(self);
|
1222
|
+
const upb_msgdef *Descriptor_GetMsgDef(VALUE desc_rb) {
|
1223
|
+
const Descriptor* desc = ruby_to_Descriptor(desc_rb);
|
1224
|
+
return desc->msgdef;
|
1624
1225
|
}
|
1625
1226
|
|
1626
|
-
|
1627
|
-
|
1628
|
-
|
1629
|
-
|
1630
|
-
* Creates a new Builder. A Builder can accumulate a set of new message and enum
|
1631
|
-
* descriptors and atomically register them into a pool in a way that allows for
|
1632
|
-
* (co)recursive type references.
|
1633
|
-
*/
|
1634
|
-
VALUE Builder_alloc(VALUE klass) {
|
1635
|
-
Builder* self = ALLOC(Builder);
|
1636
|
-
VALUE ret = TypedData_Wrap_Struct(
|
1637
|
-
klass, &_Builder_type, self);
|
1638
|
-
self->pending_list = rb_ary_new();
|
1639
|
-
self->defs = NULL;
|
1640
|
-
return ret;
|
1641
|
-
}
|
1642
|
-
|
1643
|
-
void Builder_register(VALUE module) {
|
1644
|
-
VALUE klass = rb_define_class_under(module, "Builder", rb_cObject);
|
1645
|
-
rb_define_alloc_func(klass, Builder_alloc);
|
1646
|
-
rb_define_method(klass, "add_message", Builder_add_message, 1);
|
1647
|
-
rb_define_method(klass, "add_enum", Builder_add_enum, 1);
|
1648
|
-
rb_define_method(klass, "finalize_to_pool", Builder_finalize_to_pool, 1);
|
1649
|
-
cBuilder = klass;
|
1650
|
-
rb_gc_register_address(&cBuilder);
|
1651
|
-
}
|
1652
|
-
|
1653
|
-
/*
|
1654
|
-
* call-seq:
|
1655
|
-
* Builder.add_message(name, &block)
|
1656
|
-
*
|
1657
|
-
* Creates a new, empty descriptor with the given name, and invokes the block in
|
1658
|
-
* the context of a MessageBuilderContext on that descriptor. The block can then
|
1659
|
-
* call, e.g., MessageBuilderContext#optional and MessageBuilderContext#repeated
|
1660
|
-
* methods to define the message fields.
|
1661
|
-
*
|
1662
|
-
* This is the recommended, idiomatic way to build message definitions.
|
1663
|
-
*/
|
1664
|
-
VALUE Builder_add_message(VALUE _self, VALUE name) {
|
1665
|
-
DEFINE_SELF(Builder, self, _self);
|
1666
|
-
VALUE msgdef = rb_class_new_instance(0, NULL, cDescriptor);
|
1667
|
-
VALUE args[2] = { msgdef, _self };
|
1668
|
-
VALUE ctx = rb_class_new_instance(2, args, cMessageBuilderContext);
|
1669
|
-
VALUE block = rb_block_proc();
|
1670
|
-
rb_funcall(msgdef, rb_intern("name="), 1, name);
|
1671
|
-
rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
|
1672
|
-
rb_ary_push(self->pending_list, msgdef);
|
1673
|
-
return Qnil;
|
1674
|
-
}
|
1675
|
-
|
1676
|
-
/*
|
1677
|
-
* call-seq:
|
1678
|
-
* Builder.add_enum(name, &block)
|
1679
|
-
*
|
1680
|
-
* Creates a new, empty enum descriptor with the given name, and invokes the
|
1681
|
-
* block in the context of an EnumBuilderContext on that descriptor. The block
|
1682
|
-
* can then call EnumBuilderContext#add_value to define the enum values.
|
1683
|
-
*
|
1684
|
-
* This is the recommended, idiomatic way to build enum definitions.
|
1685
|
-
*/
|
1686
|
-
VALUE Builder_add_enum(VALUE _self, VALUE name) {
|
1687
|
-
DEFINE_SELF(Builder, self, _self);
|
1688
|
-
VALUE enumdef = rb_class_new_instance(0, NULL, cEnumDescriptor);
|
1689
|
-
VALUE ctx = rb_class_new_instance(1, &enumdef, cEnumBuilderContext);
|
1690
|
-
VALUE block = rb_block_proc();
|
1691
|
-
rb_funcall(enumdef, rb_intern("name="), 1, name);
|
1692
|
-
rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
|
1693
|
-
rb_ary_push(self->pending_list, enumdef);
|
1694
|
-
return Qnil;
|
1695
|
-
}
|
1696
|
-
|
1697
|
-
static void validate_msgdef(const upb_msgdef* msgdef) {
|
1698
|
-
// Verify that no required fields exist. proto3 does not support these.
|
1699
|
-
upb_msg_field_iter it;
|
1700
|
-
for (upb_msg_field_begin(&it, msgdef);
|
1701
|
-
!upb_msg_field_done(&it);
|
1702
|
-
upb_msg_field_next(&it)) {
|
1703
|
-
const upb_fielddef* field = upb_msg_iter_field(&it);
|
1704
|
-
if (upb_fielddef_label(field) == UPB_LABEL_REQUIRED) {
|
1705
|
-
rb_raise(rb_eTypeError, "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);
|
1706
1231
|
}
|
1232
|
+
return argv[skip_arg];
|
1233
|
+
} else {
|
1234
|
+
return Qnil;
|
1707
1235
|
}
|
1708
1236
|
}
|
1709
1237
|
|
1710
|
-
|
1711
|
-
|
1712
|
-
|
1713
|
-
const char* lookup = upb_enumdef_iton(enumdef, 0);
|
1714
|
-
if (lookup == NULL) {
|
1715
|
-
rb_raise(rb_eTypeError,
|
1716
|
-
"Enum definition does not contain a value for '0'.");
|
1717
|
-
}
|
1718
|
-
}
|
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])};
|
1719
1241
|
|
1720
|
-
|
1721
|
-
|
1722
|
-
|
1723
|
-
|
1724
|
-
|
1725
|
-
|
1726
|
-
|
1727
|
-
* way to build (co)recursive message definitions.
|
1728
|
-
*
|
1729
|
-
* This method is usually called automatically by DescriptorPool#build after it
|
1730
|
-
* invokes the given user block in the context of the builder. The user should
|
1731
|
-
* not normally need to call this manually because a Builder is not normally
|
1732
|
-
* created manually.
|
1733
|
-
*/
|
1734
|
-
VALUE Builder_finalize_to_pool(VALUE _self, VALUE pool_rb) {
|
1735
|
-
DEFINE_SELF(Builder, self, _self);
|
1242
|
+
if (ret.type == UPB_TYPE_MESSAGE || ret.type == UPB_TYPE_ENUM) {
|
1243
|
+
*init_arg = TypeInfo_InitArg(argc, argv, skip_arg + 2);
|
1244
|
+
|
1245
|
+
if (argc < 2 + skip_arg) {
|
1246
|
+
rb_raise(rb_eArgError, "Expected at least %d arguments for message/enum.",
|
1247
|
+
2 + skip_arg);
|
1248
|
+
}
|
1736
1249
|
|
1737
|
-
|
1250
|
+
VALUE klass = argv[1 + skip_arg];
|
1251
|
+
VALUE desc = MessageOrEnum_GetDescriptor(klass);
|
1252
|
+
*type_class = klass;
|
1738
1253
|
|
1739
|
-
|
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
|
+
}
|
1740
1259
|
|
1741
|
-
|
1742
|
-
|
1743
|
-
|
1744
|
-
|
1745
|
-
|
1746
|
-
|
1747
|
-
self->defs[i] = (upb_def*)ruby_to_EnumDescriptor(def_rb)->enumdef;
|
1748
|
-
validate_enumdef((const upb_enumdef*)self->defs[i]);
|
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;
|
1749
1266
|
}
|
1267
|
+
} else {
|
1268
|
+
*init_arg = TypeInfo_InitArg(argc, argv, skip_arg + 1);
|
1750
1269
|
}
|
1751
1270
|
|
1752
|
-
|
1753
|
-
|
1754
|
-
"Unable to add defs to DescriptorPool");
|
1271
|
+
return ret;
|
1272
|
+
}
|
1755
1273
|
|
1756
|
-
|
1757
|
-
|
1758
|
-
|
1759
|
-
|
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);
|
1760
1281
|
|
1761
|
-
|
1762
|
-
|
1282
|
+
rb_gc_register_address(&c_only_cookie);
|
1283
|
+
c_only_cookie = rb_class_new_instance(0, NULL, rb_cObject);
|
1763
1284
|
}
|