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 |
||
28 |
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; |
|
29 |
import java.lang.reflect.Array; |
|
30 |
import jdk.nashorn.internal.runtime.BitVector; |
|
31 |
||
32 |
/** |
|
33 |
* This filter handles the deletion of array elements. |
|
34 |
*/ |
|
35 |
final class DeletedArrayFilter extends ArrayFilter { |
|
36 |
/** Bit vector tracking deletions. */ |
|
37 |
private final BitVector deleted; |
|
38 |
||
39 |
DeletedArrayFilter(final ArrayData underlying) { |
|
40 |
super(underlying); |
|
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
26886
diff
changeset
|
41 |
this.deleted = new BitVector(underlying.length()); |
16147 | 42 |
} |
43 |
||
44 |
@Override |
|
19236
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18606
diff
changeset
|
45 |
public ArrayData copy() { |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
19236
diff
changeset
|
46 |
final DeletedArrayFilter copy = new DeletedArrayFilter(underlying.copy()); |
19236
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18606
diff
changeset
|
47 |
copy.getDeleted().copy(deleted); |
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18606
diff
changeset
|
48 |
return copy; |
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18606
diff
changeset
|
49 |
} |
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18606
diff
changeset
|
50 |
|
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18606
diff
changeset
|
51 |
@Override |
16147 | 52 |
public Object[] asObjectArray() { |
53 |
final Object[] value = super.asObjectArray(); |
|
54 |
||
55 |
for (int i = 0; i < value.length; i++) { |
|
56 |
if (deleted.isSet(i)) { |
|
57 |
value[i] = UNDEFINED; |
|
58 |
} |
|
59 |
} |
|
60 |
||
61 |
return value; |
|
62 |
} |
|
63 |
||
64 |
@Override |
|
65 |
public Object asArrayOfType(final Class<?> componentType) { |
|
66 |
final Object value = super.asArrayOfType(componentType); |
|
67 |
final Object undefValue = convertUndefinedValue(componentType); |
|
68 |
final int l = Array.getLength(value); |
|
69 |
for (int i = 0; i < l; i++) { |
|
70 |
if (deleted.isSet(i)) { |
|
71 |
Array.set(value, i, undefValue); |
|
72 |
} |
|
73 |
} |
|
74 |
||
75 |
return value; |
|
76 |
} |
|
77 |
||
78 |
@Override |
|
42790
edb30aa1a99b
8171219: Missing checks in sparse array shift() implementation
hannesw
parents:
34732
diff
changeset
|
79 |
public ArrayData shiftLeft(final int by) { |
16147 | 80 |
super.shiftLeft(by); |
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
26886
diff
changeset
|
81 |
deleted.shiftLeft(by, length()); |
42790
edb30aa1a99b
8171219: Missing checks in sparse array shift() implementation
hannesw
parents:
34732
diff
changeset
|
82 |
return this; |
16147 | 83 |
} |
84 |
||
85 |
@Override |
|
86 |
public ArrayData shiftRight(final int by) { |
|
87 |
super.shiftRight(by); |
|
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
26886
diff
changeset
|
88 |
deleted.shiftRight(by, length()); |
16147 | 89 |
return this; |
90 |
} |
|
91 |
||
92 |
@Override |
|
93 |
public ArrayData ensure(final long safeIndex) { |
|
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
26886
diff
changeset
|
94 |
if (safeIndex >= SparseArrayData.MAX_DENSE_LENGTH && safeIndex >= length()) { |
16147 | 95 |
return new SparseArrayData(this, safeIndex + 1); |
96 |
} |
|
97 |
||
98 |
super.ensure(safeIndex); |
|
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
26886
diff
changeset
|
99 |
deleted.resize(length()); |
16147 | 100 |
|
101 |
return this; |
|
102 |
} |
|
103 |
||
104 |
@Override |
|
105 |
public ArrayData shrink(final long newLength) { |
|
106 |
super.shrink(newLength); |
|
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
26886
diff
changeset
|
107 |
deleted.resize(length()); |
16147 | 108 |
return this; |
109 |
} |
|
110 |
||
111 |
@Override |
|
112 |
public ArrayData set(final int index, final Object value, final boolean strict) { |
|
113 |
deleted.clear(ArrayIndex.toLongIndex(index)); |
|
114 |
return super.set(index, value, strict); |
|
115 |
} |
|
116 |
||
117 |
@Override |
|
118 |
public ArrayData set(final int index, final int value, final boolean strict) { |
|
119 |
deleted.clear(ArrayIndex.toLongIndex(index)); |
|
120 |
return super.set(index, value, strict); |
|
121 |
} |
|
122 |
||
123 |
@Override |
|
124 |
public ArrayData set(final int index, final double value, final boolean strict) { |
|
125 |
deleted.clear(ArrayIndex.toLongIndex(index)); |
|
126 |
return super.set(index, value, strict); |
|
127 |
} |
|
128 |
||
129 |
@Override |
|
130 |
public boolean has(final int index) { |
|
131 |
return super.has(index) && deleted.isClear(ArrayIndex.toLongIndex(index)); |
|
132 |
} |
|
133 |
||
134 |
@Override |
|
135 |
public ArrayData delete(final int index) { |
|
136 |
final long longIndex = ArrayIndex.toLongIndex(index); |
|
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
26886
diff
changeset
|
137 |
assert longIndex >= 0 && longIndex < length(); |
16147 | 138 |
deleted.set(longIndex); |
18606 | 139 |
underlying.setEmpty(index); |
16147 | 140 |
return this; |
141 |
} |
|
142 |
||
143 |
@Override |
|
144 |
public ArrayData delete(final long fromIndex, final long toIndex) { |
|
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
26886
diff
changeset
|
145 |
assert fromIndex >= 0 && fromIndex <= toIndex && toIndex < length(); |
16147 | 146 |
deleted.setRange(fromIndex, toIndex + 1); |
18606 | 147 |
underlying.setEmpty(fromIndex, toIndex); |
16147 | 148 |
return this; |
149 |
} |
|
150 |
||
151 |
@Override |
|
152 |
public Object pop() { |
|
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
26886
diff
changeset
|
153 |
final long index = length() - 1; |
16147 | 154 |
|
155 |
if (super.has((int)index)) { |
|
156 |
final boolean isDeleted = deleted.isSet(index); |
|
157 |
final Object value = super.pop(); |
|
158 |
||
159 |
return isDeleted ? UNDEFINED : value; |
|
160 |
} |
|
161 |
||
162 |
return super.pop(); |
|
163 |
} |
|
164 |
||
165 |
@Override |
|
166 |
public ArrayData slice(final long from, final long to) { |
|
167 |
final ArrayData newArray = underlying.slice(from, to); |
|
168 |
final DeletedArrayFilter newFilter = new DeletedArrayFilter(newArray); |
|
169 |
newFilter.getDeleted().copy(deleted); |
|
27525
e7d3c00b9fb8
8035312: Various array and ScriptObject length issues for non writable length fields
lagergren
parents:
26886
diff
changeset
|
170 |
newFilter.getDeleted().shiftLeft(from, newFilter.length()); |
16147 | 171 |
|
172 |
return newFilter; |
|
173 |
} |
|
174 |
||
175 |
private BitVector getDeleted() { |
|
176 |
return deleted; |
|
177 |
} |
|
178 |
} |