1
0
mirror of synced 2025-01-19 01:24:15 +01:00

patterns: Refactor creation of default runtime

This commit is contained in:
WerWolv 2022-09-15 09:34:40 +02:00
parent 42f5c0f484
commit 66d64cf020
4 changed files with 17 additions and 16 deletions

@ -1 +1 @@
Subproject commit 6c4d5d3a6c5618e841d425471e1359efda4de8d3
Subproject commit 24456df3284af42b5589099d67f56648a8a50092

View File

@ -136,7 +136,7 @@ namespace hex {
}
std::unique_ptr<pl::PatternLanguage> createDefaultRuntime(prv::Provider *provider);
void configureRuntime(pl::PatternLanguage &runtime, prv::Provider *provider);
void addPragma(const std::string &name, const pl::api::PragmaHandler &handler);

View File

@ -241,29 +241,30 @@ namespace hex {
return functionName;
}
std::unique_ptr<pl::PatternLanguage> createDefaultRuntime(prv::Provider *provider) {
auto runtime = std::make_unique<pl::PatternLanguage>();
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) {

View File

@ -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<pl::PatternLanguage>();
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<EventProviderOpened>(this, [this](prv::Provider *provider) {
auto &patternLanguageData = ProviderExtraData::get(provider).patternLanguage;
patternLanguageData.runtime = ContentRegistry::PatternLanguage::createDefaultRuntime(provider);
patternLanguageData.runtime = std::make_unique<pl::PatternLanguage>();
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<EventHighlightingChanged>();