28
28
import static com .oracle .graal .python .runtime .exception .PythonErrorType .KeyError ;
29
29
import static com .oracle .graal .python .runtime .exception .PythonErrorType .NameError ;
30
30
31
+ import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
31
32
import com .oracle .graal .python .builtins .objects .PNone ;
33
+ import com .oracle .graal .python .builtins .objects .common .HashingStorageNodes ;
34
+ import com .oracle .graal .python .builtins .objects .dict .PDict ;
32
35
import com .oracle .graal .python .builtins .objects .function .PArguments ;
36
+ import com .oracle .graal .python .builtins .objects .mappingproxy .PMappingproxy ;
37
+ import com .oracle .graal .python .builtins .objects .object .PythonObject ;
38
+ import com .oracle .graal .python .nodes .SpecialMethodNames ;
33
39
import com .oracle .graal .python .nodes .attributes .ReadAttributeFromObjectNode ;
40
+ import com .oracle .graal .python .nodes .expression .BinaryComparisonNode ;
34
41
import com .oracle .graal .python .nodes .expression .ExpressionNode ;
35
42
import com .oracle .graal .python .nodes .statement .StatementNode ;
36
43
import com .oracle .graal .python .nodes .subscript .GetItemNode ;
42
49
import com .oracle .truffle .api .dsl .Specialization ;
43
50
import com .oracle .truffle .api .frame .VirtualFrame ;
44
51
import com .oracle .truffle .api .nodes .NodeInfo ;
52
+ import com .oracle .truffle .api .profiles .BranchProfile ;
45
53
import com .oracle .truffle .api .profiles .ConditionProfile ;
46
54
47
55
@ NodeInfo (shortName = "read_global" )
@@ -52,6 +60,8 @@ public abstract class ReadGlobalOrBuiltinNode extends ExpressionNode implements
52
60
protected final String attributeId ;
53
61
protected final ConditionProfile isGlobalProfile = ConditionProfile .createBinaryProfile ();
54
62
protected final ConditionProfile isBuiltinProfile = ConditionProfile .createBinaryProfile ();
63
+ @ Child private HashingStorageNodes .GetItemNode getHashingItemNode ;
64
+ @ Child private GetItemNode readFromDictNode ;
55
65
56
66
protected ReadGlobalOrBuiltinNode (String attributeId ) {
57
67
this .attributeId = attributeId ;
@@ -72,19 +82,49 @@ protected Object readGlobal(VirtualFrame frame) {
72
82
return returnGlobalOrBuiltin (result );
73
83
}
74
84
85
+ protected BinaryComparisonNode createContainsNode () {
86
+ return BinaryComparisonNode .create (SpecialMethodNames .__CONTAINS__ , null , "in" );
87
+ }
88
+
75
89
@ Specialization (guards = "isInDict(frame)" , rewriteOn = PException .class )
76
90
protected Object readGlobalDict (VirtualFrame frame ,
77
- @ Cached ("create()" ) GetItemNode readFromDictNode ) {
78
- Object result = readFromDictNode .execute (PArguments .getGlobals (frame ), attributeId );
79
- return returnGlobalOrBuiltin (result );
91
+ @ Cached ("create()" ) BranchProfile isDict ,
92
+ @ Cached ("create()" ) BranchProfile isMappingproxy ) {
93
+ PythonObject globals = PArguments .getGlobals (frame );
94
+ if (globals instanceof PMappingproxy && globals .getPythonClass () == lookupClass (PythonBuiltinClassType .PMappingproxy )) {
95
+ isMappingproxy .enter ();
96
+ Object result = getGetItemNode ().execute (((PMappingproxy ) globals ).getDictStorage (), attributeId );
97
+ return returnGlobalOrBuiltin (result == null ? PNone .NO_VALUE : result );
98
+ } else if (globals instanceof PDict && globals .getPythonClass () == lookupClass (PythonBuiltinClassType .PDict )) {
99
+ isDict .enter ();
100
+ Object result = getGetItemNode ().execute (((PDict ) globals ).getDictStorage (), attributeId );
101
+ return returnGlobalOrBuiltin (result == null ? PNone .NO_VALUE : result );
102
+ } else {
103
+ return returnGlobalOrBuiltin (getReadFromDict ().execute (globals , attributeId ));
104
+ }
105
+ }
106
+
107
+ private GetItemNode getReadFromDict () {
108
+ if (readFromDictNode == null ) {
109
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
110
+ readFromDictNode = insert (GetItemNode .create ());
111
+ }
112
+ return readFromDictNode ;
113
+ }
114
+
115
+ private HashingStorageNodes .GetItemNode getGetItemNode () {
116
+ if (getHashingItemNode == null ) {
117
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
118
+ getHashingItemNode = insert (HashingStorageNodes .GetItemNode .create ());
119
+ }
120
+ return getHashingItemNode ;
80
121
}
81
122
82
123
@ Specialization (guards = "isInDict(frame)" )
83
124
protected Object readGlobalDictWithException (VirtualFrame frame ,
84
- @ Cached ("create()" ) GetItemNode readFromDictNode ,
85
125
@ Cached ("createBinaryProfile()" ) ConditionProfile errorProfile ) {
86
126
try {
87
- Object result = readFromDictNode .execute (PArguments .getGlobals (frame ), attributeId );
127
+ Object result = getReadFromDict () .execute (PArguments .getGlobals (frame ), attributeId );
88
128
return returnGlobalOrBuiltin (result );
89
129
} catch (PException e ) {
90
130
e .expect (KeyError , getCore (), errorProfile );
0 commit comments