1// Copyright 2014 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Flags: --expose-debug-as debug --allow-natives-syntax
6
7// Test debug events when we only listen to uncaught exceptions and
8// there is only a default reject handler for the to-be-rejected Promise.
9// We expect two Exception debug events:
10//  - when the first Promise is rejected and only has default reject handlers.
11//  - when the default reject handler passes the rejection on.
12
13Debug = debug.Debug;
14
15var expected_events = 2;
16var log = [];
17
18var resolve, reject;
19var p0 = new Promise(function(res, rej) { resolve = res; reject = rej; });
20var p1 = p0.then(function() {
21  log.push("p0.then");
22  throw new Error("123");  // event
23});
24var p2 = p1.then(function() {
25  log.push("p1.then");
26});
27
28var q = new Promise(function(res, rej) {
29  log.push("resolve q");
30  res();
31});
32
33q.then(function() {
34  log.push("resolve p");
35  resolve();
36})
37
38
39function listener(event, exec_state, event_data, data) {
40  try {
41    if (event == Debug.DebugEvent.Exception) {
42      expected_events--;
43      assertTrue(expected_events >= 0);
44      assertTrue(event_data.uncaught());
45      assertTrue(event_data.promise() instanceof Promise);
46      if (expected_events == 1) {
47        // p1 is rejected, uncaught except for its default reject handler.
48        assertTrue(
49            exec_state.frame(0).sourceLineText().indexOf("// event") > 0);
50        assertSame(p1, event_data.promise());
51      } else {
52        // p2 is rejected by p1's default reject handler.
53        assertEquals(0, exec_state.frameCount());
54        assertSame(p2, event_data.promise());
55      }
56    }
57  } catch (e) {
58    %AbortJS(e + "\n" + e.stack);
59  }
60}
61
62Debug.setBreakOnUncaughtException();
63Debug.setListener(listener);
64
65log.push("end main");
66
67function testDone(iteration) {
68  function checkResult() {
69    try {
70      assertTrue(iteration < 10);
71      if (expected_events === 0) {
72        assertEquals(["resolve q", "end main", "resolve p", "p0.then"], log);
73      } else {
74        testDone(iteration + 1);
75      }
76    } catch (e) {
77      %AbortJS(e + "\n" + e.stack);
78    }
79  }
80
81  // Run testDone through the Object.observe processing loop.
82  var dummy = {};
83  Object.observe(dummy, checkResult);
84  dummy.dummy = dummy;
85}
86
87testDone(0);
88