author | hannesw |
Wed, 21 Mar 2018 16:55:34 +0100 | |
changeset 49275 | c639a6b33c5c |
parent 47216 | 71c04702a3d5 |
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.runtime.arrays; |
|
27 |
||
24720 | 28 |
import static jdk.nashorn.internal.codegen.CompilerConstants.specialCall; |
28576 | 29 |
|
24719 | 30 |
import java.lang.invoke.MethodHandle; |
31 |
import java.lang.invoke.MethodHandles; |
|
16147 | 32 |
import java.util.Arrays; |
33 |
import jdk.nashorn.internal.runtime.JSType; |
|
34 |
import jdk.nashorn.internal.runtime.ScriptRuntime; |
|
35 |
||
36 |
/** |
|
37 |
* Implementation of {@link ArrayData} as soon as an int has been |
|
38 |
* written to the array. This is the default data for new arrays |
|
39 |
*/ |
|
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
40 |
final class IntArrayData extends ContinuousArrayData implements IntElements { |
16147 | 41 |
/** |
42 |
* The wrapped array |
|
43 |
*/ |
|
44 |
private int[] array; |
|
45 |
||
46 |
IntArrayData() { |
|
47 |
this(new int[ArrayData.CHUNK_SIZE], 0); |
|
48 |
} |
|
49 |
||
50 |
IntArrayData(final int length) { |
|
51 |
super(length); |
|
52 |
this.array = new int[ArrayData.nextSize(length)]; |
|
53 |
} |
|
54 |
||
55 |
/** |
|
56 |
* Constructor |
|
57 |
* @param array an int array |
|
58 |
* @param length a length, not necessarily array.length |
|
59 |
*/ |
|
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:
27104
diff
changeset
|
60 |
IntArrayData(final int[] array, final int length) { |
16147 | 61 |
super(length); |
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:
27104
diff
changeset
|
62 |
assert array == null || array.length >= length; |
16147 | 63 |
this.array = array; |
19236
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18879
diff
changeset
|
64 |
} |
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18879
diff
changeset
|
65 |
|
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
66 |
@Override |
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:
27104
diff
changeset
|
67 |
public final Class<?> getElementType() { |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
68 |
return int.class; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
69 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
70 |
|
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:
27104
diff
changeset
|
71 |
@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:
27104
diff
changeset
|
72 |
public final Class<?> 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:
27104
diff
changeset
|
73 |
return Integer.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:
27104
diff
changeset
|
74 |
} |
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:
27104
diff
changeset
|
75 |
|
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:
27104
diff
changeset
|
76 |
@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:
27104
diff
changeset
|
77 |
public final int getElementWeight() { |
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:
27104
diff
changeset
|
78 |
return 1; |
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:
27104
diff
changeset
|
79 |
} |
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:
27104
diff
changeset
|
80 |
|
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:
27104
diff
changeset
|
81 |
@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:
27104
diff
changeset
|
82 |
public final ContinuousArrayData widest(final ContinuousArrayData otherData) { |
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:
27104
diff
changeset
|
83 |
return otherData; |
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:
27104
diff
changeset
|
84 |
} |
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:
27104
diff
changeset
|
85 |
|
24741
4232289c3235
8040102: Remove all references to Unsafe and definition of anonymous clases from the code
lagergren
parents:
24720
diff
changeset
|
86 |
private static final MethodHandle HAS_GET_ELEM = specialCall(MethodHandles.lookup(), IntArrayData.class, "getElem", int.class, int.class).methodHandle(); |
4232289c3235
8040102: Remove all references to Unsafe and definition of anonymous clases from the code
lagergren
parents:
24720
diff
changeset
|
87 |
private static final MethodHandle SET_ELEM = specialCall(MethodHandles.lookup(), IntArrayData.class, "setElem", void.class, int.class, int.class).methodHandle(); |
24719 | 88 |
|
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
89 |
@Override |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
90 |
public Object[] asObjectArray() { |
27098
2875b30458d3
8060011: Concatenating an array and converting it to Java gives wrong result
hannesw
parents:
26886
diff
changeset
|
91 |
return toObjectArray(true); |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
92 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
93 |
|
24719 | 94 |
@SuppressWarnings("unused") |
95 |
private int getElem(final int index) { |
|
96 |
if (has(index)) { |
|
97 |
return array[index]; |
|
98 |
} |
|
99 |
throw new ClassCastException(); |
|
100 |
} |
|
101 |
||
102 |
@SuppressWarnings("unused") |
|
103 |
private void setElem(final int index, final int elem) { |
|
24720 | 104 |
if (hasRoomFor(index)) { |
105 |
array[index] = elem; |
|
106 |
return; |
|
107 |
} |
|
108 |
throw new ClassCastException(); |
|
24719 | 109 |
} |
110 |
||
111 |
@Override |
|
112 |
public MethodHandle getElementGetter(final Class<?> returnType, final int programPoint) { |
|
113 |
return getContinuousElementGetter(HAS_GET_ELEM, returnType, programPoint); |
|
114 |
} |
|
115 |
||
116 |
@Override |
|
117 |
public MethodHandle getElementSetter(final Class<?> elementType) { |
|
118 |
return elementType == int.class ? getContinuousElementSetter(SET_ELEM, elementType) : null; |
|
119 |
} |
|
120 |
||
19236
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18879
diff
changeset
|
121 |
@Override |
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:
27104
diff
changeset
|
122 |
public IntArrayData copy() { |
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
123 |
return new IntArrayData(array.clone(), (int)length()); |
16147 | 124 |
} |
125 |
||
126 |
@Override |
|
127 |
public Object asArrayOfType(final Class<?> componentType) { |
|
24719 | 128 |
if (componentType == int.class) { |
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
129 |
final int len = (int)length(); |
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
130 |
return array.length == len ? array.clone() : Arrays.copyOf(array, len); |
16147 | 131 |
} |
132 |
return super.asArrayOfType(componentType); |
|
133 |
} |
|
134 |
||
27098
2875b30458d3
8060011: Concatenating an array and converting it to Java gives wrong result
hannesw
parents:
26886
diff
changeset
|
135 |
private Object[] toObjectArray(final boolean trim) { |
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
136 |
assert length() <= array.length : "length exceeds internal array size"; |
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
137 |
final int len = (int)length(); |
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
138 |
final Object[] oarray = new Object[trim ? len : array.length]; |
18879
382986b34105
8020283: Don't use exceptions for widening of ArrayData
hannesw
parents:
16151
diff
changeset
|
139 |
|
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
140 |
for (int index = 0; index < len; index++) { |
31549
b627094c5649
8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents:
30703
diff
changeset
|
141 |
oarray[index] = array[index]; |
16147 | 142 |
} |
143 |
||
144 |
return oarray; |
|
145 |
} |
|
146 |
||
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
147 |
private double[] toDoubleArray() { |
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
148 |
assert length() <= array.length : "length exceeds internal array size"; |
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
149 |
final int len = (int)length(); |
16147 | 150 |
final double[] darray = new double[array.length]; |
18879
382986b34105
8020283: Don't use exceptions for widening of ArrayData
hannesw
parents:
16151
diff
changeset
|
151 |
|
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
152 |
for (int index = 0; index < len; index++) { |
16147 | 153 |
darray[index] = array[index]; |
154 |
} |
|
155 |
||
156 |
return darray; |
|
157 |
} |
|
158 |
||
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
159 |
private NumberArrayData convertToDouble() { |
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
160 |
return new NumberArrayData(toDoubleArray(), (int)length()); |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
161 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
162 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
163 |
private ObjectArrayData convertToObject() { |
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
164 |
return new ObjectArrayData(toObjectArray(false), (int)length()); |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
165 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
166 |
|
16147 | 167 |
@Override |
168 |
public ArrayData convert(final Class<?> type) { |
|
30703
60aecf19ea09
8079145: jdk.nashorn.internal.runtime.arrays.IntArrayData.convert assertion
sundar
parents:
28787
diff
changeset
|
169 |
if (type == Integer.class || type == Byte.class || type == Short.class) { |
16147 | 170 |
return this; |
30703
60aecf19ea09
8079145: jdk.nashorn.internal.runtime.arrays.IntArrayData.convert assertion
sundar
parents:
28787
diff
changeset
|
171 |
} else if (type == Double.class || type == Float.class) { |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
172 |
return convertToDouble(); |
16147 | 173 |
} else { |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
174 |
return convertToObject(); |
16147 | 175 |
} |
176 |
} |
|
177 |
||
178 |
@Override |
|
42790
edb30aa1a99b
8171219: Missing checks in sparse array shift() implementation
hannesw
parents:
34974
diff
changeset
|
179 |
public ArrayData shiftLeft(final int by) { |
edb30aa1a99b
8171219: Missing checks in sparse array shift() implementation
hannesw
parents:
34974
diff
changeset
|
180 |
if (by >= length()) { |
edb30aa1a99b
8171219: Missing checks in sparse array shift() implementation
hannesw
parents:
34974
diff
changeset
|
181 |
shrink(0); |
edb30aa1a99b
8171219: Missing checks in sparse array shift() implementation
hannesw
parents:
34974
diff
changeset
|
182 |
} else { |
edb30aa1a99b
8171219: Missing checks in sparse array shift() implementation
hannesw
parents:
34974
diff
changeset
|
183 |
System.arraycopy(array, by, array, 0, array.length - by); |
edb30aa1a99b
8171219: Missing checks in sparse array shift() implementation
hannesw
parents:
34974
diff
changeset
|
184 |
} |
edb30aa1a99b
8171219: Missing checks in sparse array shift() implementation
hannesw
parents:
34974
diff
changeset
|
185 |
setLength(Math.max(0, length() - by)); |
edb30aa1a99b
8171219: Missing checks in sparse array shift() implementation
hannesw
parents:
34974
diff
changeset
|
186 |
|
edb30aa1a99b
8171219: Missing checks in sparse array shift() implementation
hannesw
parents:
34974
diff
changeset
|
187 |
return this; |
16147 | 188 |
} |
189 |
||
190 |
@Override |
|
191 |
public ArrayData shiftRight(final int by) { |
|
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
192 |
final ArrayData newData = ensure(by + length() - 1); |
16147 | 193 |
if (newData != this) { |
194 |
newData.shiftRight(by); |
|
195 |
return newData; |
|
196 |
} |
|
197 |
System.arraycopy(array, 0, array, by, array.length - by); |
|
198 |
||
199 |
return this; |
|
200 |
} |
|
201 |
||
202 |
@Override |
|
203 |
public ArrayData ensure(final long safeIndex) { |
|
24720 | 204 |
if (safeIndex >= SparseArrayData.MAX_DENSE_LENGTH) { |
16147 | 205 |
return new SparseArrayData(this, safeIndex + 1); |
206 |
} |
|
24720 | 207 |
final int alen = array.length; |
208 |
if (safeIndex >= alen) { |
|
209 |
final int newLength = ArrayData.nextSize((int)safeIndex); |
|
16147 | 210 |
array = Arrays.copyOf(array, newLength); |
211 |
} |
|
28787 | 212 |
if (safeIndex >= length()) { |
213 |
setLength(safeIndex + 1); |
|
214 |
} |
|
16147 | 215 |
return this; |
216 |
} |
|
217 |
||
218 |
@Override |
|
219 |
public ArrayData shrink(final long newLength) { |
|
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:
27104
diff
changeset
|
220 |
Arrays.fill(array, (int)newLength, array.length, 0); |
16147 | 221 |
return this; |
222 |
} |
|
223 |
||
224 |
@Override |
|
225 |
public ArrayData set(final int index, final Object value, final boolean strict) { |
|
24719 | 226 |
if (JSType.isRepresentableAsInt(value)) { |
227 |
return set(index, JSType.toInt32(value), strict); |
|
18879
382986b34105
8020283: Don't use exceptions for widening of ArrayData
hannesw
parents:
16151
diff
changeset
|
228 |
} else if (value == ScriptRuntime.UNDEFINED) { |
382986b34105
8020283: Don't use exceptions for widening of ArrayData
hannesw
parents:
16151
diff
changeset
|
229 |
return new UndefinedArrayFilter(this).set(index, value, strict); |
16147 | 230 |
} |
231 |
||
232 |
final ArrayData newData = convert(value == null ? Object.class : value.getClass()); |
|
233 |
return newData.set(index, value, strict); |
|
234 |
} |
|
235 |
||
236 |
@Override |
|
237 |
public ArrayData set(final int index, final int value, final boolean strict) { |
|
238 |
array[index] = value; |
|
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
239 |
setLength(Math.max(index + 1, length())); |
16147 | 240 |
|
241 |
return this; |
|
242 |
} |
|
243 |
||
244 |
@Override |
|
245 |
public ArrayData set(final int index, final double value, final boolean strict) { |
|
246 |
if (JSType.isRepresentableAsInt(value)) { |
|
247 |
array[index] = (int)(long)value; |
|
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
248 |
setLength(Math.max(index + 1, length())); |
16147 | 249 |
return this; |
250 |
} |
|
251 |
||
252 |
return convert(Double.class).set(index, value, strict); |
|
253 |
} |
|
254 |
||
255 |
@Override |
|
256 |
public int getInt(final int index) { |
|
257 |
return array[index]; |
|
258 |
} |
|
259 |
||
260 |
@Override |
|
24719 | 261 |
public int getIntOptimistic(final int index, final int programPoint) { |
262 |
return array[index]; |
|
263 |
} |
|
264 |
||
265 |
@Override |
|
16147 | 266 |
public double getDouble(final int index) { |
267 |
return array[index]; |
|
268 |
} |
|
269 |
||
270 |
@Override |
|
24719 | 271 |
public double getDoubleOptimistic(final int index, final int programPoint) { |
272 |
return array[index]; |
|
273 |
} |
|
274 |
||
275 |
@Override |
|
16147 | 276 |
public Object getObject(final int index) { |
277 |
return array[index]; |
|
278 |
} |
|
279 |
||
280 |
@Override |
|
281 |
public boolean has(final int index) { |
|
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
282 |
return 0 <= index && index < length(); |
16147 | 283 |
} |
284 |
||
285 |
@Override |
|
286 |
public ArrayData delete(final int index) { |
|
287 |
return new DeletedRangeArrayFilter(this, index, index); |
|
288 |
} |
|
289 |
||
290 |
@Override |
|
291 |
public ArrayData delete(final long fromIndex, final long toIndex) { |
|
292 |
return new DeletedRangeArrayFilter(this, fromIndex, toIndex); |
|
293 |
} |
|
294 |
||
295 |
@Override |
|
296 |
public Object pop() { |
|
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
297 |
final int len = (int)length(); |
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
298 |
if (len == 0) { |
16147 | 299 |
return ScriptRuntime.UNDEFINED; |
300 |
} |
|
301 |
||
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
302 |
final int newLength = len - 1; |
16147 | 303 |
final int elem = array[newLength]; |
304 |
array[newLength] = 0; |
|
305 |
setLength(newLength); |
|
306 |
||
307 |
return elem; |
|
308 |
} |
|
309 |
||
310 |
@Override |
|
311 |
public ArrayData slice(final long from, final long to) { |
|
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
312 |
return new IntArrayData(Arrays.copyOfRange(array, (int)from, (int)to), (int)(to - (from < 0 ? from + length() : from))); |
16147 | 313 |
} |
21438
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
314 |
|
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
315 |
@Override |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
316 |
public ArrayData fastSplice(final int start, final int removed, final int added) throws UnsupportedOperationException { |
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
317 |
final long oldLength = length(); |
21438
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
318 |
final long newLength = oldLength - removed + added; |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
319 |
if (newLength > SparseArrayData.MAX_DENSE_LENGTH && newLength > array.length) { |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
320 |
throw new UnsupportedOperationException(); |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
321 |
} |
24720 | 322 |
final ArrayData returnValue = removed == 0 ? |
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:
27104
diff
changeset
|
323 |
EMPTY_ARRAY : |
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:
27104
diff
changeset
|
324 |
new IntArrayData( |
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:
27104
diff
changeset
|
325 |
Arrays.copyOfRange( |
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:
27104
diff
changeset
|
326 |
array, |
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:
27104
diff
changeset
|
327 |
start, |
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:
27104
diff
changeset
|
328 |
start + removed), |
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:
27104
diff
changeset
|
329 |
removed); |
21438
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
330 |
|
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
331 |
if (newLength != oldLength) { |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
332 |
final int[] newArray; |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
333 |
|
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
334 |
if (newLength > array.length) { |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
335 |
newArray = new int[ArrayData.nextSize((int)newLength)]; |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
336 |
System.arraycopy(array, 0, newArray, 0, start); |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
337 |
} else { |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
338 |
newArray = array; |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
339 |
} |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
340 |
|
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
341 |
System.arraycopy(array, start + removed, newArray, start + added, (int)(oldLength - start - removed)); |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
342 |
array = newArray; |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
343 |
setLength(newLength); |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
344 |
} |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
345 |
|
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
346 |
return returnValue; |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
347 |
} |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
348 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
349 |
@Override |
34974
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
34732
diff
changeset
|
350 |
public double fastPush(final int arg) { |
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
351 |
final int len = (int)length(); |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
352 |
if (len == array.length) { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
353 |
array = Arrays.copyOf(array, nextSize(len)); |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
354 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
355 |
array[len] = arg; |
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
356 |
return increaseLength(); |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
357 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
358 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
359 |
//length must not be zero |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
360 |
@Override |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
361 |
public int fastPopInt() { |
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
362 |
if (length() == 0) { |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
363 |
throw new ClassCastException(); //relink |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
364 |
} |
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
365 |
final int newLength = (int)decreaseLength(); |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
366 |
final int elem = array[newLength]; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
367 |
array[newLength] = 0; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
368 |
return elem; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
369 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
370 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
371 |
@Override |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
372 |
public double fastPopDouble() { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
373 |
return fastPopInt(); |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
374 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
375 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
376 |
@Override |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
377 |
public Object fastPopObject() { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
378 |
return fastPopInt(); |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
379 |
} |
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:
27104
diff
changeset
|
380 |
|
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:
27104
diff
changeset
|
381 |
@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:
27104
diff
changeset
|
382 |
public ContinuousArrayData fastConcat(final ContinuousArrayData otherData) { |
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
383 |
final int otherLength = (int)otherData.length(); |
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
384 |
final int thisLength = (int)length(); |
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:
27104
diff
changeset
|
385 |
assert otherLength > 0 && thisLength > 0; |
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:
27104
diff
changeset
|
386 |
|
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:
27104
diff
changeset
|
387 |
final int[] otherArray = ((IntArrayData)otherData).array; |
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:
27104
diff
changeset
|
388 |
final int newLength = otherLength + thisLength; |
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:
27104
diff
changeset
|
389 |
final int[] newArray = new int[ArrayData.alignUp(newLength)]; |
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:
27104
diff
changeset
|
390 |
|
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:
27104
diff
changeset
|
391 |
System.arraycopy(array, 0, newArray, 0, thisLength); |
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:
27104
diff
changeset
|
392 |
System.arraycopy(otherArray, 0, newArray, thisLength, otherLength); |
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:
27104
diff
changeset
|
393 |
|
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:
27104
diff
changeset
|
394 |
return new IntArrayData(newArray, newLength); |
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:
27104
diff
changeset
|
395 |
} |
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:
27104
diff
changeset
|
396 |
|
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:
27104
diff
changeset
|
397 |
@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:
27104
diff
changeset
|
398 |
public String toString() { |
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
399 |
assert length() <= array.length : length() + " > " + array.length; |
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
400 |
return getClass().getSimpleName() + ':' + Arrays.toString(Arrays.copyOf(array, (int)length())); |
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:
27104
diff
changeset
|
401 |
} |
16147 | 402 |
} |