From 2ba659dcb1391218dceb38720c5722878f2e7f85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Lindqvist?= Date: Wed, 30 Nov 2016 11:46:40 +0100 Subject: [PATCH] io.sockets.secure,alien.libraries: new word word>dlsym to replace the dlsym? word --- basis/alien/libraries/libraries-docs.factor | 84 ++++++++++---------- basis/alien/libraries/libraries-tests.factor | 13 ++- basis/alien/libraries/libraries.factor | 9 +-- basis/io/sockets/secure/secure.factor | 10 +-- 4 files changed, 65 insertions(+), 51 deletions(-) diff --git a/basis/alien/libraries/libraries-docs.factor b/basis/alien/libraries/libraries-docs.factor index c824f8ca4d..262ff71e32 100644 --- a/basis/alien/libraries/libraries-docs.factor +++ b/basis/alien/libraries/libraries-docs.factor @@ -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 diff --git a/basis/alien/libraries/libraries-tests.factor b/basis/alien/libraries/libraries-tests.factor index 46b8ce6699..33bdeaf2db 100755 --- a/basis/alien/libraries/libraries-tests.factor +++ b/basis/alien/libraries/libraries-tests.factor @@ -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 diff --git a/basis/alien/libraries/libraries.factor b/basis/alien/libraries/libraries.factor index 070aee7092..0eb4dad7ab 100755 --- a/basis/alien/libraries/libraries.factor +++ b/basis/alien/libraries/libraries.factor @@ -27,12 +27,11 @@ TUPLE: library { path string } dll dlerror { abi abi initial: cdecl } ; C: 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 ] diff --git a/basis/io/sockets/secure/secure.factor b/basis/io/sockets/secure/secure.factor index 1f1de35182..8a80c59bf9 100644 --- a/basis/io/sockets/secure/secure.factor +++ b/basis/io/sockets/secure/secure.factor @@ -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 ;