Thu, 21 May 2015 21:51:48 +0530
8079145: jdk.nashorn.internal.runtime.arrays.IntArrayData.convert assertion
Reviewed-by: jlaskey, hannesw
1.1 --- a/src/jdk/nashorn/internal/runtime/arrays/IntArrayData.java Thu May 21 18:44:51 2015 +0530 1.2 +++ b/src/jdk/nashorn/internal/runtime/arrays/IntArrayData.java Thu May 21 21:51:48 2015 +0530 1.3 @@ -182,14 +182,13 @@ 1.4 1.5 @Override 1.6 public ArrayData convert(final Class<?> type) { 1.7 - if (type == Integer.class) { 1.8 + if (type == Integer.class || type == Byte.class || type == Short.class) { 1.9 return this; 1.10 } else if (type == Long.class) { 1.11 return convertToLong(); 1.12 - } else if (type == Double.class) { 1.13 + } else if (type == Double.class || type == Float.class) { 1.14 return convertToDouble(); 1.15 } else { 1.16 - assert type == null || (!Number.class.isAssignableFrom(type) && !type.isPrimitive()); 1.17 return convertToObject(); 1.18 } 1.19 }
2.1 --- a/src/jdk/nashorn/internal/runtime/arrays/LongArrayData.java Thu May 21 18:44:51 2015 +0530 2.2 +++ b/src/jdk/nashorn/internal/runtime/arrays/LongArrayData.java Thu May 21 21:51:48 2015 +0530 2.3 @@ -120,11 +120,11 @@ 2.4 2.5 @Override 2.6 public ContinuousArrayData convert(final Class<?> type) { 2.7 - if (type == Integer.class || type == Long.class) { 2.8 + if (type == Integer.class || type == Long.class || type == Byte.class || type == Short.class) { 2.9 return this; 2.10 } 2.11 final int len = (int)length(); 2.12 - if (type == Double.class) { 2.13 + if (type == Double.class || type == Float.class) { 2.14 return new NumberArrayData(toDoubleArray(), len); 2.15 } 2.16 return new ObjectArrayData(toObjectArray(false), len); 2.17 @@ -171,7 +171,8 @@ 2.18 2.19 @Override 2.20 public ArrayData set(final int index, final Object value, final boolean strict) { 2.21 - if (value instanceof Long || value instanceof Integer) { 2.22 + if (value instanceof Long || value instanceof Integer || 2.23 + value instanceof Byte || value instanceof Short) { 2.24 return set(index, ((Number)value).longValue(), strict); 2.25 } else if (value == ScriptRuntime.UNDEFINED) { 2.26 return new UndefinedArrayFilter(this).set(index, value, strict);
3.1 --- a/src/jdk/nashorn/internal/runtime/arrays/NumberArrayData.java Thu May 21 18:44:51 2015 +0530 3.2 +++ b/src/jdk/nashorn/internal/runtime/arrays/NumberArrayData.java Thu May 21 21:51:48 2015 +0530 3.3 @@ -29,6 +29,7 @@ 3.4 import static jdk.nashorn.internal.lookup.Lookup.MH; 3.5 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; 3.6 3.7 +import jdk.internal.dynalink.support.TypeUtilities; 3.8 import java.lang.invoke.MethodHandle; 3.9 import java.lang.invoke.MethodHandles; 3.10 import java.util.Arrays; 3.11 @@ -104,9 +105,14 @@ 3.12 return super.asArrayOfType(componentType); 3.13 } 3.14 3.15 + private static boolean canWiden(final Class<?> type) { 3.16 + return TypeUtilities.isWrapperType(type) && 3.17 + type != Boolean.class && type != Character.class; 3.18 + } 3.19 + 3.20 @Override 3.21 public ContinuousArrayData convert(final Class<?> type) { 3.22 - if (type != Double.class && type != Integer.class && type != Long.class) { 3.23 + if (! canWiden(type)) { 3.24 final int len = (int)length(); 3.25 return new ObjectArrayData(toObjectArray(false), len); 3.26 } 3.27 @@ -154,7 +160,7 @@ 3.28 3.29 @Override 3.30 public ArrayData set(final int index, final Object value, final boolean strict) { 3.31 - if (value instanceof Double || value instanceof Integer || value instanceof Long) { 3.32 + if (value instanceof Double || (value != null && canWiden(value.getClass()))) { 3.33 return set(index, ((Number)value).doubleValue(), strict); 3.34 } else if (value == UNDEFINED) { 3.35 return new UndefinedArrayFilter(this).set(index, value, strict);
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/script/basic/JDK-8079145.js Thu May 21 21:51:48 2015 +0530 4.3 @@ -0,0 +1,89 @@ 4.4 +/* 4.5 + * Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved. 4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.7 + * 4.8 + * This code is free software; you can redistribute it and/or modify it 4.9 + * under the terms of the GNU General Public License version 2 only, as 4.10 + * published by the Free Software Foundation. 4.11 + * 4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 4.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 4.15 + * version 2 for more details (a copy is included in the LICENSE file that 4.16 + * accompanied this code). 4.17 + * 4.18 + * You should have received a copy of the GNU General Public License version 4.19 + * 2 along with this work; if not, write to the Free Software Foundation, 4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 4.21 + * 4.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 4.23 + * or visit www.oracle.com if you need additional information or have any 4.24 + * questions. 4.25 + */ 4.26 + 4.27 +/** 4.28 + * JDK-8079145: jdk.nashorn.internal.runtime.arrays.IntArrayData.convert assertion 4.29 + * 4.30 + * @test 4.31 + * @fork 4.32 + * @option -Dnashorn.debug=true 4.33 + * @run 4.34 + */ 4.35 + 4.36 +var Byte = java.lang.Byte; 4.37 +var Short = java.lang.Short; 4.38 +var Integer = java.lang.Integer; 4.39 +var Long = java.lang.Long; 4.40 +var Float = java.lang.Float; 4.41 +var Double = java.lang.Double; 4.42 +var Character = java.lang.Character; 4.43 + 4.44 +function checkWiden(arr, value, name) { 4.45 + switch (typeof value) { 4.46 + case 'object': 4.47 + case 'undefined': 4.48 + print(name + ": check widen for " + value); 4.49 + break; 4.50 + default: 4.51 + print(name + ": check widen for " + value + 4.52 + " [" + Debug.getClass(value) + "]"); 4.53 + } 4.54 + 4.55 + arr[0] = value; 4.56 +} 4.57 + 4.58 +function checkIntWiden(value) { 4.59 + checkWiden([34], value, "int array"); 4.60 +} 4.61 + 4.62 +function checkLongWiden(value) { 4.63 + checkWiden([Integer.MAX_VALUE + 1], value, "long array"); 4.64 +} 4.65 + 4.66 +function checkNumberWiden(value) { 4.67 + checkWiden([Math.PI], value, "number array"); 4.68 +} 4.69 + 4.70 +function checkObjectWiden(value) { 4.71 + checkWiden([null], value, "object array"); 4.72 +} 4.73 + 4.74 +var values = [{}, null, undefined, false, true, new Byte(34), 4.75 + new Integer(344454), new Long(454545), new Long(Integer.MAX_VALUE + 1), 4.76 + new Float(34.3), new Double(Math.PI), new Character('s')]; 4.77 + 4.78 +for each (var v in values) { 4.79 + checkIntWiden(v); 4.80 +} 4.81 + 4.82 +for each (var v in values) { 4.83 + checkLongWiden(v); 4.84 +} 4.85 + 4.86 +for each (var v in values) { 4.87 + checkNumberWiden(v); 4.88 +} 4.89 + 4.90 +for each (var v in values) { 4.91 + checkObjectWiden(v); 4.92 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/test/script/basic/JDK-8079145.js.EXPECTED Thu May 21 21:51:48 2015 +0530 5.3 @@ -0,0 +1,48 @@ 5.4 +int array: check widen for [object Object] 5.5 +int array: check widen for null 5.6 +int array: check widen for undefined 5.7 +int array: check widen for false [class java.lang.Boolean] 5.8 +int array: check widen for true [class java.lang.Boolean] 5.9 +int array: check widen for 34 [class java.lang.Byte] 5.10 +int array: check widen for 344454 [class java.lang.Integer] 5.11 +int array: check widen for 454545 [class java.lang.Long] 5.12 +int array: check widen for 2147483648 [class java.lang.Long] 5.13 +int array: check widen for 34.29999923706055 [class java.lang.Float] 5.14 +int array: check widen for 3.141592653589793 [class java.lang.Double] 5.15 +int array: check widen for s 5.16 +long array: check widen for [object Object] 5.17 +long array: check widen for null 5.18 +long array: check widen for undefined 5.19 +long array: check widen for false [class java.lang.Boolean] 5.20 +long array: check widen for true [class java.lang.Boolean] 5.21 +long array: check widen for 34 [class java.lang.Byte] 5.22 +long array: check widen for 344454 [class java.lang.Integer] 5.23 +long array: check widen for 454545 [class java.lang.Long] 5.24 +long array: check widen for 2147483648 [class java.lang.Long] 5.25 +long array: check widen for 34.29999923706055 [class java.lang.Float] 5.26 +long array: check widen for 3.141592653589793 [class java.lang.Double] 5.27 +long array: check widen for s 5.28 +number array: check widen for [object Object] 5.29 +number array: check widen for null 5.30 +number array: check widen for undefined 5.31 +number array: check widen for false [class java.lang.Boolean] 5.32 +number array: check widen for true [class java.lang.Boolean] 5.33 +number array: check widen for 34 [class java.lang.Byte] 5.34 +number array: check widen for 344454 [class java.lang.Integer] 5.35 +number array: check widen for 454545 [class java.lang.Long] 5.36 +number array: check widen for 2147483648 [class java.lang.Long] 5.37 +number array: check widen for 34.29999923706055 [class java.lang.Float] 5.38 +number array: check widen for 3.141592653589793 [class java.lang.Double] 5.39 +number array: check widen for s 5.40 +object array: check widen for [object Object] 5.41 +object array: check widen for null 5.42 +object array: check widen for undefined 5.43 +object array: check widen for false [class java.lang.Boolean] 5.44 +object array: check widen for true [class java.lang.Boolean] 5.45 +object array: check widen for 34 [class java.lang.Byte] 5.46 +object array: check widen for 344454 [class java.lang.Integer] 5.47 +object array: check widen for 454545 [class java.lang.Long] 5.48 +object array: check widen for 2147483648 [class java.lang.Long] 5.49 +object array: check widen for 34.29999923706055 [class java.lang.Float] 5.50 +object array: check widen for 3.141592653589793 [class java.lang.Double] 5.51 +object array: check widen for s