7
(test-start "c-wrapper (ffi)")
11
(test-module 'c-wrapper)
13
(define DYLIB (string-append "./ffitest." (gauche-config "--dylib-suffix")))
14
(define dlopen (with-module c-wrapper dlopen))
15
(define dlclose (with-module c-wrapper dlclose))
16
(define dlsym (with-module c-wrapper dlsym))
17
(define ffi-type-uint (with-module c-wrapper ffi-type-uint))
18
(define ffi-type-sint (with-module c-wrapper ffi-type-sint))
19
(define ffi-type-pointer (with-module c-wrapper ffi-type-pointer))
20
(define ffi-prep-cif (with-module c-wrapper ffi-prep-cif))
21
(define ffi-call (with-module c-wrapper ffi-call))
22
(define ffi-prep-closure (with-module c-wrapper ffi-prep-closure))
27
(let ((handle (dlopen DYLIB (with-module c-wrapper RTLD_NOW))))
36
(let* ((handle (dlopen DYLIB (with-module c-wrapper RTLD_NOW)))
37
(ptr (dlsym handle "add_uint")))
43
(with-module c-wrapper FFI_OK)
45
(let* ((handle (dlopen DYLIB (with-module c-wrapper RTLD_NOW)))
46
(fn (dlsym handle "add_uint")))
48
(ffi-prep-cif (ffi-type-uint) (list (ffi-type-uint)
57
(let* ((handle (dlopen DYLIB (with-module c-wrapper RTLD_NOW)))
58
(fn (dlsym handle "add_uint"))
59
(rvalue (make <c-uint>))
63
(ffi-prep-cif (ffi-type-sint) (list (ffi-type-uint)
67
(ffi-call cif fn (ptr rvalue)
68
(list (ptr v1) (ptr v2)))
76
(let* ((handle (dlopen DYLIB (with-module c-wrapper RTLD_NOW)))
77
(fn (dlsym handle "callback")))
79
(ffi-prep-cif (ffi-type-sint) (list (ffi-type-sint)
81
(receive (status closure)
82
(ffi-prep-closure cif (lambda (v1 v2)
83
(let ((result (make <c-int>)))
85
(+ ((deref (cast (ptr <c-int>) v1)))
86
((deref (cast (ptr <c-int>) v2)))))
90
(and (= status (with-module c-wrapper FFI_OK)) (not (null-ptr? closure)))
91
(dlclose handle)))))))
96
(let* ((handle (dlopen DYLIB (with-module c-wrapper RTLD_NOW)))
97
(fn (dlsym handle "callback_sint"))
100
(rvalue (make <c-int>)))
101
(receive (status cif_closure)
102
(ffi-prep-cif (ffi-type-sint) (list (ffi-type-sint)
104
(receive (status closure)
105
(ffi-prep-closure cif_closure
107
(let ((result (make <c-int>)))
109
(+ ((deref (cast (ptr <c-int>) v1)))
110
((deref (cast (ptr <c-int>) v2)))))
112
(receive (status cif)
113
(ffi-prep-cif (ffi-type-sint) (list (ffi-type-pointer)
118
(ffi-call cif fn (ptr rvalue) (list (ptr closure)
123
(dlclose handle))))))))