| 1 |
// Test constructing a function when the |.prototype| property isn't an object. |
| 2 |
|
| 3 |
function test(proto) { |
| 4 |
function Klass() { |
| 5 |
this.prop = 1; |
| 6 |
} |
| 7 |
Klass.prototype = proto; |
| 8 |
|
| 9 |
const N = 100; |
| 10 |
|
| 11 |
let c = 0; |
| 12 |
for (let i = 0; i < N; ++i) { |
| 13 |
// Create a new object. |
| 14 |
let o = new Klass(); |
| 15 |
|
| 16 |
// Read a property from the new object to ensure it was correctly allocated |
| 17 |
// and initialised. |
| 18 |
c += o.prop; |
| 19 |
|
| 20 |
// The prototype defaults to %Object.prototype% when the |.prototype| |
| 21 |
// property isn't an object. |
| 22 |
assertEq(Object.getPrototypeOf(o), Object.prototype); |
| 23 |
} |
| 24 |
|
| 25 |
assertEq(c, N); |
| 26 |
} |
| 27 |
|
| 28 |
const primitivesTypes = [ |
| 29 |
undefined, |
| 30 |
null, |
| 31 |
123, |
| 32 |
true, |
| 33 |
"str", |
| 34 |
Symbol(), |
| 35 |
123n, |
| 36 |
]; |
| 37 |
|
| 38 |
for (let primitive of primitivesTypes) { |
| 39 |
// Create a fresh function object to avoid type pollution. |
| 40 |
let fn = Function(`return ${test}`)(); |
| 41 |
|
| 42 |
fn(primitive); |
| 43 |
} |
| 44 |
|
| 45 |
// Repeat the test from above, but this time |Klass| is a cross-realm function. |
| 46 |
|
| 47 |
function testCrossRealm(proto) { |
| 48 |
const otherGlobal = newGlobal(); |
| 49 |
const Klass = otherGlobal.eval(` |
| 50 |
function Klass() { |
| 51 |
this.prop = 1; |
| 52 |
} |
| 53 |
Klass; |
| 54 |
`); |
| 55 |
Klass.prototype = proto; |
| 56 |
|
| 57 |
const N = 100; |
| 58 |
|
| 59 |
let c = 0; |
| 60 |
for (let i = 0; i < N; ++i) { |
| 61 |
// Create a new object. |
| 62 |
let o = new Klass(); |
| 63 |
|
| 64 |
// Read a property from the new object to ensure it was correctly allocated |
| 65 |
// and initialised. |
| 66 |
c += o.prop; |
| 67 |
|
| 68 |
// The prototype defaults to %Object.prototype% when the |.prototype| |
| 69 |
// property isn't an object. |
| 70 |
assertEq(Object.getPrototypeOf(o), otherGlobal.Object.prototype); |
| 71 |
} |
| 72 |
|
| 73 |
assertEq(c, N); |
| 74 |
} |
| 75 |
|
| 76 |
for (let primitive of primitivesTypes) { |
| 77 |
// Create a fresh function object to avoid type pollution. |
| 78 |
let fn = Function(`return ${testCrossRealm}`)(); |
| 79 |
|
| 80 |
fn(primitive); |
| 81 |
} |
| 82 |
|
| 83 |
// Repeat the test from above, but this time |Klass| is a cross-realm new.target. |
| 84 |
|
| 85 |
function testCrossRealmNewTarget(proto) { |
| 86 |
const otherGlobal = newGlobal(); |
| 87 |
const Klass = otherGlobal.eval(` |
| 88 |
function Klass() {} |
| 89 |
Klass; |
| 90 |
`); |
| 91 |
Klass.prototype = proto; |
| 92 |
|
| 93 |
class C { |
| 94 |
constructor() { |
| 95 |
this.prop = 1; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
class D extends C { |
| 100 |
constructor() { |
| 101 |
super(); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
const N = 100; |
| 106 |
|
| 107 |
let c = 0; |
| 108 |
for (let i = 0; i < N; ++i) { |
| 109 |
// Create a new object. |
| 110 |
let o = Reflect.construct(D, [], Klass); |
| 111 |
|
| 112 |
// Read a property from the new object to ensure it was correctly allocated |
| 113 |
// and initialised. |
| 114 |
c += o.prop; |
| 115 |
|
| 116 |
// The prototype defaults to %Object.prototype% when the |.prototype| |
| 117 |
// property isn't an object. |
| 118 |
assertEq(Object.getPrototypeOf(o), otherGlobal.Object.prototype); |
| 119 |
} |
| 120 |
|
| 121 |
assertEq(c, N); |
| 122 |
} |
| 123 |
|
| 124 |
for (let primitive of primitivesTypes) { |
| 125 |
// Create a fresh function object to avoid type pollution. |
| 126 |
let fn = Function(`return ${testCrossRealmNewTarget}`)(); |
| 127 |
|
| 128 |
fn(primitive); |
| 129 |
} |