From 310a4a247c455ef5a5ea9858815ceaef85ed7440 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Mon, 19 Feb 2018 10:34:06 -0600 Subject: [PATCH] alien.libraries: load-library is misnamed, refactor library-dll. load-library implies that dlopen() is called, which is totally not the case. add-library calls dlopen() and load-library just looks the opened library up in the ``libraries`` global. What load-library did is now library-dll. If we want to do it right, add-library should be a declarative top-level form that tries to call a real load-library on demand instead of memoizing the first try which would fix the case where the library is not found, is moved into position by the user and Factor declines to look it up again. --- basis/alien/libraries/libraries-docs.factor | 10 +++++----- basis/alien/libraries/libraries.factor | 12 ++++++++---- basis/compiler/tests/alien.factor | 4 ++-- basis/compiler/tests/linkage-errors.factor | 2 +- extra/readline/readline.factor | 2 +- 5 files changed, 17 insertions(+), 13 deletions(-) diff --git a/basis/alien/libraries/libraries-docs.factor b/basis/alien/libraries/libraries-docs.factor index 741a5be60c..366cc91da6 100644 --- a/basis/alien/libraries/libraries-docs.factor +++ b/basis/alien/libraries/libraries-docs.factor @@ -38,7 +38,7 @@ 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." } ; +{ $notes "This is the low-level facility used to implement " { $link add-library } ". Use the latter instead." } ; HELP: dlsym { $values { "name" "a C symbol name" } { "dll" "a DLL handle" } { "alien" { $maybe alien } } } @@ -72,9 +72,9 @@ HELP: library } } ; -HELP: load-library +HELP: library-dll { $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." } ; +{ $description "Looks up a library by logical name and outputs a handle which may be passed to " { $link dlsym } " or " { $link dlclose } "." } ; HELP: remove-library { $values { "name" string } } @@ -86,8 +86,8 @@ ARTICLE: "loading-libs" "Loading native libraries" add-library remove-library } -"Once a library has been defined, you can try loading it to see if the path name is correct:" -{ $subsections load-library } +"Once a library has been defined, you can see if the library has correctly loaded:" +{ $subsections library-dll } "If the compiler cannot load a library, or cannot resolve a symbol in a library, a linkage error is reported using the compiler error mechanism (see " { $link "compiler-errors" } "). Once you install the right library, reload the source file containing the " { $link add-library } " form to force the compiler to try loading the library again." $nl "Libraries that do not come standard with the operating system need to be included with deployed applications that use them. A word is provided to instruct " { $link "tools.deploy" } " that a library must be so deployed:" diff --git a/basis/alien/libraries/libraries.factor b/basis/alien/libraries/libraries.factor index 0f95141e2d..2365e0ee42 100755 --- a/basis/alien/libraries/libraries.factor +++ b/basis/alien/libraries/libraries.factor @@ -36,14 +36,18 @@ C: library : make-library ( path abi -- library ) [ dup open-dll ] dip ; -: library-dll ( library -- dll ) +GENERIC: library-dll ( obj -- dll ) + +M: f library-dll ; + +M: library library-dll dup [ dll>> ] when ; -: load-library ( name -- dll ) +M: string library-dll ( library -- dll ) lookup-library library-dll ; : dlsym? ( function library -- alien/f ) - load-library dlsym ; + library-dll dlsym ; M: dll dispose dlclose ; @@ -84,7 +88,7 @@ M: library dispose dll>> [ dispose ] when* ; lookup-library [ abi>> ] [ cdecl ] if* ; : address-of ( name library -- value ) - 2dup load-library dlsym-raw + 2dup library-dll dlsym-raw [ 2nip ] [ no-such-symbol ] if* ; SYMBOL: deploy-libraries diff --git a/basis/compiler/tests/alien.factor b/basis/compiler/tests/alien.factor index 4d8a37a71e..08fdcf8761 100755 --- a/basis/compiler/tests/alien.factor +++ b/basis/compiler/tests/alien.factor @@ -132,7 +132,7 @@ unit-test int { int int int int } stdcall alien-indirect gc ; -[ f ] [ "f-stdcall" load-library f = ] unit-test +[ f ] [ "f-stdcall" library-dll f = ] unit-test [ stdcall ] [ "f-stdcall" lookup-library abi>> ] unit-test : ffi_test_18 ( w x y z -- int ) @@ -642,7 +642,7 @@ os windows? [ [ ] [ assembly-test-1 ] unit-test -[ f ] [ "f-fastcall" load-library f = ] unit-test +[ f ] [ "f-fastcall" library-dll f = ] unit-test [ fastcall ] [ "f-fastcall" lookup-library abi>> ] unit-test : ffi_test_49 ( x -- int ) diff --git a/basis/compiler/tests/linkage-errors.factor b/basis/compiler/tests/linkage-errors.factor index 1b60462aa2..e5f3d21c9a 100644 --- a/basis/compiler/tests/linkage-errors.factor +++ b/basis/compiler/tests/linkage-errors.factor @@ -33,7 +33,7 @@ FUNCTION: void no_such_function ( ) ${ KERNEL-ERROR ERROR-UNDEFINED-SYMBOL "no_such_function" string>symbol - "no_such_library" load-library + "no_such_library" library-dll } = ] must-fail-with diff --git a/extra/readline/readline.factor b/extra/readline/readline.factor index 75a90545b5..bbad24a074 100644 --- a/extra/readline/readline.factor +++ b/extra/readline/readline.factor @@ -21,7 +21,7 @@ IN: readline current-line readline.ffi:rl_point head ; : has-readline? ( -- ? ) - "readline" dup load-library dlsym-raw >boolean ; + "readline" dup library-dll dlsym-raw >boolean ; : set-completion ( quot -- ) [