source-academy / source-programs

Notable Source programs, developed for SICP JS and other educational projects
GNU General Public License v3.0
7 stars 18 forks source link

Bug in source-1-with-copying-gc.js where environment extension count is sometimes very large #1

Closed btzy closed 4 years ago

btzy commented 4 years ago

Steps to reproduce:

  1. Load source-1-with-copying-gc.js into Source Academy
  2. Replace the examples at the bottom with the test case below
  3. Add the following these two lines before the EXTEND() in M[CALL] (line 1384 on GitHub / line 1379 in Source Academy):
    display(A, "env ext count:");
    display(N, "OS size:");
  4. Run the code

Test case

initialize_machine(448);
P = parse_and_compile("                                     \
function recurse(x, y, operation, initvalue) {              \
    return y === 0                                          \
        ? initvalue                                         \
        : operation(x, recurse(x, y - 1,                    \
                    operation, initvalue));                 \
}                                                           \
                                                            \
function f(x, z) { return x * z; }                          \
recurse(2, 2, f, 1);                                        ");
//print_program(P);
run();

Actual output

Running VM with heap size: 448
env ext count: 5
OS size: 5
env ext count: 17
OS size: 7
env ext count: 17
OS size: 7
env ext count: 17
OS size: 7
env ext count: 17
OS size: 2
Flip! FREE: 220
env ext count: 314
OS size: 2
result: heap node of type = number, value = 4 ""
undefined

Expected The env ext count in the output above should be much less than 314.

martin-henz commented 4 years ago

The EXTEND macro expects the old environment (environment to be extended) in Register A. So 314 is the address of an environment.