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 compiler.errors
4 io.backend kernel namespaces destructors sequences strings
5 system io.pathnames fry combinators vocabs ;
8 PRIMITIVE: dll-valid? ( dll -- ? )
9 PRIMITIVE: (dlopen) ( path -- dll )
10 PRIMITIVE: (dlsym) ( name dll -- alien )
11 PRIMITIVE: dlclose ( dll -- )
12 PRIMITIVE: (dlsym-raw) ( name dll -- alien )
14 : dlopen ( path -- dll ) native-string>alien (dlopen) ;
16 : dlsym ( name dll -- alien ) [ string>symbol ] dip (dlsym) ;
18 : dlsym-raw ( name dll -- alien ) [ string>symbol ] dip (dlsym-raw) ;
20 HOOK: dlerror os ( -- message/f )
24 libraries [ H{ } clone ] initialize
26 TUPLE: library { path string } dll dlerror { abi abi initial: cdecl } ;
30 : lookup-library ( name -- library ) libraries get at ;
32 : open-dll ( path -- dll dll-error/f )
33 [ dlopen dup dll-valid? [ f ] [ dlerror ] if ]
36 : make-library ( path abi -- library )
37 [ dup open-dll ] dip <library> ;
39 : library-dll ( library -- dll )
42 : load-library ( name -- dll )
43 lookup-library library-dll ;
45 M: dll dispose dlclose ;
47 M: library dispose dll>> [ dispose ] when* ;
49 : remove-library ( name -- )
50 libraries get delete-at* [ dispose ] [ drop ] if ;
52 : same-library? ( library path abi -- ? )
53 [ swap path>> = ] [ swap abi>> = ] bi-curry* bi and ;
55 : add-library? ( name path abi -- ? )
56 [ lookup-library ] 2dip '[ _ _ same-library? not ] [ t ] if* ;
58 : add-library ( name path abi -- )
60 [ 2drop remove-library ]
61 [ [ nip ] dip make-library ]
62 [ 2drop libraries get set-at ] 3tri
65 : change-dll ( library path abi -- )
70 [ swap >>dlerror swap >>dll drop ] tri ;
72 : update-library ( name path abi -- )
74 [ 2over same-library? not ] keep swap
75 [ change-dll drop ] [ 4drop ] if
76 ] [ add-library ] if* ;
78 : library-abi ( library -- abi )
79 lookup-library [ abi>> ] [ cdecl ] if* ;
81 : address-of ( name library -- value )
82 2dup load-library dlsym-raw
83 [ 2nip ] [ no-such-symbol ] if* ;
85 SYMBOL: deploy-libraries
87 deploy-libraries [ V{ } clone ] initialize
89 : deploy-library ( name -- )
90 dup libraries get key?
91 [ deploy-libraries get 2dup member? [ 2drop ] [ push ] if ]
92 [ "deploy-library failure" no-such-library ] if ;
94 HOOK: >deployed-library-path os ( path -- path' )
97 { [ os windows? ] [ "alien.libraries.windows" ] }
98 { [ os unix? ] [ "alien.libraries.unix" ] }