From dfca1c67f21bf8709e2a074837d55740350223d2 Mon Sep 17 00:00:00 2001 From: Rochet2 Date: Fri, 26 Dec 2014 01:30:48 +0200 Subject: [PATCH] Eluna fix reload crash and add more asserts --- ElunaTemplate.h | 52 ++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/ElunaTemplate.h b/ElunaTemplate.h index 791fbd2..966be62 100644 --- a/ElunaTemplate.h +++ b/ElunaTemplate.h @@ -33,9 +33,10 @@ public: int args = lua_gettop(L); int expected = l->mfunc(E, L); args = lua_gettop(L) - args; - if (args < 0 || args > expected) // Assert instead? + if (args < 0 || args > expected) { ELUNA_LOG_ERROR("[Eluna]: %s returned unexpected amount of arguments %i out of %i. Report to devs", l->name, args, expected); + ASSERT(false); } for (; args < expected; ++args) lua_pushnil(L); @@ -44,8 +45,8 @@ public: static void SetMethods(Eluna* E, ElunaRegister* methodTable) { - if (!methodTable) - return; + ASSERT(E); + ASSERT(methodTable); lua_pushglobaltable(E->L); @@ -132,20 +133,33 @@ public: // that will only be needed on lua side and will not be managed by TC/mangos/ static void Register(Eluna* E, const char* name, bool gc = false) { - ASSERT(!tname && name); + ASSERT(E); + ASSERT(name); + + // check that metatable isn't already there + luaL_getmetatable(E->L, name); + ASSERT(lua_isnoneornil(E->L, -1)); + + // check that metatable isn't already there + lua_getglobal(E->L, name); + ASSERT(lua_isnoneornil(E->L, -1)); + + // pop metatable and methodtable values + lua_pop(E->L, 2); tname = name; manageMemory = gc; + // create methodtable for userdata of this type lua_newtable(E->L); int methods = lua_gettop(E->L); - // store method table in globals so that - // scripts can add functions in Lua + // push methodtable to stack to be accessed and modified by users lua_pushvalue(E->L, methods); lua_setglobal(E->L, tname); - luaL_newmetatable(E->L, tname); + // create metatable for userdatas of this type + ASSERT(luaL_newmetatable(E->L, tname)); int metatable = lua_gettop(E->L); // tostring @@ -231,25 +245,18 @@ public: template static void SetMethods(Eluna* E, ElunaRegister* methodTable) { - if (!methodTable) - return; + ASSERT(E); + ASSERT(tname); + ASSERT(methodTable); + // get metatable luaL_getmetatable(E->L, tname); - if (!lua_istable(E->L, -1)) - { - lua_remove(E->L, -1); - ELUNA_LOG_ERROR("%s missing metatable", tname); - return; - } + ASSERT(lua_istable(E->L, -1)); + // get method table lua_getfield(E->L, -1, "__index"); lua_remove(E->L, -2); - if (!lua_istable(E->L, -1)) - { - lua_remove(E->L, -1); - ELUNA_LOG_ERROR("%s missing method table from metatable", tname); - return; - } + ASSERT(lua_istable(E->L, -1)); for (; methodTable && methodTable->name && methodTable->mfunc; ++methodTable) { @@ -395,9 +402,10 @@ public: int top = lua_gettop(L); int expected = l->mfunc(E, L, obj); int args = lua_gettop(L) - top; - if (args < 0 || args > expected) // Assert instead? + if (args < 0 || args > expected) { ELUNA_LOG_ERROR("[Eluna]: %s returned unexpected amount of arguments %i out of %i. Report to devs", l->name, args, expected); + ASSERT(false); } if (args == expected) return expected;