OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Based on Mozilla Array.of() tests at http://dxr.mozilla.org/mozilla-central/s
ource/js/src/jit-test/tests/collections | 5 // Based on Mozilla Array.of() tests at http://dxr.mozilla.org/mozilla-central/s
ource/js/src/jit-test/tests/collections |
6 | 6 |
7 // Flags: --harmony-arrays | 7 // Flags: --harmony-arrays |
8 | 8 |
9 var typedArrayConstructors = [ | 9 var typedArrayConstructors = [ |
10 Uint8Array, | 10 Uint8Array, |
11 Int8Array, | 11 Int8Array, |
12 Uint16Array, | 12 Uint16Array, |
13 Int16Array, | 13 Int16Array, |
14 Uint32Array, | 14 Uint32Array, |
15 Int32Array, | 15 Int32Array, |
16 Uint8ClampedArray, | 16 Uint8ClampedArray, |
17 Float32Array, | 17 Float32Array, |
18 Float64Array]; | 18 Float64Array |
| 19 ]; |
19 | 20 |
| 21 function GetPropertyDescriptor(object, field, expectedDepth) { |
| 22 for (var depth = 0; depth < expectedDepth; depth++) { |
| 23 assertFalse(Object.hasOwnProperty(object, field)); |
| 24 object = object.__proto__; |
| 25 } |
| 26 return Object.getOwnPropertyDescriptor(object, field); |
| 27 } |
20 | 28 |
21 function TestTypedArrayOf(constructor) { | 29 function TestTypedArrayOf(constructor) { |
22 // %TypedArray%.of basics. | 30 // %TypedArray%.of basics. |
23 var a = constructor.of(); | 31 var a = constructor.of(); |
24 assertEquals(0, a.length); | 32 assertEquals(0, a.length); |
25 assertEquals(constructor.prototype, Object.getPrototypeOf(a)); | 33 assertEquals(constructor.prototype, Object.getPrototypeOf(a)); |
26 assertEquals(false, Array.isArray(a)); | 34 assertEquals(false, Array.isArray(a)); |
27 | 35 |
28 // Items are coerced to numerical values. | 36 // Items are coerced to numerical values. |
29 a = constructor.of(undefined, null, [], true, false, 3.14); | 37 a = constructor.of(undefined, null, [], true, false, 3.14); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 // when the setter is on the new object's prototype | 112 // when the setter is on the new object's prototype |
105 function Bevy() {} | 113 function Bevy() {} |
106 Object.defineProperty(Bevy.prototype, "length", { | 114 Object.defineProperty(Bevy.prototype, "length", { |
107 set: function (v) { status = "fail"; } | 115 set: function (v) { status = "fail"; } |
108 }); | 116 }); |
109 Bevy.of = constructor.of; | 117 Bevy.of = constructor.of; |
110 var bevy = Bevy.of("quail"); | 118 var bevy = Bevy.of("quail"); |
111 assertEquals("pass", status); | 119 assertEquals("pass", status); |
112 | 120 |
113 // Check superficial features of %TypedArray%.of. | 121 // Check superficial features of %TypedArray%.of. |
114 var desc = Object.getOwnPropertyDescriptor(constructor, "of"); | 122 var desc = GetPropertyDescriptor(constructor, "of", 1); |
115 | 123 |
116 assertEquals(desc.configurable, false); | 124 assertEquals(desc.configurable, false); |
117 assertEquals(desc.enumerable, false); | 125 assertEquals(desc.enumerable, false); |
118 assertEquals(desc.writable, false); | 126 assertEquals(desc.writable, false); |
119 assertEquals(constructor.of.length, 0); | 127 assertEquals(constructor.of.length, 0); |
120 | 128 |
121 // %TypedArray%.of is not a constructor. | 129 // %TypedArray%.of is not a constructor. |
122 assertThrows(function() { new constructor.of(); }, TypeError); | 130 assertThrows(function() { new constructor.of(); }, TypeError); |
123 | 131 |
124 // For receivers which are not constructors %TypedArray%.of does not | 132 // For receivers which are not constructors %TypedArray%.of does not |
125 // allocate a typed array using a default constructor, but throws an | 133 // allocate a typed array using a default constructor, but throws an |
126 // exception. Note that this is different from Array.of, which uses | 134 // exception. Note that this is different from Array.of, which uses |
127 // Array as default constructor. | 135 // Array as default constructor. |
128 for (var x of [undefined, null, false, true, "cow", 42, 3.14]) { | 136 for (var x of [undefined, null, false, true, "cow", 42, 3.14]) { |
129 assertThrows(function () { constructor.of.call(x); }, TypeError); | 137 assertThrows(function () { constructor.of.call(x); }, TypeError); |
130 } | 138 } |
131 } | 139 } |
132 | 140 |
133 for (var constructor of typedArrayConstructors) { | 141 for (var constructor of typedArrayConstructors) { |
134 TestTypedArrayOf(constructor); | 142 TestTypedArrayOf(constructor); |
135 } | 143 } |
OLD | NEW |