Skip to content

Commit

Permalink
fix while(true) inside a while(true) loop. #43
Browse files Browse the repository at this point in the history
  • Loading branch information
Horcrux7 committed Jun 16, 2022
1 parent f5edb58 commit 062e2ae
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/de/inetsoftware/jwebassembly/module/BranchManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ private void addLoops( List<ParsedBlock> parsedOperations ) {
if( loop == null ) {
loop = new ParsedBlock( JavaBlockOperator.LOOP, start, 0, start, parsedBlock.lineNumber );
loops.put( start, loop );
// if a condition form outside of the loop point inside the loop then it must be conditional return and a jump to the loop condition.
// if a condition before the loop points to a position within the loop, then it must be a conditional return and a jump to the loop condition.
for( int n = b - 1; n >= 0; n-- ) {
ParsedBlock prevBlock = parsedOperations.get( n );
switch( prevBlock.op ) {
Expand All @@ -221,8 +221,20 @@ private void addLoops( List<ParsedBlock> parsedOperations ) {
}
}
}
loop.nextPosition = parsedBlock.startPosition; // Jump position for Continue
loop.endPosition = parsedBlock.nextPosition;
if( loop.endPosition < parsedBlock.nextPosition ) {
int nextPosition = parsedBlock.startPosition; // Jump position for Continue
int endPosition = parsedBlock.nextPosition;
// if a condition behind the loop points to a position inside the loop, the loop must be extended to avoid overlapping blocks.
for( int n = b + 1; n < parsedOperations.size(); n++ ) {
ParsedBlock block = parsedOperations.get( n );
if( block.startPosition > endPosition && endPosition > block.endPosition && block.endPosition > start ) {
nextPosition = block.startPosition;
endPosition = block.nextPosition;
}
}
loop.nextPosition = nextPosition; // Jump position for Continue
loop.endPosition = endPosition;
}
break;
default:
throw new WasmException( "Unimplemented loop code operation: " + parsedBlock.op, parsedBlock.lineNumber );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public static Collection<Object[]> data() {
addParam( list, script, "whileLoopInElseAfterReturn" );
addParam( list, script, "whileLoopAfterIfWithReturn" );
addParam( list, script, "whileLoopInsideLoop" );
addParam( list, script, "whileTrueInsideWhileTrue" );
addParam( list, script, "forLoop" );
addParam( list, script, "conditionalOperator" );
addParam( list, script, "conditionalOperator2" );
Expand Down Expand Up @@ -489,6 +490,26 @@ public static int whileLoopInsideLoop() {
}
}

@Export
static int whileTrueInsideWhileTrue() {
int sw = 1;
while( true ) {
if( sw != 1 ) {
sw++;
break;
}
sub: while( true ) {
if( sw == 1 ) {
sw = 2;
break;
} else {
sw = 17;
}
}
}
return sw;
}

@Export
static int forLoop() {
int a = 0;
Expand Down

0 comments on commit 062e2ae

Please sign in to comment.