Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: test/mjsunit/harmony/async-await-basic.js

Issue 1895603002: [esnext] prototype runtime implementation for async functions (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@AsyncFunction
Patch Set: Fix more bugs Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Flags: --harmony-async-await
6
7 // Do not install `AsyncFunction` constructor on global object
8 assertEquals(undefined, this.AsyncFunction);
9 let AsyncFunction = (async function() {}).constructor;
10
11 // Let functionPrototype be the intrinsic object %AsyncFunctionPrototype%.
12 async function asyncFunctionForProto() {}
13 assertEquals(AsyncFunction.prototype,
14 Object.getPrototypeOf(asyncFunctionForProto));
15 assertEquals(AsyncFunction.prototype,
16 Object.getPrototypeOf(async function() {}));
17 assertEquals(AsyncFunction.prototype, Object.getPrototypeOf(async () => {}));
18 assertEquals(AsyncFunction.prototype,
19 Object.getPrototypeOf({ async method() {} }.method));
20 assertEquals(AsyncFunction.prototype, Object.getPrototypeOf(AsyncFunction()));
21
22 // AsyncFunctionCreate does not produce an object with a Prototype
23 assertEquals(undefined, asyncFunctionForProto.prototype);
24 assertEquals(false, asyncFunctionForProto.hasOwnProperty("prototype"));
25 assertEquals(undefined, (async function() {}).prototype);
26 assertEquals(false, (async function() {}).hasOwnProperty("prototype"));
27 assertEquals(undefined, (async() => {}).prototype);
28 assertEquals(false, (async() => {}).hasOwnProperty("prototype"));
29 assertEquals(undefined, ({ async method() {} }).method.prototype);
30 assertEquals(false, ({ async method() {} }).method.hasOwnProperty("prototype"));
31 assertEquals(undefined, AsyncFunction().prototype);
32 assertEquals(false, AsyncFunction().hasOwnProperty("prototype"));
33
34 // AsyncFunction.prototype[ @@toStringTag ]
35 var descriptor =
36 Object.getOwnPropertyDescriptor(AsyncFunction.prototype,
37 Symbol.toStringTag);
38 assertEquals("AsyncFunction", descriptor.value);
39 assertEquals(false, descriptor.enumerable);
40 assertEquals(false, descriptor.writable);
41 assertEquals(true, descriptor.configurable);
42
43 assertEquals(1, AsyncFunction.length);
44
45 // Let F be ! FunctionAllocate(functionPrototype, Strict, "non-constructor")
46 async function asyncNonConstructorDecl() {}
47 assertThrows(
48 () => new asyncNonConstructorDecl(), TypeError);
49 assertThrows(
50 () => new (async function() {}), TypeError);
51 assertThrows(
52 () => new ({ async nonConstructor() {} }).nonConstructor(), TypeError);
53 assertThrows(
54 () => new (() => "not a constructor!"), TypeError);
55 assertThrows(
56 () => new (AsyncFunction()), TypeError);
57
58 // Normal completion
59 async function asyncDecl() { return "test"; }
60 assertEqualsAsync("test", asyncDecl);
61 assertEqualsAsync("test2", async function() { return "test2"; });
62 assertEqualsAsync("test3", async () => "test3");
63 assertEqualsAsync("test4", () => ({ async f() { return "test4"; } }).f());
64 assertEqualsAsync("test5", () => AsyncFunction("no", "return 'test' + no;")(5));
65
66 class MyError extends Error {};
67
68 // Throw completion
69 async function asyncDeclThrower(e) { throw new MyError(e); }
70 assertThrowsAsync(() => asyncDeclThrower("boom!"), MyError, "boom!");
71 assertThrowsAsync(
72 () => (async function(e) { throw new MyError(e); })("boom!!!"),
73 MyError, "boom!!!");
74 assertThrowsAsync(
75 () => (async e => { throw new MyError(e) })("boom!!"), MyError, "boom!!");
76 assertThrowsAsync(
77 () => ({ async thrower(e) { throw new MyError(e); } }).thrower("boom!1!"),
78 MyError, "boom!1!");
79 assertThrowsAsync(
80 () => AsyncFunction("msg", "throw new MyError(msg)")("boom!2!!"),
81 MyError, "boom!2!!");
82
83 function resolveLater(value) { return Promise.resolve(value); }
84 function rejectLater(error) { return Promise.reject(error); }
85
86 // Resume after Normal completion
87 var log = [];
88 async function resumeAfterNormal(value) {
89 log.push("start:" + value);
90 value = await resolveLater(value + 1);
91 log.push("resume:" + value);
92 value = await resolveLater(value + 1);
93 log.push("resume:" + value);
94 return value + 1;
95 }
96
97 assertEqualsAsync(4, () => resumeAfterNormal(1));
98 assertEquals("start:1 resume:2 resume:3", log.join(" "));
99
100 var O = {
101 async resumeAfterNormal(value) {
102 log.push("start:" + value);
103 value = await resolveLater(value + 1);
104 log.push("resume:" + value);
105 value = await resolveLater(value + 1);
106 log.push("resume:" + value);
107 return value + 1;
108 }
109 };
110 log = [];
111 assertEqualsAsync(5, () => O.resumeAfterNormal(2));
112 assertEquals("start:2 resume:3 resume:4", log.join(" "));
113
114 var resumeAfterNormalArrow = async (value) => {
115 log.push("start:" + value);
116 value = await resolveLater(value + 1);
117 log.push("resume:" + value);
118 value = await resolveLater(value + 1);
119 log.push("resume:" + value);
120 return value + 1;
121 };
122 log = [];
123 assertEqualsAsync(6, () => resumeAfterNormalArrow(3));
124 assertEquals("start:3 resume:4 resume:5", log.join(" "));
125
126 var resumeAfterNormalEval = AsyncFunction("value", `
127 log.push("start:" + value);
128 value = await resolveLater(value + 1);
129 log.push("resume:" + value);
130 value = await resolveLater(value + 1);
131 log.push("resume:" + value);
132 return value + 1;`);
133 log = [];
134 assertEqualsAsync(7, () => resumeAfterNormalEval(4));
135 assertEquals("start:4 resume:5 resume:6", log.join(" "));
136
137 // Resume after Throw completion
138 async function resumeAfterThrow(value) {
139 log.push("start:" + value);
140 try {
141 value = await rejectLater("throw1");
142 } catch (e) {
143 log.push("resume:" + e);
144 }
145 try {
146 value = await rejectLater("throw2");
147 } catch (e) {
148 log.push("resume:" + e);
149 }
150 return value + 1;
151 }
152
153 log = [];
154 assertEqualsAsync(2, () => resumeAfterThrow(1));
155 assertEquals("start:1 resume:throw1 resume:throw2", log.join(" "));
156
157 var O = {
158 async resumeAfterThrow(value) {
159 log.push("start:" + value);
160 try {
161 value = await rejectLater("throw1");
162 } catch (e) {
163 log.push("resume:" + e);
164 }
165 try {
166 value = await rejectLater("throw2");
167 } catch (e) {
168 log.push("resume:" + e);
169 }
170 return value + 1;
171 }
172 }
173 log = [];
174 assertEqualsAsync(3, () => O.resumeAfterThrow(2));
175 assertEquals("start:2 resume:throw1 resume:throw2", log.join(" "));
176
177 var resumeAfterThrowArrow = async (value) => {
178 log.push("start:" + value);
179 try {
180 value = await rejectLater("throw1");
181 } catch (e) {
182 log.push("resume:" + e);
183 }
184 try {
185 value = await rejectLater("throw2");
186 } catch (e) {
187 log.push("resume:" + e);
188 }
189 return value + 1;
190 };
191
192 log = [];
193
194 assertEqualsAsync(4, () => resumeAfterThrowArrow(3));
195 assertEquals("start:3 resume:throw1 resume:throw2", log.join(" "));
196
197 var resumeAfterThrowEval = AsyncFunction("value", `
198 log.push("start:" + value);
199 try {
200 value = await rejectLater("throw1");
201 } catch (e) {
202 log.push("resume:" + e);
203 }
204 try {
205 value = await rejectLater("throw2");
206 } catch (e) {
207 log.push("resume:" + e);
208 }
209 return value + 1;`);
210 log = [];
211 assertEqualsAsync(5, () => resumeAfterThrowEval(4));
212 assertEquals("start:4 resume:throw1 resume:throw2", log.join(" "));
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698