ffi-module 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b5eb3b1aa3f99163ffd15dddc88852709b31677be988ebd0c2d3758866f19d43
4
- data.tar.gz: 138e20272596fe28a109d49634f3357e7c544fe3b266ccc158dfcfc44906efff
3
+ metadata.gz: 7a52ed552e03d97ac432cdb4abda13d253fdecac9e40325473f1f0c9d2a7f3b6
4
+ data.tar.gz: 2bd69738708f23141cb530fce7d8f125fa1094510951455594b9af6f8ef5cfce
5
5
  SHA512:
6
- metadata.gz: bd1516bf65a1d8d19bd4a972f03e47ef078441f6c5927eb699a79428bb727b740e62193f810cce6c2ebc4d4e1e39d90abcf38a076da04c8d4e628af4e1a3ae50
7
- data.tar.gz: e194ca5bd89f3c75716e50b9f389803c73b0a5136e7eef766c43a1b31f0d193e2e097bd93373454d458fdd5057d20042b955b4e8c88b81053ce5aac8038d6dcc
6
+ metadata.gz: bfe6e9cbaed35a167175ef42dd15ba6ab42ccf8d9554c5d1d38aea0a72a4c2996966f4afe111cd572ab7a6886214b4c681121bffa5f82612393dc57665da6e27
7
+ data.tar.gz: 6f2087778c13121cc6c43c9fa2d71343bf9177dbcd0041f33b10122b93877086c311774117b44728202eccd422282819d49d75222d86afd897e6fb6e715a7263
@@ -33,6 +33,8 @@ module FFI
33
33
  return false unless output = ::IO.popen(command).read
34
34
 
35
35
  arguments = ::Shellwords.split(output)
36
+ search_paths = search_paths.dup
37
+ names = names.dup
36
38
 
37
39
  arguments.each do |argument|
38
40
  if match = argument.match(/\A(-[lL])(.*)\z/)
@@ -43,16 +45,22 @@ module FFI
43
45
  when '-l'
44
46
  names << value
45
47
  end
46
- else
48
+ elsif File.directory?(argument)
47
49
  # Assume it's a search path:
48
- search_paths << value
50
+ search_paths << argument
49
51
  end
50
52
  end
51
53
 
54
+ result = false
55
+
52
56
  # Load all specified libraries:
53
57
  names.each do |name|
54
- ffi_load(name, search_paths: search_paths, **options)
58
+ result = ffi_load(name, search_paths: search_paths, **options) || result
55
59
  end
60
+
61
+ return result
62
+ rescue Errno::ENOENT
63
+ return nil
56
64
  end
57
65
  end
58
66
  end
@@ -40,7 +40,8 @@ module FFI
40
40
  @ffi_libraries << DynamicLibrary.open(name, flags)
41
41
 
42
42
  return true
43
- rescue LoadError
43
+ rescue LoadError, RuntimeError
44
+ # TruffleRuby raises a RuntimeError if the library can't be found.
44
45
  return nil
45
46
  end
46
47
 
@@ -52,7 +53,7 @@ module FFI
52
53
  return @ffi_calling_convention
53
54
  end
54
55
 
55
- def ffi_attach_function(name, cname, argument_types, return_type = :void, **options)
56
+ def ffi_attach_function(name, argument_types, return_type = :void, as: name, **options)
56
57
  argument_types = argument_types.map{|type| self.ffi_find_type(type)}
57
58
  return_type = self.ffi_find_type(return_type)
58
59
 
@@ -65,7 +66,7 @@ module FFI
65
66
  @ffi_libraries.each do |library|
66
67
  function = nil
67
68
 
68
- ffi_function_names(cname, argument_types).each do |function_name|
69
+ ffi_function_names(name, argument_types).each do |function_name|
69
70
  break if function = library.find_function(function_name.to_s)
70
71
  end
71
72
 
@@ -81,29 +82,29 @@ module FFI
81
82
  end
82
83
 
83
84
  if invoker
84
- invoker.attach(self, name.to_s)
85
+ invoker.attach(self, as.to_s)
85
86
  return true
86
87
  else
87
- raise FFI::NotFoundError.new(cname, @ffi_libraries)
88
+ raise FFI::NotFoundError.new(name, @ffi_libraries)
88
89
  end
89
90
  end
90
91
 
91
- def ffi_attach_variable(name, cname, type)
92
+ def ffi_attach_variable(name, type, as: name)
92
93
  address = @ffi_libraries.find do |library|
93
94
  begin
94
- library.find_variable(cname)
95
+ library.find_variable(name)
95
96
  rescue LoadError
96
97
  end
97
98
  end
98
99
 
99
100
  if address.nil? || address.null?
