nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/arrays/LongArrayData.java
author attila
Mon, 19 Jan 2015 16:07:16 +0100
changeset 28576 37b0c5bda589
parent 27525 e7d3c00b9fb8
child 28787 cab4b0497d27
permissions -rw-r--r--
8067880: Dead typed push methods in ArrayData Reviewed-by: hannesw, jlaskey
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
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24741
diff changeset
    28
import static jdk.nashorn.internal.codegen.CompilerConstants.specialCall;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    29
import static jdk.nashorn.internal.lookup.Lookup.MH;
28576
37b0c5bda589 8067880: Dead typed push methods in ArrayData
attila
parents: 27525
diff changeset
    30
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    31
import java.lang.invoke.MethodHandle;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    32
import java.lang.invoke.MethodHandles;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    33
import java.util.Arrays;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    34
import jdk.nashorn.internal.runtime.JSType;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    35
import jdk.nashorn.internal.runtime.ScriptRuntime;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    36
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    37
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    38
 * Implementation of {@link ArrayData} as soon as a long has been
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    39
 * written to the array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    40
 */
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
    41
final class LongArrayData extends ContinuousArrayData implements IntOrLongElements {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    42
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    43
     * The wrapped array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    44
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    45
    private long[] array;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    46
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    47
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    48
     * Constructor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    49
     * @param array an int array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
     * @param length a length, not necessarily array.length
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    51
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    52
    LongArrayData(final long array[], final int length) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    53
        super(length);
19236
73d242d205f9 8020132: Big object literal with numerical keys exceeds method size
hannesw
parents: 18879
diff changeset
    54
        assert array.length >= 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
    55
        this.array = array;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    56
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    57
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    58
    @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
    59
    public final Class<?> getElementType() {
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
    60
        return long.class;
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
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
    63
    @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
    64
    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
    65
        return Long.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
    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
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
    @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
    69
    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
    70
        return otherData instanceof IntElements ? 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
    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
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
    @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
    74
    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
    75
        return 2;
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
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
    @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
    79
    public LongArrayData copy() {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
    80
        return new LongArrayData(array.clone(), (int)length());
19236
73d242d205f9 8020132: Big object literal with numerical keys exceeds method size
hannesw
parents: 18879
diff changeset
    81
    }
73d242d205f9 8020132: Big object literal with numerical keys exceeds method size
hannesw
parents: 18879
diff changeset
    82
73d242d205f9 8020132: Big object literal with numerical keys exceeds method size
hannesw
parents: 18879
diff changeset
    83
    @Override
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    84
    public Object[] asObjectArray() {
27098
2875b30458d3 8060011: Concatenating an array and converting it to Java gives wrong result
hannesw
parents: 26886
diff changeset
    85
        return toObjectArray(true);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    86
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    87
27098
2875b30458d3 8060011: Concatenating an array and converting it to Java gives wrong result
hannesw
parents: 26886
diff changeset
    88
    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
    89
        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
    90
        final int len = (int)length();
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
    91
        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
    92
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
    93
        for (int index = 0; index < len; index++) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    94
            oarray[index] = Long.valueOf(array[index]);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    95
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    96
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    97
        return oarray;
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
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   101
    public Object asArrayOfType(final Class<?> componentType) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   102
        if (componentType == long.class) {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   103
            final int len = (int)length();
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   104
            return array.length == len ? array.clone() : Arrays.copyOf(array, len);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   105
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   106
        return super.asArrayOfType(componentType);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   107
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   108
27098
2875b30458d3 8060011: Concatenating an array and converting it to Java gives wrong result
hannesw
parents: 26886
diff changeset
   109
    private double[] toDoubleArray() {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   110
        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
   111
        final int len = (int)length();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   112
        final double[] darray = new double[array.length];
18879
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   113
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   114
        for (int index = 0; index < len; index++) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   115
            darray[index] = array[index];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   116
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   117
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   118
        return darray;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   119
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   120
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
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 ContinuousArrayData convert(final Class<?> type) {
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19472
diff changeset
   123
        if (type == Integer.class || type == Long.class) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   124
            return this;
18879
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   125
        }
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   126
        final int len = (int)length();
18879
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   127
        if (type == Double.class) {
27098
2875b30458d3 8060011: Concatenating an array and converting it to Java gives wrong result
hannesw
parents: 26886
diff changeset
   128
            return new NumberArrayData(toDoubleArray(), len);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   129
        }
27098
2875b30458d3 8060011: Concatenating an array and converting it to Java gives wrong result
hannesw
parents: 26886
diff changeset
   130
        return new ObjectArrayData(toObjectArray(false), len);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   131
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   132
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   133
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   134
    public void shiftLeft(final int by) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   135
        System.arraycopy(array, by, array, 0, array.length - by);
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 shiftRight(final int by) {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   140
        final ArrayData newData = ensure(by + length() - 1);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   141
        if (newData != this) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   142
            newData.shiftRight(by);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   143
            return newData;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   144
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   145
        System.arraycopy(array, 0, array, by, array.length - by);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   146
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   147
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   148
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   149
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   150
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   151
    public ArrayData ensure(final long safeIndex) {
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   152
        if (safeIndex >= SparseArrayData.MAX_DENSE_LENGTH) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   153
            return new SparseArrayData(this, safeIndex + 1);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   154
        }
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   155
        final int alen = array.length;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   156
        if (safeIndex >= alen) {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   157
            final int newLength = ArrayData.nextSize((int)safeIndex);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   158
            array = Arrays.copyOf(array, newLength);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   159
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   160
        setLength(safeIndex + 1);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   161
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   162
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   163
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   164
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   165
    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
   166
        Arrays.fill(array, (int)newLength, array.length, 0L);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   167
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   168
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   169
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   170
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   171
    public ArrayData set(final int index, final Object value, final boolean strict) {
18879
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   172
        if (value instanceof Long || value instanceof Integer) {
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   173
            return set(index, ((Number)value).longValue(), strict);
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   174
        } else if (value == ScriptRuntime.UNDEFINED) {
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   175
            return new UndefinedArrayFilter(this).set(index, value, strict);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   176
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   177
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   178
        final ArrayData newData = convert(value == null ? Object.class : value.getClass());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   179
        return newData.set(index, value, strict);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   180
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   181
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   182
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   183
    public ArrayData set(final int index, final int value, final boolean strict) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   184
        array[index] = value;
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   185
        setLength(Math.max(index + 1, length()));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   186
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   187
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   188
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   189
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   190
    public ArrayData set(final int index, final long value, final boolean strict) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   191
        array[index] = value;
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   192
        setLength(Math.max(index + 1, length()));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   193
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   194
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   195
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   196
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   197
    public ArrayData set(final int index, final double value, final boolean strict) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   198
        if (JSType.isRepresentableAsLong(value)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   199
            array[index] = (long)value;
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   200
            setLength(Math.max(index + 1, length()));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   201
            return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   202
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   203
        return convert(Double.class).set(index, value, strict);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   204
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   205
24741
4232289c3235 8040102: Remove all references to Unsafe and definition of anonymous clases from the code
lagergren
parents: 24720
diff changeset
   206
    private static final MethodHandle HAS_GET_ELEM = specialCall(MethodHandles.lookup(), LongArrayData.class, "getElem", long.class, int.class).methodHandle();
4232289c3235 8040102: Remove all references to Unsafe and definition of anonymous clases from the code
lagergren
parents: 24720
diff changeset
   207
    private static final MethodHandle SET_ELEM     = specialCall(MethodHandles.lookup(), LongArrayData.class, "setElem", void.class, int.class, long.class).methodHandle();
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   208
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   209
    @SuppressWarnings("unused")
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   210
    private long getElem(final int index) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   211
        if (has(index)) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   212
            return array[index];
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   213
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   214
        throw new ClassCastException();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   215
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   216
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   217
    @SuppressWarnings("unused")
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   218
    private void setElem(final int index, final long elem) {
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   219
        if (hasRoomFor(index)) {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   220
            array[index] = elem;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   221
            return;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   222
        }
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   223
        throw new ClassCastException();
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   224
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   225
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   226
    @Override
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   227
    public MethodHandle getElementGetter(final Class<?> returnType, final int programPoint) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   228
        if (returnType == int.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   229
            return null;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   230
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   231
        return getContinuousElementGetter(HAS_GET_ELEM, returnType, programPoint);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   232
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   233
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   234
    @Override
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   235
    public MethodHandle getElementSetter(final Class<?> elementType) {
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   236
        return elementType == int.class || elementType == long.class ? getContinuousElementSetter(MH.asType(SET_ELEM, SET_ELEM.type().changeParameterType(2, elementType)), elementType) : null;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   237
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   238
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   239
    @Override
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   240
    public int getInt(final int index) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   241
        return (int)array[index];
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 long getLong(final int index) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   246
        return array[index];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   247
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   248
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   249
    @Override
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   250
    public long getLongOptimistic(final int index, final int programPoint) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   251
        return array[index];
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   252
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   253
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   254
    @Override
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   255
    public double getDouble(final int index) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   256
        return array[index];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   257
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   258
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   259
    @Override
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   260
    public double getDoubleOptimistic(final int index, final int programPoint) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   261
        return array[index];
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   262
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   263
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   264
    @Override
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   265
    public Object getObject(final int index) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   266
        return array[index];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   267
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   268
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   269
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   270
    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
   271
        return 0 <= index && index < length();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   272
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   273
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   274
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   275
    public ArrayData delete(final int index) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   276
        return new DeletedRangeArrayFilter(this, index, index);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   277
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   278
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   279
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   280
    public ArrayData delete(final long fromIndex, final long toIndex) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   281
        return new DeletedRangeArrayFilter(this, fromIndex, toIndex);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   282
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   283
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   284
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   285
    public Object pop() {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   286
        final int len = (int)length();
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   287
        if (len == 0) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   288
            return ScriptRuntime.UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   289
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   290
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   291
        final int newLength = len - 1;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   292
        final long elem = array[newLength];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   293
        array[newLength] = 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   294
        setLength(newLength);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   295
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   296
        return elem;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   297
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   298
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   299
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   300
    public 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
   301
        final long start     = from < 0 ? from + length() : from;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   302
        final long newLength = to - start;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   303
        return new LongArrayData(Arrays.copyOfRange(array, (int)from, (int)to), (int)newLength);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   304
    }
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19472
diff changeset
   305
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19472
diff changeset
   306
    @Override
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19472
diff changeset
   307
    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
   308
        final long oldLength = length();
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19472
diff changeset
   309
        final long newLength = oldLength - removed + added;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19472
diff changeset
   310
        if (newLength > SparseArrayData.MAX_DENSE_LENGTH && newLength > array.length) {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19472
diff changeset
   311
            throw new UnsupportedOperationException();
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19472
diff changeset
   312
        }
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   313
        final ArrayData returnValue = removed == 0 ?
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19472
diff changeset
   314
                EMPTY_ARRAY : new LongArrayData(Arrays.copyOfRange(array, start, start + removed), removed);
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19472
diff changeset
   315
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19472
diff changeset
   316
        if (newLength != oldLength) {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19472
diff changeset
   317
            final long[] newArray;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19472
diff changeset
   318
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19472
diff changeset
   319
            if (newLength > array.length) {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19472
diff changeset
   320
                newArray = new long[ArrayData.nextSize((int)newLength)];
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19472
diff changeset
   321
                System.arraycopy(array, 0, newArray, 0, start);
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19472
diff changeset
   322
            } else {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19472
diff changeset
   323
                newArray = array;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19472
diff changeset
   324
            }
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19472
diff changeset
   325
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19472
diff changeset
   326
            System.arraycopy(array, start + removed, newArray, start + added, (int)(oldLength - start - removed));
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19472
diff changeset
   327
            array = newArray;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19472
diff changeset
   328
            setLength(newLength);
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19472
diff changeset
   329
        }
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19472
diff changeset
   330
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19472
diff changeset
   331
        return returnValue;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19472
diff changeset
   332
    }
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   333
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   334
    @Override
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   335
    public long fastPush(final int arg) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   336
        return fastPush((long)arg);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   337
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   338
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   339
    @Override
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   340
    public long fastPush(final long arg) {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   341
        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
   342
        if (len == array.length) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   343
            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
   344
        }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   345
        array[len] = arg;
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   346
        return increaseLength();
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   347
    }
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
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   350
    public long fastPopLong() {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   351
        if (length() == 0) {
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   352
            throw new ClassCastException(); //undefined result
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   353
        }
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   354
        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
   355
        final long elem = array[newLength];
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   356
        array[newLength] = 0;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   357
        return elem;
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
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 double fastPopDouble() {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   362
        return fastPopLong();
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   363
   }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   364
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   365
    @Override
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   366
    public Object fastPopObject() {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   367
        return fastPopLong();
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   368
    }
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
   369
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
   370
    @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
   371
    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
   372
        final int   otherLength = (int)otherData.length();
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   373
        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
   374
        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
   375
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
   376
        final long[] otherArray  = ((LongArrayData)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
   377
        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
   378
        final long[] newArray   = new long[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
   379
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
        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
   381
        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
   382
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
   383
        return new LongArrayData(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
   384
    }
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
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
    @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
   387
    public String toString() {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   388
        assert length() <= array.length : length() + " > " + array.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
   389
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
        final StringBuilder sb = new StringBuilder(getClass().getSimpleName()).
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
                append(": [");
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   392
        final int len = (int)length();
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   393
        for (int i = 0; i < len; i++) {
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
   394
            sb.append(array[i]).append('L'); //make sure L suffix is on elements, to discriminate this from IntArrayData.toString()
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27209
diff changeset
   395
            if (i + 1 < len) {
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
   396
                sb.append(", ");
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
            }
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
        }
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
   399
        sb.append(']');
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
   400
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
        return sb.toString();
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
   402
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   403
}