Repository navigation
Expand file tree
/
Copy pathOpenLibsExtensions.cs
More file actions
197 lines (171 loc) · 6.55 KB
/
Copy pathOpenLibsExtensions.cs
File metadata and controls
197 lines (171 loc) · 6.55 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
using Lua.Runtime;
namespace Lua.Standard;
public static class OpenLibsExtensions
{
public static void OpenBasicLibrary(this LuaState state)
{
var globalState = state.GlobalState;
globalState.Environment["_G"] = globalState.Environment;
globalState.LoadedModules["_G"] = globalState.Environment;
globalState.Environment["_VERSION"] = "Lua 5.2";
foreach (var func in BasicLibrary.Instance.Functions)
{
globalState.Environment[func.Name] = func;
}
}
public static void OpenBitwiseLibrary(this LuaState state)
{
var globalState = state.GlobalState;
LuaTable bit32 = new(0, BitwiseLibrary.Instance.Functions.Length);
foreach (var func in BitwiseLibrary.Instance.Functions)
{
bit32[func.Name] = func.Func;
}
globalState.Environment["bit32"] = bit32;
globalState.LoadedModules["bit32"] = bit32;
}
public static void OpenCoroutineLibrary(this LuaState state)
{
var globalState = state.GlobalState;
LuaTable coroutine = new(0, CoroutineLibrary.Instance.Functions.Length);
foreach (var func in CoroutineLibrary.Instance.Functions)
{
coroutine[func.Name] = func.Func;
}
globalState.Environment["coroutine"] = coroutine;
globalState.LoadedModules["coroutine"] = coroutine;
}
public static void OpenIOLibrary(this LuaState state)
{
var globalState = state.GlobalState;
LuaTable io = new(0, IOLibrary.Instance.Functions.Length);
foreach (var func in IOLibrary.Instance.Functions)
{
io[func.Name] = func.Func;
}
var registry = globalState.Registry;
var standardIO = globalState.Platform.StandardIO;
var metatable = globalState.FileHandleMetatable;
LuaValue stdin = new(new FileHandle(standardIO.Input, metatable));
LuaValue stdout = new(new FileHandle(standardIO.Output, metatable));
LuaValue stderr = new(new FileHandle(standardIO.Error, metatable));
registry["_IO_input"] = stdin;
registry["_IO_output"] = stdout;
io["stdin"] = stdin;
io["stdout"] = stdout;
io["stderr"] = stderr;
globalState.Environment["io"] = io;
globalState.LoadedModules["io"] = io;
}
public static void OpenMathLibrary(this LuaState state)
{
var globalState = state.GlobalState;
globalState.Environment[MathematicsLibrary.RandomInstanceKey] = new(
new MathematicsLibrary.RandomUserData(new())
);
LuaTable math = new(0, MathematicsLibrary.Instance.Functions.Length);
foreach (var func in MathematicsLibrary.Instance.Functions)
{
math[func.Name] = func.Func;
}
math["pi"] = Math.PI;
math["huge"] = double.PositiveInfinity;
globalState.Environment["math"] = math;
globalState.LoadedModules["math"] = math;
}
public static void OpenModuleLibrary(this LuaState state)
{
var globalState = state.GlobalState;
LuaTable package = new(0, 8);
package["loaded"] = globalState.LoadedModules;
package["preload"] = globalState.PreloadModules;
var moduleLibrary = ModuleLibrary.Instance;
LuaTable searchers = new();
searchers[1] = new LuaFunction("preload", moduleLibrary.SearcherPreload);
searchers[2] = new CSharpClosure("searcher_Lua", [package], moduleLibrary.SearcherLua);
package["searchers"] = searchers;
package["path"] = "?.lua";
package["searchpath"] = moduleLibrary.SearchPathFunction;
package["config"] = $"{Path.DirectorySeparatorChar}\n;\n?\n!\n-\n";
globalState.Environment["package"] = package;
globalState.LoadedModules["package"] = package;
globalState.Environment["require"] = new CSharpClosure(
"require",
[package],
moduleLibrary.Require
);
}
public static void OpenOperatingSystemLibrary(this LuaState state)
{
var globalState = state.GlobalState;
LuaTable os = new(0, OperatingSystemLibrary.Instance.Functions.Length);
foreach (var func in OperatingSystemLibrary.Instance.Functions)
{
os[func.Name] = func.Func;
}
globalState.Environment["os"] = os;
globalState.LoadedModules["os"] = os;
}
public static void OpenStringLibrary(this LuaState state)
{
var globalState = state.GlobalState;
LuaTable @string = new(0, StringLibrary.Instance.Functions.Length);
foreach (var func in StringLibrary.Instance.Functions)
{
@string[func.Name] = func.Func;
}
globalState.Environment["string"] = @string;
globalState.LoadedModules["string"] = @string;
// set __index
LuaValue key = new("");
if (!globalState.TryGetMetatable(key, out var metatable))
{
metatable = new();
globalState.SetMetatable(key, metatable);
}
metatable[Metamethods.Index] = new LuaFunction(
"index",
(context, cancellationToken) =>
{
context.GetStringArgument(0);
var key = context.GetArgument(1);
return new(context.Return(@string[key]));
}
);
}
public static void OpenTableLibrary(this LuaState state)
{
var globalState = state.GlobalState;
LuaTable table = new(0, TableLibrary.Instance.Functions.Length);
foreach (var func in TableLibrary.Instance.Functions)
{
table[func.Name] = func.Func;
}
globalState.Environment["table"] = table;
globalState.LoadedModules["table"] = table;
}
public static void OpenDebugLibrary(this LuaState state)
{
var globalState = state.GlobalState;
LuaTable debug = new(0, DebugLibrary.Instance.Functions.Length);
foreach (var func in DebugLibrary.Instance.Functions)
{
debug[func.Name] = func.Func;
}
globalState.Environment["debug"] = debug;
globalState.LoadedModules["debug"] = debug;
}
public static void OpenStandardLibraries(this LuaState state)
{
state.OpenBasicLibrary();
state.OpenBitwiseLibrary();
state.OpenCoroutineLibrary();
state.OpenIOLibrary();
state.OpenMathLibrary();
state.OpenModuleLibrary();
state.OpenOperatingSystemLibrary();
state.OpenStringLibrary();
state.OpenTableLibrary();
state.OpenDebugLibrary();
}
}