Skip to content

Commit

Permalink
Fix dlopen functions
Browse files Browse the repository at this point in the history
  • Loading branch information
acquamarin committed Aug 20, 2024
1 parent 56e5d80 commit f8ecb2d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 35 deletions.
37 changes: 37 additions & 0 deletions src/extension/extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
#include "main/client_context.h"
#include "main/database.h"
#include "transaction/transaction.h"
#ifdef _WIN32

#include "windows.h"
#define RTLD_NOW 0
#define RTLD_LOCAL 0

#else
#include <dlfcn.h>
#endif

namespace kuzu {
namespace extension {
Expand Down Expand Up @@ -197,5 +206,33 @@ main::ExtensionOption* ExtensionOptions::getExtensionOption(std::string name) {
return extensionOptions.contains(name) ? &extensionOptions.at(name) : nullptr;
}

#ifdef _WIN32
std::wstring utf8ToUnicode(const char* input) {
uint32_t result;

result = MultiByteToWideChar(CP_UTF8, 0, input, -1, nullptr, 0);
if (result == 0) {
throw common::IOException("Failure in MultiByteToWideChar");
}
auto buffer = std::make_unique<wchar_t[]>(result);
result = MultiByteToWideChar(CP_UTF8, 0, input, -1, buffer.get(), result);
if (result == 0) {
throw common::IOException("Failure in MultiByteToWideChar");
}
return std::wstring(buffer.get(), result);
}

void* dlopen(const char* file, int /*mode*/) {
KU_ASSERT(file);
auto fpath = utf8ToUnicode(file);
return (void*)LoadLibraryW(fpath.c_str());
}

void* dlsym(void* handle, const char* name) {
KU_ASSERT(handle);
return (void*)GetProcAddress((HINSTANCE)handle, name);
}
#endif

} // namespace extension
} // namespace kuzu
40 changes: 5 additions & 35 deletions src/include/extension/extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@
#include "main/db_config.h"
#include "common/exception/io.h"

#ifdef _WIN32

#include "windows.h"
#define RTLD_NOW 0
#define RTLD_LOCAL 0

#else
#include <dlfcn.h>
#endif

#define ADD_FUNC(FUNC_STRUCT) \
kuzu::extension::ExtensionUtils::registerFunctionSet(db, std::string(FUNC_STRUCT::name), \
FUNC_STRUCT::getFunctionSet())
Expand Down Expand Up @@ -137,31 +127,11 @@ struct ExtensionOptions {
};

#ifdef _WIN32
std::wstring utf8ToUnicode(const char* input) {
uint32_t result;

result = MultiByteToWideChar(CP_UTF8, 0, input, -1, nullptr, 0);
if (result == 0) {
throw common::IOException("Failure in MultiByteToWideChar");
}
auto buffer = std::make_unique<wchar_t[]>(result);
result = MultiByteToWideChar(CP_UTF8, 0, input, -1, buffer.get(), result);
if (result == 0) {
throw common::IOException("Failure in MultiByteToWideChar");
}
return std::wstring(buffer.get(), result);
}

void* dlopen(const char* file, int /*mode*/) {
KU_ASSERT(file);
auto fpath = utf8ToUnicode(file);
return (void*)LoadLibraryW(fpath.c_str());
}

void* dlsym(void* handle, const char* name) {
KU_ASSERT(handle);
return (void*)GetProcAddress((HINSTANCE)handle, name);
}
std::wstring utf8ToUnicode(const char* input);

void* dlopen(const char* file, int /*mode*/);

void* dlsym(void* handle, const char* name);
#endif

} // namespace extension
Expand Down

0 comments on commit f8ecb2d

Please sign in to comment.