Skip to content

Commit e7ee54a

Browse files
committed
更新tolua#到1.0.7.355版
1 parent 8dae325 commit e7ee54a

44 files changed

Lines changed: 1117 additions & 714 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Assets/LuaFramework/Lua/3rd/debug.meta

Lines changed: 0 additions & 5 deletions
This file was deleted.

Assets/LuaFramework/Scripts/Manager/LuaManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ void InitLuaBundle() {
104104
}
105105

106106
public object[] DoFile(string filename) {
107-
return lua.DoFile(filename);
107+
return lua.DoFile<object[]>(filename);
108108
}
109109

110110
// Update is called once per frame

Assets/LuaFramework/ToLua/BaseType/LuaInterface_EventObjectWrap.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static int op_Subtraction(IntPtr L)
1919
try
2020
{
2121
EventObject arg0 = (EventObject)ToLua.CheckObject(L, 1, typeof(EventObject));
22-
arg0.func = ToLua.CheckLuaFunction(L, 2);
22+
arg0.func = ToLua.CheckDelegate(arg0.type, L, 2);
2323
arg0.op = EventOp.Sub;
2424
ToLua.Push(L, arg0);
2525
return 1;
@@ -36,7 +36,7 @@ static int op_Addition(IntPtr L)
3636
try
3737
{
3838
EventObject arg0 = (EventObject)ToLua.CheckObject(L, 1, typeof(EventObject));
39-
arg0.func = ToLua.CheckLuaFunction(L, 2);
39+
arg0.func = ToLua.CheckDelegate(arg0.type, L, 2);
4040
arg0.op = EventOp.Add;
4141
ToLua.Push(L, arg0);
4242
return 1;

Assets/LuaFramework/ToLua/Core/LuaDLL.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public string short_src
187187

188188
public class LuaDLL
189189
{
190-
public static string version = "1.0.7.350";
190+
public static string version = "1.0.7.356";
191191
public static int LUA_MULTRET = -1;
192192
public static string[] LuaTypeName = { "none", "nil", "boolean", "lightuserdata", "number", "string", "table", "function", "userdata", "thread" };
193193

Assets/LuaFramework/ToLua/Core/LuaMatchType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public bool CheckULong(IntPtr L, int pos)
5959
switch (luaType)
6060
{
6161
case LuaTypes.LUA_TNUMBER:
62-
return true;
62+
return LuaDLL.lua_tonumber(L, pos) >= 0;
6363
case LuaTypes.LUA_TUSERDATA:
6464
return LuaDLL.tolua_getvaluetype(L, pos) == LuaValueType.UInt64;
6565
default:

Assets/LuaFramework/ToLua/Core/LuaMisc.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -530,35 +530,29 @@ public class EventObject
530530
[NoToLuaAttribute]
531531
public EventOp op = EventOp.None;
532532
[NoToLuaAttribute]
533-
public LuaFunction func = null;
533+
public Delegate func = null;
534534
[NoToLuaAttribute]
535-
public string name = string.Empty;
535+
public Type type;
536536

537537
[NoToLuaAttribute]
538-
public EventObject(string name)
538+
public EventObject(Type t)
539539
{
540-
this.name = name;
540+
type = t;
541541
}
542542

543-
public static EventObject operator +(EventObject a, LuaFunction b)
543+
public static EventObject operator +(EventObject a, Delegate b)
544544
{
545545
a.op = EventOp.Add;
546546
a.func = b;
547547
return a;
548548
}
549549

550-
public static EventObject operator -(EventObject a, LuaFunction b)
550+
public static EventObject operator -(EventObject a, Delegate b)
551551
{
552552
a.op = EventOp.Sub;
553553
a.func = b;
554554
return a;
555555
}
556-
557-
[NoToLuaAttribute]
558-
public override string ToString()
559-
{
560-
return name;
561-
}
562556
}
563557
}
564558

Assets/LuaFramework/ToLua/Core/LuaState.cs

Lines changed: 67 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ public LuaState()
105105
mainState = this;
106106
}
107107

108-
float time = Time.realtimeSinceStartup;
108+
float time = Time.realtimeSinceStartup;
109109
InitTypeTraits();
110-
InitStackTraits();
111-
L = LuaNewState();
110+
InitStackTraits();
111+
L = LuaNewState();
112112
LuaException.Init(L);
113113
stateMap.Add(L, this);
114-
OpenToLuaLibs();
114+
OpenToLuaLibs();
115115
ToLua.OpenLibs(L);
116116
OpenBaseLibs();
117117
LuaSetTop(0);
@@ -561,7 +561,7 @@ public static LuaReflection GetReflection(IntPtr ptr)
561561
#endif
562562
}
563563

