aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhannesw <none@none>2013-07-15 16:35:38 +0200
committerhannesw <none@none>2013-07-15 16:35:38 +0200
commitd83551540bbea3857bf2125db88b226f3b60424e (patch)
tree848b3fd9eeb0b320c0ba9d7268169e28a85cd369
parentc918bb3473c65462a0d7c8ade9fd16fac8d15b0e (diff)
downloadnashorn-d83551540bbea3857bf2125db88b226f3b60424e.tar.gz
8020283: Don't use exceptions for widening of ArrayData
Reviewed-by: jlaskey, attila
-rw-r--r--src/jdk/nashorn/internal/runtime/arrays/IntArrayData.java47
-rw-r--r--src/jdk/nashorn/internal/runtime/arrays/LongArrayData.java32
-rw-r--r--src/jdk/nashorn/internal/runtime/arrays/NumberArrayData.java21
3 files changed, 47 insertions, 53 deletions
diff --git a/src/jdk/nashorn/internal/runtime/arrays/IntArrayData.java b/src/jdk/nashorn/internal/runtime/arrays/IntArrayData.java
index 3c34081f..54d98d9b 100644
--- a/src/jdk/nashorn/internal/runtime/arrays/IntArrayData.java
+++ b/src/jdk/nashorn/internal/runtime/arrays/IntArrayData.java
@@ -75,7 +75,9 @@ public final class IntArrayData extends ArrayData {
}
private static Object[] toObjectArray(final int[] array, final int length) {
- final Object[] oarray = new Object[length];
+ assert length <= array.length : "length exceeds internal array size";
+ final Object[] oarray = new Object[array.length];
+
for (int index = 0; index < length; index++) {
oarray[index] = Integer.valueOf(array[index]);
}
@@ -83,18 +85,22 @@ public final class IntArrayData extends ArrayData {
return oarray;
}
- private static double[] toDoubleArray(final int[] array) {
+ private static double[] toDoubleArray(final int[] array, final int length) {
+ assert length <= array.length : "length exceeds internal array size";
final double[] darray = new double[array.length];
- for (int index = 0; index < array.length; index++) {
+
+ for (int index = 0; index < length; index++) {
darray[index] = array[index];
}
return darray;
}
- private static long[] toLongArray(final int[] array) {
+ private static long[] toLongArray(final int[] array, final int length) {
+ assert length <= array.length : "length exceeds internal array size";
final long[] larray = new long[array.length];
- for (int index = 0; index < array.length; index++) {
+
+ for (int index = 0; index < length; index++) {
larray[index] = array[index];
}
@@ -105,12 +111,14 @@ public final class IntArrayData extends ArrayData {
public ArrayData convert(final Class<?> type) {
if (type == Integer.class) {
return this;
- } else if (type == Long.class) {
- return new LongArrayData(IntArrayData.toLongArray(array), (int) length());
+ }
+ final int length = (int) length();
+ if (type == Long.class) {
+ return new LongArrayData(IntArrayData.toLongArray(array, length), length);
} else if (type == Double.class) {
- return new NumberArrayData(IntArrayData.toDoubleArray(array), (int) length());
+ return new NumberArrayData(IntArrayData.toDoubleArray(array, length), length);
} else {
- return new ObjectArrayData(IntArrayData.toObjectArray(array, array.length), (int) length());
+ return new ObjectArrayData(IntArrayData.toObjectArray(array, length), length);
}
}
@@ -161,26 +169,13 @@ public final class IntArrayData extends ArrayData {
@Override
public ArrayData set(final int index, final Object value, final boolean strict) {
- try {
- final int intValue = ((Integer)value).intValue();
- array[index] = intValue;
- setLength(Math.max(index + 1, length()));
- return this;
- } catch (final NullPointerException | ClassCastException e) {
- if (value instanceof Short || value instanceof Byte) {
- final int intValue = ((Number)value).intValue();
- array[index] = intValue;
- setLength(Math.max(index + 1, length()));
- return this;
- }
-
- if (value == ScriptRuntime.UNDEFINED) {
- return new UndefinedArrayFilter(this).set(index, value, strict);
- }
+ if (value instanceof Integer) {
+ return set(index, ((Number)value).intValue(), strict);
+ } else if (value == ScriptRuntime.UNDEFINED) {
+ return new UndefinedArrayFilter(this).set(index, value, strict);
}
final ArrayData newData = convert(value == null ? Object.class : value.getClass());
-
return newData.set(index, value, strict);
}
diff --git a/src/jdk/nashorn/internal/runtime/arrays/LongArrayData.java b/src/jdk/nashorn/internal/runtime/arrays/LongArrayData.java
index 800c7c67..f33c8638 100644
--- a/src/jdk/nashorn/internal/runtime/arrays/LongArrayData.java
+++ b/src/jdk/nashorn/internal/runtime/arrays/LongArrayData.java
@@ -55,7 +55,9 @@ final class LongArrayData extends ArrayData {
}
private static Object[] toObjectArray(final long[] array, final int length) {
- final Object[] oarray = new Object[length];
+ assert length <= array.length : "length exceeds internal array size";
+ final Object[] oarray = new Object[array.length];
+
for (int index = 0; index < length; index++) {
oarray[index] = Long.valueOf(array[index]);
}
@@ -71,9 +73,11 @@ final class LongArrayData extends ArrayData {
return super.asArrayOfType(componentType);
}
- private static double[] toDoubleArray(final long[] array) {
+ private static double[] toDoubleArray(final long[] array, final int length) {
+ assert length <= array.length : "length exceeds internal array size";
final double[] darray = new double[array.length];
- for (int index = 0; index < array.length; index++) {
+
+ for (int index = 0; index < length; index++) {
darray[index] = array[index];
}
@@ -84,10 +88,12 @@ final class LongArrayData extends ArrayData {
public ArrayData convert(final Class<?> type) {
if (type == Long.class) {
return this;
- } else if (type == Double.class) {
- return new NumberArrayData(LongArrayData.toDoubleArray(array), (int) length());
+ }
+ final int length = (int) length();
+ if (type == Double.class) {
+ return new NumberArrayData(LongArrayData.toDoubleArray(array, length), length);
} else {
- return new ObjectArrayData(LongArrayData.toObjectArray(array, array.length), (int) length());
+ return new ObjectArrayData(LongArrayData.toObjectArray(array, length), length);
}
}
@@ -138,19 +144,13 @@ final class LongArrayData extends ArrayData {
@Override
public ArrayData set(final int index, final Object value, final boolean strict) {
- try {
- final long longValue = ((Long)value).longValue();
- array[index] = longValue;
- setLength(Math.max(index + 1, length()));
- return this;
- } catch (final NullPointerException | ClassCastException e) {
- if (value == ScriptRuntime.UNDEFINED) {
- return new UndefinedArrayFilter(this).set(index, value, strict);
- }
+ if (value instanceof Long || value instanceof Integer) {
+ return set(index, ((Number)value).longValue(), strict);
+ } else if (value == ScriptRuntime.UNDEFINED) {
+ return new UndefinedArrayFilter(this).set(index, value, strict);
}
final ArrayData newData = convert(value == null ? Object.class : value.getClass());
-
return newData.set(index, value, strict);
}
diff --git a/src/jdk/nashorn/internal/runtime/arrays/NumberArrayData.java b/src/jdk/nashorn/internal/runtime/arrays/NumberArrayData.java
index 149cbf0c..e555ec45 100644
--- a/src/jdk/nashorn/internal/runtime/arrays/NumberArrayData.java
+++ b/src/jdk/nashorn/internal/runtime/arrays/NumberArrayData.java
@@ -58,7 +58,9 @@ final class NumberArrayData extends ArrayData {
}
private static Object[] toObjectArray(final double[] array, final int length) {
- final Object[] oarray = new Object[length];
+ assert length <= array.length : "length exceeds internal array size";
+ final Object[] oarray = new Object[array.length];
+
for (int index = 0; index < length; index++) {
oarray[index] = Double.valueOf(array[index]);
}
@@ -76,7 +78,8 @@ final class NumberArrayData extends ArrayData {
@Override
public ArrayData convert(final Class<?> type) {
if (type != Double.class && type != Integer.class && type != Long.class) {
- return new ObjectArrayData(NumberArrayData.toObjectArray(array, array.length), (int) length());
+ final int length = (int) length();
+ return new ObjectArrayData(NumberArrayData.toObjectArray(array, length), length);
}
return this;
}
@@ -127,16 +130,12 @@ final class NumberArrayData extends ArrayData {
@Override
public ArrayData set(final int index, final Object value, final boolean strict) {
- try {
- final double doubleValue = ((Number)value).doubleValue();
- array[index] = doubleValue;
- setLength(Math.max(index + 1, length()));
- return this;
- } catch (final NullPointerException | ClassCastException e) {
- if (value == UNDEFINED) {
- return new UndefinedArrayFilter(this).set(index, value, strict);
- }
+ if (value instanceof Double || value instanceof Integer || value instanceof Long) {
+ return set(index, ((Number)value).doubleValue(), strict);
+ } else if (value == UNDEFINED) {
+ return new UndefinedArrayFilter(this).set(index, value, strict);
}
+
final ArrayData newData = convert(value == null ? Object.class : value.getClass());
return newData.set(index, value, strict);
}