google-protobuf 3.0.0.alpha.5.0.3-universal-darwin
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of google-protobuf might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/ext/google/protobuf_c/defs.c +1753 -0
- data/ext/google/protobuf_c/encode_decode.c +1200 -0
- data/ext/google/protobuf_c/extconf.rb +10 -0
- data/ext/google/protobuf_c/map.c +808 -0
- data/ext/google/protobuf_c/message.c +580 -0
- data/ext/google/protobuf_c/protobuf.c +115 -0
- data/ext/google/protobuf_c/protobuf.h +536 -0
- data/ext/google/protobuf_c/repeated_field.c +651 -0
- data/ext/google/protobuf_c/storage.c +863 -0
- data/ext/google/protobuf_c/upb.c +11990 -0
- data/ext/google/protobuf_c/upb.h +8217 -0
- data/lib/google/2.0/protobuf_c.bundle +0 -0
- data/lib/google/2.1/protobuf_c.bundle +0 -0
- data/lib/google/2.2/protobuf_c.bundle +0 -0
- data/lib/google/2.3/protobuf_c.bundle +0 -0
- data/lib/google/protobuf.rb +76 -0
- data/lib/google/protobuf/message_exts.rb +53 -0
- data/lib/google/protobuf/repeated_field.rb +188 -0
- data/tests/basic.rb +1140 -0
- data/tests/generated_code_test.rb +17 -0
- data/tests/stress.rb +38 -0
- metadata +123 -0
@@ -0,0 +1,580 @@
|
|
1
|
+
// Protocol Buffers - Google's data interchange format
|
2
|
+
// Copyright 2014 Google Inc. All rights reserved.
|
3
|
+
// https://developers.google.com/protocol-buffers/
|
4
|
+
//
|
5
|
+
// Redistribution and use in source and binary forms, with or without
|
6
|
+
// modification, are permitted provided that the following conditions are
|
7
|
+
// met:
|
8
|
+
//
|
9
|
+
// * Redistributions of source code must retain the above copyright
|
10
|
+
// notice, this list of conditions and the following disclaimer.
|
11
|
+
// * Redistributions in binary form must reproduce the above
|
12
|
+
// copyright notice, this list of conditions and the following disclaimer
|
13
|
+
// in the documentation and/or other materials provided with the
|
14
|
+
// distribution.
|
15
|
+
// * Neither the name of Google Inc. nor the names of its
|
16
|
+
// contributors may be used to endorse or promote products derived from
|
17
|
+
// this software without specific prior written permission.
|
18
|
+
//
|
19
|
+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
20
|
+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
21
|
+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
22
|
+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
23
|
+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
24
|
+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
25
|
+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
26
|
+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
27
|
+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
28
|
+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
29
|
+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30
|
+
|
31
|
+
#include "protobuf.h"
|
32
|
+
|
33
|
+
// -----------------------------------------------------------------------------
|
34
|
+
// Class/module creation from msgdefs and enumdefs, respectively.
|
35
|
+
// -----------------------------------------------------------------------------
|
36
|
+
|
37
|
+
void* Message_data(void* msg) {
|
38
|
+
return ((uint8_t *)msg) + sizeof(MessageHeader);
|
39
|
+
}
|
40
|
+
|
41
|
+
void Message_mark(void* _self) {
|
42
|
+
MessageHeader* self = (MessageHeader *)_self;
|
43
|
+
layout_mark(self->descriptor->layout, Message_data(self));
|
44
|
+
}
|
45
|
+
|
46
|
+
void Message_free(void* self) {
|
47
|
+
xfree(self);
|
48
|
+
}
|
49
|
+
|
50
|
+
rb_data_type_t Message_type = {
|
51
|
+
"Message",
|
52
|
+
{ Message_mark, Message_free, NULL },
|
53
|
+
};
|
54
|
+
|
55
|
+
VALUE Message_alloc(VALUE klass) {
|
56
|
+
VALUE descriptor = rb_ivar_get(klass, descriptor_instancevar_interned);
|
57
|
+
Descriptor* desc = ruby_to_Descriptor(descriptor);
|
58
|
+
MessageHeader* msg = (MessageHeader*)ALLOC_N(
|
59
|
+
uint8_t, sizeof(MessageHeader) + desc->layout->size);
|
60
|
+
VALUE ret;
|
61
|
+
|
62
|
+
memset(Message_data(msg), 0, desc->layout->size);
|
63
|
+
|
64
|
+
// We wrap first so that everything in the message object is GC-rooted in case
|
65
|
+
// a collection happens during object creation in layout_init().
|
66
|
+
ret = TypedData_Wrap_Struct(klass, &Message_type, msg);
|
67
|
+
msg->descriptor = desc;
|
68
|
+
rb_ivar_set(ret, descriptor_instancevar_interned, descriptor);
|
69
|
+
|
70
|
+
layout_init(desc->layout, Message_data(msg));
|
71
|
+
|
72
|
+
return ret;
|
73
|
+
}
|
74
|
+
|
75
|
+
static VALUE which_oneof_field(MessageHeader* self, const upb_oneofdef* o) {
|
76
|
+
upb_oneof_iter it;
|
77
|
+
size_t case_ofs;
|
78
|
+
uint32_t oneof_case;
|
79
|
+
const upb_fielddef* first_field;
|
80
|
+
const upb_fielddef* f;
|
81
|
+
|
82
|
+
// If no fields in the oneof, always nil.
|
83
|
+
if (upb_oneofdef_numfields(o) == 0) {
|
84
|
+
return Qnil;
|
85
|
+
}
|
86
|
+
// Grab the first field in the oneof so we can get its layout info to find the
|
87
|
+
// oneof_case field.
|
88
|
+
upb_oneof_begin(&it, o);
|
89
|
+
assert(!upb_oneof_done(&it));
|
90
|
+
first_field = upb_oneof_iter_field(&it);
|
91
|
+
assert(upb_fielddef_containingoneof(first_field) != NULL);
|
92
|
+
|
93
|
+
case_ofs =
|
94
|
+
self->descriptor->layout->
|
95
|
+
fields[upb_fielddef_index(first_field)].case_offset;
|
96
|
+
oneof_case = *((uint32_t*)((char*)Message_data(self) + case_ofs));
|
97
|
+
|
98
|
+
if (oneof_case == ONEOF_CASE_NONE) {
|
99
|
+
return Qnil;
|
100
|
+
}
|
101
|
+
|
102
|
+
// oneof_case is a field index, so find that field.
|
103
|
+
f = upb_oneofdef_itof(o, oneof_case);
|
104
|
+
assert(f != NULL);
|
105
|
+
|
106
|
+
return ID2SYM(rb_intern(upb_fielddef_name(f)));
|
107
|
+
}
|
108
|
+
|
109
|
+
/*
|
110
|
+
* call-seq:
|
111
|
+
* Message.method_missing(*args)
|
112
|
+
*
|
113
|
+
* Provides accessors and setters for message fields according to their field
|
114
|
+
* names. For any field whose name does not conflict with a built-in method, an
|
115
|
+
* accessor is provided with the same name as the field, and a setter is
|
116
|
+
* provided with the name of the field plus the '=' suffix. Thus, given a
|
117
|
+
* message instance 'msg' with field 'foo', the following code is valid:
|
118
|
+
*
|
119
|
+
* msg.foo = 42
|
120
|
+
* puts msg.foo
|
121
|
+
*
|
122
|
+
* This method also provides read-only accessors for oneofs. If a oneof exists
|
123
|
+
* with name 'my_oneof', then msg.my_oneof will return a Ruby symbol equal to
|
124
|
+
* the name of the field in that oneof that is currently set, or nil if none.
|
125
|
+
*/
|
126
|
+
VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self) {
|
127
|
+
MessageHeader* self;
|
128
|
+
VALUE method_name, method_str;
|
129
|
+
char* name;
|
130
|
+
size_t name_len;
|
131
|
+
bool setter;
|
132
|
+
const upb_oneofdef* o;
|
133
|
+
const upb_fielddef* f;
|
134
|
+
|
135
|
+
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
136
|
+
if (argc < 1) {
|
137
|
+
rb_raise(rb_eArgError, "Expected method name as first argument.");
|
138
|
+
}
|
139
|
+
method_name = argv[0];
|
140
|
+
if (!SYMBOL_P(method_name)) {
|
141
|
+
rb_raise(rb_eArgError, "Expected symbol as method name.");
|
142
|
+
}
|
143
|
+
method_str = rb_id2str(SYM2ID(method_name));
|
144
|
+
name = RSTRING_PTR(method_str);
|
145
|
+
name_len = RSTRING_LEN(method_str);
|
146
|
+
setter = false;
|
147
|
+
|
148
|
+
// Setters have names that end in '='.
|
149
|
+
if (name[name_len - 1] == '=') {
|
150
|
+
setter = true;
|
151
|
+
name_len--;
|
152
|
+
}
|
153
|
+
|
154
|
+
// Check for a oneof name first.
|
155
|
+
o = upb_msgdef_ntoo(self->descriptor->msgdef,
|
156
|
+
name, name_len);
|
157
|
+
if (o != NULL) {
|
158
|
+
if (setter) {
|
159
|
+
rb_raise(rb_eRuntimeError, "Oneof accessors are read-only.");
|
160
|
+
}
|
161
|
+
return which_oneof_field(self, o);
|
162
|
+
}
|
163
|
+
|
164
|
+
// Otherwise, check for a field with that name.
|
165
|
+
f = upb_msgdef_ntof(self->descriptor->msgdef,
|
166
|
+
name, name_len);
|
167
|
+
|
168
|
+
if (f == NULL) {
|
169
|
+
rb_raise(rb_eArgError, "Unknown field");
|
170
|
+
}
|
171
|
+
|
172
|
+
if (setter) {
|
173
|
+
if (argc < 2) {
|
174
|
+
rb_raise(rb_eArgError, "No value provided to setter.");
|
175
|
+
}
|
176
|
+
layout_set(self->descriptor->layout, Message_data(self), f, argv[1]);
|
177
|
+
return Qnil;
|
178
|
+
} else {
|
179
|
+
return layout_get(self->descriptor->layout, Message_data(self), f);
|
180
|
+
}
|
181
|
+
}
|
182
|
+
|
183
|
+
int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
|
184
|
+
MessageHeader* self;
|
185
|
+
VALUE method_str;
|
186
|
+
char* name;
|
187
|
+
const upb_fielddef* f;
|
188
|
+
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
189
|
+
|
190
|
+
if (!SYMBOL_P(key)) {
|
191
|
+
rb_raise(rb_eArgError,
|
192
|
+
"Expected symbols as hash keys in initialization map.");
|
193
|
+
}
|
194
|
+
|
195
|
+
method_str = rb_id2str(SYM2ID(key));
|
196
|
+
name = RSTRING_PTR(method_str);
|
197
|
+
f = upb_msgdef_ntofz(self->descriptor->msgdef, name);
|
198
|
+
if (f == NULL) {
|
199
|
+
rb_raise(rb_eArgError,
|
200
|
+
"Unknown field name in initialization map entry.");
|
201
|
+
}
|
202
|
+
|
203
|
+
if (is_map_field(f)) {
|
204
|
+
VALUE map;
|
205
|
+
|
206
|
+
if (TYPE(val) != T_HASH) {
|
207
|
+
rb_raise(rb_eArgError,
|
208
|
+
"Expected Hash object as initializer value for map field.");
|
209
|
+
}
|
210
|
+
map = layout_get(self->descriptor->layout, Message_data(self), f);
|
211
|
+
Map_merge_into_self(map, val);
|
212
|
+
} else if (upb_fielddef_label(f) == UPB_LABEL_REPEATED) {
|
213
|
+
VALUE ary;
|
214
|
+
|
215
|
+
if (TYPE(val) != T_ARRAY) {
|
216
|
+
rb_raise(rb_eArgError,
|
217
|
+
"Expected array as initializer value for repeated field.");
|
218
|
+
}
|
219
|
+
ary = layout_get(self->descriptor->layout, Message_data(self), f);
|
220
|
+
for (int i = 0; i < RARRAY_LEN(val); i++) {
|
221
|
+
RepeatedField_push(ary, rb_ary_entry(val, i));
|
222
|
+
}
|
223
|
+
} else {
|
224
|
+
layout_set(self->descriptor->layout, Message_data(self), f, val);
|
225
|
+
}
|
226
|
+
return 0;
|
227
|
+
}
|
228
|
+
|
229
|
+
/*
|
230
|
+
* call-seq:
|
231
|
+
* Message.new(kwargs) => new_message
|
232
|
+
*
|
233
|
+
* Creates a new instance of the given message class. Keyword arguments may be
|
234
|
+
* provided with keywords corresponding to field names.
|
235
|
+
*
|
236
|
+
* Note that no literal Message class exists. Only concrete classes per message
|
237
|
+
* type exist, as provided by the #msgclass method on Descriptors after they
|
238
|
+
* have been added to a pool. The method definitions described here on the
|
239
|
+
* Message class are provided on each concrete message class.
|
240
|
+
*/
|
241
|
+
VALUE Message_initialize(int argc, VALUE* argv, VALUE _self) {
|
242
|
+
VALUE hash_args;
|
243
|
+
|
244
|
+
if (argc == 0) {
|
245
|
+
return Qnil;
|
246
|
+
}
|
247
|
+
if (argc != 1) {
|
248
|
+
rb_raise(rb_eArgError, "Expected 0 or 1 arguments.");
|
249
|
+
}
|
250
|
+
hash_args = argv[0];
|
251
|
+
if (TYPE(hash_args) != T_HASH) {
|
252
|
+
rb_raise(rb_eArgError, "Expected hash arguments.");
|
253
|
+
}
|
254
|
+
|
255
|
+
rb_hash_foreach(hash_args, Message_initialize_kwarg, _self);
|
256
|
+
return Qnil;
|
257
|
+
}
|
258
|
+
|
259
|
+
/*
|
260
|
+
* call-seq:
|
261
|
+
* Message.dup => new_message
|
262
|
+
*
|
263
|
+
* Performs a shallow copy of this message and returns the new copy.
|
264
|
+
*/
|
265
|
+
VALUE Message_dup(VALUE _self) {
|
266
|
+
MessageHeader* self;
|
267
|
+
VALUE new_msg;
|
268
|
+
MessageHeader* new_msg_self;
|
269
|
+
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
270
|
+
|
271
|
+
new_msg = rb_class_new_instance(0, NULL, CLASS_OF(_self));
|
272
|
+
TypedData_Get_Struct(new_msg, MessageHeader, &Message_type, new_msg_self);
|
273
|
+
|
274
|
+
layout_dup(self->descriptor->layout,
|
275
|
+
Message_data(new_msg_self),
|
276
|
+
Message_data(self));
|
277
|
+
|
278
|
+
return new_msg;
|
279
|
+
}
|
280
|
+
|
281
|
+
// Internal only; used by Google::Protobuf.deep_copy.
|
282
|
+
VALUE Message_deep_copy(VALUE _self) {
|
283
|
+
MessageHeader* self;
|
284
|
+
MessageHeader* new_msg_self;
|
285
|
+
VALUE new_msg;
|
286
|
+
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
287
|
+
|
288
|
+
new_msg = rb_class_new_instance(0, NULL, CLASS_OF(_self));
|
289
|
+
TypedData_Get_Struct(new_msg, MessageHeader, &Message_type, new_msg_self);
|
290
|
+
|
291
|
+
layout_deep_copy(self->descriptor->layout,
|
292
|
+
Message_data(new_msg_self),
|
293
|
+
Message_data(self));
|
294
|
+
|
295
|
+
return new_msg;
|
296
|
+
}
|
297
|
+
|
298
|
+
/*
|
299
|
+
* call-seq:
|
300
|
+
* Message.==(other) => boolean
|
301
|
+
*
|
302
|
+
* Performs a deep comparison of this message with another. Messages are equal
|
303
|
+
* if they have the same type and if each field is equal according to the :==
|
304
|
+
* method's semantics (a more efficient comparison may actually be done if the
|
305
|
+
* field is of a primitive type).
|
306
|
+
*/
|
307
|
+
VALUE Message_eq(VALUE _self, VALUE _other) {
|
308
|
+
MessageHeader* self;
|
309
|
+
MessageHeader* other;
|
310
|
+
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
311
|
+
TypedData_Get_Struct(_other, MessageHeader, &Message_type, other);
|
312
|
+
|
313
|
+
if (self->descriptor != other->descriptor) {
|
314
|
+
return Qfalse;
|
315
|
+
}
|
316
|
+
|
317
|
+
return layout_eq(self->descriptor->layout,
|
318
|
+
Message_data(self),
|
319
|
+
Message_data(other));
|
320
|
+
}
|
321
|
+
|
322
|
+
/*
|
323
|
+
* call-seq:
|
324
|
+
* Message.hash => hash_value
|
325
|
+
*
|
326
|
+
* Returns a hash value that represents this message's field values.
|
327
|
+
*/
|
328
|
+
VALUE Message_hash(VALUE _self) {
|
329
|
+
MessageHeader* self;
|
330
|
+
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
331
|
+
|
332
|
+
return layout_hash(self->descriptor->layout, Message_data(self));
|
333
|
+
}
|
334
|
+
|
335
|
+
/*
|
336
|
+
* call-seq:
|
337
|
+
* Message.inspect => string
|
338
|
+
*
|
339
|
+
* Returns a human-readable string representing this message. It will be
|
340
|
+
* formatted as "<MessageType: field1: value1, field2: value2, ...>". Each
|
341
|
+
* field's value is represented according to its own #inspect method.
|
342
|
+
*/
|
343
|
+
VALUE Message_inspect(VALUE _self) {
|
344
|
+
MessageHeader* self;
|
345
|
+
VALUE str;
|
346
|
+
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
347
|
+
|
348
|
+
str = rb_str_new2("<");
|
349
|
+
str = rb_str_append(str, rb_str_new2(rb_class2name(CLASS_OF(_self))));
|
350
|
+
str = rb_str_cat2(str, ": ");
|
351
|
+
str = rb_str_append(str, layout_inspect(
|
352
|
+
self->descriptor->layout, Message_data(self)));
|
353
|
+
str = rb_str_cat2(str, ">");
|
354
|
+
return str;
|
355
|
+
}
|
356
|
+
|
357
|
+
|
358
|
+
VALUE Message_to_h(VALUE _self) {
|
359
|
+
MessageHeader* self;
|
360
|
+
VALUE hash;
|
361
|
+
upb_msg_field_iter it;
|
362
|
+
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
363
|
+
|
364
|
+
hash = rb_hash_new();
|
365
|
+
|
366
|
+
for (upb_msg_field_begin(&it, self->descriptor->msgdef);
|
367
|
+
!upb_msg_field_done(&it);
|
368
|
+
upb_msg_field_next(&it)) {
|
369
|
+
const upb_fielddef* field = upb_msg_iter_field(&it);
|
370
|
+
VALUE msg_value = layout_get(self->descriptor->layout, Message_data(self),
|
371
|
+
field);
|
372
|
+
VALUE msg_key = ID2SYM(rb_intern(upb_fielddef_name(field)));
|
373
|
+
if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
|
374
|
+
msg_value = RepeatedField_to_ary(msg_value);
|
375
|
+
}
|
376
|
+
rb_hash_aset(hash, msg_key, msg_value);
|
377
|
+
}
|
378
|
+
return hash;
|
379
|
+
}
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
/*
|
384
|
+
* call-seq:
|
385
|
+
* Message.[](index) => value
|
386
|
+
*
|
387
|
+
* Accesses a field's value by field name. The provided field name should be a
|
388
|
+
* string.
|
389
|
+
*/
|
390
|
+
VALUE Message_index(VALUE _self, VALUE field_name) {
|
391
|
+
MessageHeader* self;
|
392
|
+
const upb_fielddef* field;
|
393
|
+
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
394
|
+
Check_Type(field_name, T_STRING);
|
395
|
+
field = upb_msgdef_ntofz(self->descriptor->msgdef, RSTRING_PTR(field_name));
|
396
|
+
if (field == NULL) {
|
397
|
+
return Qnil;
|
398
|
+
}
|
399
|
+
return layout_get(self->descriptor->layout, Message_data(self), field);
|
400
|
+
}
|
401
|
+
|
402
|
+
/*
|
403
|
+
* call-seq:
|
404
|
+
* Message.[]=(index, value)
|
405
|
+
*
|
406
|
+
* Sets a field's value by field name. The provided field name should be a
|
407
|
+
* string.
|
408
|
+
*/
|
409
|
+
VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value) {
|
410
|
+
MessageHeader* self;
|
411
|
+
const upb_fielddef* field;
|
412
|
+
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
413
|
+
Check_Type(field_name, T_STRING);
|
414
|
+
field = upb_msgdef_ntofz(self->descriptor->msgdef, RSTRING_PTR(field_name));
|
415
|
+
if (field == NULL) {
|
416
|
+
rb_raise(rb_eArgError, "Unknown field: %s", RSTRING_PTR(field_name));
|
417
|
+
}
|
418
|
+
layout_set(self->descriptor->layout, Message_data(self), field, value);
|
419
|
+
return Qnil;
|
420
|
+
}
|
421
|
+
|
422
|
+
/*
|
423
|
+
* call-seq:
|
424
|
+
* Message.descriptor => descriptor
|
425
|
+
*
|
426
|
+
* Class method that returns the Descriptor instance corresponding to this
|
427
|
+
* message class's type.
|
428
|
+
*/
|
429
|
+
VALUE Message_descriptor(VALUE klass) {
|
430
|
+
return rb_ivar_get(klass, descriptor_instancevar_interned);
|
431
|
+
}
|
432
|
+
|
433
|
+
VALUE build_class_from_descriptor(Descriptor* desc) {
|
434
|
+
const char *name;
|
435
|
+
VALUE klass;
|
436
|
+
|
437
|
+
if (desc->layout == NULL) {
|
438
|
+
desc->layout = create_layout(desc->msgdef);
|
439
|
+
}
|
440
|
+
if (desc->fill_method == NULL) {
|
441
|
+
desc->fill_method = new_fillmsg_decodermethod(desc, &desc->fill_method);
|
442
|
+
}
|
443
|
+
|
444
|
+
name = upb_msgdef_fullname(desc->msgdef);
|
445
|
+
if (name == NULL) {
|
446
|
+
rb_raise(rb_eRuntimeError, "Descriptor does not have assigned name.");
|
447
|
+
}
|
448
|
+
|
449
|
+
klass = rb_define_class_id(
|
450
|
+
// Docs say this parameter is ignored. User will assign return value to
|
451
|
+
// their own toplevel constant class name.
|
452
|
+
rb_intern("Message"),
|
453
|
+
rb_cObject);
|
454
|
+
rb_ivar_set(klass, descriptor_instancevar_interned,
|
455
|
+
get_def_obj(desc->msgdef));
|
456
|
+
rb_define_alloc_func(klass, Message_alloc);
|
457
|
+
rb_require("google/protobuf/message_exts");
|
458
|
+
rb_include_module(klass, rb_eval_string("Google::Protobuf::MessageExts"));
|
459
|
+
rb_extend_object(
|
460
|
+
klass, rb_eval_string("Google::Protobuf::MessageExts::ClassMethods"));
|
461
|
+
|
462
|
+
rb_define_method(klass, "method_missing",
|
463
|
+
Message_method_missing, -1);
|
464
|
+
rb_define_method(klass, "initialize", Message_initialize, -1);
|
465
|
+
rb_define_method(klass, "dup", Message_dup, 0);
|
466
|
+
// Also define #clone so that we don't inherit Object#clone.
|
467
|
+
rb_define_method(klass, "clone", Message_dup, 0);
|
468
|
+
rb_define_method(klass, "==", Message_eq, 1);
|
469
|
+
rb_define_method(klass, "hash", Message_hash, 0);
|
470
|
+
rb_define_method(klass, "to_h", Message_to_h, 0);
|
471
|
+
rb_define_method(klass, "to_hash", Message_to_h, 0);
|
472
|
+
rb_define_method(klass, "inspect", Message_inspect, 0);
|
473
|
+
rb_define_method(klass, "[]", Message_index, 1);
|
474
|
+
rb_define_method(klass, "[]=", Message_index_set, 2);
|
475
|
+
rb_define_singleton_method(klass, "decode", Message_decode, 1);
|
476
|
+
rb_define_singleton_method(klass, "encode", Message_encode, 1);
|
477
|
+
rb_define_singleton_method(klass, "decode_json", Message_decode_json, 1);
|
478
|
+
rb_define_singleton_method(klass, "encode_json", Message_encode_json, 1);
|
479
|
+
rb_define_singleton_method(klass, "descriptor", Message_descriptor, 0);
|
480
|
+
|
481
|
+
return klass;
|
482
|
+
}
|
483
|
+
|
484
|
+
/*
|
485
|
+
* call-seq:
|
486
|
+
* Enum.lookup(number) => name
|
487
|
+
*
|
488
|
+
* This module method, provided on each generated enum module, looks up an enum
|
489
|
+
* value by number and returns its name as a Ruby symbol, or nil if not found.
|
490
|
+
*/
|
491
|
+
VALUE enum_lookup(VALUE self, VALUE number) {
|
492
|
+
int32_t num = NUM2INT(number);
|
493
|
+
VALUE desc = rb_ivar_get(self, descriptor_instancevar_interned);
|
494
|
+
EnumDescriptor* enumdesc = ruby_to_EnumDescriptor(desc);
|
495
|
+
|
496
|
+
const char* name = upb_enumdef_iton(enumdesc->enumdef, num);
|
497
|
+
if (name == NULL) {
|
498
|
+
return Qnil;
|
499
|
+
} else {
|
500
|
+
return ID2SYM(rb_intern(name));
|
501
|
+
}
|
502
|
+
}
|
503
|
+
|
504
|
+
/*
|
505
|
+
* call-seq:
|
506
|
+
* Enum.resolve(name) => number
|
507
|
+
*
|
508
|
+
* This module method, provided on each generated enum module, looks up an enum
|
509
|
+
* value by name (as a Ruby symbol) and returns its name, or nil if not found.
|
510
|
+
*/
|
511
|
+
VALUE enum_resolve(VALUE self, VALUE sym) {
|
512
|
+
const char* name = rb_id2name(SYM2ID(sym));
|
513
|
+
VALUE desc = rb_ivar_get(self, descriptor_instancevar_interned);
|
514
|
+
EnumDescriptor* enumdesc = ruby_to_EnumDescriptor(desc);
|
515
|
+
|
516
|
+
int32_t num = 0;
|
517
|
+
bool found = upb_enumdef_ntoiz(enumdesc->enumdef, name, &num);
|
518
|
+
if (!found) {
|
519
|
+
return Qnil;
|
520
|
+
} else {
|
521
|
+
return INT2NUM(num);
|
522
|
+
}
|
523
|
+
}
|
524
|
+
|
525
|
+
/*
|
526
|
+
* call-seq:
|
527
|
+
* Enum.descriptor
|
528
|
+
*
|
529
|
+
* This module method, provided on each generated enum module, returns the
|
530
|
+
* EnumDescriptor corresponding to this enum type.
|
531
|
+
*/
|
532
|
+
VALUE enum_descriptor(VALUE self) {
|
533
|
+
return rb_ivar_get(self, descriptor_instancevar_interned);
|
534
|
+
}
|
535
|
+
|
536
|
+
VALUE build_module_from_enumdesc(EnumDescriptor* enumdesc) {
|
537
|
+
VALUE mod = rb_define_module_id(
|
538
|
+
rb_intern(upb_enumdef_fullname(enumdesc->enumdef)));
|
539
|
+
|
540
|
+
upb_enum_iter it;
|
541
|
+
for (upb_enum_begin(&it, enumdesc->enumdef);
|
542
|
+
!upb_enum_done(&it);
|
543
|
+
upb_enum_next(&it)) {
|
544
|
+
const char* name = upb_enum_iter_name(&it);
|
545
|
+
int32_t value = upb_enum_iter_number(&it);
|
546
|
+
if (name[0] < 'A' || name[0] > 'Z') {
|
547
|
+
rb_raise(rb_eTypeError,
|
548
|
+
"Enum value '%s' does not start with an uppercase letter "
|
549
|
+
"as is required for Ruby constants.",
|
550
|
+
name);
|
551
|
+
}
|
552
|
+
rb_define_const(mod, name, INT2NUM(value));
|
553
|
+
}
|
554
|
+
|
555
|
+
rb_define_singleton_method(mod, "lookup", enum_lookup, 1);
|
556
|
+
rb_define_singleton_method(mod, "resolve", enum_resolve, 1);
|
557
|
+
rb_define_singleton_method(mod, "descriptor", enum_descriptor, 0);
|
558
|
+
rb_ivar_set(mod, descriptor_instancevar_interned,
|
559
|
+
get_def_obj(enumdesc->enumdef));
|
560
|
+
|
561
|
+
return mod;
|
562
|
+
}
|
563
|
+
|
564
|
+
/*
|
565
|
+
* call-seq:
|
566
|
+
* Google::Protobuf.deep_copy(obj) => copy_of_obj
|
567
|
+
*
|
568
|
+
* Performs a deep copy of a RepeatedField instance, a Map instance, or a
|
569
|
+
* message object, recursively copying its members.
|
570
|
+
*/
|
571
|
+
VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj) {
|
572
|
+
VALUE klass = CLASS_OF(obj);
|
573
|
+
if (klass == cRepeatedField) {
|
574
|
+
return RepeatedField_deep_copy(obj);
|
575
|
+
} else if (klass == cMap) {
|
576
|
+
return Map_deep_copy(obj);
|
577
|
+
} else {
|
578
|
+
return Message_deep_copy(obj);
|
579
|
+
}
|
580
|
+
}
|