jitera-google-protobuf 3.21.12.pre.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +7 -0
  2. data/ext/google/protobuf_c/BUILD.bazel +72 -0
  3. data/ext/google/protobuf_c/convert.c +361 -0
  4. data/ext/google/protobuf_c/convert.h +75 -0
  5. data/ext/google/protobuf_c/defs.c +1280 -0
  6. data/ext/google/protobuf_c/defs.h +107 -0
  7. data/ext/google/protobuf_c/extconf.rb +28 -0
  8. data/ext/google/protobuf_c/map.c +687 -0
  9. data/ext/google/protobuf_c/map.h +66 -0
  10. data/ext/google/protobuf_c/message.c +1435 -0
  11. data/ext/google/protobuf_c/message.h +104 -0
  12. data/ext/google/protobuf_c/naive.c +92 -0
  13. data/ext/google/protobuf_c/protobuf.c +480 -0
  14. data/ext/google/protobuf_c/protobuf.h +120 -0
  15. data/ext/google/protobuf_c/range2-neon.c +157 -0
  16. data/ext/google/protobuf_c/range2-sse.c +170 -0
  17. data/ext/google/protobuf_c/repeated_field.c +657 -0
  18. data/ext/google/protobuf_c/repeated_field.h +63 -0
  19. data/ext/google/protobuf_c/ruby-upb.c +13707 -0
  20. data/ext/google/protobuf_c/ruby-upb.h +10582 -0
  21. data/ext/google/protobuf_c/utf8_range.h +21 -0
  22. data/ext/google/protobuf_c/wrap_memcpy.c +52 -0
  23. data/lib/google/protobuf/any_pb.rb +19 -0
  24. data/lib/google/protobuf/api_pb.rb +42 -0
  25. data/lib/google/protobuf/descriptor_dsl.rb +465 -0
  26. data/lib/google/protobuf/descriptor_pb.rb +279 -0
  27. data/lib/google/protobuf/duration_pb.rb +19 -0
  28. data/lib/google/protobuf/empty_pb.rb +17 -0
  29. data/lib/google/protobuf/field_mask_pb.rb +18 -0
  30. data/lib/google/protobuf/message_exts.rb +58 -0
  31. data/lib/google/protobuf/repeated_field.rb +201 -0
  32. data/lib/google/protobuf/source_context_pb.rb +18 -0
  33. data/lib/google/protobuf/struct_pb.rb +37 -0
  34. data/lib/google/protobuf/timestamp_pb.rb +19 -0
  35. data/lib/google/protobuf/type_pb.rb +92 -0
  36. data/lib/google/protobuf/well_known_types.rb +240 -0
  37. data/lib/google/protobuf/wrappers_pb.rb +50 -0
  38. data/lib/google/protobuf.rb +79 -0
  39. metadata +129 -0
