Browse 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 7 years ago
parent
commit
e6e1a81fd6
2 changed files with 46 additions and 48 deletions
  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"
 #endif
 
-#ifdef LOVE_ANDROID
-#include "common/android.h"
-extern "C" 
-{
-#include "luajit.h"
-}
-#endif
-
 #ifdef LOVE_WINDOWS
 extern "C"
 {
@@ -140,39 +132,6 @@ static int love_preload(lua_State *L, lua_CFunction f, const char *name)
 	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
 {
 	DONE_QUIT,
@@ -201,11 +160,6 @@ static DoneAction runlove(int argc, char **argv, int &retval)
 	lua_State *L = luaL_newstate();
 	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.
 	love_preload(L, luaopen_love, "love");
 
@@ -257,9 +211,7 @@ static DoneAction runlove(int argc, char **argv, int &retval)
 	lua_pushvalue(L, -2);
 	int stackpos = lua_gettop(L);
 	while (lua_resume(L, 0) == LUA_YIELD)
-	{
 		lua_pop(L, lua_gettop(L) - stackpos);
-	}
 
 	retval = 0;
 	DoneAction done = DONE_QUIT;

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

@@ -34,6 +34,14 @@
 #include <windows.h>
 #endif // LOVE_WINDOWS
 
+#ifdef LOVE_ANDROID
+#include <SDL.h>
+extern "C"
+{
+#include "luajit.h"
+}
+#endif // LOVE_ANDROID
+
 #ifdef LOVE_LEGENDARY_CONSOLE_IO_HACK
 #include <fcntl.h>
 #include <io.h>
@@ -202,6 +210,39 @@ int w__openConsole(lua_State *L);
 int w__setAccelerometerAsJoystick(lua_State *L);
 #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()
 {
 	// 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_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
 	lua_pushcfunction(L, w__openConsole);
 	lua_setfield(L, -2, "_openConsole");