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 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 | 45 |
46 // No-parameter constructor should fail right now. | 46 // No-parameter constructor should fail right now. |
47 function abfunc1() { | 47 function abfunc1() { |
48 return new ArrayBuffer(); | 48 return new ArrayBuffer(); |
49 } | 49 } |
50 assertThrows(abfunc1); | 50 assertThrows(abfunc1); |
51 | 51 |
52 // Test derivation from an ArrayBuffer | 52 // Test derivation from an ArrayBuffer |
53 var ab = new ArrayBuffer(12); | 53 var ab = new ArrayBuffer(12); |
54 var derived_uint8 = new Uint8Array(ab); | 54 var derived_uint8 = new Uint8Array(ab); |
| 55 assertSame(ab, derived_uint8.buffer); |
55 assertEquals(12, derived_uint8.length); | 56 assertEquals(12, derived_uint8.length); |
| 57 assertEquals(12, derived_uint8.byteLength); |
| 58 assertEquals(0, derived_uint8.byteOffset); |
| 59 assertEquals(1, derived_uint8.BYTES_PER_ELEMENT); |
| 60 var derived_uint8_2 = new Uint8Array(ab,7); |
| 61 assertSame(ab, derived_uint8_2.buffer); |
| 62 assertEquals(5, derived_uint8_2.length); |
| 63 assertEquals(5, derived_uint8_2.byteLength); |
| 64 assertEquals(7, derived_uint8_2.byteOffset); |
| 65 assertEquals(1, derived_uint8_2.BYTES_PER_ELEMENT); |
| 66 var derived_int16 = new Int16Array(ab); |
| 67 assertSame(ab, derived_int16.buffer); |
| 68 assertEquals(6, derived_int16.length); |
| 69 assertEquals(12, derived_int16.byteLength); |
| 70 assertEquals(0, derived_int16.byteOffset); |
| 71 assertEquals(2, derived_int16.BYTES_PER_ELEMENT); |
| 72 var derived_int16_2 = new Int16Array(ab,6); |
| 73 assertSame(ab, derived_int16_2.buffer); |
| 74 assertEquals(3, derived_int16_2.length); |
| 75 assertEquals(6, derived_int16_2.byteLength); |
| 76 assertEquals(6, derived_int16_2.byteOffset); |
| 77 assertEquals(2, derived_int16_2.BYTES_PER_ELEMENT); |
56 var derived_uint32 = new Uint32Array(ab); | 78 var derived_uint32 = new Uint32Array(ab); |
| 79 assertSame(ab, derived_uint32.buffer); |
57 assertEquals(3, derived_uint32.length); | 80 assertEquals(3, derived_uint32.length); |
| 81 assertEquals(12, derived_uint32.byteLength); |
| 82 assertEquals(0, derived_uint32.byteOffset); |
| 83 assertEquals(4, derived_uint32.BYTES_PER_ELEMENT); |
58 var derived_uint32_2 = new Uint32Array(ab,4); | 84 var derived_uint32_2 = new Uint32Array(ab,4); |
| 85 assertSame(ab, derived_uint32_2.buffer); |
59 assertEquals(2, derived_uint32_2.length); | 86 assertEquals(2, derived_uint32_2.length); |
| 87 assertEquals(8, derived_uint32_2.byteLength); |
| 88 assertEquals(4, derived_uint32_2.byteOffset); |
| 89 assertEquals(4, derived_uint32_2.BYTES_PER_ELEMENT); |
60 var derived_uint32_3 = new Uint32Array(ab,4,1); | 90 var derived_uint32_3 = new Uint32Array(ab,4,1); |
| 91 assertSame(ab, derived_uint32_3.buffer); |
61 assertEquals(1, derived_uint32_3.length); | 92 assertEquals(1, derived_uint32_3.length); |
| 93 assertEquals(4, derived_uint32_3.byteLength); |
| 94 assertEquals(4, derived_uint32_3.byteOffset); |
| 95 assertEquals(4, derived_uint32_3.BYTES_PER_ELEMENT); |
| 96 var derived_float64 = new Float64Array(ab,0,1); |
| 97 assertSame(ab, derived_float64.buffer); |
| 98 assertEquals(1, derived_float64.length); |
| 99 assertEquals(8, derived_float64.byteLength); |
| 100 assertEquals(0, derived_float64.byteOffset); |
| 101 assertEquals(8, derived_float64.BYTES_PER_ELEMENT); |
62 | 102 |
63 // If a given byteOffset and length references an area beyond the end of the | 103 // If a given byteOffset and length references an area beyond the end of the |
64 // ArrayBuffer an exception is raised. | 104 // ArrayBuffer an exception is raised. |
65 function abfunc3() { | 105 function abfunc3() { |
66 new Uint32Array(ab,4,3); | 106 new Uint32Array(ab,4,3); |
67 } | 107 } |
68 assertThrows(abfunc3); | 108 assertThrows(abfunc3); |
69 function abfunc4() { | 109 function abfunc4() { |
70 new Uint32Array(ab,16); | 110 new Uint32Array(ab,16); |
71 } | 111 } |
72 assertThrows(abfunc4); | 112 assertThrows(abfunc4); |
73 | 113 |
74 // The given byteOffset must be a multiple of the element size of the specific | 114 // The given byteOffset must be a multiple of the element size of the specific |
75 // type, otherwise an exception is raised. | 115 // type, otherwise an exception is raised. |
76 function abfunc5() { | 116 function abfunc5() { |
77 new Uint32Array(ab,5); | 117 new Uint32Array(ab,5); |
78 } | 118 } |
79 assertThrows(abfunc5); | 119 assertThrows(abfunc5); |
80 | 120 |
81 // If length is not explicitly specified, the length of the ArrayBuffer minus | 121 // If length is not explicitly specified, the length of the ArrayBuffer minus |
82 // the byteOffset must be a multiple of the element size of the specific type, | 122 // the byteOffset must be a multiple of the element size of the specific type, |
83 // or an exception is raised. | 123 // or an exception is raised. |
84 var ab2 = new ArrayBuffer(13); | 124 var ab2 = new ArrayBuffer(13); |
85 function abfunc6() { | 125 function abfunc6() { |
86 new Uint32Array(ab2,4); | 126 new Uint32Array(ab2,4); |
87 } | 127 } |
88 assertThrows(abfunc6); | 128 assertThrows(abfunc6); |
89 | 129 |
| 130 // Test that an array constructed without an array buffer creates one properly. |
| 131 a = new Uint8Array(31); |
| 132 assertEquals(a.byteLength, a.buffer.byteLength); |
| 133 assertEquals(a.length, a.buffer.byteLength); |
| 134 assertEquals(a.length * a.BYTES_PER_ELEMENT, a.buffer.byteLength); |
| 135 a = new Int16Array(5); |
| 136 assertEquals(a.byteLength, a.buffer.byteLength); |
| 137 assertEquals(a.length * a.BYTES_PER_ELEMENT, a.buffer.byteLength); |
| 138 a = new Float64Array(7); |
| 139 assertEquals(a.byteLength, a.buffer.byteLength); |
| 140 assertEquals(a.length * a.BYTES_PER_ELEMENT, a.buffer.byteLength); |
| 141 |
| 142 // Test that an implicitly created buffer is a valid buffer. |
| 143 a = new Float64Array(7); |
| 144 assertSame(a.buffer, (new Uint16Array(a.buffer)).buffer); |
| 145 assertSame(a.buffer, (new Float32Array(a.buffer,4)).buffer); |
| 146 assertSame(a.buffer, (new Int8Array(a.buffer,3,51)).buffer); |
| 147 |
90 // Test the correct behavior of the |BYTES_PER_ELEMENT| property (which is | 148 // Test the correct behavior of the |BYTES_PER_ELEMENT| property (which is |
91 // "constant", but not read-only). | 149 // "constant", but not read-only). |
92 a = new Int32Array(2); | 150 a = new Int32Array(2); |
93 assertEquals(4, a.BYTES_PER_ELEMENT); | 151 assertEquals(4, a.BYTES_PER_ELEMENT); |
94 a.BYTES_PER_ELEMENT = 42; | 152 a.BYTES_PER_ELEMENT = 42; |
95 assertEquals(42, a.BYTES_PER_ELEMENT); | 153 assertEquals(42, a.BYTES_PER_ELEMENT); |
96 a = new Uint8Array(2); | 154 a = new Uint8Array(2); |
97 assertEquals(1, a.BYTES_PER_ELEMENT); | 155 assertEquals(1, a.BYTES_PER_ELEMENT); |
98 a = new Int16Array(2); | 156 a = new Int16Array(2); |
99 assertEquals(2, a.BYTES_PER_ELEMENT); | 157 assertEquals(2, a.BYTES_PER_ELEMENT); |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 // Make sure runtime does it right | 402 // Make sure runtime does it right |
345 store_float64_undefined(float64_array); | 403 store_float64_undefined(float64_array); |
346 assertTrue(isNaN(float64_array[0])); | 404 assertTrue(isNaN(float64_array[0])); |
347 // Make sure the ICs do it right | 405 // Make sure the ICs do it right |
348 store_float64_undefined(float64_array); | 406 store_float64_undefined(float64_array); |
349 assertTrue(isNaN(float64_array[0])); | 407 assertTrue(isNaN(float64_array[0])); |
350 // Make sure that Cranskshft does it right. | 408 // Make sure that Cranskshft does it right. |
351 %OptimizeFunctionOnNextCall(store_float64_undefined); | 409 %OptimizeFunctionOnNextCall(store_float64_undefined); |
352 store_float64_undefined(float64_array); | 410 store_float64_undefined(float64_array); |
353 assertTrue(isNaN(float64_array[0])); | 411 assertTrue(isNaN(float64_array[0])); |
| 412 |
| 413 |
| 414 // Check handling of 0-sized buffers and arrays. |
| 415 |
| 416 ab = new ArrayBuffer(0); |
| 417 assertEquals(0, ab.byteLength); |
| 418 a = new Int8Array(ab); |
| 419 assertEquals(0, a.byteLength); |
| 420 assertEquals(0, a.length); |
| 421 a[0] = 1; |
| 422 assertEquals(undefined, a[0]) |
| 423 ab = new ArrayBuffer(16); |
| 424 a = new Float32Array(ab,4,0); |
| 425 assertEquals(0, a.byteLength); |
| 426 assertEquals(0, a.length); |
| 427 a[0] = 1; |
| 428 assertEquals(undefined, a[0]) |
| 429 a = new Uint16Array(0); |
| 430 assertEquals(0, a.byteLength); |
| 431 assertEquals(0, a.length); |
| 432 a[0] = 1; |
| 433 assertEquals(undefined, a[0]) |
OLD | NEW |