author | lagergren |
Fri, 26 Sep 2014 18:47:20 +0200 | |
changeset 26886 | 18c744ab4df2 |
parent 25865 | d38d876f1654 |
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.lang.reflect.Array; |
|
29 |
import jdk.nashorn.internal.runtime.ScriptRuntime; |
|
30 |
||
31 |
/** |
|
32 |
* Place holding array data for non-array objects. Activates a true array when |
|
33 |
* accessed. Should only exist as a singleton defined in ArrayData. |
|
34 |
*/ |
|
35 |
final class NoTypeArrayData extends ArrayData { |
|
36 |
NoTypeArrayData() { |
|
37 |
super(0); |
|
38 |
} |
|
39 |
||
40 |
NoTypeArrayData(final long length) { |
|
41 |
super(length); |
|
42 |
} |
|
43 |
||
44 |
@Override |
|
45 |
public Object[] asObjectArray() { |
|
24719 | 46 |
return ScriptRuntime.EMPTY_ARRAY; |
16147 | 47 |
} |
48 |
||
49 |
@Override |
|
19236
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
16151
diff
changeset
|
50 |
public ArrayData copy() { |
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
16151
diff
changeset
|
51 |
return new NoTypeArrayData(); |
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 |
|
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
16151
diff
changeset
|
54 |
@Override |
16147 | 55 |
public Object asArrayOfType(final Class<?> componentType) { |
56 |
return Array.newInstance(componentType, 0); |
|
57 |
} |
|
58 |
||
59 |
@Override |
|
60 |
public ArrayData convert(final Class<?> type) { |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
25865
diff
changeset
|
61 |
final long len = length; |
16147 | 62 |
final ArrayData arrayData; |
63 |
if (type == Long.class) { |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
25865
diff
changeset
|
64 |
arrayData = new LongArrayData(new long[ArrayData.nextSize((int)len)], (int)len); |
16147 | 65 |
} else if (type == Double.class) { |
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
25865
diff
changeset
|
66 |
arrayData = new NumberArrayData(new double[ArrayData.nextSize((int)len)], (int)len); |
16147 | 67 |
} else if (type == Integer.class) { |
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
25865
diff
changeset
|
68 |
arrayData = new IntArrayData(new int[ArrayData.nextSize((int)len)], (int)len); |
16147 | 69 |
} else { |
70 |
assert !type.isPrimitive(); |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
25865
diff
changeset
|
71 |
arrayData = new ObjectArrayData(new Object[ArrayData.nextSize((int)len)], (int)len); |
16147 | 72 |
} |
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
25865
diff
changeset
|
73 |
return length == 0 ? arrayData : new DeletedRangeArrayFilter(arrayData, 0, len - 1); |
16147 | 74 |
} |
75 |
||
76 |
@Override |
|
77 |
public void shiftLeft(final int by) { |
|
78 |
//empty |
|
79 |
} |
|
80 |
||
81 |
@Override |
|
82 |
public ArrayData shiftRight(final int by) { |
|
83 |
return this; |
|
84 |
} |
|
85 |
||
86 |
@Override |
|
87 |
public ArrayData ensure(final long safeIndex) { |
|
88 |
if (safeIndex >= SparseArrayData.MAX_DENSE_LENGTH) { |
|
89 |
return new SparseArrayData(this, safeIndex + 1); |
|
90 |
} |
|
91 |
||
92 |
// Don't trample the shared EMPTY_ARRAY. |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
25865
diff
changeset
|
93 |
if (length == 0) { |
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
25865
diff
changeset
|
94 |
return new NoTypeArrayData(Math.max(safeIndex + 1, length)); |
16147 | 95 |
} |
96 |
||
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
25865
diff
changeset
|
97 |
setLength(Math.max(safeIndex + 1, length)); |
16147 | 98 |
return this; |
99 |
} |
|
100 |
||
101 |
@Override |
|
102 |
public ArrayData shrink(final long newLength) { |
|
103 |
return this; |
|
104 |
} |
|
105 |
||
106 |
@Override |
|
107 |
public ArrayData set(final int index, final Object value, final boolean strict) { |
|
108 |
ArrayData newData; |
|
109 |
||
110 |
if (value instanceof Double) { |
|
111 |
newData = convert(Double.class); |
|
112 |
} else if (value instanceof Long) { |
|
113 |
newData = convert(Long.class); |
|
114 |
} else if (value instanceof Integer) { |
|
115 |
newData = convert(Integer.class); |
|
116 |
} else { |
|
117 |
assert !(value instanceof Number); |
|
118 |
newData = convert(value == null ? Object.class : value.getClass()); |
|
119 |
} |
|
120 |
||
121 |
return newData.set(index, value, strict); |
|
122 |
} |
|
123 |
||
124 |
@Override |
|
125 |
public ArrayData set(final int index, final int value, final boolean strict) { |
|
126 |
final ArrayData newData = convert(Integer.class); |
|
127 |
return newData.set(index, value, strict); |
|
128 |
} |
|
129 |
||
130 |
@Override |
|
131 |
public ArrayData set(final int index, final long value, final boolean strict) { |
|
132 |
final ArrayData newData = convert(Long.class); |
|
133 |
return newData.set(index, value, strict); |
|
134 |
} |
|
135 |
||
136 |
@Override |
|
137 |
public ArrayData set(final int index, final double value, final boolean strict) { |
|
138 |
final ArrayData newData = convert(Double.class); |
|
139 |
return newData.set(index, value, strict); |
|
140 |
} |
|
141 |
||
142 |
@Override |
|
143 |
public int getInt(final int index) { |
|
144 |
throw new ArrayIndexOutOfBoundsException(index); |
|
145 |
} |
|
146 |
||
147 |
@Override |
|
148 |
public long getLong(final int index) { |
|
149 |
throw new ArrayIndexOutOfBoundsException(index); |
|
150 |
} |
|
151 |
||
152 |
@Override |
|
153 |
public double getDouble(final int index) { |
|
154 |
throw new ArrayIndexOutOfBoundsException(index); |
|
155 |
} |
|
156 |
||
157 |
@Override |
|
158 |
public Object getObject(final int index) { |
|
159 |
throw new ArrayIndexOutOfBoundsException(index); |
|
160 |
} |
|
161 |
||
162 |
@Override |
|
163 |
public boolean has(final int index) { |
|
164 |
return false; |
|
165 |
} |
|
166 |
||
167 |
@Override |
|
168 |
public ArrayData delete(final int index) { |
|
169 |
return new DeletedRangeArrayFilter(this, index, index); |
|
170 |
} |
|
171 |
||
172 |
@Override |
|
173 |
public ArrayData delete(final long fromIndex, final long toIndex) { |
|
174 |
return new DeletedRangeArrayFilter(this, fromIndex, toIndex); |
|
175 |
} |
|
176 |
||
177 |
@Override |
|
178 |
public Object pop() { |
|
179 |
return ScriptRuntime.UNDEFINED; |
|
180 |
} |
|
181 |
||
182 |
@Override |
|
183 |
public ArrayData slice(final long from, final long to) { |
|
184 |
return this; |
|
185 |
} |
|
186 |
} |