OLD | NEW |
(Empty) | |
| 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 // Flags: --allow-natives-syntax |
| 6 |
| 7 "use strict"; |
| 8 |
| 9 |
| 10 function checkPrototypeChain(object, constructors) { |
| 11 var proto = object.__proto__; |
| 12 for (var i = 0; i < constructors.length; i++) { |
| 13 assertEquals(constructors[i].prototype, proto); |
| 14 assertEquals(constructors[i], proto.constructor); |
| 15 proto = proto.__proto__; |
| 16 } |
| 17 } |
| 18 |
| 19 |
| 20 (function() { |
| 21 class A extends Boolean { |
| 22 constructor(...args) { |
| 23 assertTrue(%IsConstructCall()); |
| 24 super(...args); |
| 25 this.a = 42; |
| 26 } |
| 27 } |
| 28 |
| 29 var o = new A(true); |
| 30 assertTrue(o instanceof Object); |
| 31 assertTrue(o instanceof Boolean); |
| 32 assertTrue(o instanceof A); |
| 33 assertEquals("object", typeof o); |
| 34 checkPrototypeChain(o, [A, Boolean]); |
| 35 assertTrue(o.valueOf()); |
| 36 assertEquals(42, o.a); |
| 37 |
| 38 var o1 = new A(false); |
| 39 assertTrue(%HaveSameMap(o, o1)); |
| 40 })(); |
| 41 |
| 42 |
| 43 function TestErrorSubclassing(error) { |
| 44 class A extends error { |
| 45 constructor(...args) { |
| 46 assertTrue(%IsConstructCall()); |
| 47 super(...args); |
| 48 this.a = 42; |
| 49 } |
| 50 } |
| 51 |
| 52 var o = new A("message"); |
| 53 assertTrue(o instanceof Object); |
| 54 assertTrue(o instanceof error); |
| 55 assertTrue(o instanceof Error); |
| 56 assertTrue(o instanceof A); |
| 57 assertEquals("object", typeof o); |
| 58 if (error == Error) { |
| 59 checkPrototypeChain(o, [A, Error, Object]); |
| 60 } else { |
| 61 checkPrototypeChain(o, [A, error, Error, Object]); |
| 62 } |
| 63 assertEquals("message", o.message); |
| 64 assertEquals(error.name + ": message", o.toString()); |
| 65 assertEquals(42, o.a); |
| 66 |
| 67 var o1 = new A("achtung!"); |
| 68 assertTrue(%HaveSameMap(o, o1)); |
| 69 } |
| 70 |
| 71 |
| 72 (function() { |
| 73 TestErrorSubclassing(Error); |
| 74 TestErrorSubclassing(EvalError); |
| 75 TestErrorSubclassing(RangeError); |
| 76 TestErrorSubclassing(ReferenceError); |
| 77 TestErrorSubclassing(SyntaxError); |
| 78 TestErrorSubclassing(TypeError); |
| 79 TestErrorSubclassing(URIError); |
| 80 })(); |
| 81 |
| 82 |
| 83 (function() { |
| 84 class A extends Number { |
| 85 constructor(...args) { |
| 86 assertTrue(%IsConstructCall()); |
| 87 super(...args); |
| 88 this.a = 42; |
| 89 } |
| 90 } |
| 91 |
| 92 var o = new A(153); |
| 93 assertTrue(o instanceof Object); |
| 94 assertTrue(o instanceof Number); |
| 95 assertTrue(o instanceof A); |
| 96 assertEquals("object", typeof o); |
| 97 checkPrototypeChain(o, [A, Number, Object]); |
| 98 assertEquals(153, o.valueOf()); |
| 99 assertEquals(42, o.a); |
| 100 |
| 101 var o1 = new A(312); |
| 102 assertTrue(%HaveSameMap(o, o1)); |
| 103 })(); |
| 104 |
| 105 |
| 106 (function() { |
| 107 class A extends Date { |
| 108 constructor(...args) { |
| 109 assertTrue(%IsConstructCall()); |
| 110 super(...args); |
| 111 this.a = 42; |
| 112 } |
| 113 } |
| 114 |
| 115 var o = new A(1234567890); |
| 116 assertTrue(o instanceof Object); |
| 117 assertTrue(o instanceof Date); |
| 118 assertTrue(o instanceof A); |
| 119 assertEquals("object", typeof o); |
| 120 checkPrototypeChain(o, [A, Date, Object]); |
| 121 assertEquals(1234567890, o.getTime()); |
| 122 assertEquals(42, o.a); |
| 123 |
| 124 var o1 = new A(2015, 10, 29); |
| 125 assertEquals(2015, o1.getFullYear()); |
| 126 assertEquals(10, o1.getMonth()); |
| 127 assertEquals(29, o1.getDate()); |
| 128 assertTrue(%HaveSameMap(o, o1)); |
| 129 })(); |
| 130 |
| 131 |
| 132 (function() { |
| 133 class A extends String { |
| 134 constructor(...args) { |
| 135 assertTrue(%IsConstructCall()); |
| 136 super(...args); |
| 137 this.a = 42; |
| 138 } |
| 139 } |
| 140 |
| 141 var o = new A("foo"); |
| 142 assertTrue(o instanceof Object); |
| 143 assertTrue(o instanceof String); |
| 144 assertTrue(o instanceof A); |
| 145 assertEquals("object", typeof o); |
| 146 checkPrototypeChain(o, [A, String, Object]); |
| 147 |
| 148 assertEquals("foo", o.valueOf()); |
| 149 assertEquals(42, o.a); |
| 150 |
| 151 var o1 = new A("bar"); |
| 152 assertTrue(%HaveSameMap(o, o1)); |
| 153 })(); |
| 154 |
| 155 |
| 156 (function() { |
| 157 class A extends RegExp { |
| 158 constructor(...args) { |
| 159 assertTrue(%IsConstructCall()); |
| 160 super(...args); |
| 161 this.a = 42; |
| 162 } |
| 163 } |
| 164 |
| 165 var o = new A("o..h"); |
| 166 assertTrue(o instanceof Object); |
| 167 assertTrue(o instanceof RegExp); |
| 168 assertTrue(o instanceof A); |
| 169 assertEquals("object", typeof o); |
| 170 checkPrototypeChain(o, [A, RegExp, Object]); |
| 171 assertTrue(o.test("ouch")); |
| 172 assertEquals(42, o.a); |
| 173 |
| 174 var o1 = new A(7); |
| 175 assertTrue(%HaveSameMap(o, o1)); |
| 176 })(); |
| 177 |
| 178 |
| 179 function TestArraySubclassing(array) { |
| 180 class A extends array { |
| 181 constructor(...args) { |
| 182 assertTrue(%IsConstructCall()); |
| 183 super(...args); |
| 184 this.a = 42; |
| 185 } |
| 186 } |
| 187 |
| 188 var o = new A(10); |
| 189 assertTrue(o instanceof Object); |
| 190 assertTrue(o instanceof array); |
| 191 assertTrue(o instanceof A); |
| 192 assertEquals("object", typeof o); |
| 193 checkPrototypeChain(o, [A, array, Object]); |
| 194 assertEquals(10, o.length); |
| 195 assertEquals(42, o.a); |
| 196 |
| 197 var o1 = new A(7); |
| 198 assertTrue(%HaveSameMap(o, o1)); |
| 199 } |
| 200 |
| 201 |
| 202 (function() { |
| 203 TestArraySubclassing(Array); |
| 204 TestArraySubclassing(Int8Array); |
| 205 TestArraySubclassing(Uint8Array); |
| 206 TestArraySubclassing(Uint8ClampedArray); |
| 207 TestArraySubclassing(Int16Array); |
| 208 TestArraySubclassing(Uint16Array); |
| 209 TestArraySubclassing(Int32Array); |
| 210 TestArraySubclassing(Uint32Array); |
| 211 TestArraySubclassing(Float32Array); |
| 212 TestArraySubclassing(Float64Array); |
| 213 })(); |
| 214 |
| 215 |
| 216 (function() { |
| 217 class A extends ArrayBuffer { |
| 218 constructor(...args) { |
| 219 assertTrue(%IsConstructCall()); |
| 220 super(...args); |
| 221 this.a = 42; |
| 222 } |
| 223 } |
| 224 |
| 225 var o = new A(16); |
| 226 assertTrue(o instanceof Object); |
| 227 assertTrue(o instanceof ArrayBuffer); |
| 228 assertTrue(o instanceof A); |
| 229 assertEquals("object", typeof o); |
| 230 checkPrototypeChain(o, [A, ArrayBuffer, Object]); |
| 231 |
| 232 assertEquals(16, o.byteLength); |
| 233 assertEquals(42, o.a); |
| 234 |
| 235 var o1 = new A("bar"); |
| 236 assertTrue(%HaveSameMap(o, o1)); |
| 237 |
| 238 |
| 239 class MyInt32Array extends Int32Array { |
| 240 constructor(v, name) { |
| 241 super(v); |
| 242 this.name = name; |
| 243 } |
| 244 } |
| 245 |
| 246 class MyUint32Array extends Uint32Array { |
| 247 constructor(v, name) { |
| 248 super(v); |
| 249 this.name = name; |
| 250 } |
| 251 } |
| 252 |
| 253 var int32view = new MyInt32Array(o, "cats"); |
| 254 var uint32view = new MyUint32Array(o, "dogs"); |
| 255 |
| 256 int32view[0] = -2; |
| 257 uint32view[1] = 0xffffffff; |
| 258 |
| 259 assertEquals("cats", int32view.name); |
| 260 assertEquals("dogs", uint32view.name); |
| 261 assertEquals(-2, int32view[0]); |
| 262 assertEquals(-1, int32view[1]); |
| 263 assertEquals(0xfffffffe, uint32view[0]); |
| 264 assertEquals(0xffffffff, uint32view[1]); |
| 265 })(); |
| 266 |
| 267 |
| 268 (function() { |
| 269 class A extends DataView { |
| 270 constructor(...args) { |
| 271 assertTrue(%IsConstructCall()); |
| 272 super(...args); |
| 273 this.a = 42; |
| 274 } |
| 275 } |
| 276 |
| 277 var buffer = new ArrayBuffer(16); |
| 278 var o = new A(buffer); |
| 279 assertTrue(o instanceof Object); |
| 280 assertTrue(o instanceof DataView); |
| 281 assertTrue(o instanceof A); |
| 282 assertEquals("object", typeof o); |
| 283 checkPrototypeChain(o, [A, DataView, Object]); |
| 284 |
| 285 o.setUint32(0, 0xcafebabe, false); |
| 286 assertEquals(0xcafebabe, o.getUint32(0, false)); |
| 287 assertEquals(0xbebafeca, o.getUint32(0, true)); |
| 288 assertEquals(42, o.a); |
| 289 |
| 290 var o1 = new A(buffer); |
| 291 assertTrue(%HaveSameMap(o, o1)); |
| 292 |
| 293 })(); |
| 294 |
| 295 |
| 296 (function() { |
| 297 class A extends Boolean { |
| 298 constructor() { |
| 299 assertTrue(%IsConstructCall()); |
| 300 super(true); |
| 301 this.a00 = 0 |
| 302 this.a01 = 0 |
| 303 this.a02 = 0 |
| 304 this.a03 = 0 |
| 305 this.a04 = 0 |
| 306 this.a05 = 0 |
| 307 this.a06 = 0 |
| 308 this.a07 = 0 |
| 309 this.a08 = 0 |
| 310 this.a09 = 0 |
| 311 this.a10 = 0 |
| 312 this.a11 = 0 |
| 313 this.a12 = 0 |
| 314 this.a13 = 0 |
| 315 this.a14 = 0 |
| 316 this.a15 = 0 |
| 317 this.a16 = 0 |
| 318 this.a17 = 0 |
| 319 this.a18 = 0 |
| 320 this.a19 = 0 |
| 321 } |
| 322 } |
| 323 |
| 324 class B extends A { |
| 325 constructor() { |
| 326 assertTrue(%IsConstructCall()); |
| 327 super(); |
| 328 this.b00 = 0 |
| 329 this.b01 = 0 |
| 330 this.b02 = 0 |
| 331 this.b03 = 0 |
| 332 this.b04 = 0 |
| 333 this.b05 = 0 |
| 334 this.b06 = 0 |
| 335 this.b07 = 0 |
| 336 this.b08 = 0 |
| 337 this.b09 = 0 |
| 338 this.b10 = 0 |
| 339 this.b11 = 0 |
| 340 this.b12 = 0 |
| 341 this.b13 = 0 |
| 342 this.b14 = 0 |
| 343 this.b15 = 0 |
| 344 this.b16 = 0 |
| 345 this.b17 = 0 |
| 346 this.b18 = 0 |
| 347 this.b19 = 0 |
| 348 } |
| 349 } |
| 350 |
| 351 class C extends B { |
| 352 constructor() { |
| 353 assertTrue(%IsConstructCall()); |
| 354 super(); |
| 355 this.c00 = 0 |
| 356 this.c01 = 0 |
| 357 this.c02 = 0 |
| 358 this.c03 = 0 |
| 359 this.c04 = 0 |
| 360 this.c05 = 0 |
| 361 this.c06 = 0 |
| 362 this.c07 = 0 |
| 363 this.c08 = 0 |
| 364 this.c09 = 0 |
| 365 this.c10 = 0 |
| 366 this.c11 = 0 |
| 367 this.c12 = 0 |
| 368 this.c13 = 0 |
| 369 this.c14 = 0 |
| 370 this.c15 = 0 |
| 371 this.c16 = 0 |
| 372 this.c17 = 0 |
| 373 this.c18 = 0 |
| 374 this.c19 = 0 |
| 375 } |
| 376 } |
| 377 |
| 378 var o = new C(); |
| 379 assertTrue(o instanceof Object); |
| 380 assertTrue(o instanceof Boolean); |
| 381 assertTrue(o instanceof A); |
| 382 assertTrue(o instanceof B); |
| 383 assertTrue(o instanceof C); |
| 384 assertEquals("object", typeof o); |
| 385 checkPrototypeChain(o, [C, B, A, Boolean, Object]); |
| 386 })(); |
| 387 |
| 388 |
| 389 (function() { |
| 390 assertThrows("class A extends undefined {}"); |
| 391 assertThrows("class B extends NaN {}"); |
| 392 assertThrows("class C extends Infinity {}"); |
| 393 })(); |
| 394 |
| 395 |
| 396 (function() { |
| 397 class A extends null {} |
| 398 assertThrows("new A"); |
| 399 })(); |
| 400 |
| 401 |
| 402 (function() { |
| 403 class A extends Symbol {} |
| 404 assertThrows("new A"); |
| 405 })(); |
OLD | NEW |