author | lagergren |
Fri, 26 Sep 2014 18:47:20 +0200 | |
changeset 26886 | 18c744ab4df2 |
parent 25865 | d38d876f1654 |
child 27525 | e7d3c00b9fb8 |
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 |
||
28 |
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; |
|
29 |
import java.lang.reflect.Array; |
|
30 |
import jdk.nashorn.internal.runtime.BitVector; |
|
24719 | 31 |
import jdk.nashorn.internal.runtime.UnwarrantedOptimismException; |
16147 | 32 |
|
33 |
/** |
|
34 |
* This filter handles the presence of undefined array elements. |
|
35 |
*/ |
|
36 |
final class UndefinedArrayFilter extends ArrayFilter { |
|
37 |
/** Bit vector tracking undefines. */ |
|
38 |
private final BitVector undefined; |
|
39 |
||
40 |
UndefinedArrayFilter(final ArrayData underlying) { |
|
41 |
super(underlying); |
|
42 |
||
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
25865
diff
changeset
|
43 |
this.undefined = new BitVector(underlying.length); |
16147 | 44 |
} |
45 |
||
46 |
@Override |
|
19236
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
16151
diff
changeset
|
47 |
public ArrayData copy() { |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24719
diff
changeset
|
48 |
final UndefinedArrayFilter copy = new UndefinedArrayFilter(underlying.copy()); |
19236
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
16151
diff
changeset
|
49 |
copy.getUndefined().copy(undefined); |
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
16151
diff
changeset
|
50 |
return copy; |
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
16151
diff
changeset
|
51 |
} |
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
16151
diff
changeset
|
52 |
|
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
16151
diff
changeset
|
53 |
@Override |
16147 | 54 |
public Object[] asObjectArray() { |
55 |
final Object[] value = super.asObjectArray(); |
|
56 |
||
57 |
for (int i = 0; i < value.length; i++) { |
|
58 |
if (undefined.isSet(i)) { |
|
59 |
value[i] = UNDEFINED; |
|
60 |
} |
|
61 |
} |
|
62 |
||
63 |
return value; |
|
64 |
} |
|
65 |
||
66 |
@Override |
|
67 |
public Object asArrayOfType(final Class<?> componentType) { |
|
68 |
final Object value = super.asArrayOfType(componentType); |
|
69 |
final Object undefValue = convertUndefinedValue(componentType); |
|
70 |
final int l = Array.getLength(value); |
|
71 |
for (int i = 0; i < l; i++) { |
|
72 |
if (undefined.isSet(i)) { |
|
73 |
Array.set(value, i,undefValue); |
|
74 |
} |
|
75 |
} |
|
76 |
||
77 |
return value; |
|
78 |
} |
|
79 |
||
80 |
@Override |
|
81 |
public void shiftLeft(final int by) { |
|
82 |
super.shiftLeft(by); |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
25865
diff
changeset
|
83 |
undefined.shiftLeft(by, length); |
16147 | 84 |
} |
85 |
||
86 |
@Override |
|
87 |
public ArrayData shiftRight(final int by) { |
|
88 |
super.shiftRight(by); |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
25865
diff
changeset
|
89 |
undefined.shiftRight(by, length); |
16147 | 90 |
|
91 |
return this; |
|
92 |
} |
|
93 |
||
94 |
@Override |
|
95 |
public ArrayData ensure(final long safeIndex) { |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
25865
diff
changeset
|
96 |
if (safeIndex >= SparseArrayData.MAX_DENSE_LENGTH && safeIndex >= length) { |
16147 | 97 |
return new SparseArrayData(this, safeIndex + 1); |
98 |
} |
|
99 |
||
100 |
super.ensure(safeIndex); |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
25865
diff
changeset
|
101 |
undefined.resize(length); |
16147 | 102 |
|
103 |
return this; |
|
104 |
} |
|
105 |
||
106 |
@Override |
|
107 |
public ArrayData shrink(final long newLength) { |
|
108 |
super.shrink(newLength); |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
25865
diff
changeset
|
109 |
undefined.resize(length); |
16147 | 110 |
|
111 |
return this; |
|
112 |
} |
|
113 |
||
114 |
@Override |
|
115 |
public ArrayData set(final int index, final Object value, final boolean strict) { |
|
116 |
undefined.clear(index); |
|
117 |
||
118 |
if (value == UNDEFINED) { |
|
119 |
undefined.set(index); |
|
120 |
return this; |
|
121 |
} |
|
122 |
||
123 |
return super.set(index, value, strict); |
|
124 |
} |
|
125 |
||
126 |
@Override |
|
127 |
public ArrayData set(final int index, final int value, final boolean strict) { |
|
128 |
undefined.clear(index); |
|
129 |
||
130 |
return super.set(index, value, strict); |
|
131 |
} |
|
132 |
||
133 |
@Override |
|
134 |
public ArrayData set(final int index, final long value, final boolean strict) { |
|
135 |
undefined.clear(index); |
|
136 |
||
137 |
return super.set(index, value, strict); |
|
138 |
} |
|
139 |
||
140 |
@Override |
|
141 |
public ArrayData set(final int index, final double value, final boolean strict) { |
|
142 |
undefined.clear(index); |
|
143 |
||
144 |
return super.set(index, value, strict); |
|
145 |
} |
|
146 |
||
147 |
@Override |
|
148 |
public int getInt(final int index) { |
|
149 |
if (undefined.isSet(index)) { |
|
150 |
return 0; |
|
151 |
} |
|
152 |
||
153 |
return super.getInt(index); |
|
154 |
} |
|
155 |
||
156 |
@Override |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24719
diff
changeset
|
157 |
public int getIntOptimistic(final int index, final int programPoint) { |
24719 | 158 |
if (undefined.isSet(index)) { |
159 |
throw new UnwarrantedOptimismException(UNDEFINED, programPoint); |
|
160 |
} |
|
161 |
||
162 |
return super.getIntOptimistic(index, programPoint); |
|
163 |
} |
|
164 |
||
165 |
@Override |
|
16147 | 166 |
public long getLong(final int index) { |
167 |
if (undefined.isSet(index)) { |
|
168 |
return 0L; |
|
169 |
} |
|
170 |
||
171 |
return super.getLong(index); |
|
172 |
} |
|
173 |
||
174 |
@Override |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24719
diff
changeset
|
175 |
public long getLongOptimistic(final int index, final int programPoint) { |
24719 | 176 |
if (undefined.isSet(index)) { |
177 |
throw new UnwarrantedOptimismException(UNDEFINED, programPoint); |
|
178 |
} |
|
179 |
||
180 |
return super.getLongOptimistic(index, programPoint); |
|
181 |
} |
|
182 |
||
183 |
@Override |
|
16147 | 184 |
public double getDouble(final int index) { |
185 |
if (undefined.isSet(index)) { |
|
186 |
return Double.NaN; |
|
187 |
} |
|
188 |
||
189 |
return super.getDouble(index); |
|
190 |
} |
|
191 |
||
192 |
@Override |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24719
diff
changeset
|
193 |
public double getDoubleOptimistic(final int index, final int programPoint) { |
24719 | 194 |
if (undefined.isSet(index)) { |
195 |
throw new UnwarrantedOptimismException(UNDEFINED, programPoint); |
|
196 |
} |
|
197 |
||
198 |
return super.getDoubleOptimistic(index, programPoint); |
|
199 |
} |
|
200 |
||
201 |
@Override |
|
16147 | 202 |
public Object getObject(final int index) { |
203 |
if (undefined.isSet(index)) { |
|
204 |
return UNDEFINED; |
|
205 |
} |
|
206 |
||
207 |
return super.getObject(index); |
|
208 |
} |
|
209 |
||
210 |
@Override |
|
211 |
public ArrayData delete(final int index) { |
|
212 |
undefined.clear(index); |
|
213 |
||
214 |
return super.delete(index); |
|
215 |
} |
|
216 |
||
217 |
@Override |
|
218 |
public Object pop() { |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
25865
diff
changeset
|
219 |
final long index = length - 1; |
16147 | 220 |
|
221 |
if (super.has((int)index)) { |
|
222 |
final boolean isUndefined = undefined.isSet(index); |
|
223 |
final Object value = super.pop(); |
|
224 |
||
225 |
return isUndefined ? UNDEFINED : value; |
|
226 |
} |
|
227 |
||
228 |
return super.pop(); |
|
229 |
} |
|
230 |
||
231 |
@Override |
|
232 |
public ArrayData slice(final long from, final long to) { |
|
233 |
final ArrayData newArray = underlying.slice(from, to); |
|
234 |
final UndefinedArrayFilter newFilter = new UndefinedArrayFilter(newArray); |
|
235 |
newFilter.getUndefined().copy(undefined); |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
25865
diff
changeset
|
236 |
newFilter.getUndefined().shiftLeft(from, newFilter.length); |
16147 | 237 |
|
238 |
return newFilter; |
|
239 |
} |
|
240 |
||
241 |
private BitVector getUndefined() { |
|
242 |
return undefined; |
|
243 |
} |
|
244 |
} |