feat: async/await 语法支持(AWAIT 帧快照挂起/恢复 + 微任务驱动 + AsyncFunction intrinsics)

This commit is contained in:
爱特每羊队
2026-08-09 22:02:15 +08:00
parent dcbe3d9320
commit 0d9e6eabc9
28 changed files with 1055 additions and 28 deletions
+7 -3
View File
@@ -20,7 +20,7 @@ impl Inst {
// CALL 系:结果隐式写 reg 0,rd 是 callee 的 use
OpCode::CALL | OpCode::CALL_NATIVE | OpCode::CALL_SPREAD => Some(0),
// YIELD/YIELD_STAR:恢复时 next(v) 的 v / 委托完成值经 reg 0 交付(与 CALL 同协议)
OpCode::YIELD | OpCode::YIELD_STAR => Some(0),
OpCode::YIELD | OpCode::YIELD_STAR | OpCode::AWAIT => Some(0),
// GET_PROP 系:结果写 a/b 槽而非 rd
OpCode::GET_PROP | OpCode::IC_GET_PROP => reg_of(&self.a),
OpCode::GET_PROP_DYNAMIC => reg_of(&self.b),
@@ -228,8 +228,12 @@ impl Inst {
}
// HALT:隐式读 reg 0(顶层返回值)
OpCode::HALT => uses.push(0),
// RETURN/THROW/YIELD/YIELD_STAR:读 rd(None→0)
OpCode::RETURN | OpCode::THROW | OpCode::YIELD | OpCode::YIELD_STAR => push_operand(&mut uses, &self.rd),
// RETURN/THROW/YIELD/YIELD_STAR/AWAIT:读 rd(None→0)
OpCode::RETURN
| OpCode::THROW
| OpCode::YIELD
| OpCode::YIELD_STAR
| OpCode::AWAIT => push_operand(&mut uses, &self.rd),
// 加载族:a 槽是 Const/Imm 立即数,无寄存器 use
OpCode::LOAD_CONST
| OpCode::CREATE_CLOSURE
+6
View File
@@ -284,6 +284,12 @@ impl Inst {
Self::new(OpCode::SUSPEND_BODY, Operand::None, Operand::None, Operand::None)
}
/// `await` 让出:`rd` 为被等待的值(运行时 PromiseResolve 包装);异步帧挂起,
/// promise settle 后恢复,值经 reg 0 交付(与 YIELD 同协议)。
pub fn await_expr(src: Operand) -> Self {
Self::new(OpCode::AWAIT, src, Operand::None, Operand::None)
}
// ── 跳转族:label 放 b 槽,offset 计算是 lowering 职责 ──
/// 无条件跳转。label 放 b 槽,offset 由 lowering 回填。
+3
View File
@@ -52,6 +52,8 @@ pub struct IRFunction {
pub needs_home_object: bool,
/// 是否为生成器函数体(`function*`)。
pub is_generator: bool,
/// 是否为异步函数体(`async function` / async 箭头)。
pub is_async: bool,
pub captured_this_const_idx: u16,
pub function_name: Option<String>,
/// 函数 `length` 属性值:第一个带默认值/解构默认的形参之前的形参数(rest 不计)。
@@ -83,6 +85,7 @@ impl IRFunction {
is_derived_constructor: false,
needs_home_object: false,
is_generator: false,
is_async: false,
captured_this_const_idx: 0,
function_name: None,
function_length: 0,
+1
View File
@@ -90,6 +90,7 @@ pub fn lower(f: &IRFunction) -> Result<CompiledModule, String> {
is_derived_constructor: f.is_derived_constructor,
needs_home_object: f.needs_home_object,
is_generator: f.is_generator,
is_async: f.is_async,
upvalue_captures: f.upvalue_captures.clone(),
cells_needed: f.cells_needed,
flat_id: 0,