| 1 |
(use gauche.test) |
| 2 |
(use gauche.config) |
| 3 |
(use gauche.ffitest) |
| 4 |
(use util.match) |
| 5 |
(use file.util) |
| 6 |
|
| 7 |
(cond-expand |
| 8 |
[gauche.os.windows (exit 0)] |
| 9 |
[else |
| 10 |
;; Currently we only support those platforms |
| 11 |
(unless (#/^x86_64-.*(-linux|-darwin)/ (gauche-config "--arch")) |
| 12 |
(exit 0))]) |
| 13 |
|
| 14 |
(test-start "ffitest") |
| 15 |
|
| 16 |
;;========================================================================== |
| 17 |
(test-section "raw call") |
| 18 |
|
| 19 |
(define (foreign-call dlo name args rettype) |
| 20 |
((with-module gauche.internal call-amd64) |
| 21 |
(dlobj-get-entry-address dlo name) |
| 22 |
args rettype)) |
| 23 |
|
| 24 |
(define (test-foreign-call dlo name expected args rettype) |
| 25 |
(test* #"call ~name" expected |
| 26 |
(foreign-call dlo name args rettype))) |
| 27 |
|
| 28 |
(let ((dlo (dynamic-load "gauche--ffitest" :init-function #f))) |
| 29 |
(test* "open dlo" #t (is-a? dlo <dlobj>)) |
| 30 |
(let ((dle (dlobj-get-entry-address dlo "_f_v"))) |
| 31 |
(test* "get dlptr" #t (is-a? dle <dlptr>)) |
| 32 |
(test* "call f_o" (list (undefined) "it works") |
| 33 |
(let* ((r #f) |
| 34 |
(s (with-output-to-string |
| 35 |
(^[] |
| 36 |
(set! r ((with-module gauche.internal call-amd64) |
| 37 |
dle '() 'v)))))) |
| 38 |
(list r s)))) |
| 39 |
|
| 40 |
(test-section "simple, register passing call (integral)") |
| 41 |
(test-foreign-call dlo "_f_o" 'it_works '() 'o) |
| 42 |
(test-foreign-call dlo "_f_i" 42 '() 'i) |
| 43 |
(test-foreign-call dlo "_f_s" "it works" '() 's) |
| 44 |
|
| 45 |
(test-foreign-call dlo "_fo_o" '(wow . huh) '((o wow)) 'o) |
| 46 |
(test-foreign-call dlo "_fi_o" '(7 . huh) '((i 6)) 'o) |
| 47 |
(test-foreign-call dlo "_fi_o" '(-9 . huh) '((i -10)) 'o) |
| 48 |
(test-foreign-call dlo "_fs_o" 5 '((s "hello")) 'o) |
| 49 |
(test-foreign-call dlo "_fo_i" 3 '((o (a b c))) 'i) |
| 50 |
(test-foreign-call dlo "_fi_i" 121 '((i 11)) 'i) |
| 51 |
(test-foreign-call dlo "_fs_i" 6 '((s "gauche")) 'i) |
| 52 |
(test-foreign-call dlo "_fo_s" "(a b c)" '((o (a b c))) 's) |
| 53 |
|
| 54 |
(test-foreign-call dlo "_foo_o" '(a . b) '((o a) (o b)) 'o) |
| 55 |
(test-foreign-call dlo "_foi_o" '(a . 1) '((o a) (i 0)) 'o) |
| 56 |
(test-foreign-call dlo "_fis_i" (char->integer #\c) '((i 2) (s "abcde")) 'i) |
| 57 |
|
| 58 |
(test-foreign-call dlo "_fois_o" '("foo" 100 (cent)) |
| 59 |
'((o (cent)) (i 100) (s "foo")) 'o) |
| 60 |
(test-foreign-call dlo "_foiso_o" '(3+2i "foo" 100 (cent)) |
| 61 |
'((o (cent)) (i 100) (s "foo") (o 3+2i)) 'o) |
| 62 |
(test-foreign-call dlo "_foisoi_o" '(-56789 3+2i "foo" 100 (cent)) |
| 63 |
'((o (cent)) (i 100) (s "foo") (o 3+2i) (i -56789)) 'o) |
| 64 |
(test-foreign-call dlo "_foisois_o" '("" -56789 3+2i "foo" 100 (cent)) |
| 65 |
'((o (cent)) (i 100) (s "foo") (o 3+2i) (i -56789) (s "")) |
| 66 |
'o) |
| 67 |
|
| 68 |
(test-section "register passing (flonum)") |
| 69 |
(test-foreign-call dlo "_fd_o" 101.0 '((d 100.0)) 'o) |
| 70 |
(test-foreign-call dlo "_fid_o" 99.0 '((i 100) (d 1.0)) 'o) |
| 71 |
(test-foreign-call dlo "_fdi_o" 99.0 '((d 100.0) (i 1)) 'o) |
| 72 |
(test-foreign-call dlo "_fiiiiii_d" 10.5 |
| 73 |
'((i 1) (i 2) (i 3) (i 4) (i 5) (i 6)) 'd) |
| 74 |
|
| 75 |
(test-section "calling back to Scheme") |
| 76 |
(test-foreign-call dlo "_fo_o_cb" '(z . z) '((o z)) 'o) |
| 77 |
(test-foreign-call dlo "_foo_o_cb" '(d c b a) |
| 78 |
`((o ,reverse) (o (a b c d))) 'o) |
| 79 |
(test-foreign-call dlo "_foo_o_cb" |
| 80 |
(test-error <error> "list required, but got zzz") |
| 81 |
`((o ,reverse) (o zzz)) 'o) |
| 82 |
|
| 83 |
(test-section "spill-to-stack case") |
| 84 |
(test-foreign-call dlo "_fooooooo_o" '((a b c d e) (f g)) |
| 85 |
`((o a) (o b) (o c) (o d) (o e) (o f) (o g)) 'o) |
| 86 |
(test-foreign-call dlo "_foooooooo_o" '((a b c d e) (f g h)) |
| 87 |
`((o a) (o b) (o c) (o d) (o e) (o f) (o g) (o h)) 'o) |
| 88 |
(test-foreign-call dlo "_fooooooooi_o" '((a b c d e) (f g h 5)) |
| 89 |
`((o a) (o b) (o c) (o d) (o e) (o f) (o g) (o h) (i 4)) |
| 90 |
'o) |
| 91 |
(test-foreign-call dlo "_fdddddddddd_d" |
| 92 |
(* (+ 1.0 1.1 1.2 1.3 1.4) (+ 1.5 1.6 1.7 1.8 1.9)) |
| 93 |
`((d 1.0) (d 1.1) (d 1.2) (d 1.3) (d 1.4) |
| 94 |
(d 1.5) (d 1.6) (d 1.7) (d 1.8) (d 1.9)) |
| 95 |
'd) |
| 96 |
(test-foreign-call dlo "_fiiiiiiddddddddidid_d" |
| 97 |
(* (+ 1 2 3 4 5 6 7 8) |
| 98 |
(+ 1.0 1.1 1.2 1.3 1.4 |
| 99 |
1.5 1.6 1.7 1.8 1.9)) |
| 100 |
`((i 1) (i 2) (i 3) (i 4) (i 5) (i 6) |
| 101 |
(d 1.0) (d 1.1) (d 1.2) (d 1.3) |
| 102 |
(d 1.4) (d 1.5) (d 1.6) (d 1.7) |
| 103 |
(i 7) (d 1.8) (i 8) (d 1.9)) |
| 104 |
'd) |
| 105 |
|
| 106 |
(test-foreign-call dlo "_fooooooooooo_o_cb" |
| 107 |
'(A B C D E F G H I . J) |
| 108 |
`((o ,list*) (o A) (o B) (o C) (o D) (o E) |
| 109 |
(o F) (o G) (o H) (o I) (o J)) |
| 110 |
'o) |
| 111 |
|
| 112 |
(test-foreign-call dlo "_fio_var_o" |
| 113 |
'(A A B B C C) |
| 114 |
'((i 3) (o A) (o B) (o C)) |
| 115 |
'o) |
| 116 |
(test-foreign-call dlo "_fio_var_o" |
| 117 |
'(A A B B C C D D E E F F G G H H) |
| 118 |
'((i 8) (o A) (o B) (o C) (o D) (o E) (o F) (o G) (o H)) |
| 119 |
'o) |
| 120 |
(test-foreign-call dlo "_fido_var_o" |
| 121 |
'(A A B B C C D D 1.0) |
| 122 |
'((i 4) (d 1.0) (o A) (o B) (o C) (o D)) |
| 123 |
'o) |
| 124 |
|
| 125 |
(test-section "ensure error frees codepad memory") |
| 126 |
(test* "error and codepad memory management" #t |
| 127 |
(let1 proc (lambda (_) (error "wow")) |
| 128 |
(dotimes [2000] |
| 129 |
(guard (e [else #t]) |
| 130 |
(foreign-call dlo "_foo_o_cb" `((o ,proc) (o #f)) 'o))) |
| 131 |
#t)) |
| 132 |
) |
| 133 |
|
| 134 |
;;========================================================================== |
| 135 |
(test-section "FFI API") |
| 136 |
|
| 137 |
(use gauche.ffi) |
| 138 |
(test-module 'gauche.ffi) |
| 139 |
|
| 140 |
(let ((ffi #f)) |
| 141 |
(define-syntax t |
| 142 |
(syntax-rules () |
| 143 |
[(t (fn args ...) expect) |
| 144 |
(test* (format "call-foreign ~s" 'fn) expect |
| 145 |
(call-foreign ffi 'fn args ...))])) |
| 146 |
|
| 147 |
(test* "load-foreign" '(#t (f_i () i) (f_i_i (i) i)) |
| 148 |
(let ((dir (sys-dirname (current-load-path)))) |
| 149 |
(set! ffi (load-foreign (build-path dir "test/f.h") |
| 150 |
"test/f")) |
| 151 |
(list (is-a? ffi <foreign-library>) |
| 152 |
(and-let1 e (hash-table-get (~ ffi 'entries) 'f_i #f) |
| 153 |
`(,(~ e'name) ,(~ e'arg-signatures) ,(~ e'ret-signature))) |
| 154 |
(and-let1 e (hash-table-get (~ ffi 'entries) 'f_i_i #f) |
| 155 |
`(,(~ e'name) ,(~ e'arg-signatures) ,(~ e'ret-signature)))))) |
| 156 |
|
| 157 |
(t (f_c) 9) |
| 158 |
(t (f_i) 42) |
| 159 |
(t (f_f) 1.25) |
| 160 |
(t (f_d) 3.14) |
| 161 |
(t (f_v) (undefined)) |
| 162 |
|
| 163 |
(t (f_i_i 98) 99) |
| 164 |
(t (f_f_f 0.25) 0.125) |
| 165 |
(t (f_d_d 0.5) 1.0) |
| 166 |
) |
| 167 |
|
| 168 |
(test-end) |