File: break-finally.js

package info (click to toggle)
kjs 5.103.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,020 kB
  • sloc: cpp: 36,704; javascript: 5,079; yacc: 790; perl: 191; sh: 52; makefile: 7
file content (139 lines) | stat: -rw-r--r-- 2,508 bytes parent folder | download | duplicates (2)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

// Easy case -- normal try-finally
out = "";
try {
    while(true) {
	out += "loop;";
	break;
    }
    out += "after;"
} catch(e) {
    out += "catch;"
} finally {
    out += "finally;"
}

shouldBe('out', '"loop;after;finally;"');

// A try nested inside the loop
out = "";
while (true) {
    out += "loop;";
    try {
	break;
    } catch(e) {
	out += "catch;"
    } finally {
	out += "finally;"
    }
    out += "after-try;";
}
out += "after-loop";

shouldBe('out', '"loop;finally;after-loop"');


// Throwing in a break shouldn't change things
out = "";
while (true) {
    out += "loop;";
    try {
	break;
    } catch(e) {
	out += "catch;"
    } finally {
	out += "finally;"
	break;
    }
    out += "after-try;";
}
out += "after-loop";

shouldBe('out', '"loop;finally;after-loop"');

// Continue & finally overriding the abrupt completion
out = "";
while (true) {
    out += "loop;";
    try {
	continue;
    } catch(e) {
	out += "catch;"
    } finally {
	out += "finally;"
	break;
    }
    out += "after-try;";
}
out += "after-loop";
shouldBe('out', '"loop;finally;after-loop"');

// Multi-level breaks. Fun.
out = "";
outer: while (true) {
    out += "out;"

    try {
	while (true) {
	    out += "in;"
	    break outer;
	}
    } finally {
	out += "finally;";
    }
    out += "after-2";
}
out += "after-1";

shouldBe('out', '"out;in;finally;after-1"');


// This testcase is by Oliver Hunt  (oliver at apple dt com)
msg="global";

out="";
function record(str) {
    out += "<" + str + ">";
}

function f() {
    L3: do {
        try {
            with ({msg:"outer3"}) {
                L2: do {
                    try {
                        with ({msg:"outer2"}) {
                            L1: do {
									try {
										with ({msg:"outer1"}) {
											record(msg);
											continue L1;
										}
									} finally {
										record("finally1")
										return;
									}
                            } while(0)
                            record(msg);
                            continue L2;
                        }
                    } finally {
                        record("finally2")
                        return;
                    }
                } while(0)
                record(msg);
                continue L3;
            }
        } finally {
            record("finally3")
            return;
        }
        record("while");
    } while(0);
}
f();
record(msg);
shouldBe('out', '"<outer1><finally1><finally2><finally3><global>"');

print("Done.");