Просмотр исходного кода

PolycodePlayer no longer exits on errors in even callbacks

Ivan Safrin 13 лет назад
Родитель
Сommit
542c04eeb0

+ 6 - 1
Bindings/Scripts/create_lua_library/create_lua_library.py

@@ -59,7 +59,12 @@ def createLUABindings(inputPath, prefix, mainInclude, libSmallName, libName, api
 		out += "		lua_getfield(L, -1, \"__handleEvent\");\n"
 		out += "		lua_rawgeti( L, LUA_REGISTRYINDEX, wrapperIndex );\n"
 		out += "		lua_pushlightuserdata(L, e);\n"
-		out += "		lua_call(L, 2, 0);\n"
+		out += "		if(lua_pcall(L, 2, 0, 0) != 0) {\n"
+		out += "			const char *msg = lua_tostring(L, -1);\n"
+		out += "			lua_getfield(L, LUA_GLOBALSINDEX, \"__customError\");\n"
+		out += "			lua_pushstring(L, msg);\n"
+		out += "			lua_call(L, 1, 0);\n"
+		out += "		}\n"
 		out += "	}\n"
 		out += "	int wrapperIndex;\n"
 		out += "	lua_State *L;\n"

+ 24 - 2
Player/Contents/Source/PolycodePlayer.cpp

@@ -61,6 +61,28 @@ extern "C" {
 		}
 		return 1;
 	}
+
+	static int customError(lua_State *L) {
+		const char *msg = lua_tostring(L, 1);
+		
+		if (msg == NULL) msg = "(error with no message)";
+		lua_pop(L, 1);
+			
+		std::vector<String> info = String(msg).split(":");
+			
+		PolycodeDebugEvent *event = new PolycodeDebugEvent();			
+		if(info.size() > 2) {
+			event->errorString = info[2];
+			event->lineNumber = atoi(info[1].c_str());
+		} else {
+			event->errorString = std::string(msg);
+			event->lineNumber = 0;
+		}
+		PolycodePlayer *player = (PolycodePlayer*)CoreServices::getInstance()->getCore()->getUserPointer();		
+		player->dispatchEvent(event, PolycodeDebugEvent::EVENT_ERROR);
+				
+		return 0;
+	}
 	
 	static int debugPrint(lua_State *L)
 	{
@@ -143,7 +165,8 @@ extern "C" {
 		// Table is still on the stack.  Get rid of it now.
 		lua_pop(L, 1);		
 		
-		lua_register(L, "debugPrint", debugPrint);			
+		lua_register(L, "debugPrint", debugPrint);	
+		lua_register(L, "__customError", customError);					
 		
 		lua_getfield(L, LUA_GLOBALSINDEX, "require");
 		lua_pushstring(L, "class");		
@@ -239,7 +262,6 @@ extern "C" {
 		lua_gettable(L, -2);
 
 */				
-		
 		//CoreServices::getInstance()->getCore()->lockMutex(CoreServices::getRenderMutex());			
 		if (report(L, luaL_loadstring(L, fullScript.c_str()) || lua_pcall(L, 0,0,0))) {