Skip to content

Commit

Permalink
AsyncIterator, pt 1
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed Sep 29, 2020
1 parent 684723e commit 20bac2c
Show file tree
Hide file tree
Showing 32 changed files with 1,369 additions and 12 deletions.
4 changes: 2 additions & 2 deletions harness/iterators.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defines: [Test262AsyncIterator, Test262Iterator]
class Test262Iterator extends Iterator {
constructor(iterable = []) {
super();
this.iterable = iterable;
this.iterable = Array.from(iterable);
this.nextCalls = 0;
}
next() {
Expand All @@ -26,7 +26,7 @@ class Test262Iterator extends Iterator {
class Test262AsyncIterator extends AsyncIterator {
constructor(iterable = []) {
super();
this.iterable = iterable;
this.iterable = Array.from(iterable);
this.nextCalls = 0;
}
async next() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (C) 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-asynciteratorprototype.asindexedpairs
description: >
Returns abrupt when next accessor is abrupt.
info: |
%AsyncIterator.prototype%.asIndexedPairs ( )
%AsyncIterator.prototype%.asIndexedPairs is a built-in async generator function which, when called, performs the following prelude steps:
Let iterated be ? GetIteratorDirect(this value).
The body of %AsyncIterator.prototype%.asIndexedPairs is composed of the following steps:
Let index be 0.
Let lastValue be undefined.
Repeat,
Let next be ? Await(? IteratorNext(iterated, lastValue)).
...
includes: [iterators.js]
features: [async-iteration, iterator-helpers]
flags: [async]
---*/
class Test262AsyncIteratorAbrupt extends Test262AsyncIterator {
get next() {
throw new Test262Error();
}
}

(async () => {
let count = 0;
let iterator = new Test262AsyncIteratorAbrupt([0, 1, 2, 3]);
assert.sameValue(iterator.nextCalls, 0, 'The value of iterator.nextCalls is 0');

try {
iterator.asIndexedPairs();
} catch (e) {
count++;
assert.sameValue(e instanceof Test262Error, true, 'The result of `(e instanceof Test262Error)` is true');
}

assert.sameValue(count, 1, 'The value of `count` is 1');
assert.sameValue(iterator.nextCalls, 0, 'The value of iterator.nextCalls is 0');
assert.sameValue(iterator.iterable.length, 4, 'The value of iterator.iterable.length is 4');
})().then($DONE, $DONE);
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (C) 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-asynciteratorprototype.asindexedpairs
description: >
Returns abrupt when return accessor is abrupt.
info: |
%AsyncIterator.prototype%.asIndexedPairs ( )
%AsyncIterator.prototype%.asIndexedPairs is a built-in async generator function which, when called, performs the following prelude steps:
Let iterated be ? GetIteratorDirect(this value).
The body of %AsyncIterator.prototype%.asIndexedPairs is composed of the following steps:
Let index be 0.
Let lastValue be undefined.
Repeat,
Let next be ? Await(? IteratorNext(iterated, lastValue)).
If ? IteratorComplete(next) is true, return undefined.
Let value be ? IteratorValue(next).
Let pair be ! CreateArrayFromList(« index, value »).
Set index to index + 1.
Set lastValue to Yield(pair).
IfAbruptCloseAsyncIterator(iterated, lastValue).
features: [async-iteration, iterator-helpers]
flags: [async]
---*/
let genCount = 0;

async function* g() {
genCount++;
}

(async () => {
let iterator = g().asIndexedPairs();
let proto = Object.getPrototypeOf(iterator);
let tryCount = 0;
let catchCount = 0;
let returnCount = 0;

Object.defineProperty(proto, 'return', {
get() {
returnCount++;
throw new Test262Error();
}
});

try {
tryCount++;
await iterator.next();
await iterator.return();
tryCount++;
} catch (e) {
catchCount++;
assert.sameValue(e instanceof Test262Error, true, 'The result of `(e instanceof Test262Error)` is true');
}

assert.sameValue(genCount, 1, 'The value of `genCount` is 1');
assert.sameValue(tryCount, 1, 'The value of `tryCount` is 1');
assert.sameValue(catchCount, 1, 'The value of `catchCount` is 1');
assert.sameValue(returnCount, 1, 'The value of `returnCount` is 1');
})().then($DONE, $DONE);
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (C) 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-asynciteratorprototype.asindexedpairs
description: >
Returns abrupt when done accessor is abrupt.
info: |
%AsyncIterator.prototype%.asIndexedPairs ( )
%AsyncIterator.prototype%.asIndexedPairs is a built-in async generator function which, when called, performs the following prelude steps:
Let iterated be ? GetIteratorDirect(this value).
The body of %AsyncIterator.prototype%.asIndexedPairs is composed of the following steps:
Let index be 0.
Let lastValue be undefined.
Repeat,
Let next be ? Await(? IteratorNext(iterated, lastValue)).
If ? IteratorComplete(next) is true, return undefined.
...
includes: [iterators.js]
features: [async-iteration, iterator-helpers]
flags: [async]
---*/
let doneCount = 0;

class Test262AsyncIteratorAbrupt extends Test262AsyncIterator {
async next() {
this.nextCalls++;

return {
get done() {
doneCount++;
throw new Test262Error();
},

value: 1
};
}
}

(async () => {
let count = 0;
let iterator = new Test262AsyncIteratorAbrupt([0, 1, 2, 3]);
assert.sameValue(iterator.nextCalls, 0, 'The value of iterator.nextCalls is 0');
let indexedPairs = iterator.asIndexedPairs();

try {
count++;

for await (const [i, v] of indexedPairs) {
$DONE('for await body must not be reachable');
}
} catch (e) {
count++;
assert.sameValue(e instanceof Test262Error, true, 'The result of `(e instanceof Test262Error)` is true');
}

assert.sameValue(doneCount, 1, 'The value of `doneCount` is 1');
assert.sameValue(iterator.nextCalls, 1, 'The value of iterator.nextCalls is 1');
assert.sameValue(iterator.iterable.length, 4, 'The value of iterator.iterable.length is 4');
})().then($DONE, $DONE);
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (C) 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-asynciteratorprototype.asindexedpairs
description: >
Returns abrupt when value accessor is abrupt.
info: |
%AsyncIterator.prototype%.asIndexedPairs ( )
%AsyncIterator.prototype%.asIndexedPairs is a built-in async generator function which, when called, performs the following prelude steps:
Let iterated be ? GetIteratorDirect(this value).
The body of %AsyncIterator.prototype%.asIndexedPairs is composed of the following steps:
Let index be 0.
Let lastValue be undefined.
Repeat,
Let next be ? Await(? IteratorNext(iterated, lastValue)).
If ? IteratorComplete(next) is true, return undefined.
Let value be ? IteratorValue(next).
...
includes: [iterators.js]
features: [async-iteration, iterator-helpers]
flags: [async]
---*/
let valueCount = 0;

class Test262AsyncIteratorAbrupt extends Test262AsyncIterator {
async next() {
this.nextCalls++;

return {
get value() {
valueCount++;
throw new Test262Error();
}
};
}
}

(async () => {
let count = 0;
let iterator = new Test262AsyncIteratorAbrupt([0, 1, 2, 3]);
assert.sameValue(iterator.nextCalls, 0, 'The value of iterator.nextCalls is 0');
let indexedPairs = iterator.asIndexedPairs();

try {
count++;

for await (const [i, v] of indexedPairs) {
$DONE('for await body must not be reachable');
}
} catch (e) {
count++;
assert.sameValue(e instanceof Test262Error, true, 'The result of `(e instanceof Test262Error)` is true');
}

assert.sameValue(valueCount, 1, 'The value of `valueCount` is 1');
assert.sameValue(iterator.nextCalls, 1, 'The value of iterator.nextCalls is 1');
assert.sameValue(iterator.iterable.length, 4, 'The value of iterator.iterable.length is 4');
})().then($DONE, $DONE);
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (C) 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-asynciteratorprototype.asindexedpairs
description: >
Returns abrupt when next call is abrupt.
info: |
%AsyncIterator.prototype%.asIndexedPairs ( )
%AsyncIterator.prototype%.asIndexedPairs is a built-in async generator function which, when called, performs the following prelude steps:
Let iterated be ? GetIteratorDirect(this value).
The body of %AsyncIterator.prototype%.asIndexedPairs is composed of the following steps:
Let index be 0.
Let lastValue be undefined.
Repeat,
Let next be ? Await(? IteratorNext(iterated, lastValue)).
...
includes: [iterators.js]
features: [async-iteration, iterator-helpers]
flags: [async]
---*/
class Test262AsyncIteratorAbrupt extends Test262AsyncIterator {
async next() {
throw new Test262Error();
}
}

(async () => {
let count = 0;
let iterator = new Test262AsyncIteratorAbrupt([0, 1, 2, 3]);
assert.sameValue(iterator.nextCalls, 0, 'The value of iterator.nextCalls is 0');
let indexedPairs = iterator.asIndexedPairs();

try {
count++;

for await (const [i, v] of indexedPairs) {
$DONE('for await body must not be reachable');
}
} catch (e) {
count++;
assert.sameValue(e instanceof Test262Error, true, 'The result of `(e instanceof Test262Error)` is true');
}

assert.sameValue(iterator.nextCalls, 0, 'The value of iterator.nextCalls is 0');
assert.sameValue(iterator.iterable.length, 4, 'The value of iterator.iterable.length is 4');
})().then($DONE, $DONE);
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (C) 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-asynciteratorprototype.asindexedpairs
description: >
Returns abrupt when return call is abrupt.
info: |
%AsyncIterator.prototype%.asIndexedPairs ( )
%AsyncIterator.prototype%.asIndexedPairs is a built-in async generator function which, when called, performs the following prelude steps:
Let iterated be ? GetIteratorDirect(this value).
The body of %AsyncIterator.prototype%.asIndexedPairs is composed of the following steps:
Let index be 0.
Let lastValue be undefined.
Repeat,
Let next be ? Await(? IteratorNext(iterated, lastValue)).
If ? IteratorComplete(next) is true, return undefined.
Let value be ? IteratorValue(next).
Let pair be ! CreateArrayFromList(« index, value »).
Set index to index + 1.
Set lastValue to Yield(pair).
IfAbruptCloseAsyncIterator(iterated, lastValue).
features: [async-iteration, iterator-helpers]
flags: [async]
---*/
let genCount = 0;

async function* g() {
genCount++;
yield 1;
genCount++;
throw new Test262Error();
}

(async () => {
let iterator = g().asIndexedPairs();
let tryCount = 0;
let catchCount = 0;
let forAwaitCount = 0;

try {
tryCount++;

for await (const [i, v] of iterator) {
forAwaitCount++;
}

tryCount++;
} catch (e) {
catchCount++;
assert.sameValue(e instanceof Test262Error, true, 'The result of `(e instanceof Test262Error)` is true');
}

assert.sameValue(genCount, 2, 'The value of `genCount` is 2');
assert.sameValue(tryCount, 1, 'The value of `tryCount` is 1');
assert.sameValue(catchCount, 1, 'The value of `catchCount` is 1');
assert.sameValue(forAwaitCount, 1, 'The value of `forAwaitCount` is 1');
})().then($DONE, $DONE);
Loading

0 comments on commit 20bac2c

Please sign in to comment.