google-protobuf 3.20.0 → 4.34.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.
Files changed (73) hide show
  1. checksums.yaml +4 -4
  2. data/ext/google/protobuf_c/Rakefile +3 -0
  3. data/ext/google/protobuf_c/convert.c +60 -86
  4. data/ext/google/protobuf_c/convert.h +3 -28
  5. data/ext/google/protobuf_c/defs.c +961 -157
  6. data/ext/google/protobuf_c/defs.h +3 -28
  7. data/ext/google/protobuf_c/extconf.rb +21 -11
  8. data/ext/google/protobuf_c/glue.c +135 -0
  9. data/ext/google/protobuf_c/map.c +187 -121
  10. data/ext/google/protobuf_c/map.h +12 -30
  11. data/ext/google/protobuf_c/message.c +354 -294
  12. data/ext/google/protobuf_c/message.h +11 -33
  13. data/ext/google/protobuf_c/protobuf.c +65 -188
  14. data/ext/google/protobuf_c/protobuf.h +21 -41
  15. data/ext/google/protobuf_c/repeated_field.c +145 -74
  16. data/ext/google/protobuf_c/repeated_field.h +11 -29
  17. data/ext/google/protobuf_c/ruby-upb.c +15734 -8866
  18. data/ext/google/protobuf_c/ruby-upb.h +16769 -4511
  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 +207 -0
  25. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.h +20 -7
  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/lib/google/protobuf/any_pb.rb +6 -8
  29. data/lib/google/protobuf/api_pb.rb +6 -26
  30. data/lib/google/protobuf/descriptor_pb.rb +27 -226
  31. data/lib/google/protobuf/duration_pb.rb +6 -8
  32. data/lib/google/protobuf/empty_pb.rb +6 -6
  33. data/lib/google/protobuf/ffi/descriptor.rb +175 -0
  34. data/lib/google/protobuf/ffi/descriptor_pool.rb +83 -0
  35. data/lib/google/protobuf/ffi/enum_descriptor.rb +183 -0
  36. data/lib/google/protobuf/ffi/ffi.rb +213 -0
  37. data/lib/google/protobuf/ffi/field_descriptor.rb +346 -0
  38. data/lib/google/protobuf/ffi/file_descriptor.rb +85 -0
  39. data/lib/google/protobuf/ffi/internal/arena.rb +60 -0
  40. data/lib/google/protobuf/ffi/internal/convert.rb +292 -0
  41. data/lib/google/protobuf/ffi/internal/pointer_helper.rb +36 -0
  42. data/lib/google/protobuf/ffi/internal/type_safety.rb +25 -0
  43. data/lib/google/protobuf/ffi/map.rb +433 -0
  44. data/lib/google/protobuf/ffi/message.rb +783 -0
  45. data/lib/google/protobuf/ffi/method_descriptor.rb +124 -0
  46. data/lib/google/protobuf/ffi/object_cache.rb +30 -0
  47. data/lib/google/protobuf/ffi/oneof_descriptor.rb +107 -0
  48. data/lib/google/protobuf/ffi/repeated_field.rb +411 -0
  49. data/lib/google/protobuf/ffi/service_descriptor.rb +117 -0
  50. data/lib/google/protobuf/field_mask_pb.rb +6 -7
  51. data/lib/google/protobuf/internal/object_cache.rb +99 -0
  52. data/lib/google/protobuf/message_exts.rb +12 -26
  53. data/lib/google/protobuf/plugin_pb.rb +25 -0
  54. data/lib/google/protobuf/repeated_field.rb +22 -33
  55. data/lib/google/protobuf/source_context_pb.rb +6 -7
  56. data/lib/google/protobuf/struct_pb.rb +6 -23
  57. data/lib/google/protobuf/timestamp_pb.rb +6 -8
  58. data/lib/google/protobuf/type_pb.rb +6 -71
  59. data/lib/google/protobuf/well_known_types.rb +5 -34
  60. data/lib/google/protobuf/wrappers_pb.rb +6 -31
  61. data/lib/google/protobuf.rb +27 -45
  62. data/lib/google/protobuf_ffi.rb +52 -0
  63. data/lib/google/protobuf_native.rb +19 -0
  64. data/lib/google/tasks/ffi.rake +100 -0
  65. metadata +103 -32
  66. data/ext/google/protobuf_c/third_party/utf8_range/naive.c +0 -92
  67. data/ext/google/protobuf_c/third_party/utf8_range/range2-neon.c +0 -157
  68. data/ext/google/protobuf_c/third_party/utf8_range/range2-sse.c +0 -170
  69. data/ext/google/protobuf_c/wrap_memcpy.c +0 -52
  70. data/lib/google/protobuf/descriptor_dsl.rb +0 -465
  71. data/tests/basic.rb +0 -670
  72. data/tests/generated_code_test.rb +0 -23
  73. data/tests/stress.rb +0 -38
