From 831e786537c6168c7850874e4cc44867480baffa Mon Sep 17 00:00:00 2001 From: Joyless <65855333+Joy-less@users.noreply.github.com> Date: Sat, 12 Aug 2023 22:17:14 +0100 Subject: [PATCH 1/2] Fix #198 (varargs count is 1 when void) --- .../VM/Processor/Processor_InstructionLoop.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/MoonSharp.Interpreter/Execution/VM/Processor/Processor_InstructionLoop.cs b/src/MoonSharp.Interpreter/Execution/VM/Processor/Processor_InstructionLoop.cs index 38c0f63e..608fc3ea 100644 --- a/src/MoonSharp.Interpreter/Execution/VM/Processor/Processor_InstructionLoop.cs +++ b/src/MoonSharp.Interpreter/Execution/VM/Processor/Processor_InstructionLoop.cs @@ -642,7 +642,18 @@ private void ExecArgs(Instruction I) { if (i >= argsList.Count) { - this.AssignLocal(I.SymbolList[i], DynValue.NewNil()); + var stackframe = m_ExecutionStack.Peek(); + SymbolRef symref = I.SymbolList[i]; + + bool nothingWasPassedToVarArgs = symref.i_Name == WellKnownSymbols.VARARGS; + if (nothingWasPassedToVarArgs) + { + stackframe.LocalScope[symref.i_Index] = DynValue.Void; + } + else + { + this.AssignLocal(symref, DynValue.NewNil()); + } } else if ((i == I.SymbolList.Length - 1) && (I.SymbolList[i].i_Name == WellKnownSymbols.VARARGS)) { From 48005e3002d62dfb56ea3423f3914a1439a52a44 Mon Sep 17 00:00:00 2001 From: Joyless <65855333+Joy-less@users.noreply.github.com> Date: Sat, 12 Aug 2023 23:08:41 +0100 Subject: [PATCH 2/2] Improved fix for #198 --- .../VM/Processor/Processor_InstructionLoop.cs | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/MoonSharp.Interpreter/Execution/VM/Processor/Processor_InstructionLoop.cs b/src/MoonSharp.Interpreter/Execution/VM/Processor/Processor_InstructionLoop.cs index 608fc3ea..29f97873 100644 --- a/src/MoonSharp.Interpreter/Execution/VM/Processor/Processor_InstructionLoop.cs +++ b/src/MoonSharp.Interpreter/Execution/VM/Processor/Processor_InstructionLoop.cs @@ -351,15 +351,25 @@ internal string PerformMessageDecorationBeforeUnwind(DynValue messageHandler, st - private void AssignLocal(SymbolRef symref, DynValue value) + private void AssignLocal(SymbolRef symref, DynValue value, bool isOmittedArgumentInFunctionCall = false) { var stackframe = m_ExecutionStack.Peek(); - DynValue v = stackframe.LocalScope[symref.i_Index]; - if (v == null) - stackframe.LocalScope[symref.i_Index] = v = DynValue.NewNil(); + DynValue currentValue = stackframe.LocalScope[symref.i_Index]; + if (currentValue == null) + { + if (isOmittedArgumentInFunctionCall && symref.i_Name == WellKnownSymbols.VARARGS) + { + stackframe.LocalScope[symref.i_Index] = DynValue.Void; + return; + } + else + { + stackframe.LocalScope[symref.i_Index] = currentValue = DynValue.NewNil(); + } + } - v.Assign(value); + currentValue.Assign(value); } private void ExecStoreLcl(Instruction i) @@ -642,18 +652,7 @@ private void ExecArgs(Instruction I) { if (i >= argsList.Count) { - var stackframe = m_ExecutionStack.Peek(); - SymbolRef symref = I.SymbolList[i]; - - bool nothingWasPassedToVarArgs = symref.i_Name == WellKnownSymbols.VARARGS; - if (nothingWasPassedToVarArgs) - { - stackframe.LocalScope[symref.i_Index] = DynValue.Void; - } - else - { - this.AssignLocal(symref, DynValue.NewNil()); - } + this.AssignLocal(I.SymbolList[i], DynValue.NewNil(), isOmittedArgumentInFunctionCall: true); } else if ((i == I.SymbolList.Length - 1) && (I.SymbolList[i].i_Name == WellKnownSymbols.VARARGS)) {