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'use strict'; 6(function TestArgumentsAccess() { 7 class Base { 8 constructor() { 9 assertEquals(2, arguments.length); 10 assertEquals(1, arguments[0]); 11 assertEquals(2, arguments[1]); 12 } 13 } 14 15 let b = new Base(1,2); 16 17 class Subclass extends Base { 18 constructor() { 19 assertEquals(2, arguments.length); 20 assertEquals(3, arguments[0]); 21 assertEquals(4, arguments[1]); 22 super(1,2); 23 } 24 } 25 26 let s = new Subclass(3,4); 27 assertEquals(0, Subclass.length); 28 29 class Subclass2 extends Base { 30 constructor(x,y) { 31 assertEquals(2, arguments.length); 32 assertEquals(3, arguments[0]); 33 assertEquals(4, arguments[1]); 34 super(1,2); 35 } 36 } 37 38 let s2 = new Subclass2(3,4); 39 assertEquals(2, Subclass2.length); 40}()); 41 42(function TestThisAccessRestriction() { 43 class Base { 44 constructor(a, b) { 45 let o = new Object(); 46 o.prp = a + b; 47 return o; 48 } 49 } 50 51 class Subclass extends Base { 52 constructor(a, b) { 53 var exn; 54 try { 55 this.prp1 = 3; 56 } catch (e) { 57 exn = e; 58 } 59 assertTrue(exn instanceof ReferenceError); 60 super(a, b); 61 assertSame(a + b, this.prp); 62 assertSame(undefined, this.prp1); 63 assertFalse(this.hasOwnProperty("prp1")); 64 return this; 65 } 66 } 67 68 let b = new Base(1, 2); 69 assertSame(3, b.prp); 70 71 72 let s = new Subclass(2, -1); 73 assertSame(1, s.prp); 74 assertSame(undefined, s.prp1); 75 assertFalse(s.hasOwnProperty("prp1")); 76 77 class Subclass2 extends Base { 78 constructor(x) { 79 super(1,2); 80 81 if (x < 0) return; 82 83 let called = false; 84 function tmp() { called = true; return 3; } 85 var exn = null; 86 try { 87 super(tmp(),4); 88 } catch (e) { exn = e; } 89 assertTrue(exn instanceof ReferenceError); 90 assertTrue(called); 91 } 92 } 93 94 var s2 = new Subclass2(1); 95 assertSame(3, s2.prp); 96 97 var s3 = new Subclass2(-1); 98 assertSame(3, s3.prp); 99 100 assertThrows(function() { Subclass.call(new Object(), 1, 2); }, TypeError); 101 assertThrows(function() { Base.call(new Object(), 1, 2); }, TypeError); 102 103 class BadSubclass extends Base { 104 constructor() {} 105 } 106 107 assertThrows(function() { new BadSubclass(); }, ReferenceError); 108}()); 109 110(function TestThisCheckOrdering() { 111 let baseCalled = 0; 112 class Base { 113 constructor() { baseCalled++ } 114 } 115 116 let fCalled = 0; 117 function f() { fCalled++; return 3; } 118 119 class Subclass1 extends Base { 120 constructor() { 121 baseCalled = 0; 122 super(); 123 assertEquals(1, baseCalled); 124 let obj = this; 125 126 let exn = null; 127 baseCalled = 0; 128 fCalled = 0; 129 try { 130 super(f()); 131 } catch (e) { exn = e; } 132 assertTrue(exn instanceof ReferenceError); 133 assertEquals(1, fCalled); 134 assertEquals(1, baseCalled); 135 assertSame(obj, this); 136 137 exn = null; 138 baseCalled = 0; 139 fCalled = 0; 140 try { 141 super(super(), f()); 142 } catch (e) { exn = e; } 143 assertTrue(exn instanceof ReferenceError); 144 assertEquals(0, fCalled); 145 assertEquals(1, baseCalled); 146 assertSame(obj, this); 147 148 exn = null; 149 baseCalled = 0; 150 fCalled = 0; 151 try { 152 super(f(), super()); 153 } catch (e) { exn = e; } 154 assertTrue(exn instanceof ReferenceError); 155 assertEquals(1, fCalled); 156 assertEquals(1, baseCalled); 157 assertSame(obj, this); 158 } 159 } 160 161 new Subclass1(); 162}()); 163 164 165(function TestPrototypeWiring() { 166 class Base { 167 constructor(x) { 168 this.foobar = x; 169 } 170 } 171 172 class Subclass extends Base { 173 constructor(x) { 174 super(x); 175 } 176 } 177 178 let s = new Subclass(1); 179 assertSame(1, s.foobar); 180 assertSame(Subclass.prototype, s.__proto__); 181 182 let s1 = new Subclass(1, 2); 183 assertSame(1, s1.foobar); 184 assertTrue(s1.__proto__ === Subclass.prototype); 185 186 let s2 = new Subclass(); 187 assertSame(undefined, s2.foobar); 188 assertSame(Subclass.prototype, s2.__proto__); 189 assertThrows(function() { Subclass(1); }, TypeError); 190 assertThrows(function() { Subclass(1,2,3,4); }, TypeError); 191 192 class Subclass2 extends Subclass { 193 constructor() { 194 super(5, 6, 7); 195 } 196 } 197 198 let ss2 = new Subclass2(); 199 assertSame(5, ss2.foobar); 200 assertSame(Subclass2.prototype, ss2.__proto__); 201 202 class Subclass3 extends Base { 203 constructor(x,y) { 204 super(x + y); 205 } 206 } 207 208 let ss3 = new Subclass3(27,42-27); 209 assertSame(42, ss3.foobar); 210 assertSame(Subclass3.prototype, ss3.__proto__); 211}()); 212 213(function TestSublclassingBuiltins() { 214 class ExtendedUint8Array extends Uint8Array { 215 constructor() { 216 super(10); 217 this[0] = 255; 218 this[1] = 0xFFA; 219 } 220 } 221 222 var eua = new ExtendedUint8Array(); 223 assertEquals(10, eua.length); 224 assertEquals(10, eua.byteLength); 225 assertEquals(0xFF, eua[0]); 226 assertEquals(0xFA, eua[1]); 227 assertSame(ExtendedUint8Array.prototype, eua.__proto__); 228 assertEquals("[object Uint8Array]", Object.prototype.toString.call(eua)); 229}()); 230 231(function TestSubclassingNull() { 232 let N = null; 233 234 class Foo extends N { 235 constructor(x,y) { 236 assertSame(1, x); 237 assertSame(2, y); 238 return {}; 239 } 240 } 241 242 new Foo(1,2); 243}()); 244 245(function TestSubclassBinding() { 246 class Base { 247 constructor(x, y) { 248 this.x = x; 249 this.y = y; 250 } 251 } 252 253 let obj = {}; 254 class Subclass extends Base { 255 constructor(x,y) { 256 super(x,y); 257 assertTrue(this !== obj); 258 } 259 } 260 261 let f = Subclass.bind(obj); 262 assertThrows(function () { f(1, 2); }, TypeError); 263 let s = new f(1, 2); 264 assertSame(1, s.x); 265 assertSame(2, s.y); 266 assertSame(Subclass.prototype, s.__proto__); 267 268 let s1 = new f(1); 269 assertSame(1, s1.x); 270 assertSame(undefined, s1.y); 271 assertSame(Subclass.prototype, s1.__proto__); 272 273 let g = Subclass.bind(obj, 1); 274 assertThrows(function () { g(8); }, TypeError); 275 let s2 = new g(8); 276 assertSame(1, s2.x); 277 assertSame(8, s2.y); 278 assertSame(Subclass.prototype, s.__proto__); 279}()); 280 281 282(function TestDefaultConstructor() { 283 class Base1 { } 284 assertThrows(function() { Base1(); }, TypeError); 285 286 class Subclass1 extends Base1 { } 287 288 assertThrows(function() { Subclass1(); }, TypeError); 289 290 let s1 = new Subclass1(); 291 assertSame(s1.__proto__, Subclass1.prototype); 292 293 class Base2 { 294 constructor(x, y) { 295 this.x = x; 296 this.y = y; 297 } 298 } 299 300 class Subclass2 extends Base2 {}; 301 302 let s2 = new Subclass2(1, 2); 303 304 assertSame(s2.__proto__, Subclass2.prototype); 305 assertSame(1, s2.x); 306 assertSame(2, s2.y); 307 308 let f = Subclass2.bind({}, 3, 4); 309 let s2prime = new f(); 310 assertSame(s2prime.__proto__, Subclass2.prototype); 311 assertSame(3, s2prime.x); 312 assertSame(4, s2prime.y); 313 314 let obj = {}; 315 class Base3 { 316 constructor() { 317 return obj; 318 } 319 } 320 321 class Subclass3 extends Base3 {}; 322 323 let s3 = new Subclass3(); 324 assertSame(obj, s3); 325 326 class ExtendedUint8Array extends Uint8Array { } 327 328 var eua = new ExtendedUint8Array(10); 329 assertEquals(10, eua.length); 330 assertEquals(10, eua.byteLength); 331 eua[0] = 0xFF; 332 eua[1] = 0xFFA; 333 assertEquals(0xFF, eua[0]); 334 assertEquals(0xFA, eua[1]); 335 assertSame(ExtendedUint8Array.prototype, eua.__proto__); 336 assertEquals("[object Uint8Array]", Object.prototype.toString.call(eua)); 337}()); 338