| OLD | NEW |
| (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 --allow-natives-syntax |
| 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 assertEquals(AsyncFunction.prototype, |
| 22 Object.getPrototypeOf(new AsyncFunction())); |
| 23 |
| 24 // AsyncFunctionCreate does not produce an object with a Prototype |
| 25 assertEquals(undefined, asyncFunctionForProto.prototype); |
| 26 assertEquals(false, asyncFunctionForProto.hasOwnProperty("prototype")); |
| 27 assertEquals(undefined, (async function() {}).prototype); |
| 28 assertEquals(false, (async function() {}).hasOwnProperty("prototype")); |
| 29 assertEquals(undefined, (async() => {}).prototype); |
| 30 assertEquals(false, (async() => {}).hasOwnProperty("prototype")); |
| 31 assertEquals(undefined, ({ async method() {} }).method.prototype); |
| 32 assertEquals(false, ({ async method() {} }).method.hasOwnProperty("prototype")); |
| 33 assertEquals(undefined, AsyncFunction().prototype); |
| 34 assertEquals(false, AsyncFunction().hasOwnProperty("prototype")); |
| 35 assertEquals(undefined, (new AsyncFunction()).prototype); |
| 36 assertEquals(false, (new AsyncFunction()).hasOwnProperty("prototype")); |
| 37 |
| 38 assertEquals(1, async function(a) { await 1; }.length); |
| 39 assertEquals(2, async function(a, b) { await 1; }.length); |
| 40 assertEquals(1, async function(a, b = 2) { await 1; }.length); |
| 41 assertEquals(2, async function(a, b, ...c) { await 1; }.length); |
| 42 |
| 43 assertEquals(1, (async(a) => await 1).length); |
| 44 assertEquals(2, (async(a, b) => await 1).length); |
| 45 assertEquals(1, (async(a, b = 2) => await 1).length); |
| 46 assertEquals(2, (async(a, b, ...c) => await 1).length); |
| 47 |
| 48 assertEquals(1, ({ async f(a) { await 1; } }).f.length); |
| 49 assertEquals(2, ({ async f(a, b) { await 1; } }).f.length); |
| 50 assertEquals(1, ({ async f(a, b = 2) { await 1; } }).f.length); |
| 51 assertEquals(2, ({ async f(a, b, ...c) { await 1; } }).f.length); |
| 52 |
| 53 assertEquals(1, AsyncFunction("a", "await 1").length); |
| 54 assertEquals(2, AsyncFunction("a", "b", "await 1").length); |
| 55 assertEquals(1, AsyncFunction("a", "b = 2", "await 1").length); |
| 56 assertEquals(2, AsyncFunction("a", "b", "...c", "await 1").length); |
| 57 |
| 58 assertEquals(1, (new AsyncFunction("a", "await 1")).length); |
| 59 assertEquals(2, (new AsyncFunction("a", "b", "await 1")).length); |
| 60 assertEquals(1, (new AsyncFunction("a", "b = 2", "await 1")).length); |
| 61 assertEquals(2, (new AsyncFunction("a", "b", "...c", "await 1")).length); |
| 62 |
| 63 // AsyncFunction.prototype[ @@toStringTag ] |
| 64 var descriptor = |
| 65 Object.getOwnPropertyDescriptor(AsyncFunction.prototype, |
| 66 Symbol.toStringTag); |
| 67 assertEquals("AsyncFunction", descriptor.value); |
| 68 assertEquals(false, descriptor.enumerable); |
| 69 assertEquals(false, descriptor.writable); |
| 70 assertEquals(true, descriptor.configurable); |
| 71 |
| 72 assertEquals(1, AsyncFunction.length); |
| 73 |
| 74 // Let F be ! FunctionAllocate(functionPrototype, Strict, "non-constructor") |
| 75 async function asyncNonConstructorDecl() {} |
| 76 assertThrows( |
| 77 () => new asyncNonConstructorDecl(), TypeError); |
| 78 assertThrows( |
| 79 () => new (async function() {}), TypeError); |
| 80 assertThrows( |
| 81 () => new ({ async nonConstructor() {} }).nonConstructor(), TypeError); |
| 82 assertThrows( |
| 83 () => new (() => "not a constructor!"), TypeError); |
| 84 assertThrows( |
| 85 () => new (AsyncFunction()), TypeError); |
| 86 assertThrows( |
| 87 () => new (new AsyncFunction()), TypeError); |
| 88 |
| 89 // Normal completion |
| 90 async function asyncDecl() { return "test"; } |
| 91 assertEqualsAsync("test", asyncDecl); |
| 92 assertEqualsAsync("test2", async function() { return "test2"; }); |
| 93 assertEqualsAsync("test3", async () => "test3"); |
| 94 assertEqualsAsync("test4", () => ({ async f() { return "test4"; } }).f()); |
| 95 assertEqualsAsync("test5", () => AsyncFunction("no", "return 'test' + no;")(5)); |
| 96 assertEqualsAsync("test6", |
| 97 () => (new AsyncFunction("no", "return 'test' + no;"))(6)); |
| 98 |
| 99 class MyError extends Error {}; |
| 100 |
| 101 // Throw completion |
| 102 async function asyncDeclThrower(e) { throw new MyError(e); } |
| 103 assertThrowsAsync(() => asyncDeclThrower("boom!"), MyError, "boom!"); |
| 104 assertThrowsAsync( |
| 105 () => (async function(e) { throw new MyError(e); })("boom!!!"), |
| 106 MyError, "boom!!!"); |
| 107 assertThrowsAsync( |
| 108 () => (async e => { throw new MyError(e) })("boom!!"), MyError, "boom!!"); |
| 109 assertThrowsAsync( |
| 110 () => ({ async thrower(e) { throw new MyError(e); } }).thrower("boom!1!"), |
| 111 MyError, "boom!1!"); |
| 112 assertThrowsAsync( |
| 113 () => AsyncFunction("msg", "throw new MyError(msg)")("boom!2!!"), |
| 114 MyError, "boom!2!!"); |
| 115 assertThrowsAsync( |
| 116 () => (new AsyncFunction("msg", "throw new MyError(msg)"))("boom!2!!!"), |
| 117 MyError, "boom!2!!!"); |
| 118 |
| 119 function resolveLater(value) { return Promise.resolve(value); } |
| 120 function rejectLater(error) { return Promise.reject(error); } |
| 121 |
| 122 // Resume after Normal completion |
| 123 var log = []; |
| 124 async function resumeAfterNormal(value) { |
| 125 log.push("start:" + value); |
| 126 value = await resolveLater(value + 1); |
| 127 log.push("resume:" + value); |
| 128 value = await resolveLater(value + 1); |
| 129 log.push("resume:" + value); |
| 130 return value + 1; |
| 131 } |
| 132 |
| 133 assertEqualsAsync(4, () => resumeAfterNormal(1)); |
| 134 assertEquals("start:1 resume:2 resume:3", log.join(" ")); |
| 135 |
| 136 var O = { |
| 137 async resumeAfterNormal(value) { |
| 138 log.push("start:" + value); |
| 139 value = await resolveLater(value + 1); |
| 140 log.push("resume:" + value); |
| 141 value = await resolveLater(value + 1); |
| 142 log.push("resume:" + value); |
| 143 return value + 1; |
| 144 } |
| 145 }; |
| 146 log = []; |
| 147 assertEqualsAsync(5, () => O.resumeAfterNormal(2)); |
| 148 assertEquals("start:2 resume:3 resume:4", log.join(" ")); |
| 149 |
| 150 var resumeAfterNormalArrow = async (value) => { |
| 151 log.push("start:" + value); |
| 152 value = await resolveLater(value + 1); |
| 153 log.push("resume:" + value); |
| 154 value = await resolveLater(value + 1); |
| 155 log.push("resume:" + value); |
| 156 return value + 1; |
| 157 }; |
| 158 log = []; |
| 159 assertEqualsAsync(6, () => resumeAfterNormalArrow(3)); |
| 160 assertEquals("start:3 resume:4 resume:5", log.join(" ")); |
| 161 |
| 162 var resumeAfterNormalEval = AsyncFunction("value", ` |
| 163 log.push("start:" + value); |
| 164 value = await resolveLater(value + 1); |
| 165 log.push("resume:" + value); |
| 166 value = await resolveLater(value + 1); |
| 167 log.push("resume:" + value); |
| 168 return value + 1;`); |
| 169 log = []; |
| 170 assertEqualsAsync(7, () => resumeAfterNormalEval(4)); |
| 171 assertEquals("start:4 resume:5 resume:6", log.join(" ")); |
| 172 |
| 173 var resumeAfterNormalNewEval = new AsyncFunction("value", ` |
| 174 log.push("start:" + value); |
| 175 value = await resolveLater(value + 1); |
| 176 log.push("resume:" + value); |
| 177 value = await resolveLater(value + 1); |
| 178 log.push("resume:" + value); |
| 179 return value + 1;`); |
| 180 log = []; |
| 181 assertEqualsAsync(8, () => resumeAfterNormalNewEval(5)); |
| 182 assertEquals("start:5 resume:6 resume:7", log.join(" ")); |
| 183 |
| 184 // Resume after Throw completion |
| 185 async function resumeAfterThrow(value) { |
| 186 log.push("start:" + value); |
| 187 try { |
| 188 value = await rejectLater("throw1"); |
| 189 } catch (e) { |
| 190 log.push("resume:" + e); |
| 191 } |
| 192 try { |
| 193 value = await rejectLater("throw2"); |
| 194 } catch (e) { |
| 195 log.push("resume:" + e); |
| 196 } |
| 197 return value + 1; |
| 198 } |
| 199 |
| 200 log = []; |
| 201 assertEqualsAsync(2, () => resumeAfterThrow(1)); |
| 202 assertEquals("start:1 resume:throw1 resume:throw2", log.join(" ")); |
| 203 |
| 204 var O = { |
| 205 async resumeAfterThrow(value) { |
| 206 log.push("start:" + value); |
| 207 try { |
| 208 value = await rejectLater("throw1"); |
| 209 } catch (e) { |
| 210 log.push("resume:" + e); |
| 211 } |
| 212 try { |
| 213 value = await rejectLater("throw2"); |
| 214 } catch (e) { |
| 215 log.push("resume:" + e); |
| 216 } |
| 217 return value + 1; |
| 218 } |
| 219 } |
| 220 log = []; |
| 221 assertEqualsAsync(3, () => O.resumeAfterThrow(2)); |
| 222 assertEquals("start:2 resume:throw1 resume:throw2", log.join(" ")); |
| 223 |
| 224 var resumeAfterThrowArrow = async (value) => { |
| 225 log.push("start:" + value); |
| 226 try { |
| 227 value = await rejectLater("throw1"); |
| 228 } catch (e) { |
| 229 log.push("resume:" + e); |
| 230 } |
| 231 try { |
| 232 value = await rejectLater("throw2"); |
| 233 } catch (e) { |
| 234 log.push("resume:" + e); |
| 235 } |
| 236 return value + 1; |
| 237 }; |
| 238 |
| 239 log = []; |
| 240 |
| 241 assertEqualsAsync(4, () => resumeAfterThrowArrow(3)); |
| 242 assertEquals("start:3 resume:throw1 resume:throw2", log.join(" ")); |
| 243 |
| 244 var resumeAfterThrowEval = AsyncFunction("value", ` |
| 245 log.push("start:" + value); |
| 246 try { |
| 247 value = await rejectLater("throw1"); |
| 248 } catch (e) { |
| 249 log.push("resume:" + e); |
| 250 } |
| 251 try { |
| 252 value = await rejectLater("throw2"); |
| 253 } catch (e) { |
| 254 log.push("resume:" + e); |
| 255 } |
| 256 return value + 1;`); |
| 257 log = []; |
| 258 assertEqualsAsync(5, () => resumeAfterThrowEval(4)); |
| 259 assertEquals("start:4 resume:throw1 resume:throw2", log.join(" ")); |
| 260 |
| 261 var resumeAfterThrowNewEval = new AsyncFunction("value", ` |
| 262 log.push("start:" + value); |
| 263 try { |
| 264 value = await rejectLater("throw1"); |
| 265 } catch (e) { |
| 266 log.push("resume:" + e); |
| 267 } |
| 268 try { |
| 269 value = await rejectLater("throw2"); |
| 270 } catch (e) { |
| 271 log.push("resume:" + e); |
| 272 } |
| 273 return value + 1;`); |
| 274 log = []; |
| 275 assertEqualsAsync(6, () => resumeAfterThrowNewEval(5)); |
| 276 assertEquals("start:5 resume:throw1 resume:throw2", log.join(" ")); |
| OLD | NEW |