author | sundar |
Mon, 11 Feb 2013 21:26:06 +0530 | |
changeset 16226 | 0e4f37e6cc40 |
parent 16151 | 97c1e756ae1e |
child 17763 | f30b7aa0e736 |
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; |
|
31 |
import jdk.nashorn.internal.objects.annotations.ScriptClass; |
|
32 |
import jdk.nashorn.internal.runtime.JSType; |
|
33 |
import jdk.nashorn.internal.runtime.ScriptObject; |
|
34 |
import jdk.nashorn.internal.runtime.arrays.ArrayData; |
|
35 |
||
36 |
/** |
|
37 |
* Float32 array for the TypedArray extension |
|
38 |
*/ |
|
39 |
@ScriptClass("Float32Array") |
|
16226
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16151
diff
changeset
|
40 |
public final class NativeFloat32Array extends ArrayBufferView { |
16147 | 41 |
private static final int BYTES_PER_ELEMENT = 4; |
42 |
private static final Factory FACTORY = new Factory(BYTES_PER_ELEMENT) { |
|
43 |
@Override |
|
44 |
public ArrayBufferView construct(final NativeArrayBuffer buffer, final int byteOffset, final int length) { |
|
45 |
return new NativeFloat32Array(buffer, byteOffset, length); |
|
46 |
} |
|
47 |
@Override |
|
48 |
public ArrayData createArrayData(final NativeArrayBuffer buffer, final int byteOffset, final int length) { |
|
49 |
return new Float32ArrayData(buffer, byteOffset, length); |
|
50 |
} |
|
51 |
}; |
|
52 |
||
53 |
private static final class Float32ArrayData extends ArrayDataImpl { |
|
54 |
private Float32ArrayData(final NativeArrayBuffer buffer, final int byteOffset, final int elementLength) { |
|
55 |
super(buffer, byteOffset, elementLength); |
|
56 |
} |
|
57 |
||
58 |
@Override |
|
59 |
protected int byteIndex(final int index) { |
|
60 |
return index * BYTES_PER_ELEMENT + byteOffset; |
|
61 |
} |
|
62 |
||
63 |
@Override |
|
64 |
protected double getDoubleImpl(final int index) { |
|
65 |
final int byteIndex = byteIndex(index); |
|
66 |
final byte[] byteArray = buffer.getByteArray(); |
|
67 |
final int bits = byteArray[byteIndex ] & 0x0000_00ff | |
|
68 |
byteArray[byteIndex+1] << 8 & 0x0000_ff00 | |
|
69 |
byteArray[byteIndex+2] << 16 & 0x00ff_0000 | |
|
70 |
byteArray[byteIndex+3] << 24 & 0xff00_0000 ; |
|
71 |
return Float.intBitsToFloat(bits); |
|
72 |
} |
|
73 |
||
74 |
@Override |
|
75 |
protected int getIntImpl(final int index) { |
|
76 |
return (int)getDoubleImpl(index); |
|
77 |
} |
|
78 |
||
79 |
@Override |
|
80 |
protected long getLongImpl(final int key) { |
|
81 |
return (long)getDoubleImpl(key); |
|
82 |
} |
|
83 |
||
84 |
@Override |
|
85 |
protected Object getObjectImpl(final int key) { |
|
86 |
return getDoubleImpl(key); |
|
87 |
} |
|
88 |
||
89 |
@Override |
|
90 |
protected void setImpl(final int index, final double value) { |
|
91 |
final int bits = Float.floatToRawIntBits((float)value); |
|
92 |
final int byteIndex = byteIndex(index); |
|
93 |
@SuppressWarnings("MismatchedReadAndWriteOfArray") |
|
94 |
final byte[] byteArray = buffer.getByteArray(); |
|
95 |
byteArray[byteIndex ] = (byte)(bits & 0xff); |
|
96 |
byteArray[byteIndex+1] = (byte)(bits >>> 8 & 0xff); |
|
97 |
byteArray[byteIndex+2] = (byte)(bits >>> 16 & 0xff); |
|
98 |
byteArray[byteIndex+3] = (byte)(bits >>> 24 & 0xff); |
|
99 |
} |
|
100 |
||
101 |
@Override |
|
102 |
protected void setImpl(final int key, final int value) { |
|
103 |
setImpl(key, (double)value); |
|
104 |
} |
|
105 |
||
106 |
@Override |
|
107 |
protected void setImpl(final int key, final long value) { |
|
108 |
setImpl(key, (double)value); |
|
109 |
} |
|
110 |
||
111 |
@Override |
|
112 |
protected void setImpl(final int key, final Object value) { |
|
113 |
setImpl(key, JSType.toNumber(value)); |
|
114 |
} |
|
115 |
} |
|
116 |
||
117 |
/** |
|
118 |
* Constructor |
|
119 |
* |
|
120 |
* @param newObj is this typed array instantiated with the new operator |
|
121 |
* @param self self reference |
|
122 |
* @param args args |
|
123 |
* |
|
124 |
* @return new typed array |
|
125 |
*/ |
|
126 |
@Constructor(arity = 1) |
|
127 |
public static Object constructor(final boolean newObj, final Object self, final Object... args) { |
|
128 |
return constructorImpl(args, FACTORY); |
|
129 |
} |
|
130 |
||
131 |
NativeFloat32Array(final NativeArrayBuffer buffer, final int byteOffset, final int length) { |
|
132 |
super(buffer, byteOffset, length); |
|
133 |
} |
|
134 |
||
135 |
@Override |
|
136 |
protected Factory factory() { |
|
137 |
return FACTORY; |
|
138 |
} |
|
139 |
||
140 |
@Override |
|
141 |
protected boolean isFloatArray() { |
|
142 |
return true; |
|
143 |
} |
|
144 |
||
145 |
/** |
|
146 |
* Set values |
|
147 |
* @param self self reference |
|
148 |
* @param array multiple values of array's type to set |
|
149 |
* @param offset optional start index, interpreted 0 if undefined |
|
150 |
* @return undefined |
|
151 |
*/ |
|
152 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
153 |
protected static Object set(final Object self, final Object array, final Object offset) { |
|
154 |
return ArrayBufferView.setImpl(self, array, offset); |
|
155 |
} |
|
156 |
||
157 |
/** |
|
158 |
* Returns a new TypedArray view of the ArrayBuffer store for this TypedArray, |
|
159 |
* referencing the elements at begin, inclusive, up to end, exclusive. If either |
|
160 |
* begin or end is negative, it refers to an index from the end of the array, |
|
161 |
* as opposed to from the beginning. |
|
162 |
* <p> |
|
163 |
* If end is unspecified, the subarray contains all elements from begin to the end |
|
164 |
* of the TypedArray. The range specified by the begin and end values is clamped to |
|
165 |
* the valid index range for the current array. If the computed length of the new |
|
166 |
* TypedArray would be negative, it is clamped to zero. |
|
167 |
* <p> |
|
168 |
* The returned TypedArray will be of the same type as the array on which this |
|
169 |
* method is invoked. |
|
170 |
* |
|
171 |
* @param self self reference |
|
172 |
* @param begin begin position |
|
173 |
* @param end end position |
|
174 |
* |
|
175 |
* @return sub array |
|
176 |
*/ |
|
177 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
178 |
protected static Object subarray(final Object self, final Object begin, final Object end) { |
|
179 |
return ArrayBufferView.subarrayImpl(self, begin, end); |
|
180 |
} |
|
181 |
||
182 |
@Override |
|
183 |
protected ScriptObject getPrototype() { |
|
184 |
return Global.instance().getFloat32ArrayPrototype(); |
|
185 |
} |
|
186 |
} |