Browse Source

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.
Alex Szpakowski 10 years ago
parent
commit
6eee2368be
1 changed files with 26 additions and 1 deletions
  1. 26 1
      src/modules/thread/wrap_ThreadModule.cpp

+ 26 - 1
src/modules/thread/wrap_ThreadModule.cpp

@@ -27,6 +27,9 @@
 #include "filesystem/File.h"
 #include "filesystem/FileData.h"
 
+// C
+#include <cstring>
+
 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");