Browse Source

Added love.threaderror event callback function (re issue #673)

Alex Szpakowski 12 years ago
parent
commit
2106ff2403
4 changed files with 43 additions and 3 deletions
  1. 29 3
      src/modules/thread/LuaThread.cpp
  2. 4 0
      src/modules/thread/LuaThread.h
  3. 3 0
      src/scripts/boot.lua
  4. 7 0
      src/scripts/boot.lua.h

+ 29 - 3
src/modules/thread/LuaThread.cpp

@@ -19,7 +19,8 @@
  **/
 
 #include "LuaThread.h"
-#include <common/config.h>
+#include "event/Event.h"
+#include "common/config.h"
 
 #ifdef LOVE_BUILD_STANDALONE
 extern "C" int luaopen_love(lua_State * L);
@@ -70,8 +71,7 @@ void LuaThread::threadFunction()
 			args[i]->toLua(L);
 			args[i]->release();
 		}
-		// Set both args and nargs to nil,
-		// prevents the deconstructor from
+		// Set both args and nargs to nil, prevents the deconstructor from
 		// accessing it again.
 		nargs = 0;
 		args = 0;
@@ -80,6 +80,8 @@ void LuaThread::threadFunction()
 			error = luax_tostring(L, -1);
 	}
 	lua_close(L);
+	if (!error.empty())
+		onError();
 	this->release();
 }
 
@@ -99,5 +101,29 @@ const std::string &LuaThread::getError() const
 	return error;
 }
 
+void LuaThread::onError()
+{
+	if (error.empty())
+		return;
+
+	// FIXME: We shouldn't specify any particular Event module implementation.
+	event::Event *event = (event::Event *) Module::getInstance("love.event.sdl");
+	if (!event)
+		return;
+
+	Proxy p;
+	p.flags = THREAD_THREAD_T;
+	p.data = this;
+
+	Variant *arg1 = new Variant(THREAD_THREAD_ID, &p);
+	Variant *arg2 = new Variant(error.c_str(), error.length());
+	event::Message *msg = new event::Message("threaderror", arg1, arg2);
+	arg1->release();
+	arg2->release();
+
+	event->push(msg);
+	msg->release();
+}
+
 } // thread
 } // love

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

@@ -37,6 +37,7 @@ namespace thread
 class LuaThread : public love::Object, public Threadable
 {
 public:
+
 	LuaThread(const std::string &name, love::Data *code);
 	~LuaThread();
 	void threadFunction();
@@ -45,6 +46,9 @@ public:
 	bool start(Variant **args, int nargs);
 
 private:
+
+	void onError();
+
 	love::Data *code;
 	std::string name;
 	std::string error;

+ 3 - 0
src/scripts/boot.lua

@@ -191,6 +191,9 @@ function love.createhandlers()
 		quit = function ()
 			return
 		end,
+		threaderror = function (t, err)
+			if love.threaderror then return love.threaderror(t, err) end
+		end,
 		resize = function(w, h)
 			local ow, oh, flags = love.window.getMode()
 			if flags.resizable then

+ 7 - 0
src/scripts/boot.lua.h

@@ -338,6 +338,13 @@ const unsigned char boot_lua[] =
 	0x28, 0x29, 0x0a,
 	0x09, 0x09, 0x09, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x0a,
 	0x09, 0x09, 0x65, 0x6e, 0x64, 0x2c, 0x0a,
+	0x09, 0x09, 0x74, 0x68, 0x72, 0x65, 0x61, 0x64, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x66, 0x75, 
+	0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x28, 0x74, 0x2c, 0x20, 0x65, 0x72, 0x72, 0x29, 0x0a,
+	0x09, 0x09, 0x09, 0x69, 0x66, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x74, 0x68, 0x72, 0x65, 0x61, 0x64, 0x65, 
+	0x72, 0x72, 0x6f, 0x72, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x20, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x6c, 
+	0x6f, 0x76, 0x65, 0x2e, 0x74, 0x68, 0x72, 0x65, 0x61, 0x64, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x28, 0x74, 0x2c, 
+	0x20, 0x65, 0x72, 0x72, 0x29, 0x20, 0x65, 0x6e, 0x64, 0x0a,
+	0x09, 0x09, 0x65, 0x6e, 0x64, 0x2c, 0x0a,
 	0x09, 0x09, 0x72, 0x65, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x3d, 0x20, 0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 
 	0x6e, 0x28, 0x77, 0x2c, 0x20, 0x68, 0x29, 0x0a,
 	0x09, 0x09, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x20, 0x6f, 0x77, 0x2c, 0x20, 0x6f, 0x68, 0x2c, 0x20, 0x66,