Source-reparse.js (1434B)
1 // reparsing a source should produce equivalent scripts and avoid invoking the 2 // onNewScript hook. 3 4 const g = newGlobal({newCompartment: true}); 5 const dbg = new Debugger; 6 dbg.addDebuggee(g); 7 8 let globalScript; 9 dbg.onNewScript = script => { globalScript = script }; 10 11 // Leave `g()` on the first line so we can check that `columnNumber` is passed to the 12 // reparsed script (`columnNumber` is only used to offset breakpoint column on the first 13 // line of the script). 14 g.evaluate(`g(); 15 function f() { 16 for (var i = 0; i < 10; i++) { 17 g(); 18 } 19 } 20 21 function g() { 22 return 3; 23 } 24 25 f(); 26 `, { 27 fileName: "foobar.js", 28 lineNumber: 3, 29 columnNumber: 42, 30 }); 31 32 let onNewScriptCalls = 0; 33 dbg.onNewScript = script => { onNewScriptCalls++; }; 34 35 const reparsedScript = globalScript.source.reparse(); 36 37 assertEq(onNewScriptCalls, 0); 38 39 assertEq(reparsedScript.url, "foobar.js"); 40 assertEq(reparsedScript.startLine, 3); 41 assertEq(reparsedScript.startColumn, 42); 42 43 // Test for the same breakpoint positions in the original and reparsed script. 44 function getBreakpointPositions(script) { 45 const offsets = script.getPossibleBreakpoints(); 46 const str = offsets.map(({ lineNumber, columnNumber }) => `${lineNumber}:${columnNumber}`).toString(); 47 const childPositions = script.getChildScripts().map(getBreakpointPositions); 48 return str + childPositions.toString(); 49 } 50 assertEq(getBreakpointPositions(globalScript), getBreakpointPositions(reparsedScript));