Debian Codegraph @

js/src/tests/non262/generators/construct-newtarget.js

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 const GeneratorFunction = function*(){}.constructor;
6
7
8 // Test subclassing %GeneratorFunction% works correctly.
9 class MyGenerator extends GeneratorFunction {}
10
11 var fn = new MyGenerator();
12 assertEq(fn instanceof MyGenerator, true);
13 assertEq(fn instanceof GeneratorFunction, true);
14 assertEq(Object.getPrototypeOf(fn), MyGenerator.prototype);
15
16 fn = Reflect.construct(MyGenerator, []);
17 assertEq(fn instanceof MyGenerator, true);
18 assertEq(fn instanceof GeneratorFunction, true);
19 assertEq(Object.getPrototypeOf(fn), MyGenerator.prototype);
20
21 fn = Reflect.construct(MyGenerator, [], MyGenerator);
22 assertEq(fn instanceof MyGenerator, true);
23 assertEq(fn instanceof GeneratorFunction, true);
24 assertEq(Object.getPrototypeOf(fn), MyGenerator.prototype);
25
26 fn = Reflect.construct(MyGenerator, [], GeneratorFunction);
27 assertEq(fn instanceof MyGenerator, false);
28 assertEq(fn instanceof GeneratorFunction, true);
29 assertEq(Object.getPrototypeOf(fn), GeneratorFunction.prototype);
30
31
32 // Set a different constructor as NewTarget.
33 fn = Reflect.construct(MyGenerator, [], Array);
34 assertEq(fn instanceof MyGenerator, false);
35 assertEq(fn instanceof GeneratorFunction, false);
36 assertEq(Object.getPrototypeOf(fn), Array.prototype);
37
38 fn = Reflect.construct(GeneratorFunction, [], Array);
39 assertEq(fn instanceof GeneratorFunction, false);
40 assertEq(Object.getPrototypeOf(fn), Array.prototype);
41
42
43 // The prototype defaults to %GeneratorFunctionPrototype% if null.
44 function NewTargetNullPrototype() {}
45 NewTargetNullPrototype.prototype = null;
46
47 fn = Reflect.construct(GeneratorFunction, [], NewTargetNullPrototype);
48 assertEq(fn instanceof GeneratorFunction, true);
49 assertEq(Object.getPrototypeOf(fn), GeneratorFunction.prototype);
50
51 fn = Reflect.construct(MyGenerator, [], NewTargetNullPrototype);
52 assertEq(fn instanceof MyGenerator, false);
53 assertEq(fn instanceof GeneratorFunction, true);
54 assertEq(Object.getPrototypeOf(fn), GeneratorFunction.prototype);
55
56
57 // "prototype" property is retrieved exactly once.
58 var trapLog = [], getLog = [];
59 var ProxiedConstructor = new Proxy(GeneratorFunction, new Proxy({
60 get(target, propertyKey, receiver) {
61 getLog.push(propertyKey);
62 return Reflect.get(target, propertyKey, receiver);
63 }
64 }, {
65 get(target, propertyKey, receiver) {
66 trapLog.push(propertyKey);
67 return Reflect.get(target, propertyKey, receiver);
68 }
69 }));
70
71 fn = Reflect.construct(GeneratorFunction, [], ProxiedConstructor);
72 assertEqArray(trapLog, ["get"]);
73 assertEqArray(getLog, ["prototype"]);
74 assertEq(fn instanceof GeneratorFunction, true);
75 assertEq(Object.getPrototypeOf(fn), GeneratorFunction.prototype);
76
77
78 if (typeof reportCompare === "function")
79 reportCompare(0, 0);