Skip to content

Commit d46d607

Browse files
authored
Fixes crash when Eluna.TraceBack is true (#501)
This reverts commit 24cae10. Fixes crash with hook calls nested within method calls when Eluna.TraceBack is true
1 parent 1283015 commit d46d607

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

LuaEngine.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ void Eluna::CloseLua()
103103
if (L)
104104
lua_close(L);
105105
L = NULL;
106-
stacktraceFunctionStackIndex = 0;
107106

108107
instanceDataRefs.clear();
109108
continentDataRefs.clear();
@@ -174,10 +173,6 @@ void Eluna::OpenLua()
174173
lua_pushcfunction(L, &PrecompiledLoader);
175174
lua_rawseti(L, -2, newLoaderIndex);
176175
lua_pop(L, 2); // pop loaders/searchers table, pop package table
177-
178-
// Leave StackTrace function on stack and save reference to it
179-
lua_pushcfunction(L, &StackTrace);
180-
stacktraceFunctionStackIndex = lua_gettop(L);
181176
}
182177

183178
void Eluna::CreateBindStores()
@@ -370,16 +365,24 @@ bool Eluna::ExecuteCall(int params, int res)
370365
}
371366

372367
bool usetrace = sElunaConfig->GetConfig(CONFIG_ELUNA_TRACEBACK);
373-
if (usetrace && !lua_iscfunction(L, stacktraceFunctionStackIndex))
368+
if (usetrace)
374369
{
375-
ELUNA_LOG_ERROR("[Eluna]: Cannot execute call: registered value is %s, not a c-function.", luaL_tolstring(L, stacktraceFunctionStackIndex, NULL));
376-
ASSERT(false); // stack probably corrupt
370+
lua_pushcfunction(L, &StackTrace);
371+
// Stack: function, [parameters], traceback
372+
lua_insert(L, base);
373+
// Stack: traceback, function, [parameters]
377374
}
378375

379376
// Objects are invalidated when event_level hits 0
380377
++event_level;
381-
int result = lua_pcall(L, params, res, usetrace ? stacktraceFunctionStackIndex : 0);
378+
int result = lua_pcall(L, params, res, usetrace ? base : 0);
382379
--event_level;
380+
381+
if (usetrace)
382+
{
383+
// Stack: traceback, [results or errmsg]
384+
lua_remove(L, base);
385+
}
383386
// Stack: [results or errmsg]
384387

385388
// lua_pcall returns 0 on success.

LuaEngine.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,6 @@ class ELUNA_GAME_API Eluna
174174
// Whether or not Eluna is in compatibility mode. Used in some method wrappers.
175175
bool compatibilityMode;
176176

177-
// Index of the Eluna::StackTrace function pushed to the lua state stack when lua is opened
178-
// We store the function to stack on lua open because it cannot be a pseudo-index (must be on stack) and we want access it on every call
179-
int stacktraceFunctionStackIndex = 0;
180-
181177
// Map from instance ID -> Lua table ref
182178
std::unordered_map<uint32, int> instanceDataRefs;
183179
// Map from map ID -> Lua table ref

0 commit comments

Comments
 (0)