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 array(13); |
| 189 assertTrue(o instanceof Object); |
| 190 assertTrue(o instanceof array); |
| 191 assertEquals("object", typeof o); |
| 192 checkPrototypeChain(o, [array, Object]); |
| 193 assertEquals(13, o.length); |
| 194 |
| 195 var o = new A(10); |
| 196 assertTrue(o instanceof Object); |
| 197 assertTrue(o instanceof array); |
| 198 assertTrue(o instanceof A); |
| 199 assertEquals("object", typeof o); |
| 200 checkPrototypeChain(o, [A, array, Object]); |
| 201 assertEquals(10, o.length); |
| 202 assertEquals(42, o.a); |
| 203 |
| 204 var o1 = new A(7); |
| 205 assertTrue(%HaveSameMap(o, o1)); |
| 206 } |
| 207 |
| 208 |
| 209 (function() { |
| 210 TestArraySubclassing(Array); |
| 211 TestArraySubclassing(Int8Array); |
| 212 TestArraySubclassing(Uint8Array); |
| 213 TestArraySubclassing(Uint8ClampedArray); |
| 214 TestArraySubclassing(Int16Array); |
| 215 TestArraySubclassing(Uint16Array); |
| 216 TestArraySubclassing(Int32Array); |
| 217 TestArraySubclassing(Uint32Array); |
| 218 TestArraySubclassing(Float32Array); |
| 219 TestArraySubclassing(Float64Array); |
| 220 })(); |
| 221 |
| 222 |
| 223 (function() { |
| 224 class A extends ArrayBuffer { |
| 225 constructor(...args) { |
| 226 assertTrue(%IsConstructCall()); |
| 227 super(...args); |
| 228 this.a = 42; |
| 229 } |
| 230 } |
| 231 |
| 232 var o = new A(16); |
| 233 assertTrue(o instanceof Object); |
| 234 assertTrue(o instanceof ArrayBuffer); |
| 235 assertTrue(o instanceof A); |
| 236 assertEquals("object", typeof o); |
| 237 checkPrototypeChain(o, [A, ArrayBuffer, Object]); |
| 238 |
| 239 assertEquals(16, o.byteLength); |
| 240 assertEquals(42, o.a); |
| 241 |
| 242 var o1 = new A("bar"); |
| 243 assertTrue(%HaveSameMap(o, o1)); |
| 244 |
| 245 |
| 246 class MyInt32Array extends Int32Array { |
| 247 constructor(v, name) { |
| 248 super(v); |
| 249 this.name = name; |
| 250 } |
| 251 } |
| 252 |
| 253 class MyUint32Array extends Uint32Array { |
| 254 constructor(v, name) { |
| 255 super(v); |
| 256 this.name = name; |
| 257 } |
| 258 } |
| 259 |
| 260 var int32view = new MyInt32Array(o, "cats"); |
| 261 var uint32view = new MyUint32Array(o, "dogs"); |
| 262 |
| 263 int32view[0] = -2; |
| 264 uint32view[1] = 0xffffffff; |
| 265 |
| 266 assertEquals("cats", int32view.name); |
| 267 assertEquals("dogs", uint32view.name); |
| 268 assertEquals(-2, int32view[0]); |
| 269 assertEquals(-1, int32view[1]); |
| 270 assertEquals(0xfffffffe, uint32view[0]); |
| 271 assertEquals(0xffffffff, uint32view[1]); |
| 272 })(); |
| 273 |
| 274 |
| 275 (function() { |
| 276 class A extends DataView { |
| 277 constructor(...args) { |
| 278 assertTrue(%IsConstructCall()); |
| 279 super(...args); |
| 280 this.a = 42; |
| 281 } |
| 282 } |
| 283 |
| 284 var buffer = new ArrayBuffer(16); |
| 285 var o = new A(buffer); |
| 286 assertTrue(o instanceof Object); |
| 287 assertTrue(o instanceof DataView); |
| 288 assertTrue(o instanceof A); |
| 289 assertEquals("object", typeof o); |
| 290 checkPrototypeChain(o, [A, DataView, Object]); |
| 291 |
| 292 o.setUint32(0, 0xcafebabe, false); |
| 293 assertEquals(0xcafebabe, o.getUint32(0, false)); |
| 294 assertEquals(0xbebafeca, o.getUint32(0, true)); |
| 295 assertEquals(42, o.a); |
| 296 |
| 297 var o1 = new A(buffer); |
| 298 assertTrue(%HaveSameMap(o, o1)); |
| 299 |
| 300 })(); |
| 301 |
| 302 |
| 303 (function() { |
| 304 class A extends Boolean { |
| 305 constructor() { |
| 306 assertTrue(%IsConstructCall()); |
| 307 super(true); |
| 308 this.a00 = 0 |
| 309 this.a01 = 0 |
| 310 this.a02 = 0 |
| 311 this.a03 = 0 |
| 312 this.a04 = 0 |
| 313 this.a05 = 0 |
| 314 this.a06 = 0 |
| 315 this.a07 = 0 |
| 316 this.a08 = 0 |
| 317 this.a09 = 0 |
| 318 this.a10 = 0 |
| 319 this.a11 = 0 |
| 320 this.a12 = 0 |
| 321 this.a13 = 0 |
| 322 this.a14 = 0 |
| 323 this.a15 = 0 |
| 324 this.a16 = 0 |
| 325 this.a17 = 0 |
| 326 this.a18 = 0 |
| 327 this.a19 = 0 |
| 328 } |
| 329 } |
| 330 |
| 331 class B extends A { |
| 332 constructor() { |
| 333 assertTrue(%IsConstructCall()); |
| 334 super(); |
| 335 this.b00 = 0 |
| 336 this.b01 = 0 |
| 337 this.b02 = 0 |
| 338 this.b03 = 0 |
| 339 this.b04 = 0 |
| 340 this.b05 = 0 |
| 341 this.b06 = 0 |
| 342 this.b07 = 0 |
| 343 this.b08 = 0 |
| 344 this.b09 = 0 |
| 345 this.b10 = 0 |
| 346 this.b11 = 0 |
| 347 this.b12 = 0 |
| 348 this.b13 = 0 |
| 349 this.b14 = 0 |
| 350 this.b15 = 0 |
| 351 this.b16 = 0 |
| 352 this.b17 = 0 |
| 353 this.b18 = 0 |
| 354 this.b19 = 0 |
| 355 } |
| 356 } |
| 357 |
| 358 class C extends B { |
| 359 constructor() { |
| 360 assertTrue(%IsConstructCall()); |
| 361 super(); |
| 362 this.c00 = 0 |
| 363 this.c01 = 0 |
| 364 this.c02 = 0 |
| 365 this.c03 = 0 |
| 366 this.c04 = 0 |
| 367 this.c05 = 0 |
| 368 this.c06 = 0 |
| 369 this.c07 = 0 |
| 370 this.c08 = 0 |
| 371 this.c09 = 0 |
| 372 this.c10 = 0 |
| 373 this.c11 = 0 |
| 374 this.c12 = 0 |
| 375 this.c13 = 0 |
| 376 this.c14 = 0 |
| 377 this.c15 = 0 |
| 378 this.c16 = 0 |
| 379 this.c17 = 0 |
| 380 this.c18 = 0 |
| 381 this.c19 = 0 |
| 382 } |
| 383 } |
| 384 |
| 385 var o = new C(); |
| 386 assertTrue(o instanceof Object); |
| 387 assertTrue(o instanceof Boolean); |
| 388 assertTrue(o instanceof A); |
| 389 assertTrue(o instanceof B); |
| 390 assertTrue(o instanceof C); |
| 391 assertEquals("object", typeof o); |
| 392 checkPrototypeChain(o, [C, B, A, Boolean, Object]); |
| 393 })(); |
| 394 |
| 395 |
| 396 (function() { |
| 397 assertThrows("class A extends undefined {}"); |
| 398 assertThrows("class B extends NaN {}"); |
| 399 assertThrows("class C extends Infinity {}"); |
| 400 })(); |
| 401 |
| 402 |
| 403 (function() { |
| 404 class A extends null {} |
| 405 assertThrows("new A"); |
| 406 })(); |
| 407 |
| 408 |
| 409 (function() { |
| 410 class A extends Symbol {} |
| 411 assertThrows("new A"); |
| 412 })(); |
OLD | NEW |