From eee1fbac7963ffb1f00fbe9d4eb1310470a931d4 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Fri, 1 May 2015 21:20:23 -0300 Subject: [PATCH] love.filesystem is now loaded by default in threads (see issue #1040.) --- src/common/runtime.cpp | 18 ++++++++++++++---- src/common/runtime.h | 7 +++++++ src/modules/love/love.cpp | 2 +- src/modules/thread/LuaThread.cpp | 26 ++++++++++++++++++++------ 4 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/common/runtime.cpp b/src/common/runtime.cpp index 0765a4214..3482c89d4 100644 --- a/src/common/runtime.cpp +++ b/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 diff --git a/src/common/runtime.h b/src/common/runtime.h index ce852162f..e9ad9e64c 100644 --- a/src/common/runtime.h +++ b/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 diff --git a/src/modules/love/love.cpp b/src/modules/love/love.cpp index 1062f98ca..3d58195ed 100644 --- a/src/modules/love/love.cpp +++ b/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 diff --git a/src/modules/thread/LuaThread.cpp b/src/modules/thread/LuaThread.cpp index 80eaa6bd7..edef4964e 100644 --- a/src/modules/thread/LuaThread.cpp +++ b/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(); }