1 IN: alien.compiler.tests
2 USING: alien alien.c-types alien.syntax compiler kernel
3 namespaces namespaces tools.test sequences inference words
4 arrays parser quotations continuations inference.backend effects
5 namespaces.private io io.streams.string memory system threads
8 FUNCTION: void ffi_test_0 ;
9 [ ] [ ffi_test_0 ] unit-test
11 FUNCTION: int ffi_test_1 ;
12 [ 3 ] [ ffi_test_1 ] unit-test
14 FUNCTION: int ffi_test_2 int x int y ;
15 [ 5 ] [ 2 3 ffi_test_2 ] unit-test
16 [ "hi" 3 ffi_test_2 ] must-fail
18 FUNCTION: int ffi_test_3 int x int y int z int t ;
19 [ 25 ] [ 2 3 4 5 ffi_test_3 ] unit-test
21 FUNCTION: float ffi_test_4 ;
22 [ 1.5 ] [ ffi_test_4 ] unit-test
24 FUNCTION: double ffi_test_5 ;
25 [ 1.5 ] [ ffi_test_5 ] unit-test
27 FUNCTION: int ffi_test_9 int a int b int c int d int e int f int g ;
28 [ 28 ] [ 1 2 3 4 5 6 7 ffi_test_9 ] unit-test
29 [ "a" 2 3 4 5 6 7 ffi_test_9 ] must-fail
30 [ 1 2 3 4 5 6 "a" ffi_test_9 ] must-fail
37 : make-foo ( x y -- foo )
38 "foo" <c-object> [ set-foo-y ] keep [ set-foo-x ] keep ;
40 FUNCTION: int ffi_test_11 int a foo b int c ;
42 [ 14 ] [ 1 2 3 make-foo 4 ffi_test_11 ] unit-test
44 FUNCTION: int ffi_test_13 int a int b int c int d int e int f int g int h int i int j int k ;
46 [ 66 ] [ 1 2 3 4 5 6 7 8 9 10 11 ffi_test_13 ] unit-test
48 FUNCTION: foo ffi_test_14 int x int y ;
50 [ 11 6 ] [ 11 6 ffi_test_14 dup foo-x swap foo-y ] unit-test
52 FUNCTION: char* ffi_test_15 char* x char* y ;
54 [ "foo" ] [ "xy" "zt" ffi_test_15 ] unit-test
55 [ "bar" ] [ "xy" "xy" ffi_test_15 ] unit-test
56 [ 1 2 ffi_test_15 ] must-fail
64 FUNCTION: bar ffi_test_16 long x long y long z ;
67 11 6 -7 ffi_test_16 dup bar-x over bar-y rot bar-z
74 FUNCTION: tiny ffi_test_17 int x ;
76 [ 11 ] [ 11 ffi_test_17 tiny-x ] unit-test
78 [ [ alien-indirect ] infer ] [ inference-error? ] must-fail-with
80 : indirect-test-1 ( ptr -- result )
81 "int" { } "cdecl" alien-indirect ;
83 { 1 1 } [ indirect-test-1 ] must-infer-as
85 [ 3 ] [ "ffi_test_1" f dlsym indirect-test-1 ] unit-test
87 [ -1 indirect-test-1 ] must-fail
89 : indirect-test-2 ( x y ptr -- result )
90 "int" { "int" "int" } "cdecl" alien-indirect gc ;
92 { 3 1 } [ indirect-test-2 ] must-infer-as
95 [ 2 3 "ffi_test_2" f dlsym indirect-test-2 ]
98 : indirect-test-3 ( a b c d ptr -- result )
99 "int" { "int" "int" "int" "int" } "stdcall" alien-indirect
102 << "f-stdcall" f "stdcall" add-library >>
104 [ f ] [ "f-stdcall" load-library ] unit-test
105 [ "stdcall" ] [ "f-stdcall" library library-abi ] unit-test
107 : ffi_test_18 ( w x y z -- int )
108 "int" "f-stdcall" "ffi_test_18" { "int" "int" "int" "int" }
111 [ 25 ] [ 2 3 4 5 ffi_test_18 ] unit-test
113 : ffi_test_19 ( x y z -- bar )
114 "bar" "f-stdcall" "ffi_test_19" { "long" "long" "long" }
118 11 6 -7 ffi_test_19 dup bar-x over bar-y rot bar-z
121 FUNCTION: double ffi_test_6 float x float y ;
122 [ 6.0 ] [ 3.0 2.0 ffi_test_6 ] unit-test
123 [ "a" "b" ffi_test_6 ] must-fail
125 FUNCTION: double ffi_test_7 double x double y ;
126 [ 6.0 ] [ 3.0 2.0 ffi_test_7 ] unit-test
128 FUNCTION: double ffi_test_8 double x float y double z float t int w ;
129 [ 19.0 ] [ 3.0 2.0 1.0 6.0 7 ffi_test_8 ] unit-test
131 FUNCTION: int ffi_test_10 int a int b double c int d float e int f int g int h ;
132 [ -34 ] [ 1 2 3.0 4 5.0 6 7 8 ffi_test_10 ] unit-test
134 FUNCTION: void ffi_test_20 double x1, double x2, double x3,
135 double y1, double y2, double y3,
136 double z1, double z2, double z3 ;
138 [ ] [ 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 ffi_test_20 ] unit-test
140 ! Make sure XT doesn't get clobbered in stack frame
142 : ffi_test_31 ( a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a ptr -- result y )
145 { "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" }
148 [ 3 ] [ 42 [ ] each ffi_test_31 ] unit-test
150 FUNCTION: longlong ffi_test_21 long x long y ;
152 [ 121932631112635269 ]
153 [ 123456789 987654321 ffi_test_21 ] unit-test
155 FUNCTION: long ffi_test_22 long x longlong y longlong z ;
158 [ 1111 121932631112635269 123456789 ffi_test_22 ] unit-test
160 [ 1111 f 123456789 ffi_test_22 ] must-fail
174 [ set-rect-x ] keep ;
176 FUNCTION: int ffi_test_12 int a int b rect c int d int e int f ;
178 [ 45 ] [ 1 2 3.0 4.0 5.0 6.0 <rect> 7 8 9 ffi_test_12 ] unit-test
180 [ 1 2 { 1 2 3 } 7 8 9 ffi_test_12 ] must-fail
182 FUNCTION: float ffi_test_23 ( float[3] x, float[3] y ) ;
184 [ 32.0 ] [ { 1.0 2.0 3.0 } >c-float-array { 4.0 5.0 6.0 } >c-float-array ffi_test_23 ] unit-test
186 ! Test odd-size structs
187 C-STRUCT: test-struct-1 { { "char" 1 } "x" } ;
189 FUNCTION: test-struct-1 ffi_test_24 ;
191 [ B{ 1 } ] [ ffi_test_24 ] unit-test
193 C-STRUCT: test-struct-2 { { "char" 2 } "x" } ;
195 FUNCTION: test-struct-2 ffi_test_25 ;
197 [ B{ 1 2 } ] [ ffi_test_25 ] unit-test
199 C-STRUCT: test-struct-3 { { "char" 3 } "x" } ;
201 FUNCTION: test-struct-3 ffi_test_26 ;
203 [ B{ 1 2 3 } ] [ ffi_test_26 ] unit-test
205 C-STRUCT: test-struct-4 { { "char" 4 } "x" } ;
207 FUNCTION: test-struct-4 ffi_test_27 ;
209 [ B{ 1 2 3 4 } ] [ ffi_test_27 ] unit-test
211 C-STRUCT: test-struct-5 { { "char" 5 } "x" } ;
213 FUNCTION: test-struct-5 ffi_test_28 ;
215 [ B{ 1 2 3 4 5 } ] [ ffi_test_28 ] unit-test
217 C-STRUCT: test-struct-6 { { "char" 6 } "x" } ;
219 FUNCTION: test-struct-6 ffi_test_29 ;
221 [ B{ 1 2 3 4 5 6 } ] [ ffi_test_29 ] unit-test
223 C-STRUCT: test-struct-7 { { "char" 7 } "x" } ;
225 FUNCTION: test-struct-7 ffi_test_30 ;
227 [ B{ 1 2 3 4 5 6 7 } ] [ ffi_test_30 ] unit-test
229 C-STRUCT: test-struct-8 { "double" "x" } { "double" "y" } ;
231 FUNCTION: double ffi_test_32 test-struct-8 x int y ;
234 "test-struct-8" <c-object>
235 1.0 over set-test-struct-8-x
236 2.0 over set-test-struct-8-y
240 C-STRUCT: test-struct-9 { "float" "x" } { "float" "y" } ;
242 FUNCTION: double ffi_test_33 test-struct-9 x int y ;
245 "test-struct-9" <c-object>
246 1.0 over set-test-struct-9-x
247 2.0 over set-test-struct-9-y
251 C-STRUCT: test-struct-10 { "float" "x" } { "int" "y" } ;
253 FUNCTION: double ffi_test_34 test-struct-10 x int y ;
256 "test-struct-10" <c-object>
257 1.0 over set-test-struct-10-x
258 2 over set-test-struct-10-y
262 C-STRUCT: test-struct-11 { "int" "x" } { "int" "y" } ;
264 FUNCTION: double ffi_test_35 test-struct-11 x int y ;
267 "test-struct-11" <c-object>
268 1 over set-test-struct-11-x
269 2 over set-test-struct-11-y
273 C-STRUCT: test-struct-12 { "int" "a" } { "double" "x" } ;
276 "test-struct-12" <c-object>
277 [ set-test-struct-12-x ] keep ;
279 FUNCTION: double ffi_test_36 ( test-struct-12 x ) ;
281 [ 1.23456 ] [ 1.23456 make-struct-12 ffi_test_36 ] unit-test
283 FUNCTION: ulonglong ffi_test_38 ( ulonglong x, ulonglong y ) ;
285 [ t ] [ 31 2^ 32 2^ ffi_test_38 63 2^ = ] unit-test
289 : callback-1 ( -- callback ) "void" { } "cdecl" [ ] alien-callback ;
291 [ 0 1 ] [ [ callback-1 ] infer dup effect-in swap effect-out ] unit-test
293 [ t ] [ callback-1 alien? ] unit-test
295 : callback_test_1 ( ptr -- ) "void" { } "cdecl" alien-indirect ;
297 [ ] [ callback-1 callback_test_1 ] unit-test
299 : callback-2 ( -- callback ) "void" { } "cdecl" [ [ 5 throw ] ignore-errors ] alien-callback ;
301 [ ] [ callback-2 callback_test_1 ] unit-test
303 : callback-3 ( -- callback ) "void" { } "cdecl" [ 5 "x" set ] alien-callback ;
307 3 "x" set callback-3 callback_test_1
313 3 "x" set callback-3 callback_test_1 "x" get
317 : callback-4 ( -- callback )
318 "void" { } "cdecl" [ "Hello world" write ] alien-callback
322 [ callback-4 callback_test_1 ] with-string-writer
325 : callback-5 ( -- callback )
326 "void" { } "cdecl" [ gc ] alien-callback ;
329 "testing" callback-5 callback_test_1
332 : callback-5a ( -- callback )
333 "void" { } "cdecl" [ 8000000 f <array> drop ] alien-callback ;
335 ! Hack; if we're on ARM, we probably don't have much RAM, so
339 ! "testing" callback-5a callback_test_1
343 : callback-6 ( -- callback )
344 "void" { } "cdecl" [ [ continue ] callcc0 ] alien-callback ;
346 [ 1 2 3 ] [ callback-6 callback_test_1 1 2 3 ] unit-test
348 : callback-7 ( -- callback )
349 "void" { } "cdecl" [ 1000 sleep ] alien-callback ;
351 [ 1 2 3 ] [ callback-7 callback_test_1 1 2 3 ] unit-test
353 [ f ] [ namespace global eq? ] unit-test
355 : callback-8 ( -- callback )
360 [ ] [ callback-8 callback_test_1 ] unit-test
362 : callback-9 ( -- callback )
363 "int" { "int" "int" "int" } "cdecl" [
367 FUNCTION: void ffi_test_36_point_5 ( ) ;
369 [ ] [ ffi_test_36_point_5 ] unit-test
371 FUNCTION: int ffi_test_37 ( void* func ) ;
373 [ 1 ] [ callback-9 ffi_test_37 ] unit-test
375 [ 7 ] [ callback-9 ffi_test_37 ] unit-test