564-
public object[] DoString(string chunk, string chunkName = "LuaState.cs")
564+
public void DoString(string chunk, string chunkName = "LuaState.cs")
565565
{
566566
#if UNITY_EDITOR
567567
if (!beStart)
@@ -570,10 +570,41 @@ public object[] DoString(string chunk, string chunkName = "LuaState.cs")
570570
}
571571
#endif
572572
byte[] buffer = Encoding.UTF8.GetBytes(chunk);
573-
return LuaLoadBuffer(buffer, chunkName);
574-
}
573+
LuaLoadBuffer(buffer, chunkName);
574+
}
575+
576+
public T DoString<T>(string chunk, string chunkName = "LuaState.cs")
577+
{
578+
byte[] buffer = Encoding.UTF8.GetBytes(chunk);
579+
return LuaLoadBuffer<T>(buffer, chunkName);
580+
}
581+
582+
public void DoFile(string fileName)
583+
{
584+
#if UNITY_EDITOR
585+
if (!beStart)
586+
{
587+
throw new LuaException("you must call Start() first to initialize LuaState");
588+
}
589+
#endif
590+
byte[] buffer = LuaFileUtils.Instance.ReadFile(fileName);
591+
592+
if (buffer == null)
593+
{
594+
string error = string.Format("cannot open {0}: No such file or directory", fileName);
595+
error += LuaFileUtils.Instance.FindFileError(fileName);
596+
throw new LuaException(error);
597+
}
598+
599+
if (LuaConst.openLuaDebugger)
600+
{
601+
fileName = LuaFileUtils.Instance.FindFile(fileName);
602+
}
603+
604+
LuaLoadBuffer(buffer, fileName);
605+
}
575606

576-
public object[] DoFile(string fileName)
607+
public T DoFile<T>(string fileName)
577608
{
578609
#if UNITY_EDITOR
579610
if (!beStart)
@@ -595,7 +626,7 @@ public object[] DoFile(string fileName)
595626
fileName = LuaFileUtils.Instance.FindFile(fileName);
596627
}
597628

598-
return LuaLoadBuffer(buffer, fileName);
629+
return LuaLoadBuffer<T>(buffer, fileName);
599630
}
600631

601632
//注意fileName与lua文件中require一致。
@@ -1328,11 +1359,6 @@ public void Push(UnityEngine.TrackedReference tracker)
13281359
ToLua.Push(L, tracker);
13291360
}
13301361

1331-
public void PushValue<T>(T v) where T : struct
1332-
{
1333-
ToLua.PushValue<T>(L, v);
1334-
}
1335-
13361362
public void PushVariant(object obj)
13371363
{
13381364
ToLua.Push(L, obj);
@@ -1348,6 +1374,11 @@ public void PushSealed<T>(T o)
13481374
ToLua.PushSealed<T>(L, o);
13491375
}
13501376

1377+
public void PushValue<T>(T v) where T : struct
1378+
{
1379+
StackTraits<T>.Push(L, v);
1380+
}
1381+
13511382
public void PushGeneric<T>(T o)
13521383
{
13531384
StackTraits<T>.Push(L, o);
@@ -2065,7 +2096,26 @@ protected void Collect(int reference, string name, bool beThread)
20652096
}
20662097
}
20672098

2068-
protected object[] LuaLoadBuffer(byte[] buffer, string chunkName)
2099+
protected void LuaLoadBuffer(byte[] buffer, string chunkName)
2100+
{
2101+
LuaDLL.tolua_pushtraceback(L);
2102+
int oldTop = LuaGetTop();
2103+
2104+
if (LuaLoadBuffer(buffer, buffer.Length, chunkName) == 0)
2105+
{
2106+
if (LuaPCall(0, LuaDLL.LUA_MULTRET, oldTop) == 0)
2107+
{
2108+
LuaSetTop(oldTop - 1);
2109+
return;
2110+
}
2111+
}
2112+
2113+
string err = LuaToString(-1);
2114+
LuaSetTop(oldTop - 1);
2115+
throw new LuaException(err, LuaException.GetLastError());
2116+
}
2117+
2118+
protected T LuaLoadBuffer<T>(byte[] buffer, string chunkName)
20692119
{
20702120
LuaDLL.tolua_pushtraceback(L);
20712121
int oldTop = LuaGetTop();
@@ -2074,14 +2124,14 @@ protected object[] LuaLoadBuffer(byte[] buffer, string chunkName)
20742124
{
20752125
if (LuaPCall(0, LuaDLL.LUA_MULTRET, oldTop) == 0)
20762126
{
2077-
object[] result = CheckObjects(oldTop);
2127+
T result = CheckValue<T>(oldTop + 1);
20782128
LuaSetTop(oldTop - 1);
20792129
return result;
20802130
}
20812131
}
20822132

20832133
string err = LuaToString(-1);
2084-
LuaSetTop(oldTop - 1);
2134+
LuaSetTop(oldTop - 1);
20852135
throw new LuaException(err, LuaException.GetLastError());
20862136
}
20872137

