From 84c426cb60751b85be817d44a21646393762edc6 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sun, 15 Dec 2019 17:43:46 +0100 Subject: [PATCH] repl: properly handle `repl.repl` The repl property is set so that it's possible to inspect the instances own properties during runtime. This was never tested and it was also only exported in case the instance was started with `.start()` instead of using the constructor directly. In case that more than a single instance was created, all instances got access to the first instance. From now on the repl property is only exported in case the repl is starte as standalone program. PR-URL: https://github.com/nodejs/node/pull/30981 Reviewed-By: James M Snell Reviewed-By: Rich Trott --- lib/repl.js | 22 +++++++++----------- test/parallel/test-repl-options.js | 25 +++++++++++++++++++++++ test/parallel/test-repl-reverse-search.js | 4 ++-- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index d9efb8c5ece..44a265a2cca 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -234,10 +234,14 @@ function REPLServer(prompt, throw new ERR_INVALID_REPL_EVAL_CONFIG(); } - // Add this listener only once and use a WeakSet that contains the REPLs - // domains. Otherwise we'd have to add a single listener to each REPL instance - // and that could trigger the `MaxListenersExceededWarning`. - if (!options[kStandaloneREPL] && !addedNewListener) { + if (options[kStandaloneREPL]) { + // It is possible to introspect the running REPL accessing this variable + // from inside the REPL. This is useful for anyone working on the REPL. + exports.repl = this; + } else if (!addedNewListener) { + // Add this listener only once and use a WeakSet that contains the REPLs + // domains. Otherwise we'd have to add a single listener to each REPL + // instance and that could trigger the `MaxListenersExceededWarning`. process.prependListener('newListener', (event, listener) => { if (event === 'uncaughtException' && process.domain && @@ -895,14 +899,8 @@ exports.start = function(prompt, useGlobal, ignoreUndefined, replMode) { - const repl = new REPLServer(prompt, - source, - eval_, - useGlobal, - ignoreUndefined, - replMode); - if (!exports.repl) exports.repl = repl; - return repl; + return new REPLServer( + prompt, source, eval_, useGlobal, ignoreUndefined, replMode); }; REPLServer.prototype.setupHistory = function setupHistory(historyFile, cb) { diff --git a/test/parallel/test-repl-options.js b/test/parallel/test-repl-options.js index 43270e7412d..99f9fa8a605 100644 --- a/test/parallel/test-repl-options.js +++ b/test/parallel/test-repl-options.js @@ -24,6 +24,9 @@ const common = require('../common'); const ArrayStream = require('../common/arraystream'); const assert = require('assert'); const repl = require('repl'); +const cp = require('child_process'); + +assert.strictEqual(repl.repl, undefined); common.expectWarning({ DeprecationWarning: { @@ -120,3 +123,25 @@ assert.strictEqual(r4.ignoreUndefined, false); assert.strictEqual(r4.replMode, repl.REPL_MODE_SLOPPY); assert.strictEqual(r4.historySize, 30); r4.close(); + +// Check the standalone REPL +{ + const child = cp.spawn(process.execPath, ['--interactive']); + let output = ''; + + child.stdout.setEncoding('utf8'); + child.stdout.on('data', (data) => { + output += data; + }); + + child.on('exit', common.mustCall(() => { + const results = output.replace(/^> /mg, '').split('\n').slice(2); + assert.deepStrictEqual(results, ['undefined', '']); + })); + + child.stdin.write( + 'assert.ok(util.inspect(repl.repl, {depth: -1}).includes("REPLServer"));\n' + ); + child.stdin.write('.exit'); + child.stdin.end(); +} diff --git a/test/parallel/test-repl-reverse-search.js b/test/parallel/test-repl-reverse-search.js index a7c73630018..baafdc9d8fb 100644 --- a/test/parallel/test-repl-reverse-search.js +++ b/test/parallel/test-repl-reverse-search.js @@ -251,8 +251,8 @@ const tests = [ '\x1B[1G', '\x1B[0J', `${prompt}ab = "aaaa"`, '\x1B[14G', '\x1B[1G', '\x1B[0J', - `${prompt}repl.repl.historyIndex`, '\x1B[25G', '\n// -1', - '\x1B[19C\x1B[1A', + `${prompt}repl.repl.historyIndex`, '\x1B[25G', '\n// 8', + '\x1B[20C\x1B[1A', '\x1B[1B', '\x1B[2K', '\x1B[1A', '\nfwd-i-search: _', '\x1B[1A', '\x1B[25G', '\x1B[3G', '\x1B[0J',