]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/libraries/libraries.factor
Merge alien/parser/parser.factor
[factor.git] / basis / alien / libraries / libraries.factor
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 ;
5 IN: alien.libraries
6
7 : dlopen ( path -- dll ) native-string>alien (dlopen) ;
8
9 : dlsym ( name dll -- alien ) [ string>symbol ] dip (dlsym) ;
10
11 SYMBOL: libraries
12
13 libraries [ H{ } clone ] initialize
14
15 TUPLE: library path abi dll ;
16
17 ERROR: no-library name ;
18
19 : library ( name -- library ) libraries get at ;
20
21 : <library> ( path abi -- library )
22     over dup [ dlopen ] when \ library boa ;
23
24 : load-library ( name -- dll )
25     library dup [ dll>> ] when ;
26
27 M: dll dispose dlclose ;
28
29 M: library dispose dll>> [ dispose ] when* ;
30
31 : remove-library ( name -- )
32     libraries get delete-at* [ dispose ] [ drop ] if ;
33
34 : add-library ( name path abi -- )
35     [ 2drop remove-library ]
36     [ <library> swap libraries get set-at ] 3bi ;
37
38 : library-abi ( library -- abi )
39     library [ abi>> ] [ "cdecl" ] if* ;
40
41 SYMBOL: deploy-libraries
42
43 deploy-libraries [ V{ } clone ] initialize
44
45 : deploy-library ( name -- )
46     dup libraries get key?
47     [ deploy-libraries get 2dup member? [ 2drop ] [ push ] if ]
48     [ no-library ] if ;
49
50 <PRIVATE
51
52 HOOK: >deployed-library-path os ( path -- path' )
53
54 M: windows >deployed-library-path
55     file-name ;
56
57 M: unix >deployed-library-path
58     file-name "$ORIGIN" prepend-path ;
59
60 M: macosx >deployed-library-path
61     file-name "@executable_path/../Frameworks" prepend-path ;
62
63 PRIVATE>