Skip to content

Commit 2944aac

Browse files
committed
add basic handling of PythonNativeVoidPtr in int.__lt__
1 parent b58003b commit 2944aac

File tree

1 file changed

+39
-0
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints

1 file changed

+39
-0
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/IntBuiltins.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@
9090
import com.oracle.truffle.api.dsl.NodeFactory;
9191
import com.oracle.truffle.api.dsl.Specialization;
9292
import com.oracle.truffle.api.dsl.TypeSystemReference;
93+
import com.oracle.truffle.api.interop.ForeignAccess;
94+
import com.oracle.truffle.api.interop.Message;
95+
import com.oracle.truffle.api.interop.UnsupportedMessageException;
96+
import com.oracle.truffle.api.nodes.Node;
9397
import com.oracle.truffle.api.profiles.BranchProfile;
9498
import com.oracle.truffle.api.profiles.ConditionProfile;
9599

@@ -1485,6 +1489,41 @@ boolean doDN(PythonNativeObject x, double y,
14851489
return fromNativeNode.execute(x) < y;
14861490
}
14871491

1492+
protected static Node createAsPointerNode() {
1493+
return Message.AS_POINTER.createNode();
1494+
}
1495+
1496+
protected static Node createIsPointerNode() {
1497+
return Message.IS_POINTER.createNode();
1498+
}
1499+
1500+
protected static Node createIsNullNode() {
1501+
return Message.IS_NULL.createNode();
1502+
}
1503+
1504+
@Specialization
1505+
boolean doVoidPtr(PythonNativeVoidPtr x, long y,
1506+
@Cached("createIsPointerNode()") Node isPointerNode,
1507+
@Cached("createAsPointerNode()") Node asPointerNode,
1508+
@Cached("createIsNullNode()") Node isNullNode) {
1509+
boolean isPointer = ForeignAccess.sendIsPointer(isPointerNode, x.object);
1510+
if (isPointer) {
1511+
try {
1512+
return ForeignAccess.sendAsPointer(asPointerNode, x.object) < y;
1513+
} catch (UnsupportedMessageException e) {
1514+
CompilerDirectives.transferToInterpreter();
1515+
throw new IllegalStateException("a void ptr should be a ptr");
1516+
}
1517+
} else if (ForeignAccess.sendIsNull(isNullNode, x.object)) {
1518+
return 0 < y;
1519+
} else if (y <= 0) {
1520+
return false;
1521+
} else {
1522+
CompilerDirectives.transferToInterpreter();
1523+
throw new IllegalStateException("cannot compare a wrapped void ptr that has no native allocation against arbitrary integers");
1524+
}
1525+
}
1526+
14881527
@SuppressWarnings("unused")
14891528
@Fallback
14901529
PNotImplemented doGeneric(Object a, Object b) {

0 commit comments

Comments
 (0)