Skip to content

Commit f0bf6bb

Browse files
rlidwkabnoordhuis
authored andcommitted
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: #1385 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 8bc8bd4 commit f0bf6bb

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/readline.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ exports.createInterface = function(input, output, completer, terminal) {
2626

2727
function Interface(input, output, completer, terminal) {
2828
if (!(this instanceof Interface)) {
29-
return new Interface(input, output, completer, terminal);
29+
// call the constructor preserving original number of arguments
30+
const self = Object.create(Interface.prototype);
31+
Interface.apply(self, arguments);
32+
return self;
3033
}
3134

3235
this._sawReturn = false;

test/parallel/test-readline-interface.js

+12
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,18 @@ function isWarned(emitter) {
216216
fi.emit('data', ''); // removes listener
217217
});
218218

219+
// calling readline without `new`
220+
fi = new FakeInput();
221+
rli = readline.Interface({ input: fi, output: fi, terminal: terminal });
222+
called = false;
223+
rli.on('line', function(line) {
224+
called = true;
225+
assert.equal(line, 'asdf');
226+
});
227+
fi.emit('data', 'asdf\n');
228+
assert.ok(called);
229+
rli.close();
230+
219231
if (terminal) {
220232
// question
221233
fi = new FakeInput();

0 commit comments

Comments
 (0)