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 function assertEqualsAsync(expected, run, msg) { |
| 8 var actual; |
| 9 var hadValue = false; |
| 10 var hadError = false; |
| 11 run().then(function(value) { hadValue = true; actual = value; }, |
| 12 function(error) { hadError = true; actual = error; }); |
| 13 flushMicrotasks(); |
| 14 |
| 15 if (hadError) |
| 16 throw actual; |
| 17 assertTrue( |
| 18 hadValue, "Expected '" + run.toString() + "' to produce a value"); |
| 19 |
| 20 assertEquals(expected, actual, msg); |
| 21 } |
| 22 |
| 23 function assertThrowsAsync(run, errorType, message) { |
| 24 let actual; |
| 25 var hadError = false; |
| 26 run().then(function(value) { actual = value; }, |
| 27 function(error) { hadError = true; actual = error; }); |
| 28 flushMicrotasks(); |
| 29 |
| 30 if (!hadError) |
| 31 throw new Error( |
| 32 "Expected " + run + "() to throw " + errorType.name + |
| 33 ", but did not throw."); |
| 34 if (!(actual instanceof errorType)) |
| 35 throw new Error( |
| 36 "Expected " + run + "() to throw " + errorType.name + |
| 37 ", but threw '" + actual + "'"); |
| 38 if (message !== void 0 && actual.message !== message) |
| 39 throw new Error( |
| 40 "Expected " + run + "() to throw '" + message + "', but threw '" + |
| 41 actual.message + "'"); |
| 42 } |
| 43 |
| 44 // TODO(caitp): give async functions a non-constructor map |
| 45 |
| 46 // Let F be ! FunctionAllocate(functionPrototype, Strict, "non-constructor") |
| 47 //async function asyncNonConstructorDecl() {} |
| 48 //assertThrows( |
| 49 // () => new asyncNonConstructorDecl(), TypeError); |
| 50 //assertThrows( |
| 51 // () => new (async function() {}), TypeError); |
| 52 //assertThrows( |
| 53 // () => new ({ async nonConstructor() {} }).nonConstructor(), TypeError); |
| 54 //assertThrows( |
| 55 // () => new (() => "not a constructor!"), TypeError); |
| 56 |
| 57 // Normal completion |
| 58 async function asyncDecl() { return "test"; } |
| 59 assertEqualsAsync("test", asyncDecl); |
| 60 assertEqualsAsync("test2", async function() { return "test2"; }); |
| 61 |
| 62 // TODO(caitp): generate proper code for async arrows |
| 63 |
| 64 // assertEqualsAsync("test3", async () => "test3"); |
| 65 // assertEqualsAsync("test4", () => ({ async f() { return "test4"; } }).f()); |
| 66 |
| 67 class MyError extends Error {}; |
| 68 |
| 69 // TODO(caitp): make catch handler lookup work correctly |
| 70 |
| 71 // Throw completion |
| 72 // async function asyncDeclThrower(e) { throw new MyError(e); } |
| 73 //assertThrowsAsync( |
| 74 // () => asyncDeclThrower("boom!"), MyError, "boom!"); |
| 75 //assertThrowsAsync( |
| 76 // () => (async function(e) { throw new MyError(e); })("boom!!!"), |
| 77 // MyError, "boom!!!"); |
| 78 //assertThrowsAsync( |
| 79 // () => (async e => { throw new MyError(e) })("boom!!"), MyError, "boom!!"); |
| 80 //assertThrowsAsync( |
| 81 // () => ({ async thrower(e) { throw new MyError(e); } }).thrower("boom!1!"), |
| 82 // MyError, "boom!1!"); |
| 83 |
| 84 function resolveLater(value) { return Promise.resolve(value); } |
| 85 function rejectLater(error) { return Promise.reject(error); } |
| 86 |
| 87 // Resume after Normal completion |
| 88 var log = []; |
| 89 async function resumeAfterNormal(value) { |
| 90 log.push("start:" + value); |
| 91 value = await resolveLater(value + 1); |
| 92 log.push("resume:" + value); |
| 93 value = await resolveLater(value + 1); |
| 94 log.push("resume:" + value); |
| 95 return value + 1; |
| 96 } |
| 97 |
| 98 assertEqualsAsync(4, () => resumeAfterNormal(1)); |
| 99 assertEquals("start:1 resume:2 resume:3", log.join(" ")); |
| 100 |
| 101 var O = { |
| 102 async resumeAfterNormal(value) { |
| 103 log.push("start:" + value); |
| 104 value = await resolveLater(value + 1); |
| 105 log.push("resume:" + value); |
| 106 value = await resolveLater(value + 1); |
| 107 log.push("resume:" + value); |
| 108 return value + 1; |
| 109 } |
| 110 }; |
| 111 log = []; |
| 112 assertEqualsAsync(5, () => O.resumeAfterNormal(2)); |
| 113 assertEquals("start:2 resume:3 resume:4", log.join(" ")); |
| 114 |
| 115 // Resume after Throw completion |
| 116 async function resumeAfterThrow(value) { |
| 117 log.push("start:" + value); |
| 118 try { |
| 119 value = await rejectLater("throw1"); |
| 120 } catch (e) { |
| 121 log.push("resume:" + e); |
| 122 } |
| 123 try { |
| 124 value = await rejectLater("throw2"); |
| 125 } catch (e) { |
| 126 log.push("resume:" + e); |
| 127 } |
| 128 return value + 1; |
| 129 } |
| 130 |
| 131 log = []; |
| 132 assertEqualsAsync(2, () => resumeAfterThrow(1)); |
| 133 assertEquals("start:1 resume:throw1 resume:throw2", log.join(" ")); |
| 134 |
| 135 var O = { |
| 136 async resumeAfterThrow(value) { |
| 137 log.push("start:" + value); |
| 138 try { |
| 139 value = await rejectLater("throw1"); |
| 140 } catch (e) { |
| 141 log.push("resume:" + e); |
| 142 } |
| 143 try { |
| 144 value = await rejectLater("throw2"); |
| 145 } catch (e) { |
| 146 log.push("resume:" + e); |
| 147 } |
| 148 return value + 1; |
| 149 } |
| 150 } |
| 151 log = []; |
| 152 assertEqualsAsync(3, () => O.resumeAfterThrow(2)); |
| 153 assertEquals("start:2 resume:throw1 resume:throw2", log.join(" ")); |
OLD | NEW |