Assets/LuaFramework/ToLua/Core/LuaStatePtr.cs

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,82 @@ public class LuaStatePtr
1010
{
1111
protected IntPtr L;
1212

13+
string jit = @"
14+
function Euler(x, y, z)
15+
x = x * 0.0087266462599716
16+
y = y * 0.0087266462599716
17+
z = z * 0.0087266462599716
18+
19+
local sinX = math.sin(x)
20+
local cosX = math.cos(x)
21+
local sinY = math.sin(y)
22+
local cosY = math.cos(y)
23+
local sinZ = math.sin(z)
24+
local cosZ = math.cos(z)
25+
26+
local w = cosY * cosX * cosZ + sinY * sinX * sinZ
27+
x = cosY* sinX * cosZ + sinY* cosX * sinZ
28+
y = sinY * cosX * cosZ - cosY * sinX * sinZ
29+
z = cosY* cosX * sinZ - sinY* sinX * cosZ
30+
31+
return {x = x, y = y, z= z, w = w}
32+
end
33+
34+
function Slerp(q1, q2, t)
35+
local x1, y1, z1, w1 = q1.x, q1.y, q1.z, q1.w
36+
local x2,y2,z2,w2 = q2.x, q2.y, q2.z, q2.w
37+
local dot = x1* x2 + y1* y2 + z1* z2 + w1* w2
38+
39+
if dot< 0 then
40+
dot = -dot
41+
x2, y2, z2, w2 = -x2, -y2, -z2, -w2
42+
end
43+
44+
if dot< 0.95 then
45+
local sin = math.sin
46+
local angle = math.acos(dot)
47+
local invSinAngle = 1 / sin(angle)
48+
local t1 = sin((1 - t) * angle) * invSinAngle
49+
local t2 = sin(t * angle) * invSinAngle
50+
return {x = x1* t1 + x2* t2, y = y1 * t1 + y2 * t2, z = z1 * t1 + z2 * t2, w = w1 * t1 + w2 * t2}
51+
else
52+
x1 = x1 + t* (x2 - x1)
53+
y1 = y1 + t* (y2 - y1)
54+
z1 = z1 + t* (z2 - z1)
55+
w1 = w1 + t* (w2 - w1)
56+
dot = x1* x1 + y1* y1 + z1* z1 + w1* w1
57+
58+
return {x = x1 / dot, y = y1 / dot, z = z1 / dot, w = w1 / dot}
59+
end
60+
end
61+
62+
if jit then
63+
if jit.status() then
64+
for i=1,10000 do
65+
local q1 = Euler(i, i, i)
66+
Slerp({ x = 0, y = 0, z = 0, w = 1}, q1, 0.5)
67+
end
68+
end
69+
end";
70+
1371
public int LuaUpValueIndex(int i)
1472
{
1573
return LuaIndexes.LUA_GLOBALSINDEX - i;
1674
}
1775

1876
public IntPtr LuaNewState()
19-
{
20-
return LuaDLL.luaL_newstate();
77+
{
78+
return LuaDLL.luaL_newstate();
79+
}
80+
81+
public void LuaOpenJit()
82+
{
83+
if (!LuaDLL.luaL_dostring(L, jit))
84+
{
85+
string str = LuaDLL.lua_tostring(L, -1);
86+
LuaDLL.lua_settop(L, 0);
87+
throw new Exception(str);
88+
}
2189
}
2290

2391
public void LuaClose()
@@ -556,6 +624,7 @@ public int LuaFixedUpdate(float fixedTime)
556624
public void OpenToLuaLibs()
557625
{
558626
LuaDLL.tolua_openlibs(L);
627+
LuaOpenJit();
559628
}
560629

561630
public void ToLuaPushTraceback()

0 commit comments

Comments
 (0)