OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 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 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 var a = new constr(ab, 64*elementSize, 128); | 269 var a = new constr(ab, 64*elementSize, 128); |
270 assertEquals("[object " + constr.name + "]", | 270 assertEquals("[object " + constr.name + "]", |
271 Object.prototype.toString.call(a)); | 271 Object.prototype.toString.call(a)); |
272 var desc = Object.getOwnPropertyDescriptor( | 272 var desc = Object.getOwnPropertyDescriptor( |
273 constr.prototype, Symbol.toStringTag); | 273 constr.prototype, Symbol.toStringTag); |
274 assertTrue(desc.configurable); | 274 assertTrue(desc.configurable); |
275 assertFalse(desc.enumerable); | 275 assertFalse(desc.enumerable); |
276 assertFalse(!!desc.writable); | 276 assertFalse(!!desc.writable); |
277 assertFalse(!!desc.set); | 277 assertFalse(!!desc.set); |
278 assertEquals("function", typeof desc.get); | 278 assertEquals("function", typeof desc.get); |
| 279 |
| 280 // Test that the constructor can be called with an iterable |
| 281 function* gen() { for (var i = 0; i < 10; i++) yield i; } |
| 282 var genArr = new constr(gen()); |
| 283 assertEquals(10, genArr.length); |
| 284 assertEquals(0, genArr[0]); |
| 285 assertEquals(9, genArr[9]); |
| 286 // Arrays can be converted to TypedArrays |
| 287 genArr = new constr([1, 2, 3]); |
| 288 assertEquals(3, genArr.length); |
| 289 assertEquals(1, genArr[0]); |
| 290 assertEquals(3, genArr[2]); |
| 291 // Redefining Array.prototype[Symbol.iterator] still works |
| 292 var arrayIterator = Array.prototype[Symbol.iterator]; |
| 293 Array.prototype[Symbol.iterator] = gen; |
| 294 genArr = new constr([1, 2, 3]); |
| 295 assertEquals(10, genArr.length); |
| 296 assertEquals(0, genArr[0]); |
| 297 assertEquals(9, genArr[9]); |
| 298 Array.prototype[Symbol.iterator] = arrayIterator; |
| 299 // Other array-like things can be made into a TypedArray |
| 300 var myObject = { 0: 5, 1: 6, length: 2 }; |
| 301 genArr = new constr(myObject); |
| 302 assertEquals(2, genArr.length); |
| 303 assertEquals(5, genArr[0]); |
| 304 assertEquals(6, genArr[1]); |
| 305 // Iterator takes precedence over array-like |
| 306 myObject[Symbol.iterator] = gen; |
| 307 genArr = new constr(myObject); |
| 308 assertEquals(10, genArr.length); |
| 309 assertEquals(0, genArr[0]); |
| 310 assertEquals(9, genArr[9]); |
279 } | 311 } |
280 | 312 |
281 TestTypedArray(Uint8Array, 1, 0xFF); | 313 TestTypedArray(Uint8Array, 1, 0xFF); |
282 TestTypedArray(Int8Array, 1, -0x7F); | 314 TestTypedArray(Int8Array, 1, -0x7F); |
283 TestTypedArray(Uint16Array, 2, 0xFFFF); | 315 TestTypedArray(Uint16Array, 2, 0xFFFF); |
284 TestTypedArray(Int16Array, 2, -0x7FFF); | 316 TestTypedArray(Int16Array, 2, -0x7FFF); |
285 TestTypedArray(Uint32Array, 4, 0xFFFFFFFF); | 317 TestTypedArray(Uint32Array, 4, 0xFFFFFFFF); |
286 TestTypedArray(Int32Array, 4, -0x7FFFFFFF); | 318 TestTypedArray(Int32Array, 4, -0x7FFFFFFF); |
287 TestTypedArray(Float32Array, 4, 0.5); | 319 TestTypedArray(Float32Array, 4, 0.5); |
288 TestTypedArray(Float64Array, 8, 0.5); | 320 TestTypedArray(Float64Array, 8, 0.5); |
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
712 TestArbitrary(new ArrayBuffer(256)); | 744 TestArbitrary(new ArrayBuffer(256)); |
713 for(i = 0; i < typedArrayConstructors.length; i++) { | 745 for(i = 0; i < typedArrayConstructors.length; i++) { |
714 TestArbitrary(new typedArrayConstructors[i](10)); | 746 TestArbitrary(new typedArrayConstructors[i](10)); |
715 } | 747 } |
716 TestArbitrary(new DataView(new ArrayBuffer(256))); | 748 TestArbitrary(new DataView(new ArrayBuffer(256))); |
717 | 749 |
718 | 750 |
719 // Test direct constructor call | 751 // Test direct constructor call |
720 assertThrows(function() { ArrayBuffer(); }, TypeError); | 752 assertThrows(function() { ArrayBuffer(); }, TypeError); |
721 assertThrows(function() { DataView(new ArrayBuffer()); }, TypeError); | 753 assertThrows(function() { DataView(new ArrayBuffer()); }, TypeError); |
OLD | NEW |