1 USING: accessors alien alien.c-types alien.libraries
2 alien.syntax arrays classes.struct combinators
3 compiler continuations effects io io.backend io.pathnames
4 io.streams.string kernel math memory namespaces
5 namespaces.private parser quotations sequences
6 specialized-arrays.float stack-checker stack-checker.errors
7 system threads tools.test words specialized-arrays.char ;
8 IN: compiler.tests.alien
11 : libfactor-ffi-tests-path ( -- string )
12 "resource:" (normalize-path)
14 { [ os winnt? ] [ "libfactor-ffi-test.dll" ] }
15 { [ os macosx? ] [ "libfactor-ffi-test.dylib" ] }
16 { [ os unix? ] [ "libfactor-ffi-test.so" ] }
19 "f-cdecl" libfactor-ffi-tests-path "cdecl" add-library
21 "f-stdcall" libfactor-ffi-tests-path "stdcall" add-library
26 FUNCTION: void ffi_test_0 ;
27 [ ] [ ffi_test_0 ] unit-test
29 FUNCTION: int ffi_test_1 ;
30 [ 3 ] [ ffi_test_1 ] unit-test
32 FUNCTION: int ffi_test_2 int x int y ;
33 [ 5 ] [ 2 3 ffi_test_2 ] unit-test
34 [ "hi" 3 ffi_test_2 ] must-fail
36 FUNCTION: int ffi_test_3 int x int y int z int t ;
37 [ 25 ] [ 2 3 4 5 ffi_test_3 ] unit-test
39 FUNCTION: float ffi_test_4 ;
40 [ 1.5 ] [ ffi_test_4 ] unit-test
42 FUNCTION: double ffi_test_5 ;
43 [ 1.5 ] [ ffi_test_5 ] unit-test
45 FUNCTION: int ffi_test_9 int a int b int c int d int e int f int g ;
46 [ 28 ] [ 1 2 3 4 5 6 7 ffi_test_9 ] unit-test
47 [ "a" 2 3 4 5 6 7 ffi_test_9 ] must-fail
48 [ 1 2 3 4 5 6 "a" ffi_test_9 ] must-fail
50 STRUCT: FOO { x int } { y int } ;
52 : make-FOO ( x y -- FOO )
53 FOO <struct> swap >>y swap >>x ;
55 FUNCTION: int ffi_test_11 int a FOO b int c ;
57 [ 14 ] [ 1 2 3 make-FOO 4 ffi_test_11 ] unit-test
59 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 ;
61 [ 66 ] [ 1 2 3 4 5 6 7 8 9 10 11 ffi_test_13 ] unit-test
63 FUNCTION: FOO ffi_test_14 int x int y ;
65 [ 11 6 ] [ 11 6 ffi_test_14 [ x>> ] [ y>> ] bi ] unit-test
67 FUNCTION: char* ffi_test_15 char* x char* y ;
69 [ "foo" ] [ "xy" "zt" ffi_test_15 ] unit-test
70 [ "bar" ] [ "xy" "xy" ffi_test_15 ] unit-test
71 [ 1 2 ffi_test_15 ] must-fail
73 STRUCT: BAR { x long } { y long } { z long } ;
75 FUNCTION: BAR ffi_test_16 long x long y long z ;
78 11 6 -7 ffi_test_16 [ x>> ] [ y>> ] [ z>> ] tri
81 STRUCT: TINY { x int } ;
83 FUNCTION: TINY ffi_test_17 int x ;
85 [ 11 ] [ 11 ffi_test_17 x>> ] unit-test
87 [ [ alien-indirect ] infer ] [ inference-error? ] must-fail-with
89 : indirect-test-1 ( ptr -- result )
90 "int" { } "cdecl" alien-indirect ;
92 { 1 1 } [ indirect-test-1 ] must-infer-as
94 [ 3 ] [ &: ffi_test_1 indirect-test-1 ] unit-test
96 : indirect-test-1' ( ptr -- )
97 "int" { } "cdecl" alien-indirect drop ;
99 { 1 0 } [ indirect-test-1' ] must-infer-as
101 [ ] [ &: ffi_test_1 indirect-test-1' ] unit-test
103 [ -1 indirect-test-1 ] must-fail
105 : indirect-test-2 ( x y ptr -- result )
106 "int" { "int" "int" } "cdecl" alien-indirect gc ;
108 { 3 1 } [ indirect-test-2 ] must-infer-as
111 [ 2 3 &: ffi_test_2 indirect-test-2 ]
114 : indirect-test-3 ( a b c d ptr -- result )
115 "int" { "int" "int" "int" "int" } "stdcall" alien-indirect
118 [ f ] [ "f-stdcall" load-library f = ] unit-test
119 [ "stdcall" ] [ "f-stdcall" library abi>> ] unit-test
121 : ffi_test_18 ( w x y z -- int )
122 "int" "f-stdcall" "ffi_test_18" { "int" "int" "int" "int" }
125 [ 25 ] [ 2 3 4 5 ffi_test_18 ] unit-test
127 : ffi_test_19 ( x y z -- BAR )
128 "BAR" "f-stdcall" "ffi_test_19" { "long" "long" "long" }
132 11 6 -7 ffi_test_19 [ x>> ] [ y>> ] [ z>> ] tri
135 FUNCTION: double ffi_test_6 float x float y ;
136 [ 6.0 ] [ 3.0 2.0 ffi_test_6 ] unit-test
137 [ "a" "b" ffi_test_6 ] must-fail
139 FUNCTION: double ffi_test_7 double x double y ;
140 [ 6.0 ] [ 3.0 2.0 ffi_test_7 ] unit-test
142 FUNCTION: double ffi_test_8 double x float y double z float t int w ;
143 [ 19.0 ] [ 3.0 2.0 1.0 6.0 7 ffi_test_8 ] unit-test
145 FUNCTION: int ffi_test_10 int a int b double c int d float e int f int g int h ;
146 [ -34 ] [ 1 2 3.0 4 5.0 6 7 8 ffi_test_10 ] unit-test
148 FUNCTION: void ffi_test_20 double x1, double x2, double x3,
149 double y1, double y2, double y3,
150 double z1, double z2, double z3 ;
152 [ ] [ 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 ffi_test_20 ] unit-test
154 ! Make sure XT doesn't get clobbered in stack frame
156 : 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 -- result y )
158 "f-cdecl" "ffi_test_31"
159 { "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" }
162 [ 861 3 ] [ 42 [ ] each ffi_test_31 ] unit-test
164 : ffi_test_31_point_5 ( 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 -- result )
166 "f-cdecl" "ffi_test_31_point_5"
167 { "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" }
170 [ 861.0 ] [ 42 [ >float ] each ffi_test_31_point_5 ] unit-test
172 FUNCTION: longlong ffi_test_21 long x long y ;
174 [ 121932631112635269 ]
175 [ 123456789 987654321 ffi_test_21 ] unit-test
177 FUNCTION: long ffi_test_22 long x longlong y longlong z ;
180 [ 1111 121932631112635269 123456789 ffi_test_22 ] unit-test
182 [ 1111 f 123456789 ffi_test_22 ] must-fail
185 { x float } { y float }
186 { w float } { h float } ;
188 : <RECT> ( x y w h -- rect )
195 FUNCTION: int ffi_test_12 int a int b RECT c int d int e int f ;
197 [ 45 ] [ 1 2 3.0 4.0 5.0 6.0 <RECT> 7 8 9 ffi_test_12 ] unit-test
199 [ 1 2 { 1 2 3 } 7 8 9 ffi_test_12 ] must-fail
201 FUNCTION: float ffi_test_23 ( float[3] x, float[3] y ) ;
204 { 1.0 2.0 3.0 } >float-array
205 { 4.0 5.0 6.0 } >float-array
209 ! Test odd-size structs
210 STRUCT: test-struct-1 { x char[1] } ;
212 FUNCTION: test-struct-1 ffi_test_24 ;
214 [ S{ test-struct-1 { x char-array{ 1 } } } ] [ ffi_test_24 ] unit-test
216 STRUCT: test-struct-2 { x char[2] } ;
218 FUNCTION: test-struct-2 ffi_test_25 ;
220 [ S{ test-struct-2 { x char-array{ 1 2 } } } ] [ ffi_test_25 ] unit-test
222 STRUCT: test-struct-3 { x char[3] } ;
224 FUNCTION: test-struct-3 ffi_test_26 ;
226 [ S{ test-struct-3 { x char-array{ 1 2 3 } } } ] [ ffi_test_26 ] unit-test
228 STRUCT: test-struct-4 { x char[4] } ;
230 FUNCTION: test-struct-4 ffi_test_27 ;
232 [ S{ test-struct-4 { x char-array{ 1 2 3 4 } } } ] [ ffi_test_27 ] unit-test
234 STRUCT: test-struct-5 { x char[5] } ;
236 FUNCTION: test-struct-5 ffi_test_28 ;
238 [ S{ test-struct-5 { x char-array{ 1 2 3 4 5 } } } ] [ ffi_test_28 ] unit-test
240 STRUCT: test-struct-6 { x char[6] } ;
242 FUNCTION: test-struct-6 ffi_test_29 ;
244 [ S{ test-struct-6 { x char-array{ 1 2 3 4 5 6 } } } ] [ ffi_test_29 ] unit-test
246 STRUCT: test-struct-7 { x char[7] } ;
248 FUNCTION: test-struct-7 ffi_test_30 ;
250 [ S{ test-struct-7 { x char-array{ 1 2 3 4 5 6 7 } } } ] [ ffi_test_30 ] unit-test
252 STRUCT: test-struct-8 { x double } { y double } ;
254 FUNCTION: double ffi_test_32 test-struct-8 x int y ;
257 test-struct-8 <struct>
263 STRUCT: test-struct-9 { x float } { y float } ;
265 FUNCTION: double ffi_test_33 test-struct-9 x int y ;
268 test-struct-9 <struct>
274 STRUCT: test-struct-10 { x float } { y int } ;
276 FUNCTION: double ffi_test_34 test-struct-10 x int y ;
279 test-struct-10 <struct>
285 STRUCT: test-struct-11 { x int } { y int } ;
287 FUNCTION: double ffi_test_35 test-struct-11 x int y ;
290 test-struct-11 <struct>
296 STRUCT: test-struct-12 { a int } { x double } ;
298 : make-struct-12 ( x -- alien )
299 test-struct-12 <struct>
302 FUNCTION: double ffi_test_36 ( test-struct-12 x ) ;
304 [ 1.23456 ] [ 1.23456 make-struct-12 ffi_test_36 ] unit-test
306 FUNCTION: ulonglong ffi_test_38 ( ulonglong x, ulonglong y ) ;
308 [ t ] [ 31 2^ 32 2^ ffi_test_38 63 2^ = ] unit-test
312 : callback-1 ( -- callback ) "void" { } "cdecl" [ ] alien-callback ;
314 [ 0 1 ] [ [ callback-1 ] infer [ in>> ] [ out>> ] bi ] unit-test
316 [ t ] [ callback-1 alien? ] unit-test
318 : callback_test_1 ( ptr -- ) "void" { } "cdecl" alien-indirect ;
320 [ ] [ callback-1 callback_test_1 ] unit-test
322 : callback-2 ( -- callback ) "void" { } "cdecl" [ [ 5 throw ] ignore-errors ] alien-callback ;
324 [ ] [ callback-2 callback_test_1 ] unit-test
326 : callback-3 ( -- callback ) "void" { } "cdecl" [ 5 "x" set ] alien-callback ;
330 3 "x" set callback-3 callback_test_1
336 3 "x" set callback-3 callback_test_1 "x" get
340 : callback-4 ( -- callback )
341 "void" { } "cdecl" [ "Hello world" write ] alien-callback
345 [ callback-4 callback_test_1 ] with-string-writer
348 : callback-5 ( -- callback )
349 "void" { } "cdecl" [ gc ] alien-callback ;
352 "testing" callback-5 callback_test_1
355 : callback-5a ( -- callback )
356 "void" { } "cdecl" [ 8000000 f <array> drop ] alien-callback ;
358 ! Hack; if we're on ARM, we probably don't have much RAM, so
362 ! "testing" callback-5a callback_test_1
366 : callback-6 ( -- callback )
367 "void" { } "cdecl" [ [ continue ] callcc0 ] alien-callback ;
369 [ 1 2 3 ] [ callback-6 callback_test_1 1 2 3 ] unit-test
371 : callback-7 ( -- callback )
372 "void" { } "cdecl" [ 1000000 sleep ] alien-callback ;
374 [ 1 2 3 ] [ callback-7 callback_test_1 1 2 3 ] unit-test
376 [ f ] [ namespace global eq? ] unit-test
378 : callback-8 ( -- callback )
383 [ ] [ callback-8 callback_test_1 ] unit-test
385 : callback-9 ( -- callback )
386 "int" { "int" "int" "int" } "cdecl" [
390 FUNCTION: void ffi_test_36_point_5 ( ) ;
392 [ ] [ ffi_test_36_point_5 ] unit-test
394 FUNCTION: int ffi_test_37 ( void* func ) ;
396 [ 1 ] [ callback-9 ffi_test_37 ] unit-test
398 [ 7 ] [ callback-9 ffi_test_37 ] unit-test
400 STRUCT: test_struct_13
408 : make-test-struct-13 ( -- alien )
409 test_struct_13 <struct>
417 FUNCTION: int ffi_test_39 ( long a, long b, test_struct_13 s ) ;
419 [ 21 ] [ 12347 12347 make-test-struct-13 ffi_test_39 ] unit-test
421 ! Joe Groff found this problem
428 : <double-rect> ( a b c d -- foo )
435 : >double-rect< ( foo -- a b c d )
443 : double-rect-callback ( -- alien )
444 "void" { "void*" "void*" "double-rect" } "cdecl"
445 [ "example" set-global 2drop ] alien-callback ;
447 : double-rect-test ( arg -- arg' )
450 "void" { "void*" "void*" "double-rect" } "cdecl" alien-indirect
451 "example" get-global ;
454 [ 1.0 2.0 3.0 4.0 <double-rect> double-rect-test >double-rect< ] unit-test
456 STRUCT: test_struct_14
460 FUNCTION: test_struct_14 ffi_test_40 ( double x1, double x2 ) ;
463 1.0 2.0 ffi_test_40 [ x1>> ] [ x2>> ] bi
466 : callback-10 ( -- callback )
467 "test_struct_14" { "double" "double" } "cdecl"
469 test_struct_14 <struct>
474 : callback-10-test ( x1 x2 callback -- result )
475 "test_struct_14" { "double" "double" } "cdecl" alien-indirect ;
478 1.0 2.0 callback-10 callback-10-test
482 FUNCTION: test-struct-12 ffi_test_41 ( int a, double x ) ;
489 : callback-11 ( -- callback )
490 "test-struct-12" { "int" "double" } "cdecl"
492 test-struct-12 <struct>
497 : callback-11-test ( x1 x2 callback -- result )
498 "test-struct-12" { "int" "double" } "cdecl" alien-indirect ;
501 1 2.0 callback-11 callback-11-test
505 STRUCT: test_struct_15
509 FUNCTION: test_struct_15 ffi_test_42 ( float x, float y ) ;
511 [ 1.0 2.0 ] [ 1.0 2.0 ffi_test_42 [ x>> ] [ y>> ] bi ] unit-test
513 : callback-12 ( -- callback )
514 "test_struct_15" { "float" "float" } "cdecl"
516 test_struct_15 <struct>
521 : callback-12-test ( x1 x2 callback -- result )
522 "test_struct_15" { "float" "float" } "cdecl" alien-indirect ;
525 1.0 2.0 callback-12 callback-12-test [ x>> ] [ y>> ] bi
528 STRUCT: test_struct_16
532 FUNCTION: test_struct_16 ffi_test_43 ( float x, int a ) ;
534 [ 1.0 2 ] [ 1.0 2 ffi_test_43 [ x>> ] [ a>> ] bi ] unit-test
536 : callback-13 ( -- callback )
537 "test_struct_16" { "float" "int" } "cdecl"
539 test_struct_16 <struct>
544 : callback-13-test ( x1 x2 callback -- result )
545 "test_struct_16" { "float" "int" } "cdecl" alien-indirect ;
548 1.0 2 callback-13 callback-13-test
552 FUNCTION: test_struct_14 ffi_test_44 ( ) ; inline
554 [ 1.0 2.0 ] [ ffi_test_44 [ x1>> ] [ x2>> ] bi ] unit-test
556 : stack-frame-bustage ( -- a b ) ffi_test_44 gc 3 ;
558 [ ] [ stack-frame-bustage 2drop ] unit-test
560 FUNCTION: complex-float ffi_test_45 ( int x ) ;
562 [ C{ 3.0 0.0 } ] [ 3 ffi_test_45 ] unit-test
564 FUNCTION: complex-double ffi_test_46 ( int x ) ;
566 [ C{ 3.0 0.0 } ] [ 3 ffi_test_46 ] unit-test
568 FUNCTION: complex-float ffi_test_47 ( complex-float x, complex-double y ) ;
572 C{ 1.5 1.0 } ffi_test_47
576 STRUCT: bool-field-test
581 FUNCTION: short ffi_test_48 ( bool-field-test x ) ;
584 bool-field-test <struct>