45
45
46
46
import com .oracle .graal .python .PythonLanguage ;
47
47
import com .oracle .graal .python .builtins .objects .PNone ;
48
+ import com .oracle .graal .python .builtins .objects .PythonAbstractObject ;
48
49
import com .oracle .graal .python .builtins .objects .cext .capi .CExtNodes .GetNativeNullNode ;
49
50
import com .oracle .graal .python .builtins .objects .cext .capi .CExtNodes .IsPointerNode ;
50
51
import com .oracle .graal .python .builtins .objects .cext .capi .CExtNodes .PCallCapiFunction ;
59
60
import com .oracle .graal .python .nodes .PNodeWithContext ;
60
61
import com .oracle .graal .python .nodes .object .GetClassNode ;
61
62
import com .oracle .graal .python .runtime .PythonContext ;
63
+ import com .oracle .graal .python .runtime .PythonContext .PythonThreadState ;
62
64
import com .oracle .graal .python .runtime .PythonOptions ;
63
65
import com .oracle .graal .python .runtime .exception .PException ;
64
66
import com .oracle .graal .python .runtime .object .PythonObjectFactory ;
83
85
import com .oracle .truffle .api .library .ExportMessage ;
84
86
import com .oracle .truffle .llvm .spi .NativeTypeLibrary ;
85
87
88
+ /**
89
+ * Emulates CPython's {@code PyThreadState} struct.
90
+ */
86
91
@ ExportLibrary (InteropLibrary .class )
87
92
@ ExportLibrary (NativeTypeLibrary .class )
88
93
public class PThreadState extends PythonNativeWrapper {
@@ -97,14 +102,10 @@ public class PThreadState extends PythonNativeWrapper {
97
102
public static final String RECURSION_DEPTH = "recursion_depth" ;
98
103
public static final String OVERFLOWED = "overflowed" ;
99
104
100
- private PDict dict ;
101
-
102
- public PDict getThreadStateDict () {
103
- return dict ;
104
- }
105
+ private final PythonThreadState threadState ;
105
106
106
- public void setThreadStateDict ( PDict dict ) {
107
- this .dict = dict ;
107
+ public PThreadState ( PythonThreadState threadState ) {
108
+ this .threadState = threadState ;
108
109
}
109
110
110
111
// READ
@@ -133,29 +134,20 @@ protected boolean isMemberReadable(String member) {
133
134
}
134
135
135
136
@ ExportMessage
136
- protected Object getMembers (@ SuppressWarnings ("unused" ) boolean includeInternal ,
137
- @ Exclusive @ Cached PythonObjectFactory factory ) {
138
- return factory .createList (new Object []{CUR_EXC_TYPE , CUR_EXC_VALUE , CUR_EXC_TRACEBACK , EXC_TYPE , EXC_VALUE , EXC_TRACEBACK , DICT , PREV , RECURSION_DEPTH , OVERFLOWED });
139
- }
140
-
141
- @ ExportMessage
142
- protected Object readMember (String member ,
143
- @ Exclusive @ Cached ThreadStateReadNode readNode ) {
144
- return readNode .execute (member );
137
+ protected Object getMembers (@ SuppressWarnings ("unused" ) boolean includeInternal ) {
138
+ return new PythonAbstractObject .Keys (new Object []{CUR_EXC_TYPE , CUR_EXC_VALUE , CUR_EXC_TRACEBACK , EXC_TYPE , EXC_VALUE , EXC_TRACEBACK , DICT , PREV , RECURSION_DEPTH , OVERFLOWED });
145
139
}
146
140
147
141
@ ImportStatic (PThreadState .class )
148
142
@ GenerateUncached
149
- abstract static class ThreadStateReadNode extends PNodeWithContext {
150
-
151
- public abstract Object execute (Object key );
143
+ @ ExportMessage
144
+ abstract static class ReadMember {
152
145
153
146
@ Specialization (guards = "eq(key, CUR_EXC_TYPE)" )
154
- Object doCurExcType (@ SuppressWarnings ("unused" ) String key ,
147
+ static Object doCurExcType (PThreadState receiver , @ SuppressWarnings ("unused" ) String key ,
155
148
@ Shared ("toSulong" ) @ Cached ToSulongNode toSulongNode ,
156
- @ Shared ("context" ) @ CachedContext (PythonLanguage .class ) PythonContext context ,
157
149
@ Shared ("getClassNode" ) @ Cached GetClassNode getClassNode ) {
158
- PException currentException = context .getCurrentException ();
150
+ PException currentException = receiver . threadState .getCurrentException ();
159
151
Object result = null ;
160
152
if (currentException != null ) {
161
153
result = getClassNode .execute (currentException .getUnreifiedException ());
@@ -164,10 +156,9 @@ Object doCurExcType(@SuppressWarnings("unused") String key,
164
156
}
165
157
166
158
@ Specialization (guards = "eq(key, CUR_EXC_VALUE)" )
167
- Object doCurExcValue (@ SuppressWarnings ("unused" ) String key ,
168
- @ Shared ("toSulong" ) @ Cached ToSulongNode toSulongNode ,
169
- @ Shared ("context" ) @ CachedContext (PythonLanguage .class ) PythonContext context ) {
170
- PException currentException = context .getCurrentException ();
159
+ static Object doCurExcValue (PThreadState receiver , @ SuppressWarnings ("unused" ) String key ,
160
+ @ Shared ("toSulong" ) @ Cached ToSulongNode toSulongNode ) {
161
+ PException currentException = receiver .threadState .getCurrentException ();
171
162
Object result = null ;
172
163
if (currentException != null ) {
173
164
result = currentException .getEscapedException ();
@@ -176,11 +167,10 @@ Object doCurExcValue(@SuppressWarnings("unused") String key,
176
167
}
177
168
178
169
@ Specialization (guards = "eq(key, CUR_EXC_TRACEBACK)" )
179
- Object doCurExcTraceback (@ SuppressWarnings ("unused" ) String key ,
170
+ static Object doCurExcTraceback (PThreadState receiver , @ SuppressWarnings ("unused" ) String key ,
180
171
@ Shared ("toSulong" ) @ Cached ToSulongNode toSulongNode ,
181
- @ Shared ("getTraceback" ) @ Cached GetTracebackNode getTracebackNode ,
182
- @ Shared ("context" ) @ CachedContext (PythonLanguage .class ) PythonContext context ) {
183
- PException currentException = context .getCurrentException ();
172
+ @ Shared ("getTraceback" ) @ Cached GetTracebackNode getTracebackNode ) {
173
+ PException currentException = receiver .threadState .getCurrentException ();
184
174
PTraceback result = null ;
185
175
if (currentException != null ) {
186
176
result = getTracebackNode .execute (currentException .getTraceback ());
@@ -189,23 +179,21 @@ Object doCurExcTraceback(@SuppressWarnings("unused") String key,
189
179
}
190
180
191
181
@ Specialization (guards = "eq(key, EXC_TYPE)" )
192
- Object doExcType (@ SuppressWarnings ("unused" ) String key ,
182
+ static Object doExcType (PThreadState receiver , @ SuppressWarnings ("unused" ) String key ,
193
183
@ Shared ("toSulong" ) @ Cached ToSulongNode toSulongNode ,
194
- @ Shared ("context" ) @ CachedContext (PythonLanguage .class ) PythonContext context ,
195
184
@ Shared ("getClassNode" ) @ Cached GetClassNode getClassNode ) {
196
- PException currentException = context .getCaughtException ();
185
+ PException caughtException = receiver . threadState .getCaughtException ();
197
186
Object result = null ;
198
- if (currentException != null ) {
199
- result = getClassNode .execute (currentException .getUnreifiedException ());
187
+ if (caughtException != null ) {
188
+ result = getClassNode .execute (caughtException .getUnreifiedException ());
200
189
}
201
190
return toSulongNode .execute (result != null ? result : PNone .NO_VALUE );
202
191
}
203
192
204
193
@ Specialization (guards = "eq(key, EXC_VALUE)" )
205
- Object doExcValue (@ SuppressWarnings ("unused" ) String key ,
206
- @ Shared ("toSulong" ) @ Cached ToSulongNode toSulongNode ,
207
- @ Shared ("context" ) @ CachedContext (PythonLanguage .class ) PythonContext context ) {
208
- PException currentException = context .getCaughtException ();
194
+ static Object doExcValue (PThreadState receiver , @ SuppressWarnings ("unused" ) String key ,
195
+ @ Shared ("toSulong" ) @ Cached ToSulongNode toSulongNode ) {
196
+ PException currentException = receiver .threadState .getCaughtException ();
209
197
Object result = null ;
210
198
if (currentException != null ) {
211
199
result = currentException .getEscapedException ();
@@ -214,11 +202,10 @@ Object doExcValue(@SuppressWarnings("unused") String key,
214
202
}
215
203
216
204
@ Specialization (guards = "eq(key, EXC_TRACEBACK)" )
217
- Object doExcTraceback (@ SuppressWarnings ("unused" ) String key ,
205
+ static Object doExcTraceback (PThreadState receiver , @ SuppressWarnings ("unused" ) String key ,
218
206
@ Shared ("toSulong" ) @ Cached ToSulongNode toSulongNode ,
219
- @ Shared ("getTraceback" ) @ Cached GetTracebackNode getTracebackNode ,
220
- @ Shared ("context" ) @ CachedContext (PythonLanguage .class ) PythonContext context ) {
221
- PException currentException = context .getCaughtException ();
207
+ @ Shared ("getTraceback" ) @ Cached GetTracebackNode getTracebackNode ) {
208
+ PException currentException = receiver .threadState .getCaughtException ();
222
209
PTraceback result = null ;
223
210
if (currentException != null ) {
224
211
result = getTracebackNode .execute (currentException .getTraceback ());
@@ -227,21 +214,20 @@ Object doExcTraceback(@SuppressWarnings("unused") String key,
227
214
}
228
215
229
216
@ Specialization (guards = "eq(key, DICT)" )
230
- Object doDict (@ SuppressWarnings ("unused" ) String key ,
217
+ static Object doDict (PThreadState receiver , @ SuppressWarnings ("unused" ) String key ,
231
218
@ Cached PythonObjectFactory factory ,
232
- @ Shared ("toSulong" ) @ Cached ToSulongNode toSulongNode ,
233
- @ Shared ("context" ) @ CachedContext (PythonLanguage .class ) PythonContext context ) {
234
- PThreadState customThreadState = context .getCustomThreadState ();
235
- PDict threadStateDict = customThreadState .getThreadStateDict ();
219
+ @ Shared ("toSulong" ) @ Cached ToSulongNode toSulongNode ) {
220
+ PDict threadStateDict = receiver .threadState .getDict ();
236
221
if (threadStateDict == null ) {
237
222
threadStateDict = factory .createDict ();
238
- customThreadState . setThreadStateDict (threadStateDict );
223
+ receiver . threadState . setDict (threadStateDict );
239
224
}
240
225
return toSulongNode .execute (threadStateDict != null ? threadStateDict : PNone .NO_VALUE );
241
226
}
242
227
243
228
@ Specialization (guards = "eq(key, PREV)" )
244
- Object doPrev (@ SuppressWarnings ("unused" ) String key ,
229
+ @ SuppressWarnings ("unused" )
230
+ static Object doPrev (PThreadState receiver , String key ,
245
231
@ Cached GetNativeNullNode getNativeNullNode ) {
246
232
return getNativeNullNode .execute (null );
247
233
}
@@ -256,14 +242,16 @@ public Object visitFrame(FrameInstance frameInstance) {
256
242
}
257
243
258
244
@ Specialization (guards = "eq(key, RECURSION_DEPTH)" )
259
- long doRecursionDepth (@ SuppressWarnings ("unused" ) String key ) {
245
+ @ SuppressWarnings ("unused" )
246
+ static long doRecursionDepth (PThreadState receiver , String key ) {
260
247
DepthCounter visitor = new DepthCounter ();
261
248
Truffle .getRuntime ().iterateFrames (visitor );
262
249
return visitor .depth ;
263
250
}
264
251
265
252
@ Specialization (guards = "eq(key, OVERFLOWED)" )
266
- long doOverflowed (@ SuppressWarnings ("unused" ) String key ) {
253
+ @ SuppressWarnings ("unused" )
254
+ static long doOverflowed (PThreadState receiver , String key ) {
267
255
return 0 ;
268
256
}
269
257
0 commit comments