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
- MODULE Fiddle::BasicTypes
- MODULE Fiddle::CParser
- MODULE Fiddle::CStructBuilder
- MODULE Fiddle::Importer
- MODULE Fiddle::Types
- MODULE Fiddle::Win32Types
- CLASS Fiddle::CStruct
- CLASS Fiddle::CStructEntity
- CLASS Fiddle::CUnion
- CLASS Fiddle::CUnionEntity
- CLASS Fiddle::ClearedReferenceError
- CLASS Fiddle::Closure
- CLASS Fiddle::CompositeHandler
- CLASS Fiddle::DLError
- CLASS Fiddle::Error
- CLASS Fiddle::Function
- CLASS Fiddle::Handle
- CLASS Fiddle::Pinned
- CLASS Fiddle::Pointer
- CLASS Fiddle::StructArray
- D
- F
- L
- M
- R
- W
Constants
ALIGN_BOOL | = | Document-const |
The alignment size of a bool |
||
ALIGN_CHAR | = | Document-const |
The alignment size of a char |
||
ALIGN_DOUBLE | = | Document-const |
The alignment size of a double |
||
ALIGN_FLOAT | = | Document-const |
The alignment size of a float |
||
ALIGN_INT | = | Document-const |
The alignment size of an int |
||
ALIGN_INT16_T | = | Document-const |
The alignment size of a int16_t |
||
ALIGN_INT32_T | = | Document-const |
The alignment size of a int32_t |
||
ALIGN_INT64_T | = | Document-const |
The alignment size of a int64_t |
||
ALIGN_INT8_T | = | Document-const |
The alignment size of a int8_t |
||
ALIGN_INTPTR_T | = | Document-const |
The alignment size of a intptr_t |
||
ALIGN_LONG | = | Document-const |
The alignment size of a long |
||
ALIGN_LONG_LONG | = | Document-const |
The alignment size of a long long |
||
ALIGN_PTRDIFF_T | = | Document-const |
The alignment size of a ptrdiff_t |
||
ALIGN_SHORT | = | Document-const |
The alignment size of a short |
||
ALIGN_SIZE_T | = | Document-const |
The alignment size of a size_t |
||
ALIGN_SSIZE_T | = | Document-const |
The alignment size of a ssize_t |
||
ALIGN_UINTPTR_T | = | Document-const |
The alignment size of a uintptr_t |
||
ALIGN_VOIDP | = | Document-const |
The alignment size of a void* |
||
BUILD_RUBY_PLATFORM | = | Document-const |
Platform built against (i.e. “x86_64-linux”, etc.) See also RUBY_PLATFORM |
||
Qfalse | = | Document-const |
The value of |
||
Qnil | = | Document-const |
The value of |
||
Qtrue | = | Document-const |
The value of |
||
Qundef | = | Document-const |
The value of |
||
RUBY_FREE | = | Document-const |
Address of the ruby_xfree() function |
||
SIZEOF_BOOL | = | Document-const |
size of a bool |
||
SIZEOF_CHAR | = | Document-const |
size of a char |
||
SIZEOF_CONST_STRING | = | Document-const |
size of a const char* |
||
SIZEOF_DOUBLE | = | Document-const |
size of a double |
||
SIZEOF_FLOAT | = | Document-const |
size of a float |
||
SIZEOF_INT | = | Document-const |
size of an int |
||
SIZEOF_INT16_T | = | Document-const |
size of a int16_t |
||
SIZEOF_INT32_T | = | Document-const |
size of a int32_t |
||
SIZEOF_INT64_T | = | Document-const |
size of a int64_t |
||
SIZEOF_INT8_T | = | Document-const |
size of a int8_t |
||
SIZEOF_INTPTR_T | = | Document-const |
size of a intptr_t |
||
SIZEOF_LONG | = | Document-const |
size of a long |
||
SIZEOF_LONG_LONG | = | Document-const |
size of a long long |
||
SIZEOF_PTRDIFF_T | = | Document-const |
size of a ptrdiff_t |
||
SIZEOF_SHORT | = | Document-const |
size of a short |
||
SIZEOF_SIZE_T | = | Document-const |
size of a size_t |
||
SIZEOF_SSIZE_T | = | Document-const |
size of a ssize_t |
||
SIZEOF_UCHAR | = | Document-const |
size of a unsigned char |
||
SIZEOF_UINT | = | Document-const |
size of an unsigned int |
||
SIZEOF_UINT16_T | = | Document-const |
size of a uint16_t |
||
SIZEOF_UINT32_T | = | Document-const |
size of a uint32_t |
||
SIZEOF_UINT64_T | = | Document-const |
size of a uint64_t |
||
SIZEOF_UINT8_T | = | Document-const |
size of a uint8_t |
||
SIZEOF_UINTPTR_T | = | Document-const |
size of a uintptr_t |
||
SIZEOF_ULONG | = | Document-const |
size of a unsigned long |
||
SIZEOF_ULONG_LONG | = | Document-const |
size of a unsigned long long |
||
SIZEOF_USHORT | = | Document-const |
size of a unsigned short |
||
SIZEOF_VOIDP | = | Document-const |
size of a void* |
||
VERSION | = | "1.1.2" |
WINDOWS | = | Qtrue |
Returns a boolean regarding whether the host is WIN32 |
Class Public methods
dlopen(library) => Fiddle::Handle Link
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.
# File ruby/ext/fiddle/lib/fiddle.rb, line 60 def dlopen library begin Fiddle::Handle.new(library) rescue DLError => error case RUBY_PLATFORM when /linux/ case error.message when /\A(\/.+?): (?:invalid ELF header|file too short)/ # This may be a linker script: # https://sourceware.org/binutils/docs/ld.html#Scripts path = $1 else raise end else raise end File.open(path) do |input| input.each_line do |line| case line when /\A\s*(?:INPUT|GROUP)\s*\(\s*([^\s,\)]+)/ # TODO: Should we support multiple files? return dlopen($1) end end end # Not found raise end end
Fiddle.dlunwrap(addr) Link
Returns the Ruby object stored at the memory address addr
Example:
x = Object.new
# => #<Object:0x0000000107c7d870>
Fiddle.dlwrap(x)
# => 4425504880
Fiddle.dlunwrap(_)
# => #<Object:0x0000000107c7d870>
Source: show
VALUE rb_fiddle_ptr2value(VALUE self, VALUE addr) { return (VALUE)NUM2PTR(addr); }
Fiddle.dlwrap(val) Link
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
Source: show
static VALUE rb_fiddle_value2ptr(VALUE self, VALUE val) { return PTR2NUM((void*)val); }
Fiddle.free(addr) Link
Free the memory at address addr
Source: show
VALUE rb_fiddle_free(VALUE self, VALUE addr) { void *ptr = NUM2PTR(addr); ruby_xfree(ptr); return Qnil; }
last_error() Link
Returns the last Error
of the current executing Thread
or nil if none
last_error=(error) Link
Sets the last Error
of the current executing Thread
to error
Fiddle.malloc(size) Link
Allocate size
bytes of memory and return the integer memory address for the allocated memory.
Source: show
static VALUE rb_fiddle_malloc(VALUE self, VALUE size) { void *ptr; ptr = (void*)ruby_xcalloc(1, NUM2SIZET(size)); return PTR2NUM(ptr); }
Fiddle.realloc(addr, size) Link
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.
Source: show
static VALUE rb_fiddle_realloc(VALUE self, VALUE addr, VALUE size) { void *ptr = NUM2PTR(addr); ptr = (void*)ruby_xrealloc(ptr, NUM2SIZET(size)); return PTR2NUM(ptr); }
win32_last_error() Link
Returns the last win32 Error
of the current executing Thread
or nil if none
win32_last_error=(error) Link
Sets the last win32 Error
of the current executing Thread
to error
win32_last_socket_error() Link
Returns the last win32 socket Error
of the current executing Thread
or nil if none