improve error handling in the BranchManager

This commit is contained in:
Volker Berlin 2020-04-10 13:40:55 +02:00
parent ea2a179161
commit 432168f002

View File

@ -1197,42 +1197,45 @@ class BranchManger {
} }
if( startBlock != null && startBlock.getOperation() == WasmBlockOperator.IF ) { if( startBlock != null && startBlock.getOperation() == WasmBlockOperator.IF ) {
ArrayDeque<AnyType> stack = new ArrayDeque<>(); try {
stack.push( ValueType.empty ); ArrayDeque<AnyType> stack = new ArrayDeque<>();
INSTRUCTIONS: stack.push( ValueType.empty );
for( int i = startIdx; i < instructions.size(); i++ ) { INSTRUCTIONS: for( int i = startIdx; i < instructions.size(); i++ ) {
WasmInstruction instr = instructions.get( i ); WasmInstruction instr = instructions.get( i );
int codePos = instr.getCodePosition(); int codePos = instr.getCodePosition();
if( codePos >= endPos ) { if( codePos >= endPos ) {
break; break;
} }
int popCount = instr.getPopCount(); int popCount = instr.getPopCount();
for( int p = 0; p < popCount; p++ ) { for( int p = 0; p < popCount; p++ ) {
stack.pop(); stack.pop();
} }
AnyType pushValue = instr.getPushValueType(); AnyType pushValue = instr.getPushValueType();
if( pushValue != null ) { if( pushValue != null ) {
stack.push( pushValue ); stack.push( pushValue );
} }
if( instr.getType() == Type.Block ) { if( instr.getType() == Type.Block ) {
switch( ((WasmBlockInstruction)instr).getOperation() ) { switch( ((WasmBlockInstruction)instr).getOperation() ) {
case RETURN: case RETURN:
// set "empty" block type // set "empty" block type
while( stack.size() > 1 ) { while( stack.size() > 1 ) {
stack.pop(); stack.pop();
} }
break INSTRUCTIONS; break INSTRUCTIONS;
case IF: case IF:
case BLOCK: case BLOCK:
case LOOP: case LOOP:
// skip the content of the block, important to not count ELSE blocks // skip the content of the block, important to not count ELSE blocks
i = findEndInstruction( instructions, i ); i = findEndInstruction( instructions, i );
break; break;
}
} }
} }
startBlock.setData( stack.pop() );
} catch( Throwable th ) {
throw WasmException.create( th, startBlock.getLineNumber() );
} }
startBlock.setData( stack.pop() );
} }
} }