tor-browser

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

Frame-onStep-generators-02.js (1287B)


      1 // Stepping into the `.throw()` method of a generator with no relevant catch block.
      2 //
      3 // The debugger fires onEnterFrame and then frame.onPop for the generator frame when
      4 // `gen.throw()` is called.
      5 
      6 load(libdir + "asserts.js");
      7 
      8 let g = newGlobal({newCompartment: true});
      9 g.eval(`\
     10 function* z() {         // line 1
     11    yield 1;            // 2
     12    yield 2;            // 3
     13 }                       // 4
     14 function f() {          // 5
     15    let gen = z();      // 6
     16    gen.next();         // 7
     17    gen.throw("fit");   // 8
     18 }                       // 9
     19 `);
     20 
     21 let log = "";
     22 let previousLine = -1;
     23 let dbg = new Debugger(g);
     24 dbg.onEnterFrame = frame => {
     25    log += frame.callee.name + "{";
     26    frame.onStep = () => {
     27        let line = frame.script.getOffsetLocation(frame.offset).lineNumber;
     28        if (previousLine != line) { // We stepped to a new line.
     29            log += line;
     30            previousLine = line;
     31        }
     32    };
     33    frame.onPop = completion => {
     34        if ("throw" in completion)
     35            log += "!";
     36        log += "}";
     37    }
     38 };
     39 
     40 assertThrowsValue(() => g.f(), "fit");
     41 // z{1} is the initial generator setup.
     42 // z{12} is the first .next() call, running to `yield 1` on line 2
     43 // The final `z{2!}` is for the .throw() call.
     44 assertEq(log, "f{56z{1}67z{12}78z{2!}!}");