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.
|
2009-07-09 07:41:51 -04:00
|
|
|
USING: accessors alien alien.strings assocs io.backend
|
2010-05-18 18:46:31 -04:00
|
|
|
kernel namespaces destructors sequences strings
|
2011-11-02 14:23:41 -04:00
|
|
|
system io.pathnames fry combinators vocabs ;
|
2009-03-26 00:00:02 -04:00
|
|
|
IN: alien.libraries
|
|
|
|
|
2013-12-21 00:07:31 -05:00
|
|
|
: dlopen ( path -- dll ) native-string>alien (dlopen) ;
|
2009-05-02 14:45:38 -04:00
|
|
|
|
2009-05-05 17:00:31 -04:00
|
|
|
: dlsym ( name dll -- alien ) [ string>symbol ] dip (dlsym) ;
|
2009-05-02 14:45:38 -04:00
|
|
|
|
2011-05-20 18:11:50 -04:00
|
|
|
: dlsym-raw ( name dll -- alien ) [ string>symbol ] dip (dlsym-raw) ;
|
|
|
|
|
2011-09-13 02:06:08 -04:00
|
|
|
HOOK: dlerror os ( -- message/f )
|
2011-09-12 15:27:34 -04:00
|
|
|
|
2009-03-26 00:00:02 -04:00
|
|
|
SYMBOL: libraries
|
|
|
|
|
|
|
|
libraries [ H{ } clone ] initialize
|
|
|
|
|
2014-06-05 17:26:38 -04:00
|
|
|
TUPLE: library { path string } dll dlerror { abi abi initial: cdecl } ;
|
|
|
|
|
|
|
|
C: <library> library
|
2009-03-26 00:00:02 -04:00
|
|
|
|
2010-02-16 16:32:14 -05:00
|
|
|
ERROR: no-library name ;
|
|
|
|
|
2013-03-23 20:16:46 -04:00
|
|
|
: lookup-library ( name -- library ) libraries get at ;
|
2009-03-26 00:00:02 -04:00
|
|
|
|
2014-06-05 17:26:38 -04:00
|
|
|
: open-dll ( path -- dll dll-error/f )
|
|
|
|
[ dlopen dup dll-valid? [ f ] [ dlerror ] if ]
|
|
|
|
[ f f ] if* ;
|
|
|
|
|
|
|
|
: make-library ( path abi -- library )
|
|
|
|
[ dup open-dll ] dip <library> ;
|
|
|
|
|
2011-09-13 02:06:08 -04:00
|
|
|
: library-dll ( library -- dll )
|
|
|
|
dup [ dll>> ] when ;
|
2009-03-26 00:00:02 -04:00
|
|
|
|
|
|
|
: load-library ( name -- dll )
|
2013-03-23 20:16:46 -04:00
|
|
|
lookup-library library-dll ;
|
2009-03-26 00:00:02 -04:00
|
|
|
|
2009-07-09 07:41:51 -04:00
|
|
|
M: dll dispose dlclose ;
|
|
|
|
|
|
|
|
M: library dispose dll>> [ dispose ] when* ;
|
|
|
|
|
|
|
|
: remove-library ( name -- )
|
|
|
|
libraries get delete-at* [ dispose ] [ drop ] if ;
|
|
|
|
|
2010-10-26 00:39:15 -04:00
|
|
|
: add-library? ( name path abi -- ? )
|
2013-03-23 20:16:46 -04:00
|
|
|
[ lookup-library ] 2dip
|
2010-10-26 00:39:15 -04:00
|
|
|
'[ [ path>> _ = ] [ abi>> _ = ] bi and not ] [ t ] if* ;
|
|
|
|
|
2009-07-31 20:46:18 -04:00
|
|
|
: add-library ( name path abi -- )
|
2010-10-26 00:39:15 -04:00
|
|
|
3dup add-library? [
|
|
|
|
[ 2drop remove-library ]
|
2014-06-07 23:48:09 -04:00
|
|
|
[ [ nip ] dip make-library ]
|
2014-06-05 17:26:38 -04:00
|
|
|
[ 2drop libraries get set-at ] 3tri
|
2010-10-26 00:39:15 -04:00
|
|
|
] [ 3drop ] if ;
|
2010-02-16 16:32:14 -05:00
|
|
|
|
2010-02-22 07:28:56 -05:00
|
|
|
: library-abi ( library -- abi )
|
2013-03-23 20:16:46 -04:00
|
|
|
lookup-library [ abi>> ] [ cdecl ] if* ;
|
2010-02-22 07:28:56 -05:00
|
|
|
|
2010-04-12 19:09:26 -04:00
|
|
|
ERROR: no-such-symbol name library ;
|
|
|
|
|
2012-08-01 01:11:25 -04:00
|
|
|
: address-of ( name library -- value )
|
|
|
|
2dup load-library dlsym-raw [ 2nip ] [ no-such-symbol ] if* ;
|
2010-04-12 19:09:26 -04:00
|
|
|
|
2010-02-22 07:28:56 -05:00
|
|
|
SYMBOL: deploy-libraries
|
|
|
|
|
|
|
|
deploy-libraries [ V{ } clone ] initialize
|
|
|
|
|
2010-02-16 16:32:14 -05:00
|
|
|
: 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' )
|
|
|
|
|
2011-09-13 02:06:08 -04:00
|
|
|
<< {
|
|
|
|
{ [ os windows? ] [ "alien.libraries.windows" ] }
|
|
|
|
{ [ os unix? ] [ "alien.libraries.unix" ] }
|
|
|
|
} cond require >>
|