author | lagergren |
Thu, 25 Sep 2014 15:53:47 +0200 | |
changeset 26768 | 751b0f427090 |
parent 25865 | d38d876f1654 |
child 27104 | bc8ce3f84b84 |
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 |
||
24720 | 28 |
import static jdk.nashorn.internal.codegen.CompilerConstants.specialCall; |
29 |
import java.lang.invoke.MethodHandle; |
|
30 |
import java.lang.invoke.MethodHandles; |
|
31 |
import java.nio.ByteBuffer; |
|
32 |
import java.nio.ShortBuffer; |
|
16147 | 33 |
import jdk.nashorn.internal.objects.annotations.Attribute; |
34 |
import jdk.nashorn.internal.objects.annotations.Constructor; |
|
35 |
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
|
36 |
import jdk.nashorn.internal.objects.annotations.Property; |
16147 | 37 |
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
|
38 |
import jdk.nashorn.internal.objects.annotations.Where; |
24720 | 39 |
import jdk.nashorn.internal.runtime.JSType; |
18618
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
17771
diff
changeset
|
40 |
import jdk.nashorn.internal.runtime.PropertyMap; |
16147 | 41 |
import jdk.nashorn.internal.runtime.ScriptObject; |
42 |
import jdk.nashorn.internal.runtime.arrays.ArrayData; |
|
24720 | 43 |
import jdk.nashorn.internal.runtime.arrays.TypedArrayData; |
16147 | 44 |
|
45 |
/** |
|
46 |
* Int16 array for the TypedArray extension |
|
47 |
*/ |
|
48 |
@ScriptClass("Int16Array") |
|
16226
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16151
diff
changeset
|
49 |
public final class NativeInt16Array extends ArrayBufferView { |
18618
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
17771
diff
changeset
|
50 |
|
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
17771
diff
changeset
|
51 |
// 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
|
52 |
@SuppressWarnings("unused") |
18618
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
17771
diff
changeset
|
53 |
private static PropertyMap $nasgenmap$; |
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
17771
diff
changeset
|
54 |
|
17771
9fedae4933e4
8015354: JSON.parse should not use [[Put]] but use [[DefineOwnProperty]] instead
sundar
parents:
17763
diff
changeset
|
55 |
/** |
9fedae4933e4
8015354: JSON.parse should not use [[Put]] but use [[DefineOwnProperty]] instead
sundar
parents:
17763
diff
changeset
|
56 |
* 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
|
57 |
*/ |
17763
f30b7aa0e736
8014735: Typed Array, BYTES_PER_ELEMENT should be a class property
sundar
parents:
16226
diff
changeset
|
58 |
@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
|
59 |
public static final int BYTES_PER_ELEMENT = 2; |
f30b7aa0e736
8014735: Typed Array, BYTES_PER_ELEMENT should be a class property
sundar
parents:
16226
diff
changeset
|
60 |
|
16147 | 61 |
private static final Factory FACTORY = new Factory(BYTES_PER_ELEMENT) { |
62 |
@Override |
|
63 |
public ArrayBufferView construct(final NativeArrayBuffer buffer, final int byteOffset, final int length) { |
|
64 |
return new NativeInt16Array(buffer, byteOffset, length); |
|
65 |
} |
|
24720 | 66 |
|
16147 | 67 |
@Override |
24720 | 68 |
public Int16ArrayData createArrayData(final ByteBuffer nb, final int start, final int end) { |
69 |
return new Int16ArrayData(nb.asShortBuffer(), start, end); |
|
70 |
} |
|
71 |
||
72 |
@Override |
|
73 |
public String getClassName() { |
|
74 |
return "Int16Array"; |
|
16147 | 75 |
} |
76 |
}; |
|
77 |
||
24720 | 78 |
private static final class Int16ArrayData extends TypedArrayData<ShortBuffer> { |
79 |
||
80 |
private static final MethodHandle GET_ELEM = specialCall(MethodHandles.lookup(), Int16ArrayData.class, "getElem", int.class, int.class).methodHandle(); |
|
81 |
private static final MethodHandle SET_ELEM = specialCall(MethodHandles.lookup(), Int16ArrayData.class, "setElem", void.class, int.class, int.class).methodHandle(); |
|
82 |
||
83 |
private Int16ArrayData(final ShortBuffer nb, final int start, final int end) { |
|
84 |
super(((ShortBuffer)nb.position(start).limit(end)).slice(), end - start); |
|
85 |
} |
|
86 |
||
87 |
@Override |
|
88 |
protected MethodHandle getGetElem() { |
|
89 |
return GET_ELEM; |
|
16147 | 90 |
} |
91 |
||
92 |
@Override |
|
24720 | 93 |
protected MethodHandle getSetElem() { |
94 |
return SET_ELEM; |
|
95 |
} |
|
96 |
||
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
97 |
@Override |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
98 |
public Class<?> getElementType() { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
99 |
return int.class; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
100 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
101 |
|
24720 | 102 |
private int getElem(final int index) { |
103 |
try { |
|
104 |
return nb.get(index); |
|
105 |
} catch (final IndexOutOfBoundsException e) { |
|
106 |
throw new ClassCastException(); //force relink - this works for unoptimistic too |
|
107 |
} |
|
108 |
} |
|
109 |
||
110 |
private void setElem(final int index, final int elem) { |
|
111 |
try { |
|
112 |
nb.put(index, (short)elem); |
|
113 |
} catch (final IndexOutOfBoundsException e) { |
|
114 |
//swallow valid array indexes. it's ok. |
|
115 |
if (index < 0) { |
|
116 |
throw new ClassCastException(); |
|
117 |
} |
|
118 |
} |
|
119 |
} |
|
120 |
||
121 |
@Override |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24769
diff
changeset
|
122 |
public int getInt(final int index) { |
24720 | 123 |
return getElem(index); |
16147 | 124 |
} |
125 |
||
126 |
@Override |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24769
diff
changeset
|
127 |
public long getLong(final int index) { |
24720 | 128 |
return getInt(index); |
129 |
} |
|
130 |
||
131 |
@Override |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24769
diff
changeset
|
132 |
public double getDouble(final int index) { |
24720 | 133 |
return getInt(index); |
134 |
} |
|
135 |
||
136 |
@Override |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24769
diff
changeset
|
137 |
public Object getObject(final int index) { |
24720 | 138 |
return getInt(index); |
16147 | 139 |
} |
140 |
||
141 |
@Override |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24769
diff
changeset
|
142 |
public ArrayData set(final int index, final Object value, final boolean strict) { |
24720 | 143 |
return set(index, JSType.toInt32(value), strict); |
144 |
} |
|
145 |
||
146 |
@Override |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24769
diff
changeset
|
147 |
public ArrayData set(final int index, final int value, final boolean strict) { |
24720 | 148 |
setElem(index, value); |
149 |
return this; |
|
150 |
} |
|
151 |
||
152 |
@Override |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24769
diff
changeset
|
153 |
public ArrayData set(final int index, final long value, final boolean strict) { |
24720 | 154 |
return set(index, (int)value, strict); |
155 |
} |
|
156 |
||
157 |
@Override |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24769
diff
changeset
|
158 |
public ArrayData set(final int index, final double value, final boolean strict) { |
24720 | 159 |
return set(index, (int)value, strict); |
16147 | 160 |
} |
161 |
} |
|
162 |
||
163 |
/** |
|
164 |
* Constructor |
|
165 |
* |
|
166 |
* @param newObj is this typed array instantiated with the new operator |
|
167 |
* @param self self reference |
|
168 |
* @param args args |
|
169 |
* |
|
170 |
* @return new typed array |
|
171 |
*/ |
|
172 |
@Constructor(arity = 1) |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
19638
diff
changeset
|
173 |
public static NativeInt16Array constructor(final boolean newObj, final Object self, final Object... args) { |
24769 | 174 |
return (NativeInt16Array)constructorImpl(newObj, args, FACTORY); |
16147 | 175 |
} |
176 |
||
177 |
NativeInt16Array(final NativeArrayBuffer buffer, final int byteOffset, final int byteLength) { |
|
178 |
super(buffer, byteOffset, byteLength); |
|
179 |
} |
|
180 |
||
181 |
@Override |
|
182 |
protected Factory factory() { |
|
183 |
return FACTORY; |
|
184 |
} |
|
185 |
||
186 |
/** |
|
187 |
* Set values |
|
188 |
* @param self self reference |
|
189 |
* @param array multiple values of array's type to set |
|
190 |
* @param offset optional start index, interpreted 0 if undefined |
|
191 |
* @return undefined |
|
192 |
*/ |
|
193 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
194 |
protected static Object set(final Object self, final Object array, final Object offset) { |
|
195 |
return ArrayBufferView.setImpl(self, array, offset); |
|
196 |
} |
|
197 |
||
198 |
/** |
|
199 |
* Returns a new TypedArray view of the ArrayBuffer store for this TypedArray, |
|
200 |
* referencing the elements at begin, inclusive, up to end, exclusive. If either |
|
201 |
* begin or end is negative, it refers to an index from the end of the array, |
|
202 |
* as opposed to from the beginning. |
|
203 |
* <p> |
|
204 |
* If end is unspecified, the subarray contains all elements from begin to the end |
|
205 |
* of the TypedArray. The range specified by the begin and end values is clamped to |
|
206 |
* the valid index range for the current array. If the computed length of the new |
|
207 |
* TypedArray would be negative, it is clamped to zero. |
|
208 |
* <p> |
|
209 |
* The returned TypedArray will be of the same type as the array on which this |
|
210 |
* method is invoked. |
|
211 |
* |
|
212 |
* @param self self reference |
|
213 |
* @param begin begin position |
|
214 |
* @param end end position |
|
215 |
* |
|
216 |
* @return sub array |
|
217 |
*/ |
|
218 |
@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
|
219 |
protected static NativeInt16Array 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
|
220 |
return (NativeInt16Array)ArrayBufferView.subarrayImpl(self, begin, end); |
16147 | 221 |
} |
222 |
||
223 |
@Override |
|
18851
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18842
diff
changeset
|
224 |
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
|
225 |
return global.getInt16ArrayPrototype(); |
16147 | 226 |
} |
227 |
} |