diff options
author | hannesw <none@none> | 2013-11-16 00:23:46 +0100 |
---|---|---|
committer | hannesw <none@none> | 2013-11-16 00:23:46 +0100 |
commit | 7bbda0ea1a878c391f767c1e851db8e4c24f020e (patch) | |
tree | 6fc4a90b0ef35aa2b208ce20ad4318452654e140 /src/jdk/nashorn/internal/runtime/arrays/ArrayIndex.java | |
parent | 127f8f3522e221e6e5eefb900e5c52f17d7d5b02 (diff) |
8028210: Missing conversions on array index expression
Reviewed-by: attila, jlaskey, lagergren
Diffstat (limited to 'src/jdk/nashorn/internal/runtime/arrays/ArrayIndex.java')
-rw-r--r-- | src/jdk/nashorn/internal/runtime/arrays/ArrayIndex.java | 41 |
1 files changed, 34 insertions, 7 deletions
diff --git a/src/jdk/nashorn/internal/runtime/arrays/ArrayIndex.java b/src/jdk/nashorn/internal/runtime/arrays/ArrayIndex.java index 58d1a49d..5c857e11 100644 --- a/src/jdk/nashorn/internal/runtime/arrays/ArrayIndex.java +++ b/src/jdk/nashorn/internal/runtime/arrays/ArrayIndex.java @@ -27,6 +27,7 @@ package jdk.nashorn.internal.runtime.arrays; import jdk.nashorn.internal.runtime.ConsString; import jdk.nashorn.internal.runtime.JSType; +import jdk.nashorn.internal.runtime.ScriptObject; /** * Array index computation helpers. that both throw exceptions or return @@ -80,7 +81,12 @@ public final class ArrayIndex { * Returns a valid array index in an int, if the object represents one. This * routine needs to perform quickly since all keys are tested with it. * - * @param key key to check for array index + * <p>The {@code key} parameter must be a JavaScript primitive type, i.e. one of + * {@code String}, {@code Number}, {@code Boolean}, {@code null}, or {@code undefined}. + * {@code ScriptObject} instances should be converted to primitive with + * {@code String.class} hint before being passed to this method.</p> + * + * @param key key to check for array index. * @return the array index, or {@code -1} if {@code key} does not represent a valid index. * Note that negative return values other than {@code -1} are considered valid and can be converted to * the actual index using {@link #toLongIndex(int)}. @@ -88,18 +94,31 @@ public final class ArrayIndex { public static int getArrayIndex(final Object key) { if (key instanceof Integer) { return getArrayIndex(((Integer) key).intValue()); - } else if (key instanceof Number) { - return getArrayIndex(((Number) key).doubleValue()); + } else if (key instanceof Double) { + return getArrayIndex(((Double) key).doubleValue()); } else if (key instanceof String) { return (int)fromString((String) key); + } else if (key instanceof Long) { + return getArrayIndex(((Long) key).longValue()); } else if (key instanceof ConsString) { return (int)fromString(key.toString()); } + assert !(key instanceof ScriptObject); return INVALID_ARRAY_INDEX; } /** + * Returns a valid array index in an int, if {@code key} represents one. + * + * @param key key to check + * @return the array index, or {@code -1} if {@code key} is not a valid array index. + */ + public static int getArrayIndex(final int key) { + return (key >= 0) ? key : INVALID_ARRAY_INDEX; + } + + /** * Returns a valid array index in an int, if the long represents one. * * @param key key to check @@ -129,10 +148,7 @@ public final class ArrayIndex { */ public static int getArrayIndex(final double key) { if (JSType.isRepresentableAsInt(key)) { - final int intKey = (int)key; - if (intKey >= 0) { - return intKey; - } + return getArrayIndex((int) key); } else if (JSType.isRepresentableAsLong(key)) { return getArrayIndex((long) key); } @@ -177,5 +193,16 @@ public final class ArrayIndex { return index & JSType.MAX_UINT; } + /** + * Convert an index to a key string. This is the same as calling {@link #toLongIndex(int)} + * and converting the result to String. + * + * @param index index to convert + * @return index as string + */ + public static String toKey(final int index) { + return Long.toString(index & JSType.MAX_UINT); + } + } |