forresto/turtle-svg

Variable persistence considered harmful.

craftoid opened this issue · 4 comments

Problem

If a variable is defined in the editor and definition is removed later, the variable is still defined.

This should not be the case, because the result of the program would depend on hidden variables, which will not be defined next time the program is reloaded or shared, resulting in runtime failure.

Scenario

Create a simple program

 var s = 10;
 f(s);

Play with it, removing the variable definition,
but still use it somewhere in the code.
The program still runs without complaint.

 f(s);

Now share the code with your mom.
Program fails, mom cries, chaos ensues.

Solution

Restart / reset the worker every time before running the code.

Hmm yes, good point. I think it isn't expensive to terminate the worker, so
might as well spin it up for every eval.

👍

This is also good if somebody evals moveForward = turnLeft = turnRight = null; or something on accident. I can't think of a way to protect those functions (and also keep the eval simple)...

Tested with

moveForward = null; // FAIL

then

// moveForward = null; // OK