google-protobuf 3.14.0 → 4.26.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/ext/google/protobuf_c/Rakefile +3 -0
  3. data/ext/google/protobuf_c/convert.c +317 -0
  4. data/ext/google/protobuf_c/convert.h +50 -0
  5. data/ext/google/protobuf_c/defs.c +759 -1709
  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 +328 -485
  10. data/ext/google/protobuf_c/map.h +44 -0
  11. data/ext/google/protobuf_c/message.c +1061 -530
  12. data/ext/google/protobuf_c/message.h +86 -0
  13. data/ext/google/protobuf_c/protobuf.c +314 -94
  14. data/ext/google/protobuf_c/protobuf.h +66 -621
  15. data/ext/google/protobuf_c/repeated_field.c +314 -353
  16. data/ext/google/protobuf_c/repeated_field.h +41 -0
  17. data/ext/google/protobuf_c/ruby-upb.c +15407 -0
  18. data/ext/google/protobuf_c/ruby-upb.h +13966 -0
  19. data/ext/google/protobuf_c/shared_convert.c +66 -0
  20. data/ext/google/protobuf_c/shared_convert.h +26 -0
  21. data/ext/google/protobuf_c/shared_message.c +67 -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/utf8_range.c +467 -0
  25. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.h +22 -0
  26. data/ext/google/protobuf_c/wrap_memcpy.c +7 -29
  27. data/lib/google/protobuf/any_pb.rb +6 -8
  28. data/lib/google/protobuf/api_pb.rb +7 -26
  29. data/lib/google/protobuf/descriptor_pb.rb +65 -0
  30. data/lib/google/protobuf/duration_pb.rb +6 -8
  31. data/lib/google/protobuf/empty_pb.rb +6 -6
  32. data/lib/google/protobuf/ffi/descriptor.rb +164 -0
  33. data/lib/google/protobuf/ffi/descriptor_pool.rb +75 -0
  34. data/lib/google/protobuf/ffi/enum_descriptor.rb +171 -0
  35. data/lib/google/protobuf/ffi/ffi.rb +215 -0
  36. data/lib/google/protobuf/ffi/field_descriptor.rb +328 -0
  37. data/lib/google/protobuf/ffi/file_descriptor.rb +47 -0
  38. data/lib/google/protobuf/ffi/internal/arena.rb +66 -0
  39. data/lib/google/protobuf/ffi/internal/convert.rb +289 -0
  40. data/lib/google/protobuf/ffi/internal/pointer_helper.rb +35 -0
  41. data/lib/google/protobuf/ffi/internal/type_safety.rb +25 -0
  42. data/lib/google/protobuf/ffi/map.rb +409 -0
  43. data/lib/google/protobuf/ffi/message.rb +659 -0
  44. data/lib/google/protobuf/ffi/object_cache.rb +30 -0
  45. data/lib/google/protobuf/ffi/oneof_descriptor.rb +95 -0
  46. data/lib/google/protobuf/ffi/repeated_field.rb +385 -0
  47. data/lib/google/protobuf/field_mask_pb.rb +6 -7
  48. data/lib/google/protobuf/internal/object_cache.rb +99 -0
  49. data/lib/google/protobuf/message_exts.rb +10 -28
  50. data/lib/google/protobuf/plugin_pb.rb +25 -0
  51. data/lib/google/protobuf/repeated_field.rb +19 -30
  52. data/lib/google/protobuf/source_context_pb.rb +6 -7
  53. data/lib/google/protobuf/struct_pb.rb +6 -23
  54. data/lib/google/protobuf/timestamp_pb.rb +6 -8
  55. data/lib/google/protobuf/type_pb.rb +7 -71
  56. data/lib/google/protobuf/well_known_types.rb +17 -36
  57. data/lib/google/protobuf/wrappers_pb.rb +6 -31
  58. data/lib/google/protobuf.rb +32 -118
  59. data/lib/google/protobuf_ffi.rb +49 -0
  60. data/lib/google/protobuf_native.rb +19 -0
  61. data/lib/google/tasks/ffi.rake +100 -0
  62. metadata +88 -37
  63. data/ext/google/protobuf_c/encode_decode.c +0 -1795
  64. data/ext/google/protobuf_c/storage.c +0 -1198
  65. data/ext/google/protobuf_c/upb.c +0 -13817
  66. data/ext/google/protobuf_c/upb.h +0 -6777
  67. data/tests/basic.rb +0 -543
  68. data/tests/generated_code_test.rb +0 -23
  69. data/tests/stress.rb +0 -38
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4efa381bee3e947252b6df3d7e26ab4163892b419131e6995d599b91da9eb069
4
- data.tar.gz: 45267244b3e9c99d5cb27aac3727f8739c6e7ad1e0097a6f0c9204aaebddb609
3
+ metadata.gz: 6b75ef03befb2ebc24da2bd1395901962c7b0ba053ebf3dbf9e48c4414e89452
4
+ data.tar.gz: 1ade6d79bcca36e4dafe613c0392c14d5cbadcb721c49cec0651699750b567a8
5
5
  SHA512:
