Browse Source

Fix love.threaderror when the error message is empty.

Fixes #1775
Alex Szpakowski 3 years ago
parent
commit
e512eff37b

+ 15 - 4
src/modules/thread/LuaThread.cpp

@@ -36,6 +36,7 @@ love::Type LuaThread::type("Thread", &Threadable::type);
 LuaThread::LuaThread(const std::string &name, love::Data *code)
 LuaThread::LuaThread(const std::string &name, love::Data *code)
 	: code(code)
 	: code(code)
 	, name(name)
 	, name(name)
+	, haserror(false)
 {
 {
 	threadName = name;
 	threadName = name;
 }
 }
@@ -47,6 +48,7 @@ LuaThread::~LuaThread()
 void LuaThread::threadFunction()
 void LuaThread::threadFunction()
 {
 {
 	error.clear();
 	error.clear();
+	haserror = false;
 
 
 	lua_State *L = luaL_newstate();
 	lua_State *L = luaL_newstate();
 	luaL_openlibs(L);
 	luaL_openlibs(L);
@@ -70,7 +72,10 @@ void LuaThread::threadFunction()
 	int tracebackidx = lua_gettop(L);
 	int tracebackidx = lua_gettop(L);
 
 
 	if (luaL_loadbuffer(L, (const char *) code->getData(), code->getSize(), name.c_str()) != 0)
 	if (luaL_loadbuffer(L, (const char *) code->getData(), code->getSize(), name.c_str()) != 0)
+	{
 		error = luax_tostring(L, -1);
 		error = luax_tostring(L, -1);
+		haserror = true;
+	}
 	else
 	else
 	{
 	{
 		int pushedargs = (int) args.size();
 		int pushedargs = (int) args.size();
@@ -81,18 +86,27 @@ void LuaThread::threadFunction()
 		args.clear();
 		args.clear();
 
 
 		if (lua_pcall(L, pushedargs, 0, tracebackidx) != 0)
 		if (lua_pcall(L, pushedargs, 0, tracebackidx) != 0)
+		{
 			error = luax_tostring(L, -1);
 			error = luax_tostring(L, -1);
+			haserror = true;
+		}
 	}
 	}
 
 
 	lua_close(L);
 	lua_close(L);
 
 
-	if (!error.empty())
+	if (haserror)
 		onError();
 		onError();
 }
 }
 
 
 bool LuaThread::start(const std::vector<Variant> &args)
 bool LuaThread::start(const std::vector<Variant> &args)
 {
 {
+	if (isRunning())
+		return false;
+
 	this->args = args;
 	this->args = args;
+	error.clear();
+	haserror = false;
+	
 	return Threadable::start();
 	return Threadable::start();
 }
 }
 
 
@@ -103,9 +117,6 @@ const std::string &LuaThread::getError() const
 
 
 void LuaThread::onError()
 void LuaThread::onError()
 {
 {
-	if (error.empty())
-		return;
-
 	auto eventmodule = Module::getInstance<event::Event>(Module::M_EVENT);
 	auto eventmodule = Module::getInstance<event::Event>(Module::M_EVENT);
 	if (!eventmodule)
 	if (!eventmodule)
 		return;
 		return;

+ 2 - 0
src/modules/thread/LuaThread.h

@@ -46,6 +46,7 @@ public:
 	virtual ~LuaThread();
 	virtual ~LuaThread();
 	void threadFunction();
 	void threadFunction();
 	const std::string &getError() const;
 	const std::string &getError() const;
+	bool hasError() const { return haserror; }
 
 
 	bool start(const std::vector<Variant> &args);
 	bool start(const std::vector<Variant> &args);
 
 
@@ -56,6 +57,7 @@ private:
 	StrongRef<love::Data> code;
 	StrongRef<love::Data> code;
 	std::string name;
 	std::string name;
 	std::string error;
 	std::string error;
+	bool haserror;
 
 
 	std::vector<Variant> args;
 	std::vector<Variant> args;
 
 

+ 3 - 4
src/modules/thread/wrap_LuaThread.cpp

@@ -63,11 +63,10 @@ int w_Thread_wait(lua_State *L)
 int w_Thread_getError(lua_State *L)
 int w_Thread_getError(lua_State *L)
 {
 {
 	LuaThread *t = luax_checkthread(L, 1);
 	LuaThread *t = luax_checkthread(L, 1);
-	std::string err = t->getError();
-	if (err.empty())
-		lua_pushnil(L);
+	if (t->hasError())
+		luax_pushstring(L, t->getError());
 	else
 	else
-		luax_pushstring(L, err);
+		lua_pushnil(L);
 	return 1;
 	return 1;
 }
 }