Skip to content

Commit

Permalink
implement plugin loading for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-kenzel committed Aug 9, 2023
1 parent 35d6164 commit cf2324d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
59 changes: 35 additions & 24 deletions src/thorin/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,51 @@
#endif

namespace thorin {
bool World::load_plugin(const char* plugin_name) {
[[nodiscard]] static void* load_plugin_module(const char* plugin_name) {
#ifdef _WIN32
return false;
return LoadLibraryA(plugin_name);
#else
void *handle = dlopen(plugin_name, RTLD_LAZY | RTLD_GLOBAL);
if (!handle) {
ELOG("Error loading plugin {}: {}", plugin_name, dlerror());
ELOG("Is plugin contained in LD_LIBRARY_PATH?");
return false;
}
dlerror();

char *error;
auto initfunc = reinterpret_cast<plugin_init_func_t*>(dlsym(handle, "init"));
if ((error = dlerror()) != NULL) {
ILOG("Plugin {} did not supply an init function", plugin_name);
} else {
initfunc();
}
return dlopen(plugin_name, RTLD_LAZY | RTLD_GLOBAL);
#endif
}

data_.plugin_modules_.push_back(handle);
return true;
static bool unload_plugin_module(void* plugin_module) {
#ifdef _WIN32
return FreeLibrary(static_cast<HMODULE>(plugin_module)) == TRUE;
#else
return dlclose(plugin_module) == 0;
#endif
}

template <typename T>
static T* lookup_plugin_function(void* plugin_module, const char* function_name) {
[[nodiscard]] static T* lookup_plugin_function(void* plugin_module, const char* function_name) {
#ifdef _WIN32
return reinterpret_cast<T*>(GetProcAddress(static_cast<HMODULE>(plugin_module), function_name));
#else
if (void* func = dlsym(plugin_module, function_name)) {
return reinterpret_cast<T*>(func);
}
return reinterpret_cast<T*>(dlsym(plugin_module, function_name));
#endif
return nullptr;
}

bool World::load_plugin(const char* plugin_name) {
void* module = load_plugin_module(plugin_name);

if (!module) {
ELOG("failed to load plugin {}", plugin_name);
return false;
}

if (auto init = lookup_plugin_function<plugin_init_func_t>(module, "init")) {
if (!init()) {
ELOG("failed to initialize plugin {}", plugin_name);
unload_plugin_module(module);
return false;
}
} else {
ILOG("plugin {} did not supply an init function", plugin_name);
}

data_.plugin_modules_.push_back(module);
return true;
}

unique_plugin_intrinsic World::load_plugin_intrinsic(const char* function_name) const {
Expand Down
2 changes: 1 addition & 1 deletion src/thorin/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace thorin {
using unique_plugin_intrinsic = std::unique_ptr<plugin_intrinsic, plugin_deleter>;

extern "C" {
using plugin_init_func_t = void();
using plugin_init_func_t = bool();
using plugin_intrinsic_create_func_t = plugin_intrinsic*();
}
}
Expand Down

0 comments on commit cf2324d

Please sign in to comment.