Nick
11/11/2024, 6:16 AMwhile
loop in JS.
fun foo() {
var value = 10
var test = 23
var someCondition = true
do {
if (someCondition) {
break
}
println("HERE")
} while (value != test)
}
fun main() {
foo()
}
this code outputs the following. notice the break is incorporated into the while condition. this is bad b/c it fails to address the case when the condition for the break is actually true upon entering the do portion of the while. in that case, the while should be broken out of before continuing to the println
. but the generated code does not do that.
function foo() {
var value = 10;
var test = 23;
var someCondition = true;
// Incorrect translation. The break check can actually be false and therefore needs to remain in the do portion of the
// loop
$l$loop: do {
println('HERE');
} while (!(value === test) && !someCondition);
}
function main() {
foo();
}
note that adding a statement above the break
causes the generated code to behave as expected:
function foo() {
var value = 10;
var test = 23;
var someCondition = true;
$l$loop: do {
if (someCondition) { // <----------- now the condition is included correctly, allowing for the case of an early break
println('test');
break $l$loop;
}
println('HERE');
} while (!(value === test));
}
function main() {
foo();
}
dmitriy.novozhilov
11/11/2024, 7:47 AMNick
11/15/2024, 4:20 AM