nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeArray.java
author hannesw
Fri, 25 Nov 2016 14:20:24 +0100
changeset 42376 8604f1a50c30
parent 41619 dc96f5cd1a9c
child 42378 88f74d63d093
permissions -rw-r--r--
8170322: Specialized functions convert booleans to numbers Reviewed-by: jlaskey, attila
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
35407
204abe4d8cbc 8147591: Revisit Collection.toArray(new T[size]) calls in nashorn and dynalink code
mhaupt
parents: 35326
diff changeset
     2
 * Copyright (c) 2010, 2016, 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.objects;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    28
import static jdk.nashorn.internal.runtime.ECMAErrors.rangeError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    29
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    30
import static jdk.nashorn.internal.runtime.PropertyDescriptor.VALUE;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    31
import static jdk.nashorn.internal.runtime.PropertyDescriptor.WRITABLE;
24769
attila
parents: 24759 23763
diff changeset
    32
import static jdk.nashorn.internal.runtime.arrays.ArrayIndex.isValidArrayIndex;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    33
import static jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator.arrayLikeIterator;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    34
import static jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator.reverseArrayLikeIterator;
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 25865
diff changeset
    35
import static jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor.CALLSITE_STRICT;
27528
4a61cb4dfc6c 8064707: Remove NativeArray link logic fields
attila
parents: 27525
diff changeset
    36
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    37
import java.lang.invoke.MethodHandle;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    38
import java.util.ArrayList;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    39
import java.util.Arrays;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    40
import java.util.Collections;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    41
import java.util.Comparator;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    42
import java.util.Iterator;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    43
import java.util.List;
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
    44
import java.util.concurrent.Callable;
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33690
diff changeset
    45
import jdk.dynalink.CallSiteDescriptor;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33690
diff changeset
    46
import jdk.dynalink.linker.GuardedInvocation;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33690
diff changeset
    47
import jdk.dynalink.linker.LinkRequest;
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19630
diff changeset
    48
import jdk.nashorn.api.scripting.JSObject;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    49
import jdk.nashorn.internal.objects.annotations.Attribute;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
import jdk.nashorn.internal.objects.annotations.Constructor;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    51
import jdk.nashorn.internal.objects.annotations.Function;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    52
import jdk.nashorn.internal.objects.annotations.Getter;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    53
import jdk.nashorn.internal.objects.annotations.ScriptClass;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    54
import jdk.nashorn.internal.objects.annotations.Setter;
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
    55
import jdk.nashorn.internal.objects.annotations.SpecializedFunction;
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
    56
import jdk.nashorn.internal.objects.annotations.SpecializedFunction.LinkLogic;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    57
import jdk.nashorn.internal.objects.annotations.Where;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
    58
import jdk.nashorn.internal.runtime.Context;
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
    59
import jdk.nashorn.internal.runtime.Debug;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    60
import jdk.nashorn.internal.runtime.JSType;
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
    61
import jdk.nashorn.internal.runtime.OptimisticBuiltins;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    62
import jdk.nashorn.internal.runtime.PropertyDescriptor;
18618
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18606
diff changeset
    63
import jdk.nashorn.internal.runtime.PropertyMap;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    64
import jdk.nashorn.internal.runtime.ScriptObject;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    65
import jdk.nashorn.internal.runtime.ScriptRuntime;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    66
import jdk.nashorn.internal.runtime.Undefined;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
import jdk.nashorn.internal.runtime.arrays.ArrayData;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    68
import jdk.nashorn.internal.runtime.arrays.ArrayIndex;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    69
import jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator;
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
    70
import jdk.nashorn.internal.runtime.arrays.ContinuousArrayData;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
    71
import jdk.nashorn.internal.runtime.arrays.IntElements;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
import jdk.nashorn.internal.runtime.arrays.IteratorAction;
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
    73
import jdk.nashorn.internal.runtime.arrays.NumericElements;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    74
import jdk.nashorn.internal.runtime.linker.Bootstrap;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    75
import jdk.nashorn.internal.runtime.linker.InvokeByName;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    77
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    78
 * Runtime representation of a JavaScript array. NativeArray only holds numeric
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    79
 * keyed values. All other values are stored in spill.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    80
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    81
@ScriptClass("Array")
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
    82
