try to fix crashes under Mono when calling a cached function with the wrong arguments

This commit is contained in:
CasualPokePlayer 2023-11-16 09:08:20 -08:00
parent fba66c3d28
commit cef72bef4b
5 changed files with 27 additions and 13 deletions

View File

@ -1,4 +1,5 @@
using System;
using NLua.Native;
namespace NLua.GenerateEventAssembly

View File

@ -14,13 +14,13 @@ namespace NLua
public class MetaFunctions
{
public static readonly LuaNativeFunction GcFunction = CollectObject;
public static readonly LuaNativeFunction IndexFunction = GetMethod;
public static readonly LuaNativeFunction IndexFunction = GetMethod;
public static readonly LuaNativeFunction NewIndexFunction = SetFieldOrProperty;
public static readonly LuaNativeFunction BaseIndexFunction = GetBaseMethod;
public static readonly LuaNativeFunction ClassIndexFunction = GetClassMethod;
public static readonly LuaNativeFunction ClassNewIndexFunction = SetClassFieldOrProperty;
public static readonly LuaNativeFunction ExecuteDelegateFunction = RunFunctionDelegate;
public static readonly LuaNativeFunction CallConstructorFunction = CallConstructor;
public static readonly LuaNativeFunction BaseIndexFunction = GetBaseMethod;
public static readonly LuaNativeFunction ClassIndexFunction = GetClassMethod;
public static readonly LuaNativeFunction ClassNewIndexFunction = SetClassFieldOrProperty;
public static readonly LuaNativeFunction ExecuteDelegateFunction = RunFunctionDelegate;
public static readonly LuaNativeFunction CallConstructorFunction = CallConstructor;
public static readonly LuaNativeFunction ToStringFunction = ToStringLua;
public static readonly LuaNativeFunction CallDelegateFunction = CallDelegate;
@ -31,7 +31,7 @@ namespace NLua
public static readonly LuaNativeFunction ModulosFunction = ModLua;
public static readonly LuaNativeFunction UnaryNegationFunction = UnaryNegationLua;
public static readonly LuaNativeFunction EqualFunction = EqualLua;
public static readonly LuaNativeFunction LessThanFunction = LessThanLua;
public static readonly LuaNativeFunction LessThanFunction = LessThanLua;
public static readonly LuaNativeFunction LessThanOrEqualFunction = LessThanOrEqualLua;
internal readonly Dictionary<object, Dictionary<object, object>> _memberCache = new();

View File

@ -103,7 +103,7 @@ namespace NLua.Method
internal int SetPendingException(Exception e)
=> _translator.interpreter.SetPendingException(e);
internal void FillMethodArguments(LuaState luaState, int numStackToSkip)
internal int FillMethodArguments(LuaState luaState, int numStackToSkip)
{
var args = _lastCalledMethod.args;
@ -114,7 +114,7 @@ namespace NLua.Method
if (_lastCalledMethod.argTypes[i].IsParamsArray)
{
var count = _lastCalledMethod.argTypes.Length - i;
var paramArray = _translator.TableToArray(luaState, type.ExtractValue, type.ParameterType, index, count);
var paramArray = ObjectTranslator.TableToArray(luaState, type.ExtractValue, type.ParameterType, index, count);
args[_lastCalledMethod.argTypes[i].Index] = paramArray;
}
else
@ -125,9 +125,11 @@ namespace NLua.Method
if (_lastCalledMethod.args[_lastCalledMethod.argTypes[i].Index] == null &&
!luaState.IsNil(i + 1 + numStackToSkip))
{
throw new LuaException($"Argument number {(i + 1)} is invalid");
return i + 1;
}
}
return 0;
}
internal int PushReturnValue(LuaState luaState)
@ -224,7 +226,13 @@ namespace NLua.Method
throw new LuaException("Lua stack overflow");
}
FillMethodArguments(luaState, numStackToSkip);
var invalidArgNum = FillMethodArguments(luaState, 0);
if (invalidArgNum != 0)
{
_translator.ThrowError(luaState, $"Argument number {invalidArgNum} is invalid");
return 1;
}
return CallInvoke(luaState, method, targetObject);
}
@ -335,7 +343,12 @@ namespace NLua.Method
throw new LuaException("Lua stack overflow");
}
FillMethodArguments(luaState, 0);
var invalidArgNum = FillMethodArguments(luaState, 0);
if (invalidArgNum != 0)
{
_translator.ThrowError(luaState, $"Argument number {invalidArgNum} is invalid");
return 1;
}
}
else if (!_translator.MatchParameters(luaState, _method, _lastCalledMethod, 0))
{

View File

@ -1097,7 +1097,7 @@ namespace NLua
internal bool MatchParameters(LuaState luaState, MethodBase method, MethodCache methodCache, int skipParam)
=> metaFunctions.MatchParameters(luaState, method, methodCache, skipParam);
internal Array TableToArray(LuaState luaState, ExtractValue extractValue, Type paramArrayType, int startIndex, int count)
internal static Array TableToArray(LuaState luaState, ExtractValue extractValue, Type paramArrayType, int startIndex, int count)
=> MetaFunctions.TableToArray(luaState, extractValue, paramArrayType, ref startIndex, count);
private Type TypeOf(LuaState luaState, int idx)

Binary file not shown.