1919use Ripple \Runtime \Exception \CoroutineStateException ;
2020use Ripple \Runtime \Exception \TerminateException ;
2121use Ripple \Coroutine ;
22- use Ripple \Runtime \Scheduler \ControlResult ;
22+ use Ripple \Runtime \Scheduler \Outcome ;
2323use Ripple \Runtime \Support \Display ;
2424use Ripple \Runtime \Support \Stdin ;
2525use SplObjectStorage ;
@@ -51,9 +51,9 @@ final class Scheduler
5151
5252 /**
5353 * 异常控制结果队列
54- * @var ControlResult []
54+ * @var Outcome []
5555 */
56- private static array $ excepts = [];
56+ private static array $ unresolvedErrors = [];
5757
5858 /**
5959 * 下一个事件循环中需要执行的回调队列
@@ -164,25 +164,29 @@ public static function dispatcher(): void
164164 }
165165 }
166166
167- while ($ report = array_shift (self ::$ excepts )) {
168- if ($ report ->isResolve ()) {
167+ $ unresolve = false ;
168+ while ($ report = array_shift (self ::$ unresolvedErrors )) {
169+ if ($ report ->isResolved ()) {
169170 continue ;
170171 }
171172
172- $ previous = $ report ->exception ();
173+ $ unresolve = true ;
174+ $ exception = $ report ->exception ();
173175
174- Stdin::println ("When self :: {$ report ->action ()} is executed, the internal Coroutine throws an unresolved exception: " );
175- Stdin::print (Display::exception ($ previous ));
176+ Stdin::println ("When Scheduler :: {$ report ->action ()} is executed, the internal Coroutine throws an unresolved exception: " );
177+ Stdin::print (Display::exception ($ exception ));
176178
177- Stdin::println (str_repeat ('- ' , 60 ));
178-
179- Stdin::println ("An uncaught exception occurs within the coroutine: " );
180- Stdin::print (Display::traces ($ report ->coroutine ()->debugTrace ()));
179+ if ($ coroutine = $ report ->coroutine ()) {
180+ Stdin::println (str_repeat ('- ' , 60 ));
181+ Stdin::println ("An uncaught exception occurs within the coroutine: " );
182+ Stdin::print (Display::traces ($ coroutine ->debugTrace ()));
183+ }
181184
182185 Stdin::println (str_repeat ('- ' , 60 ));
183-
184- Stdin::print (Display::trace ($ report ->debugTrace ()));
186+ Stdin::print (Display::trace ($ report ->trace ()));
185187 }
188+
189+ $ unresolve && exit (1 );
186190 }
187191
188192 /**
@@ -227,12 +231,12 @@ public static function remove(object $key): void
227231
228232 /**
229233 * 报告异常控制结果
230- * @param ControlResult $controlException 异常控制结果
231- * @return ControlResult 返回传入的异常控制结果
234+ * @param Outcome $controlException 异常控制结果
235+ * @return Outcome 返回传入的异常控制结果
232236 */
233- public static function reportException ( ControlResult $ controlException ): ControlResult
237+ public static function auditFailure ( Outcome $ controlException ): Outcome
234238 {
235- self ::$ excepts [] = $ controlException ;
239+ self ::$ unresolvedErrors [] = $ controlException ;
236240 return $ controlException ;
237241 }
238242
@@ -286,7 +290,7 @@ public static function start(Coroutine $coroutine): void
286290 try {
287291 $ coroutine ->start ();
288292 } catch (Throwable $ exception ) {
289- new ControlResult ('start ' , false , $ coroutine , $ exception );
293+ new Outcome ('start ' , false , $ coroutine , $ exception );
290294 } finally {
291295 if ($ coroutine ->isTerminated ()) {
292296 Scheduler::remove ($ coroutine ->key ());
@@ -299,14 +303,14 @@ public static function start(Coroutine $coroutine): void
299303 * 恢复协程
300304 * @param Coroutine $coroutine 要恢复的协程
301305 * @param ?mixed $value 恢复时传入的值
302- * @return ControlResult 控制结果, 包含恢复操作的结果和异常信息
306+ * @return Outcome 控制结果, 包含恢复操作的结果和异常信息
303307 */
304- public static function resume (Coroutine $ coroutine , mixed $ value = null ): ControlResult
308+ public static function resume (Coroutine $ coroutine , mixed $ value = null ): Outcome
305309 {
306310 try {
307- return new ControlResult ('resume ' , $ coroutine ->resume ($ value ), $ coroutine );
311+ return new Outcome ('resume ' , $ coroutine ->resume ($ value ), $ coroutine );
308312 } catch (Throwable $ exception ) {
309- return new ControlResult ('resume ' , null , $ coroutine , $ exception );
313+ return new Outcome ('resume ' , null , $ coroutine , $ exception );
310314 } finally {
311315 if ($ coroutine ->isTerminated ()) {
312316 Scheduler::remove ($ coroutine ->key ());
@@ -319,14 +323,14 @@ public static function resume(Coroutine $coroutine, mixed $value = null): Contro
319323 * 向协程抛出异常
320324 * @param Coroutine $coroutine 要抛出异常的协程
321325 * @param Throwable $exception 要抛出的异常对象
322- * @return ControlResult 控制结果, 包含异常抛出操作的结果和异常信息
326+ * @return Outcome 控制结果, 包含异常抛出操作的结果和异常信息
323327 */
324- public static function throw (Coroutine $ coroutine , Throwable $ exception ): ControlResult
328+ public static function throw (Coroutine $ coroutine , Throwable $ exception ): Outcome
325329 {
326330 try {
327- return new ControlResult ('throw ' , $ coroutine ->throw ($ exception ), $ coroutine );
331+ return new Outcome ('throw ' , $ coroutine ->throw ($ exception ), $ coroutine );
328332 } catch (Throwable $ exception ) {
329- return new ControlResult ('resume ' , null , $ coroutine , $ exception );
333+ return new Outcome ('resume ' , null , $ coroutine , $ exception );
330334 } finally {
331335 if ($ coroutine ->isTerminated ()) {
332336 Scheduler::remove ($ coroutine ->key ());
@@ -338,9 +342,9 @@ public static function throw(Coroutine $coroutine, Throwable $exception): Contro
338342 /**
339343 * 终止协程
340344 * @param Coroutine $coroutine 要终止的协程
341- * @return ControlResult 控制结果, 包含终止操作的结果
345+ * @return Outcome 控制结果, 包含终止操作的结果
342346 */
343- public static function terminate (Coroutine $ coroutine ): ControlResult
347+ public static function terminate (Coroutine $ coroutine ): Outcome
344348 {
345349 if ($ coroutine ->state () === Coroutine::STATE_RUNNING ) {
346350 $ coroutine ->onState (
@@ -349,7 +353,7 @@ public static function terminate(Coroutine $coroutine): ControlResult
349353 prepare: true
350354 );
351355
352- return new ControlResult ('terminate ' , null , $ coroutine );
356+ return new Outcome ('terminate ' , null , $ coroutine );
353357 }
354358
355359 return self ::throw ($ coroutine , new TerminateException ());
0 commit comments