@@ -1,16 +1,14 @@
1
+ # frozen_string_literal: true
1
2
  # Generated by the protocol buffer compiler. DO NOT EDIT!
2
3
  # source: google/protobuf/duration.proto
3
4
 
4
5
  require 'google/protobuf'
5
6
 
6
- Google::Protobuf::DescriptorPool.generated_pool.build do
7
- add_file("google/protobuf/duration.proto", :syntax => :proto3) do
8
- add_message "google.protobuf.Duration" do
9
- optional :seconds, :int64, 1
10
- optional :nanos, :int32, 2
11
- end
12
- end
13
- end
7
+
8
+ descriptor_data = "\n\x1egoogle/protobuf/duration.proto\x12\x0fgoogle.protobuf\"*\n\x08\x44uration\x12\x0f\n\x07seconds\x18\x01 \x01(\x03\x12\r\n\x05nanos\x18\x02 \x01(\x05\x42\x83\x01\n\x13\x63om.google.protobufB\rDurationProtoP\x01Z1google.golang.org/protobuf/types/known/durationpb\xf8\x01\x01\xa2\x02\x03GPB\xaa\x02\x1eGoogle.Protobuf.WellKnownTypesb\x06proto3"
9
+
10
+ pool = ::Google::Protobuf::DescriptorPool.generated_pool
11
+ pool.add_serialized_file(descriptor_data)
14
12
 
15
13
  module Google
16
14
  module Protobuf
@@ -1,14 +1,14 @@
1
+ # frozen_string_literal: true
1
2
  # Generated by the protocol buffer compiler. DO NOT EDIT!
2
3
  # source: google/protobuf/empty.proto
3
4
 
4
5
  require 'google/protobuf'
5
6
 
6
- Google::Protobuf::DescriptorPool.generated_pool.build do
7
- add_file("google/protobuf/empty.proto", :syntax => :proto3) do
8
- add_message "google.protobuf.Empty" do
9
- end
10
- end
11
- end
7
+
8
+ descriptor_data = "\n\x1bgoogle/protobuf/empty.proto\x12\x0fgoogle.protobuf\"\x07\n\x05\x45mptyB}\n\x13\x63om.google.protobufB\nEmptyProtoP\x01Z.google.golang.org/protobuf/types/known/emptypb\xf8\x01\x01\xa2\x02\x03GPB\xaa\x02\x1eGoogle.Protobuf.WellKnownTypesb\x06proto3"
9
+
10
+ pool = ::Google::Protobuf::DescriptorPool.generated_pool
11
+ pool.add_serialized_file(descriptor_data)
12
12
 
13
13
  module Google
14
14
  module Protobuf
