Module

A libffi wrapper for Ruby.

Description

Fiddle is an extension to translate a foreign function interface (FFI) with ruby.

It wraps libffi, a popular C library which provides a portable interface that allows code written in one language to call code written in another language.

Example

Here we will use Fiddle::Function to wrap floor(3) from libm

require 'fiddle'

libm = Fiddle.dlopen('/lib/libm.so.6')

floor = Fiddle::Function.new(
  libm['floor'],
  [Fiddle::TYPE_DOUBLE],
  Fiddle::TYPE_DOUBLE
)

puts floor.call(3.14159) #=> 3.0
Constants

ALIGN_VOIDP

The alignment size of a void*

ALIGN_CHAR

The alignment size of a char

ALIGN_SHORT

The alignment size of a short

ALIGN_INT

The alignment size of an int

ALIGN_LONG

The alignment size of a long

ALIGN_LONG_LONG

The alignment size of a long long

ALIGN_INT8_T

The alignment size of a int8_t

ALIGN_INT16_T

The alignment size of a int16_t

ALIGN_INT32_T

The alignment size of a int32_t

ALIGN_INT64_T

The alignment size of a int64_t

ALIGN_FLOAT

The alignment size of a float

ALIGN_DOUBLE

The alignment size of a double

ALIGN_SIZE_T

The alignment size of a size_t

ALIGN_SSIZE_T

The alignment size of a ssize_t

ALIGN_PTRDIFF_T

The alignment size of a ptrdiff_t

ALIGN_INTPTR_T

The alignment size of a intptr_t

ALIGN_UINTPTR_T

The alignment size of a uintptr_t

ALIGN_BOOL

The alignment size of a bool

Returns a boolean regarding whether the host is WIN32

SIZEOF_VOIDP

size of a void*

SIZEOF_CHAR

size of a char

SIZEOF_UCHAR

size of a unsigned char

SIZEOF_SHORT

size of a short

SIZEOF_USHORT

size of a unsigned short

SIZEOF_INT

size of an int

SIZEOF_UINT

size of an unsigned int

SIZEOF_LONG

size of a long

SIZEOF_ULONG

size of a unsigned long

SIZEOF_LONG_LONG

size of a long long

SIZEOF_ULONG_LONG

size of a unsigned long long

SIZEOF_INT8_T

size of a int8_t

SIZEOF_UINT8_T

size of a uint8_t

SIZEOF_INT16_T

size of a int16_t

SIZEOF_UINT16_T

size of a uint16_t

SIZEOF_INT32_T

size of a int32_t

SIZEOF_UINT32_T

size of a uint32_t

SIZEOF_INT64_T

size of a int64_t

SIZEOF_UINT64_T

size of a uint64_t

SIZEOF_FLOAT

size of a float

SIZEOF_DOUBLE

size of a double

SIZEOF_SIZE_T

size of a size_t

SIZEOF_SSIZE_T

size of a ssize_t

SIZEOF_PTRDIFF_T

size of a ptrdiff_t

SIZEOF_INTPTR_T

size of a intptr_t

SIZEOF_UINTPTR_T

size of a uintptr_t

SIZEOF_BOOL

size of a bool

RUBY_FREE

Address of the ruby_xfree() function

BUILD_RUBY_PLATFORM

Platform built against (i.e. “x86_64-linux”, etc.)

See also RUBY_PLATFORM

Qtrue

The value of Qtrue

Qfalse

The value of Qfalse

Qnil

The value of Qnil

Qundef

The value of Qundef

Add constants for backwards compat

No documentation available
No documentation available
No documentation available
Class Methods

Creates a new handler that opens library, and returns an instance of Fiddle::Handle.

If nil is given for the library, Fiddle::Handle::DEFAULT is used, which is the equivalent to RTLD_DEFAULT. See man 3 dlopen for more.

lib = Fiddle.dlopen(nil)

The default is dependent on OS, and provide a handle for all libraries already loaded. For example, in most cases you can use this to access libc functions, or ruby functions like rb_str_new.

See Fiddle::Handle.new for more.

Returns the Ruby object stored at the memory address addr

Example:

x = Object.new
# => #<Object:0x0000000107c7d870>
Fiddle.dlwrap(x)
# => 4425504880
Fiddle.dlunwrap(_)
# => #<Object:0x0000000107c7d870>

Returns the memory address of the Ruby object stored at val

Example:

x = Object.new
# => #<Object:0x0000000107c7d870>
Fiddle.dlwrap(x)
# => 4425504880

In the case val is not a heap allocated object, this method will return the tagged pointer value.

Example:

Fiddle.dlwrap(123)
# => 247

Free the memory at address addr

Returns the last Error of the current executing Thread or nil if none

Sets the last Error of the current executing Thread to error

Allocate size bytes of memory and return the integer memory address for the allocated memory.

Change the size of the memory allocated at the memory location addr to size bytes. Returns the memory address of the reallocated memory, which may be different than the address passed in.

Returns the last win32 Error of the current executing Thread or nil if none

Sets the last win32 Error of the current executing Thread to error

Returns the last win32 socket Error of the current executing Thread or nil if none

Sets the last win32 socket Error of the current executing Thread to error

Instance Methods

Creates a new handler that opens library, and returns an instance of Fiddle::Handle.

If nil is given for the library, Fiddle::Handle::DEFAULT is used, which is the equivalent to RTLD_DEFAULT. See man 3 dlopen for more.

lib = Fiddle.dlopen(nil)

The default is dependent on OS, and provide a handle for all libraries already loaded. For example, in most cases you can use this to access libc functions, or ruby functions like rb_str_new.

See Fiddle::Handle.new for more.