File indexing completed on 2024-04-28 15:28:37
0001 var global = this; 0002 function myfunc() { 0003 } 0004 function throwex() { 0005 throw new Error("test exception"); 0006 } 0007 0008 //--------------------------- 0009 var func_ret_with_ex_func = 4; 0010 try { 0011 func_ret_with_ex_func = throwex()(); 0012 } 0013 catch (e) { 0014 } 0015 shouldBe("func_ret_with_ex_func", "4"); 0016 0017 // --------------------------------- 0018 0019 var func_ret_from_ex_throw_args = 4; 0020 try { 0021 func_ret_from_ex_throw_args = Math.abs(throwex()); 0022 } 0023 catch (e) { 0024 } 0025 shouldBe("func_ret_from_ex_throw_args", "4"); 0026 0027 // --------------------------------- 0028 0029 var set_from_throw_func_args = 4; 0030 try { 0031 throwex()(set_from_throw_func_args = 1); 0032 } 0033 catch (e) { 0034 } 0035 shouldBe("set_from_throw_func_args","4"); 0036 0037 // --------------------------------- 0038 0039 var set_from_func_throw_args = 4; 0040 try { 0041 myfunc(throwex(), set_from_func_throw_args = 1); 0042 } 0043 catch (e) { 0044 } 0045 shouldBe("set_from_func_throw_args","4"); 0046 0047 // --------------------------------- 0048 0049 var set_from_before_func_throw_args = 4; 0050 try { 0051 myfunc(set_from_before_func_throw_args = 1, throwex()); 0052 } 0053 catch (e) { 0054 } 0055 shouldBe("set_from_before_func_throw_args","1"); 0056 0057 // --------------------------------- 0058 0059 // ### move to function.js 0060 var function_param_order = ""; 0061 function aparam() { 0062 function_param_order += "a"; 0063 } 0064 function bparam() { 0065 function_param_order += "b"; 0066 } 0067 function cparam() { 0068 function_param_order += "c"; 0069 } 0070 myfunc(aparam(),bparam(),cparam()); 0071 shouldBe("function_param_order","'abc'"); 0072 0073 // --------------------------------- 0074 // ### move to function.js 0075 var new_param_order = ""; 0076 function anewparam() { 0077 new_param_order += "a"; 0078 } 0079 function bnewparam() { 0080 new_param_order += "b"; 0081 } 0082 function cnewparam() { 0083 new_param_order += "c"; 0084 } 0085 new myfunc(anewparam(),bnewparam(),cnewparam()); 0086 shouldBe("new_param_order","'abc'"); 0087 0088 // --------------------------------- 0089 // ### move to function.js 0090 var elision_param_order = ""; 0091 function aelision() { 0092 elision_param_order += "a"; 0093 } 0094 function belision() { 0095 elision_param_order += "b"; 0096 } 0097 function celision() { 0098 elision_param_order += "c"; 0099 } 0100 [aelision(),belision(),celision()]; 0101 shouldBe("elision_param_order","'abc'"); 0102 0103 // --------------------------------- 0104 // ### move to function.js 0105 var comma_order = ""; 0106 function acomma() { 0107 comma_order += "a"; 0108 } 0109 function bcomma() { 0110 comma_order += "b"; 0111 } 0112 function ccomma() { 0113 comma_order += "c"; 0114 } 0115 acomma(),bcomma(),ccomma(); 0116 shouldBe("comma_order","'abc'"); 0117 0118 // --------------------------------- 0119 0120 function checkOperator(op,name) { 0121 var code =( 0122 "global."+name+"_part1 = 4;\n"+ 0123 "try {\n"+ 0124 " ("+name+"_part1 = 1) "+op+" throwex();\n"+ 0125 "}\n"+ 0126 "catch (e) {\n"+ 0127 "}\n"+ 0128 "shouldBe('"+name+"_part1', '1');\n"+ 0129 "global."+name+"_part2 = 4;\n"+ 0130 "try {\n"+ 0131 " throwex() "+op+" ("+name+"_part2 = 1);\n"+ 0132 "}\n"+ 0133 "catch (e) {\n"+ 0134 "}\n"+ 0135 "shouldBe('"+name+"_part2', '4');\n"); 0136 // print("\n\n\n"); 0137 // print(code); 0138 eval(code); 0139 } 0140 0141 checkOperator("==","OpEqEq"); 0142 checkOperator("!=","OpNotEq"); 0143 checkOperator("===","OpStrEq"); 0144 checkOperator("!==","OpStrNEq"); 0145 // ### these generate a syntax error in mozilla - kjs should do the same (?) 0146 //checkOperator("+=","OpPlusEq"); 0147 //checkOperator("-=","OpMinusEq"); 0148 //checkOperator("*=","OpMultEq"); 0149 //checkOperator("/=","OpDivEq"); 0150 // OpPlusPlus, 0151 // OpMinusMinus, 0152 checkOperator("<","OpLess"); 0153 checkOperator("<=","OpLessEq"); 0154 checkOperator(">","OpGreater"); 0155 checkOperator(">=","OpGreaterEq"); 0156 //checkOperator("&=","OpAndEq"); 0157 //checkOperator("^=","OpXOrEq"); 0158 //checkOperator("|=","OpOrEq"); 0159 //checkOperator("%=","OpModEq"); 0160 checkOperator("&&","OpAnd"); 0161 checkOperator("||","OpOr"); 0162 checkOperator("&","OpBitAnd"); 0163 checkOperator("^","OpBitXOr"); 0164 checkOperator("|","OpBitOr"); 0165 checkOperator("<<","OpLShift"); 0166 checkOperator(">>","OpRShift"); 0167 checkOperator(">>>","OpURShift"); 0168 // OpIn, 0169 checkOperator("instanceof","OpInstanceOf"); 0170 0171 // --------------------------------- 0172 var set_from_if_stmt = 4; 0173 try { 0174 if (throwex()) { 0175 set_from_if_stmt = 1; 0176 } 0177 } 0178 catch (e) { 0179 } 0180 shouldBe("set_from_if_stmt","4"); 0181 0182 // --------------------------------- 0183 var set_from_if_else_stmt = 4; 0184 try { 0185 if (throwex()) { 0186 set_from_if_else_stmt = 1; 0187 } 0188 else { 0189 undefined; 0190 } 0191 } 0192 catch (e) { 0193 } 0194 shouldBe("set_from_if_else_stmt","4"); 0195 0196 // --------------------------------- 0197 0198 var set_from_else_in_if_else_stmt = 4; 0199 try { 0200 if (throwex()) { 0201 undefined; 0202 } 0203 else { 0204 set_from_else_in_if_else_stmt = 1; 0205 } 0206 } 0207 catch (e) { 0208 } 0209 shouldBe("set_from_else_in_if_else_stmt","4"); 0210 0211 // --------------------------------- 0212 0213 var comma_left = 4; 0214 try { 0215 comma_left = 1, throwex(); 0216 } 0217 catch (e) { 0218 } 0219 shouldBe("comma_left","1"); 0220 0221 // --------------------------------- 0222 0223 var comma_left = 4; 0224 try { 0225 throwex(), comma_left = 1; 0226 } 0227 catch (e) { 0228 } 0229 shouldBe("comma_left","4"); 0230 0231 var vardecl_assign_throws = 4; 0232 try { 0233 var vardecl_assign_throws = throwex(); 0234 } 0235 catch (e) { 0236 } 0237 shouldBe("vardecl_assign_throws","4"); 0238 0239 // --------------------------------- 0240 0241 var var_assign_before_throw_run = false; 0242 function var_assign_before_throw() { 0243 var_assign_before_throw_run = true; 0244 return 1; 0245 } 0246 0247 var var_assign_after_throw_run = false; 0248 function var_assign_after_throw() { 0249 var_assign_after_throw_run = true; 0250 return 1; 0251 } 0252 0253 try { 0254 var var_assign1 = var_assign_before_throw(), 0255 var_assign2 = throwex(), 0256 var_assign1 = var_assign_before_throw(); 0257 } 0258 catch (e) { 0259 } 0260 shouldBe("var_assign_before_throw_run","true"); 0261 shouldBe("var_assign_after_throw_run","false"); 0262 0263 // --------------------------------- 0264 0265 var do_val = 4; 0266 try { 0267 do { 0268 do_val++; 0269 } 0270 while (throwex()); 0271 } 0272 catch (e) { 0273 } 0274 shouldBe("do_val","5"); 0275 0276 // --------------------------------- 0277 var while_val = 4; 0278 try { 0279 while (throwex()) { 0280 while_val++; 0281 } 0282 } 0283 catch (e) { 0284 } 0285 shouldBe("while_val","4"); 0286 0287 // --------------------------------- 0288 var for_val_part1_throw2 = 4; 0289 try { 0290 for (for_val_part1_throw2 = 1; throwex(); ) { 0291 } 0292 } 0293 catch (e) { 0294 } 0295 shouldBe("for_val_part1_throw2","1"); 0296 0297 // --------------------------------- 0298 var for_val_part1_throw3 = 4; 0299 try { 0300 for (for_val_part1_throw3 = 1; ; throwex()) { 0301 } 0302 } 0303 catch (e) { 0304 } 0305 shouldBe("for_val_part1_throw3","1"); 0306 0307 // --------------------------------- 0308 var for_val_part2_throw1 = 4; 0309 try { 0310 for (throwex(); for_val_part2_throw1 = 1; ) { 0311 } 0312 } 0313 catch (e) { 0314 } 0315 shouldBe("for_val_part2_throw1","4"); 0316 0317 // --------------------------------- 0318 var for_val_part2_throw3 = 4; 0319 try { 0320 for (; for_val_part2_throw3 = 1; throwex()) { 0321 } 0322 } 0323 catch (e) { 0324 } 0325 shouldBe("for_val_part2_throw3","1"); 0326 0327 // --------------------------------- 0328 var for_val_part3_throw1 = 4; 0329 try { 0330 for (throwex(); ; for_val_part3_throw1 = 1) { 0331 } 0332 } 0333 catch (e) { 0334 } 0335 shouldBe("for_val_part3_throw1","4"); 0336 0337 // --------------------------------- 0338 var for_val_part3_throw2 = 4; 0339 try { 0340 for (; throwex(); for_val_part3_throw2 = 1) { 0341 } 0342 } 0343 catch (e) { 0344 } 0345 shouldBe("for_val_part3_throw2","4"); 0346 0347 // --------------------------------- 0348 var for_val_part1_throwbody = 4; 0349 try { 0350 for (for_val_part1_throwbody = 1; ;) { 0351 throwex(); 0352 } 0353 } 0354 catch (e) { 0355 } 0356 shouldBe("for_val_part1_throwbody","1"); 0357 0358 // --------------------------------- 0359 var for_val_part2_throwbody = 4; 0360 try { 0361 for (; for_val_part2_throwbody = 1; ) { 0362 throwex(); 0363 } 0364 } 0365 catch (e) { 0366 } 0367 shouldBe("for_val_part2_throwbody","1"); 0368 0369 // --------------------------------- 0370 var for_val_part3_throwbody = 4; 0371 try { 0372 for (; ; for_val_part3_throwbody = 1) { 0373 throwex(); 0374 } 0375 } 0376 catch (e) { 0377 } 0378 shouldBe("for_val_part3_throwbody","4"); 0379 0380 // --------------------------------- 0381 // ### mozilla gives a syntax error on for (throwex() in forin_test_obj)... should we? 0382 // --- per spec, no, but we do in KDE4, so I commented this out for now. -- Maks 0383 0384 /* 0385 var forin_test_obj = new Object(); 0386 forin_test_obj.a = 1; 0387 forin_test_obj.b = 2; 0388 forin_test_obj.c = 3; 0389 var forin_count = 4; 0390 function forin_lexpr() { 0391 // if (forincount == 1); 0392 // throwex(); 0393 return new Object(); 0394 } 0395 try { 0396 for (throwex() in forin_test_obj) { 0397 forin_count++; 0398 } 0399 } 0400 catch (e) { 0401 } 0402 shouldBe("forin_count","4"); 0403 */ 0404 testFailed("Parsing of a CallExpression in for .. in"); 0405 0406 // --------------------------------- 0407 var set_inside_with_throw = 4; 0408 try { 0409 with (throwex()) { 0410 set_inside_with_throw = 1; 0411 } 0412 } 0413 catch (e) { 0414 } 0415 shouldBe("set_inside_with_throw","4"); 0416 0417 // --------------------------------- 0418 var set_inside_with_cantconverttoobject = 4; 0419 try { 0420 with (undefined) { 0421 print("FAIL. This message should not be displayed"); 0422 set_inside_with_cantconverttoobject = 1; 0423 } 0424 } 0425 catch (e) { 0426 } 0427 shouldBe("set_inside_with_cantconverttoobject","4"); 0428 // ### test case, sw 0429 0430 0431 // --------------------------------- 0432 // Test to make sure finally propagates exceptions 0433 0434 finallyRan = false; 0435 function testFinally() { 0436 try { 0437 throw 42; 0438 } finally { 0439 finallyRan = true; 0440 } 0441 } 0442 0443 var finallyPropagated = null; 0444 try { 0445 testFinally(); 0446 } catch (e) { 0447 finallyPropagated = e; 0448 } 0449 0450 shouldBe('finallyRan', 'true'); 0451 shouldBe('finallyPropagated', '42'); 0452 0453 // A messier one, throwing things all over the place 0454 out = ""; 0455 try { 0456 try { 0457 throw "a"; 0458 } catch(e) { 0459 out += "<catch:" + e + ">"; 0460 throw "b"; 0461 } finally { 0462 out += "<finally>"; 0463 throw("c"); 0464 } 0465 } catch (e) { 0466 out += "<catch:" + e + ">"; 0467 } 0468 0469 shouldBe('out', '"<catch:a><finally><catch:c>"'); 0470 0471 // Basically the same thing, only not throwing a new one from finally 0472 out = ""; 0473 try { 0474 try { 0475 throw "a"; 0476 } catch(e) { 0477 out += "<catch:" + e + ">"; 0478 throw "b"; 0479 } finally { 0480 out += "<finally>"; 0481 } 0482 } catch (e) { 0483 out += "<catch:" + e + ">"; 0484 } 0485 0486 shouldBe('out', '"<catch:a><finally><catch:b>"'); 0487 0488 0489 debug("Done.");