From a0196470941b6b329ba63770c1da2a3d2fddb6fe Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Fri, 26 Jul 2019 23:05:54 +0200 Subject: [PATCH] fix && operator in if expression --- .../jwebassembly/module/BranchManger.java | 22 ++++++++ .../runtime/ControlFlowOperators.java | 50 +++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/src/de/inetsoftware/jwebassembly/module/BranchManger.java b/src/de/inetsoftware/jwebassembly/module/BranchManger.java index f5a9458..29e37c3 100644 --- a/src/de/inetsoftware/jwebassembly/module/BranchManger.java +++ b/src/de/inetsoftware/jwebassembly/module/BranchManger.java @@ -322,6 +322,28 @@ class BranchManger { } } + while( i > 0 ) { + // check if there is a second condition in the IF expression that is concatenated with "&&" operator + parsedBlock = parsedOperations.get( 0 ); + if( parsedBlock.op == JavaBlockOperator.IF && parsedBlock.endPosition == elsePos ) { + parent.add( new BranchNode( startPos, parsedBlock.nextPosition - 1, WasmBlockOperator.IF, null ) ); + parent.add( new BranchNode( parsedBlock.nextPosition - 1, parsedBlock.nextPosition, WasmBlockOperator.ELSE, WasmBlockOperator.END ) ); + startPos = parsedBlock.nextPosition; + parsedOperations.remove( 0 ); + i--; + ((IfParsedBlock)parsedBlock).negateCompare(); + for( int k = 0; k < instructions.size(); k++ ) { + WasmInstruction instr = instructions.get( k ); + if( instr.getCodePosition() >= startPos ) { + instructions.add( k, new WasmConstInstruction( 0, startPos - 1, parsedBlock.lineNumber ) ); + break; + } + } + continue; + } + break; + } + branch = new BranchNode( startPos, elsePos, WasmBlockOperator.IF, null ); parent.add( branch ); if( i > 0 ) { diff --git a/test/de/inetsoftware/jwebassembly/runtime/ControlFlowOperators.java b/test/de/inetsoftware/jwebassembly/runtime/ControlFlowOperators.java index d56e0d0..ef4431b 100644 --- a/test/de/inetsoftware/jwebassembly/runtime/ControlFlowOperators.java +++ b/test/de/inetsoftware/jwebassembly/runtime/ControlFlowOperators.java @@ -56,6 +56,11 @@ public class ControlFlowOperators extends AbstractBaseTest { addParam( list, script, "forLoop" ); addParam( list, script, "conditionalOperator" ); addParam( list, script, "redifineVariable" ); + addParam( list, script, "ifAnd_0" ); + addParam( list, script, "ifAnd_3" ); + addParam( list, script, "ifAnd_6" ); + addParam( list, script, "if4And_6" ); + addParam( list, script, "if4And_7" ); } rule.setTestParameters( list ); return list; @@ -320,5 +325,50 @@ public class ControlFlowOperators extends AbstractBaseTest { return a + b; } } + + @Export + static int ifAnd_0() { + return ifAnd( 0 ); + } + + @Export + static int ifAnd_3() { + return ifAnd( 3 ); + } + + @Export + static int ifAnd_6() { + return ifAnd( 6 ); + } + + private static int ifAnd( int condition ) { + int result; + if( condition > 0 && condition < 5 ) { + result = 42; + } else { + result = 76; + } + return result; + } + + @Export + static int if4And_6() { + return if4And( 6 ); + } + + @Export + static int if4And_7() { + return if4And( 7 ); + } + + private static int if4And( int condition ) { + int result; + if( condition > 1 && condition > 3 && condition > 5 && condition > 7 ) { + result = 42; + } else { + result = 76; + } + return result; + } } }