- try/finally(无 except):正常/异常路径都执行 fbody,异常时 finally 后 重新传播(done+br exit);finally 内暂不支持 await - 异常消息:帧加 msg 槽(i8*,index 4),raise X(msg) 存消息,await 传播, coro_exception_msg 查询 - raise 支持自定义异常类(exception_registry 码),多 except 匹配子类码 - 帧池复用:per-type free-list 全局池,create 弹池/空则 malloc,coro_destroy 回池(帧头部存 next),避免反复 malloc/free - 修复 ast.walk 传 list 崩溃(finalbody/For body) - CoroTest 验证:finally 执行、MyError exc=100 msg=boom msg、3 次循环池复用
51 lines
1.5 KiB
Plaintext
51 lines
1.5 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
|
||
|
||
def coro_exception_msg(c: Coroutine) -> t.CPtr:
|
||
# M7 占位:编译器将 coro.coro_exception_msg(帧) 内联翻译为帧 msg 槽读取。
|
||
return null
|