public final class NativeArray extends ScriptObject implements OptimisticBuiltins {
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
    83
    private static final Object JOIN                     = new Object();
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
    84
    private static final Object EVERY_CALLBACK_INVOKER   = new Object();
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
    85
    private static final Object SOME_CALLBACK_INVOKER    = new Object();
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
    86
    private static final Object FOREACH_CALLBACK_INVOKER = new Object();
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
    87
    private static final Object MAP_CALLBACK_INVOKER     = new Object();
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
    88
    private static final Object FILTER_CALLBACK_INVOKER  = new Object();
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
    89
    private static final Object REDUCE_CALLBACK_INVOKER  = new Object();
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
    90
    private static final Object CALL_CMP                 = new Object();
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
    91
    private static final Object TO_LOCALE_STRING         = new Object();
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
    92
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
    93
    /*
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
    94
     * Constructors.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
    95
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
    96
    NativeArray() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
    97
        this(ArrayData.initialArray());
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
    98
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
    99
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   100
    NativeArray(final long length) {
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   101
        this(ArrayData.allocate(length));
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   102
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   103
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   104
    NativeArray(final int[] array) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   105
        this(ArrayData.allocate(array));
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   106
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   107
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   108
    NativeArray(final double[] array) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   109
        this(ArrayData.allocate(array));
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   110
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   111
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   112
    NativeArray(final long[] array) {
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   113
        this(ArrayData.allocate(array.length));
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   114
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   115
        ArrayData arrayData = this.getArray();
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   116
        Class<?> widest = int.class;
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   117
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   118
        for (int index = 0; index < array.length; index++) {
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   119
            final long value = array[index];
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   120
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   121
            if (widest == int.class && JSType.isRepresentableAsInt(value)) {
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   122
                arrayData = arrayData.set(index, (int) value, false);
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   123
            } else if (widest != Object.class && JSType.isRepresentableAsDouble(value)) {
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   124
                arrayData = arrayData.set(index, (double) value, false);
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   125
                widest = double.class;
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   126
            } else {
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   127
                arrayData = arrayData.set(index, (Object) value, false);
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   128
                widest = Object.class;
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   129
            }
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   130
        }
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   131
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   132
        this.setArray(arrayData);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   133
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   134
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   135
    NativeArray(final Object[] array) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   136
        this(ArrayData.allocate(array.length));
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   137
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   138
        ArrayData arrayData = this.getArray();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   139
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   140
        for (int index = 0; index < array.length; index++) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   141
            final Object value = array[index];
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   142
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   143
            if (value == ScriptRuntime.EMPTY) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   144
                arrayData = arrayData.delete(index);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   145
            } else {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   146
                arrayData = arrayData.set(index, value, false);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   147
            }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   148
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   149
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   150
        this.setArray(arrayData);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   151
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   152
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   153
    NativeArray(final ArrayData arrayData) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   154
        this(arrayData, Global.instance());
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   155
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   156
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   157
    NativeArray(final ArrayData arrayData, final Global global) {
24727
attila
parents: 24720 23375
diff changeset
   158
        super(global.getArrayPrototype(), $nasgenmap$);
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
   159
        setArray(arrayData);
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
   160
        setIsArray();
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   161
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   162
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   163
    @Override
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   164
    protected GuardedInvocation findGetIndexMethod(final CallSiteDescriptor desc, final LinkRequest request) {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   165
        final GuardedInvocation inv = getArray().findFastGetIndexMethod(getArray().getClass(), desc, request);
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   166
        if (inv != null) {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   167
            return inv;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   168
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   169
        return super.findGetIndexMethod(desc, request);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   170
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   171
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   172
    @Override
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   173
    protected GuardedInvocation findSetIndexMethod(final CallSiteDescriptor desc, final LinkRequest request) {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   174
        final GuardedInvocation inv = getArray().findFastSetIndexMethod(getArray().getClass(), desc, request);
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   175
        if (inv != null) {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   176
            return inv;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   177
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   178
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   179
        return super.findSetIndexMethod(desc, request);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   180
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   181
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   182
    private static InvokeByName getJOIN() {
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   183
        return Global.instance().getInvokeByName(JOIN,
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   184
                new Callable<InvokeByName>() {
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   185
                    @Override
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   186
                    public InvokeByName call() {
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   187
                        return new InvokeByName("join", ScriptObject.class);
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   188
                    }
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   189
                });
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   190
    }
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   191
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   192
    private static MethodHandle createIteratorCallbackInvoker(final Object key, final Class<?> rtype) {
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   193
        return Global.instance().getDynamicInvoker(key,
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   194
            new Callable<MethodHandle>() {
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   195
                @Override
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   196
                public MethodHandle call() {
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 31549
diff changeset
   197
                    return Bootstrap.createDynamicCallInvoker(rtype, Object.class, Object.class, Object.class,
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   198
                        double.class, Object.class);
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   199
                }
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   200
            });
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   201
    }
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   202
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   203
    private static MethodHandle getEVERY_CALLBACK_INVOKER() {
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   204
        return createIteratorCallbackInvoker(EVERY_CALLBACK_INVOKER, boolean.class);
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   205
    }
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   206
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   207
    private static MethodHandle getSOME_CALLBACK_INVOKER() {
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   208
        return createIteratorCallbackInvoker(SOME_CALLBACK_INVOKER, boolean.class);
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   209
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   210
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   211
    private static MethodHandle getFOREACH_CALLBACK_INVOKER() {
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   212
        return createIteratorCallbackInvoker(FOREACH_CALLBACK_INVOKER, void.class);
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   213
    }
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   214
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   215
    private static MethodHandle getMAP_CALLBACK_INVOKER() {
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   216
        return createIteratorCallbackInvoker(MAP_CALLBACK_INVOKER, Object.class);
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   217
    }
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   218
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   219
    private static MethodHandle getFILTER_CALLBACK_INVOKER() {
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   220
        return createIteratorCallbackInvoker(FILTER_CALLBACK_INVOKER, boolean.class);
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   221
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   222
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   223
    private static MethodHandle getREDUCE_CALLBACK_INVOKER() {
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   224
        return Global.instance().getDynamicInvoker(REDUCE_CALLBACK_INVOKER,
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   225
                new Callable<MethodHandle>() {
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   226
                    @Override
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   227
                    public MethodHandle call() {
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 31549
diff changeset
   228
                        return Bootstrap.createDynamicCallInvoker(Object.class, Object.class,
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   229
                             Undefined.class, Object.class, Object.class, double.class, Object.class);
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   230
                    }
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   231
                });
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   232
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   233
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   234
    private static MethodHandle getCALL_CMP() {
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   235
        return Global.instance().getDynamicInvoker(CALL_CMP,
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   236
                new Callable<MethodHandle>() {
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   237
                    @Override
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   238
                    public MethodHandle call() {
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 31549
diff changeset
   239
                        return Bootstrap.createDynamicCallInvoker(double.class,
38595
6eceed1919cf 8157680: Callback parameter of any JS builtin implementation should accept any Callable
sundar
parents: 36023
diff changeset
   240
                            Object.class, Object.class, Object.class, Object.class);
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   241
                    }
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   242
                });
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   243
    }
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   244
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   245
    private static InvokeByName getTO_LOCALE_STRING() {
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   246
        return Global.instance().getInvokeByName(TO_LOCALE_STRING,
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   247
                new Callable<InvokeByName>() {
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   248
                    @Override
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   249
                    public InvokeByName call() {
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   250
                        return new InvokeByName("toLocaleString", ScriptObject.class, String.class);
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   251
                    }
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   252
                });
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   253
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   254
18618
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18606
diff changeset
   255
    // initialized by nasgen
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18606
diff changeset
   256
    private static PropertyMap $nasgenmap$;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   257
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   258
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   259
    public String getClassName() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   260
        return "Array";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   261
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   262
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   263
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   264
    public Object getLength() {
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   265
        final long length = getArray().length();
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   266
        assert length >= 0L;
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   267
        if (length <= Integer.MAX_VALUE) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   268
            return (int)length;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   269
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   270
        return length;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   271
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   272
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   273
    private boolean defineLength(final long oldLen, final PropertyDescriptor oldLenDesc, final PropertyDescriptor desc, final boolean reject) {
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   274
        // Step 3a
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   275
        if (!desc.has(VALUE)) {
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   276
            return super.defineOwnProperty("length", desc, reject);
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   277
        }
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   278
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   279
        // Step 3b
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   280
        final PropertyDescriptor newLenDesc = desc;
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   281
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   282
        // Step 3c and 3d - get new length and convert to long
28124
7737b6eae7cb 8066215: Fuzzing bug: length valueOf bug
hannesw
parents: 27528
diff changeset
   283
        final long newLen = NativeArray.validLength(newLenDesc.getValue());
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   284
34974
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 34732
diff changeset
   285
        // Step 3e - note that we need to convert to int or double as long is not considered a JS number type anymore
35326
d3ec391b995e 8147845: Varargs Array functions still leaking longs
hannesw
parents: 34974
diff changeset
   286
        newLenDesc.setValue(JSType.toNarrowestNumber(newLen));
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   287
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   288
        // Step 3f
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   289
        // increasing array length - just need to set new length value (and attributes if any) and return
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   290
        if (newLen >= oldLen) {
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   291
            return super.defineOwnProperty("length", newLenDesc, reject);
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   292
        }
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   293
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   294
        // Step 3g
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   295
        if (!oldLenDesc.isWritable()) {
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   296
            if (reject) {
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   297
                throw typeError("property.not.writable", "length", ScriptRuntime.safeToString(this));
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   298
            }
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   299
            return false;
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   300
        }
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   301
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   302
        // Step 3h and 3i
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   303
        final boolean newWritable = !newLenDesc.has(WRITABLE) || newLenDesc.isWritable();
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   304
        if (!newWritable) {
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   305
            newLenDesc.setWritable(true);
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   306
        }
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   307
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   308
        // Step 3j and 3k
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   309
        final boolean succeeded = super.defineOwnProperty("length", newLenDesc, reject);
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   310
        if (!succeeded) {
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   311
            return false;
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   312
        }
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   313
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   314
        // Step 3l
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   315
        // make sure that length is set till the point we can delete the old elements
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   316
        long o = oldLen;
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   317
        while (newLen < o) {
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   318
            o--;
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   319
            final boolean deleteSucceeded = delete(o, false);
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   320
            if (!deleteSucceeded) {
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   321
                newLenDesc.setValue(o + 1);
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   322
                if (!newWritable) {
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   323
                    newLenDesc.setWritable(false);
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   324
                }
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   325
                super.defineOwnProperty("length", newLenDesc, false);
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   326
                if (reject) {
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   327
                    throw typeError("property.not.writable", "length", ScriptRuntime.safeToString(this));
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   328
                }
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   329
                return false;
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   330
            }
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   331
        }
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   332
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   333
        // Step 3m
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   334
        if (!newWritable) {
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   335
            // make 'length' property not writable
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   336
            final ScriptObject newDesc = Global.newEmptyInstance();
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   337
            newDesc.set(WRITABLE, false, 0);
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   338
            return super.defineOwnProperty("length", newDesc, false);
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   339
        }
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   340
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   341
        return true;
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   342
    }
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   343
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   344
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   345
     * ECMA 15.4.5.1 [[DefineOwnProperty]] ( P, Desc, Throw )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   346
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   347
    @Override
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 33343
diff changeset
   348
    public boolean defineOwnProperty(final Object key, final Object propertyDesc, final boolean reject) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   349
        final PropertyDescriptor desc = toPropertyDescriptor(Global.instance(), propertyDesc);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   350
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   351
        // never be undefined as "length" is always defined and can't be deleted for arrays
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   352
        // Step 1
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   353
        final PropertyDescriptor oldLenDesc = (PropertyDescriptor) super.getOwnPropertyDescriptor("length");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   354
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   355
        // Step 2
28124
7737b6eae7cb 8066215: Fuzzing bug: length valueOf bug
hannesw
parents: 27528
diff changeset
   356
        // get old length and convert to long. Always a Long/Uint32 but we take the safe road.
7737b6eae7cb 8066215: Fuzzing bug: length valueOf bug
hannesw
parents: 27528
diff changeset
   357
        final long oldLen = JSType.toUint32(oldLenDesc.getValue());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   358
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   359
        // Step 3
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   360
        if ("length".equals(key)) {
18328
ebd24057f163 8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents: 18318
diff changeset
   361
            // check for length being made non-writable
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   362
            final boolean result = defineLength(oldLen, oldLenDesc, desc, reject);
18328
ebd24057f163 8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents: 18318
diff changeset
   363
            if (desc.has(WRITABLE) && !desc.isWritable()) {
ebd24057f163 8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents: 18318
diff changeset
   364
                setIsLengthNotWritable();
ebd24057f163 8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents: 18318
diff changeset
   365
            }
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   366
            return result;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   367
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   368
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   369
        // Step 4a
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17983
diff changeset
   370
        final int index = ArrayIndex.getArrayIndex(key);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   371
        if (ArrayIndex.isValidArrayIndex(index)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   372
            final long longIndex = ArrayIndex.toLongIndex(index);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   373
            // Step 4b
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   374
            // setting an element beyond current length, but 'length' is not writable
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   375
            if (longIndex >= oldLen && !oldLenDesc.isWritable()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   376
                if (reject) {
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16226
diff changeset
   377
                    throw typeError("property.not.writable", Long.toString(longIndex), ScriptRuntime.safeToString(this));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   378
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   379
                return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   380
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   381
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   382
            // Step 4c
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   383
            // set the new array element
16773
ed65c8a9ae7c 8011421: When using Object.defineProperty on arrays, PropertyDescriptor's property accessors are invoked multiple times
sundar
parents: 16769
diff changeset
   384
            final boolean succeeded = super.defineOwnProperty(key, desc, false);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   385
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   386
            // Step 4d
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   387
            if (!succeeded) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   388
                if (reject) {
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 33343
diff changeset
   389
                    throw typeError("cant.redefine.property", key.toString(), ScriptRuntime.safeToString(this));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   390
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   391
                return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   392
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   393
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   394
            // Step 4e -- adjust new length based on new element index that is set
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   395
            if (longIndex >= oldLen) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   396
                oldLenDesc.setValue(longIndex + 1);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   397
                super.defineOwnProperty("length", oldLenDesc, false);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   398
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   399
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   400
            // Step 4f
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   401
            return true;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   402
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   403
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   404
        // not an index property
16773
ed65c8a9ae7c 8011421: When using Object.defineProperty on arrays, PropertyDescriptor's property accessors are invoked multiple times
sundar
parents: 16769
diff changeset
   405
        return super.defineOwnProperty(key, desc, reject);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   406
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   407
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   408
    /**
23761
5f351bdb2317 8037562: Nashorn: JSON.parse comes up with nonexistent entries if there are gaps between the keys
sundar
parents: 23375
diff changeset
   409
     * Spec. mentions use of [[DefineOwnProperty]] for indexed properties in
5f351bdb2317 8037562: Nashorn: JSON.parse comes up with nonexistent entries if there are gaps between the keys
sundar
parents: 23375
diff changeset
   410
     * certain places (eg. Array.prototype.map, filter). We can not use ScriptObject.set
5f351bdb2317 8037562: Nashorn: JSON.parse comes up with nonexistent entries if there are gaps between the keys
sundar
parents: 23375
diff changeset
   411
     * method in such cases. This is because set method uses inherited setters (if any)
5f351bdb2317 8037562: Nashorn: JSON.parse comes up with nonexistent entries if there are gaps between the keys
sundar
parents: 23375
diff changeset
   412
     * from any object in proto chain such as Array.prototype, Object.prototype.
5f351bdb2317 8037562: Nashorn: JSON.parse comes up with nonexistent entries if there are gaps between the keys
sundar
parents: 23375
diff changeset
   413
     * This method directly sets a particular element value in the current object.
5f351bdb2317 8037562: Nashorn: JSON.parse comes up with nonexistent entries if there are gaps between the keys
sundar
parents: 23375
diff changeset
   414
     *
5f351bdb2317 8037562: Nashorn: JSON.parse comes up with nonexistent entries if there are gaps between the keys
sundar
parents: 23375
diff changeset
   415
     * @param index key for property
5f351bdb2317 8037562: Nashorn: JSON.parse comes up with nonexistent entries if there are gaps between the keys
sundar
parents: 23375
diff changeset
   416
     * @param value value to define
5f351bdb2317 8037562: Nashorn: JSON.parse comes up with nonexistent entries if there are gaps between the keys
sundar
parents: 23375
diff changeset
   417
     */
5f351bdb2317 8037562: Nashorn: JSON.parse comes up with nonexistent entries if there are gaps between the keys
sundar
parents: 23375
diff changeset
   418
    @Override
5f351bdb2317 8037562: Nashorn: JSON.parse comes up with nonexistent entries if there are gaps between the keys
sundar
parents: 23375
diff changeset
   419
    public final void defineOwnProperty(final int index, final Object value) {
5f351bdb2317 8037562: Nashorn: JSON.parse comes up with nonexistent entries if there are gaps between the keys
sundar
parents: 23375
diff changeset
   420
        assert isValidArrayIndex(index) : "invalid array index";
5f351bdb2317 8037562: Nashorn: JSON.parse comes up with nonexistent entries if there are gaps between the keys
sundar
parents: 23375
diff changeset
   421
        final long longIndex = ArrayIndex.toLongIndex(index);
5f351bdb2317 8037562: Nashorn: JSON.parse comes up with nonexistent entries if there are gaps between the keys
sundar
parents: 23375
diff changeset
   422
        if (longIndex >= getArray().length()) {
5f351bdb2317 8037562: Nashorn: JSON.parse comes up with nonexistent entries if there are gaps between the keys
sundar
parents: 23375
diff changeset
   423
            // make array big enough to hold..
5f351bdb2317 8037562: Nashorn: JSON.parse comes up with nonexistent entries if there are gaps between the keys
sundar
parents: 23375
diff changeset
   424
            setArray(getArray().ensure(longIndex));
5f351bdb2317 8037562: Nashorn: JSON.parse comes up with nonexistent entries if there are gaps between the keys
sundar
parents: 23375
diff changeset
   425
        }
5f351bdb2317 8037562: Nashorn: JSON.parse comes up with nonexistent entries if there are gaps between the keys
sundar
parents: 23375
diff changeset
   426
        setArray(getArray().set(index, value, false));
5f351bdb2317 8037562: Nashorn: JSON.parse comes up with nonexistent entries if there are gaps between the keys
sundar
parents: 23375
diff changeset
   427
    }
5f351bdb2317 8037562: Nashorn: JSON.parse comes up with nonexistent entries if there are gaps between the keys
sundar
parents: 23375
diff changeset
   428
5f351bdb2317 8037562: Nashorn: JSON.parse comes up with nonexistent entries if there are gaps between the keys
sundar
parents: 23375
diff changeset
   429
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   430
     * Return the array contents upcasted as an ObjectArray, regardless of
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   431
     * representation
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   432
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   433
     * @return an object array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   434
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   435
    public Object[] asObjectArray() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   436
        return getArray().asObjectArray();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   437
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   438
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   439
    @Override
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   440
    public void setIsLengthNotWritable() {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   441
        super.setIsLengthNotWritable();
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
   442
        setArray(ArrayData.setIsLengthNotWritable(getArray()));
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   443
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   444
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   445
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   446
     * ECMA 15.4.3.2 Array.isArray ( arg )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   447
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   448
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   449
     * @param arg  argument - object to check
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   450
     * @return true if argument is an array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   451
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   452
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23761
diff changeset
   453
    public static boolean isArray(final Object self, final Object arg) {
21439
31c57355a4a7 8026805: Array.prototype.length doesn't work as expected
hannesw
parents: 21438
diff changeset
   454
        return isArray(arg) || (arg instanceof JSObject && ((JSObject)arg).isArray());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   455
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   456
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   457
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   458
     * Length getter
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   459
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   460
     * @return the length of the object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   461
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   462
    @Getter(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_CONFIGURABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   463
    public static Object length(final Object self) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   464
        if (isArray(self)) {
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   465
            final long length = ((ScriptObject) self).getArray().length();
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   466
            assert length >= 0L;
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   467
            // Cast to the narrowest supported numeric type to help optimistic type calculator
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   468
            if (length <= Integer.MAX_VALUE) {
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   469
                return (int) length;
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   470
            }
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   471
            return (double) length;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   472
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   473
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   474
        return 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   475
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   476
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   477
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   478
     * Length setter
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   479
     * @param self   self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   480
     * @param length new length property
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   481
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   482
    @Setter(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_CONFIGURABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   483
    public static void length(final Object self, final Object length) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   484
        if (isArray(self)) {
28124
7737b6eae7cb 8066215: Fuzzing bug: length valueOf bug
hannesw
parents: 27528
diff changeset
   485
            ((ScriptObject)self).setLength(validLength(length));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   486
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   487
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   488
21439
31c57355a4a7 8026805: Array.prototype.length doesn't work as expected
hannesw
parents: 21438
diff changeset
   489
    /**
31c57355a4a7 8026805: Array.prototype.length doesn't work as expected
hannesw
parents: 21438
diff changeset
   490
     * Prototype length getter
31c57355a4a7 8026805: Array.prototype.length doesn't work as expected
hannesw
parents: 21438
diff changeset
   491
     * @param self self reference
31c57355a4a7 8026805: Array.prototype.length doesn't work as expected
hannesw
parents: 21438
diff changeset
   492
     * @return the length of the object
31c57355a4a7 8026805: Array.prototype.length doesn't work as expected
hannesw
parents: 21438
diff changeset
   493
     */
31c57355a4a7 8026805: Array.prototype.length doesn't work as expected
hannesw
parents: 21438
diff changeset
   494
    @Getter(name = "length", where = Where.PROTOTYPE, attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_CONFIGURABLE)
31c57355a4a7 8026805: Array.prototype.length doesn't work as expected
hannesw
parents: 21438
diff changeset
   495
    public static Object getProtoLength(final Object self) {
31c57355a4a7 8026805: Array.prototype.length doesn't work as expected
hannesw
parents: 21438
diff changeset
   496
        return length(self);  // Same as instance getter but we can't make nasgen use the same method for prototype
31c57355a4a7 8026805: Array.prototype.length doesn't work as expected
hannesw
parents: 21438
diff changeset
   497
    }
31c57355a4a7 8026805: Array.prototype.length doesn't work as expected
hannesw
parents: 21438
diff changeset
   498
31c57355a4a7 8026805: Array.prototype.length doesn't work as expected
hannesw
parents: 21438
diff changeset
   499
    /**
31c57355a4a7 8026805: Array.prototype.length doesn't work as expected
hannesw
parents: 21438
diff changeset
   500
     * Prototype length setter
31c57355a4a7 8026805: Array.prototype.length doesn't work as expected
hannesw
parents: 21438
diff changeset
   501
     * @param self   self reference
31c57355a4a7 8026805: Array.prototype.length doesn't work as expected
hannesw
parents: 21438
diff changeset
   502
     * @param length new length property
31c57355a4a7 8026805: Array.prototype.length doesn't work as expected
hannesw
parents: 21438
diff changeset
   503
     */
31c57355a4a7 8026805: Array.prototype.length doesn't work as expected
hannesw
parents: 21438
diff changeset
   504
    @Setter(name = "length", where = Where.PROTOTYPE, attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_CONFIGURABLE)
31c57355a4a7 8026805: Array.prototype.length doesn't work as expected
hannesw
parents: 21438
diff changeset
   505
    public static void setProtoLength(final Object self, final Object length) {
31c57355a4a7 8026805: Array.prototype.length doesn't work as expected
hannesw
parents: 21438
diff changeset
   506
        length(self, length);  // Same as instance setter but we can't make nasgen use the same method for prototype
31c57355a4a7 8026805: Array.prototype.length doesn't work as expected
hannesw
parents: 21438
diff changeset
   507
    }
31c57355a4a7 8026805: Array.prototype.length doesn't work as expected
hannesw
parents: 21438
diff changeset
   508
28124
7737b6eae7cb 8066215: Fuzzing bug: length valueOf bug
hannesw
parents: 27528
diff changeset
   509
    static long validLength(final Object length) {
7737b6eae7cb 8066215: Fuzzing bug: length valueOf bug
hannesw
parents: 27528
diff changeset
   510
        // ES5 15.4.5.1, steps 3.c and 3.d require two ToNumber conversions here
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   511
        final double doubleLength = JSType.toNumber(length);
28124
7737b6eae7cb 8066215: Fuzzing bug: length valueOf bug
hannesw
parents: 27528
diff changeset
   512
        if (doubleLength != JSType.toUint32(length)) {
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16226
diff changeset
   513
            throw rangeError("inappropriate.array.length", ScriptRuntime.safeToString(length));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   514
        }
28124
7737b6eae7cb 8066215: Fuzzing bug: length valueOf bug
hannesw
parents: 27528
diff changeset
   515
        return (long) doubleLength;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   516
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   517
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   518
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   519
     * ECMA 15.4.4.2 Array.prototype.toString ( )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   520
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   521
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   522
     * @return string representation of array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   523
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   524
    @Function(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   525
    public static Object toString(final Object self) {
16769
f0d7cca4e7fd 8011365: Array.prototype.join and Array.prototype.toString do not throw TypeError on null, undefined
sundar
parents: 16767
diff changeset
   526
        final Object obj = Global.toObject(self);
f0d7cca4e7fd 8011365: Array.prototype.join and Array.prototype.toString do not throw TypeError on null, undefined
sundar
parents: 16767
diff changeset
   527
        if (obj instanceof ScriptObject) {
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   528
            final InvokeByName joinInvoker = getJOIN();
16769
f0d7cca4e7fd 8011365: Array.prototype.join and Array.prototype.toString do not throw TypeError on null, undefined
sundar
parents: 16767
diff changeset
   529
            final ScriptObject sobj = (ScriptObject)obj;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   530
            try {
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   531
                final Object join = joinInvoker.getGetter().invokeExact(sobj);
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18885
diff changeset
   532
                if (Bootstrap.isCallable(join)) {
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   533
                    return joinInvoker.getInvoker().invokeExact(join, sobj);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   534
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   535
            } catch (final RuntimeException | Error e) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   536
                throw e;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   537
            } catch (final Throwable t) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   538
                throw new RuntimeException(t);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   539
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   540
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   541
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   542
        // FIXME: should lookup Object.prototype.toString and call that?
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   543
        return ScriptRuntime.builtinObjectToString(self);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   544
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   545
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   546
    /**
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   547
     * Assert that an array is numeric, if not throw type error
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   548
     * @param self self array to check
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   549
     * @return true if numeric
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   550
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   551
    @Function(attributes = Attribute.NOT_ENUMERABLE)
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   552
    public static Object assertNumeric(final Object self) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   553
        if(!(self instanceof NativeArray && ((NativeArray)self).getArray().getOptimisticType().isNumeric())) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   554
            throw typeError("not.a.numeric.array", ScriptRuntime.safeToString(self));
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   555
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   556
        return Boolean.TRUE;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   557
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   558
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   559
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   560
     * ECMA 15.4.4.3 Array.prototype.toLocaleString ( )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   561
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   562
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   563
     * @return locale specific string representation for array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   564
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   565
    @Function(attributes = Attribute.NOT_ENUMERABLE)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23761
diff changeset
   566
    public static String toLocaleString(final Object self) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   567
        final StringBuilder sb = new StringBuilder();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   568
        final Iterator<Object> iter = arrayLikeIterator(self, true);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   569
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   570
        while (iter.hasNext()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   571
            final Object obj = iter.next();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   572
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   573
            if (obj != null && obj != ScriptRuntime.UNDEFINED) {
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16185
diff changeset
   574
                final Object val = JSType.toScriptObject(obj);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   575
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   576
                try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   577
                    if (val instanceof ScriptObject) {
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   578
                        final InvokeByName localeInvoker = getTO_LOCALE_STRING();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   579
                        final ScriptObject sobj           = (ScriptObject)val;
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   580
                        final Object       toLocaleString = localeInvoker.getGetter().invokeExact(sobj);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   581
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18885
diff changeset
   582
                        if (Bootstrap.isCallable(toLocaleString)) {
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   583
                            sb.append((String)localeInvoker.getInvoker().invokeExact(toLocaleString, sobj));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   584
                        } else {
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16226
diff changeset
   585
                            throw typeError("not.a.function", "toLocaleString");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   586
                        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   587
                    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   588
                } catch (final Error|RuntimeException t) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   589
                    throw t;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   590
                } catch (final Throwable t) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   591
                    throw new RuntimeException(t);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   592
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   593
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   594
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   595
            if (iter.hasNext()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   596
                sb.append(",");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   597
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   598
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   599
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   600
        return sb.toString();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   601
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   602
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   603
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   604
     * ECMA 15.4.2.2 new Array (len)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   605
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   606
     * @param newObj was the new operator used to instantiate this array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   607
     * @param self   self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   608
     * @param args   arguments (length)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   609
     * @return the new NativeArray
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   610
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   611
    @Constructor(arity = 1)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23761
diff changeset
   612
    public static NativeArray construct(final boolean newObj, final Object self, final Object... args) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   613
        switch (args.length) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   614
        case 0:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   615
            return new NativeArray(0);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   616
        case 1:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   617
            final Object len = args[0];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   618
            if (len instanceof Number) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   619
                long length;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   620
                if (len instanceof Integer || len instanceof Long) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   621
                    length = ((Number) len).longValue();
17241
c337fefb8c84 8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents: 16942
diff changeset
   622
                    if (length >= 0 && length < JSType.MAX_UINT) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   623
                        return new NativeArray(length);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   624
                    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   625
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   626
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   627
                length = JSType.toUint32(len);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   628
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   629
                /*
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   630
                 * If the argument len is a Number and ToUint32(len) is equal to
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   631
                 * len, then the length property of the newly constructed object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   632
                 * is set to ToUint32(len). If the argument len is a Number and
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   633
                 * ToUint32(len) is not equal to len, a RangeError exception is
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   634
                 * thrown.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   635
                 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   636
                final double numberLength = ((Number) len).doubleValue();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   637
                if (length != numberLength) {
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16226
diff changeset
   638
                    throw rangeError("inappropriate.array.length", JSType.toString(numberLength));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   639
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   640
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   641
                return new NativeArray(length);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   642
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   643
            /*
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   644
             * If the argument len is not a Number, then the length property of
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   645
             * the newly constructed object is set to 1 and the 0 property of
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   646
             * the newly constructed object is set to len
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   647
             */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   648
            return new NativeArray(new Object[]{args[0]});
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   649
            //fallthru
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   650
        default:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   651
            return new NativeArray(args);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   652
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   653
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   654
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   655
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   656
     * ECMA 15.4.2.2 new Array (len)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   657
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   658
     * Specialized constructor for zero arguments - empty array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   659
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   660
     * @param newObj was the new operator used to instantiate this array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   661
     * @param self   self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   662
     * @return the new NativeArray
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   663
     */
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   664
    @SpecializedFunction(isConstructor=true)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23761
diff changeset
   665
    public static NativeArray construct(final boolean newObj, final Object self) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   666
        return new NativeArray(0);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   667
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   668
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   669
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   670
     * ECMA 15.4.2.2 new Array (len)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   671
     *
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   672
     * Specialized constructor for zero arguments - empty array
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   673
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   674
     * @param newObj  was the new operator used to instantiate this array
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   675
     * @param self    self reference
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   676
     * @param element first element
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   677
     * @return the new NativeArray
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   678
     */
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   679
    @SpecializedFunction(isConstructor=true)
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   680
    public static Object construct(final boolean newObj, final Object self, final boolean element) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   681
        return new NativeArray(new Object[] { element });
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   682
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   683
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   684
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   685
     * ECMA 15.4.2.2 new Array (len)
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
   686
     *
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   687
     * Specialized constructor for one integer argument (length)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   688
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   689
     * @param newObj was the new operator used to instantiate this array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   690
     * @param self   self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   691
     * @param length array length
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   692
     * @return the new NativeArray
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   693
     */
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   694
    @SpecializedFunction(isConstructor=true)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23761
diff changeset
   695
    public static NativeArray construct(final boolean newObj, final Object self, final int length) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   696
        if (length >= 0) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   697
            return new NativeArray(length);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   698
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   699
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   700
        return construct(newObj, self, new Object[]{length});
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   701
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   702
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   703
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   704
     * ECMA 15.4.2.2 new Array (len)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   705
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   706
     * Specialized constructor for one long argument (length)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   707
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   708
     * @param newObj was the new operator used to instantiate this array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   709
     * @param self   self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   710
     * @param length array length
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   711
     * @return the new NativeArray
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   712
     */
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   713
    @SpecializedFunction(isConstructor=true)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23761
diff changeset
   714
    public static NativeArray construct(final boolean newObj, final Object self, final long length) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   715
        if (length >= 0L && length <= JSType.MAX_UINT) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   716
            return new NativeArray(length);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   717
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   718
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   719
        return construct(newObj, self, new Object[]{length});
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   720
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   721
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   722
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   723
     * ECMA 15.4.2.2 new Array (len)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   724
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   725
     * Specialized constructor for one double argument (length)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   726
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   727
     * @param newObj was the new operator used to instantiate this array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   728
     * @param self   self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   729
     * @param length array length
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   730
     * @return the new NativeArray
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   731
     */
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   732
    @SpecializedFunction(isConstructor=true)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23761
diff changeset
   733
    public static NativeArray construct(final boolean newObj, final Object self, final double length) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   734
        final long uint32length = JSType.toUint32(length);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   735
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   736
        if (uint32length == length) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   737
            return new NativeArray(uint32length);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   738
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   739
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   740
        return construct(newObj, self, new Object[]{length});
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   741
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   742
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   743
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   744
     * ECMA 15.4.4.4 Array.prototype.concat ( [ item1 [ , item2 [ , ... ] ] ] )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   745
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   746
     * @param self self reference
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: 26886
diff changeset
   747
     * @param arg argument
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: 26886
diff changeset
   748
     * @return resulting NativeArray
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: 26886
diff changeset
   749
     */
42376
8604f1a50c30 8170322: Specialized functions convert booleans to numbers
hannesw
parents: 41619
diff changeset
   750
    @SpecializedFunction(linkLogic=ConcatLinkLogic.class, convertsNumericArgs = false)
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: 26886
diff changeset
   751
    public static NativeArray concat(final Object self, final int arg) {
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: 26886
diff changeset
   752
        final ContinuousArrayData newData = getContinuousArrayDataCCE(self, Integer.class).copy(); //get at least an integer data copy of this data
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: 26886
diff changeset
   753
        newData.fastPush(arg); //add an integer to its end
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: 26886
diff changeset
   754
        return new NativeArray(newData);
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: 26886
diff changeset
   755
    }
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: 26886
diff changeset
   756
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: 26886
diff changeset
   757
    /**
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: 26886
diff changeset
   758
     * ECMA 15.4.4.4 Array.prototype.concat ( [ item1 [ , item2 [ , ... ] ] ] )
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: 26886
diff changeset
   759
     *
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: 26886
diff changeset
   760
     * @param self self reference
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: 26886
diff changeset
   761
     * @param arg argument
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: 26886
diff changeset
   762
     * @return resulting NativeArray
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: 26886
diff changeset
   763
     */
42376
8604f1a50c30 8170322: Specialized functions convert booleans to numbers
hannesw
parents: 41619
diff changeset
   764
    @SpecializedFunction(linkLogic=ConcatLinkLogic.class, convertsNumericArgs = false)
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: 26886
diff changeset
   765
    public static NativeArray concat(final Object self, final double arg) {
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: 26886
diff changeset
   766
        final ContinuousArrayData newData = getContinuousArrayDataCCE(self, Double.class).copy(); //get at least a number array data copy of this data
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: 26886
diff changeset
   767
        newData.fastPush(arg); //add a double at the end
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: 26886
diff changeset
   768
        return new NativeArray(newData);
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: 26886
diff changeset
   769
    }
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: 26886
diff changeset
   770
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: 26886
diff changeset
   771
    /**
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: 26886
diff changeset
   772
     * ECMA 15.4.4.4 Array.prototype.concat ( [ item1 [ , item2 [ , ... ] ] ] )
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: 26886
diff changeset
   773
     *
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: 26886
diff changeset
   774
     * @param self self reference
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: 26886
diff changeset
   775
     * @param arg argument
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: 26886
diff changeset
   776
     * @return resulting NativeArray
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: 26886
diff changeset
   777
     */
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: 26886
diff changeset
   778
    @SpecializedFunction(linkLogic=ConcatLinkLogic.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: 26886
diff changeset
   779
    public static NativeArray concat(final Object self, final Object arg) {
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: 26886
diff changeset
   780
        //arg is [NativeArray] of same type.
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: 26886
diff changeset
   781
        final ContinuousArrayData selfData = getContinuousArrayDataCCE(self);
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: 26886
diff changeset
   782
        final ContinuousArrayData newData;
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: 26886
diff changeset
   783
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: 26886
diff changeset
   784
        if (arg instanceof NativeArray) {
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: 26886
diff changeset
   785
            final ContinuousArrayData argData = (ContinuousArrayData)((NativeArray)arg).getArray();
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: 26886
diff changeset
   786
            if (argData.isEmpty()) {
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: 26886
diff changeset
   787
                newData = selfData.copy();
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: 26886
diff changeset
   788
            } else if (selfData.isEmpty()) {
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: 26886
diff changeset
   789
                newData = argData.copy();
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: 26886
diff changeset
   790
            } else {
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: 26886
diff changeset
   791
                final Class<?> widestElementType = selfData.widest(argData).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: 26886
diff changeset
   792
                newData = ((ContinuousArrayData)selfData.convert(widestElementType)).fastConcat((ContinuousArrayData)argData.convert(widestElementType));
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: 26886
diff changeset
   793
            }
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: 26886
diff changeset
   794
        } else {
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: 26886
diff changeset
   795
            newData = getContinuousArrayDataCCE(self, Object.class).copy();
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: 26886
diff changeset
   796
            newData.fastPush(arg);
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: 26886
diff changeset
   797
        }
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: 26886
diff changeset
   798
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: 26886
diff changeset
   799
        return new NativeArray(newData);
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: 26886
diff changeset
   800
    }
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: 26886
diff changeset
   801
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: 26886
diff changeset
   802
    /**
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: 26886
diff changeset
   803
     * ECMA 15.4.4.4 Array.prototype.concat ( [ item1 [ , item2 [ , ... ] ] ] )
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: 26886
diff changeset
   804
     *
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: 26886
diff changeset
   805
     * @param self self reference
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   806
     * @param args arguments
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   807
     * @return resulting NativeArray
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   808
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   809
    @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23761
diff changeset
   810
    public static NativeArray concat(final Object self, final Object... args) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   811
        final ArrayList<Object> list = new ArrayList<>();
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: 26886
diff changeset
   812
18885
6b6801c3b81a 8015356: array concatenation should skip empty elements
attila
parents: 18851
diff changeset
   813
        concatToList(list, Global.toObject(self));
6b6801c3b81a 8015356: array concatenation should skip empty elements
attila
parents: 18851
diff changeset
   814
6b6801c3b81a 8015356: array concatenation should skip empty elements
attila
parents: 18851
diff changeset
   815
        for (final Object obj : args) {
6b6801c3b81a 8015356: array concatenation should skip empty elements
attila
parents: 18851
diff changeset
   816
            concatToList(list, obj);
6b6801c3b81a 8015356: array concatenation should skip empty elements
attila
parents: 18851
diff changeset
   817
        }
6b6801c3b81a 8015356: array concatenation should skip empty elements
attila
parents: 18851
diff changeset
   818
6b6801c3b81a 8015356: array concatenation should skip empty elements
attila
parents: 18851
diff changeset
   819
        return new NativeArray(list.toArray());
6b6801c3b81a 8015356: array concatenation should skip empty elements
attila
parents: 18851
diff changeset
   820
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   821
18885
6b6801c3b81a 8015356: array concatenation should skip empty elements
attila
parents: 18851
diff changeset
   822
    private static void concatToList(final ArrayList<Object> list, final Object obj) {
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24742
diff changeset
   823
        final boolean isScriptArray  = isArray(obj);
18885
6b6801c3b81a 8015356: array concatenation should skip empty elements
attila
parents: 18851
diff changeset
   824
        final boolean isScriptObject = isScriptArray || obj instanceof ScriptObject;
6b6801c3b81a 8015356: array concatenation should skip empty elements
attila
parents: 18851
diff changeset
   825
        if (isScriptArray || obj instanceof Iterable || (obj != null && obj.getClass().isArray())) {
6b6801c3b81a 8015356: array concatenation should skip empty elements
attila
parents: 18851
diff changeset
   826
            final Iterator<Object> iter = arrayLikeIterator(obj, true);
6b6801c3b81a 8015356: array concatenation should skip empty elements
attila
parents: 18851
diff changeset
   827
            if (iter.hasNext()) {
19472
9476460521b3 8023017: SUB missing for widest op == number for BinaryNode
lagergren
parents: 19456
diff changeset
   828
                for (int i = 0; iter.hasNext(); ++i) {
18885
6b6801c3b81a 8015356: array concatenation should skip empty elements
attila
parents: 18851
diff changeset
   829
                    final Object value = iter.next();
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24742
diff changeset
   830
                    final boolean lacksIndex = obj != null && !((ScriptObject)obj).has(i);
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24742
diff changeset
   831
                    if (value == ScriptRuntime.UNDEFINED && isScriptObject && lacksIndex) {
18885
6b6801c3b81a 8015356: array concatenation should skip empty elements
attila
parents: 18851
diff changeset
   832
                        // TODO: eventually rewrite arrayLikeIterator to use a three-state enum for handling
6b6801c3b81a 8015356: array concatenation should skip empty elements
attila
parents: 18851
diff changeset
   833
                        // UNDEFINED instead of an "includeUndefined" boolean with states SKIP, INCLUDE,
6b6801c3b81a 8015356: array concatenation should skip empty elements
attila
parents: 18851
diff changeset
   834
                        // RETURN_EMPTY. Until then, this is how we'll make sure that empty elements don't make it
6b6801c3b81a 8015356: array concatenation should skip empty elements
attila
parents: 18851
diff changeset
   835
                        // into the concatenated array.
6b6801c3b81a 8015356: array concatenation should skip empty elements
attila
parents: 18851
diff changeset
   836
                        list.add(ScriptRuntime.EMPTY);
6b6801c3b81a 8015356: array concatenation should skip empty elements
attila
parents: 18851
diff changeset
   837
                    } else {
6b6801c3b81a 8015356: array concatenation should skip empty elements
attila
parents: 18851
diff changeset
   838
                        list.add(value);
6b6801c3b81a 8015356: array concatenation should skip empty elements
attila
parents: 18851
diff changeset
   839
                    }
6b6801c3b81a 8015356: array concatenation should skip empty elements
attila
parents: 18851
diff changeset
   840
                }
6b6801c3b81a 8015356: array concatenation should skip empty elements
attila
parents: 18851
diff changeset
   841
            } else if (!isScriptArray) {
6b6801c3b81a 8015356: array concatenation should skip empty elements
attila
parents: 18851
diff changeset
   842
                list.add(obj); // add empty object, but not an empty array
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   843
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   844
        } else {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   845
            // single element, add it
18885
6b6801c3b81a 8015356: array concatenation should skip empty elements
attila
parents: 18851
diff changeset
   846
            list.add(obj);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   847
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   848
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   849
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   850
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   851
     * ECMA 15.4.4.5 Array.prototype.join (separator)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   852
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   853
     * @param self      self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   854
     * @param separator element separator
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   855
     * @return string representation after join
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   856
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   857
    @Function(attributes = Attribute.NOT_ENUMERABLE)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23761
diff changeset
   858
    public static String join(final Object self, final Object separator) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   859
        final StringBuilder    sb   = new StringBuilder();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   860
        final Iterator<Object> iter = arrayLikeIterator(self, true);
16769
f0d7cca4e7fd 8011365: Array.prototype.join and Array.prototype.toString do not throw TypeError on null, undefined
sundar
parents: 16767
diff changeset
   861
        final String           sep  = separator == ScriptRuntime.UNDEFINED ? "," : JSType.toString(separator);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   862
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   863
        while (iter.hasNext()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   864
            final Object obj = iter.next();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   865
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   866
            if (obj != null && obj != ScriptRuntime.UNDEFINED) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   867
                sb.append(JSType.toString(obj));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   868
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   869
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   870
            if (iter.hasNext()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   871
                sb.append(sep);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   872
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   873
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   874
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   875
        return sb.toString();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   876
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   877
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   878
    /**
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   879
     * Specialization of pop for ContinuousArrayData
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   880
     *   The link guard checks that the array is continuous AND not empty.
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   881
     *   The runtime guard checks that the guard is continuous (CCE otherwise)
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   882
     *
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   883
     * Primitive specialization, {@link LinkLogic}
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   884
     *
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   885
     * @param self self reference
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   886
     * @return element popped
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   887
     * @throws ClassCastException if array is empty, facilitating Undefined return value
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   888
     */
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   889
    @SpecializedFunction(name="pop", linkLogic=PopLinkLogic.class)
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   890
    public static int popInt(final Object self) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   891
        //must be non empty IntArrayData
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   892
        return getContinuousNonEmptyArrayDataCCE(self, IntElements.class).fastPopInt();
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   893
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   894
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   895
    /**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   896
     * Specialization of pop for ContinuousArrayData
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   897
     *
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   898
     * Primitive specialization, {@link LinkLogic}
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   899
     *
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   900
     * @param self self reference
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   901
     * @return element popped
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   902
     * @throws ClassCastException if array is empty, facilitating Undefined return value
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   903
     */
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   904
    @SpecializedFunction(name="pop", linkLogic=PopLinkLogic.class)
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   905
    public static double popDouble(final Object self) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   906
        //must be non empty int long or double array data
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   907
        return getContinuousNonEmptyArrayDataCCE(self, NumericElements.class).fastPopDouble();
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   908
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   909
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   910
    /**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   911
     * Specialization of pop for ContinuousArrayData
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   912
     *
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   913
     * Primitive specialization, {@link LinkLogic}
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   914
     *
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   915
     * @param self self reference
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   916
     * @return element popped
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   917
     * @throws ClassCastException if array is empty, facilitating Undefined return value
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   918
     */
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   919
    @SpecializedFunction(name="pop", linkLogic=PopLinkLogic.class)
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   920
    public static Object popObject(final Object self) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   921
        //can be any data, because the numeric ones will throw cce and force relink
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   922
        return getContinuousArrayDataCCE(self, null).fastPopObject();
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   923
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   924
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   925
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   926
     * ECMA 15.4.4.6 Array.prototype.pop ()
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   927
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   928
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   929
     * @return array after pop
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   930
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   931
    @Function(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   932
    public static Object pop(final Object self) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   933
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   934
            final ScriptObject sobj = (ScriptObject)self;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   935
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   936
            if (bulkable(sobj)) {
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16256
diff changeset
   937
                return sobj.getArray().pop();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   938
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   939
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   940
            final long len = JSType.toUint32(sobj.getLength());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   941
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   942
            if (len == 0) {
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 25865
diff changeset
   943
                sobj.set("length", 0, CALLSITE_STRICT);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   944
                return ScriptRuntime.UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   945
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   946
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   947
            final long   index   = len - 1;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   948
            final Object element = sobj.get(index);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   949
18328
ebd24057f163 8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents: 18318
diff changeset
   950
            sobj.delete(index, true);
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 25865
diff changeset
   951
            sobj.set("length", index, CALLSITE_STRICT);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   952
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   953
            return element;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   954
        } catch (final ClassCastException | NullPointerException e) {
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16226
diff changeset
   955
            throw typeError("not.an.object", ScriptRuntime.safeToString(self));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   956
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   957
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   958
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   959
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   960
     * ECMA 15.4.4.7 Array.prototype.push (args...)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   961
     *
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   962
     * Primitive specialization, {@link LinkLogic}
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   963
     *
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   964
     * @param self self reference
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   965
     * @param arg a primitive to push
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   966
     * @return array length after push
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   967
     */
42376
8604f1a50c30 8170322: Specialized functions convert booleans to numbers
hannesw
parents: 41619
diff changeset
   968
    @SpecializedFunction(linkLogic=PushLinkLogic.class, convertsNumericArgs = false)
34974
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 34732
diff changeset
   969
    public static double push(final Object self, final int arg) {
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   970
        return getContinuousArrayDataCCE(self, Integer.class).fastPush(arg);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   971
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   972
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   973
    /**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   974
     * ECMA 15.4.4.7 Array.prototype.push (args...)
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   975
     *
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   976
     * Primitive specialization, {@link LinkLogic}
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   977
     *
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   978
     * @param self self reference
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   979
     * @param arg a primitive to push
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   980
     * @return array length after push
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   981
     */
42376
8604f1a50c30 8170322: Specialized functions convert booleans to numbers
hannesw
parents: 41619
diff changeset
   982
    @SpecializedFunction(linkLogic=PushLinkLogic.class, convertsNumericArgs = false)
34974
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 34732
diff changeset
   983
    public static double push(final Object self, final double arg) {
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   984
        return getContinuousArrayDataCCE(self, Double.class).fastPush(arg);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   985
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   986
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   987
    /**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   988
     * ECMA 15.4.4.7 Array.prototype.push (args...)
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   989
     *
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   990
     * Primitive specialization, {@link LinkLogic}
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   991
     *
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   992
     * @param self self reference
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   993
     * @param arg a primitive to push
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   994
     * @return array length after push
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   995
     */
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   996
    @SpecializedFunction(name="push", linkLogic=PushLinkLogic.class)
34974
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 34732
diff changeset
   997
    public static double pushObject(final Object self, final Object arg) {
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   998
        return getContinuousArrayDataCCE(self, Object.class).fastPush(arg);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
   999
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1000
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1001
    /**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1002
     * ECMA 15.4.4.7 Array.prototype.push (args...)
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1003
     *
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1004
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1005
     * @param args arguments to push
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
  1006
     * @return array length after pushes
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1007
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1008
    @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1009
    public static Object push(final Object self, final Object... args) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1010
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1011
            final ScriptObject sobj   = (ScriptObject)self;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1012
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1013
            if (bulkable(sobj) && sobj.getArray().length() + args.length <= JSType.MAX_UINT) {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1014
                final ArrayData newData = sobj.getArray().push(true, args);
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1015
                sobj.setArray(newData);
35326
d3ec391b995e 8147845: Varargs Array functions still leaking longs
hannesw
parents: 34974
diff changeset
  1016
                return JSType.toNarrowestNumber(newData.length());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1017
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1018
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1019
            long len = JSType.toUint32(sobj.getLength());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1020
            for (final Object element : args) {
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 25865
diff changeset
  1021
                sobj.set(len++, element, CALLSITE_STRICT);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1022
            }
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 25865
diff changeset
  1023
            sobj.set("length", len, CALLSITE_STRICT);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1024
35326
d3ec391b995e 8147845: Varargs Array functions still leaking longs
hannesw
parents: 34974
diff changeset
  1025
            return JSType.toNarrowestNumber(len);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1026
        } catch (final ClassCastException | NullPointerException e) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21444
diff changeset
  1027
            throw typeError(Context.getGlobal(), e, "not.an.object", ScriptRuntime.safeToString(self));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1028
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1029
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1030
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1031
    /**
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1032
     * ECMA 15.4.4.7 Array.prototype.push (args...) specialized for single object argument
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1033
     *
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1034
     * @param self self reference
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1035
     * @param arg argument to push
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1036
     * @return array after pushes
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1037
     */
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1038
    @SpecializedFunction
34974
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 34732
diff changeset
  1039
    public static double push(final Object self, final Object arg) {
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1040
        try {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1041
            final ScriptObject sobj = (ScriptObject)self;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1042
            final ArrayData arrayData = sobj.getArray();
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1043
            final long length = arrayData.length();
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1044
            if (bulkable(sobj) && length < JSType.MAX_UINT) {
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1045
                sobj.setArray(arrayData.push(true, arg));
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1046
                return length + 1;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1047
            }
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1048
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1049
            long len = JSType.toUint32(sobj.getLength());
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 25865
diff changeset
  1050
            sobj.set(len++, arg, CALLSITE_STRICT);
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 25865
diff changeset
  1051
            sobj.set("length", len, CALLSITE_STRICT);
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1052
            return len;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1053
        } catch (final ClassCastException | NullPointerException e) {
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16226
diff changeset
  1054
            throw typeError("not.an.object", ScriptRuntime.safeToString(self));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1055
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1056
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1057
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1058
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1059
     * ECMA 15.4.4.8 Array.prototype.reverse ()
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1060
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1061
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1062
     * @return reversed array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1063
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1064
    @Function(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1065
    public static Object reverse(final Object self) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1066
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1067
            final ScriptObject sobj   = (ScriptObject)self;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1068
            final long         len    = JSType.toUint32(sobj.getLength());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1069
            final long         middle = len / 2;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1070
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1071
            for (long lower = 0; lower != middle; lower++) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1072
                final long    upper       = len - lower - 1;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1073
                final Object  lowerValue  = sobj.get(lower);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1074
                final Object  upperValue  = sobj.get(upper);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1075
                final boolean lowerExists = sobj.has(lower);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1076
                final boolean upperExists = sobj.has(upper);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1077
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1078
                if (lowerExists && upperExists) {
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 25865
diff changeset
  1079
                    sobj.set(lower, upperValue, CALLSITE_STRICT);
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 25865
diff changeset
  1080
                    sobj.set(upper, lowerValue, CALLSITE_STRICT);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1081
                } else if (!lowerExists && upperExists) {
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 25865
diff changeset
  1082
                    sobj.set(lower, upperValue, CALLSITE_STRICT);
18328
ebd24057f163 8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents: 18318
diff changeset
  1083
                    sobj.delete(upper, true);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1084
                } else if (lowerExists && !upperExists) {
18328
ebd24057f163 8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents: 18318
diff changeset
  1085
                    sobj.delete(lower, true);
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 25865
diff changeset
  1086
                    sobj.set(upper, lowerValue, CALLSITE_STRICT);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1087
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1088
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1089
            return sobj;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1090
        } catch (final ClassCastException | NullPointerException e) {
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16226
diff changeset
  1091
            throw typeError("not.an.object", ScriptRuntime.safeToString(self));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1092
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1093
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1094
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1095
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1096
     * ECMA 15.4.4.9 Array.prototype.shift ()
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1097
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1098
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1099
     * @return shifted array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1100
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1101
    @Function(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1102
    public static Object shift(final Object self) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1103
        final Object obj = Global.toObject(self);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1104
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1105
        Object first = ScriptRuntime.UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1106
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1107
        if (!(obj instanceof ScriptObject)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1108
            return first;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1109
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1110
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1111
        final ScriptObject sobj   = (ScriptObject) obj;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1112
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1113
        long len = JSType.toUint32(sobj.getLength());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1114
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1115
        if (len > 0) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1116
            first = sobj.get(0);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1117
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1118
            if (bulkable(sobj)) {
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16256
diff changeset
  1119
                sobj.getArray().shiftLeft(1);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1120
            } else {
20561
9f1da74085ae 8025589: Array.prototype.shift should only copy defined elements in generic mode
hannesw
parents: 20560
diff changeset
  1121
                boolean hasPrevious = true;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1122
                for (long k = 1; k < len; k++) {
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1123
                    final boolean hasCurrent = sobj.has(k);
20561
9f1da74085ae 8025589: Array.prototype.shift should only copy defined elements in generic mode
hannesw
parents: 20560
diff changeset
  1124
                    if (hasCurrent) {
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 25865
diff changeset
  1125
                        sobj.set(k - 1, sobj.get(k), CALLSITE_STRICT);
20561
9f1da74085ae 8025589: Array.prototype.shift should only copy defined elements in generic mode
hannesw
parents: 20560
diff changeset
  1126
                    } else if (hasPrevious) {
9f1da74085ae 8025589: Array.prototype.shift should only copy defined elements in generic mode
hannesw
parents: 20560
diff changeset
  1127
                        sobj.delete(k - 1, true);
9f1da74085ae 8025589: Array.prototype.shift should only copy defined elements in generic mode
hannesw
parents: 20560
diff changeset
  1128
                    }
9f1da74085ae 8025589: Array.prototype.shift should only copy defined elements in generic mode
hannesw
parents: 20560
diff changeset
  1129
                    hasPrevious = hasCurrent;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1130
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1131
            }
18328
ebd24057f163 8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents: 18318
diff changeset
  1132
            sobj.delete(--len, true);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1133
        } else {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1134
            len = 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1135
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1136
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 25865
diff changeset
  1137
        sobj.set("length", len, CALLSITE_STRICT);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1138
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1139
        return first;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1140
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1141
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1142
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1143
     * ECMA 15.4.4.10 Array.prototype.slice ( start [ , end ] )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1144
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1145
     * @param self  self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1146
     * @param start start of slice (inclusive)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1147
     * @param end   end of slice (optional, exclusive)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1148
     * @return sliced array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1149
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1150
    @Function(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1151
    public static Object slice(final Object self, final Object start, final Object end) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1152
        final Object       obj                 = Global.toObject(self);
20926
9c7625a91b68 8026125: Array.prototype.slice.call(Java.type("java.util.HashMap")) throws ClassCastException: jdk.internal.dynalink.beans.StaticClass cannot be cast to jdk.nashorn.internal.runtime.ScriptObject
sundar
parents: 20561
diff changeset
  1153
        if (!(obj instanceof ScriptObject)) {
9c7625a91b68 8026125: Array.prototype.slice.call(Java.type("java.util.HashMap")) throws ClassCastException: jdk.internal.dynalink.beans.StaticClass cannot be cast to jdk.nashorn.internal.runtime.ScriptObject
sundar
parents: 20561
diff changeset
  1154
            return ScriptRuntime.UNDEFINED;
9c7625a91b68 8026125: Array.prototype.slice.call(Java.type("java.util.HashMap")) throws ClassCastException: jdk.internal.dynalink.beans.StaticClass cannot be cast to jdk.nashorn.internal.runtime.ScriptObject
sundar
parents: 20561
diff changeset
  1155
        }
9c7625a91b68 8026125: Array.prototype.slice.call(Java.type("java.util.HashMap")) throws ClassCastException: jdk.internal.dynalink.beans.StaticClass cannot be cast to jdk.nashorn.internal.runtime.ScriptObject
sundar
parents: 20561
diff changeset
  1156
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1157
        final ScriptObject sobj                = (ScriptObject)obj;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1158
        final long         len                 = JSType.toUint32(sobj.getLength());
17764
29e6eb3bb9df 8010804: Review long and integer usage conventions
hannesw
parents: 17520
diff changeset
  1159
        final long         relativeStart       = JSType.toLong(start);
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1160
        final long         relativeEnd         = end == ScriptRuntime.UNDEFINED ? len : JSType.toLong(end);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1161
17764
29e6eb3bb9df 8010804: Review long and integer usage conventions
hannesw
parents: 17520
diff changeset
  1162
        long k = relativeStart < 0 ? Math.max(len + relativeStart, 0) : Math.min(relativeStart, len);
29e6eb3bb9df 8010804: Review long and integer usage conventions
hannesw
parents: 17520
diff changeset
  1163
        final long finale = relativeEnd < 0 ? Math.max(len + relativeEnd, 0) : Math.min(relativeEnd, len);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1164
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1165
        if (k >= finale) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1166
            return new NativeArray(0);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1167
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1168
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1169
        if (bulkable(sobj)) {
17520
dfba83a3589d 8013878: ClassCastException in Regex
hannesw
parents: 17241
diff changeset
  1170
            return new NativeArray(sobj.getArray().slice(k, finale));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1171
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1172
20560
25be0d2aa1c0 8025520: Array.prototype.slice should only copy defined elements
hannesw
parents: 19889
diff changeset
  1173
        // Construct array with proper length to have a deleted filter on undefined elements
25be0d2aa1c0 8025520: Array.prototype.slice should only copy defined elements
hannesw
parents: 19889
diff changeset
  1174
        final NativeArray copy = new NativeArray(finale - k);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1175
        for (long n = 0; k < finale; n++, k++) {
20560
25be0d2aa1c0 8025520: Array.prototype.slice should only copy defined elements
hannesw
parents: 19889
diff changeset
  1176
            if (sobj.has(k)) {
25be0d2aa1c0 8025520: Array.prototype.slice should only copy defined elements
hannesw
parents: 19889
diff changeset
  1177
                copy.defineOwnProperty(ArrayIndex.getArrayIndex(n), sobj.get(k));
25be0d2aa1c0 8025520: Array.prototype.slice should only copy defined elements
hannesw
parents: 19889
diff changeset
  1178
            }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1179
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1180
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1181
        return copy;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1182
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1183
38595
6eceed1919cf 8157680: Callback parameter of any JS builtin implementation should accept any Callable
sundar
parents: 36023
diff changeset
  1184
    private static Object compareFunction(final Object comparefn) {
16941
f5088aaca810 8011960: [2,1].sort(null) should throw TypeError
sundar
parents: 16779
diff changeset
  1185
        if (comparefn == ScriptRuntime.UNDEFINED) {
f5088aaca810 8011960: [2,1].sort(null) should throw TypeError
sundar
parents: 16779
diff changeset
  1186
            return null;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1187
        }
16941
f5088aaca810 8011960: [2,1].sort(null) should throw TypeError
sundar
parents: 16779
diff changeset
  1188
38595
6eceed1919cf 8157680: Callback parameter of any JS builtin implementation should accept any Callable
sundar
parents: 36023
diff changeset
  1189
        if (!Bootstrap.isCallable(comparefn)) {
16941
f5088aaca810 8011960: [2,1].sort(null) should throw TypeError
sundar
parents: 16779
diff changeset
  1190
            throw typeError("not.a.function", ScriptRuntime.safeToString(comparefn));
f5088aaca810 8011960: [2,1].sort(null) should throw TypeError
sundar
parents: 16779
diff changeset
  1191
        }
f5088aaca810 8011960: [2,1].sort(null) should throw TypeError
sundar
parents: 16779
diff changeset
  1192
38595
6eceed1919cf 8157680: Callback parameter of any JS builtin implementation should accept any Callable
sundar
parents: 36023
diff changeset
  1193
        return comparefn;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1194
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1195
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1196
    private static Object[] sort(final Object[] array, final Object comparefn) {
38595
6eceed1919cf 8157680: Callback parameter of any JS builtin implementation should accept any Callable
sundar
parents: 36023
diff changeset
  1197
        final Object cmp = compareFunction(comparefn);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1198
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1199
        final List<Object> list = Arrays.asList(array);
38595
6eceed1919cf 8157680: Callback parameter of any JS builtin implementation should accept any Callable
sundar
parents: 36023
diff changeset
  1200
        final Object cmpThis = cmp == null || Bootstrap.isStrictCallable(cmp) ? ScriptRuntime.UNDEFINED : Global.instance();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1201
30514
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1202
        try {
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1203
            Collections.sort(list, new Comparator<Object>() {
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1204
                private final MethodHandle call_cmp = getCALL_CMP();
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1205
                @Override
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1206
                public int compare(final Object x, final Object y) {
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1207
                    if (x == ScriptRuntime.UNDEFINED && y == ScriptRuntime.UNDEFINED) {
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1208
                        return 0;
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1209
                    } else if (x == ScriptRuntime.UNDEFINED) {
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1210
                        return 1;
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1211
                    } else if (y == ScriptRuntime.UNDEFINED) {
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1212
                        return -1;
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1213
                    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1214
30514
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1215
                    if (cmp != null) {
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1216
                        try {
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1217
                            return (int)Math.signum((double)call_cmp.invokeExact(cmp, cmpThis, x, y));
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1218
                        } catch (final RuntimeException | Error e) {
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1219
                            throw e;
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1220
                        } catch (final Throwable t) {
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1221
                            throw new RuntimeException(t);
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1222
                        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1223
                    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1224
30514
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1225
                    return JSType.toString(x).compareTo(JSType.toString(y));
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1226
                }
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1227
            });
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1228
        } catch (final IllegalArgumentException iae) {
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1229
            // Collections.sort throws IllegalArgumentException when
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1230
            // Comparison method violates its general contract
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1231
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1232
            // See ECMA spec 15.4.4.11 Array.prototype.sort (comparefn).
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1233
            // If "comparefn" is not undefined and is not a consistent
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1234
            // comparison function for the elements of this array, the
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1235
            // behaviour of sort is implementation-defined.
25583ec3b3a1 8080182: Array.prototype.sort throws IAE on inconsistent comparison
sundar
parents: 28787
diff changeset
  1236
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1237
35407
204abe4d8cbc 8147591: Revisit Collection.toArray(new T[size]) calls in nashorn and dynalink code
mhaupt
parents: 35326
diff changeset
  1238
        return list.toArray(new Object[0]);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1239
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1240
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1241
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1242
     * ECMA 15.4.4.11 Array.prototype.sort ( comparefn )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1243
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1244
     * @param self       self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1245
     * @param comparefn  element comparison function
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1246
     * @return sorted array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1247
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1248
    @Function(attributes = Attribute.NOT_ENUMERABLE)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23761
diff changeset
  1249
    public static ScriptObject sort(final Object self, final Object comparefn) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1250
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1251
            final ScriptObject sobj    = (ScriptObject) self;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1252
            final long         len     = JSType.toUint32(sobj.getLength());
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17983
diff changeset
  1253
            ArrayData          array   = sobj.getArray();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1254
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1255
            if (len > 1) {
16779
61fd89656ce3 8011552: Arrays with missing elements are not properly sorted
sundar
parents: 16773
diff changeset
  1256
                // Get only non-missing elements. Missing elements go at the end
61fd89656ce3 8011552: Arrays with missing elements are not properly sorted
sundar
parents: 16773
diff changeset
  1257
                // of the sorted array. So, just don't copy these to sort input.
61fd89656ce3 8011552: Arrays with missing elements are not properly sorted
sundar
parents: 16773
diff changeset
  1258
                final ArrayList<Object> src = new ArrayList<>();
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
  1259
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
  1260
                for (final Iterator<Long> iter = array.indexIterator(); iter.hasNext(); ) {
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
  1261
                    final long index = iter.next();
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
  1262
                    if (index >= len) {
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
  1263
                        break;
16779
61fd89656ce3 8011552: Arrays with missing elements are not properly sorted
sundar
parents: 16773
diff changeset
  1264
                    }
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
  1265
                    src.add(array.getObject((int)index));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1266
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1267
16779
61fd89656ce3 8011552: Arrays with missing elements are not properly sorted
sundar
parents: 16773
diff changeset
  1268
                final Object[] sorted = sort(src.toArray(), comparefn);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1269
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1270
                for (int i = 0; i < sorted.length; i++) {
18328
ebd24057f163 8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents: 18318
diff changeset
  1271
                    array = array.set(i, sorted[i], true);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1272
                }
16779
61fd89656ce3 8011552: Arrays with missing elements are not properly sorted
sundar
parents: 16773
diff changeset
  1273
61fd89656ce3 8011552: Arrays with missing elements are not properly sorted
sundar
parents: 16773
diff changeset
  1274
                // delete missing elements - which are at the end of sorted array
18606
5704d7a4a0a8 8010697: DeletedArrayFilter seems to leak memory
jlaskey
parents: 18334
diff changeset
  1275
                if (sorted.length != len) {
5704d7a4a0a8 8010697: DeletedArrayFilter seems to leak memory
jlaskey
parents: 18334
diff changeset
  1276
                    array = array.delete(sorted.length, len - 1);
5704d7a4a0a8 8010697: DeletedArrayFilter seems to leak memory
jlaskey
parents: 18334
diff changeset
  1277
                }
5704d7a4a0a8 8010697: DeletedArrayFilter seems to leak memory
jlaskey
parents: 18334
diff changeset
  1278
5704d7a4a0a8 8010697: DeletedArrayFilter seems to leak memory
jlaskey
parents: 18334
diff changeset
  1279
                sobj.setArray(array);
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1280
            }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1281
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1282
            return sobj;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1283
        } catch (final ClassCastException | NullPointerException e) {
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16226
diff changeset
  1284
            throw typeError("not.an.object", ScriptRuntime.safeToString(self));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1285
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1286
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1287
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1288
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1289
     * ECMA 15.4.4.12 Array.prototype.splice ( start, deleteCount [ item1 [ , item2 [ , ... ] ] ] )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1290
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1291
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1292
     * @param args arguments
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1293
     * @return result of splice
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1294
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1295
    @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 2)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1296
    public static Object splice(final Object self, final Object... args) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1297
        final Object obj = Global.toObject(self);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1298
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1299
        if (!(obj instanceof ScriptObject)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1300
            return ScriptRuntime.UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1301
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1302
39795
3de843c943c0 8068972: Array.splice should follow the ES6 specification
hannesw
parents: 38595
diff changeset
  1303
        final ScriptObject sobj          = (ScriptObject)obj;
3de843c943c0 8068972: Array.splice should follow the ES6 specification
hannesw
parents: 38595
diff changeset
  1304
        final long         len           = JSType.toUint32(sobj.getLength());
3de843c943c0 8068972: Array.splice should follow the ES6 specification
hannesw
parents: 38595
diff changeset
  1305
        final long         relativeStart = JSType.toLong(args.length > 0 ? args[0] : ScriptRuntime.UNDEFINED);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1306
17764
29e6eb3bb9df 8010804: Review long and integer usage conventions
hannesw
parents: 17520
diff changeset
  1307
        final long actualStart = relativeStart < 0 ? Math.max(len + relativeStart, 0) : Math.min(relativeStart, len);
39795
3de843c943c0 8068972: Array.splice should follow the ES6 specification
hannesw
parents: 38595
diff changeset
  1308
        final long actualDeleteCount;
3de843c943c0 8068972: Array.splice should follow the ES6 specification
hannesw
parents: 38595
diff changeset
  1309
        Object[] items = ScriptRuntime.EMPTY_ARRAY;
3de843c943c0 8068972: Array.splice should follow the ES6 specification
hannesw
parents: 38595
diff changeset
  1310
3de843c943c0 8068972: Array.splice should follow the ES6 specification
hannesw
parents: 38595
diff changeset
  1311
        if (args.length == 0) {
3de843c943c0 8068972: Array.splice should follow the ES6 specification
hannesw
parents: 38595
diff changeset
  1312
            actualDeleteCount = 0;
3de843c943c0 8068972: Array.splice should follow the ES6 specification
hannesw
parents: 38595
diff changeset
  1313
        } else if (args.length == 1) {
3de843c943c0 8068972: Array.splice should follow the ES6 specification
hannesw
parents: 38595
diff changeset
  1314
            actualDeleteCount = len - actualStart;
3de843c943c0 8068972: Array.splice should follow the ES6 specification
hannesw
parents: 38595
diff changeset
  1315
        } else {
3de843c943c0 8068972: Array.splice should follow the ES6 specification
hannesw
parents: 38595
diff changeset
  1316
            actualDeleteCount = Math.min(Math.max(JSType.toLong(args[1]), 0), len - actualStart);
3de843c943c0 8068972: Array.splice should follow the ES6 specification
hannesw
parents: 38595
diff changeset
  1317
            if (args.length > 2) {
3de843c943c0 8068972: Array.splice should follow the ES6 specification
hannesw
parents: 38595
diff changeset
  1318
                items = new Object[args.length - 2];
3de843c943c0 8068972: Array.splice should follow the ES6 specification
hannesw
parents: 38595
diff changeset
  1319
                System.arraycopy(args, 2, items, 0, items.length);
3de843c943c0 8068972: Array.splice should follow the ES6 specification
hannesw
parents: 38595
diff changeset
  1320
            }
3de843c943c0 8068972: Array.splice should follow the ES6 specification
hannesw
parents: 38595
diff changeset
  1321
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1322
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1323
        NativeArray returnValue;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1324
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1325
        if (actualStart <= Integer.MAX_VALUE && actualDeleteCount <= Integer.MAX_VALUE && bulkable(sobj)) {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1326
            try {
39795
3de843c943c0 8068972: Array.splice should follow the ES6 specification
hannesw
parents: 38595
diff changeset
  1327
                returnValue = new NativeArray(sobj.getArray().fastSplice((int)actualStart, (int)actualDeleteCount, items.length));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1328
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1329
                // Since this is a dense bulkable array we can use faster defineOwnProperty to copy new elements
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1330
                int k = (int) actualStart;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1331
                for (int i = 0; i < items.length; i++, k++) {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1332
                    sobj.defineOwnProperty(k, items[i]);
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1333
                }
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1334
            } catch (final UnsupportedOperationException uoe) {
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1335
                returnValue = slowSplice(sobj, actualStart, actualDeleteCount, items, len);
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1336
            }
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1337
        } else {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1338
            returnValue = slowSplice(sobj, actualStart, actualDeleteCount, items, len);
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1339
        }
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1340
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1341
        return returnValue;
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1342
    }
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1343
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1344
    private static NativeArray slowSplice(final ScriptObject sobj, final long start, final long deleteCount, final Object[] items, final long len) {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1345
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1346
        final NativeArray array = new NativeArray(deleteCount);
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1347
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1348
        for (long k = 0; k < deleteCount; k++) {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1349
            final long from = start + k;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1350
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1351
            if (sobj.has(from)) {
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17983
diff changeset
  1352
                array.defineOwnProperty(ArrayIndex.getArrayIndex(k), sobj.get(from));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1353
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1354
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1355
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1356
        if (items.length < deleteCount) {
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1357
            for (long k = start; k < len - deleteCount; k++) {
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1358
                final long from = k + deleteCount;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1359
                final long to   = k + items.length;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1360
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1361
                if (sobj.has(from)) {
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 25865
diff changeset
  1362
                    sobj.set(to, sobj.get(from), CALLSITE_STRICT);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1363
                } else {
18328
ebd24057f163 8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents: 18318
diff changeset
  1364
                    sobj.delete(to, true);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1365
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1366
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1367
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1368
            for (long k = len; k > len - deleteCount + items.length; k--) {
18328
ebd24057f163 8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents: 18318
diff changeset
  1369
                sobj.delete(k - 1, true);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1370
            }
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1371
        } else if (items.length > deleteCount) {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1372
            for (long k = len - deleteCount; k > start; k--) {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1373
                final long from = k + deleteCount - 1;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1374
                final long to   = k + items.length - 1;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1375
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1376
                if (sobj.has(from)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1377
                    final Object fromValue = sobj.get(from);
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 25865
diff changeset
  1378
                    sobj.set(to, fromValue, CALLSITE_STRICT);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1379
                } else {
18328
ebd24057f163 8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents: 18318
diff changeset
  1380
                    sobj.delete(to, true);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1381
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1382
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1383
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1384
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1385
        long k = start;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1386
        for (int i = 0; i < items.length; i++, k++) {
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 25865
diff changeset
  1387
            sobj.set(k, items[i], CALLSITE_STRICT);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1388
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1389
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 20926
diff changeset
  1390
        final long newLength = len - deleteCount + items.length;
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 25865
diff changeset
  1391
        sobj.set("length", newLength, CALLSITE_STRICT);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1392
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1393
        return array;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1394
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1395
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1396
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1397
     * ECMA 15.4.4.13 Array.prototype.unshift ( [ item1 [ , item2 [ , ... ] ] ] )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1398
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1399
     * @param self  self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1400
     * @param items items for unshift
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1401
     * @return unshifted array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1402
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1403
    @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1404
    public static Object unshift(final Object self, final Object... items) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1405
        final Object obj = Global.toObject(self);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1406
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1407
        if (!(obj instanceof ScriptObject)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1408
            return ScriptRuntime.UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1409
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1410
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1411
        final ScriptObject sobj   = (ScriptObject)obj;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1412
        final long         len    = JSType.toUint32(sobj.getLength());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1413
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1414
        if (items == null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1415
            return ScriptRuntime.UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1416
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1417
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1418
        if (bulkable(sobj)) {
17520
dfba83a3589d 8013878: ClassCastException in Regex
hannesw
parents: 17241
diff changeset
  1419
            sobj.getArray().shiftRight(items.length);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1420
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1421
            for (int j = 0; j < items.length; j++) {
18328
ebd24057f163 8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents: 18318
diff changeset
  1422
                sobj.setArray(sobj.getArray().set(j, items[j], true));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1423
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1424
        } else {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1425
            for (long k = len; k > 0; k--) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1426
                final long from = k - 1;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1427
                final long to = k + items.length - 1;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1428
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1429
                if (sobj.has(from)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1430
                    final Object fromValue = sobj.get(from);
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 25865
diff changeset
  1431
                    sobj.set(to, fromValue, CALLSITE_STRICT);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1432
                } else {
18328
ebd24057f163 8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents: 18318
diff changeset
  1433
                    sobj.delete(to, true);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1434
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1435
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1436
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1437
            for (int j = 0; j < items.length; j++) {
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 25865
diff changeset
  1438
                sobj.set(j, items[j], CALLSITE_STRICT);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1439
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1440
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1441
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1442
        final long newLength = len + items.length;
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 25865
diff changeset
  1443
        sobj.set("length", newLength, CALLSITE_STRICT);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1444
35326
d3ec391b995e 8147845: Varargs Array functions still leaking longs
hannesw
parents: 34974
diff changeset
  1445
        return JSType.toNarrowestNumber(newLength);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1446
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1447
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1448
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1449
     * ECMA 15.4.4.14 Array.prototype.indexOf ( searchElement [ , fromIndex ] )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1450
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1451
     * @param self           self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1452
     * @param searchElement  element to search for
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1453
     * @param fromIndex      start index of search
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1454
     * @return index of element, or -1 if not found
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1455
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1456
    @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
34974
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 34732
diff changeset
  1457
    public static double indexOf(final Object self, final Object searchElement, final Object fromIndex) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1458
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1459
            final ScriptObject sobj = (ScriptObject)Global.toObject(self);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1460
            final long         len  = JSType.toUint32(sobj.getLength());
21444
72ad507e18e0 8027016: Array.prototype.indexOf should return -1 when array is of length zero
sundar
parents: 21439
diff changeset
  1461
            if (len == 0) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1462
                return -1;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1463
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1464
21444
72ad507e18e0 8027016: Array.prototype.indexOf should return -1 when array is of length zero
sundar
parents: 21439
diff changeset
  1465
            final long         n = JSType.toLong(fromIndex);
72ad507e18e0 8027016: Array.prototype.indexOf should return -1 when array is of length zero
sundar
parents: 21439
diff changeset
  1466
            if (n >= len) {
72ad507e18e0 8027016: Array.prototype.indexOf should return -1 when array is of length zero
sundar
parents: 21439
diff changeset
  1467
                return -1;
72ad507e18e0 8027016: Array.prototype.indexOf should return -1 when array is of length zero
sundar
parents: 21439
diff changeset
  1468
            }
72ad507e18e0 8027016: Array.prototype.indexOf should return -1 when array is of length zero
sundar
parents: 21439
diff changeset
  1469
72ad507e18e0 8027016: Array.prototype.indexOf should return -1 when array is of length zero
sundar
parents: 21439
diff changeset
  1470
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1471
            for (long k = Math.max(0, n < 0 ? len - Math.abs(n) : n); k < len; k++) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1472
                if (sobj.has(k)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1473
                    if (ScriptRuntime.EQ_STRICT(sobj.get(k), searchElement)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1474
                        return k;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1475
                    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1476
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1477
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1478
        } catch (final ClassCastException | NullPointerException e) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1479
            //fallthru
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1480
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1481
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1482
        return -1;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1483
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1484
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1485
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1486
     * ECMA 15.4.4.15 Array.prototype.lastIndexOf ( searchElement [ , fromIndex ] )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1487
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1488
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1489
     * @param args arguments: element to search for and optional from index
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1490
     * @return index of element, or -1 if not found
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1491
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1492
    @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
34974
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 34732
diff changeset
  1493
    public static double lastIndexOf(final Object self, final Object... args) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1494
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1495
            final ScriptObject sobj = (ScriptObject)Global.toObject(self);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1496
            final long         len  = JSType.toUint32(sobj.getLength());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1497
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1498
            if (len == 0) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1499
                return -1;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1500
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1501
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1502
            final Object searchElement = args.length > 0 ? args[0] : ScriptRuntime.UNDEFINED;
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1503
            final long   n             = args.length > 1 ? JSType.toLong(args[1]) : len - 1;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1504
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1505
            for (long k = n < 0 ? len - Math.abs(n) : Math.min(n, len - 1); k >= 0; k--) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1506
                if (sobj.has(k)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1507
                    if (ScriptRuntime.EQ_STRICT(sobj.get(k), searchElement)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1508
                        return k;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1509
                    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1510
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1511
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1512
        } catch (final ClassCastException | NullPointerException e) {
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16226
diff changeset
  1513
            throw typeError("not.an.object", ScriptRuntime.safeToString(self));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1514
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1515
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1516
        return -1;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1517
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1518
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1519
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1520
     * ECMA 15.4.4.16 Array.prototype.every ( callbackfn [ , thisArg ] )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1521
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1522
     * @param self        self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1523
     * @param callbackfn  callback function per element
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1524
     * @param thisArg     this argument
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1525
     * @return true if callback function return true for every element in the array, false otherwise
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1526
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1527
    @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23761
diff changeset
  1528
    public static boolean every(final Object self, final Object callbackfn, final Object thisArg) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1529
        return applyEvery(Global.toObject(self), callbackfn, thisArg);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1530
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1531
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1532
    private static boolean applyEvery(final Object self, final Object callbackfn, final Object thisArg) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1533
        return new IteratorAction<Boolean>(Global.toObject(self), callbackfn, thisArg, true) {
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
  1534
            private final MethodHandle everyInvoker = getEVERY_CALLBACK_INVOKER();
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
  1535
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1536
            @Override
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
  1537
            protected boolean forEach(final Object val, final double i) throws Throwable {
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1538
                return result = (boolean)everyInvoker.invokeExact(callbackfn, thisArg, val, i, self);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1539
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1540
        }.apply();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1541
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1542
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1543
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1544
     * ECMA 15.4.4.17 Array.prototype.some ( callbackfn [ , thisArg ] )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1545
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1546
     * @param self        self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1547
     * @param callbackfn  callback function per element
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1548
     * @param thisArg     this argument
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1549
     * @return true if callback function returned true for any element in the array, false otherwise
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1550
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1551
    @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23761
diff changeset
  1552
    public static boolean some(final Object self, final Object callbackfn, final Object thisArg) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1553
        return new IteratorAction<Boolean>(Global.toObject(self), callbackfn, thisArg, false) {
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
  1554
            private final MethodHandle someInvoker = getSOME_CALLBACK_INVOKER();
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
  1555
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1556
            @Override
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
  1557
            protected boolean forEach(final Object val, final double i) throws Throwable {
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
  1558
                return !(result = (boolean)someInvoker.invokeExact(callbackfn, thisArg, val, i, self));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1559
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1560
        }.apply();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1561
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1562
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1563
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1564
     * ECMA 15.4.4.18 Array.prototype.forEach ( callbackfn [ , thisArg ] )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1565
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1566
     * @param self        self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1567
     * @param callbackfn  callback function per element
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1568
     * @param thisArg     this argument
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1569
     * @return undefined
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1570
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1571
    @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1572
    public static Object forEach(final Object self, final Object callbackfn, final Object thisArg) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1573
        return new IteratorAction<Object>(Global.toObject(self), callbackfn, thisArg, ScriptRuntime.UNDEFINED) {
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
  1574
            private final MethodHandle forEachInvoker = getFOREACH_CALLBACK_INVOKER();
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
  1575
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1576
            @Override
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
  1577
            protected boolean forEach(final Object val, final double i) throws Throwable {
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
  1578
                forEachInvoker.invokeExact(callbackfn, thisArg, val, i, self);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1579
                return true;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1580
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1581
        }.apply();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1582
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1583
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1584
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1585
     * ECMA 15.4.4.19 Array.prototype.map ( callbackfn [ , thisArg ] )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1586
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1587
     * @param self        self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1588
     * @param callbackfn  callback function per element
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1589
     * @param thisArg     this argument
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1590
     * @return array with elements transformed by map function
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1591
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1592
    @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23761
diff changeset
  1593
    public static NativeArray map(final Object self, final Object callbackfn, final Object thisArg) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1594
        return new IteratorAction<NativeArray>(Global.toObject(self), callbackfn, thisArg, null) {
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
  1595
            private final MethodHandle mapInvoker = getMAP_CALLBACK_INVOKER();
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
  1596
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1597
            @Override
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
  1598
            protected boolean forEach(final Object val, final double i) throws Throwable {
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
  1599
                final Object r = mapInvoker.invokeExact(callbackfn, thisArg, val, i, self);
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17983
diff changeset
  1600
                result.defineOwnProperty(ArrayIndex.getArrayIndex(index), r);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1601
                return true;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1602
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1603
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1604
            @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1605
            public void applyLoopBegin(final ArrayLikeIterator<Object> iter0) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1606
                // map return array should be of same length as source array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1607
                // even if callback reduces source array length
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1608
                result = new NativeArray(iter0.getLength());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1609
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1610
        }.apply();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1611
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1612
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1613
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1614
     * ECMA 15.4.4.20 Array.prototype.filter ( callbackfn [ , thisArg ] )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1615
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1616
     * @param self        self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1617
     * @param callbackfn  callback function per element
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1618
     * @param thisArg     this argument
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1619
     * @return filtered array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1620
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1621
    @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23761
diff changeset
  1622
    public static NativeArray filter(final Object self, final Object callbackfn, final Object thisArg) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1623
        return new IteratorAction<NativeArray>(Global.toObject(self), callbackfn, thisArg, new NativeArray()) {
17983
4b1e3d565f9b 8015350: Array.prototype.reduceRight issue with large length and index
hannesw
parents: 17764
diff changeset
  1624
            private long to = 0;
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
  1625
            private final MethodHandle filterInvoker = getFILTER_CALLBACK_INVOKER();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1626
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1627
            @Override
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
  1628
            protected boolean forEach(final Object val, final double i) throws Throwable {
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
  1629
                if ((boolean)filterInvoker.invokeExact(callbackfn, thisArg, val, i, self)) {
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents: 17983
diff changeset
  1630
                    result.defineOwnProperty(ArrayIndex.getArrayIndex(to++), val);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1631
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1632
                return true;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1633
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1634
        }.apply();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1635
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1636
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1637
    private static Object reduceInner(final ArrayLikeIterator<Object> iter, final Object self, final Object... args) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1638
        final Object  callbackfn          = args.length > 0 ? args[0] : ScriptRuntime.UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1639
        final boolean initialValuePresent = args.length > 1;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1640
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1641
        Object initialValue = initialValuePresent ? args[1] : ScriptRuntime.UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1642
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1643
        if (callbackfn == ScriptRuntime.UNDEFINED) {
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16226
diff changeset
  1644
            throw typeError("not.a.function", "undefined");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1645
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1646
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1647
        if (!initialValuePresent) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1648
            if (iter.hasNext()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1649
                initialValue = iter.next();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1650
            } else {
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16226
diff changeset
  1651
                throw typeError("array.reduce.invalid.init");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1652
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1653
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1654
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1655
        //if initial value is ScriptRuntime.UNDEFINED - step forward once.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1656
        return new IteratorAction<Object>(Global.toObject(self), callbackfn, ScriptRuntime.UNDEFINED, initialValue, iter) {
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
  1657
            private final MethodHandle reduceInvoker = getREDUCE_CALLBACK_INVOKER();
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
  1658
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1659
            @Override
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
  1660
            protected boolean forEach(final Object val, final double i) throws Throwable {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1661
                // TODO: why can't I declare the second arg as Undefined.class?
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
  1662
                result = reduceInvoker.invokeExact(callbackfn, ScriptRuntime.UNDEFINED, result, val, i, self);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1663
                return true;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1664
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1665
        }.apply();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1666
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1667
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1668
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1669
     * ECMA 15.4.4.21 Array.prototype.reduce ( callbackfn [ , initialValue ] )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1670
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1671
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1672
     * @param args arguments to reduce
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1673
     * @return accumulated result
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1674
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1675
    @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1676
    public static Object reduce(final Object self, final Object... args) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1677
        return reduceInner(arrayLikeIterator(self), self, args);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1678
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1679
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1680
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1681
     * ECMA 15.4.4.22 Array.prototype.reduceRight ( callbackfn [ , initialValue ] )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1682
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1683
     * @param self        self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1684
     * @param args arguments to reduce
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1685
     * @return accumulated result
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1686
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1687
    @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1688
    public static Object reduceRight(final Object self, final Object... args) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1689
        return reduceInner(reverseArrayLikeIterator(self), self, args);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1690
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1691
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1692
    /**
36023
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1693
     * ECMA6 22.1.3.4 Array.prototype.entries ( )
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1694
     *
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1695
     * @param self the self reference
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1696
     * @return an iterator over the array's entries
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1697
     */
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1698
    @Function(attributes = Attribute.NOT_ENUMERABLE)
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1699
    public static Object entries(final Object self) {
41619
dc96f5cd1a9c 8168140: TypedArrays should implement ES6 iterator protocol
hannesw
parents: 39795
diff changeset
  1700
        return ArrayIterator.newArrayKeyValueIterator(self);
36023
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1701
    }
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1702
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1703
    /**
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1704
     * ECMA6 22.1.3.13 Array.prototype.keys ( )
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1705
     *
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1706
     * @param self the self reference
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1707
     * @return an iterator over the array's keys
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1708
     */
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1709
    @Function(attributes = Attribute.NOT_ENUMERABLE)
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1710
    public static Object keys(final Object self) {
41619
dc96f5cd1a9c 8168140: TypedArrays should implement ES6 iterator protocol
hannesw
parents: 39795
diff changeset
  1711
        return ArrayIterator.newArrayKeyIterator(self);
36023
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1712
    }
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1713
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1714
    /**
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1715
     * ECMA6 22.1.3.29 Array.prototype.values ( )
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1716
     *
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1717
     * @param self the self reference
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1718
     * @return an iterator over the array's values
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1719
     */
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1720
    @Function(attributes = Attribute.NOT_ENUMERABLE)
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1721
    public static Object values(final Object self) {
41619
dc96f5cd1a9c 8168140: TypedArrays should implement ES6 iterator protocol
hannesw
parents: 39795
diff changeset
  1722
        return ArrayIterator.newArrayValueIterator(self);
36023
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1723
    }
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1724
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1725
    /**
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1726
     * 22.1.3.30 Array.prototype [ @@iterator ] ( )
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1727
     *
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1728
     * @param self the self reference
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1729
     * @return an iterator over the array's values
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1730
     */
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1731
    @Function(attributes = Attribute.NOT_ENUMERABLE, name = "@@iterator")
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1732
    public static Object getIterator(final Object self) {
41619
dc96f5cd1a9c 8168140: TypedArrays should implement ES6 iterator protocol
hannesw
parents: 39795
diff changeset
  1733
        return ArrayIterator.newArrayValueIterator(self);
36023
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1734
    }
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1735
528cc67f1289 8147558: Add support for ES6 collections
hannesw
parents: 35407
diff changeset
  1736
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1737
     * Determine if Java bulk array operations may be used on the underlying
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1738
     * storage. This is possible only if the object's prototype chain is empty
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1739
     * or each of the prototypes in the chain is empty.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1740
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1741
     * @param self the object to examine
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1742
     * @return true if optimizable
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1743
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1744
    private static boolean bulkable(final ScriptObject self) {
18328
ebd24057f163 8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents: 18318
diff changeset
  1745
        return self.isArray() && !hasInheritedArrayEntries(self) && !self.isLengthNotWritable();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1746
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1747
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1748
    private static boolean hasInheritedArrayEntries(final ScriptObject self) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1749
        ScriptObject proto = self.getProto();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1750
        while (proto != null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1751
            if (proto.hasArrayEntries()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1752
                return true;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1753
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1754
            proto = proto.getProto();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1755
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1756
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1757
        return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1758
    }
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1759
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1760
    @Override
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1761
    public String toString() {
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1762
        return "NativeArray@" + Debug.id(this) + " [" + getArray().getClass().getSimpleName() + ']';
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1763
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1764
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1765
    @Override
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1766
    public SpecializedFunction.LinkLogic getLinkLogic(final Class<? extends LinkLogic> clazz) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1767
        if (clazz == PushLinkLogic.class) {
27528
4a61cb4dfc6c 8064707: Remove NativeArray link logic fields
attila
parents: 27525
diff changeset
  1768
            return PushLinkLogic.INSTANCE;
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1769
        } else if (clazz == PopLinkLogic.class) {
27528
4a61cb4dfc6c 8064707: Remove NativeArray link logic fields
attila
parents: 27525
diff changeset
  1770
            return PopLinkLogic.INSTANCE;
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: 26886
diff changeset
  1771
        } else if (clazz == ConcatLinkLogic.class) {
27528
4a61cb4dfc6c 8064707: Remove NativeArray link logic fields
attila
parents: 27525
diff changeset
  1772
            return ConcatLinkLogic.INSTANCE;
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1773
        }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1774
        return null;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1775
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1776
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1777
    @Override
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1778
    public boolean hasPerInstanceAssumptions() {
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: 26886
diff changeset
  1779
        return true; //length writable switchpoint
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1780
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1781
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1782
    /**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1783
     * This is an abstract super class that contains common functionality for all
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1784
     * specialized optimistic builtins in NativeArray. For example, it handles the
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1785
     * modification switchpoint which is touched when length is written.
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1786
     */
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1787
    private static abstract class ArrayLinkLogic extends SpecializedFunction.LinkLogic {
27525
e7d3c00b9fb8 8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents: 27212
diff changeset
  1788
        protected ArrayLinkLogic() {
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1789
        }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1790
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1791
        protected static ContinuousArrayData getContinuousArrayData(final Object self) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1792
            try {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1793
                //cast to NativeArray, to avoid cases like x = {0:0, 1:1}, x.length = 2, where we can't use the array push/pop
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1794
                return (ContinuousArrayData)((NativeArray)self).getArray();
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1795
            } catch (final Exception e) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1796
                return null;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1797
            }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1798
        }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1799
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1800
        /**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1801
         * Push and pop callsites can throw ClassCastException as a mechanism to have them
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1802
         * relinked - this enabled fast checks of the kind of ((IntArrayData)arrayData).push(x)
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1803
         * for an IntArrayData only push - if this fails, a CCE will be thrown and we will relink
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1804
         */
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1805
        @Override
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1806
        public Class<? extends Throwable> getRelinkException() {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1807
            return ClassCastException.class;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1808
        }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1809
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1810
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1811
    /**
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: 26886
diff changeset
  1812
     * This is linker logic for optimistic concatenations
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: 26886
diff changeset
  1813
     */
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: 26886
diff changeset
  1814
    private static final class ConcatLinkLogic extends ArrayLinkLogic {
27528
4a61cb4dfc6c 8064707: Remove NativeArray link logic fields
attila
parents: 27525
diff changeset
  1815
        private static final LinkLogic INSTANCE = new ConcatLinkLogic();
4a61cb4dfc6c 8064707: Remove NativeArray link logic fields
attila
parents: 27525
diff changeset
  1816
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: 26886
diff changeset
  1817
        @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: 26886
diff changeset
  1818
        public boolean canLink(final Object self, final CallSiteDescriptor desc, final LinkRequest request) {
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: 26886
diff changeset
  1819
            final Object[] args = request.getArguments();
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: 26886
diff changeset
  1820
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: 26886
diff changeset
  1821
            if (args.length != 3) { //single argument check
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: 26886
diff changeset
  1822
                return false;
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: 26886
diff changeset
  1823
            }
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: 26886
diff changeset
  1824
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: 26886
diff changeset
  1825
            final ContinuousArrayData selfData = getContinuousArrayData(self);
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: 26886
diff changeset
  1826
            if (selfData == null) {
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: 26886
diff changeset
  1827
                return false;
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: 26886
diff changeset
  1828
            }
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: 26886
diff changeset
  1829
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: 26886
diff changeset
  1830
            final Object arg = args[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: 26886
diff changeset
  1831
            //args[2] continuousarray or non arraydata, let past non array datas
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: 26886
diff changeset
  1832
            if (arg instanceof NativeArray) {
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: 26886
diff changeset
  1833
                final ContinuousArrayData argData = getContinuousArrayData(arg);
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: 26886
diff changeset
  1834
                if (argData == null) {
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: 26886
diff changeset
  1835
                    return false;
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: 26886
diff changeset
  1836
                }
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: 26886
diff changeset
  1837
            }
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: 26886
diff changeset
  1838
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: 26886
diff changeset
  1839
            return true;
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: 26886
diff changeset
  1840
        }
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: 26886
diff changeset
  1841
    }
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: 26886
diff changeset
  1842
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: 26886
diff changeset
  1843
    /**
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1844
     * This is linker logic for optimistic pushes
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1845
     */
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1846
    private static final class PushLinkLogic extends ArrayLinkLogic {
27528
4a61cb4dfc6c 8064707: Remove NativeArray link logic fields
attila
parents: 27525
diff changeset
  1847
        private static final LinkLogic INSTANCE = new PushLinkLogic();
4a61cb4dfc6c 8064707: Remove NativeArray link logic fields
attila
parents: 27525
diff changeset
  1848
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1849
        @Override
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1850
        public boolean canLink(final Object self, final CallSiteDescriptor desc, final LinkRequest request) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1851
            return getContinuousArrayData(self) != null;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1852
        }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1853
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1854
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1855
    /**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1856
     * This is linker logic for optimistic pops
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1857
     */
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1858
    private static final class PopLinkLogic extends ArrayLinkLogic {
27528
4a61cb4dfc6c 8064707: Remove NativeArray link logic fields
attila
parents: 27525
diff changeset
  1859
        private static final LinkLogic INSTANCE = new PopLinkLogic();
4a61cb4dfc6c 8064707: Remove NativeArray link logic fields
attila
parents: 27525
diff changeset
  1860
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1861
        /**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1862
         * We need to check if we are dealing with a continuous non empty array data here,
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1863
         * as pop with a primitive return value returns undefined for arrays with length 0
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1864
         */
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1865
        @Override
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1866
        public boolean canLink(final Object self, final CallSiteDescriptor desc, final LinkRequest request) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1867
            final ContinuousArrayData data = getContinuousNonEmptyArrayData(self);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1868
            if (data != null) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1869
                final Class<?> elementType = data.getElementType();
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1870
                final Class<?> returnType  = desc.getMethodType().returnType();
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1871
                final boolean  typeFits    = JSType.getAccessorTypeIndex(returnType) >= JSType.getAccessorTypeIndex(elementType);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1872
                return typeFits;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1873
            }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1874
            return false;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1875
        }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1876
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
  1877
        private static ContinuousArrayData getContinuousNonEmptyArrayData(final Object self) {
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1878
            final ContinuousArrayData data = getContinuousArrayData(self);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1879
            if (data != null) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1880
                return data.length() == 0 ? null : data;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1881
            }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1882
            return null;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1883
        }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1884
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1885
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1886
    //runtime calls for push and pops. they could be used as guards, but they also perform the runtime logic,
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1887
    //so rather than synthesizing them into a guard method handle that would also perform the push on the
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1888
    //retrieved receiver, we use this as runtime logic
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1889
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1890
    //TODO - fold these into the Link logics, but I'll do that as a later step, as I want to do a checkin
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1891
    //where everything works first
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1892
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 30514
diff changeset
  1893
    private static <T> ContinuousArrayData getContinuousNonEmptyArrayDataCCE(final Object self, final Class<T> clazz) {
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1894
        try {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1895
            @SuppressWarnings("unchecked")
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1896
            final ContinuousArrayData data = (ContinuousArrayData)(T)((NativeArray)self).getArray();
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1897
            if (data.length() != 0L) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1898
                return data; //if length is 0 we cannot pop and have to relink, because then we'd have to return an undefined, which is a wider type than e.g. int
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1899
           }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1900
        } catch (final NullPointerException e) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1901
            //fallthru
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1902
        }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1903
        throw new ClassCastException();
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1904
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1905
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 30514
diff changeset
  1906
    private static ContinuousArrayData getContinuousArrayDataCCE(final Object self) {
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: 26886
diff changeset
  1907
        try {
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: 26886
diff changeset
  1908
            return (ContinuousArrayData)((NativeArray)self).getArray();
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: 26886
diff changeset
  1909
         } catch (final NullPointerException e) {
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: 26886
diff changeset
  1910
             throw new ClassCastException();
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: 26886
diff changeset
  1911
         }
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: 26886
diff changeset
  1912
    }
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: 26886
diff changeset
  1913
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 30514
diff changeset
  1914
    private static ContinuousArrayData getContinuousArrayDataCCE(final Object self, final Class<?> elementType) {
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1915
        try {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1916
           return (ContinuousArrayData)((NativeArray)self).getArray(elementType); //ensure element type can fit "elementType"
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1917
        } catch (final NullPointerException e) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1918
            throw new ClassCastException();
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1919
        }
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1920
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1921
}