src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/arrays/IntArrayData.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
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
    28
import static jdk.nashorn.internal.codegen.CompilerConstants.specialCall;
28576
37b0c5bda589 8067880: Dead typed push methods in ArrayData
attila
parents: 27525
diff changeset
    29
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    30
import java.lang.invoke.MethodHandle;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    31
import java.lang.invoke.MethodHandles;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    32
import java.util.Arrays;
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
     */
27209
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
    60
    IntArrayData(final int[] array, final int length) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    61
        super(length);
27209
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
    62
        assert array == null || 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
27209
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
    67
    public final Class<?> getElementType() {
26768
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
27209
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
    71
    @Override
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
    72
    public final Class<?> getBoxedElementType() {
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
    73
        return Integer.class;
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
    74
    }
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
    75
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
    76
    @Override
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
    77
    public final int getElementWeight() {
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
    78
        return 1;
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
    79
    }
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
    80
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
    81
    @Override
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
    82
    public final ContinuousArrayData widest(final ContinuousArrayData otherData) {
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
    83
        return otherData;
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
    84
    }
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
    85
24741
4232289c3235 8040102: Remove all references to Unsafe and definition of anonymous clases from the code
lagergren
parents: 24720
diff changeset
    86
    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
    87
    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
    88
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
    89
    @Override
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
    90
    public Object[] asObjectArray() {
27098
2875b30458d3 8060011: Concatenating an array and converting it to Java gives wrong result
hannesw
parents: 26886
diff changeset
    91
        return toObjectArray(true);
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
    92
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
    93
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    94
    @SuppressWarnings("unused")
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    95
    private int getElem(final int index) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    96
        if (has(index)) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    97
            return array[index];
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    98
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    99
        throw new ClassCastException();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   100
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   101
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   102
    @SuppressWarnings("unused")
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   103
    private void setElem(final int index, final int elem) {
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   104
        if (hasRoomFor(index)) {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   105
            array[index] = elem;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   106
            return;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   107
        }
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   108
        throw new ClassCastException();
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   109
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   110
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   111
    @Override
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   112
    public MethodHandle getElementGetter(final Class<?> returnType, final int programPoint) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   113
        return getContinuousElementGetter(HAS_GET_ELEM, returnType, programPoint);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   114
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   115
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   116
    @Override
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   117
    public MethodHandle getElementSetter(final Class<?> elementType) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   118
        return elementType == int.class ? getContinuousElementSetter(SET_ELEM, elementType) : null;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   119
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   120
19236
73d242d205f9 8020132: Big object literal with numerical keys exceeds method size
hannesw
parents: 18879
diff changeset
   121
    @Override
27209
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
   122
    public IntArrayData copy() {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   123
        return new IntArrayData(array.clone(), (int)length());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   124
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   125
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   126
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   127
    public Object asArrayOfType(final Class<?> componentType) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   128
        if (componentType == int.class) {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   129
            final int len = (int)length();
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   130
            return array.length == len ? array.clone() : Arrays.copyOf(array, len);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   131
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   132
        return super.asArrayOfType(componentType);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   133
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   134
27098
2875b30458d3 8060011: Concatenating an array and converting it to Java gives wrong result
hannesw
parents: 26886
diff changeset
   135
    private Object[] toObjectArray(final boolean trim) {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   136
        assert length() <= array.length : "length exceeds internal array size";
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   137
        final int len = (int)length();
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   138
        final Object[] oarray = new Object[trim ? len : array.length];
18879
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   139
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   140
        for (int index = 0; index < len; index++) {
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 30703
diff changeset
   141
            oarray[index] = array[index];
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   142
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   143
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   144
        return oarray;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   145
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   146
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   147
    private double[] toDoubleArray() {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   148
        assert length() <= array.length : "length exceeds internal array size";
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   149
        final int len = (int)length();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   150
        final double[] darray = new double[array.length];
18879
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   151
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   152
        for (int index = 0; index < len; index++) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   153
            darray[index] = array[index];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   154
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   155
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   156
        return darray;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   157
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   158
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   159
    private NumberArrayData convertToDouble() {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   160
        return new NumberArrayData(toDoubleArray(), (int)length());
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   161
    }
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
    private ObjectArrayData convertToObject() {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   164
        return new ObjectArrayData(toObjectArray(false), (int)length());
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   165
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   166
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   167
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   168
    public ArrayData convert(final Class<?> type) {
30703
60aecf19ea09 8079145: jdk.nashorn.internal.runtime.arrays.IntArrayData.convert assertion
sundar
parents: 28787
diff changeset
   169
        if (type == Integer.class || type == Byte.class || type == Short.class) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   170
            return this;
30703
60aecf19ea09 8079145: jdk.nashorn.internal.runtime.arrays.IntArrayData.convert assertion
sundar
parents: 28787
diff changeset
   171
        } else if (type == Double.class || type == Float.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
            return convertToObject();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   175
        }
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
    @Override
42790
edb30aa1a99b 8171219: Missing checks in sparse array shift() implementation
hannesw
parents: 34974
diff changeset
   179
    public ArrayData shiftLeft(final int by) {
edb30aa1a99b 8171219: Missing checks in sparse array shift() implementation
hannesw
parents: 34974
diff changeset
   180
        if (by >= length()) {
edb30aa1a99b 8171219: Missing checks in sparse array shift() implementation
hannesw
parents: 34974
diff changeset
   181
            shrink(0);
edb30aa1a99b 8171219: Missing checks in sparse array shift() implementation
hannesw
parents: 34974
diff changeset
   182
        } else {
edb30aa1a99b 8171219: Missing checks in sparse array shift() implementation
hannesw
parents: 34974
diff changeset
   183
            System.arraycopy(array, by, array, 0, array.length - by);
edb30aa1a99b 8171219: Missing checks in sparse array shift() implementation
hannesw
parents: 34974
diff changeset
   184
        }
edb30aa1a99b 8171219: Missing checks in sparse array shift() implementation
hannesw
parents: 34974
diff changeset
   185
        setLength(Math.max(0, length() - by));
edb30aa1a99b 8171219: Missing checks in sparse array shift() implementation
hannesw
parents: 34974
diff changeset
   186
edb30aa1a99b 8171219: Missing checks in sparse array shift() implementation
hannesw
parents: 34974
diff changeset
   187
        return this;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   188
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   189
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   190
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   191
    public ArrayData shiftRight(final int by) {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   192
        final ArrayData newData = ensure(by + length() - 1);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   193
        if (newData != this) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   194
            newData.shiftRight(by);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   195
            return newData;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   196
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   197
        System.arraycopy(array, 0, array, by, array.length - by);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   198
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   199
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   200
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   201
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   202
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   203
    public ArrayData ensure(final long safeIndex) {
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   204
        if (safeIndex >= SparseArrayData.MAX_DENSE_LENGTH) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   205
            return new SparseArrayData(this, safeIndex + 1);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   206
        }
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   207
        final int alen = array.length;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   208
        if (safeIndex >= alen) {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   209
            final int newLength = ArrayData.nextSize((int)safeIndex);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   210
            array = Arrays.copyOf(array, newLength);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   211
        }
28787
cab4b0497d27 8068872: Nashorn JSON.parse drops numeric keys
hannesw
parents: 28576
diff changeset
   212
        if (safeIndex >= length()) {
cab4b0497d27 8068872: Nashorn JSON.parse drops numeric keys
hannesw
parents: 28576
diff changeset
   213
            setLength(safeIndex + 1);
cab4b0497d27 8068872: Nashorn JSON.parse drops numeric keys
hannesw
parents: 28576
diff changeset
   214
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   215
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   216
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   217
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   218
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   219
    public ArrayData shrink(final long newLength) {
27209
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
   220
        Arrays.fill(array, (int)newLength, array.length, 0);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   221
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   222
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   223
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   224
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   225
    public ArrayData set(final int index, final Object value, final boolean strict) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   226
        if (JSType.isRepresentableAsInt(value)) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   227
            return set(index, JSType.toInt32(value), strict);
18879
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   228
        } else if (value == ScriptRuntime.UNDEFINED) {
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   229
            return new UndefinedArrayFilter(this).set(index, value, strict);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   230
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   231
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   232
        final ArrayData newData = convert(value == null ? Object.class : value.getClass());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   233
        return newData.set(index, value, strict);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   234
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   235
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   236
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   237
    public ArrayData set(final int index, final int value, final boolean strict) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   238
        array[index] = value;
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   239
        setLength(Math.max(index + 1, length()));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   240
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   241
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   242
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   243
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   244
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   245
    public ArrayData set(final int index, final double value, final boolean strict) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   246
        if (JSType.isRepresentableAsInt(value)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   247
            array[index] = (int)(long)value;
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   248
            setLength(Math.max(index + 1, length()));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   249
            return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   250
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   251
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   252
        return convert(Double.class).set(index, value, strict);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   253
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   254
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   255
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   256
    public int getInt(final int index) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   257
        return array[index];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   258
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   259
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   260
    @Override
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   261
    public int getIntOptimistic(final int index, final int programPoint) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   262
        return array[index];
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   263
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   264
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   265
    @Override
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   266
    public double getDouble(final int index) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   267
        return array[index];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   268
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   269
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   270
    @Override
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   271
    public double getDoubleOptimistic(final int index, final int programPoint) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   272
        return array[index];
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   273
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   274
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   275
    @Override
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   276
    public Object getObject(final int index) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   277
        return array[index];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   278
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   279
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   280
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   281
    public boolean has(final int index) {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   282
        return 0 <= index && index < length();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   283
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   284
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   285
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   286
    public ArrayData delete(final int index) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   287
        return new DeletedRangeArrayFilter(this, index, index);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   288
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   289
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   290
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   291
    public ArrayData delete(final long fromIndex, final long toIndex) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   292
        return new DeletedRangeArrayFilter(this, fromIndex, toIndex);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   293
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   294
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   295
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   296
    public Object pop() {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   297
        final int len = (int)length();
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   298
        if (len == 0) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   299
            return ScriptRuntime.UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   300
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   301
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   302
        final int newLength = len - 1;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   303
        final int elem = array[newLength];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   304
        array[newLength] = 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   305
        setLength(newLength);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   306
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   307
        return elem;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   308
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   309
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   310
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   311
    public ArrayData slice(final long from, final long to) {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   312
        return new IntArrayData(Arrays.copyOfRange(array, (int)from, (int)to), (int)(to - (from < 0 ? from + length() : from)));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   313
    }
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   314
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   315
    @Override
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   316
    public ArrayData fastSplice(final int start, final int removed, final int added) throws UnsupportedOperationException {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   317
        final long oldLength = length();
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   318
        final long newLength = oldLength - removed + added;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   319
        if (newLength > SparseArrayData.MAX_DENSE_LENGTH && newLength > array.length) {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   320
            throw new UnsupportedOperationException();
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   321
        }
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   322
        final ArrayData returnValue = removed == 0 ?
27209
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
   323
                EMPTY_ARRAY :
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
   324
                new IntArrayData(
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
   325
                        Arrays.copyOfRange(
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
   326
                                array,
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
   327
                                start,
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
   328
                                start + removed),
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
   329
                        removed);
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   330
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   331
        if (newLength != oldLength) {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   332
            final int[] newArray;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   333
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   334
            if (newLength > array.length) {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   335
                newArray = new int[ArrayData.nextSize((int)newLength)];
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   336
                System.arraycopy(array, 0, newArray, 0, start);
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   337
            } else {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   338
                newArray = array;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   339
            }
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   340
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   341
            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
   342
            array = newArray;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   343
            setLength(newLength);
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   344
        }
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   345
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   346
        return returnValue;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   347
    }
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   348
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   349
    @Override
34974
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 34732
diff changeset
   350
    public double fastPush(final int arg) {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   351
        final int len = (int)length();
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   352
        if (len == array.length) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   353
            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
   354
        }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   355
        array[len] = arg;
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   356
        return increaseLength();
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   357
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   358
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   359
    //length must not be zero
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   360
    @Override
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   361
    public int fastPopInt() {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   362
        if (length() == 0) {
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   363
            throw new ClassCastException(); //relink
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   364
        }
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   365
        final int newLength = (int)decreaseLength();
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   366
        final int elem = array[newLength];
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   367
        array[newLength] = 0;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   368
        return elem;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   369
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   370
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   371
    @Override
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   372
    public double fastPopDouble() {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   373
        return fastPopInt();
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
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   376
    @Override
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   377
    public Object fastPopObject() {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   378
        return fastPopInt();
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   379
    }
27209
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
   380
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
   381
    @Override
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
   382
    public ContinuousArrayData fastConcat(final ContinuousArrayData otherData) {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   383
        final int   otherLength = (int)otherData.length();
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   384
        final int   thisLength  = (int)length();
27209
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
   385
        assert otherLength > 0 && thisLength > 0;
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
   386
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
   387
        final int[] otherArray  = ((IntArrayData)otherData).array;
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
   388
        final int   newLength   = otherLength + thisLength;
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
   389
        final int[] newArray    = new int[ArrayData.alignUp(newLength)];
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
   390
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
   391
        System.arraycopy(array, 0, newArray, 0, thisLength);
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
   392
        System.arraycopy(otherArray, 0, newArray, thisLength, otherLength);
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
   393
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
   394
        return new IntArrayData(newArray, newLength);
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
   395
    }
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
   396
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
   397
    @Override
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
   398
    public String toString() {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   399
        assert length() <= array.length : length() + " > " + array.length;
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   400
        return getClass().getSimpleName() + ':' + Arrays.toString(Arrays.copyOf(array, (int)length()));
27209
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27104
diff changeset
   401
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   402
}