google-protobuf 3.21.0 → 4.28.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 (71) hide show
  1. checksums.yaml +4 -4
  2. data/ext/google/protobuf_c/Rakefile +3 -0
  3. data/ext/google/protobuf_c/convert.c +67 -86
  4. data/ext/google/protobuf_c/convert.h +3 -28
  5. data/ext/google/protobuf_c/defs.c +537 -75
  6. data/ext/google/protobuf_c/defs.h +3 -28
  7. data/ext/google/protobuf_c/extconf.rb +4 -4
  8. data/ext/google/protobuf_c/glue.c +72 -0
  9. data/ext/google/protobuf_c/map.c +114 -85
  10. data/ext/google/protobuf_c/map.h +12 -30
  11. data/ext/google/protobuf_c/message.c +250 -234
  12. data/ext/google/protobuf_c/message.h +11 -33
  13. data/ext/google/protobuf_c/protobuf.c +63 -185
  14. data/ext/google/protobuf_c/protobuf.h +27 -39
  15. data/ext/google/protobuf_c/repeated_field.c +72 -38
  16. data/ext/google/protobuf_c/repeated_field.h +11 -29
  17. data/ext/google/protobuf_c/ruby-upb.c +14513 -9003
  18. data/ext/google/protobuf_c/ruby-upb.h +13990 -4496
  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 +1 -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 +20 -7
  26. data/ext/google/protobuf_c/wrap_memcpy.c +3 -26
  27. data/lib/google/protobuf/any_pb.rb +6 -8
  28. data/lib/google/protobuf/api_pb.rb +6 -26
  29. data/lib/google/protobuf/descriptor_pb.rb +23 -226
  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 +165 -0
  33. data/lib/google/protobuf/ffi/descriptor_pool.rb +77 -0
  34. data/lib/google/protobuf/ffi/enum_descriptor.rb +173 -0
  35. data/lib/google/protobuf/ffi/ffi.rb +210 -0
  36. data/lib/google/protobuf/ffi/field_descriptor.rb +330 -0
  37. data/lib/google/protobuf/ffi/file_descriptor.rb +49 -0
  38. data/lib/google/protobuf/ffi/internal/arena.rb +60 -0
  39. data/lib/google/protobuf/ffi/internal/convert.rb +296 -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 +433 -0
  43. data/lib/google/protobuf/ffi/message.rb +781 -0
  44. data/lib/google/protobuf/ffi/method_descriptor.rb +114 -0
  45. data/lib/google/protobuf/ffi/object_cache.rb +30 -0
  46. data/lib/google/protobuf/ffi/oneof_descriptor.rb +97 -0
  47. data/lib/google/protobuf/ffi/repeated_field.rb +411 -0
  48. data/lib/google/protobuf/ffi/service_descriptor.rb +107 -0
  49. data/lib/google/protobuf/field_mask_pb.rb +6 -7
  50. data/lib/google/protobuf/internal/object_cache.rb +99 -0
  51. data/lib/google/protobuf/message_exts.rb +8 -26
  52. data/lib/google/protobuf/plugin_pb.rb +25 -0
  53. data/lib/google/protobuf/repeated_field.rb +7 -31
  54. data/lib/google/protobuf/source_context_pb.rb +6 -7
  55. data/lib/google/protobuf/struct_pb.rb +6 -23
  56. data/lib/google/protobuf/timestamp_pb.rb +6 -8
  57. data/lib/google/protobuf/type_pb.rb +6 -71
  58. data/lib/google/protobuf/well_known_types.rb +5 -34
  59. data/lib/google/protobuf/wrappers_pb.rb +6 -31
  60. data/lib/google/protobuf.rb +27 -45
  61. data/lib/google/protobuf_ffi.rb +51 -0
  62. data/lib/google/protobuf_native.rb +19 -0
  63. data/lib/google/tasks/ffi.rake +100 -0
  64. metadata +92 -16
  65. data/ext/google/protobuf_c/third_party/utf8_range/naive.c +0 -92
  66. data/ext/google/protobuf_c/third_party/utf8_range/range2-neon.c +0 -157
  67. data/ext/google/protobuf_c/third_party/utf8_range/range2-sse.c +0 -170
  68. data/lib/google/protobuf/descriptor_dsl.rb +0 -465
  69. data/tests/basic.rb +0 -739
  70. data/tests/generated_code_test.rb +0 -23
  71. data/tests/stress.rb +0 -38
