repl: fix tab-complete warning

When create a nest repl, will register `Runtime.executionContextCreated`
listener to the inspector session.This patch will fix listener
repeatedly register.

PR-URL: https://github.com/nodejs/node/pull/18881
Fixes: https://github.com/nodejs/node/issues/18284
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
killagu 2018-02-20 20:12:58 +08:00 committed by Ruben Bridgewater
parent 51be03cd57
commit 26231548ad
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
2 changed files with 23 additions and 2 deletions

View File

@ -753,7 +753,7 @@ REPLServer.prototype.createContext = function() {
} else {
sendInspectorCommand((session) => {
session.post('Runtime.enable');
session.on('Runtime.executionContextCreated', ({ params }) => {
session.once('Runtime.executionContextCreated', ({ params }) => {
this[kContextId] = params.context.id;
});
context = vm.createContext();
@ -937,7 +937,6 @@ function complete(line, callback) {
var flat = new ArrayStream(); // make a new "input" stream
var magic = new REPLServer('', flat); // make a nested REPL
replMap.set(magic, replMap.get(this));
magic.resetContext();
flat.run(tmp); // eval the flattened code
// all this is only profitable if the nested REPL
// does not have a bufferedCommand

View File

@ -0,0 +1,22 @@
'use strict';
const common = require('../common');
const repl = require('repl');
const DEFAULT_MAX_LISTENERS = require('events').defaultMaxListeners;
common.ArrayStream.prototype.write = () => {
};
const putIn = new common.ArrayStream();
const testMe = repl.start('', putIn);
// https://github.com/nodejs/node/issues/18284
// Tab-completion should not repeatedly add the
// `Runtime.executionContextCreated` listener
process.on('warning', common.mustNotCall());
putIn.run(['.clear']);
putIn.run(['async function test() {']);
for (let i = 0; i < DEFAULT_MAX_LISTENERS; i++) {
testMe.complete('await Promise.resolve()', () => {});
}