ffi 1.17.0-aarch64-linux-musl

Sign up to get free protection for your applications and to get access to all the features.
Files changed (134) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +456 -0
  3. data/COPYING +49 -0
  4. data/Gemfile +21 -0
  5. data/LICENSE +24 -0
  6. data/LICENSE.SPECS +22 -0
  7. data/README.md +137 -0
  8. data/Rakefile +206 -0
  9. data/ffi.gemspec +42 -0
  10. data/lib/2.5/ffi_c.so +0 -0
  11. data/lib/2.6/ffi_c.so +0 -0
  12. data/lib/2.7/ffi_c.so +0 -0
  13. data/lib/3.0/ffi_c.so +0 -0
  14. data/lib/3.1/ffi_c.so +0 -0
  15. data/lib/3.2/ffi_c.so +0 -0
  16. data/lib/3.3/ffi_c.so +0 -0
  17. data/lib/ffi/abstract_memory.rb +44 -0
  18. data/lib/ffi/autopointer.rb +180 -0
  19. data/lib/ffi/buffer.rb +4 -0
  20. data/lib/ffi/callback.rb +4 -0
  21. data/lib/ffi/compat.rb +43 -0
  22. data/lib/ffi/data_converter.rb +67 -0
  23. data/lib/ffi/dynamic_library.rb +118 -0
  24. data/lib/ffi/enum.rb +302 -0
  25. data/lib/ffi/errno.rb +43 -0
  26. data/lib/ffi/ffi.rb +50 -0
  27. data/lib/ffi/function.rb +71 -0
  28. data/lib/ffi/io.rb +62 -0
  29. data/lib/ffi/library.rb +576 -0
  30. data/lib/ffi/library_path.rb +109 -0
  31. data/lib/ffi/managedstruct.rb +84 -0
  32. data/lib/ffi/memorypointer.rb +1 -0
  33. data/lib/ffi/platform/aarch64-darwin/types.conf +130 -0
  34. data/lib/ffi/platform/aarch64-freebsd/types.conf +128 -0
  35. data/lib/ffi/platform/aarch64-freebsd12/types.conf +181 -0
  36. data/lib/ffi/platform/aarch64-linux/types.conf +175 -0
  37. data/lib/ffi/platform/aarch64-openbsd/types.conf +134 -0
  38. data/lib/ffi/platform/aarch64-windows/types.conf +52 -0
  39. data/lib/ffi/platform/arm-freebsd/types.conf +152 -0
  40. data/lib/ffi/platform/arm-freebsd12/types.conf +152 -0
  41. data/lib/ffi/platform/arm-linux/types.conf +132 -0
  42. data/lib/ffi/platform/hppa1.1-linux/types.conf +178 -0
  43. data/lib/ffi/platform/hppa2.0-linux/types.conf +178 -0
  44. data/lib/ffi/platform/i386-cygwin/types.conf +3 -0
  45. data/lib/ffi/platform/i386-darwin/types.conf +100 -0
  46. data/lib/ffi/platform/i386-freebsd/types.conf +152 -0
  47. data/lib/ffi/platform/i386-freebsd12/types.conf +152 -0
  48. data/lib/ffi/platform/i386-gnu/types.conf +107 -0
  49. data/lib/ffi/platform/i386-linux/types.conf +103 -0
  50. data/lib/ffi/platform/i386-netbsd/types.conf +126 -0
  51. data/lib/ffi/platform/i386-openbsd/types.conf +128 -0
  52. data/lib/ffi/platform/i386-solaris/types.conf +122 -0
  53. data/lib/ffi/platform/i386-windows/types.conf +52 -0
  54. data/lib/ffi/platform/ia64-linux/types.conf +104 -0
  55. data/lib/ffi/platform/loongarch64-linux/types.conf +141 -0
  56. data/lib/ffi/platform/mips-linux/types.conf +102 -0
  57. data/lib/ffi/platform/mips64-linux/types.conf +104 -0
  58. data/lib/ffi/platform/mips64el-linux/types.conf +104 -0
  59. data/lib/ffi/platform/mipsel-linux/types.conf +102 -0
  60. data/lib/ffi/platform/mipsisa32r6-linux/types.conf +102 -0
  61. data/lib/ffi/platform/mipsisa32r6el-linux/types.conf +102 -0
  62. data/lib/ffi/platform/mipsisa64r6-linux/types.conf +104 -0
  63. data/lib/ffi/platform/mipsisa64r6el-linux/types.conf +104 -0
  64. data/lib/ffi/platform/powerpc-aix/types.conf +180 -0
  65. data/lib/ffi/platform/powerpc-darwin/types.conf +100 -0
  66. data/lib/ffi/platform/powerpc-linux/types.conf +130 -0
  67. data/lib/ffi/platform/powerpc-openbsd/types.conf +156 -0
  68. data/lib/ffi/platform/powerpc64-linux/types.conf +104 -0
  69. data/lib/ffi/platform/powerpc64le-linux/types.conf +100 -0
  70. data/lib/ffi/platform/riscv64-linux/types.conf +104 -0
  71. data/lib/ffi/platform/s390-linux/types.conf +102 -0
  72. data/lib/ffi/platform/s390x-linux/types.conf +102 -0
  73. data/lib/ffi/platform/sparc-linux/types.conf +102 -0
  74. data/lib/ffi/platform/sparc-solaris/types.conf +128 -0
  75. data/lib/ffi/platform/sparcv9-linux/types.conf +102 -0
  76. data/lib/ffi/platform/sparcv9-openbsd/types.conf +156 -0
  77. data/lib/ffi/platform/sparcv9-solaris/types.conf +128 -0
  78. data/lib/ffi/platform/sw_64-linux/types.conf +141 -0
  79. data/lib/ffi/platform/x86_64-cygwin/types.conf +3 -0
  80. data/lib/ffi/platform/x86_64-darwin/types.conf +130 -0
  81. data/lib/ffi/platform/x86_64-dragonflybsd/types.conf +130 -0
  82. data/lib/ffi/platform/x86_64-freebsd/types.conf +128 -0
  83. data/lib/ffi/platform/x86_64-freebsd12/types.conf +158 -0
  84. data/lib/ffi/platform/x86_64-haiku/types.conf +117 -0
  85. data/lib/ffi/platform/x86_64-linux/types.conf +132 -0
  86. data/lib/ffi/platform/x86_64-msys/types.conf +119 -0
  87. data/lib/ffi/platform/x86_64-netbsd/types.conf +128 -0
  88. data/lib/ffi/platform/x86_64-openbsd/types.conf +134 -0
  89. data/lib/ffi/platform/x86_64-solaris/types.conf +122 -0
  90. data/lib/ffi/platform/x86_64-windows/types.conf +52 -0
  91. data/lib/ffi/platform.rb +187 -0
  92. data/lib/ffi/pointer.rb +167 -0
  93. data/lib/ffi/struct.rb +317 -0
  94. data/lib/ffi/struct_by_reference.rb +72 -0
  95. data/lib/ffi/struct_layout.rb +96 -0
  96. data/lib/ffi/struct_layout_builder.rb +227 -0
  97. data/lib/ffi/tools/const_generator.rb +232 -0
  98. data/lib/ffi/tools/generator.rb +105 -0
  99. data/lib/ffi/tools/generator_task.rb +32 -0
  100. data/lib/ffi/tools/struct_generator.rb +195 -0
  101. data/lib/ffi/tools/types_generator.rb +137 -0
  102. data/lib/ffi/types.rb +222 -0
  103. data/lib/ffi/union.rb +43 -0
  104. data/lib/ffi/variadic.rb +80 -0
  105. data/lib/ffi/version.rb +3 -0
  106. data/lib/ffi.rb +27 -0
  107. data/rakelib/ffi_gem_helper.rb +65 -0
  108. data/samples/getlogin.rb +8 -0
  109. data/samples/getpid.rb +8 -0
  110. data/samples/gettimeofday.rb +18 -0
  111. data/samples/hello.rb +8 -0
  112. data/samples/hello_ractor.rb +11 -0
  113. data/samples/inotify.rb +60 -0
  114. data/samples/pty.rb +75 -0
  115. data/samples/qsort.rb +20 -0
  116. data/samples/qsort_ractor.rb +28 -0
  117. data/sig/ffi/abstract_memory.rbs +165 -0
  118. data/sig/ffi/auto_pointer.rbs +27 -0
  119. data/sig/ffi/buffer.rbs +18 -0
  120. data/sig/ffi/data_converter.rbs +10 -0
  121. data/sig/ffi/dynamic_library.rbs +9 -0
  122. data/sig/ffi/enum.rbs +38 -0
  123. data/sig/ffi/function.rbs +39 -0
  124. data/sig/ffi/library.rbs +42 -0
  125. data/sig/ffi/native_type.rbs +86 -0
  126. data/sig/ffi/pointer.rbs +42 -0
  127. data/sig/ffi/struct.rbs +76 -0
  128. data/sig/ffi/struct_by_reference.rbs +11 -0
  129. data/sig/ffi/struct_by_value.rbs +7 -0
  130. data/sig/ffi/struct_layout.rbs +9 -0
  131. data/sig/ffi/struct_layout_builder.rbs +5 -0
  132. data/sig/ffi/type.rbs +39 -0
  133. data/sig/ffi.rbs +26 -0
  134. metadata +241 -0
