Repository navigation
Expand file tree
/
Copy pathModuleLibrary.cs
More file actions
209 lines (184 loc) · 6.32 KB
/
Copy pathModuleLibrary.cs
File metadata and controls
209 lines (184 loc) · 6.32 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 Lua.Internal.CompilerServices;
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;
[AsyncMethodBuilder(typeof(LightAsyncValueTaskMethodBuilder<>))]
public async ValueTask<int> Require(
LuaFunctionExecutionContext context,
CancellationToken cancellationToken
)
{
var arg0 = context.GetStringArgument(0);
var loaded = context.GlobalState.LoadedModules;
if (!loaded.TryGetValue(arg0, out var loadedTable))
{
LuaFunction loader;
var moduleLoader = context.GlobalState.ModuleLoader;
var hostName = moduleLoader is null ? arg0 : context.State.Platform.DecodeText(arg0);
if (moduleLoader != null && moduleLoader.Exists(hostName))
{
var module = await moduleLoader.LoadAsync(hostName, cancellationToken);
loader = context.State.Load(module);
}
else
{
loader = await FindLoader(context.State, arg0, cancellationToken);
}
await context.State.RunAsync(loader, 0, context.ReturnFrameBase, cancellationToken);
loadedTable = context.State.Stack.Get(context.ReturnFrameBase);
loaded[arg0] = loadedTable;
}
return context.Return(loadedTable);
}
[AsyncMethodBuilder(typeof(LightAsyncValueTaskMethodBuilder<>))]
internal static async ValueTask<string?> FindFile(
LuaState state,
string name,
string pName,
string dirSeparator
)
{
var globalState = state.GlobalState;
var package = globalState.Environment["package"];
var p = await state.GetTableAsync(package, pName);
if (!p.TryReadString(out var path))
{
throw new LuaRuntimeException(state, $"package.{pName} must be a string");
}
return SearchPath(state, name, path, ".", dirSeparator);
}
public ValueTask<int> SearchPath(
LuaFunctionExecutionContext context,
CancellationToken cancellationToken
)
{
var name = context.GetStringArgument(0);
var path = context.GetStringArgument(1);
var separator = context.GetStringArgument(2);
var dirSeparator = context.GetStringArgument(3);
var fileName = SearchPath(context.State, name, path, separator, dirSeparator);
return new(context.Return(fileName ?? LuaValue.Nil));
}
internal static string? SearchPath(
LuaState state,
string name,
string path,
string separator,
string dirSeparator
)
{
if (separator != "")
{
name = name.Replace(separator, dirSeparator);
}
var pathSpan = path.AsSpan();
var nextIndex = pathSpan.IndexOf(';');
if (nextIndex == -1)
{
nextIndex = pathSpan.Length;
}
do
{
path = pathSpan[..nextIndex].ToString();
var fileName = path.Replace("?", name);
if (state.Platform.FileSystem.IsReadable(state.Platform.DecodeText(fileName)))
{
return fileName;
}
if (pathSpan.Length <= nextIndex)
{
break;
}
pathSpan = pathSpan[(nextIndex + 1)..];
nextIndex = pathSpan.IndexOf(';');
if (nextIndex == -1)
{
nextIndex = pathSpan.Length;
}
} while (nextIndex != -1);
return null;
}
[AsyncMethodBuilder(typeof(LightAsyncValueTaskMethodBuilder<>))]
internal static async ValueTask<LuaFunction> FindLoader(
LuaState state,
string name,
CancellationToken cancellationToken
)
{
var package = state.GlobalState.Environment["package"].Read<LuaTable>();
var searchers = package["searchers"].Read<LuaTable>();
for (var i = 0; i < searchers.GetArrayReadOnlySpan().Length; i++)
{
var searcher = searchers.GetArrayReadOnlySpan()[i];
if (searcher.Type == LuaValueType.Nil)
{
continue;
}
var loader = searcher;
var top = state.Stack.Count;
state.Stack.Push(loader);
state.Stack.Push(name);
var resultCount = await state.CallAsync(top, top, cancellationToken);
if (0 < resultCount)
{
var result = state.Stack.Get(top);
if (result.Type == LuaValueType.Function)
{
state.Stack.SetTop(top);
return result.Read<LuaFunction>();
}
}
state.Stack.SetTop(top);
}
throw new LuaRuntimeException(state, $"Module '{name}' not found");
}
public ValueTask<int> SearcherPreload(
LuaFunctionExecutionContext context,
CancellationToken cancellationToken
)
{
var name = context.GetStringArgument(0);
var preload = context.GlobalState.PreloadModules[name];
if (preload == LuaValue.Nil)
{
return new(context.Return());
}
return new(context.Return(preload));
}
public async ValueTask<int> SearcherLua(
LuaFunctionExecutionContext context,
CancellationToken cancellationToken
)
{
var name = context.GetStringArgument(0);
var fileName = await FindFile(
context.State,
name,
"path",
context.GlobalState.Platform.FileSystem.DirectorySeparator
);
if (fileName == null)
{
return context.Return(LuaValue.Nil);
}
return context.Return(
await context.State.LoadFileAsync(
context.State.Platform.DecodeText(fileName),
"bt",
null,
cancellationToken
)
);
}
}