summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/debug/Script-getPossibleBreakpoints.js
blob: 5c52f52de33b0e130ed338708ed00ba55565c047 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// simple ExpressionStatement
assertBreakpoints(`
  /*S*/a;
  /*S*/obj.prop;
`);

// ExpressionStatement with calls
assertBreakpoints(`
  /*S*/a();
  /*S*/obj./*B*/prop();
`);

// calls with many args
assertBreakpoints(`
  /*S*/a/*B*/(1);
  /*S*/a/*B*/(1,2);
  /*S*/a/*B*/(1,2,3);
`);


// ExpressionStatement with nested expression calls.
assertBreakpoints(`
  "45";
  /*S*/"45" + /*B*/a();
  /*S*/b() + "45";

  /*S*/"45" + o./*B*/a();
  /*S*/o./*B*/b() + "45";
  /*S*/"45" + o./*B*/a() + o./*B*/b();
  /*S*/o./*B*/b() + "45" + o./*B*/a();
  /*S*/o./*B*/b() + o./*B*/a() + "45";
`);

// var VariableStatement initializers
assertBreakpoints(`
  var foo1 = /*S*/"" + o.a + "" + /*B*/b(),
      foo2 = /*S*/"45",
      foo3 = /*S*/"45" + /*B*/a(),
      foo4 = /*S*/b() + "45",
      foo5 = /*S*/"45" + /*B*/a() + /*B*/b(),
      foo6 = /*S*/b() + "45" + /*B*/a(),
      foo7 = /*S*/b() + /*B*/a() + "45",
      foo8 = /*S*/"45" + o./*B*/a(),
      foo9 = /*S*/o./*B*/b() + "45",
      foo10 = /*S*/"45" + o./*B*/a() + o./*B*/b(),
      foo11 = /*S*/o./*B*/b() + "45" + o./*B*/a(),
      foo12 = /*S*/o./*B*/b() + o./*B*/a() + "45";
`);

// let VariableStatement initializers
assertBreakpoints(`
  let foo1 = /*S*/"" + o.a + "" + /*B*/b(),
      foo2 = /*S*/"45",
      foo3 = /*S*/"45" + /*B*/a(),
      foo4 = /*S*/b() + "45",
      foo5 = /*S*/"45" + /*B*/a() + /*B*/b(),
      foo6 = /*S*/b() + "45" + /*B*/a(),
      foo7 = /*S*/b() + /*B*/a() + "45",
      foo8 = /*S*/"45" + o./*B*/a(),
      foo9 = /*S*/o./*B*/b() + "45",
      foo10 = /*S*/"45" + o./*B*/a() + o./*B*/b(),
      foo11 = /*S*/o./*B*/b() + "45" + o./*B*/a(),
      foo12 = /*S*/o./*B*/b() + o./*B*/a() + "45";
`);

// const VariableStatement initializers
assertBreakpoints(`
  const foo1 = /*S*/"" + o.a + "" + /*B*/b(),
        foo2 = /*S*/"45",
        foo3 = /*S*/"45" + /*B*/a(),
        foo4 = /*S*/b() + "45",
        foo5 = /*S*/"45" + /*B*/a() + /*B*/b(),
        foo6 = /*S*/b() + "45" + /*B*/a(),
        foo7 = /*S*/b() + /*B*/a() + "45",
        foo8 = /*S*/"45" + o./*B*/a(),
        foo9 = /*S*/o./*B*/b() + "45",
        foo10 = /*S*/"45" + o./*B*/a() + o./*B*/b(),
        foo11 = /*S*/o./*B*/b() + "45" + o./*B*/a(),
        foo12 = /*S*/o./*B*/b() + o./*B*/a() + "45";
`);

// EmptyStatement
assertBreakpoints(`
  ;
  ;
  ;
  /*S*/a();
`);

// IfStatement
assertBreakpoints(`
  if (/*S*/a) {}
  if (/*S*/a()) {}
  if (/*S*/obj.prop) {}
  if (/*S*/obj./*B*/prop()) {}
  if (/*S*/"42" + a) {}
  if (/*S*/"42" + /*B*/a()) {}
  if (/*S*/"42" + obj.prop) {}
  if (/*S*/"42" + obj./*B*/prop()) {}
`);

