Skip to content
This repository was archived by the owner on Feb 1, 2022. It is now read-only.

Commit 86b5812

Browse files
author
Jan Krems
committed
feat: scripts & listScripts(true)
1 parent a463d15 commit 86b5812

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

lib/node-inspect.js

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const path = require('path');
3131
const util = require('util');
3232
const vm = require('vm');
3333

34+
const natives = process.binding('natives');
35+
3436
const debuglog = util.debuglog('inspect');
3537

3638
const kOpCodeContinuation = 0x0;
@@ -474,7 +476,7 @@ function createCommandContext(inspector) {
474476
let currentFrame = undefined;
475477
let currentBacktrace = undefined;
476478

477-
const scripts = {};
479+
const knownScripts = {};
478480
const watchedExpressions = [];
479481
const knownBreakpoints = [];
480482
const history = { debug: [], control: [] };
@@ -580,6 +582,21 @@ function createCommandContext(inspector) {
580582
print(helpMessage);
581583
},
582584

585+
listScripts(displayNatives = false) {
586+
for (const scriptId of Object.keys(knownScripts)) {
587+
const { isNative, url } = knownScripts[scriptId];
588+
const isCurrent = currentSourceLocation && currentSourceLocation.scriptId === scriptId;
589+
if (displayNatives || !isNative || isCurrent) {
590+
const name = getRelativePath(url);
591+
print(`${isCurrent ? '*' : ' '} ${scriptId}: ${name}${isNative ? ' <native>' : ''}`);
592+
}
593+
}
594+
},
595+
596+
get scripts() {
597+
return ctx.listScripts();
598+
},
599+
583600
debugEval(code) {
584601
// Repl asked for scope variables
585602
if (code === '.scope') {
@@ -692,11 +709,11 @@ function createCommandContext(inspector) {
692709
// setBreakpoint('scriptname')
693710
let scriptId = null;
694711
let ambiguous = false;
695-
if (scripts[script]) {
712+
if (knownScripts[script]) {
696713
scriptId = script;
697714
} else {
698-
for (const id of Object.keys(scripts)) {
699-
if (scripts[id] && scripts[id].url && scripts[id].url.indexOf(script) !== -1) {
715+
for (const id of Object.keys(knownScripts)) {
716+
if (knownScripts[id].url && knownScripts[id].url.indexOf(script) !== -1) {
700717
if (scriptId !== null) {
701718
ambiguous = true;
702719
}
@@ -741,7 +758,7 @@ function createCommandContext(inspector) {
741758
get breakpoints() {
742759
function formatLocation(location) {
743760
if (!location) return '<unknown location>';
744-
const script = scripts[location.scriptId];
761+
const script = knownScripts[location.scriptId];
745762
const scriptUrl = script ? script.url : location.scriptUrl;
746763
return `${getRelativePath(scriptUrl)}:${location.lineNumber}`;
747764
}
@@ -773,7 +790,7 @@ function createCommandContext(inspector) {
773790
get backtrace() {
774791
currentBacktrace.forEach((callFrame, idx) => {
775792
const { location: { scriptId, lineNumber, columnNumber }, functionName } = callFrame;
776-
const script = scripts[scriptId];
793+
const script = knownScripts[scriptId];
777794
const relativeUrl = (script && getRelativePath(script && script.url)) || '<unknown>';
778795
const name = functionName || '(anonymous)';
779796
print(`#${idx} ${name} ${relativeUrl}:${lineNumber + 1}:${columnNumber}`);
@@ -875,7 +892,7 @@ function createCommandContext(inspector) {
875892
const { scriptId, lineNumber } = currentSourceLocation = topFrame.location;
876893
currentFrame = topFrame.callFrameId;
877894

878-
const script = scripts[scriptId];
895+
const script = knownScripts[scriptId];
879896
const scriptUrl = script ? getRelativePath(script.url) : '[unknown]';
880897
print(`${reason} in ${scriptUrl}:${lineNumber + 1}`);
881898

@@ -887,9 +904,11 @@ function createCommandContext(inspector) {
887904

888905
Debugger.on('breakpointResolved', handleBreakpointResolved);
889906

890-
Debugger.on('scriptParsed', ({ scriptId, url }) => {
907+
Debugger.on('scriptParsed', script => {
908+
const { scriptId, url } = script;
891909
if (url) {
892-
scripts[scriptId] = { url };
910+
script.isNative = url.replace('.js', '') in natives || url === 'bootstrap_node.js';
911+
knownScripts[scriptId] = script;
893912
}
894913
});
895914

0 commit comments

Comments
 (0)