Skip to content

Commit fe23f35

Browse files
committed
fix Yield crash on lua5.3
1 parent ee0cbbb commit fe23f35

4 files changed

Lines changed: 35 additions & 38 deletions

File tree

Assets/Slua/Script/Coroutine.cs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,30 @@ public class LuaCoroutine : LuaObject
3636
static public void reg(IntPtr l, MonoBehaviour m)
3737
{
3838
mb = m;
39-
reg(l, Yield, "UnityEngine");
39+
reg(l, Yieldk, "UnityEngine");
40+
41+
string yield =
42+
@"
43+
local Yield = UnityEngine.Yieldk
44+
UnityEngine.Yield = function(x)
45+
local co,ismain=coroutine.running()
46+
if ismain then error('Can not yield in main thread') end
47+
48+
Yield(x,function()
49+
coroutine.resume(co)
50+
end)
51+
coroutine.yield()
52+
end
53+
return yield
54+
";
55+
// overload resume function for report error
56+
if(LuaDLL.lua_dostring(l, yield)!=0)
57+
LuaObject.throwLuaError(l);
58+
LuaDLL.lua_pop(l, 1);
4059
}
4160

4261
[MonoPInvokeCallback(typeof(LuaCSFunction))]
43-
static public int Yield(IntPtr l)
62+
static public int Yieldk(IntPtr l)
4463
{
4564
try
4665
{
@@ -50,28 +69,11 @@ static public int Yield(IntPtr l)
5069
return 0;
5170
}
5271
object y = checkObj(l, 1);
72+
LuaFunction f;
73+
checkType(l, 2, out f);
5374

54-
Action act = () =>
55-
{
56-
#if LUA_5_3
57-
if(LuaDLL.lua_resume(l,IntPtr.Zero,0) > (int) LuaThreadStatus.LUA_YIELD )
58-
#else
59-
if (LuaDLL.lua_resume(l, 0) > (int) LuaThreadStatus.LUA_YIELD )
60-
#endif
61-
{
62-
LuaObject.pushTry(l);
63-
LuaDLL.lua_pushvalue(l, -2);
64-
LuaDLL.lua_call(l, 1, 0);
65-
LuaDLL.lua_pop(l, 1);
66-
}
67-
};
68-
69-
mb.StartCoroutine(yieldReturn(y, act));
70-
#if LUA_5_3
71-
return LuaDLL.luaS_yield(l, 0);
72-
#else
73-
return LuaDLL.lua_yield(l, 0);
74-
#endif
75+
mb.StartCoroutine(yieldReturn(y, f));
76+
return 0;
7577
}
7678
catch (Exception e)
7779
{
@@ -80,13 +82,13 @@ static public int Yield(IntPtr l)
8082
}
8183
}
8284

83-
static public IEnumerator yieldReturn(object y, Action act)
85+
static public IEnumerator yieldReturn(object y, LuaFunction f)
8486
{
8587
if (y is IEnumerator)
8688
yield return mb.StartCoroutine((IEnumerator)y);
8789
else
8890
yield return y;
89-
act();
91+
f.call();
9092
}
9193

9294
}

Assets/Slua/Script/LuaDLL.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,11 @@ public static void lua_remove(IntPtr l, int idx)
291291
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
292292
public static extern int luaS_yield(IntPtr luaState,int nrets);
293293

294+
public static int lua_yield(IntPtr luaState,int nrets) {
295+
return luaS_yield(luaState,nrets);
296+
}
297+
298+
294299
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
295300
public static extern int lua_resume(IntPtr L, IntPtr from, int narg);
296301

Assets/Slua/Script/LuaState.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -500,25 +500,16 @@ public LuaState()
500500
LuaDLL.lua_pushcfunction(L, import);
501501
LuaDLL.lua_setglobal(L, "import");
502502

503-
#if LUA_5_3
504-
string resumefunc = @"
505-
local resume = coroutine.resume
506-
coroutine.resume=function(co,...)
507-
local ret={resume(co,...)}
508-
if not ret[1] then UnityEngine.Debug.LogError(debug.traceback(co,ret[2])) end
509-
return table.unpack(ret)
510-
end
511-
";
512-
#else
503+
513504
string resumefunc = @"
514505
local resume = coroutine.resume
506+
local unpack = unpack or table.unpack
515507
coroutine.resume=function(co,...)
516508
local ret={resume(co,...)}
517509
if not ret[1] then UnityEngine.Debug.LogError(debug.traceback(co,ret[2])) end
518510
return unpack(ret)
519511
end
520512
";
521-
#endif
522513
// overload resume function for report error
523514
if(LuaDLL.lua_dostring(L, resumefunc)!=0)
524515
LuaObject.throwLuaError(L);

Assets/slua_src/slua.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ static int k(lua_State *L, int status, lua_KContext ctx) {
132132
}
133133

134134
LUA_API int luaS_yield(lua_State *L, int nrets) {
135-
int ret = lua_yieldk(L, nrets, 0, k);
136-
return ret;
135+
return k(L, lua_yieldk(L, nrets, 0, k), 0);
137136
}
138137

139138
LUA_API int luaS_pcall(lua_State *L, int nargs, int nresults, int err) {

0 commit comments

Comments
 (0)