Fix complex catch blocks with reverse multiple catch

This commit is contained in:
Volker Berlin 2022-08-01 19:52:10 +02:00
parent 50ca777dc3
commit 7d2152a5df
No known key found for this signature in database
GPG Key ID: 988423EF815BE4CB
2 changed files with 19 additions and 5 deletions

View File

@ -1254,13 +1254,14 @@ class BranchManager {
TryCatchFinally tryCurrent = tryCatch;
for( int i = catches.size()-1; i > 0; i-- ) {
TryCatchFinally tryCat = catches.get( i );
int handler = tryCat.getHandler();
if( tryCurrent.getHandler() == tryCat.getHandler() ) {
if( tryCurrent.getHandler() == handler || tryCatch.getHandler() == handler ) {
// multiple exception in catch block like "catch ( ArrayIndexOutOfBoundsException | IllegalArgumentException ex )"
continue;
}
int blockGotoPos = tryCat.getHandler()-3;
int blockGotoPos = handler - 3;
for( idx = 0; idx < parsedOperations.size(); idx++ ) {
ParsedBlock parsedBlock = parsedOperations.get( idx );
if( parsedBlock.startPosition == blockGotoPos && parsedBlock.op == JavaBlockOperator.GOTO && parsedBlock.startPosition < parsedBlock.endPosition ) {
@ -1272,12 +1273,12 @@ class BranchManager {
}
}
BranchNode block = new BranchNode( catchPos + 1, tryCat.getHandler(), WasmBlockOperator.BLOCK, WasmBlockOperator.END );
block.add( new BranchNode( tryCat.getHandler(), tryCat.getHandler(), WasmBlockOperator.BR, null, catches.size() - i ) );
BranchNode block = new BranchNode( catchPos + 1, handler, WasmBlockOperator.BLOCK, WasmBlockOperator.END );
block.add( new BranchNode( handler, handler, WasmBlockOperator.BR, null, catches.size() - i ) );
node.add( 0, block );
node = block;
int instrPos = findIdxOfCodePos( tryCat.getHandler() ) + 1;
int instrPos = findIdxOfCodePos( handler ) + 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

View File

@ -58,6 +58,7 @@ public class Exceptions extends AbstractBaseTest {
addParam( list, script, "multiCatch" );
addParam( list, script, "multiCatch2" );
addParam( list, script, "multiCatch3" );
addParam( list, script, "multiCatch4" );
addParam( list, script, "serialCatch" );
addParam( list, script, "tryReturn" );
addParam( list, script, "whileTrueTryFinally" );
@ -254,6 +255,18 @@ public class Exceptions extends AbstractBaseTest {
return 13;
}
@Export
static int multiCatch4() throws Throwable {
try {
Object val = null;
return 42;
} catch( ArrayIndexOutOfBoundsException | IllegalArgumentException x ) {
throw new Throwable( x );
} catch( Throwable x ) {
throw new Throwable( x );
}
}
@Export
static int serialCatch() {
int r = 42;