Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repl: minor improvements #25731

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
70 changes: 23 additions & 47 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,6 @@ let processTopLevelAwait;
const parentModule = module;
const replMap = new WeakMap();

const GLOBAL_OBJECT_PROPERTIES = [
'NaN', 'Infinity', 'undefined', 'eval', 'parseInt', 'parseFloat', 'isNaN',
'isFinite', 'decodeURI', 'decodeURIComponent', 'encodeURI',
'encodeURIComponent', 'Object', 'Function', 'Array', 'String', 'Boolean',
'Number', 'Date', 'RegExp', 'Error', 'EvalError', 'RangeError',
'ReferenceError', 'SyntaxError', 'TypeError', 'URIError', 'Math', 'JSON'
];
const GLOBAL_OBJECT_PROPERTY_MAP = {};
for (var n = 0; n < GLOBAL_OBJECT_PROPERTIES.length; n++) {
GLOBAL_OBJECT_PROPERTY_MAP[GLOBAL_OBJECT_PROPERTIES[n]] =
GLOBAL_OBJECT_PROPERTIES[n];
}
const kBufferedCommandSymbol = Symbol('bufferedCommand');
const kContextId = Symbol('contextId');

Expand Down Expand Up @@ -227,10 +215,11 @@ function REPLServer(prompt,
}

function defaultEval(code, context, file, cb) {
var err, result, script, wrappedErr;
var wrappedCmd = false;
var awaitPromise = false;
var input = code;
let result, script, wrappedErr;
let err = null;
let wrappedCmd = false;
let awaitPromise = false;
const input = code;

if (/^\s*\{/.test(code) && /\}\s*$/.test(code)) {
// It's confusing for `{ a : 1 }` to be interpreted as a block
Expand Down Expand Up @@ -374,7 +363,7 @@ function REPLServer(prompt,
}

promise.then((result) => {
finishExecution(undefined, result);
finishExecution(null, result);
}, (err) => {
if (err && process.domain) {
debug('not recoverable, send to domain');
Expand Down Expand Up @@ -648,7 +637,6 @@ function REPLServer(prompt,
self.outputStream.write('npm should be run outside of the ' +
'node repl, in your normal shell.\n' +
'(Press Control-D to exit.)\n');
self.clearBufferedCommand();
self.displayPrompt();
return;
}
Expand Down Expand Up @@ -807,24 +795,17 @@ REPLServer.prototype.createContext = function() {
}, () => {
context = vm.createContext();
});
for (const name of Object.getOwnPropertyNames(global)) {
Object.defineProperty(context, name,
Object.getOwnPropertyDescriptor(global, name));
}
Copy link
Member

Choose a reason for hiding this comment

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

Does this really skip console like it used to? Raw V8 contexts have a (primordial) console property.

It's quite possible the answer is "it doesn't matter either way" but I thought I'd ask anyway. :)

Copy link
Member Author

Choose a reason for hiding this comment

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

It does not skip it. Instead, it just sets it twice. The overhead should be minimal and it's less code that way, so it seemed fine to me.

context.global = context;
const _console = new Console(this.outputStream);
Object.defineProperty(context, 'console', {
configurable: true,
writable: true,
value: _console
});

var names = Object.getOwnPropertyNames(global);
for (var n = 0; n < names.length; n++) {
var name = names[n];
if (name === 'console' || name === 'global')
continue;
if (GLOBAL_OBJECT_PROPERTY_MAP[name] === undefined) {
Object.defineProperty(context, name,
Object.getOwnPropertyDescriptor(global, name));
}
}
}

var module = new CJSModule('<repl>');
Expand Down Expand Up @@ -1137,19 +1118,19 @@ function complete(line, callback) {
}
completionGroups.push(
filteredOwnPropertyNames.call(this, this.context));
addStandardGlobals(completionGroups, filter);
if (filter !== '') addCommonWords(completionGroups);
completionGroupsLoaded();
} else {
this.eval('.scope', this.context, 'repl', function ev(err, globals) {
if (err || !Array.isArray(globals)) {
addStandardGlobals(completionGroups, filter);
if (filter !== '') addCommonWords(completionGroups);
} else if (Array.isArray(globals[0])) {
// Add grouped globals
for (var n = 0; n < globals.length; n++)
completionGroups.push(globals[n]);
} else {
completionGroups.push(globals);
addStandardGlobals(completionGroups, filter);
if (filter !== '') addCommonWords(completionGroups);
}
completionGroupsLoaded();
});
Expand Down Expand Up @@ -1373,21 +1354,16 @@ function _memory(cmd) {
}
}

function addStandardGlobals(completionGroups, filter) {
// Global object properties
// (http://www.ecma-international.org/publications/standards/Ecma-262.htm)
completionGroups.push(GLOBAL_OBJECT_PROPERTIES);
// Common keywords. Exclude for completion on the empty string, b/c
// they just get in the way.
if (filter) {
completionGroups.push([
'async', 'await', 'break', 'case', 'catch', 'const', 'continue',
'debugger', 'default', 'delete', 'do', 'else', 'export', 'false',
'finally', 'for', 'function', 'if', 'import', 'in', 'instanceof', 'let',
'new', 'null', 'return', 'switch', 'this', 'throw', 'true', 'try',
'typeof', 'undefined', 'var', 'void', 'while', 'with', 'yield'
]);
}
function addCommonWords(completionGroups) {
// Only words which do not yet exist as global property should be added to
// this list.
completionGroups.push([
'async', 'await', 'break', 'case', 'catch', 'const', 'continue',
'debugger', 'default', 'delete', 'do', 'else', 'export', 'false',
'finally', 'for', 'function', 'if', 'import', 'in', 'instanceof', 'let',
'new', 'null', 'return', 'switch', 'this', 'throw', 'true', 'try',
'typeof', 'var', 'void', 'while', 'with', 'yield'
]);
}

function _turnOnEditorMode(repl) {
Expand Down
1 change: 0 additions & 1 deletion test/parallel/test-repl-tab-complete-no-warn.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const testMe = repl.start('', putIn);
// `Runtime.executionContextCreated` listener
process.on('warning', common.mustNotCall());

putIn.run(['.clear']);
putIn.run(['async function test() {']);
for (let i = 0; i < DEFAULT_MAX_LISTENERS; i++) {
testMe.complete('await Promise.resolve()', () => {});
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-repl-tab-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ const testNonGlobal = repl.start({
useGlobal: false
});

const builtins = [['Infinity', '', 'Int16Array', 'Int32Array',
const builtins = [['Infinity', 'Int16Array', 'Int32Array',
'Int8Array'], 'I'];

if (common.hasIntl) {
Expand Down