100
- raise FFI::NotFoundError.new(cname, @ffi_libraries)
101
+ raise FFI::NotFoundError.new(name, @ffi_libraries)
101
102
  end
102
103
 
103
104
  if type.is_a?(Class) && type < FFI::Struct
104
105
  variable = type.new(address)
105
106
 
106
- self.define_singleton_method(name) do
107
+ self.define_singleton_method(as) do
107
108
  variable
108
109
  end
109
110
  else
@@ -111,11 +112,11 @@ module FFI
111
112
  container_type.layout :value, self.ffi_find_type(type)
112
113
  container = container_type.new(address)
113
114
 
114
- self.define_singleton_method(name) do
115
+ self.define_singleton_method(as) do
115
116
  container[:value]
116
117
  end
117
118
 
118
- self.define_singleton_method(:"#{name}=") do |value|
119
+ self.define_singleton_method(:"#{as}=") do |value|
119
120
  container[:value] = value
120
121
  end
121
122
  end
@@ -150,9 +151,9 @@ module FFI
150
151
  end
151
152
 
152
153
  def ffi_define_type(name, value)
153
- case type
154
+ case value
154
155
  when FFI::Type
155
- @ffi_type_map[name] = type
156
+ @ffi_type_map[name] = value
156
157
  when FFI::DataConverter
157
158
  @ffi_type_map[name] = FFI::Type::Mapped.new(value)
158
159
  else
@@ -163,13 +164,36 @@ module FFI
163
164
  def ffi_define_enumeration(name, *arguments)
164
165
  native_type = arguments.first.kind_of?(FFI::Type) ? arguments.shift : nil
165
166
 
166
- ffi_generic_enumeration(name, FFI::Enum, native_type, arguments)
167
+ ffi_define_generic_enumeration(name, FFI::Enum, native_type, *arguments)
167
168
  end
168
169
 
169
170
  def ffi_define_bitmask(name, *arguments)
170
171
  native_type = arguments.first.kind_of?(FFI::Type) ? arguments.shift : nil
171
172
 
172
- ffi_generic_enumeration(name, FFI::Bitmask, native_type, arguments)
173
+ ffi_define_generic_enumeration(name, FFI::Bitmask, native_type, *arguments)
174
+ end
175
+
176
+ def ffi_find_type(argument)
177
+ if argument.kind_of?(Type)
178
+ return argument
179
+ end
180
+
181
+ if type = @ffi_type_map[argument]
182
+ return type
183
+ end
184
+
185
+ if argument.is_a?(Class) && argument < Struct
186
+ return Type::POINTER
187
+ end
188
+
189
+ if argument.is_a?(DataConverter)
190
+ # Cache the mapped type:
191
+ return ffi_define_type(argument, Type::Mapped.new(argument))
192
+ end
193
+
194
+ if argument
195
+ return FFI.find_type(argument)
196
+ end
173
197
  end
174
198
 
175
199
  private
@@ -213,29 +237,6 @@ module FFI
213
237
 
214
238
  return result
215
239
  end
216
-
217
- def ffi_find_type(argument)
218
- if argument.kind_of?(Type)
219
- return argument
220
- end
221
-
222
- if type = @ffi_type_map[argument]
223
- return type
224
- end
225
-
226
- if argument.is_a?(Class) && argument < Struct
227
- return Type::POINTER
228
- end
229
-
230
- if argument.is_a?(DataConverter)
231
- # Cache the mapped type:
232
- return ffi_define_type(argument, Type::Mapped.new(argument))
233
- end
234
-
235
- if argument
236
- return FFI.find_type(argument)
237
- end
238
- end
239
240
  end
240
241
  end
241
242
  end
@@ -35,22 +35,18 @@ module FFI
35
35
 
36
36
  return nil
37
37
  end
38
-
38
+
39
39
  def ffi_load(name, search_paths: nil, **options)
40
40
  # Try to load the library directly:
41
- return true if ffi_load_library(name, **options)
41
+ return true if ffi_open_library(name, **options)
42
42
 
43
43
  # If that fails, try to load it from the specified search paths:
44
44
  if search_paths&.any?
45
45
  name = FFI.map_library_name(name)
46
46
 
47
- if path = ffi_find_library_path(library_name, search_paths)
48
- if library = ffi_load_library(path, **options)
49
- libraries << library
50
- end
47
+ if path = ffi_find_library_path(name, search_paths)
48
+ return true if ffi_open_library(path, **options)
51
49
  end
52
-
53
- return true if ffi_load_library(name, **options)
54
50
  end
55
51
 
56
52
  return nil
@@ -31,6 +31,6 @@ end
31
31
 
32
32
  module FFI
33
33
  module Module
34
- VERSION = "0.1.0"
34
+ VERSION = "0.2.1"
35
35
  end
36
36
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ffi-module
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams