From d06706b346854cd73631aeb6297418ff82cabdf9 Mon Sep 17 00:00:00 2001
From: David Walter Seikel
Date: Wed, 14 May 2014 14:33:50 +1000
Subject: Actually implement LSL script resetting, and some associated fixes.

---
 src/LuaSL/LuaSL_main.c    | 25 +++++++++++++++++++++----
 src/LuaSL/LuaSL_threads.c | 10 +++++++---
 2 files changed, 28 insertions(+), 7 deletions(-)

(limited to 'src/LuaSL')

diff --git a/src/LuaSL/LuaSL_main.c b/src/LuaSL/LuaSL_main.c
index 33b15c6..cb2726e 100644
--- a/src/LuaSL/LuaSL_main.c
+++ b/src/LuaSL/LuaSL_main.c
@@ -45,10 +45,27 @@ static script *findThem(gameGlobals *ourGlobals, const char *base, const char *t
 
 static void resetScript(script *victim)
 {
-//    gameGlobals *ourGlobals = victim->game;
-
-    PD("Resetting %s", victim->fileName);
-    // TODO - now what?
+  gameGlobals *ourGlobals = victim->game;
+  script *me;
+  char buf[PATH_MAX];
+
+  PD("RESETTING %s", victim->name);
+  sendToChannel(ourGlobals, victim->SID, "quit()");
+  eina_hash_del(ourGlobals->scripts, victim->SID, NULL);
+  eina_hash_del(ourGlobals->names, victim->fileName, NULL);
+
+  // The old one will eventually die, create a new one.
+  me = calloc(1, sizeof(script));
+  gettimeofday(&me->startTime, NULL);
+  strncpy(me->SID, victim->SID, sizeof(me->SID));
+  strncpy(me->fileName, victim->fileName, sizeof(me->fileName));
+  me->name = &me->fileName[sizeof(PACKAGE_DATA_DIR)];
+  me->game = ourGlobals;
+  me->client = victim->client;
+  eina_hash_add(ourGlobals->scripts, me->SID, me);
+  eina_hash_add(ourGlobals->names, me->fileName, me);
+  sprintf(buf, "%s.lua.out", me->fileName);
+  newProc(buf, TRUE, me);
 }
 
 void scriptSendBack(void * data)
diff --git a/src/LuaSL/LuaSL_threads.c b/src/LuaSL/LuaSL_threads.c
index 234a7c5..e326e14 100644
--- a/src/LuaSL/LuaSL_threads.c
+++ b/src/LuaSL/LuaSL_threads.c
@@ -298,27 +298,31 @@ void newProc(const char *code, int file, script *lp)
     recycled *trash;
 
     // Try to recycle a Lua state, otherwise create one from scratch.
+#if 0	// TODO - something about this causes a crash.
     pthread_mutex_lock(&mutex_recycle_list);
     /* pop list head */
     if ((trash = (recycled *) eina_clist_head(&recyclelp)))
     {
+	printf("  Reusing Lua trash.\n");
 	eina_clist_remove(&(trash->node));
 	lp->L = trash->L;
 	free(trash);
     }
     pthread_mutex_unlock(&mutex_recycle_list);
+#endif
 
     if (NULL == lp->L)
     {
 	lp->L = luaL_newstate();
 
-	/* store the script struct in its own Lua state */
-	lua_pushlightuserdata(lp->L, lp);
-	lua_setfield(lp->L, LUA_REGISTRYINDEX, "_SELF");
 	luaL_openlibs(lp->L);
 	luaL_register(lp->L, "luaproc", luaproc_funcs_child);
     }
 
+    /* store the script struct in its own Lua state */
+    lua_pushlightuserdata(lp->L, lp);
+    lua_setfield(lp->L, LUA_REGISTRYINDEX, "_SELF");
+
     lp->status = LUAPROC_STAT_IDLE;
     lp->args = 0;
     eina_clist_element_init(&(lp->node));
-- 
cgit v1.1