summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/debug/private-methods-eval-in-frame.js
blob: 5122cfa56b0c94b1b24042554154d9089cb06ce2 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
load(libdir + 'evalInFrame.js');

class B {
  #priv() {
    return 12;
  }

  #privF = () => { return 12; }

  callPriv() {
    return this.#priv();
  }

  callPrivF() {
    return this.#privF();
  }

  #val = 'constructed';
  set #x(x) {
    this.#val = x + ' haha';
  }
  get #x() {
    return this.#val;
  }

  ef(str) {
    return evalInFrame(0, str);
  }

  layerEf(str) {
    // Nested functions here result in computeEffectiveScope traversing
    // more than one environment, necessitating more hops.
    function tester(o) {
      let a = 10;
      function interior(o) {
        let b = a;
        return evalInFrame(0, str.replace("this", "o"));
      };
      return interior(o);
    }
    return tester(this);
  }

  moreLayerEf(str) {
    // Nested functions here result in computeEffectiveScope traversing
    // more than one environment, necessitating more hops.
    function tester(o) {
      let a = 0;
      function interior(o) {
        let b = a;
        return (() => {
          let k = o;
          let replace = str.replace("this", "k");
          print(replace);
          return evalInFrame(b, replace);
        })();
      };
      return interior(o);
    }
    return tester(this);
  }

  callFunc(f) {
    return f();
  }

  static #smethod() {
    return 14;
  }

  static #unusedmethod() {
    return 19;
  }

  static get #unusedgetter() {
    return 10;
  }

  static setter = undefined;
  static set #unusedsetter(x) { this.setter = x }


  static f() {
    return this.#smethod();
  }

  static seval(str) {
    return eval(str);
  }

  static sef(str) {
    return evalInFrame(0, str);
  }


  static sLayerEf(str) {
    // Nested functions here result in computeEffectiveScope traversing
    // more than one environment, necessitating more hops.
    function tester(o) {
      let a = 10;
      function interior(o) {
        if (a == 10) {
          let b = 10 - a;
          return evalInFrame(b, str.replace("this", "o"));
        }
      };
      return interior(o);
    }
    return tester(this);
  }


}

var b = new B();

assertEq(b.ef("this.callPriv()"), 12);
assertEq(b.ef("this.callPrivF()"), 12);

// Private fields don't need brand checking and should succeed.
assertEq(b.ef("this.#val"), 'constructed')

// PrivF is a field, not a method, and so isn't brand checked like a method.
assertEq(b.ef(`this.callFunc(() => { return this.#privF() })`), 12);
assertEq(b.ef(`this.#privF()`), 12);

// Accesors are stored like fields, and so successfully execute, as they don't
// need brand checking.
assertEq(b.ef(`this.#x = 'Bye'; this.#x`), 'Bye haha');
assertEq(b.ef(`var x = () => { this.#x = 'Hi'; return this.#x}; x()`), 'Hi haha');


// // Private methods require very special brand checking.
assertEq(b.ef(`this.#priv()`), 12);
assertEq(b.ef(`let hello;
let f = () => {
  hello = this.#priv();
  assertEq(hello, 12);
};
f();
hello`), 12);
assertEq(b.layerEf(`this.#priv()`), 12);
assertEq(b.moreLayerEf(`this.#priv()`), 12);

if ('dis' in this) {
  // Ensure disassembly of GetAliasedDebugVar wroks.
  assertEq(b.ef(`dis(); this.#priv()`), 12);
}

assertEq(b.ef(`var x = () => { return this.#priv(); }; x()`), 12);
assertEq(b.ef(`function x(o) { function y(o) { return o.#priv(); }; return y(o); } x(this)`), 12);

assertEq(b.ef("B.#smethod()"), 14)
assertEq(b.ef("B.#unusedmethod()"), 19);
assertEq(b.ef("B.#unusedgetter"), 10);

b.ef("B.#unusedsetter = 19");
assertEq(B.setter, 19);

assertEq(B.f(), 14);
assertEq(B.sef(`this.#smethod()`), 14);
assertEq(B.sLayerEf(`this.#smethod()`), 14);
assertEq(B.sef("this.#unusedmethod()"), 19);
assertEq(B.sef("this.#unusedgetter"), 10);


B.sef("this.#unusedsetter = 13");
assertEq(B.setter, 13);

// Test case variant from Arai.
class C {
  #priv() {
    return 12;
  }

  efInFunction(str) {
    return (() => {
      let self = this;
      return evalInFrame(0, str);
    })();
  }
}
c = new C;
assertEq(c.efInFunction(`self.#priv()`), 12);

// JIT testing
assertEq(b.ef(`
let result;
let f = () => {
  result = this.#priv();
  assertEq(result, 12);
};
for (let i = 0; i < 1000; i++) {
  f();
}
result
`), 12);

assertEq(b.ef("function f(o) { return o.callPriv() }; for (let i = 0; i < 1000; i++) { f(this); } f(this)"), 12);
assertEq(b.ef("function f(o) { return o.callPrivF() }; for (let i = 0; i < 1000; i++) { f(this); } f(this)"), 12);
assertEq(b.ef(`function x(o) { function y(o) { return o.#priv(); }; return y(o); } x(this)`), 12);

assertEq(B.sef(`function f(o) { return o.#smethod() }; for (let i = 0; i < 1000; i ++) { f(this); }; f(this)`), 14);

assertEq(b.ef(`
var x = () => {
  return (() => {
    return (() => {
      let a;
      return (() => {
        let b = a;
        return this.#priv();
      })();
    })();
  })();
};
x()
`), 12);