Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
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
35 changes: 31 additions & 4 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const Module = require('module');
const domain = require('domain');
const debug = util.debuglog('repl');
const errors = require('internal/errors');
const Buffer = require('buffer').Buffer;

const parentModule = module;
const replMap = new WeakMap();
Expand Down Expand Up @@ -689,8 +690,31 @@ function intFilter(item) {
return /^[A-Za-z_$]/.test(item);
}

const DEFAULT_PROPERTIES = {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if these should be uppercased. (I'd prefer them not to be, fwiw.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better by using uppercase on such a constant.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear: I'm fine with it either way, it just looks a bit weird to me in this specific case, especially the uppercased properties, not the object name itself.

ARRAY: Object.getOwnPropertyNames([]).filter(intFilter),
BUFFER: Object.getOwnPropertyNames(Buffer.alloc(1)).filter(intFilter)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not Buffer.alloc(0)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I don't know that we can alloc a zero-size buffer.

};

function mayBeLargeObject(obj) {
return (Array.isArray(obj) || Buffer.isBuffer(obj));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't address other TypedArray types.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, currently, Buffer.prototype passes the Buffer.isBuffer test. On the other hand, getting Buffer.prototype.length will throw the following error:

> Buffer.prototype.length
TypeError: Method get TypedArray.prototype.length called on incompatible receiver [object Object]
    at Uint8Array.get length [as length] (<anonymous>)
    at repl:1:17
    at ContextifyScript.Script.runInThisContext (vm.js:44:33)
    at REPLServer.defaultEval (repl.js:239:29)
    at bound (domain.js:301:14)
    at REPLServer.runBound [as eval] (domain.js:314:12)
    at REPLServer.onLine (repl.js:433:10)
    at emitOne (events.js:120:20)
    at REPLServer.emit (events.js:210:7)
    at REPLServer.Interface._onLine (readline.js:278:10)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, currently, Buffer.prototype passes the Buffer.isBuffer test.

How about obj instanceof Buffer?

}

function filteredOwnPropertyNames(obj) {
if (!obj) return [];
if (mayBeLargeObject(obj) && obj.length > 1e6) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use a constant for 1e6 like const ARRAY_LENGTH_THRESHOLD?

this._writeToOutput('\r\n');
process.emitWarning(
'Instance is too large that the completion may missing ' +
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like "so" might be better than "that" here (but I'm not a native speaker).

'some customized properties.',
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And "custom" might be better than "customized" here. (As for me, "customized" implies properties that have been changed, not necessarily added.)

'REPLWarning',
undefined,
undefined,
true);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I'm okay with not showing the warning. Autocomplete is not a feature that necessarily has to work 100% of the time.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if there's no warning, I think after this PR landed, developers use Node.js may open several issue about this bug "why autocompletion is wrong", though it's not exactly a bug.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@XadillaX fwiw, the autocompletion in REPL is far from perfect and I, as a user, would not and do not expect it to always show all the completions (though it would be an awesome thing). Even IDEs don't do that reliably for JavaScript.

Copy link
Copy Markdown
Contributor Author

@XadillaX XadillaX Jun 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aqrln The point is:

> var ele = Buffer.alloc(1);
> ele.biu = 1;
> ...
> ele.<tab>
ele.__defineGetter__      ele.__defineSetter__      ele.__lookupGetter__      ele.__lookupSetter__
ele.__proto__             ele.constructor           ele.hasOwnProperty        ele.isPrototypeOf
ele.propertyIsEnumerable  ele.toLocaleString        ele.toString              ele.valueOf

...

ele.biu

> var ele = Buffer.alloc(1e6 + 1); ele.biu = 1;
> ele.<tab>
(node:3635) REPLWarning: Instance is too large so the completion may missing some custom properties.

ele.__defineGetter__      ele.__defineSetter__      ele.__lookupGetter__      ele.__lookupSetter__
ele.__proto__             ele.constructor           ele.hasOwnProperty        ele.isPrototypeOf
ele.propertyIsEnumerable  ele.toLocaleString        ele.toString              ele.valueOf

...

Without warning, the developers may be confused that why there's no biu, that completion is what truly they want to find.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. In that case, the message could be clearer:

The current object has too many own properties. Completion output may be truncated.

or

The current array, Buffer, or TypedArray has too many entries. Certain properties may be missing from completion output.


return Array.isArray(obj) ?
DEFAULT_PROPERTIES.ARRAY :
DEFAULT_PROPERTIES.BUFFER;
}
return Object.getOwnPropertyNames(obj).filter(intFilter);
}

Expand Down Expand Up @@ -732,6 +756,7 @@ function complete(line, callback) {
}
}

