[foreign-memaccess+abi] RFR: 8314592: Add shortcut to SymbolLookup::find [v2]
Maurizio Cimadamore
mcimadamore at openjdk.org
Wed Aug 23 18:37:36 UTC 2023
On Tue, 22 Aug 2023 12:33:26 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:
>> This patch adds a default method in `SymbolLookup`, namely `SymbolLookup::get`. Its implementation is very simple: it calls `SymbolLookup::find` and then `Optional::get` on the result. This allows to simplify many clients that end up calling `Optional::get` or `Optional::orElseThrow` after a symbol lookup, but still retains the compositional advantage of the optional-returning lookup primitive.
>
> Maurizio Cimadamore has updated the pull request incrementally with one additional commit since the last revision:
>
> Address review comments
It occurred to me that another possible way to reduce boilerplate could be to leave `SymbolLookup` alone, and act on `Linker` instead, by adding two additional `default` overloads of `downcallHandle`:
default downcallHandle(String funcName, FunctionDescriptor desc, Linker.Options... options) {
return downcallHandle(defaultLookup(), funcName, desc, options);
}
default downcallHandle(SymbolLookup lookup, String funcName, FunctionDescriptor desc, Linker.Options... options) {
return downcallHandle(lookup.find(funcName).orElseThrow(), desc, options);
}
Now, let's look at some code creating a downcall handle. This is with 21 API:
MethodHandle strlen = linker.downcallHandle(
linker.defaultLookup().find("strlen").orElseThrow(),
FunctionDescriptor.of(JAVA_LONG, ADDRESS)
);
This is with the changes proposed in this PR:
MethodHandle strlen = linker.downcallHandle(
linker.defaultLookup().get("strlen"),
FunctionDescriptor.of(JAVA_LONG, ADDRESS)
);
```
And this is with the provided `Linker` overloads:
```
MethodHandle strlen = linker.downcallHandle(
"strlen",
FunctionDescriptor.of(JAVA_LONG, ADDRESS)
);
```
The latter is certainly the most succinct. And if we were to lookup a symbol in some other library:
MethodHandle clangVersion = linker.downcallHandle(
clangLookup,
"clang_getClangVersion",
FunctionDescriptor.of(JAVA_LONG, ADDRESS)
);
Which seems to scale well. Of course this is a bit more ad-hoc as it would only work when interacting with `Linker::downcallHandle`, but perhaps that's where most of the verbosity comes from (at least looking at this patch) ?
-------------
PR Comment: https://git.openjdk.org/panama-foreign/pull/871#issuecomment-1690447754
More information about the panama-dev
mailing list