-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathLuaFunctionExecutionContext.cs
More file actions
209 lines (181 loc) · 6.26 KB
/
Copy pathLuaFunctionExecutionContext.cs
File metadata and controls
209 lines (181 loc) · 6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Lua.Runtime;
namespace Lua;
[StructLayout(LayoutKind.Auto)]
public readonly record struct LuaFunctionExecutionContext
{
internal LuaGlobalState GlobalState => State.GlobalState;
public LuaState State { get; init; }
public required int ArgumentCount { get; init; }
public required int ReturnFrameBase { get; init; }
// public object? AdditionalContext { get; init; }
public int FrameBase => State.Stack.Count - ArgumentCount;
public ReadOnlySpan<LuaValue> Arguments
{
get
{
var stack = State.Stack.AsSpan();
return stack[^ArgumentCount..];
}
}
public ReadOnlyMemory<LuaValue> ArgumentsAsMemory
{
get
{
var stack = State.Stack;
var memory = stack.GetBufferMemory();
return memory[(stack.Count-ArgumentCount)..stack.Count];
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool HasArgument(int index)
{
return ArgumentCount > index && Arguments[index].Type is not LuaValueType.Nil;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public LuaValue GetArgument(int index)
{
ThrowIfArgumentNotExists(index);
return Arguments[index];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal LuaValue GetArgumentOrDefault(int index, LuaValue defaultValue = default)
{
if (ArgumentCount <= index)
{
return defaultValue;
}
return Arguments[index];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T GetArgument<T>(int index)
{
ThrowIfArgumentNotExists(index);
var arg = Arguments[index];
if (!arg.TryRead<T>(out var argValue))
{
var t = typeof(T);
if ((t == typeof(int) || t == typeof(long)) && arg.TryReadNumber(out _))
{
LuaRuntimeException.BadArgumentNumberIsNotInteger(State, index + 1);
}
else if (LuaValue.TryGetLuaValueType(t, out var type))
{
LuaRuntimeException.BadArgument(State, index + 1, type, arg.Type);
}
else if (arg.Type is LuaValueType.UserData or LuaValueType.LightUserData)
{
LuaRuntimeException.BadArgument(State, index + 1, t.Name, arg.UnsafeRead<object>()?.GetType().ToString() ?? "userdata: 0");
}
else
{
LuaRuntimeException.BadArgument(State, index + 1, t.Name, arg.TypeToString());
}
}
return argValue;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal T GetArgumentOrDefault<T>(int index, T defaultValue = default!)
{
if (ArgumentCount <= index)
{
return defaultValue;
}
var arg = Arguments[index];
if (arg.Type is LuaValueType.Nil)
{
return defaultValue;
}
if (!arg.TryRead<T>(out var argValue))
{
var t = typeof(T);
if ((t == typeof(int) || t == typeof(long)) && arg.TryReadNumber(out _))
{
LuaRuntimeException.BadArgumentNumberIsNotInteger(State, index + 1);
}
else if (LuaValue.TryGetLuaValueType(t, out var type))
{
LuaRuntimeException.BadArgument(State, index + 1, type, arg.Type);
}
else if (arg.Type is LuaValueType.UserData or LuaValueType.LightUserData)
{
LuaRuntimeException.BadArgument(State, index + 1, t.Name, arg.UnsafeRead<object>()?.GetType().ToString() ?? "userdata: 0");
}
else
{
LuaRuntimeException.BadArgument(State, index + 1, t.Name, arg.TypeToString());
}
}
return argValue;
}
public int Return()
{
State.Stack.PopUntil(ReturnFrameBase);
return 0;
}
public int Return(LuaValue result)
{
var stack = State.Stack;
stack.SetTop(ReturnFrameBase + 1);
stack.FastGet(ReturnFrameBase) = result;
return 1;
}
public int Return(LuaValue result0, LuaValue result1)
{
var stack = State.Stack;
stack.SetTop(ReturnFrameBase + 2);
stack.FastGet(ReturnFrameBase) = result0;
stack.FastGet(ReturnFrameBase + 1) = result1;
return 2;
}
public int Return(LuaValue result0, LuaValue result1, LuaValue result2)
{
var stack = State.Stack;
stack.SetTop(ReturnFrameBase + 3);
stack.FastGet(ReturnFrameBase) = result0;
stack.FastGet(ReturnFrameBase + 1) = result1;
stack.FastGet(ReturnFrameBase + 2) = result2;
return 3;
}
public int Return(ReadOnlySpan<LuaValue> results)
{
var stack = State.Stack;
stack.EnsureCapacity(ReturnFrameBase + results.Length);
results.CopyTo(stack.GetBuffer()[ReturnFrameBase..(ReturnFrameBase + results.Length)]);
stack.SetTop(ReturnFrameBase + results.Length);
return results.Length;
}
internal int Return(LuaValue result0, ReadOnlySpan<LuaValue> results)
{
var stack = State.Stack;
stack.EnsureCapacity(ReturnFrameBase + results.Length);
stack.SetTop(ReturnFrameBase + results.Length + 1);
var buffer = stack.GetBuffer();
buffer[ReturnFrameBase] = result0;
results.CopyTo(buffer[(ReturnFrameBase + 1)..(ReturnFrameBase + results.Length + 1)]);
return results.Length + 1;
}
public Span<LuaValue> GetReturnBuffer(int count)
{
var stack = State.Stack;
stack.SetTop(ReturnFrameBase + count);
var buffer = stack.GetBuffer()[ReturnFrameBase..(ReturnFrameBase + count)];
return buffer;
}
public CSharpClosure? GetCsClosure()
{
return State.GetCurrentFrame().Function as CSharpClosure;
}
internal void ThrowBadArgument(int index, string message)
{
LuaRuntimeException.BadArgument(State, index, State.GetCurrentFrame().Function.Name, message);
}
void ThrowIfArgumentNotExists(int index)
{
if (ArgumentCount <= index)
{
LuaRuntimeException.BadArgument(State, index + 1);
}
}
}