From b17cd3696cb272fcd915c5acaabfa542c683c1db Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 3 Oct 2022 10:36:19 +0200 Subject: [PATCH] fix: Handling of exceptions that are not being caught --- lib/external/pattern_language | 2 +- main/source/main.cpp | 10 +++- main/source/window/window.cpp | 11 +--- .../content/helpers/provider_extra_data.hpp | 1 + .../content/views/view_pattern_data.cpp | 4 +- .../content/views/view_pattern_editor.cpp | 57 ++++++++++--------- 6 files changed, 46 insertions(+), 39 deletions(-) diff --git a/lib/external/pattern_language b/lib/external/pattern_language index 8d78e153d..a94d3e718 160000 --- a/lib/external/pattern_language +++ b/lib/external/pattern_language @@ -1 +1 @@ -Subproject commit 8d78e153d9ee628a8fe80019effcdd2f7785224f +Subproject commit a94d3e7189f3dd7e5f9107b27e52924f047570f3 diff --git a/main/source/main.cpp b/main/source/main.cpp index 8fb69f927..e26c8b667 100644 --- a/main/source/main.cpp +++ b/main/source/main.cpp @@ -57,7 +57,15 @@ int main(int argc, char **argv, char **envp) { EventManager::post(argv[i]); } - window.loop(); + try { + window.loop(); + } catch (const std::exception &e) { + log::fatal("Exception thrown in main loop: {}", e.what()); + return EXIT_FAILURE; + } catch (...) { + log::fatal("Unknown exception thrown in main loop!"); + return EXIT_FAILURE; + } } } while (shouldRestart); diff --git a/main/source/window/window.cpp b/main/source/window/window.cpp index 4765efe89..b5b2bca5b 100644 --- a/main/source/window/window.cpp +++ b/main/source/window/window.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -200,15 +201,7 @@ namespace hex { } this->frameBegin(); - - try { - this->frame(); - } catch (const std::exception &e) { - log::error("Exception thrown in main loop: {}", e.what()); - } catch (...) { - log::error("Unknown exception thrown in main loop!"); - } - + this->frame(); this->frameEnd(); const auto targetFps = ImHexApi::System::getTargetFPS(); diff --git a/plugins/builtin/include/content/helpers/provider_extra_data.hpp b/plugins/builtin/include/content/helpers/provider_extra_data.hpp index c9c2de9c2..b248f08e1 100644 --- a/plugins/builtin/include/content/helpers/provider_extra_data.hpp +++ b/plugins/builtin/include/content/helpers/provider_extra_data.hpp @@ -18,6 +18,7 @@ namespace hex::plugin::builtin { struct { std::string sourceCode; std::unique_ptr runtime; + bool executionDone = true; } patternLanguage; std::list bookmarks; diff --git a/plugins/builtin/source/content/views/view_pattern_data.cpp b/plugins/builtin/source/content/views/view_pattern_data.cpp index 5e0f691a9..e06e1e8ce 100644 --- a/plugins/builtin/source/content/views/view_pattern_data.cpp +++ b/plugins/builtin/source/content/views/view_pattern_data.cpp @@ -108,9 +108,9 @@ namespace hex::plugin::builtin { if (ImGui::Begin(View::toWindowName("hex.builtin.view.pattern_data.name").c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) { if (ImHexApi::Provider::isValid()) { auto provider = ImHexApi::Provider::get(); - auto &runtime = ProviderExtraData::get(provider).patternLanguage.runtime; + auto &patternLanguage = ProviderExtraData::get(provider).patternLanguage; - if (provider->isReadable() && runtime != nullptr && !runtime->isRunning()) { + if (provider->isReadable() && patternLanguage.runtime != nullptr && patternLanguage.executionDone) { auto &sortedPatterns = this->m_sortedPatterns[ImHexApi::Provider::get()]; if (beginPatternTable(provider, ProviderExtraData::get(provider).patternLanguage.runtime->getAllPatterns(), sortedPatterns)) { ImGui::TableHeadersRow(); diff --git a/plugins/builtin/source/content/views/view_pattern_editor.cpp b/plugins/builtin/source/content/views/view_pattern_editor.cpp index 0f73ed64a..7e4f1457d 100644 --- a/plugins/builtin/source/content/views/view_pattern_editor.cpp +++ b/plugins/builtin/source/content/views/view_pattern_editor.cpp @@ -445,30 +445,31 @@ namespace hex::plugin::builtin { } View::discardNavigationRequests(); + + if (!this->m_lastEvaluationProcessed) { + this->m_console = this->m_lastEvaluationLog; + + if (!this->m_lastEvaluationResult) { + if (this->m_lastEvaluationError) { + TextEditor::ErrorMarkers errorMarkers = { + { this->m_lastEvaluationError->line, this->m_lastEvaluationError->message } + }; + this->m_textEditor.SetErrorMarkers(errorMarkers); + } + } else { + for (auto &[name, variable] : this->m_patternVariables) { + if (variable.outVariable && this->m_lastEvaluationOutVars.contains(name)) + variable.value = this->m_lastEvaluationOutVars.at(name); + } + + EventManager::post(); + } + + this->m_lastEvaluationProcessed = true; + ProviderExtraData::get(provider).patternLanguage.executionDone = true; + } } ImGui::End(); - - if (!this->m_lastEvaluationProcessed) { - this->m_console = this->m_lastEvaluationLog; - - if (!this->m_lastEvaluationResult) { - if (this->m_lastEvaluationError) { - TextEditor::ErrorMarkers errorMarkers = { - { this->m_lastEvaluationError->line, this->m_lastEvaluationError->message } - }; - this->m_textEditor.SetErrorMarkers(errorMarkers); - } - } else { - for (auto &[name, variable] : this->m_patternVariables) { - if (variable.outVariable && this->m_lastEvaluationOutVars.contains(name)) - variable.value = this->m_lastEvaluationOutVars.at(name); - } - - EventManager::post(); - } - - this->m_lastEvaluationProcessed = true; - } } void ViewPatternEditor::drawConsole(ImVec2 size) { @@ -814,19 +815,23 @@ namespace hex::plugin::builtin { } void ViewPatternEditor::evaluatePattern(const std::string &code) { + auto provider = ImHexApi::Provider::get(); + auto &patternLanguage = ProviderExtraData::get(provider).patternLanguage; + this->m_runningEvaluators++; + patternLanguage.executionDone = false; this->m_textEditor.SetErrorMarkers({}); this->m_console.clear(); - auto provider = ImHexApi::Provider::get(); - auto &runtime = ProviderExtraData::get(provider).patternLanguage.runtime; - ContentRegistry::PatternLanguage::configureRuntime(*runtime, provider); + ContentRegistry::PatternLanguage::configureRuntime(*patternLanguage.runtime, provider); EventManager::post(); - TaskManager::createTask("hex.builtin.view.pattern_editor.evaluating", TaskManager::NoProgress, [this, &runtime, code](auto &task) { + TaskManager::createTask("hex.builtin.view.pattern_editor.evaluating", TaskManager::NoProgress, [this, &patternLanguage, code](auto &task) { + auto &runtime = patternLanguage.runtime; + task.setInterruptCallback([&runtime] { runtime->abort(); }); std::map envVars;