Skip to content

Commit 83da2e5

Browse files
committed
Fix: do full 'native_to_java' resolving except for 'int' and 'long'.
1 parent e355c79 commit 83da2e5

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

graalpython/com.oracle.graal.python.cext/src/capi.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ void* native_pointer_to_java(PyObject* obj) {
209209
} else if (!truffle_cannot_be_handle(obj)) {
210210
return resolve_handle(cache, (uint64_t)obj);
211211
}
212+
return obj;
212213
}
213214

214215
__attribute__((always_inline))

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/CExtNodes.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,8 @@ public abstract static class ToJavaNode extends CExtBaseNode {
592592
@Child private PCallNativeNode callNativeNode;
593593
@Child private AsPythonObjectNode toJavaNode = AsPythonObjectNode.create();
594594

595-
@CompilationFinal TruffleObject nativeToJavaFunction;
595+
@CompilationFinal private TruffleObject nativeToJavaFunction;
596+
@CompilationFinal private TruffleObject nativePointerToJavaFunction;
596597

597598
public abstract Object execute(Object value);
598599

@@ -626,25 +627,48 @@ boolean doBoolean(boolean b) {
626627
return b;
627628
}
628629

630+
@Specialization
631+
Object doInt(int i) {
632+
// Unfortunately, an int could be a native pointer and therefore a handle. So, we must
633+
// try resolving it. At least we know that it's not a native type.
634+
return native_pointer_to_java(i);
635+
}
636+
637+
@Specialization
638+
Object doLong(long l) {
639+
// Unfortunately, a long could be a native pointer and therefore a handle. So, we must
640+
// try resolving it. At least we know that it's not a native type.
641+
return native_pointer_to_java(l);
642+
}
643+
629644
@Specialization
630645
byte doLong(byte b) {
631646
return b;
632647
}
633648

634649
@Fallback
635650
Object doForeign(Object value) {
636-
if (callNativeNode == null) {
651+
if (nativeToJavaFunction == null) {
637652
CompilerDirectives.transferToInterpreterAndInvalidate();
638-
callNativeNode = insert(PCallNativeNode.create());
653+
nativeToJavaFunction = importCAPISymbol(NativeCAPISymbols.FUN_NATIVE_TO_JAVA);
639654
}
640-
if (callNativeNode == null) {
655+
return call_native_conversion(nativeToJavaFunction, value);
656+
}
657+
658+
private Object native_pointer_to_java(Object value) {
659+
if (nativePointerToJavaFunction == null) {
641660
CompilerDirectives.transferToInterpreterAndInvalidate();
661+
nativePointerToJavaFunction = importCAPISymbol(NativeCAPISymbols.FUN_NATIVE_POINTER_TO_JAVA);
642662
}
643-
if (nativeToJavaFunction == null) {
663+
return call_native_conversion(nativePointerToJavaFunction, value);
664+
}
665+
666+
private Object call_native_conversion(TruffleObject target, Object value) {
667+
if (callNativeNode == null) {
644668
CompilerDirectives.transferToInterpreterAndInvalidate();
645-
nativeToJavaFunction = importCAPISymbol(NativeCAPISymbols.FUN_NATIVE_TO_JAVA);
669+
callNativeNode = insert(PCallNativeNode.create());
646670
}
647-
return toJavaNode.execute(callNativeNode.execute(nativeToJavaFunction, new Object[]{value}));
671+
return toJavaNode.execute(callNativeNode.execute(target, new Object[]{value}));
648672
}
649673

650674
public static ToJavaNode create() {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/NativeCAPISymbols.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242

4343
public abstract class NativeCAPISymbols {
4444

45-
public static final String FUN_NATIVE_TO_JAVA = "native_pointer_to_java";
45+
public static final String FUN_NATIVE_POINTER_TO_JAVA = "native_pointer_to_java";
46+
public static final String FUN_NATIVE_TO_JAVA = "native_to_java_exported";
4647
public static final String FUN_PY_TRUFFLE_STRING_TO_CSTR = "PyTruffle_StringToCstr";
4748
public static final String FUN_PY_OBJECT_HANDLE_FOR_JAVA_OBJECT = "PyObjectHandle_ForJavaObject";
4849
public static final String FUN_PY_OBJECT_HANDLE_FOR_JAVA_TYPE = "PyObjectHandle_ForJavaType";

0 commit comments

Comments
 (0)