From ae5e23310e64db99ddefbf4904f74a42906d6451 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Sat, 18 Feb 2012 00:18:11 +0600 Subject: [PATCH] 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 --- lib/repl.js | 4 +++- test/simple/test-repl.js | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/repl.js b/lib/repl.js index b63b254..aeb5111 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -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 { diff --git a/test/simple/test-repl.js b/test/simple/test-repl.js index 4fc65f3..3008210 100644 --- a/test/simple/test-repl.js +++ b/test/simple/test-repl.js @@ -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 }, -- 2.7.4