source-academy / js-slang

Implementations of sublanguages of JavaScript, TypeScript, Scheme and Python
https://source-academy.github.io/source/
Apache License 2.0
70 stars 104 forks source link

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

Closed parnikkapore closed 1 year ago

parnikkapore commented 1 year ago

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.

martin-henz commented 1 year ago

Possibly the same bug as this: https://github.com/source-academy/js-slang/issues/1493

parnikkapore commented 1 year ago

Likely fixed by https://github.com/source-academy/js-slang/pull/1494

martin-henz commented 1 year ago

Yes, fixed:

Screenshot 2023-10-25 at 6 34 40 AM Screenshot 2023-10-25 at 6 35 50 AM