diff options
author | pixel <pixel> | 2003-12-14 21:13:32 +0000 |
---|---|---|
committer | pixel <pixel> | 2003-12-14 21:13:32 +0000 |
commit | 187cf873560ca736900db3bb6c8c10d1f75724c4 (patch) | |
tree | 2dabb12d484e35b8072d789c7bbf96f09c0e525d | |
parent | a174ecb1bd01b4c7b28693ec08eda5acd436867d (diff) |
Trying to fix garbage collector (failed...)
-rw-r--r-- | include/BLua.h | 3 | ||||
-rw-r--r-- | lib/BLua.cc | 48 | ||||
-rw-r--r-- | lib/Input.cc | 4 | ||||
-rw-r--r-- | lib/LuaHandle.cc | 14 |
4 files changed, 47 insertions, 22 deletions
diff --git a/include/BLua.h b/include/BLua.h index 53eb1bb..911b17c 100644 --- a/include/BLua.h +++ b/include/BLua.h @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: BLua.h,v 1.9 2003-12-11 16:53:27 pixel Exp $ */ +/* $Id: BLua.h,v 1.10 2003-12-14 21:13:32 pixel Exp $ */ #ifndef __BLUA_H__ #define __BLUA_H__ @@ -50,6 +50,7 @@ class Lua : public Base { void push(lua_CFunction, int = 0); void pop(int = 1); void newtable(); + void * newuser(size_t); void settable(int = -3, bool raw = false); void gettable(int = -2, bool raw = false); void setvar(); diff --git a/lib/BLua.cc b/lib/BLua.cc index a9780fa..e3e2561 100644 --- a/lib/BLua.cc +++ b/lib/BLua.cc @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: BLua.cc,v 1.12 2003-12-11 16:53:28 pixel Exp $ */ +/* $Id: BLua.cc,v 1.13 2003-12-14 21:13:33 pixel Exp $ */ #include <lualib.h> @@ -35,6 +35,7 @@ class LuaStatics : public Base { static int putF(lua_State *, const void *, size_t, void *); static int luapanic(lua_State *); static int trueluapanic(lua_State *) throw(GeneralException); + static int collector(lua_State *); static int destructor(lua_State *); static int andB(lua_State *); @@ -297,6 +298,10 @@ void Lua::newtable() { lua_newtable(L); } +void * Lua::newuser(size_t s) { + return lua_newuserdata(L, s); +} + void Lua::settable(int i, bool raw) { if (raw) { lua_rawset(L, i); @@ -519,28 +524,31 @@ void LuaObject::push(Lua * L) throw (GeneralException) { } void LuaObject::pushme(Lua * L, void * o) { + void ** u; L->push("__obj"); - L->push(o); + u = (void **) L->newuser(sizeof(o)); + *u = o; L->settable(-3, true); } void * LuaObject::getme(Lua * L, int i) { - void * r; + void ** r = 0; if (L->istable(i)) { L->push("__obj"); L->gettable(i, true); - if (!(r = L->touserdata())) { - L->error("Lua object already destroyed, or table is not an object."); - } + if (!(r = (void **) L->touserdata())) + L->error("Table is not an object."); + if (!*r) + L->error("Object already destroyed."); L->pop(); } else if (L->isnil(i)) { r = 0; } else { - L->error("Not an object"); + L->error("Not an object (not even a table)."); } - - return r; + + return r ? *r : 0; } void LuaObject::pushit(Lua * L, const String & s, lua_CFunction f) { @@ -559,13 +567,26 @@ void LuaObject::pushmeta(Lua * L, const String & s, lua_CFunction f) { L->setmetatable(); } +int LuaStatics::collector(lua_State * _L) { + Lua * L = Lua::find(_L); + void ** u = (void **) L->touserdata(); + Base * b = (Base *) *u; + delete b; + *u = 0; + printm(M_INFO, "Collecting an object from LUA\n"); + return 0; +} + int LuaStatics::destructor(lua_State * _L) { + void ** u; Lua * L = Lua::find(_L); Base * b = (Base *) LuaObject::getme(L); delete b; L->push("__obj"); - L->push((void *) 0); - L->settable(-3, true); + L->gettable(-2, true); + u = (void **) L->touserdata(); + *u = 0; + L->pop(); printm(M_INFO, "Destructing an object from LUA\n"); return 0; } @@ -575,7 +596,10 @@ void LuaObject::pushdestruct(Lua * L) throw (GeneralException) { throw GeneralException("Error: can't push destructor, object already pushed"); } push(L); - pushmeta(L, "__gc", LuaStatics::destructor); + L->push("__obj"); + L->gettable(-2, true); + pushmeta(L, "__gc", LuaStatics::collector); + L->pop(); pushit(L, "destroy", LuaStatics::destructor); wantdestruct = true; diff --git a/lib/Input.cc b/lib/Input.cc index 3f2fe21..417ddab 100644 --- a/lib/Input.cc +++ b/lib/Input.cc @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: Input.cc,v 1.38 2003-12-07 05:50:41 pixel Exp $ */ +/* $Id: Input.cc,v 1.39 2003-12-14 21:13:33 pixel Exp $ */ #include <stdio.h> #include <string.h> @@ -95,7 +95,7 @@ Input::Input(const String & no) throw (GeneralException) : #endif if (GetHandle() < 0) { - printm(M_BARE, "Got handle: %i\n", GetHandle()); + printm(M_BARE, "Got handle: %i opening file " + no + "\n", GetHandle()); throw IOGeneral(String(_("Error opening file ")) + no + _(" for reading: ") + strerror(errno)); } diff --git a/lib/LuaHandle.cc b/lib/LuaHandle.cc index d1659a2..5e9a8ce 100644 --- a/lib/LuaHandle.cc +++ b/lib/LuaHandle.cc @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: LuaHandle.cc,v 1.10 2003-12-12 12:45:20 pixel Exp $ */ +/* $Id: LuaHandle.cc,v 1.11 2003-12-14 21:13:33 pixel Exp $ */ #include "LuaHandle.h" @@ -287,9 +287,9 @@ int sLuaHandle::read(lua_State * _L, int t) { h = (Handle *) LuaObject::getme(L); switch (t) { - case U8: r = h->readU8(); - case U16: r = h->readU16(); - case U32: r = h->readU32(); + case U8: r = h->readU8(); break; + case U16: r = h->readU16(); break; + case U32: r = h->readU32(); break; } L->push(r); @@ -311,9 +311,9 @@ int sLuaHandle::write(lua_State * _L, int t) { r = L->tonumber(); switch (t) { - case U8: h->writeU8(r); - case U16: h->writeU16(r); - case U32: h->writeU32(r); + case U8: h->writeU8(r); break; + case U16: h->writeU16(r); break; + case U32: h->writeU32(r); break; } return 0; |