Frame-onStep-lines-01.js (2991B)
1 // Test that a frame's onStep handler gets called at least once on each line of a function. 2 3 var g = newGlobal({newCompartment: true}); 4 var dbg = new Debugger(g); 5 6 // When we hit a 'debugger' statement, set offsets to the frame's script's 7 // table of line offsets --- a sparse array indexed by line number. Begin 8 // single-stepping the current frame; for each source line we hit, delete 9 // the line's entry in offsets. Thus, at the end, offsets is an array with 10 // an element for each line we did not reach. 11 var doSingleStep = true; 12 var offsets; 13 dbg.onDebuggerStatement = function (frame) { 14 var script = frame.script; 15 offsets = script.getAllOffsets(); 16 print("debugger line: " + script.getOffsetLocation(frame.offset).lineNumber); 17 print("original lines: " + JSON.stringify(Object.keys(offsets))); 18 if (doSingleStep) { 19 frame.onStep = function onStepHandler() { 20 var line = script.getOffsetLocation(this.offset).lineNumber; 21 delete offsets[line]; 22 }; 23 } 24 }; 25 26 g.eval( 27 'function t(a, b, c) { \n' + 28 ' debugger; \n' + 29 ' var x = a; \n' + 30 ' x += b; \n' + 31 ' if (x < 10) \n' + 32 ' x -= c; \n' + 33 ' return x; \n' + 34 '} \n' 35 ); 36 37 // This should stop at every line but the first of the function. 38 g.eval('t(1,2,3)'); 39 assertEq(Object.keys(offsets).length, 1); 40 41 // This should stop at every line but the first of the function, and the 42 // body of the 'if'. 43 g.eval('t(10,20,30)'); 44 assertEq(Object.keys(offsets).length, 2); 45 46 // This shouldn't stop at all. It's the frame that's in single-step mode, 47 // not the script, so the prior execution of t in single-step mode should 48 // have no effect on this one. 49 doSingleStep = false; 50 g.eval('t(0, 0, 0)'); 51 assertEq(Object.keys(offsets).length, 7); 52 doSingleStep = true; 53 54 // Single-step in an eval frame. This should reach every line but the 55 // first. 56 g.eval( 57 'debugger; \n' + 58 'var a=1, b=2, c=3; \n' + 59 'var x = a; \n' + 60 'x += b; \n' + 61 'if (x < 10) \n' + 62 ' x -= c; \n' 63 ); 64 print("final lines: " + JSON.stringify(Object.keys(offsets))); 65 assertEq(Object.keys(offsets).length, 1); 66 67 // Single-step in a global code frame. This should reach every line but the 68 // first. 69 g.evaluate( 70 'debugger; \n' + 71 'var a=1, b=2, c=3; \n' + 72 'var x = a; \n' + 73 'x += b; \n' + 74 'if (x < 10) \n' + 75 ' x -= c; \n' 76 ); 77 print("final lines: " + JSON.stringify(Object.keys(offsets))); 78 assertEq(Object.keys(offsets).length, 1);