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: --expose-debug-as debug
6
7Debug = debug.Debug
8
9var exception = null;
10
11function listener(event, exec_state, event_data, data) {
12  try {
13    if (event == Debug.DebugEvent.Break) {
14      exec_state.prepareStep(Debug.StepAction.StepIn);
15      print(event_data.sourceLineText());
16      assertTrue(
17          event_data.sourceLineText().indexOf(`B${breaks++}`) > 0);
18    }
19  } catch (e) {
20    print(e);
21    quit();
22    exception = e;
23  }
24}
25
26function cb_set(num) {
27  print("element " + num);  // B2 B5 B8
28  return true;              // B3 B6 B9
29}                           // B4 B7 B10
30
31function cb_map(key, val) {
32  print("key " + key + ", value " + val);  // B2 B5 B8
33  return true;                             // B3 B6 B9
34}                                          // B4 B7 B10
35
36var s = new Set();
37s.add(1);
38s.add(2);
39s.add(3);
40
41var m = new Map();
42m.set('foo', 1);
43m.set('bar', 2);
44m.set('baz', 3);
45
46var breaks = 0;
47Debug.setListener(listener);
48debugger;                 // B0
49s.forEach(cb_set);        // B1
50Debug.setListener(null);  // B11
51assertNull(exception);
52assertEquals(12, breaks);
53
54breaks = 0;
55Debug.setListener(listener);
56debugger;                 // B0
57m.forEach(cb_map);        // B1
58Debug.setListener(null);  // B11
59assertNull(exception);
60assertEquals(12, breaks);
61
62// Test two levels of builtin callbacks:
63// Array.forEach calls a callback function, which by itself uses
64// Array.forEach with another callback function.
65
66function cb_set_2(num) {
67  print("element " + num);  // B3 B6 B9  B15 B18 B21 B27 B30 B33
68  return true;              // B4 B7 B10 B16 B19 B22 B28 B31 B34
69}                           // B5 B8 B11 B17 B20 B23 B29 B32 B35
70
71function cb_map_2(k, v) {
72  print(`key ${k}, value ${v}`);  // B3 B6 B9  B15 B18 B21 B27 B30 B33
73  return true;                    // B4 B7 B10 B16 B19 B22 B28 B31 B34
74}                                 // B5 B8 B11 B17 B20 B23 B29 B32 B35
75
76function cb_set_foreach(num) {
77  s.forEach(cb_set_2);      // B2  B14 B26
78  print("back.");           // B12 B24 B36
79}                           // B13 B25 B37
80
81function cb_map_foreach(key, val) {
82  m.forEach(cb_map_2);      // B2  B14 B26
83  print("back.");           // B12 B24 B36
84}                           // B13 B25 B37
85
86breaks = 0;
87Debug.setListener(listener);
88debugger;                   // B0
89s.forEach(cb_set_foreach);  // B1
90Debug.setListener(null);    // B38
91assertNull(exception);
92assertEquals(39, breaks);
93
94breaks = 0;
95Debug.setListener(listener);
96debugger;                   // B0
97m.forEach(cb_map_foreach);  // B1
98Debug.setListener(null);    // B38
99assertNull(exception);
100assertEquals(39, breaks);
101