- For 内 await:range 迭代变换为 While(__f_idx_N/__f_len_N 帧槽),复用协程版 While 就地挂起/恢复;For 迭代目标进帧槽跨挂起存活;仅支持 range 1/2 参数 - coro_sleep(ms):帧加 __coro_wake_us(i64)唤醒槽,挂起后 coro_run 轮询 GetTickCount64,未到点 Sleep(1) 续轮询,到点自动恢复 - coro_destroy(帧):coro.coro_destroy 内联翻译为 free 释放帧内存 - coro.vp 追加 coro_sleep/coro_destroy 占位;CoroTest 用例覆盖 for+await/sleep/destroy(期望 coro result = 15)
39 lines
1.1 KiB
Plaintext
39 lines
1.1 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
|