From f383ef26e828a2972443b92a3d57c95edfa7bbae Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Sun, 17 May 2020 13:57:30 +0200 Subject: [PATCH] fix bug with cascaded if blocks --- .../jwebassembly/module/BranchManger.java | 17 ++++++++++++++--- .../runtime/ControlFlowOperators.java | 18 +++++++++++++++++- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/module/BranchManger.java b/src/de/inetsoftware/jwebassembly/module/BranchManger.java index 3cd0a17..4591fa2 100644 --- a/src/de/inetsoftware/jwebassembly/module/BranchManger.java +++ b/src/de/inetsoftware/jwebassembly/module/BranchManger.java @@ -433,10 +433,21 @@ class BranchManger { // first search for first GOTO, any IF that point after the GOTO can not be part of the primary IF condition. // This occur with a second IF inside of the THEN. This can jump directly to the end of the ELSE. int maxElse = Integer.MAX_VALUE; - for( int i = 0; i < parsedOperations.size(); i++ ) { + int parsedOpCount = parsedOperations.size(); + for( int i = 0; i < parsedOpCount; i++ ) { ParsedBlock parsedBlock = parsedOperations.get( i ); if( parsedBlock.op == JavaBlockOperator.GOTO ) { - maxElse = parsedBlock.nextPosition; + maxElse = parsedBlock.endPosition; + + // find the last IF that point to this GOTO + int gotoNext = parsedBlock.nextPosition; + for( ; i > 0; i-- ) { + parsedBlock = parsedOperations.get( i-1 ); + if( parsedBlock.endPosition == gotoNext ) { + break; + } + } + parsedOpCount = i; break; } } @@ -444,7 +455,7 @@ class BranchManger { int ifCount = 0; int thenPos = startBlock.nextPosition; int elsePos = startBlock.endPosition; - for( ; ifCount < parsedOperations.size(); ifCount++ ) { + for( ; ifCount < parsedOpCount; ifCount++ ) { ParsedBlock parsedBlock = parsedOperations.get( ifCount ); if( parsedBlock.op != JavaBlockOperator.IF || parsedBlock.endPosition < elsePos || parsedBlock.endPosition > maxElse ) { // seems a second IF inside the THEN part. diff --git a/test/de/inetsoftware/jwebassembly/runtime/ControlFlowOperators.java b/test/de/inetsoftware/jwebassembly/runtime/ControlFlowOperators.java index aa96dd1..a44ae16 100644 --- a/test/de/inetsoftware/jwebassembly/runtime/ControlFlowOperators.java +++ b/test/de/inetsoftware/jwebassembly/runtime/ControlFlowOperators.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 - 2019 Volker Berlin (i-net software) + * Copyright 2018 - 2020 Volker Berlin (i-net software) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -73,6 +73,7 @@ public class ControlFlowOperators extends AbstractBaseTest { addParam( list, script, "ifAndOr6" ); addParam( list, script, "ifAndOr8" ); addParam( list, script, "ifWithoutElseAndLoop" ); + addParam( list, script, "ifOrWithMulti" ); addParam( list, script, "stringSwitchNormalFoo" ); addParam( list, script, "stringSwitchNormalBar" ); addParam( list, script, "stringSwitchNormalDefault" ); @@ -495,6 +496,21 @@ public class ControlFlowOperators extends AbstractBaseTest { return ifWithoutElseAndLoop; } + @Export + static int ifOrWithMulti() { + int len = 4; + + // the GOTO before the ELSE is not related to the main IF condition + if( (len == 4 || len == 9) ) { + if( len == 9 ) { + len = 13; + } else { + len = 42; + } + } + return len; + } + @Export static int stringSwitchNormalFoo() { return stringSwitchNormal( "foo" );