feat(coro): M7——try/finally、异常消息、自定义异常类、帧池复用
- 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 次循环池复用
This commit is contained in:
1 parent
8b9e88287a
commit
8ce08493e8
4 files changed
+191
-40
No files matched your search
+23
-20
@@ -4,35 +4,38 @@ import stdio
|
||||
import coro
|
||||
|
||||
|
||||
class MyError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
async def leaf(x: int) -> int:
|
||||
return x + 1
|
||||
|
||||
|
||||
async def boom(kind: int) -> int:
|
||||
if kind == 1:
|
||||
raise ValueError
|
||||
raise KeyError
|
||||
async def boom() -> int:
|
||||
raise MyError("boom msg")
|
||||
return 0
|
||||
|
||||
|
||||
async def task(kind: int) -> int:
|
||||
caught: int = 0
|
||||
async def task() -> int:
|
||||
fin: int = 0
|
||||
try:
|
||||
await boom(kind)
|
||||
except ValueError as e:
|
||||
caught = 1
|
||||
except KeyError:
|
||||
caught = 2
|
||||
return caught
|
||||
fin = 1
|
||||
await boom()
|
||||
finally:
|
||||
fin = fin + 1
|
||||
stdio.printf("finally ran, fin = %d\n", fin)
|
||||
return 99
|
||||
|
||||
|
||||
def main() -> t.CInt:
|
||||
a: coro.Coroutine = task(1)
|
||||
b: coro.Coroutine = task(2)
|
||||
n: t.CInt = coro.coro_gather(a, b)
|
||||
r1: t.CInt = coro.coro_run(a)
|
||||
r2: t.CInt = coro.coro_run(b)
|
||||
stdio.printf("gather = %d, r1 = %d, r2 = %d\n", n, r1, r2)
|
||||
coro.coro_destroy(a)
|
||||
coro.coro_destroy(b)
|
||||
n: t.CInt = 0
|
||||
for i in range(3):
|
||||
tt: coro.Coroutine = task()
|
||||
r: t.CInt = coro.coro_run(tt)
|
||||
e: t.CInt = coro.coro_exception(tt)
|
||||
m: t.CPtr = coro.coro_exception_msg(tt)
|
||||
stdio.printf("[%d] r = %d, exc = %d, msg = %s\n", i, r, e, m)
|
||||
coro.coro_destroy(tt)
|
||||
n = n + 1
|
||||
return 0
|
||||
Reference in new issue
Block a user