Skip to content

Commit 267cb99

Browse files
committed
Implement '__str__' and '__repr__' for foreign objects.
1 parent 0cd6b24 commit 267cb99

File tree

1 file changed

+58
-5
lines changed

1 file changed

+58
-5
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/foreign/TruffleObjectBuiltins.java

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@
4444
import static com.oracle.graal.python.nodes.SpecialMethodNames.__MUL__;
4545
import static com.oracle.graal.python.nodes.SpecialMethodNames.__NEW__;
4646
import static com.oracle.graal.python.nodes.SpecialMethodNames.__RADD__;
47+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__REPR__;
4748
import static com.oracle.graal.python.nodes.SpecialMethodNames.__RFLOORDIV__;
4849
import static com.oracle.graal.python.nodes.SpecialMethodNames.__RMUL__;
4950
import static com.oracle.graal.python.nodes.SpecialMethodNames.__RSUB__;
5051
import static com.oracle.graal.python.nodes.SpecialMethodNames.__RTRUEDIV__;
5152
import static com.oracle.graal.python.nodes.SpecialMethodNames.__SETATTR__;
5253
import static com.oracle.graal.python.nodes.SpecialMethodNames.__SETITEM__;
54+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__STR__;
5355
import static com.oracle.graal.python.nodes.SpecialMethodNames.__SUB__;
5456
import static com.oracle.graal.python.nodes.SpecialMethodNames.__TRUEDIV__;
5557
import static com.oracle.graal.python.runtime.exception.PythonErrorType.AttributeError;
@@ -67,6 +69,7 @@
6769
import com.oracle.graal.python.builtins.objects.list.PList;
6870
import com.oracle.graal.python.nodes.PGuards;
6971
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
72+
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
7073
import com.oracle.graal.python.nodes.expression.BinaryArithmetic;
7174
import com.oracle.graal.python.nodes.expression.BinaryComparisonNode;
7275
import com.oracle.graal.python.nodes.expression.CastToBooleanNode;
@@ -912,17 +915,67 @@ protected Object doIt(TruffleObject object,
912915
@GenerateNodeFactory
913916
abstract static class IndexNode extends UnboxNode {
914917
@Specialization(guards = "isForeignObject(object)")
915-
protected Object doIt(TruffleObject object,
916-
@Cached("IS_BOXED.createNode()") Node isBoxedNode,
917-
@Cached("UNBOX.createNode()") Node unboxNode) {
918-
if (ForeignAccess.sendIsBoxed(isBoxedNode, object)) {
918+
protected Object doIt(TruffleObject object) {
919+
if (isBoxed(object)) {
919920
try {
920-
return ForeignAccess.sendUnbox(unboxNode, object);
921+
return unboxLeft(object);
921922
} catch (UnsupportedMessageException e) {
922923
throw new IllegalStateException("The object '%s' claims to be boxed, but does not support the UNBOX message");
923924
}
924925
}
925926
throw raiseIndexError();
926927
}
927928
}
929+
930+
@Builtin(name = __STR__, fixedNumOfArguments = 1)
931+
@GenerateNodeFactory
932+
abstract static class StrNode extends UnboxNode {
933+
@Child private LookupAndCallUnaryNode callStrNode;
934+
935+
@Specialization(guards = "isForeignObject(object)")
936+
protected Object doIt(TruffleObject object) {
937+
if (isBoxed(object)) {
938+
try {
939+
return getCallStrNode().executeObject(unboxLeft(object));
940+
} catch (UnsupportedMessageException e) {
941+
throw new IllegalStateException("The object '%s' claims to be boxed, but does not support the UNBOX message");
942+
}
943+
}
944+
throw raise(PythonErrorType.AttributeError);
945+
}
946+
947+
private LookupAndCallUnaryNode getCallStrNode() {
948+
if (callStrNode == null) {
949+
CompilerDirectives.transferToInterpreterAndInvalidate();
950+
callStrNode = insert(LookupAndCallUnaryNode.create(__STR__));
951+
}
952+
return callStrNode;
953+
}
954+
}
955+
956+
@Builtin(name = __REPR__, fixedNumOfArguments = 1)
957+
@GenerateNodeFactory
958+
abstract static class ReprNode extends UnboxNode {
959+
@Child private LookupAndCallUnaryNode callReprNode;
960+
961+
@Specialization(guards = "isForeignObject(object)")
962+
protected Object doIt(TruffleObject object) {
963+
if (isBoxed(object)) {
964+
try {
965+
return getCallReprNode().executeObject(unboxLeft(object));
966+
} catch (UnsupportedMessageException e) {
967+
throw new IllegalStateException("The object '%s' claims to be boxed, but does not support the UNBOX message");
968+
}
969+
}
970+
throw raise(PythonErrorType.AttributeError);
971+
}
972+
973+
private LookupAndCallUnaryNode getCallReprNode() {
974+
if (callReprNode == null) {
975+
CompilerDirectives.transferToInterpreterAndInvalidate();
976+
callReprNode = insert(LookupAndCallUnaryNode.create(__REPR__));
977+
}
978+
return callReprNode;
979+
}
980+
}
928981
}

0 commit comments

Comments
 (0)