author | sundar |
Fri, 05 Jul 2013 14:38:04 +0530 | |
changeset 18851 | bdb92c95f886 |
parent 18842 | 3c3be808b593 |
child 19638 | bf08be79957a |
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 |
||
28 |
import jdk.nashorn.internal.objects.annotations.Attribute; |
|
29 |
import jdk.nashorn.internal.objects.annotations.Constructor; |
|
30 |
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
|
31 |
import jdk.nashorn.internal.objects.annotations.Property; |
16147 | 32 |
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
|
33 |
import jdk.nashorn.internal.objects.annotations.Where; |
18618
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
17771
diff
changeset
|
34 |
import jdk.nashorn.internal.runtime.PropertyMap; |
16147 | 35 |
import jdk.nashorn.internal.runtime.ScriptObject; |
36 |
import jdk.nashorn.internal.runtime.arrays.ArrayData; |
|
37 |
||
38 |
/** |
|
39 |
* Int16 array for the TypedArray extension |
|
40 |
*/ |
|
41 |
@ScriptClass("Int16Array") |
|
16226
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16151
diff
changeset
|
42 |
public final class NativeInt16Array extends ArrayBufferView { |
18618
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
17771
diff
changeset
|
43 |
|
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
17771
diff
changeset
|
44 |
// 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
|
45 |
@SuppressWarnings("unused") |
18618
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
17771
diff
changeset
|
46 |
private static PropertyMap $nasgenmap$; |
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
17771
diff
changeset
|
47 |
|
17771
9fedae4933e4
8015354: JSON.parse should not use [[Put]] but use [[DefineOwnProperty]] instead
sundar
parents:
17763
diff
changeset
|
48 |
/** |
9fedae4933e4
8015354: JSON.parse should not use [[Put]] but use [[DefineOwnProperty]] instead
sundar
parents:
17763
diff
changeset
|
49 |
* 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
|
50 |
*/ |
17763
f30b7aa0e736
8014735: Typed Array, BYTES_PER_ELEMENT should be a class property
sundar
parents:
16226
diff
changeset
|
51 |
@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
|
52 |
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
|
53 |
|
16147 | 54 |
private static final Factory FACTORY = new Factory(BYTES_PER_ELEMENT) { |
55 |
@Override |
|
56 |
public ArrayBufferView construct(final NativeArrayBuffer buffer, final int byteOffset, final int length) { |
|
57 |
return new NativeInt16Array(buffer, byteOffset, length); |
|
58 |
} |
|
59 |
@Override |
|
60 |
public ArrayData createArrayData(final NativeArrayBuffer buffer, final int byteOffset, final int length) { |
|
61 |
return new Int16ArrayData(buffer, byteOffset, length); |
|
62 |
} |
|
63 |
}; |
|
64 |
||
65 |
private static final class Int16ArrayData extends ArrayDataImpl { |
|
66 |
private Int16ArrayData(final NativeArrayBuffer buffer, final int byteOffset, final int elementLength) { |
|
67 |
super(buffer, byteOffset, elementLength); |
|
68 |
} |
|
69 |
||
70 |
@Override |
|
71 |
protected int byteIndex(final int index) { |
|
72 |
return index * BYTES_PER_ELEMENT + byteOffset; |
|
73 |
} |
|
74 |
||
75 |
@Override |
|
76 |
protected int getIntImpl(final int index) { |
|
77 |
final int byteIndex = byteIndex(index); |
|
78 |
final byte[] byteArray = buffer.getByteArray(); |
|
79 |
return byteArray[byteIndex ] & (short)0x00ff | |
|
80 |
byteArray[byteIndex+1] << 8 & (short)0xff00 ; |
|
81 |
} |
|
82 |
||
83 |
@Override |
|
84 |
protected void setImpl(final int index, final int value) { |
|
85 |
final int byteIndex = byteIndex(index); |
|
86 |
@SuppressWarnings("MismatchedReadAndWriteOfArray") |
|
87 |
final byte[] byteArray = buffer.getByteArray(); |
|
88 |
byteArray[byteIndex ] = (byte)(value & 0xff); |
|
89 |
byteArray[byteIndex+1] = (byte)(value >>> 8 & 0xff); |
|
90 |
} |
|
91 |
} |
|
92 |
||
93 |
/** |
|
94 |
* Constructor |
|
95 |
* |
|
96 |
* @param newObj is this typed array instantiated with the new operator |
|
97 |
* @param self self reference |
|
98 |
* @param args args |
|
99 |
* |
|
100 |
* @return new typed array |
|
101 |
*/ |
|
102 |
@Constructor(arity = 1) |
|
103 |
public static Object constructor(final boolean newObj, final Object self, final Object... args) { |
|
104 |
return constructorImpl(args, FACTORY); |
|
105 |
} |
|
106 |
||
107 |
NativeInt16Array(final NativeArrayBuffer buffer, final int byteOffset, final int byteLength) { |
|
108 |
super(buffer, byteOffset, byteLength); |
|
109 |
} |
|
110 |
||
111 |
@Override |
|
112 |
protected Factory factory() { |
|
113 |
return FACTORY; |
|
114 |
} |
|
115 |
||
116 |
/** |
|
117 |
* Set values |
|
118 |
* @param self self reference |
|
119 |
* @param array multiple values of array's type to set |
|
120 |
* @param offset optional start index, interpreted 0 if undefined |
|
121 |
* @return undefined |
|
122 |
*/ |
|
123 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
124 |
protected static Object set(final Object self, final Object array, final Object offset) { |
|
125 |
return ArrayBufferView.setImpl(self, array, offset); |
|
126 |
} |
|
127 |
||
128 |
/** |
|
129 |
* Returns a new TypedArray view of the ArrayBuffer store for this TypedArray, |
|
130 |
* referencing the elements at begin, inclusive, up to end, exclusive. If either |
|
131 |
* begin or end is negative, it refers to an index from the end of the array, |
|
132 |
* as opposed to from the beginning. |
|
133 |
* <p> |
|
134 |
* If end is unspecified, the subarray contains all elements from begin to the end |
|
135 |
* of the TypedArray. The range specified by the begin and end values is clamped to |
|
136 |
* the valid index range for the current array. If the computed length of the new |
|
137 |
* TypedArray would be negative, it is clamped to zero. |
|
138 |
* <p> |
|
139 |
* The returned TypedArray will be of the same type as the array on which this |
|
140 |
* method is invoked. |
|
141 |
* |
|
142 |
* @param self self reference |
|
143 |
* @param begin begin position |
|
144 |
* @param end end position |
|
145 |
* |
|
146 |
* @return sub array |
|
147 |
*/ |
|
148 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
149 |
protected static Object subarray(final Object self, final Object begin, final Object end) { |
|
150 |
return ArrayBufferView.subarrayImpl(self, begin, end); |
|
151 |
} |
|
152 |
||
153 |
@Override |
|
18851
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18842
diff
changeset
|
154 |
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
|
155 |
return global.getInt16ArrayPrototype(); |
16147 | 156 |
} |
157 |
} |