40
40
*/
41
41
package com .oracle .graal .python .builtins .objects .asyncio ;
42
42
43
+ import static com .oracle .graal .python .nodes .SpecialMethodNames .J___AITER__ ;
44
+ import static com .oracle .graal .python .nodes .SpecialMethodNames .J___ANEXT__ ;
45
+
46
+ import java .util .List ;
47
+
43
48
import com .oracle .graal .python .builtins .Builtin ;
44
49
import com .oracle .graal .python .builtins .CoreFunctions ;
45
50
import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
46
51
import com .oracle .graal .python .builtins .PythonBuiltins ;
47
52
import com .oracle .graal .python .builtins .objects .PNone ;
48
53
import com .oracle .graal .python .builtins .objects .generator .GeneratorBuiltins ;
54
+ import com .oracle .graal .python .nodes .call .special .CallUnaryMethodNode ;
49
55
import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
50
56
import com .oracle .graal .python .nodes .function .PythonBuiltinNode ;
51
57
import com .oracle .graal .python .nodes .function .builtins .PythonBinaryBuiltinNode ;
52
58
import com .oracle .graal .python .nodes .function .builtins .PythonUnaryBuiltinNode ;
53
59
import com .oracle .graal .python .runtime .PAsyncGen ;
60
+ import com .oracle .graal .python .runtime .PythonContext ;
54
61
import com .oracle .truffle .api .dsl .Bind ;
55
62
import com .oracle .truffle .api .dsl .Cached ;
56
63
import com .oracle .truffle .api .dsl .GenerateNodeFactory ;
60
67
import com .oracle .truffle .api .nodes .Node ;
61
68
import com .oracle .truffle .api .profiles .InlinedConditionProfile ;
62
69
63
- import java .util .List ;
64
-
65
- import static com .oracle .graal .python .nodes .SpecialMethodNames .J___AITER__ ;
66
- import static com .oracle .graal .python .nodes .SpecialMethodNames .J___ANEXT__ ;
67
-
68
70
@ CoreFunctions (extendClasses = PythonBuiltinClassType .PAsyncGenerator )
69
71
public class AsyncGeneratorBuiltins extends PythonBuiltins {
72
+ private static void callHooks (VirtualFrame frame , PAsyncGen self , PythonContext .PythonThreadState state , CallUnaryMethodNode invokeFirstIter ) {
73
+ Object firstIter = state .getAsyncgenFirstIter ();
74
+ if (firstIter == null ) {
75
+ return ;
76
+ }
77
+ if (self .isHookCalled ()) {
78
+ return ;
79
+ }
80
+ self .setHookCalled (true );
81
+ invokeFirstIter .executeObject (frame , firstIter , self );
82
+ }
83
+
70
84
@ Override
71
85
protected List <? extends NodeFactory <? extends PythonBuiltinBaseNode >> getNodeFactories () {
72
86
return AsyncGeneratorBuiltinsFactory .getFactories ();
@@ -116,7 +130,9 @@ public boolean isRunning(PAsyncGen self) {
116
130
@ GenerateNodeFactory
117
131
public abstract static class ASend extends PythonBinaryBuiltinNode {
118
132
@ Specialization
119
- public Object aSend (PAsyncGen self , Object sent ) {
133
+ public Object aSend (VirtualFrame frame , PAsyncGen self , Object sent ,
134
+ @ Cached CallUnaryMethodNode callFirstIter ) {
135
+ callHooks (frame , self , getContext ().getThreadState (getLanguage ()), callFirstIter );
120
136
return factory ().createAsyncGeneratorASend (self , sent );
121
137
}
122
138
}
@@ -127,7 +143,9 @@ public abstract static class AThrow extends PythonBuiltinNode {
127
143
public abstract Object execute (VirtualFrame frame , PAsyncGen self , Object arg1 , Object arg2 , Object arg3 );
128
144
129
145
@ Specialization
130
- public Object athrow (PAsyncGen self , Object arg1 , Object arg2 , Object arg3 ) {
146
+ public Object athrow (VirtualFrame frame , PAsyncGen self , Object arg1 , Object arg2 , Object arg3 ,
147
+ @ Cached CallUnaryMethodNode callFirstIter ) {
148
+ callHooks (frame , self , getContext ().getThreadState (getLanguage ()), callFirstIter );
131
149
return factory ().createAsyncGeneratorAThrow (self , arg1 , arg2 , arg3 );
132
150
}
133
151
}
@@ -145,7 +163,9 @@ public Object aIter(PAsyncGen self) {
145
163
@ GenerateNodeFactory
146
164
public abstract static class ANext extends PythonUnaryBuiltinNode {
147
165
@ Specialization
148
- public Object aNext (PAsyncGen self ) {
166
+ public Object aNext (VirtualFrame frame , PAsyncGen self ,
167
+ @ Cached CallUnaryMethodNode callFirstIter ) {
168
+ callHooks (frame , self , getContext ().getThreadState (getLanguage ()), callFirstIter );
149
169
return factory ().createAsyncGeneratorASend (self , PNone .NONE );
150
170
}
151
171
}
@@ -154,7 +174,9 @@ public Object aNext(PAsyncGen self) {
154
174
@ GenerateNodeFactory
155
175
public abstract static class AClose extends PythonUnaryBuiltinNode {
156
176
@ Specialization
157
- public Object aClose (PAsyncGen self ) {
177
+ public Object aClose (VirtualFrame frame , PAsyncGen self ,
178
+ @ Cached CallUnaryMethodNode callFirstIter ) {
179
+ callHooks (frame , self , getContext ().getThreadState (getLanguage ()), callFirstIter );
158
180
return factory ().createAsyncGeneratorAThrow (self , null , PNone .NO_VALUE , PNone .NO_VALUE );
159
181
}
160
182
}
0 commit comments