Skip to content

Truly independent web browser

LibJS: Forward rejected async generator returns through yield*

Shannon Booth1a0a5fed7de5
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/generators.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/generators.rs
index bdd73e5ed5b..76863dc89d9 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/generators.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/generators.rs
@@ -153,17 +153,14 @@ pub(super) fn generate_yield_of_value(
received_completion_value.clone()
}
-/// Like generate_await but uses caller-provided completion registers.
-///
-/// Returns the received_completion_value on the normal path.
-/// Emits a Throw on the throw path.
-pub(super) fn generate_await_with_completions(
+/// Await `argument` and capture its completion without propagating a rejection.
+fn generate_await_completion(
generator: &mut Generator<'_>,
argument: &ScopedOperand,
received_completion: &ScopedOperand,
received_completion_type: &ScopedOperand,
received_completion_value: &ScopedOperand,
-) -> ScopedOperand {
+) {
let continuation = generator.new_label();
generator.emit(op::Await {
continuation_label: continuation,
@@ -178,6 +175,23 @@ pub(super) fn generate_await_with_completions(
value_dst: received_completion_value.operand(),
completion: received_completion.operand(),
});
+}
+
+/// Await `argument` using caller-provided completion registers, throwing on rejection.
+pub(super) fn generate_await_with_completions(
+ generator: &mut Generator<'_>,
+ argument: &ScopedOperand,
+ received_completion: &ScopedOperand,
+ received_completion_type: &ScopedOperand,
+ received_completion_value: &ScopedOperand,
+) -> ScopedOperand {
+ generate_await_completion(
+ generator,
+ argument,
+ received_completion,
+ received_completion_type,
+ received_completion_value,
+ );
let normal_block = generator.new_label();
let throw_block = generator.new_label();
@@ -711,7 +725,8 @@ fn generate_yield(
// Return path: Await(resumptionValue.[[Value]]).
generator.bind_reachable_label(return_block);
- generate_await_with_completions(
+ // NB: A rejection must reach the yield continuation so yield* can forward it to the delegated iterator.
+ generate_await_completion(
generator,
received_completion_value,
received_completion,
diff --git a/Tests/LibJS/Bytecode/expected/async-generator-yield-await.txt b/Tests/LibJS/Bytecode/expected/async-generator-yield-await.txt
index e0d7b3d2b23..3a029b15444 100644
--- a/Tests/LibJS/Bytecode/expected/async-generator-yield-await.txt
+++ b/Tests/LibJS/Bytecode/expected/async-generator-yield-await.txt
@@ -13,9 +13,9 @@ block0:
[ 14] End value:reg5
-f$f97cf73a async-generator-yield-await.js:4:5
- Registers: 15
- Blocks: 19
+f$2d76a1c1 async-generator-yield-await.js:4:5
+ Registers: 14
+ Blocks: 17
Constants:
[0] = Undefined
@@ -56,7 +56,7 @@ block8:
[ 59] Mov dst:reg6, src:reg0
[ 5c] GetCompletionFields type_dst:reg7, value_dst:reg8, completion:reg6
[ 60] StrictlyInequalsRhsInt32 arith_feedback:2, dst:reg10, lhs:reg7, rhs:4
- [ 69] JumpIf condition:reg10, true_target:block14, false_target:block9
+ [ 69] JumpIf condition:reg10, true_target:block12, false_target:block9
block9:
[ 73] Await continuation_label:block10, argument:reg8
@@ -64,36 +64,29 @@ block9:
block10:
[ 79] Mov dst:reg6, src:reg0
[ 7c] GetCompletionFields type_dst:reg7, value_dst:reg8, completion:reg6
- [ 80] StrictlyEqualsRhsInt32 arith_feedback:3, dst:reg11, lhs:reg7, rhs:1
+ [ 80] StrictlyEqualsRhsInt32 arith_feedback:3, dst:reg11, lhs:reg7, rhs:5
[ 89] JumpIf condition:reg11, true_target:block12, false_target:block11
block11:
- [ 93] Throw src:reg8
+ [ 93] SetCompletionType completion:reg6
+ [ 96] Jump target:block12
block12:
- [ 95] StrictlyEqualsRhsInt32 arith_feedback:4, dst:reg12, lhs:reg7, rhs:5
- [ 9e] JumpIf condition:reg12, true_target:block14, false_target:block13
+ [ 9b] Mov dst:reg6, src:reg0
+ [ 9e] GetCompletionFields type_dst:reg7, value_dst:reg8, completion:reg6
+ [ a2] StrictlyEqualsRhsInt32 arith_feedback:4, dst:reg12, lhs:reg7, rhs:1
+ [ ab] JumpIf condition:reg12, true_target:block16, false_target:block13
block13:
- [ a8] SetCompletionType completion:reg6
- [ ab] Jump target:block14
+ [ b5] StrictlyEqualsRhsInt32 arith_feedback:5, dst:reg13, lhs:reg7, rhs:5
+ [ be] JumpIf condition:reg13, true_target:block14, false_target:block15
block14:
- [ b0] Mov dst:reg6, src:reg0
- [ b3] GetCompletionFields type_dst:reg7, value_dst:reg8, completion:reg6
- [ b7] StrictlyEqualsRhsInt32 arith_feedback:5, dst:reg13, lhs:reg7, rhs:1
- [ c0] JumpIf condition:reg13, true_target:block18, false_target:block15
+ [ c8] Throw src:reg8
block15:
- [ ca] StrictlyEqualsRhsInt32 arith_feedback:6, dst:reg14, lhs:reg7, rhs:5
- [ d3] JumpIf condition:reg14, true_target:block16, false_target:block17
+ [ ca] Yield value:reg8
block16:
- [ dd] Throw src:reg8
-
-block17:
- [ df] Yield value:reg8
-
-block18:
- [ e6] Mov dst:reg5, src:reg8
- [ e9] Yield value:Undefined
+ [ d1] Mov dst:reg5, src:reg8
+ [ d4] Yield value:Undefined
diff --git a/Tests/LibJS/Bytecode/expected/async-generator-yield.txt b/Tests/LibJS/Bytecode/expected/async-generator-yield.txt
index afae7cb7b2a..902b3c51c08 100644
--- a/Tests/LibJS/Bytecode/expected/async-generator-yield.txt
+++ b/Tests/LibJS/Bytecode/expected/async-generator-yield.txt
@@ -13,9 +13,9 @@ block0:
[ 14] End value:reg5
-f$62153dd3 async-generator-yield.js:5:5
- Registers: 14
- Blocks: 16
+f$fb3e4c58 async-generator-yield.js:5:5
+ Registers: 13
+ Blocks: 14
Constants:
[0] = Undefined
@@ -42,7 +42,7 @@ block5:
[ 31] Mov dst:reg5, src:reg0
[ 34] GetCompletionFields type_dst:reg6, value_dst:reg7, completion:reg5
[ 38] StrictlyInequalsRhsInt32 arith_feedback:1, dst:reg9, lhs:reg6, rhs:4
- [ 41] JumpIf condition:reg9, true_target:block11, false_target:block6
+ [ 41] JumpIf condition:reg9, true_target:block9, false_target:block6
block6:
[ 4b] Await continuation_label:block7, argument:reg7
@@ -50,36 +50,29 @@ block6:
block7:
[ 51] Mov dst:reg5, src:reg0
[ 54] GetCompletionFields type_dst:reg6, value_dst:reg7, completion:reg5
- [ 58] StrictlyEqualsRhsInt32 arith_feedback:2, dst:reg10, lhs:reg6, rhs:1
+ [ 58] StrictlyEqualsRhsInt32 arith_feedback:2, dst:reg10, lhs:reg6, rhs:5
[ 61] JumpIf condition:reg10, true_target:block9, false_target:block8
block8:
- [ 6b] Throw src:reg7
+ [ 6b] SetCompletionType completion:reg5
+ [ 6e] Jump target:block9
block9:
- [ 6d] StrictlyEqualsRhsInt32 arith_feedback:3, dst:reg11, lhs:reg6, rhs:5
- [ 76] JumpIf condition:reg11, true_target:block11, false_target:block10
+ [ 73] Mov dst:reg5, src:reg0
+ [ 76] GetCompletionFields type_dst:reg6, value_dst:reg7, completion:reg5
+ [ 7a] StrictlyEqualsRhsInt32 arith_feedback:3, dst:reg11, lhs:reg6, rhs:1
+ [ 83] JumpIf condition:reg11, true_target:block13, false_target:block10
block10:
- [ 80] SetCompletionType completion:reg5
- [ 83] Jump target:block11
+ [ 8d] StrictlyEqualsRhsInt32 arith_feedback:4, dst:reg12, lhs:reg6, rhs:5
+ [ 96] JumpIf condition:reg12, true_target:block11, false_target:block12
block11:
- [ 88] Mov dst:reg5, src:reg0
- [ 8b] GetCompletionFields type_dst:reg6, value_dst:reg7, completion:reg5
- [ 8f] StrictlyEqualsRhsInt32 arith_feedback:4, dst:reg12, lhs:reg6, rhs:1
- [ 98] JumpIf condition:reg12, true_target:block15, false_target:block12
+ [ a0] Throw src:reg7
block12:
- [ a2] StrictlyEqualsRhsInt32 arith_feedback:5, dst:reg13, lhs:reg6, rhs:5
- [ ab] JumpIf condition:reg13, true_target:block13, false_target:block14
+ [ a2] Yield value:reg7
block13:
- [ b5] Throw src:reg7
-
-block14:
- [ b7] Yield value:reg7
-
-block15:
- [ be] Mov dst:reg5, src:reg7
- [ c1] Yield value:Undefined
+ [ a9] Mov dst:reg5, src:reg7
+ [ ac] Yield value:Undefined
diff --git a/Tests/LibJS/Runtime/regress/async-yield-star-rejected-return.js b/Tests/LibJS/Runtime/regress/async-yield-star-rejected-return.js
new file mode 100644
index 00000000000..759510041d3
--- /dev/null
+++ b/Tests/LibJS/Runtime/regress/async-yield-star-rejected-return.js
@@ -0,0 +1,174 @@
+describe("async yield* with a rejected return value", () => {
+ for (const method of ["next", "throw", "return"]) {
+ test(`forwards the rejection after yielding from ${method}()`, () => {
+ const reason = {};
+ const calls = [];
+ const delegate = {
+ [Symbol.asyncIterator]() {
+ return this;
+ },
+ next() {
+ calls.push("next");
+ return { value: "next", done: false };
+ },
+ throw(value) {
+ calls.push(["throw", value]);
+ return { value: "handled", done: value === reason };
+ },
+ return(value) {
+ calls.push(["return", value]);
+ return { value: "return", done: false };
+ },
+ };
+ async function* outer() {
+ const value = yield* delegate;
+ calls.push(["resumed", value]);
+ return "finished";
+ }
+
+ let result;
+ let rejection;
+ (async () => {
+ const iterator = outer();
+ await iterator.next();
+ if (method !== "next") {
+ await iterator[method]("continue");
+ }
+ return iterator.return(Promise.reject(reason));
+ })().then(
+ value => (result = value),
+ error => (rejection = error)
+ );
+ runQueuedPromiseJobs();
+
+ expect(rejection).toBeUndefined();
+ expect(result).toEqual({ value: "finished", done: true });
+ const expected = ["next"];
+ if (method !== "next") {
+ expected.push([method, "continue"]);
+ }
+ expected.push(["throw", reason], ["resumed", "handled"]);
+ expect(calls).toEqual(expected);
+ });
+ }
+
+ test("can yield again from the delegated throw() method", () => {
+ const reason = {};
+ const calls = [];
+ let nextCount = 0;
+ const delegate = {
+ [Symbol.asyncIterator]() {
+ return this;
+ },
+ next(value) {
+ calls.push(["next", value]);
+ return { value: ++nextCount, done: nextCount === 2 };
+ },
+ throw(value) {
+ calls.push(["throw", value]);
+ return { value: "recovered", done: false };
+ },
+ };
+ async function* outer() {
+ return yield* delegate;
+ }
+
+ let results;
+ let rejection;
+ (async () => {
+ const iterator = outer();
+ await iterator.next();
+ return [await iterator.return(Promise.reject(reason)), await iterator.next("continue")];
+ })().then(
+ value => (results = value),
+ error => (rejection = error)
+ );
+ runQueuedPromiseJobs();
+
+ expect(rejection).toBeUndefined();
+ expect(results).toEqual([
+ { value: "recovered", done: false },
+ { value: 2, done: true },
+ ]);
+ expect(calls).toEqual([
+ ["next", undefined],
+ ["throw", reason],
+ ["next", "continue"],
+ ]);
+ });
+
+ for (const cleanupThrows of [false, true]) {
+ test(`awaits cleanup when throw() is missing and cleanup ${cleanupThrows ? "throws" : "succeeds"}`, () => {
+ const reason = {};
+ const cleanupError = {};
+ const calls = [];
+ const delegate = {
+ [Symbol.asyncIterator]() {
+ return this;
+ },
+ next() {
+ return { value: 1, done: false };
+ },
+ async return() {
+ calls.push("cleanup started");
+ await Promise.resolve();
+ calls.push("cleanup finished");
+ if (cleanupThrows) {
+ throw cleanupError;
+ }
+ return { done: true };
+ },
+ };
+ async function* outer() {
+ yield* delegate;
+ }
+
+ let rejection;
+ (async () => {
+ const iterator = outer();
+ await iterator.next();
+ return iterator.return(Promise.reject(reason));
+ })().catch(error => {
+ calls.push("rejected");
+ rejection = error;
+ });
+ runQueuedPromiseJobs();
+
+ expect(calls).toEqual(["cleanup started", "cleanup finished", "rejected"]);
+ if (cleanupThrows) {
+ expect(rejection).toBe(cleanupError);
+ } else {
+ expect(rejection).toBeInstanceOf(TypeError);
+ }
+ });
+ }
+
+ test("a rejected return still throws into an ordinary async yield", () => {
+ const reason = {};
+ let caught;
+ async function* generator() {
+ try {
+ yield 1;
+ } catch (error) {
+ caught = error;
+ yield "recovered";
+ }
+ }
+
+ let result;
+ let rejection;
+ (async () => {
+ const iterator = generator();
+ await iterator.next();
+ return iterator.return(Promise.reject(reason));
+ })().then(
+ value => (result = value),
+ error => (rejection = error)
+ );
+ runQueuedPromiseJobs();
+
+ expect(rejection).toBeUndefined();
+ expect(caught).toBe(reason);
+ expect(result).toEqual({ value: "recovered", done: false });
+ });
+});