8026161: Don't narrow floating-point literals in the lexer
authorattila
Thu, 17 Oct 2013 12:38:50 +0200
changeset 21437 9f558215d924
parent 20950 5fdef2bba733
child 21438 4292865c758b
8026161: Don't narrow floating-point literals in the lexer Reviewed-by: hannesw, jlaskey
nashorn/src/jdk/nashorn/internal/parser/Lexer.java
nashorn/src/jdk/nashorn/internal/runtime/JSType.java
nashorn/test/script/basic/JDK-8026161.js
nashorn/test/script/basic/JDK-8026161.js.EXPECTED
nashorn/test/src/jdk/nashorn/api/javaaccess/MethodAccessTest.java
--- a/nashorn/src/jdk/nashorn/internal/parser/Lexer.java	Wed Oct 16 10:15:55 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/parser/Lexer.java	Thu Oct 17 12:38:50 2013 +0200
@@ -47,7 +47,6 @@
 import jdk.nashorn.internal.runtime.ECMAErrors;
 import jdk.nashorn.internal.runtime.ErrorManager;
 import jdk.nashorn.internal.runtime.JSErrorType;
-import jdk.nashorn.internal.runtime.JSType;
 import jdk.nashorn.internal.runtime.ParserException;
 import jdk.nashorn.internal.runtime.Source;
 import jdk.nashorn.internal.runtime.options.Options;
@@ -1054,16 +1053,6 @@
     }
 
     /**
-     * Convert string to number.
-     *
-     * @param valueString String to convert.
-     * @return Converted number.
-     */
-    private static Number valueOf(final String valueString) throws NumberFormatException {
-        return JSType.narrowestIntegerRepresentation(Double.valueOf(valueString));
-    }
-
-    /**
      * Scan a number.
      */
     protected void scanNumber() {
@@ -1623,7 +1612,7 @@
         case HEXADECIMAL:
             return Lexer.valueOf(source.getString(start + 2, len - 2), 16); // number
         case FLOATING:
-            return Lexer.valueOf(source.getString(start, len)); // number
+            return Double.valueOf(source.getString(start, len)); // number
         case STRING:
             return source.getString(start, len); // String
         case ESCSTRING:
--- a/nashorn/src/jdk/nashorn/internal/runtime/JSType.java	Wed Oct 16 10:15:55 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/JSType.java	Thu Oct 17 12:38:50 2013 +0200
@@ -210,26 +210,6 @@
     }
 
     /**
-     * Get the smallest integer representation of a number. Returns an Integer
-     * for something that is int representable, and Long for something that
-     * is long representable. If the number needs to be a double, this is an
-     * identity function
-     *
-     * @param number number to check
-     *
-     * @return Number instanceof the narrowest possible integer representation for number
-     */
-    public static Number narrowestIntegerRepresentation(final double number) {
-        if (isRepresentableAsInt(number)) {
-            return (int)number;
-        } else if (isRepresentableAsLong(number)) {
-            return (long)number;
-        } else {
-            return number;
-        }
-    }
-
-    /**
      * Check whether an object is primitive
      *
      * @param obj an object
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8026161.js	Thu Oct 17 12:38:50 2013 +0200
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ * 
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ * 
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ * 
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ * 
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8026161: Don't narrow floating-point literals in the lexer
+ *
+ * @test
+ * @run
+ */
+
+print(new java.awt.Color(1, 1, 1)) // creates Color[r=1,g=1,b=1]
+print(new java.awt.Color(1.0, 1.0, 1.0)) // Color[r=255,g=255,b=255]
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8026161.js.EXPECTED	Thu Oct 17 12:38:50 2013 +0200
@@ -0,0 +1,2 @@
+java.awt.Color[r=1,g=1,b=1]
+java.awt.Color[r=255,g=255,b=255]
--- a/nashorn/test/src/jdk/nashorn/api/javaaccess/MethodAccessTest.java	Wed Oct 16 10:15:55 2013 +0200
+++ b/nashorn/test/src/jdk/nashorn/api/javaaccess/MethodAccessTest.java	Thu Oct 17 12:38:50 2013 +0200
@@ -412,7 +412,7 @@
 
     @Test
     public void accessMethodMixedWithEllipsis() throws ScriptException {
-        assertArrayEquals(new Object[] { "Hello", 10, true, -100500, 80 }, (Object[])e.eval("o.methodMixedWithEllipsis('Hello', 10, true, -100500,80.0);"));
+        assertArrayEquals(new Object[] { "Hello", 10, true, -100500, 80d }, (Object[])e.eval("o.methodMixedWithEllipsis('Hello', 10, true, -100500,80.0);"));
         assertArrayEquals(new Object[] { "Nashorn", 15 }, (Object[])e.eval("o.methodMixedWithEllipsis('Nashorn',15);"));
     }
 
@@ -431,8 +431,8 @@
 
     @Test
     public void accessMethodDoubleVSintOverloaded() throws ScriptException {
-        assertEquals("int", e.eval("o.overloadedMethodDoubleVSint(0.0);"));
-        assertEquals("int", e.eval("o.overloadedMethodDoubleVSint(1000.0);"));
+        assertEquals("double", e.eval("o.overloadedMethodDoubleVSint(0.0);"));
+        assertEquals("double", e.eval("o.overloadedMethodDoubleVSint(1000.0);"));
         assertEquals("double", e.eval("o.overloadedMethodDoubleVSint(0.01);"));
         assertEquals("double", e.eval("o.overloadedMethodDoubleVSint(100.02);"));
         assertEquals("int", e.eval("o.overloadedMethodDoubleVSint(0);"));