From 7bee7847e97d86608aabed10b26362f6dd9a6791 Mon Sep 17 00:00:00 2001 From: Nathanne Isip Date: Fri, 13 Dec 2024 18:33:14 +0800 Subject: [PATCH] Fixed implementation of VariableDeclarationExpression::loadNativeFunction() for Windows target. --- .../VariableDeclarationExpression.cpp | 44 +++++++------------ 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/src/n8/ast/expression/VariableDeclarationExpression.cpp b/src/n8/ast/expression/VariableDeclarationExpression.cpp index 787226c..c042305 100644 --- a/src/n8/ast/expression/VariableDeclarationExpression.cpp +++ b/src/n8/ast/expression/VariableDeclarationExpression.cpp @@ -85,40 +85,28 @@ NativeFunction VariableDeclarationExpression::loadNativeFunction( ); #endif else { + #if defined(_WIN32) || defined(_WIN64) || defined(WIN32) || defined(WIN64) + std::filesystem::path path(library); std::filesystem::path parentFolder = path.parent_path(); - for(const auto& entry : std::filesystem::directory_iterator(parentFolder)) - if(entry.is_regular_file() && - #if defined(__APPLE__) - entry.path().extension() == ".dylib" - #elif defined(__unix__) || defined(__linux__) - entry.path().extension() == ".so" - #elif defined(_WIN32) || defined(_WIN64) || defined(WIN32) || defined(WIN64) - entry.path().extension() == ".dll" - #endif - ) { - std::string entryDep = entry.path().filename().string(); - void* depHandle; - - #if defined(__APPLE__) - depHandle = dlopen(entryDep.c_str(), RTLD_LAZY); - #elif defined(__unix__) || defined(__linux__) - depHandle = dlopen(entryDep.c_str(), RTLD_LAZY); - #elif defined(_WIN32) || defined(_WIN64) || defined(WIN32) || defined(WIN64) - depHandle = LoadLibraryA(entryDep.c_str()); - #endif - - if(!Runtime::hasLoadedLibrary(entryDep)) - Runtime::addLoadedLibrary(entryDep, depHandle); - } - - #if defined(__APPLE__) + std::string parentPathFolder = path.parent_path(); + std::wstring wstr(parentPathFolder.begin(), parentPathFolder.end()); + + PWSTR searchPath = static_cast( + CoTaskMemAlloc((wstr.size() + 1) * sizeof(wchar_t)) + ); + + if(searchPath) + wcscpy_s(searchPath, wstr.size() + 1, wstr.c_str()); + + AddDllDirectory(searchPath); + handle = LoadLibraryA(library.c_str()); + + #elif defined(__APPLE__) handle = dlopen(library.c_str(), RTLD_LAZY); #elif defined(__unix__) || defined(__linux__) handle = dlopen(library.c_str(), RTLD_LAZY); - #elif defined(_WIN32) || defined(_WIN64) || defined(WIN32) || defined(WIN64) - handle = LoadLibraryA(library.c_str()); #endif Runtime::addLoadedLibrary(library, handle);