Skip to content
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 6 commits into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
Copy link
Contributor

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?

Copy link
Contributor Author

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. :-)

Copy link
Contributor

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.

}).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"));
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");
}
});
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);
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);
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);
Loading