From 1d9e8dbda7cad419eb454e6856a25fadeaa66b9c Mon Sep 17 00:00:00 2001 From: WerWolv Date: Tue, 2 Aug 2022 23:36:18 +0200 Subject: [PATCH] fix: Always evaluate in/out variables correctly Fixes #629 --- .../content/views/view_pattern_editor.hpp | 1 - .../content/views/view_pattern_editor.cpp | 60 ++++++++----------- 2 files changed, 26 insertions(+), 35 deletions(-) diff --git a/plugins/builtin/include/content/views/view_pattern_editor.hpp b/plugins/builtin/include/content/views/view_pattern_editor.hpp index 57dd2ee3f..9989478c9 100644 --- a/plugins/builtin/include/content/views/view_pattern_editor.hpp +++ b/plugins/builtin/include/content/views/view_pattern_editor.hpp @@ -100,7 +100,6 @@ namespace hex::plugin::builtin { void drawPatternTooltip(pl::Pattern *pattern); void loadPatternFile(const std::fs::path &path); - void clearPatterns(); void parsePattern(const std::string &code); void evaluatePattern(const std::string &code); diff --git a/plugins/builtin/source/content/views/view_pattern_editor.cpp b/plugins/builtin/source/content/views/view_pattern_editor.cpp index 847f2502b..7aa485d6e 100644 --- a/plugins/builtin/source/content/views/view_pattern_editor.cpp +++ b/plugins/builtin/source/content/views/view_pattern_editor.cpp @@ -345,8 +345,9 @@ namespace hex::plugin::builtin { if (ImGui::IconButton(ICON_VS_DEBUG_STOP, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarRed))) runtime.abort(); } else { - if (ImGui::IconButton(ICON_VS_DEBUG_START, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarGreen))) + if (ImGui::IconButton(ICON_VS_DEBUG_START, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarGreen))) { this->evaluatePattern(this->m_textEditor.GetText()); + } } @@ -381,7 +382,7 @@ namespace hex::plugin::builtin { if (this->m_runAutomatically) this->evaluatePattern(this->m_textEditor.GetText()); else - this->parsePattern(this->m_textEditor.GetText()); + std::thread([this, code = this->m_textEditor.GetText()] { this->parsePattern(code); }).detach(); } } @@ -732,51 +733,40 @@ namespace hex::plugin::builtin { this->evaluatePattern(code); this->m_textEditor.SetText(code); - this->parsePattern(code); + std::thread([this, code] { this->parsePattern(code); }).detach(); } } - void ViewPatternEditor::clearPatterns() { - if (!ImHexApi::Provider::isValid()) return; - - ImHexApi::Provider::get()->getPatternLanguageRuntime().reset(); - this->parsePattern(""); - } - - void ViewPatternEditor::parsePattern(const std::string &code) { - this->m_runningParsers++; - std::thread([this, code] { - auto ast = this->m_parserRuntime->parseString(code); + auto ast = this->m_parserRuntime->parseString(code); - this->m_patternVariables.clear(); + this->m_patternVariables.clear(); - if (ast) { - for (auto &node : *ast) { - if (auto variableDecl = dynamic_cast(node.get())) { - auto type = dynamic_cast(variableDecl->getType().get()); - if (type == nullptr) continue; + if (ast) { + for (auto &node : *ast) { + if (auto variableDecl = dynamic_cast(node.get())) { + auto type = dynamic_cast(variableDecl->getType().get()); + if (type == nullptr) continue; - auto builtinType = dynamic_cast(type->getType().get()); - if (builtinType == nullptr) continue; + auto builtinType = dynamic_cast(type->getType().get()); + if (builtinType == nullptr) continue; - PatternVariable variable = { - .inVariable = variableDecl->isInVariable(), - .outVariable = variableDecl->isOutVariable(), - .type = builtinType->getType(), - .value = { } - }; + PatternVariable variable = { + .inVariable = variableDecl->isInVariable(), + .outVariable = variableDecl->isOutVariable(), + .type = builtinType->getType(), + .value = { } + }; - if (variable.inVariable || variable.outVariable) { - if (!this->m_patternVariables.contains(variableDecl->getName())) - this->m_patternVariables[variableDecl->getName()] = variable; - } + if (variable.inVariable || variable.outVariable) { + if (!this->m_patternVariables.contains(variableDecl->getName())) + this->m_patternVariables[variableDecl->getName()] = variable; } } } + } - this->m_runningParsers--; - }).detach(); + this->m_runningParsers--; } void ViewPatternEditor::evaluatePattern(const std::string &code) { @@ -793,6 +783,8 @@ namespace hex::plugin::builtin { for (const auto &[id, name, value, type] : this->m_envVarEntries) envVars.insert({ name, value }); + this->parsePattern(code); + std::map inVariables; for (auto &[name, variable] : this->m_patternVariables) { if (variable.inVariable)