@@ -0,0 +1,72 @@
1
+ #
2
+ # Copyright (C) 2010 Wayne Meissner
3
+ #
4
+ # This file is part of ruby-ffi.
5
+ #
6
+ # All rights reserved.
7
+ #
8
+ # Redistribution and use in source and binary forms, with or without
9
+ # modification, are permitted provided that the following conditions are met:
10
+ #
11
+ # * Redistributions of source code must retain the above copyright notice, this
12
+ # list of conditions and the following disclaimer.
13
+ # * Redistributions in binary form must reproduce the above copyright notice
14
+ # this list of conditions and the following disclaimer in the documentation
15
+ # and/or other materials provided with the distribution.
16
+ # * Neither the name of the Ruby FFI project nor the names of its contributors
17
+ # may be used to endorse or promote products derived from this software
18
+ # without specific prior written permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
24
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.#
30
+
31
+ module FFI
32
+ # This class includes the {FFI::DataConverter} module.
33
+ class StructByReference
34
+ include DataConverter
35
+
36
+ attr_reader :struct_class
37
+
38
+ # @param [Struct] struct_class
39
+ def initialize(struct_class)
40
+ unless Class === struct_class and struct_class < FFI::Struct
41
+ raise TypeError, 'wrong type (expected subclass of FFI::Struct)'
42
+ end
43
+ @struct_class = struct_class
44
+ end
45
+
46
+ # Always get {FFI::Type}::POINTER.
47
+ def native_type
48
+ FFI::Type::POINTER
49
+ end
50
+
51
+ # @param [nil, Struct] value
52
+ # @param [nil] ctx
53
+ # @return [AbstractMemory] Pointer on +value+.
54
+ def to_native(value, ctx)
55
+ return Pointer::NULL if value.nil?
56
+
57
+ unless @struct_class === value
58
+ raise TypeError, "wrong argument type #{value.class} (expected #{@struct_class})"
59
+ end
60
+
61
+ value.pointer
62
+ end
63
+
64
+ # @param [AbstractMemory] value
65
+ # @param [nil] ctx
66
+ # @return [Struct]
67
+ # Create a struct from content of memory +value+.
68
+ def from_native(value, ctx)
69
+ @struct_class.new(value)
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,96 @@
1
+ #
2
+ # Copyright (C) 2008-2010 Wayne Meissner
3
+ # Copyright (C) 2008, 2009 Andrea Fazzi
4
+ # Copyright (C) 2008, 2009 Luc Heinrich
5
+ #
6
+ # This file is part of ruby-ffi.
7
+ #
8
+ # All rights reserved.
9
+ #
10
+ # Redistribution and use in source and binary forms, with or without
11
+ # modification, are permitted provided that the following conditions are met:
12
+ #
13
+ # * Redistributions of source code must retain the above copyright notice, this
14
+ # list of conditions and the following disclaimer.
15
+ # * Redistributions in binary form must reproduce the above copyright notice
16
+ # this list of conditions and the following disclaimer in the documentation
17
+ # and/or other materials provided with the distribution.
18
+ # * Neither the name of the Ruby FFI project nor the names of its contributors
19
+ # may be used to endorse or promote products derived from this software
20
+ # without specific prior written permission.
21
+ #
22
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
26
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
+ #
33
+
34
+ module FFI
35
+
36
+ class StructLayout
37
+
38
+ # @return [Array<Array(Symbol, Integer)>
39
+ # Get an array of tuples (field name, offset of the field).
40
+ def offsets
41
+ members.map { |m| [ m, self[m].offset ] }
42
+ end
43
+
44
+ # @return [Integer]
45
+ # Get the offset of a field.
46
+ def offset_of(field_name)
47
+ self[field_name].offset
48
+ end
49
+
50
+ # An enum {Field} in a {StructLayout}.
51
+ class Enum < Field
52
+
53
+ # @param [AbstractMemory] ptr pointer on a {Struct}
54
+ # @return [Object]
55
+ # Get an object of type {#type} from memory pointed by +ptr+.
56
+ def get(ptr)
57
+ type.find(ptr.get_int(offset))
58
+ end
59
+
60
+ # @param [AbstractMemory] ptr pointer on a {Struct}
61
+ # @param value
62
+ # @return [nil]
63
+ # Set +value+ into memory pointed by +ptr+.
64
+ def put(ptr, value)
65
+ ptr.put_int(offset, type.find(value))
66
+ end
67
+
68
+ end
69
+
70
+ class InnerStruct < Field
71
+ def get(ptr)
72
+ type.struct_class.new(ptr.slice(self.offset, self.size))
73
+ end
74
+
75
+ def put(ptr, value)
76
+ raise TypeError, "wrong value type (expected #{type.struct_class})" unless value.is_a?(type.struct_class)
77
+ ptr.slice(self.offset, self.size).__copy_from__(value.pointer, self.size)
78
+ end
79
+ end
80
+
81
+ class Mapped < Field
82
+ def initialize(name, offset, type, orig_field)
83
+ @orig_field = orig_field
84
+ super(name, offset, type)
85
+ end
86
+
87
+ def get(ptr)
88
+ type.from_native(@orig_field.get(ptr), nil)
89
+ end
90
+
91
+ def put(ptr, value)
92
+ @orig_field.put(ptr, type.to_native(value, nil))
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,227 @@
1
+ #
2
+ # Copyright (C) 2008-2010 Wayne Meissner
3
+ #
4
+ # This file is part of ruby-ffi.
5
+ #
6
+ # All rights reserved.
7
+ #
8
+ # Redistribution and use in source and binary forms, with or without
9
+ # modification, are permitted provided that the following conditions are met:
10
+ #
11
+ # * Redistributions of source code must retain the above copyright notice, this
12
+ # list of conditions and the following disclaimer.
13
+ # * Redistributions in binary form must reproduce the above copyright notice
14
+ # this list of conditions and the following disclaimer in the documentation
15
+ # and/or other materials provided with the distribution.
16
+ # * Neither the name of the Ruby FFI project nor the names of its contributors
17
+ # may be used to endorse or promote products derived from this software
18
+ # without specific prior written permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
24
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+ #
31
+
32
+ module FFI
33
+
34
+ # Build a {StructLayout struct layout}.
35
+ class StructLayoutBuilder
36
+ attr_reader :size
37
+ attr_reader :alignment
38
+
39
+ def initialize
40
+ @size = 0
41
+ @alignment = 1
42
+ @min_alignment = 1
43
+ @packed = false
44
+ @union = false
45
+ @fields = Array.new
46
+ end
47
+
48
+ # Set size attribute with +size+ only if +size+ is greater than attribute value.
49
+ # @param [Integer] size
50
+ def size=(size)
51
+ @size = size if size > @size
52
+ end
53
+
54
+ # Set alignment attribute with +align+ only if it is greater than attribute value.
55
+ # @param [Integer] align
56
+ def alignment=(align)
57
+ @alignment = align if align > @alignment
58
+ @min_alignment = align
59
+ end
60
+
61
+ # Set union attribute.
62
+ # Set to +true+ to build a {Union} instead of a {Struct}.
63
+ # @param [Boolean] is_union
64
+ # @return [is_union]
65
+ def union=(is_union)
66
+ @union = is_union
67
+ end
68
+
69
+ # Building a {Union} or a {Struct} ?
70
+ #
71
+ # @return [Boolean]
72
+ #
73
+ def union?
74
+ @union
75
+ end
76
+
77
+ # Set packed attribute
78
+ # @overload packed=(packed) Set alignment and packed attributes to
79
+ # +packed+.
80
+ #
81
+ # @param [Integer] packed
82
+ #
83
+ # @return [packed]
84
+ # @overload packed=(packed) Set packed attribute.
85
+ # @param packed
86
+ #
87
+ # @return [0,1]
88
+ #
89
+ def packed=(packed)
90
+ if packed.is_a?(0.class)
91
+ @alignment = packed
92
+ @packed = packed
93
+ else
94
+ @packed = packed ? 1 : 0
95
+ end
96
+ end
97
+
98
+
99
+ # List of number types
100
+ NUMBER_TYPES = [
101
+ Type::INT8,
102
+ Type::UINT8,
103
+ Type::INT16,
104
+ Type::UINT16,
105
+ Type::INT32,
106
+ Type::UINT32,
107
+ Type::LONG,
108
+ Type::ULONG,
109
+ Type::INT64,
110
+ Type::UINT64,
111
+ Type::FLOAT32,
112
+ Type::FLOAT64,
113
+ Type::LONGDOUBLE,
114
+ Type::BOOL,
115
+ ].freeze
116
+
117
+ # @param [String, Symbol] name name of the field
118
+ # @param [Array, DataConverter, Struct, StructLayout::Field, Symbol, Type] type type of the field
119
+ # @param [Integer, nil] offset
120
+ # @return [self]
121
+ # Add a field to the builder.
122
+ # @note Setting +offset+ to +nil+ or +-1+ is equivalent to +0+.
123
+ def add(name, type, offset = nil)
124
+
125
+ if offset.nil? || offset == -1
126
+ offset = @union ? 0 : align(@size, @packed ? [ @packed, type.alignment ].min : [ @min_alignment, type.alignment ].max)
127
+ end
128
+
129
+ #
130
+ # If a FFI::Type type was passed in as the field arg, try and convert to a StructLayout::Field instance
131
+ #
132
+ field = type.is_a?(StructLayout::Field) ? type : field_for_type(name, offset, type)
133
+ @fields << field
134
+ @alignment = [ @alignment, field.alignment ].max unless @packed
135
+ @size = [ @size, field.size + (@union ? 0 : field.offset) ].max
136
+
137
+ return self
138
+ end
139
+
140
+ # @param (see #add)
141
+ # @return (see #add)
142
+ # Same as {#add}.
143
+ # @see #add
144
+ def add_field(name, type, offset = nil)
145
+ add(name, type, offset)
146
+ end
147
+
148
+ # @param (see #add)
149
+ # @return (see #add)
150
+ # Add a struct as a field to the builder.
151
+ def add_struct(name, type, offset = nil)
152
+ add(name, Type::Struct.new(type), offset)
153
+ end
154
+
155
+ # @param name (see #add)
156
+ # @param type (see #add)
157
+ # @param [Integer] count array length
158
+ # @param offset (see #add)
159
+ # @return (see #add)
160
+ # Add an array as a field to the builder.
161
+ def add_array(name, type, count, offset = nil)
162
+ add(name, Type::Array.new(type, count), offset)
163
+ end
164
+
165
+ # @return [StructLayout]
166
+ # Build and return the struct layout.
167
+ def build
168
+ # Add tail padding if the struct is not packed
169
+ size = @packed ? @size : align(@size, @alignment)
170
+
171
+ layout = StructLayout.new(@fields, size, @alignment)
172
+ layout.__union! if @union
173
+ layout
174
+ end
175
+
176
+ private
177
+
178
+ # @param [Integer] offset
179
+ # @param [Integer] align
180
+ # @return [Integer]
181
+ def align(offset, align)
182
+ align + ((offset - 1) & ~(align - 1));
183
+ end
184
+
185
+ # @param (see #add)
186
+ # @return [StructLayout::Field]
187
+ def field_for_type(name, offset, type)
188
+ field_class = case
189
+ when type.is_a?(Type::Function)
190
+ StructLayout::Function
191
+
192
+ when type.is_a?(Type::Struct)
193
+ StructLayout::InnerStruct
194
+
195
+ when type.is_a?(Type::Array)
196
+ StructLayout::Array
197
+
198
+ when type.is_a?(FFI::Enum)
199
+ StructLayout::Enum
200
+
201
+ when NUMBER_TYPES.include?(type)
202
+ StructLayout::Number
203
+
204
+ when type == Type::POINTER
205
+ StructLayout::Pointer
206
+
207
+ when type == Type::STRING
208
+ StructLayout::String
209
+
210
+ when type.is_a?(Class) && type < StructLayout::Field
211
+ type
212
+
213
+ when type.is_a?(DataConverter)
214
+ return StructLayout::Mapped.new(name, offset, Type::Mapped.new(type), field_for_type(name, offset, type.native_type))
215
+
216
+ when type.is_a?(Type::Mapped)
217
+ return StructLayout::Mapped.new(name, offset, type, field_for_type(name, offset, type.native_type))
218
+
219
+ else
220
+ raise TypeError, "invalid struct field type #{type.inspect}"
221
+ end
222
+
223
+ field_class.new(name, offset, type)
224
+ end
225
+ end
226
+
227
+ end
@@ -0,0 +1,232 @@
1
+ require 'tempfile'
2
+ require 'open3'
3
+
4
+ module FFI
5
+
6
+ # ConstGenerator turns C constants into ruby values.
7
+ #
8
+ # @example a simple example for stdio
9
+ # require 'ffi/tools/const_generator'
10
+ # cg = FFI::ConstGenerator.new('stdio') do |gen|
11
+ # gen.const(:SEEK_SET)
12
+ # gen.const('SEEK_CUR')
13
+ # gen.const('seek_end') # this constant does not exist
14
+ # end # #calculate called automatically at the end of the block
15
+ #
16
+ # cg['SEEK_SET'] # => 0
17
+ # cg['SEEK_CUR'] # => 1
18
+ # cg['seek_end'] # => nil
19
+ # cg.to_ruby # => "SEEK_SET = 0\nSEEK_CUR = 1\n# seek_end not available"
20
+ class ConstGenerator
21
+ @options = {}
22
+ attr_reader :constants
23
+
24
+ # Creates a new constant generator that uses +prefix+ as a name, and an
25
+ # options hash.
26
+ #
27
+ # The only option is +:required+, which if set to +true+ raises an error if a
28
+ # constant you have requested was not found.
29
+ #
30
+ # @param [#to_s] prefix
31
+ # @param [Hash] options
32
+ # @return
33
+ # @option options [Boolean] :required
34
+ # @overload initialize(prefix, options)
35
+ # @overload initialize(prefix, options) { |gen| ... }
36
+ # @yieldparam [ConstGenerator] gen new generator is passed to the block
37
+ # When passed a block, {#calculate} is automatically called at the end of
38
+ # the block, otherwise you must call it yourself.
39
+ def initialize(prefix = nil, options = {})
40
+ @includes = ['stdio.h', 'stddef.h']
41
+ @constants = {}
42
+ @prefix = prefix
43
+
44
+ @required = options[:required]
45
+ @options = options
46
+
47
+ if block_given? then
48
+ yield self
49
+ calculate self.class.options.merge(options)
50
+ end
51
+ end
52
+ # Set class options
53
+ # These options are merged with {#initialize} options when it is called with a block.
54
+ # @param [Hash] options
55
+ # @return [Hash] class options
56
+ def self.options=(options)
57
+ @options = options
58
+ end
59
+ # Get class options.
60
+ # @return [Hash] class options
61
+ def self.options
62
+ @options
63
+ end
64
+ # @param [String] name
65
+ # @return constant value (converted if a +converter+ was defined).
66
+ # Access a constant by name.
67
+ def [](name)
68
+ @constants[name].converted_value
69
+ end
70
+
71
+ # Request the value for C constant +name+.
72
+ #
73
+ # @param [#to_s] name C constant name
74
+ # @param [String] format a printf format string to print the value out
75
+ # @param [String] cast a C cast for the value
76
+ # @param ruby_name alternate ruby name for {#to_ruby}
77
+ #
78
+ # @overload const(name, format=nil, cast='', ruby_name=nil, converter=nil)
79
+ # +converter+ is a Method or a Proc.
80
+ # @param [#call] converter convert the value from a string to the appropriate
81
+ # type for {#to_ruby}.
82
+ # @overload const(name, format=nil, cast='', ruby_name=nil) { |value| ... }
83
+ # Use a converter block. This block convert the value from a string to the
84
+ # appropriate type for {#to_ruby}.
85
+ # @yieldparam value constant value
86
+ def const(name, format = nil, cast = '', ruby_name = nil, converter = nil,
87
+ &converter_proc)
88
+ format ||= '%d'
89
+ cast ||= ''
90
+
91
+ if converter_proc and converter then
92
+ raise ArgumentError, "Supply only converter or converter block"
93
+ end
94
+
95
+ converter = converter_proc if converter.nil?
96
+
97
+ const = Constant.new name, format, cast, ruby_name, converter
98
+ @constants[name.to_s] = const
99
+ return const
100
+ end
101
+
102
+ # Calculate constants values.
103
+ # @param [Hash] options
104
+ # @option options [String] :cppflags flags for C compiler
105
+ # @return [nil]
106
+ # @raise if a constant is missing and +:required+ was set to +true+ (see {#initialize})
107
+ def calculate(options = {})
108
+ binary_path = nil
109
+
110
+ Tempfile.open("#{@prefix}.const_generator") do |f|
111
+ binary_path = f.path + ".bin"
112
+ @includes.each do |inc|
113
+ f.puts "#include <#{inc}>"
114
+ end
115
+ f.puts "\nint main(int argc, char **argv)\n{"
116
+
117
+ @constants.each_value do |const|
118
+ f.puts <<-EOF
119
+ #ifdef #{const.name}
120
+ printf("#{const.name} #{const.format}\\n", #{const.cast}#{const.name});
121
+ #endif
122
+ EOF
123
+ end
124
+
125
+ f.puts "\n\treturn 0;\n}"
126
+ f.flush
127
+
128
+ cc = ENV['CC'] || 'gcc'
129
+ output = `#{cc} #{options[:cppflags]} -D_DARWIN_USE_64_BIT_INODE -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -x c -Wall -Werror #{f.path} -o #{binary_path} 2>&1`
130
+
131
+ unless $?.success? then
132
+ output = output.split("\n").map { |l| "\t#{l}" }.join "\n"
133
+ raise "Compilation error generating constants #{@prefix}:\n#{output}"
134
+ end
135
+ end
136
+
137
+ output = `#{binary_path}`
138
+ File.unlink(binary_path + (FFI::Platform.windows? ? ".exe" : ""))
139
+ output.each_line do |line|
140
+ line =~ /^(\S+)\s(.*)$/
141
+ const = @constants[$1]
142
+ const.value = $2
143
+ end
144
+
145
+ missing_constants = @constants.select do |name, constant|
146
+ constant.value.nil?
147
+ end.map { |name,| name }
148
+
149
+ if @required and not missing_constants.empty? then
150
+ raise "Missing required constants for #{@prefix}: #{missing_constants.join ', '}"
151
+ end
152
+ end
153
+
154
+ # Dump constants to +io+.
155
+ # @param [#puts] io
156
+ # @return [nil]
157
+ def dump_constants(io)
158
+ @constants.each do |name, constant|
159
+ name = [@prefix, name].join '.' if @prefix
160
+ io.puts "#{name} = #{constant.converted_value}"
161
+ end
162
+ end
163
+
164
+ # Outputs values for discovered constants. If the constant's value was
165
+ # not discovered it is not omitted.
166
+ # @return [String]
167
+ def to_ruby
168
+ @constants.sort_by { |name,| name }.map do |name, constant|
169
+ if constant.value.nil? then
170
+ "# #{name} not available"
171
+ else
172
+ constant.to_ruby
173
+ end
174
+ end.join "\n"
175
+ end
176
+
177
+ # Add additional C include file(s) to calculate constants from.
178
+ # @note +stdio.h+ and +stddef.h+ automatically included
179
+ # @param [List<String>, Array<String>] i include file(s)
180
+ # @return [Array<String>] array of include files
181
+ def include(*i)
182
+ @includes |= i.flatten
183
+ end
184
+
185
+ end
186
+
187
+ # This class hold constants for {ConstGenerator}
188
+ class ConstGenerator::Constant
189
+
190
+ attr_reader :name, :format, :cast
191
+ attr_accessor :value
192
+
193
+ # @param [#to_s] name
194
+ # @param [String] format a printf format string to print the value out
195
+ # @param [String] cast a C cast for the value
196
+ # @param ruby_name alternate ruby name for {#to_ruby}
197
+ # @param [#call] converter convert the value from a string to the appropriate
198
+ # type for {#to_ruby}.
199
+ def initialize(name, format, cast, ruby_name = nil, converter=nil)
200
+ @name = name
201
+ @format = format
202
+ @cast = cast
203
+ @ruby_name = ruby_name
204
+ @converter = converter
205
+ @value = nil
206
+ end
207
+
208
+ # Return constant value (converted if a +converter+ was defined).
209
+ # @return constant value.
210
+ def converted_value
211
+ if @converter
212
+ @converter.call(@value)
213
+ else
214
+ @value
215
+ end
216
+ end
217
+
218
+ # get constant ruby name
219
+ # @return [String]
220
+ def ruby_name
221
+ @ruby_name || @name
222
+ end
223
+
224
+ # Get an evaluable string from constant.
225
+ # @return [String]
226
+ def to_ruby
227
+ "#{ruby_name} = #{converted_value}"
228
+ end
229
+
230
+ end
231
+
232
+ end