Module: FFI::Native::Library

Defined in:
lib/ffi/native/library.rb

Constant Summary collapse

DEFAULT_FLAGS =
FFI::DynamicLibrary::RTLD_LAZY | FFI::DynamicLibrary::RTLD_LOCAL

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(target) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/ffi/native/library.rb', line 11

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ffi/native/library.rb', line 39

def ffi_attach_function(name, argument_types, return_type = :void, as: name, **options)
	argument_types = argument_types.map{|type| self.ffi_find_type(type)}
	return_type = self.ffi_find_type(return_type)
	
	options[:convention] ||= @ffi_calling_convention
	options[:type_map] ||= @ffi_type_map
	options[: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, options)
			else
				invoker = Function.new(return_type, argument_types, function, options)
			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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ffi/native/library.rb', line 75

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



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ffi/native/library.rb', line 110

def ffi_callback(argument_types, return_type, **options)
	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
	
	options[:convention] ||= @ffi_calling_convention
	options[: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, options)
end

#ffi_calling_convention(value = nil) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/ffi/native/library.rb', line 31

def ffi_calling_convention(value = nil)
	if value
		@ffi_calling_convention = value
	end
	
	return @ffi_calling_convention
end

#ffi_define_bitmask(name, *arguments) ⇒ Object



153
154
155
156
157
# File 'lib/ffi/native/library.rb', line 153

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



128
129
130
131
132
133
134
# File 'lib/ffi/native/library.rb', line 128

def ffi_define_callback(name, *arguments, **options)
	callback = ffi_callback(*arguments, **options)
	
	ffi_define_type(name, callback)
	
	return callback
end

#ffi_define_enumeration(name, *arguments) ⇒ Object



147
148
149
150
151
# File 'lib/ffi/native/library.rb', line 147

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



136
137
138
139
140
141
142
143
144
145
# File 'lib/ffi/native/library.rb', line 136

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



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/ffi/native/library.rb', line 159

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



22
23
24
25
26
27
28
29
# File 'lib/ffi/native/library.rb', line 22

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