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.
shared-errnoWhen invoked with no argument, returns errno (On Windows getLastError()).
pointer?Returns #t if obj is pointer, otherwise #f
pointer->integerconvert pointer to integer
pointer->stringReturns string value at which pointer points.
open-shared-libraryOpen shared library.
c-functionMake foreign c-function closure.
pointer-ref-c-uint16Get a value from pointer + offset as uint16.
pointer-ref-c-uint32Get a value from pointer + offset as uint32.
pointer-ref-c-uint64Get a value from pointer + offset as uint64.
pointer-ref-c-int8Get a value from pointer + offset as int8.
pointer-ref-c-int16Get a value from pointer + offset as int16.
pointer-ref-c-int32Get a value from pointer + offset as int32.
pointer-ref-c-int64Get a value from pointer + offset as int64.
pointer-ref-c-signed-charGet a value from pointer + offset as signed-char.
pointer-ref-c-unsigned-charGet a value from pointer + offset as unsigned-char.
pointer-ref-c-signed-shortGet a value from pointer + offset as signed-short.
pointer-ref-c-unsigned-shortGet a value from pointer + offset as unsigned-short.
pointer-ref-c-signed-intGet a value from pointer + offset as signed-int.
pointer-ref-c-unsigned-intGet a value from pointer + offset as unsigned-int.
pointer-ref-c-signed-longGet a value from pointer + offset as signed-long.
pointer-ref-c-unsigned-longGet a value from pointer + offset as unsigned-long.
pointer-ref-c-signed-long-longGet a value from pointer + offset as signed-long-long.
pointer-ref-c-unsigned-long-longGet a value from pointer + offset as unsigned-long-long.
pointer-ref-c-floatGet a value from pointer + offset as float.
pointer-ref-c-doubleGet a value from pointer + offset as double.
pointer-ref-c-pointerGet a value from pointer + offset as pointer.
pointer-set-c-char!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-short!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-int!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-long!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-long-long!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-float!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-double!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-int8!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-int16!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-int32!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-int64!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-uint8!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-uint16!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-uint32!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-uint64!Returns #t when ffi is supported, otherwise #f.
Constants
size-of-boolsize-of(bool)
size-of-shortsize-of(short)
size-of-intsize-of(int)
size-of-longsize-of(long)
size-of-void*size-of(void*)
size-of-pointeralias for size-of(void*)
size-of-size_tsize-of(size_t)
align-of-boolstruct x { char y; bool z; };
align-of-shortstruct x { char y; short z; };
align-of-intstruct x { char y; int z; };
align-of-longstruct x { char y; long z; };
align-of-void*struct x { char y; void* z; };
align-of-size_tstruct x { char y; size_t z; };
align-of-floatstruct x { char y; float z; };
align-of-doublestruct x { char y; double z; };
align-of-int8_tstruct x { char y; int8_t z; };
align-of-int16_tstruct x { char y; int16_t z; };
align-of-int32_tstruct x { char y; int32_t z; };
align-of-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.
shared-errnoWhen invoked with no argument, returns errno (On Windows getLastError()).
pointer?Returns #t if obj is pointer, otherwise #f
pointer->integerconvert pointer to integer
pointer->stringReturns string value at which pointer points.
open-shared-libraryOpen shared library.
c-functionMake foreign c-function closure.
pointer-ref-c-uint16Get a value from pointer + offset as uint16.
pointer-ref-c-uint32Get a value from pointer + offset as uint32.
pointer-ref-c-uint64Get a value from pointer + offset as uint64.
pointer-ref-c-int8Get a value from pointer + offset as int8.
pointer-ref-c-int16Get a value from pointer + offset as int16.
pointer-ref-c-int32Get a value from pointer + offset as int32.
pointer-ref-c-int64Get a value from pointer + offset as int64.
pointer-ref-c-signed-charGet a value from pointer + offset as signed-char.
pointer-ref-c-unsigned-charGet a value from pointer + offset as unsigned-char.
pointer-ref-c-signed-shortGet a value from pointer + offset as signed-short.
pointer-ref-c-unsigned-shortGet a value from pointer + offset as unsigned-short.
pointer-ref-c-signed-intGet a value from pointer + offset as signed-int.
pointer-ref-c-unsigned-intGet a value from pointer + offset as unsigned-int.
pointer-ref-c-signed-longGet a value from pointer + offset as signed-long.
pointer-ref-c-unsigned-longGet a value from pointer + offset as unsigned-long.
pointer-ref-c-signed-long-longGet a value from pointer + offset as signed-long-long.
pointer-ref-c-unsigned-long-longGet a value from pointer + offset as unsigned-long-long.
pointer-ref-c-floatGet a value from pointer + offset as float.
pointer-ref-c-doubleGet a value from pointer + offset as double.
pointer-ref-c-pointerGet a value from pointer + offset as pointer.
pointer-set-c-char!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-short!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-int!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-long!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-long-long!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-float!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-double!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-int8!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-int16!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-int32!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-int64!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-uint8!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-uint16!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-uint32!Returns #t when ffi is supported, otherwise #f.
pointer-set-c-uint64!Returns #t when ffi is supported, otherwise #f.
Constants
size-of-boolsize-of(bool)
size-of-shortsize-of(short)
size-of-intsize-of(int)
size-of-longsize-of(long)
size-of-void*size-of(void*)
size-of-pointeralias for size-of(void*)
size-of-size_tsize-of(size_t)
align-of-boolstruct x { char y; bool z; };
align-of-shortstruct x { char y; short z; };
align-of-intstruct x { char y; int z; };
align-of-longstruct x { char y; long z; };
align-of-void*struct x { char y; void* z; };
align-of-size_tstruct x { char y; size_t z; };
align-of-floatstruct x { char y; float z; };
align-of-doublestruct x { char y; double z; };
align-of-int8_tstruct x { char y; int8_t z; };
align-of-int16_tstruct x { char y; int16_t z; };
align-of-int32_tstruct x { char y; int32_t z; };
align-of-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.

