@@ -34,9 +34,9 @@ final class FiberCoroutine extends Coroutine
3434 */
3535 public function __get (string $ name ): mixed
3636 {
37- return match ($ name ) {
37+ return match ($ name ) {
3838 'result ' => $ this ->result ,
39- default => new UnexpectedValueException ("Property {$ name } does not exist or is not accessible " )
39+ default => new UnexpectedValueException ("Property {$ name } does not exist or is not accessible " )
4040 };
4141 }
4242
@@ -74,7 +74,6 @@ public function start(): mixed
7474 try {
7575 $ result = $ this ->fiber ->start (...$ this ->args );
7676 } catch (Throwable $ exception ) {
77- $ this ->result = $ exception ;
7877 $ this ->setState (Coroutine::STATE_DEAD );
7978 throw $ exception ;
8079 }
@@ -103,15 +102,9 @@ public function suspend(mixed $value = null): mixed
103102 }
104103
105104 $ this ->setState (Coroutine::STATE_WAITING );
106-
107- try {
108- $ result = $ this ->fiber ->suspend ($ value );
109- } catch (Throwable $ exception ) {
110- $ this ->setState (Coroutine::STATE_DEAD );
111- throw $ exception ;
112- }
113-
105+ $ result = $ this ->fiber ->suspend ($ value );
114106 $ this ->setState (Coroutine::STATE_RUNNING );
107+
115108 return $ result ;
116109 }
117110
@@ -123,6 +116,10 @@ public function suspend(mixed $value = null): mixed
123116 */
124117 public function resume (mixed $ value = null ): mixed
125118 {
119+ if ($ this ->state !== Coroutine::STATE_WAITING ) {
120+ throw new CoroutineStateException ('Coroutine::resume() called with coroutine not in running state ' );
121+ }
122+
126123 $ this ->setState (Coroutine::STATE_RUNNING );
127124
128125 try {
@@ -151,6 +148,10 @@ public function resume(mixed $value = null): mixed
151148 */
152149 public function throw (Throwable $ exception ): mixed
153150 {
151+ if ($ this ->state !== Coroutine::STATE_WAITING ) {
152+ throw new CoroutineStateException ('Coroutine::throw() called with coroutine not in running state ' );
153+ }
154+
154155 $ this ->setState (Coroutine::STATE_RUNNING );
155156
156157 try {
@@ -182,7 +183,7 @@ public function recycle(?Closure $callback = null): void
182183 new CoroutineStateException ("Unable to recycle a coroutine in use " );
183184 }
184185
185- $ this ->fiber = new Fiber ( $ callback) ;
186+ $ this ->callback = $ callback ;
186187 $ this ->recycleReset ();
187188 }
188189
0 commit comments