Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Side by Side Diff: test/mjsunit/es6/classes-subclass-builtins.js

Issue 1510753005: Fix Function subclassing. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Adressing comments Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/transitions-inl.h ('k') | test/mjsunit/harmony/reflect-construct.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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) {
83 assertThrows(()=>{ this.a = 10; }, TypeError);
84 return;
85 }
81 this.a = 42; 86 this.a = 42;
82 this.d = 4.2; 87 this.d = 4.2;
83 this.o = {foo:153}; 88 this.o = {foo:153};
84 } 89 }
85 } 90 }
91 var sloppy_func = new A("");
92 var strict_func = new A("'use strict';");
93 assertNull(sloppy_func.caller);
94 assertThrows("strict_f.caller");
95 assertNull(Object.getOwnPropertyDescriptor(sloppy_func, "caller").value);
96 assertEquals(undefined, Object.getOwnPropertyDescriptor(strict_func, "caller") );
86 97
87 var o = new A("this.foo = 113;"); 98 function CheckFunction(func, is_strong) {
88 assertTrue(o instanceof Object); 99 assertEquals("function", typeof func);
89 assertTrue(o instanceof Function); 100 assertTrue(func instanceof Object);
90 assertTrue(o instanceof A); 101 assertTrue(func instanceof Function);
91 assertEquals("function", typeof o); 102 assertTrue(func instanceof A);
92 checkPrototypeChain(o, [A, Function, Object]); 103 checkPrototypeChain(func, [A, Function, Object]);
93 assertEquals(42, o.a); 104 if (!is_strong) {
94 assertEquals(4.2, o.d); 105 assertEquals(42, func.a);
95 assertEquals(153, o.o.foo); 106 assertEquals(4.2, func.d);
96 var oo = new o(); 107 assertEquals(153, func.o.foo);
97 assertEquals(113, oo.foo); 108 assertTrue(undefined !== func.prototype);
109 func.prototype.bar = "func.bar";
110 var obj = new func();
111 assertTrue(obj instanceof Object);
112 assertTrue(obj instanceof func);
113 assertEquals("object", typeof obj);
114 assertEquals(113, obj.foo);
115 assertEquals("func.bar", obj.bar);
116 delete func.prototype.bar;
117 }
118 }
98 119
99 var o1 = new A("return 312;"); 120 var source = "this.foo = 113;";
100 assertTrue(%HaveSameMap(o, o1)); 121
122 // Sloppy function
123 var sloppy_func = new A(source);
124 assertTrue(undefined !== sloppy_func.prototype);
125 CheckFunction(sloppy_func, false);
126
127 var sloppy_func1 = new A("return 312;");
128 assertTrue(%HaveSameMap(sloppy_func, sloppy_func1));
129
130 // Strict function
131 var strict_func = new A("'use strict'; " + source);
132 assertFalse(%HaveSameMap(strict_func, sloppy_func));
133 CheckFunction(strict_func, false);
134
135 var strict_func1 = new A("'use strict'; return 312;");
136 assertTrue(%HaveSameMap(strict_func, strict_func1));
137
138 // Strong function
139 var strong_func = new A("'use strong'; " + source);
140 assertFalse(%HaveSameMap(strong_func, sloppy_func));
141 assertFalse(%HaveSameMap(strong_func, strict_func));
142 CheckFunction(strong_func, true);
143
144 var strong_func1 = new A("'use strong'; return 312;");
145 assertTrue(%HaveSameMap(strong_func, strong_func1));
101 146
102 gc(); 147 gc();
103 })(); 148 })();
104 149
105 150
106 (function() { 151 (function() {
107 class A extends Boolean { 152 class A extends Boolean {
108 constructor(...args) { 153 constructor(...args) {
109 assertFalse(new.target === undefined); 154 assertFalse(new.target === undefined);
110 super(...args); 155 super(...args);
(...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after
498 assertEquals(153, o.o.foo); 543 assertEquals(153, o.o.foo);
499 544
500 var o1 = new A(buffer); 545 var o1 = new A(buffer);
501 assertTrue(%HaveSameMap(o, o1)); 546 assertTrue(%HaveSameMap(o, o1));
502 547
503 gc(); 548 gc();
504 })(); 549 })();
505 550
506 551
507 (function() { 552 (function() {
508 // TODO(ishell): remove once GeneratorFunction is available. 553 var GeneratorFunction = (function*() {}).constructor;
509 var GeneratorFunction = (function*() {}).__proto__.constructor;
510 class A extends GeneratorFunction { 554 class A extends GeneratorFunction {
511 constructor(...args) { 555 constructor(...args) {
512 assertFalse(new.target === undefined); 556 assertFalse(new.target === undefined);
513 super(...args); 557 super(...args);
558 // Strong functions are not extensible, so don't add fields.
559 if (args[args.length - 1].indexOf("use strong") >= 0) {
560 assertThrows(()=>{ this.a = 10; }, TypeError);
561 return;
562 }
514 this.a = 42; 563 this.a = 42;
515 this.d = 4.2; 564 this.d = 4.2;
516 this.o = {foo:153}; 565 this.o = {foo:153};
517 } 566 }
518 } 567 }
519 var generator_func = new A("var index = 0; while (index < 5) { yield ++index; }"); 568 var sloppy_func = new A("yield 153;");
520 assertTrue(generator_func instanceof Object); 569 var strict_func = new A("'use strict'; yield 153;");
521 assertTrue(generator_func instanceof Function); 570 // Unfortunately the difference is not observable from outside.
522 assertTrue(generator_func instanceof GeneratorFunction); 571 assertThrows("sloppy_func.caller");
523 assertTrue(generator_func instanceof A); 572 assertThrows("strict_f.caller");
524 assertEquals("function", typeof generator_func); 573 assertEquals(undefined, Object.getOwnPropertyDescriptor(sloppy_func, "caller") );
525 checkPrototypeChain(generator_func, [A, GeneratorFunction, Function, Object]); 574 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 575
530 var o = new generator_func(); 576 function CheckFunction(func, is_strong) {
531 assertTrue(o instanceof Object); 577 assertEquals("function", typeof func);
532 assertTrue(o instanceof generator_func); 578 assertTrue(func instanceof Object);
533 assertEquals("object", typeof o); 579 assertTrue(func instanceof Function);
580 assertTrue(func instanceof GeneratorFunction);
581 assertTrue(func instanceof A);
582 checkPrototypeChain(func, [A, GeneratorFunction, Function, Object]);
583 if (!is_strong) {
584 assertEquals(42, func.a);
585 assertEquals(4.2, func.d);
586 assertEquals(153, func.o.foo);
534 587
535 assertPropertiesEqual({done: false, value: 1}, o.next()); 588 assertTrue(undefined !== func.prototype);
536 assertPropertiesEqual({done: false, value: 2}, o.next()); 589 func.prototype.bar = "func.bar";
537 assertPropertiesEqual({done: false, value: 3}, o.next()); 590 var obj = func(); // Generator object.
538 assertPropertiesEqual({done: false, value: 4}, o.next()); 591 assertTrue(obj instanceof Object);
539 assertPropertiesEqual({done: false, value: 5}, o.next()); 592 assertTrue(obj instanceof func);
540 assertPropertiesEqual({done: true, value: undefined}, o.next()); 593 assertEquals("object", typeof obj);
594 assertEquals("func.bar", obj.bar);
595 delete func.prototype.bar;
541 596
542 var generator_func1 = new A("return 0;"); 597 assertPropertiesEqual({done: false, value: 1}, obj.next());
543 assertTrue(%HaveSameMap(generator_func, generator_func1)); 598 assertPropertiesEqual({done: false, value: 1}, obj.next());
599 assertPropertiesEqual({done: false, value: 2}, obj.next());
600 assertPropertiesEqual({done: false, value: 3}, obj.next());
601 assertPropertiesEqual({done: false, value: 5}, obj.next());
602 assertPropertiesEqual({done: false, value: 8}, obj.next());
603 assertPropertiesEqual({done: true, value: undefined}, obj.next());
604 }
605 }
606
607 var source = "yield 1; yield 1; yield 2; yield 3; yield 5; yield 8;";
608
609 // Sloppy generator function
610 var sloppy_func = new A(source);
611 assertTrue(undefined !== sloppy_func.prototype);
612 CheckFunction(sloppy_func, false);
613
614 var sloppy_func1 = new A("yield 312;");
615 assertTrue(%HaveSameMap(sloppy_func, sloppy_func1));
616
617 // Strict generator function
618 var strict_func = new A("'use strict'; " + source);
619 assertFalse(%HaveSameMap(strict_func, sloppy_func));
620 CheckFunction(strict_func, false);
621
622 var strict_func1 = new A("'use strict'; yield 312;");
623 assertTrue(%HaveSameMap(strict_func, strict_func1));
624
625 // Strong generator function
626 var strong_func = new A("'use strong'; " + source);
627 assertFalse(%HaveSameMap(strong_func, sloppy_func));
628 assertFalse(%HaveSameMap(strong_func, strict_func));
629 CheckFunction(strong_func, true);
630
631 var strong_func1 = new A("'use strong'; yield 312;");
632 assertTrue(%HaveSameMap(strong_func, strong_func1));
544 633
545 gc(); 634 gc();
546 })(); 635 })();
547 636
548 637
549 (function() { 638 (function() {
550 class A extends Promise { 639 class A extends Promise {
551 constructor(...args) { 640 constructor(...args) {
552 assertFalse(new.target === undefined); 641 assertFalse(new.target === undefined);
553 super(...args); 642 super(...args);
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
821 910
822 Object.defineProperty(pattern, Symbol.match, { 911 Object.defineProperty(pattern, Symbol.match, {
823 get() { log.push("match"); f.prototype = p2; return false; }}); 912 get() { log.push("match"); f.prototype = p2; return false; }});
824 913
825 var o = Reflect.construct(RegExp, [pattern], f); 914 var o = Reflect.construct(RegExp, [pattern], f);
826 assertEquals(["match", "tostring"], log); 915 assertEquals(["match", "tostring"], log);
827 assertEquals(/biep/, o); 916 assertEquals(/biep/, o);
828 assertTrue(o.__proto__ === p2); 917 assertTrue(o.__proto__ === p2);
829 assertTrue(f.prototype === p3); 918 assertTrue(f.prototype === p3);
830 })(); 919 })();
OLDNEW
« no previous file with comments | « src/transitions-inl.h ('k') | test/mjsunit/harmony/reflect-construct.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698