google-protobuf 3.11.1 → 3.25.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/ext/google/protobuf_c/Rakefile +3 -0
  3. data/ext/google/protobuf_c/convert.c +314 -0
  4. data/ext/google/protobuf_c/convert.h +50 -0
  5. data/ext/google/protobuf_c/defs.c +745 -1620
  6. data/ext/google/protobuf_c/defs.h +82 -0
  7. data/ext/google/protobuf_c/extconf.rb +15 -8
  8. data/ext/google/protobuf_c/glue.c +56 -0
  9. data/ext/google/protobuf_c/map.c +336 -512
  10. data/ext/google/protobuf_c/map.h +44 -0
  11. data/ext/google/protobuf_c/message.c +1096 -520
  12. data/ext/google/protobuf_c/message.h +86 -0
  13. data/ext/google/protobuf_c/protobuf.c +301 -94
  14. data/ext/google/protobuf_c/protobuf.h +66 -620
  15. data/ext/google/protobuf_c/repeated_field.c +323 -353
  16. data/ext/google/protobuf_c/repeated_field.h +41 -0
  17. data/ext/google/protobuf_c/ruby-upb.c +14414 -0
  18. data/ext/google/protobuf_c/ruby-upb.h +13044 -0
  19. data/ext/google/protobuf_c/shared_convert.c +64 -0
  20. data/ext/google/protobuf_c/shared_convert.h +26 -0
  21. data/ext/google/protobuf_c/shared_message.c +65 -0
  22. data/ext/google/protobuf_c/shared_message.h +25 -0
  23. data/ext/google/protobuf_c/third_party/utf8_range/LICENSE +22 -0
  24. data/ext/google/protobuf_c/third_party/utf8_range/naive.c +92 -0
  25. data/ext/google/protobuf_c/third_party/utf8_range/range2-neon.c +157 -0
  26. data/ext/google/protobuf_c/third_party/utf8_range/range2-sse.c +170 -0
  27. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.h +21 -0
  28. data/ext/google/protobuf_c/wrap_memcpy.c +7 -29
  29. data/lib/google/protobuf/any_pb.rb +24 -5
  30. data/lib/google/protobuf/api_pb.rb +27 -23
  31. data/lib/google/protobuf/descriptor_dsl.rb +465 -0
  32. data/lib/google/protobuf/descriptor_pb.rb +86 -0
  33. data/lib/google/protobuf/duration_pb.rb +24 -5
  34. data/lib/google/protobuf/empty_pb.rb +24 -3
  35. data/lib/google/protobuf/ffi/descriptor.rb +165 -0
  36. data/lib/google/protobuf/ffi/descriptor_pool.rb +75 -0
  37. data/lib/google/protobuf/ffi/enum_descriptor.rb +171 -0
  38. data/lib/google/protobuf/ffi/ffi.rb +213 -0
  39. data/lib/google/protobuf/ffi/field_descriptor.rb +319 -0
  40. data/lib/google/protobuf/ffi/file_descriptor.rb +59 -0
  41. data/lib/google/protobuf/ffi/internal/arena.rb +66 -0
  42. data/lib/google/protobuf/ffi/internal/convert.rb +305 -0
  43. data/lib/google/protobuf/ffi/internal/pointer_helper.rb +35 -0
  44. data/lib/google/protobuf/ffi/internal/type_safety.rb +25 -0
  45. data/lib/google/protobuf/ffi/map.rb +407 -0
  46. data/lib/google/protobuf/ffi/message.rb +662 -0
  47. data/lib/google/protobuf/ffi/object_cache.rb +30 -0
  48. data/lib/google/protobuf/ffi/oneof_descriptor.rb +95 -0
  49. data/lib/google/protobuf/ffi/repeated_field.rb +383 -0
  50. data/lib/google/protobuf/field_mask_pb.rb +24 -4
  51. data/lib/google/protobuf/message_exts.rb +10 -28
  52. data/lib/google/protobuf/object_cache.rb +97 -0
  53. data/lib/google/protobuf/plugin_pb.rb +47 -0
  54. data/lib/google/protobuf/repeated_field.rb +18 -28
  55. data/lib/google/protobuf/source_context_pb.rb +24 -4
  56. data/lib/google/protobuf/struct_pb.rb +24 -20
  57. data/lib/google/protobuf/timestamp_pb.rb +24 -5
  58. data/lib/google/protobuf/type_pb.rb +27 -68
  59. data/lib/google/protobuf/well_known_types.rb +17 -36
  60. data/lib/google/protobuf/wrappers_pb.rb +24 -28
  61. data/lib/google/protobuf.rb +32 -118
  62. data/lib/google/protobuf_ffi.rb +50 -0
  63. data/lib/google/protobuf_native.rb +20 -0
  64. data/lib/google/tasks/ffi.rake +102 -0
  65. metadata +92 -32
  66. data/ext/google/protobuf_c/encode_decode.c +0 -1758
  67. data/ext/google/protobuf_c/storage.c +0 -1149
  68. data/ext/google/protobuf_c/upb.c +0 -13289
  69. data/ext/google/protobuf_c/upb.h +0 -7085
  70. data/tests/basic.rb +0 -445
  71. data/tests/generated_code_test.rb +0 -23
  72. data/tests/stress.rb +0 -38
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ccdfc3e22293ef0bc4f5b3e1d13453c49f3db15ac6b781c7f2443f5d2a55dda4
4
- data.tar.gz: b004de271b4f57589c5a847bd5aeb772fd6d383e3b2ffa27e7c53d46623325ef
3
+ metadata.gz: 634d898bbe3be4cc53cdd85c2bb7774be145905c391514519b32ce409f1781d9
4
+ data.tar.gz: ee14834c2b11e87c437dbe005ac5518505770324b2202da40a732eaa2c9a5ff6
5
5
  SHA512:
