OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 // Flags: --harmony-tostring | 28 // Flags: --harmony-sharedarraybuffer |
29 | 29 |
30 // ArrayBuffer | 30 // SharedArrayBuffer |
31 | 31 |
32 function TestByteLength(param, expectedByteLength) { | 32 function TestByteLength(param, expectedByteLength) { |
33 var ab = new ArrayBuffer(param); | 33 var sab = new SharedArrayBuffer(param); |
34 assertSame(expectedByteLength, ab.byteLength); | 34 assertSame(expectedByteLength, sab.byteLength); |
35 } | 35 } |
36 | 36 |
37 function TestArrayBufferCreation() { | 37 function TestArrayBufferCreation() { |
38 TestByteLength(1, 1); | 38 TestByteLength(1, 1); |
39 TestByteLength(256, 256); | 39 TestByteLength(256, 256); |
40 TestByteLength(2.567, 2); | 40 TestByteLength(2.567, 2); |
41 | 41 |
42 TestByteLength("abc", 0); | 42 TestByteLength("abc", 0); |
43 | 43 |
44 TestByteLength(0, 0); | 44 TestByteLength(0, 0); |
45 | 45 |
46 assertThrows(function() { new ArrayBuffer(-10); }, RangeError); | 46 assertThrows(function() { new SharedArrayBuffer(-10); }, RangeError); |
47 assertThrows(function() { new ArrayBuffer(-2.567); }, RangeError); | 47 assertThrows(function() { new SharedArrayBuffer(-2.567); }, RangeError); |
48 | 48 |
49 /* TODO[dslomov]: Reenable the test | 49 /* TODO[dslomov]: Reenable the test |
50 assertThrows(function() { | 50 assertThrows(function() { |
51 var ab1 = new ArrayBuffer(0xFFFFFFFFFFFF) | 51 var ab1 = new SharedArrayBuffer(0xFFFFFFFFFFFF) |
52 }, RangeError); | 52 }, RangeError); |
53 */ | 53 */ |
54 | 54 |
55 var ab = new ArrayBuffer(); | 55 var sab = new SharedArrayBuffer(); |
56 assertSame(0, ab.byteLength); | 56 assertSame(0, sab.byteLength); |
57 assertEquals("[object ArrayBuffer]", | 57 assertEquals("[object SharedArrayBuffer]", |
58 Object.prototype.toString.call(ab)); | 58 Object.prototype.toString.call(sab)); |
59 } | 59 } |
60 | 60 |
61 TestArrayBufferCreation(); | 61 TestArrayBufferCreation(); |
62 | 62 |
63 function TestByteLengthNotWritable() { | 63 function TestByteLengthNotWritable() { |
64 var ab = new ArrayBuffer(1024); | 64 var sab = new SharedArrayBuffer(1024); |
65 assertSame(1024, ab.byteLength); | 65 assertSame(1024, sab.byteLength); |
66 | 66 |
67 assertThrows(function() { "use strict"; ab.byteLength = 42; }, TypeError); | 67 assertThrows(function() { "use strict"; sab.byteLength = 42; }, TypeError); |
68 } | 68 } |
69 | 69 |
70 TestByteLengthNotWritable(); | 70 TestByteLengthNotWritable(); |
71 | 71 |
72 function TestSlice(expectedResultLen, initialLen, start, end) { | 72 function TestArrayBufferNoSlice() { |
73 var ab = new ArrayBuffer(initialLen); | 73 var sab = new SharedArrayBuffer(10); |
74 var a1 = new Uint8Array(ab); | 74 assertEquals(undefined, sab.slice); |
75 for (var i = 0; i < a1.length; i++) { | |
76 a1[i] = 0xCA; | |
77 } | |
78 var slice = ab.slice(start, end); | |
79 assertSame(expectedResultLen, slice.byteLength); | |
80 var a2 = new Uint8Array(slice); | |
81 for (var i = 0; i < a2.length; i++) { | |
82 assertSame(0xCA, a2[i]); | |
83 } | |
84 } | 75 } |
85 | 76 |
86 function TestArrayBufferSlice() { | 77 TestArrayBufferNoSlice(); |
87 var ab = new ArrayBuffer(1024); | |
88 var ab1 = ab.slice(512, 1024); | |
89 assertSame(512, ab1.byteLength); | |
90 | 78 |
91 TestSlice(512, 1024, 512, 1024); | 79 // Typed arrays using SharedArrayBuffers |
92 TestSlice(512, 1024, 512); | |
93 | 80 |
94 TestSlice(0, 0, 1, 20); | 81 // TODO(binji): how many of these tests are necessary if there are no new |
95 TestSlice(100, 100, 0, 100); | 82 // TypedArray types? |
96 TestSlice(100, 100, 0, 1000); | |
97 | 83 |
98 TestSlice(0, 100, 5, 1); | 84 function MakeSharedTypedArray(constr, numElements) { |
99 | 85 var sab = new SharedArrayBuffer(constr.BYTES_PER_ELEMENT * numElements); |
100 TestSlice(1, 100, -11, -10); | 86 return new constr(sab); |
101 TestSlice(9, 100, -10, 99); | |
102 TestSlice(0, 100, -10, 80); | |
103 TestSlice(10, 100, 80, -10); | |
104 | |
105 TestSlice(10, 100, 90, "100"); | |
106 TestSlice(10, 100, "90", "100"); | |
107 | |
108 TestSlice(0, 100, 90, "abc"); | |
109 TestSlice(10, 100, "abc", 10); | |
110 | |
111 TestSlice(10, 100, 0.96, 10.96); | |
112 TestSlice(10, 100, 0.96, 10.01); | |
113 TestSlice(10, 100, 0.01, 10.01); | |
114 TestSlice(10, 100, 0.01, 10.96); | |
115 | |
116 TestSlice(10, 100, 90); | |
117 TestSlice(10, 100, -10); | |
118 } | 87 } |
119 | 88 |
120 TestArrayBufferSlice(); | |
121 | |
122 // Typed arrays | |
123 | |
124 function TestTypedArray(constr, elementSize, typicalElement) { | 89 function TestTypedArray(constr, elementSize, typicalElement) { |
125 assertSame(elementSize, constr.BYTES_PER_ELEMENT); | 90 assertSame(elementSize, constr.BYTES_PER_ELEMENT); |
126 | 91 |
127 var ab = new ArrayBuffer(256*elementSize); | 92 var sab = new SharedArrayBuffer(256*elementSize); |
128 | 93 |
129 var a0 = new constr(30); | 94 var a0 = new constr(30); |
130 assertEquals("[object " + constr.name + "]", | 95 assertEquals("[object " + constr.name + "]", |
131 Object.prototype.toString.call(a0)); | 96 Object.prototype.toString.call(a0)); |
132 | 97 |
133 assertTrue(ArrayBuffer.isView(a0)); | 98 // TODO(binji): Should this return false here? It is a view, but it doesn't |
| 99 // view a SharedArrayBuffer... |
| 100 assertTrue(SharedArrayBuffer.isView(a0)); |
134 assertSame(elementSize, a0.BYTES_PER_ELEMENT); | 101 assertSame(elementSize, a0.BYTES_PER_ELEMENT); |
135 assertSame(30, a0.length); | 102 assertSame(30, a0.length); |
136 assertSame(30*elementSize, a0.byteLength); | 103 assertSame(30*elementSize, a0.byteLength); |
137 assertSame(0, a0.byteOffset); | 104 assertSame(0, a0.byteOffset); |
138 assertSame(30*elementSize, a0.buffer.byteLength); | 105 assertSame(30*elementSize, a0.buffer.byteLength); |
139 | 106 |
140 var aLen0 = new constr(0); | 107 var aOverBufferLen0 = new constr(sab, 128*elementSize, 0); |
141 assertSame(elementSize, aLen0.BYTES_PER_ELEMENT); | 108 assertSame(sab, aOverBufferLen0.buffer); |
142 assertSame(0, aLen0.length); | |
143 assertSame(0, aLen0.byteLength); | |
144 assertSame(0, aLen0.byteOffset); | |
145 assertSame(0, aLen0.buffer.byteLength); | |
146 | |
147 var aOverBufferLen0 = new constr(ab, 128*elementSize, 0); | |
148 assertSame(ab, aOverBufferLen0.buffer); | |
149 assertSame(elementSize, aOverBufferLen0.BYTES_PER_ELEMENT); | 109 assertSame(elementSize, aOverBufferLen0.BYTES_PER_ELEMENT); |
150 assertSame(0, aOverBufferLen0.length); | 110 assertSame(0, aOverBufferLen0.length); |
151 assertSame(0, aOverBufferLen0.byteLength); | 111 assertSame(0, aOverBufferLen0.byteLength); |
152 assertSame(128*elementSize, aOverBufferLen0.byteOffset); | 112 assertSame(128*elementSize, aOverBufferLen0.byteOffset); |
153 | 113 |
154 var a1 = new constr(ab, 128*elementSize, 128); | 114 var a1 = new constr(sab, 128*elementSize, 128); |
155 assertSame(ab, a1.buffer); | 115 assertSame(sab, a1.buffer); |
156 assertSame(elementSize, a1.BYTES_PER_ELEMENT); | 116 assertSame(elementSize, a1.BYTES_PER_ELEMENT); |
157 assertSame(128, a1.length); | 117 assertSame(128, a1.length); |
158 assertSame(128*elementSize, a1.byteLength); | 118 assertSame(128*elementSize, a1.byteLength); |
159 assertSame(128*elementSize, a1.byteOffset); | 119 assertSame(128*elementSize, a1.byteOffset); |
160 | 120 |
161 | 121 |
162 var a2 = new constr(ab, 64*elementSize, 128); | 122 var a2 = new constr(sab, 64*elementSize, 128); |
163 assertSame(ab, a2.buffer); | 123 assertSame(sab, a2.buffer); |
164 assertSame(elementSize, a2.BYTES_PER_ELEMENT); | 124 assertSame(elementSize, a2.BYTES_PER_ELEMENT); |
165 assertSame(128, a2.length); | 125 assertSame(128, a2.length); |
166 assertSame(128*elementSize, a2.byteLength); | 126 assertSame(128*elementSize, a2.byteLength); |
167 assertSame(64*elementSize, a2.byteOffset); | 127 assertSame(64*elementSize, a2.byteOffset); |
168 | 128 |
169 var a3 = new constr(ab, 192*elementSize); | 129 var a3 = new constr(sab, 192*elementSize); |
170 assertSame(ab, a3.buffer); | 130 assertSame(sab, a3.buffer); |
171 assertSame(64, a3.length); | 131 assertSame(64, a3.length); |
172 assertSame(64*elementSize, a3.byteLength); | 132 assertSame(64*elementSize, a3.byteLength); |
173 assertSame(192*elementSize, a3.byteOffset); | 133 assertSame(192*elementSize, a3.byteOffset); |
174 | 134 |
175 var a4 = new constr(ab); | 135 var a4 = new constr(sab); |
176 assertSame(ab, a4.buffer); | 136 assertSame(sab, a4.buffer); |
177 assertSame(256, a4.length); | 137 assertSame(256, a4.length); |
178 assertSame(256*elementSize, a4.byteLength); | 138 assertSame(256*elementSize, a4.byteLength); |
179 assertSame(0, a4.byteOffset); | 139 assertSame(0, a4.byteOffset); |
180 | 140 |
181 | 141 |
182 var i; | 142 var i; |
183 for (i = 0; i < 128; i++) { | 143 for (i = 0; i < 128; i++) { |
184 a1[i] = typicalElement; | 144 a1[i] = typicalElement; |
185 } | 145 } |
186 | 146 |
(...skipping 14 matching lines...) Expand all Loading... |
201 } | 161 } |
202 | 162 |
203 for (i = 0; i < 128; i++) { | 163 for (i = 0; i < 128; i++) { |
204 assertSame(0, a4[i]); | 164 assertSame(0, a4[i]); |
205 } | 165 } |
206 | 166 |
207 for (i = 128; i < 256; i++) { | 167 for (i = 128; i < 256; i++) { |
208 assertSame(typicalElement, a4[i]); | 168 assertSame(typicalElement, a4[i]); |
209 } | 169 } |
210 | 170 |
211 var aAtTheEnd = new constr(ab, 256*elementSize); | 171 var aAtTheEnd = new constr(sab, 256*elementSize); |
212 assertSame(elementSize, aAtTheEnd.BYTES_PER_ELEMENT); | 172 assertSame(elementSize, aAtTheEnd.BYTES_PER_ELEMENT); |
213 assertSame(0, aAtTheEnd.length); | 173 assertSame(0, aAtTheEnd.length); |
214 assertSame(0, aAtTheEnd.byteLength); | 174 assertSame(0, aAtTheEnd.byteLength); |
215 assertSame(256*elementSize, aAtTheEnd.byteOffset); | 175 assertSame(256*elementSize, aAtTheEnd.byteOffset); |
216 | 176 |
217 assertThrows(function () { new constr(ab, 257*elementSize); }, RangeError); | 177 assertThrows(function () { new constr(sab, 257*elementSize); }, RangeError); |
218 assertThrows( | 178 assertThrows( |
219 function () { new constr(ab, 128*elementSize, 192); }, | 179 function () { new constr(sab, 128*elementSize, 192); }, |
220 RangeError); | 180 RangeError); |
221 | 181 |
222 if (elementSize !== 1) { | 182 if (elementSize !== 1) { |
223 assertThrows(function() { new constr(ab, 128*elementSize - 1, 10); }, | 183 assertThrows(function() { new constr(sab, 128*elementSize - 1, 10); }, |
224 RangeError); | 184 RangeError); |
225 var unalignedArrayBuffer = new ArrayBuffer(10*elementSize + 1); | 185 var unalignedArrayBuffer = new SharedArrayBuffer(10*elementSize + 1); |
226 var goodArray = new constr(unalignedArrayBuffer, 0, 10); | 186 var goodArray = new constr(unalignedArrayBuffer, 0, 10); |
227 assertSame(10, goodArray.length); | 187 assertSame(10, goodArray.length); |
228 assertSame(10*elementSize, goodArray.byteLength); | 188 assertSame(10*elementSize, goodArray.byteLength); |
229 assertThrows(function() { new constr(unalignedArrayBuffer)}, RangeError); | 189 assertThrows(function() { new constr(unalignedArrayBuffer)}, RangeError); |
230 assertThrows(function() { new constr(unalignedArrayBuffer, 5*elementSize)}, | 190 assertThrows(function() { new constr(unalignedArrayBuffer, 5*elementSize)}, |
231 RangeError); | 191 RangeError); |
232 } | 192 } |
233 | 193 |
234 var aFromString = new constr("30"); | 194 var abLen0 = new SharedArrayBuffer(0); |
235 assertSame(elementSize, aFromString.BYTES_PER_ELEMENT); | |
236 assertSame(30, aFromString.length); | |
237 assertSame(30*elementSize, aFromString.byteLength); | |
238 assertSame(0, aFromString.byteOffset); | |
239 assertSame(30*elementSize, aFromString.buffer.byteLength); | |
240 | |
241 var jsArray = []; | |
242 for (i = 0; i < 30; i++) { | |
243 jsArray.push(typicalElement); | |
244 } | |
245 var aFromArray = new constr(jsArray); | |
246 assertSame(elementSize, aFromArray.BYTES_PER_ELEMENT); | |
247 assertSame(30, aFromArray.length); | |
248 assertSame(30*elementSize, aFromArray.byteLength); | |
249 assertSame(0, aFromArray.byteOffset); | |
250 assertSame(30*elementSize, aFromArray.buffer.byteLength); | |
251 for (i = 0; i < 30; i++) { | |
252 assertSame(typicalElement, aFromArray[i]); | |
253 } | |
254 | |
255 var abLen0 = new ArrayBuffer(0); | |
256 var aOverAbLen0 = new constr(abLen0); | 195 var aOverAbLen0 = new constr(abLen0); |
257 assertSame(abLen0, aOverAbLen0.buffer); | 196 assertSame(abLen0, aOverAbLen0.buffer); |
258 assertSame(elementSize, aOverAbLen0.BYTES_PER_ELEMENT); | 197 assertSame(elementSize, aOverAbLen0.BYTES_PER_ELEMENT); |
259 assertSame(0, aOverAbLen0.length); | 198 assertSame(0, aOverAbLen0.length); |
260 assertSame(0, aOverAbLen0.byteLength); | 199 assertSame(0, aOverAbLen0.byteLength); |
261 assertSame(0, aOverAbLen0.byteOffset); | 200 assertSame(0, aOverAbLen0.byteOffset); |
262 | 201 |
263 var aNoParam = new constr(); | 202 var a = new constr(sab, 64*elementSize, 128); |
264 assertSame(elementSize, aNoParam.BYTES_PER_ELEMENT); | |
265 assertSame(0, aNoParam.length); | |
266 assertSame(0, aNoParam.byteLength); | |
267 assertSame(0, aNoParam.byteOffset); | |
268 | |
269 var a = new constr(ab, 64*elementSize, 128); | |
270 assertEquals("[object " + constr.name + "]", | 203 assertEquals("[object " + constr.name + "]", |
271 Object.prototype.toString.call(a)); | 204 Object.prototype.toString.call(a)); |
272 var desc = Object.getOwnPropertyDescriptor( | 205 var desc = Object.getOwnPropertyDescriptor( |
273 constr.prototype, Symbol.toStringTag); | 206 constr.prototype, Symbol.toStringTag); |
274 assertTrue(desc.configurable); | 207 assertTrue(desc.configurable); |
275 assertFalse(desc.enumerable); | 208 assertFalse(desc.enumerable); |
276 assertFalse(!!desc.writable); | 209 assertFalse(!!desc.writable); |
277 assertFalse(!!desc.set); | 210 assertFalse(!!desc.set); |
278 assertEquals("function", typeof desc.get); | 211 assertEquals("function", typeof desc.get); |
279 } | 212 } |
280 | 213 |
281 TestTypedArray(Uint8Array, 1, 0xFF); | 214 TestTypedArray(Uint8Array, 1, 0xFF); |
282 TestTypedArray(Int8Array, 1, -0x7F); | 215 TestTypedArray(Int8Array, 1, -0x7F); |
283 TestTypedArray(Uint16Array, 2, 0xFFFF); | 216 TestTypedArray(Uint16Array, 2, 0xFFFF); |
284 TestTypedArray(Int16Array, 2, -0x7FFF); | 217 TestTypedArray(Int16Array, 2, -0x7FFF); |
285 TestTypedArray(Uint32Array, 4, 0xFFFFFFFF); | 218 TestTypedArray(Uint32Array, 4, 0xFFFFFFFF); |
286 TestTypedArray(Int32Array, 4, -0x7FFFFFFF); | 219 TestTypedArray(Int32Array, 4, -0x7FFFFFFF); |
287 TestTypedArray(Float32Array, 4, 0.5); | 220 TestTypedArray(Float32Array, 4, 0.5); |
288 TestTypedArray(Float64Array, 8, 0.5); | 221 TestTypedArray(Float64Array, 8, 0.5); |
289 TestTypedArray(Uint8ClampedArray, 1, 0xFF); | 222 TestTypedArray(Uint8ClampedArray, 1, 0xFF); |
290 | 223 |
291 function SubarrayTestCase(constructor, item, expectedResultLen, expectedStartInd
ex, | 224 |
292 initialLen, start, end) { | 225 function SubarrayTestCase(constructor, item, expectedResultLen, |
293 var a = new constructor(initialLen); | 226 expectedStartIndex, initialLen, start, end) { |
| 227 var a = MakeSharedTypedArray(constructor, initialLen); |
294 var s = a.subarray(start, end); | 228 var s = a.subarray(start, end); |
295 assertSame(constructor, s.constructor); | 229 assertSame(constructor, s.constructor); |
296 assertSame(expectedResultLen, s.length); | 230 assertSame(expectedResultLen, s.length); |
297 if (s.length > 0) { | 231 if (s.length > 0) { |
298 s[0] = item; | 232 s[0] = item; |
299 assertSame(item, a[expectedStartIndex]); | 233 assertSame(item, a[expectedStartIndex]); |
300 } | 234 } |
301 } | 235 } |
302 | 236 |
303 function TestSubArray(constructor, item) { | 237 function TestSubArray(constructor, item) { |
(...skipping 17 matching lines...) Expand all Loading... |
321 SubarrayTestCase(constructor, item, 10,0, 100, "abc", 10); | 255 SubarrayTestCase(constructor, item, 10,0, 100, "abc", 10); |
322 | 256 |
323 SubarrayTestCase(constructor, item, 10,0, 100, 0.96, 10.96); | 257 SubarrayTestCase(constructor, item, 10,0, 100, 0.96, 10.96); |
324 SubarrayTestCase(constructor, item, 10,0, 100, 0.96, 10.01); | 258 SubarrayTestCase(constructor, item, 10,0, 100, 0.96, 10.01); |
325 SubarrayTestCase(constructor, item, 10,0, 100, 0.01, 10.01); | 259 SubarrayTestCase(constructor, item, 10,0, 100, 0.01, 10.01); |
326 SubarrayTestCase(constructor, item, 10,0, 100, 0.01, 10.96); | 260 SubarrayTestCase(constructor, item, 10,0, 100, 0.01, 10.96); |
327 | 261 |
328 | 262 |
329 SubarrayTestCase(constructor, item, 10,90, 100, 90); | 263 SubarrayTestCase(constructor, item, 10,90, 100, 90); |
330 SubarrayTestCase(constructor, item, 10,90, 100, -10); | 264 SubarrayTestCase(constructor, item, 10,90, 100, -10); |
331 | |
332 var method = constructor.prototype.subarray; | |
333 method.call(new constructor(100), 0, 100); | |
334 var o = {}; | |
335 assertThrows(function() { method.call(o, 0, 100); }, TypeError); | |
336 } | 265 } |
337 | 266 |
338 TestSubArray(Uint8Array, 0xFF); | 267 TestSubArray(Uint8Array, 0xFF); |
339 TestSubArray(Int8Array, -0x7F); | 268 TestSubArray(Int8Array, -0x7F); |
340 TestSubArray(Uint16Array, 0xFFFF); | 269 TestSubArray(Uint16Array, 0xFFFF); |
341 TestSubArray(Int16Array, -0x7FFF); | 270 TestSubArray(Int16Array, -0x7FFF); |
342 TestSubArray(Uint32Array, 0xFFFFFFFF); | 271 TestSubArray(Uint32Array, 0xFFFFFFFF); |
343 TestSubArray(Int32Array, -0x7FFFFFFF); | 272 TestSubArray(Int32Array, -0x7FFFFFFF); |
344 TestSubArray(Float32Array, 0.5); | 273 TestSubArray(Float32Array, 0.5); |
345 TestSubArray(Float64Array, 0.5); | 274 TestSubArray(Float64Array, 0.5); |
346 TestSubArray(Uint8ClampedArray, 0xFF); | 275 TestSubArray(Uint8ClampedArray, 0xFF); |
347 | 276 |
348 function TestTypedArrayOutOfRange(constructor, value, result) { | 277 function TestTypedArrayOutOfRange(constructor, value, result) { |
349 var a = new constructor(1); | 278 var a = MakeSharedTypedArray(constructor, 1); |
350 a[0] = value; | 279 a[0] = value; |
351 assertSame(result, a[0]); | 280 assertSame(result, a[0]); |
352 } | 281 } |
353 | 282 |
354 TestTypedArrayOutOfRange(Uint8Array, 0x1FA, 0xFA); | 283 TestTypedArrayOutOfRange(Uint8Array, 0x1FA, 0xFA); |
355 TestTypedArrayOutOfRange(Uint8Array, -1, 0xFF); | 284 TestTypedArrayOutOfRange(Uint8Array, -1, 0xFF); |
356 | 285 |
357 TestTypedArrayOutOfRange(Int8Array, 0x1FA, 0x7A - 0x80); | 286 TestTypedArrayOutOfRange(Int8Array, 0x1FA, 0x7A - 0x80); |
358 | 287 |
359 TestTypedArrayOutOfRange(Uint16Array, 0x1FFFA, 0xFFFA); | 288 TestTypedArrayOutOfRange(Uint16Array, 0x1FFFA, 0xFFFA); |
(...skipping 18 matching lines...) Expand all Loading... |
378 Float32Array, | 307 Float32Array, |
379 Float64Array]; | 308 Float64Array]; |
380 | 309 |
381 function TestPropertyTypeChecks(constructor) { | 310 function TestPropertyTypeChecks(constructor) { |
382 function CheckProperty(name) { | 311 function CheckProperty(name) { |
383 var d = Object.getOwnPropertyDescriptor(constructor.prototype, name); | 312 var d = Object.getOwnPropertyDescriptor(constructor.prototype, name); |
384 var o = {}; | 313 var o = {}; |
385 assertThrows(function() {d.get.call(o);}, TypeError); | 314 assertThrows(function() {d.get.call(o);}, TypeError); |
386 for (var i = 0; i < typedArrayConstructors.length; i++) { | 315 for (var i = 0; i < typedArrayConstructors.length; i++) { |
387 var ctor = typedArrayConstructors[i]; | 316 var ctor = typedArrayConstructors[i]; |
388 var a = new ctor(10); | 317 var a = MakeSharedTypedArray(ctor, 10); |
389 if (ctor === constructor) { | 318 if (ctor === constructor) { |
390 d.get.call(a); // shouldn't throw | 319 d.get.call(a); // shouldn't throw |
391 } else { | 320 } else { |
392 assertThrows(function() {d.get.call(a);}, TypeError); | 321 assertThrows(function() {d.get.call(a);}, TypeError); |
393 } | 322 } |
394 } | 323 } |
395 } | 324 } |
396 | 325 |
397 CheckProperty("buffer"); | 326 CheckProperty("buffer"); |
398 CheckProperty("byteOffset"); | 327 CheckProperty("byteOffset"); |
399 CheckProperty("byteLength"); | 328 CheckProperty("byteLength"); |
400 CheckProperty("length"); | 329 CheckProperty("length"); |
401 } | 330 } |
402 | 331 |
403 for(i = 0; i < typedArrayConstructors.length; i++) { | 332 for(i = 0; i < typedArrayConstructors.length; i++) { |
404 TestPropertyTypeChecks(typedArrayConstructors[i]); | 333 TestPropertyTypeChecks(typedArrayConstructors[i]); |
405 } | 334 } |
406 | 335 |
407 | |
408 function TestTypedArraySet() { | 336 function TestTypedArraySet() { |
409 // Test array.set in different combinations. | 337 // Test array.set in different combinations. |
410 | 338 |
411 function assertArrayPrefix(expected, array) { | 339 function assertArrayPrefix(expected, array) { |
412 for (var i = 0; i < expected.length; ++i) { | 340 for (var i = 0; i < expected.length; ++i) { |
413 assertEquals(expected[i], array[i]); | 341 assertEquals(expected[i], array[i]); |
414 } | 342 } |
415 } | 343 } |
416 | 344 |
417 var a11 = new Int16Array([1, 2, 3, 4, 0, -1]) | 345 // SharedTypedArrays don't allow initialization via array-like |
418 var a12 = new Uint16Array(15) | 346 function initializeFromArray(constructor, array) { |
| 347 var buffer = MakeSharedTypedArray(constructor, array.length); |
| 348 for (var i = 0; i < array.length; ++i) { |
| 349 buffer[i] = array[i]; |
| 350 } |
| 351 return buffer; |
| 352 } |
| 353 |
| 354 var a11 = initializeFromArray(Int16Array, [1, 2, 3, 4, 0, -1]) |
| 355 var a12 = MakeSharedTypedArray(Uint16Array, 15); |
419 a12.set(a11, 3) | 356 a12.set(a11, 3) |
420 assertArrayPrefix([0, 0, 0, 1, 2, 3, 4, 0, 0xffff, 0, 0], a12) | 357 assertArrayPrefix([0, 0, 0, 1, 2, 3, 4, 0, 0xffff, 0, 0], a12) |
421 assertThrows(function(){ a11.set(a12) }) | 358 assertThrows(function(){ a11.set(a12) }) |
422 | 359 |
423 var a21 = [1, undefined, 10, NaN, 0, -1, {valueOf: function() {return 3}}] | 360 var a21 = [1, undefined, 10, NaN, 0, -1, {valueOf: function() {return 3}}] |
424 var a22 = new Int32Array(12) | 361 var a22 = MakeSharedTypedArray(Int32Array, 12) |
425 a22.set(a21, 2) | 362 a22.set(a21, 2) |
426 assertArrayPrefix([0, 0, 1, 0, 10, 0, 0, -1, 3, 0], a22) | 363 assertArrayPrefix([0, 0, 1, 0, 10, 0, 0, -1, 3, 0], a22) |
427 | 364 |
428 var a31 = new Float32Array([2, 4, 6, 8, 11, NaN, 1/0, -3]) | 365 var a31 = initializeFromArray(Float32Array, [2, 4, 6, 8, 11, NaN, 1/0, -3]) |
429 var a32 = a31.subarray(2, 6) | 366 var a32 = a31.subarray(2, 6) |
430 a31.set(a32, 4) | 367 a31.set(a32, 4) |
431 assertArrayPrefix([2, 4, 6, 8, 6, 8, 11, NaN], a31) | 368 assertArrayPrefix([2, 4, 6, 8, 6, 8, 11, NaN], a31) |
432 assertArrayPrefix([6, 8, 6, 8], a32) | 369 assertArrayPrefix([6, 8, 6, 8], a32) |
433 | 370 |
434 var a4 = new Uint8ClampedArray([3,2,5,6]) | 371 var a4 = initializeFromArray(Uint8ClampedArray, [3,2,5,6]) |
435 a4.set(a4) | 372 a4.set(a4) |
436 assertArrayPrefix([3, 2, 5, 6], a4) | 373 assertArrayPrefix([3, 2, 5, 6], a4) |
437 | 374 |
438 // Cases with overlapping backing store but different element sizes. | 375 // Cases with overlapping backing store but different element sizes. |
439 var b = new ArrayBuffer(4) | 376 var b = new SharedArrayBuffer(4) |
440 var a5 = new Int16Array(b) | 377 var a5 = new Int16Array(b) |
441 var a50 = new Int8Array(b) | 378 var a50 = new Int8Array(b) |
442 var a51 = new Int8Array(b, 0, 2) | 379 var a51 = new Int8Array(b, 0, 2) |
443 var a52 = new Int8Array(b, 1, 2) | 380 var a52 = new Int8Array(b, 1, 2) |
444 var a53 = new Int8Array(b, 2, 2) | 381 var a53 = new Int8Array(b, 2, 2) |
445 | 382 |
446 a5.set([0x5050, 0x0a0a]) | 383 a5.set([0x5050, 0x0a0a]) |
447 assertArrayPrefix([0x50, 0x50, 0x0a, 0x0a], a50) | 384 assertArrayPrefix([0x50, 0x50, 0x0a, 0x0a], a50) |
448 assertArrayPrefix([0x50, 0x50], a51) | 385 assertArrayPrefix([0x50, 0x50], a51) |
449 assertArrayPrefix([0x50, 0x0a], a52) | 386 assertArrayPrefix([0x50, 0x0a], a52) |
(...skipping 17 matching lines...) Expand all Loading... |
467 | 404 |
468 a50.set([0x50, 0x51, 0x0a, 0x0b]) | 405 a50.set([0x50, 0x51, 0x0a, 0x0b]) |
469 a5.set(a52) | 406 a5.set(a52) |
470 assertArrayPrefix([0x0051, 0x000a], a5) | 407 assertArrayPrefix([0x0051, 0x000a], a5) |
471 | 408 |
472 a50.set([0x50, 0x51, 0x0a, 0x0b]) | 409 a50.set([0x50, 0x51, 0x0a, 0x0b]) |
473 a5.set(a53) | 410 a5.set(a53) |
474 assertArrayPrefix([0x000a, 0x000b], a5) | 411 assertArrayPrefix([0x000a, 0x000b], a5) |
475 | 412 |
476 // Mixed types of same size. | 413 // Mixed types of same size. |
477 var a61 = new Float32Array([1.2, 12.3]) | 414 var a61 = initializeFromArray(Float32Array, [1.2, 12.3]) |
478 var a62 = new Int32Array(2) | 415 var a62 = MakeSharedTypedArray(Int32Array, 2) |
479 a62.set(a61) | 416 a62.set(a61) |
480 assertArrayPrefix([1, 12], a62) | 417 assertArrayPrefix([1, 12], a62) |
481 a61.set(a62) | 418 a61.set(a62) |
482 assertArrayPrefix([1, 12], a61) | 419 assertArrayPrefix([1, 12], a61) |
483 | 420 |
484 // Invalid source | 421 // Invalid source |
485 var a = new Uint16Array(50); | 422 var a = MakeSharedTypedArray(Uint16Array, 50); |
486 var expected = []; | 423 var expected = []; |
487 for (i = 0; i < 50; i++) { | 424 for (i = 0; i < 50; i++) { |
488 a[i] = i; | 425 a[i] = i; |
489 expected.push(i); | 426 expected.push(i); |
490 } | 427 } |
491 a.set({}); | 428 a.set({}); |
492 assertArrayPrefix(expected, a); | 429 assertArrayPrefix(expected, a); |
493 assertThrows(function() { a.set.call({}) }, TypeError); | 430 assertThrows(function() { a.set.call({}) }, TypeError); |
494 assertThrows(function() { a.set.call([]) }, TypeError); | 431 assertThrows(function() { a.set.call([]) }, TypeError); |
495 | 432 |
496 assertThrows(function() { a.set(0); }, TypeError); | 433 assertThrows(function() { a.set(0); }, TypeError); |
497 assertThrows(function() { a.set(0, 1); }, TypeError); | 434 assertThrows(function() { a.set(0, 1); }, TypeError); |
498 } | 435 } |
499 | 436 |
500 TestTypedArraySet(); | 437 TestTypedArraySet(); |
501 | 438 |
502 function TestTypedArraysWithIllegalIndices() { | 439 function TestTypedArraysWithIllegalIndices() { |
503 var a = new Int32Array(100); | 440 var a = MakeSharedTypedArray(Int32Array, 100); |
504 | 441 |
505 a[-10] = 10; | 442 a[-10] = 10; |
506 assertEquals(undefined, a[-10]); | 443 assertEquals(undefined, a[-10]); |
507 a["-10"] = 10; | 444 a["-10"] = 10; |
508 assertEquals(undefined, a["-10"]); | 445 assertEquals(undefined, a["-10"]); |
509 | 446 |
510 var s = " -10"; | 447 var s = " -10"; |
511 a[s] = 10; | 448 a[s] = 10; |
512 assertEquals(10, a[s]); | 449 assertEquals(10, a[s]); |
513 var s1 = " -10 "; | 450 var s1 = " -10 "; |
(...skipping 28 matching lines...) Expand all Loading... |
542 a[x] = 5; | 479 a[x] = 5; |
543 a[y] = 27; | 480 a[y] = 27; |
544 assertEquals(27, a[x]); | 481 assertEquals(27, a[x]); |
545 assertEquals(27, a[y]); | 482 assertEquals(27, a[y]); |
546 } | 483 } |
547 | 484 |
548 TestTypedArraysWithIllegalIndices(); | 485 TestTypedArraysWithIllegalIndices(); |
549 | 486 |
550 function TestTypedArraysWithIllegalIndicesStrict() { | 487 function TestTypedArraysWithIllegalIndicesStrict() { |
551 'use strict'; | 488 'use strict'; |
552 var a = new Int32Array(100); | 489 var a = MakeSharedTypedArray(Int32Array, 100); |
553 | 490 |
554 a[-10] = 10; | 491 a[-10] = 10; |
555 assertEquals(undefined, a[-10]); | 492 assertEquals(undefined, a[-10]); |
556 a["-10"] = 10; | 493 a["-10"] = 10; |
557 assertEquals(undefined, a["-10"]); | 494 assertEquals(undefined, a["-10"]); |
558 | 495 |
559 var s = " -10"; | 496 var s = " -10"; |
560 a[s] = 10; | 497 a[s] = 10; |
561 assertEquals(10, a[s]); | 498 assertEquals(10, a[s]); |
562 var s1 = " -10 "; | 499 var s1 = " -10 "; |
(...skipping 26 matching lines...) Expand all Loading... |
589 assertEquals(Infinity, 1/x); | 526 assertEquals(Infinity, 1/x); |
590 assertEquals(-Infinity, 1/y); | 527 assertEquals(-Infinity, 1/y); |
591 a[x] = 5; | 528 a[x] = 5; |
592 a[y] = 27; | 529 a[y] = 27; |
593 assertEquals(27, a[x]); | 530 assertEquals(27, a[x]); |
594 assertEquals(27, a[y]); | 531 assertEquals(27, a[y]); |
595 } | 532 } |
596 | 533 |
597 TestTypedArraysWithIllegalIndicesStrict(); | 534 TestTypedArraysWithIllegalIndicesStrict(); |
598 | 535 |
599 // DataView | |
600 function TestDataViewConstructor() { | |
601 var ab = new ArrayBuffer(256); | |
602 | |
603 var d1 = new DataView(ab, 1, 255); | |
604 assertTrue(ArrayBuffer.isView(d1)); | |
605 assertSame(ab, d1.buffer); | |
606 assertSame(1, d1.byteOffset); | |
607 assertSame(255, d1.byteLength); | |
608 | |
609 var d2 = new DataView(ab, 2); | |
610 assertSame(ab, d2.buffer); | |
611 assertSame(2, d2.byteOffset); | |
612 assertSame(254, d2.byteLength); | |
613 | |
614 var d3 = new DataView(ab); | |
615 assertSame(ab, d3.buffer); | |
616 assertSame(0, d3.byteOffset); | |
617 assertSame(256, d3.byteLength); | |
618 | |
619 var d3a = new DataView(ab, 1, 0); | |
620 assertSame(ab, d3a.buffer); | |
621 assertSame(1, d3a.byteOffset); | |
622 assertSame(0, d3a.byteLength); | |
623 | |
624 var d3b = new DataView(ab, 256, 0); | |
625 assertSame(ab, d3b.buffer); | |
626 assertSame(256, d3b.byteOffset); | |
627 assertSame(0, d3b.byteLength); | |
628 | |
629 var d3c = new DataView(ab, 256); | |
630 assertSame(ab, d3c.buffer); | |
631 assertSame(256, d3c.byteOffset); | |
632 assertSame(0, d3c.byteLength); | |
633 | |
634 var d4 = new DataView(ab, 1, 3.1415926); | |
635 assertSame(ab, d4.buffer); | |
636 assertSame(1, d4.byteOffset); | |
637 assertSame(3, d4.byteLength); | |
638 | |
639 | |
640 // error cases | |
641 assertThrows(function() { new DataView(ab, -1); }, RangeError); | |
642 assertThrows(function() { new DataView(ab, 1, -1); }, RangeError); | |
643 assertThrows(function() { new DataView(); }, TypeError); | |
644 assertThrows(function() { new DataView([]); }, TypeError); | |
645 assertThrows(function() { new DataView(ab, 257); }, RangeError); | |
646 assertThrows(function() { new DataView(ab, 1, 1024); }, RangeError); | |
647 } | |
648 | |
649 TestDataViewConstructor(); | |
650 | |
651 function TestDataViewPropertyTypeChecks() { | |
652 var a = new DataView(new ArrayBuffer(10)); | |
653 function CheckProperty(name) { | |
654 var d = Object.getOwnPropertyDescriptor(DataView.prototype, name); | |
655 var o = {} | |
656 assertThrows(function() {d.get.call(o);}, TypeError); | |
657 d.get.call(a); // shouldn't throw | |
658 } | |
659 | |
660 CheckProperty("buffer"); | |
661 CheckProperty("byteOffset"); | |
662 CheckProperty("byteLength"); | |
663 } | |
664 | |
665 | |
666 TestDataViewPropertyTypeChecks(); | |
667 | |
668 | |
669 function TestDataViewToStringTag() { | |
670 var a = new DataView(new ArrayBuffer(10)); | |
671 assertEquals("[object DataView]", Object.prototype.toString.call(a)); | |
672 var desc = Object.getOwnPropertyDescriptor( | |
673 DataView.prototype, Symbol.toStringTag); | |
674 assertTrue(desc.configurable); | |
675 assertFalse(desc.enumerable); | |
676 assertFalse(desc.writable); | |
677 assertEquals("DataView", desc.value); | |
678 } | |
679 | |
680 | |
681 // General tests for properties | 536 // General tests for properties |
682 | 537 |
683 // Test property attribute [[Enumerable]] | 538 // Test property attribute [[Enumerable]] |
684 function TestEnumerable(func, obj) { | 539 function TestEnumerable(func, obj) { |
685 function props(x) { | 540 function props(x) { |
686 var array = []; | 541 var array = []; |
687 for (var p in x) array.push(p); | 542 for (var p in x) array.push(p); |
688 return array.sort(); | 543 return array.sort(); |
689 } | 544 } |
690 assertArrayEquals([], props(func)); | 545 assertArrayEquals([], props(func)); |
691 assertArrayEquals([], props(func.prototype)); | 546 assertArrayEquals([], props(func.prototype)); |
692 if (obj) | 547 if (obj) |
693 assertArrayEquals([], props(obj)); | 548 assertArrayEquals([], props(obj)); |
694 } | 549 } |
695 TestEnumerable(ArrayBuffer, new ArrayBuffer()); | 550 TestEnumerable(ArrayBuffer, new SharedArrayBuffer()); |
696 for(i = 0; i < typedArrayConstructors.length; i++) { | 551 for(i = 0; i < typedArrayConstructors.length; i++) { |
697 TestEnumerable(typedArrayConstructors[i]); | 552 TestEnumerable(typedArrayConstructors[i]); |
698 } | 553 } |
699 TestEnumerable(DataView, new DataView(new ArrayBuffer())); | |
700 | 554 |
701 // Test arbitrary properties on ArrayBuffer | 555 // Test arbitrary properties on ArrayBuffer |
702 function TestArbitrary(m) { | 556 function TestArbitrary(m) { |
703 function TestProperty(map, property, value) { | 557 function TestProperty(map, property, value) { |
704 map[property] = value; | 558 map[property] = value; |
705 assertEquals(value, map[property]); | 559 assertEquals(value, map[property]); |
706 } | 560 } |
707 for (var i = 0; i < 20; i++) { | 561 for (var i = 0; i < 20; i++) { |
708 TestProperty(m, 'key' + i, 'val' + i); | 562 TestProperty(m, 'key' + i, 'val' + i); |
709 TestProperty(m, 'foo' + i, 'bar' + i); | 563 TestProperty(m, 'foo' + i, 'bar' + i); |
710 } | 564 } |
711 } | 565 } |
712 TestArbitrary(new ArrayBuffer(256)); | 566 TestArbitrary(new SharedArrayBuffer(256)); |
713 for(i = 0; i < typedArrayConstructors.length; i++) { | 567 for(i = 0; i < typedArrayConstructors.length; i++) { |
714 TestArbitrary(new typedArrayConstructors[i](10)); | 568 TestArbitrary(MakeSharedTypedArray(typedArrayConstructors[i], 10)); |
715 } | 569 } |
716 TestArbitrary(new DataView(new ArrayBuffer(256))); | |
717 | |
718 | 570 |
719 // Test direct constructor call | 571 // Test direct constructor call |
720 assertThrows(function() { ArrayBuffer(); }, TypeError); | 572 assertThrows(function() { SharedArrayBuffer(); }, TypeError); |
721 assertThrows(function() { DataView(new ArrayBuffer()); }, TypeError); | 573 for(i = 0; i < typedArrayConstructors.length; i++) { |
| 574 assertThrows(function(i) { typedArrayConstructors[i](); }.bind(this, i), |
| 575 TypeError); |
| 576 } |
OLD | NEW |