From 59f6b5da2ab8d5db7029641707e9aa0b0f4dae15 Mon Sep 17 00:00:00 2001 From: Sakthipriyan Vairamani Date: Tue, 7 Jul 2015 22:31:21 +0530 Subject: [PATCH] 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 https://github.com/nodejs/io.js/issues/2119 > var proxy = Proxy.create({ fix: function() { return {}; } }); undefined > proxy. TypeError: Proxy handler # 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: https://github.com/nodejs/io.js/pull/2120 Fixes: https://github.com/nodejs/io.js/issues/2119 Reviewed-By: Colin Ihrig Reviewed-By: Evan Lucas --- lib/repl.js | 9 ++++++++- test/parallel/test-repl-tab-complete.js | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/repl.js b/lib/repl.js index 96532910b4d7e4..763e283816bb64 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -641,7 +641,14 @@ REPLServer.prototype.complete = function(line, callback) { if (obj != null) { if (typeof obj === 'object' || typeof obj === 'function') { - memberGroups.push(Object.getOwnPropertyNames(obj)); + try { + memberGroups.push(Object.getOwnPropertyNames(obj)); + } catch (ex) { + // Probably a Proxy object without `getOwnPropertyNames` trap. + // We simply ignore it here, as we don't want to break the + // autocompletion. Fixes the bug + // https://github.com/nodejs/io.js/issues/2119 + } } // works for non-objects try { diff --git a/test/parallel/test-repl-tab-complete.js b/test/parallel/test-repl-tab-complete.js index a1b901f065eace..856fd9b041f973 100644 --- a/test/parallel/test-repl-tab-complete.js +++ b/test/parallel/test-repl-tab-complete.js @@ -1,4 +1,7 @@ 'use strict'; + +// Flags: --harmony-proxies + var common = require('../common'); var assert = require('assert'); var util = require('util'); @@ -232,3 +235,16 @@ putIn.run([ testMe.complete('cus', common.mustCall(function(error, data) { assert.deepEqual(data, [['custom'], 'cus']); })); + +// Make sure tab completion doesn't crash REPL with half-baked proxy objects. +// See: https://github.com/nodejs/io.js/issues/2119 +putIn.run(['.clear']); + +putIn.run([ + 'var proxy = Proxy.create({});' +]); + +testMe.complete('proxy.', common.mustCall(function(error, data) { + assert.strictEqual(error, null); + assert.deepEqual(data, [[], 'proxy.']); +}));