google-protobuf 3.22.3 → 3.25.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of google-protobuf might be problematic. Click here for more details.

Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/ext/google/protobuf_c/Rakefile +3 -0
  3. data/ext/google/protobuf_c/convert.c +23 -70
  4. data/ext/google/protobuf_c/convert.h +3 -28
  5. data/ext/google/protobuf_c/defs.c +36 -48
  6. data/ext/google/protobuf_c/defs.h +3 -28
  7. data/ext/google/protobuf_c/extconf.rb +2 -1
  8. data/ext/google/protobuf_c/glue.c +21 -0
  9. data/ext/google/protobuf_c/map.c +7 -28
  10. data/ext/google/protobuf_c/map.h +3 -28
  11. data/ext/google/protobuf_c/message.c +37 -75
  12. data/ext/google/protobuf_c/message.h +3 -28
  13. data/ext/google/protobuf_c/protobuf.c +39 -176
  14. data/ext/google/protobuf_c/protobuf.h +24 -32
  15. data/ext/google/protobuf_c/repeated_field.c +9 -29
  16. data/ext/google/protobuf_c/repeated_field.h +3 -28
  17. data/ext/google/protobuf_c/ruby-upb.c +2982 -2494
  18. data/ext/google/protobuf_c/ruby-upb.h +5836 -3465
  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/wrap_memcpy.c +3 -26
  24. data/lib/google/protobuf/any_pb.rb +24 -5
  25. data/lib/google/protobuf/api_pb.rb +26 -23
  26. data/lib/google/protobuf/descriptor_pb.rb +40 -252
  27. data/lib/google/protobuf/duration_pb.rb +24 -5
  28. data/lib/google/protobuf/empty_pb.rb +24 -3
  29. data/lib/google/protobuf/ffi/descriptor.rb +154 -0
  30. data/lib/google/protobuf/ffi/descriptor_pool.rb +70 -0
  31. data/lib/google/protobuf/ffi/enum_descriptor.rb +161 -0
  32. data/lib/google/protobuf/ffi/ffi.rb +213 -0
  33. data/lib/google/protobuf/ffi/field_descriptor.rb +309 -0
  34. data/lib/google/protobuf/ffi/file_descriptor.rb +48 -0
  35. data/lib/google/protobuf/ffi/internal/arena.rb +66 -0
  36. data/lib/google/protobuf/ffi/internal/convert.rb +305 -0
  37. data/lib/google/protobuf/ffi/internal/pointer_helper.rb +35 -0
  38. data/lib/google/protobuf/ffi/internal/type_safety.rb +25 -0
  39. data/lib/google/protobuf/ffi/map.rb +396 -0
  40. data/lib/google/protobuf/ffi/message.rb +641 -0
  41. data/lib/google/protobuf/ffi/object_cache.rb +30 -0
  42. data/lib/google/protobuf/ffi/oneof_descriptor.rb +88 -0
  43. data/lib/google/protobuf/ffi/repeated_field.rb +503 -0
  44. data/lib/google/protobuf/field_mask_pb.rb +24 -4
  45. data/lib/google/protobuf/message_exts.rb +3 -26
  46. data/lib/google/protobuf/object_cache.rb +97 -0
  47. data/lib/google/protobuf/plugin_pb.rb +25 -28
  48. data/lib/google/protobuf/repeated_field.rb +3 -26
  49. data/lib/google/protobuf/source_context_pb.rb +24 -4
  50. data/lib/google/protobuf/struct_pb.rb +24 -20
  51. data/lib/google/protobuf/timestamp_pb.rb +24 -5
  52. data/lib/google/protobuf/type_pb.rb +26 -68
  53. data/lib/google/protobuf/well_known_types.rb +5 -34
  54. data/lib/google/protobuf/wrappers_pb.rb +24 -28
  55. data/lib/google/protobuf.rb +27 -45
  56. data/lib/google/protobuf_ffi.rb +50 -0
  57. data/lib/google/protobuf_native.rb +20 -0
  58. data/lib/google/tasks/ffi.rake +102 -0
  59. metadata +72 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 36a0cf54a74c46392a4b56ba35af0b7d1c97af893b39da281a5c18261f358039
