js/src/jit-test/tests/debug/Script-isInCatchScope.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/jit-test/tests/debug/Script-isInCatchScope.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,68 @@
     1.4 +// Test if isInCatchScope properly detects catch blocks.
     1.5 +
     1.6 +let g = newGlobal();
     1.7 +let dbg = new Debugger(g);
     1.8 +
     1.9 +function test(string, mustBeCaught) {
    1.10 +    let index = 0;
    1.11 +    dbg.onExceptionUnwind = function (frame) {
    1.12 +        let willBeCaught = false;
    1.13 +        do {
    1.14 +            if (frame.script.isInCatchScope(frame.offset)) {
    1.15 +                willBeCaught = true;
    1.16 +                break;
    1.17 +            }
    1.18 +            frame = frame.older;
    1.19 +        } while (frame != null);
    1.20 +        assertEq(willBeCaught, mustBeCaught[index++]);
    1.21 +    };
    1.22 +
    1.23 +    try {
    1.24 +        g.eval(string);
    1.25 +    } catch (ex) {}
    1.26 +    assertEq(index, mustBeCaught.length);
    1.27 +}
    1.28 +
    1.29 +// Should correctly detect catch blocks
    1.30 +test("throw new Error();", [false]);
    1.31 +test("try { throw new Error(); } catch (e) {}", [true]);
    1.32 +test("try { throw new Error(); } finally {}", [false, false]);
    1.33 +test("try { throw new Error(); } catch (e) {} finally {}", [true]);
    1.34 +
    1.35 +// Source of the exception shouldn't matter
    1.36 +test("(null)();", [false]);
    1.37 +test("try { (null)(); } catch (e) {}", [true]);
    1.38 +test("try { (null)(); } finally {}", [false, false]);
    1.39 +test("try { (null)(); } catch (e) {} finally {}", [true]);
    1.40 +
    1.41 +// Should correctly detect catch blocks in functions
    1.42 +test("function f() { throw new Error(); } f();", [false, false]);
    1.43 +test("function f() { try { throw new Error(); } catch (e) {} } f();", [true]);
    1.44 +test("function f() { try { throw new Error(); } finally {} } f();", [false, false, false]);
    1.45 +test("function f() { try { throw new Error(); } catch (e) {} finally {} } f();", [true]);
    1.46 +
    1.47 +// Should correctly detect catch blocks in evals
    1.48 +test("eval('throw new Error();')", [false, false]);
    1.49 +test("eval('try { throw new Error(); } catch (e) {}');", [true]);
    1.50 +test("eval('try { throw new Error(); } finally {}');", [false, false, false]);
    1.51 +test("eval('try { throw new Error(); } catch (e) {} finally {}');", [true]);
    1.52 +
    1.53 +// Should correctly detect rethrows
    1.54 +test("try { throw new Error(); } catch (e) { throw e; }", [true, false]);
    1.55 +test("try { try { throw new Error(); } catch (e) { throw e; } } catch (e) {}", [true, true]);
    1.56 +test("try { try { throw new Error(); } finally {} } catch (e) {}", [true, true]);
    1.57 +test("function f() { try { throw new Error(); } catch (e) { throw e; } } f();", [true, false, false]);
    1.58 +test("function f() { try { try { throw new Error(); } catch (e) { throw e; } } catch (e) {} } f();", [true, true]);
    1.59 +test("function f() { try { try { throw new Error(); } finally {} } catch (e) {} } f();", [true, true]);
    1.60 +test("eval('try { throw new Error(); } catch (e) { throw e; }')", [true, false, false]);
    1.61 +test("eval('try { try { throw new Error(); } catch (e) { throw e; } } catch (e) {}')", [true, true]);
    1.62 +
    1.63 +// Should correctly detect catch blocks across frame boundaries
    1.64 +test("function f() { throw new Error(); } try { f(); } catch (e) {}", [true, true]);
    1.65 +test("function f() { throw new Error(); } try { f(); } catch (e) { throw e; }", [true, true, false]);
    1.66 +test("try { eval('throw new Error()'); } catch (e) {}", [true, true]);
    1.67 +test("try { eval('throw new Error()'); } catch (e) { throw e; }", [true, true, false]);
    1.68 +
    1.69 +// Should correctly detect catch blocks just before and just after throws
    1.70 +test("throw new Error; try {} catch (e) {}", [false]);
    1.71 +test("try {} catch (e) {} throw new Error();", [false]);

mercurial