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 |
| 6 |
| 7 // TODO(caitp): give async functions a non-constructor map |
| 8 |
| 9 // Do not install `AsyncFunction` constructor on global object |
| 10 assertEquals(undefined, this.AsyncFunction); |
| 11 let AsyncFunction = (async function() {}).constructor; |
| 12 |
| 13 // Let functionPrototype be the intrinsic object %AsyncFunctionPrototype%. |
| 14 async function asyncFunctionForProto() {} |
| 15 assertEquals(AsyncFunction.prototype, |
| 16 Object.getPrototypeOf(asyncFunctionForProto)); |
| 17 assertEquals(AsyncFunction.prototype, |
| 18 Object.getPrototypeOf(async function() {})); |
| 19 assertEquals(AsyncFunction.prototype, Object.getPrototypeOf(async () => {})); |
| 20 assertEquals(AsyncFunction.prototype, |
| 21 Object.getPrototypeOf({ async method() {} }.method)); |
| 22 assertEquals(AsyncFunction.prototype, Object.getPrototypeOf(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 |
| 36 // AsyncFunction.prototype[ @@toStringTag ] |
| 37 var descriptor = |
| 38 Object.getOwnPropertyDescriptor(AsyncFunction.prototype, |
| 39 Symbol.toStringTag); |
| 40 assertEquals("AsyncFunction", descriptor.value); |
| 41 assertEquals(false, descriptor.enumerable); |
| 42 assertEquals(false, descriptor.writable); |
| 43 assertEquals(true, descriptor.configurable); |
| 44 |
| 45 assertEquals(1, AsyncFunction.length); |
| 46 |
| 47 // Let F be ! FunctionAllocate(functionPrototype, Strict, "non-constructor") |
| 48 async function asyncNonConstructorDecl() {} |
| 49 assertThrows( |
| 50 () => new asyncNonConstructorDecl(), TypeError); |
| 51 assertThrows( |
| 52 () => new (async function() {}), TypeError); |
| 53 assertThrows( |
| 54 () => new ({ async nonConstructor() {} }).nonConstructor(), TypeError); |
| 55 assertThrows( |
| 56 () => new (() => "not a constructor!"), TypeError); |
| 57 assertThrows( |
| 58 () => new (AsyncFunction()), TypeError); |
| 59 |
| 60 // Normal completion |
| 61 async function asyncDecl() { return "test"; } |
| 62 assertEqualsAsync("test", asyncDecl); |
| 63 assertEqualsAsync("test2", async function() { return "test2"; }); |
| 64 assertEqualsAsync("test3", async () => "test3"); |
| 65 assertEqualsAsync("test4", () => ({ async f() { return "test4"; } }).f()); |
| 66 assertEqualsAsync("test5", () => AsyncFunction("no", "return 'test' + no;")(5)); |
| 67 |
| 68 class MyError extends Error {}; |
| 69 |
| 70 // Throw completion |
| 71 async function asyncDeclThrower(e) { throw new MyError(e); } |
| 72 assertThrowsAsync(() => asyncDeclThrower("boom!"), MyError, "boom!"); |
| 73 assertThrowsAsync( |
| 74 () => (async function(e) { throw new MyError(e); })("boom!!!"), |
| 75 MyError, "boom!!!"); |
| 76 assertThrowsAsync( |
| 77 () => (async e => { throw new MyError(e) })("boom!!"), MyError, "boom!!"); |
| 78 assertThrowsAsync( |
| 79 () => ({ async thrower(e) { throw new MyError(e); } }).thrower("boom!1!"), |
| 80 MyError, "boom!1!"); |
| 81 assertThrowsAsync( |
| 82 () => AsyncFunction("msg", "throw new MyError(msg)")("boom!2!!"), |
| 83 MyError, "boom!2!!"); |
| 84 |
| 85 function resolveLater(value) { return Promise.resolve(value); } |
| 86 function rejectLater(error) { return Promise.reject(error); } |
| 87 |
| 88 // Resume after Normal completion |
| 89 var log = []; |
| 90 async function resumeAfterNormal(value) { |
| 91 log.push("start:" + value); |
| 92 value = await resolveLater(value + 1); |
| 93 log.push("resume:" + value); |
| 94 value = await resolveLater(value + 1); |
| 95 log.push("resume:" + value); |
| 96 return value + 1; |
| 97 } |
| 98 |
| 99 assertEqualsAsync(4, () => resumeAfterNormal(1)); |
| 100 assertEquals("start:1 resume:2 resume:3", log.join(" ")); |
| 101 |
| 102 var O = { |
| 103 async resumeAfterNormal(value) { |
| 104 log.push("start:" + value); |
| 105 value = await resolveLater(value + 1); |
| 106 log.push("resume:" + value); |
| 107 value = await resolveLater(value + 1); |
| 108 log.push("resume:" + value); |
| 109 return value + 1; |
| 110 } |
| 111 }; |
| 112 log = []; |
| 113 assertEqualsAsync(5, () => O.resumeAfterNormal(2)); |
| 114 assertEquals("start:2 resume:3 resume:4", log.join(" ")); |
| 115 |
| 116 var resumeAfterNormalArrow = async (value) => { |
| 117 try { |
| 118 log.push("start:" + value); |
| 119 value = await resolveLater(value + 1); |
| 120 log.push("resume:" + value); |
| 121 value = await resolveLater(value + 1); |
| 122 log.push("resume:" + value); |
| 123 return value + 1; |
| 124 } catch (e) { |
| 125 print(e.stack); |
| 126 } |
| 127 }; |
| 128 log = []; |
| 129 assertEqualsAsync(6, () => resumeAfterNormalArrow(3)); |
| 130 assertEquals("start:3 resume:4 resume:5", log.join(" ")); |
| 131 |
| 132 var resumeAfterNormalEval = AsyncFunction("value", ` |
| 133 log.push("start:" + value); |
| 134 value = await resolveLater(value + 1); |
| 135 log.push("resume:" + value); |
| 136 value = await resolveLater(value + 1); |
| 137 log.push("resume:" + value); |
| 138 return value + 1;`); |
| 139 log = []; |
| 140 assertEqualsAsync(7, () => resumeAfterNormalEval(4)); |
| 141 assertEquals("start:4 resume:5 resume:6", log.join(" ")); |
| 142 |
| 143 // Resume after Throw completion |
| 144 async function resumeAfterThrow(value) { |
| 145 log.push("start:" + value); |
| 146 try { |
| 147 value = await rejectLater("throw1"); |
| 148 } catch (e) { |
| 149 log.push("resume:" + e); |
| 150 } |
| 151 try { |
| 152 value = await rejectLater("throw2"); |
| 153 } catch (e) { |
| 154 log.push("resume:" + e); |
| 155 } |
| 156 return value + 1; |
| 157 } |
| 158 |
| 159 log = []; |
| 160 assertEqualsAsync(2, () => resumeAfterThrow(1)); |
| 161 assertEquals("start:1 resume:throw1 resume:throw2", log.join(" ")); |
| 162 |
| 163 var O = { |
| 164 async resumeAfterThrow(value) { |
| 165 log.push("start:" + value); |
| 166 try { |
| 167 value = await rejectLater("throw1"); |
| 168 } catch (e) { |
| 169 log.push("resume:" + e); |
| 170 } |
| 171 try { |
| 172 value = await rejectLater("throw2"); |
| 173 } catch (e) { |
| 174 log.push("resume:" + e); |
| 175 } |
| 176 return value + 1; |
| 177 } |
| 178 } |
| 179 log = []; |
| 180 assertEqualsAsync(3, () => O.resumeAfterThrow(2)); |
| 181 assertEquals("start:2 resume:throw1 resume:throw2", log.join(" ")); |
| 182 |
| 183 var resumeAfterThrowArrow = async (value) => { |
| 184 log.push("start:" + value); |
| 185 try { |
| 186 value = await rejectLater("throw1"); |
| 187 } catch (e) { |
| 188 log.push("resume:" + e); |
| 189 } |
| 190 try { |
| 191 value = await rejectLater("throw2"); |
| 192 } catch (e) { |
| 193 log.push("resume:" + e); |
| 194 } |
| 195 return value + 1; |
| 196 }; |
| 197 |
| 198 log = []; |
| 199 |
| 200 assertEqualsAsync(4, () => resumeAfterThrowArrow(3)); |
| 201 assertEquals("start:3 resume:throw1 resume:throw2", log.join(" ")); |
| 202 |
| 203 var resumeAfterThrowEval = AsyncFunction("value", ` |
| 204 log.push("start:" + value); |
| 205 try { |
| 206 value = await rejectLater("throw1"); |
| 207 } catch (e) { |
| 208 log.push("resume:" + e); |
| 209 } |
| 210 try { |
| 211 value = await rejectLater("throw2"); |
| 212 } catch (e) { |
| 213 log.push("resume:" + e); |
| 214 } |
| 215 return value + 1;`); |
| 216 log = []; |
| 217 assertEqualsAsync(5, () => resumeAfterThrowEval(4)); |
| 218 assertEquals("start:4 resume:throw1 resume:throw2", log.join(" ")); |
OLD | NEW |