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 32Debug = debug.Debug 33 34var exception = false; 35 36function 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 55function cb_false(num) { 56 print("element " + num); // Expected to step to this point. 57 return false; 58} 59 60function cb_true(num) { 61 print("element " + num); // Expected to step to this point. 62 return true; 63} 64 65function cb_reduce(a, b) { 66 print("elements " + a + " and " + b); // Expected to step to this point. 67 return a + b; 68} 69 70var a = [1, 2, 3, 4]; 71 72Debug.setListener(array_listener); 73 74var breaks = 0; 75debugger; 76a.forEach(cb_true); 77assertFalse(exception); 78assertEquals(4, breaks); 79 80breaks = 0; 81debugger; 82a.some(cb_false); 83assertFalse(exception); 84assertEquals(4, breaks); 85 86breaks = 0; 87debugger; 88a.every(cb_true); 89assertEquals(4, breaks); 90assertFalse(exception); 91 92breaks = 0; 93debugger; 94a.map(cb_true); 95assertFalse(exception); 96assertEquals(4, breaks); 97 98breaks = 0; 99debugger; 100a.filter(cb_true); 101assertFalse(exception); 102assertEquals(4, breaks); 103 104breaks = 0; 105debugger; 106a.reduce(cb_reduce); 107assertFalse(exception); 108assertEquals(4, breaks); 109 110breaks = 0; 111debugger; 112a.reduceRight(cb_reduce); 113assertFalse(exception); 114assertEquals(4, breaks); 115 116Debug.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 123function 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 144function cb_foreach(num) { 145 a.forEach(cb_true); 146 print("back to the first level of recursion."); 147} 148 149Debug.setListener(second_level_listener); 150 151breaks = 0; 152debugger; 153a.forEach(cb_foreach); 154assertFalse(exception); 155assertEquals(17, breaks); 156 157Debug.setListener(null); 158