io.sockets.secure,alien.libraries: new word word>dlsym to replace the dlsym? word

char-rename
Björn Lindqvist 2016-11-30 11:46:40 +01:00
parent b42668ebeb
commit 2ba659dcb1
4 changed files with 65 additions and 51 deletions

View File

@ -1,47 +1,9 @@
! Copyright (C) 2009 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien alien.syntax assocs help.markup
help.syntax io.backend kernel namespaces strings ;
USING: alien alien.syntax assocs help.markup help.syntax strings words
;
IN: alien.libraries
HELP: make-library
{ $values
{ "path" "a pathname string" } { "abi" "the ABI used by the library, either " { $link cdecl } " or " { $link stdcall } }
{ "library" library } }
{ $description "Opens a C library using the path and ABI parameters and outputs a library tuple." }
{ $notes "User code should use " { $link add-library } " so that the opened library is added to a global hashtable, " { $link libraries } "." } ;
HELP: libraries
{ $description "A global hashtable that keeps a list of open libraries. Use the " { $link add-library } " word to construct a library and add it with a single call." } ;
HELP: library
{ $values { "name" string } { "library" assoc } }
{ $description "Looks up a library by its logical name. The library object is a hashtable with the following keys:"
{ $list
{ { $snippet "name" } " - the full path of the C library binary" }
{ { $snippet "abi" } " - the ABI used by the library, either " { $link cdecl } " or " { $link stdcall } }
{ { $snippet "dll" } " - an instance of the " { $link dll } " class; only set if the library is loaded" }
}
} ;
HELP: dlopen
{ $values { "path" "a pathname string" } { "dll" "a DLL handle" } }
{ $description "Opens a native library and outputs a handle which may be passed to " { $link dlsym } " or " { $link dlclose } "." }
{ $errors "Throws an error if the library could not be found, or if loading fails for some other reason." }
{ $notes "This is the low-level facility used to implement " { $link load-library } ". Use the latter instead." } ;
HELP: dlsym
{ $values { "name" "a C symbol name" } { "dll" "a DLL handle" } { "alien" { $maybe alien } } }
{ $description "Looks up a symbol in a native library. If " { $snippet "dll" } " is " { $link f } " looks for the symbol in the runtime executable. If the symbol was not found, outputs " { $link f } "." } ;
HELP: dlclose
{ $values { "dll" "a DLL handle" } }
{ $description "Closes a DLL handle created by " { $link dlopen } ". This word might not be implemented on all platforms." } ;
HELP: load-library
{ $values { "name" string } { "dll" "a DLL handle" } }
{ $description "Loads a library by logical name and outputs a handle which may be passed to " { $link dlsym } " or " { $link dlclose } ". If the library is already loaded, returns the existing handle." } ;
HELP: add-library
{ $values { "name" string } { "path" string } { "abi" "one of " { $link cdecl } " or " { $link stdcall } } }
{ $description "Defines a new logical library named " { $snippet "name" } " located in the file system at " { $snippet "path" } " and the specified ABI. You can find the location of the library via words in " { $vocab-link "alien.libraries.finder" } ". The logical library name can then be used by a " { $link POSTPONE: LIBRARY: } " form to specify the logical library for subsequent " { $link POSTPONE: FUNCTION: } " definitions." }
@ -68,10 +30,52 @@ HELP: deploy-library
{ $values { "name" string } }
{ $description "Specifies that the logical library named " { $snippet "name" } " should be included during " { $link "tools.deploy" } ". " { $snippet "name" } " must be the name of a library previously loaded with " { $link add-library } "." } ;
HELP: dlclose
{ $values { "dll" "a DLL handle" } }
{ $description "Closes a DLL handle created by " { $link dlopen } ". This word might not be implemented on all platforms." } ;
HELP: dlopen
{ $values { "path" "a pathname string" } { "dll" "a DLL handle" } }
{ $description "Opens a native library and outputs a handle which may be passed to " { $link dlsym } " or " { $link dlclose } "." }
{ $errors "Throws an error if the library could not be found, or if loading fails for some other reason." }
{ $notes "This is the low-level facility used to implement " { $link load-library } ". Use the latter instead." } ;
HELP: dlsym
{ $values { "name" "a C symbol name" } { "dll" "a DLL handle" } { "alien" { $maybe alien } } }
{ $description "Looks up a symbol in a native library. If " { $snippet "dll" } " is " { $link f } " looks for the symbol in the runtime executable. If the symbol was not found, outputs " { $link f } "." } ;
HELP: make-library
{ $values
{ "path" "a pathname string" } { "abi" "the ABI used by the library, either " { $link cdecl } " or " { $link stdcall } }
{ "library" library } }
{ $description "Opens a C library using the path and ABI parameters and outputs a library tuple." }
{ $notes "User code should use " { $link add-library } " so that the opened library is added to a global hashtable, " { $link libraries } "." } ;
HELP: libraries
{ $description "A global hashtable that keeps a list of open libraries. Use the " { $link add-library } " word to construct a library and add it with a single call." } ;
HELP: library
{ $values { "name" string } { "library" assoc } }
{ $description "Looks up a library by its logical name. The library object is a hashtable with the following keys:"
{ $list
{ { $snippet "name" } " - the full path of the C library binary" }
{ { $snippet "abi" } " - the ABI used by the library, either " { $link cdecl } " or " { $link stdcall } }
{ { $snippet "dll" } " - an instance of the " { $link dll } " class; only set if the library is loaded" }
}
} ;
HELP: load-library
{ $values { "name" string } { "dll" "a DLL handle" } }
{ $description "Loads a library by logical name and outputs a handle which may be passed to " { $link dlsym } " or " { $link dlclose } ". If the library is already loaded, returns the existing handle." } ;
HELP: remove-library
{ $values { "name" string } }
{ $description "Unloads a library and removes it from the internal list of libraries. The " { $snippet "name" } " parameter should be a name that was previously passed to " { $link add-library } ". If no library with that name exists, this word does nothing." } ;
HELP: word>dlsym
{ $values { "word" word } { "alien/f" maybe{ alien } } }
{ $description "Takes a word which calls a C library function and outputs the address of the symbol it points to as an alien. If the symbol isn't loaded, outputs f." } ;
ARTICLE: "loading-libs" "Loading native libraries"
"Before calling a C library, you must associate its path name on disk with a logical name which Factor uses to identify the library:"
{ $subsections

View File

@ -1,4 +1,6 @@
USING: alien alien.libraries alien.syntax kernel tools.test ;
USING: accessors alien alien.libraries alien.syntax kernel libc
tools.test ;
IN: alien.libraries.tests
{ f } [ DLL" fadfasdfsada" dll-valid? ] unit-test
@ -25,3 +27,12 @@ USING: alien alien.libraries alien.syntax kernel tools.test ;
"test-library" "blah" cdecl add-library?
"blah" remove-library
] unit-test
{ "blah" f } [
"blah" cdecl make-library [ path>> ] [ dll>> dll-valid? ] bi
] unit-test
! word>dlsym
{ t } [
\ errno word>dlsym alien?
] unit-test

