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 = null;
35
36function array_listener(event, exec_state, event_data, data) {
37  try {
38    if (event == Debug.DebugEvent.Break) {
39      print(event_data.sourceLineText(), breaks);
40      assertTrue(event_data.sourceLineText().indexOf(`B${breaks++}`) > 0);
41      exec_state.prepareStep(Debug.StepAction.StepIn);
42    }
43  } catch (e) {
44    print(e);
45    quit();
46    exception = e;
47  }
48};
49
50function cb_false(num) {
51  print("element " + num);  // B2 B5 B8
52  return false;             // B3 B6 B9
53}                           // B4 B7 B10
54
55function cb_true(num) {
56  print("element " + num);  // B2 B5 B8
57  return true;              // B3 B6 B9
58}                           // B4 B7 B10
59
60function cb_reduce(a, b) {
61  print("elements " + a + " and " + b);  // B2 B5
62  return a + b;                          // B3 B6
63}                                        // B4 B7
64
65var a = [1, 2, 3];
66
67var breaks = 0;
68Debug.setListener(array_listener);
69debugger;                 // B0
70a.forEach(cb_true);       // B1
71Debug.setListener(null);  // B11
72assertNull(exception);
73assertEquals(12, breaks);
74
75breaks = 0;
76Debug.setListener(array_listener);
77debugger;                 // B0
78a.some(cb_false);         // B1
79Debug.setListener(null);  // B11
80assertNull(exception);
81assertEquals(12, breaks);
82
83breaks = 0;
84Debug.setListener(array_listener);
85debugger;                 // B0
86a.every(cb_true);         // B1
87Debug.setListener(null);  // B11
88assertNull(exception);
89assertEquals(12, breaks);
90
91breaks = 0;
92Debug.setListener(array_listener);
93debugger;                 // B0
94a.map(cb_true);           // B1
95Debug.setListener(null);  // B11
96assertNull(exception);
97assertEquals(12, breaks);
98
99breaks = 0;
100Debug.setListener(array_listener);
101debugger;                 // B0
102a.filter(cb_true);        // B1
103Debug.setListener(null);  // B11
104assertNull(exception);
105assertEquals(12, breaks);
106
107breaks = 0;
108Debug.setListener(array_listener);
109debugger;                 // B0
110a.reduce(cb_reduce);      // B1
111Debug.setListener(null);  // B8
112assertNull(exception);
113assertEquals(9, breaks);
114
115breaks = 0;
116Debug.setListener(array_listener);
117debugger;                  // B0
118a.reduceRight(cb_reduce);  // B1
119Debug.setListener(null);   // B8
120assertNull(exception);
121assertEquals(9, breaks);
122
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
128function 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
132
133function cb_foreach(num) {
134  a.forEach(cb_true_2);     // B2  B14 B20 B26
135  print("back.");           // B12 B18 B24 B36
136}                           // B13 B19 B25 B37
137
138breaks = 0;
139Debug.setListener(array_listener);
140debugger;                   // B0
141a.forEach(cb_foreach);      // B1
142Debug.setListener(null);    // B38
143assertNull(exception);
144assertEquals(39, breaks);
145