| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript; |
| 8 |
|
| 9 |
import java.io.Serializable; |
| 10 |
import java.io.IOException; |
| 11 |
import java.io.ObjectInputStream; |
| 12 |
import java.io.ObjectOutputStream; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
public class ObjArray implements Serializable |
| 19 |
{ |
| 20 |
static final long serialVersionUID = 4174889037736658296L; |
| 21 |
|
| 22 |
public ObjArray() { } |
| 23 |
|
| 24 |
public final boolean isSealed() |
| 25 |
{ |
| 26 |
return sealed; |
| 27 |
} |
| 28 |
|
| 29 |
public final void seal() |
| 30 |
{ |
| 31 |
sealed = true; |
| 32 |
} |
| 33 |
|
| 34 |
public final boolean isEmpty() |
| 35 |
{ |
| 36 |
return size == 0; |
| 37 |
} |
| 38 |
|
| 39 |
public final int size() |
| 40 |
{ |
| 41 |
return size; |
| 42 |
} |
| 43 |
|
| 44 |
public final void setSize(int newSize) |
| 45 |
{ |
| 46 |
if (newSize < 0) throw new IllegalArgumentException(); |
| 47 |
if (sealed) throw onSeledMutation(); |
| 48 |
int N = size; |
| 49 |
if (newSize < N) { |
| 50 |
for (int i = newSize; i != N; ++i) { |
| 51 |
setImpl(i, null); |
| 52 |
} |
| 53 |
} else if (newSize > N) { |
| 54 |
if (newSize > FIELDS_STORE_SIZE) { |
| 55 |
ensureCapacity(newSize); |
| 56 |
} |
| 57 |
} |
| 58 |
size = newSize; |
| 59 |
} |
| 60 |
|
| 61 |
public final Object get(int index) |
| 62 |
{ |
| 63 |
if (!(0 <= index && index < size)) throw onInvalidIndex(index, size); |
| 64 |
return getImpl(index); |
| 65 |
} |
| 66 |
|
| 67 |
public final void set(int index, Object value) |
| 68 |
{ |
| 69 |
if (!(0 <= index && index < size)) throw onInvalidIndex(index, size); |
| 70 |
if (sealed) throw onSeledMutation(); |
| 71 |
setImpl(index, value); |
| 72 |
} |
| 73 |
|
| 74 |
private Object getImpl(int index) |
| 75 |
{ |
| 76 |
switch (index) { |
| 77 |
case 0: return f0; |
| 78 |
case 1: return f1; |
| 79 |
case 2: return f2; |
| 80 |
case 3: return f3; |
| 81 |
case 4: return f4; |
| 82 |
} |
| 83 |
return data[index - FIELDS_STORE_SIZE]; |
| 84 |
} |
| 85 |
|
| 86 |
private void setImpl(int index, Object value) |
| 87 |
{ |
| 88 |
switch (index) { |
| 89 |
case 0: f0 = value; break; |
| 90 |
case 1: f1 = value; break; |
| 91 |
case 2: f2 = value; break; |
| 92 |
case 3: f3 = value; break; |
| 93 |
case 4: f4 = value; break; |
| 94 |
default: data[index - FIELDS_STORE_SIZE] = value; |
| 95 |
} |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
public int indexOf(Object obj) |
| 100 |
{ |
| 101 |
int N = size; |
| 102 |
for (int i = 0; i != N; ++i) { |
| 103 |
Object current = getImpl(i); |
| 104 |
if (current == obj || (current != null && current.equals(obj))) { |
| 105 |
return i; |
| 106 |
} |
| 107 |
} |
| 108 |
return -1; |
| 109 |
} |
| 110 |
|
| 111 |
public int lastIndexOf(Object obj) |
| 112 |
{ |
| 113 |
for (int i = size; i != 0;) { |
| 114 |
--i; |
| 115 |
Object current = getImpl(i); |
| 116 |
if (current == obj || (current != null && current.equals(obj))) { |
| 117 |
return i; |
| 118 |
} |
| 119 |
} |
| 120 |
return -1; |
| 121 |
} |
| 122 |
|
| 123 |
public final Object peek() |
| 124 |
{ |
| 125 |
int N = size; |
| 126 |
if (N == 0) throw onEmptyStackTopRead(); |
| 127 |
return getImpl(N - 1); |
| 128 |
} |
| 129 |
|
| 130 |
public final Object pop() |
| 131 |
{ |
| 132 |
if (sealed) throw onSeledMutation(); |
| 133 |
int N = size; |
| 134 |
--N; |
| 135 |
Object top; |
| 136 |
switch (N) { |
| 137 |
case -1: throw onEmptyStackTopRead(); |
| 138 |
case 0: top = f0; f0 = null; break; |
| 139 |
case 1: top = f1; f1 = null; break; |
| 140 |
case 2: top = f2; f2 = null; break; |
| 141 |
case 3: top = f3; f3 = null; break; |
| 142 |
case 4: top = f4; f4 = null; break; |
| 143 |
default: |
| 144 |
top = data[N - FIELDS_STORE_SIZE]; |
| 145 |
data[N - FIELDS_STORE_SIZE] = null; |
| 146 |
} |
| 147 |
size = N; |
| 148 |
return top; |
| 149 |
} |
| 150 |
|
| 151 |
public final void push(Object value) |
| 152 |
{ |
| 153 |
add(value); |
| 154 |
} |
| 155 |
|
| 156 |
public final void add(Object value) |
| 157 |
{ |
| 158 |
if (sealed) throw onSeledMutation(); |
| 159 |
int N = size; |
| 160 |
if (N >= FIELDS_STORE_SIZE) { |
| 161 |
ensureCapacity(N + 1); |
| 162 |
} |
| 163 |
size = N + 1; |
| 164 |
setImpl(N, value); |
| 165 |
} |
| 166 |
|
| 167 |
public final void add(int index, Object value) |
| 168 |
{ |
| 169 |
int N = size; |
| 170 |
if (!(0 <= index && index <= N)) throw onInvalidIndex(index, N + 1); |
| 171 |
if (sealed) throw onSeledMutation(); |
| 172 |
Object tmp; |
| 173 |
switch (index) { |
| 174 |
case 0: |
| 175 |
if (N == 0) { f0 = value; break; } |
| 176 |
tmp = f0; f0 = value; value = tmp; |
| 177 |
case 1: |
| 178 |
if (N == 1) { f1 = value; break; } |
| 179 |
tmp = f1; f1 = value; value = tmp; |
| 180 |
case 2: |
| 181 |
if (N == 2) { f2 = value; break; } |
| 182 |
tmp = f2; f2 = value; value = tmp; |
| 183 |
case 3: |
| 184 |
if (N == 3) { f3 = value; break; } |
| 185 |
tmp = f3; f3 = value; value = tmp; |
| 186 |
case 4: |
| 187 |
if (N == 4) { f4 = value; break; } |
| 188 |
tmp = f4; f4 = value; value = tmp; |
| 189 |
|
| 190 |
index = FIELDS_STORE_SIZE; |
| 191 |
default: |
| 192 |
ensureCapacity(N + 1); |
| 193 |
if (index != N) { |
| 194 |
System.arraycopy(data, index - FIELDS_STORE_SIZE, |
| 195 |
data, index - FIELDS_STORE_SIZE + 1, |
| 196 |
N - index); |
| 197 |
} |
| 198 |
data[index - FIELDS_STORE_SIZE] = value; |
| 199 |
} |
| 200 |
size = N + 1; |
| 201 |
} |
| 202 |
|
| 203 |
public final void remove(int index) |
| 204 |
{ |
| 205 |
int N = size; |
| 206 |
if (!(0 <= index && index < N)) throw onInvalidIndex(index, N); |
| 207 |
if (sealed) throw onSeledMutation(); |
| 208 |
--N; |
| 209 |
switch (index) { |
| 210 |
case 0: |
| 211 |
if (N == 0) { f0 = null; break; } |
| 212 |
f0 = f1; |
| 213 |
case 1: |
| 214 |
if (N == 1) { f1 = null; break; } |
| 215 |
f1 = f2; |
| 216 |
case 2: |
| 217 |
if (N == 2) { f2 = null; break; } |
| 218 |
f2 = f3; |
| 219 |
case 3: |
| 220 |
if (N == 3) { f3 = null; break; } |
| 221 |
f3 = f4; |
| 222 |
case 4: |
| 223 |
if (N == 4) { f4 = null; break; } |
| 224 |
f4 = data[0]; |
| 225 |
|
| 226 |
index = FIELDS_STORE_SIZE; |
| 227 |
default: |
| 228 |
if (index != N) { |
| 229 |
System.arraycopy(data, index - FIELDS_STORE_SIZE + 1, |
| 230 |
data, index - FIELDS_STORE_SIZE, |
| 231 |
N - index); |
| 232 |
} |
| 233 |
data[N - FIELDS_STORE_SIZE] = null; |
| 234 |
} |
| 235 |
size = N; |
| 236 |
} |
| 237 |
|
| 238 |
public final void clear() |
| 239 |
{ |
| 240 |
if (sealed) throw onSeledMutation(); |
| 241 |
int N = size; |
| 242 |
for (int i = 0; i != N; ++i) { |
| 243 |
setImpl(i, null); |
| 244 |
} |
| 245 |
size = 0; |
| 246 |
} |
| 247 |
|
| 248 |
public final Object[] toArray() |
| 249 |
{ |
| 250 |
Object[] array = new Object[size]; |
| 251 |
toArray(array, 0); |
| 252 |
return array; |
| 253 |
} |
| 254 |
|
| 255 |
public final void toArray(Object[] array) |
| 256 |
{ |
| 257 |
toArray(array, 0); |
| 258 |
} |
| 259 |
|
| 260 |
public final void toArray(Object[] array, int offset) |
| 261 |
{ |
| 262 |
int N = size; |
| 263 |
switch (N) { |
| 264 |
default: |
| 265 |
System.arraycopy(data, 0, array, offset + FIELDS_STORE_SIZE, |
| 266 |
N - FIELDS_STORE_SIZE); |
| 267 |
case 5: array[offset + 4] = f4; |
| 268 |
case 4: array[offset + 3] = f3; |
| 269 |
case 3: array[offset + 2] = f2; |
| 270 |
case 2: array[offset + 1] = f1; |
| 271 |
case 1: array[offset + 0] = f0; |
| 272 |
case 0: break; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
private void ensureCapacity(int minimalCapacity) |
| 277 |
{ |
| 278 |
int required = minimalCapacity - FIELDS_STORE_SIZE; |
| 279 |
if (required <= 0) throw new IllegalArgumentException(); |
| 280 |
if (data == null) { |
| 281 |
int alloc = FIELDS_STORE_SIZE * 2; |
| 282 |
if (alloc < required) { |
| 283 |
alloc = required; |
| 284 |
} |
| 285 |
data = new Object[alloc]; |
| 286 |
} else { |
| 287 |
int alloc = data.length; |
| 288 |
if (alloc < required) { |
| 289 |
if (alloc <= FIELDS_STORE_SIZE) { |
| 290 |
alloc = FIELDS_STORE_SIZE * 2; |
| 291 |
} else { |
| 292 |
alloc *= 2; |
| 293 |
} |
| 294 |
if (alloc < required) { |
| 295 |
alloc = required; |
| 296 |
} |
| 297 |
Object[] tmp = new Object[alloc]; |
| 298 |
if (size > FIELDS_STORE_SIZE) { |
| 299 |
System.arraycopy(data, 0, tmp, 0, |
| 300 |
size - FIELDS_STORE_SIZE); |
| 301 |
} |
| 302 |
data = tmp; |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
private static RuntimeException onInvalidIndex(int index, int upperBound) |
| 308 |
{ |
| 309 |
|
| 310 |
String msg = index+" \u2209 [0, "+upperBound+')'; |
| 311 |
throw new IndexOutOfBoundsException(msg); |
| 312 |
} |
| 313 |
|
| 314 |
private static RuntimeException onEmptyStackTopRead() |
| 315 |
{ |
| 316 |
throw new RuntimeException("Empty stack"); |
| 317 |
} |
| 318 |
|
| 319 |
private static RuntimeException onSeledMutation() |
| 320 |
{ |
| 321 |
throw new IllegalStateException("Attempt to modify sealed array"); |
| 322 |
} |
| 323 |
|
| 324 |
private void writeObject(ObjectOutputStream os) throws IOException |
| 325 |
{ |
| 326 |
os.defaultWriteObject(); |
| 327 |
int N = size; |
| 328 |
for (int i = 0; i != N; ++i) { |
| 329 |
Object obj = getImpl(i); |
| 330 |
os.writeObject(obj); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
private void readObject(ObjectInputStream is) |
| 335 |
throws IOException, ClassNotFoundException |
| 336 |
{ |
| 337 |
is.defaultReadObject(); |
| 338 |
int N = size; |
| 339 |
if (N > FIELDS_STORE_SIZE) { |
| 340 |
data = new Object[N - FIELDS_STORE_SIZE]; |
| 341 |
} |
| 342 |
for (int i = 0; i != N; ++i) { |
| 343 |
Object obj = is.readObject(); |
| 344 |
setImpl(i, obj); |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
private int size; |
| 350 |
|
| 351 |
private boolean sealed; |
| 352 |
|
| 353 |
private static final int FIELDS_STORE_SIZE = 5; |
| 354 |
private transient Object f0, f1, f2, f3, f4; |
| 355 |
private transient Object[] data; |
| 356 |
} |