Module: Fiddle
| Relationships & Source Files | |
| Namespace Children | |
| Modules: | |
| Classes: | |
| Exceptions: | |
| Extension / Inclusion / Inheritance Descendants | |
| Included In: | |
| Defined in: | ext/fiddle/fiddle.c, ext/fiddle/closure.c, ext/fiddle/pointer.c, ext/fiddle/lib/fiddle.rb, ext/fiddle/lib/fiddle/closure.rb, ext/fiddle/lib/fiddle/cparser.rb, ext/fiddle/lib/fiddle/function.rb, ext/fiddle/lib/fiddle/import.rb, ext/fiddle/lib/fiddle/pack.rb, ext/fiddle/lib/fiddle/struct.rb, ext/fiddle/lib/fiddle/types.rb, ext/fiddle/lib/fiddle/value.rb | 
Overview
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.0Constant Summary
- 
    ALIGN_CHAR =
    # File 'ext/fiddle/fiddle.c', line 269The alignment size of a char INT2NUM(ALIGN_CHAR) 
- 
    ALIGN_DOUBLE =
    # File 'ext/fiddle/fiddle.c', line 307The alignment size of a double INT2NUM(ALIGN_DOUBLE) 
- 
    ALIGN_FLOAT =
    # File 'ext/fiddle/fiddle.c', line 301The alignment size of a float INT2NUM(ALIGN_FLOAT) 
- 
    ALIGN_INT =
    # File 'ext/fiddle/fiddle.c', line 281The alignment size of an int INT2NUM(ALIGN_INT) 
- 
    ALIGN_INTPTR_T =
    # File 'ext/fiddle/fiddle.c', line 331The alignment size of a intptr_t INT2NUM(ALIGN_OF(intptr_t)) 
- 
    ALIGN_LONG =
    # File 'ext/fiddle/fiddle.c', line 287The alignment size of a long INT2NUM(ALIGN_LONG) 
- 
    ALIGN_LONG_LONG =
    # File 'ext/fiddle/fiddle.c', line 294The alignment size of a long long INT2NUM(ALIGN_LONG_LONG) 
- 
    ALIGN_PTRDIFF_T =
    # File 'ext/fiddle/fiddle.c', line 325The alignment size of a ptrdiff_t INT2NUM(ALIGN_OF(ptrdiff_t)) 
- 
    ALIGN_SHORT =
    # File 'ext/fiddle/fiddle.c', line 275The alignment size of a short INT2NUM(ALIGN_SHORT) 
- 
    ALIGN_SIZE_T =
    # File 'ext/fiddle/fiddle.c', line 313The alignment size of a size_t INT2NUM(ALIGN_OF(size_t)) 
- 
    ALIGN_SSIZE_T =
    # File 'ext/fiddle/fiddle.c', line 319same as size_t INT2NUM(ALIGN_OF(size_t)) 
- 
    ALIGN_UINTPTR_T =
    # File 'ext/fiddle/fiddle.c', line 337The alignment size of a uintptr_t INT2NUM(ALIGN_OF(uintptr_t)) 
- 
    ALIGN_VOIDP =
    # File 'ext/fiddle/fiddle.c', line 263The alignment size of a void* INT2NUM(ALIGN_VOIDP) 
- 
    BUILD_RUBY_PLATFORM =
    # File 'ext/fiddle/fiddle.c', line 441Platform built against (i.e. “x86_64-linux”, etc.) See also RUBY_PLATFORM rb_str_new2(RUBY_PLATFORM) 
- 
    NULL =
    # File 'ext/fiddle/pointer.c', line 720A NULL pointer rb_fiddle_ptr_new(0, 0, 0) 
- 
    RUBY_FREE =
    # File 'ext/fiddle/fiddle.c', line 433Address of the ruby_xfree() function PTR2NUM(ruby_xfree) 
- 
    SIZEOF_CHAR =
    # File 'ext/fiddle/fiddle.c', line 359size of a char INT2NUM(sizeof(char)) 
- 
    SIZEOF_DOUBLE =
    # File 'ext/fiddle/fiddle.c', line 397size of a double INT2NUM(sizeof(double)) 
- 
    SIZEOF_FLOAT =
    # File 'ext/fiddle/fiddle.c', line 391size of a float INT2NUM(sizeof(float)) 
- 
    SIZEOF_INT =
    # File 'ext/fiddle/fiddle.c', line 371size of an int INT2NUM(sizeof(int)) 
- 
    SIZEOF_INTPTR_T =
    # File 'ext/fiddle/fiddle.c', line 421size of a intptr_t INT2NUM(sizeof(intptr_t)) 
- 
    SIZEOF_LONG =
    # File 'ext/fiddle/fiddle.c', line 377size of a long INT2NUM(sizeof(long)) 
- 
    SIZEOF_LONG_LONG =
    # File 'ext/fiddle/fiddle.c', line 384size of a long long INT2NUM(sizeof(LONG_LONG)) 
- 
    SIZEOF_PTRDIFF_T =
    # File 'ext/fiddle/fiddle.c', line 415size of a ptrdiff_t INT2NUM(sizeof(ptrdiff_t)) 
- 
    SIZEOF_SHORT =
    # File 'ext/fiddle/fiddle.c', line 365size of a short INT2NUM(sizeof(short)) 
- 
    SIZEOF_SIZE_T =
    # File 'ext/fiddle/fiddle.c', line 403size of a size_t INT2NUM(sizeof(size_t)) 
- 
    SIZEOF_SSIZE_T =
    # File 'ext/fiddle/fiddle.c', line 409same as size_t INT2NUM(sizeof(size_t)) 
- 
    SIZEOF_UINTPTR_T =
    # File 'ext/fiddle/fiddle.c', line 427size of a uintptr_t INT2NUM(sizeof(uintptr_t)) 
