google-protobuf 4.31.0.rc.1-x86_64-linux-gnu

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. checksums.yaml +7 -0
  2. data/ext/google/protobuf_c/Rakefile +3 -0
  3. data/ext/google/protobuf_c/convert.c +335 -0
  4. data/ext/google/protobuf_c/convert.h +50 -0
  5. data/ext/google/protobuf_c/defs.c +1932 -0
  6. data/ext/google/protobuf_c/defs.h +82 -0
  7. data/ext/google/protobuf_c/extconf.rb +44 -0
  8. data/ext/google/protobuf_c/glue.c +135 -0
  9. data/ext/google/protobuf_c/map.c +731 -0
  10. data/ext/google/protobuf_c/map.h +48 -0
  11. data/ext/google/protobuf_c/message.c +1426 -0
  12. data/ext/google/protobuf_c/message.h +82 -0
  13. data/ext/google/protobuf_c/protobuf.c +357 -0
  14. data/ext/google/protobuf_c/protobuf.h +102 -0
  15. data/ext/google/protobuf_c/repeated_field.c +691 -0
  16. data/ext/google/protobuf_c/repeated_field.h +45 -0
  17. data/ext/google/protobuf_c/ruby-upb.c +18305 -0
  18. data/ext/google/protobuf_c/ruby-upb.h +16315 -0
  19. data/ext/google/protobuf_c/shared_convert.c +69 -0
  20. data/ext/google/protobuf_c/shared_convert.h +26 -0
  21. data/ext/google/protobuf_c/shared_message.c +37 -0
  22. data/ext/google/protobuf_c/shared_message.h +21 -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 +207 -0
  25. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.h +22 -0
  26. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range_neon.inc +117 -0
  27. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range_sse.inc +272 -0
  28. data/ext/google/protobuf_c/wrap_memcpy.c +29 -0
  29. data/lib/google/3.1/protobuf_c.so +0 -0
  30. data/lib/google/3.2/protobuf_c.so +0 -0
  31. data/lib/google/3.3/protobuf_c.so +0 -0
  32. data/lib/google/3.4/protobuf_c.so +0 -0
  33. data/lib/google/protobuf/any_pb.rb +17 -0
  34. data/lib/google/protobuf/api_pb.rb +22 -0
  35. data/lib/google/protobuf/descriptor_pb.rb +70 -0
  36. data/lib/google/protobuf/duration_pb.rb +17 -0
  37. data/lib/google/protobuf/empty_pb.rb +17 -0
  38. data/lib/google/protobuf/ffi/descriptor.rb +175 -0
  39. data/lib/google/protobuf/ffi/descriptor_pool.rb +79 -0
  40. data/lib/google/protobuf/ffi/enum_descriptor.rb +183 -0
  41. data/lib/google/protobuf/ffi/ffi.rb +214 -0
  42. data/lib/google/protobuf/ffi/field_descriptor.rb +346 -0
  43. data/lib/google/protobuf/ffi/file_descriptor.rb +85 -0
  44. data/lib/google/protobuf/ffi/internal/arena.rb +60 -0
  45. data/lib/google/protobuf/ffi/internal/convert.rb +292 -0
  46. data/lib/google/protobuf/ffi/internal/pointer_helper.rb +36 -0
  47. data/lib/google/protobuf/ffi/internal/type_safety.rb +25 -0
  48. data/lib/google/protobuf/ffi/map.rb +433 -0
  49. data/lib/google/protobuf/ffi/message.rb +783 -0
  50. data/lib/google/protobuf/ffi/method_descriptor.rb +124 -0
  51. data/lib/google/protobuf/ffi/object_cache.rb +30 -0
  52. data/lib/google/protobuf/ffi/oneof_descriptor.rb +107 -0
  53. data/lib/google/protobuf/ffi/repeated_field.rb +411 -0
  54. data/lib/google/protobuf/ffi/service_descriptor.rb +117 -0
  55. data/lib/google/protobuf/field_mask_pb.rb +17 -0
  56. data/lib/google/protobuf/internal/object_cache.rb +99 -0
  57. data/lib/google/protobuf/message_exts.rb +39 -0
  58. data/lib/google/protobuf/plugin_pb.rb +25 -0
  59. data/lib/google/protobuf/repeated_field.rb +177 -0
  60. data/lib/google/protobuf/source_context_pb.rb +17 -0
  61. data/lib/google/protobuf/struct_pb.rb +20 -0
  62. data/lib/google/protobuf/timestamp_pb.rb +17 -0
  63. data/lib/google/protobuf/type_pb.rb +27 -0
  64. data/lib/google/protobuf/well_known_types.rb +211 -0
  65. data/lib/google/protobuf/wrappers_pb.rb +25 -0
  66. data/lib/google/protobuf.rb +61 -0
  67. data/lib/google/protobuf_ffi.rb +52 -0
  68. data/lib/google/protobuf_native.rb +19 -0
  69. data/lib/google/tasks/ffi.rake +100 -0
  70. metadata +215 -0