4
- data.tar.gz: d1980f60cb8e4bcfb97480b646cde1ade2435ed76b5554e924b5027da883acc3
3
+ metadata.gz: f07b3e363c6a69664f352f94158f1c150d11fc6947fd8aafe1d06decbed0a545
4
+ data.tar.gz: dd3bb39d53484b82469238a0fd009d2049d4c1ce5732b1c141e73a1f5b23c8bb
5
5
  SHA512:
6
- metadata.gz: e7c05d8fc134b72971ac14d911ea0d45a183fbcf308346bcf510dbbefd190c4f84caa1232be8a367fc5ee0d7c119239c21d6365b54d85cb0a775676a11e231d6
7
- data.tar.gz: 4f7bd0f43ebd8fdab52094a54974ff366239dc7886127d5e119159538e83512bafd2da5a3d5df70af559abd9155cddd94e21f17cbf7060c249423d03a0791401
6
+ metadata.gz: '028e14308853bce11c830bd55735df413954ac79a986e46375964907ca984f5f93bf4cf0ed8632c175b5fe9946724d270a999733778e735f568fa215018275f9'
7
+ data.tar.gz: 3f6832e092254e27ca64fdf11367cb3a4e4a0e8f34d6e824044a8e701bcea5e88e4b8aaeba81ea8b83ab5e5a834969c074ba034e056cdc26484822064c0403f6
@@ -0,0 +1,3 @@
1
+ import '../../../lib/google/tasks/ffi.rake'
2
+
3
+ task default: ['ffi-protobuf:default']
@@ -1,32 +1,9 @@
1
1
  // Protocol Buffers - Google's data interchange format
2
2
  // Copyright 2008 Google Inc. All rights reserved.
3
- // https://developers.google.com/protocol-buffers/
4
3
  //
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.
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
30
7
 
31
8
  // -----------------------------------------------------------------------------
32
9
  // Ruby <-> upb data conversion functions.
@@ -41,6 +18,7 @@
41
18
 
42
19
  #include "message.h"
43
20
  #include "protobuf.h"
21
+ #include "shared_convert.h"
44
22
 