View File

@ -27,12 +27,11 @@ TUPLE: library { path string } dll dlerror { abi abi initial: cdecl } ;
C: <library> library
: lookup-library ( name -- library ) libraries get at ;
: lookup-library ( name -- library/f ) libraries get at ;
ERROR: no-library-named name ;
GENERIC: dlsym? ( name string/dll -- ? )
M: string dlsym? dup lookup-library [ nip dll>> dlsym? ] [ no-library-named ] if* ;
M: dll dlsym? dlsym >boolean ;
: word>dlsym ( word -- alien/f )
def>> [ third ] [ second dup [ lookup-library dll>> ] when ] bi
dlsym ;
: open-dll ( path -- dll dll-error/f )
[ dlopen dup dll-valid? [ f ] [ dlerror ] if ]

View File

@ -1,8 +1,8 @@
! Copyright (C) 2008, 2010 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien.libraries calendar combinators delegate
destructors io io.sockets io.sockets.private kernel memoize
namespaces present sequences summary system vocabs ;
destructors io io.sockets io.sockets.private kernel memoize namespaces
openssl.libssl present sequences summary system vocabs ;
IN: io.sockets.secure
SYMBOL: secure-socket-timeout
@ -23,9 +23,9 @@ ERROR: no-tls-supported ;
MEMO: best-tls-method ( -- class )
{
{ [ "TLSv1_2_method" "libssl" dlsym? ] [ TLSv1.2 ] }
{ [ "TLSv1_1_method" "libssl" dlsym? ] [ TLSv1.1 ] }
{ [ "TLSv1_method" "libssl" dlsym? ] [ TLSv1 ] }
{ [ \ TLSv1_2_method word>dlsym ] [ TLSv1.2 ] }
{ [ \ TLSv1_1_method word>dlsym ] [ TLSv1.1 ] }
{ [ \ TLSv1_method word>dlsym ] [ TLSv1 ] }
[ no-tls-supported ]
} cond ;