From f0bf6bb024f86412c2dbe6f0ea1f984a3a25ec2a Mon Sep 17 00:00:00 2001 From: Alex Kocharin Date: Thu, 9 Apr 2015 18:55:26 +0300 Subject: [PATCH] readline: fix calling constructor without new Previously, we detected options object based on amount of arguments supplied. But if we're calling readline without new operator, constructor gets re-called and will always have 4 arguments. PR-URL: https://github.com/iojs/io.js/pull/1385 Reviewed-By: Ben Noordhuis --- lib/readline.js | 5 ++++- test/parallel/test-readline-interface.js | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/readline.js b/lib/readline.js index 63a7aa8..a684501 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -26,7 +26,10 @@ exports.createInterface = function(input, output, completer, terminal) { function Interface(input, output, completer, terminal) { if (!(this instanceof Interface)) { - return new Interface(input, output, completer, terminal); + // call the constructor preserving original number of arguments + const self = Object.create(Interface.prototype); + Interface.apply(self, arguments); + return self; } this._sawReturn = false; diff --git a/test/parallel/test-readline-interface.js b/test/parallel/test-readline-interface.js index ae8a418..69eb4bf 100644 --- a/test/parallel/test-readline-interface.js +++ b/test/parallel/test-readline-interface.js @@ -216,6 +216,18 @@ function isWarned(emitter) { fi.emit('data', ''); // removes listener }); + // calling readline without `new` + fi = new FakeInput(); + rli = readline.Interface({ input: fi, output: fi, terminal: terminal }); + called = false; + rli.on('line', function(line) { + called = true; + assert.equal(line, 'asdf'); + }); + fi.emit('data', 'asdf\n'); + assert.ok(called); + rli.close(); + if (terminal) { // question fi = new FakeInput(); -- 2.7.4