Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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-classes | |
| 6 | |
| 7 | |
| 8 (function TestSuperKeyword() { | |
| 9 function Base() { } | |
| 10 function Derived() { | |
| 11 this.derivedDataProperty = "xxx"; | |
| 12 } | |
| 13 Derived.prototype = Object.create(Base.prototype); | |
| 14 | |
| 15 function fBase() { return "Base " + this.toString(); } | |
| 16 | |
| 17 Base.prototype.f = fBase.toMethod(Base.prototype); | |
| 18 | |
| 19 function fDerived() { | |
| 20 assertEquals("Base this is Derived", super.f()); | |
| 21 assertEquals("128 from Base", super[128]()); | |
| 22 assertEquals("128 from Derived", this[128]()); | |
| 23 assertEquals(15, super.x); | |
| 24 assertEquals(27, this.x); | |
| 25 assertEquals(27, super[42]); | |
| 26 assertEquals(33, this[42]); | |
| 27 super.x = 5; | |
| 28 assertEquals(5, Base.prototype.x); | |
| 29 assertEquals(5, super.x); | |
| 30 assertEquals(27, this.x); | |
| 31 super[42] = 6; | |
| 32 assertEquals(6, Base.prototype[42]); | |
| 33 assertEquals(6, super[42]); | |
| 34 assertEquals(33, this[42]); | |
| 35 | |
| 36 return "Derived" | |
| 37 } | |
| 38 | |
| 39 Base.prototype.x = 15; | |
| 40 Base.prototype[42] = 27; | |
| 41 Base.prototype[128] = function() { return "128 from Base"; } | |
| 42 Derived.prototype[128] = function() { return "128 from Derived"; } | |
| 43 Base.prototype.toString = function() { return "this is Base"; }; | |
| 44 Derived.prototype.toString = function() { return "this is Derived"; }; | |
| 45 Derived.prototype.x = 27; | |
| 46 Derived.prototype[42] = 33; | |
| 47 Derived.prototype.f = fDerived.toMethod(Derived.prototype); | |
| 48 | |
| 49 assertEquals("Base this is Base", new Base().f()); | |
| 50 assertEquals("Derived", new Derived().f()); | |
| 51 }()); | |
| 52 | |
| 53 (function TestSuperKeywordNonMethod() { | |
| 54 function f() { | |
| 55 super.unknown(); | |
| 56 } | |
| 57 | |
| 58 function g() { | |
| 59 super['unknown'] = 5; | |
| 60 } | |
| 61 assertThrows(f, ReferenceError); | |
| 62 assertThrows(g, ReferenceError); | |
| 63 }()); | |
| 64 | |
| 65 | |
| 66 (function TestGetter() { | |
| 67 function Base() {} | |
| 68 Base.prototype = { | |
| 69 constructor: Base, | |
| 70 get x() { | |
| 71 return this._x; | |
|
arv (Not doing code reviews)
2014/09/09 22:38:46
maybe assert that this === derived
var derived =
Dmitry Lomov (no reviews)
2014/09/15 12:31:12
Done.
| |
| 72 }, | |
| 73 _x: 'base' | |
| 74 }; | |
| 75 | |
| 76 function Derived() {} | |
| 77 Derived.__proto__ = Base; | |
| 78 Derived.prototype = { | |
| 79 __proto__: Base.prototype, | |
| 80 constructor: Derived, | |
| 81 _x: 'derived' | |
| 82 }; | |
| 83 Derived.prototype.testGetter = function() { | |
| 84 return super.x; | |
| 85 }.toMethod(Derived.prototype); | |
|
arv (Not doing code reviews)
2014/09/09 22:38:46
wrong indentation
Dmitry Lomov (no reviews)
2014/09/15 12:31:12
Done.
| |
| 86 assertEquals('derived', new Derived().testGetter()); | |
| 87 }()); | |
| 88 | |
| 89 /* | |
| 90 * TODO[dslomov]: named stores and keyed loads/stores not implemented yet. | |
| 91 (function TestSetter() { | |
| 92 function Base() {} | |
| 93 Base.prototype = { | |
| 94 constructor: Base, | |
| 95 get x() { | |
| 96 return this._x; | |
| 97 }, | |
| 98 set x(v) { | |
| 99 this._x = v; | |
| 100 }, | |
| 101 _x: 'base' | |
| 102 }; | |
| 103 | |
| 104 function Derived() {} | |
| 105 Derived.__proto__ = Base; | |
| 106 Derived.prototype = { | |
| 107 __proto__: Base.prototype, | |
| 108 constructor: Derived, | |
| 109 _x: 'derived' | |
| 110 }; | |
| 111 Derived.prototype.testSetter = function() { | |
| 112 super.x = 'foobar'; | |
| 113 }.toMethod(Derived.prototype); | |
| 114 var d = new Derived(); | |
| 115 d.testSetter(); | |
| 116 assertEquals('base', Base.prototype._x); | |
| 117 assertEquals('foobar', d._x); | |
| 118 }()); | |
| 119 | |
| 120 | |
| 121 (function TestKeyedGetter() { | |
| 122 function Base() {} | |
| 123 Base.prototype = { | |
| 124 constructor: Base, | |
| 125 _x: 'base' | |
| 126 }; | |
| 127 | |
| 128 Object.defineProperty(Base.prototype, '0', | |
| 129 { get: function() { return this._x; } }); | |
| 130 | |
| 131 function Derived() {} | |
| 132 Derived.__proto__ = Base; | |
| 133 Derived.prototype = { | |
| 134 __proto__: Base.prototype, | |
| 135 constructor: Derived, | |
| 136 _x: 'derived' | |
| 137 }; | |
| 138 Derived.prototype.testGetter = function() { | |
| 139 return super[0]; | |
| 140 }.toMethod(Derived.prototype); | |
| 141 assertEquals('derived', new Derived()[0]); | |
| 142 // assertEquals('derived', new Derived().testGetter()); | |
| 143 }()); | |
| 144 */ | |
| OLD | NEW |