HaxeFoundation / haxe

Haxe - The Cross-Platform Toolkit
https://haxe.org
6.2k stars 658 forks source link

[LUA] Fix do while loops on lua #11807

Open NeeEoo opened 3 weeks ago

NeeEoo commented 3 weeks ago

This fixes an issue where

var test = null;

do {
    test = {type: 5};
}
while (test.type != 5);

would crash

Raltyro commented 3 weeks ago

why did it need to have first vsriable anyway

Simn commented 3 weeks ago

A test would be good!

jdonaldson commented 2 weeks ago

It looks like you just changed the order of the elements in the "or" clause:

while (cond or _hx_do_first_x)

to

while(_hx_do_first_x or cond)

What's the reason why the order matters?

NeeEoo commented 2 weeks ago

It looks like you just changed the order of the elements in the "or" clause:


while (cond or _hx_do_first_x)

to


while(_hx_do_first_x or cond)

What's the reason why the order matters?

Because if the loop expression depends on something to be changed in the loop it breaks.

NeeEoo commented 2 weeks ago

Basically it doesn't need to run the conditional expression, thus if the expression has code that depends on one iteration of the loop to run then it wont crash.

Do while loops in other languages only runs the conditional expression after the loop has run once, and if it returns true it goes back to the beginning. Replicating this behavior on lua means the _hx_do_first_x has to be at the start, so the entire condition doesn't run.

jdonaldson commented 2 weeks ago

This seems like it might be unspecified behavior. I think a test would be a good idea to make sure the other targets have the same behavior. If all targets work the same I think it's a good change.