nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/arrays/IntArrayData.java
author lagergren
Fri, 26 Sep 2014 18:47:20 +0200
changeset 26886 18c744ab4df2
parent 26768 751b0f427090
child 27098 2875b30458d3
permissions -rw-r--r--
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this Reviewed-by: attila, hannesw
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
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
    28
import static jdk.nashorn.internal.codegen.CompilerConstants.specialCall;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    29
import java.lang.invoke.MethodHandle;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    30
import java.lang.invoke.MethodHandles;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    31
import java.util.Arrays;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    32
import jdk.nashorn.internal.codegen.types.Type;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    33
import jdk.nashorn.internal.runtime.JSType;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    34
import jdk.nashorn.internal.runtime.ScriptRuntime;
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
 * Implementation of {@link ArrayData} as soon as an int has been
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    38
 * written to the array. This is the default data for new arrays
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    39
 */
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
    40
final class IntArrayData extends ContinuousArrayData implements IntElements {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    41
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    42
     * The wrapped array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    43
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    44
    private int[] array;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    45
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    46
    IntArrayData() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    47
        this(new int[ArrayData.CHUNK_SIZE], 0);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    48
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    49
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
    IntArrayData(final int length) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    51
        super(length);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    52
        this.array  = new int[ArrayData.nextSize(length)];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    53
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    54
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    55
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    56
     * Constructor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    57
     * @param array an int array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    58
     * @param length a length, not necessarily array.length
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    59
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    60
    IntArrayData(final int array[], final int length) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    61
        super(length);
19236
73d242d205f9 8020132: Big object literal with numerical keys exceeds method size
hannesw
parents: 18879
diff changeset
    62
        assert array.length >= length;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    63
        this.array = array;
19236
73d242d205f9 8020132: Big object literal with numerical keys exceeds method size
hannesw
parents: 18879
diff changeset
    64
    }
