Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 95c12a1

Browse files
committedAug 4, 2023
implement plugin loading for Windows
1 parent 2993d99 commit 95c12a1

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed
 

‎src/thorin/plugin.cpp

+35-24
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,51 @@
1010
#endif
1111

1212
namespace thorin {
13-
bool World::load_plugin(const char* plugin_name) {
13+
[[nodiscard]] static void* load_plugin_module(const char* plugin_name) {
1414
#ifdef _WIN32
15-
return false;
15+
return LoadLibraryA(plugin_name);
1616
#else
17-
void *handle = dlopen(plugin_name, RTLD_LAZY | RTLD_GLOBAL);
18-
if (!handle) {
19-
ELOG("Error loading plugin {}: {}", plugin_name, dlerror());
20-
ELOG("Is plugin contained in LD_LIBRARY_PATH?");
21-
return false;
22-
}
23-
dlerror();
24-
25-
char *error;
26-
auto initfunc = reinterpret_cast<plugin_init_func_t*>(dlsym(handle, "init"));
27-
if ((error = dlerror()) != NULL) {
28-
ILOG("Plugin {} did not supply an init function", plugin_name);
29-
} else {
30-
initfunc();
31-
}
17+
return dlopen(plugin_name, RTLD_LAZY | RTLD_GLOBAL);
18+
#endif
19+
}
3220

33-
data_.plugin_modules_.push_back(handle);
34-
return true;
21+
static bool unload_plugin_module(void* plugin_module) {
22+
#ifdef _WIN32
23+
return FreeLibrary(static_cast<HMODULE>(plugin_module)) == TRUE;
24+
#else
25+
return dlclose(plugin_module) == 0;
3526
#endif
3627
}
3728

3829
template <typename T>
39-
static T* lookup_plugin_function(void* plugin_module, const char* function_name) {
30+
[[nodiscard]] static T* lookup_plugin_function(void* plugin_module, const char* function_name) {
4031
#ifdef _WIN32
32+
return reinterpret_cast<T*>(GetProcAddress(static_cast<HMODULE>(plugin_module), function_name));
4133
#else
42-
if (void* func = dlsym(plugin_module, function_name)) {
43-
return reinterpret_cast<T*>(func);
44-
}
34+
return reinterpret_cast<T*>(dlsym(plugin_module, function_name));
4535
#endif
46-
return nullptr;
36+
}
37+
38+
bool World::load_plugin(const char* plugin_name) {
39+
void* module = load_plugin_module(plugin_name);
40+
41+
if (!module) {
42+
ELOG("failed to load plugin {}", plugin_name);
43+
return false;
44+
}
45+
46+
if (auto init = lookup_plugin_function<plugin_init_func_t>(module, "init")) {
47+
if (!init()) {
48+
ELOG("failed to initialize plugin {}", plugin_name);
49+
unload_plugin_module(module);
50+
return false;
51+
}
52+
} else {
53+
ILOG("plugin {} did not supply an init function", plugin_name);
54+
}
55+
56+
data_.plugin_modules_.push_back(module);
57+
return true;
4758
}
4859

4960
unique_plugin_intrinsic World::load_plugin_intrinsic(const char* function_name) const {

‎src/thorin/plugin.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace thorin {
3131
using unique_plugin_intrinsic = std::unique_ptr<plugin_intrinsic, plugin_deleter>;
3232

3333
extern "C" {
34-
using plugin_init_func_t = void();
34+
using plugin_init_func_t = bool();
3535
using plugin_intrinsic_create_func_t = plugin_intrinsic*();
3636
}
3737
}

0 commit comments

Comments
 (0)
Please sign in to comment.