From 104e0f73bf66345df328771d94c351353e1066ff Mon Sep 17 00:00:00 2001 From: lsemp3d <58790905+lsemp3d@users.noreply.github.com> Date: Wed, 14 Jul 2021 18:31:50 -0700 Subject: [PATCH] Fixed Lua class enumeration, there was a crash when traversing into methods Signed-off-by: lsemp3d <58790905+lsemp3d@users.noreply.github.com> --- .../AzCore/Script/ScriptContextDebug.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Code/Framework/AzCore/AzCore/Script/ScriptContextDebug.cpp b/Code/Framework/AzCore/AzCore/Script/ScriptContextDebug.cpp index b68b19ddda..0444c4ce2a 100644 --- a/Code/Framework/AzCore/AzCore/Script/ScriptContextDebug.cpp +++ b/Code/Framework/AzCore/AzCore/Script/ScriptContextDebug.cpp @@ -157,8 +157,11 @@ ScriptContextDebug::EnumRegisteredClasses(EnumClass enumClass, EnumMethod enumMe lua_pop(l, 2); // pop the Class name and behaviorClass lua_pushnil(l); + + // iterate over the key/value pairs while (lua_next(l, -2) != 0) { + // if key: string value: function if (lua_isstring(l, -2) && lua_isfunction(l, -1)) { const char* name = lua_tostring(l, -2); @@ -167,6 +170,7 @@ ScriptContextDebug::EnumRegisteredClasses(EnumClass enumClass, EnumMethod enumMe bool isRead = true; bool isWrite = true; + // check if there is a getter provided lua_getupvalue(l, -1, 1); if (lua_isnil(l, -1)) { @@ -174,6 +178,7 @@ ScriptContextDebug::EnumRegisteredClasses(EnumClass enumClass, EnumMethod enumMe } lua_pop(l, 1); + // check if there is a setter provided lua_getupvalue(l, -1, 2); if (lua_isnil(l, -1)) { @@ -181,6 +186,7 @@ ScriptContextDebug::EnumRegisteredClasses(EnumClass enumClass, EnumMethod enumMe } lua_pop(l, 1); + // enumerate the remaining property if (!enumProperty(&behaviorClass->m_typeId, name, isRead, isWrite, userData)) { lua_pop(l, 5); @@ -189,21 +195,30 @@ ScriptContextDebug::EnumRegisteredClasses(EnumClass enumClass, EnumMethod enumMe } else { + // for any non-built in methods if (strncmp(name, "__", 2) != 0) { const char* dbgParamInfo = NULL; - lua_getupvalue(l, -1, 2); + + // attempt to get the name + bool popDebugName = lua_getupvalue(l, -1, 2) != nullptr; if (lua_isstring(l, -1)) { dbgParamInfo = lua_tostring(l, -1); } + // enumerate the method's parameters if (!enumMethod(&behaviorClass->m_typeId, name, dbgParamInfo, userData)) { lua_pop(l, 6); return; } - lua_pop(l, 1); // pop the DBG name + + // if we were able to get the name, pop it from the stack + if (popDebugName) + { + lua_pop(l, 1); + } } } }