summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/debug/Frame-environment-06.js
blob: 6faea6ad529a5a1b840c53a518f8eda89ba09889 (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
// |jit-test| error:all-jobs-completed-successfully
// Test that Debugger.Frame.prototype.environment works on suspended
// async function.

load(libdir + "asserts.js");

const g = newGlobal({ newCompartment: true });
const dbg = new Debugger(g);

g.eval(`
var resolveTop;
var resolveBlock;
var resolveLoop;
var resolveCatch;

async function f() {
  var promises = {
    top: new Promise(r => { resolveTop = r; }),
    block: new Promise(r => { resolveBlock = r; }),
    loop: new Promise(r => { resolveLoop = r; }),
    catch: new Promise(r => { resolveCatch = r; }),
  };

  var value = 42;
  Promise.resolve().then(resolveTop);
  await promises.top;
  {
    let block = "block";
    Promise.resolve().then(resolveBlock);
    await promises.block;
  }
  for (let loop of ["loop"]) {
    Promise.resolve().then(resolveLoop);
    await promises.loop;
  }
  try {
    throw "err";
  } catch (err) {
    Promise.resolve().then(resolveCatch);
    await promises.catch;
  }
  return value;
}
`);

const waitForOnPop = frame => new Promise(r => {
  assertEq(frame.onPop, undefined);
  frame.onPop = () => {
    frame.onPop = undefined;
    r();
  };
});

let frame;
dbg.onEnterFrame = f => {
  frame = f;
  dbg.onEnterFrame = undefined;
};

(async () => {
  const promise = g.f();

  assertEq(!!frame, true);
  assertEq(
    JSON.stringify(frame.environment.names()),
    JSON.stringify(["arguments", "promises", "value"])
  );
  assertEq(frame.environment.getVariable("value"), 42);

  frame.environment.setVariable("value", 43);

  assertEq(frame.environment.getVariable("value"), 43);

  await waitForOnPop(frame);

  assertEq(
    JSON.stringify(frame.environment.names()),
    JSON.stringify(["block"])
  );
  assertEq(frame.environment.getVariable("block"), "block");

  await waitForOnPop(frame);

  assertEq(
    JSON.stringify(frame.environment.names()),
    JSON.stringify(["loop"])
  );
  assertEq(frame.environment.getVariable("loop"), "loop");

  await waitForOnPop(frame);

  assertEq(
    JSON.stringify(frame.environment.names()),
    JSON.stringify(["err"])
  );
  assertEq(frame.environment.getVariable("err"), "err");

  const result = await promise;

  assertEq(result, 43);

  assertThrowsInstanceOf(() => frame.environment, Error);

  throw "all-jobs-completed-successfully";
})();