From f8a79c16e0f1b50cf3c111e83c5e15cdd05b1616 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Fri, 29 Jul 2022 22:15:39 +0200 Subject: [PATCH] Fix complex catch blocks with multiple catch --- .../jwebassembly/module/BranchManager.java | 6 +++++- .../jwebassembly/runtime/Exceptions.java | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/de/inetsoftware/jwebassembly/module/BranchManager.java b/src/de/inetsoftware/jwebassembly/module/BranchManager.java index 4814347..478e5f9 100644 --- a/src/de/inetsoftware/jwebassembly/module/BranchManager.java +++ b/src/de/inetsoftware/jwebassembly/module/BranchManager.java @@ -1251,10 +1251,11 @@ class BranchManager { // Create a block/end structure for every CATCH without the first CATCH BranchNode node = catchNode; + TryCatchFinally tryCurrent = tryCatch; for( int i = catches.size()-1; i > 0; i-- ) { TryCatchFinally tryCat = catches.get( i ); - if( tryCatch.getHandler() == tryCat.getHandler() ) { + if( tryCurrent.getHandler() == tryCat.getHandler() ) { // multiple exception in catch block like "catch ( ArrayIndexOutOfBoundsException | IllegalArgumentException ex )" continue; } @@ -1277,7 +1278,10 @@ class BranchManager { node = block; int instrPos = findIdxOfCodePos( tryCat.getHandler() ) + 1; + assert (instructions.get( instrPos ) instanceof WasmLoadStoreInstruction) && ((WasmLoadStoreInstruction)instructions.get( instrPos )).getOperator() == VariableOperator.set : "Catch block does not start with astore instruction " + instructions.get( instrPos ); instructions.remove( instrPos ); // every catch block store the exception from the stack but in WASM we can do this only once for all exceptions + + tryCurrent = tryCat; } // calculate branch operations inside the CATCH/FINALLY blocks diff --git a/test/de/inetsoftware/jwebassembly/runtime/Exceptions.java b/test/de/inetsoftware/jwebassembly/runtime/Exceptions.java index 0b27b1f..918823d 100644 --- a/test/de/inetsoftware/jwebassembly/runtime/Exceptions.java +++ b/test/de/inetsoftware/jwebassembly/runtime/Exceptions.java @@ -57,6 +57,7 @@ public class Exceptions extends AbstractBaseTest { addParam( list, script, "emptyCatch" ); addParam( list, script, "multiCatch" ); addParam( list, script, "multiCatch2" ); + addParam( list, script, "multiCatch3" ); addParam( list, script, "serialCatch" ); addParam( list, script, "tryReturn" ); addParam( list, script, "whileTrueTryFinally" ); @@ -241,6 +242,18 @@ public class Exceptions extends AbstractBaseTest { return r; } + @Export + public static int multiCatch3() throws Throwable { + try { + new Throwable(); + return 42; + } catch( IllegalStateException x ) { + } catch( IllegalArgumentException | NullPointerException x ) { + throw new Throwable( x ); + } + return 13; + } + @Export static int serialCatch() { int r = 42;