@@ -36,10 +36,14 @@ describe('FREE VARIABLE CAPTURE', () => {
3636 // With TRUE lexical scoping lc_f0() should still return 5 (defining scope).
3737 // BUG: currently returns 10 because the calling scope's eval context is
3838 // still on the stack and is found before the defining scope's value.
39- ce . pushScope ( ) ;
40- ce . declare ( 'lc_c' , { value : 10 } ) ;
41- const result = ce . box ( [ 'lc_f0' ] ) . evaluate ( ) . valueOf ( ) ;
42- ce . popScope ( ) ;
39+ let result : unknown ;
40+ try {
41+ ce . pushScope ( ) ;
42+ ce . declare ( 'lc_c' , { value : 10 } ) ;
43+ result = ce . box ( [ 'lc_f0' ] ) . evaluate ( ) . valueOf ( ) ;
44+ } finally {
45+ ce . popScope ( ) ;
46+ }
4347 expect ( result ) . toMatchInlineSnapshot ( `10` ) ; // BUG: should be 5, not 10
4448 } ) ;
4549
@@ -88,7 +92,7 @@ describe('PARAMETER SHADOWING', () => {
8892 afterAll ( ( ) => ce . popScope ( ) ) ;
8993
9094 test ( 'one param + one free var: param bound, free var from outer scope' , ( ) => {
91- // Function(lc_x + lc_c2, lc_x ): lc_x is the param, lc_c2 is free (= 5)
95+ // Function(lc_x2p + lc_c2, lc_x2p ): lc_x2p is the param, lc_c2 is free (= 5)
9296 // Apply to 3 → 3 + 5 = 8
9397 const f = ce . box ( [ 'Function' , [ 'Add' , 'lc_x2p' , 'lc_c2' ] , 'lc_x2p' ] ) ;
9498 expect (
@@ -235,13 +239,13 @@ describe('LAMBDAS INSIDE BigOps', () => {
235239 // lc_k4b should still be an 'unknown' symbol (no assigned value) after Sum
236240 expect ( ce . box ( 'lc_k4b' ) . value ?. toString ( ) ) . toMatchInlineSnapshot (
237241 `undefined`
238- ) ;
242+ ) ; // correct: index var leaves no stale value in outer scope after Sum
239243 } ) ;
240244
241245 test ( 'Sum with free var in calling scope (scope pollution interaction)' , ( ) => {
242246 // If lc_c4 gets auto-declared in Sum's scope with type 'unknown',
243247 // Sum might not see the outer lc_c4 = 10.
244- // BUG candidate : the result may be wrong due to scope pollution.
248+ // BUG: the result is wrong due to dynamic scoping (not scope pollution) .
245249 let result : unknown ;
246250 try {
247251 ce . pushScope ( ) ;
0 commit comments