nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/arrays/NumberArrayData.java
author lagergren
Fri, 26 Sep 2014 18:47:20 +0200
changeset 26886 18c744ab4df2
parent 26768 751b0f427090
child 27098 2875b30458d3
permissions -rw-r--r--
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this Reviewed-by: attila, hannesw
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
16151
97c1e756ae1e 8005663: Update copyright year to 2013
jlaskey
parents: 16147
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     4
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    10
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    15
 * accompanied this code).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    16
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    20
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    23
 * questions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    24
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    25
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    26
package jdk.nashorn.internal.runtime.arrays;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
    28
import static jdk.nashorn.internal.codegen.CompilerConstants.specialCall;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    29
import static jdk.nashorn.internal.lookup.Lookup.MH;
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24741
diff changeset
    30
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
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;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    34
import jdk.nashorn.internal.codegen.types.Type;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
    35
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    36
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    37
 * Implementation of {@link ArrayData} as soon as a double has been
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    38
 * written to the array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    39
 */
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
    40
final class NumberArrayData extends ContinuousArrayData implements NumericElements {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    41
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    42
     * The wrapped array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    43
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    44
    private double[] 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
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    51
    NumberArrayData(final double array[], final int length) {
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: 18879
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: 18879
diff changeset
    55
    }
73d242d205f9 8020132: Big object literal with numerical keys exceeds method size
hannesw
parents: 18879
diff changeset
    56
73d242d205f9 8020132: Big object literal with numerical keys exceeds method size
hannesw
parents: 18879
diff changeset
    57
    @Override
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
    58
    public Class<?> getElementType() {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
    59
        return double.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
19236
73d242d205f9 8020132: Big object literal with numerical keys exceeds method size
hannesw
parents: 18879
diff changeset
    63
    public ArrayData copy() {
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
    64
        return new NumberArrayData(array.clone(), (int)length);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    65
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    66
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    68
    public Object[] asObjectArray() {
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
    69
        return toObjectArray(array, (int)length);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    70
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
    private static Object[] toObjectArray(final double[] array, final int length) {
18879
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
    73
        assert length <= array.length : "length exceeds internal array size";
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
    74
        final Object[] oarray = new Object[array.length];
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
    75
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
        for (int index = 0; index < length; index++) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    77
            oarray[index] = Double.valueOf(array[index]);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    78
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    79
        return oarray;
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 asArrayOfType(final Class<?> componentType) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    84
        if(componentType == double.class) {
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
    85
            return array.length == length ? array.clone() : Arrays.copyOf(array, (int)length);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    86
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    87
        return super.asArrayOfType(componentType);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    88
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    89
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    90
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    91
    public ArrayData convert(final Class<?> type) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    92
        if (type != Double.class && type != Integer.class && type != Long.class) {
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
    93
            final int len = (int)length;
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
    94
            return new ObjectArrayData(NumberArrayData.toObjectArray(array, len), len);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    95
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    96
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    97
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    98
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    99
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   100
    public void shiftLeft(final int by) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   101
        System.arraycopy(array, by, array, 0, array.length - by);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   102
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   103
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   104
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   105
    public ArrayData shiftRight(final int by) {
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   106
        final ArrayData newData = ensure(by + length - 1);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   107
        if (newData != this) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   108
            newData.shiftRight(by);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   109
            return newData;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   110
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   111
        System.arraycopy(array, 0, array, by, array.length - by);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   112
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   113
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   114
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   115
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   116
    public ArrayData ensure(final long safeIndex) {
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   117
        if (safeIndex >= SparseArrayData.MAX_DENSE_LENGTH) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   118
            return new SparseArrayData(this, safeIndex + 1);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   119
        }
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   120
        final int alen = array.length;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   121
        if (safeIndex >= alen) {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   122
            final int newLength = ArrayData.nextSize((int)safeIndex);
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   123
            array = Arrays.copyOf(array, newLength); //todo fill with nan or never accessed?
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   124
        }
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   125
        setLength(safeIndex + 1);
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   126
        return this;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   127
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   128
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   129
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   130
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   131
    public ArrayData shrink(final long newLength) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   132
        Arrays.fill(array, (int) newLength, array.length, 0.0);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   133
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   134
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   135
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   136
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   137
    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
   138
        if (value instanceof Double || value instanceof Integer || value instanceof Long) {
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   139
            return set(index, ((Number)value).doubleValue(), strict);
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   140
        } else if (value == UNDEFINED) {
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   141
            return new UndefinedArrayFilter(this).set(index, value, strict);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   142
        }
18879
382986b34105 8020283: Don't use exceptions for widening of ArrayData
hannesw
parents: 16151
diff changeset
   143
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   144
        final ArrayData newData = convert(value == null ? Object.class : value.getClass());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   145
        return newData.set(index, value, strict);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   146
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   148
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   149
    public ArrayData set(final int index, final int value, final boolean strict) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   150
        array[index] = value;
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   151
        setLength(Math.max(index + 1, length));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   152
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   153
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   154
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   155
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   156
    public ArrayData set(final int index, final long value, final boolean strict) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   157
        array[index] = value;
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   158
        setLength(Math.max(index + 1, length));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   159
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   160
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   161
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   162
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   163
    public ArrayData set(final int index, final double value, final boolean strict) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   164
        array[index] = value;
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   165
        setLength(Math.max(index + 1, length));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   166
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   167
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   168
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   169
    @Override
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   170
    public Type getOptimisticType() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   171
        return Type.NUMBER;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   172
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   173
24741
4232289c3235 8040102: Remove all references to Unsafe and definition of anonymous clases from the code
lagergren
parents: 24720
diff changeset
   174
    private static final MethodHandle HAS_GET_ELEM = specialCall(MethodHandles.lookup(), NumberArrayData.class, "getElem", double.class, int.class).methodHandle();
4232289c3235 8040102: Remove all references to Unsafe and definition of anonymous clases from the code
lagergren
parents: 24720
diff changeset
   175
    private static final MethodHandle SET_ELEM     = specialCall(MethodHandles.lookup(), NumberArrayData.class, "setElem", void.class, int.class, double.class).methodHandle();
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   176
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   177
    @SuppressWarnings("unused")
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   178
    private double getElem(final int index) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   179
        if (has(index)) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   180
            return array[index];
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   181
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   182
        throw new ClassCastException();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   183
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   184
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   185
    @SuppressWarnings("unused")
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   186
    private void setElem(final int index, final double elem) {
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   187
        if (hasRoomFor(index)) {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   188
            array[index] = elem;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   189
            return;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   190
        }
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   191
        throw new ClassCastException();
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   192
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   193
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   194
    @Override
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   195
    public MethodHandle getElementGetter(final Class<?> returnType, final int programPoint) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   196
        if (returnType == int.class || returnType == long.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   197
            return null;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   198
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   199
        return getContinuousElementGetter(HAS_GET_ELEM, returnType, programPoint);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   200
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   201
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   202
    @Override
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   203
    public MethodHandle getElementSetter(final Class<?> elementType) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   204
        return elementType.isPrimitive() ? getContinuousElementSetter(MH.asType(SET_ELEM, SET_ELEM.type().changeParameterType(2, elementType)), elementType) : null;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   205
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   206
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   207
    @Override
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   208
    public int getInt(final int index) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   209
        return (int)array[index];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   210
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   211
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   212
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   213
    public long getLong(final int index) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   214
        return (long)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 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
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   223
    public double getDoubleOptimistic(final int index, final int programPoint) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   224
        return array[index];
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   225
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   226
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21438
diff changeset
   227
    @Override
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   228
    public Object getObject(final int index) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   229
        return array[index];
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 boolean has(final int index) {
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   234
        return 0 <= index && index < length;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   235
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   236
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   237
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   238
    public ArrayData delete(final int index) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   239
        return new DeletedRangeArrayFilter(this, index, index);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   240
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   241
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   242
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   243
    public ArrayData delete(final long fromIndex, final long toIndex) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   244
        return new DeletedRangeArrayFilter(this, fromIndex, toIndex);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   245
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   246
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   247
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   248
    public Object pop() {
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   249
        if (length == 0) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   250
            return UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   251
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   252
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   253
        final int newLength = (int)length - 1;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   254
        final double elem = array[newLength];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   255
        array[newLength] = 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   256
        setLength(newLength);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   257
        return elem;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   258
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   259
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   260
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   261
    public ArrayData slice(final long from, final long to) {
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   262
        final long start     = from < 0 ? from + length : from;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   263
        final long newLength = to - start;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   264
        return new NumberArrayData(Arrays.copyOfRange(array, (int)from, (int)to), (int)newLength);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   265
    }
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   266
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   267
    @Override
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   268
    public final ArrayData push(final boolean strict, final double item) {
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   269
        final long      len     = length;
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   270
        final ArrayData newData = ensure(len);
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   271
        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
   272
            array[(int)len] = item;
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   273
            return this;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   274
        }
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   275
        return newData.set((int)len, item, strict);
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   276
    }
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   277
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   278
    @Override
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   279
    public ArrayData fastSplice(final int start, final int removed, final int added) throws UnsupportedOperationException {
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   280
        final long oldLength = length;
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   281
        final long newLength = oldLength - removed + added;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   282
        if (newLength > SparseArrayData.MAX_DENSE_LENGTH && newLength > array.length) {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   283
            throw new UnsupportedOperationException();
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   284
        }
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   285
        final ArrayData returnValue = removed == 0 ?
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   286
                EMPTY_ARRAY : new NumberArrayData(Arrays.copyOfRange(array, start, start + removed), removed);
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   287
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   288
        if (newLength != oldLength) {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   289
            final double[] newArray;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   290
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   291
            if (newLength > array.length) {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   292
                newArray = new double[ArrayData.nextSize((int)newLength)];
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   293
                System.arraycopy(array, 0, newArray, 0, start);
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   294
            } else {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   295
                newArray = array;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   296
            }
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   297
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   298
            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
   299
            array = newArray;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   300
            setLength(newLength);
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   301
        }
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   302
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   303
        return returnValue;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 19236
diff changeset
   304
    }
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   305
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   306
    @Override
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   307
    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
   308
        return fastPush((double)arg);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   309
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   310
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   311
    @Override
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   312
    public long fastPush(final long arg) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   313
        return fastPush((double)arg);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   314
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   315
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   316
    @Override
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   317
    public long fastPush(final double arg) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   318
        final int len = (int)length;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   319
        if (len == array.length) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   320
           //note that fastpush never creates spares arrays, there is nothing to gain by that - it will just use even more memory
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   321
           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
   322
        }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   323
        array[len] = arg;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   324
        return ++length;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   325
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   326
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   327
    @Override
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   328
    public double fastPopDouble() {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   329
        if (length == 0) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   330
            throw new ClassCastException();
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   331
        }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   332
        final int newLength = (int)--length;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   333
        final double elem = array[newLength];
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   334
        array[newLength] = 0;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   335
        return elem;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   336
    }
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
    @Override
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   339
    public Object fastPopObject() {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   340
        return fastPopDouble();
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   341
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   342
}