@@ -987,8 +987,10 @@ struct InstructionTranslator<Context: TranslatorContext>: InstructionVisitor {
987987 case . local( let localIndex) :
988988 // Re-push local variables to the stack
989989 _ = try valueStack. pushLocal ( localIndex, locals: & locals)
990- case . vreg, nil :
990+ case . vreg:
991991 _ = valueStack. push ( type)
992+ case nil :
993+ _ = valueStack. push ( . unknown)
992994 case . const( let index, let type) :
993995 valueStack. pushConst ( index, type: type)
994996 }
@@ -1408,6 +1410,13 @@ struct InstructionTranslator<Context: TranslatorContext>: InstructionVisitor {
14081410 // +---> [0x0b] ...
14091411 for (entryIndex, labelIndex) in allLabelIndices. enumerated ( ) {
14101412 let frame = try controlStack. branchTarget ( relativeDepth: labelIndex)
1413+
1414+ // Check copyTypes consistency
1415+ guard frame. copyTypes. count == defaultFrame. copyTypes. count else {
1416+ throw ValidationError ( " Expected the same copy types for all branches in `br_table` but got \( frame. copyTypes) and \( defaultFrame. copyTypes) " )
1417+ }
1418+ try popPushValues ( frame. copyTypes)
1419+
14111420 do {
14121421 let relativeOffset = iseqBuilder. insertingPC. offsetFromHead - brTableAt. offsetFromHead
14131422 tableBuffer [ entryIndex] = Instruction . BrTableOperand. Entry (
@@ -1427,6 +1436,10 @@ struct InstructionTranslator<Context: TranslatorContext>: InstructionVisitor {
14271436 }
14281437 }
14291438 }
1439+ // Pop branch copy values for type checking
1440+ for type in defaultFrame. copyTypes. reversed ( ) {
1441+ _ = try popOperand ( type)
1442+ }
14301443 try markUnreachable ( )
14311444 }
14321445
0 commit comments