Module: FFI::Library
- Included in:
- Foo, INotify::Native, Inotify, LibC, PTY::LibC, Sys::Uname
- Defined in:
- lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/ffi-1.15.5/lib/ffi/library.rb
Overview
Constant Summary collapse
- CURRENT_PROCESS =
FFI::CURRENT_PROCESS
- LIBC =
FFI::Platform::LIBC
- FlagsMap =
Flags used in #ffi_lib.
This map allows you to supply symbols to #ffi_lib_flags instead of the actual constants.
{ :global => DynamicLibrary::RTLD_GLOBAL, :local => DynamicLibrary::RTLD_LOCAL, :lazy => DynamicLibrary::RTLD_LAZY, :now => DynamicLibrary::RTLD_NOW }
Class Method Summary collapse
-
.extended(mod) ⇒ nil
Test if extended object is a Module.
Instance Method Summary collapse
-
#attach_function(name, func, args, returns = nil, options = nil) ⇒ FFI::VariadicInvoker
Attach C function
func
to this module. -
#attach_variable(mname, a1, a2 = nil) ⇒ DynamicLibrary::Symbol
Attach C variable
cname
to this module. -
#bitmask(*args) ⇒ FFI::Bitmask
Create a new FFI::Bitmask.
- #callback(*args) ⇒ FFI::CallbackInfo
-
#enum(*args) ⇒ FFI::Enum
Create a new Enum.
-
#enum_type(name) ⇒ FFI::Enum
Find an enum by name.
-
#enum_value(symbol) ⇒ FFI::Enum
Find an enum by a symbol it contains.
-
#ffi_convention(convention = nil) ⇒ Symbol
Set the calling convention for #attach_function and #callback.
-
#ffi_lib(*names) ⇒ Array<DynamicLibrary>
Load native libraries.
-
#ffi_lib_flags(*flags) ⇒ Fixnum
Sets library flags for #ffi_lib.
-
#ffi_libraries ⇒ Array<FFI::DynamicLibrary>
Get FFI libraries loaded using #ffi_lib.
-
#find_type(t) ⇒ Type
Find a type definition.
-
#function_names(name, arg_types) ⇒ Array<String>
This function returns a list of possible names to lookup.
-
#typedef(old, add, info = nil) ⇒ FFI::Enum, FFI::Type
Register or get an already registered type definition.
Class Method Details
.extended(mod) ⇒ nil
Test if extended object is a Module. If not, raise RuntimeError.
86 87 88 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/ffi-1.15.5/lib/ffi/library.rb', line 86 def self.extended(mod) raise RuntimeError.new("must only be extended by module") unless mod.kind_of?(::Module) end |
Instance Method Details
#attach_function(func, args, returns, options = {}) ⇒ FFI::VariadicInvoker #attach_function(name, func, args, returns, options = {}) ⇒ FFI::VariadicInvoker
Attach C function func
to this module.
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/ffi-1.15.5/lib/ffi/library.rb', line 234 def attach_function(name, func, args, returns = nil, = nil) mname, a2, a3, a4, a5 = name, func, args, returns, cname, arg_types, ret_type, opts = (a4 && (a2.is_a?(String) || a2.is_a?(Symbol))) ? [ a2, a3, a4, a5 ] : [ mname.to_s, a2, a3, a4 ] # Convert :foo to the native type arg_types = arg_types.map { |e| find_type(e) } = { :convention => ffi_convention, :type_map => defined?(@ffi_typedefs) ? @ffi_typedefs : nil, :blocking => defined?(@blocking) && @blocking, :enums => defined?(@ffi_enums) ? @ffi_enums : nil, } @blocking = false .merge!(opts) if opts && opts.is_a?(Hash) # Try to locate the function in any of the libraries invokers = [] ffi_libraries.each do |lib| if invokers.empty? begin function = nil function_names(cname, arg_types).find do |fname| function = lib.find_function(fname) end raise LoadError unless function invokers << if arg_types.length > 0 && arg_types[arg_types.length - 1] == FFI::NativeType::VARARGS VariadicInvoker.new(function, arg_types, find_type(ret_type), ) else Function.new(find_type(ret_type), arg_types, function, ) end rescue LoadError end end end invoker = invokers.compact.shift raise FFI::NotFoundError.new(cname.to_s, ffi_libraries.map { |lib| lib.name }) unless invoker invoker.attach(self, mname.to_s) invoker end |
#attach_variable(mname, cname, type) ⇒ DynamicLibrary::Symbol #attach_variable(cname, type) ⇒ DynamicLibrary::Symbol
Attach C variable cname
to this module.
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/ffi-1.15.5/lib/ffi/library.rb', line 331 def attach_variable(mname, a1, a2 = nil) cname, type = a2 ? [ a1, a2 ] : [ mname.to_s, a1 ] address = nil ffi_libraries.each do |lib| begin address = lib.find_variable(cname.to_s) break unless address.nil? rescue LoadError end end raise FFI::NotFoundError.new(cname, ffi_libraries) if address.nil? || address.null? if type.is_a?(Class) && type < FFI::Struct # If it is a global struct, just attach directly to the pointer s = s = type.new(address) # Assigning twice to suppress unused variable warning self.module_eval <<-code, __FILE__, __LINE__ @@ffi_gvar_#{mname} = s def self.#{mname} @@ffi_gvar_#{mname} end code else sc = Class.new(FFI::Struct) sc.layout :gvar, find_type(type) s = sc.new(address) # # Attach to this module as mname/mname= # self.module_eval <<-code, __FILE__, __LINE__ @@ffi_gvar_#{mname} = s def self.#{mname} @@ffi_gvar_#{mname}[:gvar] end def self.#{mname}=(value) @@ffi_gvar_#{mname}[:gvar] = value end code end address end |
#bitmask(name, values) ⇒ FFI::Bitmask #bitmask(*args) ⇒ FFI::Bitmask #bitmask(values) ⇒ FFI::Bitmask #bitmask(native_type, name, values) ⇒ FFI::Bitmask #bitmask(native_type, *args) ⇒ FFI::Bitmask #bitmask(native_type, values) ⇒ FFI::Bitmask
Create a new FFI::Bitmask
554 555 556 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/ffi-1.15.5/lib/ffi/library.rb', line 554 def bitmask(*args) generic_enum(FFI::Bitmask, *args) end |
#callback(name, params, ret) ⇒ FFI::CallbackInfo #callback(params, ret) ⇒ FFI::CallbackInfo
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/ffi-1.15.5/lib/ffi/library.rb', line 384 def callback(*args) raise ArgumentError, "wrong number of arguments" if args.length < 2 || args.length > 3 name, params, ret = if args.length == 3 args else [ nil, args[0], args[1] ] end native_params = params.map { |e| find_type(e) } raise ArgumentError, "callbacks cannot have variadic parameters" if native_params.include?(FFI::Type::VARARGS) = Hash.new [:convention] = ffi_convention [:enums] = @ffi_enums if defined?(@ffi_enums) ret_type = find_type(ret) if ret_type == Type::STRING raise TypeError, ":string is not allowed as return type of callbacks" end cb = FFI::CallbackInfo.new(ret_type, native_params, ) # Add to the symbol -> type map (unless there was no name) unless name.nil? typedef cb, name end cb end |
#enum(name, values) ⇒ FFI::Enum #enum(*args) ⇒ FFI::Enum #enum(values) ⇒ FFI::Enum #enum(native_type, name, values) ⇒ FFI::Enum #enum(native_type, *args) ⇒ FFI::Enum #enum(native_type, values) ⇒ FFI::Enum
Create a new Enum.
511 512 513 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/ffi-1.15.5/lib/ffi/library.rb', line 511 def enum(*args) generic_enum(FFI::Enum, *args) end |
#enum_type(name) ⇒ FFI::Enum
Find an enum by name.
561 562 563 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/ffi-1.15.5/lib/ffi/library.rb', line 561 def enum_type(name) @ffi_enums.find(name) if defined?(@ffi_enums) end |
#enum_value(symbol) ⇒ FFI::Enum
Find an enum by a symbol it contains.
568 569 570 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/ffi-1.15.5/lib/ffi/library.rb', line 568 def enum_value(symbol) @ffi_enums.__map_symbol(symbol) end |
#ffi_convention(convention = nil) ⇒ Symbol
:stdcall
is typically used for attaching Windows API functions
Set the calling convention for #attach_function and #callback
163 164 165 166 167 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/ffi-1.15.5/lib/ffi/library.rb', line 163 def ffi_convention(convention = nil) @ffi_convention ||= :default @ffi_convention = convention if convention @ffi_convention end |
#ffi_lib(*names) ⇒ Array<DynamicLibrary>
Load native libraries.
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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/ffi-1.15.5/lib/ffi/library.rb', line 95 def ffi_lib(*names) raise LoadError.new("library names list must not be empty") if names.empty? lib_flags = defined?(@ffi_lib_flags) ? @ffi_lib_flags : FFI::DynamicLibrary::RTLD_LAZY | FFI::DynamicLibrary::RTLD_LOCAL ffi_libs = names.map do |name| if name == FFI::CURRENT_PROCESS FFI::DynamicLibrary.open(nil, FFI::DynamicLibrary::RTLD_LAZY | FFI::DynamicLibrary::RTLD_LOCAL) else libnames = (name.is_a?(::Array) ? name : [ name ]).map(&:to_s).map { |n| [ n, FFI.map_library_name(n) ].uniq }.flatten.compact lib = nil errors = {} libnames.each do |libname| begin orig = libname lib = FFI::DynamicLibrary.open(libname, lib_flags) break if lib rescue Exception => ex ldscript = false if ex. =~ /(([^ \t()])+\.so([^ \t:()])*):([ \t])*(invalid ELF header|file too short|invalid file format)/ if File.binread($1) =~ /(?:GROUP|INPUT) *\( *([^ \)]+)/ libname = $1 ldscript = true end end if ldscript retry else # TODO better library lookup logic unless libname.start_with?("/") || FFI::Platform.windows? path = ['/usr/lib/','/usr/local/lib/','/opt/local/lib/', '/opt/homebrew/lib/'].find do |pth| File.exist?(pth + libname) end if path libname = path + libname retry end end libr = (orig == libname ? orig : "#{orig} #{libname}") errors[libr] = ex end end end if lib.nil? raise LoadError.new(errors.values.join(".\n")) end # return the found lib lib end end @ffi_libs = ffi_libs end |
#ffi_lib_flags(*flags) ⇒ Fixnum
Sets library flags for #ffi_lib.
196 197 198 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/ffi-1.15.5/lib/ffi/library.rb', line 196 def ffi_lib_flags(*flags) @ffi_lib_flags = flags.inject(0) { |result, f| result | FlagsMap[f] } end |
#ffi_libraries ⇒ Array<FFI::DynamicLibrary>
Get FFI libraries loaded using #ffi_lib.
173 174 175 176 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/ffi-1.15.5/lib/ffi/library.rb', line 173 def ffi_libraries raise LoadError.new("no library specified") if !defined?(@ffi_libs) || @ffi_libs.empty? @ffi_libs end |
#find_type(t) ⇒ Type
Find a type definition.
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/ffi-1.15.5/lib/ffi/library.rb', line 575 def find_type(t) if t.kind_of?(Type) t elsif defined?(@ffi_typedefs) && @ffi_typedefs.has_key?(t) @ffi_typedefs[t] elsif t.is_a?(Class) && t < Struct Type::POINTER elsif t.is_a?(DataConverter) # Add a typedef so next time the converter is used, it hits the cache typedef Type::Mapped.new(t), t end || FFI.find_type(t) end |
#function_names(name, arg_types) ⇒ Array<String>
Function names on windows may be decorated if they are using stdcall. See
-
en.wikipedia.org/wiki/Name_mangling#C_name_decoration_in_Microsoft_Windows
-
msdn.microsoft.com/en-us/library/zxk0tw93%28v=VS.100%29.aspx
-
en.wikibooks.org/wiki/X86_Disassembly/Calling_Conventions#STDCALL
Note that decorated names can be overridden via def files. Also note that the windows api, although using, doesn’t have decorated names.
This function returns a list of possible names to lookup.
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/ffi-1.15.5/lib/ffi/library.rb', line 289 def function_names(name, arg_types) result = [name.to_s] if ffi_convention == :stdcall # Get the size of each parameter size = arg_types.inject(0) do |mem, arg| size = arg.size # The size must be a multiple of 4 size += (4 - size) % 4 mem + size end result << "_#{name.to_s}@#{size}" # win32 result << "#{name.to_s}@#{size}" # win64 end result end |
#typedef(old, add, info = nil) ⇒ FFI::Enum, FFI::Type
Register or get an already registered type definition.
To register a new type definition, old
should be a Type. add
is in this case the type definition.
If old
is a DataConverter, a Type::Mapped is returned.
If old
is :enum
-
and
add
is anArray
, a call to #enum is made withadd
as single parameter; -
in others cases,
info
is used to create a named enum.
If old
is a key for type map, #typedef get old
type definition.
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/ffi-1.15.5/lib/ffi/library.rb', line 428 def typedef(old, add, info=nil) @ffi_typedefs = Hash.new unless defined?(@ffi_typedefs) @ffi_typedefs[add] = if old.kind_of?(FFI::Type) old elsif @ffi_typedefs.has_key?(old) @ffi_typedefs[old] elsif old.is_a?(DataConverter) FFI::Type::Mapped.new(old) elsif old == :enum if add.kind_of?(Array) self.enum(add) else self.enum(info, add) end else FFI.find_type(old) end end |