- 帧布局加固定 exc 槽(i32,0=无异常),协程内 raise 存异常码并终止 - await 子协程后检测异常并传播到父帧(父协程带异常终止,状态机短路) - return await x:await 值直接存父帧 ret 槽(挂起前),修复 child_ret 跨块 dominate 错误;挂起不再写 ret=0(避免覆盖返回值) - coro.coro_gather(帧...):并发推进全部协程到完成(含 coro_sleep 轮询), 返回完成数 - coro.vp 追加 coro_exception/coro_gather 占位 - CoroTest 验证:异常传播 exc=42;gather=2 r1=2 r2=3
47 lines
1.4 KiB
Plaintext
47 lines
1.4 KiB
Plaintext
import t, c
|
||
from stdint import *
|
||
import stdlib
|
||
|
||
|
||
@t.CVTable
|
||
class Coroutine:
|
||
"""协程帧句柄(M1:__ptr__ 持有 create_<name>() 返回的帧指针)"""
|
||
__ptr__: t.CPtr
|
||
|
||
|
||
def coro_resume(c: Coroutine) -> t.CInt:
|
||
# M1 占位:实际由编译器将 coro.coro_run 内联翻译为 resume_<name> 循环,
|
||
# 本函数不会被真正调用(仅保证模块可编译)。
|
||
return 0
|
||
|
||
|
||
def coro_result(c: Coroutine) -> t.CInt:
|
||
# M1 占位:coro_run 内联翻译直接从帧 ret 槽取结果。
|
||
return 0
|
||
|
||
|
||
def coro_run(c: Coroutine) -> t.CInt:
|
||
# M1 占位实现:编译器在调用点对 coro.coro_run 做内联翻译(resume 循环)。
|
||
s: t.CInt = 0
|
||
while s == 0:
|
||
s = coro_resume(c)
|
||
return coro_result(c)
|
||
|
||
def coro_sleep(ms: t.CInt) -> t.CInt:
|
||
# M3 占位:编译器将 await coro_sleep(ms) 内联翻译为帧 wake 槽挂起,
|
||
# 本函数不会被真正调用(仅保证模块可编译)。
|
||
return 0
|
||
|
||
|
||
def coro_destroy(c: Coroutine) -> t.CVoid:
|
||
# M3 占位:编译器将 coro.coro_destroy(帧) 内联翻译为 free。
|
||
return
|
||
|
||
def coro_exception(c: Coroutine) -> t.CInt:
|
||
# M4 占位:编译器将 coro.coro_exception(帧) 内联翻译为帧 exc 槽读取。
|
||
return 0
|
||
|
||
def coro_gather(a: Coroutine) -> t.CInt:
|
||
# M4 占位:编译器将 coro.coro_gather(帧...) 内联翻译为并发推进循环。
|
||
return 0
|