Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | var typedArrayConstructors = [ |
| 6 | Uint8Array, |
| 7 | Int8Array, |
| 8 | Uint16Array, |
| 9 | Int16Array, |
| 10 | Uint32Array, |
| 11 | Int32Array, |
| 12 | Uint8ClampedArray, |
| 13 | Float32Array, |
| 14 | Float64Array |
| 15 | ]; |
| 16 | |
| 17 | var lengthCalled = false; |
| 18 | function lengthValue() { |
| 19 | assertFalse(lengthCalled); |
| 20 | lengthCalled = true; |
| 21 | return 5; |
| 22 | } |
| 23 | |
| 24 | // ToLength should convert these to usable lengths. |
| 25 | var goodNonIntegerLengths = [ |
| 26 | function() { return 4.6; }, |
| 27 | function() { return -5; }, |
| 28 | function() { return NaN; }, |
| 29 | function() { return "5"; }, |
| 30 | function() { return "abc"; }, |
| 31 | function() { return true; }, |
| 32 | function() { return null; }, |
| 33 | function() { return undefined; } |
| 34 | ]; |
| 35 | |
| 36 | // This will fail if you use ToLength on it. |
| 37 | function badNonIntegerLength() { |
| 38 | return Symbol("5"); |
| 39 | } |
| 40 | |
| 41 | for (var constructor of typedArrayConstructors) { |
| 42 | lengthCalled = false; |
| 43 | var a = new constructor(10); |
| 44 | a.set({length: {valueOf: lengthValue}}); |
| 45 | assertTrue(lengthCalled); |
| 46 | |
| 47 | for (var lengthFun of goodNonIntegerLengths) { |
| 48 | a.set({length: {valueOf: lengthFun}}); |
| 49 | } |
| 50 | |
| 51 | assertThrows(function() { |
| 52 | a.set({length: {valueOf: badNonIntegerLength}}); |
| 53 | }, TypeError); |
| 54 | } |