diff --git a/src/n8/ast/expression/VariableDeclarationExpression.cpp b/src/n8/ast/expression/VariableDeclarationExpression.cpp index 0b50e13..53d771c 100644 --- a/src/n8/ast/expression/VariableDeclarationExpression.cpp +++ b/src/n8/ast/expression/VariableDeclarationExpression.cpp @@ -58,7 +58,7 @@ DynamicObject VariableDeclarationExpression::visit(SymbolTable& symbols) { return {}; } -#include + NativeFunction VariableDeclarationExpression::loadNativeFunction( std::string& libName, std::string& funcName, @@ -69,22 +69,17 @@ NativeFunction VariableDeclarationExpression::loadNativeFunction( std::string(libName.c_str()) ); - std::filesystem::path path(library); - std::cout << "Loading module: " << library << std::endl; - std::cout << "Loading DLLs from: " << path.parent_path().string() << std::endl; - + std::filesystem::path searchPath(library); if(Runtime::hasLoadedLibrary(library)) #if defined(__unix__) || defined(__linux__) || defined(__APPLE__) handle = Runtime::getLoadedLibrary(library); #elif defined(_WIN32) || defined(_WIN64) || defined(WIN32) || defined(WIN64) - handle = static_cast( - Runtime::getLoadedLibrary(library) - ); + handle = static_cast(Runtime::getLoadedLibrary(library)); #endif else { #if defined(_WIN32) || defined(_WIN64) || defined(WIN32) || defined(WIN64) - AddDllDirectory(path.parent_path().wstring().c_str()); + AddDllDirectory(searchPath.parent_path().wstring().c_str()); handle = LoadLibraryA(library.c_str()); #elif defined(__APPLE__) diff --git a/src/n8/util/PathHelper.cpp b/src/n8/util/PathHelper.cpp index 5cbca3d..de2bac7 100644 --- a/src/n8/util/PathHelper.cpp +++ b/src/n8/util/PathHelper.cpp @@ -21,6 +21,8 @@ #include #include +#include +#include namespace N8Util { @@ -43,37 +45,47 @@ bool PathHelper::isLibraryInstalled( } std::string PathHelper::findSharedLibrary(std::string name) { - if(std::filesystem::exists(name)) - return name; + std::string + #if defined(__APPLE__) + extension = ".dylib"; + #elif defined(__unix__) || defined(__linux__) || defined(__APPLE__) + extension = ".so"; + #elif defined(_WIN32) || defined(_WIN64) || defined(WIN32) || defined(WIN64) + extension = ".dll"; + #endif + + std::string rawName = name + extension; + if(std::filesystem::exists(rawName)) + return rawName; namespace fs = std::filesystem; - std::string directoryPath = PathHelper::installationPath() + + std::string libPath = PathHelper::installationPath() + FS_FILE_SEPARATOR + "modules"; - try { - if(fs::exists(directoryPath) && - fs::is_directory(directoryPath) - ) { - std::string libPath = directoryPath + - FS_FILE_SEPARATOR + name + - FS_FILE_SEPARATOR + "lib" + - FS_FILE_SEPARATOR + name; - - #if defined(__APPLE__) - libPath += ".dylib"; - #elif defined(__unix__) || defined(__linux__) || defined(__APPLE__) - libPath += ".so"; - #elif defined(_WIN32) || defined(_WIN64) || defined(WIN32) || defined(WIN64) - libPath += ".dll"; - #endif - - if(fs::exists(libPath)) - return libPath; - } + for(const auto& modules : std::filesystem::directory_iterator(libPath)) { + std::string modPath = modules.path().string() + + FS_FILE_SEPARATOR + "lib"; + std::string folderName = fs::path(modules) + .filename() + .string(); + + if(folderName.rfind(name, 0) == 0) + for(const auto& entry : std::filesystem::directory_iterator(modPath)) + if(entry.is_regular_file()) { + std::string filename = entry + .path() + .filename() + .string(); + std::string ext = fs::path(filename) + .extension() + .string(); + + if(filename.rfind(name, 0) == 0 && ext == extension) + return entry.path().string(); + } } - catch(const std::exception& ex) {} - return ""; + throw std::runtime_error("Cannot find shared library: " + name); } std::vector PathHelper::getLibraryFiles(