-
Notifications
You must be signed in to change notification settings - Fork 461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test cases for async function and async generator edge-cases #2096
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bb838d8
Add test when IteratorValue argument to AsyncGeneratorYield in yield*…
anba 9d711fd
Add test for "constructor" lookups and correct job order in for-await…
anba 1111d49
Add test that return resumption value in async generator is awaited
anba 252ca73
Add test that only explicit undefined return value is awaited in asyn…
anba c843ff0
Test for abrupt completion of PromiseResolve in AsyncFromSyncIterator…
anba 3428a59
Add test case for tc39/ecma262#1172
anba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
test/language/statements/async-generator/return-undefined-implicit-and-explicit.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright (C) 2019 André Bargull. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
|
||
/*--- | ||
esid: sec-return-statement-runtime-semantics-evaluation | ||
description: > | ||
Return with an explicit return value awaits this value. | ||
info: | | ||
13.10.1 Runtime Semantics: Evaluation | ||
|
||
ReturnStatement : return; | ||
1. Return Completion { [[Type]]: return, [[Value]]: undefined, [[Target]]: empty }. | ||
|
||
ReturnStatement : return Expression ; | ||
1. Let exprRef be the result of evaluating Expression. | ||
2. Let exprValue be ? GetValue(exprRef). | ||
3. If ! GetGeneratorKind() is async, set exprValue to ? Await(exprValue). | ||
4. Return Completion { [[Type]]: return, [[Value]]: exprValue, [[Target]]: empty }. | ||
|
||
25.5.3.2 AsyncGeneratorStart ( generator, generatorBody ) | ||
|
||
... | ||
5. Set the code evaluation state of genContext such that when evaluation is resumed for that | ||
execution context the following steps will be performed: | ||
a. Let result be the result of evaluating generatorBody. | ||
... | ||
e. If result is a normal completion, let resultValue be undefined. | ||
... | ||
g. Return ! AsyncGeneratorResolve(generator, resultValue, true). | ||
|
||
includes: [compareArray.js] | ||
flags: [async] | ||
features: [async-iteration] | ||
---*/ | ||
|
||
// 25.5.3.2, step 5.e: |generatorBody| execution ends with a normal completion. | ||
async function* g1() { | ||
// no return | ||
} | ||
|
||
// 13.10.1: No expression form means direct return. | ||
async function* g2() { | ||
return; | ||
} | ||
|
||
// 13.10.1: Explicit expression requires Await. | ||
async function* g3() { | ||
return undefined; // Return undefined via global value `undefined`. | ||
} | ||
|
||
// 13.10.1: Explicit expression requires Await. | ||
async function* g4() { | ||
return void 0; // Return undefined via void expression. | ||
} | ||
|
||
var expected = [ | ||
"tick 1", | ||
|
||
"g1 ret", | ||
"g2 ret", | ||
|
||
"tick 2", | ||
|
||
"g3 ret", | ||
"g4 ret", | ||
]; | ||
|
||
var actual = []; | ||
|
||
Promise.resolve(0) | ||
.then(() => actual.push("tick 1")) | ||
.then(() => actual.push("tick 2")) | ||
.then(() => { | ||
assert.compareArray(actual, expected, "Ticks for implicit and explicit return undefined"); | ||
}).then($DONE, $DONE); | ||
|
||
g1().next().then(v => actual.push("g1 ret")); | ||
g2().next().then(v => actual.push("g2 ret")); | ||
g3().next().then(v => actual.push("g3 ret")); | ||
g4().next().then(v => actual.push("g4 ret")); |
104 changes: 104 additions & 0 deletions
104
test/language/statements/async-generator/yield-return-then-getter-ticks.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright (C) 2019 André Bargull. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
|
||
/*--- | ||
esid: sec-generator-function-definitions-runtime-semantics-evaluation | ||
description: > | ||
Return resumption value is awaited upon and hence is treated as a thenable. | ||
info: | | ||
14.4.14 Runtime Semantics: Evaluation | ||
YieldExpression : yield AssignmentExpression | ||
|
||
... | ||
3. Let value be ? GetValue(exprRef). | ||
4. If generatorKind is async, then return ? AsyncGeneratorYield(value). | ||
... | ||
|
||
25.5.3.7 AsyncGeneratorYield ( value ) | ||
... | ||
5. Set value to ? Await(value). | ||
... | ||
8. Set the code evaluation state of genContext such that when evaluation is resumed with a | ||
Completion resumptionValue the following steps will be performed: | ||
... | ||
b. Let awaited be Await(resumptionValue.[[Value]]). | ||
... | ||
e. Return Completion { [[Type]]: return, [[Value]]: awaited.[[Value]], [[Target]]: empty }. | ||
... | ||
|
||
6.2.3.1 Await | ||
... | ||
2. Let promise be ? PromiseResolve(%Promise%, « value »). | ||
... | ||
|
||
25.6.4.5.1 PromiseResolve ( C, x ) | ||
... | ||
3. Let promiseCapability be ? NewPromiseCapability(C). | ||
4. Perform ? Call(promiseCapability.[[Resolve]], undefined, « x »). | ||
... | ||
|
||
25.6.1.5 NewPromiseCapability ( C ) | ||
... | ||
7. Let promise be ? Construct(C, « executor »). | ||
... | ||
|
||
25.6.3.1 Promise ( executor ) | ||
... | ||
8. Let resolvingFunctions be CreateResolvingFunctions(promise). | ||
... | ||
|
||
25.6.1.3 CreateResolvingFunctions ( promise ) | ||
... | ||
2. Let stepsResolve be the algorithm steps defined in Promise Resolve Functions (25.6.1.3.2). | ||
3. Let resolve be CreateBuiltinFunction(stepsResolve, « [[Promise]], [[AlreadyResolved]] »). | ||
... | ||
|
||
25.6.1.3.2 Promise Resolve Functions | ||
... | ||
9. Let then be Get(resolution, "then"). | ||
... | ||
|
||
includes: [compareArray.js] | ||
flags: [async] | ||
features: [async-iteration] | ||
---*/ | ||
|
||
var expected = [ | ||
"start", | ||
|
||
// `Await(value)` promise resolved. | ||
"tick 1", | ||
|
||
// "then" of `resumptionValue.[[Value]]` accessed. | ||
"get then", | ||
|
||
// `Await(resumptionValue.[[Value]])` promise resolved. | ||
"tick 2", | ||
]; | ||
|
||
var actual = []; | ||
|
||
async function* f() { | ||
actual.push("start"); | ||
yield 123; | ||
actual.push("stop - never reached"); | ||
} | ||
|
||
Promise.resolve(0) | ||
.then(() => actual.push("tick 1")) | ||
.then(() => actual.push("tick 2")) | ||
.then(() => { | ||
assert.compareArray(actual, expected, "Ticks for return with thenable getter"); | ||
}).then($DONE, $DONE); | ||
|
||
var it = f(); | ||
|
||
// Start generator execution. | ||
it.next(); | ||
|
||
// Stop generator execution. | ||
it.return({ | ||
get then() { | ||
actual.push("get then"); | ||
} | ||
}); |
55 changes: 55 additions & 0 deletions
55
test/language/statements/async-generator/yield-star-async-from-sync-iterator-inaccessible.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright (C) 2019 André Bargull. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
|
||
/*--- | ||
esid: sec-createasyncfromsynciterator | ||
description: > | ||
Async-from-Sync Iterator instances are not accessible from user code. | ||
info: | | ||
25.1.4.1 CreateAsyncFromSyncIterator ( syncIteratorRecord ) | ||
1. Let asyncIterator be ! ObjectCreate(%AsyncFromSyncIteratorPrototype%, « [[SyncIteratorRecord]] »). | ||
2. Set asyncIterator.[[SyncIteratorRecord]] to syncIteratorRecord. | ||
3. Let nextMethod be ! Get(asyncIterator, "next"). | ||
4. Let iteratorRecord be Record { [[Iterator]]: asyncIterator, [[NextMethod]]: nextMethod, [[Done]]: false }. | ||
5. Return iteratorRecord. | ||
|
||
14.4.14 Runtime Semantics: Evaluation | ||
YieldExpression : yield * AssignmentExpression | ||
1. Let generatorKind be ! GetGeneratorKind(). | ||
... | ||
4. Let iteratorRecord be ? GetIterator(value, generatorKind). | ||
... | ||
|
||
7.4.1 GetIterator ( obj [ , hint [ , method ] ] ) | ||
... | ||
3. If method is not present, then | ||
a. If hint is async, then | ||
i. Set method to ? GetMethod(obj, @@asyncIterator). | ||
ii. If method is undefined, then | ||
1. Let syncMethod be ? GetMethod(obj, @@iterator). | ||
2. Let syncIteratorRecord be ? GetIterator(obj, sync, syncMethod). | ||
3. Return ? CreateAsyncFromSyncIterator(syncIteratorRecord). | ||
... | ||
|
||
flags: [async] | ||
features: [async-iteration] | ||
---*/ | ||
|
||
var AsyncIteratorPrototype = Object.getPrototypeOf(async function*(){}.constructor.prototype.prototype); | ||
|
||
Object.defineProperty(AsyncIteratorPrototype, Symbol.iterator, { | ||
get() { | ||
throw new Error("@@iterator accessed"); | ||
} | ||
}); | ||
|
||
Object.defineProperty(AsyncIteratorPrototype, Symbol.asyncIterator, { | ||
get() { | ||
throw new Error("@@asyncIterator accessed"); | ||
} | ||
}); | ||
|
||
async function* g() { | ||
yield* []; | ||
} | ||
g().next().then(() => $DONE(), $DONE); |
53 changes: 53 additions & 0 deletions
53
test/language/statements/async-generator/yield-star-normal-notdone-iter-value-throws.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (C) 2019 André Bargull. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
|
||
/*--- | ||
esid: sec-generator-function-definitions-runtime-semantics-evaluation | ||
description: > | ||
Abrupt completion when calling IteratorValue is propagated when received.[[Type]] is normal. | ||
info: | | ||
14.4.14 Runtime Semantics: Evaluation | ||
YieldExpression : yield* AssignmentExpression | ||
|
||
... | ||
7. Repeat, | ||
a. If received.[[Type]] is normal, then | ||
... | ||
vi. If generatorKind is async, then set received to AsyncGeneratorYield(? IteratorValue(innerResult)). | ||
... | ||
|
||
flags: [async] | ||
features: [async-iteration] | ||
---*/ | ||
|
||
var token = {}; | ||
|
||
var asyncIter = { | ||
[Symbol.asyncIterator]() { | ||
return this; | ||
}, | ||
next() { | ||
return { | ||
done: false, | ||
get value() { | ||
throw token; | ||
} | ||
}; | ||
} | ||
}; | ||
|
||
async function* f() { | ||
var thrown; | ||
try { | ||
yield* asyncIter; | ||
} catch (e) { | ||
thrown = e; | ||
} | ||
return thrown; | ||
} | ||
|
||
var iter = f(); | ||
|
||
iter.next().then(({value}) => { | ||
assert.sameValue(value, token); | ||
}).then($DONE, $DONE); |
63 changes: 63 additions & 0 deletions
63
test/language/statements/async-generator/yield-star-return-notdone-iter-value-throws.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (C) 2019 André Bargull. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
|
||
/*--- | ||
esid: sec-generator-function-definitions-runtime-semantics-evaluation | ||
description: > | ||
Abrupt completion when calling IteratorValue is propagated when received.[[Type]] is return. | ||
info: | | ||
14.4.14 Runtime Semantics: Evaluation | ||
YieldExpression : yield* AssignmentExpression | ||
|
||
... | ||
7. Repeat, | ||
... | ||
c. Else, | ||
i. Assert: received.[[Type]] is return. | ||
... | ||
ix. If generatorKind is async, then set received to AsyncGeneratorYield(? IteratorValue(innerReturnResult)). | ||
... | ||
|
||
flags: [async] | ||
features: [async-iteration] | ||
---*/ | ||
|
||
var token = {}; | ||
|
||
var asyncIter = { | ||
[Symbol.asyncIterator]() { | ||
return this; | ||
}, | ||
next() { | ||
return { | ||
done: false, | ||
value: undefined, | ||
}; | ||
}, | ||
return() { | ||
return { | ||
done: false, | ||
get value() { | ||
throw token; | ||
} | ||
}; | ||
} | ||
}; | ||
|
||
async function* f() { | ||
var thrown; | ||
try { | ||
yield* asyncIter; | ||
} catch (e) { | ||
thrown = e; | ||
} | ||
return thrown; | ||
} | ||
|
||
var iter = f(); | ||
|
||
iter.next().then(() => { | ||
iter.return().then(({value}) => { | ||
assert.sameValue(value, token); | ||
}).then($DONE, $DONE); | ||
}).catch($DONE); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't actually test the return values, does it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct, it doesn't test that each of the async generators actually returns
undefined
. But that was actually intentional, because the test should only ensure that the async generators finish at the correct turn. :-)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, that sounds right :) Then the assertion text should be changed.