Commit | Line | Data |
---|---|---|
37371ea1 AW |
1 | #!/bin/sh |
2 | exec guile -q -s "$0" "$@" | |
3 | !# | |
4 | ||
17d819d4 | 5 | (use-modules (system foreign) |
07d22c02 | 6 | (rnrs bytevectors)) |
37371ea1 AW |
7 | |
8 | (define lib | |
9 | (dynamic-link (string-append (getenv "builddir") "/libtest-ffi"))) | |
10 | ||
8b8ce935 LC |
11 | (define failed? #f) |
12 | ||
37371ea1 AW |
13 | (define-syntax test |
14 | (syntax-rules () | |
15 | ((_ exp res) | |
16 | (let ((expected res) | |
17 | (actual exp)) | |
18 | (if (not (equal? actual expected)) | |
8b8ce935 LC |
19 | (begin |
20 | (set! failed? #t) | |
21 | (format (current-error-port) | |
22 | "bad return from expression `~a': expected ~A; got ~A~%" | |
23 | 'exp expected actual))))))) | |
37371ea1 AW |
24 | |
25 | ;;; | |
26 | ;;; No args | |
27 | ;;; | |
28 | (define f-v- | |
29 | (make-foreign-function void (dynamic-func "test_ffi_v_" lib) '())) | |
30 | (test (f-v-) *unspecified*) | |
31 | ||
32 | (define f-s8- | |
33 | (make-foreign-function int8 (dynamic-func "test_ffi_s8_" lib) '())) | |
34 | (test (f-s8-) -100) | |
35 | ||
36 | (define f-u8- | |
37 | (make-foreign-function uint8 (dynamic-func "test_ffi_u8_" lib) '())) | |
38 | (test (f-u8-) 200) | |
39 | ||
40 | (define f-s16- | |
41 | (make-foreign-function int16 (dynamic-func "test_ffi_s16_" lib) '())) | |
42 | (test (f-s16-) -20000) | |
43 | ||
44 | (define f-u16- | |
45 | (make-foreign-function uint16 (dynamic-func "test_ffi_u16_" lib) '())) | |
46 | (test (f-u16-) 40000) | |
47 | ||
48 | (define f-s32- | |
49 | (make-foreign-function int32 (dynamic-func "test_ffi_s32_" lib) '())) | |
50 | (test (f-s32-) -2000000000) | |
51 | ||
52 | (define f-u32- | |
53 | (make-foreign-function uint32 (dynamic-func "test_ffi_u32_" lib) '())) | |
54 | (test (f-u32-) 4000000000) | |
55 | ||
56 | (define f-s64- | |
57 | (make-foreign-function int64 (dynamic-func "test_ffi_s64_" lib) '())) | |
58 | (test (f-s64-) -2000000000) | |
59 | ||
60 | (define f-u64- | |
61 | (make-foreign-function uint64 (dynamic-func "test_ffi_u64_" lib) '())) | |
62 | (test (f-u64-) 4000000000) | |
63 | ||
64 | ;;; | |
65 | ;;; One u8 arg | |
66 | ;;; | |
67 | (define f-v-u8 | |
68 | (make-foreign-function void (dynamic-func "test_ffi_v_u8" lib) (list uint8))) | |
69 | (test (f-v-u8 10) *unspecified*) | |
70 | ||
71 | (define f-s8-u8 | |
72 | (make-foreign-function int8 (dynamic-func "test_ffi_s8_u8" lib) (list uint8))) | |
73 | (test (f-s8-u8 10) -90) | |
74 | ||
75 | (define f-u8-u8 | |
76 | (make-foreign-function uint8 (dynamic-func "test_ffi_u8_u8" lib) (list uint8))) | |
77 | (test (f-u8-u8 10) 210) | |
78 | ||
79 | (define f-s16-u8 | |
80 | (make-foreign-function int16 (dynamic-func "test_ffi_s16_u8" lib) (list uint8))) | |
81 | (test (f-s16-u8 10) -19990) | |
82 | ||
83 | (define f-u16-u8 | |
84 | (make-foreign-function uint16 (dynamic-func "test_ffi_u16_u8" lib) (list uint8))) | |
85 | (test (f-u16-u8 10) 40010) | |
86 | ||
87 | (define f-s32-u8 | |
88 | (make-foreign-function int32 (dynamic-func "test_ffi_s32_u8" lib) (list uint8))) | |
89 | (test (f-s32-u8 10) -1999999990) | |
90 | ||
91 | (define f-u32-u8 | |
92 | (make-foreign-function uint32 (dynamic-func "test_ffi_u32_u8" lib) (list uint8))) | |
93 | (test (f-u32-u8 10) 4000000010) | |
94 | ||
95 | (define f-s64-u8 | |
96 | (make-foreign-function int64 (dynamic-func "test_ffi_s64_u8" lib) (list uint8))) | |
97 | (test (f-s64-u8 10) -1999999990) | |
98 | ||
99 | (define f-u64-u8 | |
100 | (make-foreign-function uint64 (dynamic-func "test_ffi_u64_u8" lib) (list uint8))) | |
101 | (test (f-u64-u8 10) 4000000010) | |
102 | ||
103 | ||
104 | ;;; | |
105 | ;;; One s64 arg | |
106 | ;;; | |
107 | (define f-v-s64 | |
108 | (make-foreign-function void (dynamic-func "test_ffi_v_s64" lib) (list int64))) | |
109 | (test (f-v-s64 10) *unspecified*) | |
110 | ||
111 | (define f-s8-s64 | |
112 | (make-foreign-function int8 (dynamic-func "test_ffi_s8_s64" lib) (list int64))) | |
113 | (test (f-s8-s64 10) -90) | |
114 | ||
115 | (define f-u8-s64 | |
116 | (make-foreign-function uint8 (dynamic-func "test_ffi_u8_s64" lib) (list int64))) | |
117 | (test (f-u8-s64 10) 210) | |
118 | ||
119 | (define f-s16-s64 | |
120 | (make-foreign-function int16 (dynamic-func "test_ffi_s16_s64" lib) (list int64))) | |
121 | (test (f-s16-s64 10) -19990) | |
122 | ||
123 | (define f-u16-s64 | |
124 | (make-foreign-function uint16 (dynamic-func "test_ffi_u16_s64" lib) (list int64))) | |
125 | (test (f-u16-s64 10) 40010) | |
126 | ||
127 | (define f-s32-s64 | |
128 | (make-foreign-function int32 (dynamic-func "test_ffi_s32_s64" lib) (list int64))) | |
129 | (test (f-s32-s64 10) -1999999990) | |
130 | ||
131 | (define f-u32-s64 | |
132 | (make-foreign-function uint32 (dynamic-func "test_ffi_u32_s64" lib) (list int64))) | |
133 | (test (f-u32-s64 10) 4000000010) | |
134 | ||
135 | (define f-s64-s64 | |
136 | (make-foreign-function int64 (dynamic-func "test_ffi_s64_s64" lib) (list int64))) | |
137 | (test (f-s64-s64 10) -1999999990) | |
138 | ||
139 | (define f-u64-s64 | |
140 | (make-foreign-function uint64 (dynamic-func "test_ffi_u64_s64" lib) (list int64))) | |
141 | (test (f-u64-s64 10) 4000000010) | |
142 | ||
143 | ||
144 | ;; | |
145 | ;; Multiple int args of differing types | |
146 | ;; | |
147 | (define f-sum | |
148 | (make-foreign-function int64 (dynamic-func "test_ffi_sum" lib) | |
149 | (list int8 int16 int32 int64))) | |
150 | (test (f-sum -1 2000 -30000 40000000000) | |
151 | (+ -1 2000 -30000 40000000000)) | |
152 | ||
c612ed59 AW |
153 | ;; |
154 | ;; Structs | |
155 | ;; | |
156 | (define f-sum-struct | |
157 | (make-foreign-function int64 (dynamic-func "test_ffi_sum_struct" lib) | |
158 | (list (list int8 int16 int32 int64)))) | |
159 | (test (f-sum-struct (make-c-struct (list int8 int16 int32 int64) | |
160 | (list -1 2000 -30000 40000000000))) | |
161 | (+ -1 2000 -30000 40000000000)) | |
17d819d4 AW |
162 | ;; |
163 | ;; Structs | |
164 | ;; | |
165 | (define f-memcpy | |
166 | (make-foreign-function '* (dynamic-func "test_ffi_memcpy" lib) | |
167 | (list '* '* int32))) | |
d4149a51 | 168 | (let* ((src* '(0 1 2 3 4 5 6 7)) |
5b46a8c2 LC |
169 | (src (bytevector->pointer (u8-list->bytevector src*))) |
170 | (dest (bytevector->pointer (make-bytevector 16 0))) | |
d4149a51 | 171 | (res (f-memcpy dest src (length src*)))) |
5b46a8c2 | 172 | (or (= (pointer-address dest) (pointer-address res)) |
17d819d4 | 173 | (error "memcpy res not equal to dest")) |
5b46a8c2 | 174 | (or (equal? (bytevector->u8-list (pointer->bytevector dest 16)) |
17d819d4 AW |
175 | '(0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0)) |
176 | (error "unexpected dest"))) | |
c612ed59 | 177 | |
d12f974b LC |
178 | \f |
179 | ;;; | |
180 | ;;; Global symbols. | |
181 | ;;; | |
182 | ||
07d22c02 | 183 | (use-modules ((rnrs bytevectors) #:select (utf8->string))) |
d12f974b LC |
184 | |
185 | (if (defined? 'setlocale) | |
186 | (setlocale LC_ALL "C")) | |
187 | ||
188 | (define global (dynamic-link)) | |
189 | ||
190 | (define strerror | |
191 | (make-foreign-function '* (dynamic-func "strerror" global) | |
192 | (list int))) | |
193 | ||
194 | (define strlen | |
195 | (make-foreign-function size_t (dynamic-func "strlen" global) | |
196 | (list '*))) | |
197 | ||
198 | (let* ((ptr (strerror ENOENT)) | |
199 | (len (strlen ptr)) | |
5b46a8c2 | 200 | (bv (pointer->bytevector ptr len 0 'u8)) |
d12f974b LC |
201 | (str (utf8->string bv))) |
202 | (test #t (not (not (string-contains str "file"))))) | |
37371ea1 | 203 | |
8b8ce935 LC |
204 | (exit (not failed?)) |
205 | ||
37371ea1 AW |
206 | ;; Local Variables: |
207 | ;; mode: scheme | |
8b8ce935 | 208 | ;; End: |