Module: FFI::Module::Library
- Defined in:
- lib/ffi/module/library.rb
Constant Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
- #ffi_attach_function(name, argument_types, return_type = :void, as: name, **options) ⇒ Object
- #ffi_attach_variable(name, type, as: name) ⇒ Object
- #ffi_callback(argument_types, return_type, **options) ⇒ Object
- #ffi_calling_convention(value = nil) ⇒ Object
- #ffi_define_bitmask(name, *arguments) ⇒ Object
- #ffi_define_callback(name, *arguments, **options) ⇒ Object
- #ffi_define_enumeration(name, *arguments) ⇒ Object
- #ffi_define_type(name, value) ⇒ Object
- #ffi_find_type(argument) ⇒ Object
- #ffi_open_library(name = nil, flags: DEFAULT_FLAGS) ⇒ Object
Class Method Details
.extended(target) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/ffi/module/library.rb', line 28 def self.extended(target) raise "Must only be extended by module, got #{target}!" unless target.kind_of?(::Module) target.instance_variable_set(:@ffi_libraries, Array.new) target.instance_variable_set(:@ffi_calling_convention, :default) target.instance_variable_set(:@ffi_type_map, Hash.new) target.instance_variable_set(:@ffi_enumerations, FFI::Enums.new) end |
Instance Method Details
#ffi_attach_function(name, argument_types, return_type = :void, as: name, **options) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/ffi/module/library.rb', line 56 def ffi_attach_function(name, argument_types, return_type = :void, as: name, **) argument_types = argument_types.map{|type| self.ffi_find_type(type)} return_type = self.ffi_find_type(return_type) [:convention] ||= @ffi_calling_convention [:type_map] ||= @ffi_type_map [:enums] ||= @ffi_enumerations invoker = nil @ffi_libraries.each do |library| function = nil ffi_function_names(name, argument_types).each do |function_name| break if function = library.find_function(function_name.to_s) end if function if argument_types.length > 0 && argument_types.last == FFI::NativeType::VARARGS invoker = VariadicInvoker.new(function, argument_types, return_type, ) else invoker = Function.new(return_type, argument_types, function, ) end break end end if invoker invoker.attach(self, as.to_s) return true else raise FFI::NotFoundError.new(name, @ffi_libraries) end end |
#ffi_attach_variable(name, type, as: name) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/ffi/module/library.rb', line 92 def ffi_attach_variable(name, type, as: name) address = @ffi_libraries.find do |library| begin library.find_variable(name) rescue LoadError end end if address.nil? || address.null? raise FFI::NotFoundError.new(name, @ffi_libraries) end if type.is_a?(Class) && type < FFI::Struct variable = type.new(address) self.define_singleton_method(as) do variable end else container_type = Class.new(FFI::Struct) container_type.layout :value, self.ffi_find_type(type) container = container_type.new(address) self.define_singleton_method(as) do container[:value] end self.define_singleton_method(:"#{as}=") do |value| container[:value] = value end end return true end |
#ffi_callback(argument_types, return_type, **options) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/ffi/module/library.rb', line 127 def ffi_callback(argument_types, return_type, **) argument_types = argument_types.map{|type| self.ffi_find_type(type)} return_type = self.ffi_find_type(return_type) if argument_types.include?(FFI::Type::VARARGS) raise ArgumentError, "Callbacks cannot have variadic parameters!" end [:convention] ||= @ffi_calling_convention [:enums] ||= @ffi_enumerations if return_type == Type::STRING raise TypeError, "String is not allowed as return type of callbacks!" end return FFI::CallbackInfo.new(return_type, argument_types, ) end |
#ffi_calling_convention(value = nil) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/ffi/module/library.rb', line 48 def ffi_calling_convention(value = nil) if value @ffi_calling_convention = value end return @ffi_calling_convention end |
#ffi_define_bitmask(name, *arguments) ⇒ Object
170 171 172 173 174 |
# File 'lib/ffi/module/library.rb', line 170 def ffi_define_bitmask(name, *arguments) native_type = arguments.first.kind_of?(FFI::Type) ? arguments.shift : nil ffi_define_generic_enumeration(name, FFI::Bitmask, native_type, *arguments) end |
#ffi_define_callback(name, *arguments, **options) ⇒ Object
145 146 147 148 149 150 151 |
# File 'lib/ffi/module/library.rb', line 145 def ffi_define_callback(name, *arguments, **) callback = ffi_callback(*arguments, **) ffi_define_type(name, callback) return callback end |
#ffi_define_enumeration(name, *arguments) ⇒ Object
164 165 166 167 168 |
# File 'lib/ffi/module/library.rb', line 164 def ffi_define_enumeration(name, *arguments) native_type = arguments.first.kind_of?(FFI::Type) ? arguments.shift : nil ffi_define_generic_enumeration(name, FFI::Enum, native_type, *arguments) end |
#ffi_define_type(name, value) ⇒ Object
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/ffi/module/library.rb', line 153 def ffi_define_type(name, value) case value when FFI::Type @ffi_type_map[name] = value when FFI::DataConverter @ffi_type_map[name] = FFI::Type::Mapped.new(value) else @ffi_type_map[name] = self.ffi_find_type(value) end end |
#ffi_find_type(argument) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/ffi/module/library.rb', line 176 def ffi_find_type(argument) if argument.kind_of?(Type) return argument end if type = @ffi_type_map[argument] return type end if argument.is_a?(Class) && argument < Struct return Type::POINTER end if argument.is_a?(DataConverter) # Cache the mapped type: return ffi_define_type(argument, Type::Mapped.new(argument)) end if argument return FFI.find_type(argument) end end |
#ffi_open_library(name = nil, flags: DEFAULT_FLAGS) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/ffi/module/library.rb', line 39 def ffi_open_library(name = nil, flags: DEFAULT_FLAGS) @ffi_libraries << DynamicLibrary.open(name, flags) return true rescue LoadError, RuntimeError # TruffleRuby raises a RuntimeError if the library can't be found. return nil end |