Skip to content

Commit 940d22f

Browse files
legendecasruyadorno
authored andcommitted
util: fix util.getCallSites plurality
`util.getCallSite` returns an array of call site objects. Rename the function to reflect that it returns a given count of frames captured as an array of call site object. Renames the first parameter `frames` to be `frameCount` to indicate that it specifies the count of returned call sites. PR-URL: #55626 Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
1 parent b6d2a4e commit 940d22f

10 files changed

Lines changed: 89 additions & 61 deletions

File tree

‎benchmark/util/get-callsite.js‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
'use strict';
22

33
constcommon=require('../common');
4-
const{getCallSite}=require('node:util');
4+
const{getCallSites}=require('node:util');
55
constassert=require('node:assert');
66

77
constbench=common.createBenchmark(main,{
88
n: [1e6],
9-
method: ['ErrorCallSite','ErrorCallSiteSerialized','CPP'],
9+
method: ['ErrorCallSites','ErrorCallSitesSerialized','CPP'],
1010
});
1111

12-
functionErrorGetCallSite(){
12+
functionErrorGetCallSites(){
1313
constoriginalStackFormatter=Error.prepareStackTrace;
1414
Error.prepareStackTrace=(_err,stack)=>{
1515
if(stack&&stack.length>1){
@@ -25,15 +25,15 @@ function ErrorGetCallSite() {
2525
returnerr.stack;
2626
}
2727

28-
functionErrorCallSiteSerialized(){
29-
constcallsite=ErrorGetCallSite();
28+
functionErrorCallSitesSerialized(){
29+
constcallSites=ErrorGetCallSites();
3030
constserialized=[];
31-
for(leti=0;i<callsite.length;++i){
31+
for(leti=0;i<callSites.length;++i){
3232
serialized.push({
33-
functionName: callsite[i].getFunctionName(),
34-
scriptName: callsite[i].getFileName(),
35-
lineNumber: callsite[i].getLineNumber(),
36-
column: callsite[i].getColumnNumber(),
33+
functionName: callSites[i].getFunctionName(),
34+
scriptName: callSites[i].getFileName(),
35+
lineNumber: callSites[i].getLineNumber(),
36+
column: callSites[i].getColumnNumber(),
3737
});
3838
}
3939
returnserialized;
@@ -42,14 +42,14 @@ function ErrorCallSiteSerialized() {
4242
functionmain({ n, method }){
4343
letfn;
4444
switch(method){
45-
case'ErrorCallSite':
46-
fn=ErrorGetCallSite;
45+
case'ErrorCallSites':
46+
fn=ErrorGetCallSites;
4747
break;
48-
case'ErrorCallSiteSerialized':
49-
fn=ErrorCallSiteSerialized;
48+
case'ErrorCallSitesSerialized':
49+
fn=ErrorCallSitesSerialized;
5050
break;
5151
case'CPP':
52-
fn=getCallSite;
52+
fn=getCallSites;
5353
break;
5454
}
5555
letlastStack={};

‎doc/api/deprecations.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3685,6 +3685,19 @@ Instantiating classes without the `new` qualifier exported by the `node:repl` mo
36853685
It is recommended to use the `new` qualifier instead. This applies to all REPL classes, including
36863686
`REPLServer` and `Recoverable`.
36873687

3688+
### DEP0186: `util.getCallSite`
3689+
3690+
<!-- YAML
3691+
changes:
3692+
- version: REPLACEME
3693+
pr-url: https://github.com/nodejs/node/pull/55626
3694+
description: Runtime deprecation.
3695+
-->
3696+
3697+
Type: Runtime
3698+
3699+
The `util.getCallSite` API has been removed. Please use [`util.getCallSites()`][] instead.
3700+
36883701
[NIST SP 800-38D]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
36893702
[RFC 6066]: https://tools.ietf.org/html/rfc6066#section-3
36903703
[RFC 8247 Section 2.4]: https://www.rfc-editor.org/rfc/rfc8247#section-2.4
@@ -3811,6 +3824,7 @@ It is recommended to use the `new` qualifier instead. This applies to all REPL c
38113824
[`url.parse()`]: url.md#urlparseurlstring-parsequerystring-slashesdenotehost
38123825
[`url.resolve()`]: url.md#urlresolvefrom-to
38133826
[`util._extend()`]: util.md#util_extendtarget-source
3827+
[`util.getCallSites()`]: util.md#utilgetcallsitesframecount
38143828
[`util.getSystemErrorName()`]: util.md#utilgetsystemerrornameerr
38153829
[`util.inspect()`]: util.md#utilinspectobject-options
38163830
[`util.inspect.custom`]: util.md#utilinspectcustom

‎doc/api/util.md‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -364,31 +364,31 @@ util.formatWithOptions({ colors: true }, 'See object %O', { foo: 42 });
364364
// when printed to a terminal.
365365
```
366366

367-
## `util.getCallSite(frames)`
367+
## `util.getCallSites(frameCount)`
368368

369369
> Stability: 1.1 - Active development
370370
371371
<!-- YAML
372372
added: v22.9.0
373373
-->
374374

375-
*`frames` {number} Number of frames returned in the stacktrace.
375+
*`frameCount` {number} Number of frames to capture as call site objects.
376376
**Default:**`10`. Allowable range is between 1 and 200.
377-
* Returns: {Object\[]} An array of stacktrace objects
378-
*`functionName` {string} Returns the name of the function associated with this stack frame.
377+
* Returns: {Object\[]} An array of call site objects
378+
*`functionName` {string} Returns the name of the function associated with this call site.
379379
*`scriptName` {string} Returns the name of the resource that contains the script for the
380-
function for this StackFrame.
380+
function for this call site.
381381
*`lineNumber` {number} Returns the number, 1-based, of the line for the associate function call.
382382
*`column` {number} Returns the 1-based column offset on the line for the associated function call.
383383

384-
Returns an array of stacktrace objects containing the stack of
384+
Returns an array of call site objects containing the stack of
385385
the caller function.
386386

387387
```js
388388
constutil=require('node:util');
389389

390390
functionexampleFunction() {
391-
constcallSites=util.getCallSite();
391+
constcallSites=util.getCallSites();
392392

393393
console.log('Call Sites:');
394394
callSites.forEach((callSite, index) => {

‎lib/util.js‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -476,13 +476,13 @@ function parseEnv(content) {
476476

477477
/**
478478
* Returns the callSite
479-
* @param {number} frames
479+
* @param {number} frameCount
480480
* @returns {object}
481481
*/
482-
functiongetCallSite(frames=10){
482+
functiongetCallSites(frameCount=10){
483483
// Using kDefaultMaxCallStackSizeToCapture as reference
484-
validateNumber(frames,'frames',1,200);
485-
returnbinding.getCallSite(frames);
484+
validateNumber(frameCount,'frameCount',1,200);
485+
returnbinding.getCallSites(frameCount);
486486
};
487487

488488
// Keep the `exports =` so that various functions can still be monkeypatched
@@ -499,7 +499,12 @@ module.exports = {
499499
format,
500500
styleText,
501501
formatWithOptions,
502-
getCallSite,
502+
// Deprecated getCallSite.
503+
// This API can be removed in next semver-minor release.
504+
getCallSite: deprecate(getCallSites,
505+
'The `util.getCallSite` API is deprecated. Please use `util.getCallSites()` instead.',
506+
'DEP0186'),
507+
getCallSites,
503508
getSystemErrorMap,
504509
getSystemErrorName,
505510
getSystemErrorMessage,

‎src/node_util.cc‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ static void ParseEnv(const FunctionCallbackInfo<Value>& args) {
247247
args.GetReturnValue().Set(dotenv.ToObject(env));
248248
}
249249

250-
staticvoidGetCallSite(const FunctionCallbackInfo<Value>& args) {
250+
staticvoidGetCallSites(const FunctionCallbackInfo<Value>& args) {
251251
Environment* env = Environment::GetCurrent(args);
252252
Isolate* isolate = env->isolate();
253253

@@ -345,7 +345,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
345345
registry->Register(GetProxyDetails);
346346
registry->Register(GetCallerLocation);
347347
registry->Register(PreviewEntries);
348-
registry->Register(GetCallSite);
348+
registry->Register(GetCallSites);
349349
registry->Register(GetOwnNonIndexProperties);
350350
registry->Register(GetConstructorName);
351351
registry->Register(GetExternalValue);
@@ -451,7 +451,7 @@ void Initialize(Local<Object> target,
451451
SetMethodNoSideEffect(
452452
context, target, "getConstructorName", GetConstructorName);
453453
SetMethodNoSideEffect(context, target, "getExternalValue", GetExternalValue);
454-
SetMethodNoSideEffect(context, target, "getCallSite", GetCallSite);
454+
SetMethodNoSideEffect(context, target, "getCallSites", GetCallSites);
455455
SetMethod(context, target, "sleep", Sleep);
456456
SetMethod(context, target, "parseEnv", ParseEnv);
457457

‎test/common/index.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const net = require('net');
3030
// Do not require 'os' until needed so that test-os-checked-function can
3131
// monkey patch it. If 'os' is required here, that test will fail.
3232
constpath=require('path');
33-
const{ inspect,getCallSite}=require('util');
33+
const{ inspect,getCallSites}=require('util');
3434
const{ isMainThread }=require('worker_threads');
3535
const{ isModuleNamespaceObject }=require('util/types');
3636

@@ -550,7 +550,7 @@ function canCreateSymLink() {
550550
}
551551

552552
functionmustNotCall(msg){
553-
constcallSite=getCallSite()[1];
553+
constcallSite=getCallSites()[1];
554554
returnfunctionmustNotCall(...args){
555555
constargsInfo=args.length>0 ?
556556
`\ncalled with arguments: ${args.map((arg)=>inspect(arg)).join(', ')}` : '';

‎test/fixtures/get-call-site.js‎

Lines changed: 0 additions & 4 deletions
This file was deleted.

‎test/fixtures/get-call-sites.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
constutil=require('node:util');
2+
constassert=require('node:assert');
3+
assert.ok(util.getCallSites().length>1);
4+
process.stdout.write(util.getCallSites()[0].scriptName);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
require('../common');
4+
const{ getCallSite }=require('node:util');
5+
const{ expectWarning }=require('../common');
6+
7+
constwarning='The `util.getCallSite` API is deprecated. Please use `util.getCallSites()` instead.';
8+
expectWarning('DeprecationWarning',warning,'DEP0186');
9+
getCallSite();
Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,68 +3,68 @@
33
constcommon=require('../common');
44

55
constfixtures=require('../common/fixtures');
6-
constfile=fixtures.path('get-call-site.js');
6+
constfile=fixtures.path('get-call-sites.js');
77

8-
const{getCallSite}=require('node:util');
8+
const{getCallSites}=require('node:util');
99
const{ spawnSync }=require('node:child_process');
1010
constassert=require('node:assert');
1111

1212
{
13-
constcallsite=getCallSite();
14-
assert.ok(callsite.length>1);
13+
constcallSites=getCallSites();
14+
assert.ok(callSites.length>1);
1515
assert.match(
16-
callsite[0].scriptName,
17-
/test-util-getCallSite/,
16+
callSites[0].scriptName,
17+
/test-util-getcallsites/,
1818
'node:util should be ignored',
1919
);
2020
}
2121

2222
{
23-
constcallsite=getCallSite(3);
24-
assert.strictEqual(callsite.length,3);
23+
constcallSites=getCallSites(3);
24+
assert.strictEqual(callSites.length,3);
2525
assert.match(
26-
callsite[0].scriptName,
27-
/test-util-getCallSite/,
26+
callSites[0].scriptName,
27+
/test-util-getcallsites/,
2828
'node:util should be ignored',
2929
);
3030
}
3131

3232
// Guarantee dot-left numbers are ignored
3333
{
34-
constcallsite=getCallSite(3.6);
35-
assert.strictEqual(callsite.length,3);
34+
constcallSites=getCallSites(3.6);
35+
assert.strictEqual(callSites.length,3);
3636
}
3737

3838
{
39-
constcallsite=getCallSite(3.4);
40-
assert.strictEqual(callsite.length,3);
39+
constcallSites=getCallSites(3.4);
40+
assert.strictEqual(callSites.length,3);
4141
}
4242

4343
{
4444
assert.throws(()=>{
4545
// Max than kDefaultMaxCallStackSizeToCapture
46-
getCallSite(201);
46+
getCallSites(201);
4747
},common.expectsError({
4848
code: 'ERR_OUT_OF_RANGE'
4949
}));
5050
assert.throws(()=>{
51-
getCallSite(-1);
51+
getCallSites(-1);
5252
},common.expectsError({
5353
code: 'ERR_OUT_OF_RANGE'
5454
}));
5555
assert.throws(()=>{
56-
getCallSite({});
56+
getCallSites({});
5757
},common.expectsError({
5858
code: 'ERR_INVALID_ARG_TYPE'
5959
}));
6060
}
6161

6262
{
63-
constcallsite=getCallSite(1);
64-
assert.strictEqual(callsite.length,1);
63+
constcallSites=getCallSites(1);
64+
assert.strictEqual(callSites.length,1);
6565
assert.match(
66-
callsite[0].scriptName,
67-
/test-util-getCallSite/,
66+
callSites[0].scriptName,
67+
/test-util-getcallsites/,
6868
'node:util should be ignored',
6969
);
7070
}
@@ -77,8 +77,8 @@ const assert = require('node:assert');
7777
'-e',
7878
`const util = require('util');
7979
const assert = require('assert');
80-
assert.ok(util.getCallSite().length > 1);
81-
process.stdout.write(util.getCallSite()[0].scriptName);
80+
assert.ok(util.getCallSites().length > 1);
81+
process.stdout.write(util.getCallSites()[0].scriptName);
8282
`,
8383
],
8484
);
@@ -100,7 +100,7 @@ const assert = require('node:assert');
100100
{
101101
constoriginalStackTraceLimit=Error.stackTraceLimit;
102102
Error.stackTraceLimit=0;
103-
constcallsite=getCallSite();
104-
assert.notStrictEqual(callsite.length,0);
103+
constcallSites=getCallSites();
104+
assert.notStrictEqual(callSites.length,0);
105105
Error.stackTraceLimit=originalStackTraceLimit;
106106
}

0 commit comments

Comments
 (0)