// DoWhile
assertBreakpoints(`
  do {
    /*S*/fn();
  } while(/*S*/a)
  do {
    /*S*/fn();
  } while(/*S*/"42" + /*B*/a());
`);

// While
assertBreakpoints(`
  while(/*S*/a) {
    /*S*/fn();
  }
  while(/*S*/"42" + /*B*/a()) {
    /*S*/fn();
  }
`);

// ForExpr
assertBreakpoints(`
  for (/*S*/b = 42; /*S*/c; /*S*/d) /*S*/fn();
  for (var b = /*S*/42; /*S*/c; /*S*/d) /*S*/fn();
  for (let b = /*S*/42; /*S*/c; /*S*/d) /*S*/fn();
  for (const b = /*S*/42; /*S*/c; /*S*/d) /*S*/fn();
  for (b in /*S*/d) /*S*/fn();
  for (var b in /*S*/d) /*S*/fn();
  for (let b in /*S*/d) /*S*/fn();
  for (const b in /*S*/d) /*S*/fn();
  for (b of /*S*/d) /*S*/fn();
  for (var b of /*S*/d) /*S*/fn();
  for (let b of /*S*/d) /*S*/fn();
  for (const b of /*S*/d) /*S*/fn();
`);

// SwitchStatement
assertBreakpoints(`
  switch (/*S*/d) {
    case 42:
      /*S*/fn();
  }
`);

// ContinueStatement
assertBreakpoints(`
  while (/*S*/a) {
    /*S*/continue;
  }
`);

// BreakStatement
assertBreakpoints(`
  while (/*S*/a) {
    /*S*/break;
  }
`);

// ReturnStatement
assertBreakpoints(`
  /*S*/return a + /*B*/b();
`);

// WithStatement
assertBreakpoints(`
  with (/*S*/a) {
    /*S*/fn();
  }
`);

// ThrowStatement
assertBreakpoints(`
  /*S*/throw /*B*/fn();
  /*S*/throw "42" + /*B*/fn();
`);

// DebuggerStatement
assertBreakpoints(`
  /*S*/debugger;
  /*S*/debugger;
`);

// BlockStatent wrapper
assertBreakpoints(`
  {
    /*S*/a();
  }
`);

// ClassDeclaration
assertBreakpoints(`
  class Foo2 {}
  /*S*/class Foo extends ("" + o.a + /*B*/a() + /*B*/b()) { }
`);

