tor-browser

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

throw.js (2191B)


      1 function thrower1(x) {
      2    throw x + 2;
      3 
      4    // Dead code, should be ignored.
      5    throw ++x;
      6    return x;
      7 }
      8 function test1() {
      9    // If we ever inline functions containing JSOP_THROW,
     10    // this shouldn't assert.
     11    function f(x) {
     12        thrower1(x + 1);
     13    }
     14    for (var i=0; i<11000; i++) {
     15        try {
     16            f(i);
     17            assertEq(0, 1);
     18        } catch(e) {
     19            assertEq(e, i + 3);
     20        }
     21    }
     22 }
     23 test1();
     24 
     25 // Test throwing from an uncompilable (interpreted) function.
     26 function getException(f) {
     27    try {
     28        f();
     29        assertEq(0, 1);
     30    } catch(e) {
     31        return e;
     32    }
     33    assertEq(0, 1);
     34 }
     35 
     36 function thrower2(x) {
     37    if (x > 90)
     38        throw x;
     39    with ({}) {}; // Abort compilation...(?)
     40 }
     41 function test2() {
     42    for (var i = 0; i < 100; i++) {
     43        thrower2(i);
     44    }
     45 }
     46 assertEq(getException(test2), 91);
     47 
     48 // Throwing |this| from a constructor.
     49 function thrower3(x) {
     50    this.x = x;
     51    if (x > 90)
     52        throw this;
     53 }
     54 function test3() {
     55    for (var i=0; i < 100; i++) {
     56        new thrower3(i);
     57    }
     58 }
     59 assertEq(getException(test3).x, 91);
     60 
     61 // Throwing an exception in various loop blocks.
     62 var count = 0;
     63 function thrower4(x) {
     64    throw count++;
     65    count += 12345; // Shouldn't be executed.
     66 }
     67 function test4_1() {
     68    var i = 0;
     69    for (new thrower4(i); i < 100; i++) {
     70        count += 2000; // Shouldn't be executed.
     71    }
     72 }
     73 function test4_2() {
     74    for (var i = 0; thrower4(i); i++) {
     75 count += 3000; // Shouldn't be executed.
     76    }
     77 }
     78 function test4_3() {
     79    for (var i = 0; i < 100; thrower4(i)) {
     80 count += 5;
     81    }
     82 }
     83 function test4_4() {
     84    for (var i = 0; i < 10; i++) {
     85        if (i > 8)
     86            thrower4();
     87        count += i;
     88    }
     89 }
     90 for (var i = 0; i < 100; i++) {
     91    assertEq(getException(test4_1), count-1);
     92    assertEq(getException(test4_2), count-1);
     93    assertEq(getException(test4_3), count-1);
     94    assertEq(getException(test4_4), count-1);
     95 }
     96 assertEq(count, 4500);
     97 
     98 function test5() {
     99    var res = 0;
    100    for (var i=0; i<40; i++) {
    101 try {
    102     throw i;
    103 } catch (e) {
    104          if (e % 2)
    105        res += e;
    106          else
    107        res += e * 3;
    108 }
    109    }
    110    return res;
    111 }
    112 assertEq(test5(), 1540);