v8/test/mjsunit/async-stack-traces.js

320 lines
7.0 KiB
JavaScript
Raw Normal View History

[async] First prototype of zero-cost async stack traces. This introduces a new flag --async-stack-traces, which enables zero-cost async stack traces. This enriches the non-standard Error.stack property with async stack frames computed from walking up the promise chains and collecting all the await suspension points along the way. In Error.stack these async frames are marked with "async" to make it possible to distinguish them from regular frames, for example: ``` Error: Some error message at bar (<anonymous>) at async foo (<anonymous>) ``` It's zero-cost because no additional information is collected during the execution of the program, but only the information already present in the promise chains is used to reconstruct an approximation of the async stack in case of an exception. But this approximation is limited to suspension points at await's in async functions. This depends on a recent ECMAScript specification change, flagged behind --harmony-await-optimization and implied the --async-stack-traces flag. Without this change there's no way to get from the outer promise of an async function to the rest of the promise chain, since the link is broken by the indirection introduced by await. For async functions the special outer promise, named .promise in the Parser desugaring, is now forcible allocated to stack slot 0 during scope resolution, to make it accessible to the stack frame construction logic. Note that this first prototype doesn't yet work fully support async generators and might have other limitations. Bug: v8:7522 Ref: nodejs/node#11865 Change-Id: I0cc8e3cdfe45dab56d3d506be2d25907409b01a9 Design-Document: http://bit.ly/v8-zero-cost-async-stack-traces Reviewed-on: https://chromium-review.googlesource.com/c/1256762 Commit-Queue: Benedikt Meurer <bmeurer@chromium.org> Reviewed-by: Adam Klein <adamk@chromium.org> Reviewed-by: Yang Guo <yangguo@chromium.org> Cr-Commit-Position: refs/heads/master@{#56363}
2018-10-04 07:19:45 +00:00
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --allow-natives-syntax --async-stack-traces
// Basic test with an explicit throw.
(function() {
async function one(x) {
await two(x);
}
async function two(x) {
await x;
throw new Error();
}
async function test(f) {
try {
await f(1);
assertUnreachable();
} catch (e) {
assertInstanceof(e, Error);
assertMatches(/Error.+at two.+at async one.+at async test/ms, e.stack);
}
}
assertPromiseResult((async () => {
%PrepareFunctionForOptimization(one);
%PrepareFunctionForOptimization(two);
[async] First prototype of zero-cost async stack traces. This introduces a new flag --async-stack-traces, which enables zero-cost async stack traces. This enriches the non-standard Error.stack property with async stack frames computed from walking up the promise chains and collecting all the await suspension points along the way. In Error.stack these async frames are marked with "async" to make it possible to distinguish them from regular frames, for example: ``` Error: Some error message at bar (<anonymous>) at async foo (<anonymous>) ``` It's zero-cost because no additional information is collected during the execution of the program, but only the information already present in the promise chains is used to reconstruct an approximation of the async stack in case of an exception. But this approximation is limited to suspension points at await's in async functions. This depends on a recent ECMAScript specification change, flagged behind --harmony-await-optimization and implied the --async-stack-traces flag. Without this change there's no way to get from the outer promise of an async function to the rest of the promise chain, since the link is broken by the indirection introduced by await. For async functions the special outer promise, named .promise in the Parser desugaring, is now forcible allocated to stack slot 0 during scope resolution, to make it accessible to the stack frame construction logic. Note that this first prototype doesn't yet work fully support async generators and might have other limitations. Bug: v8:7522 Ref: nodejs/node#11865 Change-Id: I0cc8e3cdfe45dab56d3d506be2d25907409b01a9 Design-Document: http://bit.ly/v8-zero-cost-async-stack-traces Reviewed-on: https://chromium-review.googlesource.com/c/1256762 Commit-Queue: Benedikt Meurer <bmeurer@chromium.org> Reviewed-by: Adam Klein <adamk@chromium.org> Reviewed-by: Yang Guo <yangguo@chromium.org> Cr-Commit-Position: refs/heads/master@{#56363}
2018-10-04 07:19:45 +00:00
await test(one);
await test(one);
%OptimizeFunctionOnNextCall(two);
await test(one);
%OptimizeFunctionOnNextCall(one);
await test(one);
})());
})();
// Basic test with an implicit throw (via ToNumber on Symbol).
(function() {
async function one(x) {
return await two(x);
}
async function two(x) {
await x;
return +x; // This will raise a TypeError.
}
async function test(f) {
try {
await f(Symbol());
assertUnreachable();
} catch (e) {
assertInstanceof(e, TypeError);
assertMatches(/TypeError.+at two.+at async one.+at async test/ms, e.stack);
}
}
assertPromiseResult((async() => {
%PrepareFunctionForOptimization(one);
%PrepareFunctionForOptimization(two);
[async] First prototype of zero-cost async stack traces. This introduces a new flag --async-stack-traces, which enables zero-cost async stack traces. This enriches the non-standard Error.stack property with async stack frames computed from walking up the promise chains and collecting all the await suspension points along the way. In Error.stack these async frames are marked with "async" to make it possible to distinguish them from regular frames, for example: ``` Error: Some error message at bar (<anonymous>) at async foo (<anonymous>) ``` It's zero-cost because no additional information is collected during the execution of the program, but only the information already present in the promise chains is used to reconstruct an approximation of the async stack in case of an exception. But this approximation is limited to suspension points at await's in async functions. This depends on a recent ECMAScript specification change, flagged behind --harmony-await-optimization and implied the --async-stack-traces flag. Without this change there's no way to get from the outer promise of an async function to the rest of the promise chain, since the link is broken by the indirection introduced by await. For async functions the special outer promise, named .promise in the Parser desugaring, is now forcible allocated to stack slot 0 during scope resolution, to make it accessible to the stack frame construction logic. Note that this first prototype doesn't yet work fully support async generators and might have other limitations. Bug: v8:7522 Ref: nodejs/node#11865 Change-Id: I0cc8e3cdfe45dab56d3d506be2d25907409b01a9 Design-Document: http://bit.ly/v8-zero-cost-async-stack-traces Reviewed-on: https://chromium-review.googlesource.com/c/1256762 Commit-Queue: Benedikt Meurer <bmeurer@chromium.org> Reviewed-by: Adam Klein <adamk@chromium.org> Reviewed-by: Yang Guo <yangguo@chromium.org> Cr-Commit-Position: refs/heads/master@{#56363}
2018-10-04 07:19:45 +00:00
await test(one);
await test(one);
%OptimizeFunctionOnNextCall(two);
await test(one);
%OptimizeFunctionOnNextCall(one);
await test(one);
})());
})();
// Basic test with throw in inlined function.
(function() {
function throwError() {
throw new Error();
}
async function one(x) {
return await two(x);
}
async function two(x) {
await x;
return throwError();
}
async function test(f) {
try {
await f(1);
assertUnreachable();
} catch (e) {
assertInstanceof(e, Error);
assertMatches(/Error.+at two.+at async one.+at async test/ms, e.stack);
}
}
assertPromiseResult((async() => {
%PrepareFunctionForOptimization(one);
%PrepareFunctionForOptimization(two);
[async] First prototype of zero-cost async stack traces. This introduces a new flag --async-stack-traces, which enables zero-cost async stack traces. This enriches the non-standard Error.stack property with async stack frames computed from walking up the promise chains and collecting all the await suspension points along the way. In Error.stack these async frames are marked with "async" to make it possible to distinguish them from regular frames, for example: ``` Error: Some error message at bar (<anonymous>) at async foo (<anonymous>) ``` It's zero-cost because no additional information is collected during the execution of the program, but only the information already present in the promise chains is used to reconstruct an approximation of the async stack in case of an exception. But this approximation is limited to suspension points at await's in async functions. This depends on a recent ECMAScript specification change, flagged behind --harmony-await-optimization and implied the --async-stack-traces flag. Without this change there's no way to get from the outer promise of an async function to the rest of the promise chain, since the link is broken by the indirection introduced by await. For async functions the special outer promise, named .promise in the Parser desugaring, is now forcible allocated to stack slot 0 during scope resolution, to make it accessible to the stack frame construction logic. Note that this first prototype doesn't yet work fully support async generators and might have other limitations. Bug: v8:7522 Ref: nodejs/node#11865 Change-Id: I0cc8e3cdfe45dab56d3d506be2d25907409b01a9 Design-Document: http://bit.ly/v8-zero-cost-async-stack-traces Reviewed-on: https://chromium-review.googlesource.com/c/1256762 Commit-Queue: Benedikt Meurer <bmeurer@chromium.org> Reviewed-by: Adam Klein <adamk@chromium.org> Reviewed-by: Yang Guo <yangguo@chromium.org> Cr-Commit-Position: refs/heads/master@{#56363}
2018-10-04 07:19:45 +00:00
await test(one);
await test(one);
%OptimizeFunctionOnNextCall(two);
await test(one);
%OptimizeFunctionOnNextCall(one);
await test(one);
})());
})();
// Basic test with async function inlined into sync function.
(function() {
function callOne(x) {
return one(x);
}
function callTwo(x) {
return two(x);
}
async function one(x) {
return await callTwo(x);
}
async function two(x) {
await x;
throw new Error();
}
async function test(f) {
try {
await f(1);
assertUnreachable();
} catch (e) {
assertInstanceof(e, Error);
assertMatches(/Error.+at two.+at async one.+at async test/ms, e.stack);
}
}
assertPromiseResult((async() => {
%PrepareFunctionForOptimization(callOne);
%PrepareFunctionForOptimization(callTwo);
[async] First prototype of zero-cost async stack traces. This introduces a new flag --async-stack-traces, which enables zero-cost async stack traces. This enriches the non-standard Error.stack property with async stack frames computed from walking up the promise chains and collecting all the await suspension points along the way. In Error.stack these async frames are marked with "async" to make it possible to distinguish them from regular frames, for example: ``` Error: Some error message at bar (<anonymous>) at async foo (<anonymous>) ``` It's zero-cost because no additional information is collected during the execution of the program, but only the information already present in the promise chains is used to reconstruct an approximation of the async stack in case of an exception. But this approximation is limited to suspension points at await's in async functions. This depends on a recent ECMAScript specification change, flagged behind --harmony-await-optimization and implied the --async-stack-traces flag. Without this change there's no way to get from the outer promise of an async function to the rest of the promise chain, since the link is broken by the indirection introduced by await. For async functions the special outer promise, named .promise in the Parser desugaring, is now forcible allocated to stack slot 0 during scope resolution, to make it accessible to the stack frame construction logic. Note that this first prototype doesn't yet work fully support async generators and might have other limitations. Bug: v8:7522 Ref: nodejs/node#11865 Change-Id: I0cc8e3cdfe45dab56d3d506be2d25907409b01a9 Design-Document: http://bit.ly/v8-zero-cost-async-stack-traces Reviewed-on: https://chromium-review.googlesource.com/c/1256762 Commit-Queue: Benedikt Meurer <bmeurer@chromium.org> Reviewed-by: Adam Klein <adamk@chromium.org> Reviewed-by: Yang Guo <yangguo@chromium.org> Cr-Commit-Position: refs/heads/master@{#56363}
2018-10-04 07:19:45 +00:00
await test(callOne);
await test(callOne);
%OptimizeFunctionOnNextCall(callTwo);
await test(callOne);
%OptimizeFunctionOnNextCall(callOne);
await test(callOne);
})());
})();
// Basic test with async functions and promises chained via
// Promise.prototype.then(), which should still work following
// the generic chain upwards.
(function() {
async function one(x) {
return await two(x).then(x => x);
}
async function two(x) {
await x.then(x => x);
throw new Error();
}
async function test(f) {
try {
await f(Promise.resolve(1));
assertUnreachable();
} catch (e) {
assertInstanceof(e, Error);
assertMatches(/Error.+at two.+at async one.+at async test/ms, e.stack);
}
}
assertPromiseResult((async() => {
%PrepareFunctionForOptimization(one);
%PrepareFunctionForOptimization(two);
[async] First prototype of zero-cost async stack traces. This introduces a new flag --async-stack-traces, which enables zero-cost async stack traces. This enriches the non-standard Error.stack property with async stack frames computed from walking up the promise chains and collecting all the await suspension points along the way. In Error.stack these async frames are marked with "async" to make it possible to distinguish them from regular frames, for example: ``` Error: Some error message at bar (<anonymous>) at async foo (<anonymous>) ``` It's zero-cost because no additional information is collected during the execution of the program, but only the information already present in the promise chains is used to reconstruct an approximation of the async stack in case of an exception. But this approximation is limited to suspension points at await's in async functions. This depends on a recent ECMAScript specification change, flagged behind --harmony-await-optimization and implied the --async-stack-traces flag. Without this change there's no way to get from the outer promise of an async function to the rest of the promise chain, since the link is broken by the indirection introduced by await. For async functions the special outer promise, named .promise in the Parser desugaring, is now forcible allocated to stack slot 0 during scope resolution, to make it accessible to the stack frame construction logic. Note that this first prototype doesn't yet work fully support async generators and might have other limitations. Bug: v8:7522 Ref: nodejs/node#11865 Change-Id: I0cc8e3cdfe45dab56d3d506be2d25907409b01a9 Design-Document: http://bit.ly/v8-zero-cost-async-stack-traces Reviewed-on: https://chromium-review.googlesource.com/c/1256762 Commit-Queue: Benedikt Meurer <bmeurer@chromium.org> Reviewed-by: Adam Klein <adamk@chromium.org> Reviewed-by: Yang Guo <yangguo@chromium.org> Cr-Commit-Position: refs/heads/master@{#56363}
2018-10-04 07:19:45 +00:00
await test(one);
await test(one);
%OptimizeFunctionOnNextCall(two);
await test(one);
%OptimizeFunctionOnNextCall(one);
await test(one);
})());
})();
// Basic test for async generators called from async
// functions with an explicit throw.
(function() {
async function one(x) {
for await (const y of two(x)) {}
}
async function* two(x) {
await x;
throw new Error();
}
async function test(f) {
try {
await f(1);
assertUnreachable();
} catch (e) {
assertInstanceof(e, Error);
assertMatches(/Error.+at two.+at async one.+at async test/ms, e.stack);
}
}
assertPromiseResult((async () => {
%PrepareFunctionForOptimization(one);
%PrepareFunctionForOptimization(two);
await test(one);
await test(one);
%OptimizeFunctionOnNextCall(two);
await test(one);
%OptimizeFunctionOnNextCall(one);
await test(one);
})());
})();
// Basic test for async functions called from async
// generators with an explicit throw.
(function() {
async function* one(x) {
await two(x);
}
async function two(x) {
await x;
throw new Error();
}
async function test(f) {
try {
for await (const x of f(1)) {}
assertUnreachable();
} catch (e) {
assertInstanceof(e, Error);
assertMatches(/Error.+at two.+at async one.+at async test/ms, e.stack);
}
}
assertPromiseResult((async () => {
%PrepareFunctionForOptimization(one);
%PrepareFunctionForOptimization(two);
await test(one);
await test(one);
%OptimizeFunctionOnNextCall(two);
await test(one);
%OptimizeFunctionOnNextCall(one);
await test(one);
})());
})();
// Basic test for async functions called from async
// generators with an explicit throw (with yield).
(function() {
async function* one(x) {
yield two(x);
}
async function two(x) {
await x;
throw new Error();
}
async function test(f) {
try {
for await (const x of f(1)) {}
assertUnreachable();
} catch (e) {
assertInstanceof(e, Error);
assertMatches(/Error.+at two.+at async one.+at async test/ms, e.stack);
}
}
assertPromiseResult((async () => {
%PrepareFunctionForOptimization(one);
%PrepareFunctionForOptimization(two);
await test(one);
await test(one);
%OptimizeFunctionOnNextCall(two);
await test(one);
%OptimizeFunctionOnNextCall(one);
await test(one);
})());
})();
// Basic test to check that we also follow initial
// promise chains created via Promise#then().
(function() {
async function one(p) {
return await p.then(two);
}
function two() {
throw new Error();
}
async function test(f) {
try {
await f(Promise.resolve());
assertUnreachable();
} catch (e) {
assertInstanceof(e, Error);
assertMatches(/Error.+at two.+at async one.+at async test/ms, e.stack);
}
}
assertPromiseResult((async () => {
%PrepareFunctionForOptimization(one);
%PrepareFunctionForOptimization(two);
await test(one);
await test(one);
%OptimizeFunctionOnNextCall(two);
await test(one);
%OptimizeFunctionOnNextCall(one);
await test(one);
})());
})();