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 system io.pathnames ;
7 : dlopen ( path -- dll ) native-string>alien (dlopen) ;
9 : dlsym ( name dll -- alien ) [ string>symbol ] dip (dlsym) ;
13 libraries [ H{ } clone ] initialize
15 TUPLE: library path abi dll ;
17 ERROR: no-library name ;
19 : library ( name -- library ) libraries get at ;
21 : <library> ( path abi -- library )
22 over dup [ dlopen ] when \ library boa ;
24 : load-library ( name -- dll )
25 library dup [ dll>> ] when ;
27 M: dll dispose dlclose ;
29 M: library dispose dll>> [ dispose ] when* ;
31 : remove-library ( name -- )
32 libraries get delete-at* [ dispose ] [ drop ] if ;
34 : add-library ( name path abi -- )
35 [ 2drop remove-library ]
36 [ <library> swap libraries get set-at ] 3bi ;
38 : library-abi ( library -- abi )
39 library [ abi>> ] [ "cdecl" ] if* ;
41 SYMBOL: deploy-libraries
43 deploy-libraries [ V{ } clone ] initialize
45 : deploy-library ( name -- )
46 dup libraries get key?
47 [ deploy-libraries get 2dup member? [ 2drop ] [ push ] if ]
52 HOOK: >deployed-library-path os ( path -- path' )
54 M: windows >deployed-library-path
57 M: unix >deployed-library-path
58 file-name "$ORIGIN" prepend-path ;
60 M: macosx >deployed-library-path
61 file-name "@executable_path/../Frameworks" prepend-path ;