1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-async-await --allow-natives-syntax
// Do not install `AsyncFunction` constructor on global object
function assertThrowsAsync(run, errorType, message) {
var actual;
var hadValue = false;
var hadError = false;
var promise = run();
if (typeof promise !== "object" || typeof promise.then !== "function") {
throw new MjsUnitAssertionError(
"Expected " + run.toString() +
" to return a Promise, but it returned " + PrettyPrint(promise));
}
promise.then(function(value) { hadValue = true; actual = value; },
function(error) { hadError = true; actual = error; });
assertFalse(hadValue || hadError);
%RunMicrotasks();
if (!hadError) {
throw new MjsUnitAssertionError(
"Expected " + run + "() to throw " + errorType.name +
", but did not throw.");
}
if (!(actual instanceof errorType))
throw new MjsUnitAssertionError(
"Expected " + run + "() to throw " + errorType.name +
", but threw '" + actual + "'");
if (message !== void 0 && actual.message !== message)
throw new MjsUnitAssertionError(
"Expected " + run + "() to throw '" + message + "', but threw '" +
actual.message + "'");
};
function assertEqualsAsync(expected, run, msg) {
var actual;
var hadValue = false;
var hadError = false;
var promise = run();
if (typeof promise !== "object" || typeof promise.then !== "function") {
throw new MjsUnitAssertionError(
"Expected " + run.toString() +
" to return a Promise, but it returned " + PrettyPrint(promise));
}
promise.then(function(value) { hadValue = true; actual = value; },
function(error) { hadError = true; actual = error; });
assertFalse(hadValue || hadError);
%RunMicrotasks();
if (hadError) throw actual;
assertTrue(
hadValue, "Expected '" + run.toString() + "' to produce a value");
assertEquals(expected, actual, msg);
};
assertEquals(undefined, this.AsyncFunction);
let AsyncFunction = (async function() {}).constructor;
// The AsyncFunction Constructor is the %AsyncFunction% intrinsic object and
// is a subclass of Function.
// (https://tc39.github.io/ecmascript-asyncawait/#async-function-constructor)
assertEquals(Object.getPrototypeOf(AsyncFunction), Function);
assertEquals(Object.getPrototypeOf(AsyncFunction.prototype),
Function.prototype);
assertTrue(async function() {} instanceof Function);
// Let functionPrototype be the intrinsic object %AsyncFunctionPrototype%.
async function asyncFunctionForProto() {}
assertEquals(AsyncFunction.prototype,
Object.getPrototypeOf(asyncFunctionForProto));
assertEquals(AsyncFunction.prototype,
Object.getPrototypeOf(async function() {}));
assertEquals(AsyncFunction.prototype, Object.getPrototypeOf(async () => {}));
assertEquals(AsyncFunction.prototype,
Object.getPrototypeOf({ async method() {} }.method));
assertEquals(AsyncFunction.prototype, Object.getPrototypeOf(AsyncFunction()));
assertEquals(AsyncFunction.prototype,
Object.getPrototypeOf(new AsyncFunction()));
// AsyncFunctionCreate does not produce an object with a Prototype
assertEquals(undefined, asyncFunctionForProto.prototype);
assertEquals(false, asyncFunctionForProto.hasOwnProperty("prototype"));
assertEquals(undefined, (async function() {}).prototype);
assertEquals(false, (async function() {}).hasOwnProperty("prototype"));
assertEquals(undefined, (async() => {}).prototype);
assertEquals(false, (async() => {}).hasOwnProperty("prototype"));
assertEquals(undefined, ({ async method() {} }).method.prototype);
assertEquals(false, ({ async method() {} }).method.hasOwnProperty("prototype"));
assertEquals(undefined, AsyncFunction().prototype);
assertEquals(false, AsyncFunction().hasOwnProperty("prototype"));
assertEquals(undefined, (new AsyncFunction()).prototype);
assertEquals(false, (new AsyncFunction()).hasOwnProperty("prototype"));
assertEquals(1, async function(a) { await 1; }.length);
assertEquals(2, async function(a, b) { await 1; }.length);
assertEquals(1, async function(a, b = 2) { await 1; }.length);
assertEquals(2, async function(a, b, ...c) { await 1; }.length);
assertEquals(1, (async(a) => await 1).length);
assertEquals(2, (async(a, b) => await 1).length);
assertEquals(1, (async(a, b = 2) => await 1).length);
assertEquals(2, (async(a, b, ...c) => await 1).length);
assertEquals(1, ({ async f(a) { await 1; } }).f.length);
assertEquals(2, ({ async f(a, b) { await 1; } }).f.length);
assertEquals(1, ({ async f(a, b = 2) { await 1; } }).f.length);
assertEquals(2, ({ async f(a, b, ...c) { await 1; } }).f.length);
assertEquals(1, AsyncFunction("a", "await 1").length);
assertEquals(2, AsyncFunction("a", "b", "await 1").length);
assertEquals(1, AsyncFunction("a", "b = 2", "await 1").length);
assertEquals(2, AsyncFunction("a", "b", "...c", "await 1").length);
assertEquals(1, (new AsyncFunction("a", "await 1")).length);
assertEquals(2, (new AsyncFunction("a", "b", "await 1")).length);
assertEquals(1, (new AsyncFunction("a", "b = 2", "await 1")).length);
assertEquals(2, (new AsyncFunction("a", "b", "...c", "await 1")).length);
// AsyncFunction.prototype[ @@toStringTag ]
var descriptor =
Object.getOwnPropertyDescriptor(AsyncFunction.prototype,
Symbol.toStringTag);
assertEquals("AsyncFunction", descriptor.value);
assertEquals(false, descriptor.enumerable);
assertEquals(false, descriptor.writable);
assertEquals(true, descriptor.configurable);
assertEquals(1, AsyncFunction.length);
// Let F be ! FunctionAllocate(functionPrototype, Strict, "non-constructor")
async function asyncNonConstructorDecl() {}
assertThrows(
() => new asyncNonConstructorDecl(), TypeError);
assertThrows(
() => new (async function() {}), TypeError);
assertThrows(
() => new ({ async nonConstructor() {} }).nonConstructor(), TypeError);
assertThrows(
() => new (() => "not a constructor!"), TypeError);
assertThrows(
() => new (AsyncFunction()), TypeError);
assertThrows(
() => new (new AsyncFunction()), TypeError);
// Normal completion
async function asyncDecl() { return "test"; }
assertEqualsAsync("test", asyncDecl);
assertEqualsAsync("test2", async function() { return "test2"; });
assertEqualsAsync("test3", async () => "test3");
assertEqualsAsync("test4", () => ({ async f() { return "test4"; } }).f());
assertEqualsAsync("test5", () => AsyncFunction("no", "return 'test' + no;")(5));
assertEqualsAsync("test6",
() => (new AsyncFunction("no", "return 'test' + no;"))(6));
class MyError extends Error {};
// Throw completion
async function asyncDeclThrower(e) { throw new MyError(e); }
assertThrowsAsync(() => asyncDeclThrower("boom!"), MyError, "boom!");
assertThrowsAsync(
() => (async function(e) { throw new MyError(e); })("boom!!!"),
MyError, "boom!!!");
assertThrowsAsync(
() => (async e => { throw new MyError(e) })("boom!!"), MyError, "boom!!");
assertThrowsAsync(
() => ({ async thrower(e) { throw new MyError(e); } }).thrower("boom!1!"),
MyError, "boom!1!");
assertThrowsAsync(
() => AsyncFunction("msg", "throw new MyError(msg)")("boom!2!!"),
MyError, "boom!2!!");
assertThrowsAsync(
() => (new AsyncFunction("msg", "throw new MyError(msg)"))("boom!2!!!"),
MyError, "boom!2!!!");
function resolveLater(value) { return Promise.resolve(value); }
function rejectLater(error) { return Promise.reject(error); }
// Resume after Normal completion
var log = [];
async function resumeAfterNormal(value) {
log.push("start:" + value);
value = await resolveLater(value + 1);
log.push("resume:" + value);
value = await resolveLater(value + 1);
log.push("resume:" + value);
return value + 1;
}
assertEqualsAsync(4, () => resumeAfterNormal(1));
assertEquals("start:1 resume:2 resume:3", log.join(" "));
var O = {
async resumeAfterNormal(value) {
log.push("start:" + value);
value = await resolveLater(value + 1);
log.push("resume:" + value);
value = await resolveLater(value + 1);
log.push("resume:" + value);
return value + 1;
}
};
log = [];
assertEqualsAsync(5, () => O.resumeAfterNormal(2));
assertEquals("start:2 resume:3 resume:4", log.join(" "));
var resumeAfterNormalArrow = async (value) => {
log.push("start:" + value);
value = await resolveLater(value + 1);
log.push("resume:" + value);
value = await resolveLater(value + 1);
log.push("resume:" + value);
return value + 1;
};
log = [];
assertEqualsAsync(6, () => resumeAfterNormalArrow(3));
assertEquals("start:3 resume:4 resume:5", log.join(" "));
var resumeAfterNormalEval = AsyncFunction("value", `
log.push("start:" + value);
value = await resolveLater(value + 1);
log.push("resume:" + value);
value = await resolveLater(value + 1);
log.push("resume:" + value);
return value + 1;`);
log = [];
assertEqualsAsync(7, () => resumeAfterNormalEval(4));
assertEquals("start:4 resume:5 resume:6", log.join(" "));
var resumeAfterNormalNewEval = new AsyncFunction("value", `
log.push("start:" + value);
value = await resolveLater(value + 1);
log.push("resume:" + value);
value = await resolveLater(value + 1);
log.push("resume:" + value);
return value + 1;`);
log = [];
assertEqualsAsync(8, () => resumeAfterNormalNewEval(5));
assertEquals("start:5 resume:6 resume:7", log.join(" "));
// Resume after Throw completion
async function resumeAfterThrow(value) {
log.push("start:" + value);
try {
value = await rejectLater("throw1");
} catch (e) {
log.push("resume:" + e);
}
try {
value = await rejectLater("throw2");
} catch (e) {
log.push("resume:" + e);
}
return value + 1;
}
log = [];
assertEqualsAsync(2, () => resumeAfterThrow(1));
assertEquals("start:1 resume:throw1 resume:throw2", log.join(" "));
var O = {
async resumeAfterThrow(value) {
log.push("start:" + value);
try {
value = await rejectLater("throw1");
} catch (e) {
log.push("resume:" + e);
}
try {
value = await rejectLater("throw2");
} catch (e) {
log.push("resume:" + e);
}
return value + 1;
}
}
log = [];
assertEqualsAsync(3, () => O.resumeAfterThrow(2));
assertEquals("start:2 resume:throw1 resume:throw2", log.join(" "));
var resumeAfterThrowArrow = async (value) => {
log.push("start:" + value);
try {
value = await rejectLater("throw1");
} catch (e) {
log.push("resume:" + e);
}
try {
value = await rejectLater("throw2");
} catch (e) {
log.push("resume:" + e);
}
return value + 1;
};
log = [];
assertEqualsAsync(4, () => resumeAfterThrowArrow(3));
assertEquals("start:3 resume:throw1 resume:throw2", log.join(" "));
var resumeAfterThrowEval = AsyncFunction("value", `
log.push("start:" + value);
try {
value = await rejectLater("throw1");
} catch (e) {
log.push("resume:" + e);
}
try {
value = await rejectLater("throw2");
} catch (e) {
log.push("resume:" + e);
}
return value + 1;`);
log = [];
assertEqualsAsync(5, () => resumeAfterThrowEval(4));
assertEquals("start:4 resume:throw1 resume:throw2", log.join(" "));
var resumeAfterThrowNewEval = new AsyncFunction("value", `
log.push("start:" + value);
try {
value = await rejectLater("throw1");
} catch (e) {
log.push("resume:" + e);
}
try {
value = await rejectLater("throw2");
} catch (e) {
log.push("resume:" + e);
}
return value + 1;`);
log = [];
assertEqualsAsync(6, () => resumeAfterThrowNewEval(5));
assertEquals("start:5 resume:throw1 resume:throw2", log.join(" "));
async function foo() {}
assertEquals("async function foo() {}", foo.toString());
assertEquals("async function () {}", async function () {}.toString());
assertEquals("async x => x", (async x => x).toString());
assertEquals("async x => { return x }", (async x => { return x }).toString());
class AsyncMethod { async foo() { } }
assertEquals("async foo() { }",
Function.prototype.toString.call(AsyncMethod.prototype.foo));
assertEquals("async foo() { }",
Function.prototype.toString.call({async foo() { }}.foo));
// Async functions are not constructible
assertThrows(() => class extends (async function() {}) {}, TypeError);
// Regress v8:5148
assertEqualsAsync("1", () => (async({ a = NaN }) => a)({ a: "1" }));
assertEqualsAsync(
"10", () => (async(foo, { a = NaN }) => foo + a)("1", { a: "0" }));
assertEqualsAsync("2", () => (async({ a = "2" }) => a)({ a: undefined }));
assertEqualsAsync(
"20", () => (async(foo, { a = "0" }) => foo + a)("2", { a: undefined }));
assertThrows(() => eval("async({ foo = 1 })"), SyntaxError);
assertThrows(() => eval("async(a, { foo = 1 })"), SyntaxError);
// https://bugs.chromium.org/p/chromium/issues/detail?id=638019
async function gaga() {
let i = 1;
while (i-- > 0) { await 42 }
}
assertDoesNotThrow(gaga);
|