Browse Source

Make sure REPL doesn't get borked when invalid REPL keywords are entered

v0.7.4-release
Brian White 14 years ago
committed by Ryan Dahl
parent
commit
e41e078159
  1. 68
      lib/repl.js

68
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 = '';
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) {
// 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;
// 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…
Cancel
Save