From 42f4c14fcca620aea76316d0e0a9c753fc5297df Mon Sep 17 00:00:00 2001 From: "eungyu.lee" Date: Sat, 25 Feb 2023 01:05:02 +0900 Subject: [PATCH 1/6] debugger: add a command to set which lines to check for context --- lib/internal/debugger/inspect_repl.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/internal/debugger/inspect_repl.js b/lib/internal/debugger/inspect_repl.js index a1b3f0d705ec4c..95d019aedccedb 100644 --- a/lib/internal/debugger/inspect_repl.js +++ b/lib/internal/debugger/inspect_repl.js @@ -81,7 +81,7 @@ out, o Step out, leaving the current function backtrace, bt Print the current backtrace list Print the source around the current line where execution is currently paused - +setContextLineNumber Set which lines to check for context setBreakpoint, sb Set a breakpoint clearBreakpoint, cb Clear a breakpoint breakpoints List all known breakpoints @@ -381,6 +381,7 @@ function createRepl(inspector) { let currentBacktrace; let selectedFrame; let exitDebugRepl; + let contextLineNumber = 2; function resetOnStart() { knownScripts = {}; @@ -685,6 +686,19 @@ function createRepl(inspector) { }); } + function setContextLineNumber(delta = 2) { + if (!selectedFrame) { + throw new ERR_DEBUGGER_ERROR('Requires execution to be paused'); + } + try { + contextLineNumber = delta; + print(`The contextLine has been changed to ${delta}.`); + } catch (error) { + print("You can't setContextLineNumber source code right now"); + throw error; + } + } + function handleBreakpointResolved({ breakpointId, location }) { const script = knownScripts[location.scriptId]; const scriptUrl = script && script.url; @@ -897,7 +911,7 @@ function createRepl(inspector) { inspector.suspendReplWhile(() => PromisePrototypeThen( - SafePromiseAllReturnArrayLike([formatWatchers(true), selectedFrame.list(2)]), + SafePromiseAllReturnArrayLike([formatWatchers(true), selectedFrame.list(contextLineNumber)]), ({ 0: watcherList, 1: context }) => { const breakContext = watcherList ? `${watcherList}\n${inspect(context)}` : @@ -1159,6 +1173,7 @@ function createRepl(inspector) { }, list, + setContextLineNumber, }); aliasProperties(context, SHORTCUTS); } From 730ad8695f8bb82cf1e5396ad484604f563ff8b8 Mon Sep 17 00:00:00 2001 From: "eungyu.lee" Date: Tue, 28 Feb 2023 15:42:41 +0900 Subject: [PATCH 2/6] test: Check the context line seen through n steps after calling setContextLineNumber --- test/fixtures/debugger/twenty-lines.js | 20 +++++++++ .../test-debugger-set-context-line-number.mjs | 44 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 test/fixtures/debugger/twenty-lines.js create mode 100644 test/sequential/test-debugger-set-context-line-number.mjs diff --git a/test/fixtures/debugger/twenty-lines.js b/test/fixtures/debugger/twenty-lines.js new file mode 100644 index 00000000000000..4aa152202f6742 --- /dev/null +++ b/test/fixtures/debugger/twenty-lines.js @@ -0,0 +1,20 @@ +let x = 0; +x = 1; +x = 2; +x = 3; +x = 4; +x = 5; +x = 6; +x = 7; +x = 8; +x = 9; +x = 10; +x = 11; +x = 12; +x = 13; +x = 14; +x = 15; +x = 16; +x = 17; +x = 18; +module.exports = x; \ No newline at end of file diff --git a/test/sequential/test-debugger-set-context-line-number.mjs b/test/sequential/test-debugger-set-context-line-number.mjs new file mode 100644 index 00000000000000..75e2f0e9a74f02 --- /dev/null +++ b/test/sequential/test-debugger-set-context-line-number.mjs @@ -0,0 +1,44 @@ +import { skipIfInspectorDisabled } from '../common/index.mjs'; +skipIfInspectorDisabled(); + +import { path } from '../common/fixtures.mjs'; +import startCLI from '../common/debugger.js'; + +import assert from 'assert'; + +const script = path('debugger', 'twenty-lines.js'); +const cli = startCLI([script]); + +function onFatal(error) { + cli.quit(); + throw error; +} + +function getLastLine(output) { + const splitedByLine = output.split(';'); + return splitedByLine[splitedByLine.length - 2]; +} + +// Stepping through breakpoints. +try { + await cli.waitForInitialBreak(); + await cli.waitForPrompt(); + + // Make sure the initial value is 2. + await cli.stepCommand('n'); + assert.ok(getLastLine(cli.output).includes('4 x = 3')); + + await cli.command('setContextLineNumber(5)'); + await cli.stepCommand('n'); + assert.ok(getLastLine(cli.output).includes('8 x = 7')); + + await cli.command('setContextLineNumber(3)'); + await cli.stepCommand('n'); + assert.ok(getLastLine(cli.output).includes('7 x = 6')); + await cli.command('list(3)'); + assert.ok(getLastLine(cli.output).includes('7 x = 6')); + + await cli.quit(); +} catch (error) { + onFatal(error); +} From d0c868c75c3b561ad066f24364c5cd84905ce160 Mon Sep 17 00:00:00 2001 From: "eungyu.lee" Date: Thu, 2 Mar 2023 18:11:17 +0900 Subject: [PATCH 3/6] debugger: Add validation parameter must be greater than 0 and must not be a string --- lib/internal/debugger/inspect_repl.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/internal/debugger/inspect_repl.js b/lib/internal/debugger/inspect_repl.js index 95d019aedccedb..1b504438c1b1e6 100644 --- a/lib/internal/debugger/inspect_repl.js +++ b/lib/internal/debugger/inspect_repl.js @@ -46,7 +46,7 @@ const { const { ERR_DEBUGGER_ERROR } = require('internal/errors').codes; -const { validateString } = require('internal/validators'); +const { validateString, validateNumber } = require('internal/validators'); const FS = require('fs'); const Path = require('path'); @@ -690,13 +690,12 @@ function createRepl(inspector) { if (!selectedFrame) { throw new ERR_DEBUGGER_ERROR('Requires execution to be paused'); } - try { - contextLineNumber = delta; - print(`The contextLine has been changed to ${delta}.`); - } catch (error) { - print("You can't setContextLineNumber source code right now"); - throw error; + if (delta === 0) { + return print('A value greater than 0 is required'); } + validateNumber(delta, 'delta', 1); + contextLineNumber = delta; + print(`The contextLine has been changed to ${delta}.`); } function handleBreakpointResolved({ breakpointId, location }) { From d686d4006378568fadeaf985ef5da035ef5813d5 Mon Sep 17 00:00:00 2001 From: "eungyu.lee" Date: Thu, 2 Mar 2023 18:11:45 +0900 Subject: [PATCH 4/6] test: Add a validation test so that the parameter must be greater than 0 and must not be a string. --- test/fixtures/debugger/twenty-lines.js | 2 +- .../test-debugger-set-context-line-number.mjs | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/test/fixtures/debugger/twenty-lines.js b/test/fixtures/debugger/twenty-lines.js index 4aa152202f6742..e29e4801baf8be 100644 --- a/test/fixtures/debugger/twenty-lines.js +++ b/test/fixtures/debugger/twenty-lines.js @@ -17,4 +17,4 @@ x = 15; x = 16; x = 17; x = 18; -module.exports = x; \ No newline at end of file +module.exports = x; diff --git a/test/sequential/test-debugger-set-context-line-number.mjs b/test/sequential/test-debugger-set-context-line-number.mjs index 75e2f0e9a74f02..7962b9bbb7354d 100644 --- a/test/sequential/test-debugger-set-context-line-number.mjs +++ b/test/sequential/test-debugger-set-context-line-number.mjs @@ -15,8 +15,8 @@ function onFatal(error) { } function getLastLine(output) { - const splitedByLine = output.split(';'); - return splitedByLine[splitedByLine.length - 2]; + const splittedByLine = output.split(';'); + return splittedByLine[splittedByLine.length - 2]; } // Stepping through breakpoints. @@ -24,6 +24,12 @@ try { await cli.waitForInitialBreak(); await cli.waitForPrompt(); + await cli.command('setContextLineNumber("1")'); + assert.ok(cli.output.includes('argument must be of type number. Received type string')); + + await cli.command('setContextLineNumber(0)'); + assert.ok(cli.output.includes('A value greater than 0 is required')); + // Make sure the initial value is 2. await cli.stepCommand('n'); assert.ok(getLastLine(cli.output).includes('4 x = 3')); From 664edf47f7322fda9c2fc64479c4382ac4119257 Mon Sep 17 00:00:00 2001 From: "eungyu.lee" Date: Fri, 3 Mar 2023 10:32:49 +0900 Subject: [PATCH 5/6] debugger: Replaced validation logic for 0 with validationNumber function. --- lib/internal/debugger/inspect_repl.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/internal/debugger/inspect_repl.js b/lib/internal/debugger/inspect_repl.js index 1b504438c1b1e6..91c0da67d7fd99 100644 --- a/lib/internal/debugger/inspect_repl.js +++ b/lib/internal/debugger/inspect_repl.js @@ -690,9 +690,6 @@ function createRepl(inspector) { if (!selectedFrame) { throw new ERR_DEBUGGER_ERROR('Requires execution to be paused'); } - if (delta === 0) { - return print('A value greater than 0 is required'); - } validateNumber(delta, 'delta', 1); contextLineNumber = delta; print(`The contextLine has been changed to ${delta}.`); From 2558dce181808180c6d2fb4629a381b5226a1ad6 Mon Sep 17 00:00:00 2001 From: "eungyu.lee" Date: Fri, 3 Mar 2023 10:35:02 +0900 Subject: [PATCH 6/6] test: Testing for error messages from the validationNumber function --- test/sequential/test-debugger-set-context-line-number.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/sequential/test-debugger-set-context-line-number.mjs b/test/sequential/test-debugger-set-context-line-number.mjs index 7962b9bbb7354d..adb6d9ab9e52b0 100644 --- a/test/sequential/test-debugger-set-context-line-number.mjs +++ b/test/sequential/test-debugger-set-context-line-number.mjs @@ -28,7 +28,7 @@ try { assert.ok(cli.output.includes('argument must be of type number. Received type string')); await cli.command('setContextLineNumber(0)'); - assert.ok(cli.output.includes('A value greater than 0 is required')); + assert.ok(cli.output.includes('It must be >= 1. Received 0')); // Make sure the initial value is 2. await cli.stepCommand('n');