8144131: ArrayData.getInt implementations do not convert to int32
authorhannesw
Fri, 27 Nov 2015 08:44:02 +0100
changeset 34454 735fec4e8ee8
parent 34453 0d6925c03155
child 34455 cc9f05d3caf0
8144131: ArrayData.getInt implementations do not convert to int32 Reviewed-by: lagergren, sundar
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/FoldConstants.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/arrays/LongArrayData.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/arrays/NumberArrayData.java
nashorn/test/script/basic/JDK-8144131.js
nashorn/test/script/basic/minuszero.js
nashorn/test/script/basic/minuszero.js.EXPECTED
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/FoldConstants.java	Mon Nov 30 13:26:07 2015 -0800
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/FoldConstants.java	Fri Nov 27 08:44:02 2015 +0100
@@ -234,6 +234,8 @@
             case ADD:
                 if (rhsInteger) {
                     literalNode = LiteralNode.newInstance(token, finish, rhs.getInt32());
+                } else if (rhsType.isLong()) {
+                    literalNode = LiteralNode.newInstance(token, finish, rhs.getLong());
                 } else {
                     literalNode = LiteralNode.newInstance(token, finish, rhs.getNumber());
                 }
@@ -241,6 +243,8 @@
             case SUB:
                 if (rhsInteger && rhs.getInt32() != 0) { // @see test/script/basic/minuszero.js
                     literalNode = LiteralNode.newInstance(token, finish, -rhs.getInt32());
+                } else if (rhsType.isLong() && rhs.getLong() != 0L) {
+                    literalNode = LiteralNode.newInstance(token, finish, -rhs.getLong());
                 } else {
                     literalNode = LiteralNode.newInstance(token, finish, -rhs.getNumber());
                 }
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/arrays/LongArrayData.java	Mon Nov 30 13:26:07 2015 -0800
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/arrays/LongArrayData.java	Fri Nov 27 08:44:02 2015 +0100
@@ -241,7 +241,7 @@
 
     @Override
     public int getInt(final int index) {
-        return (int)array[index];
+        return JSType.toInt32(array[index]);
     }
 
     @Override
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/arrays/NumberArrayData.java	Mon Nov 30 13:26:07 2015 -0800
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/arrays/NumberArrayData.java	Fri Nov 27 08:44:02 2015 +0100
@@ -33,6 +33,7 @@
 import java.lang.invoke.MethodHandles;
 import java.util.Arrays;
 import jdk.dynalink.linker.support.TypeUtilities;
+import jdk.nashorn.internal.runtime.JSType;
 
 /**
  * Implementation of {@link ArrayData} as soon as a double has been
@@ -226,7 +227,7 @@
 
     @Override
     public int getInt(final int index) {
-        return (int)array[index];
+        return JSType.toInt32(array[index]);
     }
 
     @Override
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8144131.js	Fri Nov 27 08:44:02 2015 +0100
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2015, 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-8144131: ArrayData.getInt implementations do not convert to int32
+ *
+ * @test
+ * @run
+ */
+
+var doubleArray = [97912312397.234, -182374983434.56];
+var doubleArrayResults = [-871935411, -1986357002];
+var longArray = [0x7fffffff8c102ebc, -0x7fffffffe9dfec18];
+var longArrayResults = [-1945096192, 371201024];
+
+// Make sure arrays use double and long array data
+Assert.assertEquals(doubleArray[0].getClass(), java.lang.Double.class);
+Assert.assertEquals(longArray[0].getClass(), java.lang.Long.class);
+
+function testBinaryOp(array, index, expected) {
+    Assert.assertEquals(array[index] & 0xffffffff, expected);
+}
+
+for (var i = 0; i < doubleArray.length; i++) {
+    testBinaryOp(doubleArray, i, doubleArrayResults[i]);
+}
+for (var i = 0; i < longArray.length; i++) {
+    testBinaryOp(longArray, i, longArrayResults[i]);
+}
+
--- a/nashorn/test/script/basic/minuszero.js	Mon Nov 30 13:26:07 2015 -0800
+++ b/nashorn/test/script/basic/minuszero.js	Fri Nov 27 08:44:02 2015 +0100
@@ -39,3 +39,7 @@
 print(obj.length === -0);
 print(1/obj.length);
 
+var mzl = -(0x7fffffffffffffff - 0x7fffffffffffffff);
+print(mzl);
+print(mzl === -0);
+print(1/mzl);
\ No newline at end of file
--- a/nashorn/test/script/basic/minuszero.js.EXPECTED	Mon Nov 30 13:26:07 2015 -0800
+++ b/nashorn/test/script/basic/minuszero.js.EXPECTED	Fri Nov 27 08:44:02 2015 +0100
@@ -4,3 +4,6 @@
 0
 true
 -Infinity
+0
+true
+-Infinity