From 6eee2368beda573395e48b184280fbd2ebe63ef2 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Thu, 11 Dec 2014 21:22:12 -0400 Subject: [PATCH] Literal strings containing Lua code can be passed into love.thread.newThread directly, similar to loadstring and love.graphics.newShader (resolves issue #622.) Threads are still *very* heavyweight objects and should be treated as such, unlike the functions returned by loadstring. --- src/modules/thread/wrap_ThreadModule.cpp | 27 +++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/modules/thread/wrap_ThreadModule.cpp b/src/modules/thread/wrap_ThreadModule.cpp index 769867b42..d9d2896d6 100644 --- a/src/modules/thread/wrap_ThreadModule.cpp +++ b/src/modules/thread/wrap_ThreadModule.cpp @@ -27,6 +27,9 @@ #include "filesystem/File.h" #include "filesystem/FileData.h" +// C +#include + namespace love { namespace thread @@ -37,7 +40,29 @@ namespace thread int w_newThread(lua_State *L) { std::string name = "Thread code"; - love::Data *data = 0; + love::Data *data = nullptr; + + if (lua_isstring(L, 1)) + { + size_t slen = 0; + const char *str = lua_tolstring(L, 1, &slen); + + // Treat the string as Lua code if it's long or has a newline. + if (slen >= 1024 || memchr(str, '\n', slen)) + { + // Construct a FileData from the string. + lua_pushvalue(L, 1); + lua_pushstring(L, "string"); + int idxs[] = {lua_gettop(L) - 1, lua_gettop(L)}; + luax_convobj(L, idxs, 2, "filesystem", "newFileData"); + lua_pop(L, 1); + lua_replace(L, 1); + } + else + luax_convobj(L, 1, "filesystem", "newFileData"); + } + else if (luax_istype(L, 1, FILESYSTEM_FILE_T)) + luax_convobj(L, 1, "filesystem", "newFileData"); if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T)) luax_convobj(L, 1, "filesystem", "newFileData");