-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
lib: add util.getCallSite() API #54380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
da4c560
lib: add util.getCallSite() API
RafaelGSS ae25f5c
benchmark: add util.getCallSite bench
RafaelGSS 7abc419
fixup! lib: add util.getCallSite() API
RafaelGSS 0bd7e23
fixup! fixup! lib: add util.getCallSite() API
RafaelGSS d3cd884
fixup! fixup! fixup! lib: add util.getCallSite() API
RafaelGSS c73110b
fixup! fixup! fixup! fixup! lib: add util.getCallSite() API
RafaelGSS 7d49b66
fixup! lib: add util.getCallSite() API
RafaelGSS 36c2839
fixup! fixup! lib: add util.getCallSite() API
RafaelGSS aa11f56
fixup! apply cr suggestions
RafaelGSS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const { getCallSite } = require('node:util'); | ||
const assert = require('node:assert'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [1e6], | ||
method: ['ErrorCallSite', 'ErrorCallSiteSerialized', 'CPP'], | ||
}); | ||
|
||
function ErrorGetCallSite() { | ||
const originalStackFormatter = Error.prepareStackTrace; | ||
Error.prepareStackTrace = (_err, stack) => { | ||
if (stack && stack.length > 1) { | ||
// Remove node:util | ||
return stack.slice(1); | ||
} | ||
return stack; | ||
}; | ||
const err = new Error(); | ||
// With the V8 Error API, the stack is not formatted until it is accessed | ||
err.stack; // eslint-disable-line no-unused-expressions | ||
Error.prepareStackTrace = originalStackFormatter; | ||
return err.stack; | ||
} | ||
|
||
function ErrorCallSiteSerialized() { | ||
const callsite = ErrorGetCallSite(); | ||
const serialized = []; | ||
for (let i = 0; i < callsite.length; ++i) { | ||
serialized.push({ | ||
functionName: callsite[i].getFunctionName(), | ||
scriptName: callsite[i].getFileName(), | ||
lineNumber: callsite[i].getLineNumber(), | ||
column: callsite[i].getColumnNumber(), | ||
}); | ||
} | ||
return serialized; | ||
} | ||
|
||
function main({ n, method }) { | ||
let fn; | ||
switch (method) { | ||
case 'ErrorCallSite': | ||
fn = ErrorGetCallSite; | ||
break; | ||
case 'ErrorCallSiteSerialized': | ||
fn = ErrorCallSiteSerialized; | ||
break; | ||
case 'CPP': | ||
fn = getCallSite; | ||
break; | ||
} | ||
let lastStack = {}; | ||
|
||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
const stack = fn(); | ||
lastStack = stack; | ||
} | ||
bench.end(n); | ||
// Attempt to avoid dead-code elimination | ||
assert.ok(lastStack); | ||
} | ||
RafaelGSS marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
const util = require('node:util'); | ||
const assert = require('node:assert'); | ||
assert.ok(util.getCallSite().length > 1); | ||
process.stdout.write(util.getCallSite()[0].scriptName); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
const fixtures = require('../common/fixtures'); | ||
const file = fixtures.path('get-call-site.js'); | ||
|
||
const { getCallSite } = require('node:util'); | ||
const { spawnSync } = require('node:child_process'); | ||
const assert = require('node:assert'); | ||
|
||
{ | ||
RafaelGSS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const callsite = getCallSite(); | ||
assert.ok(callsite.length > 1); | ||
assert.match( | ||
callsite[0].scriptName, | ||
/test-util-getCallSite/, | ||
'node:util should be ignored', | ||
); | ||
} | ||
|
||
{ | ||
const callsite = getCallSite(3); | ||
assert.strictEqual(callsite.length, 3); | ||
assert.match( | ||
callsite[0].scriptName, | ||
/test-util-getCallSite/, | ||
'node:util should be ignored', | ||
); | ||
} | ||
|
||
// Guarantee dot-left numbers are ignored | ||
{ | ||
const callsite = getCallSite(3.6); | ||
assert.strictEqual(callsite.length, 3); | ||
} | ||
|
||
{ | ||
const callsite = getCallSite(3.4); | ||
assert.strictEqual(callsite.length, 3); | ||
} | ||
|
||
{ | ||
assert.throws(() => { | ||
// Max than kDefaultMaxCallStackSizeToCapture | ||
getCallSite(201); | ||
}, common.expectsError({ | ||
code: 'ERR_OUT_OF_RANGE' | ||
})); | ||
assert.throws(() => { | ||
getCallSite(-1); | ||
}, common.expectsError({ | ||
code: 'ERR_OUT_OF_RANGE' | ||
})); | ||
assert.throws(() => { | ||
getCallSite({}); | ||
}, common.expectsError({ | ||
code: 'ERR_INVALID_ARG_TYPE' | ||
})); | ||
} | ||
|
||
{ | ||
const callsite = getCallSite(1); | ||
assert.strictEqual(callsite.length, 1); | ||
assert.match( | ||
callsite[0].scriptName, | ||
/test-util-getCallSite/, | ||
'node:util should be ignored', | ||
); | ||
} | ||
|
||
// Guarantee [eval] will appear on stacktraces when using -e | ||
{ | ||
const { status, stderr, stdout } = spawnSync( | ||
process.execPath, | ||
[ | ||
'-e', | ||
`const util = require('util'); | ||
const assert = require('assert'); | ||
assert.ok(util.getCallSite().length > 1); | ||
process.stdout.write(util.getCallSite()[0].scriptName); | ||
`, | ||
], | ||
); | ||
assert.strictEqual(status, 0, stderr.toString()); | ||
assert.strictEqual(stdout.toString(), '[eval]'); | ||
} | ||
|
||
// Guarantee the stacktrace[0] is the filename | ||
{ | ||
const { status, stderr, stdout } = spawnSync( | ||
process.execPath, | ||
[file], | ||
); | ||
assert.strictEqual(status, 0, stderr.toString()); | ||
assert.strictEqual(stdout.toString(), file); | ||
} | ||
|
||
// Error.stackTraceLimit should not influence callsite size | ||
{ | ||
const originalStackTraceLimit = Error.stackTraceLimit; | ||
Error.stackTraceLimit = 0; | ||
const callsite = getCallSite(); | ||
assert.notStrictEqual(callsite.length, 0); | ||
Error.stackTraceLimit = originalStackTraceLimit; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.