diff options
-rw-r--r-- | src/lua-interface.cpp | 94 |
1 files changed, 46 insertions, 48 deletions
diff --git a/src/lua-interface.cpp b/src/lua-interface.cpp index 8350680..91af42d 100644 --- a/src/lua-interface.cpp +++ b/src/lua-interface.cpp @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: lua-interface.cpp,v 1.1 2008-07-04 12:03:01 pixel Exp $ */ +/* $Id: lua-interface.cpp,v 1.2 2008-07-04 12:59:11 pixel Exp $ */ #define WIP @@ -157,21 +157,49 @@ int my_getc (FILE * stream) bool interactive = false, server = false; -Lua * L = 0; - -String server_fname = "server.lua"; +class threaded_Lua : public Lua { + public: + threaded_Lua() { + init_mutex(); + } + virtual ~threaded_Lua() { + pthread_mutex_destroy(&mutex); + } + virtual void lock() { + pthread_mutex_lock(&mutex); + } + virtual void unlock() { + pthread_mutex_unlock(&mutex); + } + protected: + virtual Lua * spawn_from_thread(lua_State * __L) { + return new threaded_Lua(__L); + } + private: + threaded_Lua(lua_State * __L) : Lua(__L) { + init_mutex(); + } + void init_mutex() { + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(&mutex, &attr); + pthread_mutexattr_destroy(&attr); + } + pthread_mutex_t mutex; +}; -static int myprint(lua_State * __L) { - Lua * L = Lua::find(__L); - String t = "From LUA: " + L->tostring() + "\n"; - char * tc = t.strdup(); +class LuaStderrPrinter : public LuaPrinter { + public: + LuaStderrPrinter() { } + virtual void puts(const char * msg) { fputs(msg, stderr); } +}; - Base::printm(M_STATUS, "%s", tc); +LuaStderrPrinter lp_stderr; - free(tc); +Lua * L = 0; - return 0; -} +String server_fname = "server.lua"; static int StartTaskLoop(void * foo) { TaskMan::MainLoop(); @@ -271,14 +299,12 @@ class Lualua_interface : public LuaObject { typedef void lua_interface; enum lua_interface_functions_t { - lua_interface_PRINT = 0, lua_interface_PRINTN, lua_interface_QUIT, lua_interface_EXIT, }; struct lua_functypes_t lua_interface_functions[] = { - { lua_interface_PRINT, "print", 0, 1, { BLUA_ANY } }, { lua_interface_PRINTN, "printn", 1, 1, { BLUA_ANY } }, { lua_interface_QUIT, "quit", 0, 0, 0 }, { lua_interface_EXIT, "exit", 0, 0, 0 }, @@ -287,7 +313,6 @@ struct lua_functypes_t lua_interface_functions[] = { class sLua_lua_interface : public Base { public: - DECLARE_FUNCTION(lua_interface, lua_interface_PRINT); DECLARE_FUNCTION(lua_interface, lua_interface_PRINTN); DECLARE_FUNCTION(lua_interface, lua_interface_QUIT); DECLARE_FUNCTION(lua_interface, lua_interface_EXIT); @@ -298,7 +323,6 @@ class sLua_lua_interface : public Base { void Lualua_interface::pushstatics(Lua * L) throw (GeneralException ) { CHECK_FUNCTIONS(lua_interface); - PUSH_FUNCTION(lua_interface, lua_interface_PRINT); PUSH_FUNCTION(lua_interface, lua_interface_PRINTN); PUSH_FUNCTION(lua_interface, lua_interface_QUIT); PUSH_FUNCTION(lua_interface, lua_interface_EXIT); @@ -311,8 +335,6 @@ int sLua_lua_interface::lua_interface_proceed_statics(Lua * L, int n, int caller String eol = ""; switch (caller) { - case lua_interface_PRINT: - eol = "\n"; case lua_interface_PRINTN: if (n) { p = L->tostring(1) + eol; @@ -358,28 +380,6 @@ struct option long_options[] = { {0, 0, NULL, 0 } }; -static Uint32 elf_hash(const unsigned char * name) { - Uint32 h = 0, g; - - while (*name) { - h = (h << 4) + *name++; - if ((g = h & 0xf0000000)) - h ^= g >> 24; - h &= ~g; - } - - return h; -} - -static int lua_elf_hash(lua_State * __L) { - Lua * L = Lua::find(__L); - String t = L->tostring(); - - L->push((lua_Number) elf_hash((unsigned char *) t.to_charp())); - - return 1; -} - String LUACall_Names[] = {"identifier"}; String LUACall_Defaults[] = {""}; String LUACall_Invites[] = {"LUA function name:"}; @@ -426,7 +426,9 @@ CODE_BEGINS /* That's the basic lua starter for non interactive mode */ Lua * start_basic_lua(void) { - L = new Lua(); + L = new threaded_Lua(); + + L->lock(); L->open_base(); L->open_math(); @@ -434,15 +436,11 @@ Lua * start_basic_lua(void) { L->open_table(); L->open_dir(); - L->push("print"); - L->push(myprint); - L->setvar(); + Luabaselua_interface::pushstatics(L); - L->push("hash"); - L->push(lua_elf_hash); - L->setvar(); + L->SetPrinter(&lp_stderr); - Luabaselua_interface::pushstatics(L); + L->unlock(); return L; } |