From ad223a4e5cb745ac82d4894142740b07455ca876 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 20 Dec 2021 20:40:28 +0100 Subject: [PATCH] sys: Refactor pattern language api functions a bit --- .../source/content/pl_builtin_functions.cpp | 85 +++++++++---------- .../include/hex/api/content_registry.hpp | 7 +- .../include/hex/helpers/shared_data.hpp | 2 +- .../include/hex/pattern_language/ast_node.hpp | 16 ++-- .../hex/pattern_language/evaluator.hpp | 6 +- .../hex/pattern_language/pattern_data.hpp | 8 +- .../libimhex/source/api/content_registry.cpp | 14 ++- .../libimhex/source/helpers/shared_data.cpp | 2 +- tests/pattern_language/source/main.cpp | 4 +- 9 files changed, 76 insertions(+), 68 deletions(-) diff --git a/plugins/builtin/source/content/pl_builtin_functions.cpp b/plugins/builtin/source/content/pl_builtin_functions.cpp index 78b14f0bf..add70e8c2 100644 --- a/plugins/builtin/source/content/pl_builtin_functions.cpp +++ b/plugins/builtin/source/content/pl_builtin_functions.cpp @@ -1,6 +1,5 @@ #include -#include #include #include #include @@ -45,11 +44,11 @@ namespace hex::plugin::builtin { void registerPatternLanguageFunctions() { using namespace hex::pl; - ContentRegistry::PatternLanguageFunctions::Namespace nsStd = { "std" }; + ContentRegistry::PatternLanguage::Namespace nsStd = { "std" }; { /* assert(condition, message) */ - ContentRegistry::PatternLanguageFunctions::add(nsStd, "assert", 2, [](Evaluator *ctx, auto params) -> std::optional { + ContentRegistry::PatternLanguage::addFunction(nsStd, "assert", 2, [](Evaluator *ctx, auto params) -> std::optional { auto condition = Token::literalToBoolean(params[0]); auto message = Token::literalToString(params[1], false); @@ -60,7 +59,7 @@ namespace hex::plugin::builtin { }); /* assert_warn(condition, message) */ - ContentRegistry::PatternLanguageFunctions::add(nsStd, "assert_warn", 2, [](auto *ctx, auto params) -> std::optional { + ContentRegistry::PatternLanguage::addFunction(nsStd, "assert_warn", 2, [](auto *ctx, auto params) -> std::optional { auto condition = Token::literalToBoolean(params[0]); auto message = Token::literalToString(params[1], false); @@ -71,19 +70,19 @@ namespace hex::plugin::builtin { }); /* print(format, args...) */ - ContentRegistry::PatternLanguageFunctions::add(nsStd, "print", ContentRegistry::PatternLanguageFunctions::MoreParametersThan | 0, [](Evaluator *ctx, auto params) -> std::optional { + ContentRegistry::PatternLanguage::addFunction(nsStd, "print", ContentRegistry::PatternLanguage::MoreParametersThan | 0, [](Evaluator *ctx, auto params) -> std::optional { ctx->getConsole().log(LogConsole::Level::Info, format(ctx, params)); return std::nullopt; }); /* format(format, args...) */ - ContentRegistry::PatternLanguageFunctions::add(nsStd, "format", ContentRegistry::PatternLanguageFunctions::MoreParametersThan | 0, [](Evaluator *ctx, auto params) -> std::optional { + ContentRegistry::PatternLanguage::addFunction(nsStd, "format", ContentRegistry::PatternLanguage::MoreParametersThan | 0, [](Evaluator *ctx, auto params) -> std::optional { return format(ctx, params); }); /* env(name) */ - ContentRegistry::PatternLanguageFunctions::add(nsStd, "env", 1, [](Evaluator *ctx, auto params) -> std::optional { + ContentRegistry::PatternLanguage::addFunction(nsStd, "env", 1, [](Evaluator *ctx, auto params) -> std::optional { auto name = Token::literalToString(params[0], false); auto env = ctx->getEnvVariable(name); @@ -97,11 +96,11 @@ namespace hex::plugin::builtin { } - ContentRegistry::PatternLanguageFunctions::Namespace nsStdMem = { "std", "mem" }; + ContentRegistry::PatternLanguage::Namespace nsStdMem = { "std", "mem" }; { /* align_to(alignment, value) */ - ContentRegistry::PatternLanguageFunctions::add(nsStdMem, "align_to", 2, [](Evaluator *ctx, auto params) -> std::optional { + ContentRegistry::PatternLanguage::addFunction(nsStdMem, "align_to", 2, [](Evaluator *ctx, auto params) -> std::optional { auto alignment = Token::literalToUnsigned(params[0]); auto value = Token::literalToUnsigned(params[1]); @@ -111,17 +110,17 @@ namespace hex::plugin::builtin { }); /* base_address() */ - ContentRegistry::PatternLanguageFunctions::add(nsStdMem, "base_address", ContentRegistry::PatternLanguageFunctions::NoParameters, [](Evaluator *ctx, auto params) -> std::optional { + ContentRegistry::PatternLanguage::addFunction(nsStdMem, "base_address", ContentRegistry::PatternLanguage::NoParameters, [](Evaluator *ctx, auto params) -> std::optional { return u128(ctx->getProvider()->getBaseAddress()); }); /* size() */ - ContentRegistry::PatternLanguageFunctions::add(nsStdMem, "size", ContentRegistry::PatternLanguageFunctions::NoParameters, [](Evaluator *ctx, auto params) -> std::optional { + ContentRegistry::PatternLanguage::addFunction(nsStdMem, "size", ContentRegistry::PatternLanguage::NoParameters, [](Evaluator *ctx, auto params) -> std::optional { return u128(ctx->getProvider()->getActualSize()); }); /* find_sequence(occurrence_index, bytes...) */ - ContentRegistry::PatternLanguageFunctions::add(nsStdMem, "find_sequence", ContentRegistry::PatternLanguageFunctions::MoreParametersThan | 1, [](Evaluator *ctx, auto params) -> std::optional { + ContentRegistry::PatternLanguage::addFunction(nsStdMem, "find_sequence", ContentRegistry::PatternLanguage::MoreParametersThan | 1, [](Evaluator *ctx, auto params) -> std::optional { auto occurrenceIndex = Token::literalToUnsigned(params[0]); std::vector sequence; @@ -153,7 +152,7 @@ namespace hex::plugin::builtin { }); /* read_unsigned(address, size) */ - ContentRegistry::PatternLanguageFunctions::add(nsStdMem, "read_unsigned", 2, [](Evaluator *ctx, auto params) -> std::optional { + ContentRegistry::PatternLanguage::addFunction(nsStdMem, "read_unsigned", 2, [](Evaluator *ctx, auto params) -> std::optional { auto address = Token::literalToUnsigned(params[0]); auto size = Token::literalToUnsigned(params[1]); @@ -167,7 +166,7 @@ namespace hex::plugin::builtin { }); /* read_signed(address, size) */ - ContentRegistry::PatternLanguageFunctions::add(nsStdMem, "read_signed", 2, [](Evaluator *ctx, auto params) -> std::optional { + ContentRegistry::PatternLanguage::addFunction(nsStdMem, "read_signed", 2, [](Evaluator *ctx, auto params) -> std::optional { auto address = Token::literalToUnsigned(params[0]); auto size = Token::literalToUnsigned(params[1]); @@ -180,7 +179,7 @@ namespace hex::plugin::builtin { }); /* read_string(address, size) */ - ContentRegistry::PatternLanguageFunctions::add(nsStdMem, "read_string", 2, [](Evaluator *ctx, auto params) -> std::optional { + ContentRegistry::PatternLanguage::addFunction(nsStdMem, "read_string", 2, [](Evaluator *ctx, auto params) -> std::optional { auto address = Token::literalToUnsigned(params[0]); auto size = Token::literalToUnsigned(params[1]); @@ -192,17 +191,17 @@ namespace hex::plugin::builtin { } - ContentRegistry::PatternLanguageFunctions::Namespace nsStdString = { "std", "string" }; + ContentRegistry::PatternLanguage::Namespace nsStdString = { "std", "string" }; { /* length(string) */ - ContentRegistry::PatternLanguageFunctions::add(nsStdString, "length", 1, [](Evaluator *ctx, auto params) -> std::optional { + ContentRegistry::PatternLanguage::addFunction(nsStdString, "length", 1, [](Evaluator *ctx, auto params) -> std::optional { auto string = Token::literalToString(params[0], false); return u128(string.length()); }); /* at(string, index) */ - ContentRegistry::PatternLanguageFunctions::add(nsStdString, "at", 2, [](Evaluator *ctx, auto params) -> std::optional { + ContentRegistry::PatternLanguage::addFunction(nsStdString, "at", 2, [](Evaluator *ctx, auto params) -> std::optional { auto string = Token::literalToString(params[0], false); auto index = Token::literalToSigned(params[1]); @@ -223,7 +222,7 @@ namespace hex::plugin::builtin { }); /* substr(string, pos, count) */ - ContentRegistry::PatternLanguageFunctions::add(nsStdString, "substr", 3, [](Evaluator *ctx, auto params) -> std::optional { + ContentRegistry::PatternLanguage::addFunction(nsStdString, "substr", 3, [](Evaluator *ctx, auto params) -> std::optional { auto string = Token::literalToString(params[0], false); auto pos = Token::literalToUnsigned(params[1]); auto size = Token::literalToUnsigned(params[2]); @@ -235,7 +234,7 @@ namespace hex::plugin::builtin { }); /* parse_int(string, base) */ - ContentRegistry::PatternLanguageFunctions::add(nsStdString, "parse_int", 2, [](Evaluator *ctx, auto params) -> std::optional { + ContentRegistry::PatternLanguage::addFunction(nsStdString, "parse_int", 2, [](Evaluator *ctx, auto params) -> std::optional { auto string = Token::literalToString(params[0], false); auto base = Token::literalToUnsigned(params[1]); @@ -243,7 +242,7 @@ namespace hex::plugin::builtin { }); /* parse_float(string) */ - ContentRegistry::PatternLanguageFunctions::add(nsStdString, "parse_float", 1, [](Evaluator *ctx, auto params) -> std::optional { + ContentRegistry::PatternLanguage::addFunction(nsStdString, "parse_float", 1, [](Evaluator *ctx, auto params) -> std::optional { auto string = Token::literalToString(params[0], false); return double(std::strtod(string.c_str(), nullptr)); @@ -251,26 +250,26 @@ namespace hex::plugin::builtin { } - ContentRegistry::PatternLanguageFunctions::Namespace nsStdHttp = { "std", "http" }; + ContentRegistry::PatternLanguage::Namespace nsStdHttp = { "std", "http" }; { /* get(url) */ - ContentRegistry::PatternLanguageFunctions::add(nsStdHttp, "get", 1, [](Evaluator *ctx, auto params) -> std::optional { + ContentRegistry::PatternLanguage::addDangerousFunction(nsStdHttp, "get", 1, [](Evaluator *ctx, auto params) -> std::optional { const auto url = Token::literalToString(params[0], false); hex::Net net; return net.getString(url).get().body; - }, true); + }); } - ContentRegistry::PatternLanguageFunctions::Namespace nsStdFile = { "std", "file" }; + ContentRegistry::PatternLanguage::Namespace nsStdFile = { "std", "file" }; { static u32 fileCounter = 0; static std::map openFiles; /* open(path, mode) */ - ContentRegistry::PatternLanguageFunctions::add(nsStdFile, "open", 2, [](Evaluator *ctx, auto params) -> std::optional { + ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "open", 2, [](Evaluator *ctx, auto params) -> std::optional { const auto path = Token::literalToString(params[0], false); const auto modeEnum = Token::literalToUnsigned(params[1]); @@ -292,10 +291,10 @@ namespace hex::plugin::builtin { openFiles.emplace(std::pair{ fileCounter, std::move(file) }); return u128(fileCounter); - }, true); + }); /* close(file) */ - ContentRegistry::PatternLanguageFunctions::add(nsStdFile, "close", 1, [](Evaluator *ctx, auto params) -> std::optional { + ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "close", 1, [](Evaluator *ctx, auto params) -> std::optional { const auto file = Token::literalToUnsigned(params[0]); if (!openFiles.contains(file)) @@ -304,10 +303,10 @@ namespace hex::plugin::builtin { openFiles.erase(file); return { }; - }, true); + }); /* read(file, size) */ - ContentRegistry::PatternLanguageFunctions::add(nsStdFile, "read", 2, [](Evaluator *ctx, auto params) -> std::optional { + ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "read", 2, [](Evaluator *ctx, auto params) -> std::optional { const auto file = Token::literalToUnsigned(params[0]); const auto size = Token::literalToUnsigned(params[1]); @@ -315,10 +314,10 @@ namespace hex::plugin::builtin { LogConsole::abortEvaluation("failed to access invalid file"); return openFiles[file].readString(size); - }, true); + }); /* write(file, data) */ - ContentRegistry::PatternLanguageFunctions::add(nsStdFile, "write", 2, [](Evaluator *ctx, auto params) -> std::optional { + ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "write", 2, [](Evaluator *ctx, auto params) -> std::optional { const auto file = Token::literalToUnsigned(params[0]); const auto data = Token::literalToString(params[1], true); @@ -328,10 +327,10 @@ namespace hex::plugin::builtin { openFiles[file].write(data); return { }; - }, true); + }); /* seek(file, offset) */ - ContentRegistry::PatternLanguageFunctions::add(nsStdFile, "seek", 2, [](Evaluator *ctx, auto params) -> std::optional { + ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "seek", 2, [](Evaluator *ctx, auto params) -> std::optional { const auto file = Token::literalToUnsigned(params[0]); const auto offset = Token::literalToUnsigned(params[1]); @@ -341,20 +340,20 @@ namespace hex::plugin::builtin { openFiles[file].seek(offset); return { }; - }, true); + }); /* size(file) */ - ContentRegistry::PatternLanguageFunctions::add(nsStdFile, "size", 1, [](Evaluator *ctx, auto params) -> std::optional { + ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "size", 1, [](Evaluator *ctx, auto params) -> std::optional { const auto file = Token::literalToUnsigned(params[0]); if (!openFiles.contains(file)) LogConsole::abortEvaluation("failed to access invalid file"); return u128(openFiles[file].getSize()); - }, true); + }); /* resize(file, size) */ - ContentRegistry::PatternLanguageFunctions::add(nsStdFile, "resize", 2, [](Evaluator *ctx, auto params) -> std::optional { + ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "resize", 2, [](Evaluator *ctx, auto params) -> std::optional { const auto file = Token::literalToUnsigned(params[0]); const auto size = Token::literalToUnsigned(params[1]); @@ -364,10 +363,10 @@ namespace hex::plugin::builtin { openFiles[file].setSize(size); return { }; - }, true); + }); /* flush(file) */ - ContentRegistry::PatternLanguageFunctions::add(nsStdFile, "flush", 1, [](Evaluator *ctx, auto params) -> std::optional { + ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "flush", 1, [](Evaluator *ctx, auto params) -> std::optional { const auto file = Token::literalToUnsigned(params[0]); if (!openFiles.contains(file)) @@ -376,10 +375,10 @@ namespace hex::plugin::builtin { openFiles[file].flush(); return { }; - }, true); + }); /* remove(file) */ - ContentRegistry::PatternLanguageFunctions::add(nsStdFile, "remove", 1, [](Evaluator *ctx, auto params) -> std::optional { + ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "remove", 1, [](Evaluator *ctx, auto params) -> std::optional { const auto file = Token::literalToUnsigned(params[0]); if (!openFiles.contains(file)) @@ -388,7 +387,7 @@ namespace hex::plugin::builtin { openFiles[file].remove(); return { }; - }, true); + }); } } diff --git a/plugins/libimhex/include/hex/api/content_registry.hpp b/plugins/libimhex/include/hex/api/content_registry.hpp index 3f603a652..15f044f27 100644 --- a/plugins/libimhex/include/hex/api/content_registry.hpp +++ b/plugins/libimhex/include/hex/api/content_registry.hpp @@ -81,7 +81,7 @@ namespace hex { } /* Pattern Language Function Registry. Allows adding of new functions that may be used inside the pattern language */ - namespace PatternLanguageFunctions { + namespace PatternLanguage { constexpr static u32 UnlimitedParameters = 0xFFFF'FFFF; constexpr static u32 MoreParametersThan = 0x8000'0000; @@ -97,8 +97,9 @@ namespace hex { bool dangerous; }; - void add(const Namespace &ns, const std::string &name, u32 parameterCount, const Callback &func, bool dangerous = false); - std::map& getEntries(); + void addFunction(const Namespace &ns, const std::string &name, u32 parameterCount, const Callback &func); + void addDangerousFunction(const Namespace &ns, const std::string &name, u32 parameterCount, const Callback &func); + std::map& getFunctions(); } /* View Registry. Allows adding of new windows */ diff --git a/plugins/libimhex/include/hex/helpers/shared_data.hpp b/plugins/libimhex/include/hex/helpers/shared_data.hpp index 501dea486..69082d2f0 100644 --- a/plugins/libimhex/include/hex/helpers/shared_data.hpp +++ b/plugins/libimhex/include/hex/helpers/shared_data.hpp @@ -62,7 +62,7 @@ namespace hex { static std::map> settingsEntries; static nlohmann::json settingsJson; static std::vector commandPaletteCommands; - static std::map patternLanguageFunctions; + static std::map patternLanguageFunctions; static std::vector views; static std::vector toolsEntries; static std::vector dataInspectorEntries; diff --git a/plugins/libimhex/include/hex/pattern_language/ast_node.hpp b/plugins/libimhex/include/hex/pattern_language/ast_node.hpp index 49b3c7dc0..721781aa8 100644 --- a/plugins/libimhex/include/hex/pattern_language/ast_node.hpp +++ b/plugins/libimhex/include/hex/pattern_language/ast_node.hpp @@ -1878,7 +1878,7 @@ namespace hex::pl { } auto &customFunctions = evaluator->getCustomFunctions(); - auto functions = ContentRegistry::PatternLanguageFunctions::getEntries(); + auto functions = ContentRegistry::PatternLanguage::getFunctions(); for (auto &func : customFunctions) functions.insert(func); @@ -1887,15 +1887,15 @@ namespace hex::pl { LogConsole::abortEvaluation(hex::format("call to unknown function '{}'", this->m_functionName), this); auto function = functions[this->m_functionName]; - if (function.parameterCount == ContentRegistry::PatternLanguageFunctions::UnlimitedParameters) { + if (function.parameterCount == ContentRegistry::PatternLanguage::UnlimitedParameters) { ; // Don't check parameter count } - else if (function.parameterCount & ContentRegistry::PatternLanguageFunctions::LessParametersThan) { - if (evaluatedParams.size() >= (function.parameterCount & ~ContentRegistry::PatternLanguageFunctions::LessParametersThan)) - LogConsole::abortEvaluation(hex::format("too many parameters for function '{0}'. Expected {1}", this->m_functionName, function.parameterCount & ~ContentRegistry::PatternLanguageFunctions::LessParametersThan), this); - } else if (function.parameterCount & ContentRegistry::PatternLanguageFunctions::MoreParametersThan) { - if (evaluatedParams.size() <= (function.parameterCount & ~ContentRegistry::PatternLanguageFunctions::MoreParametersThan)) - LogConsole::abortEvaluation(hex::format("too few parameters for function '{0}'. Expected {1}", this->m_functionName, function.parameterCount & ~ContentRegistry::PatternLanguageFunctions::MoreParametersThan), this); + else if (function.parameterCount & ContentRegistry::PatternLanguage::LessParametersThan) { + if (evaluatedParams.size() >= (function.parameterCount & ~ContentRegistry::PatternLanguage::LessParametersThan)) + LogConsole::abortEvaluation(hex::format("too many parameters for function '{0}'. Expected {1}", this->m_functionName, function.parameterCount & ~ContentRegistry::PatternLanguage::LessParametersThan), this); + } else if (function.parameterCount & ContentRegistry::PatternLanguage::MoreParametersThan) { + if (evaluatedParams.size() <= (function.parameterCount & ~ContentRegistry::PatternLanguage::MoreParametersThan)) + LogConsole::abortEvaluation(hex::format("too few parameters for function '{0}'. Expected {1}", this->m_functionName, function.parameterCount & ~ContentRegistry::PatternLanguage::MoreParametersThan), this); } else if (function.parameterCount != evaluatedParams.size()) { LogConsole::abortEvaluation(hex::format("invalid number of parameters for function '{0}'. Expected {1}", this->m_functionName, function.parameterCount), this); } diff --git a/plugins/libimhex/include/hex/pattern_language/evaluator.hpp b/plugins/libimhex/include/hex/pattern_language/evaluator.hpp index 4396c459b..74d62a542 100644 --- a/plugins/libimhex/include/hex/pattern_language/evaluator.hpp +++ b/plugins/libimhex/include/hex/pattern_language/evaluator.hpp @@ -142,14 +142,14 @@ namespace hex::pl { u64& dataOffset() { return this->m_currOffset; } - bool addCustomFunction(const std::string &name, u32 numParams, const ContentRegistry::PatternLanguageFunctions::Callback &function) { + bool addCustomFunction(const std::string &name, u32 numParams, const ContentRegistry::PatternLanguage::Callback &function) { const auto [iter, inserted] = this->m_customFunctions.insert({ name, { numParams, function } }); return inserted; } [[nodiscard]] - const std::map& getCustomFunctions() const { + const std::map& getCustomFunctions() const { return this->m_customFunctions; } @@ -227,7 +227,7 @@ namespace hex::pl { std::atomic m_aborted; std::vector m_scopes; - std::map m_customFunctions; + std::map m_customFunctions; std::vector m_customFunctionDefinitions; std::vector m_stack; diff --git a/plugins/libimhex/include/hex/pattern_language/pattern_data.hpp b/plugins/libimhex/include/hex/pattern_language/pattern_data.hpp index b10fe02f0..84c90a5ac 100644 --- a/plugins/libimhex/include/hex/pattern_language/pattern_data.hpp +++ b/plugins/libimhex/include/hex/pattern_language/pattern_data.hpp @@ -129,9 +129,9 @@ namespace hex::pl { [[nodiscard]] Evaluator* getEvaluator() const { return this->m_evaluator; } [[nodiscard]] const auto& getTransformFunction() const { return this->m_transformFunction; } - void setTransformFunction(const ContentRegistry::PatternLanguageFunctions::Function &function) { this->m_transformFunction = function; } + void setTransformFunction(const ContentRegistry::PatternLanguage::Function &function) { this->m_transformFunction = function; } [[nodiscard]] const auto& getFormatterFunction() const { return this->m_formatterFunction; } - void setFormatterFunction(const ContentRegistry::PatternLanguageFunctions::Function &function) { this->m_formatterFunction = function; } + void setFormatterFunction(const ContentRegistry::PatternLanguage::Function &function) { this->m_formatterFunction = function; } virtual void createEntry(prv::Provider* &provider) = 0; [[nodiscard]] virtual std::string getFormattedName() const = 0; @@ -317,8 +317,8 @@ namespace hex::pl { std::string m_typeName; Evaluator *m_evaluator = nullptr; - std::optional m_formatterFunction; - std::optional m_transformFunction; + std::optional m_formatterFunction; + std::optional m_transformFunction; PatternData *m_parent; bool m_local = false; diff --git a/plugins/libimhex/source/api/content_registry.cpp b/plugins/libimhex/source/api/content_registry.cpp index 2cb9365c3..cfcc1b8bd 100644 --- a/plugins/libimhex/source/api/content_registry.cpp +++ b/plugins/libimhex/source/api/content_registry.cpp @@ -167,17 +167,25 @@ namespace hex { /* Pattern Language Functions */ - void ContentRegistry::PatternLanguageFunctions::add(const Namespace &ns, const std::string &name, u32 parameterCount, const ContentRegistry::PatternLanguageFunctions::Callback &func, bool dangerous) { + static std::string getFunctionName(const ContentRegistry::PatternLanguage::Namespace &ns, const std::string &name) { std::string functionName; for (auto &scope : ns) functionName += scope + "::"; functionName += name; - getEntries()[functionName] = Function { parameterCount, func, dangerous }; + return functionName; } - std::map& ContentRegistry::PatternLanguageFunctions::getEntries() { + void ContentRegistry::PatternLanguage::addFunction(const Namespace &ns, const std::string &name, u32 parameterCount, const ContentRegistry::PatternLanguage::Callback &func) { + getFunctions()[getFunctionName(ns, name)] = Function { parameterCount, func, false }; + } + + void ContentRegistry::PatternLanguage::addDangerousFunction(const Namespace &ns, const std::string &name, u32 parameterCount, const ContentRegistry::PatternLanguage::Callback &func) { + getFunctions()[getFunctionName(ns, name)] = Function { parameterCount, func, true }; + } + + std::map& ContentRegistry::PatternLanguage::getFunctions() { return SharedData::patternLanguageFunctions; } diff --git a/plugins/libimhex/source/helpers/shared_data.cpp b/plugins/libimhex/source/helpers/shared_data.cpp index 4472dccb3..87299d109 100644 --- a/plugins/libimhex/source/helpers/shared_data.cpp +++ b/plugins/libimhex/source/helpers/shared_data.cpp @@ -12,7 +12,7 @@ namespace hex { std::map> SharedData::settingsEntries; nlohmann::json SharedData::settingsJson; std::vector SharedData::commandPaletteCommands; - std::map SharedData::patternLanguageFunctions; + std::map SharedData::patternLanguageFunctions; std::vector SharedData::views; std::vector SharedData::toolsEntries; std::vector SharedData::dataInspectorEntries; diff --git a/tests/pattern_language/source/main.cpp b/tests/pattern_language/source/main.cpp index 5cbee911f..01e8fde76 100644 --- a/tests/pattern_language/source/main.cpp +++ b/tests/pattern_language/source/main.cpp @@ -16,8 +16,8 @@ using namespace hex::test; void addFunctions() { - hex::ContentRegistry::PatternLanguageFunctions::Namespace nsStd = { "std" }; - hex::ContentRegistry::PatternLanguageFunctions::add(nsStd, "assert", 2, [](Evaluator *ctx, auto params) -> Token::Literal { + hex::ContentRegistry::PatternLanguage::Namespace nsStd = { "std" }; + hex::ContentRegistry::PatternLanguage::addFunction(nsStd, "assert", 2, [](Evaluator *ctx, auto params) -> Token::Literal { auto condition = Token::literalToBoolean(params[0]); auto message = Token::literalToString(params[1], false);