diff --git a/src/de/inetsoftware/jwebassembly/module/BranchManager.java b/src/de/inetsoftware/jwebassembly/module/BranchManager.java index e391e09..aea886e 100644 --- a/src/de/inetsoftware/jwebassembly/module/BranchManager.java +++ b/src/de/inetsoftware/jwebassembly/module/BranchManager.java @@ -1507,7 +1507,23 @@ class BranchManager { * @return the new node */ private BranchNode addMiddleNode( BranchNode parent, int startPos, int endPos ) { - BranchNode middleNode = new BranchNode( startPos, endPos, WasmBlockOperator.BLOCK, WasmBlockOperator.END ); + Object data = null; + // use same input parameter if parent or child start on same position + if( parent.startPos == startPos ) { + WasmBlockOperator startOp = parent.startOp; + if( startOp != null ) { + switch( parent.startOp ) { + case BLOCK: + data = parent.data; + break; + case CATCH: + // first instruction of a CATCH block ever saved the exception from the stack to a local variable that we start the middle block an instruction later + startPos++; + } + } + } + + BranchNode middleNode = new BranchNode( startPos, endPos, WasmBlockOperator.BLOCK, WasmBlockOperator.END, data ); int idx = 0; for( Iterator it = parent.iterator(); it.hasNext(); ) { BranchNode child = it.next(); @@ -1520,18 +1536,13 @@ class BranchManager { } middleNode.add( child ); it.remove(); - } - // use same input parameter if parent or child start on same position - if( parent.startPos == startPos ) { - middleNode.data = parent.data; - } else if( middleNode.size() > 0 ) { - BranchNode child = middleNode.get( 0 ); - if( child.startPos == startPos ) { + if( child.startPos == startPos && child.startOp == WasmBlockOperator.BLOCK ) { middleNode.data = child.data; } } + parent.add( idx, middleNode ); patchBrDeep( middleNode ); return middleNode; diff --git a/test/de/inetsoftware/jwebassembly/runtime/Exceptions.java b/test/de/inetsoftware/jwebassembly/runtime/Exceptions.java index 77e9a7e..2c768d0 100644 --- a/test/de/inetsoftware/jwebassembly/runtime/Exceptions.java +++ b/test/de/inetsoftware/jwebassembly/runtime/Exceptions.java @@ -59,6 +59,7 @@ public class Exceptions extends AbstractBaseTest { addParam( list, script, "serialCatch" ); addParam( list, script, "tryReturn" ); addParam( list, script, "whileTrueTryFinally" ); + addParam( list, script, "ifMultipleInFinally" ); } rule.setTestParameters( list ); rule.setProperty( JWebAssembly.WASM_USE_EH, "true" ); @@ -276,6 +277,21 @@ public class Exceptions extends AbstractBaseTest { return sw; } + @Export + private static int ifMultipleInFinally() throws Throwable { + int pos = 0; + try { + pos = 13; + } catch( Throwable ex ) { + pos = 42; + } finally { + if( pos < -13 || pos == 0 ) { + return 17; + } + } + return pos; + } + // @Export // static int npe() { // Object obj = new NullPointerException();