8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
authorhannesw
Wed, 24 Apr 2013 13:28:25 +0200
changeset 17241 c337fefb8c84
parent 17240 5e9ad5f6bb6e
child 17242 476e2a192a1e
8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec Reviewed-by: lagergren, attila
nashorn/src/jdk/nashorn/internal/codegen/CodeGenerator.java
nashorn/src/jdk/nashorn/internal/codegen/FoldConstants.java
nashorn/src/jdk/nashorn/internal/objects/NativeArray.java
nashorn/src/jdk/nashorn/internal/objects/NativeUint32Array.java
nashorn/src/jdk/nashorn/internal/runtime/JSType.java
nashorn/src/jdk/nashorn/internal/runtime/ScriptObject.java
nashorn/src/jdk/nashorn/internal/runtime/arrays/SparseArrayData.java
nashorn/test/examples/int-micro.js
nashorn/test/script/basic/JDK-8012334.js
nashorn/test/script/basic/JDK-8012334.js.EXPECTED
nashorn/test/src/jdk/nashorn/internal/runtime/JSTypeTest.java
--- a/nashorn/src/jdk/nashorn/internal/codegen/CodeGenerator.java	Tue Apr 23 16:48:57 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/codegen/CodeGenerator.java	Wed Apr 24 13:28:25 2013 +0200
@@ -117,6 +117,7 @@
 import jdk.nashorn.internal.runtime.Debug;
 import jdk.nashorn.internal.runtime.DebugLogger;
 import jdk.nashorn.internal.runtime.ECMAException;
+import jdk.nashorn.internal.runtime.JSType;
 import jdk.nashorn.internal.runtime.Property;
 import jdk.nashorn.internal.runtime.PropertyMap;
 import jdk.nashorn.internal.runtime.RecompilableScriptFunctionData;
@@ -2572,7 +2573,7 @@
             @Override
             protected void op() {
                 method.shr();
-                method.convert(Type.LONG).load(0xffff_ffffL).and();
+                method.convert(Type.LONG).load(JSType.MAX_UINT).and();
             }
         }.store();
 
@@ -2807,7 +2808,7 @@
             @Override
             protected void op() {
                 method.shr();
-                method.convert(Type.LONG).load(0xffff_ffffL).and();
+                method.convert(Type.LONG).load(JSType.MAX_UINT).and();
             }
         }.evaluate(binaryNode);
 
--- a/nashorn/src/jdk/nashorn/internal/codegen/FoldConstants.java	Tue Apr 23 16:48:57 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/codegen/FoldConstants.java	Wed Apr 24 13:28:25 2013 +0200
@@ -247,7 +247,7 @@
                 value = lhs.getNumber() - rhs.getNumber();
                 break;
             case SHR:
-                return LiteralNode.newInstance(source, token, finish, (lhs.getInt32() >>> rhs.getInt32()) & 0xffff_ffffL);
+                return LiteralNode.newInstance(source, token, finish, (lhs.getInt32() >>> rhs.getInt32()) & JSType.MAX_UINT);
             case SAR:
                 return LiteralNode.newInstance(source, token, finish, lhs.getInt32() >> rhs.getInt32());
             case SHL:
