27
27
28
28
import static com .oracle .graal .python .runtime .exception .PythonErrorType .TypeError ;
29
29
30
+ import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
30
31
import com .oracle .graal .python .builtins .objects .bytes .PByteArray ;
31
32
import com .oracle .graal .python .builtins .objects .bytes .PBytes ;
32
33
import com .oracle .graal .python .builtins .objects .ints .PInt ;
35
36
import com .oracle .graal .python .nodes .call .special .LookupAndCallUnaryNode ;
36
37
import com .oracle .graal .python .nodes .expression .CastToBooleanNodeFactory .NotNodeGen ;
37
38
import com .oracle .graal .python .nodes .expression .CastToBooleanNodeFactory .YesNodeGen ;
39
+ import com .oracle .graal .python .nodes .object .GetClassNode ;
40
+ import com .oracle .truffle .api .CompilerDirectives ;
38
41
import com .oracle .truffle .api .dsl .Cached ;
39
42
import com .oracle .truffle .api .dsl .ImportStatic ;
40
43
import com .oracle .truffle .api .dsl .Specialization ;
43
46
import com .oracle .truffle .api .interop .TruffleObject ;
44
47
45
48
public abstract class CastToBooleanNode extends UnaryOpNode {
49
+ @ Child private GetClassNode getClassNode ;
46
50
47
51
public static CastToBooleanNode createIfTrueNode () {
48
52
return YesNodeGen .create (null );
@@ -67,6 +71,14 @@ public static CastToBooleanNode createIfFalseNode(PNode operand) {
67
71
68
72
public abstract boolean executeWith (Object value );
69
73
74
+ GetClassNode getGetClassNode () {
75
+ if (getClassNode == null ) {
76
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
77
+ getClassNode = insert (GetClassNode .create ());
78
+ }
79
+ return getClassNode ;
80
+ }
81
+
70
82
@ ImportStatic (Message .class )
71
83
public abstract static class YesNode extends CastToBooleanNode {
72
84
@@ -116,8 +128,10 @@ boolean doObject(Object object,
116
128
Object value = callBoolNode .executeObject (object );
117
129
if (value instanceof Boolean ) {
118
130
return (boolean ) value ;
131
+ } else if (value instanceof PInt && getGetClassNode ().execute (value ) == getCore ().lookupType (PythonBuiltinClassType .Boolean )) {
132
+ return ((PInt ) value ).isOne ();
119
133
} else {
120
- throw raise (TypeError , "__bool__ should return bool, returned %s " , object );
134
+ throw raise (TypeError , "__bool__ should return bool, returned %p " , value );
121
135
}
122
136
}
123
137
}
@@ -182,8 +196,10 @@ boolean doObject(Object object,
182
196
Object value = callBoolNode .executeObject (object );
183
197
if (value instanceof Boolean ) {
184
198
return !((boolean ) value );
199
+ } else if (value instanceof PInt && getGetClassNode ().execute (value ) == getCore ().lookupType (PythonBuiltinClassType .Boolean )) {
200
+ return ((PInt ) value ).isZero ();
185
201
} else {
186
- throw raise (TypeError , "__bool__ should return bool, returned %s " , object );
202
+ throw raise (TypeError , "__bool__ should return bool, returned %p " , value );
187
203
}
188
204
}
189
205
}
0 commit comments