[foreign-preview] RFR: 8282873: Bring back SymbolLookup
Paul Sandoz
psandoz at openjdk.java.net
Thu Mar 10 20:58:06 UTC 2022
On Wed, 9 Mar 2022 11:10:03 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:
> This patch brings back the `SymbolLookup` abstraction, which was initially left behind during the move to java.base.
>
> In hindsight, moving lookup capabilities to `CLinker` and `ClassLoader`, while economical in terms of API surface, is problematic, as it is much harder to users of the API to understand how to look up for library symbols (since lookup capabilities are scattered across different classes).
>
> Moreover, recent JDK changes, such as JDK-8281335 and JDK-8282608, make library loading more flexible, and much closer to a raw dlopen/dlsym/dlclose. Which means we can now provide, in addition to loader and system lookup, a *third* kind of lookup, which features deterministic library loading/unloading (see `SymbolLoojup::libraryLookup`).
>
> Overall, I think that having a dedicated abstraction for looking up symbols in libraries is a good thing; not only it makes the API more discoverable, but it also allows clients to define custom lookup (as `SymbolLookup` is a simple functional interface).
With hindsight this is better (had it right before), since its consolidated plus we enable the new functionality of loading libraries independent of the class loader.
Had a minor comment on code duplication in the implementation of `SymbolLookup`, up to you if you wanna address that or not.
src/java.base/share/classes/java/lang/foreign/SymbolLookup.java line 126:
> 124: */
> 125: @CallerSensitive
> 126: static SymbolLookup libraryLookup(String name, MemorySession session) {
This could just call the Path version, creating a path from the name (or vice versa), rather than duplicating the code.
src/java.base/share/classes/java/lang/foreign/SymbolLookup.java line 162:
> 160: if (library == null) {
> 161: throw new IllegalArgumentException("Cannot open library: " + libName);
> 162: }
Du-duping the String/Path accepting methods means there is no need to pass libName and perform a null check here, it can be pushed up, or this whole method inlined.
-------------
Marked as reviewed by psandoz (Committer).
PR: https://git.openjdk.java.net/panama-foreign/pull/666
More information about the panama-dev
mailing list