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