Skip to content

Commit 9a2497f

Browse files
committed
Consider that a capsules name may be a TruffleString
1 parent a59374c commit 9a2497f

File tree

1 file changed

+29
-4
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/hpy/jni

1 file changed

+29
-4
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/hpy/jni/GraalHPyJNIContext.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
import java.util.LinkedList;
6060
import java.util.List;
6161

62-
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyData;
6362
import org.graalvm.nativeimage.ImageInfo;
6463

6564
import com.oracle.graal.python.PythonLanguage;
@@ -86,6 +85,7 @@
8685
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyContextFunctions.GraalHPyContextVarGet;
8786
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyContextFunctions.HPyBinaryContextFunction;
8887
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyContextFunctions.HPyTernaryContextFunction;
88+
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyData;
8989
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyDef;
9090
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyHandle;
9191
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyNativeContext;
@@ -137,7 +137,6 @@
137137
import com.oracle.graal.python.nodes.PGuards;
138138
import com.oracle.graal.python.nodes.PRaiseNode;
139139
import com.oracle.graal.python.nodes.attributes.LookupCallableSlotInMRONode;
140-
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
141140
import com.oracle.graal.python.nodes.call.CallNode;
142141
import com.oracle.graal.python.nodes.call.special.CallTernaryMethodNode;
143142
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
@@ -837,6 +836,31 @@ public static long expectPointer(Object value) {
837836
return 0;
838837
}
839838

839+
/**
840+
* Transforms the given {@link TruffleString} to a string with native buffer and returns the
841+
* internal pointer object (which is guaranteed to answer
842+
* {@link InteropLibrary#isPointer(Object)} with {@code true}).
843+
*/
844+
private static Object truffleStringToNative(TruffleString value) {
845+
TruffleString nativeTName = TruffleString.AsNativeNode.getUncached().execute(value, (size) -> {
846+
// over-allocate by 1 byte and write a zero terminator
847+
long ptr = UNSAFE.allocateMemory(size + 1);
848+
UNSAFE.putByte(ptr + size, (byte) 0);
849+
return new NativePointer(ptr);
850+
}, TS_ENCODING, true, true);
851+
Object result = TruffleString.GetInternalNativePointerNode.getUncached().execute(nativeTName, TS_ENCODING);
852+
assert InteropLibrary.getUncached().isPointer(result);
853+
return result;
854+
}
855+
856+
private static Object capsuleNameToNative(Object name) {
857+
if (name instanceof TruffleString tname) {
858+
// The capsule's name may either be a native pointer or a TruffleString.
859+
return truffleStringToNative(tname);
860+
}
861+
return name;
862+
}
863+
840864
// {{start ctx funcs}}
841865
public int ctxTypeCheck(long bits, long typeBits) {
842866
increment(HPyJNIUpcall.HPyTypeCheck);
@@ -1099,13 +1123,14 @@ public long ctxCapsuleGet(long capsuleBits, int key, long namePtr) {
10991123
Object result;
11001124
switch (key) {
11011125
case CapsuleKey.Pointer -> {
1102-
if (!capsuleNameMatches(namePtr, coerceToPointer(pyCapsule.getName()))) {
1126+
if (!capsuleNameMatches(namePtr, coerceToPointer(capsuleNameToNative(pyCapsule.getName())))) {
11031127
return HPyRaiseNodeGen.getUncached().raiseIntWithoutFrame(context, 0, ValueError, GraalHPyCapsuleGet.INCORRECT_NAME);
11041128
}
11051129
result = pyCapsule.getPointer();
11061130
}
11071131
case CapsuleKey.Context -> result = pyCapsule.getContext();
1108-
case CapsuleKey.Name -> result = pyCapsule.getName();
1132+
// The capsule's name may either be a native pointer or a TruffleString.
1133+
case CapsuleKey.Name -> result = capsuleNameToNative(pyCapsule.getName());
11091134
case CapsuleKey.Destructor -> result = pyCapsule.getDestructor();
11101135
default -> throw CompilerDirectives.shouldNotReachHere("invalid key");
11111136
}

0 commit comments

Comments
 (0)