From 6085ccec55c8cfecf054a330d72cb6551ab33337 Mon Sep 17 00:00:00 2001 From: PaddiM8 Date: Thu, 5 Dec 2024 22:33:30 +0100 Subject: [PATCH] vm: Fix ExitBlock crashing when stack only contains one element --- src/Vm/InstructionExecutor.cs | 3 +++ src/Vm/InstructionGenerator.cs | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Vm/InstructionExecutor.cs b/src/Vm/InstructionExecutor.cs index bb01091..551c8eb 100644 --- a/src/Vm/InstructionExecutor.cs +++ b/src/Vm/InstructionExecutor.cs @@ -652,6 +652,9 @@ private void UnpackUpper(byte count) private void ExitBlock(byte popCount) { + if (_stack.Count == 1) + return; + var returnValue = _stack.Pop(); for (byte i = 0; i < popCount; i++) Pop(); diff --git a/src/Vm/InstructionGenerator.cs b/src/Vm/InstructionGenerator.cs index 619fe39..f3ac414 100644 --- a/src/Vm/InstructionGenerator.cs +++ b/src/Vm/InstructionGenerator.cs @@ -453,7 +453,8 @@ private void ExitBlock(bool isPrimaryExitPoint, int? newScopeDepth = null) if (popCount > byte.MaxValue) throw new RuntimeException("Too many variables in block"); - Emit(InstructionKind.ExitBlock, (byte)popCount); + if (popCount > 0) + Emit(InstructionKind.ExitBlock, (byte)popCount); } private void ClearBlock(bool isPrimaryExitPoint, int? newScopeDepth = null)