google-protobuf 4.31.0.rc.1-aarch64-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.
- checksums.yaml +7 -0
- data/ext/google/protobuf_c/Rakefile +3 -0
- data/ext/google/protobuf_c/convert.c +335 -0
- data/ext/google/protobuf_c/convert.h +50 -0
- data/ext/google/protobuf_c/defs.c +1932 -0
- data/ext/google/protobuf_c/defs.h +82 -0
- data/ext/google/protobuf_c/extconf.rb +44 -0
- data/ext/google/protobuf_c/glue.c +135 -0
- data/ext/google/protobuf_c/map.c +731 -0
- data/ext/google/protobuf_c/map.h +48 -0
- data/ext/google/protobuf_c/message.c +1426 -0
- data/ext/google/protobuf_c/message.h +82 -0
- data/ext/google/protobuf_c/protobuf.c +357 -0
- data/ext/google/protobuf_c/protobuf.h +102 -0
- data/ext/google/protobuf_c/repeated_field.c +691 -0
- data/ext/google/protobuf_c/repeated_field.h +45 -0
- data/ext/google/protobuf_c/ruby-upb.c +18305 -0
- data/ext/google/protobuf_c/ruby-upb.h +16315 -0
- 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/LICENSE +22 -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 +22 -0
- 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 +29 -0
- data/lib/google/3.1/protobuf_c.so +0 -0
- data/lib/google/3.2/protobuf_c.so +0 -0
- data/lib/google/3.3/protobuf_c.so +0 -0
- data/lib/google/3.4/protobuf_c.so +0 -0
- data/lib/google/protobuf/any_pb.rb +17 -0
- data/lib/google/protobuf/api_pb.rb +22 -0
- data/lib/google/protobuf/descriptor_pb.rb +70 -0
- data/lib/google/protobuf/duration_pb.rb +17 -0
- data/lib/google/protobuf/empty_pb.rb +17 -0
- data/lib/google/protobuf/ffi/descriptor.rb +175 -0
- data/lib/google/protobuf/ffi/descriptor_pool.rb +79 -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 +346 -0
- data/lib/google/protobuf/ffi/file_descriptor.rb +85 -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 +36 -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 +17 -0
- data/lib/google/protobuf/internal/object_cache.rb +99 -0
- data/lib/google/protobuf/message_exts.rb +39 -0
- data/lib/google/protobuf/plugin_pb.rb +25 -0
- data/lib/google/protobuf/repeated_field.rb +177 -0
- data/lib/google/protobuf/source_context_pb.rb +17 -0
- data/lib/google/protobuf/struct_pb.rb +20 -0
- data/lib/google/protobuf/timestamp_pb.rb +17 -0
- data/lib/google/protobuf/type_pb.rb +27 -0
- data/lib/google/protobuf/well_known_types.rb +211 -0
- data/lib/google/protobuf/wrappers_pb.rb +25 -0
- data/lib/google/protobuf.rb +61 -0
- 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 +215 -0
@@ -0,0 +1,175 @@
|
|
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
|
+
##
|
11
|
+
# Message Descriptor - Descriptor for short.
|
12
|
+
class Descriptor
|
13
|
+
attr :descriptor_pool, :msg_class
|
14
|
+
include Enumerable
|
15
|
+
|
16
|
+
# FFI Interface methods and setup
|
17
|
+
extend ::FFI::DataConverter
|
18
|
+
native_type ::FFI::Type::POINTER
|
19
|
+
|
20
|
+
class << self
|
21
|
+
prepend Google::Protobuf::Internal::TypeSafety
|
22
|
+
include Google::Protobuf::Internal::PointerHelper
|
23
|
+
|
24
|
+
# @param value [Descriptor] Descriptor to convert to an FFI native type
|
25
|
+
# @param _ [Object] Unused
|
26
|
+
def to_native(value, _ = nil)
|
27
|
+
msg_def_ptr = value.nil? ? nil : value.instance_variable_get(:@msg_def)
|
28
|
+
return ::FFI::Pointer::NULL if msg_def_ptr.nil?
|
29
|
+
raise "Underlying msg_def was null!" if msg_def_ptr.null?
|
30
|
+
msg_def_ptr
|
31
|
+
end
|
32
|
+
|
33
|
+
##
|
34
|
+
# @param msg_def [::FFI::Pointer] MsgDef pointer to be wrapped
|
35
|
+
# @param _ [Object] Unused
|
36
|
+
def from_native(msg_def, _ = nil)
|
37
|
+
return nil if msg_def.nil? or msg_def.null?
|
38
|
+
file_def = Google::Protobuf::FFI.get_message_file_def msg_def
|
39
|
+
descriptor_from_file_def(file_def, msg_def)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def to_native
|
44
|
+
self.class.to_native(self)
|
45
|
+
end
|
46
|
+
|
47
|
+
##
|
48
|
+
# Great write up of this strategy:
|
49
|
+
# See https://blog.appsignal.com/2018/08/07/ruby-magic-changing-the-way-ruby-creates-objects.html
|
50
|
+
def self.new(*arguments, &block)
|
51
|
+
raise "Descriptor objects may not be created from Ruby."
|
52
|
+
end
|
53
|
+
|
54
|
+
def to_s
|
55
|
+
inspect
|
56
|
+
end
|
57
|
+
|
58
|
+
def inspect
|
59
|
+
"Descriptor - (not the message class) #{name}"
|
60
|
+
end
|
61
|
+
|
62
|
+
def file_descriptor
|
63
|
+
@descriptor_pool.send(:get_file_descriptor, Google::Protobuf::FFI.get_message_file_def(@msg_def))
|
64
|
+
end
|
65
|
+
|
66
|
+
def name
|
67
|
+
@name ||= Google::Protobuf::FFI.get_message_fullname(self)
|
68
|
+
end
|
69
|
+
|
70
|
+
def each_oneof &block
|
71
|
+
n = Google::Protobuf::FFI.oneof_count(self)
|
72
|
+
0.upto(n-1) do |i|
|
73
|
+
yield(Google::Protobuf::FFI.get_oneof_by_index(self, i))
|
74
|
+
end
|
75
|
+
nil
|
76
|
+
end
|
77
|
+
|
78
|
+
def each &block
|
79
|
+
n = Google::Protobuf::FFI.field_count(self)
|
80
|
+
0.upto(n-1) do |i|
|
81
|
+
yield(Google::Protobuf::FFI.get_field_by_index(self, i))
|
82
|
+
end
|
83
|
+
nil
|
84
|
+
end
|
85
|
+
|
86
|
+
def lookup(name)
|
87
|
+
Google::Protobuf::FFI.get_field_by_name(self, name, name.size)
|
88
|
+
end
|
89
|
+
|
90
|
+
def lookup_oneof(name)
|
91
|
+
Google::Protobuf::FFI.get_oneof_by_name(self, name, name.size)
|
92
|
+
end
|
93
|
+
|
94
|
+
def msgclass
|
95
|
+
@msg_class ||= build_message_class
|
96
|
+
end
|
97
|
+
|
98
|
+
def options
|
99
|
+
@options ||= begin
|
100
|
+
size_ptr = ::FFI::MemoryPointer.new(:size_t, 1)
|
101
|
+
temporary_arena = Google::Protobuf::FFI.create_arena
|
102
|
+
buffer = Google::Protobuf::FFI.message_options(self, size_ptr, temporary_arena)
|
103
|
+
opts = Google::Protobuf::MessageOptions.decode(buffer.read_string_length(size_ptr.read(:size_t)).force_encoding("ASCII-8BIT").freeze)
|
104
|
+
opts.clear_features()
|
105
|
+
opts.freeze
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def to_proto
|
110
|
+
@to_proto ||= begin
|
111
|
+
size_ptr = ::FFI::MemoryPointer.new(:size_t, 1)
|
112
|
+
temporary_arena = Google::Protobuf::FFI.create_arena
|
113
|
+
buffer = Google::Protobuf::FFI.message_to_proto(self, size_ptr, temporary_arena)
|
114
|
+
Google::Protobuf::DescriptorProto.decode(buffer.read_string_length(size_ptr.read(:size_t)).force_encoding("ASCII-8BIT").freeze)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
private
|
119
|
+
|
120
|
+
extend Google::Protobuf::Internal::Convert
|
121
|
+
|
122
|
+
def initialize(msg_def, descriptor_pool)
|
123
|
+
@msg_def = msg_def
|
124
|
+
@msg_class = nil
|
125
|
+
@descriptor_pool = descriptor_pool
|
126
|
+
end
|
127
|
+
|
128
|
+
def self.private_constructor(msg_def, descriptor_pool)
|
129
|
+
instance = allocate
|
130
|
+
instance.send(:initialize, msg_def, descriptor_pool)
|
131
|
+
instance
|
132
|
+
end
|
133
|
+
|
134
|
+
def wrapper?
|
135
|
+
if defined? @wrapper
|
136
|
+
@wrapper
|
137
|
+
else
|
138
|
+
@wrapper = case Google::Protobuf::FFI.get_well_known_type self
|
139
|
+
when :DoubleValue, :FloatValue, :Int64Value, :UInt64Value, :Int32Value, :UInt32Value, :StringValue, :BytesValue, :BoolValue
|
140
|
+
true
|
141
|
+
else
|
142
|
+
false
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def self.get_message(msg, descriptor, arena)
|
148
|
+
return nil if msg.nil? or msg.null?
|
149
|
+
message = OBJECT_CACHE.get(msg.address)
|
150
|
+
if message.nil?
|
151
|
+
message = descriptor.msgclass.send(:private_constructor, arena, msg: msg)
|
152
|
+
end
|
153
|
+
message
|
154
|
+
end
|
155
|
+
|
156
|
+
def pool
|
157
|
+
@descriptor_pool
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
class FFI
|
162
|
+
# MessageDef
|
163
|
+
attach_function :new_message_from_def, :upb_Message_New, [MiniTable.by_ref, Internal::Arena], :Message
|
164
|
+
attach_function :field_count, :upb_MessageDef_FieldCount, [Descriptor], :int
|
165
|
+
attach_function :get_message_file_def, :upb_MessageDef_File, [:pointer], :FileDef
|
166
|
+
attach_function :get_message_fullname, :upb_MessageDef_FullName, [Descriptor], :string
|
167
|
+
attach_function :get_mini_table, :upb_MessageDef_MiniTable, [Descriptor], MiniTable.ptr
|
168
|
+
attach_function :oneof_count, :upb_MessageDef_OneofCount, [Descriptor], :int
|
169
|
+
attach_function :message_options, :Descriptor_serialized_options, [Descriptor, :pointer, Internal::Arena], :pointer
|
170
|
+
attach_function :get_well_known_type, :upb_MessageDef_WellKnownType, [Descriptor], WellKnown
|
171
|
+
attach_function :find_msg_def_by_name, :upb_MessageDef_FindByNameWithSize, [Descriptor, :string, :size_t, :FieldDefPointer, :OneofDefPointer], :bool
|
172
|
+
attach_function :message_to_proto, :Descriptor_serialized_to_proto, [Descriptor, :pointer, Internal::Arena], :pointer
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
@@ -0,0 +1,79 @@
|
|
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
|
+
attach_function :lookup_file, :upb_DefPool_FindFileByName, [:DefPool, :string], FileDescriptor
|
21
|
+
|
22
|
+
# FileDescriptorProto
|
23
|
+
attach_function :parse, :FileDescriptorProto_parse, [:binary_string, :size_t, Internal::Arena], :FileDescriptorProto
|
24
|
+
end
|
25
|
+
class DescriptorPool
|
26
|
+
attr :descriptor_pool
|
27
|
+
attr_accessor :descriptor_class_by_def
|
28
|
+
|
29
|
+
def initialize
|
30
|
+
@descriptor_pool = ::FFI::AutoPointer.new(Google::Protobuf::FFI.create_descriptor_pool, Google::Protobuf::FFI.method(:free_descriptor_pool))
|
31
|
+
@descriptor_class_by_def = {}
|
32
|
+
|
33
|
+
# Should always be the last expression of the initializer to avoid
|
34
|
+
# leaking references to this object before construction is complete.
|
35
|
+
Google::Protobuf::OBJECT_CACHE.try_add @descriptor_pool.address, self
|
36
|
+
end
|
37
|
+
|
38
|
+
def add_serialized_file(file_contents)
|
39
|
+
# Allocate memory sized to file_contents
|
40
|
+
memBuf = ::FFI::MemoryPointer.new(:char, file_contents.bytesize)
|
41
|
+
# Insert the data
|
42
|
+
memBuf.put_bytes(0, file_contents)
|
43
|
+
temporary_arena = Google::Protobuf::FFI.create_arena
|
44
|
+
file_descriptor_proto = Google::Protobuf::FFI.parse memBuf, file_contents.bytesize, temporary_arena
|
45
|
+
raise ArgumentError.new("Unable to parse FileDescriptorProto") if file_descriptor_proto.null?
|
46
|
+
|
47
|
+
status = Google::Protobuf::FFI::Status.new
|
48
|
+
file_descriptor = Google::Protobuf::FFI.add_serialized_file @descriptor_pool, file_descriptor_proto, status
|
49
|
+
if file_descriptor.null?
|
50
|
+
raise TypeError.new("Unable to build file to DescriptorPool: #{Google::Protobuf::FFI.error_message(status)}")
|
51
|
+
else
|
52
|
+
@descriptor_class_by_def[file_descriptor.address] = FileDescriptor.new file_descriptor, self
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def lookup name
|
57
|
+
Google::Protobuf::FFI.lookup_msg(@descriptor_pool, name) ||
|
58
|
+
Google::Protobuf::FFI.lookup_enum(@descriptor_pool, name) ||
|
59
|
+
Google::Protobuf::FFI.lookup_extension(@descriptor_pool, name) ||
|
60
|
+
Google::Protobuf::FFI.lookup_service(@descriptor_pool, name) ||
|
61
|
+
Google::Protobuf::FFI.lookup_file(@descriptor_pool, name)
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.generated_pool
|
65
|
+
@@generated_pool ||= DescriptorPool.new
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
# Implementation details below are subject to breaking changes without
|
71
|
+
# warning and are intended for use only within the gem.
|
72
|
+
|
73
|
+
def get_file_descriptor file_def
|
74
|
+
return nil if file_def.null?
|
75
|
+
@descriptor_class_by_def[file_def.address] ||= FileDescriptor.new(file_def, self)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -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
|