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 --promise-extra
      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 two Exception debug events:
     10 //  1) when the exception is thrown in the promise q.
     11 //  2) when calling the undefined custom reject closure in MyPromise throws.
     12 
     13 Debug = debug.Debug;
     14 
     15 var expected_events = 2;
     16 var log = [];
     17 
     18 var p = new Promise(function(resolve, reject) {
     19   log.push("resolve");
     20   resolve();
     21 });
     22 
     23 function MyPromise(resolver) {
     24   var reject = undefined;
     25   var resolve = function() { };
     26   log.push("construct");
     27   resolver(resolve, reject);
     28 };
     29 
     30 MyPromise.prototype = new Promise(function() {});
     31 p.constructor = MyPromise;
     32 
     33 var q = p.chain(
     34   function() {
     35     log.push("throw caught");
     36     throw new Error("caught");  // event
     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       if (expected_events == 1) {
     45         assertTrue(
     46             exec_state.frame(0).sourceLineText().indexOf('// event') > 0);
     47         assertEquals("caught", event_data.exception().message);
     48       } else if (expected_events == 0) {
     49         // All of the frames on the stack are from native Javascript.
     50         assertEquals(0, exec_state.frameCount());
     51         assertEquals("(var).reject is not a function",
     52                      event_data.exception().message);
     53       } else {
     54         assertUnreachable();
     55       }
     56       assertSame(q, event_data.promise());
     57     }
     58   } catch (e) {
     59     %AbortJS(e + "\n" + e.stack);
     60   }
     61 }
     62 
     63 Debug.setBreakOnUncaughtException();
     64 Debug.setListener(listener);
     65 
     66 log.push("end main");
     67 
     68 function testDone(iteration) {
     69   function checkResult() {
     70     try {
     71       assertTrue(iteration < 10);
     72       if (expected_events === 0) {
     73         assertEquals(["resolve", "construct", "end main", "throw caught"], log);
     74       } else {
     75         testDone(iteration + 1);
     76       }
     77     } catch (e) {
     78       %AbortJS(e + "\n" + e.stack);
     79     }
     80   }
     81 
     82   %EnqueueMicrotask(checkResult);
     83 }
     84 
     85 testDone(0);
     86