From 0459a230631f6ff44f63d46396c474c13e6c232c Mon Sep 17 00:00:00 2001 From: Nirk Niggler Date: Thu, 3 Jan 2013 09:27:55 -0500 Subject: [PATCH] REPL: fix floating point number parsing In JS, the expression ".1" is a floating point number. Issue 4268 concerns the REPL interpreting floating point numbers that lead with a "." as keywords. The original bugfix worked for this specific case but not for the general case: var x = [ .1, .2, .3 ]; The attached change and test (`.1+.1` should be `.2`) fix the bug. Closes #4513. --- lib/repl.js | 2 +- test/simple/test-repl.js | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/repl.js b/lib/repl.js index e257f55e23..4b10f6bb48 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -206,7 +206,7 @@ function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) { // Check to see if a REPL keyword was used. If it returns true, // display next prompt and return. - if (cmd && cmd.charAt(0) === '.' && cmd != parseFloat(cmd)) { + if (cmd && cmd.charAt(0) === '.' && isNaN(parseFloat(cmd))) { var matches = cmd.match(/^(\.[^\s]+)\s*(.*)$/); var keyword = matches && matches[1]; var rest = matches && matches[2]; diff --git a/test/simple/test-repl.js b/test/simple/test-repl.js index d03cccc355..944d7a2385 100644 --- a/test/simple/test-repl.js +++ b/test/simple/test-repl.js @@ -122,6 +122,9 @@ function error_test() { // Floating point numbers are not interpreted as REPL commands. { client: client_unix, send: '.1234', expect: '0.1234' }, + // Floating point expressions are not interpreted as REPL commands + { client: client_unix, send: '.1+.1', + expect: '0.2' }, // Can parse valid JSON { client: client_unix, send: 'JSON.parse(\'{"valid": "json"}\');', expect: '{ valid: \'json\' }'},