Skip to content

Commit a630079

Browse files
committed
AsyncIterator, pt 4
1 parent 4242ff3 commit a630079

14 files changed

+531
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.find
5+
description: >
6+
Returns abrupt when callback call is abrupt. Closes iterator
7+
info: |
8+
%AsyncIterator.prototype%.find ( fn )
9+
10+
Let iterated be ? GetIteratorDirect(this value).
11+
If IsCallable(fn) is false, throw a TypeError exception.
12+
Repeat,
13+
Let next be ? Await(? IteratorNext(iterated)).
14+
If ? IteratorComplete(next) is true, return undefined.
15+
Let value be ? IteratorValue(next).
16+
Let result be Call(fn, undefined, « value »).
17+
IfAbruptCloseAsyncIterator(iterated, result).
18+
19+
includes: [iterators.js]
20+
features: [async-iteration, iterator-helpers]
21+
flags: [async]
22+
---*/
23+
24+
(async () => {
25+
let tryCount = 0;
26+
let catchCount = 0;
27+
let callbackCount = 0;
28+
let iterator = new Test262AsyncIterator([1, 2]);
29+
30+
try {
31+
tryCount++;
32+
await iterator.find(() => {
33+
callbackCount++;
34+
throw new Test262Error();
35+
});
36+
} catch (e) {
37+
catchCount++;
38+
assert.sameValue(e instanceof Test262Error, true, 'The result of evaluating `(e instanceof Test262Error)` is true');
39+
}
40+
41+
assert.sameValue(tryCount, 1, 'The value of `tryCount` is 1');
42+
assert.sameValue(catchCount, 1, 'The value of `catchCount` is 1');
43+
assert.sameValue(callbackCount, 1, 'The value of `callbackCount` is 1');
44+
45+
})().then($DONE, $DONE);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.find
5+
description: >
6+
Returns abrupt when next accessor is abrupt.
7+
info: |
8+
%AsyncIterator.prototype%.find ( fn )
9+
10+
Let iterated be ? GetIteratorDirect(this value).
11+
If IsCallable(fn) is false, throw a TypeError exception.
12+
Repeat,
13+
Let next be ? Await(? IteratorNext(iterated)).
14+
...
15+
16+
IteratorNext ( iteratorRecord [ , value ] )
17+
18+
If value is not present, then
19+
Let result be ? Call(iteratorRecord.[[NextMethod]], iteratorRecord.[[Iterator]]).
20+
Else,
21+
Let result be ? Call(iteratorRecord.[[NextMethod]], iteratorRecord.[[Iterator]], « value »).
22+
If Type(result) is not Object, throw a TypeError exception.
23+
Return result.
24+
25+
26+
includes: [iterators.js]
27+
features: [async-iteration, iterator-helpers]
28+
flags: [async]
29+
---*/
30+
let nextGets = 0;
31+
class Test262AsyncIteratorAbrupt extends Test262AsyncIterator {
32+
get next() {
33+
nextGets++;
34+
throw new Test262Error();
35+
}
36+
}
37+
38+
(async () => {
39+
let tryCount = 0;
40+
let catchCount = 0;
41+
let iterator = new Test262AsyncIteratorAbrupt([1, 2]);
42+
assert.sameValue(nextGets, 0, 'The value of `nextGets` is 0');
43+
44+
try {
45+
tryCount++;
46+
await iterator.find(x => false);
47+
} catch (e) {
48+
catchCount++;
49+
assert.sameValue(e instanceof Test262Error, true, 'The result of evaluating `(e instanceof Test262Error)` is true');
50+
}
51+
52+
assert.sameValue(tryCount, 1, 'The value of `tryCount` is 1');
53+
assert.sameValue(catchCount, 1, 'The value of `catchCount` is 1');
54+
assert.sameValue(nextGets, 1, 'The value of `nextGets` is 1');
55+
})().then($DONE, $DONE);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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.find
5+
description: >
6+
Returns abrupt when done accessor is abrupt.
7+
info: |
8+
%AsyncIterator.prototype%.find ( fn )
9+
10+
Let iterated be ? GetIteratorDirect(this value).
11+
If IsCallable(fn) is false, throw a TypeError exception.
12+
Repeat,
13+
Let next be ? Await(? IteratorNext(iterated)).
14+
If ? IteratorComplete(next) is true, return undefined.
15+
Let value be ? IteratorValue(next).
16+
Let result be Call(fn, undefined, « value »).
17+
IfAbruptCloseAsyncIterator(iterated, result).
18+
Set result to Await(result).
19+
IfAbruptCloseAsyncIterator(iterated, result).
20+
If ! ToBoolean(result) is true, return ? AsyncIteratorClose(iterated, NormalCompletion(value)).
21+
22+
includes: [iterators.js]
23+
features: [async-iteration, iterator-helpers]
24+
flags: [async]
25+
---*/
26+
let doneGets = 0;
27+
let nextCalls = 0;
28+
class Test262AsyncIteratorAbrupt extends Test262AsyncIterator {
29+
async next() {
30+
nextCalls++;
31+
return {
32+
get done() {
33+
doneGets++;
34+
throw new Test262Error();
35+
},
36+
37+
value: 1
38+
};
39+
}
40+
}
41+
42+
(async () => {
43+
let tryCount = 0;
44+
let catchCount = 0;
45+
let iterator = (new Test262AsyncIteratorAbrupt([1, 2]));
46+
assert.sameValue(nextCalls, 0, 'The value of `nextCalls` is 0');
47+
48+
try {
49+
tryCount++;
50+
51+
await iterator.find(() => true);
52+
} catch (e) {
53+
catchCount++;
54+
assert.sameValue(e instanceof Test262Error, true, 'The result of evaluating `(e instanceof Test262Error)` is true');
55+
}
56+
57+
assert.sameValue(tryCount, 1, 'The value of `tryCount` is 1');
58+
assert.sameValue(catchCount, 1, 'The value of `catchCount` is 1');
59+
assert.sameValue(doneGets, 1, 'The value of `doneGets` is 1');
60+
assert.sameValue(nextCalls, 1, 'The value of `nextCalls` is 1');
61+
})().then($DONE, $DONE);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.find
5+
description: >
6+
Returns abrupt when value accessor is abrupt.
7+
info: |
8+
%AsyncIterator.prototype%.find ( fn )
9+
10+
Let iterated be ? GetIteratorDirect(this value).
11+
If IsCallable(fn) is false, throw a TypeError exception.
12+
Repeat,
13+
Let next be ? Await(? IteratorNext(iterated)).
14+
If ? IteratorComplete(next) is true, return undefined.
15+
Let value be ? IteratorValue(next).
16+
17+
includes: [iterators.js]
18+
features: [async-iteration, iterator-helpers]
19+
flags: [async]
20+
---*/
21+
let valueGets = 0;
22+
let nextCalls = 0;
23+
class Test262AsyncIteratorAbrupt extends Test262AsyncIterator {
24+
async next() {
25+
nextCalls++;
26+
return {
27+
done: false,
28+
get value() {
29+
valueGets++;
30+
throw new Test262Error();
31+
}
32+
};
33+
}
34+
}
35+
36+
(async () => {
37+
let tryCount = 0;
38+
let catchCount = 0;
39+
let iterator = (new Test262AsyncIteratorAbrupt([1, 2]));
40+
assert.sameValue(nextCalls, 0, 'The value of `nextCalls` is 0');
41+
42+
try {
43+
tryCount++;
44+
await iterator.find(() => true);
45+
} catch (e) {
46+
catchCount++;
47+
assert.sameValue(e instanceof Test262Error, true, 'The result of evaluating `(e instanceof Test262Error)` is true');
48+
}
49+
50+
assert.sameValue(tryCount, 1, 'The value of `tryCount` is 1');
51+
assert.sameValue(catchCount, 1, 'The value of `catchCount` is 1');
52+
assert.sameValue(valueGets, 1, 'The value of `valueGets` is 1');
53+
assert.sameValue(nextCalls, 1, 'The value of `nextCalls` is 1');
54+
})().then($DONE, $DONE);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.find
5+
description: >
6+
Returns abrupt when next call is abrupt.
7+
info: |
8+
%AsyncIterator.prototype%.find ( fn )
9+
10+
Let iterated be ? GetIteratorDirect(this value).
11+
If IsCallable(fn) is false, throw a TypeError exception.
12+
Repeat,
13+
Let next be ? Await(? IteratorNext(iterated)).
14+
15+
includes: [iterators.js]
16+
features: [async-iteration, iterator-helpers]
17+
flags: [async]
18+
---*/
19+
let nextCalls = 0;
20+
class Test262AsyncIteratorAbrupt extends Test262AsyncIterator {
21+
async next() {
22+
nextCalls++;
23+
throw new Test262Error();
24+
}
25+
}
26+
27+
(async () => {
28+
let tryCount = 0;
29+
let catchCount = 0;
30+
let iterator = new Test262AsyncIteratorAbrupt([1, 2]);
31+
assert.sameValue(nextCalls, 0, 'The value of `nextCalls` is 0');
32+
33+
try {
34+
tryCount++;
35+
await iterator.find(() => true);
36+
} catch (e) {
37+
catchCount++;
38+
assert.sameValue(e instanceof Test262Error, true, 'The result of evaluating `(e instanceof Test262Error)` is true');
39+
}
40+
41+
assert.sameValue(nextCalls, 1, 'The value of `nextCalls` is 1');
42+
assert.sameValue(tryCount, 1, 'The value of `tryCount` is 1');
43+
assert.sameValue(catchCount, 1, 'The value of `catchCount` is 1');
44+
})().then($DONE, $DONE);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.find
5+
description: >
6+
Returns abrupt when return call is abrupt.
7+
info: |
8+
%AsyncIterator.prototype%.find ( fn )
9+
10+
Let iterated be ? GetIteratorDirect(this value).
11+
If IsCallable(fn) is false, throw a TypeError exception.
12+
Repeat,
13+
Let next be ? Await(? IteratorNext(iterated)).
14+
If ? IteratorComplete(next) is true, return undefined.
15+
Let value be ? IteratorValue(next).
16+
Let result be Call(fn, undefined, « value »).
17+
IfAbruptCloseAsyncIterator(iterated, result).
18+
Set result to Await(result).
19+
IfAbruptCloseAsyncIterator(iterated, result).
20+
If ! ToBoolean(result) is true, return ? AsyncIteratorClose(iterated, NormalCompletion(value)).
21+
22+
features: [async-iteration, iterator-helpers]
23+
flags: [async]
24+
---*/
25+
let yieldCount = 0;
26+
27+
async function* g() {
28+
yieldCount++;
29+
yield 1;
30+
throw new Test262Error();
31+
}
32+
33+
(async () => {
34+
let tryCount = 0;
35+
let catchCount = 0;
36+
let callbackCount = 0;
37+
let iterator = await g();
38+
39+
try {
40+
tryCount++;
41+
42+
await iterator.find(() => {
43+
callbackCount++;
44+
return false;
45+
});
46+
} catch (e) {
47+
catchCount++;
48+
assert.sameValue(e instanceof Test262Error, true, 'The result of evaluating `(e instanceof Test262Error)` is true');
49+
}
50+
51+
assert.sameValue(yieldCount, 1, 'The value of `yieldCount` is 1');
52+
assert.sameValue(tryCount, 1, 'The value of `tryCount` is 1');
53+
assert.sameValue(catchCount, 1, 'The value of `catchCount` is 1');
54+
assert.sameValue(callbackCount, 1, 'The value of `callbackCount` is 1');
55+
})().then($DONE, $DONE);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.find
5+
description: >
6+
AsyncIterator.prototype.find expects to be called with a function argument.
7+
features: [iterator-helpers]
8+
flags: [async]
9+
---*/
10+
(async () => {
11+
let tryCount = 0;
12+
let catchCount = 0;
13+
const nonCallable = {};
14+
async function* g() {}
15+
let iter = g();
16+
17+
try {
18+
tryCount++;
19+
await AsyncIterator.prototype.find.call(iter, nonCallable);
20+
} catch (e) {
21+
catchCount++;
22+
}
23+
24+
try {
25+
tryCount++;
26+
await iter.find(nonCallable);
27+
} catch (e) {
28+
catchCount++;
29+
}
30+
31+
assert.sameValue(tryCount, 2, 'The value of `tryCount` is 2');
32+
assert.sameValue(catchCount, 2, 'The value of `catchCount` is 2');
33+
})().then($DONE, $DONE);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.find
5+
description: >
6+
AsyncIterator.prototype.find is callable, but not constructable.
7+
features: [iterator-helpers]
8+
flags: [async]
9+
---*/
10+
(async () => {
11+
let tryCount = 0;
12+
let catchCount = 0;
13+
const nonCallable = {};
14+
async function* g() {}
15+
let iter = g();
16+
17+
try {
18+
tryCount++;
19+
await AsyncIterator.prototype.find.call(iter, () => {});
20+
} catch (e) {
21+
catchCount++;
22+
}
23+
24+
try {
25+
tryCount++;
26+
await iter.find(() => {});
27+
} catch (e) {
28+
catchCount++;
29+
}
30+
31+
try {
32+
tryCount++;
33+
await new iter.find(() => {});
34+
} catch (e) {
35+
catchCount++;
36+
}
37+
38+
assert.sameValue(tryCount, 3, 'The value of `tryCount` is 3');
39+
assert.sameValue(catchCount, 1, 'The value of `catchCount` is 1');
40+
})().then($DONE, $DONE);

0 commit comments

Comments
 (0)