mirror of
https://github.com/openjdk/jdk.git
synced 2026-05-11 05:59:52 +00:00
8060011: Concatenating an array and converting it to Java gives wrong result
Reviewed-by: lagergren, attila
This commit is contained in:
parent
f2913f0270
commit
4fcc6c8f4f
@ -66,9 +66,9 @@ final class DeletedRangeArrayFilter extends ArrayFilter {
|
||||
public Object[] asObjectArray() {
|
||||
final Object[] value = super.asObjectArray();
|
||||
|
||||
if (lo <= Integer.MAX_VALUE) {
|
||||
final int intHi = (int)Math.min(hi, Integer.MAX_VALUE);
|
||||
for (int i = (int)lo; i <= intHi; i++) {
|
||||
if (lo < Integer.MAX_VALUE) {
|
||||
final int end = (int)Math.min(hi + 1, Integer.MAX_VALUE);
|
||||
for (int i = (int)lo; i < end; i++) {
|
||||
value[i] = ScriptRuntime.UNDEFINED;
|
||||
}
|
||||
}
|
||||
@ -81,9 +81,9 @@ final class DeletedRangeArrayFilter extends ArrayFilter {
|
||||
final Object value = super.asArrayOfType(componentType);
|
||||
final Object undefValue = convertUndefinedValue(componentType);
|
||||
|
||||
if (lo <= Integer.MAX_VALUE) {
|
||||
final int intHi = (int)Math.min(hi, Integer.MAX_VALUE);
|
||||
for (int i = (int)lo; i <= intHi; i++) {
|
||||
if (lo < Integer.MAX_VALUE) {
|
||||
final int end = (int)Math.min(hi + 1, Integer.MAX_VALUE);
|
||||
for (int i = (int)lo; i < end; i++) {
|
||||
Array.set(value, i, undefValue);
|
||||
}
|
||||
}
|
||||
|
||||
@ -73,7 +73,7 @@ final class IntArrayData extends ContinuousArrayData implements IntElements {
|
||||
|
||||
@Override
|
||||
public Object[] asObjectArray() {
|
||||
return toObjectArray();
|
||||
return toObjectArray(true);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@ -116,9 +116,9 @@ final class IntArrayData extends ContinuousArrayData implements IntElements {
|
||||
return super.asArrayOfType(componentType);
|
||||
}
|
||||
|
||||
private Object[] toObjectArray() {
|
||||
private Object[] toObjectArray(final boolean trim) {
|
||||
assert length <= array.length : "length exceeds internal array size";
|
||||
final Object[] oarray = new Object[array.length];
|
||||
final Object[] oarray = new Object[trim ? (int)length : array.length];
|
||||
|
||||
for (int index = 0; index < length; index++) {
|
||||
oarray[index] = Integer.valueOf(array[index]);
|
||||
@ -158,7 +158,7 @@ final class IntArrayData extends ContinuousArrayData implements IntElements {
|
||||
}
|
||||
|
||||
private ObjectArrayData convertToObject() {
|
||||
return new ObjectArrayData(toObjectArray(), (int)length);
|
||||
return new ObjectArrayData(toObjectArray(false), (int)length);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -67,12 +67,12 @@ final class LongArrayData extends ContinuousArrayData implements IntOrLongElemen
|
||||
|
||||
@Override
|
||||
public Object[] asObjectArray() {
|
||||
return toObjectArray(array, (int)length);
|
||||
return toObjectArray(true);
|
||||
}
|
||||
|
||||
private static Object[] toObjectArray(final long[] array, final int length) {
|
||||
private Object[] toObjectArray(final boolean trim) {
|
||||
assert length <= array.length : "length exceeds internal array size";
|
||||
final Object[] oarray = new Object[array.length];
|
||||
final Object[] oarray = new Object[trim ? (int)length : array.length];
|
||||
|
||||
for (int index = 0; index < length; index++) {
|
||||
oarray[index] = Long.valueOf(array[index]);
|
||||
@ -89,7 +89,7 @@ final class LongArrayData extends ContinuousArrayData implements IntOrLongElemen
|
||||
return super.asArrayOfType(componentType);
|
||||
}
|
||||
|
||||
private static double[] toDoubleArray(final long[] array, final int length) {
|
||||
private double[] toDoubleArray() {
|
||||
assert length <= array.length : "length exceeds internal array size";
|
||||
final double[] darray = new double[array.length];
|
||||
|
||||
@ -107,9 +107,9 @@ final class LongArrayData extends ContinuousArrayData implements IntOrLongElemen
|
||||
}
|
||||
final int len = (int)length;
|
||||
if (type == Double.class) {
|
||||
return new NumberArrayData(LongArrayData.toDoubleArray(array, len), len);
|
||||
return new NumberArrayData(toDoubleArray(), len);
|
||||
}
|
||||
return new ObjectArrayData(LongArrayData.toObjectArray(array, len), len);
|
||||
return new ObjectArrayData(toObjectArray(false), len);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -66,12 +66,12 @@ final class NumberArrayData extends ContinuousArrayData implements NumericElemen
|
||||
|
||||
@Override
|
||||
public Object[] asObjectArray() {
|
||||
return toObjectArray(array, (int)length);
|
||||
return toObjectArray(true);
|
||||
}
|
||||
|
||||
private static Object[] toObjectArray(final double[] array, final int length) {
|
||||
private Object[] toObjectArray(final boolean trim) {
|
||||
assert length <= array.length : "length exceeds internal array size";
|
||||
final Object[] oarray = new Object[array.length];
|
||||
final Object[] oarray = new Object[trim ? (int)length : array.length];
|
||||
|
||||
for (int index = 0; index < length; index++) {
|
||||
oarray[index] = Double.valueOf(array[index]);
|
||||
@ -91,7 +91,7 @@ final class NumberArrayData extends ContinuousArrayData implements NumericElemen
|
||||
public ArrayData convert(final Class<?> type) {
|
||||
if (type != Double.class && type != Integer.class && type != Long.class) {
|
||||
final int len = (int)length;
|
||||
return new ObjectArrayData(NumberArrayData.toObjectArray(array, len), len);
|
||||
return new ObjectArrayData(toObjectArray(false), len);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -155,7 +155,8 @@ final class ObjectArrayData extends ContinuousArrayData {
|
||||
|
||||
@Override
|
||||
public ArrayData setEmpty(final long lo, final long hi) {
|
||||
Arrays.fill(array, (int)Math.max(lo, 0L), (int)Math.min(hi, Integer.MAX_VALUE), ScriptRuntime.EMPTY);
|
||||
// hi parameter is inclusive, but Arrays.fill toIndex parameter is exclusive
|
||||
Arrays.fill(array, (int)Math.max(lo, 0L), (int)Math.min(hi + 1, Integer.MAX_VALUE), ScriptRuntime.EMPTY);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@ -78,7 +78,7 @@ class SparseArrayData extends ArrayData {
|
||||
|
||||
for (final Map.Entry<Long, Object> entry : sparseMap.entrySet()) {
|
||||
final long key = entry.getKey();
|
||||
if (key <= Integer.MAX_VALUE) {
|
||||
if (key < Integer.MAX_VALUE) {
|
||||
objArray[(int)key] = entry.getValue();
|
||||
} else {
|
||||
break; // ascending key order
|
||||
|
||||
58
nashorn/test/script/basic/JDK-8060011.js
Normal file
58
nashorn/test/script/basic/JDK-8060011.js
Normal file
@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (c) 2014, 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-8060011: Concatenating an array and converting it to Java gives wrong result
|
||||
*
|
||||
* @test
|
||||
* @run
|
||||
*/
|
||||
|
||||
|
||||
function compareAsJavaArrays(a1, a2) {
|
||||
var ja1 = Java.to(a1);
|
||||
var ja2 = Java.to(a2);
|
||||
if (ja1.length !== ja2.length) {
|
||||
throw "different length";
|
||||
}
|
||||
for (var i = 0; i < ja1.length; i++) {
|
||||
if (ja1[i] !== ja2[i]) {
|
||||
throw "different element at " + i;
|
||||
}
|
||||
}
|
||||
if (java.util.Arrays.toString(ja1) !== java.util.Arrays.toString(ja2)) {
|
||||
throw "different string representation";
|
||||
}
|
||||
}
|
||||
|
||||
compareAsJavaArrays([0, 1, 2, 3],
|
||||
[0].concat([1, 2, 3]));
|
||||
compareAsJavaArrays([1000000000, 2000000000, 3000000000, 4000000000],
|
||||
[1000000000].concat([2000000000, 3000000000, 4000000000]));
|
||||
compareAsJavaArrays([0.5, 1.5, 2.5, 3.5],
|
||||
[0.5].concat([1.5, 2.5, 3.5]));
|
||||
compareAsJavaArrays(["0", "1", "2", "3"],
|
||||
["0"].concat(["1", "2", "3"]));
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user