Codechange: refactor the Windows-only DllLoader in a cross-platform LibraryLoader #11751
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation / Problem
For another PR (#11628) I had need to dynamically load a library on any OS. We already had some code to do something similar for Windows only (in
DllLoader
). So this PR extends its capabilities and makes it cross-platform.Description
DllLoader
was very scoped around Windows (both in name and implementation). This PR recreates the API of this loader, making sure it can also be implemented for Linux and MacOS.When-ever an instance of
LibraryLoader
gets out of scope, everything is cleaned up nicely. This means that any function pointer you retrieve from it only remains valid for as long as the object lives. It -might- live longer, but that is OS-dependent, and kinda depends if anything else still has the library loaded or not.During implementation I found out that some headers were missing from other files, now
<windows.h>
wasn't included anymore inos/windows/win32.h
.LibraryLoader
has functions to see if loading succeeded and/or what the error was (if any). This helps in producing nice error-messages to the user, instead of internal error codes.PS: yes, the MacOS implementation is in there: it is the same as the Linux implementation, and both OSes share the implementation from
os/unix/..
, as is already the case with some other pieces of code in there.PPS: no, Emscripten is not (and will not) be supported, as there is no concept of "library" in that sense. We could, in theory, make them WASM plugins, but that requires other infrastructure to load in.
Limitations
Checklist for review
Some things are not automated, and forgotten often. This list is a reminder for the reviewers.