shared-errno

When invoked with no argument, returns errno (On Windows getLastError()).  Invoked with one argument, set value to errno.

Prototype

(shared-errno . value)

Returns

errno

pointer?

Returns #t if obj is pointer, otherwise #f

Prototype

(pointer? obj)

Returns

#t if obj is pointer, otherwise #f

pointer->integer

convert pointer to integer

Prototype

(pointer->integer pointer)

Returns

integer represention of pointer.

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.

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

pointer-ref-c-uint16

Get a value from pointer + offset as uint16.

Prototype

(pointer-set-c-char! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.

Returns

A value from pointer + offset as uint16.

pointer-ref-c-uint32

Get a value from pointer + offset as uint32.

Prototype

(pointer-set-c-char! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.

Returns

A value from pointer + offset as uint32.

pointer-ref-c-uint64

Get a value from pointer + offset as uint64.

Prototype

(pointer-set-c-char! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.

Returns

A value from pointer + offset as uint64.

pointer-ref-c-int8

Get a value from pointer + offset as int8.

Prototype

(pointer-set-c-char! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.

Returns

A value from pointer + offset as int8.

pointer-ref-c-int16

Get a value from pointer + offset as int16.

Prototype

(pointer-set-c-char! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.

Returns

A value from pointer + offset as int16.

pointer-ref-c-int32

Get a value from pointer + offset as int32.

Prototype

(pointer-set-c-char! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.

Returns

A value from pointer + offset as int32.

pointer-ref-c-int64

Get a value from pointer + offset as int64.

Prototype

(pointer-set-c-char! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.

Returns

A value from pointer + offset as int64.

pointer-ref-c-signed-char

Get a value from pointer + offset as signed-char.

Prototype

(pointer-set-c-char! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.

Returns

A value from pointer + offset as signed-char.

pointer-ref-c-unsigned-char

Get a value from pointer + offset as unsigned-char.

Prototype

(pointer-set-c-char! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.

Returns

A value from pointer + offset as unsigned-char.

pointer-ref-c-signed-short

Get a value from pointer + offset as signed-short.

Prototype

(pointer-set-c-char! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.

Returns

A value from pointer + offset as signed-short.

pointer-ref-c-unsigned-short

Get a value from pointer + offset as unsigned-short.

Prototype

(pointer-set-c-char! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.

Returns

A value from pointer + offset as unsigned-short.

pointer-ref-c-signed-int

Get a value from pointer + offset as signed-int.

Prototype

(pointer-set-c-char! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.

Returns

A value from pointer + offset as signed-int.

pointer-ref-c-unsigned-int

Get a value from pointer + offset as unsigned-int.

Prototype

(pointer-set-c-char! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.

Returns

A value from pointer + offset as unsigned-int.

pointer-ref-c-signed-long

Get a value from pointer + offset as signed-long.

Prototype

(pointer-set-c-char! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.

Returns

A value from pointer + offset as signed-long.

pointer-ref-c-unsigned-long

Get a value from pointer + offset as unsigned-long.

Prototype

(pointer-set-c-char! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.

Returns

A value from pointer + offset as unsigned-long.

pointer-ref-c-signed-long-long

Get a value from pointer + offset as signed-long-long.

Prototype

(pointer-set-c-char! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.

Returns

A value from pointer + offset as signed-long-long.

pointer-ref-c-unsigned-long-long

Get a value from pointer + offset as unsigned-long-long.

Prototype

(pointer-set-c-char! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.

Returns

A value from pointer + offset as unsigned-long-long.

pointer-ref-c-float

Get a value from pointer + offset as float.

Prototype

(pointer-set-c-char! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.

Returns

A value from pointer + offset as float.

pointer-ref-c-double

Get a value from pointer + offset as double.

Prototype

(pointer-set-c-char! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.

Returns

A value from pointer + offset as double.

pointer-ref-c-pointer

Get a value from pointer + offset as pointer.

Prototype

(pointer-set-c-char! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.

Returns

A value from pointer + offset as pointer.

pointer-set-c-char!

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

Prototype

(pointer-set-c-char! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.
valuevalue to set.

pointer-set-c-short!

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

Prototype

(pointer-set-c-short! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.
valuevalue to set.

pointer-set-c-int!

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

Prototype

(pointer-set-c-int! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.
valuevalue to set.

pointer-set-c-long!

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

Prototype

(pointer-set-c-long! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.
valuevalue to set.

pointer-set-c-long-long!

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

Prototype

(pointer-set-c-long-long! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.
valuevalue to set.

pointer-set-c-float!

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

Prototype

(pointer-set-c-float! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.
valuevalue to set.

pointer-set-c-double!

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

Prototype

(pointer-set-c-double! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.
valuevalue to set.

pointer-set-c-int8!

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

Prototype

(pointer-set-c-int8! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.
valuevalue to set.

pointer-set-c-int16!

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

Prototype

(pointer-set-c-int16! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.
valuevalue to set.

pointer-set-c-int32!

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

Prototype

(pointer-set-c-int32! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.
valuevalue to set.

pointer-set-c-int64!

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

Prototype

(pointer-set-c-int64! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.
valuevalue to set.

pointer-set-c-uint8!

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

Prototype

(pointer-set-c-uint8! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.
valuevalue to set.

pointer-set-c-uint16!

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

Prototype

(pointer-set-c-uint16! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.
valuevalue to set.

pointer-set-c-uint32!

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

Prototype

(pointer-set-c-uint32! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.
valuevalue to set.

pointer-set-c-uint64!

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

Prototype

(pointer-set-c-uint64! pointer offset value)

Parameters

pointerpointer.
offsetoffset from pointer.
valuevalue to set.

Constants

size-of-bool

size-of(bool)

size-of-short

size-of(short)

size-of-int

size-of(int)

size-of-long

size-of(long)

size-of-void*

size-of(void*)

size-of-pointer

alias for size-of(void*)

size-of-size_t

size-of(size_t)

align-of-bool

struct x { char y; bool z; };

-offset-of(x, z)

align-of-short

struct x { char y; short z; };

-offset-of(x, z)

align-of-int

struct x { char y; int z; };

-offset-of(x, z)

align-of-long

struct x { char y; long z; };

-offset-of(x, z)

align-of-void*

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

-offset-of(x, z)

align-of-size_t

struct x { char y; size_t z; };

-offset-of(x, z)

align-of-float

struct x { char y; float z; };

-offset-of(x, z)

align-of-double

struct x { char y; double z; };

-offset-of(x, z)

align-of-int8_t

struct x { char y; int8_t z; };

-offset-of(x, z)

align-of-int16_t

struct x { char y; int16_t z; };

-offset-of(x, z)

align-of-int32_t

struct x { char y; int32_t z; };

-offset-of(x, z)

align-of-int64_t

struct x { char y; int64_t z; };

-offset-of(x, z)

on-darwin

on-linux

on-freebsd

on-openbsd

on-windows

Open shared library.
Close