]> jfr.im git - irc/quakenet/newserv.git/blobdiff - lua/lua.c
Lua version now excludes "Lua engine", at least for MODULEVERSION.
[irc/quakenet/newserv.git] / lua / lua.c
index a7d35a083edda38fe58ae89cf87243e31f174c6e..7681bf62a1e2e83b81b9f274f70a42ad223273f9 100644 (file)
--- a/lua/lua.c
+++ b/lua/lua.c
@@ -14,7 +14,7 @@
 
 #include "lua.h"
 
-MODULE_VERSION(LUA_FULLVERSION);
+MODULE_VERSION(LUA_SMALLVERSION);
 
 #ifdef LUA_DEBUGSOCKET
 
@@ -151,6 +151,7 @@ void _fini() {
   freesstring(dummy.name);
 
   lua_freedebugsocket();
+  nscheckfreeall(POOL_LUA);
 }
 
 void lua_loadscripts(void) {
@@ -167,6 +168,17 @@ void lua_loadscripts(void) {
   }
 }
 
+/* taken from the lua manual, modified to use nsmalloc */
+static void *lua_nsmalloc(void *ud, void *ptr, size_t osize, size_t nsize) {
+  if(nsize == 0) {
+    if(ptr != NULL)
+      luafree(ptr);
+    return NULL;
+  }
+
+  return luarealloc(ptr, nsize);
+}
+
 lua_State *lua_loadscript(char *file) {
   char fullpath[LUA_PATHLEN];
   int top;
@@ -181,11 +193,11 @@ lua_State *lua_loadscript(char *file) {
   if(lua_scriptloaded(file))
     return NULL;
 
-  l = lua_open();
+  l = lua_newstate(lua_nsmalloc, NULL);
   if(!l)
     return NULL;
 
-  n = (lua_list *)malloc(sizeof(lua_list));;
+  n = (lua_list *)luamalloc(sizeof(lua_list));;
   if(!n) {
     Error("lua", ERR_ERROR, "Error allocing list for %s.", file);
     return NULL;
@@ -194,7 +206,7 @@ lua_State *lua_loadscript(char *file) {
   n->name = getsstring(file, LUA_PATHLEN);
   if(!n->name) {
     Error("lua", ERR_ERROR, "Error allocing name item for %s.", file);
-    free(n);
+    luafree(n);
     return NULL;
   }
   n->calls = 0;
@@ -223,7 +235,7 @@ lua_State *lua_loadscript(char *file) {
     Error("lua", ERR_ERROR, "Error loading %s.", file);
     lua_close(l);
     freesstring(n->name);
-    free(n);
+    luafree(n);
     return NULL;
   }
 
@@ -248,7 +260,15 @@ lua_State *lua_loadscript(char *file) {
     Error("lua", ERR_ERROR, "Error pcalling: %s.", file);
     lua_close(l);
     freesstring(n->name);
-    free(n);
+
+    if(lua_head == n)
+      lua_head = NULL;
+
+    lua_tail = n->prev;
+    if(lua_tail)
+      lua_tail->next = NULL;
+
+    luafree(n);
     return NULL;
   }
 
@@ -290,7 +310,7 @@ void lua_unloadscript(lua_list *l) {
      }
   }
 
-  free(l);
+  luafree(l);
 }
 
 void lua_setpath(lua_State *l) {