tor-browser

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

Script-isInCatchScope.js (3246B)


      1 // Test if isInCatchScope properly detects catch blocks.
      2 
      3 let g = newGlobal({newCompartment: true});
      4 let dbg = new Debugger(g);
      5 
      6 function test(string, mustBeCaught) {
      7    let index = 0;
      8    dbg.onExceptionUnwind = function (frame) {
      9        let willBeCaught = false;
     10        do {
     11            if (frame.script.isInCatchScope(frame.offset)) {
     12                willBeCaught = true;
     13                break;
     14            }
     15            frame = frame.older;
     16        } while (frame != null);
     17        assertEq(willBeCaught, mustBeCaught[index++]);
     18    };
     19 
     20    try {
     21        g.eval(string);
     22    } catch (ex) {}
     23    assertEq(index, mustBeCaught.length);
     24 }
     25 
     26 // Should correctly detect catch blocks
     27 test("throw new Error();", [false]);
     28 test("try { throw new Error(); } catch (e) {}", [true]);
     29 test("try { throw new Error(); } finally {}", [false, false]);
     30 test("try { throw new Error(); } catch (e) {} finally {}", [true]);
     31 
     32 // Source of the exception shouldn't matter
     33 test("(null)();", [false]);
     34 test("try { (null)(); } catch (e) {}", [true]);
     35 test("try { (null)(); } finally {}", [false, false]);
     36 test("try { (null)(); } catch (e) {} finally {}", [true]);
     37 
     38 // Should correctly detect catch blocks in functions
     39 test("function f() { throw new Error(); } f();", [false, false]);
     40 test("function f() { try { throw new Error(); } catch (e) {} } f();", [true]);
     41 test("function f() { try { throw new Error(); } finally {} } f();", [false, false, false]);
     42 test("function f() { try { throw new Error(); } catch (e) {} finally {} } f();", [true]);
     43 
     44 // Should correctly detect catch blocks in evals
     45 test("eval('throw new Error();')", [false, false]);
     46 test("eval('try { throw new Error(); } catch (e) {}');", [true]);
     47 test("eval('try { throw new Error(); } finally {}');", [false, false, false]);
     48 test("eval('try { throw new Error(); } catch (e) {} finally {}');", [true]);
     49 
     50 // Should correctly detect rethrows
     51 test("try { throw new Error(); } catch (e) { throw e; }", [true, false]);
     52 test("try { try { throw new Error(); } catch (e) { throw e; } } catch (e) {}", [true, true]);
     53 test("try { try { throw new Error(); } finally {} } catch (e) {}", [true, true]);
     54 test("function f() { try { throw new Error(); } catch (e) { throw e; } } f();", [true, false, false]);
     55 test("function f() { try { try { throw new Error(); } catch (e) { throw e; } } catch (e) {} } f();", [true, true]);
     56 test("function f() { try { try { throw new Error(); } finally {} } catch (e) {} } f();", [true, true]);
     57 test("eval('try { throw new Error(); } catch (e) { throw e; }')", [true, false, false]);
     58 test("eval('try { try { throw new Error(); } catch (e) { throw e; } } catch (e) {}')", [true, true]);
     59 
     60 // Should correctly detect catch blocks across frame boundaries
     61 test("function f() { throw new Error(); } try { f(); } catch (e) {}", [true, true]);
     62 test("function f() { throw new Error(); } try { f(); } catch (e) { throw e; }", [true, true, false]);
     63 test("try { eval('throw new Error()'); } catch (e) {}", [true, true]);
     64 test("try { eval('throw new Error()'); } catch (e) { throw e; }", [true, true, false]);
     65 
     66 // Should correctly detect catch blocks just before and just after throws
     67 test("throw new Error; try {} catch (e) {}", [false]);
     68 test("try {} catch (e) {} throw new Error();", [false]);