8079145: jdk.nashorn.internal.runtime.arrays.IntArrayData.convert assertion
Reviewed-by: jlaskey, hannesw
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/arrays/IntArrayData.java Thu May 21 18:44:51 2015 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/arrays/IntArrayData.java Thu May 21 21:51:48 2015 +0530
@@ -182,14 +182,13 @@
@Override
public ArrayData convert(final Class<?> type) {
- if (type == Integer.class) {
+ if (type == Integer.class || type == Byte.class || type == Short.class) {
return this;
} else if (type == Long.class) {
return convertToLong();
- } else if (type == Double.class) {
+ } else if (type == Double.class || type == Float.class) {
return convertToDouble();
} else {
- assert type == null || (!Number.class.isAssignableFrom(type) && !type.isPrimitive());
return convertToObject();
}
}
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/arrays/LongArrayData.java Thu May 21 18:44:51 2015 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/arrays/LongArrayData.java Thu May 21 21:51:48 2015 +0530
@@ -120,11 +120,11 @@
@Override
public ContinuousArrayData convert(final Class<?> type) {
- if (type == Integer.class || type == Long.class) {
+ if (type == Integer.class || type == Long.class || type == Byte.class || type == Short.class) {
return this;
}
final int len = (int)length();
- if (type == Double.class) {
+ if (type == Double.class || type == Float.class) {
return new NumberArrayData(toDoubleArray(), len);
}
return new ObjectArrayData(toObjectArray(false), len);
@@ -171,7 +171,8 @@
@Override
public ArrayData set(final int index, final Object value, final boolean strict) {
- if (value instanceof Long || value instanceof Integer) {
+ if (value instanceof Long || value instanceof Integer ||
+ value instanceof Byte || value instanceof Short) {
return set(index, ((Number)value).longValue(), strict);
} else if (value == ScriptRuntime.UNDEFINED) {
return new UndefinedArrayFilter(this).set(index, value, strict);
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/arrays/NumberArrayData.java Thu May 21 18:44:51 2015 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/arrays/NumberArrayData.java Thu May 21 21:51:48 2015 +0530
@@ -29,6 +29,7 @@
import static jdk.nashorn.internal.lookup.Lookup.MH;
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
+import jdk.internal.dynalink.support.TypeUtilities;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.util.Arrays;
@@ -104,9 +105,14 @@
return super.asArrayOfType(componentType);
}
+ private static boolean canWiden(final Class<?> type) {
+ return TypeUtilities.isWrapperType(type) &&
+ type != Boolean.class && type != Character.class;
+ }
+
@Override
public ContinuousArrayData convert(final Class<?> type) {
- if (type != Double.class && type != Integer.class && type != Long.class) {
+ if (! canWiden(type)) {
final int len = (int)length();
return new ObjectArrayData(toObjectArray(false), len);
}
@@ -154,7 +160,7 @@
@Override
public ArrayData set(final int index, final Object value, final boolean strict) {
- if (value instanceof Double || value instanceof Integer || value instanceof Long) {
+ if (value instanceof Double || (value != null && canWiden(value.getClass()))) {
return set(index, ((Number)value).doubleValue(), strict);
} else if (value == UNDEFINED) {
return new UndefinedArrayFilter(this).set(index, value, strict);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8079145.js Thu May 21 21:51:48 2015 +0530
@@ -0,0 +1,89 @@
+/*
+ * 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-8079145: jdk.nashorn.internal.runtime.arrays.IntArrayData.convert assertion
+ *
+ * @test
+ * @fork
+ * @option -Dnashorn.debug=true
+ * @run
+ */
+
+var Byte = java.lang.Byte;
+var Short = java.lang.Short;
+var Integer = java.lang.Integer;
+var Long = java.lang.Long;
+var Float = java.lang.Float;
+var Double = java.lang.Double;
+var Character = java.lang.Character;
+
+function checkWiden(arr, value, name) {
+ switch (typeof value) {
+ case 'object':
+ case 'undefined':
+ print(name + ": check widen for " + value);
+ break;
+ default:
+ print(name + ": check widen for " + value +
+ " [" + Debug.getClass(value) + "]");
+ }
+
+ arr[0] = value;
+}
+
+function checkIntWiden(value) {
+ checkWiden([34], value, "int array");
+}
+
+function checkLongWiden(value) {
+ checkWiden([Integer.MAX_VALUE + 1], value, "long array");
+}
+
+function checkNumberWiden(value) {
+ checkWiden([Math.PI], value, "number array");
+}
+
+function checkObjectWiden(value) {
+ checkWiden([null], value, "object array");
+}
+
+var values = [{}, null, undefined, false, true, new Byte(34),
+ new Integer(344454), new Long(454545), new Long(Integer.MAX_VALUE + 1),
+ new Float(34.3), new Double(Math.PI), new Character('s')];
+
+for each (var v in values) {
+ checkIntWiden(v);
+}
+
+for each (var v in values) {
+ checkLongWiden(v);
+}
+
+for each (var v in values) {
+ checkNumberWiden(v);
+}
+
+for each (var v in values) {
+ checkObjectWiden(v);
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8079145.js.EXPECTED Thu May 21 21:51:48 2015 +0530
@@ -0,0 +1,48 @@
+int array: check widen for [object Object]
+int array: check widen for null
+int array: check widen for undefined
+int array: check widen for false [class java.lang.Boolean]
+int array: check widen for true [class java.lang.Boolean]
+int array: check widen for 34 [class java.lang.Byte]
+int array: check widen for 344454 [class java.lang.Integer]
+int array: check widen for 454545 [class java.lang.Long]
+int array: check widen for 2147483648 [class java.lang.Long]
+int array: check widen for 34.29999923706055 [class java.lang.Float]
+int array: check widen for 3.141592653589793 [class java.lang.Double]
+int array: check widen for s
+long array: check widen for [object Object]
+long array: check widen for null
+long array: check widen for undefined
+long array: check widen for false [class java.lang.Boolean]
+long array: check widen for true [class java.lang.Boolean]
+long array: check widen for 34 [class java.lang.Byte]
+long array: check widen for 344454 [class java.lang.Integer]
+long array: check widen for 454545 [class java.lang.Long]
+long array: check widen for 2147483648 [class java.lang.Long]
+long array: check widen for 34.29999923706055 [class java.lang.Float]
+long array: check widen for 3.141592653589793 [class java.lang.Double]
+long array: check widen for s
+number array: check widen for [object Object]
+number array: check widen for null
+number array: check widen for undefined
+number array: check widen for false [class java.lang.Boolean]
+number array: check widen for true [class java.lang.Boolean]
+number array: check widen for 34 [class java.lang.Byte]
+number array: check widen for 344454 [class java.lang.Integer]
+number array: check widen for 454545 [class java.lang.Long]
+number array: check widen for 2147483648 [class java.lang.Long]
+number array: check widen for 34.29999923706055 [class java.lang.Float]
+number array: check widen for 3.141592653589793 [class java.lang.Double]
+number array: check widen for s
+object array: check widen for [object Object]
+object array: check widen for null
+object array: check widen for undefined
+object array: check widen for false [class java.lang.Boolean]
+object array: check widen for true [class java.lang.Boolean]
+object array: check widen for 34 [class java.lang.Byte]
+object array: check widen for 344454 [class java.lang.Integer]
+object array: check widen for 454545 [class java.lang.Long]
+object array: check widen for 2147483648 [class java.lang.Long]
+object array: check widen for 34.29999923706055 [class java.lang.Float]
+object array: check widen for 3.141592653589793 [class java.lang.Double]
+object array: check widen for s