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 'use strict'; |
| 8 |
7 var typedArrayConstructors = [ | 9 var typedArrayConstructors = [ |
8 Uint8Array, | 10 Uint8Array, |
9 Int8Array, | 11 Int8Array, |
10 Uint16Array, | 12 Uint16Array, |
11 Int16Array, | 13 Int16Array, |
12 Uint32Array, | 14 Uint32Array, |
13 Int32Array, | 15 Int32Array, |
14 Uint8ClampedArray, | 16 Uint8ClampedArray, |
15 Float32Array, | 17 Float32Array, |
16 Float64Array]; | 18 Float64Array]; |
(...skipping 27 matching lines...) Expand all Loading... |
44 } | 46 } |
45 | 47 |
46 var aux = []; | 48 var aux = []; |
47 for (var i = 0; i < 100; i++) | 49 for (var i = 0; i < 100; i++) |
48 aux[i] = i; | 50 aux[i] = i; |
49 | 51 |
50 a = constructor.of.apply(constructor, aux); | 52 a = constructor.of.apply(constructor, aux); |
51 assertEquals(aux.length, a.length); | 53 assertEquals(aux.length, a.length); |
52 assertArrayEquals(aux, a); | 54 assertArrayEquals(aux, a); |
53 | 55 |
54 // %TypedArray%.of can be transplanted to other constructors. | 56 // %TypedArray%.of can be called on subclasses of TypedArrays |
55 var hits = 0; | 57 var hits = 0; |
56 function Bag(length) { | 58 class Bag extends constructor { |
57 assertEquals(arguments.length, 1); | 59 constructor(length) { |
58 assertEquals(length, 2); | 60 super(length); |
59 this.length = length; | 61 assertEquals(arguments.length, 1); |
60 hits++; | 62 assertEquals(length, 2); |
| 63 hits++; |
| 64 } |
61 } | 65 } |
62 Bag.of = constructor.of; | |
63 | 66 |
64 hits = 0; | 67 hits = 0; |
65 a = Bag.of("zero", "one"); | 68 a = Bag.of(5, 6); |
66 assertEquals(1, hits); | 69 assertEquals(1, hits); |
67 assertEquals(2, a.length); | 70 assertEquals(2, a.length); |
68 assertArrayEquals(["zero", "one"], a); | 71 assertArrayEquals([5, 6], a); |
69 assertEquals(Bag.prototype, a.__proto__); | 72 assertEquals(Bag.prototype, a.__proto__); |
70 | 73 |
71 hits = 0; | 74 hits = 0; |
72 actual = constructor.of.call(Bag, "zero", "one"); | 75 var actual = constructor.of.call(Bag, 5, 6); |
73 assertEquals(1, hits); | 76 assertEquals(1, hits); |
74 assertEquals(2, a.length); | 77 assertEquals(2, a.length); |
75 assertArrayEquals(["zero", "one"], a); | 78 assertArrayEquals([5, 6], a); |
76 assertEquals(Bag.prototype, a.__proto__); | 79 assertEquals(Bag.prototype, a.__proto__); |
77 | 80 |
78 // %TypedArray%.of does not trigger prototype setters. | 81 // %TypedArray%.of does not trigger prototype setters. |
79 // (It defines elements rather than assigning to them.) | 82 // (It defines elements rather than assigning to them.) |
80 var status = "pass"; | 83 var status = "pass"; |
81 Object.defineProperty(constructor.prototype, "0", { | 84 Object.defineProperty(constructor.prototype, "0", { |
82 set: function(v) { status = "fail"; } | 85 set: function(v) { status = "fail"; } |
83 }); | 86 }); |
84 assertEquals(1, constructor.of(1)[0], 1); | 87 assertEquals(1, constructor.of(1)[0], 1); |
85 assertEquals("pass", status); | 88 assertEquals("pass", status); |
86 | 89 |
87 // Note that %TypedArray%.of does not trigger "length" setter itself, as | 90 // Note that %TypedArray%.of does not trigger "length" setter itself, as |
88 // it relies on the constructor to set "length" to the value passed to it. | 91 // it relies on the constructor to set "length" to the value passed to it. |
89 // If the constructor does not assign "length", the setter should not be | 92 // If the constructor does not assign "length", the setter should not be |
90 // invoked. | 93 // invoked. |
91 | 94 |
92 // Setter on the newly created object. | 95 // Setter on the newly created object. |
93 function Pack() { | 96 class Pack extends constructor { |
94 Object.defineProperty(this, "length", { | 97 constructor(length) { |
95 set: function (v) { status = "fail"; } | 98 super(length); |
96 }); | 99 Object.defineProperty(this, "length", { |
| 100 set: function (v) { status = "fail"; } |
| 101 }); |
| 102 } |
97 } | 103 } |
98 Pack.of = constructor.of; | 104 var pack = Pack.of(5, 6, 7, 8); |
99 var pack = Pack.of("wolves", "cards", "cigarettes", "lies"); | |
100 assertEquals("pass", status); | 105 assertEquals("pass", status); |
101 | 106 |
102 // when the setter is on the new object's prototype | 107 // when the setter is on the new object's prototype |
103 function Bevy() {} | 108 class Bevy extends constructor {} |
104 Object.defineProperty(Bevy.prototype, "length", { | 109 Object.defineProperty(Bevy.prototype, "length", { |
105 set: function (v) { status = "fail"; } | 110 set: function (v) { status = "fail"; } |
106 }); | 111 }); |
107 Bevy.of = constructor.of; | 112 var bevy = Bevy.of(3); |
108 var bevy = Bevy.of("quail"); | |
109 assertEquals("pass", status); | 113 assertEquals("pass", status); |
110 | 114 |
111 // Check superficial features of %TypedArray%.of. | 115 // Check superficial features of %TypedArray%.of. |
112 var desc = Object.getOwnPropertyDescriptor(constructor.__proto__, "of"); | 116 var desc = Object.getOwnPropertyDescriptor(constructor.__proto__, "of"); |
113 | 117 |
114 assertEquals(desc.configurable, false); | 118 assertEquals(desc.configurable, false); |
115 assertEquals(desc.enumerable, false); | 119 assertEquals(desc.enumerable, false); |
116 assertEquals(desc.writable, false); | 120 assertEquals(desc.writable, false); |
117 assertEquals(constructor.of.length, 0); | 121 assertEquals(constructor.of.length, 0); |
118 | 122 |
119 // %TypedArray%.of is not a constructor. | 123 // %TypedArray%.of is not a constructor. |
120 assertThrows(function() { new constructor.of(); }, TypeError); | 124 assertThrows(function() { new constructor.of(); }, TypeError); |
121 | 125 |
122 // For receivers which are not constructors %TypedArray%.of does not | 126 // For receivers which are not constructors %TypedArray%.of does not |
123 // allocate a typed array using a default constructor, but throws an | 127 // allocate a typed array using a default constructor, but throws an |
124 // exception. Note that this is different from Array.of, which uses | 128 // exception. Note that this is different from Array.of, which uses |
125 // Array as default constructor. | 129 // Array as default constructor. |
126 for (var x of [undefined, null, false, true, "cow", 42, 3.14]) { | 130 for (var x of [undefined, null, false, true, "cow", 42, 3.14]) { |
127 assertThrows(function () { constructor.of.call(x); }, TypeError); | 131 assertThrows(function () { constructor.of.call(x); }, TypeError); |
128 } | 132 } |
129 } | 133 } |
130 | 134 |
131 for (var constructor of typedArrayConstructors) { | 135 for (var constructor of typedArrayConstructors) { |
132 TestTypedArrayOf(constructor); | 136 TestTypedArrayOf(constructor); |
133 } | 137 } |
OLD | NEW |