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
This commit is contained in:
Fedor Indutny 2012-02-18 00:18:11 +06:00
parent ce485791db
commit ae5e23310e
2 changed files with 6 additions and 1 deletions

View File

@ -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 File

@ -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 },