author | hannesw |
Thu, 01 Aug 2013 12:23:38 +0200 | |
changeset 19236 | 73d242d205f9 |
parent 18842 | 3c3be808b593 |
child 21438 | 4292865c758b |
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 java.util.Arrays; |
|
29 |
import jdk.nashorn.internal.runtime.JSType; |
|
30 |
import jdk.nashorn.internal.runtime.ScriptRuntime; |
|
31 |
||
32 |
/** |
|
33 |
* Implementation of {@link ArrayData} as soon as an Object has been |
|
34 |
* written to the array |
|
35 |
*/ |
|
36 |
final class ObjectArrayData extends ArrayData { |
|
37 |
||
38 |
/** |
|
39 |
* The wrapped array |
|
40 |
*/ |
|
41 |
private Object[] array; |
|
42 |
||
43 |
/** |
|
44 |
* Constructor |
|
45 |
* @param array an int array |
|
46 |
* @param length a length, not necessarily array.length |
|
47 |
*/ |
|
48 |
ObjectArrayData(final Object array[], final int length) { |
|
49 |
super(length); |
|
19236
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18842
diff
changeset
|
50 |
assert array.length >= length; |
16147 | 51 |
this.array = array; |
19236
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18842
diff
changeset
|
52 |
} |
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18842
diff
changeset
|
53 |
|
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18842
diff
changeset
|
54 |
@Override |
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18842
diff
changeset
|
55 |
public ArrayData copy() { |
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18842
diff
changeset
|
56 |
return new ObjectArrayData(array.clone(), (int) length()); |
16147 | 57 |
} |
58 |
||
59 |
@Override |
|
60 |
public Object[] asObjectArray() { |
|
61 |
return Arrays.copyOf(array, (int) length()); |
|
62 |
} |
|
63 |
||
64 |
@Override |
|
65 |
public ArrayData convert(final Class<?> type) { |
|
66 |
return this; |
|
67 |
} |
|
68 |
||
69 |
@Override |
|
70 |
public void shiftLeft(final int by) { |
|
71 |
System.arraycopy(array, by, array, 0, array.length - by); |
|
72 |
} |
|
73 |
||
74 |
@Override |
|
75 |
public ArrayData shiftRight(final int by) { |
|
76 |
final ArrayData newData = ensure(by + length() - 1); |
|
77 |
if (newData != this) { |
|
78 |
newData.shiftRight(by); |
|
79 |
return newData; |
|
80 |
} |
|
81 |
System.arraycopy(array, 0, array, by, array.length - by); |
|
82 |
return this; |
|
83 |
} |
|
84 |
||
85 |
@Override |
|
86 |
public ArrayData ensure(final long safeIndex) { |
|
87 |
if (safeIndex >= SparseArrayData.MAX_DENSE_LENGTH && safeIndex >= array.length) { |
|
88 |
return new SparseArrayData(this, safeIndex + 1); |
|
89 |
} |
|
90 |
||
91 |
int newLength = array.length; |
|
92 |
||
93 |
while (newLength <= safeIndex) { |
|
94 |
newLength = ArrayData.nextSize(newLength); |
|
95 |
} |
|
96 |
||
97 |
if (array.length <= safeIndex) { |
|
98 |
array = Arrays.copyOf(array, newLength); |
|
99 |
Arrays.fill(array, (int) length(), newLength, ScriptRuntime.UNDEFINED); |
|
100 |
} |
|
101 |
||
102 |
setLength(safeIndex + 1); |
|
103 |
||
104 |
return this; |
|
105 |
} |
|
106 |
||
107 |
@Override |
|
108 |
public ArrayData shrink(final long newLength) { |
|
109 |
Arrays.fill(array, (int) newLength, array.length, ScriptRuntime.UNDEFINED); |
|
110 |
return this; |
|
111 |
} |
|
112 |
||
113 |
@Override |
|
114 |
public ArrayData set(final int index, final Object value, final boolean strict) { |
|
115 |
array[index] = value; |
|
116 |
setLength(Math.max(index + 1, length())); |
|
117 |
return this; |
|
118 |
} |
|
119 |
||
120 |
@Override |
|
121 |
public ArrayData set(final int index, final int value, final boolean strict) { |
|
122 |
array[index] = value; |
|
123 |
setLength(Math.max(index + 1, length())); |
|
124 |
return this; |
|
125 |
} |
|
126 |
||
127 |
@Override |
|
128 |
public ArrayData set(final int index, final long value, final boolean strict) { |
|
129 |
array[index] = value; |
|
130 |
setLength(Math.max(index + 1, length())); |
|
131 |
return this; |
|
132 |
} |
|
133 |
||
134 |
@Override |
|
135 |
public ArrayData set(final int index, final double value, final boolean strict) { |
|
136 |
array[index] = value; |
|
137 |
setLength(Math.max(index + 1, length())); |
|
138 |
return this; |
|
139 |
} |
|
140 |
||
141 |
@Override |
|
18606 | 142 |
public ArrayData setEmpty(final int index) { |
143 |
array[index] = ScriptRuntime.EMPTY; |
|
144 |
return this; |
|
145 |
} |
|
146 |
||
147 |
@Override |
|
148 |
public ArrayData setEmpty(final long lo, final long hi) { |
|
18842
3c3be808b593
8019585: Sometimes a var declaration using itself in its init wasn't declared as canBeUndefined, causing erroneous bytecode
lagergren
parents:
18606
diff
changeset
|
149 |
Arrays.fill(array, (int)Math.max(lo, 0L), (int)Math.min(hi, Integer.MAX_VALUE), ScriptRuntime.EMPTY); |
18606 | 150 |
return this; |
151 |
} |
|
152 |
||
153 |
@Override |
|
16147 | 154 |
public int getInt(final int index) { |
155 |
return JSType.toInt32(array[index]); |
|
156 |
} |
|
157 |
||
158 |
@Override |
|
159 |
public long getLong(final int index) { |
|
160 |
return JSType.toLong(array[index]); |
|
161 |
} |
|
162 |
||
163 |
@Override |
|
164 |
public double getDouble(final int index) { |
|
165 |
return JSType.toNumber(array[index]); |
|
166 |
} |
|
167 |
||
168 |
@Override |
|
169 |
public Object getObject(final int index) { |
|
170 |
return array[index]; |
|
171 |
} |
|
172 |
||
173 |
@Override |
|
174 |
public boolean has(final int index) { |
|
175 |
return 0 <= index && index < length(); |
|
176 |
} |
|
177 |
||
178 |
@Override |
|
179 |
public ArrayData delete(final int index) { |
|
18606 | 180 |
setEmpty(index); |
16147 | 181 |
return new DeletedRangeArrayFilter(this, index, index); |
182 |
} |
|
183 |
||
184 |
@Override |
|
185 |
public ArrayData delete(final long fromIndex, final long toIndex) { |
|
18606 | 186 |
setEmpty(fromIndex, toIndex); |
16147 | 187 |
return new DeletedRangeArrayFilter(this, fromIndex, toIndex); |
188 |
} |
|
189 |
||
190 |
@Override |
|
191 |
public Object pop() { |
|
192 |
if (length() == 0) { |
|
193 |
return ScriptRuntime.UNDEFINED; |
|
194 |
} |
|
195 |
||
196 |
final int newLength = (int) (length() - 1); |
|
197 |
final Object elem = array[newLength]; |
|
18606 | 198 |
setEmpty(newLength); |
16147 | 199 |
setLength(newLength); |
200 |
return elem; |
|
201 |
} |
|
202 |
||
203 |
@Override |
|
204 |
public ArrayData slice(final long from, final long to) { |
|
205 |
final long start = from < 0 ? (from + length()) : from; |
|
206 |
final long newLength = to - start; |
|
207 |
return new ObjectArrayData(Arrays.copyOfRange(array, (int)from, (int)to), (int)newLength); |
|
208 |
} |
|
209 |
} |