mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
Make sure REPL doesn't get borked when invalid REPL keywords are entered
This commit is contained in:
parent
e4bca19843
commit
e41e078159
70
lib/repl.js
70
lib/repl.js
@ -84,6 +84,7 @@ function REPLServer(prompt, stream) {
|
||||
});
|
||||
|
||||
rli.addListener('line', function(cmd) {
|
||||
var skipCatchall = false;
|
||||
cmd = trimWhitespace(cmd);
|
||||
|
||||
// Check to see if a REPL keyword was used. If it returns true,
|
||||
@ -92,43 +93,50 @@ function REPLServer(prompt, stream) {
|
||||
var matches = cmd.match(/^(\.[^\s]+)\s*(.*)$/);
|
||||
var keyword = matches && matches[1];
|
||||
var rest = matches && matches[2];
|
||||
if (self.parseREPLKeyword(keyword, rest) === true) return;
|
||||
if (self.parseREPLKeyword(keyword, rest) === true) {
|
||||
return;
|
||||
} else {
|
||||
self.stream.write('Invalid REPL keyword\n');
|
||||
skipCatchall = true;
|
||||
}
|
||||
}
|
||||
|
||||
// The catchall for errors
|
||||
try {
|
||||
self.buffered_cmd += cmd;
|
||||
// This try is for determining if the command is complete, or should
|
||||
// continue onto the next line.
|
||||
if (!skipCatchall) {
|
||||
// The catchall for errors
|
||||
try {
|
||||
// Use evalcx to supply the global context
|
||||
var ret = evalcx(self.buffered_cmd, context, 'repl');
|
||||
if (ret !== undefined) {
|
||||
context._ = ret;
|
||||
self.stream.write(exports.writer(ret) + '\n');
|
||||
}
|
||||
self.buffered_cmd += cmd;
|
||||
// This try is for determining if the command is complete, or should
|
||||
// continue onto the next line.
|
||||
try {
|
||||
// Use evalcx to supply the global context
|
||||
var ret = evalcx(self.buffered_cmd, context, 'repl');
|
||||
if (ret !== undefined) {
|
||||
context._ = ret;
|
||||
self.stream.write(exports.writer(ret) + '\n');
|
||||
}
|
||||
|
||||
self.buffered_cmd = '';
|
||||
} catch (e) {
|
||||
// instanceof doesn't work across context switches.
|
||||
if (!(e && e.constructor && e.constructor.name === 'SyntaxError')) {
|
||||
throw e;
|
||||
// It could also be an error from JSON.parse
|
||||
} else if (e &&
|
||||
e.stack &&
|
||||
e.stack.match('Unexpected token ILLEGAL') &&
|
||||
e.stack.match(/Object.parse \(native\)/)) {
|
||||
throw e;
|
||||
self.buffered_cmd = '';
|
||||
} catch (e) {
|
||||
// instanceof doesn't work across context switches.
|
||||
if (!(e && e.constructor && e.constructor.name === 'SyntaxError')) {
|
||||
throw e;
|
||||
// It could also be an error from JSON.parse
|
||||
} else if (e &&
|
||||
e.stack &&
|
||||
e.stack.match('Unexpected token ILLEGAL') &&
|
||||
e.stack.match(/Object.parse \(native\)/)) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// On error: Print the error and clear the buffer
|
||||
if (e.stack) {
|
||||
self.stream.write(e.stack + '\n');
|
||||
} else {
|
||||
self.stream.write(e.toString() + '\n');
|
||||
}
|
||||
self.buffered_cmd = '';
|
||||
}
|
||||
} catch (e) {
|
||||
// On error: Print the error and clear the buffer
|
||||
if (e.stack) {
|
||||
self.stream.write(e.stack + '\n');
|
||||
} else {
|
||||
self.stream.write(e.toString() + '\n');
|
||||
}
|
||||
self.buffered_cmd = '';
|
||||
}
|
||||
|
||||
self.displayPrompt();
|
||||
|
Loading…
Reference in New Issue
Block a user