@@ -0,0 +1,1435 @@
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 "message.h"
32
+
33
+ #include "convert.h"
34
+ #include "defs.h"
35
+ #include "map.h"
36
+ #include "protobuf.h"
37
+ #include "repeated_field.h"
38
+
39
+ static VALUE cParseError = Qnil;
40
+ static VALUE cAbstractMessage = Qnil;
41
+ static ID descriptor_instancevar_interned;
42
+
43
+ static VALUE initialize_rb_class_with_no_args(VALUE klass) {
44
+ return rb_funcall(klass, rb_intern("new"), 0);
45
+ }
46
+
47
+ VALUE MessageOrEnum_GetDescriptor(VALUE klass) {
48
+ return rb_ivar_get(klass, descriptor_instancevar_interned);
49
+ }
50
+
51
+ // -----------------------------------------------------------------------------
52
+ // Class/module creation from msgdefs and enumdefs, respectively.
53
+ // -----------------------------------------------------------------------------
54
+
55
+ typedef struct {
56
+ VALUE arena;
57
+ const upb_Message* msg; // Can get as mutable when non-frozen.
58
+ const upb_MessageDef*
59
+ msgdef; // kept alive by self.class.descriptor reference.
60
+ } Message;
61
+
62
+ static void Message_mark(void* _self) {
63
+ Message* self = (Message*)_self;
64
+ rb_gc_mark(self->arena);
65
+ }
66
+
67
+ static rb_data_type_t Message_type = {
68
+ "Message",
69
+ {Message_mark, RUBY_DEFAULT_FREE, NULL},
70
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
71
+ };
72
+
73
+ static Message* ruby_to_Message(VALUE msg_rb) {
74
+ Message* msg;
75
+ TypedData_Get_Struct(msg_rb, Message, &Message_type, msg);
76
+ return msg;
77
+ }
78
+
79
+ static VALUE Message_alloc(VALUE klass) {
80
+ VALUE descriptor = rb_ivar_get(klass, descriptor_instancevar_interned);
81
+ Message* msg = ALLOC(Message);
82
+ VALUE ret;
83
+
84
+ msg->msgdef = Descriptor_GetMsgDef(descriptor);
85
+ msg->arena = Qnil;
86
+ msg->msg = NULL;
87
+
88
+ ret = TypedData_Wrap_Struct(klass, &Message_type, msg);
89
+ rb_ivar_set(ret, descriptor_instancevar_interned, descriptor);
90
+
91
+ return ret;
92
+ }
93
+
94
+ const upb_Message* Message_Get(VALUE msg_rb, const upb_MessageDef** m) {
95
+ Message* msg = ruby_to_Message(msg_rb);
96
+ if (m) *m = msg->msgdef;
97
+ return msg->msg;
98
+ }
99
+
100
+ upb_Message* Message_GetMutable(VALUE msg_rb, const upb_MessageDef** m) {
101
+ rb_check_frozen(msg_rb);
102
+ return (upb_Message*)Message_Get(msg_rb, m);
103
+ }
104
+
105
+ void Message_InitPtr(VALUE self_, upb_Message* msg, VALUE arena) {
106
+ Message* self = ruby_to_Message(self_);
107
+ self->msg = msg;
108
+ self->arena = arena;
109
+ ObjectCache_Add(msg, self_);
110
+ }
111
+
112
+ VALUE Message_GetArena(VALUE msg_rb) {
113
+ Message* msg = ruby_to_Message(msg_rb);
114
+ return msg->arena;
115
+ }
116
+
117
+ void Message_CheckClass(VALUE klass) {
118
+ if (rb_get_alloc_func(klass) != &Message_alloc) {
119
+ rb_raise(rb_eArgError,
120
+ "Message class was not returned by the DescriptorPool.");
121
+ }
122
+ }
123
+
124
+ VALUE Message_GetRubyWrapper(upb_Message* msg, const upb_MessageDef* m,
125
+ VALUE arena) {
126
+ if (msg == NULL) return Qnil;
127
+
128
+ VALUE val = ObjectCache_Get(msg);
129
+
130
+ if (val == Qnil) {
131
+ VALUE klass = Descriptor_DefToClass(m);
132
+ val = Message_alloc(klass);
133
+ Message_InitPtr(val, msg, arena);
134
+ }
135
+
136
+ return val;
137
+ }
138
+
139
+ void Message_PrintMessage(StringBuilder* b, const upb_Message* msg,
140
+ const upb_MessageDef* m) {
141
+ bool first = true;
142
+ int n = upb_MessageDef_FieldCount(m);
143
+ VALUE klass = Descriptor_DefToClass(m);
144
+ StringBuilder_Printf(b, "<%s: ", rb_class2name(klass));
145
+
146
+ for (int i = 0; i < n; i++) {
147
+ const upb_FieldDef* field = upb_MessageDef_Field(m, i);
148
+
149
+ if (upb_FieldDef_HasPresence(field) &&
150
+ !upb_Message_HasFieldByDef(msg, field)) {
151
+ continue;
152
+ }
153
+
154
+ if (!first) {
155
+ StringBuilder_Printf(b, ", ");
156
+ } else {
157
+ first = false;
158
+ }
159
+
160
+ upb_MessageValue msgval = upb_Message_GetFieldByDef(msg, field);
161
+
162
+ StringBuilder_Printf(b, "%s: ", upb_FieldDef_Name(field));
163
+
164
+ if (upb_FieldDef_IsMap(field)) {
165
+ const upb_MessageDef* entry_m = upb_FieldDef_MessageSubDef(field);
166
+ const upb_FieldDef* key_f = upb_MessageDef_FindFieldByNumber(entry_m, 1);
167
+ const upb_FieldDef* val_f = upb_MessageDef_FindFieldByNumber(entry_m, 2);
168
+ TypeInfo val_info = TypeInfo_get(val_f);
169
+ Map_Inspect(b, msgval.map_val, upb_FieldDef_CType(key_f), val_info);
170
+ } else if (upb_FieldDef_IsRepeated(field)) {
171
+ RepeatedField_Inspect(b, msgval.array_val, TypeInfo_get(field));
172
+ } else {
173
+ StringBuilder_PrintMsgval(b, msgval, TypeInfo_get(field));
174
+ }
175
+ }
176
+
177
+ StringBuilder_Printf(b, ">");
178
+ }
179
+
180
+ // Helper functions for #method_missing ////////////////////////////////////////
181
+
182
+ enum {
183
+ METHOD_UNKNOWN = 0,
184
+ METHOD_GETTER = 1,
185
+ METHOD_SETTER = 2,
186
+ METHOD_CLEAR = 3,
187
+ METHOD_PRESENCE = 4,
188
+ METHOD_ENUM_GETTER = 5,
189
+ METHOD_WRAPPER_GETTER = 6,
190
+ METHOD_WRAPPER_SETTER = 7
191
+ };
192
+
193
+ // Check if the field is a well known wrapper type
194
+ static bool IsWrapper(const upb_MessageDef* m) {
195
+ if (!m) return false;
196
+ switch (upb_MessageDef_WellKnownType(m)) {
197
+ case kUpb_WellKnown_DoubleValue:
198
+ case kUpb_WellKnown_FloatValue:
199
+ case kUpb_WellKnown_Int64Value:
200
+ case kUpb_WellKnown_UInt64Value:
201
+ case kUpb_WellKnown_Int32Value:
202
+ case kUpb_WellKnown_UInt32Value:
203
+ case kUpb_WellKnown_StringValue:
204
+ case kUpb_WellKnown_BytesValue:
205
+ case kUpb_WellKnown_BoolValue:
206
+ return true;
207
+ default:
208
+ return false;
209
+ }
210
+ }
211
+
212
+ static bool IsFieldWrapper(const upb_FieldDef* f) {
213
+ return IsWrapper(upb_FieldDef_MessageSubDef(f));
214
+ }
215
+
216
+ static bool Match(const upb_MessageDef* m, const char* name,
217
+ const upb_FieldDef** f, const upb_OneofDef** o,
218
+ const char* prefix, const char* suffix) {
219
+ size_t sp = strlen(prefix);
220
+ size_t ss = strlen(suffix);
221
+ size_t sn = strlen(name);
222
+
223
+ if (sn <= sp + ss) return false;
224
+
225
+ if (memcmp(name, prefix, sp) != 0 ||
226
+ memcmp(name + sn - ss, suffix, ss) != 0) {
227
+ return false;
228
+ }
229
+
230
+ return upb_MessageDef_FindByNameWithSize(m, name + sp, sn - sp - ss, f, o);
231
+ }
232
+
233
+ static int extract_method_call(VALUE method_name, Message* self,
234
+ const upb_FieldDef** f, const upb_OneofDef** o) {
235
+ const upb_MessageDef* m = self->msgdef;
236
+ const char* name;
237
+
238
+ Check_Type(method_name, T_SYMBOL);
239
+ name = rb_id2name(SYM2ID(method_name));
240
+
241
+ if (Match(m, name, f, o, "", "")) return METHOD_GETTER;
242
+ if (Match(m, name, f, o, "", "=")) return METHOD_SETTER;
243
+ if (Match(m, name, f, o, "clear_", "")) return METHOD_CLEAR;
244
+ if (Match(m, name, f, o, "has_", "?") &&
245
+ (*o || (*f && upb_FieldDef_HasPresence(*f)))) {
246
+ // Disallow oneof hazzers for proto3.
247
+ // TODO(haberman): remove this test when we are enabling oneof hazzers for
248
+ // proto3.
249
+ if (*f && !upb_FieldDef_IsSubMessage(*f) &&
250
+ upb_FieldDef_RealContainingOneof(*f) &&
251
+ upb_MessageDef_Syntax(upb_FieldDef_ContainingType(*f)) !=
252
+ kUpb_Syntax_Proto2) {
253
+ return METHOD_UNKNOWN;
254
+ }
255
+ return METHOD_PRESENCE;
256
+ }
257
+ if (Match(m, name, f, o, "", "_as_value") && *f &&
258
+ !upb_FieldDef_IsRepeated(*f) && IsFieldWrapper(*f)) {
259
+ return METHOD_WRAPPER_GETTER;
260
+ }
261
+ if (Match(m, name, f, o, "", "_as_value=") && *f &&
262
+ !upb_FieldDef_IsRepeated(*f) && IsFieldWrapper(*f)) {
263
+ return METHOD_WRAPPER_SETTER;
264
+ }
265
+ if (Match(m, name, f, o, "", "_const") && *f &&
266
+ upb_FieldDef_CType(*f) == kUpb_CType_Enum) {
267
+ return METHOD_ENUM_GETTER;
268
+ }
269
+
270
+ return METHOD_UNKNOWN;
271
+ }
272
+
273
+ static VALUE Message_oneof_accessor(VALUE _self, const upb_OneofDef* o,
274
+ int accessor_type) {
275
+ Message* self = ruby_to_Message(_self);
276
+ const upb_FieldDef* oneof_field = upb_Message_WhichOneof(self->msg, o);
277
+
278
+ switch (accessor_type) {
279
+ case METHOD_PRESENCE:
280
+ return oneof_field == NULL ? Qfalse : Qtrue;
281
+ case METHOD_CLEAR:
282
+ if (oneof_field != NULL) {
283
+ upb_Message_ClearFieldByDef(Message_GetMutable(_self, NULL),
284
+ oneof_field);
285
+ }
286
+ return Qnil;
287
+ case METHOD_GETTER:
288
+ return oneof_field == NULL
289
+ ? Qnil
290
+ : ID2SYM(rb_intern(upb_FieldDef_Name(oneof_field)));
291
+ case METHOD_SETTER:
292
+ rb_raise(rb_eRuntimeError, "Oneof accessors are read-only.");
293
+ }
294
+ rb_raise(rb_eRuntimeError, "Invalid access of oneof field.");
295
+ }
296
+
297
+ static void Message_setfield(upb_Message* msg, const upb_FieldDef* f, VALUE val,
298
+ upb_Arena* arena) {
299
+ upb_MessageValue msgval;
300
+ if (upb_FieldDef_IsMap(f)) {
301
+ msgval.map_val = Map_GetUpbMap(val, f, arena);
302
+ } else if (upb_FieldDef_IsRepeated(f)) {
303
+ msgval.array_val = RepeatedField_GetUpbArray(val, f, arena);
304
+ } else {
305
+ if (val == Qnil &&
306
+ (upb_FieldDef_IsSubMessage(f) || upb_FieldDef_RealContainingOneof(f))) {
307
+ upb_Message_ClearFieldByDef(msg, f);
308
+ return;
309
+ }
310
+ msgval =
311
+ Convert_RubyToUpb(val, upb_FieldDef_Name(f), TypeInfo_get(f), arena);
312
+ }
313
+ upb_Message_SetFieldByDef(msg, f, msgval, arena);
314
+ }
315
+
316
+ VALUE Message_getfield(VALUE _self, const upb_FieldDef* f) {
317
+ Message* self = ruby_to_Message(_self);
318
+ // This is a special-case: upb_Message_Mutable() for map & array are logically
319
+ // const (they will not change what is serialized) but physically
320
+ // non-const, as they do allocate a repeated field or map. The logical
321
+ // constness means it's ok to do even if the message is frozen.
322
+ upb_Message* msg = (upb_Message*)self->msg;
323
+ upb_Arena* arena = Arena_get(self->arena);
324
+ if (upb_FieldDef_IsMap(f)) {
325
+ upb_Map* map = upb_Message_Mutable(msg, f, arena).map;
326
+ const upb_FieldDef* key_f = map_field_key(f);
327
+ const upb_FieldDef* val_f = map_field_value(f);
328
+ upb_CType key_type = upb_FieldDef_CType(key_f);
329
+ TypeInfo value_type_info = TypeInfo_get(val_f);
330
+ return Map_GetRubyWrapper(map, key_type, value_type_info, self->arena);
331
+ } else if (upb_FieldDef_IsRepeated(f)) {
332
+ upb_Array* arr = upb_Message_Mutable(msg, f, arena).array;
333
+ return RepeatedField_GetRubyWrapper(arr, TypeInfo_get(f), self->arena);
334
+ } else if (upb_FieldDef_IsSubMessage(f)) {
335
+ if (!upb_Message_HasFieldByDef(self->msg, f)) return Qnil;
336
+ upb_Message* submsg = upb_Message_Mutable(msg, f, arena).msg;
337
+ const upb_MessageDef* m = upb_FieldDef_MessageSubDef(f);
338
+ return Message_GetRubyWrapper(submsg, m, self->arena);
339
+ } else {
340
+ upb_MessageValue msgval = upb_Message_GetFieldByDef(self->msg, f);
341
+ return Convert_UpbToRuby(msgval, TypeInfo_get(f), self->arena);
342
+ }
343
+ }
344
+
345
+ static VALUE Message_field_accessor(VALUE _self, const upb_FieldDef* f,
346
+ int accessor_type, int argc, VALUE* argv) {
347
+ upb_Arena* arena = Arena_get(Message_GetArena(_self));
348
+
349
+ switch (accessor_type) {
350
+ case METHOD_SETTER:
351
+ Message_setfield(Message_GetMutable(_self, NULL), f, argv[1], arena);
352
+ return Qnil;
353
+ case METHOD_CLEAR:
354
+ upb_Message_ClearFieldByDef(Message_GetMutable(_self, NULL), f);
355
+ return Qnil;
356
+ case METHOD_PRESENCE:
357
+ if (!upb_FieldDef_HasPresence(f)) {
358
+ rb_raise(rb_eRuntimeError, "Field does not have presence.");
359
+ }
360
+ return upb_Message_HasFieldByDef(Message_Get(_self, NULL), f);
361
+ case METHOD_WRAPPER_GETTER: {
362
+ Message* self = ruby_to_Message(_self);
363
+ if (upb_Message_HasFieldByDef(self->msg, f)) {
364
+ PBRUBY_ASSERT(upb_FieldDef_IsSubMessage(f) &&
365
+ !upb_FieldDef_IsRepeated(f));
366
+ upb_MessageValue wrapper = upb_Message_GetFieldByDef(self->msg, f);
367
+ const upb_MessageDef* wrapper_m = upb_FieldDef_MessageSubDef(f);
368
+ const upb_FieldDef* value_f =
369
+ upb_MessageDef_FindFieldByNumber(wrapper_m, 1);
370
+ upb_MessageValue value =
371
+ upb_Message_GetFieldByDef(wrapper.msg_val, value_f);
372
+ return Convert_UpbToRuby(value, TypeInfo_get(value_f), self->arena);
373
+ } else {
374
+ return Qnil;
375
+ }
376
+ }
377
+ case METHOD_WRAPPER_SETTER: {
378
+ upb_Message* msg = Message_GetMutable(_self, NULL);
379
+ if (argv[1] == Qnil) {
380
+ upb_Message_ClearFieldByDef(msg, f);
381
+ } else {
382
+ const upb_FieldDef* val_f =
383
+ upb_MessageDef_FindFieldByNumber(upb_FieldDef_MessageSubDef(f), 1);
384
+ upb_MessageValue msgval = Convert_RubyToUpb(
385
+ argv[1], upb_FieldDef_Name(f), TypeInfo_get(val_f), arena);
386
+ upb_Message* wrapper = upb_Message_Mutable(msg, f, arena).msg;
387
+ upb_Message_SetFieldByDef(wrapper, val_f, msgval, arena);
388
+ }
389
+ return Qnil;
390
+ }
391
+ case METHOD_ENUM_GETTER: {
392
+ upb_MessageValue msgval =
393
+ upb_Message_GetFieldByDef(Message_Get(_self, NULL), f);
394
+
395
+ if (upb_FieldDef_Label(f) == kUpb_Label_Repeated) {
396
+ // Map repeated fields to a new type with ints
397
+ VALUE arr = rb_ary_new();
398
+ size_t i, n = upb_Array_Size(msgval.array_val);
399
+ for (i = 0; i < n; i++) {
400
+ upb_MessageValue elem = upb_Array_Get(msgval.array_val, i);
401
+ rb_ary_push(arr, INT2NUM(elem.int32_val));
402
+ }
403
+ return arr;
404
+ } else {
405
+ return INT2NUM(msgval.int32_val);
406
+ }
407
+ }
408
+ case METHOD_GETTER:
409
+ return Message_getfield(_self, f);
410
+ default:
411
+ rb_raise(rb_eRuntimeError, "Internal error, no such accessor: %d",
412
+ accessor_type);
413
+ }
414
+ }
415
+
416
+ /*
417
+ * call-seq:
418
+ * Message.method_missing(*args)
419
+ *
420
+ * Provides accessors and setters and methods to clear and check for presence of
421
+ * message fields according to their field names.
422
+ *
423
+ * For any field whose name does not conflict with a built-in method, an
424
+ * accessor is provided with the same name as the field, and a setter is
425
+ * provided with the name of the field plus the '=' suffix. Thus, given a
426
+ * message instance 'msg' with field 'foo', the following code is valid:
427
+ *
428
+ * msg.foo = 42
429
+ * puts msg.foo
430
+ *
431
+ * This method also provides read-only accessors for oneofs. If a oneof exists
432
+ * with name 'my_oneof', then msg.my_oneof will return a Ruby symbol equal to
433
+ * the name of the field in that oneof that is currently set, or nil if none.
434
+ *
435
+ * It also provides methods of the form 'clear_fieldname' to clear the value
436
+ * of the field 'fieldname'. For basic data types, this will set the default
437
+ * value of the field.
438
+ *
439
+ * Additionally, it provides methods of the form 'has_fieldname?', which returns
440
+ * true if the field 'fieldname' is set in the message object, else false. For
441
+ * 'proto3' syntax, calling this for a basic type field will result in an error.
442
+ */
443
+ static VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self) {
444
+ Message* self = ruby_to_Message(_self);
445
+ const upb_OneofDef* o;
446
+ const upb_FieldDef* f;
447
+ int accessor_type;
448
+
449
+ if (argc < 1) {
450
+ rb_raise(rb_eArgError, "Expected method name as first argument.");
451
+ }
452
+
453
+ accessor_type = extract_method_call(argv[0], self, &f, &o);
454
+
455
+ if (accessor_type == METHOD_UNKNOWN) return rb_call_super(argc, argv);
456
+
457
+ // Validate argument count.
458
+ switch (accessor_type) {
459
+ case METHOD_SETTER:
460
+ case METHOD_WRAPPER_SETTER:
461
+ if (argc != 2) {
462
+ rb_raise(rb_eArgError, "Expected 2 arguments, received %d", argc);
463
+ }
464
+ rb_check_frozen(_self);
465
+ break;
466
+ default:
467
+ if (argc != 1) {
468
+ rb_raise(rb_eArgError, "Expected 1 argument, received %d", argc);
469
+ }
470
+ break;
471
+ }
472
+
473
+ // Dispatch accessor.
474
+ if (o != NULL) {
475
+ return Message_oneof_accessor(_self, o, accessor_type);
476
+ } else {
477
+ return Message_field_accessor(_self, f, accessor_type, argc, argv);
478
+ }
479
+ }
480
+
481
+ static VALUE Message_respond_to_missing(int argc, VALUE* argv, VALUE _self) {
482
+ Message* self = ruby_to_Message(_self);
483
+ const upb_OneofDef* o;
484
+ const upb_FieldDef* f;
485
+ int accessor_type;
486
+
487
+ if (argc < 1) {
488
+ rb_raise(rb_eArgError, "Expected method name as first argument.");
489
+ }
490
+
491
+ accessor_type = extract_method_call(argv[0], self, &f, &o);
492
+
493
+ if (accessor_type == METHOD_UNKNOWN) {
494
+ return rb_call_super(argc, argv);
495
+ } else if (o != NULL) {
496
+ return accessor_type == METHOD_SETTER ? Qfalse : Qtrue;
497
+ } else {
498
+ return Qtrue;
499
+ }
500
+ }
501
+
502
+ void Message_InitFromValue(upb_Message* msg, const upb_MessageDef* m, VALUE val,
503
+ upb_Arena* arena);
504
+
505
+ typedef struct {
506
+ upb_Map* map;
507
+ TypeInfo key_type;
508
+ TypeInfo val_type;
509
+ upb_Arena* arena;
510
+ } MapInit;
511
+
512
+ static int Map_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
513
+ MapInit* map_init = (MapInit*)_self;
514
+ upb_MessageValue k, v;
515
+ k = Convert_RubyToUpb(key, "", map_init->key_type, NULL);
516
+
517
+ if (map_init->val_type.type == kUpb_CType_Message && TYPE(val) == T_HASH) {
518
+ upb_MiniTable* t = upb_MessageDef_MiniTable(map_init->val_type.def.msgdef);
519
+ upb_Message* msg = upb_Message_New(t, map_init->arena);
520
+ Message_InitFromValue(msg, map_init->val_type.def.msgdef, val,
521
+ map_init->arena);
522
+ v.msg_val = msg;
523
+ } else {
524
+ v = Convert_RubyToUpb(val, "", map_init->val_type, map_init->arena);
525
+ }
526
+ upb_Map_Set(map_init->map, k, v, map_init->arena);
527
+ return ST_CONTINUE;
528
+ }
529
+
530
+ static void Map_InitFromValue(upb_Map* map, const upb_FieldDef* f, VALUE val,
531
+ upb_Arena* arena) {
532
+ const upb_MessageDef* entry_m = upb_FieldDef_MessageSubDef(f);
533
+ const upb_FieldDef* key_f = upb_MessageDef_FindFieldByNumber(entry_m, 1);
534
+ const upb_FieldDef* val_f = upb_MessageDef_FindFieldByNumber(entry_m, 2);
535
+ if (TYPE(val) != T_HASH) {
536
+ rb_raise(rb_eArgError,
537
+ "Expected Hash object as initializer value for map field '%s' "
538
+ "(given %s).",
539
+ upb_FieldDef_Name(f), rb_class2name(CLASS_OF(val)));
540
+ }
541
+ MapInit map_init = {map, TypeInfo_get(key_f), TypeInfo_get(val_f), arena};
542
+ rb_hash_foreach(val, Map_initialize_kwarg, (VALUE)&map_init);
543
+ }
544
+
545
+ static upb_MessageValue MessageValue_FromValue(VALUE val, TypeInfo info,
546
+ upb_Arena* arena) {
547
+ if (info.type == kUpb_CType_Message) {
548
+ upb_MessageValue msgval;
549
+ upb_MiniTable* t = upb_MessageDef_MiniTable(info.def.msgdef);
550
+ upb_Message* msg = upb_Message_New(t, arena);
551
+ Message_InitFromValue(msg, info.def.msgdef, val, arena);
552
+ msgval.msg_val = msg;
553
+ return msgval;
554
+ } else {
555
+ return Convert_RubyToUpb(val, "", info, arena);
556
+ }
557
+ }
558
+
559
+ static void RepeatedField_InitFromValue(upb_Array* arr, const upb_FieldDef* f,
560
+ VALUE val, upb_Arena* arena) {
561
+ TypeInfo type_info = TypeInfo_get(f);
562
+
563
+ if (TYPE(val) != T_ARRAY) {
564
+ rb_raise(rb_eArgError,
565
+ "Expected array as initializer value for repeated field '%s' "
566
+ "(given %s).",
567
+ upb_FieldDef_Name(f), rb_class2name(CLASS_OF(val)));
568
+ }
569
+
570
+ for (int i = 0; i < RARRAY_LEN(val); i++) {
571
+ VALUE entry = rb_ary_entry(val, i);
572
+ upb_MessageValue msgval;
573
+ if (upb_FieldDef_IsSubMessage(f) && TYPE(entry) == T_HASH) {
574
+ msgval = MessageValue_FromValue(entry, type_info, arena);
575
+ } else {
576
+ msgval = Convert_RubyToUpb(entry, upb_FieldDef_Name(f), type_info, arena);
577
+ }
578
+ upb_Array_Append(arr, msgval, arena);
579
+ }
580
+ }
581
+
582
+ static void Message_InitFieldFromValue(upb_Message* msg, const upb_FieldDef* f,
583
+ VALUE val, upb_Arena* arena) {
584
+ if (TYPE(val) == T_NIL) return;
585
+
586
+ if (upb_FieldDef_IsMap(f)) {
587
+ upb_Map* map = upb_Message_Mutable(msg, f, arena).map;
588
+ Map_InitFromValue(map, f, val, arena);
589
+ } else if (upb_FieldDef_Label(f) == kUpb_Label_Repeated) {
590
+ upb_Array* arr = upb_Message_Mutable(msg, f, arena).array;
591
+ RepeatedField_InitFromValue(arr, f, val, arena);
592
+ } else if (upb_FieldDef_IsSubMessage(f)) {
593
+ if (TYPE(val) == T_HASH) {
594
+ upb_Message* submsg = upb_Message_Mutable(msg, f, arena).msg;
595
+ Message_InitFromValue(submsg, upb_FieldDef_MessageSubDef(f), val, arena);
596
+ } else {
597
+ Message_setfield(msg, f, val, arena);
598
+ }
599
+ } else {
600
+ upb_MessageValue msgval =
601
+ Convert_RubyToUpb(val, upb_FieldDef_Name(f), TypeInfo_get(f), arena);
602
+ upb_Message_SetFieldByDef(msg, f, msgval, arena);
603
+ }
604
+ }
605
+
606
+ typedef struct {
607
+ upb_Message* msg;
608
+ const upb_MessageDef* msgdef;
609
+ upb_Arena* arena;
610
+ } MsgInit;
611
+
612
+ static int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
613
+ MsgInit* msg_init = (MsgInit*)_self;
614
+ const char* name;
615
+
616
+ if (TYPE(key) == T_STRING) {
617
+ name = RSTRING_PTR(key);
618
+ } else if (TYPE(key) == T_SYMBOL) {
619
+ name = RSTRING_PTR(rb_id2str(SYM2ID(key)));
620
+ } else {
621
+ rb_raise(rb_eArgError,
622
+ "Expected string or symbols as hash keys when initializing proto "
623
+ "from hash.");
624
+ }
625
+
626
+ const upb_FieldDef* f =
627
+ upb_MessageDef_FindFieldByName(msg_init->msgdef, name);
628
+
629
+ if (f == NULL) {
630
+ rb_raise(rb_eArgError,
631
+ "Unknown field name '%s' in initialization map entry.", name);
632
+ }
633
+
634
+ Message_InitFieldFromValue(msg_init->msg, f, val, msg_init->arena);
635
+ return ST_CONTINUE;
636
+ }
637
+
638
+ void Message_InitFromValue(upb_Message* msg, const upb_MessageDef* m, VALUE val,
639
+ upb_Arena* arena) {
640
+ MsgInit msg_init = {msg, m, arena};
641
+ if (TYPE(val) == T_HASH) {
642
+ rb_hash_foreach(val, Message_initialize_kwarg, (VALUE)&msg_init);
643
+ } else {
644
+ rb_raise(rb_eArgError, "Expected hash arguments or message, not %s",
645
+ rb_class2name(CLASS_OF(val)));
646
+ }
647
+ }
648
+
649
+ /*
650
+ * call-seq:
651
+ * Message.new(kwargs) => new_message
652
+ *
653
+ * Creates a new instance of the given message class. Keyword arguments may be
654
+ * provided with keywords corresponding to field names.
655
+ *
656
+ * Note that no literal Message class exists. Only concrete classes per message
657
+ * type exist, as provided by the #msgclass method on Descriptors after they
658
+ * have been added to a pool. The method definitions described here on the
659
+ * Message class are provided on each concrete message class.
660
+ */
661
+ static VALUE Message_initialize(int argc, VALUE* argv, VALUE _self) {
662
+ Message* self = ruby_to_Message(_self);
663
+ VALUE arena_rb = Arena_new();
664
+ upb_Arena* arena = Arena_get(arena_rb);
665
+ upb_MiniTable* t = upb_MessageDef_MiniTable(self->msgdef);
666
+ upb_Message* msg = upb_Message_New(t, arena);
667
+
668
+ Message_InitPtr(_self, msg, arena_rb);
669
+
670
+ if (argc == 0) {
671
+ return Qnil;
672
+ }
673
+ if (argc != 1) {
674
+ rb_raise(rb_eArgError, "Expected 0 or 1 arguments.");
675
+ }
676
+ Message_InitFromValue((upb_Message*)self->msg, self->msgdef, argv[0], arena);
677
+ return Qnil;
678
+ }
679
+
680
+ /*
681
+ * call-seq:
682
+ * Message.dup => new_message
683
+ *
684
+ * Performs a shallow copy of this message and returns the new copy.
685
+ */
686
+ static VALUE Message_dup(VALUE _self) {
687
+ Message* self = ruby_to_Message(_self);
688
+ VALUE new_msg = rb_class_new_instance(0, NULL, CLASS_OF(_self));
689
+ Message* new_msg_self = ruby_to_Message(new_msg);
690
+ size_t size = upb_MessageDef_MiniTable(self->msgdef)->size;
691
+
692
+ // TODO(copy unknown fields?)
693
+ // TODO(use official upb msg copy function)
694
+ memcpy((upb_Message*)new_msg_self->msg, self->msg, size);
695
+ Arena_fuse(self->arena, Arena_get(new_msg_self->arena));
696
+ return new_msg;
697
+ }
698
+
699
+ // Support function for Message_eq, and also used by other #eq functions.
700
+ bool Message_Equal(const upb_Message* m1, const upb_Message* m2,
701
+ const upb_MessageDef* m) {
702
+ if (m1 == m2) return true;
703
+
704
+ size_t size1, size2;
705
+ int encode_opts =
706
+ kUpb_EncodeOption_SkipUnknown | kUpb_EncodeOption_Deterministic;
707
+ upb_Arena* arena_tmp = upb_Arena_New();
708
+ const upb_MiniTable* layout = upb_MessageDef_MiniTable(m);
709
+
710
+ // Compare deterministically serialized payloads with no unknown fields.
711
+ char* data1;
712
+ char* data2;
713
+ upb_EncodeStatus status1 =
714
+ upb_Encode(m1, layout, encode_opts, arena_tmp, &data1, &size1);
715
+ upb_EncodeStatus status2 =
716
+ upb_Encode(m2, layout, encode_opts, arena_tmp, &data2, &size2);
717
+
718
+ if (status1 == kUpb_EncodeStatus_Ok && status2 == kUpb_EncodeStatus_Ok) {
719
+ bool ret = (size1 == size2) && (memcmp(data1, data2, size1) == 0);
720
+ upb_Arena_Free(arena_tmp);
721
+ return ret;
722
+ } else {
723
+ upb_Arena_Free(arena_tmp);
724
+ rb_raise(cParseError, "Error comparing messages");
725
+ }
726
+ }
727
+
728
+ /*
729
+ * call-seq:
730
+ * Message.==(other) => boolean
731
+ *
732
+ * Performs a deep comparison of this message with another. Messages are equal
733
+ * if they have the same type and if each field is equal according to the :==
734
+ * method's semantics (a more efficient comparison may actually be done if the
735
+ * field is of a primitive type).
736
+ */
737
+ static VALUE Message_eq(VALUE _self, VALUE _other) {
738
+ if (CLASS_OF(_self) != CLASS_OF(_other)) return Qfalse;
739
+
740
+ Message* self = ruby_to_Message(_self);
741
+ Message* other = ruby_to_Message(_other);
742
+ assert(self->msgdef == other->msgdef);
743
+
744
+ return Message_Equal(self->msg, other->msg, self->msgdef) ? Qtrue : Qfalse;
745
+ }
746
+
747
+ uint64_t Message_Hash(const upb_Message* msg, const upb_MessageDef* m,
748
+ uint64_t seed) {
749
+ upb_Arena* arena = upb_Arena_New();
750
+ char* data;
751
+ size_t size;
752
+
753
+ // Hash a deterministically serialized payloads with no unknown fields.
754
+ upb_EncodeStatus status = upb_Encode(
755
+ msg, upb_MessageDef_MiniTable(m),
756
+ kUpb_EncodeOption_SkipUnknown | kUpb_EncodeOption_Deterministic, arena,
757
+ &data, &size);
758
+
759
+ if (status == kUpb_EncodeStatus_Ok) {
760
+ uint64_t ret = _upb_Hash(data, size, seed);
761
+ upb_Arena_Free(arena);
762
+ return ret;
763
+ } else {
764
+ upb_Arena_Free(arena);
765
+ rb_raise(cParseError, "Error calculating hash");
766
+ }
767
+ }
768
+
769
+ /*
770
+ * call-seq:
771
+ * Message.hash => hash_value
772
+ *
773
+ * Returns a hash value that represents this message's field values.
774
+ */
775
+ static VALUE Message_hash(VALUE _self) {
776
+ Message* self = ruby_to_Message(_self);
777
+ uint64_t hash_value = Message_Hash(self->msg, self->msgdef, 0);
778
+ // RUBY_FIXNUM_MAX should be one less than a power of 2.
779
+ assert((RUBY_FIXNUM_MAX & (RUBY_FIXNUM_MAX + 1)) == 0);
780
+ return INT2FIX(hash_value & RUBY_FIXNUM_MAX);
781
+ }
782
+
783
+ /*
784
+ * call-seq:
785
+ * Message.inspect => string
786
+ *
787
+ * Returns a human-readable string representing this message. It will be
788
+ * formatted as "<MessageType: field1: value1, field2: value2, ...>". Each
789
+ * field's value is represented according to its own #inspect method.
790
+ */
791
+ static VALUE Message_inspect(VALUE _self) {
792
+ Message* self = ruby_to_Message(_self);
793
+
794
+ StringBuilder* builder = StringBuilder_New();
795
+ Message_PrintMessage(builder, self->msg, self->msgdef);
796
+ VALUE ret = StringBuilder_ToRubyString(builder);
797
+ StringBuilder_Free(builder);
798
+ return ret;
799
+ }
800
+
801
+ // Support functions for Message_to_h //////////////////////////////////////////
802
+
803
+ static VALUE RepeatedField_CreateArray(const upb_Array* arr,
804
+ TypeInfo type_info) {
805
+ int size = arr ? upb_Array_Size(arr) : 0;
806
+ VALUE ary = rb_ary_new2(size);
807
+
808
+ for (int i = 0; i < size; i++) {
809
+ upb_MessageValue msgval = upb_Array_Get(arr, i);
810
+ VALUE val = Scalar_CreateHash(msgval, type_info);
811
+ rb_ary_push(ary, val);
812
+ }
813
+
814
+ return ary;
815
+ }
816
+
817
+ static VALUE Message_CreateHash(const upb_Message* msg,
818
+ const upb_MessageDef* m) {
819
+ if (!msg) return Qnil;
820
+
821
+ VALUE hash = rb_hash_new();
822
+ int n = upb_MessageDef_FieldCount(m);
823
+ bool is_proto2;
824
+
825
+ // We currently have a few behaviors that are specific to proto2.
826
+ // This is unfortunate, we should key behaviors off field attributes (like
827
+ // whether a field has presence), not proto2 vs. proto3. We should see if we
828
+ // can change this without breaking users.
829
+ is_proto2 = upb_MessageDef_Syntax(m) == kUpb_Syntax_Proto2;
830
+
831
+ for (int i = 0; i < n; i++) {
832
+ const upb_FieldDef* field = upb_MessageDef_Field(m, i);
833
+ TypeInfo type_info = TypeInfo_get(field);
834
+ upb_MessageValue msgval;
835
+ VALUE msg_value;
836
+ VALUE msg_key;
837
+
838
+ if (!is_proto2 && upb_FieldDef_IsSubMessage(field) &&
839
+ !upb_FieldDef_IsRepeated(field) &&
840
+ !upb_Message_HasFieldByDef(msg, field)) {
841
+ // TODO: Legacy behavior, remove when we fix the is_proto2 differences.
842
+ msg_key = ID2SYM(rb_intern(upb_FieldDef_Name(field)));
843
+ rb_hash_aset(hash, msg_key, Qnil);
844
+ continue;
845
+ }
846
+
847
+ // Do not include fields that are not present (oneof or optional fields).
848
+ if (is_proto2 && upb_FieldDef_HasPresence(field) &&
849
+ !upb_Message_HasFieldByDef(msg, field)) {
850
+ continue;
851
+ }
852
+
853
+ msg_key = ID2SYM(rb_intern(upb_FieldDef_Name(field)));
854
+ msgval = upb_Message_GetFieldByDef(msg, field);
855
+
856
+ // Proto2 omits empty map/repeated filds also.
857
+
858
+ if (upb_FieldDef_IsMap(field)) {
859
+ const upb_MessageDef* entry_m = upb_FieldDef_MessageSubDef(field);
860
+ const upb_FieldDef* key_f = upb_MessageDef_FindFieldByNumber(entry_m, 1);
861
+ const upb_FieldDef* val_f = upb_MessageDef_FindFieldByNumber(entry_m, 2);
862
+ upb_CType key_type = upb_FieldDef_CType(key_f);
863
+ msg_value = Map_CreateHash(msgval.map_val, key_type, TypeInfo_get(val_f));
864
+ } else if (upb_FieldDef_IsRepeated(field)) {
865
+ if (is_proto2 &&
866
+ (!msgval.array_val || upb_Array_Size(msgval.array_val) == 0)) {
867
+ continue;
868
+ }
869
+ msg_value = RepeatedField_CreateArray(msgval.array_val, type_info);
870
+ } else {
871
+ msg_value = Scalar_CreateHash(msgval, type_info);
872
+ }
873
+
874
+ rb_hash_aset(hash, msg_key, msg_value);
875
+ }
876
+
877
+ return hash;
878
+ }
879
+
880
+ VALUE Scalar_CreateHash(upb_MessageValue msgval, TypeInfo type_info) {
881
+ if (type_info.type == kUpb_CType_Message) {
882
+ return Message_CreateHash(msgval.msg_val, type_info.def.msgdef);
883
+ } else {
884
+ return Convert_UpbToRuby(msgval, type_info, Qnil);
885
+ }
886
+ }
887
+
888
+ /*
889
+ * call-seq:
890
+ * Message.to_h => {}
891
+ *
892
+ * Returns the message as a Ruby Hash object, with keys as symbols.
893
+ */
894
+ static VALUE Message_to_h(VALUE _self) {
895
+ Message* self = ruby_to_Message(_self);
896
+ return Message_CreateHash(self->msg, self->msgdef);
897
+ }
898
+
899
+ /*
900
+ * call-seq:
901
+ * Message.freeze => self
902
+ *
903
+ * Freezes the message object. We have to intercept this so we can pin the
904
+ * Ruby object into memory so we don't forget it's frozen.
905
+ */
906
+ static VALUE Message_freeze(VALUE _self) {
907
+ Message* self = ruby_to_Message(_self);
908
+ if (!RB_OBJ_FROZEN(_self)) {
909
+ Arena_Pin(self->arena, _self);
910
+ RB_OBJ_FREEZE(_self);
911
+ }
912
+ return _self;
913
+ }
914
+
915
+ /*
916
+ * call-seq:
917
+ * Message.[](index) => value
918
+ *
919
+ * Accesses a field's value by field name. The provided field name should be a
920
+ * string.
921
+ */
922
+ static VALUE Message_index(VALUE _self, VALUE field_name) {
923
+ Message* self = ruby_to_Message(_self);
924
+ const upb_FieldDef* field;
925
+
926
+ Check_Type(field_name, T_STRING);
927
+ field = upb_MessageDef_FindFieldByName(self->msgdef, RSTRING_PTR(field_name));
928
+
929
+ if (field == NULL) {
930
+ return Qnil;
931
+ }
932
+
933
+ return Message_getfield(_self, field);
934
+ }
935
+
936
+ /*
937
+ * call-seq:
938
+ * Message.[]=(index, value)
939
+ *
940
+ * Sets a field's value by field name. The provided field name should be a
941
+ * string.
942
+ */
943
+ static VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value) {
944
+ Message* self = ruby_to_Message(_self);
945
+ const upb_FieldDef* f;
946
+ upb_MessageValue val;
947
+ upb_Arena* arena = Arena_get(self->arena);
948
+
949
+ Check_Type(field_name, T_STRING);
950
+ f = upb_MessageDef_FindFieldByName(self->msgdef, RSTRING_PTR(field_name));
951
+
952
+ if (f == NULL) {
953
+ rb_raise(rb_eArgError, "Unknown field: %s", RSTRING_PTR(field_name));
954
+ }
955
+
956
+ val = Convert_RubyToUpb(value, upb_FieldDef_Name(f), TypeInfo_get(f), arena);
957
+ upb_Message_SetFieldByDef(Message_GetMutable(_self, NULL), f, val, arena);
958
+
959
+ return Qnil;
960
+ }
961
+
962
+ /*
963
+ * call-seq:
964
+ * MessageClass.decode(data, options) => message
965
+ *
966
+ * Decodes the given data (as a string containing bytes in protocol buffers wire
967
+ * format) under the interpretration given by this message class's definition
968
+ * and returns a message object with the corresponding field values.
969
+ * @param options [Hash] options for the decoder
970
+ * recursion_limit: set to maximum decoding depth for message (default is 64)
971
+ */
972
+ static VALUE Message_decode(int argc, VALUE* argv, VALUE klass) {
973
+ VALUE data = argv[0];
974
+ int options = 0;
975
+
976
+ if (argc < 1 || argc > 2) {
977
+ rb_raise(rb_eArgError, "Expected 1 or 2 arguments.");
978
+ }
979
+
980
+ if (argc == 2) {
981
+ VALUE hash_args = argv[1];
982
+ if (TYPE(hash_args) != T_HASH) {
983
+ rb_raise(rb_eArgError, "Expected hash arguments.");
984
+ }
985
+
986
+ VALUE depth =
987
+ rb_hash_lookup(hash_args, ID2SYM(rb_intern("recursion_limit")));
988
+
989
+ if (depth != Qnil && TYPE(depth) == T_FIXNUM) {
990
+ options |= UPB_DECODE_MAXDEPTH(FIX2INT(depth));
991
+ }
992
+ }
993
+
994
+ if (TYPE(data) != T_STRING) {
995
+ rb_raise(rb_eArgError, "Expected string for binary protobuf data.");
996
+ }
997
+
998
+ VALUE msg_rb = initialize_rb_class_with_no_args(klass);
999
+ Message* msg = ruby_to_Message(msg_rb);
1000
+
1001
+ upb_DecodeStatus status =
1002
+ upb_Decode(RSTRING_PTR(data), RSTRING_LEN(data), (upb_Message*)msg->msg,
1003
+ upb_MessageDef_MiniTable(msg->msgdef), NULL, options,
1004
+ Arena_get(msg->arena));
1005
+
1006
+ if (status != kUpb_DecodeStatus_Ok) {
1007
+ rb_raise(cParseError, "Error occurred during parsing");
1008
+ }
1009
+
1010
+ return msg_rb;
1011
+ }
1012
+
1013
+ /*
1014
+ * call-seq:
1015
+ * MessageClass.decode_json(data, options = {}) => message
1016
+ *
1017
+ * Decodes the given data (as a string containing bytes in protocol buffers wire
1018
+ * format) under the interpretration given by this message class's definition
1019
+ * and returns a message object with the corresponding field values.
1020
+ *
1021
+ * @param options [Hash] options for the decoder
1022
+ * ignore_unknown_fields: set true to ignore unknown fields (default is to
1023
+ * raise an error)
1024
+ */
1025
+ static VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass) {
1026
+ VALUE data = argv[0];
1027
+ int options = 0;
1028
+ upb_Status status;
1029
+
1030
+ // TODO(haberman): use this message's pool instead.
1031
+ const upb_DefPool* symtab = DescriptorPool_GetSymtab(generated_pool);
1032
+
1033
+ if (argc < 1 || argc > 2) {
1034
+ rb_raise(rb_eArgError, "Expected 1 or 2 arguments.");
1035
+ }
1036
+
1037
+ if (argc == 2) {
1038
+ VALUE hash_args = argv[1];
1039
+ if (TYPE(hash_args) != T_HASH) {
1040
+ rb_raise(rb_eArgError, "Expected hash arguments.");
1041
+ }
1042
+
1043
+ if (RTEST(rb_hash_lookup2(
1044
+ hash_args, ID2SYM(rb_intern("ignore_unknown_fields")), Qfalse))) {
1045
+ options |= upb_JsonDecode_IgnoreUnknown;
1046
+ }
1047
+ }
1048
+
1049
+ if (TYPE(data) != T_STRING) {
1050
+ rb_raise(rb_eArgError, "Expected string for JSON data.");
1051
+ }
1052
+
1053
+ // TODO(cfallin): Check and respect string encoding. If not UTF-8, we need to
1054
+ // convert, because string handlers pass data directly to message string
1055
+ // fields.
1056
+
1057
+ VALUE msg_rb = initialize_rb_class_with_no_args(klass);
1058
+ Message* msg = ruby_to_Message(msg_rb);
1059
+
1060
+ // We don't allow users to decode a wrapper type directly.
1061
+ if (IsWrapper(msg->msgdef)) {
1062
+ rb_raise(rb_eRuntimeError, "Cannot parse a wrapper directly.");
1063
+ }
1064
+
1065
+ upb_Status_Clear(&status);
1066
+ if (!upb_JsonDecode(RSTRING_PTR(data), RSTRING_LEN(data),
1067
+ (upb_Message*)msg->msg, msg->msgdef, symtab, options,
1068
+ Arena_get(msg->arena), &status)) {
1069
+ rb_raise(cParseError, "Error occurred during parsing: %s",
1070
+ upb_Status_ErrorMessage(&status));
1071
+ }
1072
+
1073
+ return msg_rb;
1074
+ }
1075
+
1076
+ /*
1077
+ * call-seq:
1078
+ * MessageClass.encode(msg, options) => bytes
1079
+ *
1080
+ * Encodes the given message object to its serialized form in protocol buffers
1081
+ * wire format.
1082
+ * @param options [Hash] options for the encoder
1083
+ * recursion_limit: set to maximum encoding depth for message (default is 64)
1084
+ */
1085
+ static VALUE Message_encode(int argc, VALUE* argv, VALUE klass) {
1086
+ Message* msg = ruby_to_Message(argv[0]);
1087
+ int options = 0;
1088
+ char* data;
1089
+ size_t size;
1090
+
1091
+ if (CLASS_OF(argv[0]) != klass) {
1092
+ rb_raise(rb_eArgError, "Message of wrong type.");
1093
+ }
1094
+
1095
+ if (argc < 1 || argc > 2) {
1096
+ rb_raise(rb_eArgError, "Expected 1 or 2 arguments.");
1097
+ }
1098
+
1099
+ if (argc == 2) {
1100
+ VALUE hash_args = argv[1];
1101
+ if (TYPE(hash_args) != T_HASH) {
1102
+ rb_raise(rb_eArgError, "Expected hash arguments.");
1103
+ }
1104
+ VALUE depth =
1105
+ rb_hash_lookup(hash_args, ID2SYM(rb_intern("recursion_limit")));
1106
+
1107
+ if (depth != Qnil && TYPE(depth) == T_FIXNUM) {
1108
+ options |= UPB_DECODE_MAXDEPTH(FIX2INT(depth));
1109
+ }
1110
+ }
1111
+
1112
+ upb_Arena* arena = upb_Arena_New();
1113
+
1114
+ upb_EncodeStatus status =
1115
+ upb_Encode(msg->msg, upb_MessageDef_MiniTable(msg->msgdef), options,
1116
+ arena, &data, &size);
1117
+
1118
+ if (status == kUpb_EncodeStatus_Ok) {
1119
+ VALUE ret = rb_str_new(data, size);
1120
+ rb_enc_associate(ret, rb_ascii8bit_encoding());
1121
+ upb_Arena_Free(arena);
1122
+ return ret;
1123
+ } else {
1124
+ upb_Arena_Free(arena);
1125
+ rb_raise(rb_eRuntimeError, "Exceeded maximum depth (possibly cycle)");
1126
+ }
1127
+ }
1128
+
1129
+ /*
1130
+ * call-seq:
1131
+ * MessageClass.encode_json(msg, options = {}) => json_string
1132
+ *
1133
+ * Encodes the given message object into its serialized JSON representation.
1134
+ * @param options [Hash] options for the decoder
1135
+ * preserve_proto_fieldnames: set true to use original fieldnames (default is
1136
+ * to camelCase) emit_defaults: set true to emit 0/false values (default is to
1137
+ * omit them)
1138
+ */
1139
+ static VALUE Message_encode_json(int argc, VALUE* argv, VALUE klass) {
1140
+ Message* msg = ruby_to_Message(argv[0]);
1141
+ int options = upb_JsonEncode_FormatEnumsAsIntegers;
1142
+ char buf[1024];
1143
+ size_t size;
1144
+ upb_Status status;
1145
+
1146
+ // TODO(haberman): use this message's pool instead.
1147
+ const upb_DefPool* symtab = DescriptorPool_GetSymtab(generated_pool);
1148
+
1149
+ if (argc < 1 || argc > 2) {
1150
+ rb_raise(rb_eArgError, "Expected 1 or 2 arguments.");
1151
+ }
1152
+
1153
+ if (argc == 2) {
1154
+ VALUE hash_args = argv[1];
1155
+ if (TYPE(hash_args) != T_HASH) {
1156
+ if (RTEST(rb_funcall(hash_args, rb_intern("respond_to?"), 1,
1157
+ rb_str_new2("to_h")))) {
1158
+ hash_args = rb_funcall(hash_args, rb_intern("to_h"), 0);
1159
+ } else {
1160
+ rb_raise(rb_eArgError, "Expected hash arguments.");
1161
+ }
1162
+ }
1163
+
1164
+ if (RTEST(rb_hash_lookup2(hash_args,
1165
+ ID2SYM(rb_intern("preserve_proto_fieldnames")),
1166
+ Qfalse))) {
1167
+ options |= upb_JsonEncode_UseProtoNames;
1168
+ }
1169
+
1170
+ if (RTEST(rb_hash_lookup2(hash_args, ID2SYM(rb_intern("emit_defaults")),
1171
+ Qfalse))) {
1172
+ options |= upb_JsonEncode_EmitDefaults;
1173
+ }
1174
+ }
1175
+
1176
+ upb_Status_Clear(&status);
1177
+ size = upb_JsonEncode(msg->msg, msg->msgdef, symtab, options, buf,
1178
+ sizeof(buf), &status);
1179
+
1180
+ if (!upb_Status_IsOk(&status)) {
1181
+ rb_raise(cParseError, "Error occurred during encoding: %s",
1182
+ upb_Status_ErrorMessage(&status));
1183
+ }
1184
+
1185
+ VALUE ret;
1186
+ if (size >= sizeof(buf)) {
1187
+ char* buf2 = malloc(size + 1);
1188
+ upb_JsonEncode(msg->msg, msg->msgdef, symtab, options, buf2, size + 1,
1189
+ &status);
1190
+ ret = rb_str_new(buf2, size);
1191
+ free(buf2);
1192
+ } else {
1193
+ ret = rb_str_new(buf, size);
1194
+ }
1195
+
1196
+ rb_enc_associate(ret, rb_utf8_encoding());
1197
+ return ret;
1198
+ }
1199
+
1200
+ /*
1201
+ * call-seq:
1202
+ * Message.descriptor => descriptor
1203
+ *
1204
+ * Class method that returns the Descriptor instance corresponding to this
1205
+ * message class's type.
1206
+ */
1207
+ static VALUE Message_descriptor(VALUE klass) {
1208
+ return rb_ivar_get(klass, descriptor_instancevar_interned);
1209
+ }
1210
+
1211
+ VALUE build_class_from_descriptor(VALUE descriptor) {
1212
+ const char* name;
1213
+ VALUE klass;
1214
+
1215
+ name = upb_MessageDef_FullName(Descriptor_GetMsgDef(descriptor));
1216
+ if (name == NULL) {
1217
+ rb_raise(rb_eRuntimeError, "Descriptor does not have assigned name.");
1218
+ }
1219
+
1220
+ klass = rb_define_class_id(
1221
+ // Docs say this parameter is ignored. User will assign return value to
1222
+ // their own toplevel constant class name.
1223
+ rb_intern("Message"), cAbstractMessage);
1224
+ rb_ivar_set(klass, descriptor_instancevar_interned, descriptor);
1225
+ return klass;
1226
+ }
1227
+
1228
+ /*
1229
+ * call-seq:
1230
+ * Enum.lookup(number) => name
1231
+ *
1232
+ * This module method, provided on each generated enum module, looks up an enum
1233
+ * value by number and returns its name as a Ruby symbol, or nil if not found.
1234
+ */
1235
+ static VALUE enum_lookup(VALUE self, VALUE number) {
1236
+ int32_t num = NUM2INT(number);
1237
+ VALUE desc = rb_ivar_get(self, descriptor_instancevar_interned);
1238
+ const upb_EnumDef* e = EnumDescriptor_GetEnumDef(desc);
1239
+ const upb_EnumValueDef* ev = upb_EnumDef_FindValueByNumber(e, num);
1240
+ if (ev) {
1241
+ return ID2SYM(rb_intern(upb_EnumValueDef_Name(ev)));
1242
+ } else {
1243
+ return Qnil;
1244
+ }
1245
+ }
1246
+
1247
+ /*
1248
+ * call-seq:
1249
+ * Enum.resolve(name) => number
1250
+ *
1251
+ * This module method, provided on each generated enum module, looks up an enum
1252
+ * value by name (as a Ruby symbol) and returns its name, or nil if not found.
1253
+ */
1254
+ static VALUE enum_resolve(VALUE self, VALUE sym) {
1255
+ const char* name = rb_id2name(SYM2ID(sym));
1256
+ VALUE desc = rb_ivar_get(self, descriptor_instancevar_interned);
1257
+ const upb_EnumDef* e = EnumDescriptor_GetEnumDef(desc);
1258
+ const upb_EnumValueDef* ev = upb_EnumDef_FindValueByName(e, name);
1259
+ if (ev) {
1260
+ return INT2NUM(upb_EnumValueDef_Number(ev));
1261
+ } else {
1262
+ return Qnil;
1263
+ }
1264
+ }
1265
+
1266
+ /*
1267
+ * call-seq:
1268
+ * Enum.descriptor
1269
+ *
1270
+ * This module method, provided on each generated enum module, returns the
1271
+ * EnumDescriptor corresponding to this enum type.
1272
+ */
1273
+ static VALUE enum_descriptor(VALUE self) {
1274
+ return rb_ivar_get(self, descriptor_instancevar_interned);
1275
+ }
1276
+
1277
+ VALUE build_module_from_enumdesc(VALUE _enumdesc) {
1278
+ const upb_EnumDef* e = EnumDescriptor_GetEnumDef(_enumdesc);
1279
+ VALUE mod = rb_define_module_id(rb_intern(upb_EnumDef_FullName(e)));
1280
+
1281
+ int n = upb_EnumDef_ValueCount(e);
1282
+ for (int i = 0; i < n; i++) {
1283
+ const upb_EnumValueDef* ev = upb_EnumDef_Value(e, i);
1284
+ upb_Arena* arena = upb_Arena_New();
1285
+ const char* src_name = upb_EnumValueDef_Name(ev);
1286
+ char* name = upb_strdup2(src_name, strlen(src_name), arena);
1287
+ int32_t value = upb_EnumValueDef_Number(ev);
1288
+ if (name[0] < 'A' || name[0] > 'Z') {
1289
+ if (name[0] >= 'a' && name[0] <= 'z') {
1290
+ name[0] -= 32; // auto capitalize
1291
+ } else {
1292
+ rb_warn(
1293
+ "Enum value '%s' does not start with an uppercase letter "
1294
+ "as is required for Ruby constants.",
1295
+ name);
1296
+ }
1297
+ }
1298
+ rb_define_const(mod, name, INT2NUM(value));
1299
+ upb_Arena_Free(arena);
1300
+ }
1301
+
1302
+ rb_define_singleton_method(mod, "lookup", enum_lookup, 1);
1303
+ rb_define_singleton_method(mod, "resolve", enum_resolve, 1);
1304
+ rb_define_singleton_method(mod, "descriptor", enum_descriptor, 0);
1305
+ rb_ivar_set(mod, descriptor_instancevar_interned, _enumdesc);
1306
+
1307
+ return mod;
1308
+ }
1309
+
1310
+ // Internal to the library; used by Google::Protobuf.deep_copy.
1311
+ upb_Message* Message_deep_copy(const upb_Message* msg, const upb_MessageDef* m,
1312
+ upb_Arena* arena) {
1313
+ // Serialize and parse.
1314
+ upb_Arena* tmp_arena = upb_Arena_New();
1315
+ const upb_MiniTable* layout = upb_MessageDef_MiniTable(m);
1316
+ size_t size;
1317
+
1318
+ upb_Message* new_msg = upb_Message_New(layout, arena);
1319
+ char* data;
1320
+
1321
+ if (upb_Encode(msg, layout, 0, tmp_arena, &data, &size) !=
1322
+ kUpb_EncodeStatus_Ok ||
1323
+ upb_Decode(data, size, new_msg, layout, NULL, 0, arena) !=
1324
+ kUpb_DecodeStatus_Ok) {
1325
+ upb_Arena_Free(tmp_arena);
1326
+ rb_raise(cParseError, "Error occurred copying proto");
1327
+ }
1328
+
1329
+ upb_Arena_Free(tmp_arena);
1330
+ return new_msg;
1331
+ }
1332
+
1333
+ const upb_Message* Message_GetUpbMessage(VALUE value, const upb_MessageDef* m,
1334
+ const char* name, upb_Arena* arena) {
1335
+ if (value == Qnil) {
1336
+ rb_raise(cTypeError, "nil message not allowed here.");
1337
+ }
1338
+
1339
+ VALUE klass = CLASS_OF(value);
1340
+ VALUE desc_rb = rb_ivar_get(klass, descriptor_instancevar_interned);
1341
+ const upb_MessageDef* val_m =
1342
+ desc_rb == Qnil ? NULL : Descriptor_GetMsgDef(desc_rb);
1343
+
1344
+ if (val_m != m) {
1345
+ // Check for possible implicit conversions
1346
+ // TODO: hash conversion?
1347
+
1348
+ switch (upb_MessageDef_WellKnownType(m)) {
1349
+ case kUpb_WellKnown_Timestamp: {
1350
+ // Time -> Google::Protobuf::Timestamp
1351
+ const upb_MiniTable* t = upb_MessageDef_MiniTable(m);
1352
+ upb_Message* msg = upb_Message_New(t, arena);
1353
+ upb_MessageValue sec, nsec;
1354
+ struct timespec time;
1355
+ const upb_FieldDef* sec_f = upb_MessageDef_FindFieldByNumber(m, 1);
1356
+ const upb_FieldDef* nsec_f = upb_MessageDef_FindFieldByNumber(m, 2);
1357
+
1358
+ if (!rb_obj_is_kind_of(value, rb_cTime)) goto badtype;
1359
+
1360
+ time = rb_time_timespec(value);
1361
+ sec.int64_val = time.tv_sec;
1362
+ nsec.int32_val = time.tv_nsec;
1363
+ upb_Message_SetFieldByDef(msg, sec_f, sec, arena);
1364
+ upb_Message_SetFieldByDef(msg, nsec_f, nsec, arena);
1365
+ return msg;
1366
+ }
1367
+ case kUpb_WellKnown_Duration: {
1368
+ // Numeric -> Google::Protobuf::Duration
1369
+ const upb_MiniTable* t = upb_MessageDef_MiniTable(m);
1370
+ upb_Message* msg = upb_Message_New(t, arena);
1371
+ upb_MessageValue sec, nsec;
1372
+ const upb_FieldDef* sec_f = upb_MessageDef_FindFieldByNumber(m, 1);
1373
+ const upb_FieldDef* nsec_f = upb_MessageDef_FindFieldByNumber(m, 2);
1374
+
1375
+ if (!rb_obj_is_kind_of(value, rb_cNumeric)) goto badtype;
1376
+
1377
+ sec.int64_val = NUM2LL(value);
1378
+ nsec.int32_val = round((NUM2DBL(value) - NUM2LL(value)) * 1000000000);
1379
+ upb_Message_SetFieldByDef(msg, sec_f, sec, arena);
1380
+ upb_Message_SetFieldByDef(msg, nsec_f, nsec, arena);
1381
+ return msg;
1382
+ }
1383
+ default:
1384
+ badtype:
1385
+ rb_raise(cTypeError,
1386
+ "Invalid type %s to assign to submessage field '%s'.",
1387
+ rb_class2name(CLASS_OF(value)), name);
1388
+ }
1389
+ }
1390
+
1391
+ Message* self = ruby_to_Message(value);
1392
+ Arena_fuse(self->arena, arena);
1393
+
1394
+ return self->msg;
1395
+ }
1396
+
1397
+ static void Message_define_class(VALUE klass) {
1398
+ rb_define_alloc_func(klass, Message_alloc);
1399
+
1400
+ rb_require("google/protobuf/message_exts");
1401
+ rb_define_method(klass, "method_missing", Message_method_missing, -1);
1402
+ rb_define_method(klass, "respond_to_missing?", Message_respond_to_missing,
1403
+ -1);
1404
+ rb_define_method(klass, "initialize", Message_initialize, -1);
1405
+ rb_define_method(klass, "dup", Message_dup, 0);
1406
+ // Also define #clone so that we don't inherit Object#clone.
1407
+ rb_define_method(klass, "clone", Message_dup, 0);
1408
+ rb_define_method(klass, "==", Message_eq, 1);
1409
+ rb_define_method(klass, "eql?", Message_eq, 1);
1410
+ rb_define_method(klass, "freeze", Message_freeze, 0);
1411
+ rb_define_method(klass, "hash", Message_hash, 0);
1412
+ rb_define_method(klass, "to_h", Message_to_h, 0);
1413
+ rb_define_method(klass, "inspect", Message_inspect, 0);
1414
+ rb_define_method(klass, "to_s", Message_inspect, 0);
1415
+ rb_define_method(klass, "[]", Message_index, 1);
1416
+ rb_define_method(klass, "[]=", Message_index_set, 2);
1417
+ rb_define_singleton_method(klass, "decode", Message_decode, -1);
1418
+ rb_define_singleton_method(klass, "encode", Message_encode, -1);
1419
+ rb_define_singleton_method(klass, "decode_json", Message_decode_json, -1);
1420
+ rb_define_singleton_method(klass, "encode_json", Message_encode_json, -1);
1421
+ rb_define_singleton_method(klass, "descriptor", Message_descriptor, 0);
1422
+ }
1423
+
1424
+ void Message_register(VALUE protobuf) {
1425
+ cParseError = rb_const_get(protobuf, rb_intern("ParseError"));
1426
+ cAbstractMessage =
1427
+ rb_define_class_under(protobuf, "AbstractMessage", rb_cObject);
1428
+ Message_define_class(cAbstractMessage);
1429
+ rb_gc_register_address(&cAbstractMessage);
1430
+
1431
+ // Ruby-interned string: "descriptor". We use this identifier to store an
1432
+ // instance variable on message classes we create in order to link them back
1433
+ // to their descriptors.
1434
+ descriptor_instancevar_interned = rb_intern("@descriptor");
1435
+ }