summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/debug/Debugger-setInstrumentation-02.js
blob: 68111b192e9ad58dcfd379d8a521dd2c5753edf2 (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
// Test main/entry/exit point instrumentation.

load(libdir + 'eqArrayHelper.js');

ignoreUnhandledRejections();

var g = newGlobal({ newCompartment: true });
var dbg = Debugger(g);
var gdbg = dbg.addDebuggee(g);

var allScripts = [];

function setScriptId(script) {
  script.setInstrumentationId(allScripts.length);
  allScripts.push(script);

  script.getChildScripts().forEach(setScriptId);
}

dbg.onNewScript = setScriptId;

const executedPoints = [];
function hitPoint(kind, scriptId) {
  const name = allScripts[scriptId].displayName;
  executedPoints.push(`${name}:${kind}`);
}

gdbg.setInstrumentation(
  gdbg.makeDebuggeeValue(hitPoint),
  ["main", "entry", "exit"]
);

function testFunction(fn, expected) {
  for (var i = 0; i < 3; i++) {
    try { fn(); } catch (e) {}
    drainJobQueue();
  }

  assertEq(executedPoints.length, 0);
  gdbg.setInstrumentationActive(true);

  for (var i = 0; i < 5; i++) {
    executedPoints.length = 0;
    try { fn(); } catch (e) {}
    drainJobQueue();
    assertEqArray(executedPoints, expected);
  }

  executedPoints.length = 0;
  gdbg.setInstrumentationActive(false);
}

g.eval(`
function basic() {
  var a = 0;
  a++;
}
`);

testFunction(() => g.basic(), ["basic:main", "basic:exit"]);

g.eval(`
function thrower(v) {
  if (v) {
    throw new Error();
  }
}
`);

testFunction(() => g.thrower(0), ["thrower:main", "thrower:exit"]);
testFunction(() => g.thrower(1), ["thrower:main"]);

g.eval(`
function* yielder() { yield 1; }
function iterator() {
  for (var n of yielder()) {}
}
`);

testFunction(() => g.iterator(), [
  "iterator:main",
  "yielder:main", "yielder:exit",
  "yielder:entry", "yielder:exit",
  "yielder:entry", "yielder:exit",
  "iterator:exit"
]);

g.eval(`
function promiser(n) {
  return new Promise(function promiseInternal(resolve, reject) {
    if (n) {
      reject(new Error());
    } else {
      resolve();
    }
  });
}
function f1() {}
async function asyncer(n) { await promiser(n) }
async function asyncerCatch(n) { try { await promiser(n) } catch (e) { f1() } }
async function asyncerFinally(n) { try { await promiser(n) } finally { f1() } }
`);

testFunction(() => g.asyncer(0), [
  "asyncer:main",
  "promiser:main", "promiseInternal:main", "promiseInternal:exit", "promiser:exit",
  "asyncer:exit",
  "asyncer:entry", "asyncer:exit"
]);
testFunction(() => g.asyncer(1), [
  "asyncer:main",
  "promiser:main", "promiseInternal:main", "promiseInternal:exit", "promiser:exit",
  "asyncer:exit",
  "asyncer:entry", // Note: no exit, GeneratorThrowOrReturn is called.
  "asyncer:entry", "asyncer:exit"
]);
testFunction(() => g.asyncerCatch(1), [
  "asyncerCatch:main",
  "promiser:main", "promiseInternal:main", "promiseInternal:exit", "promiser:exit",
  "asyncerCatch:exit",
  "asyncerCatch:entry", // Note: no exit, GeneratorThrowOrReturn is called.
  "asyncerCatch:entry", "f1:main", "f1:exit", "asyncerCatch:exit"
]);
testFunction(() => g.asyncerFinally(1), [
  "asyncerFinally:main",
  "promiser:main", "promiseInternal:main", "promiseInternal:exit", "promiser:exit",
  "asyncerFinally:exit",
  "asyncerFinally:entry", // Note: no exit, GeneratorThrowOrReturn is called.
  "asyncerFinally:entry", "f1:main", "f1:exit", "asyncerFinally:entry", "asyncerFinally:exit"
]);