[foreign-memaccess+abi] RFR: 8314592: Add shortcut to SymbolLookup::find [v2]
Per Minborg
pminborg at openjdk.org
Thu Aug 24 08:21:51 UTC 2023
On Wed, 23 Aug 2023 18:34:55 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:
> 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) ?
In my panamization prototype, I have added utility methods that do something similar. I think this would be a welcomed addition, especially if the the default method is refined slightly to provide guidance as to what symbol could not be looked up, should that be the case.
-------------
PR Comment: https://git.openjdk.org/panama-foreign/pull/871#issuecomment-1691226518
More information about the panama-dev
mailing list