Debian Codegraph @

js/src/tests/non262/async-functions/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 AsyncFunction = async function(){}.constructor;
6
7
8 // Test subclassing %AsyncFunction% works correctly.
9 class MyAsync extends AsyncFunction {}
10
11 var fn = new MyAsync();
12 assertEq(fn instanceof MyAsync, true);
13 assertEq(fn instanceof AsyncFunction, true);
14 assertEq(Object.getPrototypeOf(fn), MyAsync.prototype);
15
16 fn = Reflect.construct(MyAsync, []);
17 assertEq(fn instanceof MyAsync, true);
18 assertEq(fn instanceof AsyncFunction, true);
19 assertEq(Object.getPrototypeOf(fn), MyAsync.prototype);
20
21 fn = Reflect.construct(MyAsync, [], MyAsync);
22 assertEq(fn instanceof MyAsync, true);
23 assertEq(fn instanceof AsyncFunction, true);
24 assertEq(Object.getPrototypeOf(fn), MyAsync.prototype);
25
26 fn = Reflect.construct(MyAsync, [], AsyncFunction);
27 assertEq(fn instanceof MyAsync, false);
28 assertEq(fn instanceof AsyncFunction, true);
29 assertEq(Object.getPrototypeOf(fn), AsyncFunction.prototype);
30
31
32 // Set a different constructor as NewTarget.
33 fn = Reflect.construct(MyAsync, [], Array);
34 assertEq(fn instanceof MyAsync, false);
35 assertEq(fn instanceof AsyncFunction, false);
36 assertEq(Object.getPrototypeOf(fn), Array.prototype);
37
38 fn = Reflect.construct(AsyncFunction, [], Array);
39 assertEq(fn instanceof AsyncFunction, false);
40 assertEq(Object.getPrototypeOf(fn), Array.prototype);
41
42
43 // The prototype defaults to %AsyncFunctionPrototype% if null.
44 function NewTargetNullPrototype() {}
45 NewTargetNullPrototype.prototype = null;
46
47 fn = Reflect.construct(AsyncFunction, [], NewTargetNullPrototype);
48 assertEq(fn instanceof AsyncFunction, true);
49 assertEq(Object.getPrototypeOf(fn), AsyncFunction.prototype);
50
51 fn = Reflect.construct(MyAsync, [], NewTargetNullPrototype);
52 assertEq(fn instanceof MyAsync, false);
53 assertEq(fn instanceof AsyncFunction, true);
54 assertEq(Object.getPrototypeOf(fn), AsyncFunction.prototype);
55
56
57 // "prototype" property is retrieved exactly once.
58 var trapLog = [], getLog = [];
59 var ProxiedConstructor = new Proxy(AsyncFunction, 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(AsyncFunction, [], ProxiedConstructor);
72 assertEqArray(trapLog, ["get"]);
73 assertEqArray(getLog, ["prototype"]);
74 assertEq(fn instanceof AsyncFunction, true);
75 assertEq(Object.getPrototypeOf(fn), AsyncFunction.prototype);
76
77
78 if (typeof reportCompare === "function")
79 reportCompare(0, 0);