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/f ) 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 GENERIC: library-dll ( obj -- dll )
43 M: library library-dll
46 M: string library-dll ( library -- dll )
47 lookup-library library-dll ;
49 : dlsym? ( function library -- alien/f )
52 M: dll dispose dlclose ;
54 M: library dispose dll>> [ dispose ] when* ;
56 : remove-library ( name -- )
57 libraries get delete-at* [ dispose ] [ drop ] if ;
59 : same-library? ( library path abi -- ? )
60 [ swap path>> = ] [ swap abi>> = ] bi-curry* bi and ;
62 : add-library? ( name path abi -- ? )
63 [ lookup-library ] 2dip '[ _ _ same-library? not ] [ t ] if* ;
65 : add-library ( name path abi -- )
67 [ 2drop remove-library ]
69 [ 2drop libraries get set-at ] 3tri
72 : change-dll ( library path abi -- )
77 [ swap >>dlerror swap >>dll drop ] tri ;
79 : update-library ( name path abi -- )
81 [ 2over same-library? not ] keep swap
82 [ change-dll drop ] [ 4drop ] if
84 make-library swap libraries get set-at
87 : library-abi ( library -- abi )
88 lookup-library [ abi>> ] [ cdecl ] if* ;
90 : address-of ( name library -- value )
91 2dup library-dll dlsym-raw
92 [ 2nip ] [ no-such-symbol ] if* ;
94 SYMBOL: deploy-libraries
96 deploy-libraries [ V{ } clone ] initialize
98 : deploy-library ( name -- )
99 dup libraries get key?
100 [ deploy-libraries get 2dup member? [ 2drop ] [ push ] if ]
101 [ "deploy-library failure" no-such-library ] if ;
103 HOOK: >deployed-library-path os ( path -- path' )
106 { [ os windows? ] [ "alien.libraries.windows" ] }
107 { [ os unix? ] [ "alien.libraries.unix" ] }