1 // Copyright 2012 the V8 project authors. All rights reserved. 2 // Redistribution and use in source and binary forms, with or without 3 // modification, are permitted provided that the following conditions are 4 // met: 5 // 6 // * Redistributions of source code must retain the above copyright 7 // notice, this list of conditions and the following disclaimer. 8 // * Redistributions in binary form must reproduce the above 9 // copyright notice, this list of conditions and the following 10 // disclaimer in the documentation and/or other materials provided 11 // with the distribution. 12 // * Neither the name of Google Inc. nor the names of its 13 // contributors may be used to endorse or promote products derived 14 // from this software without specific prior written permission. 15 // 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28 // Flags: --expose-debug-as debug 29 30 // Test stepping into callbacks passed to builtin functions. 31 32 Debug = debug.Debug 33 34 var exception = false; 35 36 function array_listener(event, exec_state, event_data, data) { 37 try { 38 if (event == Debug.DebugEvent.Break) { 39 if (breaks == 0) { 40 exec_state.prepareStep(Debug.StepAction.StepIn, 2); 41 breaks = 1; 42 } else if (breaks <= 3) { 43 breaks++; 44 // Check whether we break at the expected line. 45 print(event_data.sourceLineText()); 46 assertTrue(event_data.sourceLineText().indexOf("Expected to step") > 0); 47 exec_state.prepareStep(Debug.StepAction.StepIn, 3); 48 } 49 } 50 } catch (e) { 51 exception = true; 52 } 53 }; 54 55 function cb_false(num) { 56 print("element " + num); // Expected to step to this point. 57 return false; 58 } 59 60 function cb_true(num) { 61 print("element " + num); // Expected to step to this point. 62 return true; 63 } 64 65 function cb_reduce(a, b) { 66 print("elements " + a + " and " + b); // Expected to step to this point. 67 return a + b; 68 } 69 70 var a = [1, 2, 3, 4]; 71 72 Debug.setListener(array_listener); 73 74 var breaks = 0; 75 debugger; 76 a.forEach(cb_true); 77 assertFalse(exception); 78 assertEquals(4, breaks); 79 80 breaks = 0; 81 debugger; 82 a.some(cb_false); 83 assertFalse(exception); 84 assertEquals(4, breaks); 85 86 breaks = 0; 87 debugger; 88 a.every(cb_true); 89 assertEquals(4, breaks); 90 assertFalse(exception); 91 92 breaks = 0; 93 debugger; 94 a.map(cb_true); 95 assertFalse(exception); 96 assertEquals(4, breaks); 97 98 breaks = 0; 99 debugger; 100 a.filter(cb_true); 101 assertFalse(exception); 102 assertEquals(4, breaks); 103 104 breaks = 0; 105 debugger; 106 a.reduce(cb_reduce); 107 assertFalse(exception); 108 assertEquals(4, breaks); 109 110 breaks = 0; 111 debugger; 112 a.reduceRight(cb_reduce); 113 assertFalse(exception); 114 assertEquals(4, breaks); 115 116 Debug.setListener(null); 117 118 119 // Test two levels of builtin callbacks: 120 // Array.forEach calls a callback function, which by itself uses 121 // Array.forEach with another callback function. 122 123 function second_level_listener(event, exec_state, event_data, data) { 124 try { 125 if (event == Debug.DebugEvent.Break) { 126 if (breaks == 0) { 127 exec_state.prepareStep(Debug.StepAction.StepIn, 3); 128 breaks = 1; 129 } else if (breaks <= 16) { 130 breaks++; 131 // Check whether we break at the expected line. 132 assertTrue(event_data.sourceLineText().indexOf("Expected to step") > 0); 133 // Step two steps further every four breaks to skip the 134 // forEach call in the first level of recurision. 135 var step = (breaks % 4 == 1) ? 6 : 3; 136 exec_state.prepareStep(Debug.StepAction.StepIn, step); 137 } 138 } 139 } catch (e) { 140 exception = true; 141 } 142 }; 143 144 function cb_foreach(num) { 145 a.forEach(cb_true); 146 print("back to the first level of recursion."); 147 } 148 149 Debug.setListener(second_level_listener); 150 151 breaks = 0; 152 debugger; 153 a.forEach(cb_foreach); 154 assertFalse(exception); 155 assertEquals(17, breaks); 156 157 Debug.setListener(null); 158