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.
(import (rnrs) (mosh ffi)) ;; use mysql client library (let* ([libmysqlclient (open-shared-library "libmysqlclient.so.15")] [mysql-init (c-function libmysqlclient void* mysql_init void*)]) (display (mysql-init pointer-null)))
;; 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"))))
;; callback example (import (mosh) (mosh ffi) (rnrs)) (define array (u8-list->bytevector '(6 5 3 4 1 7 2))) (let* ([libc (open-shared-library "libc.so.6")] ;; Ubuntu [qsort (c-function libc void qsort void* int int callback)] [compare (c-callback int (void* void*) (lambda (x y) (if (> (pointer-ref-c-uint8 x 0) (pointer-ref-c-uint8 y 0)) 1 0)))]) (qsort array (bytevector-length array) 1 compare) (display array) (free-c-callback compare))
Foreign Function Interface | Mosh 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-errno | When invoked with no argument, returns errno (On Windows getLastError()). |
pointer? | Returns #t if obj is pointer, otherwise #f |
pointer->integer | convert pointer to integer |
pointer->string | Returns string value at which pointer points. |
open-shared-library | Open shared library. |
close-shared-library | Close shared library. |
shared-library-error | Get a description of the last error that occurred during loading a shared library. |
c-function | Make foreign c-function closure. |
make-c-callback | Make c-callback. |
c-callback | Make c-callback |
free-c-callback | Free c-callback object. |
malloc | Allocate n bytes of memory. |
free | Frees the memory allocated by <<malloc>>. |
pointer->c-function | Make foreign c-function closure from pointer. |
make-c-function | Make foreign c-function closure by lookuping a function named “name” in “lib” library. |
null-terminated-bytevector->string | Returns a newly allocated (unless empty) string whose character sequence is encoded by the given null(\0) terminated bytevector. |
null-terminated-utf8->string | Returns a newly allocated (unless empty) string whose character sequence is encoded by the given null(\0) terminated bytevector. |
pointer-ref-c-uint16 | Get a value from pointer + offset as uint16. |
pointer-ref-c-uint32 | Get a value from pointer + offset as uint32. |
pointer-ref-c-uint64 | Get a value from pointer + offset as uint64. |
pointer-ref-c-int8 | Get a value from pointer + offset as int8. |
pointer-ref-c-int16 | Get a value from pointer + offset as int16. |
pointer-ref-c-int32 | Get a value from pointer + offset as int32. |
pointer-ref-c-int64 | Get a value from pointer + offset as int64. |
pointer-ref-c-signed-char | Get a value from pointer + offset as signed-char. |
pointer-ref-c-unsigned-char | Get a value from pointer + offset as unsigned-char. |
pointer-ref-c-signed-short | Get a value from pointer + offset as signed-short. |
pointer-ref-c-unsigned-short | Get a value from pointer + offset as unsigned-short. |
pointer-ref-c-signed-int | Get a value from pointer + offset as signed-int. |
pointer-ref-c-unsigned-int | Get a value from pointer + offset as unsigned-int. |
pointer-ref-c-signed-long | Get a value from pointer + offset as signed-long. |
pointer-ref-c-unsigned-long | Get a value from pointer + offset as unsigned-long. |
pointer-ref-c-signed-long-long | Get 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. |
pointer-ref-c-float | Get a value from pointer + offset as float. |
pointer-ref-c-double | Get a value from pointer + offset as double. |
pointer-ref-c-pointer | Get 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-bool | sizeof(bool) |
size-of-short | sizeof(short) |
size-of-unsigned-short | sizeof(unsigned short) |
size-of-int | sizeof(int) |
size-of-unsigned-int | sizeof(unsigned int) |
size-of-long | sizeof(long) |
size-of-unsigned-long | sizeof(unsigned long) |
size-of-unsigned-long-long | sizeof(unsigned long long) |
size-of-long-long | sizeof(long long) |
size-of-void* | sizeof(void*) |
size-of-pointer | alias for sizeof(void*) |
size-of-size_t | sizeof(size_t) |
size-of-float | sizeof(float) |
size-of-double | sizeof(double) |
align-of-bool | struct x { char y; bool z; }; |
align-of-short | struct x { char y; short z; }; |
align-of-int | struct x { char y; int z; }; |
align-of-long | struct x { char y; long z; }; |
align-of-long-long | struct x { char y; long long z; }; |
align-of-unsigned-long | struct x { char y; unsigned long z; }; |
align-of-unsigned-long-long | struct x { char y; unsigned-long long z; }; |
align-of-void* | struct x { char y; void* z; }; |
align-of-size_t | struct x { char y; size_t z; }; |
align-of-float | struct x { char y; float z; }; |
align-of-double | struct x { char y; double z; }; |
align-of-int8_t | struct x { char y; int8_t z; }; |
align-of-int16_t | struct x { char y; int16_t z; }; |
align-of-int32_t | struct x { char y; int32_t z; }; |
align-of-int64_t | struct x { char y; int64_t z; }; |
on-darwin | |
on-linux | |
on-freebsd | |
on-openbsd | |
on-windows |
Foreign Function Interface Library
Functions | |
ffi-supported? | Returns #t when ffi is supported, otherwise #f. |
shared-errno | When invoked with no argument, returns errno (On Windows getLastError()). |
pointer? | Returns #t if obj is pointer, otherwise #f |
pointer->integer | convert pointer to integer |
pointer->string | Returns string value at which pointer points. |
open-shared-library | Open shared library. |
close-shared-library | Close shared library. |
shared-library-error | Get a description of the last error that occurred during loading a shared library. |
c-function | Make foreign c-function closure. |
make-c-callback | Make c-callback. |
c-callback | Make c-callback |
free-c-callback | Free c-callback object. |
malloc | Allocate n bytes of memory. |
free | Frees the memory allocated by <<malloc>>. |
pointer->c-function | Make foreign c-function closure from pointer. |
make-c-function | Make foreign c-function closure by lookuping a function named “name” in “lib” library. |
null-terminated-bytevector->string | Returns a newly allocated (unless empty) string whose character sequence is encoded by the given null(\0) terminated bytevector. |
null-terminated-utf8->string | Returns a newly allocated (unless empty) string whose character sequence is encoded by the given null(\0) terminated bytevector. |
pointer-ref-c-uint16 | Get a value from pointer + offset as uint16. |
pointer-ref-c-uint32 | Get a value from pointer + offset as uint32. |
pointer-ref-c-uint64 | Get a value from pointer + offset as uint64. |
pointer-ref-c-int8 | Get a value from pointer + offset as int8. |
pointer-ref-c-int16 | Get a value from pointer + offset as int16. |
pointer-ref-c-int32 | Get a value from pointer + offset as int32. |
pointer-ref-c-int64 | Get a value from pointer + offset as int64. |
pointer-ref-c-signed-char | Get a value from pointer + offset as signed-char. |
pointer-ref-c-unsigned-char | Get a value from pointer + offset as unsigned-char. |
pointer-ref-c-signed-short | Get a value from pointer + offset as signed-short. |
pointer-ref-c-unsigned-short | Get a value from pointer + offset as unsigned-short. |
pointer-ref-c-signed-int | Get a value from pointer + offset as signed-int. |
pointer-ref-c-unsigned-int | Get a value from pointer + offset as unsigned-int. |
pointer-ref-c-signed-long | Get a value from pointer + offset as signed-long. |
pointer-ref-c-unsigned-long | Get a value from pointer + offset as unsigned-long. |
pointer-ref-c-signed-long-long | Get 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. |
pointer-ref-c-float | Get a value from pointer + offset as float. |
pointer-ref-c-double | Get a value from pointer + offset as double. |
pointer-ref-c-pointer | Get 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-bool | sizeof(bool) |
size-of-short | sizeof(short) |
size-of-unsigned-short | sizeof(unsigned short) |
size-of-int | sizeof(int) |
size-of-unsigned-int | sizeof(unsigned int) |
size-of-long | sizeof(long) |
size-of-unsigned-long | sizeof(unsigned long) |
size-of-unsigned-long-long | sizeof(unsigned long long) |
size-of-long-long | sizeof(long long) |
size-of-void* | sizeof(void*) |
size-of-pointer | alias for sizeof(void*) |
size-of-size_t | sizeof(size_t) |
size-of-float | sizeof(float) |
size-of-double | sizeof(double) |
align-of-bool | struct x { char y; bool z; }; |
align-of-short | struct x { char y; short z; }; |
align-of-int | struct x { char y; int z; }; |
align-of-long | struct x { char y; long z; }; |
align-of-long-long | struct x { char y; long long z; }; |
align-of-unsigned-long | struct x { char y; unsigned long z; }; |
align-of-unsigned-long-long | struct x { char y; unsigned-long long z; }; |
align-of-void* | struct x { char y; void* z; }; |
align-of-size_t | struct x { char y; size_t z; }; |
align-of-float | struct x { char y; float z; }; |
align-of-double | struct x { char y; double z; }; |
align-of-int8_t | struct x { char y; int8_t z; }; |
align-of-int16_t | struct x { char y; int16_t z; }; |
align-of-int32_t | struct x { char y; int32_t z; }; |
align-of-int64_t | struct x { char y; int64_t z; }; |
on-darwin | |
on-linux | |
on-freebsd | |
on-openbsd | |
on-windows |
Make foreign c-function closure.
(c-function lib ret func . arg)
lib | library object returned by open-shared-library |
ret | return type of c-function. bool, int, char, double, float, void, char*, long-long, long, unsigned-long-long, unsigned-long, unsigned-int, unsigned-short, short, size_t, uint8_t, uint16_t, uint32_t, uint64_t, int8_t, int16_t, int32_t, int64_t and void* are supported. |
func | name of c-function as symbol |
arg | list of argument types. void*, int, double and char* are supported. |
Foreign function closure
Make c-callback. c-callback object should be free-ed with <<free-c-callback>>.
(make-c-callback return-type arg-type* proc)
return-tupe | return value type as symbol. bool, void, char, short, int, long, long-long, unsigned-short, unsigned-int, unsigned-long, unsigned-long-long, int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t, uint32_t, uint64_t, float, double, size_t and void* are supported. |
arg-type* | arguments type as list of symbols. bool, void, char, short, int, long, long-long, unsigned-short, unsigned-int, unsigned-long, unsigned-long-long, int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t, uint32_t, uint64_t, float, double, size_t and void* are supported. |
proc | procedure |
A pointer of c-callback
Make foreign c-function closure from pointer.
(pointer->c-function pointer ret-type name arg-types)
pointer | pointer to c-function which is lookuped by <<lookup-shared-library>>. |
ret-type | return type of c-function. void*, char*, void, double and int are supported. |
name | name of c-function as symbol arg-types- list of argument types. bool, int, char, double, float, void, char*, long-long, long, unsigned-long-long, unsigned-long, unsigned-int, unsigned-short, short, size_t, uint8_t, uint16_t, uint32_t, uint64_t, int8_t, int16_t, int32_t, int64_t and void* are supported. |
Foreign function closure
Make foreign c-function closure by lookuping a function named “name” in “lib” library.
(make-c-function lib ret-type name arg-types)
lib | library |
ret-type | return type of c-function. bool, int, char, double, float, void, char*, long-long, long, unsigned-long-long, unsigned-long, unsigned-int, unsigned-short, short, size_t, uint8_t, uint16_t, uint32_t, uint64_t, int8_t, int16_t, int32_t, int64_t and void* are supported. |
name | name of c-function as symbol arg-types- list of argument types. bool, int, char, double, float, void, char*, long-long, long, unsigned-long-long, unsigned-long, unsigned-int, unsigned-short, short, size_t, uint8_t, uint16_t, uint32_t, uint64_t, int8_t, int16_t, int32_t, int64_t and void* are supported. |
Foreign function closure
Returns a newly allocated (unless empty) string whose character sequence is encoded by the given null(\0) terminated bytevector. This is useful for converting “C” string to Scheme string.
(bytevector->string '#vu8(65 66 67 0 65 66 67) (native-transcoder)) => "ABC\x0;ABC" (null-terminated-bytevector->string '#vu8(65 66 67 0 65 66 67)) (native-transcoder) => "ABC"
(null-terminated-bytevector->string bv transcoder)
bv | null(\0) terminated bytevector. |
transcoder | transcoder |
decoded string.
Returns a newly allocated (unless empty) string whose character sequence is encoded by the given null(\0) terminated bytevector. This is useful for converting “C” string to Scheme string.
(utf8->string '#vu8(65 66 67 0 65 66 67)) => "ABC\x0;ABC" (null-terminated-utf8->string '#vu8(65 66 67 0 65 66 67)) => "ABC"
(null-terminated-bytevector->string bv transcoder)
bv | null(\0) terminated bytevector. |
transcoder | transcoder |
decoded string.