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