Lua: Fix multilevel tree traversal in connectHooks function

Previously, only leaf nodes from the first branch of the tree were processed.
* Handled: root.branch1.leafA, root.branch1.leafB
* Skipped: root.branch2.leafC, etc.
This change ensures all branches are traversed so no leaf nodes are skipped.
The fix iterates over each key in the table and, for sub-tables, recursively
calls connectHooks instead of returning early.

Change-Id: I17182f4000e83e5d4747e71573d5ac2d6558d7e7
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
Lukasz Papierkowski
2024-12-24 10:52:52 +01:00
committed by lie
parent 051eff6d5b
commit 8cce192dab

View File

@@ -230,18 +230,38 @@ expected_str<void> connectHooks(
qCDebug(logLuaEngine) << "connectHooks called with path: " << path;
for (const auto &[k, v] : table) {
qCDebug(logLuaEngine) << "Processing key: " << k.as<QString>();
if (v.get_type() == sol::type::table) {
return connectHooks(
lua, v.as<sol::table>(), QStringList{path, k.as<QString>()}.join("."), guard);
} else if (v.get_type() == sol::type::function) {
QString hookName = QStringList{path, k.as<QString>()}.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
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<QString>();
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<sol::table>(), 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<sol::function>(), guard);
break;
}
default: {
return make_unexpected(Tr::tr("Unsupported value type \"%1\" at path \"%2\".")
.arg(static_cast<int>(v.get_type()))
.arg(currentPath));
}
}
}