From 66d64cf020fdd8155577d3ff74c1211cd4839bae Mon Sep 17 00:00:00 2001 From: WerWolv Date: Thu, 15 Sep 2022 09:34:40 +0200 Subject: [PATCH] patterns: Refactor creation of default runtime --- lib/external/pattern_language | 2 +- .../include/hex/api/content_registry.hpp | 2 +- lib/libimhex/source/api/content_registry.cpp | 17 +++++++++-------- .../content/views/view_pattern_editor.cpp | 12 ++++++------ 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/lib/external/pattern_language b/lib/external/pattern_language index 6c4d5d3a6..24456df32 160000 --- a/lib/external/pattern_language +++ b/lib/external/pattern_language @@ -1 +1 @@ -Subproject commit 6c4d5d3a6c5618e841d425471e1359efda4de8d3 +Subproject commit 24456df3284af42b5589099d67f56648a8a50092 diff --git a/lib/libimhex/include/hex/api/content_registry.hpp b/lib/libimhex/include/hex/api/content_registry.hpp index 761bc12b4..657c139dd 100644 --- a/lib/libimhex/include/hex/api/content_registry.hpp +++ b/lib/libimhex/include/hex/api/content_registry.hpp @@ -136,7 +136,7 @@ namespace hex { } - std::unique_ptr createDefaultRuntime(prv::Provider *provider); + void configureRuntime(pl::PatternLanguage &runtime, prv::Provider *provider); void addPragma(const std::string &name, const pl::api::PragmaHandler &handler); diff --git a/lib/libimhex/source/api/content_registry.cpp b/lib/libimhex/source/api/content_registry.cpp index 47dcc4e5a..285566828 100644 --- a/lib/libimhex/source/api/content_registry.cpp +++ b/lib/libimhex/source/api/content_registry.cpp @@ -241,29 +241,30 @@ namespace hex { return functionName; } - std::unique_ptr createDefaultRuntime(prv::Provider *provider) { - auto runtime = std::make_unique(); + void configureRuntime(pl::PatternLanguage &runtime, prv::Provider *provider) { + runtime.reset(); if (provider != nullptr) { - runtime->setDataSource([provider](u64 offset, u8 *buffer, size_t size) { + runtime.setDataSource([provider](u64 offset, u8 *buffer, size_t size) { provider->read(offset, buffer, size); }, provider->getBaseAddress(), provider->getActualSize()); } - runtime->setIncludePaths(fs::getDefaultPaths(fs::ImHexPath::PatternsInclude) | fs::getDefaultPaths(fs::ImHexPath::Patterns)); + runtime.setIncludePaths(fs::getDefaultPaths(fs::ImHexPath::PatternsInclude) | fs::getDefaultPaths(fs::ImHexPath::Patterns)); for (const auto &func : getFunctions()) { if (func.dangerous) - runtime->addDangerousFunction(func.ns, func.name, func.parameterCount, func.callback); + runtime.addDangerousFunction(func.ns, func.name, func.parameterCount, func.callback); else - runtime->addFunction(func.ns, func.name, func.parameterCount, func.callback); + runtime.addFunction(func.ns, func.name, func.parameterCount, func.callback); } for (const auto &[name, callback] : getPragmas()) { - runtime->addPragma(name, callback); + runtime.addPragma(name, callback); } - return runtime; + runtime.addDefine("__IMHEX__"); + runtime.addDefine("__IMHEX_VERSION__", IMHEX_VERSION); } void addPragma(const std::string &name, const pl::api::PragmaHandler &handler) { diff --git a/plugins/builtin/source/content/views/view_pattern_editor.cpp b/plugins/builtin/source/content/views/view_pattern_editor.cpp index ee9e4a0ca..3cb3781bc 100644 --- a/plugins/builtin/source/content/views/view_pattern_editor.cpp +++ b/plugins/builtin/source/content/views/view_pattern_editor.cpp @@ -80,7 +80,8 @@ namespace hex::plugin::builtin { ViewPatternEditor::ViewPatternEditor() : View("hex.builtin.view.pattern_editor.name") { - this->m_parserRuntime = ContentRegistry::PatternLanguage::createDefaultRuntime(nullptr); + this->m_parserRuntime = std::make_unique(); + ContentRegistry::PatternLanguage::configureRuntime(*this->m_parserRuntime, nullptr); this->m_textEditor.SetLanguageDefinition(PatternLanguage()); this->m_textEditor.SetShowWhitespaces(false); @@ -110,7 +111,8 @@ namespace hex::plugin::builtin { EventManager::subscribe(this, [this](prv::Provider *provider) { auto &patternLanguageData = ProviderExtraData::get(provider).patternLanguage; - patternLanguageData.runtime = ContentRegistry::PatternLanguage::createDefaultRuntime(provider); + patternLanguageData.runtime = std::make_unique(); + ContentRegistry::PatternLanguage::configureRuntime(*patternLanguageData.runtime, provider); if (!this->m_autoLoadPatterns) return; @@ -775,6 +777,7 @@ namespace hex::plugin::builtin { void ViewPatternEditor::parsePattern(const std::string &code) { this->m_runningParsers++; + ContentRegistry::PatternLanguage::configureRuntime(*this->m_parserRuntime, nullptr); auto ast = this->m_parserRuntime->parseString(code); this->m_patternVariables.clear(); @@ -815,10 +818,7 @@ namespace hex::plugin::builtin { auto provider = ImHexApi::Provider::get(); auto &runtime = ProviderExtraData::get(provider).patternLanguage.runtime; - runtime->reset(); - runtime->setIncludePaths(fs::getDefaultPaths(fs::ImHexPath::PatternsInclude) | fs::getDefaultPaths(fs::ImHexPath::Patterns)); - runtime->setDataBaseAddress(provider->getBaseAddress()); - runtime->setDataSize(provider->getActualSize()); + ContentRegistry::PatternLanguage::configureRuntime(*runtime, provider); EventManager::post();