6
- metadata.gz: a303ec68579ff29392be8a4b2cc6569aaa2873b7be493be1428e8708b5e6b45a2a53da4ed91a64d47f838b623131cc8e81ce9c9568183313df273fedaf6e462b
7
- data.tar.gz: 7088cec472bd5d998f5e37f2e326c4027114445e59e30cc7868ec3764394789b60ea6eaffdc3f4014a7a64eb2c685ae5e996a7066f3b790cbe6d649c63ca4902
6
+ metadata.gz: 1baa4d20ce995a96ba0916a7d4ed0847e25645062d7b3e9ba655aae55f0224f9d7b6355b7a5f5d52a8f5100879cd46a21c077a5e4521ca3fd42f2751184a21e9
7
+ data.tar.gz: 2dc9e891af8d7c553476b9cfa29525746067b8200bf7c8f221da461376446b2c6a62050223d5e9b9f6a6af488c75d3b536fb3f40125069cc50851b06a1a8f748
@@ -0,0 +1,3 @@
1
+ import '../../../lib/google/tasks/ffi.rake'
2
+
3
+ task default: ['ffi-protobuf:default']
@@ -0,0 +1,314 @@
1
+ // Protocol Buffers - Google's data interchange format
2
+ // Copyright 2008 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
+ // -----------------------------------------------------------------------------
9
+ // Ruby <-> upb data conversion functions.
10
+ //
11
+ // This file Also contains a few other assorted algorithms on upb_MessageValue.
12
+ //
13
+ // None of the algorithms in this file require any access to the internal
14
+ // representation of Ruby or upb objects.
15
+ // -----------------------------------------------------------------------------
16
+
17
+ #include "convert.h"
18
+
19
+ #include "message.h"
20
+ #include "protobuf.h"
21
+ #include "shared_convert.h"
22
+
23
+ static upb_StringView Convert_StringData(VALUE str, upb_Arena* arena) {
24
+ upb_StringView ret;
25
+ if (arena) {
26
+ char* ptr = upb_Arena_Malloc(arena, RSTRING_LEN(str));
27
+ memcpy(ptr, RSTRING_PTR(str), RSTRING_LEN(str));
28
+ ret.data = ptr;
29
+ } else {
30
+ // Data is only needed temporarily (within map lookup).
31
+ ret.data = RSTRING_PTR(str);
32
+ }
33
+ ret.size = RSTRING_LEN(str);
34
+ return ret;
35
+ }
36
+
37
+ static bool is_ruby_num(VALUE value) {
38
+ return (TYPE(value) == T_FLOAT || TYPE(value) == T_FIXNUM ||
39
+ TYPE(value) == T_BIGNUM);
40
+ }
41
+
42
+ static void Convert_CheckInt(const char* name, upb_CType type, VALUE val) {
43
+ if (!is_ruby_num(val)) {
44
+ rb_raise(cTypeError,
45
+ "Expected number type for integral field '%s' (given %s).", name,
46
+ rb_class2name(CLASS_OF(val)));
47
+ }
48
+
49
+ // NUM2{INT,UINT,LL,ULL} macros do the appropriate range checks on upper
50
+ // bound; we just need to do precision checks (i.e., disallow rounding) and
51
+ // check for < 0 on unsigned types.
52
+ if (TYPE(val) == T_FLOAT) {
53
+ double dbl_val = NUM2DBL(val);
54
+ if (floor(dbl_val) != dbl_val) {
55
+ rb_raise(rb_eRangeError,
56
+ "Non-integral floating point value assigned to integer field "
57
+ "'%s' (given %s).",
58
+ name, rb_class2name(CLASS_OF(val)));
59
+ }
60
+ }
61
+ if (type == kUpb_CType_UInt32 || type == kUpb_CType_UInt64) {
62
+ if (NUM2DBL(val) < 0) {
63
+ rb_raise(
64
+ rb_eRangeError,
65
+ "Assigning negative value to unsigned integer field '%s' (given %s).",
66
+ name, rb_class2name(CLASS_OF(val)));
67
+ }
68
+ }
69
+ }
70
+
71
+ static int32_t Convert_ToEnum(VALUE value, const char* name,
72
+ const upb_EnumDef* e) {
73
+ int32_t val;
74
+
75
+ switch (TYPE(value)) {
76
+ case T_FLOAT:
77
+ case T_FIXNUM:
78
+ case T_BIGNUM:
79
+ Convert_CheckInt(name, kUpb_CType_Int32, value);
80
+ val = NUM2INT(value);
81
+ break;
82
+ case T_STRING: {
83
+ const upb_EnumValueDef* ev = upb_EnumDef_FindValueByNameWithSize(
84
+ e, RSTRING_PTR(value), RSTRING_LEN(value));
85
+ if (!ev) goto unknownval;
86
+ val = upb_EnumValueDef_Number(ev);
87
+ break;
88
+ }
89
+ case T_SYMBOL: {
90
+ const upb_EnumValueDef* ev =
91
+ upb_EnumDef_FindValueByName(e, rb_id2name(SYM2ID(value)));
92
+ if (!ev) goto unknownval;
93
+ val = upb_EnumValueDef_Number(ev);
94
+ break;
95
+ }
96
+ default:
97
+ rb_raise(cTypeError,
98
+ "Expected number or symbol type for enum field '%s'.", name);
99
+ }
100
+
101
+ return val;
102
+
103
+ unknownval:
104
+ rb_raise(rb_eRangeError, "Unknown symbol value for enum field '%s'.", name);
105
+ }
106
+
107
+ upb_MessageValue Convert_RubyToUpb(VALUE value, const char* name,
108
+ TypeInfo type_info, upb_Arena* arena) {
109
+ upb_MessageValue ret;
110
+
111
+ switch (type_info.type) {
112
+ case kUpb_CType_Float:
113
+ if (!is_ruby_num(value)) {
114
+ rb_raise(cTypeError,
115
+ "Expected number type for float field '%s' (given %s).", name,
116
+ rb_class2name(CLASS_OF(value)));
117
+ }
118
+ ret.float_val = NUM2DBL(value);
119
+ break;
120
+ case kUpb_CType_Double:
121
+ if (!is_ruby_num(value)) {
122
+ rb_raise(cTypeError,
123
+ "Expected number type for double field '%s' (given %s).", name,
124
+ rb_class2name(CLASS_OF(value)));
125
+ }
126
+ ret.double_val = NUM2DBL(value);
127
+ break;
128
+ case kUpb_CType_Bool: {
129
+ if (value == Qtrue) {
130
+ ret.bool_val = 1;
131
+ } else if (value == Qfalse) {
132
+ ret.bool_val = 0;
133
+ } else {
134
+ rb_raise(cTypeError,
135
+ "Invalid argument for boolean field '%s' (given %s).", name,
136
+ rb_class2name(CLASS_OF(value)));
137
+ }
138
+ break;
139
+ }
140
+ case kUpb_CType_String: {
141
+ VALUE utf8 = rb_enc_from_encoding(rb_utf8_encoding());
142
+ if (rb_obj_class(value) == rb_cSymbol) {
143
+ value = rb_funcall(value, rb_intern("to_s"), 0);
144
+ } else if (rb_obj_class(value) != rb_cString) {
145
+ rb_raise(cTypeError,
146
+ "Invalid argument for string field '%s' (given %s).", name,
147
+ rb_class2name(CLASS_OF(value)));
148
+ }
149
+
150
+ if (rb_obj_encoding(value) != utf8) {
151
+ // Note: this will not duplicate underlying string data unless
152
+ // necessary.
153
+ value = rb_str_encode(value, utf8, 0, Qnil);
154
+
155
+ if (rb_enc_str_coderange(value) == ENC_CODERANGE_BROKEN) {
156
+ rb_raise(rb_eEncodingError, "String is invalid UTF-8");
157
+ }
158
+ }
159
+
160
+ ret.str_val = Convert_StringData(value, arena);
161
+ break;
162
+ }
163
+ case kUpb_CType_Bytes: {
164
+ VALUE bytes = rb_enc_from_encoding(rb_ascii8bit_encoding());
165
+ if (rb_obj_class(value) != rb_cString) {
166
+ rb_raise(cTypeError,
167
+ "Invalid argument for bytes field '%s' (given %s).", name,
168
+ rb_class2name(CLASS_OF(value)));
169
+ }
170
+
171
+ if (rb_obj_encoding(value) != bytes) {
172
+ // Note: this will not duplicate underlying string data unless
173
+ // necessary.
174
+ // TODO: is this really necessary to get raw bytes?
175
+ value = rb_str_encode(value, bytes, 0, Qnil);
176
+ }
177
+
178
+ ret.str_val = Convert_StringData(value, arena);
179
+ break;
180
+ }
181
+ case kUpb_CType_Message:
182
+ ret.msg_val =
183
+ Message_GetUpbMessage(value, type_info.def.msgdef, name, arena);
184
+ break;
185
+ case kUpb_CType_Enum:
186
+ ret.int32_val = Convert_ToEnum(value, name, type_info.def.enumdef);
187
+ break;
188
+ case kUpb_CType_Int32:
189
+ case kUpb_CType_Int64:
190
+ case kUpb_CType_UInt32:
191
+ case kUpb_CType_UInt64:
192
+ Convert_CheckInt(name, type_info.type, value);
193
+ switch (type_info.type) {
194
+ case kUpb_CType_Int32:
195
+ ret.int32_val = NUM2INT(value);
196
+ break;
197
+ case kUpb_CType_Int64:
198
+ ret.int64_val = NUM2LL(value);
199
+ break;
200
+ case kUpb_CType_UInt32:
201
+ ret.uint32_val = NUM2UINT(value);
202
+ break;
203
+ case kUpb_CType_UInt64:
204
+ ret.uint64_val = NUM2ULL(value);
205
+ break;
206
+ default:
207
+ break;
208
+ }
209
+ break;
210
+ default:
211
+ break;
212
+ }
213
+
214
+ return ret;
215
+ }
216
+
217
+ VALUE Convert_UpbToRuby(upb_MessageValue upb_val, TypeInfo type_info,
218
+ VALUE arena) {
219
+ switch (type_info.type) {
220
+ case kUpb_CType_Float:
221
+ return DBL2NUM(upb_val.float_val);
222
+ case kUpb_CType_Double:
223
+ return DBL2NUM(upb_val.double_val);
224
+ case kUpb_CType_Bool:
225
+ return upb_val.bool_val ? Qtrue : Qfalse;
226
+ case kUpb_CType_Int32:
227
+ return INT2NUM(upb_val.int32_val);
228
+ case kUpb_CType_Int64:
229
+ return LL2NUM(upb_val.int64_val);
230
+ case kUpb_CType_UInt32:
231
+ return UINT2NUM(upb_val.uint32_val);
232
+ case kUpb_CType_UInt64:
233
+ return ULL2NUM(upb_val.int64_val);
234
+ case kUpb_CType_Enum: {
235
+ const upb_EnumValueDef* ev = upb_EnumDef_FindValueByNumber(
236
+ type_info.def.enumdef, upb_val.int32_val);
237
+ if (ev) {
238
+ return ID2SYM(rb_intern(upb_EnumValueDef_Name(ev)));
239
+ } else {
240
+ return INT2NUM(upb_val.int32_val);
241
+ }
242
+ }
243
+ case kUpb_CType_String: {
244
+ VALUE str_rb = rb_str_new(upb_val.str_val.data, upb_val.str_val.size);
245
+ rb_enc_associate(str_rb, rb_utf8_encoding());
246
+ rb_obj_freeze(str_rb);
247
+ return str_rb;
248
+ }
249
+ case kUpb_CType_Bytes: {
250
+ VALUE str_rb = rb_str_new(upb_val.str_val.data, upb_val.str_val.size);
251
+ rb_enc_associate(str_rb, rb_ascii8bit_encoding());
252
+ rb_obj_freeze(str_rb);
253
+ return str_rb;
254
+ }
255
+ case kUpb_CType_Message:
256
+ return Message_GetRubyWrapper((upb_Message*)upb_val.msg_val,
257
+ type_info.def.msgdef, arena);
258
+ default:
259
+ rb_raise(rb_eRuntimeError, "Convert_UpbToRuby(): Unexpected type %d",
260
+ (int)type_info.type);
261
+ }
262
+ }
263
+
264
+ upb_MessageValue Msgval_DeepCopy(upb_MessageValue msgval, TypeInfo type_info,
265
+ upb_Arena* arena) {
266
+ upb_MessageValue new_msgval;
267
+
268
+ switch (type_info.type) {
269
+ default:
270
+ memcpy(&new_msgval, &msgval, sizeof(msgval));
271
+ break;
272
+ case kUpb_CType_String:
273
+ case kUpb_CType_Bytes: {
274
+ size_t n = msgval.str_val.size;
275
+ char* mem = upb_Arena_Malloc(arena, n);
276
+ new_msgval.str_val.data = mem;
277
+ new_msgval.str_val.size = n;
278
+ memcpy(mem, msgval.str_val.data, n);
279
+ break;
280
+ }
281
+ case kUpb_CType_Message:
282
+ new_msgval.msg_val =
283
+ Message_deep_copy(msgval.msg_val, type_info.def.msgdef, arena);
284
+ break;
285
+ }
286
+
287
+ return new_msgval;
288
+ }
289
+
290
+ bool Msgval_IsEqual(upb_MessageValue val1, upb_MessageValue val2,
291
+ TypeInfo type_info) {
292
+ upb_Status status;
293
+ upb_Status_Clear(&status);
294
+ bool return_value = shared_Msgval_IsEqual(val1, val2, type_info.type,
295
+ type_info.def.msgdef, &status);
296
+ if (upb_Status_IsOk(&status)) {
297
+ return return_value;
298
+ } else {
299
+ rb_raise(rb_eRuntimeError, upb_Status_ErrorMessage(&status));
300
+ }
301
+ }
302
+
303
+ uint64_t Msgval_GetHash(upb_MessageValue val, TypeInfo type_info,
304
+ uint64_t seed) {
305
+ upb_Status status;
306
+ upb_Status_Clear(&status);
307
+ uint64_t return_value = shared_Msgval_GetHash(
308
+ val, type_info.type, type_info.def.msgdef, seed, &status);
309
+ if (upb_Status_IsOk(&status)) {
310
+ return return_value;
311
+ } else {
312
+ rb_raise(rb_eRuntimeError, upb_Status_ErrorMessage(&status));
313
+ }
314
+ }
@@ -0,0 +1,50 @@
1
+ // Protocol Buffers - Google's data interchange format
2
+ // Copyright 2008 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
+ #ifndef RUBY_PROTOBUF_CONVERT_H_
9
+ #define RUBY_PROTOBUF_CONVERT_H_
10
+
11
+ #include "protobuf.h"
12
+ #include "ruby-upb.h"
13
+
14
+ // Converts |ruby_val| to a upb_MessageValue according to |type_info|.
15
+ //
16
+ // The |arena| parameter indicates the lifetime of the container where this
17
+ // value will be assigned. It is used as follows:
18
+ // - If type is string or bytes, the string data will be copied into |arena|.
19
+ // - If type is message, and we need to auto-construct a message due to implicit
20
+ // conversions (eg. Time -> Google::Protobuf::Timestamp), the new message
21
+ // will be created in |arena|.
22
+ // - If type is message and the Ruby value is a message instance, we will fuse
23
+ // the message's arena into |arena|, to ensure that this message outlives the
24
+ // container.
25
+ upb_MessageValue Convert_RubyToUpb(VALUE ruby_val, const char *name,
26
+ TypeInfo type_info, upb_Arena *arena);
27
+
28
+ // Converts |upb_val| to a Ruby VALUE according to |type_info|. This may involve
29
+ // creating a Ruby wrapper object.
30
+ //
31
+ // The |arena| parameter indicates the arena that owns the lifetime of
32
+ // |upb_val|. Any Ruby wrapper object that is created will reference |arena|
33
+ // and ensure it outlives the wrapper.
34
+ VALUE Convert_UpbToRuby(upb_MessageValue upb_val, TypeInfo type_info,
35
+ VALUE arena);
36
+
37
+ // Creates a deep copy of |msgval| in |arena|.
38
+ upb_MessageValue Msgval_DeepCopy(upb_MessageValue msgval, TypeInfo type_info,
39
+ upb_Arena *arena);
40
+
41
+ // Returns true if |val1| and |val2| are equal. Their type is given by
42
+ // |type_info|.
43
+ bool Msgval_IsEqual(upb_MessageValue val1, upb_MessageValue val2,
44
+ TypeInfo type_info);
45
+
46
+ // Returns a hash value for the given upb_MessageValue.
47
+ uint64_t Msgval_GetHash(upb_MessageValue val, TypeInfo type_info,
48
+ uint64_t seed);
49
+
50
+ #endif // RUBY_PROTOBUF_CONVERT_H_