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