From b8ac69f1c3e21822b1d4e0713b2d77331f29c9aa Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Sun, 5 Feb 2012 13:09:13 +1000 Subject: Turn on the script running stuff, clean out the now excess tests, then move the runner to it's own file. --- LuaSL/build.sh | 2 +- LuaSL/src/LuaSL.h | 9 +- LuaSL/src/LuaSL_main.c | 245 +---------------------------------------------- LuaSL/src/LuaSL_runner.c | 160 +++++++++++++++++++++++++++++++ 4 files changed, 169 insertions(+), 247 deletions(-) create mode 100644 LuaSL/src/LuaSL_runner.c (limited to 'LuaSL') diff --git a/LuaSL/build.sh b/LuaSL/build.sh index 4433120..edddb31 100755 --- a/LuaSL/build.sh +++ b/LuaSL/build.sh @@ -63,7 +63,7 @@ command="edje_cc $EDJE_FLAGS LuaSL.edc ../LuaSL.edj" echo $command $command -names="LuaSL_main LuaSL_compile LuaSL_utilities LuaSL_lexer LuaSL_lemon_yaccer" +names="LuaSL_main LuaSL_compile LuaSL_runner LuaSL_utilities LuaSL_lexer LuaSL_lemon_yaccer" objects="../../libraries/luaproc/channel.o ../../libraries/luaproc/list.o ../../libraries/luaproc/luaproc.o ../../libraries/luaproc/sched.o " for i in $names do diff --git a/LuaSL/src/LuaSL.h b/LuaSL/src/LuaSL.h index e3b7065..c35b394 100644 --- a/LuaSL/src/LuaSL.h +++ b/LuaSL/src/LuaSL.h @@ -13,7 +13,7 @@ #include #include #include - + #define WIDTH (1024) #define HEIGHT (768) @@ -41,7 +41,7 @@ // NEVER change this typedef enum { - FALSE = 0, + FALSE = 0, TRUE = 1 } boolean; #endif @@ -60,9 +60,12 @@ typedef struct boolean compilerSetup(gameGlobals *game); boolean compileLSL(gameGlobals *game, char *script, boolean doConstants); +void runnerSetup(gameGlobals *game); +void runnerTearDown(gameGlobals *game); +void runLuaFile(gameGlobals *game, const char *filename); + void loggingStartup(gameGlobals *game); char *getDateTime(struct tm **nowOut, char *dateOut, time_t *tiemOut); float timeDiff(struct timeval *now, struct timeval *then); #include "LuaSL_LSL_tree.h" - diff --git a/LuaSL/src/LuaSL_main.c b/LuaSL/src/LuaSL_main.c index 5601dde..247b360 100644 --- a/LuaSL/src/LuaSL_main.c +++ b/LuaSL/src/LuaSL_main.c @@ -1,7 +1,6 @@ #include "LuaSL.h" -#define LUA_TEST 0 static int scriptCount; @@ -84,188 +83,13 @@ static void dirList_cb(const char *name, const char *path, void *data) scriptCount++; snprintf(buf, sizeof(buf), "%s/%s", path, name); if (compileLSL(game, buf, FALSE)) - PD("Against all odds, the compile of %s worked! lol", buf); + PD("The compile of %s worked,", buf); else PE("The compile of %s failed!", buf); } } } -#if LUA_TEST -static void dirListLua_cb(const char *name, const char *path, void *data) -{ - char buf[PATH_MAX]; - char *ext = rindex(name, '.'); - - if (ext) - { - if (0 == strcmp(ext, ".lua")) - { - scriptCount++; - snprintf(buf, sizeof(buf), "luac %s/%s 2>/dev/null", path, name); - system(buf); - } - } -} - -#ifdef _WIN32 -# define FMT_SIZE_T "%Iu" -#else -# define FMT_SIZE_T "%zu" -#endif - -#define MAX_LUA_MEM (4 * (1024 * 1024)) - -#define _edje_lua2_error(L, err_code) _edje_lua2_error_full(__FILE__, __FUNCTION__, __LINE__, L, err_code) - -/* -typedef struct _Edje_Lua_Alloc Edje_Lua_Alloc; - -struct _Edje_Lua_Alloc -{ - size_t max, cur; -}; - -static void * -_elua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) -{ - Edje_Lua_Alloc *ela = ud; - void *ptr2 = NULL; - - if (ela) - { - ela->cur += nsize - osize; - if (ela->cur > ela->max) - { - printf("Lua memory limit of " FMT_SIZE_T " bytes reached (" FMT_SIZE_T " allocated)", ela->max, ela->cur); - } - else if (nsize == 0) - { - free(ptr); - } - else - { - ptr2 = realloc(ptr, nsize); - if (NULL == ptr2) - printf("Lua cannot re-allocate " FMT_SIZE_T " bytes", nsize); - } - } - else - printf("Lua cannoct allocate memory, no Edje_Lua_Alloc"); - - return ptr2; -} - -static int panics = 0; -static int -_elua_custom_panic(lua_State *L) // Stack usage [-0, +0, m] -{ - // If we somehow manage to have multiple panics, it's likely due to being out - // of memory in the following lua_tostring() call. - panics++; - if (panics) - { - printf("Lua PANICS!!!!!"); - } - else - { - printf("Lua PANIC!!!!!: %s", lua_tostring(L, -1)); // Stack usage [-0, +0, m] - } - // The docs say that this will cause an exit(EXIT_FAILURE) if we return, - // and that we we should long jump some where to avoid that. This is only - // called for things not called from a protected environment. We always - // use pcalls though, except for the library load calls. If we can't load - // the standard libraries, then perhaps a crash is the right thing. - return 0; -} - -static void -_edje_lua2_error_full(const char *file, const char *fnc, int line, - lua_State *L, int err_code) // Stack usage [-0, +0, m] -{ - const char *err_type; - - switch (err_code) - { - case LUA_ERRRUN: - err_type = "runtime"; - break; - case LUA_ERRSYNTAX: - err_type = "syntax"; - break; - case LUA_ERRMEM: - err_type = "memory allocation"; - break; - case LUA_ERRERR: - err_type = "error handler"; - break; - default: - err_type = "unknown"; - break; - } - printf("Lua %s error: %s\n", err_type, lua_tostring(L, -1)); // Stack usage [-0, +0, m] -} - -static int errFunc(lua_State *L) -{ - int i = 0; - lua_Debug ar; - - while (lua_getstack(L, i++, &ar)) - { - if (lua_getinfo(L, "nSlu", &ar)) - { - if (NULL == ar.name) - ar.name = "DUNNO"; - printf("Lua error in the %s %s %s @ line %d in %s\n%s!", ar.what, ar.namewhat, ar.name, ar.currentline, ar.short_src, ar.source); - } - } - return 0; -} -*/ - -static void runnerSetup(gameGlobals *game) -{ - luaprocInit(); - - if ( sched_create_worker( ) != LUAPROC_SCHED_OK ) - PE("Error creating luaproc worker thread."); - if ( sched_create_worker( ) != LUAPROC_SCHED_OK ) - PE("Error creating luaproc worker thread."); - if ( sched_create_worker( ) != LUAPROC_SCHED_OK ) - PE("Error creating luaproc worker thread."); - if ( sched_create_worker( ) != LUAPROC_SCHED_OK ) - PE("Error creating luaproc worker thread."); -} - -static void runnerTearDown(gameGlobals *game) -{ -// TODO - this is what hangs the system. - sched_join_workerthreads(); -} - -static void runLuaFile(gameGlobals *game, const char *filename) -{ - newProc(filename, TRUE); - -// TODO, should set up our panic and errfunc as below. Plus the other TODO stuff. - -/* -// TODO - hack up LuaJIT so that we can limit memory per state. -// lua_setallocf(L, _elua_alloc, &ela); // LuaJIT uses a heavily hacked up dlmalloc. Seems that standard realloc is not so thread safe? - lua_atpanic(L, _elua_custom_panic); -// TODO - Sandbox out what this opens. See lib_init.c from LuaJIT. -// Just noticed this in the LuaJIT docs - "To change or extend the list of standard libraries to load, copy src/lib_init.c to your project and modify it accordingly. Make sure the jit library is loaded or the JIT compiler will not be activated." - luaL_openlibs(L); - - lua_pushcfunction(L, errFunc); - ... - if ((err = lua_pcall(L, 0, 0, -2))) - _edje_lua2_error(L, err); -*/ -} - -#endif int main(int argc, char **argv) @@ -329,10 +153,6 @@ main(int argc, char **argv) struct timeval lastTime2; struct timeval thisTime2; float diff; -#if LUA_TEST - unsigned int lslCount; - float diff0; -#endif if (game.ui) { @@ -405,71 +225,14 @@ main(int argc, char **argv) // Do the compiles. scriptCount = 0; - gettimeofday(&lastTime2, 0); compilerSetup(&game); -#if LUA_TEST runnerSetup(&game); -#endif + gettimeofday(&lastTime2, 0); snprintf(buf, sizeof(buf), "%s/Test sim/objects", PACKAGE_DATA_DIR); eina_file_dir_list(buf, EINA_TRUE, dirList_cb, &game); diff = timeDiff(&thisTime2, &lastTime2); printf("Compiling %d LSL scripts took %f seconds, that's %f scripts per second.\n", scriptCount, diff, scriptCount / diff); -#if LUA_TEST - lslCount = scriptCount; - diff0 = diff; - scriptCount = 0; - gettimeofday(&lastTime2, 0); - snprintf(buf, sizeof(buf), "%s/testLua", PACKAGE_DATA_DIR); - eina_file_dir_list(buf, EINA_TRUE, dirListLua_cb, &game); - diff = timeDiff(&thisTime2, &lastTime2); - printf("Compiling %d Lua scripts took %f seconds, that's %f scripts per second.\n\n", scriptCount, diff, scriptCount / diff); - - printf("Combined estimate of compiling speed is %f scripts per second.\n", 1 / ((diff0 / lslCount) + (diff / scriptCount))); - - gettimeofday(&lastTime2, 0); - snprintf(buf, sizeof(buf), "lua luaprocTest0.lua"); - system(buf); - diff = timeDiff(&thisTime2, &lastTime2); - printf("%s TOOK %f seconds......................................................................................................\n", buf, diff); - - gettimeofday(&lastTime2, 0); - snprintf(buf, sizeof(buf), "%s/testLua/luaprocTest0_C.lua", PACKAGE_DATA_DIR); - runLuaFile(&game, buf); - diff = timeDiff(&thisTime2, &lastTime2); - printf("Running that last one locally TOOK %f seconds.\n",diff); - - gettimeofday(&lastTime2, 0); - snprintf(buf, sizeof(buf), "lua luaprocTest1.lua"); - system(buf); - diff = timeDiff(&thisTime2, &lastTime2); - printf("%s TOOK %f seconds.\n", buf, diff); - - gettimeofday(&lastTime2, 0); - snprintf(buf, sizeof(buf), "../../libraries/luajit-2.0/src/luajit luaprocTest1.lua"); - system(buf); - diff = timeDiff(&thisTime2, &lastTime2); - printf("%s TOOK %f seconds.\n", buf, diff); - - gettimeofday(&lastTime2, 0); - snprintf(buf, sizeof(buf), "lua luaprocTest2.lua"); - system(buf); - diff = timeDiff(&thisTime2, &lastTime2); - printf("%s TOOK %f seconds.\n", buf, diff); - - gettimeofday(&lastTime2, 0); - snprintf(buf, sizeof(buf), "../../libraries/luajit-2.0/src/luajit luaprocTest2.lua"); - system(buf); - diff = timeDiff(&thisTime2, &lastTime2); - printf("%s TOOK %f seconds.\n", buf, diff); - - gettimeofday(&lastTime2, 0); - snprintf(buf, sizeof(buf), "%s/testLua/luaprocTest2_C.lua", PACKAGE_DATA_DIR); - runLuaFile(&game, buf); - diff = timeDiff(&thisTime2, &lastTime2); - printf("Running that last one locally TOOK %f seconds.\n",diff); -#endif - if (game.ui) { ecore_main_loop_begin(); @@ -477,11 +240,7 @@ main(int argc, char **argv) ecore_evas_free(game.ee); } -#if LUA_TEST runnerTearDown(&game); - diff = timeDiff(&thisTime2, &lastTime2); - printf("Running that last one locally TOOK %f seconds.\n",diff); -#endif edje_shutdown(); ecore_evas_shutdown(); } diff --git a/LuaSL/src/LuaSL_runner.c b/LuaSL/src/LuaSL_runner.c new file mode 100644 index 0000000..411c437 --- /dev/null +++ b/LuaSL/src/LuaSL_runner.c @@ -0,0 +1,160 @@ + +#include "LuaSL.h" + + +#ifdef _WIN32 +# define FMT_SIZE_T "%Iu" +#else +# define FMT_SIZE_T "%zu" +#endif + +#define MAX_LUA_MEM (4 * (1024 * 1024)) + +#define _edje_lua2_error(L, err_code) _edje_lua2_error_full(__FILE__, __FUNCTION__, __LINE__, L, err_code) + +/* +typedef struct _Edje_Lua_Alloc Edje_Lua_Alloc; + +struct _Edje_Lua_Alloc +{ + size_t max, cur; +}; + +static void * +_elua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) +{ + Edje_Lua_Alloc *ela = ud; + void *ptr2 = NULL; + + if (ela) + { + ela->cur += nsize - osize; + if (ela->cur > ela->max) + { + printf("Lua memory limit of " FMT_SIZE_T " bytes reached (" FMT_SIZE_T " allocated)", ela->max, ela->cur); + } + else if (nsize == 0) + { + free(ptr); + } + else + { + ptr2 = realloc(ptr, nsize); + if (NULL == ptr2) + printf("Lua cannot re-allocate " FMT_SIZE_T " bytes", nsize); + } + } + else + printf("Lua cannoct allocate memory, no Edje_Lua_Alloc"); + + return ptr2; +} + +static int panics = 0; +static int +_elua_custom_panic(lua_State *L) // Stack usage [-0, +0, m] +{ + // If we somehow manage to have multiple panics, it's likely due to being out + // of memory in the following lua_tostring() call. + panics++; + if (panics) + { + printf("Lua PANICS!!!!!"); + } + else + { + printf("Lua PANIC!!!!!: %s", lua_tostring(L, -1)); // Stack usage [-0, +0, m] + } + // The docs say that this will cause an exit(EXIT_FAILURE) if we return, + // and that we we should long jump some where to avoid that. This is only + // called for things not called from a protected environment. We always + // use pcalls though, except for the library load calls. If we can't load + // the standard libraries, then perhaps a crash is the right thing. + return 0; +} + +static void +_edje_lua2_error_full(const char *file, const char *fnc, int line, + lua_State *L, int err_code) // Stack usage [-0, +0, m] +{ + const char *err_type; + + switch (err_code) + { + case LUA_ERRRUN: + err_type = "runtime"; + break; + case LUA_ERRSYNTAX: + err_type = "syntax"; + break; + case LUA_ERRMEM: + err_type = "memory allocation"; + break; + case LUA_ERRERR: + err_type = "error handler"; + break; + default: + err_type = "unknown"; + break; + } + printf("Lua %s error: %s\n", err_type, lua_tostring(L, -1)); // Stack usage [-0, +0, m] +} + +static int errFunc(lua_State *L) +{ + int i = 0; + lua_Debug ar; + + while (lua_getstack(L, i++, &ar)) + { + if (lua_getinfo(L, "nSlu", &ar)) + { + if (NULL == ar.name) + ar.name = "DUNNO"; + printf("Lua error in the %s %s %s @ line %d in %s\n%s!", ar.what, ar.namewhat, ar.name, ar.currentline, ar.short_src, ar.source); + } + } + return 0; +} +*/ + +void runnerSetup(gameGlobals *game) +{ + luaprocInit(); + + if ( sched_create_worker( ) != LUAPROC_SCHED_OK ) + PE("Error creating luaproc worker thread."); + if ( sched_create_worker( ) != LUAPROC_SCHED_OK ) + PE("Error creating luaproc worker thread."); + if ( sched_create_worker( ) != LUAPROC_SCHED_OK ) + PE("Error creating luaproc worker thread."); + if ( sched_create_worker( ) != LUAPROC_SCHED_OK ) + PE("Error creating luaproc worker thread."); +} + +void runnerTearDown(gameGlobals *game) +{ +// TODO - this is what hangs the system. + sched_join_workerthreads(); +} + +void runLuaFile(gameGlobals *game, const char *filename) +{ + newProc(filename, TRUE); + +// TODO, should set up our panic and errfunc as below. Plus the other TODO stuff. + +/* +// TODO - hack up LuaJIT so that we can limit memory per state. +// lua_setallocf(L, _elua_alloc, &ela); // LuaJIT uses a heavily hacked up dlmalloc. Seems that standard realloc is not so thread safe? + lua_atpanic(L, _elua_custom_panic); +// TODO - Sandbox out what this opens. See lib_init.c from LuaJIT. +// Just noticed this in the LuaJIT docs - "To change or extend the list of standard libraries to load, copy src/lib_init.c to your project and modify it accordingly. Make sure the jit library is loaded or the JIT compiler will not be activated." + luaL_openlibs(L); + + lua_pushcfunction(L, errFunc); + ... + if ((err = lua_pcall(L, 0, 0, -2))) + _edje_lua2_error(L, err); +*/ +} -- cgit v1.1