From e7ce5bfec812467b33b8eb35476746ce142b4502 Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Mon, 18 Jan 2016 20:40:18 +1000 Subject: Actually send compile messages back. Plus some refactoring of the script engine. --- src/LuaSL/LuaSL_LSL_tree.h | 4 --- src/LuaSL/LuaSL_compile.c | 17 ++------- src/LuaSL/LuaSL_main.c | 23 ++++-------- src/libraries/Runnr.c | 90 +++++++++++++++++++++++++++++++--------------- src/libraries/Runnr.h | 7 +++- 5 files changed, 78 insertions(+), 63 deletions(-) diff --git a/src/LuaSL/LuaSL_LSL_tree.h b/src/LuaSL/LuaSL_LSL_tree.h index e7c5e36..514443a 100644 --- a/src/LuaSL/LuaSL_LSL_tree.h +++ b/src/LuaSL/LuaSL_LSL_tree.h @@ -3,8 +3,6 @@ #define __LUASL_TREE_H__ #define LUASL_DEBUG 0 -#define COMPILE_THREADED 1 -#define COMPILE_OUTPUT 0 #define LUASL_DIFF_CHECK 0 #define LUASL_BAD_CHECK 0 @@ -408,8 +406,6 @@ typedef struct #endif -void finishMessage(LuaCompile *compiler, compileMessage *message, int type, int column, int line); - boolean compilerSetup(gameGlobals *ourGlobals); void compileLSL(LuaCompiler *compiler); void burnLeaf(void *data); diff --git a/src/LuaSL/LuaSL_compile.c b/src/LuaSL/LuaSL_compile.c index 13ebe1e..2b838e3 100644 --- a/src/LuaSL/LuaSL_compile.c +++ b/src/LuaSL/LuaSL_compile.c @@ -220,15 +220,6 @@ LSL_Script constants; int lowestToken = 999999; -void finishMessage(LuaCompile *compiler, compileMessage *message, int type, int column, int line) -{ - message->type = type; - message->column = column; - message->line = line; - if (type) - compiler->bugCount++; -} - static LSL_Leaf *newLeaf(LSL_Type type, LSL_Leaf *left, LSL_Leaf *right) { LSL_Leaf *leaf = calloc(1, sizeof(LSL_Leaf)); @@ -596,7 +587,7 @@ else if (right) printf("NOLEFT %s %s\n", lval->toKen->toKen, right->toKen->toKen); else printf("NOLEFT %s NORIGHT\n", lval->toKen->toKen); - printf("############################################################################## left\n"); + printf("############################################################################## left - column %i, line %i %s\n", lval->column, lval->line, compiler->compiler->file); #endif left->flags |= MF_WRAPFUNC; if (LSL_PARENTHESIS_OPEN == left->toKen->type) @@ -2222,7 +2213,7 @@ boolean compilerSetup(gameGlobals *ourGlobals) if (tokens) { char buf[PATH_MAX]; - LuaCompiler *compiler = calloc(1, sizeof(LuaCompiler)); + LuaCompiler *compiler; // Sort the token table. for (i = 0; LSL_Tokens[i].toKen != NULL; i++) @@ -2236,10 +2227,8 @@ boolean compilerSetup(gameGlobals *ourGlobals) snprintf(buf, sizeof(buf), "luajit -e 'require(\"LSL\").gimmeLSL()' > %s/constants.lsl", prefix_lib_get()); system(buf); snprintf(buf, sizeof(buf), "%s/constants.lsl", prefix_lib_get()); - compiler->file = strdup(buf); - compiler->SID = strdup("FAKE_SID"); + compiler = createCompiler("FAKE_SID", buf, (compileCb) compileLSL, NULL); compiler->doConstants = TRUE; - compiler->parser = (compileCb) compileLSL; compileScript(compiler, FALSE); return TRUE; diff --git a/src/LuaSL/LuaSL_main.c b/src/LuaSL/LuaSL_main.c index 664cdd1..b72a155 100644 --- a/src/LuaSL/LuaSL_main.c +++ b/src/LuaSL/LuaSL_main.c @@ -228,7 +228,7 @@ static Eina_Bool parser(void *data, Connection *connection, char *SID, char *com { char *temp; char *file; - LuaCompiler *compiler = calloc(1, sizeof(LuaCompiler)); + LuaCompiler *compiler; strcpy(buf, &command[8]); temp = buf; @@ -237,15 +237,10 @@ static Eina_Bool parser(void *data, Connection *connection, char *SID, char *com temp++; temp[0] = '\0'; - eina_clist_init(&(compiler->messages)); - compiler->file = strdup(file); - compiler->SID = strdup(SID); + compiler = createCompiler(SID, file, (compileCb) compileLSL, _compileCb); compiler->client = connection; - compiler->doConstants = FALSE; - compiler->parser = (compileCb) compileLSL; - compiler->cb = _compileCb; -PD("Compiling script %s", file); - compileScript(compiler, COMPILE_THREADED); + PI("Compiling script %s", file); + compileScript(compiler, TRUE); } else if (0 == strncmp(command, "run(", 4)) { @@ -266,7 +261,7 @@ PD("Compiling script %s", file); me = getScript(SID); if (me) { -PD("Running script %s", me->fileName); + PI("Running script %s", me->fileName); runScript(me); releaseScript(me); } @@ -311,13 +306,9 @@ int main(int argc, char **argv) // get the arguments passed in while (--argc > 0 && *++argv != '\0') { - LuaCompiler *compiler = calloc(1, sizeof(LuaCompiler)); + LuaCompiler *compiler; - eina_clist_init(&(compiler->messages)); - compiler->file = strdup(*argv); - compiler->SID = strdup("0"); - compiler->doConstants = FALSE; - compiler->parser = (compileCb) compileLSL; + compiler = createCompiler("0", *argv, (compileCb) compileLSL, _compileCbSingle); compiler->cb = _compileCbSingle; compileScript(compiler, FALSE); } diff --git a/src/libraries/Runnr.c b/src/libraries/Runnr.c index 04547b4..9324800 100644 --- a/src/libraries/Runnr.c +++ b/src/libraries/Runnr.c @@ -21,6 +21,14 @@ static const struct luaL_reg runnrFunctions[] = { NULL, NULL } }; +void finishMessage(LuaCompile *compiler, compileMessage *message, int type, int column, int line) +{ + message->type = type; + message->column = column; + message->line = line; + if (type) + compiler->bugCount++; +} void dumpStack(lua_State *L, int i) { @@ -201,7 +209,7 @@ static void _workerFunction(void *data, Ecore_Thread *thread) // The documentation is not clear on which thread is which inside and out, // but states that at least for some they are different. // So store the internal one as well. -#if THREADIT +#if COMPILE_THREADED s->me = thread; #endif @@ -289,7 +297,7 @@ static void _workerFunction(void *data, Ecore_Thread *thread) if (RUNNR_FINISHED == s->status) { //PD("_workerFunction() FINISHED %s", s->name); -#if THREADIT +#if COMPILE_THREADED ecore_thread_cancel(thread); #else _cancel(s, NULL); @@ -299,7 +307,7 @@ static void _workerFunction(void *data, Ecore_Thread *thread) { ;//PD("_workerFunction() WAIT %s", s->name); } -#if THREADIT +#if COMPILE_THREADED else if (RUNNR_READY == s->status) ecore_thread_reschedule(thread); #endif @@ -326,13 +334,13 @@ static void _cancel(void *data, Ecore_Thread *thread) //PD("^^^^^^^^^^^^^^^^^^^_del(, %s)", s->name); // TODO - Perhaps have our own deletion callback to pass back? releaseScript(s); -#if THREADIT +#if COMPILE_THREADED eina_lock_free(&s->mutex); #endif free(s); } -#if THREADIT +#if COMPILE_THREADED static void _end(void *data, Ecore_Thread *thread) { } @@ -348,7 +356,7 @@ static Eina_Bool _enterer(void *data) if ((RUNNR_WAIT == s->status) && (eina_clist_count(&s->messages))) { s->status = RUNNR_READY; -#if THREADIT +#if COMPILE_THREADED ecore_thread_feedback_run(_workerFunction, _notify, _end, _cancel, s, EINA_FALSE); #else _workerFunction(s, NULL); @@ -356,7 +364,7 @@ static Eina_Bool _enterer(void *data) } if ((RUNNR_RESET == s->status) || (RUNNR_READY == s->status) || (RUNNR_RUNNING == s->status) || (RUNNR_NOT_STARTED == s->status)) { -#if THREADIT +#if COMPILE_THREADED ecore_thread_feedback_run(_workerFunction, _notify, _end, _cancel, s, EINA_FALSE); #else _workerFunction(s, NULL); @@ -389,7 +397,7 @@ script *scriptAdd(char *file, char *SID, RunnrServerCb send2server, void *data) result->name = &result->fileName[strlen(prefix_data_get())]; sprintf(result->binName, "%s.lua.out", result->fileName); -#if THREADIT +#if COMPILE_THREADED eina_lock_new(&result->mutex); #endif eina_clist_init(&(result->messages)); @@ -400,6 +408,20 @@ script *scriptAdd(char *file, char *SID, RunnrServerCb send2server, void *data) return result; } +LuaCompiler *createCompiler(char *SID, char *file, compileCb parser, compileCb cb) +{ + LuaCompiler *compiler = calloc(1, sizeof(LuaCompiler)); + + eina_clist_init(&(compiler->messages)); + compiler->file = strdup(file); + compiler->SID = strdup(SID); + compiler->doConstants = FALSE; + compiler->parser = parser; + compiler->cb = cb; + + return compiler; +} + static int luaWriter(lua_State *L, const void* p, size_t sz, void* ud) { FILE *out = ud; @@ -441,15 +463,21 @@ static void _compileThread(void *data, Ecore_Thread *thread) err = luaL_loadfile(L, name); if (err) { - compiler->bugCount++; -#if COMPILE_OUTPUT + compileMessage *message; + if (LUA_ERRSYNTAX == err) - PI("Lua syntax error in %s: %s", name, lua_tostring(L, -1)); + message = addMessage(&(compiler->messages), sizeof(compileMessage), + "Lua syntax error in %s: %s", name, lua_tostring(L, -1)); else if (LUA_ERRFILE == err) - PE("Lua compile file error in %s: %s", name, lua_tostring(L, -1)); + message = addMessage(&(compiler->messages), sizeof(compileMessage), + "Lua compile file error in %s: %s", name, lua_tostring(L, -1)); else if (LUA_ERRMEM == err) - PC("Lua compile memory allocation error in %s: %s", name, lua_tostring(L, -1)); -#endif + message = addMessage(&(compiler->messages), sizeof(compileMessage), + "Lua compile memory allocation error in %s: %s", name, lua_tostring(L, -1)); + else + message = addMessage(&(compiler->messages), sizeof(compileMessage), + "Lua unknown error %d in %s.", err, name); + finishMessage(compiler, message, 1, 0, 0); } else { @@ -461,29 +489,33 @@ static void _compileThread(void *data, Ecore_Thread *thread) err = lua_dump(L, luaWriter, out); if (err) { - compiler->bugCount++; - PE("Lua compile file error writing to %s", name); + finishMessage(compiler, addMessage(&(compiler->messages), sizeof(compileMessage), + "Lua compile file error writing to %s", name), + 1, 0, 0); } fclose(out); } else { - compiler->bugCount++; - PE("CRITICAL! Unable to open file %s for writing!", name); + finishMessage(compiler, addMessage(&(compiler->messages), sizeof(compileMessage), + "CRITICAL! Unable to open file %s for writing!", name), + 1, 0, 0); } } } else if (!compiler->doConstants) { - compiler->bugCount++; - PC("Can't create a new Lua state!"); + finishMessage(compiler, addMessage(&(compiler->messages), sizeof(compileMessage), + "Can't create a new Lua state!"), + 1, 0, 0); } } else { - compiler->bugCount++; #if COMPILE_OUTPUT - PW("Nothing for Lua to compile!"); + finishMessage(compiler, addMessage(&(compiler->messages), sizeof(compileMessage), + "Nothing for Lua to compile!"), + 1, 0, 0); #endif } } @@ -493,9 +525,11 @@ static void _compileThread(void *data, Ecore_Thread *thread) // But with outputting to the console - 450 - 700 750 - 800 void compileScript(LuaCompiler *compiler, int threadIt) { +#if COMPILE_THREADED if (threadIt) ecore_thread_run(_compileThread, _compileEnd, _compileEnd, compiler); else +#endif { _compileThread(compiler, NULL); _compileEnd(compiler, NULL); @@ -505,11 +539,11 @@ void compileScript(LuaCompiler *compiler, int threadIt) // Assumes the scripts mutex is taken already. void runScript(script *s) { -#if THREADIT +#if COMPILE_THREADED if ((RUNNR_NOT_STARTED == s->status) || (RUNNR_FINISHED == s->status)) #endif { -#if THREADIT +#if COMPILE_THREADED ecore_thread_feedback_run(_workerFunction, _notify, _end, _cancel, s, EINA_FALSE); #else _workerFunction(s, NULL); @@ -542,7 +576,7 @@ script *getScript(char *SID) void takeScript(script *s) { -#if THREADIT +#if COMPILE_THREADED Eina_Lock_Result result = eina_lock_take(&s->mutex); if (EINA_LOCK_DEADLOCK == result) PE("Script %s IS DEADLOCKED!", s->name); if (EINA_LOCK_FAIL == result) PE("Script %s LOCK FAILED!", s->name); @@ -551,7 +585,7 @@ void takeScript(script *s) void releaseScript(script *s) { -#if THREADIT +#if COMPILE_THREADED eina_lock_release(&s->mutex); #endif } @@ -580,7 +614,7 @@ void send2script(const char *SID, const char *message) stat = s->status; s->status = RUNNR_READY; if (RUNNR_WAIT == stat) -#if THREADIT +#if COMPILE_THREADED ecore_thread_feedback_run(_workerFunction, _notify, _end, _cancel, s, EINA_FALSE); #else _workerFunction(s, NULL); @@ -616,7 +650,7 @@ static int _send(lua_State *L) else { takeScript(self); -#if THREADIT +#if COMPILE_THREADED ecore_thread_feedback(self->me, strdup(message)); #else _notify(self, NULL, strdup(message)); diff --git a/src/libraries/Runnr.h b/src/libraries/Runnr.h index b44dca4..b3b394c 100644 --- a/src/libraries/Runnr.h +++ b/src/libraries/Runnr.h @@ -18,6 +18,9 @@ // TODO - Should make this choosable at run time after more testing of Ecore_Thead. #define THREADIT 0 +#define COMPILE_THREADED 0 +#define COMPILE_OUTPUT 1 + typedef enum { RUNNR_COMPILING, @@ -56,7 +59,7 @@ typedef void (* RunnrServerCb)(script *me, const char *message); typedef struct _script { Eina_Clist node; -#if THREADIT +#if COMPILE_THREADED Eina_Lock mutex; Ecore_Thread *me; #endif @@ -83,7 +86,9 @@ typedef struct } scriptMessage; +void finishMessage(LuaCompile *compiler, compileMessage *message, int type, int column, int line); script *scriptAdd(char *file, char *SID, RunnrServerCb send2server, void *data); +LuaCompiler *createCompiler(char *SID, char *file, compileCb parser, compileCb cb); void compileScript(LuaCompiler *compiler, int threadIt); void runScript(script *me); void resetScript(script *me); -- cgit v1.1