Skip to content

Commit

Permalink
更新tolua#到1.0.7.355版
Browse files Browse the repository at this point in the history
  • Loading branch information
jarjin2000 committed Aug 7, 2017
1 parent 8dae325 commit fc2751f
Show file tree
Hide file tree
Showing 44 changed files with 1,118 additions and 715 deletions.
5 changes: 0 additions & 5 deletions Assets/LuaFramework/Lua/3rd/debug.meta

This file was deleted.

4 changes: 2 additions & 2 deletions Assets/LuaFramework/Scripts/Manager/LuaManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ void InitLuaBundle() {
}
}

public object[] DoFile(string filename) {
return lua.DoFile(filename);
public void DoFile(string filename) {
lua.DoFile(filename);
}

// Update is called once per frame
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static int op_Subtraction(IntPtr L)
try
{
EventObject arg0 = (EventObject)ToLua.CheckObject(L, 1, typeof(EventObject));
arg0.func = ToLua.CheckLuaFunction(L, 2);
arg0.func = ToLua.CheckDelegate(arg0.type, L, 2);
arg0.op = EventOp.Sub;
ToLua.Push(L, arg0);
return 1;
Expand All @@ -36,7 +36,7 @@ static int op_Addition(IntPtr L)
try
{
EventObject arg0 = (EventObject)ToLua.CheckObject(L, 1, typeof(EventObject));
arg0.func = ToLua.CheckLuaFunction(L, 2);
arg0.func = ToLua.CheckDelegate(arg0.type, L, 2);
arg0.op = EventOp.Add;
ToLua.Push(L, arg0);
return 1;
Expand Down
2 changes: 1 addition & 1 deletion Assets/LuaFramework/ToLua/Core/LuaDLL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public string short_src

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

Expand Down
2 changes: 1 addition & 1 deletion Assets/LuaFramework/ToLua/Core/LuaMatchType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public bool CheckULong(IntPtr L, int pos)
switch (luaType)
{
case LuaTypes.LUA_TNUMBER:
return true;
return LuaDLL.lua_tonumber(L, pos) >= 0;
case LuaTypes.LUA_TUSERDATA:
return LuaDLL.tolua_getvaluetype(L, pos) == LuaValueType.UInt64;
default:
Expand Down
18 changes: 6 additions & 12 deletions Assets/LuaFramework/ToLua/Core/LuaMisc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -530,35 +530,29 @@ public class EventObject
[NoToLuaAttribute]
public EventOp op = EventOp.None;
[NoToLuaAttribute]
public LuaFunction func = null;
public Delegate func = null;
[NoToLuaAttribute]
public string name = string.Empty;
public Type type;

[NoToLuaAttribute]
public EventObject(string name)
public EventObject(Type t)
{
this.name = name;
type = t;
}

public static EventObject operator +(EventObject a, LuaFunction b)
public static EventObject operator +(EventObject a, Delegate b)
{
a.op = EventOp.Add;
a.func = b;
return a;
}

public static EventObject operator -(EventObject a, LuaFunction b)
public static EventObject operator -(EventObject a, Delegate b)
{
a.op = EventOp.Sub;
a.func = b;
return a;
}

[NoToLuaAttribute]
public override string ToString()
{
return name;
}
}
}

84 changes: 67 additions & 17 deletions Assets/LuaFramework/ToLua/Core/LuaState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ public LuaState()
mainState = this;
}

float time = Time.realtimeSinceStartup;
float time = Time.realtimeSinceStartup;
InitTypeTraits();
InitStackTraits();
L = LuaNewState();
InitStackTraits();
L = LuaNewState();
LuaException.Init(L);
stateMap.Add(L, this);
OpenToLuaLibs();
OpenToLuaLibs();
ToLua.OpenLibs(L);
OpenBaseLibs();
LuaSetTop(0);
Expand Down Expand Up @@ -561,7 +561,7 @@ public static LuaReflection GetReflection(IntPtr ptr)
#endif
}

public object[] DoString(string chunk, string chunkName = "LuaState.cs")
public void DoString(string chunk, string chunkName = "LuaState.cs")
{
#if UNITY_EDITOR
if (!beStart)
Expand All @@ -570,10 +570,41 @@ public object[] DoString(string chunk, string chunkName = "LuaState.cs")
}
#endif
byte[] buffer = Encoding.UTF8.GetBytes(chunk);
return LuaLoadBuffer(buffer, chunkName);
}
LuaLoadBuffer(buffer, chunkName);
}

public T DoString<T>(string chunk, string chunkName = "LuaState.cs")
{
byte[] buffer = Encoding.UTF8.GetBytes(chunk);
return LuaLoadBuffer<T>(buffer, chunkName);
}

public void DoFile(string fileName)
{
#if UNITY_EDITOR
if (!beStart)
{
throw new LuaException("you must call Start() first to initialize LuaState");
}
#endif
byte[] buffer = LuaFileUtils.Instance.ReadFile(fileName);

if (buffer == null)
{
string error = string.Format("cannot open {0}: No such file or directory", fileName);
error += LuaFileUtils.Instance.FindFileError(fileName);
throw new LuaException(error);
}

if (LuaConst.openLuaDebugger)
{
fileName = LuaFileUtils.Instance.FindFile(fileName);
}

LuaLoadBuffer(buffer, fileName);
}

public object[] DoFile(string fileName)
public T DoFile<T>(string fileName)
{
#if UNITY_EDITOR
if (!beStart)
Expand All @@ -595,7 +626,7 @@ public object[] DoFile(string fileName)
fileName = LuaFileUtils.Instance.FindFile(fileName);
}

