Skip to content

Commit d066acf

Browse files
legendecasaduh95
authored andcommitted
util: expose CallSite.scriptId
The `scriptId` is essential to construct chrome devtools protocol structs like `Network.Initiator`, allowing inspectors to associate a `CallSite` with a unique script. PR-URL: #56551 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent c9a211a commit d066acf

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

doc/api/util.md

+5
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,9 @@ util.formatWithOptions({ colors: true }, 'See object %O', { foo: 42 });
371371
<!-- YAML
372372
added: v22.9.0
373373
changes:
374+
- version: REPLACEME
375+
pr-url: https://github.com/nodejs/node/pull/56551
376+
description: Property `CallSite.scriptId` is exposed.
374377
- version: v23.3.0
375378
pr-url: https://github.com/nodejs/node/pull/55626
376379
description: The API is renamed from `util.getCallSite` to `util.getCallSites()`.
@@ -385,6 +388,7 @@ changes:
385388
* `functionName` {string} Returns the name of the function associated with this call site.
386389
* `scriptName` {string} Returns the name of the resource that contains the script for the
387390
function for this call site.
391+
* `scriptId` {string} Returns the unique id of the script, as in Chrome DevTools protocol [`Runtime.ScriptId`][].
388392
* `lineNumber` {number} Returns the number, 1-based, of the line for the associate function call.
389393
* `column` {number} Returns the 1-based column offset on the line for the associated function call.
390394

@@ -3184,6 +3188,7 @@ util.isArray({});
31843188
[`Object.freeze()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze
31853189
[`Promise`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
31863190
[`Proxy`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
3191+
[`Runtime.ScriptId`]: https://chromedevtools.github.io/devtools-protocol/1-3/Runtime/#type-ScriptId
31873192
[`Set`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
31883193
[`SharedArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer
31893194
[`TypedArray`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray

src/env_properties.h

+1
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@
320320
V(salt_length_string, "saltLength") \
321321
V(scheme_string, "scheme") \
322322
V(scopeid_string, "scopeid") \
323+
V(script_id_string, "scriptId") \
323324
V(script_name_string, "scriptName") \
324325
V(serial_number_string, "serialNumber") \
325326
V(serial_string, "serial") \

src/node_util.cc

+4
Original file line numberDiff line numberDiff line change
@@ -275,14 +275,18 @@ static void GetCallSites(const FunctionCallbackInfo<Value>& args) {
275275
script_name = v8::String::Empty(isolate);
276276
}
277277

278+
std::string script_id = std::to_string(stack_frame->GetScriptId());
279+
278280
Local<Name> names[] = {
279281
env->function_name_string(),
282+
env->script_id_string(),
280283
env->script_name_string(),
281284
env->line_number_string(),
282285
env->column_string(),
283286
};
284287
Local<Value> values[] = {
285288
function_name,
289+
OneByteString(isolate, script_id),
286290
script_name,
287291
Integer::NewFromUnsigned(isolate, stack_frame->GetLineNumber()),
288292
Integer::NewFromUnsigned(isolate, stack_frame->GetColumn()),

test/parallel/test-util-getcallsites.js

+7
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ const assert = require('node:assert');
7979
);
8080
}
8181

82+
// ScriptId is a string.
83+
{
84+
const callSites = getCallSites(1);
85+
assert.strictEqual(callSites.length, 1);
86+
assert.strictEqual(typeof callSites[0].scriptId, 'string');
87+
}
88+
8289
// Guarantee [eval] will appear on stacktraces when using -e
8390
{
8491
const { status, stderr, stdout } = spawnSync(

0 commit comments

Comments
 (0)