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: --harmony-promises --expose-debug-as debug
6
7// Test debug events when an exception is thrown inside a Promise, which is
8// caught by a custom promise, which has no reject handler.
9// We expect an Exception event with a promise to be triggered.
10
11Debug = debug.Debug;
12
13var log = [];
14var step = 0;
15
16var p = new Promise(function(resolve, reject) {
17  log.push("resolve");
18  resolve();
19});
20
21function MyPromise(resolver) {
22  var reject = undefined;
23  var resolve = function() { };
24  log.push("construct");
25  resolver(resolve, reject);
26};
27
28MyPromise.prototype = p;
29p.constructor = MyPromise;
30
31var q = p.chain(
32  function() {
33    log.push("throw caught");
34    throw new Error("caught");  // event
35  });
36
37function listener(event, exec_state, event_data, data) {
38  try {
39    if (event == Debug.DebugEvent.Exception) {
40      assertEquals(["resolve", "construct", "end main", "throw caught"], log);
41      assertEquals("undefined is not a function",
42                   event_data.exception().message);
43      assertEquals(q, event_data.promise());
44    }
45  } catch (e) {
46    // Signal a failure with exit code 1.  This is necessary since the
47    // debugger swallows exceptions and we expect the chained function
48    // and this listener to be executed after the main script is finished.
49    print("Unexpected exception: " + e + "\n" + e.stack);
50    quit(1);
51  }
52}
53
54Debug.setBreakOnUncaughtException();
55Debug.setListener(listener);
56
57log.push("end main");
58