factor/basis/alien/libraries/libraries.factor

81 lines
2.1 KiB
Factor
Raw Normal View History

2010-02-22 07:28:56 -05:00
! Copyright (C) 2009, 2010 Slava Pestov, Joe Groff.
2009-03-26 00:00:02 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien alien.strings assocs io.backend
kernel namespaces destructors sequences strings
system io.pathnames fry combinators vocabs ;
2009-03-26 00:00:02 -04:00
IN: alien.libraries
: dlopen ( path -- dll ) native-string>alien (dlopen) ;
2009-05-05 17:00:31 -04:00
: dlsym ( name dll -- alien ) [ string>symbol ] dip (dlsym) ;
2011-05-20 18:11:50 -04:00
: dlsym-raw ( name dll -- alien ) [ string>symbol ] dip (dlsym-raw) ;
HOOK: dlerror os ( -- message/f )
2009-03-26 00:00:02 -04:00
SYMBOL: libraries
libraries [ H{ } clone ] initialize
TUPLE: library { path string } { abi abi initial: cdecl } dll dlerror ;
2009-03-26 00:00:02 -04:00
ERROR: no-library name ;
2009-03-26 00:00:02 -04:00
: library ( name -- library ) libraries get at ;
: <library> ( path abi -- library )
over dup
[ dlopen dup dll-valid? [ f ] [ dlerror ] if ] [ f ] if
\ library boa ;
: library-dll ( library -- dll )
dup [ dll>> ] when ;
2009-03-26 00:00:02 -04:00
: load-library ( name -- dll )
library library-dll ;
2009-03-26 00:00:02 -04:00
M: dll dispose dlclose ;
M: library dispose dll>> [ dispose ] when* ;
: remove-library ( name -- )
libraries get delete-at* [ dispose ] [ drop ] if ;
: add-library? ( name path abi -- ? )
[ library ] 2dip
'[ [ path>> _ = ] [ abi>> _ = ] bi and not ] [ t ] if* ;
: add-library ( name path abi -- )
3dup add-library? [
[ 2drop remove-library ]
[ <library> swap libraries get set-at ] 3bi
] [ 3drop ] if ;
2010-02-22 07:28:56 -05:00
: library-abi ( library -- abi )
2010-03-31 22:20:35 -04:00
library [ abi>> ] [ cdecl ] if* ;
2010-02-22 07:28:56 -05:00
ERROR: no-such-symbol name library ;
: (address-of) ( name -- value )
dup f dlsym-raw [ nip ] [ f no-such-symbol ] if* ; foldable
: address-of ( name library/f -- value )
[ 2dup load-library dlsym-raw [ 2nip ] [ no-such-symbol ] if* ]
[ (address-of) ] if* ; inline
2010-02-22 07:28:56 -05:00
SYMBOL: deploy-libraries
deploy-libraries [ V{ } clone ] initialize
: deploy-library ( name -- )
dup libraries get key?
[ deploy-libraries get 2dup member? [ 2drop ] [ push ] if ]
[ no-library ] if ;
HOOK: >deployed-library-path os ( path -- path' )
<< {
{ [ os windows? ] [ "alien.libraries.windows" ] }
{ [ os unix? ] [ "alien.libraries.unix" ] }
} cond require >>