45
23
  static upb_StringView Convert_StringData(VALUE str, upb_Arena* arena) {
46
24
  upb_StringView ret;
@@ -111,8 +89,7 @@ static int32_t Convert_ToEnum(VALUE value, const char* name,
111
89
  case T_SYMBOL: {
112
90
  const upb_EnumValueDef* ev =
113
91
  upb_EnumDef_FindValueByName(e, rb_id2name(SYM2ID(value)));
114
- if (!ev)
115
- goto unknownval;
92
+ if (!ev) goto unknownval;
116
93
  val = upb_EnumValueDef_Number(ev);
117
94
  break;
118
95
  }
@@ -194,7 +171,7 @@ upb_MessageValue Convert_RubyToUpb(VALUE value, const char* name,
194
171
  if (rb_obj_encoding(value) != bytes) {
195
172
  // Note: this will not duplicate underlying string data unless
196
173
  // necessary.
197
- // TODO(haberman): is this really necessary to get raw bytes?
174
+ // TODO: is this really necessary to get raw bytes?
198
175
  value = rb_str_encode(value, bytes, 0, Qnil);
199
176
  }
200
177
 
@@ -255,7 +232,7 @@ VALUE Convert_UpbToRuby(upb_MessageValue upb_val, TypeInfo type_info,
255
232
  case kUpb_CType_UInt64:
256
233
  return ULL2NUM(upb_val.int64_val);
257
234
  case kUpb_CType_Enum: {
258
- const upb_EnumValueDef *ev = upb_EnumDef_FindValueByNumber(
235
+ const upb_EnumValueDef* ev = upb_EnumDef_FindValueByNumber(
259
236
  type_info.def.enumdef, upb_val.int32_val);
260
237
  if (ev) {
261
238
  return ID2SYM(rb_intern(upb_EnumValueDef_Name(ev)));
@@ -312,50 +289,26 @@ upb_MessageValue Msgval_DeepCopy(upb_MessageValue msgval, TypeInfo type_info,
312
289
 
313
290
  bool Msgval_IsEqual(upb_MessageValue val1, upb_MessageValue val2,
314
291
  TypeInfo type_info) {
315
- switch (type_info.type) {
316
- case kUpb_CType_Bool:
317
- return memcmp(&val1, &val2, 1) == 0;
318
- case kUpb_CType_Float:
319
- case kUpb_CType_Int32:
320
- case kUpb_CType_UInt32:
321
- case kUpb_CType_Enum:
322
- return memcmp(&val1, &val2, 4) == 0;
323
- case kUpb_CType_Double:
324
- case kUpb_CType_Int64:
325
- case kUpb_CType_UInt64:
326
- return memcmp(&val1, &val2, 8) == 0;
327
- case kUpb_CType_String:
328
- case kUpb_CType_Bytes:
329
- return val1.str_val.size == val2.str_val.size &&
330
- memcmp(val1.str_val.data, val2.str_val.data, val1.str_val.size) ==
331
- 0;
332
- case kUpb_CType_Message:
333
- return Message_Equal(val1.msg_val, val2.msg_val, type_info.def.msgdef);
334
- default:
335
- rb_raise(rb_eRuntimeError, "Internal error, unexpected type");
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));
336
300
  }
337
301
  }
338
302
 
339
303
  uint64_t Msgval_GetHash(upb_MessageValue val, TypeInfo type_info,
340
304
  uint64_t seed) {
341
- switch (type_info.type) {
342
- case kUpb_CType_Bool:
343
- return _upb_Hash(&val, 1, seed);
344
- case kUpb_CType_Float:
345
- case kUpb_CType_Int32:
346
- case kUpb_CType_UInt32:
347
- case kUpb_CType_Enum:
348
- return _upb_Hash(&val, 4, seed);
349
- case kUpb_CType_Double:
350
- case kUpb_CType_Int64:
351
- case kUpb_CType_UInt64:
352
- return _upb_Hash(&val, 8, seed);
353
- case kUpb_CType_String:
354
- case kUpb_CType_Bytes:
355
- return _upb_Hash(val.str_val.data, val.str_val.size, seed);
356
- case kUpb_CType_Message:
357
- return Message_Hash(val.msg_val, type_info.def.msgdef, seed);
358
- default:
359
- rb_raise(rb_eRuntimeError, "Internal error, unexpected type");
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));
360
313
  }
361
314
  }
@@ -1,38 +1,13 @@
1
1
  // Protocol Buffers - Google's data interchange format
2
2
  // Copyright 2008 Google Inc. All rights reserved.
3
- // https://developers.google.com/protocol-buffers/
4
3
  //
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.
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
30
7
 
31
8
  #ifndef RUBY_PROTOBUF_CONVERT_H_
32
9
  #define RUBY_PROTOBUF_CONVERT_H_
33
10
 
34
- #include <ruby/ruby.h>
35
-
36
11
  #include "protobuf.h"
37
12
  #include "ruby-upb.h"
38
13
 
@@ -1,32 +1,9 @@
1
1
  // Protocol Buffers - Google's data interchange format
2
2
  // Copyright 2014 Google Inc. All rights reserved.
3
- // https://developers.google.com/protocol-buffers/
4
3
  //
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.
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
30
7
 
31
8
  #include <ctype.h>
32
9
  #include <errno.h>
@@ -73,6 +50,8 @@ static VALUE rb_str_maybe_null(const char* s) {
73
50
  // -----------------------------------------------------------------------------
74
51
 
75
52
  typedef struct {
53
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
54
+ // macro to update VALUE references, as to trigger write barriers.
76
55
  VALUE def_to_descriptor; // Hash table of def* -> Ruby descriptor.
77
56
  upb_DefPool* symtab;
78
57
  } DescriptorPool;
@@ -97,7 +76,7 @@ static void DescriptorPool_free(void* _self) {
97
76
  static const rb_data_type_t DescriptorPool_type = {
98
77
  "Google::Protobuf::DescriptorPool",
99
78
  {DescriptorPool_mark, DescriptorPool_free, NULL},
100
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
79
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
101
80
  };
102
81
 
103
82
  static DescriptorPool* ruby_to_DescriptorPool(VALUE val) {
@@ -125,11 +104,9 @@ static VALUE DescriptorPool_alloc(VALUE klass) {
125
104
  self->def_to_descriptor = Qnil;
126
105
  ret = TypedData_Wrap_Struct(klass, &DescriptorPool_type, self);
127
106
 
128
- self->def_to_descriptor = rb_hash_new();
107
+ RB_OBJ_WRITE(ret, &self->def_to_descriptor, rb_hash_new());
129
108
  self->symtab = upb_DefPool_New();
130
- ObjectCache_Add(self->symtab, ret);
131
-
132
- return ret;
109
+ return ObjectCache_TryAdd(self->symtab, ret);
133
110
  }
134
111
 
135
112
  /*
@@ -223,6 +200,8 @@ static void DescriptorPool_register(VALUE module) {
223
200
 
224
201
  typedef struct {
225
202
  const upb_MessageDef* msgdef;
203
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
204
+ // macro to update VALUE references, as to trigger write barriers.
226
205
  VALUE klass;
227
206
  VALUE descriptor_pool;
228
207
  } Descriptor;
@@ -238,7 +217,7 @@ static void Descriptor_mark(void* _self) {
238
217
  static const rb_data_type_t Descriptor_type = {
239
218
  "Google::Protobuf::Descriptor",
240
219
  {Descriptor_mark, RUBY_DEFAULT_FREE, NULL},
241
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
220
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
242
221
  };
243
222
 
244
223
  static Descriptor* ruby_to_Descriptor(VALUE val) {
@@ -280,7 +259,7 @@ static VALUE Descriptor_initialize(VALUE _self, VALUE cookie,
280
259
  "Descriptor objects may not be created from Ruby.");
281
260
  }
282
261
 
283
- self->descriptor_pool = descriptor_pool;
262
+ RB_OBJ_WRITE(_self, &self->descriptor_pool, descriptor_pool);
284
263
  self->msgdef = (const upb_MessageDef*)NUM2ULL(ptr);
285
264
 
286
265
  return Qnil;
@@ -390,7 +369,7 @@ static VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
390
369
  static VALUE Descriptor_msgclass(VALUE _self) {
391
370
  Descriptor* self = ruby_to_Descriptor(_self);
392
371
  if (self->klass == Qnil) {
393
- self->klass = build_class_from_descriptor(_self);
372
+ RB_OBJ_WRITE(_self, &self->klass, build_class_from_descriptor(_self));
394
373
  }
395
374
  return self->klass;
396
375
  }
@@ -417,6 +396,8 @@ static void Descriptor_register(VALUE module) {
417
396
 
418
397
  typedef struct {
419
398
  const upb_FileDef* filedef;
399
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
400
+ // macro to update VALUE references, as to trigger write barriers.
420
401
  VALUE descriptor_pool; // Owns the upb_FileDef.
421
402
  } FileDescriptor;
422
403
 
@@ -430,7 +411,7 @@ static void FileDescriptor_mark(void* _self) {
430
411
  static const rb_data_type_t FileDescriptor_type = {
431
412
  "Google::Protobuf::FileDescriptor",
432
413
  {FileDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
433
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
414
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
434
415
  };
435
416
 
436
417
  static FileDescriptor* ruby_to_FileDescriptor(VALUE val) {
@@ -463,7 +444,7 @@ static VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
463
444
  "Descriptor objects may not be created from Ruby.");
464
445
  }
465
446
 
466
- self->descriptor_pool = descriptor_pool;
447
+ RB_OBJ_WRITE(_self, &self->descriptor_pool, descriptor_pool);
467
448
  self->filedef = (const upb_FileDef*)NUM2ULL(ptr);
468
449
 
469
450
  return Qnil;
@@ -519,6 +500,8 @@ static void FileDescriptor_register(VALUE module) {
519
500
 
520
501
  typedef struct {
521
502
  const upb_FieldDef* fielddef;
503
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
504
+ // macro to update VALUE references, as to trigger write barriers.
522
505
  VALUE descriptor_pool; // Owns the upb_FieldDef.
523
506
  } FieldDescriptor;
524
507
 
@@ -532,7 +515,7 @@ static void FieldDescriptor_mark(void* _self) {
532
515
  static const rb_data_type_t FieldDescriptor_type = {
533
516
  "Google::Protobuf::FieldDescriptor",
534
517
  {FieldDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
535
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
518
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
536
519
  };
537
520
 
538
521
  static FieldDescriptor* ruby_to_FieldDescriptor(VALUE val) {
@@ -570,7 +553,7 @@ static VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
570
553
  "Descriptor objects may not be created from Ruby.");
571
554
  }
572
555
 
573
- self->descriptor_pool = descriptor_pool;
556
+ RB_OBJ_WRITE(_self, &self->descriptor_pool, descriptor_pool);
574
557
  self->fielddef = (const upb_FieldDef*)NUM2ULL(ptr);
575
558
 
576
559
  return Qnil;
@@ -595,7 +578,7 @@ upb_CType ruby_to_fieldtype(VALUE type) {
595
578
 
596
579
  #define CONVERT(upb, ruby) \
597
580
  if (SYM2ID(type) == rb_intern(#ruby)) { \
598
- return kUpb_CType_##upb; \
581
+ return kUpb_CType_##upb; \
599
582
  }
600
583
 
601
584
  CONVERT(Float, float);
@@ -618,7 +601,7 @@ upb_CType ruby_to_fieldtype(VALUE type) {
618
601
 
619
602
  static VALUE descriptortype_to_ruby(upb_FieldType type) {
620
603
  switch (type) {
621
- #define CONVERT(upb, ruby) \
604
+ #define CONVERT(upb, ruby) \
622
605
  case kUpb_FieldType_##upb: \
623
606
  return ID2SYM(rb_intern(#ruby));
624
607
  CONVERT(Float, float);
@@ -703,7 +686,7 @@ static VALUE FieldDescriptor_label(VALUE _self) {
703
686
  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
704
687
  switch (upb_FieldDef_Label(self->fielddef)) {
705
688
  #define CONVERT(upb, ruby) \
706
- case kUpb_Label_##upb: \
689
+ case kUpb_Label_##upb: \
707
690
  return ID2SYM(rb_intern(#ruby));
708
691
 
709
692
  CONVERT(Optional, optional);
@@ -884,6 +867,8 @@ static void FieldDescriptor_register(VALUE module) {
884
867
 
885
868
  typedef struct {
886
869
  const upb_OneofDef* oneofdef;
870
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
871
+ // macro to update VALUE references, as to trigger write barriers.
887
872
  VALUE descriptor_pool; // Owns the upb_OneofDef.
888
873
  } OneofDescriptor;
889
874
 
@@ -897,7 +882,7 @@ static void OneofDescriptor_mark(void* _self) {
897
882
  static const rb_data_type_t OneofDescriptor_type = {
898
883
  "Google::Protobuf::OneofDescriptor",
899
884
  {OneofDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
900
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
885
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
901
886
  };
902
887
 
903
888
  static OneofDescriptor* ruby_to_OneofDescriptor(VALUE val) {
@@ -936,7 +921,7 @@ static VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
936
921
  "Descriptor objects may not be created from Ruby.");
937
922
  }
938
923
 
939
- self->descriptor_pool = descriptor_pool;
924
+ RB_OBJ_WRITE(_self, &self->descriptor_pool, descriptor_pool);
940
925
  self->oneofdef = (const upb_OneofDef*)NUM2ULL(ptr);
941
926
 
942
927
  return Qnil;
@@ -988,6 +973,8 @@ static void OneofDescriptor_register(VALUE module) {
988
973
 
989
974
  typedef struct {
990
975
  const upb_EnumDef* enumdef;
976
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
977
+ // macro to update VALUE references, as to trigger write barriers.
991
978
  VALUE module; // begins as nil
992
979
  VALUE descriptor_pool; // Owns the upb_EnumDef.
993
980
  } EnumDescriptor;
@@ -1003,7 +990,7 @@ static void EnumDescriptor_mark(void* _self) {
1003
990
  static const rb_data_type_t EnumDescriptor_type = {
1004
991
  "Google::Protobuf::EnumDescriptor",
1005
992
  {EnumDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
1006
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
993
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
1007
994
  };
1008
995
 
1009
996
  static EnumDescriptor* ruby_to_EnumDescriptor(VALUE val) {
@@ -1042,7 +1029,7 @@ static VALUE EnumDescriptor_initialize(VALUE _self, VALUE cookie,
1042
1029
  "Descriptor objects may not be created from Ruby.");
1043
1030
  }
1044
1031
 
1045
- self->descriptor_pool = descriptor_pool;
1032
+ RB_OBJ_WRITE(_self, &self->descriptor_pool, descriptor_pool);
1046
1033
  self->enumdef = (const upb_EnumDef*)NUM2ULL(ptr);
1047
1034
 
1048
1035
  return Qnil;
@@ -1081,7 +1068,7 @@ static VALUE EnumDescriptor_name(VALUE _self) {
1081
1068
  static VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
1082
1069
  EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1083
1070
  const char* name_str = rb_id2name(SYM2ID(name));
1084
- const upb_EnumValueDef *ev =
1071
+ const upb_EnumValueDef* ev =
1085
1072
  upb_EnumDef_FindValueByName(self->enumdef, name_str);
1086
1073
  if (ev) {
1087
1074
  return INT2NUM(upb_EnumValueDef_Number(ev));
@@ -1100,7 +1087,8 @@ static VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
1100
1087
  static VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
1101
1088
  EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1102
1089
  int32_t val = NUM2INT(number);
1103
- const upb_EnumValueDef* ev = upb_EnumDef_FindValueByNumber(self->enumdef, val);
1090
+ const upb_EnumValueDef* ev =
1091
+ upb_EnumDef_FindValueByNumber(self->enumdef, val);
1104
1092
  if (ev) {
1105
1093
  return ID2SYM(rb_intern(upb_EnumValueDef_Name(ev)));
1106
1094
  } else {
@@ -1138,7 +1126,7 @@ static VALUE EnumDescriptor_each(VALUE _self) {
1138
1126
  static VALUE EnumDescriptor_enummodule(VALUE _self) {
1139
1127
  EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1140
1128
  if (self->module == Qnil) {
1141
- self->module = build_module_from_enumdesc(_self);
1129
+ RB_OBJ_WRITE(_self, &self->module, build_module_from_enumdesc(_self));
1142
1130
  }
1143
1131
  return self->module;
1144
1132
  }
@@ -1,38 +1,13 @@
1
1
  // Protocol Buffers - Google's data interchange format
2
2
  // Copyright 2008 Google Inc. All rights reserved.
3
- // https://developers.google.com/protocol-buffers/
4
3
  //
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.
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
30
7
 
31
8
  #ifndef RUBY_PROTOBUF_DEFS_H_
32
9
  #define RUBY_PROTOBUF_DEFS_H_
33
10
 
34
- #include <ruby/ruby.h>
35
-
36
11
  #include "protobuf.h"
37
12
  #include "ruby-upb.h"
38
13
 
@@ -22,6 +22,7 @@ $INCFLAGS += " -I$(srcdir)/third_party/utf8_range"
22
22
 
23
23
  $srcs = ["protobuf.c", "convert.c", "defs.c", "message.c",
24
24
  "repeated_field.c", "map.c", "ruby-upb.c", "wrap_memcpy.c",
25
- "naive.c", "range2-neon.c", "range2-sse.c"]
25
+ "naive.c", "range2-neon.c", "range2-sse.c", "shared_convert.c",
26
+ "shared_message.c"]
26
27
 
27
28
  create_makefile(ext_name)
@@ -0,0 +1,21 @@
1
+ // Protocol Buffers - Google's data interchange format
2
+ // Copyright 2023 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
+ // Exposing inlined UPB functions. Strictly free of dependencies on
10
+ // Ruby interpreter internals.
11
+
12
+ #include "ruby-upb.h"
13
+
14
+ upb_Arena* Arena_create() { return upb_Arena_Init(NULL, 0, &upb_alloc_global); }
15
+
16
+ google_protobuf_FileDescriptorProto* FileDescriptorProto_parse(
17
+ const char* serialized_file_proto, size_t length) {
18
+ upb_Arena* arena = Arena_create();
19
+ return google_protobuf_FileDescriptorProto_parse(serialized_file_proto,
20
+ length, arena);
21
+ }
@@ -1,32 +1,9 @@
1
1
  // Protocol Buffers - Google's data interchange format
2
2
  // Copyright 2014 Google Inc. All rights reserved.
3
- // https://developers.google.com/protocol-buffers/
4
3
  //
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.
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
30
7
 
31
8
  #include "convert.h"
32
9
  #include "defs.h"
@@ -93,7 +70,6 @@ VALUE Map_GetRubyWrapper(upb_Map* map, upb_CType key_type, TypeInfo value_type,
93
70
  if (val == Qnil) {
94
71
  val = Map_alloc(cMap);
95
72
  Map* self;
96
- ObjectCache_Add(map, val);
97
73
  TypedData_Get_Struct(val, Map, &Map_type, self);
98
74
  self->map = map;
99
75
  self->arena = arena;
@@ -103,6 +79,7 @@ VALUE Map_GetRubyWrapper(upb_Map* map, upb_CType key_type, TypeInfo value_type,
103
79
  const upb_MessageDef* val_m = self->value_type_info.def.msgdef;
104
80
  self->value_type_class = Descriptor_DefToClass(val_m);
105
81
  }
82
+ return ObjectCache_TryAdd(map, val);
106
83
  }
107
84
 
108
85
  return val;
@@ -319,7 +296,9 @@ static VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
319
296
 
320
297
  self->map = upb_Map_New(Arena_get(self->arena), self->key_type,
321
298
  self->value_type_info.type);
322
- ObjectCache_Add(self->map, _self);
299
+ VALUE stored = ObjectCache_TryAdd(self->map, _self);
300
+ (void)stored;
301
+ PBRUBY_ASSERT(stored == _self);
323
302
 
324
303
  if (init_arg != Qnil) {
325
304
  Map_merge_into_self(_self, init_arg);
@@ -1,38 +1,13 @@
1
1
  // Protocol Buffers - Google's data interchange format
2
2
  // Copyright 2008 Google Inc. All rights reserved.
3
- // https://developers.google.com/protocol-buffers/
4
3
  //
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.
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
30
7
 
31
8
  #ifndef RUBY_PROTOBUF_MAP_H_
32
9
  #define RUBY_PROTOBUF_MAP_H_
33
10
 
34
- #include <ruby/ruby.h>
35
-
36
11
  #include "protobuf.h"
37
12
  #include "ruby-upb.h"
38
13