Foreign Function Interface

What is FFI?

Mosh provides a general Foreign Function Interface (FFI) methods.

With these methods, you can load shared library, call C-function in it and get a result of function call.

FFI is not supported on Windows.

Example

;; use mysql client library
(let* ([libmysqlclient (open-shared-library "libmysqlclient.so.15")]
       [mysql-init     (c-function libmysqlclient void* mysql_init void*)])
  (display (mysql-init 0)))
;; generate png image with Cairo library.
(import (rnrs)
        (mosh ffi))

(let* ((libcairo (open-shared-library "libcairo.so"))
         (cairo-image-surface-create (c-function libcairo void* cairo_image_surface_create int int int))
         (cairo-surface-write-to-png (c-function libcairo int cairo_surface_write_to_png void* char*))
         (cairo-create (c-function libcairo void* cairo_create void*))
         (set-line-width (c-function libcairo void cairo_set_line_width void* double))
         (rgba (c-function libcairo void cairo_set_source_rgba void* double double double double))
         (move-to (c-function libcairo void cairo_move_to void* double double))
         (line-to (c-function libcairo void cairo_line_to void* double double))
         (TOY-show-text (c-function libcairo void cairo_show_text void* char*))
         (stroke (c-function libcairo void cairo_stroke void*)))

       (let* ((surface (cairo-image-surface-create 1 300 300))
              (ctx (cairo-create surface)))
         (rgba ctx 1.0 1.0 1.0 1.0)
         (set-line-width ctx 8.0)
         (move-to ctx 10.0 10.0)
         (line-to ctx 10.0 290.0)
         (line-to ctx 290.0 290.0)
         (line-to ctx 290.0 10.0)
         (line-to ctx 10.0 10.0)
         (move-to ctx 100.0 150.0)
         (TOY-show-text ctx "mosh")
         (stroke ctx)
         (display (cairo-surface-write-to-png surface "test.png"))))
Summary
Foreign Function InterfaceMosh provides a general Foreign Function Interface (FFI) methods.
(mosh ffi)Foreign Function Interface Library
Functions
ffi-supported?Returns #t when ffi is supported, otherwise #f.
pointer->stringReturns string value at which pointer points.
pointer-refRefer the value of pointer[index].
open-shared-libraryOpen shared library.
c-functionMake foreign c-function closure.
Constants
sizeof:boolsizeof(bool)
sizeof:shortsizeof(short)
sizeof:intsizeof(int)
sizeof:longsizeof(long)
sizeof:void*sizeof(void*)
sizeof:size_tsizeof(size_t)
alignof:boolstruct x { char y; bool z; };
alignof:shortstruct x { char y; short z; };
alignof:intstruct x { char y; int z; };
alignof:longstruct x { char y; long z; };
alignof:void*struct x { char y; void* z; };
alignof:size_tstruct x { char y; size_t z; };
alignof:floatstruct x { char y; float z; };
alignof:doublestruct x { char y; double z; };
alignof:int8_tstruct x { char y; int8_t z; };
alignof:int16_tstruct x { char y; int16_t z; };
alignof:int32_tstruct x { char y; int32_t z; };
alignof:int64_tstruct x { char y; int64_t z; };
on-darwin
on-linux
on-freebsd
on-openbsd
on-windows

(mosh ffi)

Foreign Function Interface Library

Summary
Functions
ffi-supported?Returns #t when ffi is supported, otherwise #f.
pointer->stringReturns string value at which pointer points.
pointer-refRefer the value of pointer[index].
open-shared-libraryOpen shared library.
c-functionMake foreign c-function closure.
Constants
sizeof:boolsizeof(bool)
sizeof:shortsizeof(short)
sizeof:intsizeof(int)
sizeof:longsizeof(long)
sizeof:void*sizeof(void*)
sizeof:size_tsizeof(size_t)
alignof:boolstruct x { char y; bool z; };
alignof:shortstruct x { char y; short z; };
alignof:intstruct x { char y; int z; };
alignof:longstruct x { char y; long z; };
alignof:void*struct x { char y; void* z; };
alignof:size_tstruct x { char y; size_t z; };
alignof:floatstruct x { char y; float z; };
alignof:doublestruct x { char y; double z; };
alignof:int8_tstruct x { char y; int8_t z; };
alignof:int16_tstruct x { char y; int16_t z; };
alignof:int32_tstruct x { char y; int32_t z; };
alignof:int64_tstruct x { char y; int64_t z; };
on-darwin
on-linux
on-freebsd
on-openbsd
on-windows

Functions

ffi-supported?

Returns #t when ffi is supported, otherwise #f.

Prototype

(ffi-supported?)

Returns

#t when ffi is supported, otherwise #f.

pointer->string

Returns string value at which pointer points.

Prototype

(pointer->string pointer)

Parameters

pointerinteger valued pointer.  When not-pointer integer is passed, it may cause crash.

Returns

string value at which pointer points.

pointer-ref

Refer the value of pointer[index].

Prototype

(pointer-ref pointer . index)

Parameters

pointerinteger valued pointer.  When not-pointer integer is passed, it may cause crash.
indexindex. default values is 0.

Returns

pointer value of pointer[index]

open-shared-library

Open shared library.

Prototype

(open-shared-library library)

Parameters

libraryPath to library.

Returns

Loaded shared library object.

Errors

Raise error when can’t load library

c-function

Make foreign c-function closure.

Prototype

(c-function lib ret func . arg)

Parameters

liblibrary object returned by open-shared-library
retreturn type of c-function. void*, char*, void, double and int are supported.
funcname of c-function as symbol
arglist of argument types. void*, int, double and char* are supported.

Returns

Foreign function closure

Constants

sizeof:bool

sizeof(bool)

sizeof:short

sizeof(short)

sizeof:int

sizeof(int)

sizeof:long

sizeof(long)

sizeof:void*

sizeof(void*)

sizeof:size_t

sizeof(size_t)

alignof:bool

struct x { char y; bool z; };

offsetof(x, z)

alignof:short

struct x { char y; short z; };

offsetof(x, z)

alignof:int

struct x { char y; int z; };

offsetof(x, z)

alignof:long

struct x { char y; long z; };

offsetof(x, z)

alignof:void*

struct x { char y; void* z; };

offsetof(x, z)

alignof:size_t

struct x { char y; size_t z; };

offsetof(x, z)

alignof:float

struct x { char y; float z; };

offsetof(x, z)

alignof:double

struct x { char y; double z; };

offsetof(x, z)

alignof:int8_t

struct x { char y; int8_t z; };

offsetof(x, z)

alignof:int16_t

struct x { char y; int16_t z; };

offsetof(x, z)

alignof:int32_t

struct x { char y; int32_t z; };

offsetof(x, z)

alignof:int64_t

struct x { char y; int64_t z; };

offsetof(x, z)

on-darwin

on-linux

on-freebsd

on-openbsd

on-windows

Open shared library.
Close