--- a/nashorn/src/jdk/nashorn/internal/objects/NativeArray.java	Tue Apr 23 16:48:57 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/objects/NativeArray.java	Wed Apr 24 13:28:25 2013 +0200
@@ -418,7 +418,7 @@
                 long length;
                 if (len instanceof Integer || len instanceof Long) {
                     length = ((Number) len).longValue();
-                    if (length >= 0 && length < 0xffff_ffffL) {
+                    if (length >= 0 && length < JSType.MAX_UINT) {
                         return new NativeArray(length);
                     }
                 }
--- a/nashorn/src/jdk/nashorn/internal/objects/NativeUint32Array.java	Tue Apr 23 16:48:57 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/objects/NativeUint32Array.java	Wed Apr 24 13:28:25 2013 +0200
@@ -29,6 +29,7 @@
 import jdk.nashorn.internal.objects.annotations.Constructor;
 import jdk.nashorn.internal.objects.annotations.Function;
 import jdk.nashorn.internal.objects.annotations.ScriptClass;
+import jdk.nashorn.internal.runtime.JSType;
 import jdk.nashorn.internal.runtime.ScriptObject;
 import jdk.nashorn.internal.runtime.arrays.ArrayData;
 
@@ -71,17 +72,17 @@
 
         @Override
         protected long getLongImpl(final int key) {
-            return getIntImpl(key) & 0xffff_ffffL;
+            return getIntImpl(key) & JSType.MAX_UINT;
         }
 
         @Override
         protected double getDoubleImpl(final int key) {
-            return getIntImpl(key) & 0xffff_ffffL;
+            return getIntImpl(key) & JSType.MAX_UINT;
         }
 
         @Override
         protected Object getObjectImpl(final int key) {
-            return getIntImpl(key) & 0xffff_ffffL;
+            return getIntImpl(key) & JSType.MAX_UINT;
         }
 
         @Override
--- a/nashorn/src/jdk/nashorn/internal/runtime/JSType.java	Tue Apr 23 16:48:57 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/JSType.java	Wed Apr 24 13:28:25 2013 +0200
@@ -102,6 +102,8 @@
     /** JavaScript compliant conversion function from Object to primitive */
     public static final Call TO_PRIMITIVE = staticCall(JSType.class, "toPrimitive", Object.class,  Object.class);
 
+    private static final double INT32_LIMIT = 4294967296.0;
+
     /**
      * The external type name as returned by ECMAScript "typeof" operator
      *
@@ -612,10 +614,7 @@
      * @return an int32
      */
     public static int toInt32(final double num) {
-        if (Double.isInfinite(num)) {
-            return 0;
-        }
-        return (int)(long)num;
+        return (int)doubleToInt32(num);
     }
 
     /**
@@ -658,10 +657,7 @@
      * @return a uint32
      */
     public static long toUint32(final double num) {
-        if (Double.isInfinite(num)) {
-            return 0L;
-        }
-        return ((long)num) & 0xffff_ffffL;
+        return doubleToInt32(num) & MAX_UINT;
     }
 
     /**
@@ -702,10 +698,22 @@
      * @return a uint16
      */
     public static int toUint16(final double num) {
-        if (Double.isInfinite(num)) {
+        return ((int)doubleToInt32(num)) & 0xffff;
+    }
+
+    private static long doubleToInt32(final double num) {
+        final int exponent = Math.getExponent(num);
+        if (exponent < 31) {
+            return (long) num;  // Fits into 32 bits
+        }
+        if (exponent >= 84) {
+            // Either infinite or NaN or so large that shift / modulo will produce 0
+            // (52 bit mantissa + 32 bit target width).
             return 0;
         }
-        return ((int)(long)num) & 0xffff;
+        // This is rather slow and could probably be sped up using bit-fiddling.
+        final double d = (num >= 0) ? Math.floor(num) : Math.ceil(num);
+        return (long)(d % INT32_LIMIT);
     }
 
     /**
--- a/nashorn/src/jdk/nashorn/internal/runtime/ScriptObject.java	Tue Apr 23 16:48:57 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/ScriptObject.java	Wed Apr 24 13:28:25 2013 +0200
@@ -2425,7 +2425,7 @@
      */
     private void doesNotHave(final int index, final Object value, final boolean strict) {
         final long oldLength = getArray().length();
-        final long longIndex = index & 0xffff_ffffL;
+        final long longIndex = index & JSType.MAX_UINT;
 
         if (!getArray().has(index)) {
             final String key = convertKey(longIndex);
--- a/nashorn/src/jdk/nashorn/internal/runtime/arrays/SparseArrayData.java	Tue Apr 23 16:48:57 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/arrays/SparseArrayData.java	Wed Apr 24 13:28:25 2013 +0200
@@ -273,7 +273,7 @@
     }
 
     private static Long indexToKey(final int index) {
-        return Long.valueOf(index & 0xffff_ffffL);
+        return Long.valueOf(index & JSType.MAX_UINT);
     }
 
     @Override
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/examples/int-micro.js	Wed Apr 24 13:28:25 2013 +0200
@@ -0,0 +1,107 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ * 
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ * 
+ *   - Neither the name of Oracle nor the names of its
+ *     contributors may be used to endorse or promote products derived
+ *     from this software without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+
+function bench(name, func) {
+    var start = Date.now();
+    for (var iter = 0; iter < 5000000; iter++) {
+        func();
+    }
+    print(name + "\t" + (Date.now() - start));
+}
+
+function uint32(value) {
+    return function() {
+        value >>> 0;
+        value >>> 0;
+        value >>> 0;
+        value >>> 0;
+        value >>> 0;
+        value >>> 0;
+        value >>> 0;
+        value >>> 0;
+        value >>> 0;
+        value >>> 0;
+        value >>> 0;
+        value >>> 0;
+        value >>> 0;
+        value >>> 0;
+        value >>> 0;
+    };
+}
+
+function int32(value) {
+    return function() {
+        value >> 0;
+        value >> 0;
+        value >> 0;
+        value >> 0;
+        value >> 0;
+        value >> 0;
+        value >> 0;
+        value >> 0;
+        value >> 0;
+        value >> 0;
+        value >> 0;
+        value >> 0;
+        value >> 0;
+        value >> 0;
+        value >> 0;
+    };
+}
+
+print("\nToUint32");
+for (var i = 1; i < 3; i++) {
+    bench("infinity      ", uint32(Infinity));
+    bench("infinity neg  ", uint32(-Infinity));
+    bench("nan           ", uint32(NaN));
+    bench("small         ", uint32(1));
+    bench("small neg     ", uint32(-1));
+    bench("small frac    ", uint32(1.5));
+    bench("small neg frac", uint32(-1.5));
+    bench("large         ", uint32(9223372036854775807));
+    bench("large neg     ", uint32(-9223372036854775808));
+}
+
+print("\nToInt32");
+for (var i = 1; i < 3; i++) {
+    bench("infinity      ", int32(Infinity));
+    bench("infinity neg  ", int32(-Infinity));
+    bench("nan           ", int32(NaN));
+    bench("small         ", int32(1));
+    bench("small neg     ", int32(-1));
+    bench("small frac    ", int32(1.5));
+    bench("small neg frac", int32(-1.5));
+    bench("large         ", int32(9223372036854775807));
+    bench("large neg     ", int32(-9223372036854775808));
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8012334.js	Wed Apr 24 13:28:25 2013 +0200
@@ -0,0 +1,80 @@
+/*
+ * 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-8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
+ *
+ * @test
+ * @run
+ */
+
+
+function test(val) {
+    print(val | 0);
+    print(val >> 0);
+    print(val >>> 0);
+    print(1 >>> val);
+    print(parseInt("10", val));
+}
+
+test(0);
+test(-0);
+test('Infinity');
+test('+Infinity');
+test('-Infinity');
+test(Number.POSITIVE_INFINITY);
+test(Number.NEGATIVE_INFINITY);
+test(Number.NaN);
+test(Number.MIN_VALUE);
+test(-Number.MIN_VALUE);
+test(1);
+test(-1);
+test(0.1);
+test(-0.1);
+test(1.1);
+test(-1.1);
+test(9223372036854775807);
+test(-9223372036854775808);
+test('9223372036854775807');
+test('-9223372036854775808');
+test(2147483647);
+test(2147483648);
+test(2147483649);
+test(-2147483647);
+test(-2147483648);
+test(-2147483649);
+test(4294967295);
+test(4294967296);
+test(4294967297);
+test(-4294967295);
+test(-4294967296);
+test(-4294967297);
+test(1e23);
+test(-1e23);
+test(1e24);
+test(-1e24);
+test(1e25);
+test(-1e25);
+test(1e26);
+test(-1e26);
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8012334.js.EXPECTED	Wed Apr 24 13:28:25 2013 +0200
@@ -0,0 +1,200 @@
+0
+0
+0
+1
+10
+0
+0
+0
+1
+10
+0
+0
+0
+1
+10
+0
+0
+0
+1
+10
+0
+0
+0
+1
+10
+0
+0
+0
+1
+10
+0
+0
+0
+1
+10
+0
+0
+0
+1
+10
+0
+0
+0
+1
+10
+0
+0
+0
+1
+10
+1
+1
+1
+0
+NaN
+-1
+-1
+4294967295
+0
+NaN
+0
+0
+0
+1
+10
+0
+0
+0
+1
+10
+1
+1
+1
+0
+NaN
+-1
+-1
+4294967295
+0
+NaN
+0
+0
+0
+1
+10
+0
+0
+0
+1
+10
+0
+0
+0
+1
+10
+0
+0
+0
+1
+10
+2147483647
+2147483647
+2147483647
+0
+NaN
+-2147483648
+-2147483648
+2147483648
+1
+NaN
+-2147483647
+-2147483647
+2147483649
+0
+NaN
+-2147483647
+-2147483647
+2147483649
+0
+NaN
+-2147483648
+-2147483648
+2147483648
+1
+NaN
+2147483647
+2147483647
+2147483647
+0
+NaN
+-1
+-1
+4294967295
+0
+NaN
+0
+0
+0
+1
+10
+1
+1
+1
+0
+NaN
+1
+1
+1
+0
+NaN
+0
+0
+0
+1
+10
+-1
+-1
+4294967295
+0
+NaN
+-167772160
+-167772160
+4127195136
+1
+NaN
+167772160
+167772160
+167772160
+1
+NaN
+-1610612736
+-1610612736
+2684354560
+1
+NaN
+1610612736
+1610612736
+1610612736
+1
+NaN
+-2147483648
+-2147483648
+2147483648
+1
+NaN
+-2147483648
+-2147483648
+2147483648
+1
+NaN
+0
+0
+0
+1
+10
+0
+0
+0
+1
+10
--- a/nashorn/test/src/jdk/nashorn/internal/runtime/JSTypeTest.java	Tue Apr 23 16:48:57 2013 +0200
+++ b/nashorn/test/src/jdk/nashorn/internal/runtime/JSTypeTest.java	Wed Apr 24 13:28:25 2013 +0200
@@ -105,4 +105,89 @@
         // FIXME: add more number-to-string test cases
         // FIXME: add case for Object type (JSObject with getDefaultValue)
     }
+
+    /**
+     * Test of JSType.toUint32(double)
+     */
+    @Test
+    public void testToUint32() {
+        assertEquals(JSType.toUint32(+0.0), 0);
+        assertEquals(JSType.toUint32(-0.0), 0);
+        assertEquals(JSType.toUint32(Double.NaN), 0);
+        assertEquals(JSType.toUint32(Double.POSITIVE_INFINITY), 0);
+        assertEquals(JSType.toUint32(Double.NEGATIVE_INFINITY), 0);
+        assertEquals(JSType.toUint32(9223372036854775807.0d), 0);
+        assertEquals(JSType.toUint32(-9223372036854775807.0d), 0);
+        assertEquals(JSType.toUint32(1099511627776.0d), 0);
+        assertEquals(JSType.toUint32(-1099511627776.0d), 0);
+        assertEquals(JSType.toUint32(4294967295.0d), 4294967295l);
+        assertEquals(JSType.toUint32(4294967296.0d), 0);
+        assertEquals(JSType.toUint32(4294967297.0d), 1);
+        assertEquals(JSType.toUint32(-4294967295.0d), 1);
+        assertEquals(JSType.toUint32(-4294967296.0d), 0);
+        assertEquals(JSType.toUint32(-4294967297.0d), 4294967295l);
+        assertEquals(JSType.toUint32(4294967295.6d), 4294967295l);
+        assertEquals(JSType.toUint32(4294967296.6d), 0);
+        assertEquals(JSType.toUint32(4294967297.6d), 1);
+        assertEquals(JSType.toUint32(-4294967295.6d), 1);
+        assertEquals(JSType.toUint32(-4294967296.6d), 0);
+        assertEquals(JSType.toUint32(-4294967297.6d), 4294967295l);
+    }
+
+    /**
+     * Test of JSType.toInt32(double)
+     */
+    @Test
+    public void testToInt32() {
+        assertEquals(JSType.toInt32(+0.0), 0);
+        assertEquals(JSType.toInt32(-0.0), 0);
+        assertEquals(JSType.toInt32(Double.NaN), 0);
+        assertEquals(JSType.toInt32(Double.POSITIVE_INFINITY), 0);
+        assertEquals(JSType.toInt32(Double.NEGATIVE_INFINITY), 0);
+        assertEquals(JSType.toInt32(9223372036854775807.0d), 0);
+        assertEquals(JSType.toInt32(-9223372036854775807.0d), 0);
+        assertEquals(JSType.toInt32(1099511627776.0d), 0);
+        assertEquals(JSType.toInt32(-1099511627776.0d), 0);
+        assertEquals(JSType.toInt32(4294967295.0d), -1);
+        assertEquals(JSType.toInt32(4294967296.0d), 0);
+        assertEquals(JSType.toInt32(4294967297.0d), 1);
+        assertEquals(JSType.toInt32(-4294967295.0d), 1);
+        assertEquals(JSType.toInt32(-4294967296.0d), 0);
+        assertEquals(JSType.toInt32(-4294967297.d), -1);
+        assertEquals(JSType.toInt32(4294967295.6d), -1);
+        assertEquals(JSType.toInt32(4294967296.6d), 0);
+        assertEquals(JSType.toInt32(4294967297.6d), 1);
+        assertEquals(JSType.toInt32(-4294967295.6d), 1);
+        assertEquals(JSType.toInt32(-4294967296.6d), 0);
+        assertEquals(JSType.toInt32(-4294967297.6d), -1);
+    }
+
+    /**
+     * Test of JSType.toUint16(double)
+     */
+    @Test
+    public void testToUint16() {
+        assertEquals(JSType.toUint16(+0.0), 0);
+        assertEquals(JSType.toUint16(-0.0), 0);
+        assertEquals(JSType.toUint16(Double.NaN), 0);
+        assertEquals(JSType.toUint16(Double.POSITIVE_INFINITY), 0);
+        assertEquals(JSType.toUint16(Double.NEGATIVE_INFINITY), 0);
+        assertEquals(JSType.toUint16(9223372036854775807.0d), 0);
+        assertEquals(JSType.toUint16(-9223372036854775807.0d), 0);
+        assertEquals(JSType.toUint16(1099511627776.0d), 0);
+        assertEquals(JSType.toUint16(-1099511627776.0d), 0);
+        assertEquals(JSType.toUint16(4294967295.0d), 65535);
+        assertEquals(JSType.toUint16(4294967296.0d), 0);
+        assertEquals(JSType.toUint16(4294967297.0d), 1);
+        assertEquals(JSType.toUint16(-4294967295.0d), 1);
+        assertEquals(JSType.toUint16(-4294967296.0d), 0);
+        assertEquals(JSType.toUint16(-4294967297.0d), 65535);
+        assertEquals(JSType.toUint16(4294967295.6d), 65535);
+        assertEquals(JSType.toUint16(4294967296.6d), 0);
+        assertEquals(JSType.toUint16(4294967297.6d), 1);
+        assertEquals(JSType.toUint16(-4294967295.6d), 1);
+        assertEquals(JSType.toUint16(-4294967296.6d), 0);
+        assertEquals(JSType.toUint16(-4294967297.6d), 65535);
+    }
+
 }