From 0e7367e23dde45378740e21874cf7dd2bf003fa3 Mon Sep 17 00:00:00 2001 From: Rochet2 Date: Mon, 5 Jan 2015 16:37:44 +0200 Subject: [PATCH] Fix hookmgr rewrite Fix: - crash on reload - crash on error in call - a warning from type change --- HookMgr.cpp | 9 +++++---- LuaEngine.cpp | 22 +++++++++++++++++----- LuaEngine.h | 7 +------ 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/HookMgr.cpp b/HookMgr.cpp index 0013dab..61980f7 100644 --- a/HookMgr.cpp +++ b/HookMgr.cpp @@ -93,7 +93,7 @@ int Eluna::SetupStack(EventBind* event_bindings, EntryBind* entry_bindings ASSERT(number_of_arguments == this->push_counter); // Stack: [arguments] - Push((int)event_id); + Push(event_id); this->push_counter = 0; ++number_of_arguments; // Stack: [arguments], event_id @@ -143,7 +143,7 @@ void Eluna::ReplaceArgument(T value, uint8 index, uint8 number_of_arguments, uin int Eluna::CallOneFunction(int number_of_functions, int number_of_arguments, int number_of_results) { ++number_of_arguments; // Caller doesn't know about `event_id`. - ASSERT(number_of_functions > 0 && number_of_results >= 0); + ASSERT(number_of_functions > 0 && number_of_arguments > 0 && number_of_results >= 0); // Stack: event_id, [arguments], [functions] int functions_top = lua_gettop(L); @@ -208,6 +208,7 @@ template bool Eluna::CallAllFunctionsBool(EventBind* event_bindings, EntryBind* entry_bindings, T event_id, uint32 entry, bool default_value) { bool result = default_value; + // Note: number_of_arguments here does not count in eventID, which is pushed in SetupStack int number_of_arguments = this->push_counter; // Stack: [arguments] @@ -220,7 +221,7 @@ bool Eluna::CallAllFunctionsBool(EventBind* event_bindings, EntryBind* ent --number_of_functions; // Stack: event_id, [arguments], [functions - 1], result - if (lua_isboolean(L, r) && (bool)lua_toboolean(L, r) != default_value) + if (lua_isboolean(L, r) && (lua_toboolean(L, r) == 1) != default_value) result = !default_value; lua_pop(L, 1); @@ -461,13 +462,13 @@ void Eluna::OnShutdownCancel() void Eluna::OnWorldUpdate(uint32 diff) { - ELUNA_LOCK(this); if (reload) { ReloadEluna(); return; } + ELUNA_LOCK(this); eventMgr->globalProcessor->Update(diff); Push(diff); diff --git a/LuaEngine.cpp b/LuaEngine.cpp index abc2451..036bcb4 100644 --- a/LuaEngine.cpp +++ b/LuaEngine.cpp @@ -432,19 +432,31 @@ void Eluna::report(lua_State* luastate) void Eluna::ExecuteCall(int params, int res) { int top = lua_gettop(L); - int type = lua_type(L, top - params); + // Expected: function, [parameters] + ASSERT(top > params); + + // Check function type + int type = lua_type(L, top - params); if (type != LUA_TFUNCTION) { - lua_pop(L, params + 1); // Cleanup the stack. ELUNA_LOG_ERROR("[Eluna]: Cannot execute call: registered value is %s, not a function.", lua_typename(L, type)); - return; + ASSERT(false); } + // Objects are invalidated when event level hits 0 ++event_level; - if (lua_pcall(L, params, res, 0)) - report(L); + int result = lua_pcall(L, params, res, 0); --event_level; + + // lua_pcall returns 0 on success. + // On error we report errors and push nils for expected amount of returned values + if (result) + { + report(L); + for (int i = 0; i < res; ++i) + lua_pushnil(L); + } } void Eluna::Push(lua_State* luastate) diff --git a/LuaEngine.h b/LuaEngine.h index 8bb5684..bf11b34 100644 --- a/LuaEngine.h +++ b/LuaEngine.h @@ -247,12 +247,7 @@ public: void Push(const double value) { Push(L, value); ++push_counter; } void Push(const std::string& value) { Push(L, value); ++push_counter; } void Push(const char* value) { Push(L, value); ++push_counter; } - template void Push(T const* ptr){ Push(L, ptr); ++push_counter; } - void Push(Object const* obj) { Push(L, obj); ++push_counter; } - void Push(WorldObject const* obj) { Push(L, obj); ++push_counter; } - void Push(Unit const* unit) { Push(L, unit); ++push_counter; } - void Push(Pet const* pet) { Push(L, pet); ++push_counter; } - void Push(TempSummon const* summon) { Push(L, summon); ++push_counter; } + template void Push(T const* ptr){ Push(L, ptr); ++push_counter; } // Checks template static T CHECKVAL(lua_State* luastate, int narg);