Skip to content

Commit 20bac2c

Browse files
committed
AsyncIterator, pt 1
1 parent 684723e commit 20bac2c

32 files changed

+1369
-12
lines changed

harness/iterators.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defines: [Test262AsyncIterator, Test262Iterator]
99
class Test262Iterator extends Iterator {
1010
constructor(iterable = []) {
1111
super();
12-
this.iterable = iterable;
12+
this.iterable = Array.from(iterable);
1313
this.nextCalls = 0;
1414
}
1515
next() {
@@ -26,7 +26,7 @@ class Test262Iterator extends Iterator {
2626
class Test262AsyncIterator extends AsyncIterator {
2727
constructor(iterable = []) {
2828
super();
29-
this.iterable = iterable;
29+
this.iterable = Array.from(iterable);
3030
this.nextCalls = 0;
3131
}
3232
async next() {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (C) 2020 Rick Waldron. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-asynciteratorprototype.asindexedpairs
5+
description: >
6+
Returns abrupt when next accessor is abrupt.
7+
info: |
8+
%AsyncIterator.prototype%.asIndexedPairs ( )
9+
10+
%AsyncIterator.prototype%.asIndexedPairs is a built-in async generator function which, when called, performs the following prelude steps:
11+
12+
Let iterated be ? GetIteratorDirect(this value).
13+
14+
The body of %AsyncIterator.prototype%.asIndexedPairs is composed of the following steps:
15+
16+
Let index be 0.
17+
Let lastValue be undefined.
18+
Repeat,
19+
Let next be ? Await(? IteratorNext(iterated, lastValue)).
20+
...
21+
22+
includes: [iterators.js]
23+
features: [async-iteration, iterator-helpers]
24+
flags: [async]
25+
---*/
26+
class Test262AsyncIteratorAbrupt extends Test262AsyncIterator {
27+
get next() {
28+
throw new Test262Error();
29+
}
30+
}
31+
32+
(async () => {
33+
let count = 0;
34+
let iterator = new Test262AsyncIteratorAbrupt([0, 1, 2, 3]);
35+
assert.sameValue(iterator.nextCalls, 0, 'The value of iterator.nextCalls is 0');
36+
37+
try {
38+
iterator.asIndexedPairs();
39+
} catch (e) {
40+
count++;
41+
assert.sameValue(e instanceof Test262Error, true, 'The result of `(e instanceof Test262Error)` is true');
42+
}
43+
44+
assert.sameValue(count, 1, 'The value of `count` is 1');
45+
assert.sameValue(iterator.nextCalls, 0, 'The value of iterator.nextCalls is 0');
46+
assert.sameValue(iterator.iterable.length, 4, 'The value of iterator.iterable.length is 4');
47+
})().then($DONE, $DONE);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (C) 2020 Rick Waldron. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-asynciteratorprototype.asindexedpairs
5+
description: >
6+
Returns abrupt when return accessor is abrupt.
7+
info: |
8+
%AsyncIterator.prototype%.asIndexedPairs ( )
9+
10+
%AsyncIterator.prototype%.asIndexedPairs is a built-in async generator function which, when called, performs the following prelude steps:
11+
12+
Let iterated be ? GetIteratorDirect(this value).
13+
14+
The body of %AsyncIterator.prototype%.asIndexedPairs is composed of the following steps:
15+
16+
Let index be 0.
17+
Let lastValue be undefined.
18+
Repeat,
19+
Let next be ? Await(? IteratorNext(iterated, lastValue)).
20+
If ? IteratorComplete(next) is true, return undefined.
21+
Let value be ? IteratorValue(next).
22+
Let pair be ! CreateArrayFromList(« index, value »).
23+
Set index to index + 1.
24+
Set lastValue to Yield(pair).
25+
IfAbruptCloseAsyncIterator(iterated, lastValue).
26+
27+
features: [async-iteration, iterator-helpers]
28+
flags: [async]
29+
---*/
30+
let genCount = 0;
31+
32+
async function* g() {
33+
genCount++;
34+
}
35+
36+
(async () => {
37+
let iterator = g().asIndexedPairs();
38+
let proto = Object.getPrototypeOf(iterator);
39+
let tryCount = 0;
40+
let catchCount = 0;
41+
let returnCount = 0;
42+
43+
Object.defineProperty(proto, 'return', {
44+
get() {
45+
returnCount++;
46+
throw new Test262Error();
47+
}
48+
});
49+
50+
try {
51+
tryCount++;
52+
await iterator.next();
53+
await iterator.return();
54+
tryCount++;
55+
} catch (e) {
56+
catchCount++;
57+
assert.sameValue(e instanceof Test262Error, true, 'The result of `(e instanceof Test262Error)` is true');
58+
}
59+
60+
assert.sameValue(genCount, 1, 'The value of `genCount` is 1');
61+
assert.sameValue(tryCount, 1, 'The value of `tryCount` is 1');
62+
assert.sameValue(catchCount, 1, 'The value of `catchCount` is 1');
63+
assert.sameValue(returnCount, 1, 'The value of `returnCount` is 1');
64+
})().then($DONE, $DONE);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (C) 2020 Rick Waldron. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-asynciteratorprototype.asindexedpairs
5+
description: >
6+
Returns abrupt when done accessor is abrupt.
7+
info: |
8+
%AsyncIterator.prototype%.asIndexedPairs ( )
9+
10+
%AsyncIterator.prototype%.asIndexedPairs is a built-in async generator function which, when called, performs the following prelude steps:
11+
12+
Let iterated be ? GetIteratorDirect(this value).
13+
14+
The body of %AsyncIterator.prototype%.asIndexedPairs is composed of the following steps:
15+
16+
Let index be 0.
17+
Let lastValue be undefined.
18+
Repeat,
19+
Let next be ? Await(? IteratorNext(iterated, lastValue)).
20+
If ? IteratorComplete(next) is true, return undefined.
21+
...
22+
23+
includes: [iterators.js]
24+
features: [async-iteration, iterator-helpers]
25+
flags: [async]
26+
---*/
27+
let doneCount = 0;
28+
29+
class Test262AsyncIteratorAbrupt extends Test262AsyncIterator {
30+
async next() {
31+
this.nextCalls++;
32+
33+
return {
34+
get done() {
35+
doneCount++;
36+
throw new Test262Error();
37+
},
38+
39+
value: 1
40+
};
41+
}
42+
}
43+
44+
(async () => {
45+
let count = 0;
46+
let iterator = new Test262AsyncIteratorAbrupt([0, 1, 2, 3]);
47+
assert.sameValue(iterator.nextCalls, 0, 'The value of iterator.nextCalls is 0');
48+
let indexedPairs = iterator.asIndexedPairs();
49+
50+
try {
51+
count++;
52+
53+
for await (const [i, v] of indexedPairs) {
54+
$DONE('for await body must not be reachable');
55+
}
56+
} catch (e) {
57+
count++;
58+
assert.sameValue(e instanceof Test262Error, true, 'The result of `(e instanceof Test262Error)` is true');
59+
}
60+
61+
assert.sameValue(doneCount, 1, 'The value of `doneCount` is 1');
62+
assert.sameValue(iterator.nextCalls, 1, 'The value of iterator.nextCalls is 1');
63+
assert.sameValue(iterator.iterable.length, 4, 'The value of iterator.iterable.length is 4');
64+
})().then($DONE, $DONE);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (C) 2020 Rick Waldron. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-asynciteratorprototype.asindexedpairs
5+
description: >
6+
Returns abrupt when value accessor is abrupt.
7+
info: |
8+
%AsyncIterator.prototype%.asIndexedPairs ( )
9+
10+
%AsyncIterator.prototype%.asIndexedPairs is a built-in async generator function which, when called, performs the following prelude steps:
11+
12+
Let iterated be ? GetIteratorDirect(this value).
13+
14+
The body of %AsyncIterator.prototype%.asIndexedPairs is composed of the following steps:
15+
16+
Let index be 0.
17+
Let lastValue be undefined.
18+
Repeat,
19+
Let next be ? Await(? IteratorNext(iterated, lastValue)).
20+
If ? IteratorComplete(next) is true, return undefined.
21+
Let value be ? IteratorValue(next).
22+
...
23+
24+
includes: [iterators.js]
25+
features: [async-iteration, iterator-helpers]
26+
flags: [async]
27+
---*/
28+
let valueCount = 0;
29+
30+
class Test262AsyncIteratorAbrupt extends Test262AsyncIterator {
31+
async next() {
32+
this.nextCalls++;
33+
34+
return {
35+
get value() {
36+
valueCount++;
37+
throw new Test262Error();
38+
}
39+
};
40+
}
41+
}
42+
43+
(async () => {
44+
let count = 0;
45+
let iterator = new Test262AsyncIteratorAbrupt([0, 1, 2, 3]);
46+
assert.sameValue(iterator.nextCalls, 0, 'The value of iterator.nextCalls is 0');
47+
let indexedPairs = iterator.asIndexedPairs();
48+
49+
try {
50+
count++;
51+
52+
for await (const [i, v] of indexedPairs) {
53+
$DONE('for await body must not be reachable');
54+
}
55+
} catch (e) {
56+
count++;
57+
assert.sameValue(e instanceof Test262Error, true, 'The result of `(e instanceof Test262Error)` is true');
58+
}
59+
60+
assert.sameValue(valueCount, 1, 'The value of `valueCount` is 1');
61+
assert.sameValue(iterator.nextCalls, 1, 'The value of iterator.nextCalls is 1');
62+
assert.sameValue(iterator.iterable.length, 4, 'The value of iterator.iterable.length is 4');
63+
})().then($DONE, $DONE);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (C) 2020 Rick Waldron. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-asynciteratorprototype.asindexedpairs
5+
description: >
6+
Returns abrupt when next call is abrupt.
7+
info: |
8+
%AsyncIterator.prototype%.asIndexedPairs ( )
9+
10+
%AsyncIterator.prototype%.asIndexedPairs is a built-in async generator function which, when called, performs the following prelude steps:
11+
12+
Let iterated be ? GetIteratorDirect(this value).
13+
14+
The body of %AsyncIterator.prototype%.asIndexedPairs is composed of the following steps:
15+
16+
Let index be 0.
17+
Let lastValue be undefined.
18+
Repeat,
19+
Let next be ? Await(? IteratorNext(iterated, lastValue)).
20+
...
21+
22+
includes: [iterators.js]
23+
features: [async-iteration, iterator-helpers]
24+
flags: [async]
25+
---*/
26+
class Test262AsyncIteratorAbrupt extends Test262AsyncIterator {
27+
async next() {
28+
throw new Test262Error();
29+
}
30+
}
31+
32+
(async () => {
33+
let count = 0;
34+
let iterator = new Test262AsyncIteratorAbrupt([0, 1, 2, 3]);
35+
assert.sameValue(iterator.nextCalls, 0, 'The value of iterator.nextCalls is 0');
36+
let indexedPairs = iterator.asIndexedPairs();
37+
38+
try {
39+
count++;
40+
41+
for await (const [i, v] of indexedPairs) {
42+
$DONE('for await body must not be reachable');
43+
}
44+
} catch (e) {
45+
count++;
46+
assert.sameValue(e instanceof Test262Error, true, 'The result of `(e instanceof Test262Error)` is true');
47+
}
48+
49+
assert.sameValue(iterator.nextCalls, 0, 'The value of iterator.nextCalls is 0');
50+
assert.sameValue(iterator.iterable.length, 4, 'The value of iterator.iterable.length is 4');
51+
})().then($DONE, $DONE);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (C) 2020 Rick Waldron. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-asynciteratorprototype.asindexedpairs
5+
description: >
6+
Returns abrupt when return call is abrupt.
7+
info: |
8+
%AsyncIterator.prototype%.asIndexedPairs ( )
9+
10+
%AsyncIterator.prototype%.asIndexedPairs is a built-in async generator function which, when called, performs the following prelude steps:
11+
12+
Let iterated be ? GetIteratorDirect(this value).
13+
14+
The body of %AsyncIterator.prototype%.asIndexedPairs is composed of the following steps:
15+
16+
Let index be 0.
17+
Let lastValue be undefined.
18+
Repeat,
19+
Let next be ? Await(? IteratorNext(iterated, lastValue)).
20+
If ? IteratorComplete(next) is true, return undefined.
21+
Let value be ? IteratorValue(next).
22+
Let pair be ! CreateArrayFromList(« index, value »).
23+
Set index to index + 1.
24+
Set lastValue to Yield(pair).
25+
IfAbruptCloseAsyncIterator(iterated, lastValue).
26+
27+
28+
features: [async-iteration, iterator-helpers]
29+
flags: [async]
30+
---*/
31+
let genCount = 0;
32+
33+
async function* g() {
34+
genCount++;
35+
yield 1;
36+
genCount++;
37+
throw new Test262Error();
38+
}
39+
40+
(async () => {
41+
let iterator = g().asIndexedPairs();
42+
let tryCount = 0;
43+
let catchCount = 0;
44+
let forAwaitCount = 0;
45+
46+
try {
47+
tryCount++;
48+
49+
for await (const [i, v] of iterator) {
50+
forAwaitCount++;
51+
}
52+
53+
tryCount++;
54+
} catch (e) {
55+
catchCount++;
56+
assert.sameValue(e instanceof Test262Error, true, 'The result of `(e instanceof Test262Error)` is true');
57+
}
58+
59+
assert.sameValue(genCount, 2, 'The value of `genCount` is 2');
60+
assert.sameValue(tryCount, 1, 'The value of `tryCount` is 1');
61+
assert.sameValue(catchCount, 1, 'The value of `catchCount` is 1');
62+
assert.sameValue(forAwaitCount, 1, 'The value of `forAwaitCount` is 1');
63+
})().then($DONE, $DONE);

0 commit comments

Comments
 (0)