[foreign-memaccess+abi] RFR: 8314592: Add shortcut to SymbolLookup::find

ExE Boss duke at openjdk.org
Sat Aug 19 12:40:47 UTC 2023


On Fri, 18 Aug 2023 17:05:46 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.

src/java.base/share/classes/java/lang/foreign/SymbolLookup.java line 142:

> 140:     default MemorySegment get(String name) {
> 141:         return find(name).get();
> 142:     }

Maybe provide a better error message:
Suggestion:

    /**
     * Returns the address of the symbol with the given name.
     * @implSpec the default implementation for this method calls {@code this.find(name).orElseThrow()}.
     * @param name the symbol name.
     * @return a zero-length memory segment whose address indicates the address of the symbol.
     * @throws java.util.NoSuchElementException if no symbol with the given name exists.
     */
    default MemorySegment get(String name) {
        var address = this.find(name);

        // Avoid using a capturing lambda
        if (address.isPresent()) {
            return address.orElseThrow();
        }
        throw new NoSuchElementException("Unable to find a native symbol named '" + name + "'");
    }


See also: https://github.com/openjdk/jdk/pull/15286

https://github.com/openjdk/panama-foreign/blob/c4e05f5c25910e5e23d3f6af585d066f671fb9b7/src/java.base/share/classes/jdk/internal/foreign/support/DefaultNativeLookupUtil.java#L47-L57

-------------

PR Review Comment: https://git.openjdk.org/panama-foreign/pull/871#discussion_r1299181558


More information about the panama-dev mailing list