diff --git a/js/src/tests/ecma_2017/AsyncFunctions/ErrorStack.js b/js/src/tests/ecma_2017/AsyncFunctions/ErrorStack.js new file mode 100644 index 000000000000..75747da99bde --- /dev/null +++ b/js/src/tests/ecma_2017/AsyncFunctions/ErrorStack.js @@ -0,0 +1,103 @@ +// |reftest| skip-if(!xulRuntime.shell) -- needs drainJobQueue + +var BUGNUMBER = 1343158; +var summary = "Error.stack should provide meaningful stack trace in async function"; + +print(BUGNUMBER + ": " + summary); + +let COOKIE = "C0F5DBB89807"; + +async function thrower() { + let stack = new Error().stack; // line 11 + assertEq(/^thrower@.+ErrorStack.js:11/m.test(stack), true, toMessage(stack)); + assertEq(/^async\*inner@.+ErrorStack.js:38/m.test(stack), true, toMessage(stack)); + assertEq(/^async\*middle@.+ErrorStack.js:58/m.test(stack), true, toMessage(stack)); + assertEq(/^async\*outer@.+ErrorStack.js:78/m.test(stack), true, toMessage(stack)); + assertEq(/^async\*@.+ErrorStack.js:82/m.test(stack), true, toMessage(stack)); + + throw new Error(COOKIE); // line 18 +} + +async function inner() { + let stack = new Error().stack; // line 22 + assertEq(/thrower@.+ErrorStack.js/m.test(stack), false, toMessage(stack)); + assertEq(/^inner@.+ErrorStack.js:22/m.test(stack), true, toMessage(stack)); + assertEq(/^async\*middle@.+ErrorStack.js:58/m.test(stack), true, toMessage(stack)); + assertEq(/^async\*outer@.+ErrorStack.js:78/m.test(stack), true, toMessage(stack)); + assertEq(/^async\*@.+ErrorStack.js:82/m.test(stack), true, toMessage(stack)); + + await Promise.resolve(100); + + stack = new Error().stack; // line 31 + assertEq(/thrower@.+ErrorStack.js/m.test(stack), false, toMessage(stack)); + assertEq(/^inner@.+ErrorStack.js:31/m.test(stack), true, toMessage(stack)); + assertEq(/^async\*middle@.+ErrorStack.js:58/m.test(stack), true, toMessage(stack)); + assertEq(/^async\*outer@.+ErrorStack.js:78/m.test(stack), true, toMessage(stack)); + assertEq(/^async\*@.+ErrorStack.js:82/m.test(stack), true, toMessage(stack)); + + await thrower(); // line 38 +} + +async function middle() { + let stack = new Error().stack; // line 42 + assertEq(/thrower@.+ErrorStack.js/m.test(stack), false, toMessage(stack)); + assertEq(/inner@.+ErrorStack.js/m.test(stack), false, toMessage(stack)); + assertEq(/^middle@.+ErrorStack.js:42/m.test(stack), true, toMessage(stack)); + assertEq(/^async\*outer@.+ErrorStack.js:78/m.test(stack), true, toMessage(stack)); + assertEq(/^async\*@.+ErrorStack.js:82/m.test(stack), true, toMessage(stack)); + + await Promise.resolve(1000); + + stack = new Error().stack; // line 51 + assertEq(/thrower@.+ErrorStack.js/m.test(stack), false, toMessage(stack)); + assertEq(/inner@.+ErrorStack.js/m.test(stack), false, toMessage(stack)); + assertEq(/^middle@.+ErrorStack.js:51/m.test(stack), true, toMessage(stack)); + assertEq(/^async\*outer@.+ErrorStack.js:78/m.test(stack), true, toMessage(stack)); + assertEq(/^async\*@.+ErrorStack.js:82/m.test(stack), true, toMessage(stack)); + + await inner(); // line 58 +} + +async function outer() { + let stack = new Error().stack; // line 62 + assertEq(/thrower@.+ErrorStack.js/m.test(stack), false, toMessage(stack)); + assertEq(/inner@.+ErrorStack.js/m.test(stack), false, toMessage(stack)); + assertEq(/middle@.+ErrorStack.js/m.test(stack), false, toMessage(stack)); + assertEq(/^outer@.+ErrorStack.js:62/m.test(stack), true, toMessage(stack)); + assertEq(/^async\*@.+ErrorStack.js:82/m.test(stack), true, toMessage(stack)); + + await Promise.resolve(10000); + + stack = new Error().stack; // line 71 + assertEq(/thrower@.+ErrorStack.js/m.test(stack), false, toMessage(stack)); + assertEq(/inner@.+ErrorStack.js/m.test(stack), false, toMessage(stack)); + assertEq(/middle@.+ErrorStack.js/m.test(stack), false, toMessage(stack)); + assertEq(/^outer@.+ErrorStack.js:71/m.test(stack), true, toMessage(stack)); + assertEq(/^async\*@.+ErrorStack.js:82/m.test(stack), true, toMessage(stack)); + + await middle(); // line 78 +} + +try { + getPromiseResult(outer()); // line 82 + assertEq(true, false); +} catch (e) { + // Re-throw the exception to log the assertion failure properly. + if (!e.message.includes(COOKIE)) + throw e; + + let stack = e.stack; + assertEq(/^thrower@.+ErrorStack.js:18/m.test(stack), true, toMessage(stack)); + assertEq(/^async\*inner@.+ErrorStack.js:38/m.test(stack), true, toMessage(stack)); + assertEq(/^async\*middle@.+ErrorStack.js:58/m.test(stack), true, toMessage(stack)); + assertEq(/^async\*outer@.+ErrorStack.js:78/m.test(stack), true, toMessage(stack)); + assertEq(/^async\*@.+ErrorStack.js:82/m.test(stack), true, toMessage(stack)); +} + +function toMessage(stack) { + // Provide the stack string in the error message for debugging. + return `[stack: ${stack.replace(/\n/g, "\\n")}]`; +} + +if (typeof reportCompare === "function") + reportCompare(true, true); diff --git a/js/src/tests/shell.js b/js/src/tests/shell.js index 35183cf6a8c8..cdbc4d36f537 100644 --- a/js/src/tests/shell.js +++ b/js/src/tests/shell.js @@ -737,6 +737,7 @@ throw error; return result; } + global.getPromiseResult = getPromiseResult; function assertEventuallyEq(promise, expected) { assertEq(getPromiseResult(promise), expected); diff --git a/js/src/vm/AsyncFunction.cpp b/js/src/vm/AsyncFunction.cpp index 7132ed984d30..43223e0eda2b 100644 --- a/js/src/vm/AsyncFunction.cpp +++ b/js/src/vm/AsyncFunction.cpp @@ -6,6 +6,8 @@ #include "vm/AsyncFunction.h" +#include "mozilla/Maybe.h" + #include "jscompartment.h" #include "builtin/Promise.h" @@ -17,6 +19,8 @@ using namespace js; using namespace js::gc; +using mozilla::Maybe; + /* static */ bool GlobalObject::initAsyncFunction(JSContext* cx, Handle global) { @@ -168,6 +172,13 @@ static bool AsyncFunctionResume(JSContext* cx, Handle resultPromise, HandleValue generatorVal, ResumeKind kind, HandleValue valueOrReason) { + RootedObject stack(cx, resultPromise->allocationSite()); + Maybe asyncStack; + if (stack) { + asyncStack.emplace(cx, stack, "async", + JS::AutoSetAsyncStackForNewCalls::AsyncCallKind::EXPLICIT); + } + // Execution context switching is handled in generator. HandlePropertyName funName = kind == ResumeKind::Normal ? cx->names().StarGeneratorNext