OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 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: --harmony-classes | 5 // Flags: --harmony-classes |
6 | 6 |
7 | 7 |
8 (function TestSuperNamedLoads() { | 8 (function TestSuperNamedLoads() { |
9 function Base() { } | 9 function Base() { } |
10 function Derived() { | 10 function Derived() { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 Base.prototype.toString = function() { return "this is Base"; }; | 65 Base.prototype.toString = function() { return "this is Base"; }; |
66 Derived.prototype.toString = function() { return "this is Derived"; }; | 66 Derived.prototype.toString = function() { return "this is Derived"; }; |
67 Derived.prototype[x] = 27; | 67 Derived.prototype[x] = 27; |
68 Derived.prototype[f] = fDerived.toMethod(Derived.prototype); | 68 Derived.prototype[f] = fDerived.toMethod(Derived.prototype); |
69 | 69 |
70 assertEquals("Base this is Base", new Base().f()); | 70 assertEquals("Base this is Base", new Base().f()); |
71 assertEquals("Derived", new Derived().f()); | 71 assertEquals("Derived", new Derived().f()); |
72 }()); | 72 }()); |
73 | 73 |
74 | 74 |
| 75 (function TestSuperNumericKeyedLoads() { |
| 76 var x = 1; |
| 77 var derivedDataProperty = 2; |
| 78 var f = 3; |
| 79 function Base() { } |
| 80 function Derived() { |
| 81 this[derivedDataProperty] = 'xxx'; |
| 82 } |
| 83 Derived.prototype = Object.create(Base.prototype); |
| 84 |
| 85 function fBase() { return "Base " + this.toString(); } |
| 86 |
| 87 Base.prototype[f] = fBase.toMethod(Base.prototype); |
| 88 |
| 89 function fDerived() { |
| 90 assertEquals("Base this is Derived", super[f]()); |
| 91 var a = super[x]; |
| 92 assertEquals(15, a); |
| 93 assertEquals(15, super[x]); |
| 94 assertEquals(27, this[x]); |
| 95 |
| 96 return "Derived" |
| 97 } |
| 98 |
| 99 Base.prototype[x] = 15; |
| 100 Base.prototype.toString = function() { return "this is Base"; }; |
| 101 Derived.prototype.toString = function() { return "this is Derived"; }; |
| 102 Derived.prototype[x] = 27; |
| 103 Derived.prototype[f] = fDerived.toMethod(Derived.prototype); |
| 104 |
| 105 assertEquals("Base this is Base", new Base()[f]()); |
| 106 assertEquals("Derived", new Derived()[f]()); |
| 107 }()); |
| 108 |
| 109 |
75 (function TestSuperKeywordNonMethod() { | 110 (function TestSuperKeywordNonMethod() { |
76 function f() { | 111 function f() { |
77 super.unknown(); | 112 super.unknown(); |
78 } | 113 } |
79 | 114 |
80 assertThrows(f, ReferenceError); | 115 assertThrows(f, ReferenceError); |
81 }()); | 116 }()); |
82 | 117 |
83 | 118 |
84 (function TestGetter() { | 119 (function TestGetter() { |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 var ex = null; | 195 var ex = null; |
161 try { | 196 try { |
162 super[oThrowsInToString]; | 197 super[oThrowsInToString]; |
163 } catch(e) { ex = e } | 198 } catch(e) { ex = e } |
164 assertEquals(eToThrow, ex); | 199 assertEquals(eToThrow, ex); |
165 | 200 |
166 var oReturnsNumericString = { toString: function() { | 201 var oReturnsNumericString = { toString: function() { |
167 return "1"; | 202 return "1"; |
168 } }; | 203 } }; |
169 | 204 |
170 ex = null; | 205 assertEquals(undefined, super[oReturnsNumericString]); |
171 try { | 206 assertEquals(undefined, super[1]); |
172 super[oReturnsNumericString]; | |
173 } catch(e) { ex = e } | |
174 assertTrue(ex instanceof ReferenceError); | |
175 | |
176 ex = null; | |
177 try { | |
178 super[1]; // Indexed properties unsupported yet. | |
179 } catch (e) { ex = e; } | |
180 assertTrue(ex instanceof ReferenceError); | |
181 }.toMethod(Derived.prototype); | 207 }.toMethod(Derived.prototype); |
182 derived = new Derived(); | 208 derived = new Derived(); |
183 assertEquals('derived', derived.testGetter()); | 209 assertEquals('derived', derived.testGetter()); |
| 210 derived = new Derived(); |
| 211 assertEquals('derived', derived.testGetterStrict()); |
| 212 derived = new Derived(); |
| 213 derived.testGetterWithToString(); |
| 214 }()); |
| 215 |
| 216 |
| 217 (function TestGetterNumericKeyed() { |
| 218 var x = 42; |
| 219 function Base() {} |
| 220 var derived; |
| 221 Base.prototype = { |
| 222 constructor: Base, |
| 223 _x: 'base' |
| 224 }; |
| 225 |
| 226 Object.defineProperty(Base.prototype, x, { get: function() { |
| 227 assertSame(this, derived); |
| 228 return this._x; |
| 229 }}); |
| 230 |
| 231 function Derived() {} |
| 232 Derived.__proto__ = Base; |
| 233 Derived.prototype = { |
| 234 __proto__: Base.prototype, |
| 235 constructor: Derived, |
| 236 _x: 'derived' |
| 237 }; |
| 238 Derived.prototype.testGetter = function() { |
| 239 return super[x]; |
| 240 }.toMethod(Derived.prototype); |
| 241 Derived.prototype.testGetterStrict = function() { |
| 242 'use strict'; |
| 243 return super[x]; |
| 244 }.toMethod(Derived.prototype); |
| 245 |
| 246 Derived.prototype.testGetterWithToString = function() { |
| 247 var toStringCalled; |
| 248 var o = { toString: function() { |
| 249 toStringCalled++; |
| 250 return '42'; |
| 251 } }; |
| 252 |
| 253 toStringCalled = 0; |
| 254 assertEquals('derived', super[o]); |
| 255 assertEquals(1, toStringCalled); |
| 256 |
| 257 var eToThrow = new Error(); |
| 258 var oThrowsInToString = { toString: function() { |
| 259 throw eToThrow; |
| 260 } }; |
| 261 |
| 262 var ex = null; |
| 263 try { |
| 264 super[oThrowsInToString]; |
| 265 } catch(e) { ex = e } |
| 266 assertEquals(eToThrow, ex); |
| 267 |
| 268 var oReturnsNumericString = { toString: function() { |
| 269 return "42"; |
| 270 } }; |
| 271 |
| 272 assertEquals('derived', super[oReturnsNumericString]); |
| 273 assertEquals('derived', super[42]); |
| 274 }.toMethod(Derived.prototype); |
| 275 derived = new Derived(); |
| 276 assertEquals('derived', derived.testGetter()); |
184 derived = new Derived(); | 277 derived = new Derived(); |
185 assertEquals('derived', derived.testGetterStrict()); | 278 assertEquals('derived', derived.testGetterStrict()); |
186 derived = new Derived(); | 279 derived = new Derived(); |
187 derived.testGetterWithToString(); | 280 derived.testGetterWithToString(); |
188 }()); | 281 }()); |
189 | 282 |
190 | 283 |
191 (function TestSetter() { | 284 (function TestSetter() { |
192 function Base() {} | 285 function Base() {} |
193 Base.prototype = { | 286 Base.prototype = { |
(...skipping 895 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1089 this.fromDerived = v2; | 1182 this.fromDerived = v2; |
1090 }); | 1183 }); |
1091 | 1184 |
1092 var d = new Derived2("base", "derived"); | 1185 var d = new Derived2("base", "derived"); |
1093 assertEquals("base", d.fromBase); | 1186 assertEquals("base", d.fromBase); |
1094 assertEquals("derived", d.fromDerived); | 1187 assertEquals("derived", d.fromDerived); |
1095 }()); | 1188 }()); |
1096 | 1189 |
1097 | 1190 |
1098 (function TestUnsupportedCases() { | 1191 (function TestUnsupportedCases() { |
1099 function f1(x) { return super[x]; } | 1192 function f(x) { super[x] = 5; } |
1100 function f2(x) { super[x] = 5; } | |
1101 var o = {}; | 1193 var o = {}; |
1102 assertThrows(function(){f1.toMethod(o)(15);}, ReferenceError); | 1194 assertThrows(function(){f.toMethod(o)(15);}, ReferenceError); |
1103 assertThrows(function(){f2.toMethod(o)(15);}, ReferenceError); | |
1104 }()); | 1195 }()); |
OLD | NEW |