Repository navigation
Expand file tree
/
Copy pathModuleLibrary.cs
More file actions
317 lines (295 loc) · 10.7 KB
/
Copy pathModuleLibrary.cs
File metadata and controls
317 lines (295 loc) · 10.7 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
using System.Runtime.CompilerServices;
using System.Text;
using Lua.Internal.CompilerServices;
using Lua.Runtime;
namespace Lua.Standard;
public sealed class ModuleLibrary
{
public static readonly ModuleLibrary Instance = new();
internal const string LoadedKeyForRegistry = "_LOADED";
internal const string PreloadKeyForRegistry = "_PRELOAD";
public ModuleLibrary()
{
RequireFunction = new("require", Require);
SearchPathFunction = new("package.searchpath", SearchPath);
}
public readonly LuaFunction RequireFunction;
public readonly LuaFunction SearchPathFunction;
public ValueTask<int> Require(
LuaFunctionExecutionContext context,
CancellationToken cancellationToken
)
{
var arg0 = context.GetStringArgument(0);
var loaded = context.GlobalState.LoadedModules;
var loadedTable = loaded[arg0];
if (loadedTable.ToBoolean())
return new(context.Return(loadedTable));
return RequireAsync(context, arg0, loaded, loadedTable, cancellationToken);
}
[AsyncMethodBuilder(typeof(LightAsyncValueTaskMethodBuilder<>))]
static async ValueTask<int> RequireAsync(
LuaFunctionExecutionContext context,
string arg0,
LuaTable loaded,
LuaValue loadedTable,
CancellationToken cancellationToken
)
{
var state = context.State;
state.NonYieldableCallCount++;
try
{
if (loadedTable.Type == LuaValueType.Nil && loaded.Metatable != null)
loadedTable = await LuaVirtualMachine.ExecuteGetTableSlowPath(
state,
loaded,
arg0,
cancellationToken
);
if (!loadedTable.ToBoolean())
{
LuaFunction loader;
LuaValue data = LuaValue.Nil;
var moduleLoader = context.GlobalState.ModuleLoader;
var hostName = moduleLoader is null ? arg0 : state.Platform.DecodeText(arg0);
if (moduleLoader != null && moduleLoader.Exists(hostName))
{
var module = await moduleLoader.LoadAsync(hostName, cancellationToken);
loader = state.Load(module);
}
else
{
(loader, data) = await FindLoader(
state,
GetPackage(context),
arg0,
cancellationToken
);
}
state.Stack.PushRange(arg0, data);
var count = await state.RunCoreAsync(
loader,
2,
context.ReturnFrameBase,
cancellationToken
);
if (count > 0 && state.Stack.Get(context.ReturnFrameBase).Type != LuaValueType.Nil)
await state.SetTableCoreAsync(
loaded,
arg0,
state.Stack.Get(context.ReturnFrameBase),
cancellationToken
);
if (!loaded.TryGetValue(arg0, out loadedTable) && loaded.Metatable != null)
loadedTable = await LuaVirtualMachine.ExecuteGetTableSlowPath(
state,
loaded,
arg0,
cancellationToken
);
if (loadedTable.Type == LuaValueType.Nil)
{
loadedTable = true;
await state.SetTableCoreAsync(loaded, arg0, loadedTable, cancellationToken);
}
}
return context.Return(loadedTable);
}
finally
{
state.NonYieldableCallCount--;
}
}
static LuaValue GetPackage(LuaFunctionExecutionContext context) =>
context.GetCsClosure() is { } closure
? closure.UpValues[0]
: context.GlobalState.Environment["package"];
[AsyncMethodBuilder(typeof(LightAsyncValueTaskMethodBuilder<>))]
internal static async ValueTask<(string? FileName, string? Error)> FindFile(
LuaState state,
LuaValue package,
string name,
string pName,
string dirSeparator,
CancellationToken cancellationToken
)
{
if (!package.TryReadTable(out var table) || !table.TryGetValue(pName, out var p))
p = await LuaVirtualMachine.ExecuteGetTableSlowPath(
state,
package,
pName,
cancellationToken
);
if (p.Type is not (LuaValueType.String or LuaValueType.Number))
{
throw new LuaRuntimeException(state, $"package.{pName} must be a string");
}
return SearchPath(
state,
name,
p.ToLuaString(state.Platform.NumberFormat),
".",
dirSeparator
);
}
public ValueTask<int> SearchPath(
LuaFunctionExecutionContext context,
CancellationToken cancellationToken
)
{
var name = context.GetStringArgument(0);
var path = context.GetStringArgument(1);
var separator = context.HasArgument(2) ? context.GetStringArgument(2) : ".";
var dirSeparator = context.HasArgument(3)
? context.GetStringArgument(3)
: context.State.Platform.FileSystem.DirectorySeparator;
var (fileName, error) = SearchPath(context.State, name, path, separator, dirSeparator);
return new(
fileName != null ? context.Return(fileName) : context.Return(LuaValue.Nil, error!)
);
}
internal static (string? FileName, string? Error) SearchPath(
LuaState state,
string name,
string path,
string separator,
string dirSeparator
)
{
if (separator != "")
{
name = name.Replace(separator, dirSeparator);
}
var pathSpan = path.AsSpan();
StringBuilder? errors = null;
while (!pathSpan.IsEmpty)
{
var nextIndex = pathSpan.IndexOf(';');
if (nextIndex < 0)
nextIndex = pathSpan.Length;
var template = pathSpan[..nextIndex];
if (!template.IsEmpty)
{
var fileName = template.ToString().Replace("?", name);
if (state.Platform.FileSystem.IsReadable(state.Platform.DecodeText(fileName)))
return (fileName, null);
(errors ??= new()).Append("\n\tno file '").Append(fileName).Append('\'');
}
pathSpan = nextIndex == pathSpan.Length ? default : pathSpan[(nextIndex + 1)..];
}
return (null, errors?.ToString() ?? "");
}
[AsyncMethodBuilder(typeof(LightAsyncValueTaskMethodBuilder<>))]
internal static async ValueTask<(LuaFunction Function, LuaValue Data)> FindLoader(
LuaState state,
LuaValue package,
string name,
CancellationToken cancellationToken
)
{
if (!package.TryReadTable(out var table) || !table.TryGetValue("searchers", out var value))
value = await LuaVirtualMachine.ExecuteGetTableSlowPath(
state,
package,
"searchers",
cancellationToken
);
if (!value.TryReadTable(out var searchers))
throw new LuaRuntimeException(state, "package.searchers must be a table");
StringBuilder? errors = null;
for (var i = 1; ; i++)
{
var searcher = searchers[i];
if (searcher.Type == LuaValueType.Nil)
break;
var loader = searcher;
var top = state.Stack.Count;
state.Stack.Push(loader);
state.Stack.Push(name);
var resultCount = await LuaVirtualMachine.Call(state, top, top, cancellationToken);
if (0 < resultCount)
{
var result = state.Stack.Get(top);
if (result.Type == LuaValueType.Function)
{
var data = resultCount > 1 ? state.Stack.Get(top + 1) : LuaValue.Nil;
state.Stack.SetTop(top);
return (result.Read<LuaFunction>(), data);
}
if (result.Type is LuaValueType.String or LuaValueType.Number)
(errors ??= new()).Append(result.ToLuaString(state.Platform.NumberFormat));
}
state.Stack.SetTop(top);
}
throw new LuaRuntimeException(state, $"module '{name}' not found:{errors}");
}
[AsyncMethodBuilder(typeof(LightAsyncValueTaskMethodBuilder<>))]
public async ValueTask<int> SearcherPreload(
LuaFunctionExecutionContext context,
CancellationToken cancellationToken
)
{
var name = context.GetStringArgument(0);
var table = context.GlobalState.PreloadModules;
if (!table.TryGetValue(name, out var preload) && table.Metatable != null)
{
context.State.NonYieldableCallCount++;
try
{
preload = await LuaVirtualMachine.ExecuteGetTableSlowPath(
context.State,
table,
name,
cancellationToken
);
}
finally
{
context.State.NonYieldableCallCount--;
}
}
if (preload == LuaValue.Nil)
{
return context.Return($"\n\tno field package.preload['{name}']");
}
return context.Return(preload);
}
public async ValueTask<int> SearcherLua(
LuaFunctionExecutionContext context,
CancellationToken cancellationToken
)
{
var name = context.GetStringArgument(0);
context.State.NonYieldableCallCount++;
try
{
var (fileName, error) = await FindFile(
context.State,
GetPackage(context),
name,
"path",
context.GlobalState.Platform.FileSystem.DirectorySeparator,
cancellationToken
);
if (fileName == null)
{
return context.Return(error!);
}
return context.Return(
await context.State.LoadFileAsync(
context.State.Platform.DecodeText(fileName),
"bt",
null,
cancellationToken
),
fileName
);
}
finally
{
context.State.NonYieldableCallCount--;
}
}
}