From 3236320311f15640083c5bf420bcdeab725ff242 Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Wed, 25 Jan 2012 02:29:35 +1000 Subject: Hack together LuaSL, LuaJIT, and luaproc. It's frankenstein right now. lol --- LuaSL/build.sh | 13 ++- LuaSL/src/LuaSL_LSL_tree.h | 7 ++ LuaSL/src/LuaSL_main.c | 185 +++++++++++++++++++++++++++++++++++++-- LuaSL/testLua/luaprocTest0.lua | 7 +- LuaSL/testLua/luaprocTest0_C.lua | 37 ++++++++ LuaSL/testLua/luaprocTest1_C.lua | 28 ++++++ LuaSL/testLua/luaprocTest2_C.lua | 65 ++++++++++++++ 7 files changed, 331 insertions(+), 11 deletions(-) create mode 100644 LuaSL/testLua/luaprocTest0_C.lua create mode 100644 LuaSL/testLua/luaprocTest1_C.lua create mode 100644 LuaSL/testLua/luaprocTest2_C.lua (limited to 'LuaSL') diff --git a/LuaSL/build.sh b/LuaSL/build.sh index 15e1d52..4433120 100755 --- a/LuaSL/build.sh +++ b/LuaSL/build.sh @@ -18,6 +18,9 @@ fi # No need for a make file, or dependencies, the entire thing takes only a few seconds to build. CFLAGS="-g -Wall -I include -I $LOCALDIR/src" +CFLAGS="$CFLAGS -I ../../libraries" +CFLAGS="$CFLAGS -I ../../libraries/luajit-2.0/src" +#CFLAGS="$CFLAGS -I /usr/include/lua5.1" CFLAGS="$CFLAGS -I $E17DIR/include/eina-1" CFLAGS="$CFLAGS -I $E17DIR/include/eina-1/eina" CFLAGS="$CFLAGS -I $E17DIR/include/eet-1" @@ -28,12 +31,13 @@ CFLAGS="$CFLAGS -I $E17DIR/include/ecore-1" CFLAGS="$CFLAGS -I $E17DIR/include" CFLAGS="$CFLAGS -DPACKAGE_DATA_DIR=\"$LOCALDIR\" $CFLAGOPTS" -LDFLAGS="-L lib -L /usr/lib -L /lib -L $E17DIR/lib -L ../../libraries/luajit-2.0/src" -libs="-lecore -levas -ledje -lembryo -leet -leina -lluajit -lpthread" +LDFLAGS="-L ../../libraries/luajit-2.0/src -L lib -L /usr/lib -L /lib -L $E17DIR/lib" +libs="-lecore -levas -ledje -lembryo -leet -leina -lluajit -lpthread -lm" +#LDFLAGS="-L /usr/lib/lua/5.1 -L lib -L /usr/lib -L /lib -L $E17DIR/lib" +#libs="-lecore -levas -ledje -lembryo -leet -leina -llua5.1 -lpthread -lm" # These need to be added to libs if linking staticaly, though some parts of EFL don't like that. #-lecore_evas \ #-lecore_file \ -#-lm \ #-ldl \ #-lfontconfig \ #-lfreetype \ @@ -43,6 +47,7 @@ libs="-lecore -levas -ledje -lembryo -leet -leina -lluajit -lpthread" LFLAGS="-d" EDJE_FLAGS="-id images -fd fonts" +LD_RUN_PATH="../../libraries/luajit-2.0/src:" # Run lemon first, flex depends on it to define the symbol values. @@ -59,7 +64,7 @@ echo $command $command names="LuaSL_main LuaSL_compile LuaSL_utilities LuaSL_lexer LuaSL_lemon_yaccer" -objects="" +objects="../../libraries/luaproc/channel.o ../../libraries/luaproc/list.o ../../libraries/luaproc/luaproc.o ../../libraries/luaproc/sched.o " for i in $names do command="gcc $CFLAGS -c -o $i.o $i.c" diff --git a/LuaSL/src/LuaSL_LSL_tree.h b/LuaSL/src/LuaSL_LSL_tree.h index 687c56c..20277b9 100644 --- a/LuaSL/src/LuaSL_LSL_tree.h +++ b/LuaSL/src/LuaSL_LSL_tree.h @@ -20,6 +20,13 @@ #include #include // For PATH_MAX. +#include +#include +#include +#include + +#include + #include "LuaSL_lemon_yaccer.h" #define YYERRCODE 256 diff --git a/LuaSL/src/LuaSL_main.c b/LuaSL/src/LuaSL_main.c index 3f20460..f2f4f17 100644 --- a/LuaSL/src/LuaSL_main.c +++ b/LuaSL/src/LuaSL_main.c @@ -106,6 +106,163 @@ static void dirListLua_cb(const char *name, const char *path, void *data) } } } + +#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. + luaL_openlibs(L); + + lua_pushcfunction(L, errFunc); + ... + if ((err = lua_pcall(L, 0, 0, -2))) + _edje_lua2_error(L, err); +*/ +} + #endif int @@ -248,6 +405,7 @@ main(int argc, char **argv) scriptCount = 0; gettimeofday(&lastTime2, 0); compilerSetup(&game); + runnerSetup(&game); 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); @@ -266,35 +424,46 @@ main(int argc, char **argv) printf("Combined estimate of compiling speed is %f scripts per second.\n", 1 / ((diff0 / lslCount) + (diff / scriptCount))); gettimeofday(&lastTime2, 0); - snprintf(buf, sizeof(buf), "cd %s/testLua; LUA_SOPATH='../../libraries/luaproc/' lua luaprocTest0.lua", PACKAGE_DATA_DIR); + snprintf(buf, sizeof(buf), "lua luaprocTest0.lua"); system(buf); diff = timeDiff(&thisTime2, &lastTime2); - printf("%s TOOK %f seconds.\n", buf, diff); + printf("%s TOOK %f seconds......................................................................................................\n", buf, diff); gettimeofday(&lastTime2, 0); - snprintf(buf, sizeof(buf), "cd %s/testLua; LUA_SOPATH='../../libraries/luaproc/' lua luaprocTest1.lua", PACKAGE_DATA_DIR); + 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), "cd %s/testLua; LUA_SOPATH='../../libraries/luaproc/' luajit luaprocTest1.lua", PACKAGE_DATA_DIR); + 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), "cd %s/testLua; LUA_SOPATH='../../libraries/luaproc/' lua luaprocTest2.lua", PACKAGE_DATA_DIR); + 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), "cd %s/testLua; LUA_SOPATH='../../libraries/luaproc/' luajit luaprocTest2.lua", PACKAGE_DATA_DIR); + 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) @@ -303,6 +472,10 @@ main(int argc, char **argv) ecore_animator_del(ani); ecore_evas_free(game.ee); } + + runnerTearDown(&game); + diff = timeDiff(&thisTime2, &lastTime2); + printf("Running that last one locally TOOK %f seconds.\n",diff); edje_shutdown(); ecore_evas_shutdown(); } diff --git a/LuaSL/testLua/luaprocTest0.lua b/LuaSL/testLua/luaprocTest0.lua index 5868d6f..94a1d20 100644 --- a/LuaSL/testLua/luaprocTest0.lua +++ b/LuaSL/testLua/luaprocTest0.lua @@ -33,7 +33,12 @@ luaproc.createworker() luaproc.newproc( [=[ luaproc.newchannel( "testchannel" ) luaproc.newproc( "luaproc.send( 'testchannel', 'luaproc is working fine!' )" ) - luaproc.newproc( "print( luaproc.receive( 'testchannel' ))" ) + luaproc.newproc( "print(luaproc.receive( 'testchannel'))" ) + print("End of parent proc") ]=] ) +print("About to luaproc.exit()!"); luaproc.exit() +print("Should have exited after the luaproc.exit()!"); + + diff --git a/LuaSL/testLua/luaprocTest0_C.lua b/LuaSL/testLua/luaprocTest0_C.lua new file mode 100644 index 0000000..0dd6078 --- /dev/null +++ b/LuaSL/testLua/luaprocTest0_C.lua @@ -0,0 +1,37 @@ +---------------------------------------------------- +-- +-- Copyright 2008 Alexandre Skyrme, Noemi Rodriguez, Roberto Ierusalimschy +-- +-- Permission is hereby granted, free of charge, to any person obtaining a copy +-- of this software and associated documentation files (the "Software"), to deal +-- in the Software without restriction, including without limitation the rights +-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +-- copies of the Software, and to permit persons to whom the Software is +-- furnished to do so, subject to the following conditions: +-- +-- The above copyright notice and this permission notice shall be included in +-- all copies or substantial portions of the Software. +-- +-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +-- THE SOFTWARE. +-- +----------------------------------------------------- +-- +-- test.lua +-- +----------------------------------------------------- + +luaproc.newproc( [=[ + luaproc.newchannel( "testchannel" ) + luaproc.newproc( "luaproc.send( 'testchannel', 'luaproc is working fine!' )" ) + luaproc.newproc( "print(luaproc.receive( 'testchannel'))" ) + print("End of parent proc") +]=] ) + +print("About to exit."); + diff --git a/LuaSL/testLua/luaprocTest1_C.lua b/LuaSL/testLua/luaprocTest1_C.lua new file mode 100644 index 0000000..61cb461 --- /dev/null +++ b/LuaSL/testLua/luaprocTest1_C.lua @@ -0,0 +1,28 @@ +result, error_msg = luaproc.newproc( [=[ + + local count = 0 + +-- Hmm, luajit 2 beta 9 crashes at about 8140, lua can go up to 50000 at least. + for i = 1, 8000 do + local channel = "channel" .. i +-- local proc = 'print(luaproc.receive("channel' .. i .. '") .. " " .. ' .. i ..')' + local proc = 'luaproc.receive("' .. channel .. '")' + +-- print(channel .. "\n" .. proc) + result, error_msg = luaproc.newchannel(channel) + if not result then print(error_msg) end + result, error_msg = luaproc.newproc(proc) + if not result then print(error_msg) else count = count + 1 end + end + + print("Started " .. count .. " Lua threads") + + if 0 ~= count then + for i = 1, count do + result, error_msg = luaproc.send("channel" .. i, 'luaproc is working fine! ' .. i) + if not result then print(error_msg .. " " .. i) end + end + end +]=] ) +if not result then print(error_msg .. " for main proc") end + diff --git a/LuaSL/testLua/luaprocTest2_C.lua b/LuaSL/testLua/luaprocTest2_C.lua new file mode 100644 index 0000000..e1a5a83 --- /dev/null +++ b/LuaSL/testLua/luaprocTest2_C.lua @@ -0,0 +1,65 @@ +count = 0 + +proc = [=[ + require "string" + local channel = "channel%d" + local message + + result, error_msg = luaproc.newchannel(channel) + if not result then print(error_msg) end + repeat + message = luaproc.receive(channel) + local x, y = string.find(message, "@") + if not x then + x, y = string.find(message, "@") + x, y = string.find(message, "@") + x, y = string.find(message, "@") + x, y = string.find(message, "@") + x, y = string.find(message, "@") + x, y = string.find(message, "@") + x, y = string.find(message, "@") + x, y = string.find(message, "@") + end + until "quit" == message +]=] + +mainProc = [=[ + local count = %d + local messages = 0 + local length = 0 + local message = "This is a test message. 56789 12" + + if 0 ~= count then + for k = 0, 16 do + for j = 0, 199 do + for i = 1, count do + result, error_msg = luaproc.send("channel" .. i, message) + if not result then print(error_msg .. " " .. i) else + messages = messages + 1 + length = length + #message + end + end + end + message = message .. message + end + + for i = 1, count do + result, error_msg = luaproc.send("channel" .. i, "quit") + if not result then print(error_msg .. " " .. i) end + end + end + + print("Sent " .. messages .. " messages totalling " .. length .. " bytes. The largest message was " .. #message .. " bytes.") +]=] + +for i = 1, 10 do + result, error_msg = luaproc.newproc(string.format(proc, i)) + if not result then print(error_msg) else count = count + 1 end +end + +print("Started " .. count .. " Lua threads.") + +result, error_msg = luaproc.newproc(string.format(mainProc, count)) +if not result then print(error_msg) end + + -- cgit v1.1