diff --git a/src/plugins/lua/luaengine.cpp b/src/plugins/lua/luaengine.cpp index a241ed428fe..9fb707cb9ca 100644 --- a/src/plugins/lua/luaengine.cpp +++ b/src/plugins/lua/luaengine.cpp @@ -230,18 +230,38 @@ expected_str connectHooks( qCDebug(logLuaEngine) << "connectHooks called with path: " << path; for (const auto &[k, v] : table) { - qCDebug(logLuaEngine) << "Processing key: " << k.as(); - if (v.get_type() == sol::type::table) { - return connectHooks( - lua, v.as(), QStringList{path, k.as()}.join("."), guard); - } else if (v.get_type() == sol::type::function) { - QString hookName = QStringList{path, k.as()}.join("."); - qCDebug(logLuaEngine) << "Connecting function to hook: " << hookName; - auto it = d->m_hooks.find(hookName); - if (it == d->m_hooks.end()) - return make_unexpected(Tr::tr("No hook with the name \"%1\" found.").arg(hookName)); - else - it.value()(v.as(), guard); + if (k.get_type() != sol::type::string) + return make_unexpected( + Tr::tr("Non-string key encountered in Lua table at path \"%1\"").arg(path)); + + const auto keyName = k.as(); + const auto currentPath = QStringList{path, keyName}.join("."); + qCDebug(logLuaEngine) << "Processing path:" << currentPath; + + switch (v.get_type()) { + case sol::type::table: { + auto result = connectHooks(lua, v.as(), currentPath, guard); + if (!result) + return result; + break; + } + case sol::type::function: { + qCDebug(logLuaEngine) << "Connecting function to hook:" << currentPath; + + auto it = d->m_hooks.find(currentPath); + if (it == d->m_hooks.end()) { + return make_unexpected( + Tr::tr("No hook with the name \"%1\" found.").arg(currentPath)); + } + + it.value()(v.as(), guard); + break; + } + default: { + return make_unexpected(Tr::tr("Unsupported value type \"%1\" at path \"%2\".") + .arg(static_cast(v.get_type())) + .arg(currentPath)); + } } }