Rhino

Rhino

34M Downloads

Reference error with loops such as `for (var x = ..., y = x)`

Fuyukai opened this issue ยท 1 comments

commented

Stupid test code:

for (var x = 0, y = x; y < 10; y++) {
    console.log(y);
}

Gives off a ReferenceError: "x" is not defined..

Note that this works fine in regular Rhino:

$ rhino
Rhino 1.7.14
js> for (var x = 0, y = x; y < 10; y++) {
  > print(y);
  > };
0
1
2
3
4
5
6
7
8
9

Note that it works fine outside of a loop body:

var x = 1, y = x;
console.log(y);

Gives [23:42:03] [INFO ] server_scripts:test.js:2: 1.0.

commented

My initial suspicious culprit is here, where the comment very helpfully explains what is going on: the loop body is being rewritten to move the variable assignment out of the loop body. This works for single assignments but results in a miscompilation for multiple assignments (I'm not sure why).

Just removing that seems to fix this but that seems obviously wrong so I'm going to keep digging as to why this causes the miscompilation.