@@ -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,83 @@
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 :disable_closed_enum_checking, :upb_DefPool_DisableClosedEnumChecking, [:DefPool], :void
16
+ attach_function :get_extension_registry,:upb_DefPool_ExtensionRegistry, [:DefPool], :ExtensionRegistry
17
+ attach_function :lookup_enum, :upb_DefPool_FindEnumByName, [:DefPool, :string], EnumDescriptor
18
+ attach_function :lookup_extension, :upb_DefPool_FindExtensionByName,[:DefPool, :string], FieldDescriptor
19
+ attach_function :lookup_msg, :upb_DefPool_FindMessageByName, [:DefPool, :string], Descriptor
20
+ attach_function :lookup_service, :upb_DefPool_FindServiceByName, [:DefPool, :string], ServiceDescriptor
21
+ attach_function :lookup_file, :upb_DefPool_FindFileByName, [:DefPool, :string], FileDescriptor
22
+
23
+ # FileDescriptorProto
24
+ attach_function :parse, :FileDescriptorProto_parse, [:binary_string, :size_t, Internal::Arena], :FileDescriptorProto
25
+ end
26
+ class DescriptorPool
27
+ attr :descriptor_pool
28
+ attr_accessor :descriptor_class_by_def
29
+
30
+ def initialize
31
+ @descriptor_pool = ::FFI::AutoPointer.new(Google::Protobuf::FFI.create_descriptor_pool, Google::Protobuf::FFI.method(:free_descriptor_pool))
32
+ @descriptor_class_by_def = {}
33
+
34
+ # Ruby treats all enums as open.
35
+ Google::Protobuf::FFI.disable_closed_enum_checking(@descriptor_pool)
36
+
37
+ # Should always be the last expression of the initializer to avoid
38
+ # leaking references to this object before construction is complete.
39
+ Google::Protobuf::OBJECT_CACHE.try_add @descriptor_pool.address, self
40
+ end
41
+
42
+ def add_serialized_file(file_contents)
43
+ # Allocate memory sized to file_contents
44
+ memBuf = ::FFI::MemoryPointer.new(:char, file_contents.bytesize)
45
+ # Insert the data
46
+ memBuf.put_bytes(0, file_contents)
47
+ temporary_arena = Google::Protobuf::FFI.create_arena
48
+ file_descriptor_proto = Google::Protobuf::FFI.parse memBuf, file_contents.bytesize, temporary_arena
49
+ raise ArgumentError.new("Unable to parse FileDescriptorProto") if file_descriptor_proto.null?
50
+
51
+ status = Google::Protobuf::FFI::Status.new
52
+ file_descriptor = Google::Protobuf::FFI.add_serialized_file @descriptor_pool, file_descriptor_proto, status
53
+ if file_descriptor.null?
54
+ raise TypeError.new("Unable to build file to DescriptorPool: #{Google::Protobuf::FFI.error_message(status)}")
55
+ else
56
+ @descriptor_class_by_def[file_descriptor.address] = FileDescriptor.new file_descriptor, self
57
+ end
58
+ end
59
+
60
+ def lookup name
61
+ Google::Protobuf::FFI.lookup_msg(@descriptor_pool, name) ||
62
+ Google::Protobuf::FFI.lookup_enum(@descriptor_pool, name) ||
63
+ Google::Protobuf::FFI.lookup_extension(@descriptor_pool, name) ||
64
+ Google::Protobuf::FFI.lookup_service(@descriptor_pool, name) ||
65
+ Google::Protobuf::FFI.lookup_file(@descriptor_pool, name)
66
+ end
67
+
68
+ def self.generated_pool
69
+ @@generated_pool ||= DescriptorPool.new
70
+ end
71
+
72
+ private
73
+
74
+ # Implementation details below are subject to breaking changes without
75
+ # warning and are intended for use only within the gem.
76
+
77
+ def get_file_descriptor file_def
78
+ return nil if file_def.null?
79
+ @descriptor_class_by_def[file_def.address] ||= FileDescriptor.new(file_def, self)
80
+ end
81
+ end
82
+ end
83
+ 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,213 @@
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 :fields, :pointer,
155
+ :size, :uint16_t,
156
+ :field_count, :uint16_t,
157
+ :ext, :uint8_t, # upb_ExtMode, declared as uint8_t so sizeof(ext) == 1
158
+ :dense_below, :uint8_t,
159
+ :table_mask, :uint8_t,
160
+ :required_count, :uint8_t # Required fields have the lowest hasbits.
161
+ # To statically initialize the tables of variable length, we need a flexible
162
+ # array member, and we need to compile in gnu99 mode (constant initialization
163
+ # of flexible array members is a GNU extension, not in C99 unfortunately. */
164
+ # _upb_FastTable_Entry fasttable[];
165
+ end
166
+
167
+ class Status < ::FFI::Struct
168
+ layout :ok, :bool,
169
+ :msg, [:char, Upb_Status_MaxMessage]
170
+
171
+ def initialize
172
+ super
173
+ FFI.clear self
174
+ end
175
+ end
176
+
177
+ class MessageValue < ::FFI::Union
178
+ layout :bool_val, :bool,
179
+ :float_val, :float,
180
+ :double_val, :double,
181
+ :int32_val, :int32_t,
182
+ :int64_val, :int64_t,
183
+ :uint32_val, :uint32_t,
184
+ :uint64_val,:uint64_t,
185
+ :map_val, :pointer,
186
+ :msg_val, :pointer,
187
+ :array_val,:pointer,
188
+ :str_val, StringView
189
+ end
190
+
191
+ Upb_Message_Begin = -1
192
+
193
+ class MutableMessageValue < ::FFI::Union
194
+ layout :map, :Map,
195
+ :msg, :Message,
196
+ :array, :Array
197
+ end
198
+
199
+ # Status
200
+ attach_function :clear, :upb_Status_Clear, [Status.by_ref], :void
201
+ attach_function :error_message, :upb_Status_ErrorMessage, [Status.by_ref], :string
202
+
203
+ # Generic
204
+ attach_function :memcmp, [:pointer, :pointer, :size_t], :int
205
+ attach_function :memcpy, [:pointer, :pointer, :size_t], :int
206
+
207
+ # Alternatives to pre-processor macros
208
+ def self.decode_max_depth(i)
209
+ i << 16
210
+ end
211
+ end
212
+ end
213
+ end