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