1 ! Copyright (C) 2009, 2010 Slava Pestov, Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.strings assocs io.backend
4 kernel namespaces destructors sequences strings
5 system io.pathnames fry combinators vocabs ;
8 : dlopen ( path -- dll ) native-string>alien (dlopen) ;
10 : dlsym ( name dll -- alien ) [ string>symbol ] dip (dlsym) ;
12 : dlsym-raw ( name dll -- alien ) [ string>symbol ] dip (dlsym-raw) ;
14 HOOK: dlerror os ( -- message/f )
18 libraries [ H{ } clone ] initialize
20 TUPLE: library { path string } { abi abi initial: cdecl } dll dlerror ;
22 ERROR: no-library name ;
24 : library ( name -- library ) libraries get at ;
26 : <library> ( path abi -- library )
28 [ dlopen dup dll-valid? [ f ] [ dlerror ] if ] [ f ] if
31 : library-dll ( library -- dll )
34 : load-library ( name -- dll )
37 M: dll dispose dlclose ;
39 M: library dispose dll>> [ dispose ] when* ;
41 : remove-library ( name -- )
42 libraries get delete-at* [ dispose ] [ drop ] if ;
44 : add-library? ( name path abi -- ? )
46 '[ [ path>> _ = ] [ abi>> _ = ] bi and not ] [ t ] if* ;
48 : add-library ( name path abi -- )
50 [ 2drop remove-library ]
51 [ <library> swap libraries get set-at ] 3bi
54 : library-abi ( library -- abi )
55 library [ abi>> ] [ cdecl ] if* ;
57 ERROR: no-such-symbol name library ;
59 : address-of ( name library -- value )
60 2dup load-library dlsym-raw [ 2nip ] [ no-such-symbol ] if* ;
62 SYMBOL: deploy-libraries
64 deploy-libraries [ V{ } clone ] initialize
66 : deploy-library ( name -- )
67 dup libraries get key?
68 [ deploy-libraries get 2dup member? [ 2drop ] [ push ] if ]
71 HOOK: >deployed-library-path os ( path -- path' )
74 { [ os windows? ] [ "alien.libraries.windows" ] }
75 { [ os unix? ] [ "alien.libraries.unix" ] }