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

This commit is contained in:
爱特每羊队
2026-08-09 20:00:16 +08:00
parent 9787208c69
commit c4e7080883
9 changed files with 543 additions and 54 deletions
+8 -2
View File
@@ -9,9 +9,15 @@ use oxide_parser::YieldExpression;
impl Emitter {
pub(crate) fn emit_yield_expression(&self, ye: &YieldExpression, ctx: &mut CompileCtx) -> Result<u32, String> {
// 未支持:`yield*` 委托迭代,后续按 IteratorYield 协议实现。
// `yield*` 委托:求值内层可迭代对象后发 YIELD_STAR,运行时转发 next/return/throw。
// 委托完成值(内层 done 的 value)经 reg 0 交付,与普通 `yield` 同协议。
if ye.delegate {
return Err("yield* not yet supported".into());
let arg = ye.argument.as_ref().ok_or_else(|| "yield* requires an argument".to_string())?;
let inner_reg = self.emit_expression(arg, ctx)?;
ctx.inst(Inst::yield_star(Operand::Reg(inner_reg)));
let result_reg = ctx.alloc_reg();
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(result_reg), Operand::None, Operand::None));
return Ok(result_reg);
}
// 求值被让出的值(`yield` 无参数时让出 undefined)。
let value_reg = if let Some(arg) = &ye.argument {