tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

Script-getLineOffsets-05.js (2044B)


      1 // getLineOffsets identifies multiple ways to land on a line.
      2 
      3 var g = newGlobal({newCompartment: true});
      4 g.line0 = null;
      5 var dbg = Debugger(g);
      6 var where;
      7 dbg.onDebuggerStatement = function (frame) {
      8    var s = frame.script, lineno, offs;
      9 
     10    lineno = g.line0 + where;
     11    offs = s.getLineOffsets(lineno);
     12    for (var i = 0; i < offs.length; i++) {
     13        assertEq(s.getOffsetLocation(offs[i]).lineNumber, lineno);
     14        s.setBreakpoint(offs[i], {hit: function () { g.log += 'B'; }});
     15    }
     16 
     17    lineno++;
     18    offs = s.getLineOffsets(lineno);
     19    for (var i = 0; i < offs.length; i++) {
     20        assertEq(s.getOffsetLocation(offs[i]).lineNumber, lineno);
     21        s.setBreakpoint(offs[i], {hit: function () { g.log += 'C'; }});
     22    }
     23 
     24    g.log += 'A';
     25 };
     26 
     27 function test(s) {
     28    assertEq(s.charAt(s.length - 1), '\n');
     29    var count = (s.split(/\n/).length - 1); // number of lines in s
     30    g.log = '';
     31    where = 1 + count;
     32    g.eval("line0 = Error().lineNumber;\n" +
     33           "debugger;\n" +          // line0 + 1
     34           s +                      // line0 + 2 ... line0 + where
     35           "log += 'D';\n");
     36    assertEq(g.log, 'AB!CD');
     37 }
     38 
     39 // if-statement with yes and no paths on a single line
     40 g.i = 0;
     41 test("if (i === 0)\n" +
     42     "    log += '!'; else log += 'X';\n");
     43 test("if (i === 2)\n" +
     44     "    log += 'X'; else log += '!';\n");
     45 
     46 // break to a line that has code inside and outside the loop
     47 g.i = 2;
     48 test("while (1) {\n" +
     49     "    if (i === 2) break;\n" +
     50     "    log += 'X'; } log += '!';\n");
     51 
     52 // leaving a while loop by failing the test, when the last line has stuff both inside and outside the loop
     53 g.i = 0;
     54 test("while (i > 0) {\n" +
     55     "    if (i === 70) log += 'X';\n" +
     56     "    --i;  } log += '!';\n");
     57 
     58 // multiple case-labels on the same line
     59 g.i = 0;
     60 test("switch (i) {\n" +
     61     "    case 0: case 1: log += '!'; break; }\n");
     62 test("switch ('' + i) {\n" +
     63     "    case '0': case '1': log += '!'; break; }\n");
     64 test("switch (i) {\n" +
     65     "    case 'ok' + i: case i - i: log += '!'; break; }\n");