4545
4646import com .oracle .graal .python .PythonLanguage ;
4747import com .oracle .graal .python .builtins .objects .PNone ;
48+ import com .oracle .graal .python .builtins .objects .PythonAbstractObject ;
4849import com .oracle .graal .python .builtins .objects .cext .capi .CExtNodes .GetNativeNullNode ;
4950import com .oracle .graal .python .builtins .objects .cext .capi .CExtNodes .IsPointerNode ;
5051import com .oracle .graal .python .builtins .objects .cext .capi .CExtNodes .PCallCapiFunction ;
5960import com .oracle .graal .python .nodes .PNodeWithContext ;
6061import com .oracle .graal .python .nodes .object .GetClassNode ;
6162import com .oracle .graal .python .runtime .PythonContext ;
63+ import com .oracle .graal .python .runtime .PythonContext .PythonThreadState ;
6264import com .oracle .graal .python .runtime .PythonOptions ;
6365import com .oracle .graal .python .runtime .exception .PException ;
6466import com .oracle .graal .python .runtime .object .PythonObjectFactory ;
8385import com .oracle .truffle .api .library .ExportMessage ;
8486import com .oracle .truffle .llvm .spi .NativeTypeLibrary ;
8587
88+ /**
89+ * Emulates CPython's {@code PyThreadState} struct.
90+ */
8691@ ExportLibrary (InteropLibrary .class )
8792@ ExportLibrary (NativeTypeLibrary .class )
8893public class PThreadState extends PythonNativeWrapper {
@@ -97,14 +102,10 @@ public class PThreadState extends PythonNativeWrapper {
97102 public static final String RECURSION_DEPTH = "recursion_depth" ;
98103 public static final String OVERFLOWED = "overflowed" ;
99104
100- private PDict dict ;
101-
102- public PDict getThreadStateDict () {
103- return dict ;
104- }
105+ private final PythonThreadState threadState ;
105106
106- public void setThreadStateDict ( PDict dict ) {
107- this .dict = dict ;
107+ public PThreadState ( PythonThreadState threadState ) {
108+ this .threadState = threadState ;
108109 }
109110
110111 // READ
@@ -133,29 +134,20 @@ protected boolean isMemberReadable(String member) {
133134 }
134135
135136 @ 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 });
145139 }
146140
147141 @ ImportStatic (PThreadState .class )
148142 @ GenerateUncached
149- abstract static class ThreadStateReadNode extends PNodeWithContext {
150-
151- public abstract Object execute (Object key );
143+ @ ExportMessage
144+ abstract static class ReadMember {
152145
153146 @ Specialization (guards = "eq(key, CUR_EXC_TYPE)" )
154- Object doCurExcType (@ SuppressWarnings ("unused" ) String key ,
147+ static Object doCurExcType (PThreadState receiver , @ SuppressWarnings ("unused" ) String key ,
155148 @ Shared ("toSulong" ) @ Cached ToSulongNode toSulongNode ,
156- @ Shared ("context" ) @ CachedContext (PythonLanguage .class ) PythonContext context ,
157149 @ Shared ("getClassNode" ) @ Cached GetClassNode getClassNode ) {
158- PException currentException = context .getCurrentException ();
150+ PException currentException = receiver . threadState .getCurrentException ();
159151 Object result = null ;
160152 if (currentException != null ) {
161153 result = getClassNode .execute (currentException .getUnreifiedException ());
@@ -164,10 +156,9 @@ Object doCurExcType(@SuppressWarnings("unused") String key,
164156 }
165157
166158 @ 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 ();
171162 Object result = null ;
172163 if (currentException != null ) {
173164 result = currentException .getEscapedException ();
@@ -176,11 +167,10 @@ Object doCurExcValue(@SuppressWarnings("unused") String key,
176167 }
177168
178169 @ Specialization (guards = "eq(key, CUR_EXC_TRACEBACK)" )
179- Object doCurExcTraceback (@ SuppressWarnings ("unused" ) String key ,
170+ static Object doCurExcTraceback (PThreadState receiver , @ SuppressWarnings ("unused" ) String key ,
180171 @ 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 ();
184174 PTraceback result = null ;
185175 if (currentException != null ) {
186176 result = getTracebackNode .execute (currentException .getTraceback ());
@@ -189,23 +179,21 @@ Object doCurExcTraceback(@SuppressWarnings("unused") String key,
189179 }
190180
191181 @ Specialization (guards = "eq(key, EXC_TYPE)" )
192- Object doExcType (@ SuppressWarnings ("unused" ) String key ,
182+ static Object doExcType (PThreadState receiver , @ SuppressWarnings ("unused" ) String key ,
193183 @ Shared ("toSulong" ) @ Cached ToSulongNode toSulongNode ,
194- @ Shared ("context" ) @ CachedContext (PythonLanguage .class ) PythonContext context ,
195184 @ Shared ("getClassNode" ) @ Cached GetClassNode getClassNode ) {
196- PException currentException = context .getCaughtException ();
185+ PException caughtException = receiver . threadState .getCaughtException ();
197186 Object result = null ;
198- if (currentException != null ) {
199- result = getClassNode .execute (currentException .getUnreifiedException ());
187+ if (caughtException != null ) {
188+ result = getClassNode .execute (caughtException .getUnreifiedException ());
200189 }
201190 return toSulongNode .execute (result != null ? result : PNone .NO_VALUE );
202191 }
203192
204193 @ 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 ();
209197 Object result = null ;
210198 if (currentException != null ) {
211199 result = currentException .getEscapedException ();
@@ -214,11 +202,10 @@ Object doExcValue(@SuppressWarnings("unused") String key,
214202 }
215203
216204 @ Specialization (guards = "eq(key, EXC_TRACEBACK)" )
217- Object doExcTraceback (@ SuppressWarnings ("unused" ) String key ,
205+ static Object doExcTraceback (PThreadState receiver , @ SuppressWarnings ("unused" ) String key ,
218206 @ 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 ();
222209 PTraceback result = null ;
223210 if (currentException != null ) {
224211 result = getTracebackNode .execute (currentException .getTraceback ());
@@ -227,21 +214,20 @@ Object doExcTraceback(@SuppressWarnings("unused") String key,
227214 }
228215
229216 @ Specialization (guards = "eq(key, DICT)" )
230- Object doDict (@ SuppressWarnings ("unused" ) String key ,
217+ static Object doDict (PThreadState receiver , @ SuppressWarnings ("unused" ) String key ,
231218 @ 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 ();
236221 if (threadStateDict == null ) {
237222 threadStateDict = factory .createDict ();
238- customThreadState . setThreadStateDict (threadStateDict );
223+ receiver . threadState . setDict (threadStateDict );
239224 }
240225 return toSulongNode .execute (threadStateDict != null ? threadStateDict : PNone .NO_VALUE );
241226 }
242227
243228 @ Specialization (guards = "eq(key, PREV)" )
244- Object doPrev (@ SuppressWarnings ("unused" ) String key ,
229+ @ SuppressWarnings ("unused" )
230+ static Object doPrev (PThreadState receiver , String key ,
245231 @ Cached GetNativeNullNode getNativeNullNode ) {
246232 return getNativeNullNode .execute (null );
247233 }
@@ -256,14 +242,16 @@ public Object visitFrame(FrameInstance frameInstance) {
256242 }
257243
258244 @ Specialization (guards = "eq(key, RECURSION_DEPTH)" )
259- long doRecursionDepth (@ SuppressWarnings ("unused" ) String key ) {
245+ @ SuppressWarnings ("unused" )
246+ static long doRecursionDepth (PThreadState receiver , String key ) {
260247 DepthCounter visitor = new DepthCounter ();
261248 Truffle .getRuntime ().iterateFrames (visitor );
262249 return visitor .depth ;
263250 }
264251
265252 @ Specialization (guards = "eq(key, OVERFLOWED)" )
266- long doOverflowed (@ SuppressWarnings ("unused" ) String key ) {
253+ @ SuppressWarnings ("unused" )
254+ static long doOverflowed (PThreadState receiver , String key ) {
267255 return 0 ;
268256 }
269257
0 commit comments