Fix hookmgr rewrite

Fix:
 - crash on reload
 - crash on error in call
 - a warning from type change
This commit is contained in:
Rochet2
2015-01-05 16:37:44 +02:00
parent 2ff25ee205
commit 0e7367e23d
3 changed files with 23 additions and 15 deletions

View File

@@ -93,7 +93,7 @@ int Eluna::SetupStack(EventBind<T>* event_bindings, EntryBind<T>* entry_bindings
ASSERT(number_of_arguments == this->push_counter); ASSERT(number_of_arguments == this->push_counter);
// Stack: [arguments] // Stack: [arguments]
Push((int)event_id); Push(event_id);
this->push_counter = 0; this->push_counter = 0;
++number_of_arguments; ++number_of_arguments;
// Stack: [arguments], event_id // 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) 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`. ++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] // Stack: event_id, [arguments], [functions]
int functions_top = lua_gettop(L); int functions_top = lua_gettop(L);
@@ -208,6 +208,7 @@ template<typename T>
bool Eluna::CallAllFunctionsBool(EventBind<T>* event_bindings, EntryBind<T>* entry_bindings, T event_id, uint32 entry, bool default_value) bool Eluna::CallAllFunctionsBool(EventBind<T>* event_bindings, EntryBind<T>* entry_bindings, T event_id, uint32 entry, bool default_value)
{ {
bool result = 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; int number_of_arguments = this->push_counter;
// Stack: [arguments] // Stack: [arguments]
@@ -220,7 +221,7 @@ bool Eluna::CallAllFunctionsBool(EventBind<T>* event_bindings, EntryBind<T>* ent
--number_of_functions; --number_of_functions;
// Stack: event_id, [arguments], [functions - 1], result // 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; result = !default_value;
lua_pop(L, 1); lua_pop(L, 1);
@@ -461,13 +462,13 @@ void Eluna::OnShutdownCancel()
void Eluna::OnWorldUpdate(uint32 diff) void Eluna::OnWorldUpdate(uint32 diff)
{ {
ELUNA_LOCK(this);
if (reload) if (reload)
{ {
ReloadEluna(); ReloadEluna();
return; return;
} }
ELUNA_LOCK(this);
eventMgr->globalProcessor->Update(diff); eventMgr->globalProcessor->Update(diff);
Push(diff); Push(diff);

View File

@@ -432,19 +432,31 @@ void Eluna::report(lua_State* luastate)
void Eluna::ExecuteCall(int params, int res) void Eluna::ExecuteCall(int params, int res)
{ {
int top = lua_gettop(L); 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) 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)); 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; ++event_level;
if (lua_pcall(L, params, res, 0)) int result = lua_pcall(L, params, res, 0);
report(L);
--event_level; --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) void Eluna::Push(lua_State* luastate)

View File

@@ -247,12 +247,7 @@ public:
void Push(const double value) { Push(L, value); ++push_counter; } void Push(const double value) { Push(L, value); ++push_counter; }
void Push(const std::string& 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; } void Push(const char* value) { Push(L, value); ++push_counter; }
template<typename T> void Push(T const* ptr){ Push<T>(L, ptr); ++push_counter; } template<typename T> 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; }
// Checks // Checks
template<typename T> static T CHECKVAL(lua_State* luastate, int narg); template<typename T> static T CHECKVAL(lua_State* luastate, int narg);