source-academy/js-slang

CSE Machine: in nullary functions, env resets are effectively skipped

Closed this issue · 3 comments

Initially reported at https://edstem.org/us/courses/42391/discussion/3688626

Reproduction instructions

Run the following program with Source 3 in the CSE machine.

let x = 1;

const g = () => 1;

function f() {
    function g() {
        return 2;
    }
    return(g());
}

f() + (a => a + 1)(3) + g();

Note that a => a+1 is parented to the f() block env instead of the program env.

Minimal example

function f() {
    const a = 1;
    return 2;
}

f() + (a => a + 1)(3);

Possible culprit

At step 31, we return from f. (contrary to the current-line pointer - eep!) As part of returning from the function, the current env should be changed to the program env.

However, as return swallows the env instruction on its way to mark, at step 33 we are already back on line 12... but with the current environment still in the f block frame.

As noted by the reporter, this issue does not manifest if f takes any arguments. This is because the env instruction corresponding to the f argument frame would not be swallowed, allowing the machine to return to the program env.

Possibly the same bug as this: #1493

Likely fixed by #1494

Yes, fixed:
Screenshot 2023-10-25 at 6 34 40 AM
Screenshot 2023-10-25 at 6 35 50 AM