google-protobuf 3.21.0 → 4.28.0

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.

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,77 @@
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
+ # DefPool
12
+ attach_function :add_serialized_file, :upb_DefPool_AddFile, [:DefPool, :FileDescriptorProto, Status.by_ref], :FileDef
13
+ attach_function :free_descriptor_pool, :upb_DefPool_Free, [:DefPool], :void
14
+ attach_function :create_descriptor_pool,:upb_DefPool_New, [], :DefPool
15
+ attach_function :get_extension_registry,:upb_DefPool_ExtensionRegistry, [:DefPool], :ExtensionRegistry
16
+ attach_function :lookup_enum, :upb_DefPool_FindEnumByName, [:DefPool, :string], EnumDescriptor
17
+ attach_function :lookup_extension, :upb_DefPool_FindExtensionByName,[:DefPool, :string], FieldDescriptor
18
+ attach_function :lookup_msg, :upb_DefPool_FindMessageByName, [:DefPool, :string], Descriptor
19
+ attach_function :lookup_service, :upb_DefPool_FindServiceByName, [:DefPool, :string], ServiceDescriptor
20
+
21
+ # FileDescriptorProto
22
+ attach_function :parse, :FileDescriptorProto_parse, [:binary_string, :size_t, Internal::Arena], :FileDescriptorProto
23
+ end
24
+ class DescriptorPool
25
+ attr :descriptor_pool
26
+ attr_accessor :descriptor_class_by_def
27
+
28
+ def initialize
29
+ @descriptor_pool = ::FFI::AutoPointer.new(Google::Protobuf::FFI.create_descriptor_pool, Google::Protobuf::FFI.method(:free_descriptor_pool))
30
+ @descriptor_class_by_def = {}
31
+
32
+ # Should always be the last expression of the initializer to avoid
33
+ # leaking references to this object before construction is complete.
34
+ Google::Protobuf::OBJECT_CACHE.try_add @descriptor_pool.address, self
35
+ end
36
+
37
+ def add_serialized_file(file_contents)
38
+ # Allocate memory sized to file_contents
39
+ memBuf = ::FFI::MemoryPointer.new(:char, file_contents.bytesize)
40
+ # Insert the data
41
+ memBuf.put_bytes(0, file_contents)
42
+ temporary_arena = Google::Protobuf::FFI.create_arena
43
+ file_descriptor_proto = Google::Protobuf::FFI.parse memBuf, file_contents.bytesize, temporary_arena
44
+ raise ArgumentError.new("Unable to parse FileDescriptorProto") if file_descriptor_proto.null?
45
+
46
+ status = Google::Protobuf::FFI::Status.new
47
+ file_descriptor = Google::Protobuf::FFI.add_serialized_file @descriptor_pool, file_descriptor_proto, status
48
+ if file_descriptor.null?
49
+ raise TypeError.new("Unable to build file to DescriptorPool: #{Google::Protobuf::FFI.error_message(status)}")
50
+ else
51
+ @descriptor_class_by_def[file_descriptor.address] = FileDescriptor.new file_descriptor, self
52
+ end
53
+ end
54
+
55
+ def lookup name
56
+ Google::Protobuf::FFI.lookup_msg(@descriptor_pool, name) ||
57
+ Google::Protobuf::FFI.lookup_enum(@descriptor_pool, name) ||
58
+ Google::Protobuf::FFI.lookup_extension(@descriptor_pool, name) ||
59
+ Google::Protobuf::FFI.lookup_service(@descriptor_pool, name)
60
+ end
61
+
62
+ def self.generated_pool
63
+ @@generated_pool ||= DescriptorPool.new
64
+ end
65
+
66
+ private
67
+
68
+ # Implementation details below are subject to breaking changes without
69
+ # warning and are intended for use only within the gem.
70
+
71
+ def get_file_descriptor file_def
72
+ return nil if file_def.null?
73
+ @descriptor_class_by_def[file_def.address] ||= FileDescriptor.new(file_def, self)
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,173 @@
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 EnumDescriptor
11
+ attr :descriptor_pool, :enum_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 [EnumDescriptor] EnumDescriptor to convert to an FFI native type
23
+ # @param _ [Object] Unused
24
+ def to_native(value, _)
25
+ value.instance_variable_get(:@enum_def) || ::FFI::Pointer::NULL
26
+ end
27
+
28
+ ##
29
+ # @param enum_def [::FFI::Pointer] EnumDef pointer to be wrapped
30
+ # @param _ [Object] Unused
31
+ def from_native(enum_def, _)
32
+ return nil if enum_def.nil? or enum_def.null?
33
+ file_def = Google::Protobuf::FFI.get_message_file_def enum_def
34
+ descriptor_from_file_def(file_def, enum_def)
35
+ end
36
+ end
37
+
38
+ def self.new(*arguments, &block)
39
+ raise "Descriptor objects may not be created from Ruby."
40
+ end
41
+
42
+ def file_descriptor
43
+ @descriptor_pool.send(:get_file_descriptor, Google::Protobuf::FFI.get_enum_file_descriptor(self))
44
+ end
45
+
46
+ def name
47
+ Google::Protobuf::FFI.get_enum_fullname(self)
48
+ end
49
+
50
+ def to_s
51
+ inspect
52
+ end
53
+
54
+ def inspect
55
+ "#{self.class.name}: #{name}"
56
+ end
57
+
58
+ def lookup_name(name)
59
+ self.class.send(:lookup_name, self, name)
60
+ end
61
+
62
+ def lookup_value(number)
63
+ self.class.send(:lookup_value, self, number)
64
+ end
65
+
66
+ def each &block
67
+ n = Google::Protobuf::FFI.enum_value_count(self)
68
+ 0.upto(n - 1) do |i|
69
+ enum_value = Google::Protobuf::FFI.enum_value_by_index(self, i)
70
+ yield(Google::Protobuf::FFI.enum_name(enum_value).to_sym, Google::Protobuf::FFI.enum_number(enum_value))
71
+ end
72
+ nil
73
+ end
74
+
75
+ def enummodule
76
+ if @module.nil?
77
+ @module = build_enum_module
78
+ end
79
+ @module
80
+ end
81
+
82
+ def options
83
+ @options ||= begin
84
+ size_ptr = ::FFI::MemoryPointer.new(:size_t, 1)
85
+ temporary_arena = Google::Protobuf::FFI.create_arena
86
+ buffer = Google::Protobuf::FFI.enum_options(self, size_ptr, temporary_arena)
87
+ opts = Google::Protobuf::EnumOptions.decode(buffer.read_string_length(size_ptr.read(:size_t)).force_encoding("ASCII-8BIT").freeze)
88
+ opts.clear_features()
89
+ opts.freeze
90
+ end
91
+ end
92
+
93
+ private
94
+
95
+ def initialize(enum_def, descriptor_pool)
96
+ @descriptor_pool = descriptor_pool
97
+ @enum_def = enum_def
98
+ @module = nil
99
+ end
100
+
101
+ def self.private_constructor(enum_def, descriptor_pool)
102
+ instance = allocate
103
+ instance.send(:initialize, enum_def, descriptor_pool)
104
+ instance
105
+ end
106
+
107
+ def self.lookup_value(enum_def, number)
108
+ enum_value = Google::Protobuf::FFI.enum_value_by_number(enum_def, number)
109
+ if enum_value.null?
110
+ nil
111
+ else
112
+ Google::Protobuf::FFI.enum_name(enum_value).to_sym
113
+ end
114
+ end
115
+
116
+ def self.lookup_name(enum_def, name)
117
+ enum_value = Google::Protobuf::FFI.enum_value_by_name(enum_def, name.to_s, name.size)
118
+ if enum_value.null?
119
+ nil
120
+ else
121
+ Google::Protobuf::FFI.enum_number(enum_value)
122
+ end
123
+ end
124
+
125
+ def build_enum_module
126
+ descriptor = self
127
+ dynamic_module = Module.new do
128
+ @descriptor = descriptor
129
+
130
+ class << self
131
+ attr_accessor :descriptor
132
+ end
133
+
134
+ def self.lookup(number)
135
+ descriptor.lookup_value number
136
+ end
137
+
138
+ def self.resolve(name)
139
+ descriptor.lookup_name name
140
+ end
141
+ end
142
+
143
+ self.each do |name, value|
144
+ if name[0] < 'A' || name[0] > 'Z'
145
+ if name[0] >= 'a' and name[0] <= 'z'
146
+ name = name[0].upcase + name[1..-1] # auto capitalize
147
+ else
148
+ warn(
149
+ "Enum value '#{name}' does not start with an uppercase letter " +
150
+ "as is required for Ruby constants.")
151
+ next
152
+ end
153
+ end
154
+ dynamic_module.const_set(name.to_sym, value)
155
+ end
156
+ dynamic_module
157
+ end
158
+ end
159
+
160
+ class FFI
161
+ # EnumDescriptor
162
+ attach_function :get_enum_file_descriptor, :upb_EnumDef_File, [EnumDescriptor], :FileDef
163
+ attach_function :enum_value_by_name, :upb_EnumDef_FindValueByNameWithSize,[EnumDescriptor, :string, :size_t], :EnumValueDef
164
+ attach_function :enum_value_by_number, :upb_EnumDef_FindValueByNumber, [EnumDescriptor, :int], :EnumValueDef
165
+ attach_function :get_enum_fullname, :upb_EnumDef_FullName, [EnumDescriptor], :string
166
+ attach_function :enum_options, :EnumDescriptor_serialized_options, [EnumDescriptor, :pointer, Internal::Arena], :pointer
167
+ attach_function :enum_value_by_index, :upb_EnumDef_Value, [EnumDescriptor, :int], :EnumValueDef
168
+ attach_function :enum_value_count, :upb_EnumDef_ValueCount, [EnumDescriptor], :int
169
+ attach_function :enum_name, :upb_EnumValueDef_Name, [:EnumValueDef], :string
170
+ attach_function :enum_number, :upb_EnumValueDef_Number, [:EnumValueDef], :int
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,210 @@
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
+ extend ::FFI::Library
12
+ # Workaround for Bazel's use of symlinks + JRuby's __FILE__ and `caller`
13
+ # that resolves them.
14
+ if ENV['BAZEL'] == 'true'
15
+ ffi_lib ::FFI::Compiler::Loader.find 'protobuf_c_ffi', ENV['PWD']
16
+ else
17
+ ffi_lib ::FFI::Compiler::Loader.find 'protobuf_c_ffi'
18
+ end
19
+
20
+ ## Map
21
+ Upb_Map_Begin = -1
22
+
23
+ ## Encoding Status
24
+ Upb_Status_MaxMessage = 511
25
+ Upb_Encode_Deterministic = 1
26
+ Upb_Encode_SkipUnknown = 2
27
+
28
+ ## JSON Encoding options
29
+ # When set, emits 0/default values. TODO: proto3 only?
30
+ Upb_JsonEncode_EmitDefaults = 1
31
+ # When set, use normal (snake_case) field names instead of JSON (camelCase) names.
32
+ Upb_JsonEncode_UseProtoNames = 2
33
+ # When set, emits enums as their integer values instead of as their names.
34
+ Upb_JsonEncode_FormatEnumsAsIntegers = 4
35
+
36
+ ## JSON Decoding options
37
+ Upb_JsonDecode_IgnoreUnknown = 1
38
+
39
+ typedef :pointer, :Array
40
+ typedef :pointer, :DefPool
41
+ typedef :pointer, :EnumValueDef
42
+ typedef :pointer, :ExtensionRegistry
43
+ typedef :pointer, :FieldDefPointer
44
+ typedef :pointer, :FileDef
45
+ typedef :pointer, :FileDescriptorProto
46
+ typedef :pointer, :Map
47
+ typedef :pointer, :Message # Instances of a message
48
+ typedef :pointer, :OneofDefPointer
49
+ typedef :pointer, :binary_string
50
+ if ::FFI::Platform::ARCH == "aarch64"
51
+ typedef :u_int8_t, :uint8_t
52
+ typedef :u_int16_t, :uint16_t
53
+ typedef :u_int32_t, :uint32_t
54
+ typedef :u_int64_t, :uint64_t
55
+ end
56
+
57
+ FieldType = enum(
58
+ :double, 1,
59
+ :float,
60
+ :int64,
61
+ :uint64,
62
+ :int32,
63
+ :fixed64,
64
+ :fixed32,
65
+ :bool,
66
+ :string,
67
+ :group,
68
+ :message,
69
+ :bytes,
70
+ :uint32,
71
+ :enum,
72
+ :sfixed32,
73
+ :sfixed64,
74
+ :sint32,
75
+ :sint64
76
+ )
77
+
78
+ CType = enum(
79
+ :bool, 1,
80
+ :float,
81
+ :int32,
82
+ :uint32,
83
+ :enum,
84
+ :message,
85
+ :double,
86
+ :int64,
87
+ :uint64,
88
+ :string,
89
+ :bytes
90
+ )
91
+
92
+ Label = enum(
93
+ :optional, 1,
94
+ :required,
95
+ :repeated
96
+ )
97
+
98
+ # All the different kind of well known type messages. For simplicity of check,
99
+ # number wrappers and string wrappers are grouped together. Make sure the
100
+ # order and merber of these groups are not changed.
101
+
102
+ WellKnown = enum(
103
+ :Unspecified,
104
+ :Any,
105
+ :FieldMask,
106
+ :Duration,
107
+ :Timestamp,
108
+ # number wrappers
109
+ :DoubleValue,
110
+ :FloatValue,
111
+ :Int64Value,
112
+ :UInt64Value,
113
+ :Int32Value,
114
+ :UInt32Value,
115
+ # string wrappers
116
+ :StringValue,
117
+ :BytesValue,
118
+ :BoolValue,
119
+ :Value,
120
+ :ListValue,
121
+ :Struct
122
+ )
123
+
124
+ DecodeStatus = enum(
125
+ :Ok,
126
+ :Malformed, # Wire format was corrupt
127
+ :OutOfMemory, # Arena alloc failed
128
+ :BadUtf8, # String field had bad UTF-8
129
+ :MaxDepthExceeded, # Exceeded UPB_DECODE_MAXDEPTH
130
+
131
+ # CheckRequired failed, but the parse otherwise succeeded.
132
+ :MissingRequired,
133
+ )
134
+
135
+ EncodeStatus = enum(
136
+ :Ok,
137
+ :OutOfMemory, # Arena alloc failed
138
+ :MaxDepthExceeded, # Exceeded UPB_DECODE_MAXDEPTH
139
+
140
+ # CheckRequired failed, but the parse otherwise succeeded.
141
+ :MissingRequired,
142
+ )
143
+
144
+ class StringView < ::FFI::Struct
145
+ layout :data, :pointer,
146
+ :size, :size_t
147
+ end
148
+
149
+ class MiniTable < ::FFI::Struct
150
+ layout :subs, :pointer,
151
+ :fields, :pointer,
152
+ :size, :uint16_t,
153
+ :field_count, :uint16_t,
154
+ :ext, :uint8_t, # upb_ExtMode, declared as uint8_t so sizeof(ext) == 1
155
+ :dense_below, :uint8_t,
156
+ :table_mask, :uint8_t,
157
+ :required_count, :uint8_t # Required fields have the lowest hasbits.
158
+ # To statically initialize the tables of variable length, we need a flexible
159
+ # array member, and we need to compile in gnu99 mode (constant initialization
160
+ # of flexible array members is a GNU extension, not in C99 unfortunately. */
161
+ # _upb_FastTable_Entry fasttable[];
162
+ end
163
+
164
+ class Status < ::FFI::Struct
165
+ layout :ok, :bool,
166
+ :msg, [:char, Upb_Status_MaxMessage]
167
+
168
+ def initialize
169
+ super
170
+ FFI.clear self
171
+ end
172
+ end
173
+
174
+ class MessageValue < ::FFI::Union
175
+ layout :bool_val, :bool,
176
+ :float_val, :float,
177
+ :double_val, :double,
178
+ :int32_val, :int32_t,
179
+ :int64_val, :int64_t,
180
+ :uint32_val, :uint32_t,
181
+ :uint64_val,:uint64_t,
182
+ :map_val, :pointer,
183
+ :msg_val, :pointer,
184
+ :array_val,:pointer,
185
+ :str_val, StringView
186
+ end
187
+
188
+ Upb_Message_Begin = -1
189
+
190
+ class MutableMessageValue < ::FFI::Union
191
+ layout :map, :Map,
192
+ :msg, :Message,
193
+ :array, :Array
194
+ end
195
+
196
+ # Status
197
+ attach_function :clear, :upb_Status_Clear, [Status.by_ref], :void
198
+ attach_function :error_message, :upb_Status_ErrorMessage, [Status.by_ref], :string
199
+
200
+ # Generic
201
+ attach_function :memcmp, [:pointer, :pointer, :size_t], :int
202
+ attach_function :memcpy, [:pointer, :pointer, :size_t], :int
203
+
204
+ # Alternatives to pre-processor macros
205
+ def self.decode_max_depth(i)
206
+ i << 16
207
+ end
208
+ end
209
+ end
210
+ end