From 5b7da77027440cae969878d0aec931209a902256 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 24 Aug 2019 18:52:11 +0200 Subject: [PATCH] Improvements for extern C scriptoptions calling --- src/Binder/Binder.cpp | 4 ++-- src/ScriptOptions.cpp | 14 +++++++------- src/ScriptOptions.hpp | 20 ++++++++++---------- tests/integration/ModuleTests.cpp | 4 ++-- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/Binder/Binder.cpp b/src/Binder/Binder.cpp index 904288d..dbf2c9b 100644 --- a/src/Binder/Binder.cpp +++ b/src/Binder/Binder.cpp @@ -645,13 +645,13 @@ namespace Porygon::Binder { auto opt = this->_scriptData->GetScriptOptions(); auto transformedKey = Utilities::StringUtils::FromUTF8(key); delete boundParameter; - if (!opt->DoesModuleExist(transformedKey)) { + if (!opt->DoesModuleExist(transformedKey.c_str(), transformedKey.size())) { this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::ModuleDoesntExist, exp->GetStartPosition(), exp->GetLength()); return new BoundBadExpression(exp->GetStartPosition(), exp->GetLength()); } - auto module = Script::Clone(opt->ResolveModule(transformedKey)); + auto module = Script::Clone(opt->ResolveModule(transformedKey.c_str(), transformedKey.size())); if (module->GetReturnType() == nullptr) { for (const auto &v: *module->GetScriptVariables()) { auto type = module->GetVariableType(v.first); diff --git a/src/ScriptOptions.cpp b/src/ScriptOptions.cpp index e3a0d24..eb90bf3 100644 --- a/src/ScriptOptions.cpp +++ b/src/ScriptOptions.cpp @@ -14,12 +14,12 @@ void Porygon::ScriptOptions::DefaultPrint(const char16_t *s) { Porygon::ScriptOptions::GetDefaultScriptOptions()->GetPrintStream() << Porygon::Utilities::StringUtils::FromUTF8(s) << std::endl; } -bool Porygon::ScriptOptions::DefaultModuleExists(const std::string& moduleName) { +bool Porygon::ScriptOptions::DefaultModuleExists(const char* moduleName, size_t size) { struct stat buffer; - return (stat (moduleName.c_str(), &buffer) == 0); + return (stat (moduleName, &buffer) == 0); } -Porygon::Script *Porygon::ScriptOptions::DefaultResolveModule(const std::string& moduleName) { +Porygon::Script *Porygon::ScriptOptions::DefaultResolveModule(const char* moduleName, size_t size) { auto stream = std::ifstream(moduleName); std::basic_stringstream stringStream; stringStream << stream.rdbuf(); @@ -32,11 +32,11 @@ extern "C"{ Porygon::ScriptOptions::GetDefaultScriptOptions()->SetPrintFunc(func); } - void SetDefaultModuleExists(bool (*func)(const std::string& moduleName)){ + void SetDefaultModuleExists(bool (*func)(const char* moduleName, size_t size)){ Porygon::ScriptOptions::GetDefaultScriptOptions()->SetModuleExistsFunc(func); } - void SetDefaultResolveModule(Porygon::Script * (*func)(const std::string& moduleName)){ + void SetDefaultResolveModule(Porygon::Script * (*func)(const char* moduleName, size_t size)){ Porygon::ScriptOptions::GetDefaultScriptOptions()->SetResolveModuleFunc(func); } @@ -48,11 +48,11 @@ extern "C"{ opt->SetPrintFunc(func); } - void SetOptionModuleExistsFunc(Porygon::ScriptOptions* opt, bool (*func)(const std::string& moduleName)){ + void SetOptionModuleExistsFunc(Porygon::ScriptOptions* opt, bool (*func)(const char* moduleName, size_t size)){ opt->SetModuleExistsFunc(func); } - void SetOptionResolveModuleFunc(Porygon::ScriptOptions* opt, Porygon::Script* (*func)(const std::string& moduleName)){ + void SetOptionResolveModuleFunc(Porygon::ScriptOptions* opt, Porygon::Script* (*func)(const char* moduleName, size_t size)){ opt->SetResolveModuleFunc(func); } } \ No newline at end of file diff --git a/src/ScriptOptions.hpp b/src/ScriptOptions.hpp index 240b17d..960fea4 100644 --- a/src/ScriptOptions.hpp +++ b/src/ScriptOptions.hpp @@ -9,12 +9,12 @@ namespace Porygon{ class ScriptOptions{ static Porygon::ScriptOptions DefaultScriptOptions; static void DefaultPrint(const char16_t* s); - static bool DefaultModuleExists(const std::string& moduleName); - static Script* DefaultResolveModule(const std::string& moduleName); + static bool DefaultModuleExists(const char* moduleName, size_t size); + static Script* DefaultResolveModule(const char* moduleName, size_t size); void (*_print)(const char16_t* s) = DefaultPrint; - bool (*_doesModuleExist)(const std::string& moduleName) = DefaultModuleExists; - Script* (*_resolveModule)(const std::string& moduleName) = DefaultResolveModule; + bool (*_doesModuleExist)(const char* moduleName, size_t size) = DefaultModuleExists; + Script* (*_resolveModule)(const char* moduleName, size_t size) = DefaultResolveModule; static std::streambuf* _printBuffer; static std::ostream* _printStream; @@ -28,23 +28,23 @@ namespace Porygon{ this -> _print(s); } - inline bool DoesModuleExist(std::string moduleName) const{ - return _doesModuleExist(std::move(moduleName)); + [[nodiscard]] inline bool DoesModuleExist(const char* moduleName, size_t size) const{ + return _doesModuleExist(moduleName, size); } - inline Script* ResolveModule(std::string moduleName) const{ - return _resolveModule(std::move(moduleName)); + [[nodiscard]] inline Script* ResolveModule(const char* moduleName, size_t size) const{ + return _resolveModule(moduleName, size); } void SetPrintFunc(void (*print)(const char16_t *)){ this -> _print = print; } - void SetModuleExistsFunc(bool (*doesModuleExist)(const std::string& moduleName)){ + void SetModuleExistsFunc(bool (*doesModuleExist)(const char* moduleName, size_t size)){ this ->_doesModuleExist = doesModuleExist; } - void SetResolveModuleFunc(Script* (*resolveModule)(const std::string& moduleName)){ + void SetResolveModuleFunc(Script* (*resolveModule)(const char* moduleName, size_t size)){ this ->_resolveModule = resolveModule; } diff --git a/tests/integration/ModuleTests.cpp b/tests/integration/ModuleTests.cpp index 6a9739d..085b8ac 100644 --- a/tests/integration/ModuleTests.cpp +++ b/tests/integration/ModuleTests.cpp @@ -30,11 +30,11 @@ class ModuleHandler{ return _internal; } - inline static bool DoesModuleExist(const string& moduleName){ + inline static bool DoesModuleExist(const char* moduleName, size_t size){ return GetInternal()->MODULES.find(moduleName) != GetInternal()->MODULES.end(); } - inline static Script* ResolveModule(const string& moduleName){ + inline static Script* ResolveModule(const char* moduleName, size_t size){ return GetInternal()->MODULES[moduleName]; }