tor-browser

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

Frame-onStep-generators-03.js (1237B)


      1 // Stepping into the `.throw()` method of a generator with a relevant catch block.
      2 
      3 load(libdir + "asserts.js");
      4 
      5 let g = newGlobal({newCompartment: true});
      6 g.eval(`\
      7 function* z() {         // line 1
      8    try {               // 2
      9        yield 1;        // 3
     10    } catch (exc) {     // 4
     11        yield 2;        // 5
     12    }                   // 6
     13 }                       // 7
     14 function f() {          // 8
     15    let gen = z();      // 9
     16    gen.next();         // 10
     17    gen.throw("fit");   // 11
     18 }                       // 12
     19 `);
     20 
     21 let log = [];
     22 let previousLine = -1;
     23 let dbg = new Debugger(g);
     24 dbg.onEnterFrame = frame => {
     25    log.push(frame.callee.name + " in");
     26    frame.onStep = () => {
     27        let line = frame.script.getOffsetLocation(frame.offset).lineNumber;
     28        if (previousLine != line) { // We stepped to a new line.
     29            log.push(line);
     30            previousLine = line;
     31        }
     32    };
     33    frame.onPop = completion => {
     34        log.push(frame.callee.name + " out");
     35    };
     36 };
     37 
     38 g.f();
     39 assertEq(
     40    log.join(", "),
     41    "f in, 8, 9, z in, 1, z out, " +
     42    "9, 10, z in, 1, 2, 3, z out, " +
     43    "10, 11, z in, 3, 2, 4, 5, z out, " +  // not sure why we hit line 2 here, source notes bug maybe
     44    "11, 12, f out"
     45 );