summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/ion/throw.js
blob: e81558194b983b7bc171ca4b4a688a7d0f0d8684 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
function thrower1(x) {
    throw x + 2;

    // Dead code, should be ignored.
    throw ++x;
    return x;
}
function test1() {
    // If we ever inline functions containing JSOP_THROW,
    // this shouldn't assert.
    function f(x) {
        thrower1(x + 1);
    }
    for (var i=0; i<11000; i++) {
        try {
            f(i);
            assertEq(0, 1);
        } catch(e) {
            assertEq(e, i + 3);
        }
    }
}
test1();

// Test throwing from an uncompilable (interpreted) function.
function getException(f) {
    try {
        f();
        assertEq(0, 1);
    } catch(e) {
        return e;
    }
    assertEq(0, 1);
}

function thrower2(x) {
    if (x > 90)
        throw x;
    with ({}) {}; // Abort compilation...(?)
}
function test2() {
    for (var i = 0; i < 100; i++) {
        thrower2(i);
    }
}
assertEq(getException(test2), 91);

// Throwing |this| from a constructor.
function thrower3(x) {
    this.x = x;
    if (x > 90)
        throw this;
}
function test3() {
    for (var i=0; i < 100; i++) {
        new thrower3(i);
    }
}
assertEq(getException(test3).x, 91);

// Throwing an exception in various loop blocks.
var count = 0;
function thrower4(x) {
    throw count++;
    count += 12345; // Shouldn't be executed.
}
function test4_1() {
    var i = 0;
    for (new thrower4(i); i < 100; i++) {
        count += 2000; // Shouldn't be executed.
    }
}
function test4_2() {
    for (var i = 0; thrower4(i); i++) {
	count += 3000; // Shouldn't be executed.
    }
}
function test4_3() {
    for (var i = 0; i < 100; thrower4(i)) {
	count += 5;
    }
}
function test4_4() {
    for (var i = 0; i < 10; i++) {
        if (i > 8)
            thrower4();
        count += i;
    }
}
for (var i = 0; i < 100; i++) {
    assertEq(getException(test4_1), count-1);
    assertEq(getException(test4_2), count-1);
    assertEq(getException(test4_3), count-1);
    assertEq(getException(test4_4), count-1);
}
assertEq(count, 4500);

function test5() {
    var res = 0;
    for (var i=0; i<40; i++) {
	try {
	    throw i;
	} catch (e) {
          if (e % 2)
	       res += e;
          else
	       res += e * 3;
	}
    }
    return res;
}
assertEq(test5(), 1540);