From a0d56ddcc349609e7169775ca9e43f42106ba728 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Wed, 27 May 2020 21:21:50 +0200 Subject: [PATCH] fix IF with complex && and || operations. see #17 --- .../jwebassembly/module/BranchManger.java | 17 ++++++++++++++++- .../runtime/ControlFlowOperators.java | 19 ++++++++++++++++--- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/module/BranchManger.java b/src/de/inetsoftware/jwebassembly/module/BranchManger.java index f214cb1..cab11a9 100644 --- a/src/de/inetsoftware/jwebassembly/module/BranchManger.java +++ b/src/de/inetsoftware/jwebassembly/module/BranchManger.java @@ -473,10 +473,25 @@ class BranchManger { int elsePos = startBlock.endPosition; for( ; ifCount < parsedOpCount; ifCount++ ) { ParsedBlock parsedBlock = parsedOperations.get( ifCount ); - if( parsedBlock.op != JavaBlockOperator.IF || parsedBlock.endPosition < elsePos || parsedBlock.endPosition > endElse ) { + if( parsedBlock.op != JavaBlockOperator.IF || parsedBlock.endPosition > endElse ) { // seems a second IF inside the THEN part. break; } + if( parsedBlock.endPosition < elsePos ) { + // The IF jumps not to ELSE part. This can be an inner IF or it is a combination of (||) and (&&) operation + boolean isContinue = false; + for( int i = ifCount + 1; i < parsedOpCount; i++ ) { + ParsedBlock op = parsedOperations.get( i ); + if( op.endPosition >= elsePos ) { + isContinue = true; + break; + } + } + if( !isContinue ) { + // really seems a second IF within the THEN part. + break; + } + } if( parsedBlock.nextPosition > elsePos ) { // occur if there are 2 IF blocks without ELSE behind the other, this IF is the second that we can cancel the analyze at this point break; diff --git a/test/de/inetsoftware/jwebassembly/runtime/ControlFlowOperators.java b/test/de/inetsoftware/jwebassembly/runtime/ControlFlowOperators.java index acb7f43..9c449e8 100644 --- a/test/de/inetsoftware/jwebassembly/runtime/ControlFlowOperators.java +++ b/test/de/inetsoftware/jwebassembly/runtime/ControlFlowOperators.java @@ -15,13 +15,10 @@ */ package de.inetsoftware.jwebassembly.runtime; -import static org.junit.Assume.assumeFalse; - import java.util.ArrayList; import java.util.Collection; import org.junit.ClassRule; -import org.junit.Test; import org.junit.runners.Parameterized.Parameters; import de.inetsoftware.jwebassembly.ScriptEngine; @@ -72,6 +69,7 @@ public class ControlFlowOperators extends AbstractBaseTest { addParam( list, script, "ifAndOr4" ); addParam( list, script, "ifAndOr6" ); addParam( list, script, "ifAndOr8" ); + addParam( list, script, "ifAndOrComplex" ); addParam( list, script, "ifWithoutElseAndLoop" ); addParam( list, script, "ifOrWithMulti" ); addParam( list, script, "ifMultipleInsideThen" ); @@ -482,6 +480,21 @@ public class ControlFlowOperators extends AbstractBaseTest { return result; } + @Export + private static int ifAndOrComplex() { + int b1 = 0; + int b2 = 0; + int result; + if( (b1 == 0xf0 && (b2 < 0x90 || b2 > 0xbf)) || + (b1 == 0xf4 && (b2 & 0xf0) != 0x80) || + (b2 & 0xc0) != 0x80) { + result = 13; + } else { + result = 42; + } + return result; + } + private static int ifWithoutElseAndLoop; @Export