| 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 function assertEqualsAsync(expected, run, msg) { | |
| 8 var actual; | |
| 9 var hadValue = false; | |
| 10 var hadError = false; | |
| 11 var promise = run(); | |
| 12 | |
| 13 if (typeof promise !== "object" || typeof promise.then !== "function") { | |
| 14 throw new MjsUnitAssertionError( | |
| 15 "Expected " + run.toString() + | |
| 16 " to return a Promise, but it returned " + PrettyPrint(promise)); | |
| 17 } | |
| 18 | |
| 19 promise.then(function(value) { hadValue = true; actual = value; }, | |
| 20 function(error) { hadError = true; actual = error; }); | |
| 21 | |
| 22 assertFalse(hadValue || hadError); | |
| 23 | |
| 24 %RunMicrotasks(); | |
| 25 | |
| 26 if (hadError) throw actual; | |
| 27 | |
| 28 assertTrue( | |
| 29 hadValue, "Expected '" + run.toString() + "' to produce a value"); | |
| 30 | |
| 31 assertEquals(expected, actual, msg); | |
| 32 }; | |
| 33 | |
| 34 // Rename a function so that it can help omit things from stack trace. | |
| 35 function test(fn) { | |
| 36 return Object.defineProperty(fn, "name", { | |
| 37 enumerable: false, | |
| 38 configurable: true, | |
| 39 value: "@" + fn.name, | |
| 40 writable: false | |
| 41 }); | |
| 42 } | |
| 43 | |
| 44 function getStack(error) { | |
| 45 var stack = error.stack.split('\n'). | |
| 46 filter(function(line) { | |
| 47 return /^\s*at @?[a-zA-Z0-9_]/.test(line); | |
| 48 }). | |
| 49 map(line => line.replace(/^\s*at (@?[a-zA-Z0-9_\.\[\]]+)(.*)/, "$1")); | |
| 50 | |
| 51 // remove `Promise.then()` invocation by assertEqualsAsync() | |
| 52 if (stack[2] === "assertEqualsAsync") return []; | |
| 53 | |
| 54 return stack.reverse(); | |
| 55 } | |
| 56 | |
| 57 var log = []; | |
| 58 class FakePromise extends Promise { | |
| 59 constructor(executor) { | |
| 60 var stack = getStack(new Error("Getting Callstack")); | |
| 61 if (stack.length) { | |
| 62 var first = -1; | |
| 63 for (var i = 0; i < stack.length; ++i) { | |
| 64 if (stack[i][0] === '@') { | |
| 65 first = i; | |
| 66 break; | |
| 67 } | |
| 68 } | |
| 69 while (first > 0) stack.shift(), --first; | |
| 70 if (stack.length) { | |
| 71 log.push("@@Species: [" + stack.join(" > ") + "]"); | |
| 72 } | |
| 73 } | |
| 74 return new Promise(executor); | |
| 75 } | |
| 76 }; | |
| 77 | |
| 78 Object.defineProperty(Promise, Symbol.species, { | |
| 79 value: FakePromise, | |
| 80 configurable: true, | |
| 81 enumerable: false, | |
| 82 writable: false | |
| 83 }); | |
| 84 | |
| 85 // Internal `AsyncFunctionAwait` only --- no @@species invocations. | |
| 86 async function asyncFn() { return await "foo"; } | |
| 87 assertEqualsAsync("foo", test(function testInternalOnly() { return asyncFn(); }, | |
| 88 "should not call Promise[@@Species]")); | |
| 89 assertEquals([], log); | |
| 90 | |
| 91 log.length = 0; | |
| 92 assertEqualsAsync( | |
| 93 "foo", | |
| 94 test(function testThenOnReturnedPromise() { | |
| 95 return asyncFn().then(x => (log.push("Then: " + x), x)); | |
| 96 }), | |
| 97 "should call Promise[@@Species] after non-internal Then"); | |
| 98 assertEquals([ | |
| 99 "@@Species: [@testThenOnReturnedPromise > Promise.then > FakePromise]", | |
| 100 "Then: foo" | |
| 101 ], log); | |
| OLD | NEW |