Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
repl: fix auto-complete crash in REPL mode when set env NODE_MODULE_C…
…ONTEXTS=1

Set env `NODE_MODULE_CONTEXTS` in REPL mode, when type the `Tab`, the process exit with an `TypeError`:

`env NODE_MODULE_CONTEXTS=1 iojs`
> repl.js:670
          if (!hasOwnProperty(uniq, c)) {
               ^
TypeError: Cannot convert undefined or null to object
    at hasOwnProperty (native)
    at completionGroupsLoaded (repl.js:670:16)
    at REPLServer.complete (repl.js:571:11)
    at REPLServer.complete [as completer] (repl.js:169:10)
    at REPLServer.Interface._tabComplete (readline.js:362:8)
    at REPLServer.Interface._ttyWrite (readline.js:830:14)
    at ReadStream.onkeypress (readline.js:90:10)
    at emitTwo (events.js:87:13)
    at ReadStream.emit (events.js:169:7)
    at readline.js:1156:14

It's a bug in `hasOwnProperty` in repl.js:

origin:
function hasOwnProperty(obj, prop) {
  return Object.prototype.hasOwnProperty.call(obj, prop);
}

fix:
var hasOwnProperty = function (obj, prop) {
  return Object.prototype.hasOwnProperty.call(obj, prop);
}
  • Loading branch information
abbshr committed Mar 16, 2015
commit 9382e0985860bb6bf2e2e8336d5a34d52aa4fff7
8 changes: 7 additions & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ const debug = util.debuglog('repl');
// If obj.hasOwnProperty has been overridden, then calling
// obj.hasOwnProperty(prop) will break.
// See: https://github.com/joyent/node/issues/1707
function hasOwnProperty(obj, prop) {

// Don't write as `function hasOwnProperty() { ... }`,
// or this custom function will never be executed.
// Because it is overrided by origin `hasOwnProperty`,
// and run origin `hasOwnProperty(anyObject)` directly will always go into crash.
var hasOwnProperty = function (obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}

Expand Down Expand Up @@ -667,6 +672,7 @@ REPLServer.prototype.complete = function(line, callback) {
group.sort();
for (var j = 0; j < group.length; j++) {
c = group[j];
// the custom `hasOwnProperty`
if (!hasOwnProperty(uniq, c)) {
completions.push(c);
uniq[c] = true;
Expand Down