Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/compat): Fix async generator #8881

Merged
merged 12 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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());
Copy link
Member

@Austaras Austaras Apr 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how this actually work, but judging from babel code

yield _iterator.return() // normal async function
yield _awaitAsyncGenerator(_iterator.return()) // async generator function

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
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
@@ -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()
Loading