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: --experimental-classes --harmony-classes | 5 // Flags: --experimental-classes --harmony-classes |
6 | 6 |
7 'use strict'; | 7 'use strict'; |
8 (function TestArgumentsAccess() { | 8 |
9 class Base { | 9 class Base { |
10 constructor() { | 10 constructor(a, b) { |
11 assertEquals(2, arguments.length); | 11 let o = new Object(); |
12 assertEquals(1, arguments[0]); | 12 o.prp = a + b; |
13 assertEquals(2, arguments[1]); | 13 return o; |
| 14 } |
| 15 } |
| 16 |
| 17 class Subclass extends Base { |
| 18 constructor(a, b) { |
| 19 var exn; |
| 20 try { |
| 21 this.prp1 = 3; |
| 22 } catch (e) { |
| 23 exn = e; |
14 } | 24 } |
| 25 assertTrue(exn instanceof ReferenceError); |
| 26 super(a, b); |
| 27 assertSame(a + b, this.prp); |
| 28 assertSame(undefined, this.prp1); |
| 29 assertFalse(this.hasOwnProperty("prp1")); |
| 30 return this; |
15 } | 31 } |
| 32 } |
16 | 33 |
17 let b = new Base(1,2); | 34 let b = new Base(1, 2); |
18 | 35 assertSame(3, b.prp); |
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 | 36 |
73 | 37 |
74 let s = new Subclass(2, -1); | 38 let s = new Subclass(2, -1); |
75 assertSame(1, s.prp); | 39 assertSame(1, s.prp); |
76 assertSame(undefined, s.prp1); | 40 assertSame(undefined, s.prp1); |
77 assertFalse(s.hasOwnProperty("prp1")); | 41 assertFalse(s.hasOwnProperty("prp1")); |
78 | 42 |
79 class Subclass2 extends Base { | 43 class Subclass2 extends Base { |
80 constructor(x) { | 44 constructor(x) { |
81 super(1,2); | 45 super(1,2); |
82 | 46 |
83 if (x < 0) return; | 47 if (x < 0) return; |
84 | 48 |
85 let called = false; | 49 let called = false; |
86 function tmp() { called = true; return 3; } | 50 function tmp() { called = true; return 3; } |
87 var exn = null; | 51 var exn = null; |
88 try { | 52 try { |
89 super(tmp(),4); | 53 super(tmp(),4); |
90 } catch (e) { exn = e; } | 54 } catch(e) { exn = e; } |
91 assertTrue(exn instanceof ReferenceError); | 55 assertTrue(exn !== null); |
92 // TODO(dslomov): should be 'true'. | 56 assertFalse(called); |
93 assertFalse(called); | |
94 } | |
95 } | 57 } |
| 58 } |
96 | 59 |
97 var s2 = new Subclass2(1); | 60 var s2 = new Subclass2(1); |
98 assertSame(3, s2.prp); | 61 assertSame(3, s2.prp); |
99 | 62 |
100 var s3 = new Subclass2(-1); | 63 var s3 = new Subclass2(-1); |
101 assertSame(3, s3.prp); | 64 assertSame(3, s3.prp); |
102 | 65 |
103 assertThrows(function() { Subclass.call(new Object(), 1, 2); }, TypeError); | 66 assertThrows(function() { Subclass.call(new Object(), 1, 2); }, TypeError); |
104 assertThrows(function() { Base.call(new Object(), 1, 2); }, TypeError); | 67 assertThrows(function() { Base.call(new Object(), 1, 2); }, TypeError); |
105 | |
106 class BadSubclass extends Base { | |
107 constructor() {} | |
108 } | |
109 | |
110 assertThrows(function() { new BadSubclass(); }, ReferenceError); | |
111 }()); | |
112 | |
113 (function TestPrototypeWiring() { | |
114 class Base { | |
115 constructor(x) { | |
116 this.foobar = x; | |
117 } | |
118 } | |
119 | |
120 class Subclass extends Base { | |
121 constructor(x) { | |
122 super(x); | |
123 } | |
124 } | |
125 | |
126 let s = new Subclass(1); | |
127 assertSame(1, s.foobar); | |
128 assertSame(Subclass.prototype, s.__proto__); | |
129 | |
130 let s1 = new Subclass(1, 2); | |
131 assertSame(1, s1.foobar); | |
132 assertTrue(s1.__proto__ === Subclass.prototype); | |
133 | |
134 let s2 = new Subclass(); | |
135 assertSame(undefined, s2.foobar); | |
136 assertSame(Subclass.prototype, s2.__proto__); | |
137 assertThrows(function() { Subclass(1); }, TypeError); | |
138 assertThrows(function() { Subclass(1,2,3,4); }, TypeError); | |
139 | |
140 class Subclass2 extends Subclass { | |
141 constructor() { | |
142 super(5, 6, 7); | |
143 } | |
144 } | |
145 | |
146 let ss2 = new Subclass2(); | |
147 assertSame(5, ss2.foobar); | |
148 assertSame(Subclass2.prototype, ss2.__proto__); | |
149 | |
150 class Subclass3 extends Base { | |
151 constructor(x,y) { | |
152 super(x + y); | |
153 } | |
154 } | |
155 | |
156 let ss3 = new Subclass3(27,42-27); | |
157 assertSame(42, ss3.foobar); | |
158 assertSame(Subclass3.prototype, ss3.__proto__); | |
159 }()); | |
160 | |
161 (function TestSublclassingBuiltins() { | |
162 class ExtendedUint8Array extends Uint8Array { | |
163 constructor() { | |
164 super(10); | |
165 this[0] = 255; | |
166 this[1] = 0xFFA; | |
167 } | |
168 } | |
169 | |
170 var eua = new ExtendedUint8Array(); | |
171 assertEquals(10, eua.length); | |
172 assertEquals(10, eua.byteLength); | |
173 assertEquals(0xFF, eua[0]); | |
174 assertEquals(0xFA, eua[1]); | |
175 assertTrue(eua.__proto__ === ExtendedUint8Array.prototype); | |
176 assertEquals("[object Uint8Array]", Object.prototype.toString.call(eua)); | |
177 }()); | |
178 | |
179 (function TestSubclassingNull() { | |
180 let N = null; | |
181 | |
182 class Foo extends N { | |
183 constructor(x,y) { | |
184 assertSame(1, x); | |
185 assertSame(2, y); | |
186 return {}; | |
187 } | |
188 } | |
189 | |
190 new Foo(1,2); | |
191 }()); | |
192 | |
193 (function TestSubclassBinding() { | |
194 class Base { | |
195 constructor(x, y) { | |
196 this.x = x; | |
197 this.y = y; | |
198 } | |
199 } | |
200 | |
201 let obj = {}; | |
202 class Subclass extends Base { | |
203 constructor(x,y) { | |
204 super(x,y); | |
205 assertTrue(this !== obj); | |
206 } | |
207 } | |
208 | |
209 let f = Subclass.bind(obj); | |
210 assertThrows(function () { f(1, 2); }, TypeError); | |
211 let s = new f(1, 2); | |
212 assertSame(1, s.x); | |
213 assertSame(2, s.y); | |
214 assertSame(Subclass.prototype, s.__proto__); | |
215 | |
216 let s1 = new f(1); | |
217 assertSame(1, s1.x); | |
218 assertSame(undefined, s1.y); | |
219 assertSame(Subclass.prototype, s1.__proto__); | |
220 | |
221 let g = Subclass.bind(obj, 1); | |
222 assertThrows(function () { g(8); }, TypeError); | |
223 let s2 = new g(8); | |
224 assertSame(1, s2.x); | |
225 assertSame(8, s2.y); | |
226 assertSame(Subclass.prototype, s.__proto__); | |
227 }()); | |
OLD | NEW |