Fix break bug in for/foreach loops.

This commit is contained in:
Jack Conradson 2016-08-24 14:25:54 -07:00
parent 9bedbbaa6a
commit c60885b5d4
4 changed files with 36 additions and 0 deletions

View File

@ -183,6 +183,8 @@ public final class SFor extends AStatement {
writer.writeLoopCounter(loopCounter.getSlot(), statementCount, location);
}
block.continu = begin;
block.brake = end;
block.write(writer, globals);
} else {
if (loopCounter != null) {

View File

@ -100,6 +100,8 @@ final class SSubEachArray extends AStatement {
writer.writeLoopCounter(loopCounter.getSlot(), statementCount, location);
}
block.continu = begin;
block.brake = end;
block.write(writer, globals);
writer.goTo(begin);

View File

@ -122,6 +122,8 @@ final class SSubEachIterable extends AStatement {
writer.writeLoopCounter(loopCounter.getSlot(), statementCount, location);
}
block.continu = begin;
block.brake = end;
block.write(writer, globals);
writer.goTo(begin);

View File

@ -272,4 +272,34 @@ public class BasicStatementTests extends ScriptTestCase {
null, true
));
}
public void testForWithBreak() {
assertEquals(1, exec(
"Map settings = ['test1' : '1'];" +
"int setting = 0;" +
"List keys = ['test0', 'test1', 'test2'];" +
"for (int i = 0; i < keys.size(); ++i) {" +
" if (settings.containsKey(keys[i])) {" +
" setting = Integer.parseInt(settings[keys[i]]);" +
" break;" +
" }" +
"}" +
"return setting;"
));
}
public void testForEachWithBreak() {
assertEquals(1, exec(
"Map settings = ['test1' : '1'];" +
"int setting = 0;" +
"List keys = ['test0', 'test1', 'test2'];" +
"for (String key : keys) {" +
" if (settings.containsKey(key)) {" +
" setting = Integer.parseInt(settings[key]);" +
" break;" +
" }" +
"}" +
"return setting;"
));
}
}