6
- metadata.gz: ff369fc09844a202a30aaf129bcd141d5e3c576c7870f8fc202c3bf4792cd427b4515ebacd9557c2056a613832bcd425156f18d17fe40887df1b09c39146e5ec
7
- data.tar.gz: 6743af82db871d76712e06fb8b6571b61ce4e4c520191243eed922f382b9a50d7938dfbd074a0faa9369a8ed8362b89c8fe3d80e1595166f63e01fe297ae7e24
6
+ metadata.gz: 87a7972ae24b8f883a5dd5fe215184afa703097f3252bc2a4a7e7306c09bdc6d23ce72ea3b0742b3e60928dc46a71fb85df600fc199802b5b8f0db88806204a9
7
+ data.tar.gz: cdda124179218236560080105ba0ed5f5dde5137a01fb9db8691533cc4bdcb907d076fe24685227928f3f41cdf456c82e9d067f38ed9a01f18289d6e26504467
@@ -0,0 +1,3 @@
1
+ import '../../../lib/google/tasks/ffi.rake'
2
+
3
+ task default: ['ffi-protobuf:default']
@@ -0,0 +1,317 @@
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_is_kind_of(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
+ rb_raise(cTypeError,
212
+ "Convert_RubyToUpb(): Unexpected type %d", (int)type_info.type);
213
+ }
214
+
215
+ return ret;
216
+ }
217
+
218
+ VALUE Convert_UpbToRuby(upb_MessageValue upb_val, TypeInfo type_info,
219
+ VALUE arena) {
220
+ switch (type_info.type) {
221
+ case kUpb_CType_Float:
222
+ return DBL2NUM(upb_val.float_val);
223
+ case kUpb_CType_Double:
224
+ return DBL2NUM(upb_val.double_val);
225
+ case kUpb_CType_Bool:
226
+ return upb_val.bool_val ? Qtrue : Qfalse;
227
+ case kUpb_CType_Int32:
228
+ return INT2NUM(upb_val.int32_val);
229
+ case kUpb_CType_Int64:
230
+ return LL2NUM(upb_val.int64_val);
231
+ case kUpb_CType_UInt32:
232
+ return UINT2NUM(upb_val.uint32_val);
233
+ case kUpb_CType_UInt64:
234
+ return ULL2NUM(upb_val.int64_val);
235
+ case kUpb_CType_Enum: {
236
+ const upb_EnumValueDef* ev = upb_EnumDef_FindValueByNumber(
237
+ type_info.def.enumdef, upb_val.int32_val);
238
+ if (ev) {
239
+ return ID2SYM(rb_intern(upb_EnumValueDef_Name(ev)));
240
+ } else {
241
+ return INT2NUM(upb_val.int32_val);
242
+ }
243
+ }
244
+ case kUpb_CType_String: {
245
+ VALUE str_rb = rb_str_new(upb_val.str_val.data, upb_val.str_val.size);
246
+ rb_enc_associate(str_rb, rb_utf8_encoding());
247
+ rb_obj_freeze(str_rb);
248
+ return str_rb;
249
+ }
250
+ case kUpb_CType_Bytes: {
251
+ VALUE str_rb = rb_str_new(upb_val.str_val.data, upb_val.str_val.size);
252
+ rb_enc_associate(str_rb, rb_ascii8bit_encoding());
253
+ rb_obj_freeze(str_rb);
254
+ return str_rb;
255
+ }
256
+ case kUpb_CType_Message:
257
+ return Message_GetRubyWrapper((upb_Message*)upb_val.msg_val,
258
+ type_info.def.msgdef, arena);
259
+ default:
260
+ rb_raise(rb_eRuntimeError, "Convert_UpbToRuby(): Unexpected type %d",
261
+ (int)type_info.type);
262
+ }
263
+ }
264
+
265
+ upb_MessageValue Msgval_DeepCopy(upb_MessageValue msgval, TypeInfo type_info,
266
+ upb_Arena* arena) {
267
+ upb_MessageValue new_msgval;
268
+
269
+ switch (type_info.type) {
270
+ default:
271
+ memcpy(&new_msgval, &msgval, sizeof(msgval));
272
+ break;
273
+ case kUpb_CType_String:
274
+ case kUpb_CType_Bytes: {
275
+ size_t n = msgval.str_val.size;
276
+ char* mem = upb_Arena_Malloc(arena, n);
277
+ new_msgval.str_val.data = mem;
278
+ new_msgval.str_val.size = n;
279
+ memcpy(mem, msgval.str_val.data, n);
280
+ break;
281
+ }
282
+ case kUpb_CType_Message:
283
+ new_msgval.msg_val =
284
+ Message_deep_copy(msgval.msg_val, type_info.def.msgdef, arena);
285
+ break;
286
+ }
287
+
288
+ return new_msgval;
289
+ }
290
+
291
+ bool Msgval_IsEqual(upb_MessageValue val1, upb_MessageValue val2,
292
+ TypeInfo type_info) {
293
+ upb_Status status;
294
+ upb_Status_Clear(&status);
295
+ bool return_value = shared_Msgval_IsEqual(val1, val2, type_info.type,
296
+ type_info.def.msgdef, &status);
297
+ if (upb_Status_IsOk(&status)) {
298
+ return return_value;
299
+ } else {
300
+ rb_raise(rb_eRuntimeError, "Msgval_IsEqual(): %s",
301
+ upb_Status_ErrorMessage(&status));
302
+ }
303
+ }
304
+
305
+ uint64_t Msgval_GetHash(upb_MessageValue val, TypeInfo type_info,
306
+ uint64_t seed) {
307
+ upb_Status status;
308
+ upb_Status_Clear(&status);
309
+ uint64_t return_value = shared_Msgval_GetHash(
310
+ val, type_info.type, type_info.def.msgdef, seed, &status);
311
+ if (upb_Status_IsOk(&status)) {
312
+ return return_value;
313
+ } else {
314
+ rb_raise(rb_eRuntimeError, "Msgval_GetHash(): %s",
315
+ upb_Status_ErrorMessage(&status));
316
+ }
317
+ }
@@ -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_