1 ! Copyright (C) 2009 Slava Pestov.
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) ;
12 SYMBOL: deploy-libraries
14 libraries [ H{ } clone ] initialize
15 deploy-libraries [ V{ } clone ] initialize
17 TUPLE: library path abi dll ;
19 ERROR: no-library name ;
21 : library ( name -- library ) libraries get at ;
23 : <library> ( path abi -- library )
24 over dup [ dlopen ] when \ library boa ;
26 : load-library ( name -- dll )
27 library dup [ dll>> ] when ;
29 M: dll dispose dlclose ;
31 M: library dispose dll>> [ dispose ] when* ;
33 : remove-library ( name -- )
34 libraries get delete-at* [ dispose ] [ drop ] if ;
36 : add-library ( name path abi -- )
37 [ 2drop remove-library ]
38 [ <library> swap libraries get set-at ] 3bi ;
40 : deploy-library ( name -- )
41 dup libraries get key?
42 [ deploy-libraries get 2dup member? [ 2drop ] [ push ] if ]
46 HOOK: >deployed-library-path os ( path -- path' )
48 M: windows >deployed-library-path
50 M: unix >deployed-library-path
51 file-name "$ORIGIN" prepend-path ;
52 M: macosx >deployed-library-path
53 file-name "@executable_path/../Frameworks" prepend-path ;