Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Flags: --harmony-sloppy --allow-natives-syntax |
| 6 | |
| 7 | (function TestBasics() { |
| 8 | var C = class C {} |
| 9 | assertEquals(typeof C, 'function'); |
| 10 | assertEquals(C.__proto__, Function.prototype); |
| 11 | assertEquals(Object.prototype, Object.getPrototypeOf(C.prototype)); |
| 12 | assertEquals(Function.prototype, Object.getPrototypeOf(C)); |
| 13 | assertEquals('C', C.name); |
| 14 | |
| 15 | class D {} |
| 16 | assertEquals(typeof D, 'function'); |
| 17 | assertEquals(D.__proto__, Function.prototype); |
| 18 | assertEquals(Object.prototype, Object.getPrototypeOf(D.prototype)); |
| 19 | assertEquals(Function.prototype, Object.getPrototypeOf(D)); |
| 20 | assertEquals('D', D.name); |
| 21 | |
| 22 | class D2 { constructor() {} } |
| 23 | assertEquals('D2', D2.name); |
| 24 | |
| 25 | // TODO(arv): The logic for the name of anonymous functions in ES6 requires |
| 26 | // the below to be 'E'; |
| 27 | var E = class {} |
| 28 | assertEquals('', E.name); // Should be 'E'. |
| 29 | |
| 30 | var F = class { constructor() {} }; |
| 31 | assertEquals('', F.name); // Should be 'F'. |
| 32 | })(); |
| 33 | |
| 34 | |
| 35 | (function TestBasicsExtends() { |
| 36 | class C extends null {} |
| 37 | assertEquals(typeof C, 'function'); |
| 38 | assertEquals(C.__proto__, Function.prototype); |
| 39 | assertEquals(null, Object.getPrototypeOf(C.prototype)); |
| 40 | |
| 41 | class D extends C {} |
| 42 | assertEquals(typeof D, 'function'); |
| 43 | assertEquals(D.__proto__, C); |
| 44 | assertEquals(C.prototype, Object.getPrototypeOf(D.prototype)); |
| 45 | })(); |
| 46 | |
| 47 | |
| 48 | (function TestSideEffectInExtends() { |
| 49 | var calls = 0; |
| 50 | class C {} |
| 51 | class D extends (calls++, C) {} |
| 52 | assertEquals(1, calls); |
| 53 | assertEquals(typeof D, 'function'); |
| 54 | assertEquals(D.__proto__, C); |
| 55 | assertEquals(C.prototype, Object.getPrototypeOf(D.prototype)); |
| 56 | })(); |
| 57 | |
| 58 | |
| 59 | (function TestInvalidExtends() { |
| 60 | assertThrows(function() { |
| 61 | class C extends 42 {} |
| 62 | }, TypeError); |
| 63 | |
| 64 | assertThrows(function() { |
| 65 | // Function but its .prototype is not null or a function. |
| 66 | class C extends Math.abs {} |
| 67 | }, TypeError); |
| 68 | |
| 69 | assertThrows(function() { |
| 70 | Math.abs.prototype = 42; |
| 71 | class C extends Math.abs {} |
| 72 | }, TypeError); |
| 73 | delete Math.abs.prototype; |
| 74 | |
| 75 | assertThrows(function() { |
| 76 | function* g() {} |
| 77 | class C extends g {} |
| 78 | }, TypeError); |
| 79 | })(); |
| 80 | |
| 81 | |
| 82 | (function TestConstructorProperty() { |
| 83 | class C {} |
| 84 | assertEquals(C, C.prototype.constructor); |
| 85 | var descr = Object.getOwnPropertyDescriptor(C.prototype, 'constructor'); |
| 86 | assertTrue(descr.configurable); |
| 87 | assertFalse(descr.enumerable); |
| 88 | assertTrue(descr.writable); |
| 89 | })(); |
| 90 | |
| 91 | |
| 92 | (function TestPrototypeProperty() { |
| 93 | class C {} |
| 94 | var descr = Object.getOwnPropertyDescriptor(C, 'prototype'); |
| 95 | assertFalse(descr.configurable); |
| 96 | assertFalse(descr.enumerable); |
| 97 | assertFalse(descr.writable); |
| 98 | })(); |
| 99 | |
| 100 | |
| 101 | (function TestConstructor() { |
| 102 | var count = 0; |
| 103 | class C { |
| 104 | constructor() { |
| 105 | assertEquals(Object.getPrototypeOf(this), C.prototype); |
| 106 | count++; |
| 107 | } |
| 108 | } |
| 109 | assertEquals(C, C.prototype.constructor); |
| 110 | var descr = Object.getOwnPropertyDescriptor(C.prototype, 'constructor'); |
| 111 | assertTrue(descr.configurable); |
| 112 | assertFalse(descr.enumerable); |
| 113 | assertTrue(descr.writable); |
| 114 | |
| 115 | var c = new C(); |
| 116 | assertEquals(1, count); |
| 117 | assertEquals(Object.getPrototypeOf(c), C.prototype); |
| 118 | })(); |
| 119 | |
| 120 | |
| 121 | (function TestImplicitConstructor() { |
| 122 | class C {} |
| 123 | var c = new C(); |
| 124 | assertEquals(Object.getPrototypeOf(c), C.prototype); |
| 125 | })(); |
| 126 | |
| 127 | |
| 128 | (function TestConstructorStrict() { |
| 129 | class C { |
| 130 | constructor() { |
| 131 | assertThrows(function() { |
| 132 | nonExistingBinding = 42; |
| 133 | }, ReferenceError); |
| 134 | } |
| 135 | } |
| 136 | new C(); |
| 137 | })(); |
| 138 | |
| 139 | |
| 140 | (function TestSuperInConstructor() { |
| 141 | var calls = 0; |
| 142 | class B {} |
| 143 | B.prototype.x = 42; |
| 144 | |
| 145 | class C extends B { |
| 146 | constructor() { |
| 147 | super(); |
| 148 | calls++; |
| 149 | assertEquals(42, super.x); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | new C; |
| 154 | assertEquals(1, calls); |
| 155 | })(); |
| 156 | |
| 157 | |
| 158 | (function TestStrictMode() { |
| 159 | class C {} |
| 160 | |
| 161 | with ({a: 1}) { |
| 162 | assertEquals(1, a); |
| 163 | } |
| 164 | |
| 165 | assertThrows('class C extends function B() { with ({}); return B; }() {}', |
| 166 | SyntaxError); |
| 167 | |
| 168 | var D = class extends function() { |
| 169 | arguments.caller; |
| 170 | } {}; |
| 171 | assertThrows(function() { |
| 172 | Object.getPrototypeOf(D).arguments; |
| 173 | }, TypeError); |
| 174 | assertThrows(function() { |
| 175 | new D; |
| 176 | }, TypeError); |
| 177 | })(); |
| 178 | |
| 179 | |
| 180 | (function TestToString() { |
| 181 | class C {} |
| 182 | assertEquals('class C {}', C.toString()); |
| 183 | |
| 184 | class D { constructor() { 42; } } |
| 185 | assertEquals('class D { constructor() { 42; } }', D.toString()); |
| 186 | |
| 187 | class E { x() { 42; } } |
| 188 | assertEquals('class E { x() { 42; } }', E.toString()); |
| 189 | })(); |
| 190 | |
| 191 | |
| 192 | function assertMethodDescriptor(object, name) { |
| 193 | var descr = Object.getOwnPropertyDescriptor(object, name); |
| 194 | assertTrue(descr.configurable); |
| 195 | assertFalse(descr.enumerable); |
| 196 | assertTrue(descr.writable); |
| 197 | assertEquals('function', typeof descr.value); |
| 198 | assertFalse('prototype' in descr.value); |
| 199 | } |
| 200 | |
| 201 | |
| 202 | function assertGetterDescriptor(object, name) { |
| 203 | var descr = Object.getOwnPropertyDescriptor(object, name); |
| 204 | assertTrue(descr.configurable); |
| 205 | assertFalse(descr.enumerable); |
| 206 | assertEquals('function', typeof descr.get); |
| 207 | assertFalse('prototype' in descr.get); |
| 208 | assertEquals(undefined, descr.set); |
| 209 | } |
| 210 | |
| 211 | |
| 212 | function assertSetterDescriptor(object, name) { |
| 213 | var descr = Object.getOwnPropertyDescriptor(object, name); |
| 214 | assertTrue(descr.configurable); |
| 215 | assertFalse(descr.enumerable); |
| 216 | assertEquals(undefined, descr.get); |
| 217 | assertEquals('function', typeof descr.set); |
| 218 | assertFalse('prototype' in descr.set); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | function assertAccessorDescriptor(object, name) { |
| 223 | var descr = Object.getOwnPropertyDescriptor(object, name); |
| 224 | assertTrue(descr.configurable); |
| 225 | assertFalse(descr.enumerable); |
| 226 | assertEquals('function', typeof descr.get); |
| 227 | assertEquals('function', typeof descr.set); |
| 228 | assertFalse('prototype' in descr.get); |
| 229 | assertFalse('prototype' in descr.set); |
| 230 | } |
| 231 | |
| 232 | |
| 233 | (function TestMethods() { |
| 234 | class C { |
| 235 | method() { return 1; } |
| 236 | static staticMethod() { return 2; } |
| 237 | method2() { return 3; } |
| 238 | static staticMethod2() { return 4; } |
| 239 | } |
| 240 | |
| 241 | assertMethodDescriptor(C.prototype, 'method'); |
| 242 | assertMethodDescriptor(C.prototype, 'method2'); |
| 243 | assertMethodDescriptor(C, 'staticMethod'); |
| 244 | assertMethodDescriptor(C, 'staticMethod2'); |
| 245 | |
| 246 | assertEquals(1, new C().method()); |
| 247 | assertEquals(2, C.staticMethod()); |
| 248 | assertEquals(3, new C().method2()); |
| 249 | assertEquals(4, C.staticMethod2()); |
| 250 | })(); |
| 251 | |
| 252 | |
| 253 | (function TestGetters() { |
| 254 | class C { |
| 255 | get x() { return 1; } |
| 256 | static get staticX() { return 2; } |
| 257 | get y() { return 3; } |
| 258 | static get staticY() { return 4; } |
| 259 | } |
| 260 | |
| 261 | assertGetterDescriptor(C.prototype, 'x'); |
| 262 | assertGetterDescriptor(C.prototype, 'y'); |
| 263 | assertGetterDescriptor(C, 'staticX'); |
| 264 | assertGetterDescriptor(C, 'staticY'); |
| 265 | |
| 266 | assertEquals(1, new C().x); |
| 267 | assertEquals(2, C.staticX); |
| 268 | assertEquals(3, new C().y); |
| 269 | assertEquals(4, C.staticY); |
| 270 | })(); |
| 271 | |
| 272 | |
| 273 | |
| 274 | (function TestSetters() { |
| 275 | var x, staticX, y, staticY; |
| 276 | class C { |
| 277 | set x(v) { x = v; } |
| 278 | static set staticX(v) { staticX = v; } |
| 279 | set y(v) { y = v; } |
| 280 | static set staticY(v) { staticY = v; } |
| 281 | } |
| 282 | |
| 283 | assertSetterDescriptor(C.prototype, 'x'); |
| 284 | assertSetterDescriptor(C.prototype, 'y'); |
| 285 | assertSetterDescriptor(C, 'staticX'); |
| 286 | assertSetterDescriptor(C, 'staticY'); |
| 287 | |
| 288 | assertEquals(1, new C().x = 1); |
| 289 | assertEquals(1, x); |
| 290 | assertEquals(2, C.staticX = 2); |
| 291 | assertEquals(2, staticX); |
| 292 | assertEquals(3, new C().y = 3); |
| 293 | assertEquals(3, y); |
| 294 | assertEquals(4, C.staticY = 4); |
| 295 | assertEquals(4, staticY); |
| 296 | })(); |
| 297 | |
| 298 | |
| 299 | (function TestSideEffectsInPropertyDefine() { |
| 300 | function B() {} |
| 301 | B.prototype = { |
| 302 | constructor: B, |
| 303 | set m(v) { |
| 304 | throw Error(); |
| 305 | } |
| 306 | }; |
| 307 | |
| 308 | class C extends B { |
| 309 | m() { return 1; } |
| 310 | } |
| 311 | |
| 312 | assertEquals(1, new C().m()); |
| 313 | })(); |
| 314 | |
| 315 | |
| 316 | (function TestAccessors() { |
| 317 | class C { |
| 318 | constructor(x) { |
| 319 | this._x = x; |
| 320 | } |
| 321 | |
| 322 | get x() { return this._x; } |
| 323 | set x(v) { this._x = v; } |
| 324 | |
| 325 | static get staticX() { return this._x; } |
| 326 | static set staticX(v) { this._x = v; } |
| 327 | } |
| 328 | |
| 329 | assertAccessorDescriptor(C.prototype, 'x'); |
| 330 | assertAccessorDescriptor(C, 'staticX'); |
| 331 | |
| 332 | var c = new C(1); |
| 333 | c._x = 1; |
| 334 | assertEquals(1, c.x); |
| 335 | c.x = 2; |
| 336 | assertEquals(2, c._x); |
| 337 | |
| 338 | C._x = 3; |
| 339 | assertEquals(3, C.staticX); |
| 340 | C._x = 4; |
| 341 | assertEquals(4, C.staticX ); |
| 342 | })(); |
| 343 | |
| 344 | |
| 345 | (function TestProto() { |
| 346 | class C { |
| 347 | __proto__() { return 1; } |
| 348 | } |
| 349 | assertMethodDescriptor(C.prototype, '__proto__'); |
| 350 | assertEquals(1, new C().__proto__()); |
| 351 | })(); |
| 352 | |
| 353 | |
| 354 | (function TestProtoStatic() { |
| 355 | class C { |
| 356 | static __proto__() { return 1; } |
| 357 | } |
| 358 | assertMethodDescriptor(C, '__proto__'); |
| 359 | assertEquals(1, C.__proto__()); |
| 360 | })(); |
| 361 | |
| 362 | |
| 363 | (function TestProtoAccessor() { |
| 364 | class C { |
| 365 | get __proto__() { return this._p; } |
| 366 | set __proto__(v) { this._p = v; } |
| 367 | } |
| 368 | assertAccessorDescriptor(C.prototype, '__proto__'); |
| 369 | var c = new C(); |
| 370 | c._p = 1; |
| 371 | assertEquals(1, c.__proto__); |
| 372 | c.__proto__ = 2; |
| 373 | assertEquals(2, c.__proto__); |
| 374 | })(); |
| 375 | |
| 376 | |
| 377 | (function TestStaticProtoAccessor() { |
| 378 | class C { |
| 379 | static get __proto__() { return this._p; } |
| 380 | static set __proto__(v) { this._p = v; } |
| 381 | } |
| 382 | assertAccessorDescriptor(C, '__proto__'); |
| 383 | C._p = 1; |
| 384 | assertEquals(1, C.__proto__); |
| 385 | C.__proto__ = 2; |
| 386 | assertEquals(2, C.__proto__); |
| 387 | })(); |
| 388 | |
| 389 | |
| 390 | (function TestSettersOnProto() { |
| 391 | function Base() {} |
| 392 | Base.prototype = { |
| 393 | set constructor(_) { |
| 394 | assertUnreachable(); |
| 395 | }, |
| 396 | set m(_) { |
| 397 | assertUnreachable(); |
| 398 | } |
| 399 | }; |
| 400 | Object.defineProperty(Base, 'staticM', { |
| 401 | set: function() { |
| 402 | assertUnreachable(); |
| 403 | } |
| 404 | }); |
| 405 | |
| 406 | class C extends Base { |
| 407 | m() { |
| 408 | return 1; |
| 409 | } |
| 410 | static staticM() { |
| 411 | return 2; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | assertEquals(1, new C().m()); |
| 416 | assertEquals(2, C.staticM()); |
| 417 | })(); |
| 418 | |
| 419 | |
| 420 | (function TestConstructableButNoPrototype() { |
| 421 | var Base = function() {}.bind(); |
| 422 | assertThrows(function() { |
| 423 | class C extends Base {} |
| 424 | }, TypeError); |
| 425 | })(); |
| 426 | |
| 427 | |
| 428 | (function TestPrototypeGetter() { |
| 429 | var calls = 0; |
| 430 | var Base = function() {}.bind(); |
| 431 | Object.defineProperty(Base, 'prototype', { |
| 432 | get: function() { |
| 433 | calls++; |
| 434 | return null; |
| 435 | }, |
| 436 | configurable: true |
| 437 | }); |
| 438 | class C extends Base {} |
| 439 | assertEquals(1, calls); |
| 440 | |
| 441 | calls = 0; |
| 442 | Object.defineProperty(Base, 'prototype', { |
| 443 | get: function() { |
| 444 | calls++; |
| 445 | return 42; |
| 446 | }, |
| 447 | configurable: true |
| 448 | }); |
| 449 | assertThrows(function() { |
| 450 | class C extends Base {} |
| 451 | }, TypeError); |
| 452 | assertEquals(1, calls); |
| 453 | })(); |
| 454 | |
| 455 | |
| 456 | (function TestPrototypeSetter() { |
| 457 | var Base = function() {}.bind(); |
| 458 | Object.defineProperty(Base, 'prototype', { |
| 459 | set: function() { |
| 460 | assertUnreachable(); |
| 461 | } |
| 462 | }); |
| 463 | assertThrows(function() { |
| 464 | class C extends Base {} |
| 465 | }, TypeError); |
| 466 | })(); |
| 467 | |
| 468 | |
| 469 | (function TestSuperInMethods() { |
| 470 | class B { |
| 471 | method() { |
| 472 | return 1; |
| 473 | } |
| 474 | get x() { |
| 475 | return 2; |
| 476 | } |
| 477 | } |
| 478 | class C extends B { |
| 479 | method() { |
| 480 | assertEquals(2, super.x); |
| 481 | return super.method(); |
| 482 | } |
| 483 | } |
| 484 | assertEquals(1, new C().method()); |
| 485 | })(); |
| 486 | |
| 487 | |
| 488 | (function TestSuperInGetter() { |
| 489 | class B { |
| 490 | method() { |
| 491 | return 1; |
| 492 | } |
| 493 | get x() { |
| 494 | return 2; |
| 495 | } |
| 496 | } |
| 497 | class C extends B { |
| 498 | get y() { |
| 499 | assertEquals(2, super.x); |
| 500 | return super.method(); |
| 501 | } |
| 502 | } |
| 503 | assertEquals(1, new C().y); |
| 504 | })(); |
| 505 | |
| 506 | |
| 507 | (function TestSuperInSetter() { |
| 508 | class B { |
| 509 | method() { |
| 510 | return 1; |
| 511 | } |
| 512 | get x() { |
| 513 | return 2; |
| 514 | } |
| 515 | } |
| 516 | class C extends B { |
| 517 | set y(v) { |
| 518 | assertEquals(3, v); |
| 519 | assertEquals(2, super.x); |
| 520 | assertEquals(1, super.method()); |
| 521 | } |
| 522 | } |
| 523 | assertEquals(3, new C().y = 3); |
| 524 | })(); |
| 525 | |
| 526 | |
| 527 | (function TestSuperInStaticMethods() { |
| 528 | class B { |
| 529 | static method() { |
| 530 | return 1; |
| 531 | } |
| 532 | static get x() { |
| 533 | return 2; |
| 534 | } |
| 535 | } |
| 536 | class C extends B { |
| 537 | static method() { |
| 538 | assertEquals(2, super.x); |
| 539 | return super.method(); |
| 540 | } |
| 541 | } |
| 542 | assertEquals(1, C.method()); |
| 543 | })(); |
| 544 | |
| 545 | |
| 546 | (function TestSuperInStaticGetter() { |
| 547 | class B { |
| 548 | static method() { |
| 549 | return 1; |
| 550 | } |
| 551 | static get x() { |
| 552 | return 2; |
| 553 | } |
| 554 | } |
| 555 | class C extends B { |
| 556 | static get x() { |
| 557 | assertEquals(2, super.x); |
| 558 | return super.method(); |
| 559 | } |
| 560 | } |
| 561 | assertEquals(1, C.x); |
| 562 | })(); |
| 563 | |
| 564 | |
| 565 | (function TestSuperInStaticSetter() { |
| 566 | class B { |
| 567 | static method() { |
| 568 | return 1; |
| 569 | } |
| 570 | static get x() { |
| 571 | return 2; |
| 572 | } |
| 573 | } |
| 574 | class C extends B { |
| 575 | static set x(v) { |
| 576 | assertEquals(3, v); |
| 577 | assertEquals(2, super.x); |
| 578 | assertEquals(1, super.method()); |
| 579 | } |
| 580 | } |
| 581 | assertEquals(3, C.x = 3); |
| 582 | })(); |
| 583 | |
| 584 | |
| 585 | (function TestNumericPropertyNames() { |
| 586 | class B { |
| 587 | 1() { return 1; } |
| 588 | get 2() { return 2; } |
| 589 | set 3(_) {} |
| 590 | |
| 591 | static 4() { return 4; } |
| 592 | static get 5() { return 5; } |
| 593 | static set 6(_) {} |
| 594 | } |
| 595 | |
| 596 | assertMethodDescriptor(B.prototype, '1'); |
| 597 | assertGetterDescriptor(B.prototype, '2'); |
| 598 | assertSetterDescriptor(B.prototype, '3'); |
| 599 | |
| 600 | assertMethodDescriptor(B, '4'); |
| 601 | assertGetterDescriptor(B, '5'); |
| 602 | assertSetterDescriptor(B, '6'); |
| 603 | |
| 604 | class C extends B { |
| 605 | 1() { return super[1](); } |
| 606 | get 2() { return super[2]; } |
| 607 | |
| 608 | static 4() { return super[4](); } |
| 609 | static get 5() { return super[5]; } |
| 610 | } |
| 611 | |
| 612 | assertEquals(1, new C()[1]()); |
| 613 | assertEquals(2, new C()[2]); |
| 614 | assertEquals(4, C[4]()); |
| 615 | assertEquals(5, C[5]); |
| 616 | })(); |
| 617 | |
| 618 | |
| 619 | (function TestDefaultConstructorNoCrash() { |
| 620 | // Regression test for https://code.google.com/p/v8/issues/detail?id=3661 |
| 621 | class C {} |
| 622 | assertThrows(function () {C();}, TypeError); |
| 623 | assertThrows(function () {C(1);}, TypeError); |
| 624 | assertTrue(new C() instanceof C); |
| 625 | assertTrue(new C(1) instanceof C); |
| 626 | })(); |
| 627 | |
| 628 | |
| 629 | (function TestConstructorCall(){ |
| 630 | var realmIndex = Realm.create(); |
| 631 | var otherTypeError = Realm.eval(realmIndex, "TypeError"); |
| 632 | var A = Realm.eval(realmIndex, '"use strict"; class A {}'); |
| 633 | var instance = new A(); |
| 634 | var constructor = instance.constructor; |
| 635 | var otherTypeError = Realm.eval(realmIndex, 'TypeError'); |
| 636 | if (otherTypeError === TypeError) { |
| 637 | throw Error('Should not happen!'); |
| 638 | } |
| 639 | |
| 640 | // ES6 9.2.1[[Call]] throws a TypeError in the caller context/Realm when the |
| 641 | // called function is a classConstructor |
| 642 | assertThrows(function() { Realm.eval(realmIndex, "A()") }, otherTypeError); |
| 643 | assertThrows(function() { instance.constructor() }, TypeError); |
| 644 | assertThrows(function() { A() }, TypeError); |
| 645 | |
| 646 | // ES6 9.3.1 call() first activates the callee context before invoking the |
| 647 | // method. The TypeError from the constructor is thus thrown in the other |
| 648 | // Realm. |
| 649 | assertThrows(function() { Realm.eval(realmIndex, "A.call()") }, |
| 650 | otherTypeError); |
| 651 | assertThrows(function() { constructor.call() }, otherTypeError); |
| 652 | assertThrows(function() { A.call() }, otherTypeError); |
| 653 | })(); |
| 654 | |
| 655 | |
| 656 | (function TestConstructorCallOptimized() { |
| 657 | class A { }; |
| 658 | |
| 659 | function invoke_constructor() { A() } |
| 660 | function call_constructor() { A.call() } |
| 661 | function apply_constructor() { A.apply() } |
| 662 | |
| 663 | for (var i=0; i<3; i++) { |
| 664 | assertThrows(invoke_constructor); |
| 665 | assertThrows(call_constructor); |
| 666 | assertThrows(apply_constructor); |
| 667 | } |
| 668 | // Make sure we still check for class constructors when calling optimized |
| 669 | // code. |
| 670 | %OptimizeFunctionOnNextCall(invoke_constructor); |
| 671 | assertThrows(invoke_constructor); |
| 672 | %OptimizeFunctionOnNextCall(call_constructor); |
| 673 | assertThrows(call_constructor); |
| 674 | %OptimizeFunctionOnNextCall(apply_constructor); |
| 675 | assertThrows(apply_constructor); |
| 676 | })(); |
| 677 | |
| 678 | |
| 679 | (function TestDefaultConstructor() { |
| 680 | var calls = 0; |
| 681 | class Base { |
| 682 | constructor() { |
| 683 | calls++; |
| 684 | } |
| 685 | } |
| 686 | class Derived extends Base {} |
| 687 | var object = new Derived; |
| 688 | assertEquals(1, calls); |
| 689 | |
| 690 | calls = 0; |
| 691 | assertThrows(function() { Derived(); }, TypeError); |
| 692 | assertEquals(0, calls); |
| 693 | })(); |
| 694 | |
| 695 | |
| 696 | (function TestDefaultConstructorArguments() { |
| 697 | var args, self; |
| 698 | class Base { |
| 699 | constructor() { |
| 700 | self = this; |
| 701 | args = arguments; |
| 702 | } |
| 703 | } |
| 704 | class Derived extends Base {} |
| 705 | |
| 706 | new Derived; |
| 707 | assertEquals(0, args.length); |
| 708 | |
| 709 | new Derived(0, 1, 2); |
| 710 | assertEquals(3, args.length); |
| 711 | assertTrue(self instanceof Derived); |
| 712 | |
| 713 | var arr = new Array(100); |
| 714 | var obj = {}; |
| 715 | assertThrows(function() {Derived.apply(obj, arr);}, TypeError); |
| 716 | })(); |
| 717 | |
| 718 | |
| 719 | (function TestDefaultConstructorArguments2() { |
| 720 | var args; |
| 721 | class Base { |
| 722 | constructor(x, y) { |
| 723 | args = arguments; |
| 724 | } |
| 725 | } |
| 726 | class Derived extends Base {} |
| 727 | |
| 728 | new Derived; |
| 729 | assertEquals(0, args.length); |
| 730 | |
| 731 | new Derived(1); |
| 732 | assertEquals(1, args.length); |
| 733 | assertEquals(1, args[0]); |
| 734 | |
| 735 | new Derived(1, 2, 3); |
| 736 | assertEquals(3, args.length); |
| 737 | assertEquals(1, args[0]); |
| 738 | assertEquals(2, args[1]); |
| 739 | assertEquals(3, args[2]); |
| 740 | })(); |
| 741 | |
| 742 | |
| 743 | (function TestNameBindingConst() { |
| 744 | assertThrows('class C { constructor() { C = 42; } }; new C();', TypeError); |
| 745 | assertThrows('new (class C { constructor() { C = 42; } })', TypeError); |
| 746 | assertThrows('class C { m() { C = 42; } }; new C().m()', TypeError); |
| 747 | assertThrows('new (class C { m() { C = 42; } }).m()', TypeError); |
| 748 | assertThrows('class C { get x() { C = 42; } }; new C().x', TypeError); |
| 749 | assertThrows('(new (class C { get x() { C = 42; } })).x', TypeError); |
| 750 | assertThrows('class C { set x(_) { C = 42; } }; new C().x = 15;', TypeError); |
| 751 | assertThrows('(new (class C { set x(_) { C = 42; } })).x = 15;', TypeError); |
| 752 | })(); |
| 753 | |
| 754 | |
| 755 | (function TestNameBinding() { |
| 756 | var C2; |
| 757 | class C { |
| 758 | constructor() { |
| 759 | C2 = C; |
| 760 | } |
| 761 | m() { |
| 762 | C2 = C; |
| 763 | } |
| 764 | get x() { |
| 765 | C2 = C; |
| 766 | } |
| 767 | set x(_) { |
| 768 | C2 = C; |
| 769 | } |
| 770 | } |
| 771 | new C(); |
| 772 | assertEquals(C, C2); |
| 773 | |
| 774 | C2 = undefined; |
| 775 | new C().m(); |
| 776 | assertEquals(C, C2); |
| 777 | |
| 778 | C2 = undefined; |
| 779 | new C().x; |
| 780 | assertEquals(C, C2); |
| 781 | |
| 782 | C2 = undefined; |
| 783 | new C().x = 1; |
| 784 | assertEquals(C, C2); |
| 785 | })(); |
| 786 | |
| 787 | |
| 788 | (function TestNameBindingExpression() { |
| 789 | var C3; |
| 790 | var C = class C2 { |
| 791 | constructor() { |
| 792 | assertEquals(C2, C); |
| 793 | C3 = C2; |
| 794 | } |
| 795 | m() { |
| 796 | assertEquals(C2, C); |
| 797 | C3 = C2; |
| 798 | } |
| 799 | get x() { |
| 800 | assertEquals(C2, C); |
| 801 | C3 = C2; |
| 802 | } |
| 803 | set x(_) { |
| 804 | assertEquals(C2, C); |
| 805 | C3 = C2; |
| 806 | } |
| 807 | } |
| 808 | new C(); |
| 809 | assertEquals(C, C3); |
| 810 | |
| 811 | C3 = undefined; |
| 812 | new C().m(); |
| 813 | assertEquals(C, C3); |
| 814 | |
| 815 | C3 = undefined; |
| 816 | new C().x; |
| 817 | assertEquals(C, C3); |
| 818 | |
| 819 | C3 = undefined; |
| 820 | new C().x = 1; |
| 821 | assertEquals(C, C3); |
| 822 | })(); |
| 823 | |
| 824 | |
| 825 | (function TestNameBindingInExtendsExpression() { |
| 826 | assertThrows(function() { |
| 827 | class x extends x {} |
| 828 | }, ReferenceError); |
| 829 | |
| 830 | assertThrows(function() { |
| 831 | (class x extends x {}); |
| 832 | }, ReferenceError); |
| 833 | |
| 834 | assertThrows(function() { |
| 835 | var x = (class x extends x {}); |
| 836 | }, ReferenceError); |
| 837 | })(); |
| 838 | |
| 839 | |
| 840 | (function TestThisAccessRestriction() { |
| 841 | class Base {} |
| 842 | (function() { |
| 843 | class C extends Base { |
| 844 | constructor() { |
| 845 | var y; |
| 846 | super(); |
| 847 | } |
| 848 | }; new C(); |
| 849 | }()); |
| 850 | assertThrows(function() { |
| 851 | class C extends Base { |
| 852 | constructor() { |
| 853 | super(this.x); |
| 854 | } |
| 855 | }; new C(); |
| 856 | }, ReferenceError); |
| 857 | assertThrows(function() { |
| 858 | class C extends Base { |
| 859 | constructor() { |
| 860 | super(this); |
| 861 | } |
| 862 | }; new C(); |
| 863 | }, ReferenceError); |
| 864 | assertThrows(function() { |
| 865 | class C extends Base { |
| 866 | constructor() { |
| 867 | super.method(); |
| 868 | super(this); |
| 869 | } |
| 870 | }; new C(); |
| 871 | }, ReferenceError); |
| 872 | assertThrows(function() { |
| 873 | class C extends Base { |
| 874 | constructor() { |
| 875 | super(super.method()); |
| 876 | } |
| 877 | }; new C(); |
| 878 | }, ReferenceError); |
| 879 | assertThrows(function() { |
| 880 | class C extends Base { |
| 881 | constructor() { |
| 882 | super(super()); |
| 883 | } |
| 884 | }; new C(); |
| 885 | }, ReferenceError); |
| 886 | assertThrows(function() { |
| 887 | class C extends Base { |
| 888 | constructor() { |
| 889 | super(1, 2, Object.getPrototypeOf(this)); |
| 890 | } |
| 891 | }; new C(); |
| 892 | }, ReferenceError); |
| 893 | (function() { |
| 894 | class C extends Base { |
| 895 | constructor() { |
| 896 | { super(1, 2); } |
| 897 | } |
| 898 | }; new C(); |
| 899 | }()); |
| 900 | (function() { |
| 901 | class C extends Base { |
| 902 | constructor() { |
| 903 | if (1) super(); |
| 904 | } |
| 905 | }; new C(); |
| 906 | }()); |
| 907 | |
| 908 | class C1 extends Object { |
| 909 | constructor() { |
| 910 | 'use strict'; |
| 911 | super(); |
| 912 | } |
| 913 | }; |
| 914 | new C1(); |
| 915 | |
| 916 | class C2 extends Object { |
| 917 | constructor() { |
| 918 | ; 'use strict';;;;; |
| 919 | super(); |
| 920 | } |
| 921 | }; |
| 922 | new C2(); |
| 923 | |
| 924 | class C3 extends Object { |
| 925 | constructor() { |
| 926 | ; 'use strict';;;;; |
| 927 | // This is a comment. |
| 928 | super(); |
| 929 | } |
| 930 | }; |
| 931 | new C3(); |
| 932 | }()); |
| 933 | |
| 934 | |
| 935 | function testClassRestrictedProperties(C) { |
| 936 | assertEquals(false, C.hasOwnProperty("arguments")); |
| 937 | assertThrows(function() { return C.arguments; }, TypeError); |
| 938 | assertThrows(function() { C.arguments = {}; }, TypeError); |
| 939 | |
| 940 | assertEquals(false, C.hasOwnProperty("caller")); |
| 941 | assertThrows(function() { return C.caller; }, TypeError); |
| 942 | assertThrows(function() { C.caller = {}; }, TypeError); |
| 943 | |
| 944 | assertEquals(false, (new C).method.hasOwnProperty("arguments")); |
| 945 | assertThrows(function() { return new C().method.arguments; }, TypeError); |
| 946 | assertThrows(function() { new C().method.arguments = {}; }, TypeError); |
| 947 | |
| 948 | assertEquals(false, (new C).method.hasOwnProperty("caller")); |
| 949 | assertThrows(function() { return new C().method.caller; }, TypeError); |
| 950 | assertThrows(function() { new C().method.caller = {}; }, TypeError); |
| 951 | } |
| 952 | |
| 953 | |
| 954 | (function testRestrictedPropertiesStrict() { |
| 955 | "use strict"; |
| 956 | class ClassWithDefaultConstructor { |
| 957 | method() {} |
| 958 | } |
| 959 | class Class { |
| 960 | constructor() {} |
| 961 | method() {} |
| 962 | } |
| 963 | class DerivedClassWithDefaultConstructor extends Class {} |
| 964 | class DerivedClass extends Class { constructor() { super(); } } |
| 965 | |
| 966 | testClassRestrictedProperties(ClassWithDefaultConstructor); |
| 967 | testClassRestrictedProperties(Class); |
| 968 | testClassRestrictedProperties(DerivedClassWithDefaultConstructor); |
| 969 | testClassRestrictedProperties(DerivedClass); |
| 970 | testClassRestrictedProperties(class { method() {} }); |
| 971 | testClassRestrictedProperties(class { constructor() {} method() {} }); |
| 972 | testClassRestrictedProperties(class extends Class { }); |
| 973 | testClassRestrictedProperties( |
| 974 | class extends Class { constructor() { super(); } }); |
| 975 | })(); |
| 976 | |
| 977 | |
| 978 | (function testRestrictedPropertiesSloppy() { |
| 979 | class ClassWithDefaultConstructor { |
| 980 | method() {} |
| 981 | } |
| 982 | class Class { |
| 983 | constructor() {} |
| 984 | method() {} |
| 985 | } |
| 986 | class DerivedClassWithDefaultConstructor extends Class {} |
| 987 | class DerivedClass extends Class { constructor() { super(); } } |
| 988 | |
| 989 | testClassRestrictedProperties(ClassWithDefaultConstructor); |
| 990 | testClassRestrictedProperties(Class); |
| 991 | testClassRestrictedProperties(DerivedClassWithDefaultConstructor); |
| 992 | testClassRestrictedProperties(DerivedClass); |
| 993 | testClassRestrictedProperties(class { method() {} }); |
| 994 | testClassRestrictedProperties(class { constructor() {} method() {} }); |
| 995 | testClassRestrictedProperties(class extends Class { }); |
| 996 | testClassRestrictedProperties( |
| 997 | class extends Class { constructor() { super(); } }); |
| 998 | })(); |