52
52
import com .oracle .truffle .api .CompilerDirectives ;
53
53
import com .oracle .truffle .api .dsl .Fallback ;
54
54
import com .oracle .truffle .api .dsl .Specialization ;
55
- import com .oracle .truffle .api .nodes .UnexpectedResultException ;
56
- import com .oracle .truffle .api .profiles .BranchProfile ;
57
55
58
56
/**
59
57
* Converts an arbitrary object to an index-sized integer (which is a Java {@code int}).
@@ -63,12 +61,14 @@ public abstract class CastToIndexNode extends PBaseNode {
63
61
private static final String ERROR_MESSAGE = "cannot fit 'int' into an index-sized integer" ;
64
62
65
63
@ Child private LookupAndCallUnaryNode callIndexNode ;
64
+ @ Child private CastToIndexNode recursiveNode ;
66
65
67
- private final BranchProfile errorProfile = BranchProfile .create ();
68
66
private final PythonErrorType errorType ;
67
+ private final boolean recursive ;
69
68
70
- public CastToIndexNode (PythonErrorType errorType ) {
69
+ public CastToIndexNode (PythonErrorType errorType , boolean recursive ) {
71
70
this .errorType = errorType ;
71
+ this .recursive = recursive ;
72
72
}
73
73
74
74
public abstract int execute (Object x );
@@ -119,23 +119,25 @@ int doPIntOvf(PInt x) {
119
119
120
120
@ Fallback
121
121
int doGeneric (Object x ) {
122
- try {
122
+ if ( recursive ) {
123
123
if (callIndexNode == null ) {
124
124
CompilerDirectives .transferToInterpreterAndInvalidate ();
125
125
callIndexNode = insert (LookupAndCallUnaryNode .create (__INDEX__ ));
126
126
}
127
- return callIndexNode .executeInt (x );
128
- } catch (UnexpectedResultException e ) {
129
- errorProfile .enter ();
130
- throw raise (TypeError , "__index__ returned non-int (type %p)" , e .getResult ());
127
+ if (recursiveNode == null ) {
128
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
129
+ recursiveNode = insert (CastToIndexNodeGen .create (errorType , false ));
130
+ }
131
+ return recursiveNode .execute (callIndexNode .executeObject (x ));
131
132
}
133
+ throw raise (TypeError , "__index__ returned non-int (type %p)" , x );
132
134
}
133
135
134
136
public static CastToIndexNode create () {
135
- return CastToIndexNodeGen .create (IndexError );
137
+ return CastToIndexNodeGen .create (IndexError , true );
136
138
}
137
139
138
140
public static CastToIndexNode createOverflow () {
139
- return CastToIndexNodeGen .create (OverflowError );
141
+ return CastToIndexNodeGen .create (OverflowError , true );
140
142
}
141
143
}
0 commit comments