Skip to content

Commit 0822d9a

Browse files
VolkerVolker
authored andcommitted
END can not be outside of parent block
1 parent b5296e6 commit 0822d9a

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/de/inetsoftware/jwebassembly/module/BranchManger.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,13 @@ private void calculateIf( BranchNode parent, IfParsedBlock startBlock, List<Pars
260260
if( i > 0 ) {
261261
calculate( branch, parsedOperations.subList( 0, i ) );
262262
}
263-
branch.data = calculateBlockType( startPos, branch.endPos );
264-
endPos = parsedBlock.endPosition;
263+
ValueType blockType = calculateBlockType( startPos, branch.endPos );
264+
branch.data = blockType;
265+
// end position can not be outside of the parent
266+
endPos = Math.min( parsedBlock.endPosition, parent.endPos );
265267

266-
int breakDeep = calculateBreakDeep( parent, endPos );
268+
// if with block type signature must have an else block
269+
int breakDeep = blockType == ValueType.empty ? calculateBreakDeep( parent, endPos ) : -1;
267270
if( breakDeep >= 0 ) {
268271
branch.endOp = WasmBlockOperator.END;
269272
branch.add( new BranchNode( startBlock.endPosition, endPos, WasmBlockOperator.BR, null, breakDeep + 1 ) );

test/de/inetsoftware/jwebassembly/runtime/ControlFlowOperators.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ static int forLoop() {
299299
@Export
300300
static int conditionalOperator () {
301301
int condition = 4;
302-
return condition >= 4 ? 1 : 2;
302+
return condition >= 4 ? condition < 4 ? 1 : 2 : condition == 4 ? 3 : 4;
303303
}
304304
}
305305
}

0 commit comments

Comments
 (0)