Lua: Fix Crash when debug category is enabled

Not all keys / values have to be strings, but we tried
to always convert to string which results in an assert.

Fixes: QTCREATORBUG-32206
Change-Id: Ibad83383e22d2bfb68a2801c3139d7c447a8b76a
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Marcus Tillmanns
2025-01-06 11:55:00 +01:00
parent 7837cb4709
commit fc4f69ed43

View File

@@ -30,6 +30,32 @@ Utils::expected_str<void> connectHooks(
static Q_LOGGING_CATEGORY(logLuaEngine, "qtc.lua.engine", QtWarningMsg); static Q_LOGGING_CATEGORY(logLuaEngine, "qtc.lua.engine", QtWarningMsg);
QString luaToString(lua_State *state, int index)
{
size_t l;
const char *s = luaL_tolstring(state, index, &l);
if (s == nullptr)
return {};
// Remove from stack what tolstring pushed onto it.
sol::stack::pop_n(state, 1);
return QString::fromUtf8(s, l);
}
template<typename T>
QString refToString(const T &ref)
{
if (ref.template is<QString>())
return ref.template as<QString>();
if (ref.template is<sol::table>())
return toJsonString(ref.template as<sol::table>());
auto pp = sol::stack::push_pop(ref);
return luaToString(ref.lua_state(), -1);
}
class LuaInterfaceImpl final : public QObject, public LuaInterface class LuaInterfaceImpl final : public QObject, public LuaInterface
{ {
public: public:
@@ -315,8 +341,8 @@ expected_str<sol::protected_function> prepareSetup(
if (logLuaEngine().isDebugEnabled()) { if (logLuaEngine().isDebugEnabled()) {
qCDebug(logLuaEngine) << "Script returned table with keys:"; qCDebug(logLuaEngine) << "Script returned table with keys:";
for (const auto &[key, value] : *pluginTable) { for (const auto &[key, value] : *pluginTable) {
qCDebug(logLuaEngine) << "Key:" << key.as<QString>(); qCDebug(logLuaEngine) << "Key:" << refToString(key);
qCDebug(logLuaEngine) << "Value:" << value.as<QString>(); qCDebug(logLuaEngine) << "Value:" << refToString(value);
} }
} }