• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 --harmony-collections
6
7Debug = debug.Debug
8
9var exception = false;
10
11function listener(event, exec_state, event_data, data) {
12  try {
13    if (event == Debug.DebugEvent.Break) {
14      if (breaks == 0) {
15        exec_state.prepareStep(Debug.StepAction.StepIn, 2);
16        breaks = 1;
17      } else if (breaks <= 3) {
18        breaks++;
19        // Check whether we break at the expected line.
20        print(event_data.sourceLineText());
21        assertTrue(event_data.sourceLineText().indexOf("Expected to step") > 0);
22        exec_state.prepareStep(Debug.StepAction.StepIn, 3);
23      }
24    }
25  } catch (e) {
26    exception = true;
27  }
28}
29
30function cb_set(num) {
31  print("element " + num);  // Expected to step to this point.
32  return true;
33}
34
35function cb_map(key, val) {
36  print("key " + key + ", value " + val);  // Expected to step to this point.
37  return true;
38}
39
40var s = new Set();
41s.add(1);
42s.add(2);
43s.add(3);
44s.add(4);
45
46var m = new Map();
47m.set('foo', 1);
48m.set('bar', 2);
49m.set('baz', 3);
50m.set('bat', 4);
51
52Debug.setListener(listener);
53
54var breaks = 0;
55debugger;
56s.forEach(cb_set);
57assertFalse(exception);
58assertEquals(4, breaks);
59
60breaks = 0;
61debugger;
62m.forEach(cb_map);
63assertFalse(exception);
64assertEquals(4, breaks);
65
66Debug.setListener(null);
67
68
69// Test two levels of builtin callbacks:
70// Array.forEach calls a callback function, which by itself uses
71// Array.forEach with another callback function.
72
73function second_level_listener(event, exec_state, event_data, data) {
74  try {
75    if (event == Debug.DebugEvent.Break) {
76      if (breaks == 0) {
77        exec_state.prepareStep(Debug.StepAction.StepIn, 3);
78        breaks = 1;
79      } else if (breaks <= 16) {
80        breaks++;
81        // Check whether we break at the expected line.
82        assertTrue(event_data.sourceLineText().indexOf("Expected to step") > 0);
83        // Step two steps further every four breaks to skip the
84        // forEach call in the first level of recurision.
85        var step = (breaks % 4 == 1) ? 6 : 3;
86        exec_state.prepareStep(Debug.StepAction.StepIn, step);
87      }
88    }
89  } catch (e) {
90    exception = true;
91  }
92}
93
94function cb_set_foreach(num) {
95  s.forEach(cb_set);
96  print("back to the first level of recursion.");
97}
98
99function cb_map_foreach(key, val) {
100  m.forEach(cb_set);
101  print("back to the first level of recursion.");
102}
103
104Debug.setListener(second_level_listener);
105
106breaks = 0;
107debugger;
108s.forEach(cb_set_foreach);
109assertFalse(exception);
110assertEquals(17, breaks);
111
112breaks = 0;
113debugger;
114m.forEach(cb_map_foreach);
115assertFalse(exception);
116assertEquals(17, breaks);
117
118Debug.setListener(null);
119