Skip to content

Commit 087aae4

Browse files
committed
Support '__index__' method for foreign objects.
1 parent 9855dfc commit 087aae4

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GE__;
3838
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GT__;
3939
import static com.oracle.graal.python.nodes.SpecialMethodNames.__ITER__;
40+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INDEX__;
4041
import static com.oracle.graal.python.nodes.SpecialMethodNames.__LEN__;
4142
import static com.oracle.graal.python.nodes.SpecialMethodNames.__LE__;
4243
import static com.oracle.graal.python.nodes.SpecialMethodNames.__LT__;
@@ -906,4 +907,22 @@ protected Object doIt(TruffleObject object,
906907
}
907908
}
908909
}
910+
911+
@Builtin(name = __INDEX__, fixedNumOfArguments = 1)
912+
@GenerateNodeFactory
913+
abstract static class IndexNode extends UnboxNode {
914+
@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)) {
919+
try {
920+
return ForeignAccess.sendUnbox(unboxNode, object);
921+
} catch (UnsupportedMessageException e) {
922+
throw new IllegalStateException("The object '%s' claims to be boxed, but does not support the UNBOX message");
923+
}
924+
}
925+
throw raiseIndexError();
926+
}
927+
}
909928
}

0 commit comments

Comments
 (0)