fix bug in lua coroutines which made error propagation get mixed up and possibly result in a bunch of other subtle bugs

This commit is contained in:
zeromus 2012-03-27 07:25:36 +00:00
parent d1b00e6d4d
commit 46a638ee96
5 changed files with 28 additions and 16 deletions

View File

@ -114,12 +114,12 @@ namespace BizHawk.MultiClient
public Lua SpawnCoroutine(string File)
{
LuaConsole Luas = new LuaConsole();
var t = lua.NewThread();
LuaRegister(t);
var main = t.LoadFile(File);
t.Push(main); //push main function on to stack for subsequent resuming
return t;
LuaConsole Luas = new LuaConsole();
var t = lua.NewThread();
LuaRegister(t);
var main = t.LoadFile(File);
t.Push(main); //push main function on to stack for subsequent resuming
return t;
}
private int LuaInt(object lua_arg)

Binary file not shown.

Binary file not shown.

View File

@ -836,13 +836,13 @@ namespace BizHawk.MultiClient
}
catch (Exception ex)
{
if (ex.ToString().Substring(0, 32) == "LuaInterface.LuaScriptException:" || ex.ToString().Substring(0, 26) == "LuaInterface.LuaException:")
{
s.Enabled = false;
AddText(ex.Message);
UpdateNumberOfScripts();
}
else MessageBox.Show(ex.ToString());
if (ex is LuaInterface.LuaScriptException || ex is LuaInterface.LuaException)
{
s.Enabled = false;
AddText(ex.ToString());
UpdateNumberOfScripts();
}
else MessageBox.Show(ex.ToString());
}
}
}

View File

@ -75,7 +75,16 @@ namespace LuaInterface
/// </summary>
object luaLock = new object();
public Lua()
internal Lua(bool newThread)
{
if (!newThread) init();
}
public Lua()
{
init();
}
void init()
{
luaState = LuaDLL.luaL_newstate(); // steffenj: Lua 5.1.1 API change (lua_open is gone)
//LuaDLL.luaopen_base(luaState); // steffenj: luaopen_* no longer used
@ -514,13 +523,15 @@ namespace LuaInterface
public int Resume(int narg)
{
int top = LuaDLL.lua_gettop(luaState);
int ret = LuaDLL.lua_resume(luaState, narg);
if (ret == 1 /*LUA_YIELD*/)
return 1; //yielded
if (ret == 0) //normal termination - what to do?
return 0;
//error. throw exception with error message (TBD - debug api to get call stack)
throw new LuaException(LuaDLL.lua_tostring(luaState, -1));
ThrowExceptionFromError(top);
return ret;
}
public void Yield(int nresults)
{
@ -529,7 +540,8 @@ namespace LuaInterface
public Lua NewThread()
{
var lua = new Lua();
var lua = new Lua(true);
lua.translator = translator;
lua.luaState = LuaDLL.lua_newthread(luaState);
return lua;
}