blob: 4be1254d676ded9c1561e875c6bfacb779c7a937 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2015 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
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005
6(function TestClass() {
7 'use strict';
8
9 var calls = 0;
10 class Base {
11 constructor(_) {
12 assertEquals(Base, new.target);
13 calls++;
14 }
15 }
16 assertInstanceof(new Base(1), Base);
17 assertInstanceof(new Base(1, 2), Base);
18 assertInstanceof(new Base(), Base);
19 assertEquals(3, calls);
20})();
21
22
23(function TestDerivedClass() {
24 'use strict';
25
26 var calls = 0;
27 class Base {
28 constructor(expected) {
29 assertEquals(expected, new.target);
30 }
31 }
32 class Derived extends Base {
33 constructor(expected) {
34 super(expected);
35 assertEquals(expected, new.target);
36 calls++;
37 }
38 }
39 new Derived(Derived, 'extra');
40 new Derived(Derived);
41 assertEquals(2, calls);
42
43 class Derived2 extends Derived {}
44 calls = 0;
45 new Derived2(Derived2);
46 new Derived2(Derived2, 'extra');
47 assertEquals(2, calls);
48})();
49
50
51(function TestFunctionCall() {
52 var calls;
53 function f(expected) {
54 calls++;
55 assertEquals(expected, new.target);
56 }
57
58 calls = 0;
59 f(undefined);
60 f(undefined, 'extra');
61 f();
62 assertEquals(3, calls);
63
64 calls = 0;
65 f.call({}, undefined);
66 f.call({}, undefined, 'extra');
67 f.call({});
68 assertEquals(3, calls);
69
70 calls = 0;
71 f.apply({}, [undefined]);
72 f.apply({}, [undefined, 'extra']);
73 f.apply({}, []);
74 assertEquals(3, calls);
75})();
76
77
78(function TestFunctionConstruct() {
79 var calls;
80 function f(expected) {
81 calls++;
82 assertEquals(expected, new.target);
83 }
84
85 calls = 0;
86 new f(f);
87 new f(f, 'extra');
88 assertEquals(2, calls);
89})();
90
91
92(function TestClassExtendsFunction() {
93 'use strict';
94
95 var calls = 0;
96 function f(expected) {
97 assertEquals(expected, new.target);
98 }
99 class Derived extends f {
100 constructor(expected) {
101 super(expected);
102 assertEquals(expected, new.target);
103 calls++;
104 }
105 }
106
107 new Derived(Derived);
108 new Derived(Derived, 'extra');
109 assertEquals(2, calls);
110})();
111
112
113(function TestFunctionReturnObject() {
114 function f(expected) {
115 assertEquals(expected, new.target);
116 return /abc/;
117 }
118
119 assertInstanceof(new f(f), RegExp);
120 assertInstanceof(new f(f, 'extra'), RegExp);
121
122 assertInstanceof(f(undefined), RegExp);
123 assertInstanceof(f(), RegExp);
124 assertInstanceof(f(undefined, 'extra'), RegExp);
125})();
126
127
128(function TestClassReturnObject() {
129 'use strict';
130
131 class Base {
132 constructor(expected) {
133 assertEquals(expected, new.target);
134 return /abc/;
135 }
136 }
137
138 assertInstanceof(new Base(Base), RegExp);
139 assertInstanceof(new Base(Base, 'extra'), RegExp);
140
141 class Derived extends Base {}
142 assertInstanceof(new Derived(Derived), RegExp);
143 assertInstanceof(new Derived(Derived, 'extra'), RegExp);
144
145 class Derived2 extends Base {
146 constructor(expected) {
147 super(expected);
148 assertInstanceof(this, RegExp);
149 }
150 }
151 assertInstanceof(new Derived2(Derived2), RegExp);
152 assertInstanceof(new Derived2(Derived2, 'extra'), RegExp);
153})();
154
155
156(function TestReflectConstruct() {
157 var calls = 0;
158 function f(expected) {
159 calls++;
160 assertEquals(expected, new.target);
161 }
162
163 var o = Reflect.construct(f, [f]);
164 assertEquals(Object.getPrototypeOf(o), f.prototype);
165 o = Reflect.construct(f, [f, 'extra']);
166 assertEquals(Object.getPrototypeOf(o), f.prototype);
167 assertEquals(2, calls);
168
169 calls = 0;
170 o = Reflect.construct(f, [f], f);
171 assertEquals(Object.getPrototypeOf(o), f.prototype);
172 o = Reflect.construct(f, [f, 'extra'], f);
173 assertEquals(Object.getPrototypeOf(o), f.prototype);
174 assertEquals(2, calls);
175
176
177 function g() {}
178 calls = 0;
179 o = Reflect.construct(f, [g], g);
180 assertEquals(Object.getPrototypeOf(o), g.prototype);
181 o = Reflect.construct(f, [g, 'extra'], g);
182 assertEquals(Object.getPrototypeOf(o), g.prototype);
183 assertEquals(2, calls);
184})();
185
186
187(function TestRestParametersFunction() {
188 function f(...rest) {
189 assertEquals(rest[0], new.target);
190 }
191
192 assertInstanceof(new f(f), f);
193 assertInstanceof(new f(f, 'extra'), f);
194})();
195
196
197(function TestRestParametersClass() {
198 'use strict';
199
200 class Base {
201 constructor(...rest) {
202 assertEquals(rest[0], new.target);
203 }
204 }
205
206 assertInstanceof(new Base(Base), Base);
207 assertInstanceof(new Base(Base, 'extra'), Base);
208
209 class Derived extends Base {}
210
211 assertInstanceof(new Derived(Derived), Derived);
212 assertInstanceof(new Derived(Derived, 'extra'), Derived);
213})();
214
215
216(function TestArrowFunction() {
217 function f(expected) {
218 (() => {
219 assertEquals(expected, new.target);
220 })();
221 }
222
223 assertInstanceof(new f(f), f);
224 assertInstanceof(new f(f, 'extra'), f);
225})();
226
227
228(function TestRestParametersClass() {
229 'use strict';
230
231 class Base {
232 constructor(expected) {
233 (() => {
234 assertEquals(expected, new.target);
235 })();
236 }
237 }
238
239 assertInstanceof(new Base(Base), Base);
240 assertInstanceof(new Base(Base, 'extra'), Base);
241
242 class Derived extends Base {}
243
244 assertInstanceof(new Derived(Derived), Derived);
245 assertInstanceof(new Derived(Derived, 'extra'), Derived);
246})();
247
248
249(function TestSloppyArguments() {
250 var length, a0, a1, a2, nt;
251 function f(x) {
252 assertEquals(length, arguments.length);
253 assertEquals(a0, arguments[0]);
254 assertEquals(a1, arguments[1]);
255 assertEquals(a2, arguments[2]);
256 assertEquals(nt, new.target);
257
258 if (length > 0) {
259 x = 42;
260 assertEquals(42, x);
261 assertEquals(42, arguments[0]);
262
263 arguments[0] = 33;
264 assertEquals(33, x);
265 assertEquals(33, arguments[0]);
266 }
267 }
268
269 nt = f;
270 length = 0;
271 new f();
272
273 length = 1;
274 a0 = 1;
275 new f(1);
276
277 length = 2;
278 a0 = 1;
279 a1 = 2;
280 new f(1, 2);
281
282 length = 3;
283 a0 = 1;
284 a1 = 2;
285 a2 = 3;
286 new f(1, 2, 3);
287
288 nt = undefined;
289 a0 = a1 = a2 = undefined;
290 length = 0;
291 f();
292
293 length = 1;
294 a0 = 1;
295 f(1);
296
297 length = 2;
298 a0 = 1;
299 a1 = 2;
300 f(1, 2);
301
302 length = 3;
303 a0 = 1;
304 a1 = 2;
305 a2 = 3;
306 f(1, 2, 3);
307})();
308
309
310(function TestStrictArguments() {
311 var length, a0, a1, a2, nt;
312 function f(x) {
313 'use strict';
314 assertEquals(length, arguments.length);
315 assertEquals(a0, arguments[0]);
316 assertEquals(a1, arguments[1]);
317 assertEquals(a2, arguments[2]);
318 assertEquals(nt, new.target);
319
320 if (length > 0) {
321 x = 42;
322 assertEquals(a0, arguments[0]);
323
324 arguments[0] = 33;
325 assertEquals(33, arguments[0]);
326 }
327 }
328
329 nt = f;
330 length = 0;
331 new f();
332
333 length = 1;
334 a0 = 1;
335 new f(1);
336
337 length = 2;
338 a0 = 1;
339 a1 = 2;
340 new f(1, 2);
341
342 length = 3;
343 a0 = 1;
344 a1 = 2;
345 a2 = 3;
346 new f(1, 2, 3);
347
348 nt = undefined;
349 a0 = a1 = a2 = undefined;
350 length = 0;
351 f();
352
353 length = 1;
354 a0 = 1;
355 f(1);
356
357 length = 2;
358 a0 = 1;
359 a1 = 2;
360 f(1, 2);
361
362 length = 3;
363 a0 = 1;
364 a1 = 2;
365 a2 = 3;
366 f(1, 2, 3);
367})();
368
369
370(function TestOtherScopes() {
371 function f1() { return eval("'use strict'; new.target") }
372 assertSame(f1, new f1);
373 function f2() { with ({}) return new.target }
374 assertSame(f2, new f2);
375 function f3({a}) { return new.target }
376 assertSame(f3, new f3({}));
377 function f4(...a) { return new.target }
378 assertSame(f4, new f4);
379 function f5() { 'use strict'; { let x; return new.target } }
380 assertSame(f5, new f5);
381 function f6() { with ({'new.target': 42}) return new.target }
382 assertSame(f6, new f6);
383})();
384
385
386// Has to be top-level to be inlined.
387function get_new_target() { return new.target; }
388(function TestInlining() {
389 "use strict";
390 new function() { assertEquals(undefined, get_new_target()); }
391 new function() { assertEquals(get_new_target, new get_new_target()); }
392
393 class A extends get_new_target {
394 constructor() {
395 var new_target = super();
396 this.new_target = new_target;
397 }
398 }
399 assertEquals(A, new A().new_target);
400})();
401
402
403(function TestEarlyErrors() {
404 assertThrows(function() { Function("new.target = 42"); }, ReferenceError);
405 assertThrows(function() { Function("var foo = 1; new.target = foo = 42"); }, ReferenceError);
406 assertThrows(function() { Function("var foo = 1; foo = new.target = 42"); }, ReferenceError);
407 assertThrows(function() { Function("new.target--"); }, ReferenceError);
408 assertThrows(function() { Function("--new.target"); }, ReferenceError);
409 assertThrows(function() { Function("(new.target)++"); }, ReferenceError);
410 assertThrows(function() { Function("++(new.target)"); }, ReferenceError);
411 assertThrows(function() { Function("for (new.target of {});"); }, SyntaxError);
412})();