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: --allow-natives-syntax --expose-debug-as debug 6 7 Debug = debug.Debug; 8 9 var eventsExpected = 16; 10 var exception = null; 11 var result = []; 12 13 function updatePromise(promise, parentPromise, status, value) { 14 var i; 15 for (i = 0; i < result.length; ++i) { 16 if (result[i].promise === promise) { 17 result[i].parentPromise = parentPromise || result[i].parentPromise; 18 result[i].status = status || result[i].status; 19 result[i].value = value || result[i].value; 20 break; 21 } 22 } 23 assertTrue(i < result.length); 24 } 25 26 function listener(event, exec_state, event_data, data) { 27 if (event != Debug.DebugEvent.PromiseEvent) return; 28 try { 29 eventsExpected--; 30 assertTrue(event_data.promise().isPromise()); 31 if (event_data.status() === 0) { 32 // New promise. 33 assertEquals("pending", event_data.promise().status()); 34 result.push({ promise: event_data.promise().value(), status: 0 }); 35 assertTrue(exec_state.frame(0).sourceLineText().indexOf("// event") > 0); 36 } else if (event_data.status() !== undefined) { 37 // Resolve/reject promise. 38 updatePromise(event_data.promise().value(), 39 undefined, 40 event_data.status(), 41 event_data.value().value()); 42 } else { 43 // Chain promises. 44 assertTrue(event_data.parentPromise().isPromise()); 45 updatePromise(event_data.promise().value(), 46 event_data.parentPromise().value()); 47 assertTrue(exec_state.frame(0).sourceLineText().indexOf("// event") > 0); 48 } 49 } catch (e) { 50 print(e + e.stack) 51 exception = e; 52 } 53 } 54 55 Debug.setListener(listener); 56 57 function resolver(resolve, reject) { resolve(); } 58 59 var p1 = new Promise(resolver); // event 60 var p2 = p1.then().then(); // event 61 var p3 = new Promise(function(resolve, reject) { // event 62 reject("rejected"); 63 }); 64 var p4 = p3.then(); // event 65 var p5 = p1.then(); // event 66 67 function assertAsync(b, s) { 68 if (b) { 69 print(s, "succeeded"); 70 } else { 71 %AbortJS(s + " FAILED!"); 72 } 73 } 74 75 function testDone(iteration) { 76 function checkResult() { 77 if (eventsExpected === 0) { 78 assertAsync(result.length === 6, "result.length"); 79 80 assertAsync(result[0].promise === p1, "result[0].promise"); 81 assertAsync(result[0].parentPromise === undefined, 82 "result[0].parentPromise"); 83 assertAsync(result[0].status === 1, "result[0].status"); 84 assertAsync(result[0].value === undefined, "result[0].value"); 85 86 assertAsync(result[1].parentPromise === p1, 87 "result[1].parentPromise"); 88 assertAsync(result[1].status === 1, "result[1].status"); 89 90 assertAsync(result[2].promise === p2, "result[2].promise"); 91 92 assertAsync(result[3].promise === p3, "result[3].promise"); 93 assertAsync(result[3].parentPromise === undefined, 94 "result[3].parentPromise"); 95 assertAsync(result[3].status === -1, "result[3].status"); 96 assertAsync(result[3].value === "rejected", "result[3].value"); 97 98 assertAsync(result[4].promise === p4, "result[4].promise"); 99 assertAsync(result[4].parentPromise === p3, 100 "result[4].parentPromise"); 101 assertAsync(result[4].status === -1, "result[4].status"); 102 assertAsync(result[4].value === "rejected", "result[4].value"); 103 104 assertAsync(result[5].promise === p5, "result[5].promise"); 105 assertAsync(result[5].parentPromise === p1, 106 "result[5].parentPromise"); 107 assertAsync(result[5].status === 1, "result[5].status"); 108 109 assertAsync(exception === null, "exception === null"); 110 Debug.setListener(null); 111 } else if (iteration > 10) { 112 %AbortJS("Not all events were received!"); 113 } else { 114 testDone(iteration + 1); 115 } 116 } 117 118 var iteration = iteration || 0; 119 %EnqueueMicrotask(checkResult); 120 } 121 122 testDone(); 123