Skip to content

Commit 59f6b5d

Browse files
thefourtheyeevanlucas
authored andcommitted
repl: Prevent crash when tab-completed with Proxy
If the proxy objects don't have a valid `hasOwnPropertyNames` trap, REPL crashes with a `TypeError`, as per the bug report #2119 > var proxy = Proxy.create({ fix: function() { return {}; } }); undefined > proxy.<tab> TypeError: Proxy handler #<Object> has no 'getOwnPropertyNames' trap at Function.getOwnPropertyNames (native) at repl.js:644:40 at REPLServer.defaultEval (repl.js:169:5) at bound (domain.js:254:14) at REPLServer.runBound [as eval] (domain.js:267:12) at REPLServer.complete (repl.js:639:14) at REPLServer.complete [as completer] (repl.js:207:10) at REPLServer.Interface._tabComplete (readline.js:377:8) at REPLServer.Interface._ttyWrite (readline.js:845:14) at ReadStream.onkeypress (readline.js:105:10) This patch traps the error thrown and suppresses it. PR-URL: #2120 Fixes: #2119 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Evan Lucas <[email protected]>
1 parent 47e2c5c commit 59f6b5d

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/repl.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,14 @@ REPLServer.prototype.complete = function(line, callback) {
641641

642642
if (obj != null) {
643643
if (typeof obj === 'object' || typeof obj === 'function') {
644-
memberGroups.push(Object.getOwnPropertyNames(obj));
644+
try {
645+
memberGroups.push(Object.getOwnPropertyNames(obj));
646+
} catch (ex) {
647+
// Probably a Proxy object without `getOwnPropertyNames` trap.
648+
// We simply ignore it here, as we don't want to break the
649+
// autocompletion. Fixes the bug
650+
// https://github.com/nodejs/io.js/issues/2119
651+
}
645652
}
646653
// works for non-objects
647654
try {

test/parallel/test-repl-tab-complete.js

+16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
'use strict';
2+
3+
// Flags: --harmony-proxies
4+
25
var common = require('../common');
36
var assert = require('assert');
47
var util = require('util');
@@ -232,3 +235,16 @@ putIn.run([
232235
testMe.complete('cus', common.mustCall(function(error, data) {
233236
assert.deepEqual(data, [['custom'], 'cus']);
234237
}));
238+
239+
// Make sure tab completion doesn't crash REPL with half-baked proxy objects.
240+
// See: https://github.com/nodejs/io.js/issues/2119
241+
putIn.run(['.clear']);
242+
243+
putIn.run([
244+
'var proxy = Proxy.create({});'
245+
]);
246+
247+
testMe.complete('proxy.', common.mustCall(function(error, data) {
248+
assert.strictEqual(error, null);
249+
assert.deepEqual(data, [[], 'proxy.']);
250+
}));

0 commit comments

Comments
 (0)