File indexing completed on 2024-04-28 15:28:37

0001 
0002 // Easy case -- normal try-finally
0003 out = "";
0004 try {
0005     while(true) {
0006         out += "loop;";
0007         break;
0008     }
0009     out += "after;"
0010 } catch(e) {
0011     out += "catch;"
0012 } finally {
0013     out += "finally;"
0014 }
0015 
0016 shouldBe('out', '"loop;after;finally;"');
0017 
0018 // A try nested inside the loop
0019 out = "";
0020 while (true) {
0021     out += "loop;";
0022     try {
0023         break;
0024     } catch(e) {
0025         out += "catch;"
0026     } finally {
0027         out += "finally;"
0028     }
0029     out += "after-try;";
0030 }
0031 out += "after-loop";
0032 
0033 shouldBe('out', '"loop;finally;after-loop"');
0034 
0035 
0036 // Throwing in a break shouldn't change things
0037 out = "";
0038 while (true) {
0039     out += "loop;";
0040     try {
0041         break;
0042     } catch(e) {
0043         out += "catch;"
0044     } finally {
0045         out += "finally;"
0046         break;
0047     }
0048     out += "after-try;";
0049 }
0050 out += "after-loop";
0051 
0052 shouldBe('out', '"loop;finally;after-loop"');
0053 
0054 // Continue & finally overriding the abrupt completion
0055 out = "";
0056 while (true) {
0057     out += "loop;";
0058     try {
0059         continue;
0060     } catch(e) {
0061         out += "catch;"
0062     } finally {
0063         out += "finally;"
0064         break;
0065     }
0066     out += "after-try;";
0067 }
0068 out += "after-loop";
0069 shouldBe('out', '"loop;finally;after-loop"');
0070 
0071 // Multi-level breaks. Fun.
0072 out = "";
0073 outer: while (true) {
0074     out += "out;"
0075 
0076     try {
0077         while (true) {
0078             out += "in;"
0079             break outer;
0080         }
0081     } finally {
0082         out += "finally;";
0083     }
0084     out += "after-2";
0085 }
0086 out += "after-1";
0087 
0088 shouldBe('out', '"out;in;finally;after-1"');
0089 
0090 
0091 // This testcase is by Oliver Hunt  (oliver at apple dt com)
0092 msg="global";
0093 
0094 out="";
0095 function record(str) {
0096     out += "<" + str + ">";
0097 }
0098 
0099 function f() {
0100     L3: do {
0101         try {
0102             with ({msg:"outer3"}) {
0103                 L2: do {
0104                     try {
0105                         with ({msg:"outer2"}) {
0106                             L1: do {
0107                                                                         try {
0108                                                                                 with ({msg:"outer1"}) {
0109                                                                                         record(msg);
0110                                                                                         continue L1;
0111                                                                                 }
0112                                                                         } finally {
0113                                                                                 record("finally1")
0114                                                                                 return;
0115                                                                         }
0116                             } while(0)
0117                             record(msg);
0118                             continue L2;
0119                         }
0120                     } finally {
0121                         record("finally2")
0122                         return;
0123                     }
0124                 } while(0)
0125                 record(msg);
0126                 continue L3;
0127             }
0128         } finally {
0129             record("finally3")
0130             return;
0131         }
0132         record("while");
0133     } while(0);
0134 }
0135 f();
0136 record(msg);
0137 shouldBe('out', '"<outer1><finally1><finally2><finally3><global>"');
0138 
0139 print("Done.");