feat: 实现 yield* 委托语法(内层迭代器 next/return/throw 转发)

This commit is contained in:
2026-08-09 20:00:16 +08:00
parent 65d96a8a08
commit 464712f7ba
9 changed files with 543 additions and 54 deletions
+4 -4
View File
@@ -19,8 +19,8 @@ impl Inst {
match self.op {
// CALL 系:结果隐式写 reg 0,rd 是 callee 的 use
OpCode::CALL | OpCode::CALL_NATIVE | OpCode::CALL_SPREAD => Some(0),
// YIELD:恢复时 next(v) 的 v 经 reg 0 交付(与 CALL 同协议)
OpCode::YIELD => Some(0),
// YIELD/YIELD_STAR:恢复时 next(v) 的 v / 委托完成值经 reg 0 交付(与 CALL 同协议)
OpCode::YIELD | OpCode::YIELD_STAR => 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,8 @@ impl Inst {
}
// HALT:隐式读 reg 0(顶层返回值)
OpCode::HALT => uses.push(0),
// RETURN/THROW/YIELD:读 rd(None→0)
OpCode::RETURN | OpCode::THROW | OpCode::YIELD => push_operand(&mut uses, &self.rd),
// RETURN/THROW/YIELD/YIELD_STAR:读 rd(None→0)
OpCode::RETURN | OpCode::THROW | OpCode::YIELD | OpCode::YIELD_STAR => push_operand(&mut uses, &self.rd),
// 加载族:a 槽是 Const/Imm 立即数,无寄存器 use
OpCode::LOAD_CONST
| OpCode::CREATE_CLOSURE
+6
View File
@@ -273,6 +273,12 @@ impl Inst {
Self::new(OpCode::YIELD, src, Operand::None, Operand::None)
}
/// `yield*` 委托:`rd` 为内层可迭代对象。运行时取迭代器并转发 next/return/throw,
/// 委托完成值(内层 done 的 value)经 reg 0 交付,与 `yield_value` 同协议。
pub fn yield_star(src: Operand) -> Self {
Self::new(OpCode::YIELD_STAR, src, Operand::None, Operand::None)
}
/// 生成器 body 起点标记:调用时参数初始化完成后挂起于此,首次 `next()` 继续。
pub fn suspend_body() -> Self {
Self::new(OpCode::SUSPEND_BODY, Operand::None, Operand::None, Operand::None)