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 --harmony-reflect --harmony-regexp-subclass | 5 // Flags: --allow-natives-syntax --harmony-reflect --harmony-regexp-subclass |
6 // Flags: --expose-gc | 6 // Flags: --expose-gc --strong-mode |
7 | 7 |
8 "use strict"; | 8 "use strict"; |
9 | 9 |
10 | 10 |
11 function checkPrototypeChain(object, constructors) { | 11 function checkPrototypeChain(object, constructors) { |
12 var proto = object.__proto__; | 12 var proto = object.__proto__; |
13 for (var i = 0; i < constructors.length; i++) { | 13 for (var i = 0; i < constructors.length; i++) { |
14 assertEquals(constructors[i].prototype, proto); | 14 assertEquals(constructors[i].prototype, proto); |
15 assertEquals(constructors[i], proto.constructor); | 15 assertEquals(constructors[i], proto.constructor); |
16 proto = proto.__proto__; | 16 proto = proto.__proto__; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 | 71 |
72 gc(); | 72 gc(); |
73 })(); | 73 })(); |
74 | 74 |
75 | 75 |
76 (function() { | 76 (function() { |
77 class A extends Function { | 77 class A extends Function { |
78 constructor(...args) { | 78 constructor(...args) { |
79 assertFalse(new.target === undefined); | 79 assertFalse(new.target === undefined); |
80 super(...args); | 80 super(...args); |
| 81 // Strong functions are not extensible, so don't add fields. |
| 82 if (args[args.length - 1].indexOf("use strong") >= 0) return; |
81 this.a = 42; | 83 this.a = 42; |
82 this.d = 4.2; | 84 this.d = 4.2; |
83 this.o = {foo:153}; | 85 this.o = {foo:153}; |
84 } | 86 } |
85 } | 87 } |
| 88 var sloppy_func = new A(""); |
| 89 var strict_func = new A("'use strict';"); |
| 90 assertNull(sloppy_func.caller); |
| 91 assertThrows("strict_f.caller"); |
| 92 assertNull(Object.getOwnPropertyDescriptor(sloppy_func, "caller").value); |
| 93 assertEquals(undefined, Object.getOwnPropertyDescriptor(strict_func, "caller")
); |
86 | 94 |
87 var o = new A("this.foo = 113;"); | 95 function CheckFunction(func, is_strong) { |
88 assertTrue(o instanceof Object); | 96 assertEquals("function", typeof func); |
89 assertTrue(o instanceof Function); | 97 assertTrue(func instanceof Object); |
90 assertTrue(o instanceof A); | 98 assertTrue(func instanceof Function); |
91 assertEquals("function", typeof o); | 99 assertTrue(func instanceof A); |
92 checkPrototypeChain(o, [A, Function, Object]); | 100 checkPrototypeChain(func, [A, Function, Object]); |
93 assertEquals(42, o.a); | 101 if (!is_strong) { |
94 assertEquals(4.2, o.d); | 102 assertEquals(42, func.a); |
95 assertEquals(153, o.o.foo); | 103 assertEquals(4.2, func.d); |
96 var oo = new o(); | 104 assertEquals(153, func.o.foo); |
97 assertEquals(113, oo.foo); | 105 assertTrue(undefined !== func.prototype); |
| 106 func.prototype.bar = "func.bar"; |
| 107 var obj = new func(); |
| 108 assertTrue(obj instanceof Object); |
| 109 assertTrue(obj instanceof func); |
| 110 assertEquals("object", typeof obj); |
| 111 assertEquals(113, obj.foo); |
| 112 assertEquals("func.bar", obj.bar); |
| 113 delete func.prototype.bar; |
| 114 } |
| 115 } |
98 | 116 |
99 var o1 = new A("return 312;"); | 117 var source = "this.foo = 113;"; |
100 assertTrue(%HaveSameMap(o, o1)); | 118 |
| 119 // Sloppy function |
| 120 var sloppy_func = new A(source); |
| 121 assertTrue(undefined !== sloppy_func.prototype); |
| 122 CheckFunction(sloppy_func, false); |
| 123 |
| 124 var sloppy_func1 = new A("return 312;"); |
| 125 assertTrue(%HaveSameMap(sloppy_func, sloppy_func1)); |
| 126 |
| 127 // Strict function |
| 128 var strict_func = new A("'use strict'; " + source); |
| 129 assertFalse(%HaveSameMap(strict_func, sloppy_func)); |
| 130 CheckFunction(strict_func, false); |
| 131 |
| 132 var strict_func1 = new A("'use strict'; return 312;"); |
| 133 assertTrue(%HaveSameMap(strict_func, strict_func1)); |
| 134 |
| 135 // Strong function |
| 136 var strong_func = new A("'use strong'; " + source); |
| 137 assertFalse(%HaveSameMap(strong_func, sloppy_func)); |
| 138 assertFalse(%HaveSameMap(strong_func, strict_func)); |
| 139 CheckFunction(strong_func, true); |
| 140 |
| 141 var strong_func1 = new A("'use strong'; return 312;"); |
| 142 assertTrue(%HaveSameMap(strong_func, strong_func1)); |
101 | 143 |
102 gc(); | 144 gc(); |
103 })(); | 145 })(); |
104 | 146 |
105 | 147 |
106 (function() { | 148 (function() { |
107 class A extends Boolean { | 149 class A extends Boolean { |
108 constructor(...args) { | 150 constructor(...args) { |
109 assertFalse(new.target === undefined); | 151 assertFalse(new.target === undefined); |
110 super(...args); | 152 super(...args); |
(...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 assertEquals(153, o.o.foo); | 540 assertEquals(153, o.o.foo); |
499 | 541 |
500 var o1 = new A(buffer); | 542 var o1 = new A(buffer); |
501 assertTrue(%HaveSameMap(o, o1)); | 543 assertTrue(%HaveSameMap(o, o1)); |
502 | 544 |
503 gc(); | 545 gc(); |
504 })(); | 546 })(); |
505 | 547 |
506 | 548 |
507 (function() { | 549 (function() { |
508 // TODO(ishell): remove once GeneratorFunction is available. | 550 var GeneratorFunction = (function*() {}).constructor; |
509 var GeneratorFunction = (function*() {}).__proto__.constructor; | |
510 class A extends GeneratorFunction { | 551 class A extends GeneratorFunction { |
511 constructor(...args) { | 552 constructor(...args) { |
512 assertFalse(new.target === undefined); | 553 assertFalse(new.target === undefined); |
513 super(...args); | 554 super(...args); |
| 555 // Strong functions are not extensible, so don't add fields. |
| 556 if (args[args.length - 1].indexOf("use strong") >= 0) return; |
514 this.a = 42; | 557 this.a = 42; |
515 this.d = 4.2; | 558 this.d = 4.2; |
516 this.o = {foo:153}; | 559 this.o = {foo:153}; |
517 } | 560 } |
518 } | 561 } |
519 var generator_func = new A("var index = 0; while (index < 5) { yield ++index;
}"); | 562 var sloppy_func = new A("yield 153;"); |
520 assertTrue(generator_func instanceof Object); | 563 var strict_func = new A("'use strict'; yield 153;"); |
521 assertTrue(generator_func instanceof Function); | 564 // Unfortunately the difference is not observable from outside. |
522 assertTrue(generator_func instanceof GeneratorFunction); | 565 assertThrows("sloppy_func.caller"); |
523 assertTrue(generator_func instanceof A); | 566 assertThrows("strict_f.caller"); |
524 assertEquals("function", typeof generator_func); | 567 assertEquals(undefined, Object.getOwnPropertyDescriptor(sloppy_func, "caller")
); |
525 checkPrototypeChain(generator_func, [A, GeneratorFunction, Function, Object]); | 568 assertEquals(undefined, Object.getOwnPropertyDescriptor(strict_func, "caller")
); |
526 assertEquals(42, generator_func.a); | |
527 assertEquals(4.2, generator_func.d); | |
528 assertEquals(153, generator_func.o.foo); | |
529 | 569 |
530 var o = new generator_func(); | 570 function CheckFunction(func, is_strong) { |
531 assertTrue(o instanceof Object); | 571 assertEquals("function", typeof func); |
532 assertTrue(o instanceof generator_func); | 572 assertTrue(func instanceof Object); |
533 assertEquals("object", typeof o); | 573 assertTrue(func instanceof Function); |
| 574 assertTrue(func instanceof GeneratorFunction); |
| 575 assertTrue(func instanceof A); |
| 576 checkPrototypeChain(func, [A, GeneratorFunction, Function, Object]); |
| 577 if (!is_strong) { |
| 578 assertEquals(42, func.a); |
| 579 assertEquals(4.2, func.d); |
| 580 assertEquals(153, func.o.foo); |
534 | 581 |
535 assertPropertiesEqual({done: false, value: 1}, o.next()); | 582 assertTrue(undefined !== func.prototype); |
536 assertPropertiesEqual({done: false, value: 2}, o.next()); | 583 func.prototype.bar = "func.bar"; |
537 assertPropertiesEqual({done: false, value: 3}, o.next()); | 584 var obj = func(); // Generator object. |
538 assertPropertiesEqual({done: false, value: 4}, o.next()); | 585 assertTrue(obj instanceof Object); |
539 assertPropertiesEqual({done: false, value: 5}, o.next()); | 586 assertTrue(obj instanceof func); |
540 assertPropertiesEqual({done: true, value: undefined}, o.next()); | 587 assertEquals("object", typeof obj); |
| 588 assertEquals("func.bar", obj.bar); |
| 589 delete func.prototype.bar; |
541 | 590 |
542 var generator_func1 = new A("return 0;"); | 591 assertPropertiesEqual({done: false, value: 1}, obj.next()); |
543 assertTrue(%HaveSameMap(generator_func, generator_func1)); | 592 assertPropertiesEqual({done: false, value: 1}, obj.next()); |
| 593 assertPropertiesEqual({done: false, value: 2}, obj.next()); |
| 594 assertPropertiesEqual({done: false, value: 3}, obj.next()); |
| 595 assertPropertiesEqual({done: false, value: 5}, obj.next()); |
| 596 assertPropertiesEqual({done: false, value: 8}, obj.next()); |
| 597 assertPropertiesEqual({done: false, value: 13}, obj.next()); |
| 598 assertPropertiesEqual({done: true, value: undefined}, obj.next()); |
| 599 } |
| 600 } |
| 601 |
| 602 var source = "yield 1; yield 1; yield 2; yield 3; yield 5; yield 8; yield 13;"
; |
| 603 |
| 604 // Sloppy generator function |
| 605 var sloppy_func = new A(source); |
| 606 assertTrue(undefined !== sloppy_func.prototype); |
| 607 CheckFunction(sloppy_func, false); |
| 608 |
| 609 var sloppy_func1 = new A("yield 312;"); |
| 610 assertTrue(%HaveSameMap(sloppy_func, sloppy_func1)); |
| 611 |
| 612 // Strict generator function |
| 613 var strict_func = new A("'use strict'; " + source); |
| 614 assertFalse(%HaveSameMap(strict_func, sloppy_func)); |
| 615 CheckFunction(strict_func, false); |
| 616 |
| 617 var strict_func1 = new A("'use strict'; yield 312;"); |
| 618 assertTrue(%HaveSameMap(strict_func, strict_func1)); |
| 619 |
| 620 // Strong generator function |
| 621 var strong_func = new A("'use strong'; " + source); |
| 622 assertFalse(%HaveSameMap(strong_func, sloppy_func)); |
| 623 assertFalse(%HaveSameMap(strong_func, strict_func)); |
| 624 CheckFunction(strong_func, true); |
| 625 |
| 626 var strong_func1 = new A("'use strong'; yield 312;"); |
| 627 assertTrue(%HaveSameMap(strong_func, strong_func1)); |
544 | 628 |
545 gc(); | 629 gc(); |
546 })(); | 630 })(); |
547 | 631 |
548 | 632 |
549 (function() { | 633 (function() { |
550 class A extends Promise { | 634 class A extends Promise { |
551 constructor(...args) { | 635 constructor(...args) { |
552 assertFalse(new.target === undefined); | 636 assertFalse(new.target === undefined); |
553 super(...args); | 637 super(...args); |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
821 | 905 |
822 Object.defineProperty(pattern, Symbol.match, { | 906 Object.defineProperty(pattern, Symbol.match, { |
823 get() { log.push("match"); f.prototype = p2; return false; }}); | 907 get() { log.push("match"); f.prototype = p2; return false; }}); |
824 | 908 |
825 var o = Reflect.construct(RegExp, [pattern], f); | 909 var o = Reflect.construct(RegExp, [pattern], f); |
826 assertEquals(["match", "tostring"], log); | 910 assertEquals(["match", "tostring"], log); |
827 assertEquals(/biep/, o); | 911 assertEquals(/biep/, o); |
828 assertTrue(o.__proto__ === p2); | 912 assertTrue(o.__proto__ === p2); |
829 assertTrue(f.prototype === p3); | 913 assertTrue(f.prototype === p3); |
830 })(); | 914 })(); |
OLD | NEW |