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; |
24719 | 29 |
import java.lang.invoke.MethodHandle; |
30 |
import java.lang.invoke.MethodHandles; |
|
16147 | 31 |
import java.util.Arrays; |
32 |
import jdk.nashorn.internal.runtime.JSType; |
|
33 |
import jdk.nashorn.internal.runtime.ScriptRuntime; |
|
34 |
||
35 |
/** |
|
36 |
* Implementation of {@link ArrayData} as soon as an Object has been |
|
37 |
* written to the array |
|
38 |
*/ |
|
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
|
39 |
final class ObjectArrayData extends ContinuousArrayData implements AnyElements { |
16147 | 40 |
|
41 |
/** |
|
42 |
* The wrapped array |
|
43 |
*/ |
|
44 |
private Object[] array; |
|
45 |
||
46 |
/** |
|
47 |
* Constructor |
|
48 |
* @param array an int array |
|
49 |
* @param length a length, not necessarily array.length |
|
50 |
*/ |
|
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
|
51 |
ObjectArrayData(final Object[] array, final int length) { |
16147 | 52 |
super(length); |
19236
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18842
diff
changeset
|
53 |
assert array.length >= length; |
16147 | 54 |
this.array = array; |
19236
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18842
diff
changeset
|
55 |
} |
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18842
diff
changeset
|
56 |
|
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18842
diff
changeset
|
57 |
@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
|
58 |
public final Class<?> getElementType() { |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
59 |
return Object.class; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
60 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
61 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
62 |
@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
|
63 |
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
|
64 |
return getElementType(); |
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
|
65 |
} |
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
|
66 |
|
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 |
@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
|
68 |
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
|
69 |
return 4; |
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
|
70 |
} |
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 |
|
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 |
@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
|
73 |
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
|
74 |
return otherData instanceof NumericElements ? this : 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
|
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 |
|
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 |
@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
|
78 |
public ObjectArrayData copy() { |
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
79 |
return new ObjectArrayData(array.clone(), (int)length()); |
16147 | 80 |
} |
81 |
||
82 |
@Override |
|
83 |
public Object[] asObjectArray() { |
|
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
84 |
return array.length == length() ? array.clone() : asObjectArrayCopy(); |
24719 | 85 |
} |
86 |
||
87 |
private Object[] asObjectArrayCopy() { |
|
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
88 |
final long len = length(); |
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
89 |
assert len <= Integer.MAX_VALUE; |
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
90 |
final Object[] copy = new Object[(int)len]; |
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
91 |
System.arraycopy(array, 0, copy, 0, (int)len); |
24719 | 92 |
return copy; |
16147 | 93 |
} |
94 |
||
95 |
@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
|
96 |
public ObjectArrayData convert(final Class<?> type) { |
16147 | 97 |
return this; |
98 |
} |
|
99 |
||
100 |
@Override |
|
42790
edb30aa1a99b
8171219: Missing checks in sparse array shift() implementation
hannesw
parents:
34974
diff
changeset
|
101 |
public ArrayData shiftLeft(final int by) { |
edb30aa1a99b
8171219: Missing checks in sparse array shift() implementation
hannesw
parents:
34974
diff
changeset
|
102 |
if (by >= length()) { |
edb30aa1a99b
8171219: Missing checks in sparse array shift() implementation
hannesw
parents:
34974
diff
changeset
|
103 |
shrink(0); |
edb30aa1a99b
8171219: Missing checks in sparse array shift() implementation
hannesw
parents:
34974
diff
changeset
|
104 |
} else { |
edb30aa1a99b
8171219: Missing checks in sparse array shift() implementation
hannesw
parents:
34974
diff
changeset
|
105 |
System.arraycopy(array, by, array, 0, array.length - by); |
edb30aa1a99b
8171219: Missing checks in sparse array shift() implementation
hannesw
parents:
34974
diff
changeset
|
106 |
} |
edb30aa1a99b
8171219: Missing checks in sparse array shift() implementation
hannesw
parents:
34974
diff
changeset
|
107 |
setLength(Math.max(0, length() - by)); |
edb30aa1a99b
8171219: Missing checks in sparse array shift() implementation
hannesw
parents:
34974
diff
changeset
|
108 |
return this; |
16147 | 109 |
} |
110 |
||
111 |
@Override |
|
112 |
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
|
113 |
final ArrayData newData = ensure(by + length() - 1); |
16147 | 114 |
if (newData != this) { |
115 |
newData.shiftRight(by); |
|
116 |
return newData; |
|
117 |
} |
|
118 |
System.arraycopy(array, 0, array, by, array.length - by); |
|
119 |
return this; |
|
120 |
} |
|
121 |
||
122 |
@Override |
|
123 |
public ArrayData ensure(final long safeIndex) { |
|
24720 | 124 |
if (safeIndex >= SparseArrayData.MAX_DENSE_LENGTH) { |
16147 | 125 |
return new SparseArrayData(this, safeIndex + 1); |
126 |
} |
|
24720 | 127 |
final int alen = array.length; |
128 |
if (safeIndex >= alen) { |
|
129 |
final int newLength = ArrayData.nextSize((int)safeIndex); |
|
130 |
array = Arrays.copyOf(array, newLength); //fill with undefined or OK? TODO |
|
16147 | 131 |
} |
28787 | 132 |
if (safeIndex >= length()) { |
133 |
setLength(safeIndex + 1); |
|
134 |
} |
|
16147 | 135 |
return this; |
136 |
} |
|
137 |
||
138 |
@Override |
|
139 |
public ArrayData shrink(final long newLength) { |
|
140 |
Arrays.fill(array, (int) newLength, array.length, ScriptRuntime.UNDEFINED); |
|
141 |
return this; |
|
142 |
} |
|
143 |
||
144 |
@Override |
|
145 |
public ArrayData set(final int index, final Object value, final boolean strict) { |
|
146 |
array[index] = value; |
|
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
147 |
setLength(Math.max(index + 1, length())); |
16147 | 148 |
return this; |
149 |
} |
|
150 |
||
151 |
@Override |
|
152 |
public ArrayData set(final int index, final int value, final boolean strict) { |
|
153 |
array[index] = value; |
|
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
154 |
setLength(Math.max(index + 1, length())); |
16147 | 155 |
return this; |
156 |
} |
|
157 |
||
158 |
@Override |
|
159 |
public ArrayData set(final int index, final double value, final boolean strict) { |
|
160 |
array[index] = value; |
|
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
161 |
setLength(Math.max(index + 1, length())); |
16147 | 162 |
return this; |
163 |
} |
|
164 |
||
165 |
@Override |
|
18606 | 166 |
public ArrayData setEmpty(final int index) { |
167 |
array[index] = ScriptRuntime.EMPTY; |
|
168 |
return this; |
|
169 |
} |
|
170 |
||
171 |
@Override |
|
172 |
public ArrayData setEmpty(final long lo, final long hi) { |
|
27098
2875b30458d3
8060011: Concatenating an array and converting it to Java gives wrong result
hannesw
parents:
26886
diff
changeset
|
173 |
// hi parameter is inclusive, but Arrays.fill toIndex parameter is exclusive |
2875b30458d3
8060011: Concatenating an array and converting it to Java gives wrong result
hannesw
parents:
26886
diff
changeset
|
174 |
Arrays.fill(array, (int)Math.max(lo, 0L), (int)Math.min(hi + 1, Integer.MAX_VALUE), ScriptRuntime.EMPTY); |
18606 | 175 |
return this; |
176 |
} |
|
177 |
||
24741
4232289c3235
8040102: Remove all references to Unsafe and definition of anonymous clases from the code
lagergren
parents:
24720
diff
changeset
|
178 |
private static final MethodHandle HAS_GET_ELEM = specialCall(MethodHandles.lookup(), ObjectArrayData.class, "getElem", Object.class, int.class).methodHandle(); |
4232289c3235
8040102: Remove all references to Unsafe and definition of anonymous clases from the code
lagergren
parents:
24720
diff
changeset
|
179 |
private static final MethodHandle SET_ELEM = specialCall(MethodHandles.lookup(), ObjectArrayData.class, "setElem", void.class, int.class, Object.class).methodHandle(); |
24719 | 180 |
|
181 |
@SuppressWarnings("unused") |
|
182 |
private Object getElem(final int index) { |
|
183 |
if (has(index)) { |
|
184 |
return array[index]; |
|
185 |
} |
|
186 |
throw new ClassCastException(); |
|
187 |
} |
|
188 |
||
189 |
@SuppressWarnings("unused") |
|
190 |
private void setElem(final int index, final Object elem) { |
|
24720 | 191 |
if (hasRoomFor(index)) { |
192 |
array[index] = elem; |
|
193 |
return; |
|
194 |
} |
|
195 |
throw new ClassCastException(); |
|
24719 | 196 |
} |
197 |
||
198 |
@Override |
|
199 |
public MethodHandle getElementGetter(final Class<?> returnType, final int programPoint) { |
|
200 |
if (returnType.isPrimitive()) { |
|
201 |
return null; |
|
202 |
} |
|
203 |
return getContinuousElementGetter(HAS_GET_ELEM, returnType, programPoint); |
|
204 |
} |
|
205 |
||
206 |
@Override |
|
207 |
public MethodHandle getElementSetter(final Class<?> elementType) { |
|
208 |
return getContinuousElementSetter(SET_ELEM, Object.class); |
|
209 |
} |
|
210 |
||
211 |
||
212 |
@Override |
|
16147 | 213 |
public int getInt(final int index) { |
214 |
return JSType.toInt32(array[index]); |
|
215 |
} |
|
216 |
||
217 |
@Override |
|
218 |
public double getDouble(final int index) { |
|
219 |
return JSType.toNumber(array[index]); |
|
220 |
} |
|
221 |
||
222 |
@Override |
|
223 |
public Object getObject(final int index) { |
|
224 |
return array[index]; |
|
225 |
} |
|
226 |
||
227 |
@Override |
|
228 |
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
|
229 |
return 0 <= index && index < length(); |
16147 | 230 |
} |
231 |
||
232 |
@Override |
|
233 |
public ArrayData delete(final int index) { |
|
18606 | 234 |
setEmpty(index); |
16147 | 235 |
return new DeletedRangeArrayFilter(this, index, index); |
236 |
} |
|
237 |
||
238 |
@Override |
|
239 |
public ArrayData delete(final long fromIndex, final long toIndex) { |
|
18606 | 240 |
setEmpty(fromIndex, toIndex); |
16147 | 241 |
return new DeletedRangeArrayFilter(this, fromIndex, toIndex); |
242 |
} |
|
243 |
||
244 |
@Override |
|
34974
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
34732
diff
changeset
|
245 |
public double fastPush(final int arg) { |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
246 |
return fastPush((Object)arg); |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
247 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
248 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
249 |
@Override |
34974
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
34732
diff
changeset
|
250 |
public double fastPush(final long arg) { |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
251 |
return fastPush((Object)arg); |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
252 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
253 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
254 |
@Override |
34974
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
34732
diff
changeset
|
255 |
public double fastPush(final double arg) { |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
256 |
return fastPush((Object)arg); |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
257 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
258 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
259 |
@Override |
34974
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
34732
diff
changeset
|
260 |
public double fastPush(final Object arg) { |
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
261 |
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
|
262 |
if (len == array.length) { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
263 |
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
|
264 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
265 |
array[len] = arg; |
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
266 |
return increaseLength(); |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
267 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
268 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
269 |
@Override |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
270 |
public Object fastPopObject() { |
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
271 |
if (length() == 0) { |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
272 |
return ScriptRuntime.UNDEFINED; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
273 |
} |
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
274 |
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
|
275 |
final Object elem = array[newLength]; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
276 |
array[newLength] = ScriptRuntime.EMPTY; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
277 |
return elem; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
278 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
279 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
280 |
@Override |
16147 | 281 |
public Object pop() { |
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
282 |
if (length() == 0) { |
16147 | 283 |
return ScriptRuntime.UNDEFINED; |
284 |
} |
|
285 |
||
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
286 |
final int newLength = (int)length() - 1; |
16147 | 287 |
final Object elem = array[newLength]; |
18606 | 288 |
setEmpty(newLength); |
16147 | 289 |
setLength(newLength); |
290 |
return elem; |
|
291 |
} |
|
292 |
||
293 |
@Override |
|
294 |
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
|
295 |
final long start = from < 0 ? from + length() : from; |
16147 | 296 |
final long newLength = to - start; |
297 |
return new ObjectArrayData(Arrays.copyOfRange(array, (int)from, (int)to), (int)newLength); |
|
298 |
} |
|
21438
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
299 |
|
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
300 |
@Override |
24720 | 301 |
public ArrayData push(final boolean strict, final Object item) { |
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
302 |
final long len = length(); |
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
303 |
final ArrayData newData = ensure(len); |
24720 | 304 |
if (newData == this) { |
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
305 |
array[(int)len] = item; |
24720 | 306 |
return this; |
307 |
} |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
308 |
return newData.set((int)len, item, strict); |
24720 | 309 |
} |
310 |
||
311 |
@Override |
|
21438
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
312 |
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
|
313 |
final long oldLength = length(); |
21438
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
314 |
final long newLength = oldLength - removed + added; |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
315 |
if (newLength > SparseArrayData.MAX_DENSE_LENGTH && newLength > array.length) { |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
316 |
throw new UnsupportedOperationException(); |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
317 |
} |
24720 | 318 |
final ArrayData returnValue = removed == 0 ? |
21438
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
319 |
EMPTY_ARRAY : new ObjectArrayData(Arrays.copyOfRange(array, start, start + removed), removed); |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
320 |
|
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
321 |
if (newLength != oldLength) { |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
322 |
final Object[] newArray; |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
323 |
|
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
324 |
if (newLength > array.length) { |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
325 |
newArray = new Object[ArrayData.nextSize((int)newLength)]; |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
326 |
System.arraycopy(array, 0, newArray, 0, start); |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
327 |
} else { |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
328 |
newArray = array; |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
329 |
} |
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 |
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
|
332 |
array = newArray; |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
333 |
setLength(newLength); |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
334 |
} |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
335 |
|
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
336 |
return returnValue; |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
337 |
} |
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
|
338 |
|
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
|
339 |
@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
|
340 |
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
|
341 |
final int otherLength = (int)otherData.length(); |
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
342 |
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
|
343 |
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
|
344 |
|
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
|
345 |
final Object[] otherArray = ((ObjectArrayData)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
|
346 |
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
|
347 |
final Object[] newArray = new Object[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
|
348 |
|
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
|
349 |
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
|
350 |
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
|
351 |
|
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
|
352 |
return new ObjectArrayData(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
|
353 |
} |
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
|
354 |
|
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
|
355 |
@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
|
356 |
public String toString() { |
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
27209
diff
changeset
|
357 |
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
|
358 |
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
|
359 |
} |
16147 | 360 |
} |