54 lines
1.8 KiB
Plaintext
54 lines
1.8 KiB
Plaintext
import t, c
|
||
from stdint import *
|
||
import stdlib
|
||
|
||
|
||
@t.NoVTable
|
||
class Coroutine:
|
||
"""协程帧句柄(M1:__ptr__ 持有 create_<name>() 返回的帧指针)。
|
||
纯句柄语义:从不实例化/多态分派,编译器全拦截 coro.* 调用;
|
||
用户代码句柄统一用 t.CPtr。"""
|
||
__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):
|
||
# 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 槽读取。
|
||
# 注意:TPV(Viper)不支持裸 null,用 t.CPtr(0) 构造空指针(两编译器兼容)。
|
||
return t.CPtr(0)
|