|
59 | 59 | import java.util.LinkedList;
|
60 | 60 | import java.util.List;
|
61 | 61 |
|
62 |
| -import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyData; |
63 | 62 | import org.graalvm.nativeimage.ImageInfo;
|
64 | 63 |
|
65 | 64 | import com.oracle.graal.python.PythonLanguage;
|
|
86 | 85 | import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyContextFunctions.GraalHPyContextVarGet;
|
87 | 86 | import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyContextFunctions.HPyBinaryContextFunction;
|
88 | 87 | import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyContextFunctions.HPyTernaryContextFunction;
|
| 88 | +import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyData; |
89 | 89 | import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyDef;
|
90 | 90 | import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyHandle;
|
91 | 91 | import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyNativeContext;
|
|
137 | 137 | import com.oracle.graal.python.nodes.PGuards;
|
138 | 138 | import com.oracle.graal.python.nodes.PRaiseNode;
|
139 | 139 | import com.oracle.graal.python.nodes.attributes.LookupCallableSlotInMRONode;
|
140 |
| -import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode; |
141 | 140 | import com.oracle.graal.python.nodes.call.CallNode;
|
142 | 141 | import com.oracle.graal.python.nodes.call.special.CallTernaryMethodNode;
|
143 | 142 | import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
|
@@ -837,6 +836,31 @@ public static long expectPointer(Object value) {
|
837 | 836 | return 0;
|
838 | 837 | }
|
839 | 838 |
|
| 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 | + |
840 | 864 | // {{start ctx funcs}}
|
841 | 865 | public int ctxTypeCheck(long bits, long typeBits) {
|
842 | 866 | increment(HPyJNIUpcall.HPyTypeCheck);
|
@@ -1099,13 +1123,14 @@ public long ctxCapsuleGet(long capsuleBits, int key, long namePtr) {
|
1099 | 1123 | Object result;
|
1100 | 1124 | switch (key) {
|
1101 | 1125 | case CapsuleKey.Pointer -> {
|
1102 |
| - if (!capsuleNameMatches(namePtr, coerceToPointer(pyCapsule.getName()))) { |
| 1126 | + if (!capsuleNameMatches(namePtr, coerceToPointer(capsuleNameToNative(pyCapsule.getName())))) { |
1103 | 1127 | return HPyRaiseNodeGen.getUncached().raiseIntWithoutFrame(context, 0, ValueError, GraalHPyCapsuleGet.INCORRECT_NAME);
|
1104 | 1128 | }
|
1105 | 1129 | result = pyCapsule.getPointer();
|
1106 | 1130 | }
|
1107 | 1131 | 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()); |
1109 | 1134 | case CapsuleKey.Destructor -> result = pyCapsule.getDestructor();
|
1110 | 1135 | default -> throw CompilerDirectives.shouldNotReachHere("invalid key");
|
1111 | 1136 | }
|
|
0 commit comments