OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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: --allow-natives-syntax --expose-gc | 28 // Flags: --allow-natives-syntax --expose-gc |
29 | 29 |
| 30 // Helper |
| 31 function assertInstance(o, f) { |
| 32 assertSame(o.constructor, f); |
| 33 assertInstanceof(o, f); |
| 34 } |
| 35 |
30 // This is a regression test for overlapping key and value registers. | 36 // This is a regression test for overlapping key and value registers. |
31 function f(a) { | 37 function f(a) { |
32 a[0] = 0; | 38 a[0] = 0; |
33 a[1] = 0; | 39 a[1] = 0; |
34 } | 40 } |
35 | 41 |
36 var a = new Int32Array(2); | 42 var a = new Int32Array(2); |
37 for (var i = 0; i < 5; i++) { | 43 for (var i = 0; i < 5; i++) { |
38 f(a); | 44 f(a); |
39 } | 45 } |
40 %OptimizeFunctionOnNextCall(f); | 46 %OptimizeFunctionOnNextCall(f); |
41 f(a); | 47 f(a); |
42 | 48 |
43 assertEquals(0, a[0]); | 49 assertEquals(0, a[0]); |
44 assertEquals(0, a[1]); | 50 assertEquals(0, a[1]); |
45 | 51 |
46 // No-parameter constructor should fail right now. | 52 // No-parameter constructor should fail right now. |
47 function abfunc1() { | 53 function abfunc1() { |
48 return new ArrayBuffer(); | 54 return new ArrayBuffer(); |
49 } | 55 } |
50 assertThrows(abfunc1); | 56 assertThrows(abfunc1); |
51 | 57 |
52 // Test derivation from an ArrayBuffer | 58 // Test derivation from an ArrayBuffer |
53 var ab = new ArrayBuffer(12); | 59 var ab = new ArrayBuffer(12); |
| 60 assertInstance(ab, ArrayBuffer); |
54 var derived_uint8 = new Uint8Array(ab); | 61 var derived_uint8 = new Uint8Array(ab); |
| 62 assertInstance(derived_uint8, Uint8Array); |
55 assertSame(ab, derived_uint8.buffer); | 63 assertSame(ab, derived_uint8.buffer); |
56 assertEquals(12, derived_uint8.length); | 64 assertEquals(12, derived_uint8.length); |
57 assertEquals(12, derived_uint8.byteLength); | 65 assertEquals(12, derived_uint8.byteLength); |
58 assertEquals(0, derived_uint8.byteOffset); | 66 assertEquals(0, derived_uint8.byteOffset); |
59 assertEquals(1, derived_uint8.BYTES_PER_ELEMENT); | 67 assertEquals(1, derived_uint8.BYTES_PER_ELEMENT); |
60 var derived_uint8_2 = new Uint8Array(ab,7); | 68 var derived_uint8_2 = new Uint8Array(ab,7); |
| 69 assertInstance(derived_uint8_2, Uint8Array); |
61 assertSame(ab, derived_uint8_2.buffer); | 70 assertSame(ab, derived_uint8_2.buffer); |
62 assertEquals(5, derived_uint8_2.length); | 71 assertEquals(5, derived_uint8_2.length); |
63 assertEquals(5, derived_uint8_2.byteLength); | 72 assertEquals(5, derived_uint8_2.byteLength); |
64 assertEquals(7, derived_uint8_2.byteOffset); | 73 assertEquals(7, derived_uint8_2.byteOffset); |
65 assertEquals(1, derived_uint8_2.BYTES_PER_ELEMENT); | 74 assertEquals(1, derived_uint8_2.BYTES_PER_ELEMENT); |
66 var derived_int16 = new Int16Array(ab); | 75 var derived_int16 = new Int16Array(ab); |
| 76 assertInstance(derived_int16, Int16Array); |
67 assertSame(ab, derived_int16.buffer); | 77 assertSame(ab, derived_int16.buffer); |
68 assertEquals(6, derived_int16.length); | 78 assertEquals(6, derived_int16.length); |
69 assertEquals(12, derived_int16.byteLength); | 79 assertEquals(12, derived_int16.byteLength); |
70 assertEquals(0, derived_int16.byteOffset); | 80 assertEquals(0, derived_int16.byteOffset); |
71 assertEquals(2, derived_int16.BYTES_PER_ELEMENT); | 81 assertEquals(2, derived_int16.BYTES_PER_ELEMENT); |
72 var derived_int16_2 = new Int16Array(ab,6); | 82 var derived_int16_2 = new Int16Array(ab,6); |
| 83 assertInstance(derived_int16_2, Int16Array); |
73 assertSame(ab, derived_int16_2.buffer); | 84 assertSame(ab, derived_int16_2.buffer); |
74 assertEquals(3, derived_int16_2.length); | 85 assertEquals(3, derived_int16_2.length); |
75 assertEquals(6, derived_int16_2.byteLength); | 86 assertEquals(6, derived_int16_2.byteLength); |
76 assertEquals(6, derived_int16_2.byteOffset); | 87 assertEquals(6, derived_int16_2.byteOffset); |
77 assertEquals(2, derived_int16_2.BYTES_PER_ELEMENT); | 88 assertEquals(2, derived_int16_2.BYTES_PER_ELEMENT); |
78 var derived_uint32 = new Uint32Array(ab); | 89 var derived_uint32 = new Uint32Array(ab); |
| 90 assertInstance(derived_uint32, Uint32Array); |
79 assertSame(ab, derived_uint32.buffer); | 91 assertSame(ab, derived_uint32.buffer); |
80 assertEquals(3, derived_uint32.length); | 92 assertEquals(3, derived_uint32.length); |
81 assertEquals(12, derived_uint32.byteLength); | 93 assertEquals(12, derived_uint32.byteLength); |
82 assertEquals(0, derived_uint32.byteOffset); | 94 assertEquals(0, derived_uint32.byteOffset); |
83 assertEquals(4, derived_uint32.BYTES_PER_ELEMENT); | 95 assertEquals(4, derived_uint32.BYTES_PER_ELEMENT); |
84 var derived_uint32_2 = new Uint32Array(ab,4); | 96 var derived_uint32_2 = new Uint32Array(ab,4); |
| 97 assertInstance(derived_uint32_2, Uint32Array); |
85 assertSame(ab, derived_uint32_2.buffer); | 98 assertSame(ab, derived_uint32_2.buffer); |
86 assertEquals(2, derived_uint32_2.length); | 99 assertEquals(2, derived_uint32_2.length); |
87 assertEquals(8, derived_uint32_2.byteLength); | 100 assertEquals(8, derived_uint32_2.byteLength); |
88 assertEquals(4, derived_uint32_2.byteOffset); | 101 assertEquals(4, derived_uint32_2.byteOffset); |
89 assertEquals(4, derived_uint32_2.BYTES_PER_ELEMENT); | 102 assertEquals(4, derived_uint32_2.BYTES_PER_ELEMENT); |
90 var derived_uint32_3 = new Uint32Array(ab,4,1); | 103 var derived_uint32_3 = new Uint32Array(ab,4,1); |
| 104 assertInstance(derived_uint32_3, Uint32Array); |
91 assertSame(ab, derived_uint32_3.buffer); | 105 assertSame(ab, derived_uint32_3.buffer); |
92 assertEquals(1, derived_uint32_3.length); | 106 assertEquals(1, derived_uint32_3.length); |
93 assertEquals(4, derived_uint32_3.byteLength); | 107 assertEquals(4, derived_uint32_3.byteLength); |
94 assertEquals(4, derived_uint32_3.byteOffset); | 108 assertEquals(4, derived_uint32_3.byteOffset); |
95 assertEquals(4, derived_uint32_3.BYTES_PER_ELEMENT); | 109 assertEquals(4, derived_uint32_3.BYTES_PER_ELEMENT); |
96 var derived_float64 = new Float64Array(ab,0,1); | 110 var derived_float64 = new Float64Array(ab,0,1); |
| 111 assertInstance(derived_float64, Float64Array); |
97 assertSame(ab, derived_float64.buffer); | 112 assertSame(ab, derived_float64.buffer); |
98 assertEquals(1, derived_float64.length); | 113 assertEquals(1, derived_float64.length); |
99 assertEquals(8, derived_float64.byteLength); | 114 assertEquals(8, derived_float64.byteLength); |
100 assertEquals(0, derived_float64.byteOffset); | 115 assertEquals(0, derived_float64.byteOffset); |
101 assertEquals(8, derived_float64.BYTES_PER_ELEMENT); | 116 assertEquals(8, derived_float64.BYTES_PER_ELEMENT); |
102 | 117 |
103 // If a given byteOffset and length references an area beyond the end of the | 118 // If a given byteOffset and length references an area beyond the end of the |
104 // ArrayBuffer an exception is raised. | 119 // ArrayBuffer an exception is raised. |
105 function abfunc3() { | 120 function abfunc3() { |
106 new Uint32Array(ab,4,3); | 121 new Uint32Array(ab,4,3); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 var foo = { valueOf: function() { return 3; } }; | 206 var foo = { valueOf: function() { return 3; } }; |
192 array_with_length_from_non_number = new Int32Array(foo); | 207 array_with_length_from_non_number = new Int32Array(foo); |
193 assertEquals(3, array_with_length_from_non_number.length); | 208 assertEquals(3, array_with_length_from_non_number.length); |
194 foo = { toString: function() { return "4"; } }; | 209 foo = { toString: function() { return "4"; } }; |
195 array_with_length_from_non_number = new Int32Array(foo); | 210 array_with_length_from_non_number = new Int32Array(foo); |
196 assertEquals(4, array_with_length_from_non_number.length); | 211 assertEquals(4, array_with_length_from_non_number.length); |
197 | 212 |
198 | 213 |
199 // Test loads and stores. | 214 // Test loads and stores. |
200 types = [Array, Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, | 215 types = [Array, Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, |
201 Uint32Array, PixelArray, Float32Array, Float64Array]; | 216 Uint32Array, Uint8ClampedArray, Float32Array, Float64Array]; |
202 | 217 |
203 test_result_nan = [NaN, 0, 0, 0, 0, 0, 0, 0, NaN, NaN]; | 218 test_result_nan = [NaN, 0, 0, 0, 0, 0, 0, 0, NaN, NaN]; |
204 test_result_low_int = [-1, -1, 255, -1, 65535, -1, 0xFFFFFFFF, 0, -1, -1]; | 219 test_result_low_int = [-1, -1, 255, -1, 65535, -1, 0xFFFFFFFF, 0, -1, -1]; |
205 test_result_low_double = [-1.25, -1, 255, -1, 65535, -1, 0xFFFFFFFF, 0, -1.25, -
1.25]; | 220 test_result_low_double = [-1.25, -1, 255, -1, 65535, -1, 0xFFFFFFFF, 0, -1.25, -
1.25]; |
206 test_result_middle = [253.75, -3, 253, 253, 253, 253, 253, 254, 253.75, 253.75]; | 221 test_result_middle = [253.75, -3, 253, 253, 253, 253, 253, 254, 253.75, 253.75]; |
207 test_result_high_int = [256, 0, 0, 256, 256, 256, 256, 255, 256, 256]; | 222 test_result_high_int = [256, 0, 0, 256, 256, 256, 256, 255, 256, 256]; |
208 test_result_high_double = [256.25, 0, 0, 256, 256, 256, 256, 255, 256.25, 256.25
]; | 223 test_result_high_double = [256.25, 0, 0, 256, 256, 256, 256, 255, 256.25, 256.25
]; |
209 | 224 |
210 const kElementCount = 40; | 225 const kElementCount = 40; |
211 | 226 |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 // Make sure the ICs do it right | 420 // Make sure the ICs do it right |
406 store_float64_undefined(float64_array); | 421 store_float64_undefined(float64_array); |
407 assertTrue(isNaN(float64_array[0])); | 422 assertTrue(isNaN(float64_array[0])); |
408 // Make sure that Cranskshft does it right. | 423 // Make sure that Cranskshft does it right. |
409 %OptimizeFunctionOnNextCall(store_float64_undefined); | 424 %OptimizeFunctionOnNextCall(store_float64_undefined); |
410 store_float64_undefined(float64_array); | 425 store_float64_undefined(float64_array); |
411 assertTrue(isNaN(float64_array[0])); | 426 assertTrue(isNaN(float64_array[0])); |
412 | 427 |
413 | 428 |
414 // Check handling of 0-sized buffers and arrays. | 429 // Check handling of 0-sized buffers and arrays. |
415 | |
416 ab = new ArrayBuffer(0); | 430 ab = new ArrayBuffer(0); |
| 431 assertInstance(ab, ArrayBuffer); |
417 assertEquals(0, ab.byteLength); | 432 assertEquals(0, ab.byteLength); |
418 a = new Int8Array(ab); | 433 a = new Int8Array(ab); |
| 434 assertInstance(a, Int8Array); |
419 assertEquals(0, a.byteLength); | 435 assertEquals(0, a.byteLength); |
420 assertEquals(0, a.length); | 436 assertEquals(0, a.length); |
421 a[0] = 1; | 437 a[0] = 1; |
422 assertEquals(undefined, a[0]) | 438 assertEquals(undefined, a[0]); |
423 ab = new ArrayBuffer(16); | 439 ab = new ArrayBuffer(16); |
| 440 assertInstance(ab, ArrayBuffer); |
424 a = new Float32Array(ab,4,0); | 441 a = new Float32Array(ab,4,0); |
| 442 assertInstance(a, Float32Array); |
425 assertEquals(0, a.byteLength); | 443 assertEquals(0, a.byteLength); |
426 assertEquals(0, a.length); | 444 assertEquals(0, a.length); |
427 a[0] = 1; | 445 a[0] = 1; |
428 assertEquals(undefined, a[0]) | 446 assertEquals(undefined, a[0]); |
429 a = new Uint16Array(0); | 447 a = new Uint16Array(0); |
| 448 assertInstance(a, Uint16Array); |
430 assertEquals(0, a.byteLength); | 449 assertEquals(0, a.byteLength); |
431 assertEquals(0, a.length); | 450 assertEquals(0, a.length); |
432 a[0] = 1; | 451 a[0] = 1; |
433 assertEquals(undefined, a[0]) | 452 assertEquals(undefined, a[0]); |
| 453 |
| 454 |
| 455 // Check construction from arrays. |
| 456 a = new Uint32Array([]); |
| 457 assertInstance(a, Uint32Array); |
| 458 assertEquals(0, a.length); |
| 459 assertEquals(0, a.byteLength); |
| 460 assertEquals(0, a.buffer.byteLength); |
| 461 assertEquals(4, a.BYTES_PER_ELEMENT); |
| 462 a = new Uint16Array([1,2,3]); |
| 463 assertInstance(a, Uint16Array); |
| 464 assertEquals(3, a.length); |
| 465 assertEquals(6, a.byteLength); |
| 466 assertEquals(6, a.buffer.byteLength); |
| 467 assertEquals(2, a.BYTES_PER_ELEMENT); |
| 468 assertEquals(1, a[0]); |
| 469 assertEquals(3, a[2]); |
| 470 a = new Uint32Array(a); |
| 471 assertInstance(a, Uint32Array); |
| 472 assertEquals(3, a.length); |
| 473 assertEquals(12, a.byteLength); |
| 474 assertEquals(12, a.buffer.byteLength); |
| 475 assertEquals(4, a.BYTES_PER_ELEMENT); |
| 476 assertEquals(1, a[0]); |
| 477 assertEquals(3, a[2]); |
| 478 |
| 479 |
| 480 // Check subarrays. |
| 481 a = new Uint16Array([1,2,3,4,5,6]); |
| 482 aa = a.subarray(3); |
| 483 assertInstance(aa, Uint16Array); |
| 484 assertEquals(3, aa.length); |
| 485 assertEquals(6, aa.byteLength); |
| 486 assertEquals(2, aa.BYTES_PER_ELEMENT); |
| 487 assertSame(a.buffer, aa.buffer); |
| 488 aa = a.subarray(3,5); |
| 489 assertInstance(aa, Uint16Array); |
| 490 assertEquals(2, aa.length); |
| 491 assertEquals(4, aa.byteLength); |
| 492 assertEquals(2, aa.BYTES_PER_ELEMENT); |
| 493 assertSame(a.buffer, aa.buffer); |
| 494 aa = a.subarray(4,8); |
| 495 assertInstance(aa, Uint16Array); |
| 496 assertEquals(2, aa.length); |
| 497 assertEquals(4, aa.byteLength); |
| 498 assertEquals(2, aa.BYTES_PER_ELEMENT); |
| 499 assertSame(a.buffer, aa.buffer); |
| 500 aa = a.subarray(9); |
| 501 assertInstance(aa, Uint16Array); |
| 502 assertEquals(0, aa.length); |
| 503 assertEquals(0, aa.byteLength); |
| 504 assertEquals(2, aa.BYTES_PER_ELEMENT); |
| 505 assertSame(a.buffer, aa.buffer); |
| 506 aa = a.subarray(-4); |
| 507 assertInstance(aa, Uint16Array); |
| 508 assertEquals(4, aa.length); |
| 509 assertEquals(8, aa.byteLength); |
| 510 assertEquals(2, aa.BYTES_PER_ELEMENT); |
| 511 assertSame(a.buffer, aa.buffer); |
| 512 aa = a.subarray(-3,-1); |
| 513 assertInstance(aa, Uint16Array); |
| 514 assertEquals(2, aa.length); |
| 515 assertEquals(4, aa.byteLength); |
| 516 assertEquals(2, aa.BYTES_PER_ELEMENT); |
| 517 assertSame(a.buffer, aa.buffer); |
| 518 aa = a.subarray(3,2); |
| 519 assertInstance(aa, Uint16Array); |
| 520 assertEquals(0, aa.length); |
| 521 assertEquals(0, aa.byteLength); |
| 522 assertEquals(2, aa.BYTES_PER_ELEMENT); |
| 523 assertSame(a.buffer, aa.buffer); |
| 524 aa = a.subarray(-3,-4); |
| 525 assertInstance(aa, Uint16Array); |
| 526 assertEquals(0, aa.length); |
| 527 assertEquals(0, aa.byteLength); |
| 528 assertEquals(2, aa.BYTES_PER_ELEMENT); |
| 529 assertSame(a.buffer, aa.buffer); |
| 530 aa = a.subarray(0,-8); |
| 531 assertInstance(aa, Uint16Array); |
| 532 assertEquals(0, aa.length); |
| 533 assertEquals(0, aa.byteLength); |
| 534 assertEquals(2, aa.BYTES_PER_ELEMENT); |
| 535 assertSame(a.buffer, aa.buffer); |
| 536 |
| 537 assertThrows(function(){ a.subarray.call({}, 0) }); |
| 538 assertThrows(function(){ a.subarray.call([], 0) }); |
| 539 assertThrows(function(){ a.subarray.call(a) }); |
OLD | NEW |