diff options
| author | Lukasz Papierkowski <lie@spyro-soft.com> | 2024-12-24 10:52:52 +0100 |
|---|---|---|
| committer | lie <lie@spyro-soft.com> | 2025-01-07 14:43:53 +0000 |
| commit | 8cce192dabffbdfe3421ced739552bb1afdbff1e (patch) | |
| tree | bfa2f017f442cc92f4f0692ac737b5c42c66ff81 /src/plugins/lua/luaengine.cpp | |
| parent | 051eff6d5bc0f0977aeef15d91896e8b2e4b6131 (diff) | |
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>
Diffstat (limited to 'src/plugins/lua/luaengine.cpp')
| -rw-r--r-- | src/plugins/lua/luaengine.cpp | 44 |
1 files changed, 32 insertions, 12 deletions
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<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 - it.value()(v.as<sol::function>(), 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<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)); + } } } |
