author | attila |
Tue, 16 Jul 2013 17:03:30 +0200 | |
changeset 18885 | 6b6801c3b81a |
parent 18851 | bdb92c95f886 |
child 19088 | 153f268bfa72 |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, 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; |
|
32 |
import static jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator.arrayLikeIterator; |
|
33 |
import static jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator.reverseArrayLikeIterator; |
|
34 |
||
35 |
import java.lang.invoke.MethodHandle; |
|
36 |
import java.util.ArrayList; |
|
37 |
import java.util.Arrays; |
|
38 |
import java.util.Collections; |
|
39 |
import java.util.Comparator; |
|
40 |
import java.util.Iterator; |
|
41 |
import java.util.List; |
|
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
18328
diff
changeset
|
42 |
import jdk.nashorn.api.scripting.ScriptObjectMirror; |
16147 | 43 |
import jdk.nashorn.internal.objects.annotations.Attribute; |
44 |
import jdk.nashorn.internal.objects.annotations.Constructor; |
|
45 |
import jdk.nashorn.internal.objects.annotations.Function; |
|
46 |
import jdk.nashorn.internal.objects.annotations.Getter; |
|
47 |
import jdk.nashorn.internal.objects.annotations.ScriptClass; |
|
48 |
import jdk.nashorn.internal.objects.annotations.Setter; |
|
49 |
import jdk.nashorn.internal.objects.annotations.SpecializedConstructor; |
|
50 |
import jdk.nashorn.internal.objects.annotations.Where; |
|
51 |
import jdk.nashorn.internal.runtime.JSType; |
|
52 |
import jdk.nashorn.internal.runtime.PropertyDescriptor; |
|
18618
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
18606
diff
changeset
|
53 |
import jdk.nashorn.internal.runtime.PropertyMap; |
16147 | 54 |
import jdk.nashorn.internal.runtime.ScriptFunction; |
55 |
import jdk.nashorn.internal.runtime.ScriptObject; |
|
56 |
import jdk.nashorn.internal.runtime.ScriptRuntime; |
|
57 |
import jdk.nashorn.internal.runtime.Undefined; |
|
58 |
import jdk.nashorn.internal.runtime.arrays.ArrayData; |
|
59 |
import jdk.nashorn.internal.runtime.arrays.ArrayIndex; |
|
60 |
import jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator; |
|
61 |
import jdk.nashorn.internal.runtime.arrays.IteratorAction; |
|
62 |
import jdk.nashorn.internal.runtime.linker.Bootstrap; |
|
63 |
import jdk.nashorn.internal.runtime.linker.InvokeByName; |
|
64 |
||
65 |
/** |
|
66 |
* Runtime representation of a JavaScript array. NativeArray only holds numeric |
|
67 |
* keyed values. All other values are stored in spill. |
|
68 |
*/ |
|
69 |
@ScriptClass("Array") |
|
70 |
public final class NativeArray extends ScriptObject { |
|
71 |
private static final InvokeByName JOIN = new InvokeByName("join", ScriptObject.class); |
|
72 |
||
73 |
private static final MethodHandle EVERY_CALLBACK_INVOKER = createIteratorCallbackInvoker(boolean.class); |
|
74 |
private static final MethodHandle SOME_CALLBACK_INVOKER = createIteratorCallbackInvoker(boolean.class); |
|
75 |
private static final MethodHandle FOREACH_CALLBACK_INVOKER = createIteratorCallbackInvoker(void.class); |
|
76 |
private static final MethodHandle MAP_CALLBACK_INVOKER = createIteratorCallbackInvoker(Object.class); |
|
77 |
private static final MethodHandle FILTER_CALLBACK_INVOKER = createIteratorCallbackInvoker(boolean.class); |
|
78 |
||
79 |
private static final MethodHandle REDUCE_CALLBACK_INVOKER = Bootstrap.createDynamicInvoker("dyn:call", Object.class, |
|
17983
4b1e3d565f9b
8015350: Array.prototype.reduceRight issue with large length and index
hannesw
parents:
17764
diff
changeset
|
80 |
Object.class, Undefined.class, Object.class, Object.class, long.class, Object.class); |
16942
76c7fe301c79
8011974: Comparator function returning negative and positive Infinity does not work as expected with Array.prototype.sort
sundar
parents:
16941
diff
changeset
|
81 |
private static final MethodHandle CALL_CMP = Bootstrap.createDynamicInvoker("dyn:call", double.class, |
16147 | 82 |
ScriptFunction.class, Object.class, Object.class, Object.class); |
83 |
||
84 |
private static final InvokeByName TO_LOCALE_STRING = new InvokeByName("toLocaleString", ScriptObject.class, String.class); |
|
85 |
||
18618
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
18606
diff
changeset
|
86 |
// initialized by nasgen |
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
18606
diff
changeset
|
87 |
private static PropertyMap $nasgenmap$; |
16147 | 88 |
|
18851
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
89 |
static PropertyMap getInitialMap() { |
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
90 |
return $nasgenmap$; |
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
91 |
} |
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
92 |
|
16147 | 93 |
/* |
94 |
* Constructors. |
|
95 |
*/ |
|
96 |
NativeArray() { |
|
97 |
this(ArrayData.initialArray()); |
|
98 |
} |
|
99 |
||
100 |
NativeArray(final long length) { |
|
101 |
// TODO assert valid index in long before casting |
|
102 |
this(ArrayData.allocate((int) length)); |
|
103 |
} |
|
104 |
||
105 |
NativeArray(final int[] array) { |
|
106 |
this(ArrayData.allocate(array)); |
|
107 |
} |
|
108 |
||
109 |
NativeArray(final long[] array) { |
|
110 |
this(ArrayData.allocate(array)); |
|
111 |
} |
|
112 |
||
113 |
NativeArray(final double[] array) { |
|
114 |
this(ArrayData.allocate(array)); |
|
115 |
} |
|
116 |
||
117 |
NativeArray(final Object[] array) { |
|
118 |
this(ArrayData.allocate(array.length)); |
|
119 |
||
120 |
ArrayData arrayData = this.getArray(); |
|
121 |
arrayData.ensure(array.length - 1); |
|
122 |
||
123 |
for (int index = 0; index < array.length; index++) { |
|
124 |
final Object value = array[index]; |
|
125 |
||
126 |
if (value == ScriptRuntime.EMPTY) { |
|
127 |
arrayData = arrayData.delete(index); |
|
128 |
} else { |
|
18328
ebd24057f163
8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents:
18318
diff
changeset
|
129 |
arrayData = arrayData.set(index, value, false); |
16147 | 130 |
} |
131 |
} |
|
132 |
||
133 |
this.setArray(arrayData); |
|
134 |
} |
|
135 |
||
18618
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
18606
diff
changeset
|
136 |
NativeArray(final ArrayData arrayData) { |
18851
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
137 |
this(arrayData, Global.instance()); |
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
138 |
} |
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
139 |
|
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
140 |
NativeArray(final ArrayData arrayData, final Global global) { |
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
141 |
super(global.getArrayPrototype(), global.getArrayMap()); |
16147 | 142 |
this.setArray(arrayData); |
143 |
this.setIsArray(); |
|
144 |
} |
|
145 |
||
146 |
@Override |
|
147 |
public String getClassName() { |
|
148 |
return "Array"; |
|
149 |
} |
|
150 |
||
151 |
@Override |
|
152 |
public Object getLength() { |
|
153 |
return getArray().length() & JSType.MAX_UINT; |
|
154 |
} |
|
155 |
||
156 |
/** |
|
157 |
* ECMA 15.4.5.1 [[DefineOwnProperty]] ( P, Desc, Throw ) |
|
158 |
*/ |
|
159 |
@Override |
|
160 |
public boolean defineOwnProperty(final String key, final Object propertyDesc, final boolean reject) { |
|
161 |
final PropertyDescriptor desc = toPropertyDescriptor(Global.instance(), propertyDesc); |
|
162 |
||
163 |
// never be undefined as "length" is always defined and can't be deleted for arrays |
|
164 |
// Step 1 |
|
165 |
final PropertyDescriptor oldLenDesc = (PropertyDescriptor) super.getOwnPropertyDescriptor("length"); |
|
166 |
||
167 |
// Step 2 |
|
168 |
// get old length and convert to long |
|
169 |
long oldLen = NativeArray.validLength(oldLenDesc.getValue(), true); |
|
170 |
||
171 |
// Step 3 |
|
172 |
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
|
173 |
// check for length being made non-writable |
ebd24057f163
8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents:
18318
diff
changeset
|
174 |
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
|
175 |
setIsLengthNotWritable(); |
ebd24057f163
8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents:
18318
diff
changeset
|
176 |
} |
ebd24057f163
8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents:
18318
diff
changeset
|
177 |
|
16147 | 178 |
// Step 3a |
179 |
if (!desc.has(VALUE)) { |
|
16773
ed65c8a9ae7c
8011421: When using Object.defineProperty on arrays, PropertyDescriptor's property accessors are invoked multiple times
sundar
parents:
16769
diff
changeset
|
180 |
return super.defineOwnProperty("length", desc, reject); |
16147 | 181 |
} |
182 |
||
183 |
// Step 3b |
|
184 |
final PropertyDescriptor newLenDesc = desc; |
|
185 |
||
186 |
// Step 3c and 3d - get new length and convert to long |
|
187 |
final long newLen = NativeArray.validLength(newLenDesc.getValue(), true); |
|
188 |
||
189 |
// Step 3e |
|
190 |
newLenDesc.setValue(newLen); |
|
191 |
||
192 |
// Step 3f |
|
193 |
// increasing array length - just need to set new length value (and attributes if any) and return |
|
194 |
if (newLen >= oldLen) { |
|
195 |
return super.defineOwnProperty("length", newLenDesc, reject); |
|
196 |
} |
|
197 |
||
198 |
// Step 3g |
|
199 |
if (!oldLenDesc.isWritable()) { |
|
200 |
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
|
201 |
throw typeError("property.not.writable", "length", ScriptRuntime.safeToString(this)); |
16147 | 202 |
} |
203 |
return false; |
|
204 |
} |
|
205 |
||
206 |
// Step 3h and 3i |
|
207 |
final boolean newWritable = (!newLenDesc.has(WRITABLE) || newLenDesc.isWritable()); |
|
208 |
if (!newWritable) { |
|
209 |
newLenDesc.setWritable(true); |
|
210 |
} |
|
211 |
||
212 |
// Step 3j and 3k |
|
213 |
final boolean succeeded = super.defineOwnProperty("length", newLenDesc, reject); |
|
214 |
if (!succeeded) { |
|
215 |
return false; |
|
216 |
} |
|
217 |
||
218 |
// Step 3l |
|
219 |
// make sure that length is set till the point we can delete the old elements |
|
220 |
while (newLen < oldLen) { |
|
221 |
oldLen--; |
|
222 |
final boolean deleteSucceeded = delete(oldLen, false); |
|
223 |
if (!deleteSucceeded) { |
|
224 |
newLenDesc.setValue(oldLen + 1); |
|
225 |
if (!newWritable) { |
|
226 |
newLenDesc.setWritable(false); |
|
227 |
} |
|
228 |
super.defineOwnProperty("length", newLenDesc, false); |
|
229 |
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
|
230 |
throw typeError("property.not.writable", "length", ScriptRuntime.safeToString(this)); |
16147 | 231 |
} |
232 |
return false; |
|
233 |
} |
|
234 |
} |
|
235 |
||
236 |
// Step 3m |
|
237 |
if (!newWritable) { |
|
238 |
// make 'length' property not writable |
|
239 |
final ScriptObject newDesc = Global.newEmptyInstance(); |
|
240 |
newDesc.set(WRITABLE, false, false); |
|
241 |
return super.defineOwnProperty("length", newDesc, false); |
|
242 |
} |
|
243 |
||
244 |
return true; |
|
245 |
} |
|
246 |
||
247 |
// 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
|
248 |
final int index = ArrayIndex.getArrayIndex(key); |
16147 | 249 |
if (ArrayIndex.isValidArrayIndex(index)) { |
250 |
final long longIndex = ArrayIndex.toLongIndex(index); |
|
251 |
// Step 4b |
|
252 |
// setting an element beyond current length, but 'length' is not writable |
|
253 |
if (longIndex >= oldLen && !oldLenDesc.isWritable()) { |
|
254 |
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
|
255 |
throw typeError("property.not.writable", Long.toString(longIndex), ScriptRuntime.safeToString(this)); |
16147 | 256 |
} |
257 |
return false; |
|
258 |
} |
|
259 |
||
260 |
// Step 4c |
|
261 |
// 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
|
262 |
final boolean succeeded = super.defineOwnProperty(key, desc, false); |
16147 | 263 |
|
264 |
// Step 4d |
|
265 |
if (!succeeded) { |
|
266 |
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
|
267 |
throw typeError("cant.redefine.property", key, ScriptRuntime.safeToString(this)); |
16147 | 268 |
} |
269 |
return false; |
|
270 |
} |
|
271 |
||
272 |
// Step 4e -- adjust new length based on new element index that is set |
|
273 |
if (longIndex >= oldLen) { |
|
274 |
oldLenDesc.setValue(longIndex + 1); |
|
275 |
super.defineOwnProperty("length", oldLenDesc, false); |
|
276 |
} |
|
277 |
||
278 |
// Step 4f |
|
279 |
return true; |
|
280 |
} |
|
281 |
||
282 |
// 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
|
283 |
return super.defineOwnProperty(key, desc, reject); |
16147 | 284 |
} |
285 |
||
286 |
/** |
|
287 |
* Return the array contents upcasted as an ObjectArray, regardless of |
|
288 |
* representation |
|
289 |
* |
|
290 |
* @return an object array |
|
291 |
*/ |
|
292 |
public Object[] asObjectArray() { |
|
293 |
return getArray().asObjectArray(); |
|
294 |
} |
|
295 |
||
296 |
/** |
|
297 |
* ECMA 15.4.3.2 Array.isArray ( arg ) |
|
298 |
* |
|
299 |
* @param self self reference |
|
300 |
* @param arg argument - object to check |
|
301 |
* @return true if argument is an array |
|
302 |
*/ |
|
303 |
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR) |
|
304 |
public static Object isArray(final Object self, final Object arg) { |
|
305 |
return isArray(arg) || (arg == Global.instance().getArrayPrototype()) |
|
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
18328
diff
changeset
|
306 |
|| (arg instanceof NativeRegExpExecResult) |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
18328
diff
changeset
|
307 |
|| (arg instanceof ScriptObjectMirror && ((ScriptObjectMirror)arg).isArray()); |
16147 | 308 |
} |
309 |
||
310 |
/** |
|
311 |
* Length getter |
|
312 |
* @param self self reference |
|
313 |
* @return the length of the object |
|
314 |
*/ |
|
315 |
@Getter(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_CONFIGURABLE) |
|
316 |
public static Object length(final Object self) { |
|
317 |
if (isArray(self)) { |
|
17520 | 318 |
return ((ScriptObject) self).getArray().length() & JSType.MAX_UINT; |
16147 | 319 |
} |
320 |
||
321 |
return 0; |
|
322 |
} |
|
323 |
||
324 |
/** |
|
325 |
* Length setter |
|
326 |
* @param self self reference |
|
327 |
* @param length new length property |
|
328 |
*/ |
|
329 |
@Setter(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_CONFIGURABLE) |
|
330 |
public static void length(final Object self, final Object length) { |
|
331 |
if (isArray(self)) { |
|
17520 | 332 |
((ScriptObject) self).setLength(validLength(length, true)); |
16147 | 333 |
} |
334 |
} |
|
335 |
||
336 |
static long validLength(final Object length, final boolean reject) { |
|
337 |
final double doubleLength = JSType.toNumber(length); |
|
338 |
if (!Double.isNaN(doubleLength) && JSType.isRepresentableAsLong(doubleLength)) { |
|
339 |
final long len = (long) doubleLength; |
|
340 |
if (len >= 0 && len <= JSType.MAX_UINT) { |
|
341 |
return len; |
|
342 |
} |
|
343 |
} |
|
344 |
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
|
345 |
throw rangeError("inappropriate.array.length", ScriptRuntime.safeToString(length)); |
16147 | 346 |
} |
347 |
return -1; |
|
348 |
} |
|
349 |
||
350 |
/** |
|
351 |
* ECMA 15.4.4.2 Array.prototype.toString ( ) |
|
352 |
* |
|
353 |
* @param self self reference |
|
354 |
* @return string representation of array |
|
355 |
*/ |
|
356 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
357 |
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
|
358 |
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
|
359 |
if (obj instanceof ScriptObject) { |
f0d7cca4e7fd
8011365: Array.prototype.join and Array.prototype.toString do not throw TypeError on null, undefined
sundar
parents:
16767
diff
changeset
|
360 |
final ScriptObject sobj = (ScriptObject)obj; |
16147 | 361 |
try { |
362 |
final Object join = JOIN.getGetter().invokeExact(sobj); |
|
363 |
if (join instanceof ScriptFunction) { |
|
364 |
return JOIN.getInvoker().invokeExact(join, sobj); |
|
365 |
} |
|
366 |
} catch (final RuntimeException | Error e) { |
|
367 |
throw e; |
|
368 |
} catch (final Throwable t) { |
|
369 |
throw new RuntimeException(t); |
|
370 |
} |
|
371 |
} |
|
372 |
||
373 |
// FIXME: should lookup Object.prototype.toString and call that? |
|
374 |
return ScriptRuntime.builtinObjectToString(self); |
|
375 |
} |
|
376 |
||
377 |
/** |
|
378 |
* ECMA 15.4.4.3 Array.prototype.toLocaleString ( ) |
|
379 |
* |
|
380 |
* @param self self reference |
|
381 |
* @return locale specific string representation for array |
|
382 |
*/ |
|
383 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
384 |
public static Object toLocaleString(final Object self) { |
|
385 |
final StringBuilder sb = new StringBuilder(); |
|
386 |
final Iterator<Object> iter = arrayLikeIterator(self, true); |
|
387 |
||
388 |
while (iter.hasNext()) { |
|
389 |
final Object obj = iter.next(); |
|
390 |
||
391 |
if (obj != null && obj != ScriptRuntime.UNDEFINED) { |
|
16188 | 392 |
final Object val = JSType.toScriptObject(obj); |
16147 | 393 |
|
394 |
try { |
|
395 |
if (val instanceof ScriptObject) { |
|
396 |
final ScriptObject sobj = (ScriptObject)val; |
|
397 |
final Object toLocaleString = TO_LOCALE_STRING.getGetter().invokeExact(sobj); |
|
398 |
||
399 |
if (toLocaleString instanceof ScriptFunction) { |
|
400 |
sb.append((String)TO_LOCALE_STRING.getInvoker().invokeExact(toLocaleString, sobj)); |
|
401 |
} else { |
|
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16226
diff
changeset
|
402 |
throw typeError("not.a.function", "toLocaleString"); |
16147 | 403 |
} |
404 |
} |
|
405 |
} catch (final Error|RuntimeException t) { |
|
406 |
throw t; |
|
407 |
} catch (final Throwable t) { |
|
408 |
throw new RuntimeException(t); |
|
409 |
} |
|
410 |
} |
|
411 |
||
412 |
if (iter.hasNext()) { |
|
413 |
sb.append(","); |
|
414 |
} |
|
415 |
} |
|
416 |
||
417 |
return sb.toString(); |
|
418 |
} |
|
419 |
||
420 |
/** |
|
421 |
* ECMA 15.4.2.2 new Array (len) |
|
422 |
* |
|
423 |
* @param newObj was the new operator used to instantiate this array |
|
424 |
* @param self self reference |
|
425 |
* @param args arguments (length) |
|
426 |
* @return the new NativeArray |
|
427 |
*/ |
|
428 |
@Constructor(arity = 1) |
|
429 |
public static Object construct(final boolean newObj, final Object self, final Object... args) { |
|
430 |
switch (args.length) { |
|
431 |
case 0: |
|
432 |
return new NativeArray(0); |
|
433 |
case 1: |
|
434 |
final Object len = args[0]; |
|
435 |
if (len instanceof Number) { |
|
436 |
long length; |
|
437 |
if (len instanceof Integer || len instanceof Long) { |
|
438 |
length = ((Number) len).longValue(); |
|
17241
c337fefb8c84
8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents:
16942
diff
changeset
|
439 |
if (length >= 0 && length < JSType.MAX_UINT) { |
16147 | 440 |
return new NativeArray(length); |
441 |
} |
|
442 |
} |
|
443 |
||
444 |
length = JSType.toUint32(len); |
|
445 |
||
446 |
/* |
|
447 |
* If the argument len is a Number and ToUint32(len) is equal to |
|
448 |
* len, then the length property of the newly constructed object |
|
449 |
* is set to ToUint32(len). If the argument len is a Number and |
|
450 |
* ToUint32(len) is not equal to len, a RangeError exception is |
|
451 |
* thrown. |
|
452 |
*/ |
|
453 |
final double numberLength = ((Number) len).doubleValue(); |
|
454 |
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
|
455 |
throw rangeError("inappropriate.array.length", JSType.toString(numberLength)); |
16147 | 456 |
} |
457 |
||
458 |
return new NativeArray(length); |
|
459 |
} |
|
460 |
/* |
|
461 |
* If the argument len is not a Number, then the length property of |
|
462 |
* the newly constructed object is set to 1 and the 0 property of |
|
463 |
* the newly constructed object is set to len |
|
464 |
*/ |
|
465 |
return new NativeArray(new Object[]{args[0]}); |
|
466 |
//fallthru |
|
467 |
default: |
|
468 |
return new NativeArray(args); |
|
469 |
} |
|
470 |
} |
|
471 |
||
472 |
/** |
|
473 |
* ECMA 15.4.2.2 new Array (len) |
|
474 |
* |
|
475 |
* Specialized constructor for zero arguments - empty array |
|
476 |
* |
|
477 |
* @param newObj was the new operator used to instantiate this array |
|
478 |
* @param self self reference |
|
479 |
* @return the new NativeArray |
|
480 |
*/ |
|
481 |
@SpecializedConstructor |
|
482 |
public static Object construct(final boolean newObj, final Object self) { |
|
483 |
return new NativeArray(0); |
|
484 |
} |
|
485 |
||
486 |
/** |
|
487 |
* ECMA 15.4.2.2 new Array (len) |
|
488 |
* |
|
489 |
* Specialized constructor for one integer argument (length) |
|
490 |
* |
|
491 |
* @param newObj was the new operator used to instantiate this array |
|
492 |
* @param self self reference |
|
493 |
* @param length array length |
|
494 |
* @return the new NativeArray |
|
495 |
*/ |
|
496 |
@SpecializedConstructor |
|
497 |
public static Object construct(final boolean newObj, final Object self, final int length) { |
|
498 |
if (length >= 0) { |
|
499 |
return new NativeArray(length); |
|
500 |
} |
|
501 |
||
502 |
return construct(newObj, self, new Object[]{length}); |
|
503 |
} |
|
504 |
||
505 |
/** |
|
506 |
* ECMA 15.4.2.2 new Array (len) |
|
507 |
* |
|
508 |
* Specialized constructor for one long argument (length) |
|
509 |
* |
|
510 |
* @param newObj was the new operator used to instantiate this array |
|
511 |
* @param self self reference |
|
512 |
* @param length array length |
|
513 |
* @return the new NativeArray |
|
514 |
*/ |
|
515 |
@SpecializedConstructor |
|
516 |
public static Object construct(final boolean newObj, final Object self, final long length) { |
|
517 |
if (length >= 0L && length <= JSType.MAX_UINT) { |
|
518 |
return new NativeArray(length); |
|
519 |
} |
|
520 |
||
521 |
return construct(newObj, self, new Object[]{length}); |
|
522 |
} |
|
523 |
||
524 |
/** |
|
525 |
* ECMA 15.4.2.2 new Array (len) |
|
526 |
* |
|
527 |
* Specialized constructor for one double argument (length) |
|
528 |
* |
|
529 |
* @param newObj was the new operator used to instantiate this array |
|
530 |
* @param self self reference |
|
531 |
* @param length array length |
|
532 |
* @return the new NativeArray |
|
533 |
*/ |
|
534 |
@SpecializedConstructor |
|
535 |
public static Object construct(final boolean newObj, final Object self, final double length) { |
|
536 |
final long uint32length = JSType.toUint32(length); |
|
537 |
||
538 |
if (uint32length == length) { |
|
539 |
return new NativeArray(uint32length); |
|
540 |
} |
|
541 |
||
542 |
return construct(newObj, self, new Object[]{length}); |
|
543 |
} |
|
544 |
||
545 |
/** |
|
546 |
* ECMA 15.4.4.4 Array.prototype.concat ( [ item1 [ , item2 [ , ... ] ] ] ) |
|
547 |
* |
|
548 |
* @param self self reference |
|
549 |
* @param args arguments to concat |
|
550 |
* @return resulting NativeArray |
|
551 |
*/ |
|
552 |
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1) |
|
553 |
public static Object concat(final Object self, final Object... args) { |
|
554 |
final ArrayList<Object> list = new ArrayList<>(); |
|
18885
6b6801c3b81a
8015356: array concatenation should skip empty elements
attila
parents:
18851
diff
changeset
|
555 |
concatToList(list, Global.toObject(self)); |
6b6801c3b81a
8015356: array concatenation should skip empty elements
attila
parents:
18851
diff
changeset
|
556 |
|
6b6801c3b81a
8015356: array concatenation should skip empty elements
attila
parents:
18851
diff
changeset
|
557 |
for (final Object obj : args) { |
6b6801c3b81a
8015356: array concatenation should skip empty elements
attila
parents:
18851
diff
changeset
|
558 |
concatToList(list, obj); |
6b6801c3b81a
8015356: array concatenation should skip empty elements
attila
parents:
18851
diff
changeset
|
559 |
} |
6b6801c3b81a
8015356: array concatenation should skip empty elements
attila
parents:
18851
diff
changeset
|
560 |
|
6b6801c3b81a
8015356: array concatenation should skip empty elements
attila
parents:
18851
diff
changeset
|
561 |
return new NativeArray(list.toArray()); |
6b6801c3b81a
8015356: array concatenation should skip empty elements
attila
parents:
18851
diff
changeset
|
562 |
} |
16147 | 563 |
|
18885
6b6801c3b81a
8015356: array concatenation should skip empty elements
attila
parents:
18851
diff
changeset
|
564 |
private static void concatToList(final ArrayList<Object> list, final Object obj) { |
6b6801c3b81a
8015356: array concatenation should skip empty elements
attila
parents:
18851
diff
changeset
|
565 |
final boolean isScriptArray = isArray(obj); |
6b6801c3b81a
8015356: array concatenation should skip empty elements
attila
parents:
18851
diff
changeset
|
566 |
final boolean isScriptObject = isScriptArray || obj instanceof ScriptObject; |
6b6801c3b81a
8015356: array concatenation should skip empty elements
attila
parents:
18851
diff
changeset
|
567 |
if (isScriptArray || obj instanceof Iterable || (obj != null && obj.getClass().isArray())) { |
6b6801c3b81a
8015356: array concatenation should skip empty elements
attila
parents:
18851
diff
changeset
|
568 |
final Iterator<Object> iter = arrayLikeIterator(obj, true); |
6b6801c3b81a
8015356: array concatenation should skip empty elements
attila
parents:
18851
diff
changeset
|
569 |
if (iter.hasNext()) { |
6b6801c3b81a
8015356: array concatenation should skip empty elements
attila
parents:
18851
diff
changeset
|
570 |
for(int i = 0; iter.hasNext(); ++i) { |
6b6801c3b81a
8015356: array concatenation should skip empty elements
attila
parents:
18851
diff
changeset
|
571 |
final Object value = iter.next(); |
6b6801c3b81a
8015356: array concatenation should skip empty elements
attila
parents:
18851
diff
changeset
|
572 |
if(value == ScriptRuntime.UNDEFINED && isScriptObject && !((ScriptObject)obj).has(i)) { |
6b6801c3b81a
8015356: array concatenation should skip empty elements
attila
parents:
18851
diff
changeset
|
573 |
// 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
|
574 |
// UNDEFINED instead of an "includeUndefined" boolean with states SKIP, INCLUDE, |
6b6801c3b81a
8015356: array concatenation should skip empty elements
attila
parents:
18851
diff
changeset
|
575 |
// 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
|
576 |
// into the concatenated array. |
6b6801c3b81a
8015356: array concatenation should skip empty elements
attila
parents:
18851
diff
changeset
|
577 |
list.add(ScriptRuntime.EMPTY); |
6b6801c3b81a
8015356: array concatenation should skip empty elements
attila
parents:
18851
diff
changeset
|
578 |
} else { |
6b6801c3b81a
8015356: array concatenation should skip empty elements
attila
parents:
18851
diff
changeset
|
579 |
list.add(value); |
6b6801c3b81a
8015356: array concatenation should skip empty elements
attila
parents:
18851
diff
changeset
|
580 |
} |
6b6801c3b81a
8015356: array concatenation should skip empty elements
attila
parents:
18851
diff
changeset
|
581 |
} |
6b6801c3b81a
8015356: array concatenation should skip empty elements
attila
parents:
18851
diff
changeset
|
582 |
} else if (!isScriptArray) { |
6b6801c3b81a
8015356: array concatenation should skip empty elements
attila
parents:
18851
diff
changeset
|
583 |
list.add(obj); // add empty object, but not an empty array |
16147 | 584 |
} |
585 |
} else { |
|
586 |
// single element, add it |
|
18885
6b6801c3b81a
8015356: array concatenation should skip empty elements
attila
parents:
18851
diff
changeset
|
587 |
list.add(obj); |
16147 | 588 |
} |
589 |
} |
|
590 |
||
591 |
/** |
|
592 |
* ECMA 15.4.4.5 Array.prototype.join (separator) |
|
593 |
* |
|
594 |
* @param self self reference |
|
595 |
* @param separator element separator |
|
596 |
* @return string representation after join |
|
597 |
*/ |
|
598 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
599 |
public static Object join(final Object self, final Object separator) { |
|
600 |
final StringBuilder sb = new StringBuilder(); |
|
601 |
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
|
602 |
final String sep = separator == ScriptRuntime.UNDEFINED ? "," : JSType.toString(separator); |
16147 | 603 |
|
604 |
while (iter.hasNext()) { |
|
605 |
final Object obj = iter.next(); |
|
606 |
||
607 |
if (obj != null && obj != ScriptRuntime.UNDEFINED) { |
|
608 |
sb.append(JSType.toString(obj)); |
|
609 |
} |
|
610 |
||
611 |
if (iter.hasNext()) { |
|
612 |
sb.append(sep); |
|
613 |
} |
|
614 |
} |
|
615 |
||
616 |
return sb.toString(); |
|
617 |
} |
|
618 |
||
619 |
/** |
|
620 |
* ECMA 15.4.4.6 Array.prototype.pop () |
|
621 |
* |
|
622 |
* @param self self reference |
|
623 |
* @return array after pop |
|
624 |
*/ |
|
625 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
626 |
public static Object pop(final Object self) { |
|
627 |
try { |
|
628 |
final ScriptObject sobj = (ScriptObject)self; |
|
629 |
||
630 |
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
|
631 |
return sobj.getArray().pop(); |
16147 | 632 |
} |
633 |
||
634 |
final long len = JSType.toUint32(sobj.getLength()); |
|
635 |
||
636 |
if (len == 0) { |
|
18328
ebd24057f163
8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents:
18318
diff
changeset
|
637 |
sobj.set("length", 0, true); |
16147 | 638 |
return ScriptRuntime.UNDEFINED; |
639 |
} |
|
640 |
||
641 |
final long index = len - 1; |
|
642 |
final Object element = sobj.get(index); |
|
643 |
||
18328
ebd24057f163
8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents:
18318
diff
changeset
|
644 |
sobj.delete(index, true); |
ebd24057f163
8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents:
18318
diff
changeset
|
645 |
sobj.set("length", index, true); |
16147 | 646 |
|
647 |
return element; |
|
648 |
} 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
|
649 |
throw typeError("not.an.object", ScriptRuntime.safeToString(self)); |
16147 | 650 |
} |
651 |
} |
|
652 |
||
653 |
/** |
|
654 |
* ECMA 15.4.4.7 Array.prototype.push (args...) |
|
655 |
* |
|
656 |
* @param self self reference |
|
657 |
* @param args arguments to push |
|
658 |
* @return array after pushes |
|
659 |
*/ |
|
660 |
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1) |
|
661 |
public static Object push(final Object self, final Object... args) { |
|
662 |
try { |
|
663 |
final ScriptObject sobj = (ScriptObject)self; |
|
664 |
||
665 |
if (bulkable(sobj)) { |
|
17520 | 666 |
if (sobj.getArray().length() + args.length <= JSType.MAX_UINT) { |
18328
ebd24057f163
8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents:
18318
diff
changeset
|
667 |
final ArrayData newData = sobj.getArray().push(true, args); |
17520 | 668 |
sobj.setArray(newData); |
16147 | 669 |
return newData.length(); |
670 |
} |
|
671 |
//fallthru |
|
672 |
} |
|
673 |
||
674 |
long len = JSType.toUint32(sobj.getLength()); |
|
675 |
for (final Object element : args) { |
|
18328
ebd24057f163
8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents:
18318
diff
changeset
|
676 |
sobj.set(len++, element, true); |
16147 | 677 |
} |
18328
ebd24057f163
8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents:
18318
diff
changeset
|
678 |
sobj.set("length", len, true); |
16147 | 679 |
|
680 |
return len; |
|
681 |
} 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
|
682 |
throw typeError("not.an.object", ScriptRuntime.safeToString(self)); |
16147 | 683 |
} |
684 |
} |
|
685 |
||
686 |
/** |
|
687 |
* ECMA 15.4.4.8 Array.prototype.reverse () |
|
688 |
* |
|
689 |
* @param self self reference |
|
690 |
* @return reversed array |
|
691 |
*/ |
|
692 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
693 |
public static Object reverse(final Object self) { |
|
694 |
try { |
|
695 |
final ScriptObject sobj = (ScriptObject)self; |
|
696 |
final long len = JSType.toUint32(sobj.getLength()); |
|
697 |
final long middle = len / 2; |
|
698 |
||
699 |
for (long lower = 0; lower != middle; lower++) { |
|
700 |
final long upper = len - lower - 1; |
|
701 |
final Object lowerValue = sobj.get(lower); |
|
702 |
final Object upperValue = sobj.get(upper); |
|
703 |
final boolean lowerExists = sobj.has(lower); |
|
704 |
final boolean upperExists = sobj.has(upper); |
|
705 |
||
706 |
if (lowerExists && upperExists) { |
|
18328
ebd24057f163
8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents:
18318
diff
changeset
|
707 |
sobj.set(lower, upperValue, true); |
ebd24057f163
8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents:
18318
diff
changeset
|
708 |
sobj.set(upper, lowerValue, true); |
16147 | 709 |
} 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
|
710 |
sobj.set(lower, upperValue, true); |
ebd24057f163
8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents:
18318
diff
changeset
|
711 |
sobj.delete(upper, true); |
16147 | 712 |
} 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
|
713 |
sobj.delete(lower, true); |
ebd24057f163
8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents:
18318
diff
changeset
|
714 |
sobj.set(upper, lowerValue, true); |
16147 | 715 |
} |
716 |
} |
|
717 |
return sobj; |
|
718 |
} 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
|
719 |
throw typeError("not.an.object", ScriptRuntime.safeToString(self)); |
16147 | 720 |
} |
721 |
} |
|
722 |
||
723 |
/** |
|
724 |
* ECMA 15.4.4.9 Array.prototype.shift () |
|
725 |
* |
|
726 |
* @param self self reference |
|
727 |
* @return shifted array |
|
728 |
*/ |
|
729 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
730 |
public static Object shift(final Object self) { |
|
731 |
final Object obj = Global.toObject(self); |
|
732 |
||
733 |
Object first = ScriptRuntime.UNDEFINED; |
|
734 |
||
735 |
if (!(obj instanceof ScriptObject)) { |
|
736 |
return first; |
|
737 |
} |
|
738 |
||
739 |
final ScriptObject sobj = (ScriptObject) obj; |
|
740 |
||
741 |
long len = JSType.toUint32(sobj.getLength()); |
|
742 |
||
743 |
if (len > 0) { |
|
744 |
first = sobj.get(0); |
|
745 |
||
746 |
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
|
747 |
sobj.getArray().shiftLeft(1); |
16147 | 748 |
} else { |
749 |
for (long k = 1; k < len; k++) { |
|
18328
ebd24057f163
8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents:
18318
diff
changeset
|
750 |
sobj.set(k - 1, sobj.get(k), true); |
16147 | 751 |
} |
752 |
} |
|
18328
ebd24057f163
8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents:
18318
diff
changeset
|
753 |
sobj.delete(--len, true); |
16147 | 754 |
} else { |
755 |
len = 0; |
|
756 |
} |
|
757 |
||
18328
ebd24057f163
8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents:
18318
diff
changeset
|
758 |
sobj.set("length", len, true); |
16147 | 759 |
|
760 |
return first; |
|
761 |
} |
|
762 |
||
763 |
/** |
|
764 |
* ECMA 15.4.4.10 Array.prototype.slice ( start [ , end ] ) |
|
765 |
* |
|
766 |
* @param self self reference |
|
767 |
* @param start start of slice (inclusive) |
|
768 |
* @param end end of slice (optional, exclusive) |
|
769 |
* @return sliced array |
|
770 |
*/ |
|
771 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
772 |
public static Object slice(final Object self, final Object start, final Object end) { |
|
773 |
final Object obj = Global.toObject(self); |
|
774 |
final ScriptObject sobj = (ScriptObject)obj; |
|
775 |
final long len = JSType.toUint32(sobj.getLength()); |
|
17764
29e6eb3bb9df
8010804: Review long and integer usage conventions
hannesw
parents:
17520
diff
changeset
|
776 |
final long relativeStart = JSType.toLong(start); |
29e6eb3bb9df
8010804: Review long and integer usage conventions
hannesw
parents:
17520
diff
changeset
|
777 |
final long relativeEnd = (end == ScriptRuntime.UNDEFINED) ? len : JSType.toLong(end); |
16147 | 778 |
|
17764
29e6eb3bb9df
8010804: Review long and integer usage conventions
hannesw
parents:
17520
diff
changeset
|
779 |
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
|
780 |
final long finale = relativeEnd < 0 ? Math.max(len + relativeEnd, 0) : Math.min(relativeEnd, len); |
16147 | 781 |
|
782 |
if (k >= finale) { |
|
783 |
return new NativeArray(0); |
|
784 |
} |
|
785 |
||
786 |
if (bulkable(sobj)) { |
|
17520 | 787 |
return new NativeArray(sobj.getArray().slice(k, finale)); |
16147 | 788 |
} |
789 |
||
790 |
final NativeArray copy = new NativeArray(0); |
|
791 |
for (long n = 0; k < finale; n++, k++) { |
|
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
|
792 |
copy.defineOwnProperty(ArrayIndex.getArrayIndex(n), sobj.get(k)); |
16147 | 793 |
} |
794 |
||
795 |
return copy; |
|
796 |
} |
|
797 |
||
798 |
private static ScriptFunction compareFunction(final Object comparefn) { |
|
16941 | 799 |
if (comparefn == ScriptRuntime.UNDEFINED) { |
800 |
return null; |
|
16147 | 801 |
} |
16941 | 802 |
|
803 |
if (! (comparefn instanceof ScriptFunction)) { |
|
804 |
throw typeError("not.a.function", ScriptRuntime.safeToString(comparefn)); |
|
805 |
} |
|
806 |
||
807 |
return (ScriptFunction)comparefn; |
|
16147 | 808 |
} |
809 |
||
810 |
private static Object[] sort(final Object[] array, final Object comparefn) { |
|
811 |
final ScriptFunction cmp = compareFunction(comparefn); |
|
812 |
||
813 |
final List<Object> list = Arrays.asList(array); |
|
16188 | 814 |
final Object cmpThis = cmp == null || cmp.isStrict() ? ScriptRuntime.UNDEFINED : Global.instance(); |
16147 | 815 |
|
816 |
Collections.sort(list, new Comparator<Object>() { |
|
817 |
@Override |
|
818 |
public int compare(final Object x, final Object y) { |
|
819 |
if (x == ScriptRuntime.UNDEFINED && y == ScriptRuntime.UNDEFINED) { |
|
820 |
return 0; |
|
821 |
} else if (x == ScriptRuntime.UNDEFINED) { |
|
822 |
return 1; |
|
823 |
} else if (y == ScriptRuntime.UNDEFINED) { |
|
824 |
return -1; |
|
825 |
} |
|
826 |
||
827 |
if (cmp != null) { |
|
828 |
try { |
|
16942
76c7fe301c79
8011974: Comparator function returning negative and positive Infinity does not work as expected with Array.prototype.sort
sundar
parents:
16941
diff
changeset
|
829 |
return (int)Math.signum((double)CALL_CMP.invokeExact(cmp, cmpThis, x, y)); |
16147 | 830 |
} catch (final RuntimeException | Error e) { |
831 |
throw e; |
|
832 |
} catch (final Throwable t) { |
|
833 |
throw new RuntimeException(t); |
|
834 |
} |
|
835 |
} |
|
836 |
||
837 |
return JSType.toString(x).compareTo(JSType.toString(y)); |
|
838 |
} |
|
839 |
}); |
|
840 |
||
841 |
return list.toArray(new Object[array.length]); |
|
842 |
} |
|
843 |
||
844 |
/** |
|
845 |
* ECMA 15.4.4.11 Array.prototype.sort ( comparefn ) |
|
846 |
* |
|
847 |
* @param self self reference |
|
848 |
* @param comparefn element comparison function |
|
849 |
* @return sorted array |
|
850 |
*/ |
|
851 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
852 |
public static Object sort(final Object self, final Object comparefn) { |
|
853 |
try { |
|
854 |
final ScriptObject sobj = (ScriptObject) self; |
|
855 |
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
|
856 |
ArrayData array = sobj.getArray(); |
16147 | 857 |
|
858 |
if (len > 1) { |
|
16779
61fd89656ce3
8011552: Arrays with missing elements are not properly sorted
sundar
parents:
16773
diff
changeset
|
859 |
// 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
|
860 |
// 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
|
861 |
final ArrayList<Object> src = new ArrayList<>(); |
18318
5e4244619d79
8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
17983
diff
changeset
|
862 |
for (long i = 0; i < len; i = array.nextIndex(i)) { |
5e4244619d79
8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
17983
diff
changeset
|
863 |
if (array.has((int) i)) { |
5e4244619d79
8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
17983
diff
changeset
|
864 |
src.add(array.getObject((int) i)); |
16779
61fd89656ce3
8011552: Arrays with missing elements are not properly sorted
sundar
parents:
16773
diff
changeset
|
865 |
} |
16147 | 866 |
} |
867 |
||
16779
61fd89656ce3
8011552: Arrays with missing elements are not properly sorted
sundar
parents:
16773
diff
changeset
|
868 |
final Object[] sorted = sort(src.toArray(), comparefn); |
16147 | 869 |
|
870 |
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
|
871 |
array = array.set(i, sorted[i], true); |
16147 | 872 |
} |
16779
61fd89656ce3
8011552: Arrays with missing elements are not properly sorted
sundar
parents:
16773
diff
changeset
|
873 |
|
61fd89656ce3
8011552: Arrays with missing elements are not properly sorted
sundar
parents:
16773
diff
changeset
|
874 |
// delete missing elements - which are at the end of sorted array |
18606 | 875 |
if (sorted.length != len) { |
876 |
array = array.delete(sorted.length, len - 1); |
|
877 |
} |
|
878 |
||
879 |
sobj.setArray(array); |
|
880 |
} |
|
16147 | 881 |
|
882 |
return sobj; |
|
883 |
} 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
|
884 |
throw typeError("not.an.object", ScriptRuntime.safeToString(self)); |
16147 | 885 |
} |
886 |
} |
|
887 |
||
888 |
/** |
|
889 |
* ECMA 15.4.4.12 Array.prototype.splice ( start, deleteCount [ item1 [ , item2 [ , ... ] ] ] ) |
|
890 |
* |
|
891 |
* @param self self reference |
|
892 |
* @param args arguments |
|
893 |
* @return result of splice |
|
894 |
*/ |
|
895 |
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 2) |
|
896 |
public static Object splice(final Object self, final Object... args) { |
|
897 |
final Object obj = Global.toObject(self); |
|
898 |
||
899 |
if (!(obj instanceof ScriptObject)) { |
|
900 |
return ScriptRuntime.UNDEFINED; |
|
901 |
} |
|
902 |
||
903 |
final Object start = (args.length > 0) ? args[0] : ScriptRuntime.UNDEFINED; |
|
904 |
final Object deleteCount = (args.length > 1) ? args[1] : ScriptRuntime.UNDEFINED; |
|
905 |
||
906 |
Object[] items; |
|
907 |
||
908 |
if (args.length > 2) { |
|
909 |
items = new Object[args.length - 2]; |
|
910 |
System.arraycopy(args, 2, items, 0, items.length); |
|
911 |
} else { |
|
912 |
items = ScriptRuntime.EMPTY_ARRAY; |
|
913 |
} |
|
914 |
||
915 |
final ScriptObject sobj = (ScriptObject)obj; |
|
916 |
final long len = JSType.toUint32(sobj.getLength()); |
|
17764
29e6eb3bb9df
8010804: Review long and integer usage conventions
hannesw
parents:
17520
diff
changeset
|
917 |
final long relativeStart = JSType.toLong(start); |
16147 | 918 |
|
17764
29e6eb3bb9df
8010804: Review long and integer usage conventions
hannesw
parents:
17520
diff
changeset
|
919 |
final long actualStart = relativeStart < 0 ? Math.max(len + relativeStart, 0) : Math.min(relativeStart, len); |
29e6eb3bb9df
8010804: Review long and integer usage conventions
hannesw
parents:
17520
diff
changeset
|
920 |
final long actualDeleteCount = Math.min(Math.max(JSType.toLong(deleteCount), 0), len - actualStart); |
16147 | 921 |
|
922 |
final NativeArray array = new NativeArray(actualDeleteCount); |
|
923 |
||
924 |
for (long k = 0; k < actualDeleteCount; k++) { |
|
925 |
final long from = actualStart + k; |
|
926 |
||
927 |
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
|
928 |
array.defineOwnProperty(ArrayIndex.getArrayIndex(k), sobj.get(from)); |
16147 | 929 |
} |
930 |
} |
|
931 |
||
932 |
if (items.length < actualDeleteCount) { |
|
933 |
for (long k = actualStart; k < (len - actualDeleteCount); k++) { |
|
934 |
final long from = k + actualDeleteCount; |
|
935 |
final long to = k + items.length; |
|
936 |
||
937 |
if (sobj.has(from)) { |
|
18328
ebd24057f163
8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents:
18318
diff
changeset
|
938 |
sobj.set(to, sobj.get(from), true); |
16147 | 939 |
} else { |
18328
ebd24057f163
8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents:
18318
diff
changeset
|
940 |
sobj.delete(to, true); |
16147 | 941 |
} |
942 |
} |
|
943 |
||
944 |
for (long k = len; k > (len - actualDeleteCount + items.length); k--) { |
|
18328
ebd24057f163
8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents:
18318
diff
changeset
|
945 |
sobj.delete(k - 1, true); |
16147 | 946 |
} |
947 |
} else if (items.length > actualDeleteCount) { |
|
948 |
for (long k = len - actualDeleteCount; k > actualStart; k--) { |
|
949 |
final long from = k + actualDeleteCount - 1; |
|
950 |
final long to = k + items.length - 1; |
|
951 |
||
952 |
if (sobj.has(from)) { |
|
953 |
final Object fromValue = sobj.get(from); |
|
18328
ebd24057f163
8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents:
18318
diff
changeset
|
954 |
sobj.set(to, fromValue, true); |
16147 | 955 |
} else { |
18328
ebd24057f163
8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents:
18318
diff
changeset
|
956 |
sobj.delete(to, true); |
16147 | 957 |
} |
958 |
} |
|
959 |
} |
|
960 |
||
961 |
long k = actualStart; |
|
962 |
for (int i = 0; i < items.length; i++, k++) { |
|
18328
ebd24057f163
8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents:
18318
diff
changeset
|
963 |
sobj.set(k, items[i], true); |
16147 | 964 |
} |
965 |
||
966 |
final long newLength = len - actualDeleteCount + items.length; |
|
18328
ebd24057f163
8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents:
18318
diff
changeset
|
967 |
sobj.set("length", newLength, true); |
16147 | 968 |
|
969 |
return array; |
|
970 |
} |
|
971 |
||
972 |
/** |
|
973 |
* ECMA 15.4.4.13 Array.prototype.unshift ( [ item1 [ , item2 [ , ... ] ] ] ) |
|
974 |
* |
|
975 |
* @param self self reference |
|
976 |
* @param items items for unshift |
|
977 |
* @return unshifted array |
|
978 |
*/ |
|
979 |
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1) |
|
980 |
public static Object unshift(final Object self, final Object... items) { |
|
981 |
final Object obj = Global.toObject(self); |
|
982 |
||
983 |
if (!(obj instanceof ScriptObject)) { |
|
984 |
return ScriptRuntime.UNDEFINED; |
|
985 |
} |
|
986 |
||
987 |
final ScriptObject sobj = (ScriptObject)obj; |
|
988 |
final long len = JSType.toUint32(sobj.getLength()); |
|
989 |
||
990 |
if (items == null) { |
|
991 |
return ScriptRuntime.UNDEFINED; |
|
992 |
} |
|
993 |
||
994 |
if (bulkable(sobj)) { |
|
17520 | 995 |
sobj.getArray().shiftRight(items.length); |
16147 | 996 |
|
997 |
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
|
998 |
sobj.setArray(sobj.getArray().set(j, items[j], true)); |
16147 | 999 |
} |
1000 |
} else { |
|
1001 |
for (long k = len; k > 0; k--) { |
|
1002 |
final long from = k - 1; |
|
1003 |
final long to = k + items.length - 1; |
|
1004 |
||
1005 |
if (sobj.has(from)) { |
|
1006 |
final Object fromValue = sobj.get(from); |
|
18328
ebd24057f163
8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents:
18318
diff
changeset
|
1007 |
sobj.set(to, fromValue, true); |
16147 | 1008 |
} else { |
18328
ebd24057f163
8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents:
18318
diff
changeset
|
1009 |
sobj.delete(to, true); |
16147 | 1010 |
} |
1011 |
} |
|
1012 |
||
1013 |
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
|
1014 |
sobj.set(j, items[j], true); |
16147 | 1015 |
} |
1016 |
} |
|
1017 |
||
1018 |
final long newLength = len + items.length; |
|
18328
ebd24057f163
8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents:
18318
diff
changeset
|
1019 |
sobj.set("length", newLength, true); |
16147 | 1020 |
|
1021 |
return newLength; |
|
1022 |
} |
|
1023 |
||
1024 |
/** |
|
1025 |
* ECMA 15.4.4.14 Array.prototype.indexOf ( searchElement [ , fromIndex ] ) |
|
1026 |
* |
|
1027 |
* @param self self reference |
|
1028 |
* @param searchElement element to search for |
|
1029 |
* @param fromIndex start index of search |
|
1030 |
* @return index of element, or -1 if not found |
|
1031 |
*/ |
|
1032 |
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1) |
|
1033 |
public static Object indexOf(final Object self, final Object searchElement, final Object fromIndex) { |
|
1034 |
try { |
|
1035 |
final ScriptObject sobj = (ScriptObject)Global.toObject(self); |
|
1036 |
final long len = JSType.toUint32(sobj.getLength()); |
|
1037 |
final long n = JSType.toLong(fromIndex); |
|
1038 |
||
1039 |
if (len == 0 || n >= len) { |
|
1040 |
return -1; |
|
1041 |
} |
|
1042 |
||
1043 |
for (long k = Math.max(0, (n < 0) ? (len - Math.abs(n)) : n); k < len; k++) { |
|
1044 |
if (sobj.has(k)) { |
|
1045 |
if (ScriptRuntime.EQ_STRICT(sobj.get(k), searchElement)) { |
|
1046 |
return k; |
|
1047 |
} |
|
1048 |
} |
|
1049 |
} |
|
1050 |
} catch (final ClassCastException | NullPointerException e) { |
|
1051 |
//fallthru |
|
1052 |
} |
|
1053 |
||
1054 |
return -1; |
|
1055 |
} |
|
1056 |
||
1057 |
/** |
|
1058 |
* ECMA 15.4.4.15 Array.prototype.lastIndexOf ( searchElement [ , fromIndex ] ) |
|
1059 |
* |
|
1060 |
* @param self self reference |
|
1061 |
* @param args arguments: element to search for and optional from index |
|
1062 |
* @return index of element, or -1 if not found |
|
1063 |
*/ |
|
1064 |
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1) |
|
1065 |
public static Object lastIndexOf(final Object self, final Object... args) { |
|
1066 |
try { |
|
1067 |
final ScriptObject sobj = (ScriptObject)Global.toObject(self); |
|
1068 |
final long len = JSType.toUint32(sobj.getLength()); |
|
1069 |
||
1070 |
if (len == 0) { |
|
1071 |
return -1; |
|
1072 |
} |
|
1073 |
||
1074 |
final Object searchElement = (args.length > 0) ? args[0] : ScriptRuntime.UNDEFINED; |
|
1075 |
final long n = (args.length > 1) ? JSType.toLong(args[1]) : (len - 1); |
|
1076 |
||
1077 |
for (long k = (n < 0) ? (len - Math.abs(n)) : Math.min(n, len - 1); k >= 0; k--) { |
|
1078 |
if (sobj.has(k)) { |
|
1079 |
if (ScriptRuntime.EQ_STRICT(sobj.get(k), searchElement)) { |
|
1080 |
return k; |
|
1081 |
} |
|
1082 |
} |
|
1083 |
} |
|
1084 |
} 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
|
1085 |
throw typeError("not.an.object", ScriptRuntime.safeToString(self)); |
16147 | 1086 |
} |
1087 |
||
1088 |
return -1; |
|
1089 |
} |
|
1090 |
||
1091 |
/** |
|
1092 |
* ECMA 15.4.4.16 Array.prototype.every ( callbackfn [ , thisArg ] ) |
|
1093 |
* |
|
1094 |
* @param self self reference |
|
1095 |
* @param callbackfn callback function per element |
|
1096 |
* @param thisArg this argument |
|
1097 |
* @return true if callback function return true for every element in the array, false otherwise |
|
1098 |
*/ |
|
1099 |
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1) |
|
1100 |
public static Object every(final Object self, final Object callbackfn, final Object thisArg) { |
|
1101 |
return applyEvery(Global.toObject(self), callbackfn, thisArg); |
|
1102 |
} |
|
1103 |
||
1104 |
private static boolean applyEvery(final Object self, final Object callbackfn, final Object thisArg) { |
|
1105 |
return new IteratorAction<Boolean>(Global.toObject(self), callbackfn, thisArg, true) { |
|
1106 |
@Override |
|
17983
4b1e3d565f9b
8015350: Array.prototype.reduceRight issue with large length and index
hannesw
parents:
17764
diff
changeset
|
1107 |
protected boolean forEach(final Object val, final long i) throws Throwable { |
16147 | 1108 |
return (result = (boolean)EVERY_CALLBACK_INVOKER.invokeExact(callbackfn, thisArg, val, i, self)); |
1109 |
} |
|
1110 |
}.apply(); |
|
1111 |
} |
|
1112 |
||
1113 |
/** |
|
1114 |
* ECMA 15.4.4.17 Array.prototype.some ( callbackfn [ , thisArg ] ) |
|
1115 |
* |
|
1116 |
* @param self self reference |
|
1117 |
* @param callbackfn callback function per element |
|
1118 |
* @param thisArg this argument |
|
1119 |
* @return true if callback function returned true for any element in the array, false otherwise |
|
1120 |
*/ |
|
1121 |
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1) |
|
1122 |
public static Object some(final Object self, final Object callbackfn, final Object thisArg) { |
|
1123 |
return new IteratorAction<Boolean>(Global.toObject(self), callbackfn, thisArg, false) { |
|
1124 |
@Override |
|
17983
4b1e3d565f9b
8015350: Array.prototype.reduceRight issue with large length and index
hannesw
parents:
17764
diff
changeset
|
1125 |
protected boolean forEach(final Object val, final long i) throws Throwable { |
16147 | 1126 |
return !(result = (boolean)SOME_CALLBACK_INVOKER.invokeExact(callbackfn, thisArg, val, i, self)); |
1127 |
} |
|
1128 |
}.apply(); |
|
1129 |
} |
|
1130 |
||
1131 |
/** |
|
1132 |
* ECMA 15.4.4.18 Array.prototype.forEach ( callbackfn [ , thisArg ] ) |
|
1133 |
* |
|
1134 |
* @param self self reference |
|
1135 |
* @param callbackfn callback function per element |
|
1136 |
* @param thisArg this argument |
|
1137 |
* @return undefined |
|
1138 |
*/ |
|
1139 |
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1) |
|
1140 |
public static Object forEach(final Object self, final Object callbackfn, final Object thisArg) { |
|
1141 |
return new IteratorAction<Object>(Global.toObject(self), callbackfn, thisArg, ScriptRuntime.UNDEFINED) { |
|
1142 |
@Override |
|
17983
4b1e3d565f9b
8015350: Array.prototype.reduceRight issue with large length and index
hannesw
parents:
17764
diff
changeset
|
1143 |
protected boolean forEach(final Object val, final long i) throws Throwable { |
16147 | 1144 |
FOREACH_CALLBACK_INVOKER.invokeExact(callbackfn, thisArg, val, i, self); |
1145 |
return true; |
|
1146 |
} |
|
1147 |
}.apply(); |
|
1148 |
} |
|
1149 |
||
1150 |
/** |
|
1151 |
* ECMA 15.4.4.19 Array.prototype.map ( callbackfn [ , thisArg ] ) |
|
1152 |
* |
|
1153 |
* @param self self reference |
|
1154 |
* @param callbackfn callback function per element |
|
1155 |
* @param thisArg this argument |
|
1156 |
* @return array with elements transformed by map function |
|
1157 |
*/ |
|
1158 |
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1) |
|
1159 |
public static Object map(final Object self, final Object callbackfn, final Object thisArg) { |
|
1160 |
return new IteratorAction<NativeArray>(Global.toObject(self), callbackfn, thisArg, null) { |
|
1161 |
@Override |
|
17983
4b1e3d565f9b
8015350: Array.prototype.reduceRight issue with large length and index
hannesw
parents:
17764
diff
changeset
|
1162 |
protected boolean forEach(final Object val, final long i) throws Throwable { |
16147 | 1163 |
final Object r = MAP_CALLBACK_INVOKER.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
|
1164 |
result.defineOwnProperty(ArrayIndex.getArrayIndex(index), r); |
16147 | 1165 |
return true; |
1166 |
} |
|
1167 |
||
1168 |
@Override |
|
1169 |
public void applyLoopBegin(final ArrayLikeIterator<Object> iter0) { |
|
1170 |
// map return array should be of same length as source array |
|
1171 |
// even if callback reduces source array length |
|
1172 |
result = new NativeArray(iter0.getLength()); |
|
1173 |
} |
|
1174 |
}.apply(); |
|
1175 |
} |
|
1176 |
||
1177 |
/** |
|
1178 |
* ECMA 15.4.4.20 Array.prototype.filter ( callbackfn [ , thisArg ] ) |
|
1179 |
* |
|
1180 |
* @param self self reference |
|
1181 |
* @param callbackfn callback function per element |
|
1182 |
* @param thisArg this argument |
|
1183 |
* @return filtered array |
|
1184 |
*/ |
|
1185 |
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1) |
|
1186 |
public static Object filter(final Object self, final Object callbackfn, final Object thisArg) { |
|
1187 |
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
|
1188 |
private long to = 0; |
16147 | 1189 |
|
1190 |
@Override |
|
17983
4b1e3d565f9b
8015350: Array.prototype.reduceRight issue with large length and index
hannesw
parents:
17764
diff
changeset
|
1191 |
protected boolean forEach(final Object val, final long i) throws Throwable { |
16147 | 1192 |
if ((boolean)FILTER_CALLBACK_INVOKER.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
|
1193 |
result.defineOwnProperty(ArrayIndex.getArrayIndex(to++), val); |
16147 | 1194 |
} |
1195 |
return true; |
|
1196 |
} |
|
1197 |
}.apply(); |
|
1198 |
} |
|
1199 |
||
1200 |
private static Object reduceInner(final ArrayLikeIterator<Object> iter, final Object self, final Object... args) { |
|
1201 |
final Object callbackfn = args.length > 0 ? args[0] : ScriptRuntime.UNDEFINED; |
|
1202 |
final boolean initialValuePresent = args.length > 1; |
|
1203 |
||
1204 |
Object initialValue = initialValuePresent ? args[1] : ScriptRuntime.UNDEFINED; |
|
1205 |
||
1206 |
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
|
1207 |
throw typeError("not.a.function", "undefined"); |
16147 | 1208 |
} |
1209 |
||
1210 |
if (!initialValuePresent) { |
|
1211 |
if (iter.hasNext()) { |
|
1212 |
initialValue = iter.next(); |
|
1213 |
} else { |
|
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16226
diff
changeset
|
1214 |
throw typeError("array.reduce.invalid.init"); |
16147 | 1215 |
} |
1216 |
} |
|
1217 |
||
1218 |
//if initial value is ScriptRuntime.UNDEFINED - step forward once. |
|
1219 |
return new IteratorAction<Object>(Global.toObject(self), callbackfn, ScriptRuntime.UNDEFINED, initialValue, iter) { |
|
1220 |
@Override |
|
17983
4b1e3d565f9b
8015350: Array.prototype.reduceRight issue with large length and index
hannesw
parents:
17764
diff
changeset
|
1221 |
protected boolean forEach(final Object val, final long i) throws Throwable { |
16147 | 1222 |
// TODO: why can't I declare the second arg as Undefined.class? |
1223 |
result = REDUCE_CALLBACK_INVOKER.invokeExact(callbackfn, ScriptRuntime.UNDEFINED, result, val, i, self); |
|
1224 |
return true; |
|
1225 |
} |
|
1226 |
}.apply(); |
|
1227 |
} |
|
1228 |
||
1229 |
/** |
|
1230 |
* ECMA 15.4.4.21 Array.prototype.reduce ( callbackfn [ , initialValue ] ) |
|
1231 |
* |
|
1232 |
* @param self self reference |
|
1233 |
* @param args arguments to reduce |
|
1234 |
* @return accumulated result |
|
1235 |
*/ |
|
1236 |
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1) |
|
1237 |
public static Object reduce(final Object self, final Object... args) { |
|
1238 |
return reduceInner(arrayLikeIterator(self), self, args); |
|
1239 |
} |
|
1240 |
||
1241 |
/** |
|
1242 |
* ECMA 15.4.4.22 Array.prototype.reduceRight ( callbackfn [ , initialValue ] ) |
|
1243 |
* |
|
1244 |
* @param self self reference |
|
1245 |
* @param args arguments to reduce |
|
1246 |
* @return accumulated result |
|
1247 |
*/ |
|
1248 |
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1) |
|
1249 |
public static Object reduceRight(final Object self, final Object... args) { |
|
1250 |
return reduceInner(reverseArrayLikeIterator(self), self, args); |
|
1251 |
} |
|
1252 |
||
1253 |
/** |
|
1254 |
* Determine if Java bulk array operations may be used on the underlying |
|
1255 |
* storage. This is possible only if the object's prototype chain is empty |
|
1256 |
* or each of the prototypes in the chain is empty. |
|
1257 |
* |
|
1258 |
* @param self the object to examine |
|
1259 |
* @return true if optimizable |
|
1260 |
*/ |
|
1261 |
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
|
1262 |
return self.isArray() && !hasInheritedArrayEntries(self) && !self.isLengthNotWritable(); |
16147 | 1263 |
} |
1264 |
||
1265 |
private static boolean hasInheritedArrayEntries(final ScriptObject self) { |
|
1266 |
ScriptObject proto = self.getProto(); |
|
1267 |
while (proto != null) { |
|
1268 |
if (proto.hasArrayEntries()) { |
|
1269 |
return true; |
|
1270 |
} |
|
1271 |
proto = proto.getProto(); |
|
1272 |
} |
|
1273 |
||
1274 |
return false; |
|
1275 |
} |
|
1276 |
||
1277 |
private static MethodHandle createIteratorCallbackInvoker(final Class<?> rtype) { |
|
1278 |
return Bootstrap.createDynamicInvoker("dyn:call", rtype, Object.class, Object.class, Object.class, |
|
17983
4b1e3d565f9b
8015350: Array.prototype.reduceRight issue with large length and index
hannesw
parents:
17764
diff
changeset
|
1279 |
long.class, Object.class); |
16147 | 1280 |
|
1281 |
} |
|
1282 |
} |