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