darknesswind / NutCracker

fork from DamianXVI's squirrel decompiler
20 stars 9 forks source link

weird error in foreach loop #22

Open AdamMil opened 5 years ago

AdamMil commented 5 years ago

Try compiling this function.

function foo() {
  foreach(o in list) {
    if (a) { continue; }
    b.push();
  }
}

Then decompile it. You get this result:

function foo() {
  foreach(o in list) {
    if (a) { continue; }
    a.push();
  }
}

In fact, whatever the condition of the 'if' is will be duplicated on the next line. E.g.

if (a.query(1,2,3)) { continue; }
b.push();

becomes:

if (a.query(1,2,3)) { continue; }
a.query(1,2,3).push();

when it's placed inside the foreach loop.

AdamMil commented 5 years ago

I believe it's because this code in DecompileLoopJumpInstruction can skip ahead even when the current op is not OP_LINE.

bool bHasLineInfo = !m_LineInfos.empty();
...
if (bHasLineInfo) // skip OP_LINE
    state.NextInstruction();

I tried adding another check:

bool bHasLineInfo = !m_LineInfos.empty() && m_Instructions[state.IP()].op == OP_LINE;