Browse Source

love.filesystem is now loaded by default in threads (see issue #1040.)

Alex Szpakowski 10 years ago
parent
commit
eee1fbac79
4 changed files with 42 additions and 11 deletions
  1. 14 4
      src/common/runtime.cpp
  2. 7 0
      src/common/runtime.h
  3. 1 1
      src/modules/love/love.cpp
  4. 20 6
      src/modules/thread/LuaThread.cpp

+ 14 - 4
src/common/runtime.cpp

@@ -188,7 +188,7 @@ int luax_assert_nilerror(lua_State *L, int idx)
 
 void luax_setfuncs(lua_State *L, const luaL_Reg *l)
 {
-	if (l == 0)
+	if (l == nullptr)
 		return;
 
 	for (; l->name != nullptr; l++)
@@ -198,6 +198,14 @@ void luax_setfuncs(lua_State *L, const luaL_Reg *l)
 	}
 }
 
+int luax_require(lua_State *L, const char *name)
+{
+	lua_getglobal(L, "require");
+	lua_pushstring(L, name);
+	lua_call(L, 1, 1);
+	return 1;
+}
+
 int luax_register_module(lua_State *L, const WrappedModule &m)
 {
 	// Put a reference to the C++ module in Lua.
@@ -224,13 +232,15 @@ int luax_register_module(lua_State *L, const WrappedModule &m)
 	lua_newtable(L);
 
 	// Register all the functions.
-	if (m.functions != 0)
+	if (m.functions != nullptr)
 		luax_setfuncs(L, m.functions);
 
 	// Register types.
-	if (m.types != 0)
-		for (const lua_CFunction *t = m.types; *t != 0; t++)
+	if (m.types != nullptr)
+	{
+		for (const lua_CFunction *t = m.types; *t != nullptr; t++)
 			(*t)(L);
+	}
 
 	lua_pushvalue(L, -1);
 	lua_setfield(L, -3, m.name); // love.graphics = table

+ 7 - 0
src/common/runtime.h

@@ -222,6 +222,13 @@ int luax_assert_nilerror(lua_State *L, int idx);
  **/
 void luax_setfuncs(lua_State *L, const luaL_Reg *l);
 
+/**
+ * Loads a Lua module using the 'require' function. Leaves the return result on
+ * the stack.
+ * @param name The name of the module to require.
+ **/
+int luax_require(lua_State *L, const char *name);
+
 /**
  * Register a module in the love table. The love table will be created if it does not exist.
  * NOTE: The module-object is expected to have a +1 reference count before calling

+ 1 - 1
src/modules/love/love.cpp

@@ -293,7 +293,7 @@ int luaopen_love(lua_State * L)
 	lua_setfield(L, -2, "_os");
 
 	// Preload module loaders.
-	for (int i = 0; modules[i].name != 0; i++)
+	for (int i = 0; modules[i].name != nullptr; i++)
 		love::luax_preload(L, modules[i].func, modules[i].name);
 
 #ifdef LOVE_ENABLE_LUASOCKET

+ 20 - 6
src/modules/thread/LuaThread.cpp

@@ -25,7 +25,6 @@
 #ifdef LOVE_BUILD_STANDALONE
 extern "C" int luaopen_love(lua_State * L);
 #endif // LOVE_BUILD_STANDALONE
-extern "C" int luaopen_love_thread(lua_State *L);
 
 namespace love
 {
@@ -34,7 +33,7 @@ namespace thread
 LuaThread::LuaThread(const std::string &name, love::Data *code)
 	: code(code)
 	, name(name)
-	, args(0)
+	, args(nullptr)
 	, nargs(0)
 {
 	threadName = name;
@@ -52,13 +51,25 @@ void LuaThread::threadFunction()
 {
 	this->retain();
 	error.clear();
+
 	lua_State *L = luaL_newstate();
 	luaL_openlibs(L);
+
 #ifdef LOVE_BUILD_STANDALONE
-	love::luax_preload(L, luaopen_love, "love");
-	luaopen_love(L);
+	luax_preload(L, luaopen_love, "love");
+	luax_require(L, "love");
+	lua_pop(L, 1);
 #endif // LOVE_BUILD_STANDALONE
-	luaopen_love_thread(L);
+
+	luax_require(L, "love.thread");
+	lua_pop(L, 1);
+
+	// We load love.filesystem by default, since require still exists without it
+	// but won't load files from the proper paths. love.filesystem also must be
+	// loaded before using any love function that can take a filepath argument.
+	luax_require(L, "love.filesystem");
+	lua_pop(L, 1);
+
 	if (luaL_loadbuffer(L, (const char *) code->getData(), code->getSize(), name.c_str()) != 0)
 		error = luax_tostring(L, -1);
 	else
@@ -72,14 +83,17 @@ void LuaThread::threadFunction()
 		// Set both args and nargs to nil, prevents the deconstructor from
 		// accessing it again.
 		nargs = 0;
-		args = 0;
+		args = nullptr;
 
 		if (lua_pcall(L, pushedargs, 0, 0) != 0)
 			error = luax_tostring(L, -1);
 	}
+
 	lua_close(L);
+
 	if (!error.empty())
 		onError();
+
 	this->release();
 }