Parcourir la source

Move Android code for rerouting print and disabling JIT compilation to inside require("love") instead of the main executable Lua state, so it works in threads.

Alex Szpakowski il y a 7 ans
Parent
commit
e6e1a81fd6
2 fichiers modifiés avec 46 ajouts et 48 suppressions
  1. 0 48
      src/love.cpp
  2. 46 0
      src/modules/love/love.cpp

+ 0 - 48
src/love.cpp

@@ -44,14 +44,6 @@ extern "C" {
 #include "common/ios.h"
 #include "common/ios.h"
 #endif
 #endif
 
 
-#ifdef LOVE_ANDROID
-#include "common/android.h"
-extern "C" 
-{
-#include "luajit.h"
-}
-#endif
-
 #ifdef LOVE_WINDOWS
 #ifdef LOVE_WINDOWS
 extern "C"
 extern "C"
 {
 {
@@ -140,39 +132,6 @@ static int love_preload(lua_State *L, lua_CFunction f, const char *name)
 	return 0;
 	return 0;
 }
 }
 
 
-#ifdef LOVE_ANDROID
-static int l_print_sdl_log(lua_State *L)
-{
-	int nargs = lua_gettop(L);
-
-	lua_getglobal(L, "tostring");
-
-	std::string outstring;
-
-	for (int i = 1; i <= nargs; i++)
-	{
-		// Call tostring(arg) and leave the result on the top of the stack.
-		lua_pushvalue(L, -1);
-		lua_pushvalue(L, i);
-		lua_call(L, 1, 1);
-
-		const char *s = lua_tostring(L, -1);
-		if (s == nullptr)
-			return luaL_error(L, "'tostring' must return a string to 'print'");
-
-		if (i > 1)
-			outstring += "\t";
-
-		outstring += s;
-
-		lua_pop(L, 1); // Pop the result of tostring(arg).
-	}
-
-	SDL_Log("[LOVE] %s", outstring.c_str());
-	return 0;
-}
-#endif
-
 enum DoneAction
 enum DoneAction
 {
 {
 	DONE_QUIT,
 	DONE_QUIT,
@@ -201,11 +160,6 @@ static DoneAction runlove(int argc, char **argv, int &retval)
 	lua_State *L = luaL_newstate();
 	lua_State *L = luaL_newstate();
 	luaL_openlibs(L);
 	luaL_openlibs(L);
 
 
-#ifdef LOVE_ANDROID
-	luaJIT_setmode(L, 0, LUAJIT_MODE_ENGINE| LUAJIT_MODE_OFF);
-	lua_register(L, "print", l_print_sdl_log);
-#endif
-
 	// Add love to package.preload for easy requiring.
 	// Add love to package.preload for easy requiring.
 	love_preload(L, luaopen_love, "love");
 	love_preload(L, luaopen_love, "love");
 
 
@@ -257,9 +211,7 @@ static DoneAction runlove(int argc, char **argv, int &retval)
 	lua_pushvalue(L, -2);
 	lua_pushvalue(L, -2);
 	int stackpos = lua_gettop(L);
 	int stackpos = lua_gettop(L);
 	while (lua_resume(L, 0) == LUA_YIELD)
 	while (lua_resume(L, 0) == LUA_YIELD)
-	{
 		lua_pop(L, lua_gettop(L) - stackpos);
 		lua_pop(L, lua_gettop(L) - stackpos);
-	}
 
 
 	retval = 0;
 	retval = 0;
 	DoneAction done = DONE_QUIT;
 	DoneAction done = DONE_QUIT;

+ 46 - 0
src/modules/love/love.cpp

@@ -34,6 +34,14 @@
 #include <windows.h>
 #include <windows.h>
 #endif // LOVE_WINDOWS
 #endif // LOVE_WINDOWS
 
 
+#ifdef LOVE_ANDROID
+#include <SDL.h>
+extern "C"
+{
+#include "luajit.h"
+}
+#endif // LOVE_ANDROID
+
 #ifdef LOVE_LEGENDARY_CONSOLE_IO_HACK
 #ifdef LOVE_LEGENDARY_CONSOLE_IO_HACK
 #include <fcntl.h>
 #include <fcntl.h>
 #include <io.h>
 #include <io.h>
@@ -202,6 +210,39 @@ int w__openConsole(lua_State *L);
 int w__setAccelerometerAsJoystick(lua_State *L);
 int w__setAccelerometerAsJoystick(lua_State *L);
 #endif
 #endif
 
 
+#ifdef LOVE_ANDROID
+static int w_print_sdl_log(lua_State *L)
+{
+	int nargs = lua_gettop(L);
+
+	lua_getglobal(L, "tostring");
+
+	std::string outstring;
+
+	for (int i = 1; i <= nargs; i++)
+	{
+		// Call tostring(arg) and leave the result on the top of the stack.
+		lua_pushvalue(L, -1);
+		lua_pushvalue(L, i);
+		lua_call(L, 1, 1);
+
+		const char *s = lua_tostring(L, -1);
+		if (s == nullptr)
+			return luaL_error(L, "'tostring' must return a string to 'print'");
+
+		if (i > 1)
+			outstring += "\t";
+
+		outstring += s;
+
+		lua_pop(L, 1); // Pop the result of tostring(arg).
+	}
+
+	SDL_Log("[LOVE] %s", outstring.c_str());
+	return 0;
+}
+#endif
+
 const char *love_version()
 const char *love_version()
 {
 {
 	// Do not refer to love::VERSION here, the linker
 	// Do not refer to love::VERSION here, the linker
@@ -303,6 +344,11 @@ int luaopen_love(lua_State *L)
 	lua_pushstring(L, love::VERSION_CODENAME);
 	lua_pushstring(L, love::VERSION_CODENAME);
 	lua_setfield(L, -2, "_version_codename");
 	lua_setfield(L, -2, "_version_codename");
 
 
+#ifdef LOVE_ANDROID
+	luaJIT_setmode(L, 0, LUAJIT_MODE_ENGINE | LUAJIT_MODE_OFF);
+	lua_register(L, "print", w_print_sdl_log);
+#endif
+
 #ifdef LOVE_LEGENDARY_CONSOLE_IO_HACK
 #ifdef LOVE_LEGENDARY_CONSOLE_IO_HACK
 	lua_pushcfunction(L, w__openConsole);
 	lua_pushcfunction(L, w__openConsole);
 	lua_setfield(L, -2, "_openConsole");
 	lua_setfield(L, -2, "_openConsole");