author | attila |
Wed, 15 Oct 2014 15:57:46 +0200 | |
changeset 27104 | bc8ce3f84b84 |
parent 26768 | 751b0f427090 |
child 27209 | 30d8609b9561 |
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.objects; |
|
27 |
||
24769 | 28 |
import static jdk.nashorn.internal.codegen.CompilerConstants.specialCall; |
24720 | 29 |
import static jdk.nashorn.internal.codegen.CompilerConstants.staticCall; |
30 |
import static jdk.nashorn.internal.lookup.Lookup.MH; |
|
27104
bc8ce3f84b84
8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents:
26768
diff
changeset
|
31 |
|
24720 | 32 |
import java.lang.invoke.MethodHandle; |
33 |
import java.lang.invoke.MethodHandles; |
|
34 |
import java.nio.ByteBuffer; |
|
16147 | 35 |
import jdk.nashorn.internal.objects.annotations.Attribute; |
36 |
import jdk.nashorn.internal.objects.annotations.Constructor; |
|
37 |
import jdk.nashorn.internal.objects.annotations.Function; |
|
17763
f30b7aa0e736
8014735: Typed Array, BYTES_PER_ELEMENT should be a class property
sundar
parents:
16226
diff
changeset
|
38 |
import jdk.nashorn.internal.objects.annotations.Property; |
16147 | 39 |
import jdk.nashorn.internal.objects.annotations.ScriptClass; |
17763
f30b7aa0e736
8014735: Typed Array, BYTES_PER_ELEMENT should be a class property
sundar
parents:
16226
diff
changeset
|
40 |
import jdk.nashorn.internal.objects.annotations.Where; |
16147 | 41 |
import jdk.nashorn.internal.runtime.JSType; |
18618
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
17771
diff
changeset
|
42 |
import jdk.nashorn.internal.runtime.PropertyMap; |
16147 | 43 |
import jdk.nashorn.internal.runtime.ScriptObject; |
44 |
import jdk.nashorn.internal.runtime.arrays.ArrayData; |
|
24720 | 45 |
import jdk.nashorn.internal.runtime.arrays.TypedArrayData; |
16147 | 46 |
|
47 |
/** |
|
48 |
* Uint8 clamped array for TypedArray extension |
|
49 |
*/ |
|
50 |
@ScriptClass("Uint8ClampedArray") |
|
16226
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16151
diff
changeset
|
51 |
public final class NativeUint8ClampedArray extends ArrayBufferView { |
17771
9fedae4933e4
8015354: JSON.parse should not use [[Put]] but use [[DefineOwnProperty]] instead
sundar
parents:
17763
diff
changeset
|
52 |
/** |
9fedae4933e4
8015354: JSON.parse should not use [[Put]] but use [[DefineOwnProperty]] instead
sundar
parents:
17763
diff
changeset
|
53 |
* The size in bytes of each element in the array. |
9fedae4933e4
8015354: JSON.parse should not use [[Put]] but use [[DefineOwnProperty]] instead
sundar
parents:
17763
diff
changeset
|
54 |
*/ |
17763
f30b7aa0e736
8014735: Typed Array, BYTES_PER_ELEMENT should be a class property
sundar
parents:
16226
diff
changeset
|
55 |
@Property(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_WRITABLE | Attribute.NOT_CONFIGURABLE, where = Where.CONSTRUCTOR) |
f30b7aa0e736
8014735: Typed Array, BYTES_PER_ELEMENT should be a class property
sundar
parents:
16226
diff
changeset
|
56 |
public static final int BYTES_PER_ELEMENT = 1; |
f30b7aa0e736
8014735: Typed Array, BYTES_PER_ELEMENT should be a class property
sundar
parents:
16226
diff
changeset
|
57 |
|
18618
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
17771
diff
changeset
|
58 |
// initialized by nasgen |
18842
3c3be808b593
8019585: Sometimes a var declaration using itself in its init wasn't declared as canBeUndefined, causing erroneous bytecode
lagergren
parents:
18618
diff
changeset
|
59 |
@SuppressWarnings("unused") |
18618
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
17771
diff
changeset
|
60 |
private static PropertyMap $nasgenmap$; |
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
17771
diff
changeset
|
61 |
|
16147 | 62 |
private static final Factory FACTORY = new Factory(BYTES_PER_ELEMENT) { |
63 |
@Override |
|
64 |
public ArrayBufferView construct(final NativeArrayBuffer buffer, final int byteOffset, final int length) { |
|
65 |
return new NativeUint8ClampedArray(buffer, byteOffset, length); |
|
66 |
} |
|
67 |
||
68 |
@Override |
|
24720 | 69 |
public Uint8ClampedArrayData createArrayData(final ByteBuffer nb, final int start, final int end) { |
70 |
return new Uint8ClampedArrayData(nb, start, end); |
|
16147 | 71 |
} |
72 |
||
73 |
@Override |
|
24720 | 74 |
public String getClassName() { |
75 |
return "Uint8ClampedArray"; |
|
16147 | 76 |
} |
77 |
}; |
|
78 |
||
24720 | 79 |
private static final class Uint8ClampedArrayData extends TypedArrayData<ByteBuffer> { |
80 |
||
81 |
private static final MethodHandle GET_ELEM = specialCall(MethodHandles.lookup(), Uint8ClampedArrayData.class, "getElem", int.class, int.class).methodHandle(); |
|
82 |
private static final MethodHandle SET_ELEM = specialCall(MethodHandles.lookup(), Uint8ClampedArrayData.class, "setElem", void.class, int.class, int.class).methodHandle(); |
|
25828
077046a5d726
8044786: Some tests fail with non-optimistic compilation
attila
parents:
24778
diff
changeset
|
83 |
private static final MethodHandle RINT_D = staticCall(MethodHandles.lookup(), Uint8ClampedArrayData.class, "rint", double.class, double.class).methodHandle(); |
077046a5d726
8044786: Some tests fail with non-optimistic compilation
attila
parents:
24778
diff
changeset
|
84 |
private static final MethodHandle RINT_O = staticCall(MethodHandles.lookup(), Uint8ClampedArrayData.class, "rint", Object.class, Object.class).methodHandle(); |
24769 | 85 |
private static final MethodHandle CLAMP_LONG = staticCall(MethodHandles.lookup(), Uint8ClampedArrayData.class, "clampLong", long.class, long.class).methodHandle(); |
24720 | 86 |
|
87 |
private Uint8ClampedArrayData(final ByteBuffer nb, final int start, final int end) { |
|
88 |
super(((ByteBuffer)nb.position(start).limit(end)).slice(), end - start); |
|
16147 | 89 |
} |
90 |
||
91 |
@Override |
|
24720 | 92 |
protected MethodHandle getGetElem() { |
93 |
return GET_ELEM; |
|
16147 | 94 |
} |
95 |
||
96 |
@Override |
|
24720 | 97 |
protected MethodHandle getSetElem() { |
98 |
return SET_ELEM; |
|
99 |
} |
|
100 |
||
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
101 |
@Override |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
102 |
public Class<?> getElementType() { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
103 |
return int.class; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
104 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
105 |
|
24720 | 106 |
private int getElem(final int index) { |
107 |
try { |
|
108 |
return nb.get(index) & 0xff; |
|
109 |
} catch (final IndexOutOfBoundsException e) { |
|
110 |
throw new ClassCastException(); //force relink - this works for unoptimistic too |
|
23947
36ce4a262b4a
8030199: Nashorn: Uint8ClampedArray - Incorrect ToUint8Clamp implementation
hannesw
parents:
23763
diff
changeset
|
111 |
} |
36ce4a262b4a
8030199: Nashorn: Uint8ClampedArray - Incorrect ToUint8Clamp implementation
hannesw
parents:
23763
diff
changeset
|
112 |
} |
36ce4a262b4a
8030199: Nashorn: Uint8ClampedArray - Incorrect ToUint8Clamp implementation
hannesw
parents:
23763
diff
changeset
|
113 |
|
36ce4a262b4a
8030199: Nashorn: Uint8ClampedArray - Incorrect ToUint8Clamp implementation
hannesw
parents:
23763
diff
changeset
|
114 |
@Override |
24720 | 115 |
public MethodHandle getElementSetter(final Class<?> elementType) { |
116 |
final MethodHandle setter = super.getElementSetter(elementType); //getContinuousElementSetter(getClass(), setElem(), elementType); |
|
24769 | 117 |
if (setter != null) { |
25828
077046a5d726
8044786: Some tests fail with non-optimistic compilation
attila
parents:
24778
diff
changeset
|
118 |
if (elementType == Object.class) { |
077046a5d726
8044786: Some tests fail with non-optimistic compilation
attila
parents:
24778
diff
changeset
|
119 |
return MH.filterArguments(setter, 2, RINT_O); |
077046a5d726
8044786: Some tests fail with non-optimistic compilation
attila
parents:
24778
diff
changeset
|
120 |
} else if (elementType == double.class) { |
077046a5d726
8044786: Some tests fail with non-optimistic compilation
attila
parents:
24778
diff
changeset
|
121 |
return MH.filterArguments(setter, 2, RINT_D); |
24769 | 122 |
} else if (elementType == long.class) { |
123 |
return MH.filterArguments(setter, 2, CLAMP_LONG); |
|
124 |
} |
|
24720 | 125 |
} |
126 |
return setter; |
|
127 |
} |
|
128 |
||
129 |
private void setElem(final int index, final int elem) { |
|
130 |
try { |
|
131 |
final byte clamped; |
|
132 |
if ((elem & 0xffff_ff00) == 0) { |
|
133 |
clamped = (byte)elem; |
|
134 |
} else { |
|
135 |
clamped = elem < 0 ? 0 : (byte)0xff; |
|
136 |
} |
|
137 |
nb.put(index, clamped); |
|
138 |
} catch (final IndexOutOfBoundsException e) { |
|
139 |
//swallow valid array indexes. it's ok. |
|
140 |
if (index < 0) { |
|
141 |
throw new ClassCastException(); |
|
142 |
} |
|
143 |
} |
|
144 |
} |
|
145 |
||
146 |
@Override |
|
147 |
public boolean isClamped() { |
|
148 |
return true; |
|
149 |
} |
|
150 |
||
151 |
@Override |
|
152 |
public boolean isUnsigned() { |
|
153 |
return true; |
|
16147 | 154 |
} |
155 |
||
156 |
@Override |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24769
diff
changeset
|
157 |
public int getInt(final int index) { |
24720 | 158 |
return getElem(index); |
16147 | 159 |
} |
160 |
||
161 |
@Override |
|
27104
bc8ce3f84b84
8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents:
26768
diff
changeset
|
162 |
public int getIntOptimistic(final int index, final int programPoint) { |
bc8ce3f84b84
8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents:
26768
diff
changeset
|
163 |
return getElem(index); |
bc8ce3f84b84
8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents:
26768
diff
changeset
|
164 |
} |
bc8ce3f84b84
8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents:
26768
diff
changeset
|
165 |
|
bc8ce3f84b84
8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents:
26768
diff
changeset
|
166 |
@Override |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24769
diff
changeset
|
167 |
public long getLong(final int index) { |
24720 | 168 |
return getInt(index); |
169 |
} |
|
170 |
||
171 |
@Override |
|
27104
bc8ce3f84b84
8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents:
26768
diff
changeset
|
172 |
public long getLongOptimistic(final int index, final int programPoint) { |
bc8ce3f84b84
8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents:
26768
diff
changeset
|
173 |
return getElem(index); |
bc8ce3f84b84
8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents:
26768
diff
changeset
|
174 |
} |
bc8ce3f84b84
8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents:
26768
diff
changeset
|
175 |
|
bc8ce3f84b84
8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents:
26768
diff
changeset
|
176 |
@Override |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24769
diff
changeset
|
177 |
public double getDouble(final int index) { |
24720 | 178 |
return getInt(index); |
179 |
} |
|
180 |
||
181 |
@Override |
|
27104
bc8ce3f84b84
8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents:
26768
diff
changeset
|
182 |
public double getDoubleOptimistic(final int index, final int programPoint) { |
bc8ce3f84b84
8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents:
26768
diff
changeset
|
183 |
return getElem(index); |
bc8ce3f84b84
8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents:
26768
diff
changeset
|
184 |
} |
bc8ce3f84b84
8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents:
26768
diff
changeset
|
185 |
|
bc8ce3f84b84
8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents:
26768
diff
changeset
|
186 |
@Override |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24769
diff
changeset
|
187 |
public Object getObject(final int index) { |
24720 | 188 |
return getInt(index); |
16147 | 189 |
} |
190 |
||
191 |
@Override |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24769
diff
changeset
|
192 |
public ArrayData set(final int index, final Object value, final boolean strict) { |
24720 | 193 |
return set(index, JSType.toNumber(value), strict); |
194 |
} |
|
195 |
||
196 |
@Override |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24769
diff
changeset
|
197 |
public ArrayData set(final int index, final int value, final boolean strict) { |
24720 | 198 |
setElem(index, value); |
199 |
return this; |
|
16147 | 200 |
} |
201 |
||
202 |
@Override |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24769
diff
changeset
|
203 |
public ArrayData set(final int index, final long value, final boolean strict) { |
24720 | 204 |
return set(index, (int)value, strict); |
16147 | 205 |
} |
24720 | 206 |
|
207 |
@Override |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24769
diff
changeset
|
208 |
public ArrayData set(final int index, final double value, final boolean strict) { |
24720 | 209 |
return set(index, rint(value), strict); |
210 |
} |
|
211 |
||
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24769
diff
changeset
|
212 |
private static double rint(final double rint) { |
24720 | 213 |
return (int)Math.rint(rint); |
214 |
} |
|
215 |
||
24769 | 216 |
@SuppressWarnings("unused") |
25828
077046a5d726
8044786: Some tests fail with non-optimistic compilation
attila
parents:
24778
diff
changeset
|
217 |
private static Object rint(final Object rint) { |
077046a5d726
8044786: Some tests fail with non-optimistic compilation
attila
parents:
24778
diff
changeset
|
218 |
return rint(JSType.toNumber(rint)); |
077046a5d726
8044786: Some tests fail with non-optimistic compilation
attila
parents:
24778
diff
changeset
|
219 |
} |
077046a5d726
8044786: Some tests fail with non-optimistic compilation
attila
parents:
24778
diff
changeset
|
220 |
|
077046a5d726
8044786: Some tests fail with non-optimistic compilation
attila
parents:
24778
diff
changeset
|
221 |
@SuppressWarnings("unused") |
24769 | 222 |
private static long clampLong(final long l) { |
223 |
if(l < 0L) { |
|
224 |
return 0L; |
|
225 |
} else if(l > 0xffL) { |
|
226 |
return 0xffL; |
|
227 |
} |
|
228 |
return l; |
|
16147 | 229 |
} |
230 |
} |
|
231 |
||
232 |
/** |
|
233 |
* Constructor |
|
234 |
* |
|
235 |
* @param newObj is this typed array instantiated with the new operator |
|
236 |
* @param self self reference |
|
237 |
* @param args args |
|
238 |
* |
|
239 |
* @return new typed array |
|
240 |
*/ |
|
241 |
@Constructor(arity = 1) |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
19638
diff
changeset
|
242 |
public static NativeUint8ClampedArray constructor(final boolean newObj, final Object self, final Object... args) { |
24769 | 243 |
return (NativeUint8ClampedArray)constructorImpl(newObj, args, FACTORY); |
16147 | 244 |
} |
245 |
||
246 |
NativeUint8ClampedArray(final NativeArrayBuffer buffer, final int byteOffset, final int length) { |
|
247 |
super(buffer, byteOffset, length); |
|
248 |
} |
|
249 |
||
250 |
@Override |
|
251 |
protected Factory factory() { |
|
252 |
return FACTORY; |
|
253 |
} |
|
254 |
||
255 |
/** |
|
256 |
* Set values |
|
257 |
* @param self self reference |
|
258 |
* @param array multiple values of array's type to set |
|
259 |
* @param offset optional start index, interpreted 0 if undefined |
|
260 |
* @return undefined |
|
261 |
*/ |
|
262 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
263 |
protected static Object set(final Object self, final Object array, final Object offset) { |
|
264 |
return ArrayBufferView.setImpl(self, array, offset); |
|
265 |
} |
|
266 |
||
267 |
/** |
|
268 |
* Returns a new TypedArray view of the ArrayBuffer store for this TypedArray, |
|
269 |
* referencing the elements at begin, inclusive, up to end, exclusive. If either |
|
270 |
* begin or end is negative, it refers to an index from the end of the array, |
|
271 |
* as opposed to from the beginning. |
|
272 |
* <p> |
|
273 |
* If end is unspecified, the subarray contains all elements from begin to the end |
|
274 |
* of the TypedArray. The range specified by the begin and end values is clamped to |
|
275 |
* the valid index range for the current array. If the computed length of the new |
|
276 |
* TypedArray would be negative, it is clamped to zero. |
|
277 |
* <p> |
|
278 |
* The returned TypedArray will be of the same type as the array on which this |
|
279 |
* method is invoked. |
|
280 |
* |
|
281 |
* @param self self reference |
|
282 |
* @param begin begin position |
|
283 |
* @param end end position |
|
284 |
* |
|
285 |
* @return sub array |
|
286 |
*/ |
|
287 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
19638
diff
changeset
|
288 |
protected static NativeUint8ClampedArray subarray(final Object self, final Object begin, final Object end) { |
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
19638
diff
changeset
|
289 |
return (NativeUint8ClampedArray)ArrayBufferView.subarrayImpl(self, begin, end); |
16147 | 290 |
} |
291 |
||
292 |
@Override |
|
18851
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18842
diff
changeset
|
293 |
protected ScriptObject getPrototype(final Global global) { |
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18842
diff
changeset
|
294 |
return global.getUint8ClampedArrayPrototype(); |
16147 | 295 |
} |
296 |
} |