This library provides an on the fly C foreign function interface.
|
load-shared-object | Loads a shared object, and returns its handle |
c-function | Transcribes a stub code—lambda expression—that calls a C foreign function |
c-function/errno | Is similar to c-function, but it transcribes a stub code that returns two values |
c-function/win32-lasterror | Is similar to c-function, but it transcribes a stub code that returns two values |
lookup-shared-object | Returns an address corresponding to a symbol name in a shared object |
make-cdecl-callout | Returns a closure that calls a foreign function using the __cdecl calling convention |
make-cdecl-callback | Returns a C callback function pointer using the __cdecl calling convention |
make-stdcall-callout | Is similar to make-cdecl-callout, but it uses the __stdcall calling convention |
make-stdcall-callback | Is similar to make-cdecl-callback, but it uses the __stdcall calling convention |
make-bytevector-mapping | Provides transparent access to an arbitrary memory block |
bytevector-mapping? | Returns #t if its argument is a bytevector-mapping object, and otherwise returns #f |
shared-object-errno | Is a parameter contains a copy of thread local errno value |
shared-object-win32-lasterror | Is a parameter contains a copy of thread local win32 lasterror value |
win32-error->string | Returns an error message string corresponding to a win32 error code |
on-darwin | <constant> |
on-linux | <constant> |
on-freebsd | <constant> |
on-openbsd | <constant> |
on-posix | <constant> |
on-windows | <constant> |
on-ia32 | <constant> |
on-x64 | <constant> |
on-ppc32 | <constant> |
on-ppc64 | <constant> |
load-shared-object loads a shared object, and returns its handle.
|
(load-shared-object)
(load-shared-object filename)
=> <handle>
|
filename:
|
<string>
|
load-shared-object returns the handle for the main program if filename is omitted.
|
> (import (rnrs) (ypsilon ffi))
> (load-shared-object)
3086820976
> (load-shared-object "libc.so.6") ; Ubuntu 9.04
3086481440
|
c-function transcribes a stub code—lambda expression—that calls a C foreign function. Ypsilon handles data conversion between C and Scheme.
|
(c-function so-handle so-name return-type function-name (argument-type ...))
(c-function so-handle so-name return-type __cdecl function-name (argument-type ...))
(c-function so-handle so-name return-type __stdcall function-name (argument-type ...))
|
so-handle:
so-name:
return-type:
function-name:
(argument-type ...):
|
<handle>
<string>
<symbol>
<symbol>
<subform>
|
|
(import (rnrs) (ypsilon ffi))
;; load GLUT library
(define lib (load-shared-object "libglut.so.3")) ; Ubuntu 8.10
;; void glutPositionWindow(int x, int y)
(define glutPositionWindow
(c-function lib "GLUT"
void glutPositionWindow (int int)))
;; void glutMouseFunc(void (*func)(int button, int state, int x, int y))
(define glutMouseFunc
(c-function lib "GLUT"
void glutMouseFunc ([c-callback void (int int int int)])))
;; void glutAddMenuEntry(const char *label, int value)
;; void glutMotionFunc(void (*func)(int x, int y))
(let-syntax ((defun-glut ; with handy macro
(syntax-rules ()
((_ ret name args)
(define name (c-function lib "GLUT" ret name args))))))
(defun-glut void glutAddMenuEntry (char* int))
(defun-glut void glutMotionFunc ([c-callback void (int int)])))
|
c-function/errno is similar to c-function, but it transcribes a stub code that returns two values. The first is a C function return value, and the second is a captured errno value on return of the C function.
|
> (import (rnrs) (ypsilon ffi))
> (define lib (load-shared-object "libc.so.6")) ; Ubuntu 8.10
> (define strerror
(c-function lib "libc"
char* strerror (int)))
> (define chdir
(c-function/errno lib "libc"
int chdir (char*)))
> (chdir "/")
#<values 0 0> ; success, errno is meaningless on success as in C.
> (chdir "/tmp/non-exists/foo/bar")
#<values -1 2> ; failure, errno is 2 (ENOENT on linux).
> (define my-chdir ; define wrapper function
(lambda (path)
(let-values (((retval errno) (chdir path)))
(when (< retval 0)
(assertion-violation 'my-chdir (strerror errno) (list path retval errno))))))
> (my-chdir "/tmp/non-exists/foo/bar")
error in my-chdir: No such file or directory
irritants:
("/non-exist/foo/bar" -1 2)
|
c-function/win32-lasterror is similar to c-function, but it transcribes a stub code that returns two values. The first is a C function return value, and the second is a value that obtained by calling GetLastError() on return of the C function.
|
lookup-shared-object returns an address corresponding to a symbol name in a shared object.
|
(lookup-shared-object so-handle so-symbol)
=> <address>
|
so-handle:
so-symbol:
|
<handle>
<string> or <symbol>
|
> (import (rnrs) (ypsilon ffi))
> (define libc (load-shared-object "libc.so.6")) ; Ubuntu 8.10
> (lookup-shared-object libc 'puts)
1075667872
> (lookup-shared-object libc "strlen")
1075757712
|
make-cdecl-callout returns a closure that calls a foreign function using the __cdecl calling convention.
|
(make-cdecl-callout return-type argument-types function-address)
=> <procedure>
|
return-type:
argument-types:
function-address:
|
<symbol>
<list>
<address>
|
Refer c-function for details of declarators.
|
> (import (rnrs) (ypsilon ffi))
> (define libc (load-shared-object "libc.so.6")) ; Ubuntu 8.10
> (define strcmp ; int strcmp(const char *s1, const char *s2)
(make-cdecl-callout 'int '(char* char*) (lookup-shared-object libc "strcmp")))
> (strcmp "foo" "bar")
1
> (strcmp "hello" "hello")
0
|
make-cdecl-callback returns a C callback function pointer using the __cdecl calling convention.
|
(make-cdecl-callback return-type argument-types procedure)
=> <address>
|
return-type:
argument-types:
procedure:
|
<symbol>
<list>
<procedure>
|
Refer c-function for details of declarators.
|
> (import (rnrs) (ypsilon ffi))
> (define my-expt (lambda (n1 n2) (expt n1 n2)))
> (define callback (make-cdecl-callback 'int '(int int) my-expt))
> callback
150958158 ; function address
> (define callout (make-cdecl-callout 'int '(int int) callback))
> callout
#<closure 0x77927a20> ; wrapper closure
> (callout 10 3)
1000
;; -> (callout 10 3) [Scheme]
;; -> callback(10, 3) [C]
;; -> (my-expt 10 3) [Scheme]
;; 1000 [Scheme]
;; 1000 [C]
;; 1000 [Scheme]
|
make-stdcall-callout is similar to make-cdecl-callout, but it uses the __stdcall calling convention.
|
make-stdcall-callback is similar to make-cdecl-callback, but it uses the __stdcall calling convention.
|
make-bytevector-mapping provides transparent access to an arbitrary memory block.
|
(make-bytevector-mapping address bytesize)
=> <bytevector-mapping>
|
address:
bytesize:
|
<address>
<int>
|
The bytesize must be non-negative.
|
make-bytevector-mapping returns a bytevector-mapping object that can be used as an ordinary bytevector. Its contents are mapped to the memory block within the range of address to (address + bytesize - 1).
|
Be aware that misuse of this procedure causes a fatal error, segmentation fault for an example.
|
> (import (rnrs) (ypsilon ffi))
> (define libc (load-shared-object "libc.so.6")) ; Ubuntu 8.10
> (define qsort
(c-function libc "libc"
void qsort (void* int int [c-callback int (void* void*)])))
> (define comp
(lambda (a1 a2)
(let ((n1 (bytevector-u32-native-ref (make-bytevector-mapping a1 4) 0))
(n2 (bytevector-u32-native-ref (make-bytevector-mapping a2 4) 0)))
(cond ((= n1 n2) 0)
((> n1 n2) 1)
(else -1)))))
> (define nums (uint-list->bytevector '(10000 1000 10 100000 100) (native-endianness) 4))
> (bytevector->uint-list nums (native-endianness) 4)
(10000 1000 10 100000 100)
> (qsort nums 5 4 comp)
> (bytevector->uint-list nums (native-endianness) 4)
(10 100 1000 10000 100000)
|
bytevector-mapping? returns #t if its argument is a bytevector-mapping object, and otherwise returns #f.
|
(bytevector-mapping? x)
=> <boolean>
|
x:
|
<object>
|
shared-object-errno is a parameter contains a copy of thread local errno value.
|
(shared-object-errno errno-value)
=> unspecified
(shared-object-errno)
=> <int>
|
errno-value:
|
<int>
|
The errno-value must be within the range [INT_MIN, INT_MAX].
|
Ypsilon captures the thread local errno value for each return of a foreign C function call. An assignment to this parameter also changes the thread local errno value.
|
shared-object-win32-lasterror is a parameter contains a copy of thread local win32 lasterror value.
|
(shared-object-win32-lasterror lasterror-value)
=> unspecified
(shared-object-win32-lasterror)
=> <int>
|
lasterror-value:
|
<int>
|
The lasterror-value must be within the range [INT32_MIN, UINT32_MAX].
|
On Windows platforms, Ypsilon captures a win32 lasterror value by calling GetLastError() for each return of a foreign C function call. An assignment to this parameter also changes the win32 lasterror value by calling SetLastError().
|
Using this parameter on that other than Windows platforms causes an assertion violation.
|
win32-error->string returns an error message string corresponding to a win32 error code.
|
(win32-error->string win32-error-code)
=> <string>
|
win32-error-code:
|
<int>
|
The win32-error-code must be within the range [INT32_MIN, UINT32_MAX].
|
On Windows platforms, this procedure uses FormatMessage() to get an error message from a system.
|
Using this procedure on that other than Windows platforms causes an assertion violation.
|
Constants: | on-darwin, on-linux, on-freebsd, on-openbsd, on-windows, on-posix |
Each constant is defined to a boolean value. It can be used to determine a operating system.
|
Constants: | on-ia32, on-x64, on-ppc32, on-ppc64 |
Each constant is defined to a boolean value. It can be used to determine a CPU instruction set.
|