// Misc examples
assertBreakpoints(`
  /*S*/void /*B*/a();
`);
assertBreakpoints(`
  /*S*/a() + /*B*/b();
`);
assertBreakpoints(`
  for (
    var i = /*S*/0;
    /*S*/i < n;  // 4
    /*S*/++i
  ) {
    /*S*/console./*B*/log("omg");
  }
`);
assertBreakpoints(`
  function * gen(){
    var foo = (
      (/*S*/console./*B*/log('before', /*B*/a())),
      (yield console./*B*/log('mid', /*B*/b())),
      (console./*B*/log('after', /*B*/a()))
    );
    var foo2 = /*S*/a() + /*B*/b();
    /*S*/console./*B*/log(foo);
  /*B*/}
  var i = /*S*/0;
  for (var foo of /*S*/gen()) {
    /*S*/console./*B*/log(i++);
  }
`);
assertBreakpoints(`
  var fn = /*S*/() => {
    /*S*/console./*B*/log("fn");
    /*S*/return /*B*/new Proxy({ prop: 42 }, {
      deleteProperty() {
        /*S*/console./*B*/log("delete");
      /*B*/}
    });
  /*B*/};
`);
assertBreakpoints(`
  var fn = /*S*/async (arg) => {
    /*S*/console./*B*/log("fn");
  /*B*/};
`);
assertBreakpoints(`
  var fn = /*S*/arg => {
    /*S*/console./*B*/log("fn");
  /*B*/};
`);
assertBreakpoints(`
  var fn = /*S*/async arg => {
    /*S*/console./*B*/log("fn");
  /*B*/};
`);
assertBreakpoints(`
  var fn = /*S*/(arg) => /*S*/console./*B*/log("fn");
  var fn = /*S*/async (arg) => /*S*/console./*B*/log("fn");
  var fn = /*S*/arg => /*S*/console./*B*/log("fn");
  var fn = /*S*/async arg => /*S*/console./*B*/log("fn");
`);
assertBreakpoints(`
  if ((/*S*/delete /*B*/fn().prop) + /*B*/b()) {
    /*S*/console./*B*/log("foo");
  }
`);
assertBreakpoints(`
  for (var j = /*S*/0; (/*S*/o.a) < 3; (/*S*/j++, /*B*/a(), /*B*/b())) {
    /*S*/console./*B*/log(i);
  }
`);
assertBreakpoints(`
  function fn2(
    [a, b] = (/*B*/a(), /*B*/b())
  ) {
    /*S*/a();
    /*S*/b();
  /*B*/}

  ({ a, b } = (/*S*/a(), /*B*/b()));
`);
assertBreakpoints(`
  /*S*/o.a + "42" + /*B*/a() + /*B*/b();
`);
assertBreakpoints(`
  /*S*/a();
  /*S*/o./*B*/a(/*B*/b());
`);
assertBreakpoints(`
  (/*S*/{}[obj.a] = 42 + /*B*/a());
`);
assertBreakpoints(`
  var {
    foo = o.a
  } = /*S*/{};
`);
assertBreakpoints(`
  var ack = /*S*/[
    o.a,
    o.b,
    /*B*/a(),
    /*B*/a(),
    /*B*/a(),
    /*B*/a(),
    /*B*/a(),
    /*B*/a(),
    /*B*/a(),
  ];
`);

function assertBreakpoints(expected) {
  const input = expected.replace(/\/\*[BS]\*\//g, "");

  var global = newGlobal({ newCompartment: true });
  var dbg = Debugger(global);
  dbg.onDebuggerStatement = function(frame) {
    const fScript = frame.environment.parent.getVariable("f").script;

    let positions = [];
    (function recurse(script) {
      const bps = script.getPossibleBreakpoints();
      const offsets = script.getPossibleBreakpointOffsets();

      assertEq(offsets.length, bps.length);
      for (let i = 0; i < bps.length; i++) {
        assertEq(offsets[i], bps[i].offset);
      }

      positions = positions.concat(bps);
      script.getChildScripts().forEach(recurse);
    })(fScript);

    const result = annotateOffsets(input, positions);
    assertEq(result, expected + "/*B*/");
  };

  global.eval(`function f(){${input}} debugger;`);
}

function annotateOffsets(code, positions) {
  const offsetLookup = createOffsetLookup(code);

  positions = positions.slice();
  positions.sort((a, b) => {
    const lineDiff = a.lineNumber - b.lineNumber;
    return lineDiff === 0 ? a.columnNumber - b.columnNumber : lineDiff;
  });
  positions.reverse();

  let output = "";
  let last = code.length;
  for (const { lineNumber, columnNumber, isStepStart } of positions) {
    const offset = offsetLookup(lineNumber, columnNumber);

    output =
      "/*" +
      (isStepStart ? "S" : "B") +
      "*/" +
      code.slice(offset, last) +
      output;
    last = offset;
  }
  return code.slice(0, last) + output;
}

function createOffsetLookup(code) {
  const lines = code.split(/(\r?\n|\r|\u2028|\u2029)/g);
  const lineOffsets = [];

  let count = 0;
  for (const [i, str] of lines.entries()) {
    if (i % 2 === 0) {
      lineOffsets[i / 2] = count;
    }
    count += str.length;
  }

  return function(line, column) {
    // Lines from getAllColumnOffsets are 1-based.
    line = line - 1;

    if (!lineOffsets.hasOwnProperty(line)) {
      throw new Error("Unknown line " + line + " column " + column);
    }
    return lineOffsets[line] + column;
  };
}