Skip to content

Commit bda0194

Browse files
fangererotethal
authored andcommitted
Type native library lookups
1 parent 467d102 commit bda0194

5 files changed

Lines changed: 59 additions & 22 deletions

File tree

‎graalpython/com.oracle.graal.python.processor/src/com/oracle/graal/python/processor/CApiBuiltinsProcessor.java‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,7 @@ private void generateNfiSupport(Element[] origins) throws IOException {
13991399
lines.add("import java.lang.invoke.MethodHandle;");
14001400
lines.add("import java.lang.invoke.MethodHandles;");
14011401
lines.add("import java.lang.invoke.MethodType;");
1402+
lines.add("import java.util.OptionalLong;");
14021403
lines.add("");
14031404
lines.add("import static com.oracle.truffle.api.CompilerDirectives.shouldNotReachHere;");
14041405
lines.add("");
@@ -1425,13 +1426,9 @@ private void generateNfiSupport(Element[] origins) throws IOException {
14251426
lines.add("");
14261427
lines.add(" @Override");
14271428
lines.add(" @SuppressWarnings(\"restricted\")");
1428-
lines.add(" protected Object libraryLookupImpl(String name, Object arena) {");
1429-
lines.add(" return SymbolLookup.libraryLookup(name, (Arena) arena);");
1430-
lines.add(" }");
1431-
lines.add("");
1432-
lines.add(" @Override");
1433-
lines.add(" protected long lookupSymbolImpl(Object lookup, String name) {");
1434-
lines.add(" return ((SymbolLookup) lookup).find(name).orElseThrow().address();");
1429+
lines.add(" protected NativeLibraryLookup libraryLookupImpl(String name, Object arena) {");
1430+
lines.add(" SymbolLookup lookup = SymbolLookup.libraryLookup(name, (Arena) arena);");
1431+
lines.add(" return symbolName -> lookup.find(symbolName).map(segment -> OptionalLong.of(segment.address())).orElseGet(OptionalLong::empty);");
14351432
lines.add(" }");
14361433
lines.add("");
14371434
lines.add(" @Override");
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights, and any and all patent rights owned or freely licensable by
11+
* each licensor hereunder covering either (i) the unmodified Software as
12+
* contributed to or provided by such licensor, or (ii) the Larger Works (as
13+
* defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software (each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.runtime.nativeaccess;
42+
43+
import java.util.OptionalLong;
44+
45+
@FunctionalInterface
46+
public interface NativeLibraryLookup {
47+
OptionalLong find(String name);
48+
}

‎graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/nativeaccess/NfiContext.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ private static boolean isWindows() {
159159
private static long getLastErrorPtr;
160160
private static long formatMessagePtr;
161161
private static Object windowsLookupArena;
162-
private static Object windowsLookup;
162+
private static NativeLibraryLookup windowsLookup;
163163

164164
private static void ensureLoader() {
165165
if (isWindows()) {

‎graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/nativeaccess/NfiSupport.java‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,12 @@ public static void closeArena(Object arena) {
9090
INSTANCE.closeArenaImpl(arena);
9191
}
9292

93-
public static Object libraryLookup(String name, Object arena) {
93+
public static NativeLibraryLookup libraryLookup(String name, Object arena) {
9494
return INSTANCE.libraryLookupImpl(name, arena);
9595
}
9696

97-
public static long lookupSymbol(Object lookup, String name) {
98-
return INSTANCE.lookupSymbolImpl(lookup, name);
97+
public static long lookupSymbol(NativeLibraryLookup lookup, String name) {
98+
return lookup.find(name).orElseThrow();
9999
}
100100

101101
public static long lookupDefault(String name) {
@@ -139,9 +139,7 @@ private MethodHandle createBoundHandleImpl(long pointer, NfiType resType, NfiTyp
139139

140140
protected abstract void closeArenaImpl(Object arena);
141141

142-
protected abstract Object libraryLookupImpl(String name, Object arena);
143-
144-
protected abstract long lookupSymbolImpl(Object lookup, String name);
142+
protected abstract NativeLibraryLookup libraryLookupImpl(String name, Object arena);
145143

146144
protected abstract long lookupDefaultImpl(String name);
147145

‎graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/nativeaccess/NfiSupportJdk21.java‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,15 @@
4646
final class NfiSupportJdk21 extends NfiSupport {
4747
@Override
4848
protected Object createArenaImpl() {
49-
throw unsupported();
49+
return null;
5050
}
5151

5252
@Override
5353
protected void closeArenaImpl(Object arena) {
54-
throw unsupported();
55-
}
56-
57-
@Override
58-
protected Object libraryLookupImpl(String name, Object arena) {
59-
throw unsupported();
6054
}
6155

6256
@Override
63-
protected long lookupSymbolImpl(Object lookup, String name) {
57+
protected NativeLibraryLookup libraryLookupImpl(String name, Object arena) {
6458
throw unsupported();
6559
}
6660

0 commit comments

Comments
 (0)