|
20 | 20 | * IN THE SOFTWARE. |
21 | 21 | */ |
22 | 22 | 'use strict'; |
| 23 | +const Path = require('path'); |
23 | 24 | const Repl = require('repl'); |
24 | 25 | const vm = require('vm'); |
25 | 26 |
|
| 27 | +const NATIVES = process.binding('natives'); |
| 28 | + |
| 29 | +function getRelativePath(filename) { |
| 30 | + const dir = `${Path.resolve()}/`; |
| 31 | + |
| 32 | + // Change path to relative, if possible |
| 33 | + if (filename.indexOf(dir) === 0) { |
| 34 | + return filename.slice(dir.length); |
| 35 | + } |
| 36 | + return filename; |
| 37 | +} |
| 38 | + |
26 | 39 | function toCallback(promise, callback) { |
27 | 40 | function forward(...args) { |
28 | 41 | process.nextTick(() => callback(...args)); |
29 | 42 | } |
30 | 43 | promise.then(forward.bind(null, null), forward); |
31 | 44 | } |
32 | 45 |
|
| 46 | +// Adds spaces and prefix to number |
| 47 | +// maxN is a maximum number we should have space for |
| 48 | +function leftPad(n, prefix, maxN) { |
| 49 | + const s = n.toString(); |
| 50 | + const nchars = Math.max(2, String(maxN).length) + 1; |
| 51 | + const nspaces = nchars - s.length - 1; |
| 52 | + |
| 53 | + return prefix + ' '.repeat(nspaces) + s; |
| 54 | +} |
| 55 | + |
| 56 | +function markSourceColumn(sourceText, position, repl) { |
| 57 | + if (!sourceText) return ''; |
| 58 | + |
| 59 | + const head = sourceText.slice(0, position); |
| 60 | + let tail = sourceText.slice(position); |
| 61 | + |
| 62 | + // Colourize char if stdout supports colours |
| 63 | + if (repl.useColors) { |
| 64 | + tail = tail.replace(/(.+?)([^\w]|$)/, '\u001b[32m$1\u001b[39m$2'); |
| 65 | + } |
| 66 | + |
| 67 | + // Return source line with coloured char at `position` |
| 68 | + return [head, tail].join(''); |
| 69 | +} |
| 70 | + |
33 | 71 | function startRepl(inspector) { |
34 | | - let repl; // forward declaration |
| 72 | + const { Debugger } = inspector; |
| 73 | + |
| 74 | + let repl; // eslint-disable-line prefer-const |
35 | 75 | let lastCommand; |
36 | 76 |
|
| 77 | + const knownScripts = {}; |
| 78 | + const watchedExpressions = []; |
| 79 | + const knownBreakpoints = []; |
| 80 | + |
| 81 | + // let currentBacktrace; |
| 82 | + let selectedFrame; |
| 83 | + |
| 84 | + const print = inspector.print.bind(inspector); |
| 85 | + |
37 | 86 | function prepareControlCode(input) { |
38 | 87 | if (input === '\n') return lastCommand; |
39 | 88 | // exec process.title => exec("process.title"); |
@@ -61,6 +110,73 @@ function startRepl(inspector) { |
61 | 110 | } |
62 | 111 | } |
63 | 112 |
|
| 113 | + function watchers() { |
| 114 | + return Promise.resolve(watchedExpressions); |
| 115 | + } |
| 116 | + |
| 117 | + // List source code |
| 118 | + function list(delta = 5) { |
| 119 | + const { scriptId, lineNumber, columnNumber } = selectedFrame.location; |
| 120 | + const start = Math.max(1, lineNumber - delta + 1); |
| 121 | + const end = lineNumber + delta + 1; |
| 122 | + |
| 123 | + return Debugger.getScriptSource({ scriptId }) |
| 124 | + .then(({ scriptSource }) => { |
| 125 | + const lines = scriptSource.split('\n'); |
| 126 | + for (let i = start; i <= lines.length && i <= end; ++i) { |
| 127 | + const isCurrent = i === (lineNumber + 1); |
| 128 | + |
| 129 | + let lineText = lines[i - 1]; |
| 130 | + if (isCurrent) { |
| 131 | + lineText = markSourceColumn(lineText, columnNumber, inspector.repl); |
| 132 | + } |
| 133 | + |
| 134 | + let isBreakpoint = false; |
| 135 | + knownBreakpoints.forEach(({ location }) => { |
| 136 | + if (location && location.scriptId === scriptId && (location.lineNumber + 1) === i) { |
| 137 | + isBreakpoint = true; |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | + let prefixChar = ' '; |
| 142 | + if (isCurrent) { |
| 143 | + prefixChar = '>'; |
| 144 | + } else if (isBreakpoint) { |
| 145 | + prefixChar = '*'; |
| 146 | + } |
| 147 | + print(`${leftPad(i, prefixChar, end)} ${lineText}`); |
| 148 | + } |
| 149 | + }) |
| 150 | + .then(null, error => { |
| 151 | + print('You can\'t list source code right now'); |
| 152 | + throw error; |
| 153 | + }); |
| 154 | + } |
| 155 | + |
| 156 | + Debugger.on('paused', ({ callFrames, reason /* , hitBreakpoints */ }) => { |
| 157 | + // Save execution context's data |
| 158 | + // currentBacktrace = callFrames; |
| 159 | + selectedFrame = callFrames[0]; |
| 160 | + const { scriptId, lineNumber } = selectedFrame.location; |
| 161 | + |
| 162 | + const script = knownScripts[scriptId]; |
| 163 | + const scriptUrl = script ? getRelativePath(script.url) : '[unknown]'; |
| 164 | + print(`${reason === 'other' ? 'break' : reason} in ${scriptUrl}:${lineNumber + 1}`); |
| 165 | + |
| 166 | + inspector.suspendReplWhile(() => |
| 167 | + watchers(true) |
| 168 | + .then(() => list(2))); |
| 169 | + }); |
| 170 | + |
| 171 | + Debugger.on('scriptParsed', (script) => { |
| 172 | + const { scriptId, url } = script; |
| 173 | + if (url) { |
| 174 | + knownScripts[scriptId] = Object.assign({ |
| 175 | + isNative: url.replace('.js', '') in NATIVES || url === 'bootstrap_node.js', |
| 176 | + }, script); |
| 177 | + } |
| 178 | + }); |
| 179 | + |
64 | 180 | const replOptions = { |
65 | 181 | prompt: 'debug> ', |
66 | 182 | input: inspector.stdin, |
|
0 commit comments