ffi-module 0.1.1 → 0.3.0

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: '0966d9341e590e8623b6e5f8b46672e1258e64d76c4b5a623fed1153d982ca97'
4
- data.tar.gz: 7c0d82c153c368cce300aa8e2e4cf45f1f2135707974d4a3da0c1f0ff6bb7aea
3
+ metadata.gz: 1b8ee4221b08ecf01f4453a41392cb0582382f5b8d377ec5a8125605ab4760e8
4
+ data.tar.gz: 4fee30465f129049c8fdd53d231d4a2baff1acacf31d2946eaa302b0551b024f
5
5
  SHA512:
6
- metadata.gz: cfc6f8d0f8d137fed89851a1b94a456b11641dc60289c80fe2122ae8b4dff21ac8144cdd1b92da35a4de9a5e8cd5990ab75c1856f70a0af34257b06f0eff322c
7
- data.tar.gz: d4306604e74a7f8211d4acfa6c2eef9e7e53ae45c9807d8a0b1e2313959a82546664708633bf108f1fc98a52eece56bf6a253d67de6e1fb46335f05066a86b4d
6
+ metadata.gz: fbca3d3dc2cb769015c260a205da7dd3e4fbd2ef8decc7cf79ff23ed6f86ac81a34411dc95a67a1578d87318a9a639a940e2fd94701be4f7a2458faa6f2b905d
7
+ data.tar.gz: 9b2871ac1451939a2b332af2f60cc1885b56b0daa688e882508b16007e88a1069aa5e95b67758ac55c07ba6e0ee2c0cc76788b777998c4405a8a32d9430c22cf
@@ -51,10 +51,16 @@ module FFI
51
51
  end
52
52
  end
53
53
 
54
+ result = false
55
+
54
56
  # Load all specified libraries:
55
57
  names.each do |name|
56
- ffi_load(name, search_paths: search_paths, **options)
58
+ result = ffi_load(name, search_paths: search_paths, **options) || result
57
59
  end
60
+
61
+ return result
62
+ rescue Errno::ENOENT
63
+ return nil
58
64
  end
59
65
  end
60
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
@@ -172,6 +173,29 @@ module FFI
172
173
  ffi_define_generic_enumeration(name, FFI::Bitmask, native_type, *arguments)
173
174
  end
174
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
197
+ end
198
+
175
199
  private
176
200
 
177
201
  def ffi_define_generic_enumeration(name, klass, native_type, values)
@@ -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
@@ -51,6 +51,10 @@ module FFI
51
51
 
52
52
  return nil
53
53
  end
54
+
55
+ def ffi_load_failure(message)
56
+ raise LoadError, message
57
+ end
54
58
  end
55
59
  end
56
60
  end
@@ -31,6 +31,6 @@ end
31
31
 
32
32
  module FFI
33
33
  module Module
34
- VERSION = "0.1.1"
34
+ VERSION = "0.3.0"
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.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams