Home | History | Annotate | Download | only in debug-promises
      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 
     13 Debug = debug.Debug;
     14 
     15 var expected_events = 2;
     16 var log = [];
     17 
     18 var resolve, reject;
     19 var p0 = new Promise(function(res, rej) { resolve = res; reject = rej; });
     20 var p1 = p0.then(function() {
     21   log.push("p0.then");
     22   return Promise.reject(new Error("123"));
     23 });
     24 var p2 = p1.then(function() {
     25   log.push("p1.then");
     26 });
     27 
     28 var q = new Promise(function(res, rej) {
     29   log.push("resolve q");
     30   res();
     31 });
     32 
     33 q.then(function() {
     34   log.push("resolve p");
     35   resolve();
     36 })
     37 
     38 
     39 function 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         assertEquals(0, exec_state.frameCount());
     49         assertSame(p1, event_data.promise());
     50       } else {
     51         // p2 is rejected by p1's default reject handler.
     52         assertEquals(0, exec_state.frameCount());
     53         assertSame(p2, event_data.promise());
     54       }
     55     }
     56   } catch (e) {
     57     %AbortJS(e + "\n" + e.stack);
     58   }
     59 }
     60 
     61 Debug.setBreakOnUncaughtException();
     62 Debug.setListener(listener);
     63 
     64 log.push("end main");
     65 
     66 function testDone(iteration) {
     67   function checkResult() {
     68     try {
     69       assertTrue(iteration < 10);
     70       if (expected_events === 0) {
     71         assertEquals(["resolve q", "end main", "resolve p", "p0.then"], log);
     72       } else {
     73         testDone(iteration + 1);
     74       }
     75     } catch (e) {
     76       %AbortJS(e + "\n" + e.stack);
     77     }
     78   }
     79 
     80   %EnqueueMicrotask(checkResult);
     81 }
     82 
     83 testDone(0);
     84