Truly independent web browser
LibJS: Complete iterator helpers when closing an iterator throws
Timothy Flynn0e3694760938
diff --git a/Libraries/LibJS/Rust/src/runtime/iterator_helper.rs b/Libraries/LibJS/Rust/src/runtime/iterator_helper.rsindex 23142ee450d..49a4879a829 100644--- a/Libraries/LibJS/Rust/src/runtime/iterator_helper.rs+++ b/Libraries/LibJS/Rust/src/runtime/iterator_helper.rs@@ -210,17 +210,14 @@ impl IteratorHelper {let closure = self.closure.get();if completion.is_abrupt() {- // NB: This leaves the helper executing when closing its iterators throws, so that it throws on every later- // call.let abrupt_result = match closure.abrupt_closure(vm, completion) {- Some(abrupt_result) => abrupt_result?,- None => {- iterator_close_all(vm, &self.underlying_iterators(vm), completion).into_throw_completion_or()?- }+ Some(abrupt_result) => abrupt_result,+ None => iterator_close_all(vm, &self.underlying_iterators(vm), completion).into_throw_completion_or(),};+ // NB: The helper remains executing while closing its iterators, but completes even if closing throws.self.set_generator_state(GeneratorState::Completed);- return Ok(IterationResult::new(abrupt_result, true));+ return Ok(IterationResult::new(abrupt_result?, true));}let result_value = closure.call(vm, self);diff --git a/Tests/LibJS/Runtime/builtins/Iterator/Iterator.concat.js b/Tests/LibJS/Runtime/builtins/Iterator/Iterator.concat.jsindex 1d2d2d89913..be15aedd6c1 100644--- a/Tests/LibJS/Runtime/builtins/Iterator/Iterator.concat.js+++ b/Tests/LibJS/Runtime/builtins/Iterator/Iterator.concat.js@@ -74,6 +74,22 @@ describe("errors", () => {iterator.next();}).toThrowWithMessage(TypeError, "iterator.next() returned a non-object value");});++ test("iterator completes when the underlying return method throws", () => {+ function* source() {+ try {+ yield 1;+ } finally {+ throw new Error("close failed");+ }+ }++ const iterator = Iterator.concat(source());+ expect(iterator.next().done).toBeFalse();+ expect(() => iterator.return()).toThrowWithMessage(Error, "close failed");+ expect(iterator.next()).toEqual({ value: undefined, done: true });+ expect(iterator.return()).toEqual({ value: undefined, done: true });+ });});describe("normal behavior", () => {diff --git a/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.chunks.js b/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.chunks.jsindex cb270e8d0ef..88b1f15f377 100644--- a/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.chunks.js+++ b/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.chunks.js@@ -207,6 +207,8 @@ describe("errors", () => {const iterator = Iterator.prototype.chunks.call(source, 1);if (started) iterator.next();expect(() => iterator.return()).toThrowWithMessage(Error, "close failed");+ expect(iterator.next()).toEqual({ value: undefined, done: true });+ expect(iterator.return()).toEqual({ value: undefined, done: true });}});diff --git a/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.drop.js b/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.drop.jsindex 420d2c907ea..cd8176e3f02 100644--- a/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.drop.js+++ b/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.drop.js@@ -88,10 +88,13 @@ describe("errors", () => {}}- expect(() => {+ for (const started of [false, true]) {const iterator = new TestIterator().drop(1);- iterator.return();- }).toThrow(TestError);+ if (started) iterator.next();+ expect(() => iterator.return()).toThrow(TestError);+ expect(iterator.next()).toEqual({ value: undefined, done: true });+ expect(iterator.return()).toEqual({ value: undefined, done: true });+ }});});diff --git a/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.filter.js b/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.filter.jsindex 89f666f2eda..fc9659e8858 100644--- a/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.filter.js+++ b/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.filter.js@@ -74,10 +74,13 @@ describe("errors", () => {}}- expect(() => {+ for (const started of [false, true]) {const iterator = new TestIterator().filter(() => true);- iterator.return();- }).toThrow(TestError);+ if (started) iterator.next();+ expect(() => iterator.return()).toThrow(TestError);+ expect(iterator.next()).toEqual({ value: undefined, done: true });+ expect(iterator.return()).toEqual({ value: undefined, done: true });+ }});test("predicate function throws", () => {diff --git a/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.flatMap.js b/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.flatMap.jsindex 0d457ddb953..05273ab7e60 100644--- a/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.flatMap.js+++ b/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.flatMap.js@@ -74,10 +74,13 @@ describe("errors", () => {}}- expect(() => {- const iterator = new TestIterator().flatMap(() => 0);- iterator.return();- }).toThrow(TestError);+ for (const started of [false, true]) {+ const iterator = new TestIterator().flatMap(() => [0]);+ if (started) iterator.next();+ expect(() => iterator.return()).toThrow(TestError);+ expect(iterator.next()).toEqual({ value: undefined, done: true });+ expect(iterator.return()).toEqual({ value: undefined, done: true });+ }});test("mapper function throws", () => {diff --git a/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.map.js b/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.map.jsindex f9cc9327daa..3c0f0024cae 100644--- a/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.map.js+++ b/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.map.js@@ -74,10 +74,13 @@ describe("errors", () => {}}- expect(() => {+ for (const started of [false, true]) {const iterator = new TestIterator().map(() => 0);- iterator.return();- }).toThrow(TestError);+ if (started) iterator.next();+ expect(() => iterator.return()).toThrow(TestError);+ expect(iterator.next()).toEqual({ value: undefined, done: true });+ expect(iterator.return()).toEqual({ value: undefined, done: true });+ }});test("mapper function throws", () => {diff --git a/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.take.js b/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.take.jsindex bce5b1a070b..b04a483fca8 100644--- a/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.take.js+++ b/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.take.js@@ -88,10 +88,13 @@ describe("errors", () => {}}- expect(() => {+ for (const started of [false, true]) {const iterator = new TestIterator().take(1);- iterator.return();- }).toThrow(TestError);+ if (started) iterator.next();+ expect(() => iterator.return()).toThrow(TestError);+ expect(iterator.next()).toEqual({ value: undefined, done: true });+ expect(iterator.return()).toEqual({ value: undefined, done: true });+ }});});diff --git a/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.windows.js b/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.windows.jsindex f7860b6997f..918416ad363 100644--- a/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.windows.js+++ b/Tests/LibJS/Runtime/builtins/Iterator/Iterator.prototype.windows.js@@ -270,6 +270,8 @@ describe("errors", () => {const iterator = Iterator.prototype.windows.call(source, 1);if (started) iterator.next();expect(() => iterator.return()).toThrowWithMessage(Error, "close failed");+ expect(iterator.next()).toEqual({ value: undefined, done: true });+ expect(iterator.return()).toEqual({ value: undefined, done: true });}});diff --git a/Tests/LibJS/Runtime/builtins/Iterator/Iterator.zip.js b/Tests/LibJS/Runtime/builtins/Iterator/Iterator.zip.jsindex bf676b25cc7..86a59c5fe37 100644--- a/Tests/LibJS/Runtime/builtins/Iterator/Iterator.zip.js+++ b/Tests/LibJS/Runtime/builtins/Iterator/Iterator.zip.js@@ -74,6 +74,22 @@ describe("errors", () => {Iterator.zip([[0], [2, 3]], { mode: "strict" }).toArray();}).toThrowWithMessage(TypeError, "Not enough iterator results in 'strict' mode");});++ test("iterator completes when the underlying return method throws", () => {+ function* source() {+ try {+ yield 1;+ } finally {+ throw new Error("close failed");+ }+ }++ const iterator = Iterator.zip([source()]);+ expect(iterator.next().done).toBeFalse();+ expect(() => iterator.return()).toThrowWithMessage(Error, "close failed");+ expect(iterator.next()).toEqual({ value: undefined, done: true });+ expect(iterator.return()).toEqual({ value: undefined, done: true });+ });});describe("normal behavior", () => {diff --git a/Tests/LibJS/Runtime/builtins/Iterator/Iterator.zipKeyed.js b/Tests/LibJS/Runtime/builtins/Iterator/Iterator.zipKeyed.jsindex 3868fabde47..b67b83d6476 100644--- a/Tests/LibJS/Runtime/builtins/Iterator/Iterator.zipKeyed.js+++ b/Tests/LibJS/Runtime/builtins/Iterator/Iterator.zipKeyed.js@@ -74,6 +74,22 @@ describe("errors", () => {Iterator.zipKeyed({ a: [0], b: [2, 3] }, { mode: "strict" }).toArray();}).toThrowWithMessage(TypeError, "Not enough iterator results in 'strict' mode");});++ test("iterator completes when the underlying return method throws", () => {+ function* source() {+ try {+ yield 1;+ } finally {+ throw new Error("close failed");+ }+ }++ const iterator = Iterator.zipKeyed({ source: source() });+ expect(iterator.next().done).toBeFalse();+ expect(() => iterator.return()).toThrowWithMessage(Error, "close failed");+ expect(iterator.next()).toEqual({ value: undefined, done: true });+ expect(iterator.return()).toEqual({ value: undefined, done: true });+ });});describe("normal behavior", () => {