feat: async-generator 语义(async function*),yield/await 共存 + next 返回 Promise
- emit 识别 generator+async 组合,模块同时标记 is_generator/is_async - 新增 AsyncGeneratorState:帧快照 + 请求队列 + 当前请求 - next/return/throw 返回 Promise,请求入队逐条处理 - YIELD 让出经 promise unwrap 后交付(yield rejected promise 关闭生成器) - AWAIT 挂起保留当前请求,微任务恢复后继续执行 - %AsyncGeneratorPrototype% / %AsyncGeneratorFunction.prototype% 内建 - session GC / promote 支持异步生成器状态盒 - 普通 async 函数嵌套调用隔离外层异步生成器上下文
This commit is contained in:
@@ -686,9 +686,7 @@ impl Emitter {
|
||||
) -> Result<(), String> {
|
||||
match argument {
|
||||
oxide_parser::BindingPattern::BindingIdentifier(bi) => {
|
||||
out.push(ParamSpec::Rest {
|
||||
name: bi.name.to_string(),
|
||||
});
|
||||
out.push(ParamSpec::Rest { name: bi.name.to_string() });
|
||||
}
|
||||
_ => return Err("rest parameters with destructuring patterns not yet supported".into()),
|
||||
}
|
||||
@@ -733,7 +731,15 @@ impl Emitter {
|
||||
&self, param_specs: &[ParamSpec<'a>], body_stmts: &[Statement<'a>], parent_ctx: &CompileCtx,
|
||||
is_expression_body: bool, is_arrow: bool,
|
||||
) -> Result<IRFunction, String> {
|
||||
self.compile_function_body_with_flags(param_specs, body_stmts, parent_ctx, is_expression_body, is_arrow, false, false)
|
||||
self.compile_function_body_with_flags(
|
||||
param_specs,
|
||||
body_stmts,
|
||||
parent_ctx,
|
||||
is_expression_body,
|
||||
is_arrow,
|
||||
false,
|
||||
false,
|
||||
)
|
||||
}
|
||||
|
||||
/// 编译函数体并显式指定生成器标志(`function*` 走此入口)。
|
||||
@@ -743,6 +749,15 @@ impl Emitter {
|
||||
self.compile_function_body_with_flags(param_specs, body_stmts, parent_ctx, false, false, true, false)
|
||||
}
|
||||
|
||||
/// 编译异步生成器函数体(`async function*` / async 生成器表达式走此入口):
|
||||
/// 同时标记 `is_generator` 与 `is_async`,VM 调用时按异步生成器协议执行
|
||||
/// (next 返回 Promise,yield 挂起与 await 挂起共存)。
|
||||
pub(crate) fn compile_async_generator_body<'a>(
|
||||
&self, param_specs: &[ParamSpec<'a>], body_stmts: &[Statement<'a>], parent_ctx: &CompileCtx,
|
||||
) -> Result<IRFunction, String> {
|
||||
self.compile_function_body_with_flags(param_specs, body_stmts, parent_ctx, false, false, true, true)
|
||||
}
|
||||
|
||||
/// 编译异步函数体(`async function` / async 箭头走此入口):`is_async` 使
|
||||
/// `assemble_ir` 标记模块,VM 调用时按异步函数协议执行。
|
||||
pub(crate) fn compile_async_body<'a>(
|
||||
@@ -1110,10 +1125,8 @@ impl Emitter {
|
||||
ParamSpec::Rest { name } => {
|
||||
// rest 数组:从实参区收集固定形参之后的实参,绑定为普通变量。
|
||||
let reg = ctx.lookup(name)?;
|
||||
let fixed_count = param_specs
|
||||
.iter()
|
||||
.filter(|s| !matches!(s, ParamSpec::Rest { .. }))
|
||||
.count() as u32;
|
||||
let fixed_count =
|
||||
param_specs.iter().filter(|s| !matches!(s, ParamSpec::Rest { .. })).count() as u32;
|
||||
ctx.inst(Inst::create_rest_array(Operand::Reg(reg), fixed_count));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,7 +81,9 @@ impl Emitter {
|
||||
|
||||
let body_stmts: &[Statement] = if let Some(body) = &fe.body { &body.statements } else { &[] };
|
||||
|
||||
let mut sub_module = if fe.generator {
|
||||
let mut sub_module = if fe.generator && fe.r#async {
|
||||
self.compile_async_generator_body(¶m_names, body_stmts, ctx)?
|
||||
} else if fe.generator {
|
||||
self.compile_generator_body(¶m_names, body_stmts, ctx)?
|
||||
} else if fe.r#async {
|
||||
self.compile_async_body(¶m_names, body_stmts, ctx, false, false)?
|
||||
|
||||
@@ -36,7 +36,9 @@ impl Emitter {
|
||||
self.push_rest_param(&rest.rest.argument, &mut param_names)?;
|
||||
}
|
||||
let body_stmts: &[Statement] = if let Some(body) = &fd.body { &body.statements } else { &[] };
|
||||
let mut sub_module = if fd.generator {
|
||||
let mut sub_module = if fd.generator && fd.r#async {
|
||||
self.compile_async_generator_body(¶m_names, body_stmts, ctx)?
|
||||
} else if fd.generator {
|
||||
self.compile_generator_body(¶m_names, body_stmts, ctx)?
|
||||
} else if fd.r#async {
|
||||
self.compile_async_body(¶m_names, body_stmts, ctx, false, false)?
|
||||
|
||||
Reference in New Issue
Block a user