@@ -0,0 +1,346 @@
1
+ # Protocol Buffers - Google's data interchange format
2
+ # Copyright 2022 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
+ module Google
9
+ module Protobuf
10
+ class FieldDescriptor
11
+ attr :field_def, :descriptor_pool
12
+
13
+ include Google::Protobuf::Internal::Convert
14
+
15
+ # FFI Interface methods and setup
16
+ extend ::FFI::DataConverter
17
+ native_type ::FFI::Type::POINTER
18
+
19
+ class << self
20
+ prepend Google::Protobuf::Internal::TypeSafety
21
+ include Google::Protobuf::Internal::PointerHelper
22
+
23
+ # @param value [FieldDescriptor] FieldDescriptor to convert to an FFI native type
24
+ # @param _ [Object] Unused
25
+ def to_native(value, _)
26
+ field_def_ptr = value.instance_variable_get(:@field_def)
27
+ warn "Underlying field_def was nil!" if field_def_ptr.nil?
28
+ raise "Underlying field_def was null!" if !field_def_ptr.nil? and field_def_ptr.null?
29
+ field_def_ptr
30
+ end
31
+
32
+ ##
33
+ # @param field_def [::FFI::Pointer] FieldDef pointer to be wrapped
34
+ # @param _ [Object] Unused
35
+ def from_native(field_def, _ = nil)
36
+ return nil if field_def.nil? or field_def.null?
37
+ file_def = Google::Protobuf::FFI.file_def_by_raw_field_def(field_def)
38
+ descriptor_from_file_def(file_def, field_def)
39
+ end
40
+ end
41
+
42
+ def self.new(*arguments, &block)
43
+ raise "Descriptor objects may not be created from Ruby."
44
+ end
45
+
46
+ def to_s
47
+ inspect
48
+ end
49
+
50
+ def inspect
51
+ "#{self.class.name}: #{name}"
52
+ end
53
+
54
+ def name
55
+ @name ||= Google::Protobuf::FFI.get_full_name(self)
56
+ end
57
+
58
+ def json_name
59
+ @json_name ||= Google::Protobuf::FFI.get_json_name(self)
60
+ end
61
+
62
+ def number
63
+ @number ||= Google::Protobuf::FFI.get_number(self)
64
+ end
65
+
66
+ def type
67
+ @type ||= Google::Protobuf::FFI.get_type(self)
68
+ end
69
+
70
+ # DEPRECATED: Use required? or repeated? instead.
71
+ def label
72
+ @label ||= Google::Protobuf::FFI.get_label(self)
73
+ end
74
+
75
+ def default
76
+ return nil if Google::Protobuf::FFI.is_sub_message(self)
77
+ if Google::Protobuf::FFI.is_repeated(self)
78
+ message_value = Google::Protobuf::FFI::MessageValue.new
79
+ else
80
+ message_value = Google::Protobuf::FFI.get_default(self)
81
+ end
82
+ enum_def = Google::Protobuf::FFI.get_subtype_as_enum(self)
83
+ if enum_def.null?
84
+ convert_upb_to_ruby message_value, c_type
85
+ else
86
+ convert_upb_to_ruby message_value, c_type, enum_def
87
+ end
88
+ end
89
+
90
+ def submsg_name
91
+ if defined? @submsg_name
92
+ @submsg_name
93
+ else
94
+ @submsg_name = case c_type
95
+ when :enum
96
+ Google::Protobuf::FFI.get_enum_fullname Google::Protobuf::FFI.get_subtype_as_enum self
97
+ when :message
98
+ Google::Protobuf::FFI.get_message_fullname Google::Protobuf::FFI.get_subtype_as_message self
99
+ else
100
+ nil
101
+ end
102
+ end
103
+ end
104
+
105
+ ##
106
+ # Tests if this field has been set on the argument message.
107
+ #
108
+ # @param msg [Google::Protobuf::Message]
109
+ # @return [Object] Value of the field on this message.
110
+ # @raise [TypeError] If the field is not defined on this message.
111
+ def get(msg)
112
+ if msg.class.descriptor == Google::Protobuf::FFI.get_containing_message_def(self)
113
+ msg.send :get_field, self
114
+ else
115
+ raise TypeError.new "get method called on wrong message type"
116
+ end
117
+ end
118
+
119
+ def subtype
120
+ if defined? @subtype
121
+ @subtype
122
+ else
123
+ @subtype = case c_type
124
+ when :enum
125
+ Google::Protobuf::FFI.get_subtype_as_enum(self)
126
+ when :message
127
+ Google::Protobuf::FFI.get_subtype_as_message(self)
128
+ else
129
+ nil
130
+ end
131
+ end
132
+ end
133
+
134
+ ##
135
+ # Tests if this field has been set on the argument message.
136
+ #
137
+ # @param msg [Google::Protobuf::Message]
138
+ # @return [Boolean] True iff message has this field set
139
+ # @raise [TypeError] If this field does not exist on the message
140
+ # @raise [ArgumentError] If this field does not track presence
141
+ def has?(msg)
142
+ if msg.class.descriptor != Google::Protobuf::FFI.get_containing_message_def(self)
143
+ raise TypeError.new "has method called on wrong message type"
144
+ end
145
+ unless has_presence?
146
+ raise ArgumentError.new "does not track presence"
147
+ end
148
+
149
+ Google::Protobuf::FFI.get_message_has msg.instance_variable_get(:@msg), self
150
+ end
151
+
152
+ ##
153
+ # Tests if this field tracks presence.
154
+ #
155
+ # @return [Boolean] True iff this field tracks presence
156
+ def has_presence?
157
+ @has_presence ||= Google::Protobuf::FFI.get_has_presence(self)
158
+ end
159
+
160
+ ##
161
+ # Tests if this is a repeated field that uses packed encoding.
162
+ #
163
+ # @return [Boolean] True iff this field is packed
164
+ def is_packed?
165
+ @is_packed ||= Google::Protobuf::FFI.get_is_packed(self)
166
+ end
167
+
168
+ # @param msg [Google::Protobuf::Message]
169
+ def clear(msg)
170
+ if msg.class.descriptor != Google::Protobuf::FFI.get_containing_message_def(self)
171
+ raise TypeError.new "clear method called on wrong message type"
172
+ end
173
+ Google::Protobuf::FFI.clear_message_field msg.instance_variable_get(:@msg), self
174
+ nil
175
+ end
176
+
177
+ ##
178
+ # call-seq:
179
+ # FieldDescriptor.set(message, value)
180
+ #
181
+ # Sets the value corresponding to this field to the given value on the given
182
+ # message. Raises an exception if message is of the wrong type. Performs the
183
+ # ordinary type-checks for field setting.
184
+ #
185
+ # @param msg [Google::Protobuf::Message]
186
+ # @param value [Object]
187
+ def set(msg, value)
188
+ if msg.class.descriptor != Google::Protobuf::FFI.get_containing_message_def(self)
189
+ raise TypeError.new "set method called on wrong message type"
190
+ end
191
+ unless set_value_on_message value, msg.instance_variable_get(:@msg), msg.instance_variable_get(:@arena)
192
+ raise RuntimeError.new "allocation failed"
193
+ end
194
+ nil
195
+ end
196
+
197
+ def map?
198
+ @map ||= Google::Protobuf::FFI.is_map self
199
+ end
200
+
201
+ def required?
202
+ @required ||= Google::Protobuf::FFI.is_required self
203
+ end
204
+
205
+ def repeated?
206
+ @repeated ||= Google::Protobuf::FFI.is_repeated self
207
+ end
208
+
209
+ def sub_message?
210
+ @sub_message ||= Google::Protobuf::FFI.is_sub_message self
211
+ end
212
+
213
+ def wrapper?
214
+ if defined? @wrapper
215
+ @wrapper
216
+ else
217
+ message_descriptor = Google::Protobuf::FFI.get_subtype_as_message(self)
218
+ @wrapper = message_descriptor.nil? ? false : message_descriptor.send(:wrapper?)
219
+ end
220
+ end
221
+
222
+ def options
223
+ @options ||= begin
224
+ size_ptr = ::FFI::MemoryPointer.new(:size_t, 1)
225
+ temporary_arena = Google::Protobuf::FFI.create_arena
226
+ buffer = Google::Protobuf::FFI.field_options(self, size_ptr, temporary_arena)
227
+ opts = Google::Protobuf::FieldOptions.decode(buffer.read_string_length(size_ptr.read(:size_t)).force_encoding("ASCII-8BIT").freeze)
228
+ opts.clear_features()
229
+ opts.freeze
230
+ end
231
+ end
232
+
233
+ def to_proto
234
+ @to_proto ||= begin
235
+ size_ptr = ::FFI::MemoryPointer.new(:size_t, 1)
236
+ temporary_arena = Google::Protobuf::FFI.create_arena
237
+ buffer = Google::Protobuf::FFI.field_to_proto(self, size_ptr, temporary_arena)
238
+ Google::Protobuf::FieldDescriptorProto.decode(buffer.read_string_length(size_ptr.read(:size_t)).force_encoding("ASCII-8BIT").freeze)
239
+ end
240
+ end
241
+
242
+ private
243
+
244
+ def initialize(field_def, descriptor_pool)
245
+ @field_def = field_def
246
+ @descriptor_pool = descriptor_pool
247
+ end
248
+
249
+ def self.private_constructor(field_def, descriptor_pool)
250
+ instance = allocate
251
+ instance.send(:initialize, field_def, descriptor_pool)
252
+ instance
253
+ end
254
+
255
+ # TODO Can this be added to the public API?
256
+ def real_containing_oneof
257
+ @real_containing_oneof ||= Google::Protobuf::FFI.real_containing_oneof self
258
+ end
259
+
260
+ # Implementation details below are subject to breaking changes without
261
+ # warning and are intended for use only within the gem.
262
+
263
+ ##
264
+ # Sets the field this FieldDescriptor represents to the given value on the given message.
265
+ # @param value [Object] Value to be set
266
+ # @param msg [::FFI::Pointer] Pointer the the upb_Message
267
+ # @param arena [Arena] Arena of the message that owns msg
268
+ def set_value_on_message(value, msg, arena, wrap: false)
269
+ message_to_alter = msg
270
+ field_def_to_set = self
271
+ if map?
272
+ raise TypeError.new "Expected map" unless value.is_a? Google::Protobuf::Map
273
+ message_descriptor = subtype
274
+
275
+ key_field_def = Google::Protobuf::FFI.get_field_by_number(message_descriptor, 1)
276
+ key_field_type = Google::Protobuf::FFI.get_type(key_field_def)
277
+ raise TypeError.new "Map key type does not match field's key type" unless key_field_type == value.send(:key_type)
278
+
279
+ value_field_def = Google::Protobuf::FFI.get_field_by_number(message_descriptor, 2)
280
+ value_field_type = Google::Protobuf::FFI.get_type(value_field_def)
281
+ raise TypeError.new "Map value type does not match field's value type" unless value_field_type == value.send(:value_type)
282
+
283
+ raise TypeError.new "Map value type has wrong message/enum class" unless value_field_def.subtype == value.send(:descriptor)
284
+
285
+ arena.fuse(value.send(:arena))
286
+ message_value = Google::Protobuf::FFI::MessageValue.new
287
+ message_value[:map_val] = value.send(:map_ptr)
288
+ elsif repeated?
289
+ raise TypeError.new "Expected repeated field array" unless value.is_a? RepeatedField
290
+ raise TypeError.new "Repeated field array has wrong message/enum class" unless value.send(:type) == type
291
+ arena.fuse(value.send(:arena))
292
+ message_value = Google::Protobuf::FFI::MessageValue.new
293
+ message_value[:array_val] = value.send(:array)
294
+ else
295
+ if value.nil? and (sub_message? or !real_containing_oneof.nil?)
296
+ Google::Protobuf::FFI.clear_message_field message_to_alter, field_def_to_set
297
+ return true
298
+ end
299
+ if wrap
300
+ value_field_def = Google::Protobuf::FFI.get_field_by_number subtype, 1
301
+ type_for_conversion = Google::Protobuf::FFI.get_c_type(value_field_def)
302
+ raise RuntimeError.new "Not expecting to get a msg or enum when unwrapping" if [:enum, :message].include? type_for_conversion
303
+ message_value = convert_ruby_to_upb(value, arena, type_for_conversion, nil)
304
+ message_to_alter = Google::Protobuf::FFI.get_mutable_message(msg, self, arena)[:msg]
305
+ field_def_to_set = value_field_def
306
+ else
307
+ message_value = convert_ruby_to_upb(value, arena, c_type, subtype)
308
+ end
309
+ end
310
+ Google::Protobuf::FFI.set_message_field message_to_alter, field_def_to_set, message_value, arena
311
+ end
312
+
313
+ def c_type
314
+ @c_type ||= Google::Protobuf::FFI.get_c_type(self)
315
+ end
316
+ end
317
+
318
+ class FFI
319
+ # MessageDef
320
+ attach_function :get_field_by_index, :upb_MessageDef_Field, [Descriptor, :int], FieldDescriptor
321
+ attach_function :get_field_by_name, :upb_MessageDef_FindFieldByNameWithSize,[Descriptor, :string, :size_t], FieldDescriptor
322
+ attach_function :get_field_by_number, :upb_MessageDef_FindFieldByNumber, [Descriptor, :uint32_t], FieldDescriptor
323
+
324
+ # FieldDescriptor
325
+ attach_function :field_options, :FieldDescriptor_serialized_options, [FieldDescriptor, :pointer, Internal::Arena], :pointer
326
+ attach_function :get_containing_message_def, :upb_FieldDef_ContainingType, [FieldDescriptor], Descriptor
327
+ attach_function :get_c_type, :upb_FieldDef_CType, [FieldDescriptor], CType
328
+ attach_function :get_default, :upb_FieldDef_Default, [FieldDescriptor], MessageValue.by_value
329
+ attach_function :get_subtype_as_enum, :upb_FieldDef_EnumSubDef, [FieldDescriptor], EnumDescriptor
330
+ attach_function :get_has_presence, :upb_FieldDef_HasPresence, [FieldDescriptor], :bool
331
+ attach_function :get_is_packed, :upb_FieldDef_IsPacked, [FieldDescriptor], :bool
332
+ attach_function :is_map, :upb_FieldDef_IsMap, [FieldDescriptor], :bool
333
+ attach_function :is_required, :upb_FieldDef_IsRequired, [FieldDescriptor], :bool
334
+ attach_function :is_repeated, :upb_FieldDef_IsRepeated, [FieldDescriptor], :bool
335
+ attach_function :is_sub_message, :upb_FieldDef_IsSubMessage, [FieldDescriptor], :bool
336
+ attach_function :get_json_name, :upb_FieldDef_JsonName, [FieldDescriptor], :string
337
+ attach_function :get_label, :upb_FieldDef_Label, [FieldDescriptor], Label
338
+ attach_function :get_subtype_as_message, :upb_FieldDef_MessageSubDef, [FieldDescriptor], Descriptor
339
+ attach_function :get_full_name, :upb_FieldDef_Name, [FieldDescriptor], :string
340
+ attach_function :get_number, :upb_FieldDef_Number, [FieldDescriptor], :uint32_t
341
+ attach_function :get_type, :upb_FieldDef_Type, [FieldDescriptor], FieldType
342
+ attach_function :file_def_by_raw_field_def, :upb_FieldDef_File, [:pointer], :FileDef
343
+ attach_function :field_to_proto, :FieldDescriptor_serialized_to_proto,[FieldDescriptor, :pointer, Internal::Arena], :pointer
344
+ end
345
+ end
346
+ end
@@ -0,0 +1,85 @@
1
+ # Protocol Buffers - Google's data interchange format
2
+ # Copyright 2022 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
+ module Google
9
+ module Protobuf
10
+ class FFI
11
+ # FileDescriptor
12
+ attach_function :file_def_name, :upb_FileDef_Name, [:FileDef], :string
13
+ attach_function :file_def_pool, :upb_FileDef_Pool, [:FileDef], :DefPool
14
+ attach_function :file_options, :FileDescriptor_serialized_options, [:FileDef, :pointer, Internal::Arena], :pointer
15
+ attach_function :file_to_proto, :FileDescriptor_serialized_to_proto, [:FileDef, :pointer, Internal::Arena], :pointer
16
+ end
17
+
18
+ class FileDescriptor
19
+ attr :descriptor_pool, :file_def
20
+
21
+ # FFI Interface methods and setup
22
+ extend ::FFI::DataConverter
23
+ native_type ::FFI::Type::POINTER
24
+
25
+ class << self
26
+ prepend Google::Protobuf::Internal::TypeSafety
27
+ include Google::Protobuf::Internal::PointerHelper
28
+
29
+ # @param value [FileDescriptor] FileDescriptor to convert to an FFI native type
30
+ # @param _ [Object] Unused
31
+ def to_native(value, _)
32
+ file_def_ptr = value.nil? ? nil : value.instance_variable_get(:@file_def)
33
+ return ::FFI::Pointer::NULL if file_def_ptr.nil?
34
+ raise "Underlying file_def was null!" if file_def_ptr.null?
35
+ file_def_ptr
36
+ end
37
+
38
+ ##
39
+ # @param file_def [::FFI::Pointer] FileDef pointer to be wrapped
40
+ # @param _ [Object] Unused
41
+ def from_native(file_def, _ = nil)
42
+ return nil if file_def.nil? or file_def.null?
43
+ descriptor_from_file_def(file_def)
44
+ end
45
+ end
46
+
47
+ def initialize(file_def, descriptor_pool)
48
+ @descriptor_pool = descriptor_pool
49
+ @file_def = file_def
50
+ end
51
+
52
+ def to_s
53
+ inspect
54
+ end
55
+
56
+ def inspect
57
+ "#{self.class.name}: #{name}"
58
+ end
59
+
60
+ def name
61
+ Google::Protobuf::FFI.file_def_name(@file_def)
62
+ end
63
+
64
+ def options
65
+ @options ||= begin
66
+ size_ptr = ::FFI::MemoryPointer.new(:size_t, 1)
67
+ temporary_arena = Google::Protobuf::FFI.create_arena
68
+ buffer = Google::Protobuf::FFI.file_options(@file_def, size_ptr, temporary_arena)
69
+ opts = Google::Protobuf::FileOptions.decode(buffer.read_string_length(size_ptr.read(:size_t)).force_encoding("ASCII-8BIT").freeze)
70
+ opts.clear_features()
71
+ opts.freeze
72
+ end
73
+ end
74
+
75
+ def to_proto
76
+ @to_proto ||= begin
77
+ size_ptr = ::FFI::MemoryPointer.new(:size_t, 1)
78
+ temporary_arena = Google::Protobuf::FFI.create_arena
79
+ buffer = Google::Protobuf::FFI.file_to_proto(@file_def, size_ptr, temporary_arena)
80
+ Google::Protobuf::FileDescriptorProto.decode(buffer.read_string_length(size_ptr.read(:size_t)).force_encoding("ASCII-8BIT").freeze)
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,60 @@
1
+ # Protocol Buffers - Google's data interchange format
2
+ # Copyright 2022 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
+ # Implementation details below are subject to breaking changes without
10
+ # warning and are intended for use only within the gem.
11
+ module Google
12
+ module Protobuf
13
+ module Internal
14
+ class Arena
15
+ # FFI Interface methods and setup
16
+ extend ::FFI::DataConverter
17
+ native_type ::FFI::Type::POINTER
18
+
19
+ class << self
20
+ prepend Google::Protobuf::Internal::TypeSafety
21
+
22
+ # @param value [Arena] Arena to convert to an FFI native type
23
+ # @param _ [Object] Unused
24
+ def to_native(value, _)
25
+ value.instance_variable_get(:@arena) || ::FFI::Pointer::NULL
26
+ end
27
+
28
+ ##
29
+ # @param value [::FFI::Pointer] Arena pointer to be wrapped
30
+ # @param _ [Object] Unused
31
+ def from_native(value, _)
32
+ new(value)
33
+ end
34
+ end
35
+
36
+ def initialize(pointer)
37
+ @arena = ::FFI::AutoPointer.new(pointer, Google::Protobuf::FFI.method(:free_arena))
38
+ @pinned_messages = []
39
+ end
40
+
41
+ def fuse(other_arena)
42
+ return if other_arena == self
43
+ unless Google::Protobuf::FFI.fuse_arena(self, other_arena)
44
+ raise RuntimeError.new "Unable to fuse arenas. This should never happen since Ruby does not use initial blocks"
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ class FFI
51
+ # Arena
52
+ attach_function :create_arena, :Arena_create, [], Internal::Arena
53
+ attach_function :fuse_arena, :upb_Arena_Fuse, [Internal::Arena, Internal::Arena], :bool
54
+ # Argument takes a :pointer rather than a typed Arena here due to
55
+ # implementation details of FFI::AutoPointer.
56
+ attach_function :free_arena, :upb_Arena_Free, [:pointer], :void
57
+ attach_function :arena_malloc, :upb_Arena_Malloc, [Internal::Arena, :size_t], :pointer
58
+ end
59
+ end
60
+ end