From 53daa1c6b082e6cc85d60f703da8c537bcc4cc07 Mon Sep 17 00:00:00 2001 From: PaddiM8 Date: Mon, 9 Dec 2024 22:42:23 +0100 Subject: [PATCH] vm: If popCount == _stack.Count in ExitBlock, just clear the stack and return --- src/Std/Iteration.cs | 6 ++++++ src/Vm/InstructionExecutor.cs | 11 ++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Std/Iteration.cs b/src/Std/Iteration.cs index 7aa1680..f00499c 100644 --- a/src/Std/Iteration.cs +++ b/src/Std/Iteration.cs @@ -601,6 +601,12 @@ public static RuntimeGenerator Repeat(RuntimeObject item, RuntimeInteger? n = nu public static RuntimeGenerator Reverse(IEnumerable items) => new(items.Reverse()); + [ElkFunction("reverseMut")] + public static void Reverse(RuntimeList items) + { + items.Values.Reverse(); + } + /// All items /// The amount of items to skip from the left /// A generator for the first n items. diff --git a/src/Vm/InstructionExecutor.cs b/src/Vm/InstructionExecutor.cs index 5dbc923..3b35757 100644 --- a/src/Vm/InstructionExecutor.cs +++ b/src/Vm/InstructionExecutor.cs @@ -655,14 +655,19 @@ private void UnpackUpper(byte count) private void ExitBlock(byte popCount) { - if (_stack.Count == 1) + if (_stack.Count == popCount) + { + while (_stack.Any()) + Pop(); + return; + } - var returnValue = _stack.Pop(); + var returnValue = _stack.PopObject() as RuntimeObject; for (byte i = 0; i < popCount; i++) Pop(); - _stack.Push(returnValue); + _stack.Push(returnValue ?? RuntimeNil.Value); } private void Ret()