- 
    SIZEOF_VOIDP =
    # File 'ext/fiddle/fiddle.c', line 353size of a void* INT2NUM(sizeof(void*)) 
- 
    TYPE_CHAR =
    # File 'ext/fiddle/fiddle.c', line 189C type - char INT2NUM(TYPE_CHAR) 
- 
    TYPE_DOUBLE =
    # File 'ext/fiddle/fiddle.c', line 227C type - double INT2NUM(TYPE_DOUBLE) 
- 
    TYPE_FLOAT =
    # File 'ext/fiddle/fiddle.c', line 221C type - float INT2NUM(TYPE_FLOAT) 
- 
    TYPE_INT =
    # File 'ext/fiddle/fiddle.c', line 201C type - int INT2NUM(TYPE_INT) 
- 
    TYPE_INTPTR_T =
    # File 'ext/fiddle/fiddle.c', line 251C type - intptr_t INT2NUM(TYPE_INTPTR_T) 
- 
    TYPE_LONG =
    # File 'ext/fiddle/fiddle.c', line 207C type - long INT2NUM(TYPE_LONG) 
- 
    TYPE_LONG_LONG =
    # File 'ext/fiddle/fiddle.c', line 214C type - long long INT2NUM(TYPE_LONG_LONG) 
- 
    TYPE_PTRDIFF_T =
    # File 'ext/fiddle/fiddle.c', line 245C type - ptrdiff_t INT2NUM(TYPE_PTRDIFF_T) 
- 
    TYPE_SHORT =
    # File 'ext/fiddle/fiddle.c', line 195C type - short INT2NUM(TYPE_SHORT) 
- 
    TYPE_SIZE_T =
    # File 'ext/fiddle/fiddle.c', line 233C type - size_t INT2NUM(TYPE_SIZE_T) 
- 
    TYPE_SSIZE_T =
    # File 'ext/fiddle/fiddle.c', line 239C type - ssize_t INT2NUM(TYPE_SSIZE_T) 
- 
    TYPE_UINTPTR_T =
    # File 'ext/fiddle/fiddle.c', line 257C type - uintptr_t INT2NUM(TYPE_UINTPTR_T) 
- 
    TYPE_VOID =
    # File 'ext/fiddle/fiddle.c', line 177C type - void INT2NUM(TYPE_VOID) 
- 
    TYPE_VOIDP =
    # File 'ext/fiddle/fiddle.c', line 183C type - void* INT2NUM(TYPE_VOIDP) 
- 
    WINDOWS =
    
 # File 'ext/fiddle/fiddle.c', line 344Qtrue
Class Attribute Summary
- 
    
      .last_error  
    
    rw
    Returns the last Errorof the current executingThreador nil if none.
- 
    
      .last_error=(error)  
    
    rw
    Sets the last Errorof the current executingThreadtoerror
- 
    
      .win32_last_error  
    
    rw
    Returns the last win32 Errorof the current executingThreador nil if none.
- 
    
      .win32_last_error=(error)  
    
    rw
    Sets the last win32 Errorof the current executingThreadtoerror
Class Method Summary
- 
    
      .dlopen(library)  ⇒ Fiddle 
    
    mod_func
    Creates a new handler that opens library, and returns an instance of Handle.
- 
    
      .dlunwrap(addr)  
    
    mod_func
    Returns the hexadecimal representation of a memory pointer address addr
- 
    
      .dlwrap(val)  
    
    mod_func
    Returns a memory pointer of a function's hexadecimal address location val
- 
    
      .free(addr)  
    
    mod_func
    Free the memory at address addr
- 
    
      .malloc(size)  
    
    mod_func
    Allocate sizebytes of memory and return the integer memory address for the allocated memory.
- 
    
      .realloc(addr, size)  
    
    mod_func
    Change the size of the memory allocated at the memory location addrtosizebytes.
Class Attribute Details
.last_error (rw)
Returns the last Error of the current executing Thread or nil if none
# File 'ext/fiddle/lib/fiddle.rb', line 21
def self.last_error Thread.current[:__FIDDLE_LAST_ERROR__] end
.last_error=(error) (rw)
Sets the last Error of the current executing Thread to error
# File 'ext/fiddle/lib/fiddle.rb', line 26
def self.last_error= error Thread.current[:__DL2_LAST_ERROR__] = error Thread.current[:__FIDDLE_LAST_ERROR__] = error end
.win32_last_error (rw)
Returns the last win32 Error of the current executing Thread or nil if none
# File 'ext/fiddle/lib/fiddle.rb', line 10
def self.win32_last_error Thread.current[:__FIDDLE_WIN32_LAST_ERROR__] end
.win32_last_error=(error) (rw)
Sets the last win32 Error of the current executing Thread to error
# File 'ext/fiddle/lib/fiddle.rb', line 15
def self.win32_last_error= error Thread.current[:__FIDDLE_WIN32_LAST_ERROR__] = error end
Class Method Details
    .dlopen(library)  ⇒ Fiddle  (mod_func)  
Creates a new handler that opens library, and returns an instance of ::Fiddle::Handle.
If nil is given for the library, 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 Handle.new for more.
.dlunwrap(addr) (mod_func)
.dlwrap(val) (mod_func)
Returns a memory pointer of a function's hexadecimal address location val
Example:
lib = Fiddle.dlopen('/lib64/libc-2.15.so')
#=> #<Fiddle::Handle:0x00000001342460>
Fiddle.dlwrap(lib['strcpy'].to_s(16))
#=> 25522520.free(addr) (mod_func)
Free the memory at address addr
.malloc(size) (mod_func)
Allocate size bytes of memory and return the integer memory address for the allocated memory.
.realloc(addr, size) (mod_func)
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.