return LuaLoadBuffer(buffer, fileName);
return LuaLoadBuffer<T>(buffer, fileName);
}

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

public void PushValue<T>(T v) where T : struct
{
ToLua.PushValue<T>(L, v);
}

public void PushVariant(object obj)
{
ToLua.Push(L, obj);
Expand All @@ -1348,6 +1374,11 @@ public void PushSealed<T>(T o)
ToLua.PushSealed<T>(L, o);
}

public void PushValue<T>(T v) where T : struct
{
StackTraits<T>.Push(L, v);
}

public void PushGeneric<T>(T o)
{
StackTraits<T>.Push(L, o);
Expand Down Expand Up @@ -2065,7 +2096,26 @@ protected void Collect(int reference, string name, bool beThread)
}
}

protected object[] LuaLoadBuffer(byte[] buffer, string chunkName)
protected void LuaLoadBuffer(byte[] buffer, string chunkName)
{
LuaDLL.tolua_pushtraceback(L);
int oldTop = LuaGetTop();

if (LuaLoadBuffer(buffer, buffer.Length, chunkName) == 0)
{
if (LuaPCall(0, LuaDLL.LUA_MULTRET, oldTop) == 0)
{
LuaSetTop(oldTop - 1);
return;
}
}

string err = LuaToString(-1);
LuaSetTop(oldTop - 1);
throw new LuaException(err, LuaException.GetLastError());
}

protected T LuaLoadBuffer<T>(byte[] buffer, string chunkName)
{
LuaDLL.tolua_pushtraceback(L);
int oldTop = LuaGetTop();
Expand All @@ -2074,14 +2124,14 @@ protected object[] LuaLoadBuffer(byte[] buffer, string chunkName)
{
if (LuaPCall(0, LuaDLL.LUA_MULTRET, oldTop) == 0)
{
object[] result = CheckObjects(oldTop);
T result = CheckValue<T>(oldTop + 1);
LuaSetTop(oldTop - 1);
return result;
}
}

string err = LuaToString(-1);
LuaSetTop(oldTop - 1);
LuaSetTop(oldTop - 1);
throw new LuaException(err, LuaException.GetLastError());
}

Expand Down
73 changes: 71 additions & 2 deletions Assets/LuaFramework/ToLua/Core/LuaStatePtr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,82 @@ public class LuaStatePtr
{
protected IntPtr L;

string jit = @"
function Euler(x, y, z)
x = x * 0.0087266462599716
y = y * 0.0087266462599716
z = z * 0.0087266462599716
local sinX = math.sin(x)
local cosX = math.cos(x)
local sinY = math.sin(y)
local cosY = math.cos(y)
local sinZ = math.sin(z)
local cosZ = math.cos(z)
local w = cosY * cosX * cosZ + sinY * sinX * sinZ
x = cosY* sinX * cosZ + sinY* cosX * sinZ
y = sinY * cosX * cosZ - cosY * sinX * sinZ
z = cosY* cosX * sinZ - sinY* sinX * cosZ
return {x = x, y = y, z= z, w = w}
end
function Slerp(q1, q2, t)
local x1, y1, z1, w1 = q1.x, q1.y, q1.z, q1.w
local x2,y2,z2,w2 = q2.x, q2.y, q2.z, q2.w
local dot = x1* x2 + y1* y2 + z1* z2 + w1* w2
if dot< 0 then
dot = -dot
x2, y2, z2, w2 = -x2, -y2, -z2, -w2
end
if dot< 0.95 then
local sin = math.sin
local angle = math.acos(dot)
local invSinAngle = 1 / sin(angle)
local t1 = sin((1 - t) * angle) * invSinAngle
local t2 = sin(t * angle) * invSinAngle
return {x = x1* t1 + x2* t2, y = y1 * t1 + y2 * t2, z = z1 * t1 + z2 * t2, w = w1 * t1 + w2 * t2}
else
x1 = x1 + t* (x2 - x1)
y1 = y1 + t* (y2 - y1)
z1 = z1 + t* (z2 - z1)
w1 = w1 + t* (w2 - w1)
dot = x1* x1 + y1* y1 + z1* z1 + w1* w1
return {x = x1 / dot, y = y1 / dot, z = z1 / dot, w = w1 / dot}
end
end
if jit then
if jit.status() then
for i=1,10000 do
local q1 = Euler(i, i, i)
Slerp({ x = 0, y = 0, z = 0, w = 1}, q1, 0.5)
end
end
end";

public int LuaUpValueIndex(int i)
{
return LuaIndexes.LUA_GLOBALSINDEX - i;
}

public IntPtr LuaNewState()
{
return LuaDLL.luaL_newstate();
{
return LuaDLL.luaL_newstate();
}

public void LuaOpenJit()
{
if (!LuaDLL.luaL_dostring(L, jit))
{
string str = LuaDLL.lua_tostring(L, -1);
LuaDLL.lua_settop(L, 0);
throw new Exception(str);
}
}

public void LuaClose()
Expand Down Expand Up @@ -556,6 +624,7 @@ public int LuaFixedUpdate(float fixedTime)
public void OpenToLuaLibs()
{
LuaDLL.tolua_openlibs(L);
LuaOpenJit();
}

public void ToLuaPushTraceback()
Expand Down
Loading

0 comments on commit fc2751f

Please sign in to comment.