]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/libraries/libraries.factor
change ERROR: words from throw-foo back to foo.
[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 compiler.errors
4 io.backend kernel namespaces destructors sequences strings
5 system io.pathnames fry combinators vocabs ;
6 IN: alien.libraries
7
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 )
13
14 : dlopen ( path -- dll ) native-string>alien (dlopen) ;
15
16 : dlsym ( name dll -- alien ) [ string>symbol ] dip (dlsym) ;
17
18 : dlsym-raw ( name dll -- alien ) [ string>symbol ] dip (dlsym-raw) ;
19
20 HOOK: dlerror os ( -- message/f )
21
22 SYMBOL: libraries
23
24 libraries [ H{ } clone ] initialize
25
26 TUPLE: library { path string } dll dlerror { abi abi initial: cdecl } ;
27
28 C: <library> library
29
30 : lookup-library ( name -- library ) libraries get at ;
31
32 : open-dll ( path -- dll dll-error/f )
33     [ dlopen dup dll-valid? [ f ] [ dlerror ] if ]
34     [ f f ] if* ;
35
36 : make-library ( path abi -- library )
37     [ dup open-dll ] dip <library> ;
38
39 : library-dll ( library -- dll )
40     dup [ dll>> ] when ;
41
42 : load-library ( name -- dll )
43     lookup-library library-dll ;
44
45 M: dll dispose dlclose ;
46
47 M: library dispose dll>> [ dispose ] when* ;
48
49 : remove-library ( name -- )
50     libraries get delete-at* [ dispose ] [ drop ] if ;
51
52 : same-library? ( library path abi -- ? )
53     [ swap path>> = ] [ swap abi>> = ] bi-curry* bi and ;
54
55 : add-library? ( name path abi -- ? )
56     [ lookup-library ] 2dip '[ _ _ same-library? not ] [ t ] if* ;
57
58 : add-library ( name path abi -- )
59     3dup add-library? [
60         [ 2drop remove-library ]
61         [ [ nip ] dip make-library ]
62         [ 2drop libraries get set-at ] 3tri
63     ] [ 3drop ] if ;
64
65 : change-dll ( library path abi -- )
66     swap >>abi
67     swap >>path
68     [ dispose ]
69     [ path>> open-dll ]
70     [ swap >>dlerror swap >>dll drop ] tri ;
71
72 : update-library ( name path abi -- )
73     pick lookup-library [
74         [ 2over same-library? not ] keep swap
75         [ change-dll drop ] [ 4drop ] if
76     ] [ add-library ] if* ;
77
78 : library-abi ( library -- abi )
79     lookup-library [ abi>> ] [ cdecl ] if* ;
80
81 : address-of ( name library -- value )
82     2dup load-library dlsym-raw
83     [ 2nip ] [ no-such-symbol ] if* ;
84
85 SYMBOL: deploy-libraries
86
87 deploy-libraries [ V{ } clone ] initialize
88
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 ;
93
94 HOOK: >deployed-library-path os ( path -- path' )
95
96 {
97     { [ os windows? ] [ "alien.libraries.windows" ] }
98     { [ os unix? ] [ "alien.libraries.unix" ] }
99 } cond require