|
@@ -1,6 +1,7 @@
|
|
|
using System.Runtime.CompilerServices;
|
|
using System.Runtime.CompilerServices;
|
|
|
using System.Runtime.InteropServices;
|
|
using System.Runtime.InteropServices;
|
|
|
using Lua.CodeAnalysis;
|
|
using Lua.CodeAnalysis;
|
|
|
|
|
+using Lua.Runtime;
|
|
|
|
|
|
|
|
namespace Lua;
|
|
namespace Lua;
|
|
|
|
|
|
|
@@ -19,10 +20,7 @@ public readonly record struct LuaFunctionExecutionContext
|
|
|
|
|
|
|
|
public ReadOnlySpan<LuaValue> Arguments
|
|
public ReadOnlySpan<LuaValue> Arguments
|
|
|
{
|
|
{
|
|
|
- get
|
|
|
|
|
- {
|
|
|
|
|
- return Thread.GetStackValues().Slice(FrameBase, ArgumentCount);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ get { return Thread.GetStackValues().Slice(FrameBase, ArgumentCount); }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
@@ -37,7 +35,7 @@ public readonly record struct LuaFunctionExecutionContext
|
|
|
ThrowIfArgumentNotExists(index);
|
|
ThrowIfArgumentNotExists(index);
|
|
|
return Arguments[index];
|
|
return Arguments[index];
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
internal LuaValue GetArgumentOrDefault(int index, LuaValue defaultValue = default)
|
|
internal LuaValue GetArgumentOrDefault(int index, LuaValue defaultValue = default)
|
|
|
{
|
|
{
|
|
@@ -45,6 +43,7 @@ public readonly record struct LuaFunctionExecutionContext
|
|
|
{
|
|
{
|
|
|
return defaultValue;
|
|
return defaultValue;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
return Arguments[index];
|
|
return Arguments[index];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -65,9 +64,9 @@ public readonly record struct LuaFunctionExecutionContext
|
|
|
{
|
|
{
|
|
|
LuaRuntimeException.BadArgument(State.GetTraceback(), index + 1, Thread.GetCurrentFrame().Function.Name, type.ToString(), arg.Type.ToString());
|
|
LuaRuntimeException.BadArgument(State.GetTraceback(), index + 1, Thread.GetCurrentFrame().Function.Name, type.ToString(), arg.Type.ToString());
|
|
|
}
|
|
}
|
|
|
- else if(arg.Type is LuaValueType.UserData or LuaValueType.LightUserData)
|
|
|
|
|
|
|
+ else if (arg.Type is LuaValueType.UserData or LuaValueType.LightUserData)
|
|
|
{
|
|
{
|
|
|
- LuaRuntimeException.BadArgument(State.GetTraceback(), index + 1, Thread.GetCurrentFrame().Function.Name, t.Name, arg.UnsafeRead<object>()?.GetType().ToString()??"userdata: 0");
|
|
|
|
|
|
|
+ LuaRuntimeException.BadArgument(State.GetTraceback(), index + 1, Thread.GetCurrentFrame().Function.Name, t.Name, arg.UnsafeRead<object>()?.GetType().ToString() ?? "userdata: 0");
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
@@ -79,7 +78,7 @@ public readonly record struct LuaFunctionExecutionContext
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
- internal T GetArgumentOrDefault<T>(int index,T defaultValue =default!)
|
|
|
|
|
|
|
+ internal T GetArgumentOrDefault<T>(int index, T defaultValue = default!)
|
|
|
{
|
|
{
|
|
|
if (ArgumentCount <= index)
|
|
if (ArgumentCount <= index)
|
|
|
{
|
|
{
|
|
@@ -87,12 +86,12 @@ public readonly record struct LuaFunctionExecutionContext
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
var arg = Arguments[index];
|
|
var arg = Arguments[index];
|
|
|
-
|
|
|
|
|
- if(arg.Type is LuaValueType.Nil)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if (arg.Type is LuaValueType.Nil)
|
|
|
{
|
|
{
|
|
|
return defaultValue;
|
|
return defaultValue;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
if (!arg.TryRead<T>(out var argValue))
|
|
if (!arg.TryRead<T>(out var argValue))
|
|
|
{
|
|
{
|
|
|
var t = typeof(T);
|
|
var t = typeof(T);
|
|
@@ -104,9 +103,9 @@ public readonly record struct LuaFunctionExecutionContext
|
|
|
{
|
|
{
|
|
|
LuaRuntimeException.BadArgument(State.GetTraceback(), index + 1, Thread.GetCurrentFrame().Function.Name, type.ToString(), arg.Type.ToString());
|
|
LuaRuntimeException.BadArgument(State.GetTraceback(), index + 1, Thread.GetCurrentFrame().Function.Name, type.ToString(), arg.Type.ToString());
|
|
|
}
|
|
}
|
|
|
- else if(arg.Type is LuaValueType.UserData or LuaValueType.LightUserData)
|
|
|
|
|
|
|
+ else if (arg.Type is LuaValueType.UserData or LuaValueType.LightUserData)
|
|
|
{
|
|
{
|
|
|
- LuaRuntimeException.BadArgument(State.GetTraceback(), index + 1, Thread.GetCurrentFrame().Function.Name, t.Name, arg.UnsafeRead<object>()?.GetType().ToString()??"userdata: 0");
|
|
|
|
|
|
|
+ LuaRuntimeException.BadArgument(State.GetTraceback(), index + 1, Thread.GetCurrentFrame().Function.Name, t.Name, arg.UnsafeRead<object>()?.GetType().ToString() ?? "userdata: 0");
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
@@ -116,7 +115,13 @@ public readonly record struct LuaFunctionExecutionContext
|
|
|
|
|
|
|
|
return argValue;
|
|
return argValue;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ public CsClosure? GetCsClosure()
|
|
|
|
|
+ {
|
|
|
|
|
+ var stack = Thread.CallStack[^1];
|
|
|
|
|
+ return stack.Function as CsClosure;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
internal void ThrowBadArgument(int index, string message)
|
|
internal void ThrowBadArgument(int index, string message)
|
|
|
{
|
|
{
|
|
|
LuaRuntimeException.BadArgument(State.GetTraceback(), index, Thread.GetCurrentFrame().Function.Name, message);
|
|
LuaRuntimeException.BadArgument(State.GetTraceback(), index, Thread.GetCurrentFrame().Function.Name, message);
|