Module: Fiddle
Relationships & Source Files | |
Namespace Children | |
Modules:
| |
Classes:
| |
Exceptions:
| |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Defined in: | lib/fiddle.rb, ext/fiddle/closure.c, ext/fiddle/fiddle.c, lib/fiddle/closure.rb, lib/fiddle/cparser.rb, lib/fiddle/ffi_backend.rb, lib/fiddle/function.rb, lib/fiddle/import.rb, lib/fiddle/pack.rb, lib/fiddle/struct.rb, lib/fiddle/types.rb, lib/fiddle/value.rb, 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_BOOL =
# File 'lib/fiddle/ffi_backend.rb', line 615Fiddle::FFIBackend::FFITypes[Types::BOOL].alignment
-
ALIGN_CHAR =
# File 'lib/fiddle/ffi_backend.rb', line 604Fiddle::FFIBackend::FFITypes[Types::CHAR].alignment
-
ALIGN_DOUBLE =
# File 'lib/fiddle/ffi_backend.rb', line 614Fiddle::FFIBackend::FFITypes[Types::DOUBLE].alignment
-
ALIGN_FLOAT =
# File 'lib/fiddle/ffi_backend.rb', line 613Fiddle::FFIBackend::FFITypes[Types::FLOAT].alignment
-
ALIGN_INT =
# File 'lib/fiddle/ffi_backend.rb', line 606Fiddle::FFIBackend::FFITypes[Types::INT].alignment
-
ALIGN_INT16_T =
# File 'lib/fiddle/ffi_backend.rb', line 610Fiddle::FFIBackend::FFITypes[Types::INT16_T].alignment
-
ALIGN_INT32_T =
# File 'lib/fiddle/ffi_backend.rb', line 611Fiddle::FFIBackend::FFITypes[Types::INT32_T].alignment
-
ALIGN_INT64_T =
# File 'lib/fiddle/ffi_backend.rb', line 612Fiddle::FFIBackend::FFITypes[Types::INT64_T].alignment
-
ALIGN_INT8_T =
# File 'lib/fiddle/ffi_backend.rb', line 609Fiddle::FFIBackend::FFITypes[Types::INT8_T].alignment
-
ALIGN_INTPTR_T =
# File 'lib/fiddle/ffi_backend.rb', line 619Fiddle::FFIBackend::FFITypes[Types::INTPTR_T].alignment
-
ALIGN_LONG =
# File 'lib/fiddle/ffi_backend.rb', line 607Fiddle::FFIBackend::FFITypes[Types::LONG].alignment
-
ALIGN_LONG_LONG =
# File 'lib/fiddle/ffi_backend.rb', line 608Fiddle::FFIBackend::FFITypes[Types::LONG_LONG].alignment
-
ALIGN_PTRDIFF_T =
# File 'lib/fiddle/ffi_backend.rb', line 618Fiddle::FFIBackend::FFITypes[Types::PTRDIFF_T].alignment
-
ALIGN_SHORT =
# File 'lib/fiddle/ffi_backend.rb', line 605Fiddle::FFIBackend::FFITypes[Types::SHORT].alignment
-
ALIGN_SIZE_T =
# File 'lib/fiddle/ffi_backend.rb', line 616Fiddle::FFIBackend::FFITypes[Types::SIZE_T].alignment
-
ALIGN_SSIZE_T =
# File 'lib/fiddle/ffi_backend.rb', line 617ALIGN_SIZE_T
-
ALIGN_UINTPTR_T =
# File 'lib/fiddle/ffi_backend.rb', line 620Fiddle::FFIBackend::FFITypes[Types::UINTPTR_T].alignment
-
ALIGN_VOIDP =
# File 'lib/fiddle/ffi_backend.rb', line 603Fiddle::FFIBackend::FFITypes[Types::VOIDP].alignment
-
BUILD_RUBY_PLATFORM =
Platform built against (i.e. “x86_64-linux”, etc.)
See also RUBY_PLATFORM
rb_str_new2(RUBY_PLATFORM)
-
NULL =
# File 'lib/fiddle/ffi_backend.rb', line 601Fiddle::Pointer.new(0)
-
Qfalse =
The value of
Qfalse
INT2NUM(Qfalse)
-
Qnil =
The value of
Qnil
INT2NUM(Qnil)
-
Qtrue =
The value of
Qtrue
INT2NUM(Qtrue)
-
Qundef =
The value of
Qundef
INT2NUM(Qundef)
-
RTLD_GLOBAL =
Internal use only
Add constants for backwards compat
Handle::RTLD_GLOBAL
-
RTLD_LAZY =
Internal use only
# File 'lib/fiddle.rb', line 133Handle::RTLD_LAZY
-
RTLD_NOW =
Internal use only
# File 'lib/fiddle.rb', line 134Handle::RTLD_NOW
-
RUBY_FREE =
# File 'lib/fiddle/ffi_backend.rb', line 600Fiddle::Pointer::LibC::FREE.address
-
SIZEOF_BOOL =
# File 'lib/fiddle/ffi_backend.rb', line 643Fiddle::FFIBackend::FFITypes[Types::BOOL].size
-
SIZEOF_CHAR =
# File 'lib/fiddle/ffi_backend.rb', line 623Fiddle::FFIBackend::FFITypes[Types::CHAR].size
-
SIZEOF_CONST_STRING =
# File 'lib/fiddle/ffi_backend.rb', line 649Fiddle::FFIBackend::FFITypes[Types::VOIDP].size
-
SIZEOF_DOUBLE =
# File 'lib/fiddle/ffi_backend.rb', line 642Fiddle::FFIBackend::FFITypes[Types::DOUBLE].size
-
SIZEOF_FLOAT =
# File 'lib/fiddle/ffi_backend.rb', line 641Fiddle::FFIBackend::FFITypes[Types::FLOAT].size
-
SIZEOF_INT =
# File 'lib/fiddle/ffi_backend.rb', line 627Fiddle::FFIBackend::FFITypes[Types::INT].size
-
SIZEOF_INT16_T =
# File 'lib/fiddle/ffi_backend.rb', line 635Fiddle::FFIBackend::FFITypes[Types::INT16_T].size
-
SIZEOF_INT32_T =
# File 'lib/fiddle/ffi_backend.rb', line 637Fiddle::FFIBackend::FFITypes[Types::INT32_T].size
-
SIZEOF_INT64_T =
# File 'lib/fiddle/ffi_backend.rb', line 639Fiddle::FFIBackend::FFITypes[Types::INT64_T].size
-
SIZEOF_INT8_T =
# File 'lib/fiddle/ffi_backend.rb', line 633Fiddle::FFIBackend::FFITypes[Types::INT8_T].size
-
SIZEOF_INTPTR_T =
# File 'lib/fiddle/ffi_backend.rb', line 647Fiddle::FFIBackend::FFITypes[Types::INTPTR_T].size
-
SIZEOF_LONG =
# File 'lib/fiddle/ffi_backend.rb', line 629Fiddle::FFIBackend::FFITypes[Types::LONG].size
-
SIZEOF_LONG_LONG =
# File 'lib/fiddle/ffi_backend.rb', line 631Fiddle::FFIBackend::FFITypes[Types::LONG_LONG].size
-
SIZEOF_PTRDIFF_T =
# File 'lib/fiddle/ffi_backend.rb', line 646Fiddle::FFIBackend::FFITypes[Types::PTRDIFF_T].size
-
SIZEOF_SHORT =
# File 'lib/fiddle/ffi_backend.rb', line 625Fiddle::FFIBackend::FFITypes[Types::SHORT].size
-
SIZEOF_SIZE_T =
# File 'lib/fiddle/ffi_backend.rb', line 644Fiddle::FFIBackend::FFITypes[Types::SIZE_T].size
-
SIZEOF_SSIZE_T =
# File 'lib/fiddle/ffi_backend.rb', line 645SIZEOF_SIZE_T
-
SIZEOF_UCHAR =
# File 'lib/fiddle/ffi_backend.rb', line 624Fiddle::FFIBackend::FFITypes[Types::UCHAR].size
-
SIZEOF_UINT =
# File 'lib/fiddle/ffi_backend.rb', line 628Fiddle::FFIBackend::FFITypes[Types::UINT].size
-
SIZEOF_UINT16_T =
# File 'lib/fiddle/ffi_backend.rb', line 636Fiddle::FFIBackend::FFITypes[Types::UINT16_T].size
-
SIZEOF_UINT32_T =
# File 'lib/fiddle/ffi_backend.rb', line 638Fiddle::FFIBackend::FFITypes[Types::UINT32_T].size
-
SIZEOF_UINT64_T =
# File 'lib/fiddle/ffi_backend.rb', line 640Fiddle::FFIBackend::FFITypes[Types::UINT64_T].size
-
SIZEOF_UINT8_T =
# File 'lib/fiddle/ffi_backend.rb', line 634Fiddle::FFIBackend::FFITypes[Types::UINT8_T].size
-
SIZEOF_UINTPTR_T =
# File 'lib/fiddle/ffi_backend.rb', line 648Fiddle::FFIBackend::FFITypes[Types::UINTPTR_T].size
-
SIZEOF_ULONG =
# File 'lib/fiddle/ffi_backend.rb', line 630Fiddle::FFIBackend::FFITypes[Types::ULONG].size
-
SIZEOF_ULONG_LONG =
# File 'lib/fiddle/ffi_backend.rb', line 632Fiddle::FFIBackend::FFITypes[Types::ULONG_LONG].size
-
SIZEOF_USHORT =
# File 'lib/fiddle/ffi_backend.rb', line 626Fiddle::FFIBackend::FFITypes[Types::USHORT].size
-
SIZEOF_VOIDP =
# File 'lib/fiddle/ffi_backend.rb', line 622Fiddle::FFIBackend::FFITypes[Types::VOIDP].size
-
VERSION =
# File 'lib/fiddle/version.rb', line 2"1.1.8"
-
WINDOWS =
# File 'lib/fiddle/ffi_backend.rb', line 75FFI::Platform.windows?
Class Attribute Summary
-
.last_error
rw
Returns the last
Error
of the current executingThread
or nil if none. -
.last_error=(error)
rw
Sets the last
Error
of the current executingThread
toerror
-
.win32_last_error
rw
Returns the last win32
Error
of the current executingThread
or nil if none. -
.win32_last_error=(error)
rw
Sets the last win32
Error
of the current executingThread
toerror
-
.win32_last_socket_error
rw
Returns the last win32 socket
Error
of the current executingThread
or nil if none. -
.win32_last_socket_error=(error)
rw
Sets the last win32 socket
Error
of the current executingThread
toerror
Class Method Summary
-
.dlwrap(val)
Returns the memory address of the Ruby object stored at
val
-
.free(addr)
Free the memory at address
addr
-
.malloc(size)
Allocate
size
bytes of memory and return the integer memory address for the allocated memory. -
.dlunwrap(addr)
mod_func
Returns the Ruby object stored at the memory address
addr
-
.realloc(addr, size)
mod_func
Change the size of the memory allocated at the memory location
addr
tosize
bytes.
Class Attribute Details
.last_error (rw)
Returns the last ::Fiddle::Error
of the current executing Thread
or nil if none
# File 'lib/fiddle.rb', line 41
def self.last_error FFI.errno end
.last_error=(error) (rw)
Sets the last ::Fiddle::Error
of the current executing Thread
to error
# File 'lib/fiddle.rb', line 46
def self.last_error=(error) FFI.errno = error || 0 end
.win32_last_error (rw)
Returns the last win32 ::Fiddle::Error
of the current executing Thread
or nil if none
# File 'lib/fiddle.rb', line 54
def win32_last_error FFI.errno.nonzero? end
.win32_last_error=(error) (rw)
Sets the last win32 ::Fiddle::Error
of the current executing Thread
to error
# File 'lib/fiddle.rb', line 59
def win32_last_error=(error) FFI.errno = error || 0 end
.win32_last_socket_error (rw)
Returns the last win32 socket ::Fiddle::Error
of the current executing Thread
or nil if none
# File 'lib/fiddle.rb', line 65
def win32_last_socket_error FFI.errno.nonzero? end
.win32_last_socket_error=(error) (rw)
Sets the last win32 socket ::Fiddle::Error
of the current executing Thread
to error
# File 'lib/fiddle.rb', line 71
def win32_last_socket_error=(error) FFI.errno = error || 0 end
Class Method Details
.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)
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
# File 'ext/fiddle/fiddle.c', line 100
static VALUE rb_fiddle_value2ptr(VALUE self, VALUE val) { return PTR2NUM((void*)val); }
.free(addr)
Free the memory at address addr
# File 'ext/fiddle/fiddle.c', line 51
VALUE rb_fiddle_free(VALUE self, VALUE addr) { void *ptr = NUM2PTR(addr); ruby_xfree(ptr); return Qnil; }
.malloc(size)
Allocate size
bytes of memory and return the integer memory address for the allocated memory.
# File 'ext/fiddle/fiddle.c', line 22
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 37
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); }