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
frozen_string_literal: false
frozen_string_literal: false
frozen_string_literal: false
frozen_string_literal: false
- MODULE Fiddle::BasicTypes
- MODULE Fiddle::CParser
- MODULE Fiddle::CStructBuilder
- MODULE Fiddle::Importer
- MODULE Fiddle::Win32Types
- CLASS Fiddle::CStruct
- CLASS Fiddle::CStructEntity
- CLASS Fiddle::CUnion
- CLASS Fiddle::CUnionEntity
- CLASS Fiddle::Closure
- CLASS Fiddle::CompositeHandler
- CLASS Fiddle::DLError
- CLASS Fiddle::Function
- CLASS Fiddle::Handle
- CLASS Fiddle::Pointer
- D
- F
- L
- M
- R
- W
| TYPE_VOID | = | Document-const |
|
C type - void |
||
| TYPE_VOIDP | = | Document-const |
|
C type - void* |
||
| TYPE_CHAR | = | Document-const |
|
C type - char |
||
| TYPE_SHORT | = | Document-const |
|
C type - short |
||
| TYPE_INT | = | Document-const |
|
C type - int |
||
| TYPE_LONG | = | Document-const |
|
C type - long |
||
| TYPE_LONG_LONG | = | Document-const |
|
C type - long long |
||
| TYPE_FLOAT | = | Document-const |
|
C type - float |
||
| TYPE_DOUBLE | = | Document-const |
|
C type - double |
||
| TYPE_SIZE_T | = | Document-const |
|
C type - size_t |
||
| TYPE_SSIZE_T | = | Document-const |
|
C type - ssize_t |
||
| TYPE_PTRDIFF_T | = | Document-const |
|
C type - ptrdiff_t |
||
| TYPE_INTPTR_T | = | Document-const |
|
C type - intptr_t |
||
| TYPE_UINTPTR_T | = | Document-const |
|
C type - uintptr_t |
||
| ALIGN_VOIDP | = | Document-const |
|
The alignment size of a void* |
||
| ALIGN_CHAR | = | Document-const |
|
The alignment size of a char |
||
| ALIGN_SHORT | = | Document-const |
|
The alignment size of a short |
||
| ALIGN_INT | = | Document-const |
|
The alignment size of an int |
||
| ALIGN_LONG | = | Document-const |
|
The alignment size of a long |
||
| ALIGN_LONG_LONG | = | Document-const |
|
The alignment size of a long long |
||
| ALIGN_FLOAT | = | Document-const |
|
The alignment size of a float |
||
| ALIGN_DOUBLE | = | Document-const |
|
The alignment size of a double |
||
| 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_PTRDIFF_T | = | Document-const |
|
The alignment size of a ptrdiff_t |
||
| ALIGN_INTPTR_T | = | Document-const |
|
The alignment size of a intptr_t |
||
| ALIGN_UINTPTR_T | = | Document-const |
|
The alignment size of a uintptr_t |
||
| WINDOWS | = | Qtrue |
Returns a boolean regarding whether the host is WIN32 |
||
| SIZEOF_VOIDP | = | Document-const |
|
size of a void* |
||
| SIZEOF_CHAR | = | Document-const |
|
size of a char |
||
| SIZEOF_SHORT | = | Document-const |
|
size of a short |
||
| SIZEOF_INT | = | Document-const |
|
size of an int |
||
| SIZEOF_LONG | = | Document-const |
|
size of a long |
||
| SIZEOF_LONG_LONG | = | Document-const |
|
size of a long long |
||
| SIZEOF_FLOAT | = | Document-const |
|
size of a float |
||
| SIZEOF_DOUBLE | = | Document-const |
|
size of a double |
||
| SIZEOF_SIZE_T | = | Document-const |
|
size of a size_t |
||
| SIZEOF_SSIZE_T | = | Document-const |
|
size of a ssize_t |
||
| SIZEOF_PTRDIFF_T | = | Document-const |
|
size of a ptrdiff_t |
||
| SIZEOF_INTPTR_T | = | Document-const |
|
size of a intptr_t |
||
| SIZEOF_UINTPTR_T | = | Document-const |
|
size of a uintptr_t |
||
| RUBY_FREE | = | Document-const |
|
Address of the ruby_xfree() function |
||
| BUILD_RUBY_PLATFORM | = | Document-const |
|
Platform built against (i.e. “x86_64-linux”, etc.) See also RUBY_PLATFORM |
||
| RTLD_GLOBAL | = | Handle::RTLD_GLOBAL # :nodoc: |
Add constants for backwards compat |
||
| RTLD_LAZY | = | Handle::RTLD_LAZY |
| RTLD_NOW | = | Handle::RTLD_NOW |
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 hexadecimal representation of a memory pointer address
addr
Example:
lib = Fiddle.dlopen('/lib64/libc-2.15.so')
=> #<Fiddle::Handle:0x00000001342460>
lib['strcpy'].to_s(16)
=> "7f59de6dd240"
Fiddle.dlunwrap(Fiddle.dlwrap(lib['strcpy'].to_s(16)))
=> "7f59de6dd240"
Source: show
VALUE
rb_fiddle_ptr2value(VALUE self, VALUE addr)
{
return (VALUE)NUM2PTR(addr);
}
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
Source: show
static VALUE
rb_fiddle_value2ptr(VALUE self, VALUE val)
{
return PTR2NUM((void*)val);
}
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;
}
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.
Source: show
static VALUE
rb_fiddle_malloc(VALUE self, VALUE size)
{
void *ptr;
ptr = (void*)ruby_xmalloc(NUM2SIZET(size));
return PTR2NUM(ptr);
}
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);
}
Returns the last win32 Error of the current executing
Thread or nil if none