Fix complex catch blocks with multiple catch

This commit is contained in:
Volker Berlin 2022-07-29 22:15:39 +02:00
parent 5013f2f819
commit f8a79c16e0
No known key found for this signature in database
GPG Key ID: 988423EF815BE4CB
2 changed files with 18 additions and 1 deletions

View File

@ -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

View File

@ -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;