fix all the analyser warnings in NLua
This commit is contained in:
parent
bb0d8f231d
commit
582446651f
|
@ -29,6 +29,7 @@
|
|||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<NoWarn>$(NoWarn);IDE0005</NoWarn>
|
||||
<Nullable>disable</Nullable>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
|
|
|
@ -1,13 +1,16 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using NLua.Method;
|
||||
|
||||
using NLua.Extensions;
|
||||
using NLua.GenerateEventAssembly;
|
||||
using NLua.Method;
|
||||
using NLua.Native;
|
||||
|
||||
namespace NLua
|
||||
{
|
||||
internal sealed class CheckType
|
||||
{
|
||||
internal readonly Dictionary<Type, ExtractValue> _extractValues = new Dictionary<Type, ExtractValue>();
|
||||
internal readonly Dictionary<Type, ExtractValue> _extractValues = new();
|
||||
internal readonly ExtractValue _extractNetObject;
|
||||
internal readonly ObjectTranslator _translator;
|
||||
|
||||
|
@ -43,45 +46,45 @@ namespace NLua
|
|||
/// returning a conversion function if it does and null otherwise.
|
||||
/// </summary>
|
||||
internal ExtractValue GetExtractor(ProxyType paramType)
|
||||
{
|
||||
return GetExtractor(paramType.UnderlyingSystemType);
|
||||
}
|
||||
=> GetExtractor(paramType.UnderlyingSystemType);
|
||||
|
||||
internal ExtractValue GetExtractor(Type paramType)
|
||||
{
|
||||
if (paramType.IsByRef)
|
||||
{
|
||||
paramType = paramType.GetElementType();
|
||||
}
|
||||
|
||||
return _extractValues.TryGetValue(paramType, out ExtractValue value) ? value : _extractNetObject;
|
||||
return _extractValues.TryGetValue(paramType!, out var value) ? value : _extractNetObject;
|
||||
}
|
||||
|
||||
internal ExtractValue CheckLuaType(LuaState luaState, int stackPos, Type paramType)
|
||||
{
|
||||
LuaType luatype = luaState.Type(stackPos);
|
||||
var luatype = luaState.Type(stackPos);
|
||||
|
||||
if (paramType.IsByRef)
|
||||
paramType = paramType.GetElementType();
|
||||
|
||||
var underlyingType = Nullable.GetUnderlyingType(paramType);
|
||||
|
||||
if (underlyingType != null)
|
||||
{
|
||||
paramType = underlyingType; // Silently convert nullable types to their non null requics
|
||||
paramType = paramType.GetElementType();
|
||||
}
|
||||
|
||||
var underlyingType = Nullable.GetUnderlyingType(paramType!);
|
||||
if (underlyingType != null)
|
||||
{
|
||||
paramType = underlyingType; // Silently convert nullable types to their non null requics
|
||||
}
|
||||
|
||||
bool netParamIsNumeric = paramType == typeof(int) ||
|
||||
paramType == typeof(uint) ||
|
||||
paramType == typeof(long) ||
|
||||
paramType == typeof(ulong) ||
|
||||
paramType == typeof(short) ||
|
||||
paramType == typeof(ushort) ||
|
||||
paramType == typeof(float) ||
|
||||
paramType == typeof(double) ||
|
||||
paramType == typeof(decimal) ||
|
||||
paramType == typeof(byte) ||
|
||||
paramType == typeof(sbyte) ||
|
||||
paramType == typeof(char);
|
||||
var netParamIsNumeric = paramType == typeof(int) ||
|
||||
paramType == typeof(uint) ||
|
||||
paramType == typeof(long) ||
|
||||
paramType == typeof(ulong) ||
|
||||
paramType == typeof(short) ||
|
||||
paramType == typeof(ushort) ||
|
||||
paramType == typeof(float) ||
|
||||
paramType == typeof(double) ||
|
||||
paramType == typeof(decimal) ||
|
||||
paramType == typeof(byte) ||
|
||||
paramType == typeof(sbyte) ||
|
||||
paramType == typeof(char);
|
||||
|
||||
// If it is a nullable
|
||||
if (underlyingType != null)
|
||||
|
@ -91,94 +94,139 @@ namespace NLua
|
|||
{
|
||||
// Return the correct extractor anyways
|
||||
if (netParamIsNumeric || paramType == typeof(bool))
|
||||
{
|
||||
return _extractValues[paramType];
|
||||
}
|
||||
|
||||
return _extractNetObject;
|
||||
}
|
||||
}
|
||||
|
||||
if (paramType == typeof(object))
|
||||
{
|
||||
return _extractValues[paramType];
|
||||
}
|
||||
|
||||
//CP: Added support for generic parameters
|
||||
// CP: Added support for generic parameters
|
||||
if (paramType.IsGenericParameter)
|
||||
{
|
||||
if (luatype == LuaType.Boolean)
|
||||
return _extractValues[typeof(bool)];
|
||||
if (luatype == LuaType.String)
|
||||
return _extractValues[typeof(string)];
|
||||
if (luatype == LuaType.Table)
|
||||
return _extractValues[typeof(LuaTable)];
|
||||
if (luatype == LuaType.Thread)
|
||||
return _extractValues[typeof(LuaThread)];
|
||||
if (luatype == LuaType.UserData)
|
||||
return _extractValues[typeof(object)];
|
||||
if (luatype == LuaType.Function)
|
||||
return _extractValues[typeof(LuaFunction)];
|
||||
if (luatype == LuaType.Number)
|
||||
return _extractValues[typeof(double)];
|
||||
switch (luatype)
|
||||
{
|
||||
case LuaType.Boolean:
|
||||
return _extractValues[typeof(bool)];
|
||||
case LuaType.String:
|
||||
return _extractValues[typeof(string)];
|
||||
case LuaType.Table:
|
||||
return _extractValues[typeof(LuaTable)];
|
||||
case LuaType.Thread:
|
||||
return _extractValues[typeof(LuaThread)];
|
||||
case LuaType.UserData:
|
||||
return _extractValues[typeof(object)];
|
||||
case LuaType.Function:
|
||||
return _extractValues[typeof(LuaFunction)];
|
||||
case LuaType.Number:
|
||||
return _extractValues[typeof(double)];
|
||||
case LuaType.None:
|
||||
case LuaType.Nil:
|
||||
case LuaType.LightUserData:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
bool netParamIsString = paramType == typeof(string) || paramType == typeof(char[]) || paramType == typeof(byte[]);
|
||||
|
||||
var netParamIsString = paramType == typeof(string) || paramType == typeof(char[]) || paramType == typeof(byte[]);
|
||||
|
||||
if (netParamIsNumeric)
|
||||
{
|
||||
if (luaState.IsNumericType(stackPos) && !netParamIsString)
|
||||
{
|
||||
return _extractValues[paramType];
|
||||
}
|
||||
}
|
||||
else if (paramType == typeof(bool))
|
||||
{
|
||||
if (luaState.IsBoolean(stackPos))
|
||||
{
|
||||
return _extractValues[paramType];
|
||||
}
|
||||
}
|
||||
else if (netParamIsString)
|
||||
{
|
||||
if (luaState.IsStringOrNumber(stackPos) || luatype == LuaType.Nil)
|
||||
{
|
||||
return _extractValues[paramType];
|
||||
}
|
||||
}
|
||||
else if (paramType == typeof(LuaTable))
|
||||
{
|
||||
if (luatype == LuaType.Table || luatype == LuaType.Nil)
|
||||
if (luatype is LuaType.Table or LuaType.Nil)
|
||||
{
|
||||
return _extractValues[paramType];
|
||||
}
|
||||
}
|
||||
else if (paramType == typeof(LuaThread))
|
||||
{
|
||||
if (luatype == LuaType.Thread || luatype == LuaType.Nil)
|
||||
if (luatype is LuaType.Thread or LuaType.Nil)
|
||||
{
|
||||
return _extractValues[paramType];
|
||||
}
|
||||
}
|
||||
else if (paramType == typeof(LuaUserData))
|
||||
{
|
||||
if (luatype == LuaType.UserData || luatype == LuaType.Nil)
|
||||
if (luatype is LuaType.UserData or LuaType.Nil)
|
||||
{
|
||||
return _extractValues[paramType];
|
||||
}
|
||||
}
|
||||
else if (paramType == typeof(LuaFunction))
|
||||
{
|
||||
if (luatype == LuaType.Function || luatype == LuaType.Nil)
|
||||
if (luatype is LuaType.Function or LuaType.Nil)
|
||||
{
|
||||
return _extractValues[paramType];
|
||||
}
|
||||
}
|
||||
else if (typeof(Delegate).IsAssignableFrom(paramType) && luatype == LuaType.Function && paramType.GetMethod("Invoke") != null)
|
||||
{
|
||||
return new DelegateGenerator(_translator, paramType).ExtractGenerated;
|
||||
else if (paramType.IsInterface && luatype == LuaType.Table)
|
||||
return new ClassGenerator(_translator, paramType).ExtractGenerated;
|
||||
else if ((paramType.IsInterface || paramType.IsClass) && luatype == LuaType.Nil)
|
||||
{
|
||||
// kevinh - allow nil to be silently converted to null - extractNetObject will return null when the item ain't found
|
||||
return _extractNetObject;
|
||||
}
|
||||
else if (luaState.Type(stackPos) == LuaType.Table)
|
||||
else switch (luatype)
|
||||
{
|
||||
if (luaState.GetMetaField(stackPos, "__index") != LuaType.Nil)
|
||||
case LuaType.Table when paramType.IsInterface:
|
||||
return new ClassGenerator(_translator, paramType).ExtractGenerated;
|
||||
case LuaType.Nil when paramType.IsInterface || paramType.IsClass:
|
||||
// kevinh - allow nil to be silently converted to null - extractNetObject will return null when the item ain't found
|
||||
return _extractNetObject;
|
||||
case LuaType.Table:
|
||||
{
|
||||
object obj = _translator.GetNetObject(luaState, -1);
|
||||
luaState.SetTop(-2);
|
||||
if (obj != null && paramType.IsInstanceOfType(obj))
|
||||
return _extractNetObject;
|
||||
if (luaState.GetMetaField(stackPos, "__index") != LuaType.Nil)
|
||||
{
|
||||
var obj = _translator.GetNetObject(luaState, -1);
|
||||
luaState.SetTop(-2);
|
||||
if (obj != null && paramType.IsInstanceOfType(obj))
|
||||
{
|
||||
return _extractNetObject;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
else
|
||||
case LuaType.None:
|
||||
case LuaType.Boolean:
|
||||
case LuaType.LightUserData:
|
||||
case LuaType.Number:
|
||||
case LuaType.String:
|
||||
case LuaType.Function:
|
||||
case LuaType.UserData:
|
||||
case LuaType.Thread:
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
||||
object netObj = _translator.GetNetObject(luaState, stackPos);
|
||||
var netObj = _translator.GetNetObject(luaState, stackPos);
|
||||
if (netObj != null && paramType.IsInstanceOfType(netObj))
|
||||
{
|
||||
return _extractNetObject;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
@ -188,186 +236,225 @@ namespace NLua
|
|||
/// index stackPos as the desired type if it can, or null
|
||||
/// otherwise.
|
||||
/// </summary>
|
||||
private object GetAsSbyte(LuaState luaState, int stackPos)
|
||||
private static object GetAsSbyte(LuaState luaState, int stackPos)
|
||||
{
|
||||
if (!luaState.IsNumericType(stackPos))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (luaState.IsInteger(stackPos))
|
||||
{
|
||||
return (sbyte)luaState.ToInteger(stackPos);
|
||||
}
|
||||
|
||||
return (sbyte)luaState.ToNumber(stackPos);
|
||||
}
|
||||
|
||||
private object GetAsByte(LuaState luaState, int stackPos)
|
||||
private static object GetAsByte(LuaState luaState, int stackPos)
|
||||
{
|
||||
if (!luaState.IsNumericType(stackPos))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (luaState.IsInteger(stackPos))
|
||||
{
|
||||
return (byte)luaState.ToInteger(stackPos);
|
||||
}
|
||||
|
||||
return (byte)luaState.ToNumber(stackPos);
|
||||
}
|
||||
|
||||
private object GetAsShort(LuaState luaState, int stackPos)
|
||||
private static object GetAsShort(LuaState luaState, int stackPos)
|
||||
{
|
||||
if (!luaState.IsNumericType(stackPos))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (luaState.IsInteger(stackPos))
|
||||
{
|
||||
return (short)luaState.ToInteger(stackPos);
|
||||
}
|
||||
|
||||
return (short)luaState.ToNumber(stackPos);
|
||||
}
|
||||
|
||||
private object GetAsUshort(LuaState luaState, int stackPos)
|
||||
private static object GetAsUshort(LuaState luaState, int stackPos)
|
||||
{
|
||||
if (!luaState.IsNumericType(stackPos))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (luaState.IsInteger(stackPos))
|
||||
{
|
||||
return (ushort)luaState.ToInteger(stackPos);
|
||||
}
|
||||
|
||||
return (ushort)luaState.ToNumber(stackPos);
|
||||
}
|
||||
|
||||
private object GetAsInt(LuaState luaState, int stackPos)
|
||||
private static object GetAsInt(LuaState luaState, int stackPos)
|
||||
{
|
||||
if (!luaState.IsNumericType(stackPos))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (luaState.IsInteger(stackPos))
|
||||
{
|
||||
return (int)luaState.ToInteger(stackPos);
|
||||
}
|
||||
|
||||
return (int)luaState.ToNumber(stackPos);
|
||||
}
|
||||
|
||||
private object GetAsUint(LuaState luaState, int stackPos)
|
||||
private static object GetAsUint(LuaState luaState, int stackPos)
|
||||
{
|
||||
if (!luaState.IsNumericType(stackPos))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (luaState.IsInteger(stackPos))
|
||||
{
|
||||
return (uint)luaState.ToInteger(stackPos);
|
||||
}
|
||||
|
||||
return (uint)luaState.ToNumber(stackPos);
|
||||
}
|
||||
|
||||
private object GetAsLong(LuaState luaState, int stackPos)
|
||||
private static object GetAsLong(LuaState luaState, int stackPos)
|
||||
{
|
||||
if (!luaState.IsNumericType(stackPos))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (luaState.IsInteger(stackPos))
|
||||
{
|
||||
return luaState.ToInteger(stackPos);
|
||||
}
|
||||
|
||||
return (long)luaState.ToNumber(stackPos);
|
||||
}
|
||||
|
||||
private object GetAsUlong(LuaState luaState, int stackPos)
|
||||
private static object GetAsUlong(LuaState luaState, int stackPos)
|
||||
{
|
||||
if (!luaState.IsNumericType(stackPos))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (luaState.IsInteger(stackPos))
|
||||
{
|
||||
return (ulong)luaState.ToInteger(stackPos);
|
||||
}
|
||||
|
||||
return (ulong)luaState.ToNumber(stackPos);
|
||||
}
|
||||
|
||||
private object GetAsDouble(LuaState luaState, int stackPos)
|
||||
private static object GetAsDouble(LuaState luaState, int stackPos)
|
||||
{
|
||||
if (!luaState.IsNumericType(stackPos))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (luaState.IsInteger(stackPos))
|
||||
{
|
||||
return (double)luaState.ToInteger(stackPos);
|
||||
}
|
||||
|
||||
return luaState.ToNumber(stackPos);
|
||||
}
|
||||
|
||||
private object GetAsChar(LuaState luaState, int stackPos)
|
||||
private static object GetAsChar(LuaState luaState, int stackPos)
|
||||
{
|
||||
if (!luaState.IsNumericType(stackPos))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (luaState.IsInteger(stackPos))
|
||||
{
|
||||
return (char)luaState.ToInteger(stackPos);
|
||||
}
|
||||
|
||||
return (char)luaState.ToNumber(stackPos);
|
||||
}
|
||||
|
||||
private object GetAsFloat(LuaState luaState, int stackPos)
|
||||
private static object GetAsFloat(LuaState luaState, int stackPos)
|
||||
{
|
||||
if (!luaState.IsNumericType(stackPos))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (luaState.IsInteger(stackPos))
|
||||
{
|
||||
return (float)luaState.ToInteger(stackPos);
|
||||
}
|
||||
|
||||
return (float)luaState.ToNumber(stackPos);
|
||||
}
|
||||
|
||||
private object GetAsDecimal(LuaState luaState, int stackPos)
|
||||
private static object GetAsDecimal(LuaState luaState, int stackPos)
|
||||
{
|
||||
if (!luaState.IsNumericType(stackPos))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (luaState.IsInteger(stackPos))
|
||||
{
|
||||
return (decimal)luaState.ToInteger(stackPos);
|
||||
}
|
||||
|
||||
return (decimal)luaState.ToNumber(stackPos);
|
||||
}
|
||||
|
||||
private object GetAsBoolean(LuaState luaState, int stackPos)
|
||||
{
|
||||
return luaState.ToBoolean(stackPos);
|
||||
}
|
||||
private static object GetAsBoolean(LuaState luaState, int stackPos)
|
||||
=> luaState.ToBoolean(stackPos);
|
||||
|
||||
private object GetAsCharArray(LuaState luaState, int stackPos)
|
||||
private static object GetAsCharArray(LuaState luaState, int stackPos)
|
||||
{
|
||||
if (!luaState.IsStringOrNumber(stackPos))
|
||||
{
|
||||
return null;
|
||||
string retVal = luaState.ToString(stackPos, false);
|
||||
}
|
||||
|
||||
var retVal = luaState.ToString(stackPos, false);
|
||||
return retVal.ToCharArray();
|
||||
}
|
||||
|
||||
private object GetAsByteArray(LuaState luaState, int stackPos)
|
||||
private static object GetAsByteArray(LuaState luaState, int stackPos)
|
||||
{
|
||||
if (!luaState.IsStringOrNumber(stackPos))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
byte [] retVal = luaState.ToBuffer(stackPos, false);
|
||||
var retVal = luaState.ToBuffer(stackPos, false);
|
||||
return retVal;
|
||||
}
|
||||
|
||||
private object GetAsString(LuaState luaState, int stackPos)
|
||||
{
|
||||
if (!luaState.IsStringOrNumber(stackPos))
|
||||
return null;
|
||||
return luaState.ToString(stackPos, false);
|
||||
}
|
||||
|
||||
private static object GetAsString(LuaState luaState, int stackPos)
|
||||
=> !luaState.IsStringOrNumber(stackPos) ? null : luaState.ToString(stackPos, false);
|
||||
|
||||
private object GetAsTable(LuaState luaState, int stackPos)
|
||||
{
|
||||
return _translator.GetTable(luaState, stackPos);
|
||||
}
|
||||
=> _translator.GetTable(luaState, stackPos);
|
||||
|
||||
private object GetAsThread(LuaState luaState, int stackPos)
|
||||
{
|
||||
return _translator.GetThread(luaState, stackPos);
|
||||
}
|
||||
=> _translator.GetThread(luaState, stackPos);
|
||||
|
||||
private object GetAsFunction(LuaState luaState, int stackPos)
|
||||
{
|
||||
return _translator.GetFunction(luaState, stackPos);
|
||||
}
|
||||
=> _translator.GetFunction(luaState, stackPos);
|
||||
|
||||
private object GetAsUserdata(LuaState luaState, int stackPos)
|
||||
{
|
||||
return _translator.GetUserData(luaState, stackPos);
|
||||
}
|
||||
=> _translator.GetUserData(luaState, stackPos);
|
||||
|
||||
public object GetAsObject(LuaState luaState, int stackPos)
|
||||
{
|
||||
|
@ -381,23 +468,27 @@ namespace NLua
|
|||
luaState.Remove(stackPos + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
luaState.SetTop(-2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
object obj = _translator.GetObject(luaState, stackPos);
|
||||
return obj;
|
||||
return _translator.GetObject(luaState, stackPos);
|
||||
}
|
||||
|
||||
public object GetAsNetObject(LuaState luaState, int stackPos)
|
||||
{
|
||||
object obj = _translator.GetNetObject(luaState, stackPos);
|
||||
|
||||
var obj = _translator.GetNetObject(luaState, stackPos);
|
||||
if (obj != null || luaState.Type(stackPos) != LuaType.Table)
|
||||
{
|
||||
return obj;
|
||||
}
|
||||
|
||||
if (luaState.GetMetaField(stackPos, "__index") == LuaType.Nil)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (luaState.CheckMetaTable(-1, _translator.Tag))
|
||||
{
|
||||
|
@ -406,7 +497,9 @@ namespace NLua
|
|||
obj = _translator.GetNetObject(luaState, stackPos);
|
||||
}
|
||||
else
|
||||
{
|
||||
luaState.SetTop(-2);
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
|
|
@ -15,6 +15,5 @@ namespace NLua.Exceptions
|
|||
public LuaException (string message, Exception innerException) : base(message, innerException)
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -43,10 +43,8 @@ namespace NLua.Exceptions
|
|||
IsNetException = true;
|
||||
}
|
||||
|
||||
// Prepend the error source
|
||||
public override string ToString()
|
||||
{
|
||||
// Prepend the error source
|
||||
return GetType().FullName + ": " + _source + Message;
|
||||
}
|
||||
=> GetType().FullName + ": " + _source + Message;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
using NLua.Native;
|
||||
|
||||
namespace NLua.Extensions
|
||||
{
|
||||
internal static class LuaExtensions
|
||||
|
@ -8,112 +10,115 @@ namespace NLua.Extensions
|
|||
public static bool CheckMetaTable(this LuaState state, int index, IntPtr tag)
|
||||
{
|
||||
if (!state.GetMetaTable(index))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
state.PushLightUserData(tag);
|
||||
state.RawGet(-2);
|
||||
bool isNotNil = !state.IsNil(-1);
|
||||
var isNotNil = !state.IsNil(-1);
|
||||
state.SetTop(-3);
|
||||
return isNotNil;
|
||||
}
|
||||
|
||||
public static void PopGlobalTable(this LuaState luaState)
|
||||
{
|
||||
luaState.RawSetInteger(LuaRegistry.Index, (long) LuaRegistryIndex.Globals);
|
||||
}
|
||||
=> luaState.RawSetInteger(LuaRegistry.Index, (long)LuaRegistryIndex.Globals);
|
||||
|
||||
public static void GetRef (this LuaState luaState, int reference)
|
||||
{
|
||||
luaState.RawGetInteger(LuaRegistry.Index, reference);
|
||||
}
|
||||
public static void GetRef(this LuaState luaState, int reference)
|
||||
=> luaState.RawGetInteger(LuaRegistry.Index, reference);
|
||||
|
||||
// ReSharper disable once IdentifierTypo
|
||||
public static void Unref (this LuaState luaState, int reference)
|
||||
{
|
||||
luaState.Unref(LuaRegistry.Index, reference);
|
||||
}
|
||||
public static void Unref(this LuaState luaState, int reference)
|
||||
=> luaState.Unref(LuaRegistry.Index, reference);
|
||||
|
||||
public static bool AreEqual(this LuaState luaState, int ref1, int ref2)
|
||||
{
|
||||
return luaState.Compare(ref1, ref2, LuaCompare.Equal);
|
||||
}
|
||||
=> luaState.Compare(ref1, ref2, LuaCompare.Equal);
|
||||
|
||||
public static IntPtr CheckUData(this LuaState state, int ud, string name)
|
||||
{
|
||||
IntPtr p = state.ToUserData(ud);
|
||||
var p = state.ToUserData(ud);
|
||||
if (p == IntPtr.Zero)
|
||||
{
|
||||
return IntPtr.Zero;
|
||||
}
|
||||
|
||||
if (!state.GetMetaTable(ud))
|
||||
{
|
||||
return IntPtr.Zero;
|
||||
}
|
||||
|
||||
state.GetField(LuaRegistry.Index, name);
|
||||
|
||||
bool isEqual = state.RawEqual(-1, -2);
|
||||
|
||||
var isEqual = state.RawEqual(-1, -2);
|
||||
state.Pop(2);
|
||||
|
||||
if (isEqual)
|
||||
return p;
|
||||
|
||||
return IntPtr.Zero;
|
||||
return isEqual ? p : IntPtr.Zero;
|
||||
}
|
||||
|
||||
public static int ToNetObject(this LuaState state, int index, IntPtr tag)
|
||||
{
|
||||
if (state.Type(index) != LuaType.UserData)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
IntPtr userData;
|
||||
|
||||
if (state.CheckMetaTable(index, tag))
|
||||
{
|
||||
userData = state.ToUserData(index);
|
||||
if (userData != IntPtr.Zero)
|
||||
{
|
||||
return Marshal.ReadInt32(userData);
|
||||
}
|
||||
}
|
||||
|
||||
userData = state.CheckUData(index, "luaNet_class");
|
||||
if (userData != IntPtr.Zero)
|
||||
{
|
||||
return Marshal.ReadInt32(userData);
|
||||
}
|
||||
|
||||
userData = state.CheckUData(index, "luaNet_searchbase");
|
||||
if (userData != IntPtr.Zero)
|
||||
{
|
||||
return Marshal.ReadInt32(userData);
|
||||
}
|
||||
|
||||
userData = state.CheckUData(index, "luaNet_function");
|
||||
if (userData != IntPtr.Zero)
|
||||
{
|
||||
return Marshal.ReadInt32(userData);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static void NewUData(this LuaState state, int val)
|
||||
{
|
||||
IntPtr pointer = state.NewUserData(Marshal.SizeOf(typeof(int)));
|
||||
var pointer = state.NewUserData(Marshal.SizeOf(typeof(int)));
|
||||
Marshal.WriteInt32(pointer, val);
|
||||
}
|
||||
|
||||
public static int RawNetObj(this LuaState state, int index)
|
||||
{
|
||||
IntPtr pointer = state.ToUserData(index);
|
||||
var pointer = state.ToUserData(index);
|
||||
if (pointer == IntPtr.Zero)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return Marshal.ReadInt32(pointer);
|
||||
}
|
||||
|
||||
public static int CheckUObject(this LuaState state, int index, string name)
|
||||
{
|
||||
IntPtr udata = state.CheckUData(index, name);
|
||||
var udata = state.CheckUData(index, name);
|
||||
if (udata == IntPtr.Zero)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return Marshal.ReadInt32(udata);
|
||||
}
|
||||
|
||||
public static bool IsNumericType(this LuaState state, int index)
|
||||
{
|
||||
return state.Type(index) == LuaType.Number;
|
||||
}
|
||||
=> state.Type(index) == LuaType.Number;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,13 +6,15 @@ namespace NLua.Extensions
|
|||
{
|
||||
public static IEnumerable<string> SplitWithEscape(this string input, char separator, char escapeCharacter)
|
||||
{
|
||||
int start = 0;
|
||||
int index = 0;
|
||||
var start = 0;
|
||||
var index = 0;
|
||||
while (index < input.Length)
|
||||
{
|
||||
index = input.IndexOf(separator, index);
|
||||
if (index == -1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (input[index - 1] == escapeCharacter)
|
||||
{
|
||||
|
@ -20,11 +22,12 @@ namespace NLua.Extensions
|
|||
continue;
|
||||
}
|
||||
|
||||
|
||||
yield return input.Substring(start, index - start);
|
||||
|
||||
index++;
|
||||
start = index;
|
||||
}
|
||||
|
||||
yield return input.Substring(start);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,93 +15,58 @@ namespace NLua.Extensions
|
|||
}
|
||||
|
||||
public static bool HasAdditionOperator(this Type t)
|
||||
{
|
||||
if (t.IsPrimitive)
|
||||
return true;
|
||||
|
||||
return t.HasMethod("op_Addition");
|
||||
}
|
||||
=> t.IsPrimitive || t.HasMethod("op_Addition");
|
||||
|
||||
public static bool HasSubtractionOperator(this Type t)
|
||||
{
|
||||
if (t.IsPrimitive)
|
||||
return true;
|
||||
|
||||
return t.HasMethod("op_Subtraction");
|
||||
}
|
||||
=> t.IsPrimitive || t.HasMethod("op_Subtraction");
|
||||
|
||||
public static bool HasMultiplyOperator(this Type t)
|
||||
{
|
||||
if (t.IsPrimitive)
|
||||
return true;
|
||||
|
||||
return t.HasMethod("op_Multiply");
|
||||
}
|
||||
=> t.IsPrimitive || t.HasMethod("op_Multiply");
|
||||
|
||||
public static bool HasDivisionOperator(this Type t)
|
||||
{
|
||||
if (t.IsPrimitive)
|
||||
return true;
|
||||
|
||||
return t.HasMethod("op_Division");
|
||||
}
|
||||
=> t.IsPrimitive || t.HasMethod("op_Division");
|
||||
|
||||
public static bool HasModulusOperator(this Type t)
|
||||
{
|
||||
if (t.IsPrimitive)
|
||||
return true;
|
||||
|
||||
return t.HasMethod("op_Modulus");
|
||||
}
|
||||
=> t.IsPrimitive || t.HasMethod("op_Modulus");
|
||||
|
||||
public static bool HasUnaryNegationOperator(this Type t)
|
||||
{
|
||||
if (t.IsPrimitive)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// Unary - will always have only one version.
|
||||
var op = t.GetMethod("op_UnaryNegation", BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
|
||||
return op != null;
|
||||
}
|
||||
|
||||
public static bool HasEqualityOperator(this Type t)
|
||||
{
|
||||
if (t.IsPrimitive)
|
||||
return true;
|
||||
return t.HasMethod("op_Equality");
|
||||
}
|
||||
=> t.IsPrimitive || t.HasMethod("op_Equality");
|
||||
|
||||
public static bool HasLessThanOperator(this Type t)
|
||||
{
|
||||
if (t.IsPrimitive)
|
||||
return true;
|
||||
|
||||
return t.HasMethod("op_LessThan");
|
||||
}
|
||||
=> t.IsPrimitive || t.HasMethod("op_LessThan");
|
||||
|
||||
public static bool HasLessThanOrEqualOperator(this Type t)
|
||||
{
|
||||
if (t.IsPrimitive)
|
||||
return true;
|
||||
return t.HasMethod("op_LessThanOrEqual");
|
||||
}
|
||||
=> t.IsPrimitive || t.HasMethod("op_LessThanOrEqual");
|
||||
|
||||
public static MethodInfo[] GetMethods(this Type t, string name, BindingFlags flags)
|
||||
{
|
||||
return t.GetMethods(flags).Where(m => m.Name == name).ToArray();
|
||||
}
|
||||
=> t.GetMethods(flags).Where(m => m.Name == name).ToArray();
|
||||
|
||||
public static MethodInfo[] GetExtensionMethods(this Type type, string name, IEnumerable<Assembly> assemblies = null)
|
||||
{
|
||||
var types = new List<Type>();
|
||||
|
||||
types.AddRange(type.Assembly.GetTypes().Where(t => t.IsPublic));
|
||||
|
||||
if (assemblies != null)
|
||||
{
|
||||
foreach (Assembly item in assemblies)
|
||||
foreach (var item in assemblies)
|
||||
{
|
||||
if (item == type.Assembly)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
types.AddRange(item.GetTypes().Where(t => t.IsPublic && t.IsClass && t.IsSealed && t.IsAbstract && !t.IsNested));
|
||||
}
|
||||
}
|
||||
|
@ -133,10 +98,8 @@ namespace NLua.Extensions
|
|||
/// <returns></returns>
|
||||
public static MethodInfo GetExtensionMethod(this Type t, string name, IEnumerable<Assembly> assemblies = null)
|
||||
{
|
||||
var mi = t.GetExtensionMethods(name, assemblies).ToArray();
|
||||
if (mi.Length == 0)
|
||||
return null;
|
||||
return mi[0];
|
||||
var mi = t.GetExtensionMethods(name, assemblies);
|
||||
return mi.Length == 0 ? null : mi[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
using System;
|
||||
using NLua.Native;
|
||||
|
||||
namespace NLua
|
||||
namespace NLua.GenerateEventAssembly
|
||||
{
|
||||
internal class ClassGenerator
|
||||
{
|
||||
|
@ -14,8 +15,6 @@ namespace NLua
|
|||
}
|
||||
|
||||
public object ExtractGenerated(LuaState luaState, int stackPos)
|
||||
{
|
||||
return CodeGeneration.Instance.GetClassInstance(_klass, _translator.GetTable(luaState, stackPos));
|
||||
}
|
||||
=> CodeGeneration.Instance.GetClassInstance(_klass, _translator.GetTable(luaState, stackPos));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,17 +1,16 @@
|
|||
using System;
|
||||
using System.Threading;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
using System.Reflection.Emit;
|
||||
using System.Collections.Generic;
|
||||
using NLua.Method;
|
||||
|
||||
namespace NLua
|
||||
namespace NLua.GenerateEventAssembly
|
||||
{
|
||||
internal class CodeGeneration
|
||||
{
|
||||
private readonly Dictionary<Type, LuaClassType> _classCollection = new Dictionary<Type, LuaClassType>();
|
||||
private readonly Dictionary<Type, Type> _delegateCollection = new Dictionary<Type, Type>();
|
||||
private readonly Dictionary<Type, LuaClassType> _classCollection = new();
|
||||
private readonly Dictionary<Type, Type> _delegateCollection = new();
|
||||
|
||||
static CodeGeneration()
|
||||
{
|
||||
|
@ -24,31 +23,14 @@ namespace NLua
|
|||
/// <summary>
|
||||
/// Singleton instance of the class
|
||||
/// </summary>
|
||||
public static CodeGeneration Instance { get; } = new CodeGeneration();
|
||||
public static CodeGeneration Instance { get; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Generates an event handler that calls a Lua function
|
||||
/// </summary>
|
||||
private Type GenerateEvent(Type eventHandlerType)
|
||||
{
|
||||
throw new NotImplementedException("Emit not available on .NET Standard ");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a type that can be used for instantiating a delegate
|
||||
/// of the provided type, given a Lua function.
|
||||
/// </summary>
|
||||
private Type GenerateDelegate(Type delegateType)
|
||||
{
|
||||
throw new NotImplementedException("GenerateDelegate is not available on Windows Store, please register your LuaDelegate type with Lua.RegisterLuaDelegateType( yourDelegate, theLuaDelegateHandler) ");
|
||||
}
|
||||
|
||||
internal void GetReturnTypesFromClass(Type klass, out Type[][] returnTypes)
|
||||
internal static void GetReturnTypesFromClass(Type klass, out Type[][] returnTypes)
|
||||
{
|
||||
var classMethods = klass.GetMethods();
|
||||
returnTypes = new Type[classMethods.Length][];
|
||||
|
||||
int i = 0;
|
||||
var i = 0;
|
||||
|
||||
foreach (var method in classMethods)
|
||||
{
|
||||
|
@ -65,16 +47,7 @@ namespace NLua
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates an implementation of klass, if it is an interface, or
|
||||
/// a subclass of klass that delegates its virtual methods to a Lua table.
|
||||
/// </summary>
|
||||
public void GenerateClass(Type klass, out Type newType, out Type[][] returnTypes)
|
||||
{
|
||||
throw new NotImplementedException("Emit not available on .NET Standard");
|
||||
}
|
||||
|
||||
internal void GetReturnTypesFromMethod(MethodInfo method, out Type[] returnTypes)
|
||||
internal static void GetReturnTypesFromMethod(MethodInfo method, out Type[] returnTypes)
|
||||
{
|
||||
var paramInfo = method.GetParameters();
|
||||
var paramTypes = new Type[paramInfo.Length];
|
||||
|
@ -82,43 +55,24 @@ namespace NLua
|
|||
|
||||
// Counts out and ref parameters, for later use,
|
||||
// and creates the list of return types
|
||||
int nOutParams = 0;
|
||||
int nOutAndRefParams = 0;
|
||||
var returnType = method.ReturnType;
|
||||
returnTypesList.Add(returnType);
|
||||
|
||||
for (int i = 0; i < paramTypes.Length; i++)
|
||||
for (var i = 0; i < paramTypes.Length; i++)
|
||||
{
|
||||
paramTypes[i] = paramInfo[i].ParameterType;
|
||||
|
||||
if (!paramInfo[i].IsIn && paramInfo[i].IsOut)
|
||||
{
|
||||
nOutParams++;
|
||||
}
|
||||
|
||||
if (paramTypes[i].IsByRef)
|
||||
{
|
||||
returnTypesList.Add(paramTypes[i].GetElementType());
|
||||
nOutAndRefParams++;
|
||||
}
|
||||
}
|
||||
|
||||
returnTypes = returnTypesList.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an event handler for the event type that delegates to the eventHandler Lua function.
|
||||
/// Caches the generated type.
|
||||
/// </summary>
|
||||
public LuaEventHandler GetEvent(Type eventHandlerType, LuaFunction eventHandler)
|
||||
{
|
||||
throw new NotImplementedException("Emit not available on .NET Standard");
|
||||
}
|
||||
|
||||
public void RegisterLuaDelegateType(Type delegateType, Type luaDelegateType)
|
||||
{
|
||||
_delegateCollection[delegateType] = luaDelegateType;
|
||||
}
|
||||
=> _delegateCollection[delegateType] = luaDelegateType;
|
||||
|
||||
public void RegisterLuaClassType(Type klass, Type luaClass)
|
||||
{
|
||||
|
@ -135,26 +89,14 @@ namespace NLua
|
|||
public Delegate GetDelegate(Type delegateType, LuaFunction luaFunc)
|
||||
{
|
||||
var returnTypes = new List<Type>();
|
||||
Type luaDelegateType;
|
||||
|
||||
if (_delegateCollection.ContainsKey(delegateType))
|
||||
{
|
||||
luaDelegateType = _delegateCollection[delegateType];
|
||||
}
|
||||
else
|
||||
{
|
||||
luaDelegateType = GenerateDelegate(delegateType);
|
||||
_delegateCollection[delegateType] = luaDelegateType;
|
||||
}
|
||||
var luaDelegateType = _delegateCollection[delegateType];
|
||||
|
||||
var methodInfo = delegateType.GetMethod("Invoke");
|
||||
returnTypes.Add(methodInfo.ReturnType);
|
||||
returnTypes.Add(methodInfo!.ReturnType);
|
||||
|
||||
foreach (ParameterInfo paramInfo in methodInfo.GetParameters())
|
||||
{
|
||||
if (paramInfo.ParameterType.IsByRef)
|
||||
returnTypes.Add(paramInfo.ParameterType);
|
||||
}
|
||||
returnTypes.AddRange(methodInfo.GetParameters()
|
||||
.Where(paramInfo => paramInfo.ParameterType.IsByRef)
|
||||
.Select(paramInfo => paramInfo.ParameterType));
|
||||
|
||||
var luaDelegate = (LuaDelegate)Activator.CreateInstance(luaDelegateType);
|
||||
luaDelegate.Function = luaFunc;
|
||||
|
@ -170,21 +112,8 @@ namespace NLua
|
|||
/// </summary>
|
||||
public object GetClassInstance(Type klass, LuaTable luaTable)
|
||||
{
|
||||
LuaClassType luaClassType;
|
||||
|
||||
if (_classCollection.ContainsKey(klass))
|
||||
luaClassType = _classCollection[klass];
|
||||
else
|
||||
{
|
||||
luaClassType = default;
|
||||
GenerateClass(klass, out luaClassType.klass, out luaClassType.returnTypes);
|
||||
_classCollection[klass] = luaClassType;
|
||||
}
|
||||
|
||||
return Activator.CreateInstance(luaClassType.klass, new object[] {
|
||||
luaTable,
|
||||
luaClassType.returnTypes
|
||||
});
|
||||
var luaClassType = _classCollection[klass];
|
||||
return Activator.CreateInstance(luaClassType.klass, luaTable, luaClassType.returnTypes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
using System;
|
||||
|
||||
namespace NLua
|
||||
using NLua.Native;
|
||||
|
||||
namespace NLua.GenerateEventAssembly
|
||||
{
|
||||
internal class DelegateGenerator
|
||||
{
|
||||
|
@ -14,8 +16,6 @@ namespace NLua
|
|||
}
|
||||
|
||||
public object ExtractGenerated(LuaState luaState, int stackPos)
|
||||
{
|
||||
return CodeGeneration.Instance.GetDelegate(_delegateType, _translator.GetFunction(luaState, stackPos));
|
||||
}
|
||||
=> CodeGeneration.Instance.GetDelegate(_delegateType, _translator.GetFunction(luaState, stackPos));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
namespace NLua
|
||||
namespace NLua.GenerateEventAssembly
|
||||
{
|
||||
/// <summary>
|
||||
/// Common interface for types generated from tables. The method
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
using System;
|
||||
|
||||
namespace NLua
|
||||
namespace NLua.GenerateEventAssembly
|
||||
{
|
||||
/// <summary>
|
||||
/// Structure to store a type and the return types of
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -43,21 +43,26 @@ namespace NLua
|
|||
internal void DisposeLuaReference(bool finalized)
|
||||
{
|
||||
if (_lua == null)
|
||||
{
|
||||
return;
|
||||
Lua lua;
|
||||
if (!TryGet(out lua))
|
||||
}
|
||||
|
||||
if (!TryGet(out var lua))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
lua.DisposeInternal(_Reference, finalized);
|
||||
}
|
||||
|
||||
public virtual void Dispose(bool disposeManagedResources)
|
||||
protected void Dispose(bool disposeManagedResources)
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
bool finalized = !disposeManagedResources;
|
||||
|
||||
var finalized = !disposeManagedResources;
|
||||
if (_Reference != 0)
|
||||
{
|
||||
DisposeLuaReference(finalized);
|
||||
|
@ -69,20 +74,15 @@ namespace NLua
|
|||
|
||||
public override bool Equals(object o)
|
||||
{
|
||||
var reference = o as LuaBase;
|
||||
if (reference == null)
|
||||
if (o is not LuaBase reference)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Lua lua;
|
||||
if (!TryGet(out lua))
|
||||
return false;
|
||||
|
||||
return lua.CompareRef(reference._Reference, _Reference);
|
||||
return TryGet(out var lua) && lua.CompareRef(reference._Reference, _Reference);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _Reference;
|
||||
}
|
||||
=> _Reference;
|
||||
}
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
using System;
|
||||
|
||||
using NLua.Native;
|
||||
|
||||
namespace NLua
|
||||
{
|
||||
public class LuaFunction : LuaBase
|
||||
|
@ -21,67 +23,59 @@ namespace NLua
|
|||
/// in returnTypes
|
||||
/// </summary>
|
||||
internal object[] Call(object[] args, Type[] returnTypes)
|
||||
{
|
||||
Lua lua;
|
||||
if (!TryGet(out lua))
|
||||
return null;
|
||||
|
||||
return lua.CallFunction(this, args, returnTypes);
|
||||
}
|
||||
=> !TryGet(out var lua) ? null : lua.CallFunction(this, args, returnTypes);
|
||||
|
||||
/// <summary>
|
||||
/// Calls the function and returns its return values inside
|
||||
/// an array
|
||||
/// </summary>
|
||||
public object[] Call(params object[] args)
|
||||
{
|
||||
Lua lua;
|
||||
if (!TryGet(out lua))
|
||||
return null;
|
||||
|
||||
return lua.CallFunction(this, args);
|
||||
}
|
||||
=> !TryGet(out var lua) ? null : lua.CallFunction(this, args);
|
||||
|
||||
/// <summary>
|
||||
/// Pushes the function into the Lua stack
|
||||
/// </summary>
|
||||
internal void Push(LuaState luaState)
|
||||
{
|
||||
Lua lua;
|
||||
if (!TryGet(out lua))
|
||||
if (!TryGet(out var lua))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_Reference != 0)
|
||||
{
|
||||
luaState.RawGetInteger(LuaRegistry.Index, _Reference);
|
||||
}
|
||||
else
|
||||
{
|
||||
lua.PushCSFunction(function);
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return "function";
|
||||
}
|
||||
=> "function";
|
||||
|
||||
public override bool Equals(object o)
|
||||
{
|
||||
var l = o as LuaFunction;
|
||||
|
||||
if (l == null)
|
||||
if (o is not LuaFunction l)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Lua lua;
|
||||
if (!TryGet(out lua))
|
||||
if (!TryGet(out var lua))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_Reference != 0 && l._Reference != 0)
|
||||
{
|
||||
return lua.CompareRef(l._Reference, _Reference);
|
||||
}
|
||||
|
||||
return function == l.function;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _Reference != 0 ? _Reference : function.GetHashCode();
|
||||
}
|
||||
=> _Reference != 0 ? _Reference : function.GetHashCode();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,8 +5,6 @@ namespace NLua
|
|||
/// <summary>
|
||||
/// Marks a method for global usage in Lua scripts
|
||||
/// </summary>
|
||||
/// <see cref="LuaRegistrationHelper.TaggedInstanceMethods"/>
|
||||
/// <see cref="LuaRegistrationHelper.TaggedStaticMethods"/>
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public sealed class LuaGlobalAttribute : Attribute
|
||||
{
|
||||
|
|
|
@ -10,15 +10,15 @@ namespace NLua
|
|||
/// <summary>
|
||||
/// Type at time of registration.
|
||||
/// </summary>
|
||||
public Type Type { get; private set; }
|
||||
public Type Type { get; }
|
||||
|
||||
public string Path { get; private set; }
|
||||
public string Path { get; }
|
||||
|
||||
/// <summary>
|
||||
/// List of global properties 'owned' by this entry.
|
||||
/// If this entry is removed all these globals should be removed as well.
|
||||
/// </summary>
|
||||
public List<string> linkedGlobals = new List<string>();
|
||||
public readonly List<string> linkedGlobals = new();
|
||||
|
||||
public LuaGlobalEntry(Type type, string path)
|
||||
{
|
||||
|
@ -29,10 +29,10 @@ namespace NLua
|
|||
|
||||
public class LuaGlobals
|
||||
{
|
||||
private List<string> _globals = new List<string>();
|
||||
private List<LuaGlobalEntry> _knownTypes = new List<LuaGlobalEntry>();
|
||||
private readonly List<string> _globals = new();
|
||||
private readonly List<LuaGlobalEntry> _knownTypes = new();
|
||||
|
||||
public bool _globalsSorted = false;
|
||||
public bool _globalsSorted;
|
||||
|
||||
public int MaximumRecursion { get; set; } = 2;
|
||||
|
||||
|
@ -52,9 +52,7 @@ namespace NLua
|
|||
}
|
||||
|
||||
public bool Contains(string fullPath)
|
||||
{
|
||||
return _globals.Contains(fullPath);
|
||||
}
|
||||
=> _globals.Contains(fullPath);
|
||||
|
||||
public void RemoveGlobal(string path)
|
||||
{
|
||||
|
@ -72,16 +70,14 @@ namespace NLua
|
|||
}
|
||||
|
||||
private LuaGlobalEntry GetKnownType(string path)
|
||||
{
|
||||
return _knownTypes.Find(x => x.Path.Equals(path));
|
||||
}
|
||||
=> _knownTypes.Find(x => x.Path.Equals(path, StringComparison.Ordinal));
|
||||
|
||||
public void RegisterGlobal(string path, Type type, int recursionCounter)
|
||||
{
|
||||
var knownType = GetKnownType(path);
|
||||
if (knownType != null)
|
||||
{
|
||||
if (type.Equals(knownType.Type))
|
||||
if (type == knownType.Type)
|
||||
{
|
||||
// Object is set to same value so no need to update
|
||||
return;
|
||||
|
@ -119,37 +115,30 @@ namespace NLua
|
|||
{
|
||||
// Format for easy method invocation
|
||||
_globals.Add(path + "(");
|
||||
|
||||
if (entry != null)
|
||||
{
|
||||
entry.linkedGlobals.Add(path);
|
||||
}
|
||||
entry?.linkedGlobals.Add(path);
|
||||
}
|
||||
|
||||
private void RegisterPrimitive(string path, LuaGlobalEntry entry = null)
|
||||
{
|
||||
_globals.Add(path);
|
||||
if (entry != null)
|
||||
{
|
||||
entry.linkedGlobals.Add(path);
|
||||
}
|
||||
entry?.linkedGlobals.Add(path);
|
||||
}
|
||||
|
||||
private void RegisterClassOrInterface(string path, Type type, int recursionCounter, LuaGlobalEntry entry = null)
|
||||
{
|
||||
if (entry == null)
|
||||
{
|
||||
entry = new LuaGlobalEntry(type, path);
|
||||
entry = new(type, path);
|
||||
_knownTypes.Add(entry);
|
||||
}
|
||||
|
||||
foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.Instance))
|
||||
{
|
||||
string name = method.Name;
|
||||
var name = method.Name;
|
||||
if (
|
||||
// Check that the LuaHideAttribute and LuaGlobalAttribute were not applied
|
||||
(!method.GetCustomAttributes(typeof(LuaHideAttribute), false).Any()) &&
|
||||
(!method.GetCustomAttributes(typeof(LuaGlobalAttribute), false).Any()) &&
|
||||
!method.GetCustomAttributes(typeof(LuaHideAttribute), false).Any() &&
|
||||
!method.GetCustomAttributes(typeof(LuaGlobalAttribute), false).Any() &&
|
||||
// Exclude some generic .NET methods that wouldn't be very usefull in Lua
|
||||
name != "GetType" && name != "GetHashCode" && name != "Equals" &&
|
||||
name != "ToString" && name != "Clone" && name != "Dispose" &&
|
||||
|
@ -160,7 +149,7 @@ namespace NLua
|
|||
!name.StartsWith("remove_", StringComparison.Ordinal))
|
||||
{
|
||||
// Format for easy method invocation
|
||||
string command = path + ":" + name + "(";
|
||||
var command = path + ":" + name + "(";
|
||||
|
||||
if (method.GetParameters().Length == 0)
|
||||
command += ")";
|
||||
|
@ -172,10 +161,9 @@ namespace NLua
|
|||
|
||||
foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.Instance))
|
||||
{
|
||||
if (
|
||||
// Check that the LuaHideAttribute and LuaGlobalAttribute were not applied
|
||||
(!field.GetCustomAttributes(typeof(LuaHideAttribute), false).Any()) &&
|
||||
(!field.GetCustomAttributes(typeof(LuaGlobalAttribute), false).Any()))
|
||||
// Check that the LuaHideAttribute and LuaGlobalAttribute were not applied
|
||||
if (!field.GetCustomAttributes(typeof(LuaHideAttribute), false).Any() &&
|
||||
!field.GetCustomAttributes(typeof(LuaGlobalAttribute), false).Any())
|
||||
{
|
||||
// Go into recursion for members
|
||||
RegisterPath(path + "." + field.Name, field.FieldType, recursionCounter + 1, entry);
|
||||
|
|
|
@ -1,89 +0,0 @@
|
|||
using System;
|
||||
using System.Reflection;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace NLua
|
||||
{
|
||||
public static class LuaRegistrationHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Registers all public instance methods in an object tagged with <see cref="LuaGlobalAttribute"/> as Lua global functions
|
||||
/// </summary>
|
||||
/// <param name="lua">The Lua VM to add the methods to</param>
|
||||
/// <param name="o">The object to get the methods from</param>
|
||||
public static void TaggedInstanceMethods(Lua lua, object o)
|
||||
{
|
||||
if (lua == null)
|
||||
throw new ArgumentNullException(nameof(lua));
|
||||
|
||||
if (o == null)
|
||||
throw new ArgumentNullException(nameof(o));
|
||||
|
||||
foreach (var method in o.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public))
|
||||
{
|
||||
foreach (LuaGlobalAttribute attribute in method.GetCustomAttributes(typeof(LuaGlobalAttribute), true))
|
||||
{
|
||||
if (string.IsNullOrEmpty(attribute.Name))
|
||||
lua.RegisterFunction(method.Name, o, method); // CLR name
|
||||
else
|
||||
lua.RegisterFunction(attribute.Name, o, method); // Custom name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers all public static methods in a class tagged with <see cref="LuaGlobalAttribute"/> as Lua global functions
|
||||
/// </summary>
|
||||
/// <param name="lua">The Lua VM to add the methods to</param>
|
||||
/// <param name="type">The class type to get the methods from</param>
|
||||
public static void TaggedStaticMethods(Lua lua, Type type)
|
||||
{
|
||||
if (lua == null)
|
||||
throw new ArgumentNullException(nameof(lua));
|
||||
|
||||
if (type == null)
|
||||
throw new ArgumentNullException(nameof(type));
|
||||
|
||||
if (!type.IsClass)
|
||||
throw new ArgumentException("The type must be a class!", nameof(type));
|
||||
|
||||
foreach (var method in type.GetMethods(BindingFlags.Static | BindingFlags.Public))
|
||||
{
|
||||
foreach (LuaGlobalAttribute attribute in method.GetCustomAttributes(typeof(LuaGlobalAttribute), false))
|
||||
{
|
||||
if (string.IsNullOrEmpty(attribute.Name))
|
||||
lua.RegisterFunction(method.Name, null, method); // CLR name
|
||||
else
|
||||
lua.RegisterFunction(attribute.Name, null, method); // Custom name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers an enumeration's values for usage as a Lua variable table
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The enum type to register</typeparam>
|
||||
/// <param name="lua">The Lua VM to add the enum to</param>
|
||||
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "The type parameter is used to select an enum type")]
|
||||
public static void Enumeration<T>(Lua lua)
|
||||
{
|
||||
if (lua == null)
|
||||
throw new ArgumentNullException(nameof(lua));
|
||||
|
||||
Type type = typeof(T);
|
||||
|
||||
if (!type.IsEnum)
|
||||
throw new InvalidOperationException($"The type must be an enumeration!");
|
||||
|
||||
string[] names = Enum.GetNames(type);
|
||||
var values = (T[])Enum.GetValues(type);
|
||||
lua.NewTable(type.Name);
|
||||
|
||||
for (int i = 0; i < names.Length; i++)
|
||||
{
|
||||
string path = type.Name + "." + names[i];
|
||||
lua.SetObjectToPath(path, values[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,6 +2,7 @@
|
|||
using System.Collections;
|
||||
|
||||
using NLua.Extensions;
|
||||
using NLua.Native;
|
||||
|
||||
namespace NLua
|
||||
{
|
||||
|
@ -14,19 +15,16 @@ namespace NLua
|
|||
/// <summary>
|
||||
/// Indexer for string fields of the table
|
||||
/// </summary>
|
||||
public object this[string field] {
|
||||
get
|
||||
{
|
||||
Lua lua;
|
||||
if (!TryGet(out lua))
|
||||
return null;
|
||||
return lua.GetObject(_Reference, field);
|
||||
}
|
||||
public object this[string field]
|
||||
{
|
||||
get => !TryGet(out var lua) ? null : lua.GetObject(_Reference, field);
|
||||
set
|
||||
{
|
||||
Lua lua;
|
||||
if (!TryGet(out lua))
|
||||
if (!TryGet(out var lua))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
lua.SetObject(_Reference, field, value);
|
||||
}
|
||||
}
|
||||
|
@ -34,20 +32,15 @@ namespace NLua
|
|||
/// <summary>
|
||||
/// Indexer for numeric fields of the table
|
||||
/// </summary>
|
||||
public object this[object field] {
|
||||
get
|
||||
{
|
||||
Lua lua;
|
||||
if (!TryGet(out lua))
|
||||
return null;
|
||||
|
||||
return lua.GetObject(_Reference, field);
|
||||
}
|
||||
public object this[object field]
|
||||
{
|
||||
get => !TryGet(out var lua) ? null : lua.GetObject(_Reference, field);
|
||||
set
|
||||
{
|
||||
Lua lua;
|
||||
if (!TryGet(out lua))
|
||||
if (!TryGet(out var lua))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
lua.SetObject(_Reference, field, value);
|
||||
}
|
||||
|
@ -55,32 +48,24 @@ namespace NLua
|
|||
|
||||
public IDictionaryEnumerator GetEnumerator()
|
||||
{
|
||||
Lua lua;
|
||||
if (!TryGet(out lua))
|
||||
if (!TryGet(out var lua))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return lua.GetTableDict(this).GetEnumerator();
|
||||
}
|
||||
|
||||
public ICollection Keys
|
||||
{
|
||||
get
|
||||
{
|
||||
Lua lua;
|
||||
if (!TryGet(out lua))
|
||||
return null;
|
||||
|
||||
return lua.GetTableDict(this).Keys;
|
||||
}
|
||||
}
|
||||
public ICollection Keys => !TryGet(out var lua) ? null : lua.GetTableDict(this).Keys;
|
||||
|
||||
public ICollection Values
|
||||
{
|
||||
get
|
||||
{
|
||||
Lua lua;
|
||||
if (!TryGet(out lua))
|
||||
if (!TryGet(out var lua))
|
||||
{
|
||||
return Array.Empty<object>();
|
||||
}
|
||||
|
||||
return lua.GetTableDict(this).Values;
|
||||
}
|
||||
|
@ -91,25 +76,15 @@ namespace NLua
|
|||
/// if it exists
|
||||
/// </summary>
|
||||
internal object RawGet(string field)
|
||||
{
|
||||
Lua lua;
|
||||
if (!TryGet(out lua))
|
||||
return null;
|
||||
|
||||
return lua.RawGetObject(_Reference, field);
|
||||
}
|
||||
=> !TryGet(out var lua) ? null : lua.RawGetObject(_Reference, field);
|
||||
|
||||
/// <summary>
|
||||
/// Pushes this table into the Lua stack
|
||||
/// </summary>
|
||||
internal void Push(LuaState luaState)
|
||||
{
|
||||
luaState.GetRef(_Reference);
|
||||
}
|
||||
=> luaState.GetRef(_Reference);
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return "table";
|
||||
}
|
||||
=> "table";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,15 +2,15 @@
|
|||
|
||||
using NLua.Exceptions;
|
||||
using NLua.Extensions;
|
||||
using NLua.Native;
|
||||
|
||||
namespace NLua
|
||||
{
|
||||
public class LuaThread : LuaBase, IEquatable<LuaThread>, IEquatable<LuaState>, IEquatable<Lua>
|
||||
{
|
||||
private LuaState _luaState;
|
||||
private ObjectTranslator _translator;
|
||||
private readonly ObjectTranslator _translator;
|
||||
|
||||
public LuaState State => _luaState;
|
||||
public LuaState State { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Get the main thread object
|
||||
|
@ -19,10 +19,10 @@ namespace NLua
|
|||
{
|
||||
get
|
||||
{
|
||||
LuaState mainThread = _luaState.MainThread;
|
||||
int oldTop = mainThread.GetTop();
|
||||
var mainThread = State.MainThread;
|
||||
var oldTop = mainThread.GetTop();
|
||||
mainThread.PushThread();
|
||||
object returnValue = _translator.GetObject(mainThread, -1);
|
||||
var returnValue = _translator.GetObject(mainThread, -1);
|
||||
|
||||
mainThread.SetTop(oldTop);
|
||||
return (LuaThread)returnValue;
|
||||
|
@ -31,7 +31,7 @@ namespace NLua
|
|||
|
||||
public LuaThread(int reference, Lua interpreter): base(reference, interpreter)
|
||||
{
|
||||
_luaState = interpreter.GetThreadState(reference);
|
||||
State = interpreter.GetThreadState(reference);
|
||||
_translator = interpreter.Translator;
|
||||
}
|
||||
|
||||
|
@ -41,20 +41,19 @@ namespace NLua
|
|||
public LuaStatus Resume()
|
||||
{
|
||||
// We leave nothing on the stack if we error
|
||||
var oldMainTop = _luaState.MainThread.GetTop();
|
||||
var oldCoTop = _luaState.GetTop();
|
||||
var oldMainTop = State.MainThread.GetTop();
|
||||
var oldCoTop = State.GetTop();
|
||||
|
||||
LuaStatus ret = _luaState.Resume(null, 0);
|
||||
|
||||
if (ret == LuaStatus.OK || ret == LuaStatus.Yield)
|
||||
var ret = State.Resume(null, 0);
|
||||
if (ret is LuaStatus.OK or LuaStatus.Yield)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
object coErr = _translator.GetObject(_luaState, -1);
|
||||
object mainErr = _translator.GetObject(_luaState.MainThread, -1);
|
||||
_luaState.SetTop(oldCoTop);
|
||||
_luaState.MainThread.SetTop(oldMainTop);
|
||||
var coErr = _translator.GetObject(State, -1);
|
||||
var mainErr = _translator.GetObject(State.MainThread, -1);
|
||||
State.SetTop(oldCoTop);
|
||||
State.MainThread.SetTop(oldMainTop);
|
||||
|
||||
if (coErr is LuaScriptException coLuaEx)
|
||||
{
|
||||
|
@ -78,87 +77,76 @@ namespace NLua
|
|||
/// Yields this thread
|
||||
/// </summary>
|
||||
public void Yield()
|
||||
{
|
||||
_luaState.Yield(0);
|
||||
}
|
||||
=> State.Yield(0);
|
||||
|
||||
public void XMove(LuaState to, object val, int index = 1)
|
||||
{
|
||||
int oldTop = _luaState.GetTop();
|
||||
var oldTop = State.GetTop();
|
||||
|
||||
_translator.Push(_luaState, val);
|
||||
_luaState.XMove(to, index);
|
||||
_translator.Push(State, val);
|
||||
State.XMove(to, index);
|
||||
|
||||
_luaState.SetTop(oldTop);
|
||||
State.SetTop(oldTop);
|
||||
}
|
||||
|
||||
public void XMove(Lua to, object val, int index = 1)
|
||||
{
|
||||
int oldTop = _luaState.GetTop();
|
||||
var oldTop = State.GetTop();
|
||||
|
||||
_translator.Push(_luaState, val);
|
||||
_luaState.XMove(to.State, index);
|
||||
_translator.Push(State, val);
|
||||
State.XMove(to.State, index);
|
||||
|
||||
_luaState.SetTop(oldTop);
|
||||
State.SetTop(oldTop);
|
||||
}
|
||||
|
||||
public void XMove(LuaThread thread, object val, int index = 1)
|
||||
{
|
||||
int oldTop = _luaState.GetTop();
|
||||
var oldTop = State.GetTop();
|
||||
|
||||
_translator.Push(_luaState, val);
|
||||
_luaState.XMove(thread.State, index);
|
||||
_translator.Push(State, val);
|
||||
State.XMove(thread.State, index);
|
||||
|
||||
_luaState.SetTop(oldTop);
|
||||
State.SetTop(oldTop);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pushes this thread into the Lua stack
|
||||
/// </summary>
|
||||
internal void Push(LuaState luaState)
|
||||
{
|
||||
luaState.GetRef(_Reference);
|
||||
}
|
||||
=> luaState.GetRef(_Reference);
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return "thread";
|
||||
}
|
||||
=> "thread";
|
||||
|
||||
public override bool Equals(object obj)
|
||||
public override bool Equals(object obj) => obj switch
|
||||
{
|
||||
if (obj is LuaThread thread)
|
||||
return this.State == thread.State;
|
||||
else if (obj is Lua interpreter)
|
||||
return this.State == interpreter.State;
|
||||
else if (obj is LuaState state)
|
||||
return this.State == state;
|
||||
return base.Equals(obj);
|
||||
}
|
||||
LuaThread thread => State == thread.State,
|
||||
Lua interpreter => State == interpreter.State,
|
||||
LuaState state => State == state,
|
||||
_ => base.Equals(obj)
|
||||
};
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return base.GetHashCode();
|
||||
}
|
||||
=> base.GetHashCode();
|
||||
|
||||
public bool Equals(LuaThread other) => this.State == other.State;
|
||||
public bool Equals(LuaState other) => this.State == other;
|
||||
public bool Equals(Lua other) => this.State == other.State;
|
||||
public bool Equals(LuaThread other) => State == other?.State;
|
||||
public bool Equals(LuaState other) => State == other;
|
||||
public bool Equals(Lua other) => State == other?.State;
|
||||
|
||||
public static explicit operator LuaState(LuaThread thread) => thread.State;
|
||||
public static explicit operator LuaThread(Lua interpreter) => interpreter.Thread;
|
||||
|
||||
public static bool operator ==(LuaThread threadA, LuaThread threadB) => threadA.State == threadB.State;
|
||||
public static bool operator !=(LuaThread threadA, LuaThread threadB) => threadA.State != threadB.State;
|
||||
public static bool operator ==(LuaThread threadA, LuaThread threadB) => threadA?.State == threadB?.State;
|
||||
public static bool operator !=(LuaThread threadA, LuaThread threadB) => threadA?.State != threadB?.State;
|
||||
|
||||
public static bool operator ==(LuaThread thread, LuaState state) => thread.State == state;
|
||||
public static bool operator !=(LuaThread thread, LuaState state) => thread.State != state;
|
||||
public static bool operator ==(LuaState state, LuaThread thread) => state == thread.State;
|
||||
public static bool operator !=(LuaState state, LuaThread thread) => state != thread.State;
|
||||
public static bool operator ==(LuaThread thread, LuaState state) => thread?.State == state;
|
||||
public static bool operator !=(LuaThread thread, LuaState state) => thread?.State != state;
|
||||
public static bool operator ==(LuaState state, LuaThread thread) => state == thread?.State;
|
||||
public static bool operator !=(LuaState state, LuaThread thread) => state != thread?.State;
|
||||
|
||||
public static bool operator ==(LuaThread thread, Lua interpreter) => thread.State == interpreter.State;
|
||||
public static bool operator !=(LuaThread thread, Lua interpreter) => thread.State != interpreter.State;
|
||||
public static bool operator ==(Lua interpreter, LuaThread thread) => interpreter.State == thread.State;
|
||||
public static bool operator !=(Lua interpreter, LuaThread thread) => interpreter.State != thread.State;
|
||||
public static bool operator ==(LuaThread thread, Lua interpreter) => thread?.State == interpreter?.State;
|
||||
public static bool operator !=(LuaThread thread, Lua interpreter) => thread?.State != interpreter?.State;
|
||||
public static bool operator ==(Lua interpreter, LuaThread thread) => interpreter?.State == thread?.State;
|
||||
public static bool operator !=(Lua interpreter, LuaThread thread) => interpreter?.State != thread?.State;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,30 +1,27 @@
|
|||
using NLua.Extensions;
|
||||
using NLua.Native;
|
||||
|
||||
namespace NLua
|
||||
{
|
||||
public class LuaUserData : LuaBase
|
||||
{
|
||||
public LuaUserData(int reference, Lua interpreter):base(reference, interpreter)
|
||||
public LuaUserData(int reference, Lua interpreter)
|
||||
: base(reference, interpreter)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indexer for string fields of the userdata
|
||||
/// </summary>
|
||||
public object this[string field] {
|
||||
get
|
||||
{
|
||||
Lua lua;
|
||||
if (!TryGet(out lua))
|
||||
return null;
|
||||
|
||||
return lua.GetObject(_Reference, field);
|
||||
}
|
||||
public object this[string field]
|
||||
{
|
||||
get => !TryGet(out var lua) ? null : lua.GetObject(_Reference, field);
|
||||
set
|
||||
{
|
||||
Lua lua;
|
||||
if (!TryGet(out lua))
|
||||
if (!TryGet(out var lua))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
lua.SetObject(_Reference, field, value);
|
||||
}
|
||||
|
@ -33,19 +30,12 @@ namespace NLua
|
|||
/// <summary>
|
||||
/// Indexer for numeric fields of the userdata
|
||||
/// </summary>
|
||||
public object this[object field] {
|
||||
get
|
||||
{
|
||||
Lua lua;
|
||||
if (!TryGet(out lua))
|
||||
return null;
|
||||
|
||||
return lua.GetObject(_Reference, field);
|
||||
}
|
||||
public object this[object field]
|
||||
{
|
||||
get => !TryGet(out var lua) ? null : lua.GetObject(_Reference, field);
|
||||
set
|
||||
{
|
||||
Lua lua;
|
||||
if (!TryGet(out lua))
|
||||
if (!TryGet(out var lua))
|
||||
return;
|
||||
|
||||
lua.SetObject(_Reference, field, value);
|
||||
|
@ -57,25 +47,15 @@ namespace NLua
|
|||
/// an array
|
||||
/// </summary>
|
||||
public object[] Call(params object[] args)
|
||||
{
|
||||
Lua lua;
|
||||
if (!TryGet(out lua))
|
||||
return null;
|
||||
|
||||
return lua.CallFunction(this, args);
|
||||
}
|
||||
=> !TryGet(out var lua) ? null : lua.CallFunction(this, args);
|
||||
|
||||
/// <summary>
|
||||
/// Pushes this userdata into the Lua stack
|
||||
/// </summary>
|
||||
internal void Push(LuaState luaState)
|
||||
{
|
||||
luaState.GetRef(_Reference);
|
||||
}
|
||||
=> luaState.GetRef(_Reference);
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return "userdata";
|
||||
}
|
||||
=> "userdata";
|
||||
}
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -9,16 +9,14 @@ namespace NLua.Method
|
|||
/// </summary>
|
||||
internal class EventHandlerContainer : IDisposable
|
||||
{
|
||||
private readonly Dictionary<Delegate, RegisterEventHandler> _dict = new Dictionary<Delegate, RegisterEventHandler>();
|
||||
private readonly Dictionary<Delegate, RegisterEventHandler> _dict = new();
|
||||
|
||||
public void Add(Delegate handler, RegisterEventHandler eventInfo)
|
||||
{
|
||||
_dict.Add(handler, eventInfo);
|
||||
}
|
||||
=> _dict.Add(handler, eventInfo);
|
||||
|
||||
public void Remove(Delegate handler)
|
||||
{
|
||||
bool found = _dict.Remove(handler);
|
||||
var found = _dict.Remove(handler);
|
||||
Debug.Assert(found);
|
||||
}
|
||||
|
||||
|
@ -27,8 +25,10 @@ namespace NLua.Method
|
|||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (KeyValuePair<Delegate, RegisterEventHandler> pair in _dict)
|
||||
foreach (var pair in _dict)
|
||||
{
|
||||
pair.Value.RemovePending(pair.Key);
|
||||
}
|
||||
|
||||
_dict.Clear();
|
||||
}
|
||||
|
|
|
@ -1,58 +0,0 @@
|
|||
using System;
|
||||
|
||||
namespace NLua.Method
|
||||
{
|
||||
public class LuaClassHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the function called name from the provided table,
|
||||
/// returning null if it does not exist
|
||||
/// </summary>
|
||||
public static LuaFunction GetTableFunction(LuaTable luaTable, string name)
|
||||
{
|
||||
if (luaTable == null)
|
||||
return null;
|
||||
|
||||
var funcObj = luaTable.RawGet(name) as LuaFunction;
|
||||
|
||||
if (funcObj != null)
|
||||
return funcObj;
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calls the provided function with the provided parameters
|
||||
/// </summary>
|
||||
public static object CallFunction(LuaFunction function, object[] args, Type[] returnTypes, object[] inArgs, int[] outArgs)
|
||||
{
|
||||
// args is the return array of arguments, inArgs is the actual array
|
||||
// of arguments passed to the function (with in parameters only), outArgs
|
||||
// has the positions of out parameters
|
||||
object returnValue;
|
||||
int iRefArgs;
|
||||
object[] returnValues = function.Call(inArgs, returnTypes);
|
||||
|
||||
if (returnValues == null || returnTypes.Length == 0)
|
||||
return null;
|
||||
|
||||
if (returnTypes[0] == typeof(void))
|
||||
{
|
||||
returnValue = null;
|
||||
iRefArgs = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
returnValue = returnValues[0];
|
||||
iRefArgs = 1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < outArgs.Length; i++)
|
||||
{
|
||||
args[outArgs[i]] = returnValues[iRefArgs];
|
||||
iRefArgs++;
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,17 +2,14 @@ using System;
|
|||
|
||||
namespace NLua.Method
|
||||
{
|
||||
// ReSharper disable once ClassNeverInstantiated.Global
|
||||
public class LuaDelegate
|
||||
{
|
||||
public LuaFunction Function;
|
||||
public Type[] ReturnTypes;
|
||||
|
||||
public LuaDelegate()
|
||||
{
|
||||
Function = null;
|
||||
ReturnTypes = null;
|
||||
}
|
||||
|
||||
// ReSharper disable once UnusedMember.Global
|
||||
// ReSharper disable once ParameterTypeCanBeEnumerable.Global
|
||||
public object CallFunction(object[] args, object[] inArgs, int[] outArgs)
|
||||
{
|
||||
// args is the return array of arguments, inArgs is the actual array
|
||||
|
@ -20,7 +17,7 @@ namespace NLua.Method
|
|||
// has the positions of out parameters
|
||||
object returnValue;
|
||||
int iRefArgs;
|
||||
object[] returnValues = Function.Call(inArgs, ReturnTypes);
|
||||
var returnValues = Function.Call(inArgs, ReturnTypes);
|
||||
|
||||
if (ReturnTypes[0] == typeof(void))
|
||||
{
|
||||
|
@ -35,9 +32,9 @@ namespace NLua.Method
|
|||
|
||||
// Sets the value of out and ref parameters (from
|
||||
// the values returned by the Lua function).
|
||||
for (int i = 0; i < outArgs.Length; i++)
|
||||
foreach (var t in outArgs)
|
||||
{
|
||||
args[outArgs[i]] = returnValues[iRefArgs];
|
||||
args[t] = returnValues[iRefArgs];
|
||||
iRefArgs++;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,12 +0,0 @@
|
|||
namespace NLua.Method
|
||||
{
|
||||
public class LuaEventHandler
|
||||
{
|
||||
public LuaFunction Handler = null;
|
||||
|
||||
public void HandleEvent(object[] args)
|
||||
{
|
||||
Handler.Call(args);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -5,6 +5,7 @@ using System.Linq;
|
|||
|
||||
using NLua.Exceptions;
|
||||
using NLua.Extensions;
|
||||
using NLua.Native;
|
||||
|
||||
namespace NLua.Method
|
||||
{
|
||||
|
@ -18,7 +19,7 @@ namespace NLua.Method
|
|||
/// </summary>
|
||||
internal class LuaMethodWrapper
|
||||
{
|
||||
internal LuaNativeFunction InvokeFunction;
|
||||
internal readonly LuaNativeFunction InvokeFunction;
|
||||
|
||||
internal readonly ObjectTranslator _translator;
|
||||
internal readonly MethodBase _method;
|
||||
|
@ -30,7 +31,7 @@ namespace NLua.Method
|
|||
internal readonly string _methodName;
|
||||
internal readonly MethodInfo[] _members;
|
||||
|
||||
private MethodCache _lastCalledMethod;
|
||||
private readonly MethodCache _lastCalledMethod;
|
||||
|
||||
/// <summary>
|
||||
/// Constructs the wrapper for a known MethodBase instance
|
||||
|
@ -45,12 +46,11 @@ namespace NLua.Method
|
|||
_translator = translator;
|
||||
_target = target;
|
||||
_extractTarget = translator.typeChecker.GetExtractor(targetType);
|
||||
_lastCalledMethod = new MethodCache();
|
||||
_lastCalledMethod = new();
|
||||
|
||||
_method = method;
|
||||
_methodName = method.Name;
|
||||
_isStatic = method.IsStatic;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -63,36 +63,35 @@ namespace NLua.Method
|
|||
_translator = translator;
|
||||
_methodName = methodName;
|
||||
_extractTarget = translator.typeChecker.GetExtractor(targetType);
|
||||
_lastCalledMethod = new MethodCache();
|
||||
_lastCalledMethod = new();
|
||||
|
||||
_isStatic = (bindingType & BindingFlags.Static) == BindingFlags.Static;
|
||||
MethodInfo [] methods = GetMethodsRecursively(targetType.UnderlyingSystemType,
|
||||
methodName,
|
||||
bindingType | BindingFlags.Public);
|
||||
var methods = GetMethodsRecursively(targetType.UnderlyingSystemType, methodName, bindingType | BindingFlags.Public);
|
||||
_members = ReorderMethods(methods);
|
||||
}
|
||||
|
||||
private static MethodInfo[] ReorderMethods(MethodInfo[] m)
|
||||
{
|
||||
int len = m.Length;
|
||||
|
||||
if (len < 2)
|
||||
if (m.Length < 2)
|
||||
{
|
||||
return m;
|
||||
}
|
||||
|
||||
return m.
|
||||
GroupBy(c => c.GetParameters().Length).
|
||||
SelectMany(g => g.OrderByDescending(ci => ci.ToString())).
|
||||
ToArray();
|
||||
return m
|
||||
.GroupBy(c => c.GetParameters().Length)
|
||||
.SelectMany(g => g.OrderByDescending(ci => ci.ToString()))
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
internal MethodInfo[] GetMethodsRecursively(Type type, string methodName, BindingFlags bindingType)
|
||||
internal static MethodInfo[] GetMethodsRecursively(Type type, string methodName, BindingFlags bindingType)
|
||||
{
|
||||
if (type == typeof(object))
|
||||
{
|
||||
return type.GetMethods(methodName, bindingType);
|
||||
}
|
||||
|
||||
var methods = type.GetMethods(methodName, bindingType);
|
||||
var baseMethods = GetMethodsRecursively(type.BaseType, methodName, bindingType);
|
||||
|
||||
return methods.Concat(baseMethods).ToArray();
|
||||
}
|
||||
|
||||
|
@ -102,26 +101,20 @@ namespace NLua.Method
|
|||
/// <returns>num of things on stack</returns>
|
||||
/// <param name="e">null for no pending exception</param>
|
||||
internal int SetPendingException(Exception e)
|
||||
{
|
||||
return _translator.interpreter.SetPendingException(e);
|
||||
}
|
||||
=> _translator.interpreter.SetPendingException(e);
|
||||
|
||||
internal void FillMethodArguments(LuaState luaState, int numStackToSkip)
|
||||
{
|
||||
object[] args = _lastCalledMethod.args;
|
||||
var args = _lastCalledMethod.args;
|
||||
|
||||
|
||||
for (int i = 0; i < _lastCalledMethod.argTypes.Length; i++)
|
||||
for (var i = 0; i < _lastCalledMethod.argTypes.Length; i++)
|
||||
{
|
||||
MethodArgs type = _lastCalledMethod.argTypes[i];
|
||||
|
||||
int index = i + 1 + numStackToSkip;
|
||||
|
||||
|
||||
var type = _lastCalledMethod.argTypes[i];
|
||||
var index = i + 1 + numStackToSkip;
|
||||
if (_lastCalledMethod.argTypes[i].IsParamsArray)
|
||||
{
|
||||
int count = _lastCalledMethod.argTypes.Length - i;
|
||||
Array paramArray = _translator.TableToArray(luaState, type.ExtractValue, type.ParameterType, index, count);
|
||||
var count = _lastCalledMethod.argTypes.Length - i;
|
||||
var paramArray = _translator.TableToArray(luaState, type.ExtractValue, type.ParameterType, index, count);
|
||||
args[_lastCalledMethod.argTypes[i].Index] = paramArray;
|
||||
}
|
||||
else
|
||||
|
@ -130,26 +123,30 @@ namespace NLua.Method
|
|||
}
|
||||
|
||||
if (_lastCalledMethod.args[_lastCalledMethod.argTypes[i].Index] == null &&
|
||||
!luaState.IsNil(i + 1 + numStackToSkip))
|
||||
throw new LuaException(string.Format("Argument number {0} is invalid", (i + 1)));
|
||||
!luaState.IsNil(i + 1 + numStackToSkip))
|
||||
{
|
||||
throw new LuaException($"Argument number {(i + 1)} is invalid");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal int PushReturnValue(LuaState luaState)
|
||||
{
|
||||
int nReturnValues = 0;
|
||||
var nReturnValues = 0;
|
||||
// Pushes out and ref return values
|
||||
for (int index = 0; index < _lastCalledMethod.outList.Length; index++)
|
||||
foreach (var t in _lastCalledMethod.outList)
|
||||
{
|
||||
nReturnValues++;
|
||||
_translator.Push(luaState, _lastCalledMethod.args[_lastCalledMethod.outList[index]]);
|
||||
_translator.Push(luaState, _lastCalledMethod.args[t]);
|
||||
}
|
||||
|
||||
// If not return void,we need add 1,
|
||||
// or we will lost the function's return value
|
||||
// when call dotnet function like "int foo(arg1,out arg2,out arg3)" in Lua code
|
||||
if (!_lastCalledMethod.IsReturnVoid && nReturnValues > 0)
|
||||
{
|
||||
nReturnValues++;
|
||||
}
|
||||
|
||||
return nReturnValues < 1 ? 1 : nReturnValues;
|
||||
}
|
||||
|
@ -157,16 +154,15 @@ namespace NLua.Method
|
|||
internal int CallInvoke(LuaState luaState, MethodBase method, object targetObject)
|
||||
{
|
||||
if (!luaState.CheckStack(_lastCalledMethod.outList.Length + 6))
|
||||
{
|
||||
throw new LuaException("Lua stack overflow");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
object result;
|
||||
|
||||
if (method.IsConstructor)
|
||||
result = ((ConstructorInfo)method).Invoke(_lastCalledMethod.args);
|
||||
else
|
||||
result = method.Invoke(targetObject, _lastCalledMethod.args);
|
||||
var result = method.IsConstructor
|
||||
? ((ConstructorInfo)method).Invoke(_lastCalledMethod.args)
|
||||
: method.Invoke(targetObject, _lastCalledMethod.args);
|
||||
|
||||
_translator.Push(luaState, result);
|
||||
}
|
||||
|
@ -187,17 +183,23 @@ namespace NLua.Method
|
|||
|
||||
internal bool IsMethodCached(LuaState luaState, int numArgsPassed, int skipParams)
|
||||
{
|
||||
if (_lastCalledMethod.cachedMethod == null)
|
||||
if (_lastCalledMethod.CachedMethod == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (numArgsPassed != _lastCalledMethod.argTypes.Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// If there is no method overloads, is ok to use the cached method
|
||||
if (_members == null || _members.Length == 1)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return _translator.MatchParameters(luaState, _lastCalledMethod.cachedMethod, _lastCalledMethod, skipParams);
|
||||
return _translator.MatchParameters(luaState, _lastCalledMethod.CachedMethod, _lastCalledMethod, skipParams);
|
||||
}
|
||||
|
||||
internal int CallMethodFromName(LuaState luaState)
|
||||
|
@ -205,24 +207,24 @@ namespace NLua.Method
|
|||
object targetObject = null;
|
||||
|
||||
if (!_isStatic)
|
||||
{
|
||||
targetObject = _extractTarget(luaState, 1);
|
||||
}
|
||||
|
||||
int numStackToSkip =
|
||||
_isStatic
|
||||
? 0
|
||||
: 1; // If this is an instance invoe we will have an extra arg on the stack for the targetObject
|
||||
int numArgsPassed = luaState.GetTop() - numStackToSkip;
|
||||
var numStackToSkip = _isStatic ? 0 : 1; // If this is an instance invoke we will have an extra arg on the stack for the targetObject
|
||||
var numArgsPassed = luaState.GetTop() - numStackToSkip;
|
||||
|
||||
// Cached?
|
||||
if (IsMethodCached(luaState, numArgsPassed, numStackToSkip))
|
||||
{
|
||||
MethodBase method = _lastCalledMethod.cachedMethod;
|
||||
var method = _lastCalledMethod.CachedMethod;
|
||||
|
||||
if (!luaState.CheckStack(_lastCalledMethod.outList.Length + 6))
|
||||
{
|
||||
throw new LuaException("Lua stack overflow");
|
||||
}
|
||||
|
||||
FillMethodArguments(luaState, numStackToSkip);
|
||||
|
||||
return CallInvoke(luaState, method, targetObject);
|
||||
}
|
||||
|
||||
|
@ -231,24 +233,25 @@ namespace NLua.Method
|
|||
{
|
||||
if (targetObject == null)
|
||||
{
|
||||
_translator.ThrowError(luaState,
|
||||
string.Format("instance method '{0}' requires a non null target object", _methodName));
|
||||
_translator.ThrowError(luaState, $"instance method '{_methodName}' requires a non null target object");
|
||||
return 1;
|
||||
}
|
||||
|
||||
luaState.Remove(1); // Pops the receiver
|
||||
}
|
||||
|
||||
bool hasMatch = false;
|
||||
var hasMatch = false;
|
||||
string candidateName = null;
|
||||
|
||||
foreach (var member in _members)
|
||||
{
|
||||
if (member.ReflectedType == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
candidateName = member.ReflectedType.Name + "." + member.Name;
|
||||
bool isMethod = _translator.MatchParameters(luaState, member, _lastCalledMethod, 0);
|
||||
var isMethod = _translator.MatchParameters(luaState, member, _lastCalledMethod, 0);
|
||||
|
||||
if (isMethod)
|
||||
{
|
||||
|
@ -259,40 +262,37 @@ namespace NLua.Method
|
|||
|
||||
if (!hasMatch)
|
||||
{
|
||||
string msg = (candidateName == null)
|
||||
var msg = candidateName == null
|
||||
? "Invalid arguments to method call"
|
||||
: ("Invalid arguments to method: " + candidateName);
|
||||
: "Invalid arguments to method: " + candidateName;
|
||||
_translator.ThrowError(luaState, msg);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (_lastCalledMethod.cachedMethod.ContainsGenericParameters)
|
||||
return CallInvokeOnGenericMethod(luaState, (MethodInfo)_lastCalledMethod.cachedMethod, targetObject);
|
||||
|
||||
return CallInvoke(luaState, _lastCalledMethod.cachedMethod, targetObject);
|
||||
return _lastCalledMethod.CachedMethod.ContainsGenericParameters
|
||||
? CallInvokeOnGenericMethod(luaState, (MethodInfo)_lastCalledMethod.CachedMethod, targetObject)
|
||||
: CallInvoke(luaState, _lastCalledMethod.CachedMethod, targetObject);
|
||||
}
|
||||
|
||||
internal int CallInvokeOnGenericMethod(LuaState luaState, MethodInfo methodToCall, object targetObject)
|
||||
{
|
||||
//need to make a concrete type of the generic method definition
|
||||
// Need to make a concrete type of the generic method definition
|
||||
var typeArgs = new List<Type>();
|
||||
|
||||
ParameterInfo [] parameters = methodToCall.GetParameters();
|
||||
|
||||
for (int i = 0; i < parameters.Length; i++)
|
||||
var parameters = methodToCall.GetParameters();
|
||||
for (var i = 0; i < parameters.Length; i++)
|
||||
{
|
||||
ParameterInfo parameter = parameters[i];
|
||||
var parameter = parameters[i];
|
||||
|
||||
if (!parameter.ParameterType.IsGenericParameter)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
typeArgs.Add(_lastCalledMethod.args[i].GetType());
|
||||
}
|
||||
|
||||
MethodInfo concreteMethod = methodToCall.MakeGenericMethod(typeArgs.ToArray());
|
||||
|
||||
var concreteMethod = methodToCall.MakeGenericMethod(typeArgs.ToArray());
|
||||
_translator.Push(luaState, concreteMethod.Invoke(targetObject, _lastCalledMethod.args));
|
||||
|
||||
return PushReturnValue(luaState);
|
||||
}
|
||||
|
||||
|
@ -303,23 +303,25 @@ namespace NLua.Method
|
|||
internal int Call(IntPtr state)
|
||||
{
|
||||
var luaState = LuaState.FromIntPtr(state);
|
||||
var targetObject = _target;
|
||||
|
||||
MethodBase methodToCall = _method;
|
||||
object targetObject = _target;
|
||||
|
||||
if (!luaState.CheckStack(5))
|
||||
{
|
||||
throw new LuaException("Lua stack overflow");
|
||||
}
|
||||
|
||||
SetPendingException(null);
|
||||
|
||||
// Method from name
|
||||
if (methodToCall == null)
|
||||
if (_method == null)
|
||||
{
|
||||
return CallMethodFromName(luaState);
|
||||
}
|
||||
|
||||
// Method from MethodBase instance
|
||||
if (!methodToCall.ContainsGenericParameters)
|
||||
if (!_method.ContainsGenericParameters)
|
||||
{
|
||||
if (!methodToCall.IsStatic && !methodToCall.IsConstructor && targetObject == null)
|
||||
if (!_method.IsStatic && !_method.IsConstructor && targetObject == null)
|
||||
{
|
||||
targetObject = _extractTarget(luaState, 1);
|
||||
luaState.Remove(1); // Pops the receiver
|
||||
|
@ -329,11 +331,13 @@ namespace NLua.Method
|
|||
if (IsMethodCached(luaState, luaState.GetTop(), 0))
|
||||
{
|
||||
if (!luaState.CheckStack(_lastCalledMethod.outList.Length + 6))
|
||||
{
|
||||
throw new LuaException("Lua stack overflow");
|
||||
}
|
||||
|
||||
FillMethodArguments(luaState, 0);
|
||||
}
|
||||
else if (!_translator.MatchParameters(luaState, methodToCall, _lastCalledMethod, 0))
|
||||
else if (!_translator.MatchParameters(luaState, _method, _lastCalledMethod, 0))
|
||||
{
|
||||
_translator.ThrowError(luaState, "Invalid arguments to method call");
|
||||
return 1;
|
||||
|
@ -341,22 +345,23 @@ namespace NLua.Method
|
|||
}
|
||||
else
|
||||
{
|
||||
if (!methodToCall.IsGenericMethodDefinition)
|
||||
if (!_method.IsGenericMethodDefinition)
|
||||
{
|
||||
_translator.ThrowError(luaState,
|
||||
"Unable to invoke method on generic class as the current method is an open generic method");
|
||||
_translator.ThrowError(luaState, "Unable to invoke method on generic class as the current method is an open generic method");
|
||||
return 1;
|
||||
}
|
||||
|
||||
_translator.MatchParameters(luaState, methodToCall, _lastCalledMethod, 0);
|
||||
_translator.MatchParameters(luaState, _method, _lastCalledMethod, 0);
|
||||
|
||||
return CallInvokeOnGenericMethod(luaState, (MethodInfo) methodToCall, targetObject);
|
||||
return CallInvokeOnGenericMethod(luaState, (MethodInfo) _method, targetObject);
|
||||
}
|
||||
|
||||
if (_isStatic)
|
||||
{
|
||||
targetObject = null;
|
||||
}
|
||||
|
||||
return CallInvoke(luaState, _lastCalledMethod.cachedMethod, targetObject);
|
||||
return CallInvoke(luaState, _lastCalledMethod.CachedMethod, targetObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,19 +5,11 @@ namespace NLua.Method
|
|||
{
|
||||
internal class MethodCache
|
||||
{
|
||||
public MethodCache()
|
||||
{
|
||||
args = Array.Empty<object>();
|
||||
argTypes = Array.Empty<MethodArgs>();
|
||||
outList = Array.Empty<int>();
|
||||
}
|
||||
private MethodBase _cachedMethod;
|
||||
|
||||
public MethodBase cachedMethod {
|
||||
get
|
||||
{
|
||||
return _cachedMethod;
|
||||
}
|
||||
public MethodBase CachedMethod
|
||||
{
|
||||
get => _cachedMethod;
|
||||
set
|
||||
{
|
||||
_cachedMethod = value;
|
||||
|
@ -31,11 +23,8 @@ namespace NLua.Method
|
|||
}
|
||||
|
||||
public bool IsReturnVoid;
|
||||
// List or arguments
|
||||
public object[] args;
|
||||
// Positions of out parameters
|
||||
public int[] outList;
|
||||
// Types of parameters
|
||||
public MethodArgs[] argTypes;
|
||||
public object[] args = Array.Empty<object>(); // List or arguments
|
||||
public int[] outList = Array.Empty<int>(); // Positions of out parameters
|
||||
public MethodArgs[] argTypes = Array.Empty<MethodArgs>(); // Types of parameters
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
using NLua.GenerateEventAssembly;
|
||||
|
||||
namespace NLua.Method
|
||||
{
|
||||
internal class RegisterEventHandler
|
||||
|
@ -17,11 +19,12 @@ namespace NLua.Method
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a new event handler
|
||||
/// Adds a new event handler, called from Lua
|
||||
/// </summary>
|
||||
// ReSharper disable once UnusedMember.Global
|
||||
public Delegate Add(LuaFunction function)
|
||||
{
|
||||
Delegate handlerDelegate = CodeGeneration.Instance.GetDelegate(_eventInfo.EventHandlerType, function);
|
||||
var handlerDelegate = CodeGeneration.Instance.GetDelegate(_eventInfo.EventHandlerType, function);
|
||||
return Add(handlerDelegate);
|
||||
}
|
||||
|
||||
|
@ -29,13 +32,13 @@ namespace NLua.Method
|
|||
{
|
||||
_eventInfo.AddEventHandler(_target, handlerDelegate);
|
||||
_pendingEvents.Add(handlerDelegate, this);
|
||||
|
||||
return handlerDelegate;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes an existing event handler
|
||||
/// Removes an existing event handler, called from Lua
|
||||
/// </summary>
|
||||
// ReSharper disable once UnusedMember.Global
|
||||
public void Remove(Delegate handlerDelegate)
|
||||
{
|
||||
RemovePending(handlerDelegate);
|
||||
|
@ -46,8 +49,6 @@ namespace NLua.Method
|
|||
/// Removes an existing event handler (without updating the pending handlers list)
|
||||
/// </summary>
|
||||
internal void RemovePending(Delegate handlerDelegate)
|
||||
{
|
||||
_eventInfo.RemoveEventHandler(_target, handlerDelegate);
|
||||
}
|
||||
=> _eventInfo.RemoveEventHandler(_target, handlerDelegate);
|
||||
}
|
||||
}
|
|
@ -1,104 +1,29 @@
|
|||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace NLua
|
||||
namespace NLua.Native
|
||||
{
|
||||
internal static class DelegateExtensions
|
||||
{
|
||||
public static LuaNativeFunction ToLuaFunction(this IntPtr ptr)
|
||||
{
|
||||
if (ptr == IntPtr.Zero)
|
||||
return null;
|
||||
|
||||
return Marshal.GetDelegateForFunctionPointer<LuaNativeFunction>(ptr);
|
||||
}
|
||||
=> ptr == IntPtr.Zero ? null : Marshal.GetDelegateForFunctionPointer<LuaNativeFunction>(ptr);
|
||||
|
||||
public static IntPtr ToFunctionPointer(this LuaNativeFunction d)
|
||||
{
|
||||
if (d == null)
|
||||
return IntPtr.Zero;
|
||||
|
||||
return Marshal.GetFunctionPointerForDelegate<LuaNativeFunction>(d);
|
||||
}
|
||||
|
||||
public static LuaHookFunction ToLuaHookFunction(this IntPtr ptr)
|
||||
{
|
||||
if (ptr == IntPtr.Zero)
|
||||
return null;
|
||||
|
||||
return Marshal.GetDelegateForFunctionPointer<LuaHookFunction>(ptr);
|
||||
}
|
||||
|
||||
public static IntPtr ToFunctionPointer(this LuaHookFunction d)
|
||||
{
|
||||
if (d == null)
|
||||
return IntPtr.Zero;
|
||||
|
||||
return Marshal.GetFunctionPointerForDelegate<LuaHookFunction>(d);
|
||||
}
|
||||
|
||||
public static LuaKFunction ToLuaKFunction(this IntPtr ptr)
|
||||
{
|
||||
if (ptr == IntPtr.Zero)
|
||||
return null;
|
||||
|
||||
return Marshal.GetDelegateForFunctionPointer<LuaKFunction>(ptr);
|
||||
}
|
||||
=> d == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate(d);
|
||||
|
||||
public static IntPtr ToFunctionPointer(this LuaKFunction d)
|
||||
{
|
||||
if (d == null)
|
||||
return IntPtr.Zero;
|
||||
|
||||
return Marshal.GetFunctionPointerForDelegate<LuaKFunction>(d);
|
||||
}
|
||||
|
||||
public static LuaReader ToLuaReader(this IntPtr ptr)
|
||||
{
|
||||
if (ptr == IntPtr.Zero)
|
||||
return null;
|
||||
|
||||
return Marshal.GetDelegateForFunctionPointer<LuaReader>(ptr);
|
||||
}
|
||||
=> d == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate(d);
|
||||
|
||||
public static IntPtr ToFunctionPointer(this LuaReader d)
|
||||
{
|
||||
if (d == null)
|
||||
return IntPtr.Zero;
|
||||
|
||||
return Marshal.GetFunctionPointerForDelegate<LuaReader>(d);
|
||||
}
|
||||
|
||||
public static LuaWriter ToLuaWriter(this IntPtr ptr)
|
||||
{
|
||||
if (ptr == IntPtr.Zero)
|
||||
return null;
|
||||
|
||||
return Marshal.GetDelegateForFunctionPointer<LuaWriter>(ptr);
|
||||
}
|
||||
=> d == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate(d);
|
||||
|
||||
public static IntPtr ToFunctionPointer(this LuaWriter d)
|
||||
{
|
||||
if (d == null)
|
||||
return IntPtr.Zero;
|
||||
|
||||
return Marshal.GetFunctionPointerForDelegate<LuaWriter>(d);
|
||||
}
|
||||
=> d == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate(d);
|
||||
|
||||
public static LuaAlloc ToLuaAlloc(this IntPtr ptr)
|
||||
{
|
||||
if (ptr == IntPtr.Zero)
|
||||
return null;
|
||||
|
||||
return Marshal.GetDelegateForFunctionPointer<LuaAlloc>(ptr);
|
||||
}
|
||||
=> ptr == IntPtr.Zero ? null : Marshal.GetDelegateForFunctionPointer<LuaAlloc>(ptr);
|
||||
|
||||
public static IntPtr ToFunctionPointer(this LuaAlloc d)
|
||||
{
|
||||
if (d == null)
|
||||
return IntPtr.Zero;
|
||||
|
||||
return Marshal.GetFunctionPointerForDelegate<LuaAlloc>(d);
|
||||
}
|
||||
=> d == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate(d);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
using System.Runtime.InteropServices;
|
||||
using System.Security;
|
||||
|
||||
using charptr_t = System.IntPtr;
|
||||
using lua_Debug = System.IntPtr;
|
||||
using lua_KContext = System.IntPtr;
|
||||
using lua_State = System.IntPtr;
|
||||
using size_t = System.UIntPtr;
|
||||
using voidptr_t = System.IntPtr;
|
||||
|
||||
namespace NLua
|
||||
namespace NLua.Native
|
||||
{
|
||||
/// <summary>
|
||||
/// Type for C# callbacks
|
||||
|
@ -19,15 +19,6 @@ namespace NLua
|
|||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
public delegate int LuaNativeFunction(lua_State luaState);
|
||||
|
||||
/// <summary>
|
||||
/// Type for debugging hook functions callbacks.
|
||||
/// </summary>
|
||||
/// <param name="luaState"></param>
|
||||
/// <param name="ar"></param>
|
||||
[SuppressUnmanagedCodeSecurity]
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
public delegate void LuaHookFunction(lua_State luaState, lua_Debug ar);
|
||||
|
||||
/// <summary>
|
||||
/// Type for continuation functions
|
||||
/// </summary>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
namespace NLua
|
||||
namespace NLua.Native
|
||||
{
|
||||
/// <summary>
|
||||
/// Used by Compare
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
// ReSharper disable IdentifierTypo
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security;
|
||||
|
||||
using BizHawk.BizInvoke;
|
||||
|
@ -7,23 +6,20 @@ using BizHawk.BizInvoke;
|
|||
using charptr_t = System.IntPtr;
|
||||
using lua_Alloc = System.IntPtr;
|
||||
using lua_CFunction = System.IntPtr;
|
||||
using lua_Debug = System.IntPtr;
|
||||
using lua_Hook = System.IntPtr;
|
||||
using lua_Integer = System.Int64;
|
||||
using lua_KContext = System.IntPtr;
|
||||
using lua_KFunction = System.IntPtr;
|
||||
using lua_Number = System.Double;
|
||||
using lua_Number_ptr = System.IntPtr;
|
||||
using lua_Reader = System.IntPtr;
|
||||
using lua_State = System.IntPtr;
|
||||
using lua_WarnFunction = System.IntPtr;
|
||||
using lua_Writer = System.IntPtr;
|
||||
using size_t = System.UIntPtr;
|
||||
using voidptr_t = System.IntPtr;
|
||||
|
||||
#pragma warning disable SA1121 // Use built-in type alias
|
||||
#pragma warning disable IDE1006 // Naming rule violation
|
||||
|
||||
namespace NLua
|
||||
namespace NLua.Native
|
||||
{
|
||||
[SuppressUnmanagedCodeSecurity]
|
||||
public abstract class LuaNativeMethods
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
namespace NLua
|
||||
namespace NLua.Native
|
||||
{
|
||||
/// <summary>
|
||||
/// Operation value used by Arith method
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace NLua
|
||||
namespace NLua.Native
|
||||
{
|
||||
/// <summary>
|
||||
/// LuaRegister store the name and the delegate to register a native function
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
namespace NLua
|
||||
namespace NLua.Native
|
||||
{
|
||||
/// <summary>
|
||||
/// Enum for pseudo-index used by registry table
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,4 +1,4 @@
|
|||
namespace NLua
|
||||
namespace NLua.Native
|
||||
{
|
||||
/// <summary>
|
||||
/// Lua Load/Call status return
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
namespace NLua
|
||||
namespace NLua.Native
|
||||
{
|
||||
/// <summary>
|
||||
/// Lua types
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,42 +1,39 @@
|
|||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
using NLua.Native;
|
||||
|
||||
namespace NLua
|
||||
{
|
||||
internal class ObjectTranslatorPool
|
||||
{
|
||||
private static volatile ObjectTranslatorPool _instance = new ObjectTranslatorPool();
|
||||
|
||||
private ConcurrentDictionary<LuaState, ObjectTranslator> translators = new ConcurrentDictionary<LuaState, ObjectTranslator>();
|
||||
|
||||
public static ObjectTranslatorPool Instance => _instance;
|
||||
|
||||
private readonly ConcurrentDictionary<LuaState, ObjectTranslator> translators = new();
|
||||
public static ObjectTranslatorPool Instance { get; } = new();
|
||||
|
||||
public void Add(LuaState luaState, ObjectTranslator translator)
|
||||
{
|
||||
if(!translators.TryAdd(luaState, translator))
|
||||
if (!translators.TryAdd(luaState, translator))
|
||||
{
|
||||
throw new ArgumentException("An item with the same key has already been added. ", nameof(luaState));
|
||||
}
|
||||
}
|
||||
|
||||
public ObjectTranslator Find(LuaState luaState)
|
||||
{
|
||||
ObjectTranslator translator;
|
||||
|
||||
if(!translators.TryGetValue(luaState, out translator))
|
||||
if (!translators.TryGetValue(luaState, out var translator))
|
||||
{
|
||||
LuaState main = luaState.MainThread;
|
||||
|
||||
var main = luaState.MainThread;
|
||||
if (!translators.TryGetValue(main, out translator))
|
||||
throw new Exception("Invalid luaState, couldn't find ObjectTranslator");
|
||||
{
|
||||
throw new("Invalid luaState, couldn't find ObjectTranslator");
|
||||
}
|
||||
}
|
||||
|
||||
return translator;
|
||||
}
|
||||
|
||||
public void Remove(LuaState luaState)
|
||||
{
|
||||
ObjectTranslator translator;
|
||||
translators.TryRemove(luaState, out translator);
|
||||
}
|
||||
=> translators.TryRemove(luaState, out _);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -5,11 +5,9 @@ namespace NLua
|
|||
{
|
||||
public class ProxyType
|
||||
{
|
||||
private readonly Type _proxy;
|
||||
|
||||
public ProxyType(Type proxy)
|
||||
{
|
||||
_proxy = proxy;
|
||||
UnderlyingSystemType = proxy;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -17,34 +15,24 @@ namespace NLua
|
|||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override string ToString()
|
||||
{
|
||||
return "ProxyType(" + UnderlyingSystemType + ")";
|
||||
}
|
||||
=> "ProxyType(" + UnderlyingSystemType + ")";
|
||||
|
||||
public Type UnderlyingSystemType => _proxy;
|
||||
public Type UnderlyingSystemType { get; }
|
||||
|
||||
public override bool Equals(object obj)
|
||||
public override bool Equals(object obj) => obj switch
|
||||
{
|
||||
if (obj is Type)
|
||||
return _proxy == (Type)obj;
|
||||
if (obj is ProxyType)
|
||||
return _proxy == ((ProxyType)obj).UnderlyingSystemType;
|
||||
return _proxy.Equals(obj);
|
||||
}
|
||||
Type type => UnderlyingSystemType == type,
|
||||
ProxyType type => UnderlyingSystemType == type.UnderlyingSystemType,
|
||||
_ => UnderlyingSystemType.Equals(obj)
|
||||
};
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _proxy.GetHashCode();
|
||||
}
|
||||
=> UnderlyingSystemType.GetHashCode();
|
||||
|
||||
public MemberInfo[] GetMember(string name, BindingFlags bindingAttr)
|
||||
{
|
||||
return _proxy.GetMember(name, bindingAttr);
|
||||
}
|
||||
=> UnderlyingSystemType.GetMember(name, bindingAttr);
|
||||
|
||||
public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Type[] signature)
|
||||
{
|
||||
return _proxy.GetMethod(name, bindingAttr, null, signature, null);
|
||||
}
|
||||
=> UnderlyingSystemType.GetMethod(name, bindingAttr, null, signature, null);
|
||||
}
|
||||
}
|
|
@ -10,8 +10,8 @@
|
|||
"NLua/1.0.0": {
|
||||
"dependencies": {
|
||||
"DotNetAnalyzers.DocumentationAnalyzers": "1.0.0-beta.59",
|
||||
"Menees.Analyzers": "3.0.8",
|
||||
"Meziantou.Analyzer": "1.0.707",
|
||||
"Menees.Analyzers": "3.0.10",
|
||||
"Meziantou.Analyzer": "2.0.33",
|
||||
"NETStandard.Library": "2.0.3",
|
||||
"Nullable": "1.3.1",
|
||||
"StyleCop.Analyzers": "1.2.0-beta.435",
|
||||
|
@ -28,8 +28,8 @@
|
|||
}
|
||||
},
|
||||
"DotNetAnalyzers.DocumentationAnalyzers.Unstable/1.0.0.59": {},
|
||||
"Menees.Analyzers/3.0.8": {},
|
||||
"Meziantou.Analyzer/1.0.707": {},
|
||||
"Menees.Analyzers/3.0.10": {},
|
||||
"Meziantou.Analyzer/2.0.33": {},
|
||||
"Microsoft.NETCore.Platforms/1.1.0": {},
|
||||
"NETStandard.Library/2.0.3": {
|
||||
"dependencies": {
|
||||
|
@ -59,19 +59,11 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"System.Memory/4.0.1.1": {
|
||||
"System.Memory/4.0.1.2": {
|
||||
"runtime": {
|
||||
"System.Memory.dll": {
|
||||
"assemblyVersion": "4.0.1.1",
|
||||
"fileVersion": "4.6.28619.1"
|
||||
}
|
||||
}
|
||||
},
|
||||
"System.Numerics.Vectors/4.1.4.0": {
|
||||
"runtime": {
|
||||
"System.Numerics.Vectors.dll": {
|
||||
"assemblyVersion": "4.1.4.0",
|
||||
"fileVersion": "4.6.26515.6"
|
||||
"assemblyVersion": "4.0.1.2",
|
||||
"fileVersion": "4.6.31308.1"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -82,6 +74,22 @@
|
|||
"fileVersion": "4.6.28619.1"
|
||||
}
|
||||
}
|
||||
},
|
||||
"System.Runtime.CompilerServices.Unsafe/6.0.0.0": {
|
||||
"runtime": {
|
||||
"System.Runtime.CompilerServices.Unsafe.dll": {
|
||||
"assemblyVersion": "6.0.0.0",
|
||||
"fileVersion": "6.0.21.52210"
|
||||
}
|
||||
}
|
||||
},
|
||||
"System.Numerics.Vectors/4.1.4.0": {
|
||||
"runtime": {
|
||||
"System.Numerics.Vectors.dll": {
|
||||
"assemblyVersion": "4.1.4.0",
|
||||
"fileVersion": "4.6.26515.6"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -105,19 +113,19 @@
|
|||
"path": "dotnetanalyzers.documentationanalyzers.unstable/1.0.0.59",
|
||||
"hashPath": "dotnetanalyzers.documentationanalyzers.unstable.1.0.0.59.nupkg.sha512"
|
||||
},
|
||||
"Menees.Analyzers/3.0.8": {
|
||||
"Menees.Analyzers/3.0.10": {
|
||||
"type": "package",
|
||||
"serviceable": true,
|
||||
"sha512": "sha512-26WtDVJZe2CEpCqJ16uR0KkCBLwDsHrr4lLSH4JZzUWoYqIQPgpwkh/9VXNJzi5bFeKn/gL0Sz77eFoNaArkhA==",
|
||||
"path": "menees.analyzers/3.0.8",
|
||||
"hashPath": "menees.analyzers.3.0.8.nupkg.sha512"
|
||||
"sha512": "sha512-ZaCxhd3KKxOqgtDv4YL0Tm6+dhlH+HtXyXrolb6zquAHBR8ONwywofNOb6d8UlafQaNiNdiqVQz8tG4+4NdLyw==",
|
||||
"path": "menees.analyzers/3.0.10",
|
||||
"hashPath": "menees.analyzers.3.0.10.nupkg.sha512"
|
||||
},
|
||||
"Meziantou.Analyzer/1.0.707": {
|
||||
"Meziantou.Analyzer/2.0.33": {
|
||||
"type": "package",
|
||||
"serviceable": true,
|
||||
"sha512": "sha512-8NklUjV3DHVxfyCbPYlwVtBjeNLF23lcS1QUBZt5ri1pxqr2UaVcg/uD+l0ZxwSnwTwTxX6A+Rfn+WksnBsLsg==",
|
||||
"path": "meziantou.analyzer/1.0.707",
|
||||
"hashPath": "meziantou.analyzer.1.0.707.nupkg.sha512"
|
||||
"sha512": "sha512-LslJaY4cjxOjIM8X7JRMRHL2roG7E28m1ZPFxIcvNp4sfiGQJz0jsHCl9HsblRmXS/IX65eu3qZ15Q3FL2EsPg==",
|
||||
"path": "meziantou.analyzer/2.0.33",
|
||||
"hashPath": "meziantou.analyzer.2.0.33.nupkg.sha512"
|
||||
},
|
||||
"Microsoft.NETCore.Platforms/1.1.0": {
|
||||
"type": "package",
|
||||
|
@ -164,12 +172,7 @@
|
|||
"serviceable": false,
|
||||
"sha512": ""
|
||||
},
|
||||
"System.Memory/4.0.1.1": {
|
||||
"type": "reference",
|
||||
"serviceable": false,
|
||||
"sha512": ""
|
||||
},
|
||||
"System.Numerics.Vectors/4.1.4.0": {
|
||||
"System.Memory/4.0.1.2": {
|
||||
"type": "reference",
|
||||
"serviceable": false,
|
||||
"sha512": ""
|
||||
|
@ -178,6 +181,16 @@
|
|||
"type": "reference",
|
||||
"serviceable": false,
|
||||
"sha512": ""
|
||||
},
|
||||
"System.Runtime.CompilerServices.Unsafe/6.0.0.0": {
|
||||
"type": "reference",
|
||||
"serviceable": false,
|
||||
"sha512": ""
|
||||
},
|
||||
"System.Numerics.Vectors/4.1.4.0": {
|
||||
"type": "reference",
|
||||
"serviceable": false,
|
||||
"sha512": ""
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
|
@ -8,6 +8,7 @@ using System.Reflection;
|
|||
using System.Threading;
|
||||
|
||||
using NLua;
|
||||
using NLua.Native;
|
||||
|
||||
using BizHawk.Common;
|
||||
using BizHawk.Emulation.Common;
|
||||
|
|
Loading…
Reference in New Issue