var self = this;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would prefer not to introduce a new var self = this into this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I need use this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please use an arrow function so that you don't need the var self = this trick?

var completions;

// list of completion lists, one for each inheritance "level"
Expand Down Expand Up @@ -843,9 +868,11 @@ function complete(line, callback) {
if (this.useGlobal || vm.isContext(this.context)) {
var contextProto = this.context;
while (contextProto = Object.getPrototypeOf(contextProto)) {
completionGroups.push(filteredOwnPropertyNames(contextProto));
completionGroups.push(
filteredOwnPropertyNames.call(this, contextProto));
}
completionGroups.push(filteredOwnPropertyNames(this.context));
completionGroups.push(
filteredOwnPropertyNames.call(this, this.context));
addStandardGlobals(completionGroups, filter);
completionGroupsLoaded();
} else {
Expand All @@ -871,7 +898,7 @@ function complete(line, callback) {
if (obj != null) {
if (typeof obj === 'object' || typeof obj === 'function') {
try {
memberGroups.push(filteredOwnPropertyNames(obj));
memberGroups.push(filteredOwnPropertyNames.call(self, obj));
} catch (ex) {
// Probably a Proxy object without `getOwnPropertyNames` trap.
// We simply ignore it here, as we don't want to break the
Expand All @@ -889,7 +916,7 @@ function complete(line, callback) {
p = obj.constructor ? obj.constructor.prototype : null;
}
while (p !== null) {
memberGroups.push(filteredOwnPropertyNames(p));
memberGroups.push(filteredOwnPropertyNames.call(self, p));
p = Object.getPrototypeOf(p);
// Circular refs possible? Let's guard against that.
sentinel--;
Expand Down
29 changes: 29 additions & 0 deletions test/parallel/test-repl-tab-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

const common = require('../common');
const assert = require('assert');
const Buffer = require('buffer').Buffer;
Copy link
Copy Markdown
Contributor

@aqrln aqrln Jun 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure you don't need to import it in the test.


// We have to change the directory to ../fixtures before requiring repl
// in order to make the tests for completion of node_modules work properly
Expand Down Expand Up @@ -305,6 +306,34 @@ testMe.complete('.b', common.mustCall((error, data) => {
assert.deepStrictEqual(data, [['break'], 'b']);
}));

// tab completion for large buffer
[ Array, Buffer ].forEach((type) => {
putIn.run(['.clear']);

if (type === Array) {
putIn.run(['var ele = []; for (let i = 0; i < 1e7; i++) ele.push(i);']);
} else {
putIn.run(['var ele = Buffer.alloc(1e8)']);
}

common.hijackStderr(common.mustCall((err) => {
process.nextTick(function() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small and not essential nit: could you please use an arrow function here to be consistent with the surrounding code?

assert.ok(/REPLWarning: Instance is too large that the/.test(err));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please test the full warning message here?

});
}));
testMe.complete('ele.', common.mustCall((err, data) => {
common.restoreStderr();
assert.ifError(err);

const ele = (type === Array) ? [] : Buffer.alloc(1);

data[0].forEach((key) => {
if (!key) return;
assert.notStrictEqual(ele[key.substr(4)], undefined);
});
}));
});

const testNonGlobal = repl.start({
input: putIn,
output: putIn,
Expand Down