-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathLuaBase.cs
More file actions
324 lines (268 loc) · 8.67 KB
/
Copy pathLuaBase.cs
File metadata and controls
324 lines (268 loc) · 8.67 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
318
319
320
321
322
323
324
// Disable warnings about XML documentation
#pragma warning disable 1591
using System;
using lua_Integer = System.Int32;
namespace MoonSharp.Interpreter.Interop.LuaStateInterop
{
/// <summary>
/// Classes using the classic interface should inherit from this class.
/// This class defines only static methods and is really meant to be used only
/// from C# and not other .NET languages.
///
/// For easier operation they should also define:
/// using ptrdiff_t = System.Int32;
/// using lua_Integer = System.Int32;
/// using LUA_INTFRM_T = System.Int64;
/// using UNSIGNED_LUA_INTFRM_T = System.UInt64;
/// </summary>
public partial class LuaBase
{
protected const int LUA_TNONE = -1;
protected const int LUA_TNIL = 0;
protected const int LUA_TBOOLEAN = 1;
protected const int LUA_TLIGHTUSERDATA = 2;
protected const int LUA_TNUMBER = 3;
protected const int LUA_TSTRING = 4;
protected const int LUA_TTABLE = 5;
protected const int LUA_TFUNCTION = 6;
protected const int LUA_TUSERDATA = 7;
protected const int LUA_TTHREAD = 8;
protected const int LUA_MULTRET = -1;
protected const string LUA_INTFRMLEN = "l";
protected static DynValue GetArgument(LuaState L, lua_Integer pos)
{
return L.At(pos);
}
protected static DynValue ArgAsType(LuaState L, lua_Integer pos, DataType type, bool allowNil = false)
{
return GetArgument(L, pos).CheckType(L.FunctionName, type, pos - 1, allowNil ? TypeValidationFlags.AllowNil | TypeValidationFlags.AutoConvert : TypeValidationFlags.AutoConvert);
}
protected static lua_Integer LuaType(LuaState L, lua_Integer p)
{
switch (GetArgument(L, p).Type)
{
case DataType.Void:
return LUA_TNONE;
case DataType.Nil:
return LUA_TNIL;
case DataType.Boolean:
return LUA_TNIL;
case DataType.Number:
return LUA_TNUMBER;
case DataType.String:
return LUA_TSTRING;
case DataType.Function:
return LUA_TFUNCTION;
case DataType.Table:
return LUA_TTABLE;
case DataType.UserData:
return LUA_TUSERDATA;
case DataType.Thread:
return LUA_TTHREAD;
case DataType.ClrFunction:
return LUA_TFUNCTION;
case DataType.TailCallRequest:
case DataType.YieldRequest:
case DataType.Tuple:
default:
throw new ScriptRuntimeException("Can't call LuaType on any type");
}
}
protected static string LuaLCheckLString(LuaState L, lua_Integer argNum, out uint l)
{
string str = ArgAsType(L, argNum, DataType.String, false).String;
l = (uint)str.Length;
return str;
}
protected static void LuaPushInteger(LuaState L, lua_Integer val)
{
L.Push(DynValue.NewNumber(val));
}
protected static lua_Integer LuaToBoolean(LuaState L, lua_Integer p)
{
return GetArgument(L, p).CastToBool() ? 1 : 0;
}
protected static string LuaToLString(LuaState luaState, lua_Integer p, out uint l)
{
return LuaLCheckLString(luaState, p, out l);
}
protected static string LuaToString(LuaState luaState, lua_Integer p)
{
uint l;
return LuaLCheckLString(luaState, p, out l);
}
protected static void LuaLAddValue(LuaLBuffer b)
{
b.StringBuilder.Append(b.LuaState.Pop().ToPrintString());
}
protected static void LuaLAddLString(LuaLBuffer b, CharPtr s, uint p)
{
b.StringBuilder.Append(s.ToString((int)p));
}
protected static void LuaLAddString(LuaLBuffer b, string s)
{
b.StringBuilder.Append(s.ToString());
}
protected static lua_Integer LuaLOptInteger(LuaState L, lua_Integer pos, lua_Integer def)
{
DynValue v = ArgAsType(L, pos, DataType.Number, true);
if (v.IsNil())
return def;
else
return (int)v.Number;
}
protected static lua_Integer LuaLCheckInteger(LuaState L, lua_Integer pos)
{
DynValue v = ArgAsType(L, pos, DataType.Number, false);
return (int)v.Number;
}
protected static void LuaLArgCheck(LuaState L, bool condition, lua_Integer argNum, string message)
{
if (!condition)
LuaLArgError(L, argNum, message);
}
protected static lua_Integer LuaLCheckInt(LuaState L, lua_Integer argNum)
{
return LuaLCheckInteger(L, argNum);
}
protected static lua_Integer LuaGetTop(LuaState L)
{
return L.Count;
}
protected static lua_Integer LuaLError(LuaState luaState, string message, params object[] args)
{
throw new ScriptRuntimeException(message, args);
}
protected static void LuaLAddChar(LuaLBuffer b, char p)
{
b.StringBuilder.Append(p);
}
protected static void LuaLBuffInit(LuaState L, LuaLBuffer b)
{
}
protected static void LuaPushLiteral(LuaState L, string literalString)
{
L.Push(DynValue.NewString(literalString));
}
protected static void LuaLPushResult(LuaLBuffer b)
{
LuaPushLiteral(b.LuaState, b.StringBuilder.ToString());
}
protected static void LuaPushLString(LuaState L, CharPtr s, uint len)
{
string ss = s.ToString((int)len);
L.Push(DynValue.NewString(ss));
}
protected static void LuaLCheckStack(LuaState L, lua_Integer n, string message)
{
// nop ?
}
protected static string LUA_QL(string p)
{
return "'" + p + "'";
}
protected static void LuaPushNil(LuaState L)
{
L.Push(DynValue.Nil);
}
protected static void LuaAssert(bool p)
{
// ??!
// A lot of KopiLua methods fall here in valid state!
//if (!p)
// throw new InternalErrorException("LuaAssert failed!");
}
protected static string LuaLTypeName(LuaState L, lua_Integer p)
{
return L.At(p).Type.ToErrorTypeString();
}
protected static lua_Integer LuaIsString(LuaState L, lua_Integer p)
{
var v = L.At(p);
return (v.Type == DataType.String || v.Type == DataType.Number) ? 1 : 0;
}
protected static void LuaPop(LuaState L, lua_Integer p)
{
for (int i = 0; i < p; i++)
L.Pop();
}
protected static void LuaGetTable(LuaState L, lua_Integer p)
{
// DEBT: this should call metamethods, now it performs raw access
DynValue key = L.Pop();
DynValue table = L.At(p);
if (table.Type != DataType.Table)
throw new NotImplementedException();
var v = table.Table.Get(key);
L.Push(v);
}
protected static int LuaLOptInt(LuaState L, lua_Integer pos, lua_Integer def)
{
return LuaLOptInteger(L, pos, def);
}
protected static CharPtr LuaLCheckString(LuaState L, lua_Integer p)
{
uint dummy;
return LuaLCheckLString(L, p, out dummy);
}
protected static string LuaLCheckStringStr(LuaState L, lua_Integer p)
{
uint dummy;
return LuaLCheckLString(L, p, out dummy);
}
protected static void LuaLArgError(LuaState L, lua_Integer arg, string p)
{
throw ScriptRuntimeException.BadArgument(arg - 1, L.FunctionName, p);
}
protected static double LuaLCheckNumber(LuaState L, lua_Integer pos)
{
DynValue v = ArgAsType(L, pos, DataType.Number, false);
return v.Number;
}
protected static void LuaPushValue(LuaState L, lua_Integer arg)
{
DynValue v = L.At(arg);
L.Push(v);
}
/// <summary>
/// Calls a function.
/// To call a function you must use the following protocol: first, the function to be called is pushed onto the stack; then,
/// the arguments to the function are pushed in direct order; that is, the first argument is pushed first. Finally you call
/// lua_call; nargs is the number of arguments that you pushed onto the stack. All arguments and the function value are
/// popped from the stack when the function is called. The function results are pushed onto the stack when the function
/// returns. The number of results is adjusted to nresults, unless nresults is LUA_MULTRET. In this case, all results from
/// the function are pushed. Lua takes care that the returned values fit into the stack space. The function results are
/// pushed onto the stack in direct order (the first result is pushed first), so that after the call the last result is on
/// the top of the stack.
/// </summary>
/// <param name="L">The LuaState</param>
/// <param name="nargs">The number of arguments.</param>
/// <param name="nresults">The number of expected results.</param>
/// <exception cref="System.NotImplementedException"></exception>
protected static void LuaCall(LuaState L, lua_Integer nargs, lua_Integer nresults = LUA_MULTRET)
{
DynValue[] args = L.GetTopArray(nargs);
L.Discard(nargs);
DynValue func = L.Pop();
DynValue ret = L.ExecutionContext.Call(func, args);
if (nresults != 0)
{
if (nresults == -1)
{
nresults = (ret.Type == DataType.Tuple) ? ret.Tuple.Length : 1;
}
DynValue[] vals = (ret.Type == DataType.Tuple) ? ret.Tuple : new DynValue[1] { ret };
int copied = 0;
for (int i = 0; i < vals.Length && copied < nresults; i++, copied++)
{
L.Push(vals[i]);
}
while (copied < nresults)
{
L.Push(DynValue.Nil);
copied++;
}
}
}
}
}