google-protobuf 3.22.2 → 4.30.1
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.
- checksums.yaml +4 -4
- data/ext/google/protobuf_c/Rakefile +3 -0
- data/ext/google/protobuf_c/convert.c +60 -86
- data/ext/google/protobuf_c/convert.h +3 -28
- data/ext/google/protobuf_c/defs.c +692 -72
- data/ext/google/protobuf_c/defs.h +3 -28
- data/ext/google/protobuf_c/extconf.rb +14 -1
- data/ext/google/protobuf_c/glue.c +135 -0
- data/ext/google/protobuf_c/map.c +89 -45
- data/ext/google/protobuf_c/map.h +12 -30
- data/ext/google/protobuf_c/message.c +169 -169
- data/ext/google/protobuf_c/message.h +11 -33
- data/ext/google/protobuf_c/protobuf.c +65 -188
- data/ext/google/protobuf_c/protobuf.h +27 -39
- data/ext/google/protobuf_c/repeated_field.c +72 -38
- data/ext/google/protobuf_c/repeated_field.h +11 -29
- data/ext/google/protobuf_c/ruby-upb.c +15064 -11220
- data/ext/google/protobuf_c/ruby-upb.h +10643 -5403
- data/ext/google/protobuf_c/shared_convert.c +69 -0
- data/ext/google/protobuf_c/shared_convert.h +26 -0
- data/ext/google/protobuf_c/shared_message.c +37 -0
- data/ext/google/protobuf_c/shared_message.h +21 -0
- data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.c +207 -0
- data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.h +9 -8
- data/ext/google/protobuf_c/third_party/utf8_range/utf8_range_neon.inc +117 -0
- data/ext/google/protobuf_c/third_party/utf8_range/utf8_range_sse.inc +272 -0
- data/ext/google/protobuf_c/wrap_memcpy.c +3 -26
- data/lib/google/protobuf/any_pb.rb +6 -8
- data/lib/google/protobuf/api_pb.rb +6 -26
- data/lib/google/protobuf/descriptor_pb.rb +21 -252
- data/lib/google/protobuf/duration_pb.rb +6 -8
- data/lib/google/protobuf/empty_pb.rb +6 -6
- data/lib/google/protobuf/ffi/descriptor.rb +175 -0
- data/lib/google/protobuf/ffi/descriptor_pool.rb +77 -0
- data/lib/google/protobuf/ffi/enum_descriptor.rb +183 -0
- data/lib/google/protobuf/ffi/ffi.rb +214 -0
- data/lib/google/protobuf/ffi/field_descriptor.rb +340 -0
- data/lib/google/protobuf/ffi/file_descriptor.rb +59 -0
- data/lib/google/protobuf/ffi/internal/arena.rb +60 -0
- data/lib/google/protobuf/ffi/internal/convert.rb +292 -0
- data/lib/google/protobuf/ffi/internal/pointer_helper.rb +35 -0
- data/lib/google/protobuf/ffi/internal/type_safety.rb +25 -0
- data/lib/google/protobuf/ffi/map.rb +433 -0
- data/lib/google/protobuf/ffi/message.rb +783 -0
- data/lib/google/protobuf/ffi/method_descriptor.rb +124 -0
- data/lib/google/protobuf/ffi/object_cache.rb +30 -0
- data/lib/google/protobuf/ffi/oneof_descriptor.rb +107 -0
- data/lib/google/protobuf/ffi/repeated_field.rb +411 -0
- data/lib/google/protobuf/ffi/service_descriptor.rb +117 -0
- data/lib/google/protobuf/field_mask_pb.rb +6 -7
- data/lib/google/protobuf/internal/object_cache.rb +99 -0
- data/lib/google/protobuf/message_exts.rb +3 -26
- data/lib/google/protobuf/plugin_pb.rb +6 -31
- data/lib/google/protobuf/repeated_field.rb +7 -31
- data/lib/google/protobuf/source_context_pb.rb +6 -7
- data/lib/google/protobuf/struct_pb.rb +6 -23
- data/lib/google/protobuf/timestamp_pb.rb +6 -8
- data/lib/google/protobuf/type_pb.rb +6 -71
- data/lib/google/protobuf/well_known_types.rb +5 -34
- data/lib/google/protobuf/wrappers_pb.rb +6 -31
- data/lib/google/protobuf.rb +27 -45
- data/lib/google/protobuf_ffi.rb +52 -0
- data/lib/google/protobuf_native.rb +19 -0
- data/lib/google/tasks/ffi.rake +100 -0
- metadata +92 -9
- data/ext/google/protobuf_c/third_party/utf8_range/naive.c +0 -92
- data/ext/google/protobuf_c/third_party/utf8_range/range2-neon.c +0 -157
- data/ext/google/protobuf_c/third_party/utf8_range/range2-sse.c +0 -170
- data/lib/google/protobuf/descriptor_dsl.rb +0 -465
@@ -0,0 +1,183 @@
|
|
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
|
+
def to_proto
|
94
|
+
@to_proto ||= begin
|
95
|
+
size_ptr = ::FFI::MemoryPointer.new(:size_t, 1)
|
96
|
+
temporary_arena = Google::Protobuf::FFI.create_arena
|
97
|
+
buffer = Google::Protobuf::FFI.enum_to_proto(self, size_ptr, temporary_arena)
|
98
|
+
Google::Protobuf::EnumDescriptorProto.decode(buffer.read_string_length(size_ptr.read(:size_t)).force_encoding("ASCII-8BIT").freeze)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
private
|
103
|
+
|
104
|
+
def initialize(enum_def, descriptor_pool)
|
105
|
+
@descriptor_pool = descriptor_pool
|
106
|
+
@enum_def = enum_def
|
107
|
+
@module = nil
|
108
|
+
end
|
109
|
+
|
110
|
+
def self.private_constructor(enum_def, descriptor_pool)
|
111
|
+
instance = allocate
|
112
|
+
instance.send(:initialize, enum_def, descriptor_pool)
|
113
|
+
instance
|
114
|
+
end
|
115
|
+
|
116
|
+
def self.lookup_value(enum_def, number)
|
117
|
+
enum_value = Google::Protobuf::FFI.enum_value_by_number(enum_def, number)
|
118
|
+
if enum_value.null?
|
119
|
+
nil
|
120
|
+
else
|
121
|
+
Google::Protobuf::FFI.enum_name(enum_value).to_sym
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def self.lookup_name(enum_def, name)
|
126
|
+
enum_value = Google::Protobuf::FFI.enum_value_by_name(enum_def, name.to_s, name.size)
|
127
|
+
if enum_value.null?
|
128
|
+
nil
|
129
|
+
else
|
130
|
+
Google::Protobuf::FFI.enum_number(enum_value)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def build_enum_module
|
135
|
+
descriptor = self
|
136
|
+
dynamic_module = Module.new do
|
137
|
+
@descriptor = descriptor
|
138
|
+
|
139
|
+
class << self
|
140
|
+
attr_accessor :descriptor
|
141
|
+
end
|
142
|
+
|
143
|
+
def self.lookup(number)
|
144
|
+
descriptor.lookup_value number
|
145
|
+
end
|
146
|
+
|
147
|
+
def self.resolve(name)
|
148
|
+
descriptor.lookup_name name
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
self.each do |name, value|
|
153
|
+
if name[0] < 'A' || name[0] > 'Z'
|
154
|
+
if name[0] >= 'a' and name[0] <= 'z'
|
155
|
+
name = name[0].upcase + name[1..-1] # auto capitalize
|
156
|
+
else
|
157
|
+
warn(
|
158
|
+
"Enum value '#{name}' does not start with an uppercase letter " +
|
159
|
+
"as is required for Ruby constants.")
|
160
|
+
next
|
161
|
+
end
|
162
|
+
end
|
163
|
+
dynamic_module.const_set(name.to_sym, value)
|
164
|
+
end
|
165
|
+
dynamic_module
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
class FFI
|
170
|
+
# EnumDescriptor
|
171
|
+
attach_function :get_enum_file_descriptor, :upb_EnumDef_File, [EnumDescriptor], :FileDef
|
172
|
+
attach_function :enum_value_by_name, :upb_EnumDef_FindValueByNameWithSize,[EnumDescriptor, :string, :size_t], :EnumValueDef
|
173
|
+
attach_function :enum_value_by_number, :upb_EnumDef_FindValueByNumber, [EnumDescriptor, :int], :EnumValueDef
|
174
|
+
attach_function :get_enum_fullname, :upb_EnumDef_FullName, [EnumDescriptor], :string
|
175
|
+
attach_function :enum_options, :EnumDescriptor_serialized_options, [EnumDescriptor, :pointer, Internal::Arena], :pointer
|
176
|
+
attach_function :enum_to_proto, :EnumDescriptor_serialized_to_proto, [EnumDescriptor, :pointer, Internal::Arena], :pointer
|
177
|
+
attach_function :enum_value_by_index, :upb_EnumDef_Value, [EnumDescriptor, :int], :EnumValueDef
|
178
|
+
attach_function :enum_value_count, :upb_EnumDef_ValueCount, [EnumDescriptor], :int
|
179
|
+
attach_function :enum_name, :upb_EnumValueDef_Name, [:EnumValueDef], :string
|
180
|
+
attach_function :enum_number, :upb_EnumValueDef_Number, [:EnumValueDef], :int
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
@@ -0,0 +1,214 @@
|
|
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
|
+
## JSON Decoding results
|
40
|
+
Upb_JsonDecodeResult_Ok = 0
|
41
|
+
Upb_JsonDecodeResult_Error = 2
|
42
|
+
|
43
|
+
typedef :pointer, :Array
|
44
|
+
typedef :pointer, :DefPool
|
45
|
+
typedef :pointer, :EnumValueDef
|
46
|
+
typedef :pointer, :ExtensionRegistry
|
47
|
+
typedef :pointer, :FieldDefPointer
|
48
|
+
typedef :pointer, :FileDef
|
49
|
+
typedef :pointer, :FileDescriptorProto
|
50
|
+
typedef :pointer, :Map
|
51
|
+
typedef :pointer, :Message # Instances of a message
|
52
|
+
typedef :pointer, :OneofDefPointer
|
53
|
+
typedef :pointer, :binary_string
|
54
|
+
if ::FFI::Platform::ARCH == "aarch64"
|
55
|
+
typedef :u_int8_t, :uint8_t
|
56
|
+
typedef :u_int16_t, :uint16_t
|
57
|
+
typedef :u_int32_t, :uint32_t
|
58
|
+
typedef :u_int64_t, :uint64_t
|
59
|
+
end
|
60
|
+
|
61
|
+
FieldType = enum(
|
62
|
+
:double, 1,
|
63
|
+
:float,
|
64
|
+
:int64,
|
65
|
+
:uint64,
|
66
|
+
:int32,
|
67
|
+
:fixed64,
|
68
|
+
:fixed32,
|
69
|
+
:bool,
|
70
|
+
:string,
|
71
|
+
:group,
|
72
|
+
:message,
|
73
|
+
:bytes,
|
74
|
+
:uint32,
|
75
|
+
:enum,
|
76
|
+
:sfixed32,
|
77
|
+
:sfixed64,
|
78
|
+
:sint32,
|
79
|
+
:sint64
|
80
|
+
)
|
81
|
+
|
82
|
+
CType = enum(
|
83
|
+
:bool, 1,
|
84
|
+
:float,
|
85
|
+
:int32,
|
86
|
+
:uint32,
|
87
|
+
:enum,
|
88
|
+
:message,
|
89
|
+
:double,
|
90
|
+
:int64,
|
91
|
+
:uint64,
|
92
|
+
:string,
|
93
|
+
:bytes
|
94
|
+
)
|
95
|
+
|
96
|
+
Label = enum(
|
97
|
+
:optional, 1,
|
98
|
+
:required,
|
99
|
+
:repeated
|
100
|
+
)
|
101
|
+
|
102
|
+
# All the different kind of well known type messages. For simplicity of check,
|
103
|
+
# number wrappers and string wrappers are grouped together. Make sure the
|
104
|
+
# order and merber of these groups are not changed.
|
105
|
+
|
106
|
+
WellKnown = enum(
|
107
|
+
:Unspecified,
|
108
|
+
:Any,
|
109
|
+
:FieldMask,
|
110
|
+
:Duration,
|
111
|
+
:Timestamp,
|
112
|
+
# number wrappers
|
113
|
+
:DoubleValue,
|
114
|
+
:FloatValue,
|
115
|
+
:Int64Value,
|
116
|
+
:UInt64Value,
|
117
|
+
:Int32Value,
|
118
|
+
:UInt32Value,
|
119
|
+
# string wrappers
|
120
|
+
:StringValue,
|
121
|
+
:BytesValue,
|
122
|
+
:BoolValue,
|
123
|
+
:Value,
|
124
|
+
:ListValue,
|
125
|
+
:Struct
|
126
|
+
)
|
127
|
+
|
128
|
+
DecodeStatus = enum(
|
129
|
+
:Ok,
|
130
|
+
:Malformed, # Wire format was corrupt
|
131
|
+
:OutOfMemory, # Arena alloc failed
|
132
|
+
:BadUtf8, # String field had bad UTF-8
|
133
|
+
:MaxDepthExceeded, # Exceeded UPB_DECODE_MAXDEPTH
|
134
|
+
|
135
|
+
# CheckRequired failed, but the parse otherwise succeeded.
|
136
|
+
:MissingRequired,
|
137
|
+
)
|
138
|
+
|
139
|
+
EncodeStatus = enum(
|
140
|
+
:Ok,
|
141
|
+
:OutOfMemory, # Arena alloc failed
|
142
|
+
:MaxDepthExceeded, # Exceeded UPB_DECODE_MAXDEPTH
|
143
|
+
|
144
|
+
# CheckRequired failed, but the parse otherwise succeeded.
|
145
|
+
:MissingRequired,
|
146
|
+
)
|
147
|
+
|
148
|
+
class StringView < ::FFI::Struct
|
149
|
+
layout :data, :pointer,
|
150
|
+
:size, :size_t
|
151
|
+
end
|
152
|
+
|
153
|
+
class MiniTable < ::FFI::Struct
|
154
|
+
layout :subs, :pointer,
|
155
|
+
:fields, :pointer,
|
156
|
+
:size, :uint16_t,
|
157
|
+
:field_count, :uint16_t,
|
158
|
+
:ext, :uint8_t, # upb_ExtMode, declared as uint8_t so sizeof(ext) == 1
|
159
|
+
:dense_below, :uint8_t,
|
160
|
+
:table_mask, :uint8_t,
|
161
|
+
:required_count, :uint8_t # Required fields have the lowest hasbits.
|
162
|
+
# To statically initialize the tables of variable length, we need a flexible
|
163
|
+
# array member, and we need to compile in gnu99 mode (constant initialization
|
164
|
+
# of flexible array members is a GNU extension, not in C99 unfortunately. */
|
165
|
+
# _upb_FastTable_Entry fasttable[];
|
166
|
+
end
|
167
|
+
|
168
|
+
class Status < ::FFI::Struct
|
169
|
+
layout :ok, :bool,
|
170
|
+
:msg, [:char, Upb_Status_MaxMessage]
|
171
|
+
|
172
|
+
def initialize
|
173
|
+
super
|
174
|
+
FFI.clear self
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
class MessageValue < ::FFI::Union
|
179
|
+
layout :bool_val, :bool,
|
180
|
+
:float_val, :float,
|
181
|
+
:double_val, :double,
|
182
|
+
:int32_val, :int32_t,
|
183
|
+
:int64_val, :int64_t,
|
184
|
+
:uint32_val, :uint32_t,
|
185
|
+
:uint64_val,:uint64_t,
|
186
|
+
:map_val, :pointer,
|
187
|
+
:msg_val, :pointer,
|
188
|
+
:array_val,:pointer,
|
189
|
+
:str_val, StringView
|
190
|
+
end
|
191
|
+
|
192
|
+
Upb_Message_Begin = -1
|
193
|
+
|
194
|
+
class MutableMessageValue < ::FFI::Union
|
195
|
+
layout :map, :Map,
|
196
|
+
:msg, :Message,
|
197
|
+
:array, :Array
|
198
|
+
end
|
199
|
+
|
200
|
+
# Status
|
201
|
+
attach_function :clear, :upb_Status_Clear, [Status.by_ref], :void
|
202
|
+
attach_function :error_message, :upb_Status_ErrorMessage, [Status.by_ref], :string
|
203
|
+
|
204
|
+
# Generic
|
205
|
+
attach_function :memcmp, [:pointer, :pointer, :size_t], :int
|
206
|
+
attach_function :memcpy, [:pointer, :pointer, :size_t], :int
|
207
|
+
|
208
|
+
# Alternatives to pre-processor macros
|
209
|
+
def self.decode_max_depth(i)
|
210
|
+
i << 16
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|