2 USING: alien alien.c-types alien.syntax compiler kernel
3 namespaces namespaces tools.test sequences stack-checker
4 stack-checker.errors words arrays parser quotations
5 continuations effects namespaces.private io io.streams.string
6 memory system threads tools.test math accessors combinators ;
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 : indirect-test-1' ( ptr -- )
88 "int" { } "cdecl" alien-indirect drop ;
90 { 1 0 } [ indirect-test-1' ] must-infer-as
92 [ ] [ "ffi_test_1" f dlsym indirect-test-1' ] unit-test
94 [ -1 indirect-test-1 ] must-fail
96 : indirect-test-2 ( x y ptr -- result )
97 "int" { "int" "int" } "cdecl" alien-indirect gc ;
99 { 3 1 } [ indirect-test-2 ] must-infer-as
102 [ 2 3 "ffi_test_2" f dlsym indirect-test-2 ]
105 : indirect-test-3 ( a b c d ptr -- result )
106 "int" { "int" "int" "int" "int" } "stdcall" alien-indirect
109 << "f-stdcall" f "stdcall" add-library >>
111 [ f ] [ "f-stdcall" load-library ] unit-test
112 [ "stdcall" ] [ "f-stdcall" library abi>> ] unit-test
114 : ffi_test_18 ( w x y z -- int )
115 "int" "f-stdcall" "ffi_test_18" { "int" "int" "int" "int" }
118 [ 25 ] [ 2 3 4 5 ffi_test_18 ] unit-test
120 : ffi_test_19 ( x y z -- bar )
121 "bar" "f-stdcall" "ffi_test_19" { "long" "long" "long" }
125 11 6 -7 ffi_test_19 dup bar-x over bar-y rot bar-z
128 FUNCTION: double ffi_test_6 float x float y ;
129 [ 6.0 ] [ 3.0 2.0 ffi_test_6 ] unit-test
130 [ "a" "b" ffi_test_6 ] must-fail
132 FUNCTION: double ffi_test_7 double x double y ;
133 [ 6.0 ] [ 3.0 2.0 ffi_test_7 ] unit-test
135 FUNCTION: double ffi_test_8 double x float y double z float t int w ;
136 [ 19.0 ] [ 3.0 2.0 1.0 6.0 7 ffi_test_8 ] unit-test
138 FUNCTION: int ffi_test_10 int a int b double c int d float e int f int g int h ;
139 [ -34 ] [ 1 2 3.0 4 5.0 6 7 8 ffi_test_10 ] unit-test
141 FUNCTION: void ffi_test_20 double x1, double x2, double x3,
142 double y1, double y2, double y3,
143 double z1, double z2, double z3 ;
145 [ ] [ 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 ffi_test_20 ] unit-test
147 ! Make sure XT doesn't get clobbered in stack frame
149 : 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 )
152 { "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" }
155 [ 3 ] [ 42 [ ] each ffi_test_31 ] unit-test
157 FUNCTION: longlong ffi_test_21 long x long y ;
159 [ 121932631112635269 ]
160 [ 123456789 987654321 ffi_test_21 ] unit-test
162 FUNCTION: long ffi_test_22 long x longlong y longlong z ;
165 [ 1111 121932631112635269 123456789 ffi_test_22 ] unit-test
167 [ 1111 f 123456789 ffi_test_22 ] must-fail
181 [ set-rect-x ] keep ;
183 FUNCTION: int ffi_test_12 int a int b rect c int d int e int f ;
185 [ 45 ] [ 1 2 3.0 4.0 5.0 6.0 <rect> 7 8 9 ffi_test_12 ] unit-test
187 [ 1 2 { 1 2 3 } 7 8 9 ffi_test_12 ] must-fail
189 FUNCTION: float ffi_test_23 ( float[3] x, float[3] y ) ;
191 [ 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
193 ! Test odd-size structs
194 C-STRUCT: test-struct-1 { { "char" 1 } "x" } ;
196 FUNCTION: test-struct-1 ffi_test_24 ;
198 [ B{ 1 } ] [ ffi_test_24 ] unit-test
200 C-STRUCT: test-struct-2 { { "char" 2 } "x" } ;
202 FUNCTION: test-struct-2 ffi_test_25 ;
204 [ B{ 1 2 } ] [ ffi_test_25 ] unit-test
206 C-STRUCT: test-struct-3 { { "char" 3 } "x" } ;
208 FUNCTION: test-struct-3 ffi_test_26 ;
210 [ B{ 1 2 3 } ] [ ffi_test_26 ] unit-test
212 C-STRUCT: test-struct-4 { { "char" 4 } "x" } ;
214 FUNCTION: test-struct-4 ffi_test_27 ;
216 [ B{ 1 2 3 4 } ] [ ffi_test_27 ] unit-test
218 C-STRUCT: test-struct-5 { { "char" 5 } "x" } ;
220 FUNCTION: test-struct-5 ffi_test_28 ;
222 [ B{ 1 2 3 4 5 } ] [ ffi_test_28 ] unit-test
224 C-STRUCT: test-struct-6 { { "char" 6 } "x" } ;
226 FUNCTION: test-struct-6 ffi_test_29 ;
228 [ B{ 1 2 3 4 5 6 } ] [ ffi_test_29 ] unit-test
230 C-STRUCT: test-struct-7 { { "char" 7 } "x" } ;
232 FUNCTION: test-struct-7 ffi_test_30 ;
234 [ B{ 1 2 3 4 5 6 7 } ] [ ffi_test_30 ] unit-test
236 C-STRUCT: test-struct-8 { "double" "x" } { "double" "y" } ;
238 FUNCTION: double ffi_test_32 test-struct-8 x int y ;
241 "test-struct-8" <c-object>
242 1.0 over set-test-struct-8-x
243 2.0 over set-test-struct-8-y
247 C-STRUCT: test-struct-9 { "float" "x" } { "float" "y" } ;
249 FUNCTION: double ffi_test_33 test-struct-9 x int y ;
252 "test-struct-9" <c-object>
253 1.0 over set-test-struct-9-x
254 2.0 over set-test-struct-9-y
258 C-STRUCT: test-struct-10 { "float" "x" } { "int" "y" } ;
260 FUNCTION: double ffi_test_34 test-struct-10 x int y ;
263 "test-struct-10" <c-object>
264 1.0 over set-test-struct-10-x
265 2 over set-test-struct-10-y
269 C-STRUCT: test-struct-11 { "int" "x" } { "int" "y" } ;
271 FUNCTION: double ffi_test_35 test-struct-11 x int y ;
274 "test-struct-11" <c-object>
275 1 over set-test-struct-11-x
276 2 over set-test-struct-11-y
280 C-STRUCT: test-struct-12 { "int" "a" } { "double" "x" } ;
282 : make-struct-12 ( x -- alien )
283 "test-struct-12" <c-object>
284 [ set-test-struct-12-x ] keep ;
286 FUNCTION: double ffi_test_36 ( test-struct-12 x ) ;
288 [ 1.23456 ] [ 1.23456 make-struct-12 ffi_test_36 ] unit-test
290 FUNCTION: ulonglong ffi_test_38 ( ulonglong x, ulonglong y ) ;
292 [ t ] [ 31 2^ 32 2^ ffi_test_38 63 2^ = ] unit-test
296 : callback-1 ( -- callback ) "void" { } "cdecl" [ ] alien-callback ;
298 [ 0 1 ] [ [ callback-1 ] infer [ in>> ] [ out>> ] bi ] unit-test
300 [ t ] [ callback-1 alien? ] unit-test
302 : callback_test_1 ( ptr -- ) "void" { } "cdecl" alien-indirect ;
304 [ ] [ callback-1 callback_test_1 ] unit-test
306 : callback-2 ( -- callback ) "void" { } "cdecl" [ [ 5 throw ] ignore-errors ] alien-callback ;
308 [ ] [ callback-2 callback_test_1 ] unit-test
310 : callback-3 ( -- callback ) "void" { } "cdecl" [ 5 "x" set ] alien-callback ;
314 3 "x" set callback-3 callback_test_1
320 3 "x" set callback-3 callback_test_1 "x" get
324 : callback-4 ( -- callback )
325 "void" { } "cdecl" [ "Hello world" write ] alien-callback
329 [ callback-4 callback_test_1 ] with-string-writer
332 : callback-5 ( -- callback )
333 "void" { } "cdecl" [ gc ] alien-callback ;
336 "testing" callback-5 callback_test_1
339 : callback-5a ( -- callback )
340 "void" { } "cdecl" [ 8000000 f <array> drop ] alien-callback ;
342 ! Hack; if we're on ARM, we probably don't have much RAM, so
346 ! "testing" callback-5a callback_test_1
350 : callback-6 ( -- callback )
351 "void" { } "cdecl" [ [ continue ] callcc0 ] alien-callback ;
353 [ 1 2 3 ] [ callback-6 callback_test_1 1 2 3 ] unit-test
355 : callback-7 ( -- callback )
356 "void" { } "cdecl" [ 1000 sleep ] alien-callback ;
358 [ 1 2 3 ] [ callback-7 callback_test_1 1 2 3 ] unit-test
360 [ f ] [ namespace global eq? ] unit-test
362 : callback-8 ( -- callback )
367 [ ] [ callback-8 callback_test_1 ] unit-test
369 : callback-9 ( -- callback )
370 "int" { "int" "int" "int" } "cdecl" [
374 FUNCTION: void ffi_test_36_point_5 ( ) ;
376 [ ] [ ffi_test_36_point_5 ] unit-test
378 FUNCTION: int ffi_test_37 ( void* func ) ;
380 [ 1 ] [ callback-9 ffi_test_37 ] unit-test
382 [ 7 ] [ callback-9 ffi_test_37 ] unit-test
384 C-STRUCT: test_struct_13
392 : make-test-struct-13 ( -- alien )
393 "test_struct_13" <c-object>
394 1.0 over set-test_struct_13-x1
395 2.0 over set-test_struct_13-x2
396 3.0 over set-test_struct_13-x3
397 4.0 over set-test_struct_13-x4
398 5.0 over set-test_struct_13-x5
399 6.0 over set-test_struct_13-x6 ;
401 FUNCTION: int ffi_test_39 ( long a, long b, test_struct_13 s ) ;
403 [ 21 ] [ 12347 12347 make-test-struct-13 ffi_test_39 ] unit-test
405 ! Joe Groff found this problem
406 C-STRUCT: double-rect
412 : <double-rect> ( a b c d -- foo )
413 "double-rect" <c-object>
415 [ set-double-rect-d ]
416 [ set-double-rect-c ]
417 [ set-double-rect-b ]
418 [ set-double-rect-a ]
422 : >double-rect< ( foo -- a b c d )
430 : double-rect-callback ( -- alien )
431 "void" { "void*" "void*" "double-rect" } "cdecl"
432 [ "example" set-global 2drop ] alien-callback ;
434 : double-rect-test ( arg -- arg' )
437 "void" { "void*" "void*" "double-rect" } "cdecl" alien-indirect
438 "example" get-global ;
441 [ 1.0 2.0 3.0 4.0 <double-rect> double-rect-test >double-rect< ] unit-test
443 C-STRUCT: test_struct_14
447 FUNCTION: test_struct_14 ffi_test_40 ( double x1, double x2 ) ;
451 [ test_struct_14-x1 ] [ test_struct_14-x2 ] bi
454 : callback-10 ( -- callback )
455 "test_struct_14" { "double" "double" } "cdecl"
457 "test_struct_14" <c-object>
458 [ set-test_struct_14-x2 ] keep
459 [ set-test_struct_14-x1 ] keep
462 : callback-10-test ( x1 x2 callback -- result )
463 "test_struct_14" { "double" "double" } "cdecl" alien-indirect ;
466 1.0 2.0 callback-10 callback-10-test
467 [ test_struct_14-x1 ] [ test_struct_14-x2 ] bi
470 FUNCTION: test-struct-12 ffi_test_41 ( int a, double x ) ;
474 [ test-struct-12-a ] [ test-struct-12-x ] bi
477 : callback-11 ( -- callback )
478 "test-struct-12" { "int" "double" } "cdecl"
480 "test-struct-12" <c-object>
481 [ set-test-struct-12-x ] keep
482 [ set-test-struct-12-a ] keep
485 : callback-11-test ( x1 x2 callback -- result )
486 "test-struct-12" { "int" "double" } "cdecl" alien-indirect ;
489 1 2.0 callback-11 callback-11-test
490 [ test-struct-12-a ] [ test-struct-12-x ] bi
493 C-STRUCT: test_struct_15
497 FUNCTION: test_struct_15 ffi_test_42 ( float x, float y ) ;
499 [ 1.0 2.0 ] [ 1.0 2.0 ffi_test_42 [ test_struct_15-x ] [ test_struct_15-y ] bi ] unit-test
501 : callback-12 ( -- callback )
502 "test_struct_15" { "float" "float" } "cdecl"
504 "test_struct_15" <c-object>
505 [ set-test_struct_15-y ] keep
506 [ set-test_struct_15-x ] keep
509 : callback-12-test ( x1 x2 callback -- result )
510 "test_struct_15" { "float" "float" } "cdecl" alien-indirect ;
513 1.0 2.0 callback-12 callback-12-test
514 [ test_struct_15-x ] [ test_struct_15-y ] bi
517 C-STRUCT: test_struct_16
521 FUNCTION: test_struct_16 ffi_test_43 ( float x, int a ) ;
523 [ 1.0 2 ] [ 1.0 2 ffi_test_43 [ test_struct_16-x ] [ test_struct_16-a ] bi ] unit-test
525 : callback-13 ( -- callback )
526 "test_struct_16" { "float" "int" } "cdecl"
528 "test_struct_16" <c-object>
529 [ set-test_struct_16-a ] keep
530 [ set-test_struct_16-x ] keep
533 : callback-13-test ( x1 x2 callback -- result )
534 "test_struct_16" { "float" "int" } "cdecl" alien-indirect ;
537 1.0 2 callback-13 callback-13-test
538 [ test_struct_16-x ] [ test_struct_16-a ] bi
541 FUNCTION: test_struct_14 ffi_test_44 ( ) ; inline
543 [ 1.0 2.0 ] [ ffi_test_44 [ test_struct_14-x1 ] [ test_struct_14-x2 ] bi ] unit-test
545 : stack-frame-bustage ( -- a b ) ffi_test_44 gc 3 ;
547 [ ] [ stack-frame-bustage 2drop ] unit-test