summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/debug/Debugger-findScripts-delazify.js
blob: 554b5dd2c8606a6e84e9123de3822dc247f8ad1f (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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// |jit-test| skip-if: isLcovEnabled()

// findScript should try to avoid delazifying unnecessarily.

function newTestcase(code) {
  var g = newGlobal({newCompartment: true});
  var dbg = new Debugger();
  var gw = dbg.addDebuggee(g);

  var lines = code.split('\n');
  // Returns the line number of the line with "<= line".
  // + 1 for 1-origin.
  var line = lines.findIndex(x => x.includes("<= line")) + 1;

  g.eval(code);

  // Functions are relazified, and the top-level script is dropped.
  relazifyFunctions();

  return [dbg, g, line];
}

var url = thisFilename();
var dbg, g, line, scripts;

// If the specified line is inside the function body, only the function should
// be delazified.
[dbg, g, line] = newTestcase(`
function f1() {
}
function f2() {
// <= line
}
function f3() {
}
`);

assertEq(g.eval(`isLazyFunction(f1)`), true);
assertEq(g.eval(`isLazyFunction(f2)`), true);
assertEq(g.eval(`isLazyFunction(f3)`), true);

scripts = dbg.findScripts({url, line});
assertEq(scripts.length, 1);
assertEq(scripts.map(s => s.displayName).sort().join(","), "f2");

assertEq(g.eval(`isLazyFunction(f1)`), true);
assertEq(g.eval(`isLazyFunction(f2)`), false);
assertEq(g.eval(`isLazyFunction(f3)`), true);

// If the functions starts at the specified line, the function shouldn't be
// the first function to delazify.
[dbg, g, line] = newTestcase(`
function f1() {
}
function f2() {
}
function f3() { // <= line
}
function f4() {
}
`);

assertEq(g.eval(`isLazyFunction(f1)`), true);
assertEq(g.eval(`isLazyFunction(f2)`), true);
assertEq(g.eval(`isLazyFunction(f3)`), true);
assertEq(g.eval(`isLazyFunction(f4)`), true);

scripts = dbg.findScripts({url, line});
assertEq(scripts.length, 1);
assertEq(scripts.map(s => s.displayName).sort().join(","), "f3");

assertEq(g.eval(`isLazyFunction(f1)`), true);
// f2 is delazified because f3 cannot be the first function to delazify.
assertEq(g.eval(`isLazyFunction(f2)`), false);
assertEq(g.eval(`isLazyFunction(f3)`), false);
assertEq(g.eval(`isLazyFunction(f4)`), true);

// Multiple functions in the specified line, and one of them starts before
// the specified line.
// All functions should be returned, and others shouldn't be delazified.
[dbg, g, line] = newTestcase(`
function f1() {}
function f2() {
} function f3() {} function f4() {} function f5() { // <= line
}
function f6() {}
`);

assertEq(g.eval(`isLazyFunction(f1)`), true);
assertEq(g.eval(`isLazyFunction(f2)`), true);
assertEq(g.eval(`isLazyFunction(f3)`), true);
assertEq(g.eval(`isLazyFunction(f4)`), true);
assertEq(g.eval(`isLazyFunction(f5)`), true);
assertEq(g.eval(`isLazyFunction(f6)`), true);

scripts = dbg.findScripts({url, line});
assertEq(scripts.length, 4);
assertEq(scripts.map(s => s.displayName).sort().join(","), "f2,f3,f4,f5");

assertEq(g.eval(`isLazyFunction(f1)`), true);
assertEq(g.eval(`isLazyFunction(f2)`), false);
assertEq(g.eval(`isLazyFunction(f3)`), false);
assertEq(g.eval(`isLazyFunction(f4)`), false);
assertEq(g.eval(`isLazyFunction(f5)`), false);
assertEq(g.eval(`isLazyFunction(f6)`), true);

// The same rule should apply to inner functions.
[dbg, g, line] = newTestcase(`
function f1() {}
function f2() {
  function g1() {
  }
  function g2() {
    function h1() {}
    function h2() {
    } function h3() {} function h4() {} function h5() { // <= line
    }
    function h6() {}

    return [h1, h2, h3, h4, h5, h6];
  }
  function g3() {
  }

  return [g1, g2, g3];
}
function f3() {}
`);

assertEq(g.eval(`isLazyFunction(f1)`), true);
assertEq(g.eval(`isLazyFunction(f2)`), true);
assertEq(g.eval(`isLazyFunction(f3)`), true);

scripts = dbg.findScripts({url, line});
assertEq(scripts.length, 6);
assertEq(scripts.map(s => s.displayName).sort().join(","), "f2,g2,h2,h3,h4,h5");

assertEq(g.eval(`isLazyFunction(f1)`), true);
assertEq(g.eval(`isLazyFunction(f2)`), false);
assertEq(g.eval(`isLazyFunction(f3)`), true);
g.eval(`var [g1, g2, g3] = f2();`);
assertEq(g.eval(`isLazyFunction(g1)`), true);
assertEq(g.eval(`isLazyFunction(g2)`), false);
assertEq(g.eval(`isLazyFunction(g3)`), true);
g.eval(`var [h1, h2, h3, h4, h5, h6] = g2();`);
assertEq(g.eval(`isLazyFunction(h1)`), true);
assertEq(g.eval(`isLazyFunction(h2)`), false);
assertEq(g.eval(`isLazyFunction(h3)`), false);
assertEq(g.eval(`isLazyFunction(h4)`), false);
assertEq(g.eval(`isLazyFunction(h5)`), false);
assertEq(g.eval(`isLazyFunction(h6)`), true);

// The same rule should apply to functions inside parameter expression.
[dbg, g, line] = newTestcase(`
function f1(
  a = function g1() {},
  b = function g2() {
  }, c = function g3() {}, d = function g4() { // <= line
  },
  e = function g5() {},
) {
  return [a, b, c, d, e];
}
`);

assertEq(g.eval(`isLazyFunction(f1)`), true);

scripts = dbg.findScripts({url, line});
assertEq(scripts.length, 4);
assertEq(scripts.map(s => s.displayName).sort().join(","), "f1,g2,g3,g4");

assertEq(g.eval(`isLazyFunction(f1)`), false);

g.eval(`var [g1, g2, g3, g4, g5] = f1();`);

assertEq(g.eval(`isLazyFunction(g1)`), true);
assertEq(g.eval(`isLazyFunction(g2)`), false);
assertEq(g.eval(`isLazyFunction(g3)`), false);
assertEq(g.eval(`isLazyFunction(g4)`), false);
assertEq(g.eval(`isLazyFunction(g5)`), true);

// The same should apply to function inside method with computed property.
[dbg, g, line] = newTestcase(`
var f1, f2, f3;
var O = {
  [(f1 = () => 0, "m1")]() {
  },
  [(f2 = () => 0, "m2")](p2) { // <= line
  },
  [(f3 = () => 0, "m3")]() {
  },
}
`);

assertEq(g.eval(`isLazyFunction(f1)`), true);
assertEq(g.eval(`isLazyFunction(O.m2)`), true);
assertEq(g.eval(`isLazyFunction(f2)`), true);
assertEq(g.eval(`isLazyFunction(O.m1)`), true);
assertEq(g.eval(`isLazyFunction(f3)`), true);
assertEq(g.eval(`isLazyFunction(O.m3)`), true);

scripts = dbg.findScripts({url, line});
assertEq(scripts.length, 2);
assertEq(scripts.map(s => s.displayName).sort().join(","), "f2,");
// Use parameterNames because displayName isn't set for computed property.
assertEq(scripts.map(s => `${s.displayName}(${s.parameterNames.join(",")})`)
         .sort().join(","), "f2(),undefined(p2)");

assertEq(g.eval(`isLazyFunction(f1)`), true);
// m1 is delazified because f2 cannot be the first function to delazify.
assertEq(g.eval(`isLazyFunction(O.m1)`), false);
assertEq(g.eval(`isLazyFunction(f2)`), false);
assertEq(g.eval(`isLazyFunction(O.m2)`), false);
assertEq(g.eval(`isLazyFunction(f3)`), true);
assertEq(g.eval(`isLazyFunction(O.m3)`), true);

[dbg, g, line] = newTestcase(`
var f1, f2, f3;
var O = {
  [(f1 = () => 0, "m1")]() {
  },
  [(f2 = () => 0 +
  1, "m2")](p2) { // <= line
  },
  [(f3 = () => 0, "m3")]() {
  },
}
`);

assertEq(g.eval(`isLazyFunction(f1)`), true);
assertEq(g.eval(`isLazyFunction(O.m1)`), true);
assertEq(g.eval(`isLazyFunction(f2)`), true);
assertEq(g.eval(`isLazyFunction(O.m2)`), true);
assertEq(g.eval(`isLazyFunction(f3)`), true);
assertEq(g.eval(`isLazyFunction(O.m3)`), true);

scripts = dbg.findScripts({url, line});
assertEq(scripts.length, 2);
assertEq(scripts.map(s => `${s.displayName}(${s.parameterNames.join(",")})`)
         .sort().join(","), "f2(),undefined(p2)");

assertEq(g.eval(`isLazyFunction(f1)`), true);
assertEq(g.eval(`isLazyFunction(O.m1)`), true);
assertEq(g.eval(`isLazyFunction(f2)`), false);
assertEq(g.eval(`isLazyFunction(O.m2)`), false);
assertEq(g.eval(`isLazyFunction(f3)`), true);
assertEq(g.eval(`isLazyFunction(O.m3)`), true);

// Class constructor shouldn't be delazified even if methods match.
[dbg, g, line] = newTestcase(`
// Use variable to access across eval.
var C = class {
  constructor() {
  }
  m1() {}
  m2() {
  } m3() {} m4() { // <= line
  }
  m5() {}
}
`);

assertEq(g.eval(`isLazyFunction(C)`), true);
assertEq(g.eval(`isLazyFunction(C.prototype.m1)`), true);
assertEq(g.eval(`isLazyFunction(C.prototype.m2)`), true);
assertEq(g.eval(`isLazyFunction(C.prototype.m3)`), true);
assertEq(g.eval(`isLazyFunction(C.prototype.m4)`), true);
assertEq(g.eval(`isLazyFunction(C.prototype.m5)`), true);

scripts = dbg.findScripts({url, line});
assertEq(scripts.length, 3);
assertEq(scripts.map(s => s.displayName).sort().join(","), "m2,m3,m4");

assertEq(g.eval(`isLazyFunction(C)`), true);
assertEq(g.eval(`isLazyFunction(C.prototype.m1)`), true);
assertEq(g.eval(`isLazyFunction(C.prototype.m2)`), false);
assertEq(g.eval(`isLazyFunction(C.prototype.m3)`), false);
assertEq(g.eval(`isLazyFunction(C.prototype.m4)`), false);
assertEq(g.eval(`isLazyFunction(C.prototype.m5)`), true);

// If the line is placed before sourceStart, the function shouldn't match,
// and the function shouldn't be delazified.
[dbg, g, line] = newTestcase(`
function f1() {
}
function f2() {
}
function
// <= line
f3() {
}
function f4() {
}
`);

assertEq(g.eval(`isLazyFunction(f1)`), true);
assertEq(g.eval(`isLazyFunction(f2)`), true);
assertEq(g.eval(`isLazyFunction(f3)`), true);
assertEq(g.eval(`isLazyFunction(f4)`), true);

scripts = dbg.findScripts({url, line});
assertEq(scripts.length, 0);

assertEq(g.eval(`isLazyFunction(f1)`), true);
// f2 is delazified because it's the first function before the specified line.
assertEq(g.eval(`isLazyFunction(f2)`), false);
assertEq(g.eval(`isLazyFunction(f3)`), true);
assertEq(g.eval(`isLazyFunction(f4)`), true);

[dbg, g, line] = newTestcase(`
function f1() {
}
function f2() {
}
function f3
// <= line
() {
}
function f4() {
}
`);

assertEq(g.eval(`isLazyFunction(f1)`), true);
assertEq(g.eval(`isLazyFunction(f2)`), true);
assertEq(g.eval(`isLazyFunction(f3)`), true);
assertEq(g.eval(`isLazyFunction(f4)`), true);

scripts = dbg.findScripts({url, line});
assertEq(scripts.length, 0);

assertEq(g.eval(`isLazyFunction(f1)`), true);
// f2 is delazified because it's the first function before the specified line.
assertEq(g.eval(`isLazyFunction(f2)`), false);
assertEq(g.eval(`isLazyFunction(f3)`), true);
assertEq(g.eval(`isLazyFunction(f4)`), true);

[dbg, g, line] = newTestcase(`
function f1() {
}
function f2() {
}
function f3
( // <= line
) {
}
function f4() {
}
`);

assertEq(g.eval(`isLazyFunction(f1)`), true);
assertEq(g.eval(`isLazyFunction(f2)`), true);
assertEq(g.eval(`isLazyFunction(f3)`), true);
assertEq(g.eval(`isLazyFunction(f4)`), true);

scripts = dbg.findScripts({url, line});
assertEq(scripts.length, 1);
assertEq(scripts.map(s => s.displayName).sort().join(","), "f3");

assertEq(g.eval(`isLazyFunction(f1)`), true);
// f2 is delazified because it's the first function _before_ the specified line.
assertEq(g.eval(`isLazyFunction(f2)`), false);
assertEq(g.eval(`isLazyFunction(f3)`), false);
assertEq(g.eval(`isLazyFunction(f4)`), true);

[dbg, g, line] = newTestcase(`
function f1() {
}
function f2() {
}
function f3
(
// <= line
) {
}
function f4() {
}
`);

assertEq(g.eval(`isLazyFunction(f1)`), true);
assertEq(g.eval(`isLazyFunction(f2)`), true);
assertEq(g.eval(`isLazyFunction(f3)`), true);
assertEq(g.eval(`isLazyFunction(f4)`), true);

scripts = dbg.findScripts({url, line});
assertEq(scripts.length, 1);
assertEq(scripts.map(s => s.displayName).sort().join(","), "f3");

assertEq(g.eval(`isLazyFunction(f1)`), true);
assertEq(g.eval(`isLazyFunction(f2)`), true);
assertEq(g.eval(`isLazyFunction(f3)`), false);
assertEq(g.eval(`isLazyFunction(f4)`), true);

// If the specified line is the next line after the function ends,
// nothing should match, but the function should be delazified.
[dbg, g, line] = newTestcase(`
function f1() {
}
function f2() {
}
// <= line
function f3() {
}
`);

assertEq(g.eval(`isLazyFunction(f1)`), true);
assertEq(g.eval(`isLazyFunction(f2)`), true);
assertEq(g.eval(`isLazyFunction(f3)`), true);

scripts = dbg.findScripts({url, line});
assertEq(scripts.length, 0);

assertEq(g.eval(`isLazyFunction(f1)`), true);
assertEq(g.eval(`isLazyFunction(f2)`), false);
assertEq(g.eval(`isLazyFunction(f3)`), true);

// The matching non-lazy script should prevent the previous function's
// delazification.
[dbg, g, line] = newTestcase(`
function f1() {
}
function f2() {
}
function f3() {
  // <= line
}
function f4() {
}
`);

assertEq(g.eval(`isLazyFunction(f1)`), true);
assertEq(g.eval(`isLazyFunction(f2)`), true);
assertEq(g.eval(`isLazyFunction(f3)`), true);
assertEq(g.eval(`isLazyFunction(f4)`), true);

// Delazify f3.
g.eval(`f3()`);

assertEq(g.eval(`isLazyFunction(f1)`), true);
assertEq(g.eval(`isLazyFunction(f2)`), true);
assertEq(g.eval(`isLazyFunction(f3)`), false);
assertEq(g.eval(`isLazyFunction(f4)`), true);

scripts = dbg.findScripts({url, line});
assertEq(scripts.length, 1);
assertEq(scripts.map(s => s.displayName).sort().join(","), "f3");

assertEq(g.eval(`isLazyFunction(f1)`), true);
assertEq(g.eval(`isLazyFunction(f2)`), true);
assertEq(g.eval(`isLazyFunction(f3)`), false);
assertEq(g.eval(`isLazyFunction(f4)`), true);

// The non-matching non-lazy script should prevent the previous function's
// delazification.
[dbg, g, line] = newTestcase(`
function f1() {
}
function f2() {
}
function f3() {
}
// <= line
function f4() {
}
`);

assertEq(g.eval(`isLazyFunction(f1)`), true);
assertEq(g.eval(`isLazyFunction(f2)`), true);
assertEq(g.eval(`isLazyFunction(f3)`), true);
assertEq(g.eval(`isLazyFunction(f4)`), true);

// Delazify f3.
g.eval(`f3()`);

assertEq(g.eval(`isLazyFunction(f1)`), true);
assertEq(g.eval(`isLazyFunction(f2)`), true);
assertEq(g.eval(`isLazyFunction(f3)`), false);
assertEq(g.eval(`isLazyFunction(f4)`), true);

scripts = dbg.findScripts({url, line});
assertEq(scripts.length, 0);

assertEq(g.eval(`isLazyFunction(f1)`), true);
assertEq(g.eval(`isLazyFunction(f2)`), true);
assertEq(g.eval(`isLazyFunction(f3)`), false);
assertEq(g.eval(`isLazyFunction(f4)`), true);