8026161: Don't narrow floating-point literals in the lexer
Reviewed-by: hannesw, jlaskey
This commit is contained in:
parent
d22bf99ead
commit
34ad8867bc
@ -47,7 +47,6 @@ import static jdk.nashorn.internal.parser.TokenType.XML;
|
||||
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;
|
||||
@ -1053,16 +1052,6 @@ public class Lexer extends Scanner {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
@ -1623,7 +1612,7 @@ public class Lexer extends Scanner {
|
||||
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:
|
||||
|
@ -209,26 +209,6 @@ public enum JSType {
|
||||
return (long)number == number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
|
32
nashorn/test/script/basic/JDK-8026161.js
Normal file
32
nashorn/test/script/basic/JDK-8026161.js
Normal file
@ -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]
|
2
nashorn/test/script/basic/JDK-8026161.js.EXPECTED
Normal file
2
nashorn/test/script/basic/JDK-8026161.js.EXPECTED
Normal file
@ -0,0 +1,2 @@
|
||||
java.awt.Color[r=1,g=1,b=1]
|
||||
java.awt.Color[r=255,g=255,b=255]
|
@ -412,7 +412,7 @@ public class MethodAccessTest {
|
||||
|
||||
@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 @@ public class MethodAccessTest {
|
||||
|
||||
@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);"));
|
||||
|
Loading…
x
Reference in New Issue
Block a user