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, ext/fiddle/lib/fiddle/version.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 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
Constant Summary
-
ALIGN_CHAR =
# File 'ext/fiddle/fiddle.c', line 365
The alignment size of a char
INT2NUM(ALIGN_CHAR)
-
ALIGN_DOUBLE =
# File 'ext/fiddle/fiddle.c', line 427
The alignment size of a double
INT2NUM(ALIGN_DOUBLE)
-
ALIGN_FLOAT =
# File 'ext/fiddle/fiddle.c', line 421
The alignment size of a float
INT2NUM(ALIGN_FLOAT)
-
ALIGN_INT =
# File 'ext/fiddle/fiddle.c', line 377
The alignment size of an int
INT2NUM(ALIGN_INT)
-
ALIGN_INT16_T =
# File 'ext/fiddle/fiddle.c', line 403
The alignment size of a int16_t
INT2NUM(ALIGN_INT16_T)
-
ALIGN_INT32_T =
# File 'ext/fiddle/fiddle.c', line 409
The alignment size of a int32_t
INT2NUM(ALIGN_INT32_T)
-
ALIGN_INT64_T =
# File 'ext/fiddle/fiddle.c', line 415
The alignment size of a int64_t
INT2NUM(ALIGN_INT64_T)
-
ALIGN_INT8_T =
# File 'ext/fiddle/fiddle.c', line 397
The alignment size of a int8_t
INT2NUM(ALIGN_INT8_T)
-
ALIGN_INTPTR_T =
# File 'ext/fiddle/fiddle.c', line 451
The alignment size of a intptr_t
INT2NUM(ALIGN_OF(intptr_t))
-
ALIGN_LONG =
# File 'ext/fiddle/fiddle.c', line 383
The alignment size of a long
INT2NUM(ALIGN_LONG)
-
ALIGN_LONG_LONG =
# File 'ext/fiddle/fiddle.c', line 390
The alignment size of a long long
INT2NUM(ALIGN_LONG_LONG)
-
ALIGN_PTRDIFF_T =
# File 'ext/fiddle/fiddle.c', line 445
The alignment size of a ptrdiff_t
INT2NUM(ALIGN_OF(ptrdiff_t))
-
ALIGN_SHORT =
# File 'ext/fiddle/fiddle.c', line 371
The alignment size of a short
INT2NUM(ALIGN_SHORT)
-
ALIGN_SIZE_T =
# File 'ext/fiddle/fiddle.c', line 433
The alignment size of a size_t
INT2NUM(ALIGN_OF(size_t))
-
ALIGN_SSIZE_T =
# File 'ext/fiddle/fiddle.c', line 439
same as size_t
INT2NUM(ALIGN_OF(size_t))
-
ALIGN_UINTPTR_T =
# File 'ext/fiddle/fiddle.c', line 457
The alignment size of a uintptr_t
INT2NUM(ALIGN_OF(uintptr_t))
-
ALIGN_VOIDP =
# File 'ext/fiddle/fiddle.c', line 359
The alignment size of a void*
INT2NUM(ALIGN_VOIDP)
-
BUILD_RUBY_PLATFORM =
# File 'ext/fiddle/fiddle.c', line 645
Platform built against (i.e. “x86_64-linux”, etc.)
See also RUBY_PLATFORM
rb_str_new2(RUBY_PLATFORM)
-
NULL =
# File 'ext/fiddle/pointer.c', line 852
A NULL pointer
rb_fiddle_ptr_new(0, 0, 0)
-
Qfalse =
# File 'ext/fiddle/fiddle.c', line 663
The value of
QfalseINT2NUM(Qfalse)
-
Qnil =
# File 'ext/fiddle/fiddle.c', line 669
The value of
QnilINT2NUM(Qnil)
-
Qtrue =
# File 'ext/fiddle/fiddle.c', line 657
The value of
QtrueINT2NUM(Qtrue)
-
Qundef =
# File 'ext/fiddle/fiddle.c', line 675
The value of
QundefINT2NUM(Qundef)
-
RTLD_GLOBAL =
Internal use only
# File 'ext/fiddle/lib/fiddle.rb', line 96
Add constants for backwards compat
Handle::RTLD_GLOBAL
-
RTLD_LAZY =
Internal use only
# File 'ext/fiddle/lib/fiddle.rb', line 97Handle::RTLD_LAZY
-
RTLD_NOW =
Internal use only
# File 'ext/fiddle/lib/fiddle.rb', line 98Handle::RTLD_NOW
-
RUBY_FREE =
# File 'ext/fiddle/fiddle.c', line 637
Address of the ruby_xfree() function
PTR2NUM(ruby_xfree)
-
SIZEOF_CHAR =
# File 'ext/fiddle/fiddle.c', line 479
size of a char
INT2NUM(sizeof(char))
-
SIZEOF_CONST_STRING =
# File 'ext/fiddle/fiddle.c', line 631
size of a const char*
INT2NUM(sizeof(const char*))
-
SIZEOF_DOUBLE =
# File 'ext/fiddle/fiddle.c', line 595
size of a double
INT2NUM(sizeof(double))
-
SIZEOF_FLOAT =
# File 'ext/fiddle/fiddle.c', line 589
size of a float
INT2NUM(sizeof(float))
-
SIZEOF_INT =
# File 'ext/fiddle/fiddle.c', line 503
size of an int
INT2NUM(sizeof(int))
-
SIZEOF_INT16_T =
# File 'ext/fiddle/fiddle.c', line 553
size of a int16_t
INT2NUM(sizeof(int16_t))
-
SIZEOF_INT32_T =
# File 'ext/fiddle/fiddle.c', line 565
size of a int32_t
INT2NUM(sizeof(int32_t))
-
SIZEOF_INT64_T =
# File 'ext/fiddle/fiddle.c', line 577
size of a int64_t
INT2NUM(sizeof(int64_t))
-
SIZEOF_INT8_T =
# File 'ext/fiddle/fiddle.c', line 541
size of a int8_t
INT2NUM(sizeof(int8_t))
-
SIZEOF_INTPTR_T =
# File 'ext/fiddle/fiddle.c', line 619
size of a intptr_t
INT2NUM(sizeof(intptr_t))
-
SIZEOF_LONG =
# File 'ext/fiddle/fiddle.c', line 515
size of a long
INT2NUM(sizeof(long))
-
SIZEOF_LONG_LONG =
# File 'ext/fiddle/fiddle.c', line 528
size of a long long
INT2NUM(sizeof(LONG_LONG))
-
SIZEOF_PTRDIFF_T =
# File 'ext/fiddle/fiddle.c', line 613
size of a ptrdiff_t
INT2NUM(sizeof(ptrdiff_t))
-
SIZEOF_SHORT =
# File 'ext/fiddle/fiddle.c', line 491
size of a short
INT2NUM(sizeof(short))
-
SIZEOF_SIZE_T =
# File 'ext/fiddle/fiddle.c', line 601
size of a size_t
INT2NUM(sizeof(size_t))
-
SIZEOF_SSIZE_T =
# File 'ext/fiddle/fiddle.c', line 607
same as size_t
INT2NUM(sizeof(size_t))
-
SIZEOF_UCHAR =
# File 'ext/fiddle/fiddle.c', line 485
size of a unsigned char
INT2NUM(sizeof(unsigned char))
-
SIZEOF_UINT =
# File 'ext/fiddle/fiddle.c', line 509
size of an unsigned int
INT2NUM(sizeof(unsigned int))
-
SIZEOF_UINT16_T =
# File 'ext/fiddle/fiddle.c', line 559
size of a uint16_t
INT2NUM(sizeof(uint16_t))
-
SIZEOF_UINT32_T =
# File 'ext/fiddle/fiddle.c', line 571
size of a uint32_t
INT2NUM(sizeof(uint32_t))
-
SIZEOF_UINT64_T =
# File 'ext/fiddle/fiddle.c', line 583
size of a uint64_t
INT2NUM(sizeof(uint64_t))
-
SIZEOF_UINT8_T =
# File 'ext/fiddle/fiddle.c', line 547
size of a uint8_t
INT2NUM(sizeof(uint8_t))
-
SIZEOF_UINTPTR_T =
# File 'ext/fiddle/fiddle.c', line 625
size of a uintptr_t
INT2NUM(sizeof(uintptr_t))
-
SIZEOF_ULONG =
# File 'ext/fiddle/fiddle.c', line 521
size of a unsigned long
INT2NUM(sizeof(unsigned long))
-
SIZEOF_ULONG_LONG =
# File 'ext/fiddle/fiddle.c', line 534
size of a unsigned long long
INT2NUM(sizeof(unsigned LONG_LONG))
-
SIZEOF_USHORT =
# File 'ext/fiddle/fiddle.c', line 497
size of a unsigned short
INT2NUM(sizeof(unsigned short))
-
SIZEOF_VOIDP =
# File 'ext/fiddle/fiddle.c', line 473
size of a void*
INT2NUM(sizeof(void*))
-
VERSION =
# File 'ext/fiddle/lib/fiddle/version.rb', line 2"1.1.1" -
WINDOWS =
# File 'ext/fiddle/fiddle.c', line 464Qtrue
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 -
.win32_last_socket_error
rw
Returns the last win32 socket
Errorof the current executingThreador nil if none. -
.win32_last_socket_error=(error)
rw
Sets the last win32 socket
Errorof the current executingThreadtoerror
Class Method Summary
-
.dlopen(library) ⇒ Fiddle
mod_func
Creates a new handler that opens
library, and returns an instance ofHandle. -
.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 ::Fiddle::Error of the current executing Thread or nil if none
# File 'ext/fiddle/lib/fiddle.rb', line 35
def self.last_error Thread.current[:__FIDDLE_LAST_ERROR__] end
.last_error=(error) (rw)
Sets the last ::Fiddle::Error of the current executing Thread to error
# File 'ext/fiddle/lib/fiddle.rb', line 40
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 ::Fiddle::Error of the current executing Thread or nil if none
# File 'ext/fiddle/lib/fiddle.rb', line 12
def self.win32_last_error Thread.current[:__FIDDLE_WIN32_LAST_ERROR__] end
.win32_last_error=(error) (rw)
Sets the last win32 ::Fiddle::Error of the current executing Thread to error
# File 'ext/fiddle/lib/fiddle.rb', line 17
def self.win32_last_error= error Thread.current[:__FIDDLE_WIN32_LAST_ERROR__] = error end
.win32_last_socket_error (rw)
Returns the last win32 socket ::Fiddle::Error of the current executing Thread or nil if none
# File 'ext/fiddle/lib/fiddle.rb', line 23
def self.win32_last_socket_error Thread.current[:__FIDDLE_WIN32_LAST_SOCKET_ERROR__] end
.win32_last_socket_error=(error) (rw)
Sets the last win32 socket ::Fiddle::Error of the current executing Thread to error
# File 'ext/fiddle/lib/fiddle.rb', line 29
def self.win32_last_socket_error= error Thread.current[:__FIDDLE_WIN32_LAST_SOCKET_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.
# File '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. 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
.dlunwrap(addr) (mod_func)
# File 'ext/fiddle/fiddle.c', line 74
VALUE
rb_fiddle_ptr2value(VALUE self, VALUE addr)
{
return (VALUE)NUM2PTR(addr);
}
.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
# File 'ext/fiddle/fiddle.c', line 93
static VALUE
rb_fiddle_value2ptr(VALUE self, VALUE val)
{
return PTR2NUM((void*)val);
}
.free(addr) (mod_func)
Free the memory at address addr
# File 'ext/fiddle/fiddle.c', line 49
VALUE
rb_fiddle_free(VALUE self, VALUE addr)
{
void *ptr = NUM2PTR(addr);
ruby_xfree(ptr);
return Qnil;
}
.malloc(size) (mod_func)
Allocate size bytes of memory and return the integer memory address for the allocated memory.
# File 'ext/fiddle/fiddle.c', line 20
static VALUE
rb_fiddle_malloc(VALUE self, VALUE size)
{
void *ptr;
ptr = (void*)ruby_xcalloc(1, NUM2SIZET(size));
return PTR2NUM(ptr);
}
.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.
# File 'ext/fiddle/fiddle.c', line 35
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);
}