@@ -0,0 +1,114 @@
1
+ # Protocol Buffers - Google's data interchange format
2
+ # Copyright 2024 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 MethodDescriptor
11
+ attr :method_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 [MethodDescriptor] MethodDescriptor to convert to an FFI native type
24
+ # @param _ [Object] Unused
25
+ def to_native(value, _)
26
+ method_def_ptr = value.nil? ? nil : value.instance_variable_get(:@method_def)
27
+ return ::FFI::Pointer::NULL if method_def_ptr.nil?
28
+ raise "Underlying method_def was null!" if method_def_ptr.null?
29
+ method_def_ptr
30
+ end
31
+
32
+ ##
33
+ # @param service_def [::FFI::Pointer] MethodDef pointer to be wrapped
34
+ # @param _ [Object] Unused
35
+ def from_native(method_def, _ = nil)
36
+ return nil if method_def.nil? or method_def.null?
37
+ service_def = Google::Protobuf::FFI.raw_service_def_by_raw_method_def(method_def)
38
+ file_def = Google::Protobuf::FFI.file_def_by_raw_service_def(service_def)
39
+ descriptor_from_file_def(file_def, method_def)
40
+ end
41
+ end
42
+
43
+ def self.new(*arguments, &block)
44
+ raise "Descriptor objects may not be created from Ruby."
45
+ end
46
+
47
+ def to_s
48
+ inspect
49
+ end
50
+
51
+ def inspect
52
+ "#{self.class.name}: #{name}"
53
+ end
54
+
55
+ def name
56
+ @name ||= Google::Protobuf::FFI.get_method_name(self)
57
+ end
58
+
59
+ def options
60
+ @options ||= begin
61
+ size_ptr = ::FFI::MemoryPointer.new(:size_t, 1)
62
+ temporary_arena = Google::Protobuf::FFI.create_arena
63
+ buffer = Google::Protobuf::FFI.method_options(self, size_ptr, temporary_arena)
64
+ Google::Protobuf::MethodOptions.decode(buffer.read_string_length(size_ptr.read(:size_t)).force_encoding("ASCII-8BIT").freeze).freeze
65
+ end
66
+ end
67
+
68
+ def input_type
69
+ @input_type ||= Google::Protobuf::FFI.method_input_type(self)
70
+ end
71
+
72
+ def output_type
73
+ @output_type ||= Google::Protobuf::FFI.method_output_type(self)
74
+ end
75
+
76
+ def client_streaming
77
+ @client_streaming ||= Google::Protobuf::FFI.method_client_streaming(self)
78
+ end
79
+
80
+ def server_streaming
81
+ @server_streaming ||= Google::Protobuf::FFI.method_server_streaming(self)
82
+ end
83
+
84
+ private
85
+
86
+ def initialize(method_def, descriptor_pool)
87
+ @method_def = method_def
88
+ @descriptor_pool = descriptor_pool
89
+ end
90
+
91
+ def self.private_constructor(method_def, descriptor_pool)
92
+ instance = allocate
93
+ instance.send(:initialize, method_def, descriptor_pool)
94
+ instance
95
+ end
96
+
97
+ def c_type
98
+ @c_type ||= Google::Protobuf::FFI.get_c_type(self)
99
+ end
100
+ end
101
+
102
+ class FFI
103
+ # MethodDef
104
+ attach_function :raw_service_def_by_raw_method_def, :upb_MethodDef_Service, [:pointer], :pointer
105
+ attach_function :get_method_name, :upb_MethodDef_Name, [MethodDescriptor], :string
106
+ attach_function :method_options, :MethodDescriptor_serialized_options, [MethodDescriptor, :pointer, Internal::Arena], :pointer
107
+ attach_function :method_input_type, :upb_MethodDef_InputType, [MethodDescriptor], Descriptor
108
+ attach_function :method_output_type, :upb_MethodDef_OutputType, [MethodDescriptor], Descriptor
109
+ attach_function :method_client_streaming, :upb_MethodDef_ClientStreaming, [MethodDescriptor], :bool
110
+ attach_function :method_server_streaming, :upb_MethodDef_ServerStreaming, [MethodDescriptor], :bool
111
+ end
112
+ end
113
+ end
114
+
@@ -0,0 +1,30 @@
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
+ private
11
+
12
+ SIZEOF_LONG = ::FFI::MemoryPointer.new(:long).size
13
+ SIZEOF_VALUE = ::FFI::Pointer::SIZE
14
+
15
+ def self.interpreter_supports_non_finalized_keys_in_weak_map?
16
+ ! defined? JRUBY_VERSION
17
+ end
18
+
19
+ def self.cache_implementation
20
+ if interpreter_supports_non_finalized_keys_in_weak_map? and SIZEOF_LONG >= SIZEOF_VALUE
21
+ Google::Protobuf::Internal::ObjectCache
22
+ else
23
+ Google::Protobuf::Internal::LegacyObjectCache
24
+ end
25
+ end
26
+
27
+ public
28
+ OBJECT_CACHE = cache_implementation.new
29
+ end
30
+ end
@@ -0,0 +1,97 @@
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 OneofDescriptor
11
+ attr :descriptor_pool, :oneof_def
12
+ include Enumerable
13
+
14
+ # FFI Interface methods and setup
15
+ extend ::FFI::DataConverter
16
+ native_type ::FFI::Type::POINTER
17
+
18
+ class << self
19
+ prepend Google::Protobuf::Internal::TypeSafety
20
+ include Google::Protobuf::Internal::PointerHelper
21
+
22
+ # @param value [OneofDescriptor] FieldDescriptor to convert to an FFI native type
23
+ # @param _ [Object] Unused
24
+ def to_native(value, _ = nil)
25
+ value.instance_variable_get(:@oneof_def) || ::FFI::Pointer::NULL
26
+ end
27
+
28
+ ##
29
+ # @param oneof_def [::FFI::Pointer] OneofDef pointer to be wrapped
30
+ # @param _ [Object] Unused
31
+ def from_native(oneof_def, _ = nil)
32
+ return nil if oneof_def.nil? or oneof_def.null?
33
+ message_descriptor = Google::Protobuf::FFI.get_oneof_containing_type oneof_def
34
+ raise RuntimeError.new "Message Descriptor is nil" if message_descriptor.nil?
35
+ file_def = Google::Protobuf::FFI.get_message_file_def message_descriptor.to_native
36
+ descriptor_from_file_def(file_def, oneof_def)
37
+ end
38
+ end
39
+
40
+ def self.new(*arguments, &block)
41
+ raise "OneofDescriptor objects may not be created from Ruby."
42
+ end
43
+
44
+ def name
45
+ Google::Protobuf::FFI.get_oneof_name(self)
46
+ end
47
+
48
+ def each &block
49
+ n = Google::Protobuf::FFI.get_oneof_field_count(self)
50
+ 0.upto(n-1) do |i|
51
+ yield(Google::Protobuf::FFI.get_oneof_field_by_index(self, i))
52
+ end
53
+ nil
54
+ end
55
+
56
+ def options
57
+ @options ||= begin
58
+ size_ptr = ::FFI::MemoryPointer.new(:size_t, 1)
59
+ temporary_arena = Google::Protobuf::FFI.create_arena
60
+ buffer = Google::Protobuf::FFI.oneof_options(self, size_ptr, temporary_arena)
61
+ opts = Google::Protobuf::OneofOptions.decode(buffer.read_string_length(size_ptr.read(:size_t)).force_encoding("ASCII-8BIT").freeze)
62
+ opts.clear_features()
63
+ opts.freeze
64
+ end
65
+ end
66
+
67
+ private
68
+
69
+ def initialize(oneof_def, descriptor_pool)
70
+ @descriptor_pool = descriptor_pool
71
+ @oneof_def = oneof_def
72
+ end
73
+
74
+ def self.private_constructor(oneof_def, descriptor_pool)
75
+ instance = allocate
76
+ instance.send(:initialize, oneof_def, descriptor_pool)
77
+ instance
78
+ end
79
+ end
80
+
81
+ class FFI
82
+ # MessageDef
83
+ attach_function :get_oneof_by_name, :upb_MessageDef_FindOneofByNameWithSize, [Descriptor, :string, :size_t], OneofDescriptor
84
+ attach_function :get_oneof_by_index, :upb_MessageDef_Oneof, [Descriptor, :int], OneofDescriptor
85
+
86
+ # OneofDescriptor
87
+ attach_function :get_oneof_name, :upb_OneofDef_Name, [OneofDescriptor], :string
88
+ attach_function :get_oneof_field_count, :upb_OneofDef_FieldCount, [OneofDescriptor], :int
89
+ attach_function :get_oneof_field_by_index, :upb_OneofDef_Field, [OneofDescriptor, :int], FieldDescriptor
90
+ attach_function :get_oneof_containing_type,:upb_OneofDef_ContainingType, [:pointer], Descriptor
91
+ attach_function :oneof_options, :OneOfDescriptor_serialized_options, [OneofDescriptor, :pointer, Internal::Arena], :pointer
92
+
93
+ # FieldDescriptor
94
+ attach_function :real_containing_oneof, :upb_FieldDef_RealContainingOneof, [FieldDescriptor], OneofDescriptor
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,411 @@
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
+ # This class makes RepeatedField act (almost-) like a Ruby Array.
10
+ # It has convenience methods that extend the core C or Java based
11
+ # methods.
12
+ #
13
+ # This is a best-effort to mirror Array behavior. Two comments:
14
+ # 1) patches always welcome :)
15
+ # 2) if performance is an issue, feel free to rewrite the method
16
+ # in C. The source code has plenty of examples
17
+ #
18
+ # KNOWN ISSUES
19
+ # - #[]= doesn't allow less used approaches such as `arr[1, 2] = 'fizz'`
20
+ # - #concat should return the orig array
21
+ # - #push should accept multiple arguments and push them all at the same time
22
+ #
23
+ module Google
24
+ module Protobuf
25
+ class FFI
26
+ # Array
27
+ attach_function :append_array, :upb_Array_Append, [:Array, MessageValue.by_value, Internal::Arena], :bool
28
+ attach_function :get_msgval_at, :upb_Array_Get, [:Array, :size_t], MessageValue.by_value
29
+ attach_function :create_array, :upb_Array_New, [Internal::Arena, CType], :Array
30
+ attach_function :array_resize, :upb_Array_Resize, [:Array, :size_t, Internal::Arena], :bool
31
+ attach_function :array_set, :upb_Array_Set, [:Array, :size_t, MessageValue.by_value], :void
32
+ attach_function :array_size, :upb_Array_Size, [:Array], :size_t
33
+ attach_function :array_freeze, :upb_Array_Freeze, [:Array, MiniTable.by_ref], :void
34
+ attach_function :array_frozen?, :upb_Array_IsFrozen, [:Array], :bool
35
+ end
36
+
37
+ class RepeatedField
38
+ include Enumerable
39
+
40
+ ##
41
+ # call-seq:
42
+ # RepeatedField.new(type, type_class = nil, initial_values = [])
43
+ #
44
+ # Creates a new repeated field. The provided type must be a Ruby symbol, and
45
+ # an take on the same values as those accepted by FieldDescriptor#type=. If
46
+ # the type is :message or :enum, type_class must be non-nil, and must be the
47
+ # Ruby class or module returned by Descriptor#msgclass or
48
+ # EnumDescriptor#enummodule, respectively. An initial list of elements may also
49
+ # be provided.
50
+ def self.new(type, type_class = nil, initial_values = [])
51
+ instance = allocate
52
+ # TODO This argument mangling doesn't agree with the type signature in the comments
53
+ # but is required to make unit tests pass;
54
+ if type_class.is_a?(Enumerable) and initial_values.empty? and ![:enum, :message].include?(type)
55
+ initial_values = type_class
56
+ type_class = nil
57
+ end
58
+ instance.send(:initialize, type, type_class: type_class, initial_values: initial_values)
59
+ instance
60
+ end
61
+
62
+ ##
63
+ # call-seq:
64
+ # RepeatedField.each(&block)
65
+ #
66
+ # Invokes the block once for each element of the repeated field. RepeatedField
67
+ # also includes Enumerable; combined with this method, the repeated field thus
68
+ # acts like an ordinary Ruby sequence.
69
+ def each &block
70
+ each_msg_val do |element|
71
+ yield(convert_upb_to_ruby(element, type, descriptor, arena))
72
+ end
73
+ self
74
+ end
75
+
76
+ def [](*args)
77
+ count = length
78
+ if args.size < 1
79
+ raise ArgumentError.new "Index or range is a required argument."
80
+ end
81
+ if args[0].is_a? Range
82
+ if args.size > 1
83
+ raise ArgumentError.new "Expected 1 when passing Range argument, but got #{args.size}"
84
+ end
85
+ range = args[0]
86
+ # Handle begin-less and/or endless ranges, when supported.
87
+ index_of_first = range.respond_to?(:begin) ? range.begin : range.last
88
+ index_of_first = 0 if index_of_first.nil?
89
+ end_of_range = range.respond_to?(:end) ? range.end : range.last
90
+ index_of_last = end_of_range.nil? ? -1 : end_of_range
91
+
92
+ if index_of_last < 0
93
+ index_of_last += count
94
+ end
95
+ unless range.exclude_end? and !end_of_range.nil?
96
+ index_of_last += 1
97
+ end
98
+ index_of_first += count if index_of_first < 0
99
+ length = index_of_last - index_of_first
100
+ return [] if length.zero?
101
+ elsif args[0].is_a? Integer
102
+ index_of_first = args[0]
103
+ index_of_first += count if index_of_first < 0
104
+ if args.size > 2
105
+ raise ArgumentError.new "Expected 1 or 2 arguments, but got #{args.size}"
106
+ end
107
+ if args.size == 1 # No length specified, return one element
108
+ if array.null? or index_of_first < 0 or index_of_first >= count
109
+ return nil
110
+ else
111
+ return convert_upb_to_ruby(Google::Protobuf::FFI.get_msgval_at(array, index_of_first), type, descriptor, arena)
112
+ end
113
+ else
114
+ length = [args[1],count].min
115
+ end
116
+ else
117
+ raise NotImplementedError
118
+ end
119
+
120
+ if array.null? or index_of_first < 0 or index_of_first >= count
121
+ nil
122
+ else
123
+ if index_of_first + length > count
124
+ length = count - index_of_first
125
+ end
126
+ if length < 0
127
+ nil
128
+ else
129
+ subarray(index_of_first, length)
130
+ end
131
+ end
132
+ end
133
+ alias at []
134
+
135
+
136
+ def []=(index, value)
137
+ raise FrozenError if frozen?
138
+ count = length
139
+ index += count if index < 0
140
+ return nil if index < 0
141
+ if index >= count
142
+ resize(index+1)
143
+ empty_message_value = Google::Protobuf::FFI::MessageValue.new # Implicitly clear
144
+ count.upto(index-1) do |i|
145
+ Google::Protobuf::FFI.array_set(array, i, empty_message_value)
146
+ end
147
+ end
148
+ Google::Protobuf::FFI.array_set(array, index, convert_ruby_to_upb(value, arena, type, descriptor))
149
+ nil
150
+ end
151
+
152
+ def push(*elements)
153
+ raise FrozenError if frozen?
154
+ internal_push(*elements)
155
+ end
156
+
157
+ def <<(element)
158
+ raise FrozenError if frozen?
159
+ push element
160
+ end
161
+
162
+ def replace(replacements)
163
+ raise FrozenError if frozen?
164
+ clear
165
+ push(*replacements)
166
+ end
167
+
168
+ def clear
169
+ raise FrozenError if frozen?
170
+ resize 0
171
+ self
172
+ end
173
+
174
+ def length
175
+ array.null? ? 0 : Google::Protobuf::FFI.array_size(array)
176
+ end
177
+ alias size :length
178
+
179
+ ##
180
+ # Is this object frozen?
181
+ # Returns true if either this Ruby wrapper or the underlying
182
+ # representation are frozen. Freezes the wrapper if the underlying
183
+ # representation is already frozen but this wrapper isn't.
184
+ def frozen?
185
+ unless Google::Protobuf::FFI.array_frozen? array
186
+ raise RuntimeError.new "Ruby frozen RepeatedField with mutable representation" if super
187
+ return false
188
+ end
189
+ method(:freeze).super_method.call unless super
190
+ true
191
+ end
192
+
193
+ ##
194
+ # Freezes the RepeatedField object. We have to intercept this so we can
195
+ # freeze the underlying representation, not just the Ruby wrapper. Returns
196
+ # self.
197
+ def freeze
198
+ if method(:frozen?).super_method.call
199
+ unless Google::Protobuf::FFI.array_frozen? array
200
+ raise RuntimeError.new "Underlying representation of repeated field still mutable despite frozen wrapper"
201
+ end
202
+ return self
203
+ end
204
+ unless Google::Protobuf::FFI.array_frozen? array
205
+ mini_table = (type == :message) ? Google::Protobuf::FFI.get_mini_table(@descriptor) : nil
206
+ Google::Protobuf::FFI.array_freeze(array, mini_table)
207
+ end
208
+ super
209
+ end
210
+
211
+ def dup
212
+ instance = self.class.allocate
213
+ instance.send(:initialize, type, descriptor: descriptor, arena: arena)
214
+ each_msg_val do |element|
215
+ instance.send(:append_msg_val, element)
216
+ end
217
+ instance
218
+ end
219
+ alias clone dup
220
+
221
+ def ==(other)
222
+ return true if other.object_id == object_id
223
+ if other.is_a? RepeatedField
224
+ return false unless other.length == length
225
+ each_msg_val_with_index do |msg_val, i|
226
+ other_msg_val = Google::Protobuf::FFI.get_msgval_at(other.send(:array), i)
227
+ unless Google::Protobuf::FFI.message_value_equal(msg_val, other_msg_val, type, descriptor)
228
+ return false
229
+ end
230
+ end
231
+ return true
232
+ elsif other.is_a? Enumerable
233
+ return to_ary == other.to_a
234
+ end
235
+ false
236
+ end
237
+
238
+ ##
239
+ # call-seq:
240
+ # RepeatedField.to_ary => array
241
+ #
242
+ # Used when converted implicitly into array, e.g. compared to an Array.
243
+ # Also called as a fallback of Object#to_a
244
+ def to_ary
245
+ return_value = []
246
+ each do |element|
247
+ return_value << element
248
+ end
249
+ return_value
250
+ end
251
+
252
+ def hash
253
+ return_value = 0
254
+ each_msg_val do |msg_val|
255
+ return_value = Google::Protobuf::FFI.message_value_hash(msg_val, type, descriptor, return_value)
256
+ end
257
+ return_value
258
+ end
259
+
260
+ def +(other)
261
+ if other.is_a? RepeatedField
262
+ if type != other.instance_variable_get(:@type) or descriptor != other.instance_variable_get(:@descriptor)
263
+ raise ArgumentError.new "Attempt to append RepeatedField with different element type."
264
+ end
265
+ fuse_arena(other.send(:arena))
266
+ super_set = dup
267
+ other.send(:each_msg_val) do |msg_val|
268
+ super_set.send(:append_msg_val, msg_val)
269
+ end
270
+ super_set
271
+ elsif other.is_a? Enumerable
272
+ super_set = dup
273
+ super_set.push(*other.to_a)
274
+ else
275
+ raise ArgumentError.new "Unknown type appending to RepeatedField"
276
+ end
277
+ end
278
+
279
+ def concat(other)
280
+ raise ArgumentError.new "Expected Enumerable, but got #{other.class}" unless other.is_a? Enumerable
281
+ push(*other.to_a)
282
+ end
283
+
284
+ private
285
+ include Google::Protobuf::Internal::Convert
286
+
287
+ attr :name, :arena, :array, :type, :descriptor
288
+
289
+ def internal_push(*elements)
290
+ elements.each do |element|
291
+ append_msg_val convert_ruby_to_upb(element, arena, type, descriptor)
292
+ end
293
+ self
294
+ end
295
+
296
+ def pop_one
297
+ raise FrozenError if frozen?
298
+ count = length
299
+ return nil if length.zero?
300
+ last_element = Google::Protobuf::FFI.get_msgval_at(array, count-1)
301
+ return_value = convert_upb_to_ruby(last_element, type, descriptor, arena)
302
+ resize(count-1)
303
+ return_value
304
+ end
305
+
306
+ def subarray(start, length)
307
+ return_result = []
308
+ (start..(start + length - 1)).each do |i|
309
+ element = Google::Protobuf::FFI.get_msgval_at(array, i)
310
+ return_result << convert_upb_to_ruby(element, type, descriptor, arena)
311
+ end
312
+ return_result
313
+ end
314
+
315
+ def each_msg_val_with_index &block
316
+ n = array.null? ? 0 : Google::Protobuf::FFI.array_size(array)
317
+ 0.upto(n-1) do |i|
318
+ yield Google::Protobuf::FFI.get_msgval_at(array, i), i
319
+ end
320
+ end
321
+
322
+ def each_msg_val &block
323
+ each_msg_val_with_index do |msg_val, _|
324
+ yield msg_val
325
+ end
326
+ end
327
+
328
+ # @param msg_val [Google::Protobuf::FFI::MessageValue] Value to append
329
+ def append_msg_val(msg_val)
330
+ unless Google::Protobuf::FFI.append_array(array, msg_val, arena)
331
+ raise NoMemoryError.new "Could not allocate room for #{msg_val} in Arena"
332
+ end
333
+ end
334
+
335
+ # @param new_size [Integer] New size of the array
336
+ def resize(new_size)
337
+ unless Google::Protobuf::FFI.array_resize(array, new_size, arena)
338
+ raise NoMemoryError.new "Array resize to #{new_size} failed!"
339
+ end
340
+ end
341
+
342
+ def initialize(type, type_class: nil, initial_values: nil, name: nil, arena: nil, array: nil, descriptor: nil)
343
+ @name = name || 'RepeatedField'
344
+ raise ArgumentError.new "Expected argument type to be a Symbol" unless type.is_a? Symbol
345
+ field_number = Google::Protobuf::FFI::FieldType[type]
346
+ raise ArgumentError.new "Unsupported type '#{type}'" if field_number.nil?
347
+ if !descriptor.nil?
348
+ @descriptor = descriptor
349
+ elsif [:message, :enum].include? type
350
+ raise ArgumentError.new "Expected at least 2 arguments for message/enum." if type_class.nil?
351
+ descriptor = type_class.respond_to?(:descriptor) ? type_class.descriptor : nil
352
+ raise ArgumentError.new "Type class #{type_class} has no descriptor. Please pass a class or enum as returned by the DescriptorPool." if descriptor.nil?
353
+ @descriptor = descriptor
354
+ else
355
+ @descriptor = nil
356
+ end
357
+ @type = type
358
+
359
+ @arena = arena || Google::Protobuf::FFI.create_arena
360
+ @array = array || Google::Protobuf::FFI.create_array(@arena, @type)
361
+ unless initial_values.nil?
362
+ unless initial_values.is_a? Enumerable
363
+ raise ArgumentError.new "Expected array as initializer value for repeated field '#{name}' (given #{initial_values.class})."
364
+ end
365
+ internal_push(*initial_values)
366
+ end
367
+
368
+ # Should always be the last expression of the initializer to avoid
369
+ # leaking references to this object before construction is complete.
370
+ OBJECT_CACHE.try_add(@array.address, self)
371
+ end
372
+
373
+ ##
374
+ # Constructor that uses the type information from the given
375
+ # FieldDescriptor to configure the new RepeatedField instance.
376
+ # @param field [FieldDescriptor] Type information for the new RepeatedField
377
+ # @param arena [Arena] Owning message's arena
378
+ # @param values [Enumerable] Initial values
379
+ # @param array [::FFI::Pointer] Existing upb_Array
380
+ def self.construct_for_field(field, arena: nil, values: nil, array: nil)
381
+ instance = allocate
382
+ options = {initial_values: values, name: field.name, arena: arena, array: array}
383
+ if [:enum, :message].include? field.type
384
+ options[:descriptor] = field.subtype
385
+ end
386
+ instance.send(:initialize, field.type, **options)
387
+ instance
388
+ end
389
+
390
+ def fuse_arena(arena)
391
+ arena.fuse(arena)
392
+ end
393
+
394
+ extend Google::Protobuf::Internal::Convert
395
+
396
+ def self.deep_copy(repeated_field)
397
+ instance = allocate
398
+ instance.send(:initialize, repeated_field.send(:type), descriptor: repeated_field.send(:descriptor))
399
+ instance.send(:resize, repeated_field.length)
400
+ new_array = instance.send(:array)
401
+ repeated_field.send(:each_msg_val_with_index) do |element, i|
402
+ Google::Protobuf::FFI.array_set(new_array, i, message_value_deep_copy(element, repeated_field.send(:type), repeated_field.send(:descriptor), instance.send(:arena)))
403
+ end
404
+ instance
405
+ end
406
+
407
+ end
408
+ end
409
+ end
410
+
411
+ require 'google/protobuf/repeated_field'