Module: Fiddle::FFIBackend

Defined in:
lib/fiddle/ffi_backend.rb

Constant Summary collapse

FFITypes =
{
    'c' => FFI::Type::INT8,
    'h' => FFI::Type::INT16,
    'i' => FFI::Type::INT32,
    'l' => FFI::Type::LONG,
    'f' => FFI::Type::FLOAT32,
    'd' => FFI::Type::FLOAT64,
    'p' => FFI::Type::POINTER,
    's' => FFI::Type::STRING,

    Types::VOID => FFI::Type::Builtin::VOID,
    Types::VOIDP => FFI::Type::Builtin::POINTER,
    Types::CHAR => FFI::Type::Builtin::CHAR,
    Types::UCHAR => FFI::Type::Builtin::UCHAR,
    Types::SHORT => FFI::Type::Builtin::SHORT,
    Types::USHORT => FFI::Type::Builtin::USHORT,
    Types::INT => FFI::Type::Builtin::INT,
    Types::UINT => FFI::Type::Builtin::UINT,
    Types::LONG => FFI::Type::Builtin::LONG,
    Types::ULONG => FFI::Type::Builtin::ULONG,
    Types::LONG_LONG => FFI::Type::Builtin::LONG_LONG,
    Types::ULONG_LONG => FFI::Type::Builtin::ULONG_LONG,
    Types::FLOAT => FFI::Type::Builtin::FLOAT,
    Types::DOUBLE => FFI::Type::Builtin::DOUBLE,
    Types::BOOL => FFI::Type::Builtin::BOOL,
    Types::CONST_STRING => FFI::Type::Builtin::POINTER,
    Types::VARIADIC => FFI::Type::Builtin::VARARGS,
}

Class Method Summary collapse

Class Method Details

.to_ffi_type(fiddle_type) ⇒ Object

Raises:

  • (TypeError)


107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/fiddle/ffi_backend.rb', line 107

def self.to_ffi_type(fiddle_type)
  if fiddle_type.is_a?(Symbol)
    fiddle_type = Types.const_get(fiddle_type.to_s.upcase)
  end
  if !fiddle_type.is_a?(Integer) && fiddle_type.respond_to?(:to_int)
    fiddle_type = fiddle_type.to_int
  end
  ffi_type = FFITypes[fiddle_type]
  ffi_type = FFITypes[-fiddle_type] if ffi_type.nil? && fiddle_type.is_a?(Integer) && fiddle_type < 0
  raise TypeError.new("cannot convert #{fiddle_type} to ffi") unless ffi_type
  ffi_type
end