1 ! Copyright (C) 2004, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs kernel math namespaces sequences system
4 kernel.private byte-arrays arrays init ;
7 ! Some predicate classes used by the compiler for optimization
9 PREDICATE: simple-alien < alien underlying>> not ;
12 simple-alien POSTPONE: f byte-array ;
16 PREDICATE: pinned-alien < alien underlying>> pinned-c-ptr? ;
19 pinned-alien POSTPONE: f ;
21 GENERIC: expired? ( c-ptr -- ? ) flushable
23 M: alien expired? expired>> ;
25 M: f expired? drop t ;
27 : <alien> ( address -- alien )
28 f <displaced-alien> { simple-c-ptr } declare ; inline
30 : <bad-alien> ( -- alien )
31 -1 <alien> t >>expired ; inline
35 2dup [ expired? ] either? [
38 [ alien-address ] bi@ =
46 libraries global [ H{ } assoc-like ] change-at
48 TUPLE: library path abi dll ;
50 : library ( name -- library ) libraries get at ;
52 : <library> ( path abi -- library )
53 over dup [ dlopen ] when \ library boa ;
55 : load-library ( name -- dll )
56 library dup [ dll>> ] when ;
58 : add-library ( name path abi -- )
59 <library> swap libraries get set-at ;
61 ERROR: alien-callback-error ;
63 : alien-callback ( return parameters abi quot -- alien )
64 alien-callback-error ;
66 ERROR: alien-indirect-error ;
68 : alien-indirect ( ... funcptr return parameters abi -- )
69 alien-indirect-error ;
71 ERROR: alien-invoke-error library symbol ;
73 : alien-invoke ( ... return library function parameters -- ... )
74 2over alien-invoke-error ;
76 ! Callbacks are registered in a global hashtable. If you clear
77 ! this hashtable, they will all be blown away by code GC, beware.
80 [ H{ } clone callbacks set-global ] "alien" add-init-hook