Permalink
Browse files

repl: remove double calls where possible

Repl is doing double evaluation of code: wrapped in parens and without
them. That's needed to allow users typing multiline chunks of code by
handling syntax errors on repl side. However if function declaration is
wrapped in parens (`(function a() {})`) calling it will be impossible,
so we're evaluating functions twice. That works fine for declaration,
but if entered code chunk returns function - it should not be called
twice.

fix #2773
  • Loading branch information...
indutny committed Feb 17, 2012
1 parent ce48579 commit ae5e23310e64db99ddefbf4904f74a42906d6451
Showing with 6 additions and 1 deletion.
  1. +3 −1 lib/repl.js
  2. +3 −0 test/simple/test-repl.js
View
@@ -185,7 +185,9 @@ function REPLServer(prompt, stream, eval, useGlobal, ignoreUndefined) {
function(e, ret) {
if (e && !isSyntaxError(e)) return finish(e);
- if (typeof ret === 'function' || e) {
+ if (typeof ret === 'function' &&
+ /^[\r\n\s]*function/.test(evalCmd) ||
+ e) {
// Now as statement without parens.
self.eval(evalCmd, self.context, 'repl', finish);
} else {
View
@@ -124,6 +124,9 @@ function error_test() {
expect: prompt_unix },
{ client: client_unix, send: 'blah()',
expect: '1\n' + prompt_unix },
+ // Functions should not evaluate twice (#2773)
+ { client: client_unix, send: 'var I = [1,2,3,function() {}]; I.pop()',
+ expect: '[Function]' },
// Multiline object
{ client: client_unix, send: '{ a: ',
expect: prompt_multiline },

0 comments on commit ae5e233

Please sign in to comment.