aboutsummaryrefslogtreecommitdiff
path: root/deps/v8/test/inspector/debugger/async-for-await-of-promise-stack.js
blob: 4e6c0bf15ecdde7bc89562cb0b20111ae5415c4e (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
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flags: --harmony-async-iteration

InspectorTest.log('Checks that async chains for for-await-of are correct.');

InspectorTest.addScript(`

function Debugger(value) {
  debugger;
}

function Reject(reason) {
  var reject;
  var promise = new Promise(function(resolvefn, rejectfn) {
    reject = rejectfn;
  });
  setTimeout(reject.bind(undefined, reason), 0);
  return promise;
}

function Throw(reason) {
  return {
    get then() { throw reason; }
  };
}

function ThrowOnReturn(items) {
  var it = items[Symbol.iterator]();
  return {
    [Symbol.iterator]() { return this; },
    next(v) { return it.next(v); },
    return(v) { throw new Error("boop"); }
  };
}

function RejectOnReturn(items) {
  var it = items[Symbol.iterator]();
  return {
    [Symbol.iterator]() { return this; },
    next(v) { return it.next(v); },
    return(v) { return Reject(new Error("boop")); }
  };
}

async function Basic() {
  for await (let x of ["a"]) {
    Debugger();
  }
}

async function UncaughtReject() {
  async function loop() {
    for await (let x of [Reject(new Error("boop"))]) {
      Debugger();
    }
  }
  return loop().catch(Debugger);
}

async function UncaughtThrow() {
  async function loop() {
    for await (let x of [Throw(new Error("boop"))]) {
      Debugger();
    }
  }
  return loop().catch(Debugger);
}

async function CaughtReject() {
  try {
    for await (let x of [Reject(new Error("boop"))]) {
      Debugger(x);
    }
  } catch (e) {
    Debugger(e);
  }
}

async function CaughtThrow() {
  try {
    for await (let x of [Throw(new Error("boop"))]) {
      Debugger(x);
    }
  } catch (e) {
    Debugger(e);
  }
}

async function UncaughtRejectOnBreak() {
  async function loop() {
    for await (let x of RejectOnReturn(["0", "1"])) {
      break;
    }
  }
  return loop().catch(Debugger);
}

async function UncaughtThrowOnBreak() {
  async function loop() {
    for await (let x of ThrowOnReturn(["0", "1"])) {
      break;
    }
  }
  return loop().catch(Debugger);
}

async function CaughtRejectOnBreak() {
  try {
    for await (let x of RejectOnReturn(["0", "1"])) {
      break;
    }
  } catch (e) {
    Debugger(e);
  }
}

async function CaughtThrowOnBreak() {
  try {
    for await (let x of ThrowOnReturn(["0", "1"])) {
      break;
    }
  } catch (e) {
    Debugger(e);
  }
}
//# sourceURL=test.js`, 7, 129);

InspectorTest.setupScriptMap();
Protocol.Debugger.onPaused(message => {
  InspectorTest.logCallFrames(message.params.callFrames);
  InspectorTest.logAsyncStackTrace(message.params.asyncStackTrace);
  InspectorTest.log('');
  Protocol.Debugger.resume();
});

Protocol.Debugger.enable();
Protocol.Debugger.setAsyncCallStackDepth({ maxDepth: 128 });
var testList = [
  'Basic',
  'UncaughtReject',
  'UncaughtThrow',
  'CaughtReject',
  'CaughtThrow',
  'UncaughtRejectOnBreak',
  'UncaughtThrowOnBreak',
  'CaughtRejectOnBreak',
  'CaughtThrowOnBreak',
]
InspectorTest.runTestSuite(testList.map(name => {
  return eval(`
  (function test${capitalize(name)}(next) {
    Protocol.Runtime.evaluate({ expression: \`${name}()
//# sourceURL=test${capitalize(name)}.js\`, awaitPromise: true})
      .then(next);
  })
  `);
}));

function capitalize(string) {
  return string.charAt(0).toUpperCase() + string.slice(1);
}