Skip to content

Commit

Permalink
Throw if iterator.return() does not fulfill with an object
Browse files Browse the repository at this point in the history
  • Loading branch information
MattiasBuelens committed Jun 7, 2023
1 parent 5275f3b commit 1796989
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
12 changes: 8 additions & 4 deletions index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -2574,15 +2574,19 @@ create them does not matter.
<!-- TODO (future): If we allow changing the queuing strategy, this Enqueue might throw.
We'll then need to catch the error and close the async iterator. -->
1. Let |cancelAlgorithm| be the following steps, given |reason|:
1. Let |returnMethod| be [$GetMethod$](|iteratorRecord|.\[[Iterator]], "`return`").
1. Let |iterator| be |iteratorRecord|.\[[Iterator]].
1. Let |returnMethod| be [$GetMethod$](|iterator|, "`return`").
1. If |returnMethod| is an abrupt completion, return [=a promise rejected with=]
|returnMethod|.\[[Value]].
1. If |returnMethod|.\[[Value]] is undefined, return [=a promise resolved with=] undefined.
1. Let |returnResult| be [$Call$](|returnMethod|.\[[Value]], |iteratorRecord|.\[[Iterator]],
« |reason| »).
1. Let |returnResult| be [$Call$](|returnMethod|.\[[Value]], |iterator|, « |reason| »).
1. If |returnResult| is an abrupt completion, return [=a promise rejected with=]
|returnResult|.\[[Value]].
1. Return [=a promise resolved with=] |returnResult|.\[[Value]].
1. Let |returnPromise| be [=a promise resolved with=] |returnResult|.\[[Value]].
1. Return the result of [=reacting=] to |returnPromise| with the following fulfillment steps,
given |iterResult|:
1. If [$Type$](|iterResult|) is not Object, throw a {{TypeError}}.
1. Return undefined.
1. Set |stream| to ! [$CreateReadableStream$](|startAlgorithm|, |pullAlgorithm|, |cancelAlgorithm|,
0).
1. Return |stream|.
Expand Down
13 changes: 10 additions & 3 deletions reference-implementation/lib/abstract-ops/readable-streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -1910,9 +1910,10 @@ function ReadableStreamFromIterable(asyncIterable) {
}

function cancelAlgorithm(reason) {
const iterator = iteratorRecord.iterator;
let returnMethod;
try {
returnMethod = GetMethod(iteratorRecord.iterator, 'return');
returnMethod = GetMethod(iterator, 'return');
} catch (e) {
return promiseRejectedWith(e);
}
Expand All @@ -1921,11 +1922,17 @@ function ReadableStreamFromIterable(asyncIterable) {
}
let returnResult;
try {
returnResult = Call(returnMethod, iteratorRecord.iterator, [reason]);
returnResult = Call(returnMethod, iterator, [reason]);
} catch (e) {
return promiseRejectedWith(e);
}
return promiseResolvedWith(returnResult);
const returnPromise = promiseResolvedWith(returnResult);
return transformPromiseWith(returnPromise, iterResult => {
if (!typeIsObject(iterResult)) {
throw new TypeError('The promise returned by the iterator.return() method must fulfill with an object');
}
return undefined;
});
}

stream = CreateReadableStream(startAlgorithm, pullAlgorithm, cancelAlgorithm, 0);
Expand Down

0 comments on commit 1796989

Please sign in to comment.