Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
fix syntax error handling for 'throw ...', fix return value assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny authored and ry committed Sep 11, 2011
1 parent 55c1546 commit df480e0
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/repl.js
Expand Up @@ -163,9 +163,9 @@ function REPLServer(prompt, stream, eval) {
self.context,
'repl',
function(e, ret) {
if (e) return finish(e);
if (e && !isSyntaxError(e)) return finish(e);

if (ret === 'function' || e) {
if (typeof ret === 'function' || e) {
// Now as statement without parens.
self.eval(self.bufferedCommand, self.context, 'repl', finish);
} else {
Expand All @@ -177,22 +177,26 @@ function REPLServer(prompt, stream, eval) {
finish(null);
}

function finish(e, ret) {
function isSyntaxError(e) {
// Convert error to string
e = e && (e.stack || e.toString());
return e && e.match(/^SyntaxError/) &&
!(e.match(/^SyntaxError: Unexpected token .*\n/) &&
e.match(/\n at Object.parse \(native\)\n/));
}

function finish(e, ret) {

// If error was SyntaxError and not JSON.parse error
if (e && e.match(/^SyntaxError/) &&
!(e.match(/^SyntaxError: Unexpected token .*\n/) &&
e.match(/\n at Object.parse \(native\)\n/))) {
if (isSyntaxError(e)) {
// Start buffering data like that:
// {
// ... x: 1
// ... }
self.displayPrompt();
return;
} else if (e) {
self.outputStream.write(e + '\n');
self.outputStream.write((e.stack || e) + '\n');
}

// Clear buffer if no SyntaxErrors
Expand Down

0 comments on commit df480e0

Please sign in to comment.