diff --git a/src/de/inetsoftware/jwebassembly/module/BranchManger.java b/src/de/inetsoftware/jwebassembly/module/BranchManger.java index 44e75dd..a0c1b22 100644 --- a/src/de/inetsoftware/jwebassembly/module/BranchManger.java +++ b/src/de/inetsoftware/jwebassembly/module/BranchManger.java @@ -283,13 +283,12 @@ class BranchManger { // calculate the block number for ever switch case depending its position order Arrays.sort( cases, ( a, b ) -> Integer.compare( a.position, b.position ) ); - int blockCount = 0; + int blockCount = -1; int lastPosition = -1; BranchNode brTableNode = null; BranchNode blockNode = null; for( int i = 0; i < cases.length; i++ ) { switchCase = cases[i]; - switchCase.block = blockCount; int currentPosition = switchCase.position; if( lastPosition != currentPosition ) { if( isTable && blockNode == null ) { @@ -303,6 +302,7 @@ class BranchManger { } blockNode = node; } + switchCase.block = blockCount; } // handle the GOTO (breaks) at the end of the CASE blocks. diff --git a/test/de/inetsoftware/jwebassembly/runtime/ControlFlowOperators.java b/test/de/inetsoftware/jwebassembly/runtime/ControlFlowOperators.java index 2735d50..a7321b1 100644 --- a/test/de/inetsoftware/jwebassembly/runtime/ControlFlowOperators.java +++ b/test/de/inetsoftware/jwebassembly/runtime/ControlFlowOperators.java @@ -140,13 +140,18 @@ public class ControlFlowOperators extends AbstractBaseTest { @Export static int switchDirect() { - return tableSwitch(10) + (tableSwitch( 9 ) * 10) + (lookupSwitch(Integer.MAX_VALUE) * 100) + (lookupSwitch(0) * 1000); + return tableSwitch(10) + (tableSwitch( 9 ) * 10) + + (tableSwitch( -1 ) * 100) + (lookupSwitch(Integer.MAX_VALUE) * 1000) + (lookupSwitch(0) * 10000 ); } private static int tableSwitch( int a ) { int b; switch(a){ + case 8: + case 9: + b = 2; + break; case 10: + case 11: b = 1; break; default: