Debian Codegraph @

js/src/jit-test/tests/cacheir/new-with-non-object-prototype-failure.js

raw
1 // Test failure when constructing a function whose |.prototype| property isn't an object.
2
3 function Klass() {
4 this.prop = 1;
5 }
6
7 // Save the original prototype.
8 const KlassPrototype = Klass.prototype;
9
10 // Set the prototype to a non-object value.
11 Klass.prototype = null;
12
13 const prototypes = [
14 null,
15 KlassPrototype,
16 ];
17
18 const N = 500;
19 let c = 0;
20
21 for (let i = 0; i <= N; ++i) {
22 // Always perform a set to avoid a cold-code bailout.
23 let proto = prototypes[(i === N)|0];
24 Klass.prototype = proto;
25
26 // Create a new object.
27 let o = new Klass();
28
29 // Read a property from the new object to ensure it was correctly allocated
30 // and initialised.
31 c += o.prop;
32
33 // The prototype defaults to %Object.prototype% when the |.prototype|
34 // property isn't an object.
35 assertEq(Object.getPrototypeOf(o), proto === null ? Object.prototype : KlassPrototype);
36 }
37
38 assertEq(c, N + 1);