From e41e078159e3eb5907e53f54234ee3e1aed21bb1 Mon Sep 17 00:00:00 2001 From: Brian White Date: Fri, 3 Dec 2010 07:45:00 -0500 Subject: [PATCH] Make sure REPL doesn't get borked when invalid REPL keywords are entered --- lib/repl.js | 68 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index fb08593b29..d5b55f2027 100644 --- a/lib/repl.js +++ b/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();