src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/arrays/ObjectArrayData.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;
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;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    32
import jdk.nashorn.internal.runtime.JSType;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    33
import jdk.nashorn.internal.runtime.ScriptRuntime;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    34
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    35
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    36
 * Implementation of {@link ArrayData} as soon as an Object has been
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    37
 * written to the array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    38
 */
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
    39
final class ObjectArrayData extends ContinuousArrayData implements AnyElements {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    40
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 Object[] array;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    45
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    46
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    47
     * Constructor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    48
     * @param array an int array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    49
     * @param length a length, not necessarily array.length
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
     */
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
    51
    ObjectArrayData(final Object[] array, final int length) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    52
        super(length);
19236
73d242d205f9 8020132: Big object literal with numerical keys exceeds method size
hannesw
parents: 18842
diff changeset
    53
        assert array.length >= length;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    54
        this.array  = array;
19236
73d242d205f9 8020132: Big object literal with numerical keys exceeds method size
hannesw
parents: 18842
diff changeset
    55
    }
73d242d205f9 8020132: Big object literal with numerical keys exceeds method size
hannesw
parents: 18842
diff changeset
    56
73d242d205f9 8020132: Big object literal with numerical keys exceeds method size
hannesw
parents: 18842
diff changeset
    57
    @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
    58
    public final Class<?> getElementType() {
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
    59
        return Object.class;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
    60
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
    61
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
    62
    @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
    63
    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
    64
        return getElementType();
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
    65
    }
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
    66
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
    @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
    68
    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
    69
        return 4;
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
    70
    }
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
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
    @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
    73
    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
    74
        return otherData instanceof NumericElements ? this : 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
    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
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
    @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
    78
    public ObjectArrayData copy() {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
    79
        return new ObjectArrayData(array.clone(), (int)length());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    80
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    81
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    82
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    83
    public Object[] asObjectArray() {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
    84
        return array.length == length() ? array.clone() : asObjectArrayCopy();
24719
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
    private Object[] asObjectArrayCopy() {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
    88
        final long len = length();
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
    89
        assert len <= Integer.MAX_VALUE;
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
    90
        final Object[] copy = new Object[(int)len];
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
    91
        System.arraycopy(array, 0, copy, 0, (int)len);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    92
        return copy;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    93
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    94
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    95
    @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
    96
    public ObjectArrayData convert(final Class<?> type) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    97
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    98
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    99
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   100
    @Override
42790
edb30aa1a99b 8171219: Missing checks in sparse array shift() implementation
hannesw
parents: 34974
diff changeset
   101
    public ArrayData shiftLeft(final int by) {
edb30aa1a99b 8171219: Missing checks in sparse array shift() implementation
hannesw
parents: 34974
diff changeset
   102
        if (by >= length()) {
edb30aa1a99b 8171219: Missing checks in sparse array shift() implementation
hannesw
parents: 34974
diff changeset
   103
            shrink(0);
edb30aa1a99b 8171219: Missing checks in sparse array shift() implementation
hannesw
parents: 34974
diff changeset
   104
        } else {
edb30aa1a99b 8171219: Missing checks in sparse array shift() implementation
hannesw
parents: 34974
diff changeset
   105
            System.arraycopy(array, by, array, 0, array.length - by);
edb30aa1a99b 8171219: Missing checks in sparse array shift() implementation
hannesw
parents: 34974
diff changeset
   106
        }
edb30aa1a99b 8171219: Missing checks in sparse array shift() implementation
hannesw
parents: 34974
diff changeset
   107
        setLength(Math.max(0, length() - by));
edb30aa1a99b 8171219: Missing checks in sparse array shift() implementation
hannesw
parents: 34974
diff changeset
   108
        return this;
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 ArrayData shiftRight(final int by) {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   113
        final ArrayData newData = ensure(by + length() - 1);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   114
        if (newData != this) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   115
            newData.shiftRight(by);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   116
            return newData;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   117
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   118
        System.arraycopy(array, 0, array, by, array.length - by);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   119
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   120
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   121
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   122
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   123
    public ArrayData ensure(final long safeIndex) {
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   124
        if (safeIndex >= SparseArrayData.MAX_DENSE_LENGTH) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   125
            return new SparseArrayData(this, safeIndex + 1);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   126
        }
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   127
        final int alen = array.length;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   128
        if (safeIndex >= alen) {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   129
            final int newLength = ArrayData.nextSize((int)safeIndex);
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   130
            array = Arrays.copyOf(array, newLength); //fill with undefined or OK? TODO
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   131
        }
28787
cab4b0497d27 8068872: Nashorn JSON.parse drops numeric keys
hannesw
parents: 27525
diff changeset
   132
        if (safeIndex >= length()) {
cab4b0497d27 8068872: Nashorn JSON.parse drops numeric keys
hannesw
parents: 27525
diff changeset
   133
            setLength(safeIndex + 1);
cab4b0497d27 8068872: Nashorn JSON.parse drops numeric keys
hannesw
parents: 27525
diff changeset
   134
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   135
        return this;
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
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   139
    public ArrayData shrink(final long newLength) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   140
        Arrays.fill(array, (int) newLength, array.length, ScriptRuntime.UNDEFINED);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   141
        return this;
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
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   145
    public ArrayData set(final int index, final Object value, final boolean strict) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   146
        array[index] = value;
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   147
        setLength(Math.max(index + 1, length()));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   148
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   149
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   150
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   151
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   152
    public ArrayData set(final int index, final int value, final boolean strict) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   153
        array[index] = value;
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   154
        setLength(Math.max(index + 1, length()));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   155
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   156
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   157
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   158
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   159
    public ArrayData set(final int index, final double value, final boolean strict) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   160
        array[index] = value;
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   161
        setLength(Math.max(index + 1, length()));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   162
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   163
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   164
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   165
    @Override
18606
5704d7a4a0a8 8010697: DeletedArrayFilter seems to leak memory
jlaskey
parents: 16151
diff changeset
   166
    public ArrayData setEmpty(final int index) {
5704d7a4a0a8 8010697: DeletedArrayFilter seems to leak memory
jlaskey
parents: 16151
diff changeset
   167
        array[index] = ScriptRuntime.EMPTY;
5704d7a4a0a8 8010697: DeletedArrayFilter seems to leak memory
jlaskey
parents: 16151
diff changeset
   168
        return this;
5704d7a4a0a8 8010697: DeletedArrayFilter seems to leak memory
jlaskey
parents: 16151
diff changeset
   169
    }
5704d7a4a0a8 8010697: DeletedArrayFilter seems to leak memory
jlaskey
parents: 16151
diff changeset
   170
5704d7a4a0a8 8010697: DeletedArrayFilter seems to leak memory
jlaskey
parents: 16151
diff changeset
   171
    @Override
5704d7a4a0a8 8010697: DeletedArrayFilter seems to leak memory
jlaskey
parents: 16151
diff changeset
   172
    public ArrayData setEmpty(final long lo, final long hi) {
27098
2875b30458d3 8060011: Concatenating an array and converting it to Java gives wrong result
hannesw
parents: 26886
diff changeset
   173
        // hi parameter is inclusive, but Arrays.fill toIndex parameter is exclusive
2875b30458d3 8060011: Concatenating an array and converting it to Java gives wrong result
hannesw
parents: 26886
diff changeset
   174
        Arrays.fill(array, (int)Math.max(lo, 0L), (int)Math.min(hi + 1, Integer.MAX_VALUE), ScriptRuntime.EMPTY);
18606
5704d7a4a0a8 8010697: DeletedArrayFilter seems to leak memory
jlaskey
parents: 16151
diff changeset
   175
        return this;
5704d7a4a0a8 8010697: DeletedArrayFilter seems to leak memory
jlaskey
parents: 16151
diff changeset
   176
    }
5704d7a4a0a8 8010697: DeletedArrayFilter seems to leak memory
jlaskey
parents: 16151
diff changeset
   177
24741
4232289c3235 8040102: Remove all references to Unsafe and definition of anonymous clases from the code
lagergren
parents: 24720
diff changeset
   178
    private static final MethodHandle HAS_GET_ELEM = specialCall(MethodHandles.lookup(), ObjectArrayData.class, "getElem", Object.class, int.class).methodHandle();
4232289c3235 8040102: Remove all references to Unsafe and definition of anonymous clases from the code
lagergren
parents: 24720
diff changeset
   179
    private static final MethodHandle SET_ELEM     = specialCall(MethodHandles.lookup(), ObjectArrayData.class, "setElem", void.class, int.class, Object.class).methodHandle();
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   180
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   181
    @SuppressWarnings("unused")
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   182
    private Object getElem(final int index) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   183
        if (has(index)) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   184
            return array[index];
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   185
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   186
        throw new ClassCastException();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   187
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   188
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   189
    @SuppressWarnings("unused")
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   190
    private void setElem(final int index, final Object elem) {
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   191
        if (hasRoomFor(index)) {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   192
            array[index] = elem;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   193
            return;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   194
        }
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   195
        throw new ClassCastException();
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   196
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   197
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   198
    @Override
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   199
    public MethodHandle getElementGetter(final Class<?> returnType, final int programPoint) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   200
        if (returnType.isPrimitive()) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   201
            return null;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   202
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   203
        return getContinuousElementGetter(HAS_GET_ELEM, returnType, programPoint);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   204
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   205
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   206
    @Override
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   207
    public MethodHandle getElementSetter(final Class<?> elementType) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   208
        return getContinuousElementSetter(SET_ELEM, Object.class);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   209
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   210
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   211
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   212
    @Override
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   213
    public int getInt(final int index) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   214
        return JSType.toInt32(array[index]);
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 double getDouble(final int index) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   219
        return JSType.toNumber(array[index]);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   220
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   221
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   222
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   223
    public Object getObject(final int index) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   224
        return array[index];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   225
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   226
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   227
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   228
    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
   229
        return 0 <= index && index < length();
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
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   233
    public ArrayData delete(final int index) {
18606
5704d7a4a0a8 8010697: DeletedArrayFilter seems to leak memory
jlaskey
parents: 16151
diff changeset
   234
        setEmpty(index);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   235
        return new DeletedRangeArrayFilter(this, index, index);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   236
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   237
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   238
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   239
    public ArrayData delete(final long fromIndex, final long toIndex) {
18606
5704d7a4a0a8 8010697: DeletedArrayFilter seems to leak memory
jlaskey
parents: 16151
diff changeset
   240
        setEmpty(fromIndex, toIndex);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   241
        return new DeletedRangeArrayFilter(this, fromIndex, toIndex);
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
34974
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 34732
diff changeset
   245
    public double fastPush(final int arg) {
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   246
        return fastPush((Object)arg);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   247
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   248
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   249
    @Override
34974
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 34732
diff changeset
   250
    public double fastPush(final long arg) {
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   251
        return fastPush((Object)arg);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   252
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   253
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   254
    @Override
34974
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 34732
diff changeset
   255
    public double fastPush(final double arg) {
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   256
        return fastPush((Object)arg);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   257
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   258
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   259
    @Override
34974
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 34732
diff changeset
   260
    public double fastPush(final Object arg) {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   261
        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
   262
        if (len == array.length) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   263
            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
   264
        }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   265
        array[len] = arg;
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   266
        return increaseLength();
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   267
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   268
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   269
    @Override
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   270
    public Object fastPopObject() {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   271
        if (length() == 0) {
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   272
            return ScriptRuntime.UNDEFINED;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   273
        }
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   274
        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
   275
        final Object elem = array[newLength];
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   276
        array[newLength] = ScriptRuntime.EMPTY;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   277
        return elem;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   278
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   279
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   280
    @Override
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   281
    public Object pop() {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   282
        if (length() == 0) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   283
            return ScriptRuntime.UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   284
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   285
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   286
        final int newLength = (int)length() - 1;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   287
        final Object elem = array[newLength];
18606
5704d7a4a0a8 8010697: DeletedArrayFilter seems to leak memory
jlaskey
parents: 16151
diff changeset
   288
        setEmpty(newLength);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   289
        setLength(newLength);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   290
        return elem;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   291
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   292
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   293
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   294
    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
   295
        final long start     = from < 0 ? from + length() : from;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   296
        final long newLength = to - start;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   297
        return new ObjectArrayData(Arrays.copyOfRange(array, (int)from, (int)to), (int)newLength);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   298
    }
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   299
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   300
    @Override
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   301
    public ArrayData push(final boolean strict, final Object item) {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   302
        final long      len     = length();
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   303
        final ArrayData newData = ensure(len);
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   304
        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
   305
            array[(int)len] = item;
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   306
            return this;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   307
        }
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   308
        return newData.set((int)len, item, strict);
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   309
    }
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   310
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   311
    @Override
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   312
    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
   313
        final long oldLength = length();
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   314
        final long newLength = oldLength - removed + added;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   315
        if (newLength > SparseArrayData.MAX_DENSE_LENGTH && newLength > array.length) {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   316
            throw new UnsupportedOperationException();
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   317
        }
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   318
        final ArrayData returnValue = removed == 0 ?
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   319
                EMPTY_ARRAY : new ObjectArrayData(Arrays.copyOfRange(array, start, start + removed), removed);
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   320
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   321
        if (newLength != oldLength) {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   322
            final Object[] newArray;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   323
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   324
            if (newLength > array.length) {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   325
                newArray = new Object[ArrayData.nextSize((int)newLength)];
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   326
                System.arraycopy(array, 0, newArray, 0, start);
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   327
            } else {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   328
                newArray = array;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   329
            }
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
            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
   332
            array = newArray;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   333
            setLength(newLength);
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   334
        }
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
        return returnValue;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   337
    }
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
   338
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
   339
    @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
   340
    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
   341
        final int   otherLength = (int)otherData.length();
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   342
        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
   343
        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
   344
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
   345
        final Object[] otherArray = ((ObjectArrayData)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
   346
        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
   347
        final Object[] newArray   = new Object[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
   348
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
   349
        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
   350
        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
   351
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
   352
        return new ObjectArrayData(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
   353
    }
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
   354
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
   355
    @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
   356
    public String toString() {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   357
        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
   358
        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
   359
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   360
}