MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at master 238 lines 4.9 kB view raw
1function test1() { 2 try { 3 throw 'error'; 4 } catch (e) { 5 return e; 6 } 7} 8for (let i = 0; i < 110; i++) test1(); 9console.log('[test1] throw string:', test1(), 'ok:', test1() === 'error'); 10 11function test2() { 12 try { 13 return 42; 14 } catch (e) { 15 return -1; 16 } 17} 18for (let i = 0; i < 110; i++) test2(); 19console.log('[test2] no throw:', test2(), 'ok:', test2() === 42); 20 21function test3() { 22 try { 23 throw 99; 24 } catch (e) { 25 return e; 26 } 27} 28for (let i = 0; i < 110; i++) test3(); 29console.log('[test3] throw number:', test3(), 'ok:', test3() === 99); 30 31function thrower() { 32 throw 'boom'; 33} 34function test4() { 35 try { 36 return thrower(); 37 } catch (e) { 38 return e; 39 } 40} 41for (let i = 0; i < 110; i++) test4(); 42console.log('[test4] catch from call:', test4(), 'ok:', test4() === 'boom'); 43 44function test5() { 45 let result = 0; 46 try { 47 throw 'err'; 48 } catch (e) { 49 result = 10; 50 } 51 return result + 5; 52} 53for (let i = 0; i < 110; i++) test5(); 54console.log('[test5] code after catch:', test5(), 'ok:', test5() === 15); 55 56function test6() { 57 try { 58 try { 59 throw 'inner'; 60 } catch (e) { 61 return 'caught: ' + e; 62 } 63 } catch (e) { 64 return 'outer: ' + e; 65 } 66} 67for (let i = 0; i < 110; i++) test6(); 68console.log('[test6] nested catch:', test6(), 'ok:', test6() === 'caught: inner'); 69 70function test7() { 71 try { 72 try { 73 throw 'propagate'; 74 } catch (e) { 75 throw 're: ' + e; 76 } 77 } catch (e) { 78 return e; 79 } 80} 81for (let i = 0; i < 110; i++) test7(); 82console.log('[test7] re-throw:', test7(), 'ok:', test7() === 're: propagate'); 83 84function test8(x) { 85 try { 86 let a = x + 1; 87 let b = a * 2; 88 return b; 89 } catch (e) { 90 return -1; 91 } 92} 93for (let i = 0; i < 110; i++) test8(i); 94console.log('[test8] complex try body:', test8(5), 'ok:', test8(5) === 12); 95 96function test9() { 97 try { 98 throw 10; 99 } catch (e) { 100 return e + 32; 101 } 102} 103for (let i = 0; i < 110; i++) test9(); 104console.log('[test9] catch computation:', test9(), 'ok:', test9() === 42); 105 106function test10(doThrow) { 107 try { 108 if (doThrow) throw 'thrown'; 109 return 'ok'; 110 } catch (e) { 111 return e; 112 } 113} 114for (let i = 0; i < 110; i++) test10(i % 2 === 0); 115console.log('[test10a] throw path:', test10(true), 'ok:', test10(true) === 'thrown'); 116console.log('[test10b] no-throw path:', test10(false), 'ok:', test10(false) === 'ok'); 117 118function test11() { 119 try { 120 throw true; 121 } catch (e) { 122 return e; 123 } 124} 125for (let i = 0; i < 110; i++) test11(); 126console.log('[test11] throw bool:', test11(), 'ok:', test11() === true); 127 128function safe() { 129 return 1; 130} 131function unsafe() { 132 throw 'fail'; 133} 134function test12() { 135 try { 136 let a = safe(); 137 let b = safe(); 138 let c = unsafe(); 139 return a + b + c; 140 } catch (e) { 141 return e; 142 } 143} 144for (let i = 0; i < 110; i++) test12(); 145console.log('[test12] multi-call catch:', test12(), 'ok:', test12() === 'fail'); 146 147function test13() { 148 try { 149 let x = undefined; 150 x.foo; 151 } catch (e) { 152 return 'caught'; 153 } 154 return 'missed'; 155} 156for (let i = 0; i < 110; i++) test13(); 157console.log('[test13] throw_error:', test13(), 'ok:', test13() === 'caught'); 158 159function test14() { 160 let obj = { 161 greet() { 162 return 'hello'; 163 } 164 }; 165 return obj.greet(); 166} 167for (let i = 0; i < 110; i++) test14(); 168console.log('[test14] get_field2:', test14(), 'ok:', test14() === 'hello'); 169 170function test15() { 171 let obj = { 172 say() { 173 return 'hi'; 174 } 175 }; 176 let key = 'say'; 177 return obj[key](); 178} 179for (let i = 0; i < 110; i++) test15(); 180console.log('[test15] get_elem2:', test15(), 'ok:', test15() === 'hi'); 181 182function test16() { 183 let base = { x: 10 }; 184 let child = { __proto__: base, y: 20 }; 185 return child.x + child.y; 186} 187for (let i = 0; i < 110; i++) test16(); 188console.log('[test16] set_proto:', test16(), 'ok:', test16() === 30); 189 190function test17() { 191 try { 192 let x = null; 193 return x.foo; 194 } catch (e) { 195 return 'caught'; 196 } 197} 198for (let i = 0; i < 110; i++) test17(); 199console.log('[test17] get_field err:', test17(), 'ok:', test17() === 'caught'); 200 201function test18() { 202 try { 203 let x = undefined; 204 return x[0]; 205 } catch (e) { 206 return 'caught'; 207 } 208} 209for (let i = 0; i < 110; i++) test18(); 210console.log('[test18] get_elem err:', test18(), 'ok:', test18() === 'caught'); 211 212function test19() { 213 let arr = [1, 2, 3]; 214 return arr['join']('-'); 215} 216for (let i = 0; i < 110; i++) test19(); 217console.log('[test19] elem2 arr:', test19(), 'ok:', test19() === '1-2-3'); 218 219function test20() { 220 let obj = { 221 val: 0, 222 add(n) { 223 return { 224 val: this.val + n, 225 add: this.add, 226 get() { 227 return this.val; 228 } 229 }; 230 }, 231 get() { 232 return this.val; 233 } 234 }; 235 return obj.add(1).add(2).get(); 236} 237for (let i = 0; i < 110; i++) test20(); 238console.log('[test20] chain calls:', test20(), 'ok:', test20() === 3);