73d242d205f9 8020132: Big object literal with numerical keys exceeds method size
hannesw
parents: 18879
diff changeset
    65
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
    66
    @Override
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
    67
    public Class<?> getElementType() {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
    68
        return int.class;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
    69
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
    70
24741
4232289c3235 8040102: Remove all references to Unsafe and definition of anonymous clases from the code
lagergren
parents: 24720
diff changeset
    71
    private static final MethodHandle HAS_GET_ELEM = specialCall(MethodHandles.lookup(), IntArrayData.class, "getElem", int.class, int.class).methodHandle();
4232289c3235 8040102: Remove all references to Unsafe and definition of anonymous clases from the code
lagergren
parents: 24720
diff changeset
    72
    private static final MethodHandle SET_ELEM     = specialCall(MethodHandles.lookup(), IntArrayData.class, "setElem", void.class, int.class, int.class).methodHandle();
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    73
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
    74
    @Override
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
    75
    public Object[] asObjectArray() {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
    76
        return toObjectArray();
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
    77
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
    78
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    79
    @SuppressWarnings("unused")
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    80
    private int getElem(final int index) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    81
        if (has(index)) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    82
            return array[index];
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    83
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    84
        throw new ClassCastException();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    85
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    86
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    87
    @SuppressWarnings("unused")
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    88
    private void setElem(final int index, final int elem) {
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
    89
        if (hasRoomFor(index)) {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
    90
            array[index] = elem;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
    91
            return;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
    92
        }
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
    93
        throw new ClassCastException();
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    94
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    95
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    96
    @Override
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    97
    public MethodHandle getElementGetter(final Class<?> returnType, final int programPoint) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    98
        return getContinuousElementGetter(HAS_GET_ELEM, returnType, programPoint);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    99
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   100
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   101
    @Override
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   102
    public MethodHandle getElementSetter(final Class<?> elementType) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   103
        return elementType == int.class ? getContinuousElementSetter(SET_ELEM, elementType) : null;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   104
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   105
19236
73d242d205f9 8020132: Big object literal with numerical keys exceeds method size
hannesw
parents: 18879
diff changeset
   106
    @Override
73d242d205f9 8020132: Big object literal with numerical keys exceeds method size
hannesw
parents: 18879
diff changeset
   107
    public ArrayData copy() {
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   108
        return new IntArrayData(array.clone(), (int)length);
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
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   112
    public Object asArrayOfType(final Class<?> componentType) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   113
        if (componentType == int.class) {
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   114
            return array.length == length ? array.clone() : Arrays.copyOf(array, (int)length);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   115
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   116
        return super.asArrayOfType(componentType);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   117
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   118
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   119
    private Object[] toObjectArray() {
18879
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   120
        assert length <= array.length : "length exceeds internal array size";
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   121
        final Object[] oarray = new Object[array.length];
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   122
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   123
        for (int index = 0; index < length; index++) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   124
            oarray[index] = Integer.valueOf(array[index]);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   125
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   126
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   127
        return oarray;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   128
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   129
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   130
    private double[] toDoubleArray() {
18879
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   131
        assert length <= array.length : "length exceeds internal array size";
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   132
        final double[] darray = new double[array.length];
18879
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   133
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   134
        for (int index = 0; index < length; index++) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   135
            darray[index] = array[index];
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
        return darray;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   139
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   140
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   141
    private long[] toLongArray() {
18879
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   142
        assert length <= array.length : "length exceeds internal array size";
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   143
        final long[] larray = new long[array.length];
18879
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   144
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   145
        for (int index = 0; index < length; index++) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   146
            larray[index] = array[index];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   147
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   148
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   149
        return larray;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   150
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   151
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   152
    private LongArrayData convertToLong() {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   153
        return new LongArrayData(toLongArray(), (int)length);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   154
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   155
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   156
    private NumberArrayData convertToDouble() {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   157
        return new NumberArrayData(toDoubleArray(), (int)length);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   158
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   159
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   160
    private ObjectArrayData convertToObject() {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   161
        return new ObjectArrayData(toObjectArray(), (int)length);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   162
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   163
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   164
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   165
    public ArrayData convert(final Class<?> type) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   166
        if (type == Integer.class) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   167
            return this;
18879
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   168
        }
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   169
        if (type == Long.class) {
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   170
            return convertToLong();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   171
        } else if (type == Double.class) {
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   172
            return convertToDouble();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   173
        } else {
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   174
            assert type == null || (!Number.class.isAssignableFrom(type) && !type.isPrimitive());
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   175
            return convertToObject();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   176
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   177
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   178
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   179
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   180
    public void shiftLeft(final int by) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   181
        System.arraycopy(array, by, array, 0, array.length - by);
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
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   185
    public ArrayData shiftRight(final int by) {
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   186
        final ArrayData newData = ensure(by + length - 1);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   187
        if (newData != this) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   188
            newData.shiftRight(by);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   189
            return newData;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   190
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   191
        System.arraycopy(array, 0, array, by, array.length - by);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   192
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   193
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   194
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   195
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   196
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   197
    public ArrayData ensure(final long safeIndex) {
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   198
        if (safeIndex >= SparseArrayData.MAX_DENSE_LENGTH) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   199
            return new SparseArrayData(this, safeIndex + 1);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   200
        }
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   201
        final int alen = array.length;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   202
        if (safeIndex >= alen) {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   203
            final int newLength = ArrayData.nextSize((int)safeIndex);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   204
            array = Arrays.copyOf(array, newLength);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   205
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   206
        setLength(safeIndex + 1);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   207
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   208
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   209
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   210
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   211
    public ArrayData shrink(final long newLength) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   212
        Arrays.fill(array, (int) newLength, array.length, 0);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   213
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   214
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   215
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   216
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   217
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   218
    public ArrayData set(final int index, final Object value, final boolean strict) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   219
        if (JSType.isRepresentableAsInt(value)) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   220
            return set(index, JSType.toInt32(value), strict);
18879
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   221
        } else if (value == ScriptRuntime.UNDEFINED) {
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   222
            return new UndefinedArrayFilter(this).set(index, value, strict);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   223
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   224
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   225
        final ArrayData newData = convert(value == null ? Object.class : value.getClass());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   226
        return newData.set(index, value, strict);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   227
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   228
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   229
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   230
    public ArrayData set(final int index, final int value, final boolean strict) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   231
        array[index] = value;
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   232
        setLength(Math.max(index + 1, length));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   233
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   234
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   235
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   236
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   237
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   238
    public ArrayData set(final int index, final long value, final boolean strict) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   239
        if (JSType.isRepresentableAsInt(value)) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   240
            array[index] = JSType.toInt32(value);
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   241
            setLength(Math.max(index + 1, length));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   242
            return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   243
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   244
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   245
        return convert(Long.class).set(index, value, strict);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   246
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   247
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   248
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   249
    public ArrayData set(final int index, final double value, final boolean strict) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   250
        if (JSType.isRepresentableAsInt(value)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   251
            array[index] = (int)(long)value;
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   252
            setLength(Math.max(index + 1, length));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   253
            return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   254
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   255
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   256
        return convert(Double.class).set(index, value, strict);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   257
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   258
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   259
    @Override
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   260
    public Type getOptimisticType() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   261
        return Type.INT;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   262
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   263
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   264
    @Override
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   265
    public int getInt(final int index) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   266
        return array[index];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   267
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   268
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   269
    @Override
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   270
    public int getIntOptimistic(final int index, final int programPoint) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   271
        return array[index];
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   272
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   273
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   274
    @Override
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   275
    public long getLong(final int index) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   276
        return array[index];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   277
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   278
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   279
    @Override
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   280
    public long getLongOptimistic(final int index, final int programPoint) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   281
        return array[index];
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   282
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   283
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   284
    @Override
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   285
    public double getDouble(final int index) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   286
        return array[index];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   287
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   288
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   289
    @Override
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   290
    public double getDoubleOptimistic(final int index, final int programPoint) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   291
        return array[index];
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   292
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   293
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   294
    @Override
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   295
    public Object getObject(final int index) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   296
        return array[index];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   297
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   298
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   299
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   300
    public boolean has(final int index) {
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   301
        return 0 <= index && index < length;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   302
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   303
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   304
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   305
    public ArrayData delete(final int index) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   306
        return new DeletedRangeArrayFilter(this, index, index);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   307
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   308
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   309
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   310
    public ArrayData delete(final long fromIndex, final long toIndex) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   311
        return new DeletedRangeArrayFilter(this, fromIndex, toIndex);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   312
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   313
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   314
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   315
    public Object pop() {
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   316
        if (length == 0) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   317
            return ScriptRuntime.UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   318
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   319
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   320
        final int newLength = (int)length - 1;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   321
        final int elem = array[newLength];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   322
        array[newLength] = 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   323
        setLength(newLength);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   324
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   325
        return elem;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   326
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   327
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   328
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   329
    public ArrayData slice(final long from, final long to) {
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   330
        final long start     = from < 0 ? from + length : from;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   331
        final long newLength = to - start;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   332
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   333
        return new IntArrayData(Arrays.copyOfRange(array, (int)from, (int)to), (int)newLength);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   334
    }
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   335
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   336
    @Override
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   337
    public final ArrayData push(final boolean strict, final int item) {
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   338
        final long      len     = length;
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   339
        final ArrayData newData = ensure(len);
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   340
        if (newData == this) {
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   341
            array[(int)len] = item;
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   342
            return this;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   343
        }
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   344
        return newData.set((int)len, item, strict);
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   345
    }
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   346
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   347
    @Override
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   348
    public ArrayData fastSplice(final int start, final int removed, final int added) throws UnsupportedOperationException {
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   349
        final long oldLength = length;
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   350
        final long newLength = oldLength - removed + added;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   351
        if (newLength > SparseArrayData.MAX_DENSE_LENGTH && newLength > array.length) {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   352
            throw new UnsupportedOperationException();
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   353
        }
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   354
        final ArrayData returnValue = removed == 0 ?
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   355
                EMPTY_ARRAY : new IntArrayData(Arrays.copyOfRange(array, start, start + removed), removed);
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   356
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   357
        if (newLength != oldLength) {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   358
            final int[] newArray;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   359
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   360
            if (newLength > array.length) {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   361
                newArray = new int[ArrayData.nextSize((int)newLength)];
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   362
                System.arraycopy(array, 0, newArray, 0, start);
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   363
            } else {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   364
                newArray = array;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   365
            }
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   366
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   367
            System.arraycopy(array, start + removed, newArray, start + added, (int)(oldLength - start - removed));
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   368
            array = newArray;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   369
            setLength(newLength);
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   370
        }
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   371
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   372
        return returnValue;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   373
    }
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   374
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   375
    @Override
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   376
    public long fastPush(final int arg) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   377
        final int len = (int)length;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   378
        if (len == array.length) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   379
            array = Arrays.copyOf(array, nextSize(len));
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   380
        }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   381
        array[len] = arg;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   382
        return ++length;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   383
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   384
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   385
    //length must not be zero
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   386
    @Override
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   387
    public int fastPopInt() {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   388
        if (length == 0) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   389
            throw new ClassCastException(); //relink
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   390
        }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   391
        final int newLength = (int)--length;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   392
        final int elem = array[newLength];
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   393
        array[newLength] = 0;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   394
        return elem;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   395
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   396
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   397
    @Override
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   398
    public long fastPopLong() {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   399
        return fastPopInt();
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   400
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   401
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   402
    @Override
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   403
    public double fastPopDouble() {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   404
        return fastPopInt();
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   405
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   406
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   407
    @Override
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   408
    public Object fastPopObject() {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   409
        return fastPopInt();
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   410
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   411
}