/
LuaObject.cs
430 lines (374 loc) · 11.5 KB
/
LuaObject.cs
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// #define DEBUG_DUMMY_TVALUE_MODIFY
namespace UniLua
{
using System;
using System.Collections.Generic;
using ULDebug = UniLua.Tools.ULDebug;
using StkId = System.Int32;
public struct TValue
{
private const UInt64 CLOSURE_LUA = 0; // lua closure
private const UInt64 CLOSURE_CS = 1; // c# closure
private const UInt64 CLOSURE_LCS = 2; // light c# closure
private const UInt64 BOOLEAN_FALSE = 0;
private const UInt64 BOOLEAN_TRUE = 1;
public int Tt;
// public double NValue;
// public UInt64 UInt64Value;
public object OValue;
#if DEBUG_DUMMY_TVALUE_MODIFY
public bool Lock_;
#endif
// public override int GetHashCode()
// {
// return Tt.GetHashCode() ^ NValue.GetHashCode()
// ^ UInt64Value.GetHashCode()
// ^ (OValue != null ? OValue.GetHashCode() : 0x12345678);
// }
public override bool Equals(object o)
{
if (!(o is TValue)) return false;
return Equals((TValue)o);
}
public bool Equals(TValue o)
{
if (Tt != o.Tt)
{ return false; }
switch (Tt)
{
case (int)LuaType.LUA_TNIL: return true;
case (int)LuaType.LUA_TBOOLEAN: return BValue() == o.BValue();
case (int)LuaType.LUA_TNUMBER:
{
return NValue == o.NValue();
}
case (int)LuaType.LUA_TUINT64: return UInt64Value == o.UInt64Value;
case (int)LuaType.LUA_TSTRING: return SValue() == o.SValue();
default: return System.Object.ReferenceEquals(OValue, o.OValue);
}
}
public static bool operator ==(TValue lhs, TValue rhs)
{
return lhs.Equals(rhs);
}
public static bool operator !=(TValue lhs, TValue rhs)
{
return !lhs.Equals(rhs);
}
#if DEBUG_DUMMY_TVALUE_MODIFY
private void CheckLock() {
if(Lock_) {
UnityEngine.ULDebug.LogError("changing a lock value");
}
}
#endif
internal bool TtIsNil() { return Tt == (int)LuaType.LUA_TNIL; }
internal bool TtIsBoolean() { return Tt == (int)LuaType.LUA_TBOOLEAN; }
internal bool TtIsNumber() { return Tt == (int)LuaType.LUA_TNUMBER; }
internal bool TtIsUInt64() { return Tt == (int)LuaType.LUA_TUINT64; }
internal bool TtIsString() { return Tt == (int)LuaType.LUA_TSTRING; }
internal bool TtIsTable() { return Tt == (int)LuaType.LUA_TTABLE; }
internal bool TtIsFunction() { return Tt == (int)LuaType.LUA_TFUNCTION; }
internal bool TtIsThread() { return Tt == (int)LuaType.LUA_TTHREAD; }
internal bool ClIsLuaClosure() { return UInt64Value == CLOSURE_LUA; }
internal bool ClIsCsClosure() { return UInt64Value == CLOSURE_CS; }
internal bool ClIsLcsClosure() { return UInt64Value == CLOSURE_LCS; }
internal bool BValue() { return UInt64Value != BOOLEAN_FALSE; }
internal UInt64 UInt64Value() { return (UInt64)OValue; }
internal Double NValue() { return (Double)OValue; }
internal String SValue() { return (String)OValue; }
internal LuaTable HValue() { return OValue as LuaTable; }
internal LuaLClosureValue ClLValue() { return (LuaLClosureValue)OValue; }
internal LuaCsClosureValue ClCsValue() { return (LuaCsClosureValue)OValue; }
internal LuaUserDataValue RawUValue() { return OValue as LuaUserDataValue; }
internal void SetNilValue()
{
#if DEBUG_DUMMY_TVALUE_MODIFY
CheckLock();
#endif
Tt = (int)LuaType.LUA_TNIL;
OValue = null;
}
internal void SetBValue(bool v)
{
#if DEBUG_DUMMY_TVALUE_MODIFY
CheckLock();
#endif
Tt = (int)LuaType.LUA_TBOOLEAN;
OValue = v ? BOOLEAN_TRUE : BOOLEAN_FALSE;
}
internal void SetObj(ref TValue v)
{
#if DEBUG_DUMMY_TVALUE_MODIFY
CheckLock();
#endif
Tt = v.Tt;
OValue = v.OValue;
}
internal void SetNValue(Double v)
{
#if DEBUG_DUMMY_TVALUE_MODIFY
CheckLock();
#endif
Tt = (int)LuaType.LUA_TNUMBER;
OValue = v;
}
internal void SetUInt64Value(UInt64 v)
{
#if DEBUG_DUMMY_TVALUE_MODIFY
CheckLock();
#endif
Tt = (int)LuaType.LUA_TUINT64;
OValue = v;
}
internal void SetSValue(string v)
{
#if DEBUG_DUMMY_TVALUE_MODIFY
CheckLock();
#endif
Tt = (int)LuaType.LUA_TSTRING;
OValue = v;
}
internal void SetHValue(LuaTable v)
{
#if DEBUG_DUMMY_TVALUE_MODIFY
CheckLock();
#endif
Tt = (int)LuaType.LUA_TTABLE;
OValue = v;
}
internal void SetThValue(LuaState v)
{
#if DEBUG_DUMMY_TVALUE_MODIFY
CheckLock();
#endif
Tt = (int)LuaType.LUA_TTHREAD;
OValue = v;
}
internal void SetPValue(object v)
{
#if DEBUG_DUMMY_TVALUE_MODIFY
CheckLock();
#endif
Tt = (int)LuaType.LUA_TLIGHTUSERDATA;
OValue = v;
}
internal void SetClLValue(LuaLClosureValue v)
{
#if DEBUG_DUMMY_TVALUE_MODIFY
CheckLock();
#endif
Tt = (int)LuaType.LUA_TFUNCTION;
OValue = v;
}
internal void SetClCsValue(LuaCsClosureValue v)
{
#if DEBUG_DUMMY_TVALUE_MODIFY
CheckLock();
#endif
Tt = (int)LuaType.LUA_TFUNCTION;
OValue = v;
}
internal void SetClLcsValue(CSharpFunctionDelegate v)
{
#if DEBUG_DUMMY_TVALUE_MODIFY
CheckLock();
#endif
Tt = (int)LuaType.LUA_TFUNCTION;
OValue = v;
}
public override string ToString()
{
if (TtIsString())
{
return string.Format("(string, {0})", SValue());
}
else if (TtIsNumber())
{
return string.Format("(number, {0})", NValue);
}
else if (TtIsNil())
{
return "(nil)";
}
else
{
return string.Format("(type:{0})", Tt);
}
}
}
// public class StkId
// {
// public TValue V;
// private StkId[] List;
// public int Index { get; private set; }
// public void SetList(StkId[] list) { List = list; }
// public void SetIndex(int index) { Index = index; }
// public static StkId inc(ref StkId val)
// {
// var ret = val;
// val = val.List[val.Index + 1];
// return ret;
// }
// public override string ToString()
// {
// string detail;
// if (V.TtIsString())
// { detail = V.SValue().Replace("\n", "»"); }
// else
// { detail = "..."; }
// return string.Format("StkId - {0} - {1}", LuaState.TypeName((LuaType)V.Tt), detail);
// }
// }
public class LuaLClosureValue
{
public LuaProto Proto;
public LuaUpvalue[] Upvals;
public LuaLClosureValue(LuaProto p)
{
Proto = p;
Upvals = new LuaUpvalue[p.Upvalues.Count];
for (int i = 0; i < p.Upvalues.Count; ++i)
{ Upvals[i] = new LuaUpvalue(); }
}
}
public class LuaUserDataValue
{
public object Value;
public LuaTable MetaTable;
public int Length;
}
public class LocVar
{
public string VarName;
public int StartPc;
public int EndPc;
}
public class UpvalDesc
{
public string Name;
public int Index;
public bool InStack;
}
public class LuaProto
{
public List<Instruction> Code;
public List<StkId> K;
public List<LuaProto> P;
public List<UpvalDesc> Upvalues;
public int LineDefined;
public int LastLineDefined;
public int NumParams;
public bool IsVarArg;
public byte MaxStackSize;
public string Source;
public List<int> LineInfo;
public List<LocVar> LocVars;
public LuaProto()
{
Code = new List<Instruction>();
K = new List<StkId>();
P = new List<LuaProto>();
Upvalues = new List<UpvalDesc>();
LineInfo = new List<int>();
LocVars = new List<LocVar>();
}
public int GetFuncLine(int pc)
{
return (0 <= pc && pc < LineInfo.Count) ? LineInfo[pc] : 0;
}
}
public class LuaUpvalue
{
public StkId V;
public StkId Value;
public LuaUpvalue()
{
Value = new StkId();
Value.V.SetNilValue();
V = Value;
}
}
public class LuaCsClosureValue
{
public CSharpFunctionDelegate F;
public StkId[] Upvals;
public LuaCsClosureValue(CSharpFunctionDelegate f)
{
F = f;
}
public LuaCsClosureValue(CSharpFunctionDelegate f, int numUpvalues)
{
F = f;
Upvals = new StkId[numUpvalues];
for (int i = 0; i < numUpvalues; ++i)
{
var newItem = new StkId();
Upvals[i] = newItem;
newItem.SetList(Upvals);
newItem.SetIndex(i);
}
}
}
public partial class LuaState
{
internal static StkId TheNilValue;
public static bool O_Str2Decimal(string s, out double result)
{
result = 0.0;
if (s.Contains("n") || s.Contains("N")) // reject `inf' and `nan'
return false;
int pos = 0;
if (s.Contains("x") || s.Contains("X"))
result = Utl.StrX2Number(s, ref pos);
else
result = Utl.Str2Number(s, ref pos);
if (pos == 0)
return false; // nothing recognized
while (pos < s.Length && Char.IsWhiteSpace(s[pos])) ++pos;
return pos == s.Length; // OK if no trailing characters
}
private static double O_Arith(LuaOp op, double v1, double v2)
{
switch (op)
{
case LuaOp.LUA_OPADD: return v1 + v2;
case LuaOp.LUA_OPSUB: return v1 - v2;
case LuaOp.LUA_OPMUL: return v1 * v2;
case LuaOp.LUA_OPDIV: return v1 / v2;
case LuaOp.LUA_OPMOD: return v1 % v2;
case LuaOp.LUA_OPPOW: return Math.Pow(v1, v2);
case LuaOp.LUA_OPUNM: return -v1;
default: throw new System.NotImplementedException();
}
}
private bool IsFalse(ref TValue v)
{
if (v.TtIsNil())
return true;
if ((v.TtIsBoolean() && v.BValue() == false))
return true;
return false;
}
private bool ToString(ref TValue o)
{
if (o.TtIsString()) { return true; }
return V_ToString(ref o);
}
internal LuaLClosureValue GetCurrentLuaFunc(CallInfo ci)
{
if (ci.IsLua)
{
return Stack[ci.FuncIndex].V.ClLValue();
}
else return null;
}
internal int GetCurrentLine(CallInfo ci)
{
Utl.Assert(ci.IsLua);
var cl = Stack[ci.FuncIndex].V.ClLValue();
return cl.Proto.GetFuncLine(ci.CurrentPc);
}
}
}