debug-stepin-collections-foreach.js 2.69 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//

Debug = debug.Debug

8
var exception = null;
9 10 11 12

function listener(event, exec_state, event_data, data) {
  try {
    if (event == Debug.DebugEvent.Break) {
13 14 15 16
      exec_state.prepareStep(Debug.StepAction.StepIn);
      print(event_data.sourceLineText());
      assertTrue(
          event_data.sourceLineText().indexOf(`B${breaks++}`) > 0);
17 18
    }
  } catch (e) {
19 20 21
    print(e);
    quit();
    exception = e;
22 23 24 25
  }
}

function cb_set(num) {
26 27 28
  print("element " + num);  // B2 B5 B8
  return true;              // B3 B6 B9
}                           // B4 B7 B10
29 30

function cb_map(key, val) {
31 32 33
  print("key " + key + ", value " + val);  // B2 B5 B8
  return true;                             // B3 B6 B9
}                                          // B4 B7 B10
34 35 36 37 38 39 40 41 42 43 44 45

var s = new Set();
s.add(1);
s.add(2);
s.add(3);

var m = new Map();
m.set('foo', 1);
m.set('bar', 2);
m.set('baz', 3);

var breaks = 0;
46 47 48 49 50 51
Debug.setListener(listener);
debugger;                 // B0
s.forEach(cb_set);        // B1
Debug.setListener(null);  // B11
assertNull(exception);
assertEquals(12, breaks);
52 53

breaks = 0;
54 55 56 57 58 59
Debug.setListener(listener);
debugger;                 // B0
m.forEach(cb_map);        // B1
Debug.setListener(null);  // B11
assertNull(exception);
assertEquals(12, breaks);
60 61 62 63 64

// Test two levels of builtin callbacks:
// Array.forEach calls a callback function, which by itself uses
// Array.forEach with another callback function.

65 66 67 68 69 70 71 72 73
function cb_set_2(num) {
  print("element " + num);  // B3 B6 B9  B15 B18 B21 B27 B30 B33
  return true;              // B4 B7 B10 B16 B19 B22 B28 B31 B34
}                           // B5 B8 B11 B17 B20 B23 B29 B32 B35

function cb_map_2(k, v) {
  print(`key ${k}, value ${v}`);  // B3 B6 B9  B15 B18 B21 B27 B30 B33
  return true;                    // B4 B7 B10 B16 B19 B22 B28 B31 B34
}                                 // B5 B8 B11 B17 B20 B23 B29 B32 B35
74 75

function cb_set_foreach(num) {
76 77 78
  s.forEach(cb_set_2);      // B2  B14 B26
  print("back.");           // B12 B24 B36
}                           // B13 B25 B37
79 80

function cb_map_foreach(key, val) {
81 82 83
  m.forEach(cb_map_2);      // B2  B14 B26
  print("back.");           // B12 B24 B36
}                           // B13 B25 B37
84 85

breaks = 0;
86 87 88 89 90 91
Debug.setListener(listener);
debugger;                   // B0
s.forEach(cb_set_foreach);  // B1
Debug.setListener(null);    // B38
assertNull(exception);
assertEquals(39, breaks);
92 93

breaks = 0;
94 95 96 97 98 99
Debug.setListener(listener);
debugger;                   // B0
m.forEach(cb_map_foreach);  // B1
Debug.setListener(null);    // B38
assertNull(exception);
assertEquals(39, breaks);