Module: FFI

Defined in:
lib/ffi/io.rb,
lib/ffi/enum.rb,
lib/ffi/errno.rb,
lib/ffi/types.rb,
lib/ffi/union.rb,
lib/ffi/struct.rb,
lib/ffi/library.rb,
lib/ffi/pointer.rb,
lib/ffi/version.rb,
lib/ffi/platform.rb,
lib/ffi/variadic.rb,
lib/ffi/autopointer.rb,
lib/ffi/managedstruct.rb,
lib/ffi/struct_layout.rb,
lib/ffi/data_converter.rb,
lib/ffi/tools/generator.rb,
lib/ffi/struct_by_reference.rb,
lib/ffi/struct_layout_builder.rb,
lib/ffi/tools/const_generator.rb,
lib/ffi/tools/types_generator.rb,
lib/ffi/tools/struct_generator.rb,
ext/ffi_c/ffi.c,
ext/ffi_c/ffi.c

Overview

This module embbed type constants from NativeType.

Defined Under Namespace

Modules: DataConverter, IO, LastError, Library, NativeType, Platform Classes: AbstractMemory, ArrayType, AutoPointer, Bitmask, Buffer, ConstGenerator, DynamicLibrary, Enum, Enums, Function, FunctionType, Generator, ManagedStruct, MemoryPointer, NotFoundError, NullPointerError, PlatformError, Pointer, StrPtrConverter, Struct, StructByReference, StructByValue, StructGenerator, StructLayout, StructLayoutBuilder, Type, TypesGenerator, Union, VariadicInvoker

Constant Summary collapse

CURRENT_PROCESS =
USE_THIS_PROCESS_AS_LIBRARY = Object.new
VERSION =
'1.11.1'
TypeDefs =

FFI::TypeDefs

Document-constant
SizeTypes =
sizeMap = rb_hash_new()
CallbackInfo =
rbffi_FunctionTypeClass
FunctionInfo =
rbffi_FunctionTypeClass
NativeLibrary =

backwards compat library

LibraryClass

Class Method Summary collapse

Class Method Details

.add_typedef(old, add) ⇒ Type

Add a definition type to type definitions.

Parameters:

  • old (Type, DataConverter, Symbol)

    type definition used by find_type

  • add (Symbol)

    new type definition’s name to add

Returns:



45
46
47
# File 'lib/ffi/types.rb', line 45

def self.add_typedef(old, add)
  typedef old, add
end

.errnoNumeric

Returns:

  • (Numeric)

See Also:



34
35
36
# File 'lib/ffi/errno.rb', line 34

def self.errno
  FFI::LastError.error
end

.errno=(error) ⇒ nil

Parameters:

  • error (Numeric)

Returns:

  • (nil)

See Also:



40
41
42
# File 'lib/ffi/errno.rb', line 40

def self.errno=(error)
  FFI::LastError.error = error
end

.find_type(name, type_map = nil) ⇒ Type

Find a type in type_map (TypeDefs, by default) from a type objet, a type name (symbol). If name is a DataConverter, a new FFI::Type::Mapped is created.

Parameters:

Returns:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ffi/types.rb', line 56

def self.find_type(name, type_map = nil)
  if name.is_a?(Type)
    name

  elsif type_map && type_map.has_key?(name)
    type_map[name]

  elsif TypeDefs.has_key?(name)
    TypeDefs[name]

  elsif name.is_a?(DataConverter)
    (type_map || TypeDefs)[name] = Type::Mapped.new(name)
  else
    raise TypeError, "unable to resolve type '#{name}'"
  end
end

.map_library_name(lib) ⇒ String

Transform a generic library name to a platform library name

Examples:

# Linux
FFI.map_library_name 'c'     # -> "libc.so.6"
FFI.map_library_name 'jpeg'  # -> "libjpeg.so"
# Windows
FFI.map_library_name 'c'     # -> "msvcrt.dll"
FFI.map_library_name 'jpeg'  # -> "jpeg.dll"

Parameters:

  • lib (#to_s)

    library name

Returns:

  • (String)

    library name formatted for current platform



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ffi/library.rb', line 44

def self.map_library_name(lib)
  # Mangle the library name to reflect the native library naming conventions
  lib = Library::LIBC if lib == 'c'

  if lib && File.basename(lib) == lib
    lib = Platform::LIBPREFIX + lib unless lib =~ /^#{Platform::LIBPREFIX}/
    r = Platform::IS_GNU ? "\\.so($|\\.[1234567890]+)" : "\\.#{Platform::LIBSUFFIX}$"
    lib += ".#{Platform::LIBSUFFIX}" unless lib =~ /#{r}/
  end

  lib
end

.type_size(type) ⇒ Numeric

Get type size, in bytes.

Parameters:

  • type

    type is an instance of class accepted by find_type

Returns:

  • (Numeric)


176
177
178
# File 'lib/ffi/types.rb', line 176

def self.type_size(type)
  find_type(type).size
end

.typedef(old, add) ⇒ Type

Add a definition type to type definitions.

Parameters:

  • old (Type, DataConverter, Symbol)

    type definition used by find_type

  • add (Symbol)

    new type definition’s name to add

Returns:



40
41
42
# File 'lib/ffi/types.rb', line 40

def self.typedef(old, add)
  TypeDefs[add] = self.find_type(old)
end