OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 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 // Flags: --allow-natives-syntax | 5 // Flags: --allow-natives-syntax |
6 | 6 |
7 "use strict"; | 7 "use strict"; |
8 | 8 |
9 | 9 |
10 function checkPrototypeChain(object, constructors) { | 10 function checkPrototypeChain(object, constructors) { |
11 var proto = object.__proto__; | 11 var proto = object.__proto__; |
12 for (var i = 0; i < constructors.length; i++) { | 12 for (var i = 0; i < constructors.length; i++) { |
13 assertEquals(constructors[i].prototype, proto); | 13 assertEquals(constructors[i].prototype, proto); |
14 assertEquals(constructors[i], proto.constructor); | 14 assertEquals(constructors[i], proto.constructor); |
15 proto = proto.__proto__; | 15 proto = proto.__proto__; |
16 } | 16 } |
17 } | 17 } |
18 | 18 |
19 | 19 |
20 (function() { | 20 (function() { |
21 class A extends Boolean { | 21 class A extends Boolean { |
22 constructor(...args) { | 22 constructor(...args) { |
23 assertTrue(%IsConstructCall()); | 23 assertTrue(%IsConstructCall()); |
24 super(...args); | 24 super(...args); |
25 this.a = 42; | 25 this.a = 42; |
| 26 this.d = 4.2; |
26 } | 27 } |
27 } | 28 } |
28 | 29 |
29 var o = new A(true); | 30 var o = new A(true); |
30 assertTrue(o instanceof Object); | 31 assertTrue(o instanceof Object); |
31 assertTrue(o instanceof Boolean); | 32 assertTrue(o instanceof Boolean); |
32 assertTrue(o instanceof A); | 33 assertTrue(o instanceof A); |
33 assertEquals("object", typeof o); | 34 assertEquals("object", typeof o); |
34 checkPrototypeChain(o, [A, Boolean]); | 35 checkPrototypeChain(o, [A, Boolean]); |
35 assertTrue(o.valueOf()); | 36 assertTrue(o.valueOf()); |
36 assertEquals(42, o.a); | 37 assertEquals(42, o.a); |
| 38 assertEquals(4.2, o.d); |
37 | 39 |
38 var o1 = new A(false); | 40 var o1 = new A(false); |
39 assertTrue(%HaveSameMap(o, o1)); | 41 assertTrue(%HaveSameMap(o, o1)); |
40 })(); | 42 })(); |
41 | 43 |
42 | 44 |
43 function TestErrorSubclassing(error) { | 45 function TestErrorSubclassing(error) { |
44 class A extends error { | 46 class A extends error { |
45 constructor(...args) { | 47 constructor(...args) { |
46 assertTrue(%IsConstructCall()); | 48 assertTrue(%IsConstructCall()); |
47 super(...args); | 49 super(...args); |
48 this.a = 42; | 50 this.a = 42; |
| 51 this.d = 4.2; |
49 } | 52 } |
50 } | 53 } |
51 | 54 |
52 var o = new A("message"); | 55 var o = new A("message"); |
53 assertTrue(o instanceof Object); | 56 assertTrue(o instanceof Object); |
54 assertTrue(o instanceof error); | 57 assertTrue(o instanceof error); |
55 assertTrue(o instanceof Error); | 58 assertTrue(o instanceof Error); |
56 assertTrue(o instanceof A); | 59 assertTrue(o instanceof A); |
57 assertEquals("object", typeof o); | 60 assertEquals("object", typeof o); |
58 if (error == Error) { | 61 if (error == Error) { |
59 checkPrototypeChain(o, [A, Error, Object]); | 62 checkPrototypeChain(o, [A, Error, Object]); |
60 } else { | 63 } else { |
61 checkPrototypeChain(o, [A, error, Error, Object]); | 64 checkPrototypeChain(o, [A, error, Error, Object]); |
62 } | 65 } |
63 assertEquals("message", o.message); | 66 assertEquals("message", o.message); |
64 assertEquals(error.name + ": message", o.toString()); | 67 assertEquals(error.name + ": message", o.toString()); |
65 assertEquals(42, o.a); | 68 assertEquals(42, o.a); |
| 69 assertEquals(4.2, o.d); |
66 | 70 |
67 var o1 = new A("achtung!"); | 71 var o1 = new A("achtung!"); |
68 assertTrue(%HaveSameMap(o, o1)); | 72 assertTrue(%HaveSameMap(o, o1)); |
69 } | 73 } |
70 | 74 |
71 | 75 |
72 (function() { | 76 (function() { |
73 TestErrorSubclassing(Error); | 77 TestErrorSubclassing(Error); |
74 TestErrorSubclassing(EvalError); | 78 TestErrorSubclassing(EvalError); |
75 TestErrorSubclassing(RangeError); | 79 TestErrorSubclassing(RangeError); |
76 TestErrorSubclassing(ReferenceError); | 80 TestErrorSubclassing(ReferenceError); |
77 TestErrorSubclassing(SyntaxError); | 81 TestErrorSubclassing(SyntaxError); |
78 TestErrorSubclassing(TypeError); | 82 TestErrorSubclassing(TypeError); |
79 TestErrorSubclassing(URIError); | 83 TestErrorSubclassing(URIError); |
80 })(); | 84 })(); |
81 | 85 |
82 | 86 |
83 (function() { | 87 (function() { |
84 class A extends Number { | 88 class A extends Number { |
85 constructor(...args) { | 89 constructor(...args) { |
86 assertTrue(%IsConstructCall()); | 90 assertTrue(%IsConstructCall()); |
87 super(...args); | 91 super(...args); |
88 this.a = 42; | 92 this.a = 42; |
| 93 this.d = 4.2; |
89 } | 94 } |
90 } | 95 } |
91 | 96 |
92 var o = new A(153); | 97 var o = new A(153); |
93 assertTrue(o instanceof Object); | 98 assertTrue(o instanceof Object); |
94 assertTrue(o instanceof Number); | 99 assertTrue(o instanceof Number); |
95 assertTrue(o instanceof A); | 100 assertTrue(o instanceof A); |
96 assertEquals("object", typeof o); | 101 assertEquals("object", typeof o); |
97 checkPrototypeChain(o, [A, Number, Object]); | 102 checkPrototypeChain(o, [A, Number, Object]); |
98 assertEquals(153, o.valueOf()); | 103 assertEquals(153, o.valueOf()); |
99 assertEquals(42, o.a); | 104 assertEquals(42, o.a); |
| 105 assertEquals(4.2, o.d); |
100 | 106 |
101 var o1 = new A(312); | 107 var o1 = new A(312); |
102 assertTrue(%HaveSameMap(o, o1)); | 108 assertTrue(%HaveSameMap(o, o1)); |
103 })(); | 109 })(); |
104 | 110 |
105 | 111 |
106 (function() { | 112 (function() { |
107 class A extends Date { | 113 class A extends Date { |
108 constructor(...args) { | 114 constructor(...args) { |
109 assertTrue(%IsConstructCall()); | 115 assertTrue(%IsConstructCall()); |
110 super(...args); | 116 super(...args); |
111 this.a = 42; | 117 this.a = 42; |
| 118 this.d = 4.2; |
112 } | 119 } |
113 } | 120 } |
114 | 121 |
115 var o = new A(1234567890); | 122 var o = new A(1234567890); |
116 assertTrue(o instanceof Object); | 123 assertTrue(o instanceof Object); |
117 assertTrue(o instanceof Date); | 124 assertTrue(o instanceof Date); |
118 assertTrue(o instanceof A); | 125 assertTrue(o instanceof A); |
119 assertEquals("object", typeof o); | 126 assertEquals("object", typeof o); |
120 checkPrototypeChain(o, [A, Date, Object]); | 127 checkPrototypeChain(o, [A, Date, Object]); |
121 assertEquals(1234567890, o.getTime()); | 128 assertEquals(1234567890, o.getTime()); |
122 assertEquals(42, o.a); | 129 assertEquals(42, o.a); |
| 130 assertEquals(4.2, o.d); |
123 | 131 |
124 var o1 = new A(2015, 10, 29); | 132 var o1 = new A(2015, 10, 29); |
125 assertEquals(2015, o1.getFullYear()); | 133 assertEquals(2015, o1.getFullYear()); |
126 assertEquals(10, o1.getMonth()); | 134 assertEquals(10, o1.getMonth()); |
127 assertEquals(29, o1.getDate()); | 135 assertEquals(29, o1.getDate()); |
128 assertTrue(%HaveSameMap(o, o1)); | 136 assertTrue(%HaveSameMap(o, o1)); |
129 })(); | 137 })(); |
130 | 138 |
131 | 139 |
132 (function() { | 140 (function() { |
133 class A extends String { | 141 class A extends String { |
134 constructor(...args) { | 142 constructor(...args) { |
135 assertTrue(%IsConstructCall()); | 143 assertTrue(%IsConstructCall()); |
136 super(...args); | 144 super(...args); |
137 this.a = 42; | 145 this.a = 42; |
| 146 this.d = 4.2; |
138 } | 147 } |
139 } | 148 } |
140 | 149 |
141 var o = new A("foo"); | 150 var o = new A("foo"); |
142 assertTrue(o instanceof Object); | 151 assertTrue(o instanceof Object); |
143 assertTrue(o instanceof String); | 152 assertTrue(o instanceof String); |
144 assertTrue(o instanceof A); | 153 assertTrue(o instanceof A); |
145 assertEquals("object", typeof o); | 154 assertEquals("object", typeof o); |
146 checkPrototypeChain(o, [A, String, Object]); | 155 checkPrototypeChain(o, [A, String, Object]); |
147 | 156 |
148 assertEquals("foo", o.valueOf()); | 157 assertEquals("foo", o.valueOf()); |
149 assertEquals(42, o.a); | 158 assertEquals(42, o.a); |
| 159 assertEquals(4.2, o.d); |
150 | 160 |
151 var o1 = new A("bar"); | 161 var o1 = new A("bar"); |
152 assertTrue(%HaveSameMap(o, o1)); | 162 assertTrue(%HaveSameMap(o, o1)); |
153 })(); | 163 })(); |
154 | 164 |
155 | 165 |
156 (function() { | 166 (function() { |
157 class A extends RegExp { | 167 class A extends RegExp { |
158 constructor(...args) { | 168 constructor(...args) { |
159 assertTrue(%IsConstructCall()); | 169 assertTrue(%IsConstructCall()); |
160 super(...args); | 170 super(...args); |
161 this.a = 42; | 171 this.a = 42; |
| 172 this.d = 4.2; |
162 } | 173 } |
163 } | 174 } |
164 | 175 |
165 var o = new A("o..h"); | 176 var o = new A("o(..)h", "g"); |
166 assertTrue(o instanceof Object); | 177 assertTrue(o instanceof Object); |
167 assertTrue(o instanceof RegExp); | 178 assertTrue(o instanceof RegExp); |
168 assertTrue(o instanceof A); | 179 assertTrue(o instanceof A); |
169 assertEquals("object", typeof o); | 180 assertEquals("object", typeof o); |
170 checkPrototypeChain(o, [A, RegExp, Object]); | 181 checkPrototypeChain(o, [A, RegExp, Object]); |
171 assertTrue(o.test("ouch")); | 182 assertTrue(o.test("ouch")); |
| 183 assertArrayEquals(["ouch", "uc"], o.exec("boom! ouch! bam!")); |
| 184 assertEquals("o(..)h", o.source); |
| 185 assertTrue(o.global); |
| 186 assertFalse(o.ignoreCase); |
| 187 assertFalse(o.multiline); |
| 188 assertEquals(10, o.lastIndex); |
172 assertEquals(42, o.a); | 189 assertEquals(42, o.a); |
| 190 assertEquals(4.2, o.d); |
173 | 191 |
174 var o1 = new A(7); | 192 var o1 = new A(7); |
175 assertTrue(%HaveSameMap(o, o1)); | 193 assertTrue(%HaveSameMap(o, o1)); |
176 })(); | 194 })(); |
177 | 195 |
178 | 196 |
179 function TestArraySubclassing(array) { | 197 function TestArraySubclassing(array) { |
180 class A extends array { | 198 class A extends array { |
181 constructor(...args) { | 199 constructor(...args) { |
182 assertTrue(%IsConstructCall()); | 200 assertTrue(%IsConstructCall()); |
183 super(...args); | 201 super(...args); |
184 this.a = 42; | 202 this.a = 42; |
| 203 this.d = 4.2; |
185 } | 204 } |
186 } | 205 } |
187 | 206 |
188 var o = new array(13); | 207 var o = new array(13); |
189 assertTrue(o instanceof Object); | 208 assertTrue(o instanceof Object); |
190 assertTrue(o instanceof array); | 209 assertTrue(o instanceof array); |
191 assertEquals("object", typeof o); | 210 assertEquals("object", typeof o); |
192 checkPrototypeChain(o, [array, Object]); | 211 checkPrototypeChain(o, [array, Object]); |
193 assertEquals(13, o.length); | 212 assertEquals(13, o.length); |
194 | 213 |
195 var o = new A(10); | 214 var o = new A(10); |
196 assertTrue(o instanceof Object); | 215 assertTrue(o instanceof Object); |
197 assertTrue(o instanceof array); | 216 assertTrue(o instanceof array); |
198 assertTrue(o instanceof A); | 217 assertTrue(o instanceof A); |
199 assertEquals("object", typeof o); | 218 assertEquals("object", typeof o); |
200 checkPrototypeChain(o, [A, array, Object]); | 219 checkPrototypeChain(o, [A, array, Object]); |
201 assertEquals(10, o.length); | 220 assertEquals(10, o.length); |
202 assertEquals(42, o.a); | 221 assertEquals(42, o.a); |
| 222 assertEquals(4.2, o.d); |
203 | 223 |
204 var o1 = new A(7); | 224 var o1 = new A(7); |
205 assertTrue(%HaveSameMap(o, o1)); | 225 assertTrue(%HaveSameMap(o, o1)); |
206 } | 226 } |
207 | 227 |
208 | 228 |
209 (function() { | 229 (function() { |
210 TestArraySubclassing(Array); | 230 TestArraySubclassing(Array); |
211 TestArraySubclassing(Int8Array); | 231 TestArraySubclassing(Int8Array); |
212 TestArraySubclassing(Uint8Array); | 232 TestArraySubclassing(Uint8Array); |
213 TestArraySubclassing(Uint8ClampedArray); | 233 TestArraySubclassing(Uint8ClampedArray); |
214 TestArraySubclassing(Int16Array); | 234 TestArraySubclassing(Int16Array); |
215 TestArraySubclassing(Uint16Array); | 235 TestArraySubclassing(Uint16Array); |
216 TestArraySubclassing(Int32Array); | 236 TestArraySubclassing(Int32Array); |
217 TestArraySubclassing(Uint32Array); | 237 TestArraySubclassing(Uint32Array); |
218 TestArraySubclassing(Float32Array); | 238 TestArraySubclassing(Float32Array); |
219 TestArraySubclassing(Float64Array); | 239 TestArraySubclassing(Float64Array); |
220 })(); | 240 })(); |
221 | 241 |
222 | 242 |
223 (function() { | 243 (function() { |
224 class A extends ArrayBuffer { | 244 class A extends ArrayBuffer { |
225 constructor(...args) { | 245 constructor(...args) { |
226 assertTrue(%IsConstructCall()); | 246 assertTrue(%IsConstructCall()); |
227 super(...args); | 247 super(...args); |
228 this.a = 42; | 248 this.a = 42; |
| 249 this.d = 4.2; |
229 } | 250 } |
230 } | 251 } |
231 | 252 |
232 var o = new A(16); | 253 var o = new A(16); |
233 assertTrue(o instanceof Object); | 254 assertTrue(o instanceof Object); |
234 assertTrue(o instanceof ArrayBuffer); | 255 assertTrue(o instanceof ArrayBuffer); |
235 assertTrue(o instanceof A); | 256 assertTrue(o instanceof A); |
236 assertEquals("object", typeof o); | 257 assertEquals("object", typeof o); |
237 checkPrototypeChain(o, [A, ArrayBuffer, Object]); | 258 checkPrototypeChain(o, [A, ArrayBuffer, Object]); |
238 | 259 |
239 assertEquals(16, o.byteLength); | 260 assertEquals(16, o.byteLength); |
240 assertEquals(42, o.a); | 261 assertEquals(42, o.a); |
| 262 assertEquals(4.2, o.d); |
241 | 263 |
242 var o1 = new A("bar"); | 264 var o1 = new A("bar"); |
243 assertTrue(%HaveSameMap(o, o1)); | 265 assertTrue(%HaveSameMap(o, o1)); |
244 | 266 |
245 | 267 |
246 class MyInt32Array extends Int32Array { | 268 class MyInt32Array extends Int32Array { |
247 constructor(v, name) { | 269 constructor(v, name) { |
248 super(v); | 270 super(v); |
249 this.name = name; | 271 this.name = name; |
250 } | 272 } |
(...skipping 20 matching lines...) Expand all Loading... |
271 assertEquals(0xffffffff, uint32view[1]); | 293 assertEquals(0xffffffff, uint32view[1]); |
272 })(); | 294 })(); |
273 | 295 |
274 | 296 |
275 (function() { | 297 (function() { |
276 class A extends DataView { | 298 class A extends DataView { |
277 constructor(...args) { | 299 constructor(...args) { |
278 assertTrue(%IsConstructCall()); | 300 assertTrue(%IsConstructCall()); |
279 super(...args); | 301 super(...args); |
280 this.a = 42; | 302 this.a = 42; |
| 303 this.d = 4.2; |
281 } | 304 } |
282 } | 305 } |
283 | 306 |
284 var buffer = new ArrayBuffer(16); | 307 var buffer = new ArrayBuffer(16); |
285 var o = new A(buffer); | 308 var o = new A(buffer); |
286 assertTrue(o instanceof Object); | 309 assertTrue(o instanceof Object); |
287 assertTrue(o instanceof DataView); | 310 assertTrue(o instanceof DataView); |
288 assertTrue(o instanceof A); | 311 assertTrue(o instanceof A); |
289 assertEquals("object", typeof o); | 312 assertEquals("object", typeof o); |
290 checkPrototypeChain(o, [A, DataView, Object]); | 313 checkPrototypeChain(o, [A, DataView, Object]); |
291 | 314 |
292 o.setUint32(0, 0xcafebabe, false); | 315 o.setUint32(0, 0xcafebabe, false); |
293 assertEquals(0xcafebabe, o.getUint32(0, false)); | 316 assertEquals(0xcafebabe, o.getUint32(0, false)); |
294 assertEquals(0xbebafeca, o.getUint32(0, true)); | 317 assertEquals(0xbebafeca, o.getUint32(0, true)); |
295 assertEquals(42, o.a); | 318 assertEquals(42, o.a); |
| 319 assertEquals(4.2, o.d); |
296 | 320 |
297 var o1 = new A(buffer); | 321 var o1 = new A(buffer); |
298 assertTrue(%HaveSameMap(o, o1)); | 322 assertTrue(%HaveSameMap(o, o1)); |
299 | 323 |
300 })(); | 324 })(); |
301 | 325 |
302 | 326 |
303 (function() { | 327 (function() { |
304 class A extends Boolean { | 328 class A extends Boolean { |
305 constructor() { | 329 constructor() { |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 (function() { | 427 (function() { |
404 class A extends null {} | 428 class A extends null {} |
405 assertThrows("new A"); | 429 assertThrows("new A"); |
406 })(); | 430 })(); |
407 | 431 |
408 | 432 |
409 (function() { | 433 (function() { |
410 class A extends Symbol {} | 434 class A extends Symbol {} |
411 assertThrows("new A"); | 435 assertThrows("new A"); |
412 })(); | 436 })(); |
OLD | NEW |