summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/debug/job-queue-03.js
blob: 7e3ce27ea42daf0202915ca8ac0b357232aad3bf (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
// Multiple debuggers get their job queues drained after each hook.
// This covers:
// - onDebuggerStatement
// - onStep
// - onEnterFrame
// - onPop
// - onExceptionUnwind
// - breakpoint handlers
// - uncaughtExceptionHook

const g = newGlobal({ newCompartment: true });
g.parent = this;

var log = '';
let expected_throws = 0;

function setup(global, label) {
  const dbg = new Debugger;
  dbg.gDO = dbg.addDebuggee(global);
  dbg.log = '';

  dbg.onDebuggerStatement = function (frame) {
    // Exercise the promise machinery: resolve a promise and perform a microtask
    // checkpoint. When called from a debugger hook, the debuggee's microtasks
    // should not run.
    function exercise(name) {
      dbg.log += name + ',';
      log += `${label}-${name}-handler\n`;
      Promise.resolve(42).then(v => {
        assertEq(v, 42);
        log += `${label}-${name}-tail\n`;
      });
    }

    exercise('debugger');

    frame.onStep = function () {
      this.onStep = undefined;
      exercise('step');
    };

    dbg.onEnterFrame = function (frame) {
      dbg.onEnterFrame = undefined;
      frame.onPop = function(completion) {
        assertEq(completion.return, 'escutcheon');
        exercise('pop');
      }

      exercise('enter');
    }

    expected_throws++;
    dbg.onExceptionUnwind = function(frame, value) {
      dbg.onExceptionUnwind = undefined;
      assertEq(value, 'myrmidon');
      exercise('exception');
      if (--expected_throws > 0) {
        return undefined;
      } else {
        return { return: 'escutcheon' };
      }
    };

    // Set a breakpoint on entry to g.breakpoint_here.
    const script = dbg.gDO.getOwnPropertyDescriptor('breakpoint_here').value.script;
    const handler = {
      hit(frame) {
        script.clearAllBreakpoints();
        exercise('bp');
      }
    };
    script.setBreakpoint(0, handler);

    dbg.uncaughtExceptionHook = function (ex) {
      assertEq(ex, 'turncoat');
      exercise('uncaught');
    };

    // Throw an uncaught exception from the Debugger handler. This should reach
    // uncaughtExceptionHook, but shouldn't affect the debuggee.
    throw 'turncoat';
  };

  return dbg;
}

const dbg1 = setup(g, '1');
const dbg2 = setup(g, '2');
const dbg3 = setup(g, '3');

g.eval(`
  function breakpoint_here() {
    throw 'myrmidon';
  }

  parent.log += 'eval-start\\n';

  // DebuggeeWouldRun detection may prevent this callback from running at all if
  // bug 1145201 is present. SpiderMonkey will try to run the promise reaction
  // job from the Debugger hook's microtask checkpoint, triggering
  // DebuggeeWouldRun. This is a little difficult to observe, since the callback
  // never even begins execution. But it should cause the 'then' promise to be
  // rejected, which the shell will report (if the assertEq(log, ...) doesn't
  // kill the test first).

  Promise.resolve(84).then(function(v) {
    assertEq(v, 84);
    parent.log += 'eval-react';
  });
  debugger;
  parent.log += 'stuff to step over\\n';
  breakpoint_here();
  parent.log += 'eval-end\\n';
`);

log += 'main-drain\n'
drainJobQueue();
log += 'main-drain-done\n';

const regex = new RegExp(`eval-start
.-debugger-handler
.-uncaught-handler
.-debugger-tail
.-uncaught-tail
.-debugger-handler
.-uncaught-handler
.-debugger-tail
.-uncaught-tail
.-debugger-handler
.-uncaught-handler
.-debugger-tail
.-uncaught-tail
.-step-handler
.-step-tail
.-step-handler
.-step-tail
.-step-handler
.-step-tail
stuff to step over
.-enter-handler
.-enter-tail
.-enter-handler
.-enter-tail
.-enter-handler
.-enter-tail
.-bp-handler
.-bp-tail
.-bp-handler
.-bp-tail
.-bp-handler
.-bp-tail
.-exception-handler
.-exception-tail
.-exception-handler
.-exception-tail
.-exception-handler
.-exception-tail
.-pop-handler
.-pop-tail
.-pop-handler
.-pop-tail
.-pop-handler
.-pop-tail
eval-end
main-drain
eval-reactmain-drain-done
`);

assertEq(!!log.match(regex), true)

assertEq(dbg1.log, 'debugger,uncaught,step,enter,bp,exception,pop,');
assertEq(dbg2.log, 'debugger,uncaught,step,enter,bp,exception,pop,');
assertEq(dbg3.log, 'debugger,uncaught,step,enter,bp,exception,pop,');