function-var-environment.js (1018B)
1 function defaultValue() { return 123; } 2 3 // 2 environment objects: Call => Var. The lambda uses both of them. 4 function testBasic(p = defaultValue()) { 5 for (var i = 0; i < 2000; i++) {} 6 return () => i + p; 7 } 8 assertEq(testBasic()(), 2123); 9 10 function testBailout(p = defaultValue()) { 11 for (var i = 0; i < 2000; i++) { 12 if (i > 1950) { 13 bailout(); 14 } 15 } 16 return () => i + p; 17 } 18 assertEq(testBailout()(), 2123); 19 20 // 3 environment objects: Call => Var => Lexical. The lambda uses all of them. 21 let escaped; 22 function testVarAndLexical(p = defaultValue()) { 23 var q = p + 1; 24 let i = 0; 25 for (; i < 2000; i++) { 26 escaped = () => i + p + q; 27 } 28 } 29 testVarAndLexical(); 30 assertEq(escaped(), 2247); 31 32 function testVarAndLexicalBailout(p = defaultValue()) { 33 var q = p + 1; 34 let i = 0; 35 for (; i < 2000; i++) { 36 escaped = () => i + p - q; 37 if (i > 1950) { 38 bailout(); 39 } 40 } 41 } 42 testVarAndLexicalBailout(); 43 assertEq(escaped(), 1999);