src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/arrays/ArrayIndex.java
author hannesw
Wed, 21 Mar 2018 16:55:34 +0100
changeset 49275 c639a6b33c5c
parent 47216 71c04702a3d5
permissions -rw-r--r--
8199869: Missing copyright headers in nashorn source code Reviewed-by: sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
16151
97c1e756ae1e 8005663: Update copyright year to 2013
jlaskey
parents: 16147
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     4
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    10
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    15
 * accompanied this code).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    16
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    20
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    23
 * questions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    24
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    25
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    26
package jdk.nashorn.internal.runtime.arrays;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    28
import jdk.nashorn.internal.runtime.ConsString;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    29
import jdk.nashorn.internal.runtime.JSType;
21867
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
    30
import jdk.nashorn.internal.runtime.ScriptObject;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    31
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    32
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    33
 * Array index computation helpers. that both throw exceptions or return
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    34
 * invalid values.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    35
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    36
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    37
public final class ArrayIndex {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    38
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    39
    private static final int  INVALID_ARRAY_INDEX = -1;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    40
    private static final long MAX_ARRAY_INDEX = 0xfffffffeL;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    41
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    42
    private ArrayIndex() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    43
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    44
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    45
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    46
     * Fast conversion of non-negative integer string to long.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    47
     * @param key Key as a string.
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
    48
     * @return long value of string or {@code -1} if string does not represent a valid index.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    49
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
    private static long fromString(final String key) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    51
        long value = 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    52
        final int length = key.length();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    53
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    54
        // Check for empty string or leading 0
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    55
        if (length == 0 || (length > 1 && key.charAt(0) == '0')) {
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
    56
            return INVALID_ARRAY_INDEX;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    57
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    58
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    59
        // Fast toNumber.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    60
        for (int i = 0; i < length; i++) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    61
            final char digit = key.charAt(i);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    62
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    63
            // If not a digit.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    64
            if (digit < '0' || digit > '9') {
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
    65
                return INVALID_ARRAY_INDEX;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    66
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    68
            // Insert digit.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    69
            value = value * 10 + digit - '0';
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    70
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
            // Check for overflow (need to catch before wrap around.)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
            if (value > MAX_ARRAY_INDEX) {
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
    73
                return INVALID_ARRAY_INDEX;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    74
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    75
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    77
        return value;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    78
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    79
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    80
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    81
     * Returns a valid array index in an int, if the object represents one. This
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    82
     * routine needs to perform quickly since all keys are tested with it.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    83
     *
21867
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
    84
     * <p>The {@code key} parameter must be a JavaScript primitive type, i.e. one of
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
    85
     * {@code String}, {@code Number}, {@code Boolean}, {@code null}, or {@code undefined}.
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
    86
     * {@code ScriptObject} instances should be converted to primitive with
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
    87
     * {@code String.class} hint before being passed to this method.</p>
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
    88
     *
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
    89
     * @param  key key to check for array index.
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
    90
     * @return the array index, or {@code -1} if {@code key} does not represent a valid index.
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
    91
     *         Note that negative return values other than {@code -1} are considered valid and can be converted to
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
    92
     *         the actual index using {@link #toLongIndex(int)}.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    93
     */
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
    94
    public static int getArrayIndex(final Object key) {
17250
e102b63819ad 8013208: Octane performance regression
jlaskey
parents: 16151
diff changeset
    95
        if (key instanceof Integer) {
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
    96
            return getArrayIndex(((Integer) key).intValue());
21867
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
    97
        } else if (key instanceof Double) {
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
    98
            return getArrayIndex(((Double) key).doubleValue());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    99
        } else if (key instanceof String) {
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
   100
            return (int)fromString((String) key);
21867
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
   101
        } else if (key instanceof Long) {
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
   102
            return getArrayIndex(((Long) key).longValue());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   103
        } else if (key instanceof ConsString) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   104
            return (int)fromString(key.toString());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   105
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   106
21867
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
   107
        assert !(key instanceof ScriptObject);
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
   108
        return INVALID_ARRAY_INDEX;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   109
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   110
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   111
    /**
21867
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
   112
     * Returns a valid array index in an int, if {@code key} represents one.
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
   113
     *
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
   114
     * @param key key to check
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
   115
     * @return the array index, or {@code -1} if {@code key} is not a valid array index.
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
   116
     */
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
   117
    public static int getArrayIndex(final int key) {
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
   118
        return (key >= 0) ? key : INVALID_ARRAY_INDEX;
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
   119
    }
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
   120
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
   121
    /**
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
   122
     * Returns a valid array index in an int, if the long represents one.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   123
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   124
     * @param key key to check
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
   125
     * @return the array index, or {@code -1} if long is not a valid array index.
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
   126
     *         Note that negative return values other than {@code -1} are considered valid and can be converted to
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
   127
     *         the actual index using {@link #toLongIndex(int)}.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   128
     */
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
   129
    public static int getArrayIndex(final long key) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   130
        if (key >= 0 && key <= MAX_ARRAY_INDEX) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   131
            return (int)key;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   132
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   133
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
   134
        return INVALID_ARRAY_INDEX;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   135
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   136
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   137
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   138
    /**
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
   139
     * Return a valid index for this double, if it represents one.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   140
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   141
     * Doubles that aren't representable exactly as longs/ints aren't working
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   142
     * array indexes, however, array[1.1] === array["1.1"] in JavaScript.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   143
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   144
     * @param key the key to check
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
   145
     * @return the array index this double represents or {@code -1} if this isn't a valid index.
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
   146
     *         Note that negative return values other than {@code -1} are considered valid and can be converted to
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
   147
     *         the actual index using {@link #toLongIndex(int)}.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   148
     */
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
   149
    public static int getArrayIndex(final double key) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   150
        if (JSType.isRepresentableAsInt(key)) {
21867
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
   151
            return getArrayIndex((int) key);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   152
        } else if (JSType.isRepresentableAsLong(key)) {
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
   153
            return getArrayIndex((long) key);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   154
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   155
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
   156
        return INVALID_ARRAY_INDEX;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   157
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   158
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   159
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   160
    /**
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
   161
     * Return a valid array index for this string, if it represents one.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   162
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   163
     * @param key the key to check
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
   164
     * @return the array index this string represents or {@code -1} if this isn't a valid index.
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
   165
     *         Note that negative return values other than {@code -1} are considered valid and can be converted to
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
   166
     *         the actual index using {@link #toLongIndex(int)}.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   167
     */
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
   168
    public static int getArrayIndex(final String key) {
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
   169
        return (int)fromString(key);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   170
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   171
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   172
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   173
     * Check whether an index is valid as an array index. This check only tests if
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   174
     * it is the special "invalid array index" type, not if it is e.g. less than zero
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   175
     * or corrupt in some other way
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   176
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   177
     * @param index index to test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   178
     * @return true if {@code index} is not the special invalid array index type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   179
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   180
    public static boolean isValidArrayIndex(final int index) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   181
        return index != INVALID_ARRAY_INDEX;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   182
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   183
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   184
    /**
27212
3361766097cd 8061955: asm.js idioms result in unnecessarily code emission
attila
parents: 25865
diff changeset
   185
     * Convert an index to a long value. This basically amounts to converting it into a
3361766097cd 8061955: asm.js idioms result in unnecessarily code emission
attila
parents: 25865
diff changeset
   186
     * {@link JSType#toUint32(int)} uint32} as the maximum array index in JavaScript
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17250
diff changeset
   187
     * is 0xfffffffe
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   188
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   189
     * @param index index to convert to long form
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   190
     * @return index as uint32 in a long
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   191
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   192
    public static long toLongIndex(final int index) {
27212
3361766097cd 8061955: asm.js idioms result in unnecessarily code emission
attila
parents: 25865
diff changeset
   193
        return JSType.toUint32(index);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   194
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   195
21867
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
   196
    /**
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
   197
     * Convert an index to a key string. This is the same as calling {@link #toLongIndex(int)}
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
   198
     * and converting the result to String.
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
   199
     *
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
   200
     * @param index index to convert
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
   201
     * @return index as string
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
   202
     */
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
   203
    public static String toKey(final int index) {
27212
3361766097cd 8061955: asm.js idioms result in unnecessarily code emission
attila
parents: 25865
diff changeset
   204
        return Long.toString(JSType.toUint32(index));
21867
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
   205
    }
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 19236
diff changeset
   206
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   207
}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   208