Skip to content

Commit

Permalink
fix(es/compat): Fix async generator (#8881)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #8805
  • Loading branch information
kdy1 committed Apr 23, 2024
1 parent 16a3885 commit 063eabd
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 17 deletions.
Expand Up @@ -89,7 +89,7 @@ function _f3() {
} finally{
try {
if (_iteratorAbruptCompletion && _iterator.return != null) {
yield _iterator.return();
yield _await_async_generator(_iterator.return());
}
} finally{
if (_didIteratorError) {
Expand Down Expand Up @@ -124,7 +124,7 @@ function _f4() {
} finally{
try {
if (_iteratorAbruptCompletion && _iterator.return != null) {
yield _iterator.return();
yield _await_async_generator(_iterator.return());
}
} finally{
if (_didIteratorError) {
Expand Down Expand Up @@ -197,7 +197,7 @@ function _f6() {
} finally{
try {
if (_iteratorAbruptCompletion && _iterator.return != null) {
yield _iterator.return();
yield _await_async_generator(_iterator.return());
}
} finally{
if (_didIteratorError) {
Expand Down Expand Up @@ -234,7 +234,7 @@ function _f7() {
} finally{
try {
if (_iteratorAbruptCompletion && _iterator.return != null) {
yield _iterator.return();
yield _await_async_generator(_iterator.return());
}
} finally{
if (_didIteratorError) {
Expand Down
Expand Up @@ -266,7 +266,7 @@ function _f3() {
];
return [
4,
_iterator.return()
_await_async_generator(_iterator.return())
];
case 8:
_state.sent();
Expand Down Expand Up @@ -366,7 +366,7 @@ function _f4() {
];
return [
4,
_iterator.return()
_await_async_generator(_iterator.return())
];
case 8:
_state.sent();
Expand Down Expand Up @@ -569,7 +569,7 @@ function _f6() {
];
return [
4,
_iterator.return()
_await_async_generator(_iterator.return())
];
case 8:
_state.sent();
Expand Down Expand Up @@ -670,7 +670,7 @@ function _f7() {
];
return [
4,
_iterator.return()
_await_async_generator(_iterator.return())
];
case 8:
_state.sent();
Expand Down
31 changes: 22 additions & 9 deletions crates/swc_ecma_compat_es2017/src/async_to_generator.rs
Expand Up @@ -790,21 +790,34 @@ fn handle_await_for(stmt: &mut Stmt, is_async_generator: bool) {
alt: None,
});

let iterator_return = Box::new(Expr::Call(CallExpr {
span: DUMMY_SP,
callee: iterator
.clone()
.make_member(quote_ident!("return"))
.as_callee(),
args: vec![],
type_args: Default::default(),
}));

// yield _iterator.return();
// or
// yield _awaitAsyncGenerator(_iterator.return());
let yield_stmt = Stmt::Expr(ExprStmt {
span: DUMMY_SP,
expr: Box::new(Expr::Yield(YieldExpr {
span: DUMMY_SP,
delegate: false,
arg: Some(Box::new(Expr::Call(CallExpr {
span: DUMMY_SP,
callee: iterator
.clone()
.make_member(quote_ident!("return"))
.as_callee(),
args: Default::default(),
type_args: Default::default(),
}))),
arg: Some(if is_async_generator {
Box::new(Expr::Call(CallExpr {
span: DUMMY_SP,
callee: helper!(await_async_generator),
args: vec![iterator_return.as_arg()],
type_args: Default::default(),
}))
} else {
iterator_return
}),
})),
});

Expand Down
@@ -0,0 +1,20 @@
async function* foo() {
yield 1
}

async function* bar(inputs, returnValues) {
for await (const input of inputs) {
if (!returnValues) {
return
}
yield input
}
}

async function run() {
for await (const number of bar(foo(), true)) {
console.log(number)
}
}

run()
@@ -0,0 +1,20 @@
async function* foo() {
yield 1
}

async function* bar(inputs, returnValues) {
for await (const input of inputs) {
if (!returnValues) {
return
}
yield input
}
}

async function run() {
for await (const value of bar(foo(), false)) {
console.log(value)
}
}

run()

0 comments on commit 063eabd

Please sign in to comment.