blob: 4fde6e440f75675d81d268665bbba6e0d2e79d14 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000034var exception = null;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000035
36function array_listener(event, exec_state, event_data, data) {
37 try {
38 if (event == Debug.DebugEvent.Break) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000039 print(event_data.sourceLineText(), breaks);
40 assertTrue(event_data.sourceLineText().indexOf(`B${breaks++}`) > 0);
41 exec_state.prepareStep(Debug.StepAction.StepIn);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000042 }
43 } catch (e) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000044 print(e);
45 quit();
46 exception = e;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000047 }
48};
49
50function cb_false(num) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000051 print("element " + num); // B2 B5 B8
52 return false; // B3 B6 B9
53} // B4 B7 B10
Ben Murdochb8a8cc12014-11-26 15:28:44 +000054
55function cb_true(num) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000056 print("element " + num); // B2 B5 B8
57 return true; // B3 B6 B9
58} // B4 B7 B10
Ben Murdochb8a8cc12014-11-26 15:28:44 +000059
60function cb_reduce(a, b) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000061 print("elements " + a + " and " + b); // B2 B5
62 return a + b; // B3 B6
63} // B4 B7
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000065var a = [1, 2, 3];
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066
67var breaks = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000068Debug.setListener(array_listener);
69debugger; // B0
70a.forEach(cb_true); // B1
71Debug.setListener(null); // B11
72assertNull(exception);
73assertEquals(12, breaks);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074
75breaks = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000076Debug.setListener(array_listener);
77debugger; // B0
78a.some(cb_false); // B1
79Debug.setListener(null); // B11
80assertNull(exception);
81assertEquals(12, breaks);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000082
83breaks = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000084Debug.setListener(array_listener);
85debugger; // B0
86a.every(cb_true); // B1
87Debug.setListener(null); // B11
88assertNull(exception);
89assertEquals(12, breaks);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000090
91breaks = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000092Debug.setListener(array_listener);
93debugger; // B0
94a.map(cb_true); // B1
95Debug.setListener(null); // B11
96assertNull(exception);
97assertEquals(12, breaks);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000098
99breaks = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000100Debug.setListener(array_listener);
101debugger; // B0
102a.filter(cb_true); // B1
103Debug.setListener(null); // B11
104assertNull(exception);
105assertEquals(12, breaks);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000106
107breaks = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000108Debug.setListener(array_listener);
109debugger; // B0
110a.reduce(cb_reduce); // B1
111Debug.setListener(null); // B8
112assertNull(exception);
113assertEquals(9, breaks);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000114
115breaks = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000116Debug.setListener(array_listener);
117debugger; // B0
118a.reduceRight(cb_reduce); // B1
119Debug.setListener(null); // B8
120assertNull(exception);
121assertEquals(9, breaks);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000122
123
124// Test two levels of builtin callbacks:
125// Array.forEach calls a callback function, which by itself uses
126// Array.forEach with another callback function.
127
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000128function cb_true_2(num) {
129 print("element " + num); // B3 B6 B9 B15 B18 B21 B27 B30 B33
130 return true; // B4 B7 B10 B16 B19 B22 B28 B31 B34
131} // B5 B8 B11 B17 B20 B23 B29 B32 B35
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000132
133function cb_foreach(num) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000134 a.forEach(cb_true_2); // B2 B14 B20 B26
135 print("back."); // B12 B18 B24 B36
136} // B13 B19 B25 B37
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000137
138breaks = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000139Debug.setListener(array_listener);
140debugger; // B0
141a.forEach(cb_foreach); // B1
142Debug.setListener(null); // B38
143assertNull(exception);
144assertEquals(39, breaks);