google-protobuf 3.24.4-java → 3.25.0-java

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