summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/cacheir/new-with-non-object-prototype.js
blob: 5e146db4b5b7fd34fd19c3fcd9da53f73e91122b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Test constructing a function when the |.prototype| property isn't an object.

function test(proto) {
  function Klass() {
    this.prop = 1;
  }
  Klass.prototype = proto;

  const N = 100;

  let c = 0;
  for (let i = 0; i < N; ++i) {
    // Create a new object.
    let o = new Klass();

    // Read a property from the new object to ensure it was correctly allocated
    // and initialised.
    c += o.prop;

    // The prototype defaults to %Object.prototype% when the |.prototype|
    // property isn't an object.
    assertEq(Object.getPrototypeOf(o), Object.prototype);
  }

  assertEq(c, N);
}

const primitivesTypes = [
  undefined,
  null,
  123,
  true,
  "str",
  Symbol(),
  123n,
];

for (let primitive of primitivesTypes) {
  // Create a fresh function object to avoid type pollution.
  let fn = Function(`return ${test}`)();

  fn(primitive);
}

// Repeat the test from above, but this time |Klass| is a cross-realm function.

function testCrossRealm(proto) {
  const otherGlobal = newGlobal();
  const Klass = otherGlobal.eval(`
    function Klass() {
      this.prop = 1;
    }
    Klass;
  `);
  Klass.prototype = proto;

  const N = 100;

  let c = 0;
  for (let i = 0; i < N; ++i) {
    // Create a new object.
    let o = new Klass();

    // Read a property from the new object to ensure it was correctly allocated
    // and initialised.
    c += o.prop;

    // The prototype defaults to %Object.prototype% when the |.prototype|
    // property isn't an object.
    assertEq(Object.getPrototypeOf(o), otherGlobal.Object.prototype);
  }

  assertEq(c, N);
}

for (let primitive of primitivesTypes) {
  // Create a fresh function object to avoid type pollution.
  let fn = Function(`return ${testCrossRealm}`)();

  fn(primitive);
}

// Repeat the test from above, but this time |Klass| is a cross-realm new.target.

function testCrossRealmNewTarget(proto) {
  const otherGlobal = newGlobal();
  const Klass = otherGlobal.eval(`
    function Klass() {}
    Klass;
  `);
  Klass.prototype = proto;

  class C {
    constructor() {
      this.prop = 1;
    }
  }

  class D extends C {
    constructor() {
      super();
    }
  }

  const N = 100;

  let c = 0;
  for (let i = 0; i < N; ++i) {
    // Create a new object.
    let o = Reflect.construct(D, [], Klass);

    // Read a property from the new object to ensure it was correctly allocated
    // and initialised.
    c += o.prop;

    // The prototype defaults to %Object.prototype% when the |.prototype|
    // property isn't an object.
    assertEq(Object.getPrototypeOf(o), otherGlobal.Object.prototype);
  }

  assertEq(c, N);
}

for (let primitive of primitivesTypes) {
  // Create a fresh function object to avoid type pollution.
  let fn = Function(`return ${testCrossRealmNewTarget}`)();

  fn(primitive);
}