Add 20+ new modules with tests: Pipeline, Matrix, Tokenizer, ScopeGuard, Bitset, Optional, Counted, Curry, TypeMap, Bench, DisjointSet, Histogram, StopWatch, and more
Also fixes Diff/Memo/PriorityQueue from previous session: LineDiff is now an alias for Edit<string> so count_op/apply work on line diffs; Memoized uses std::apply instead of type-based get to handle duplicate arg types. 874/877 tests pass (3 are platform-gated plugin tests). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
cb4dd58500
commit
58ff7b38a0
65 files changed
+8987
-37
No files matched your search
@@ -0,0 +1,172 @@
|
||||
#include <Void/Plugin/Loader.hpp>
|
||||
|
||||
#if defined(__unix__) || defined(__APPLE__)
|
||||
# include <dlfcn.h>
|
||||
# define VOID_HAS_DLOPEN 1
|
||||
#endif
|
||||
|
||||
#include <filesystem>
|
||||
#include <system_error>
|
||||
#include <utility>
|
||||
|
||||
namespace Void::Plugin
|
||||
{
|
||||
|
||||
std::string_view to_string(LoadError e) noexcept
|
||||
{
|
||||
switch (e) {
|
||||
case LoadError::FileNotFound: return "file not found";
|
||||
case LoadError::DlopenFailed: return "dlopen failed";
|
||||
case LoadError::SymbolMissing: return "required symbol missing";
|
||||
case LoadError::AbiMismatch: return "ABI version mismatch";
|
||||
case LoadError::CreateFailed: return "plugin create() returned null";
|
||||
case LoadError::NotSupported: return "platform does not support dlopen";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
// ── internal helpers ──────────────────────────────────────────────────────────
|
||||
|
||||
void PluginLoader::destroy_current() noexcept
|
||||
{
|
||||
#ifdef VOID_HAS_DLOPEN
|
||||
if (plugin_) {
|
||||
plugin_->on_unload();
|
||||
if (destroy_) destroy_(plugin_);
|
||||
plugin_ = nullptr;
|
||||
}
|
||||
if (handle_) {
|
||||
dlclose(handle_);
|
||||
handle_ = nullptr;
|
||||
}
|
||||
destroy_ = nullptr;
|
||||
transfer_ = nullptr;
|
||||
#endif
|
||||
}
|
||||
|
||||
std::expected<void, LoadError>
|
||||
PluginLoader::load_from(const std::filesystem::path& path)
|
||||
{
|
||||
#ifdef VOID_HAS_DLOPEN
|
||||
void* h = dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
|
||||
if (!h) return std::unexpected{LoadError::DlopenFailed};
|
||||
|
||||
const auto sym_info = reinterpret_cast<info_fn> (dlsym(h, kInfoSymbol));
|
||||
const auto sym_create = reinterpret_cast<create_fn> (dlsym(h, kCreateSymbol));
|
||||
const auto sym_destroy = reinterpret_cast<destroy_fn>(dlsym(h, kDestroySymbol));
|
||||
|
||||
if (!sym_info || !sym_create || !sym_destroy) {
|
||||
dlclose(h);
|
||||
return std::unexpected{LoadError::SymbolMissing};
|
||||
}
|
||||
|
||||
const PluginInfo info = sym_info();
|
||||
if (info.abi_version != ABI_VERSION) {
|
||||
dlclose(h);
|
||||
return std::unexpected{LoadError::AbiMismatch};
|
||||
}
|
||||
|
||||
void* raw = sym_create();
|
||||
if (!raw) {
|
||||
dlclose(h);
|
||||
return std::unexpected{LoadError::CreateFailed};
|
||||
}
|
||||
|
||||
handle_ = h;
|
||||
plugin_ = static_cast<IPlugin*>(raw);
|
||||
destroy_ = sym_destroy;
|
||||
transfer_ = reinterpret_cast<transfer_fn>(dlsym(h, kTransferSymbol));
|
||||
info_ = info;
|
||||
return {};
|
||||
#else
|
||||
(void)path;
|
||||
return std::unexpected{LoadError::NotSupported};
|
||||
#endif
|
||||
}
|
||||
|
||||
// ── public API ────────────────────────────────────────────────────────────────
|
||||
|
||||
std::expected<PluginLoader, LoadError>
|
||||
PluginLoader::load(std::filesystem::path path)
|
||||
{
|
||||
if (!std::filesystem::exists(path))
|
||||
return std::unexpected{LoadError::FileNotFound};
|
||||
|
||||
PluginLoader ldr;
|
||||
ldr.path_ = path;
|
||||
|
||||
if (auto r = ldr.load_from(path); !r)
|
||||
return std::unexpected{r.error()};
|
||||
|
||||
ldr.plugin_->on_load();
|
||||
return ldr;
|
||||
}
|
||||
|
||||
PluginLoader::~PluginLoader() { destroy_current(); }
|
||||
|
||||
PluginLoader::PluginLoader(PluginLoader&& o) noexcept
|
||||
: handle_ {std::exchange(o.handle_, nullptr)}
|
||||
, plugin_ {std::exchange(o.plugin_, nullptr)}
|
||||
, destroy_ {std::exchange(o.destroy_, nullptr)}
|
||||
, transfer_{std::exchange(o.transfer_, nullptr)}
|
||||
, info_ {o.info_}
|
||||
, path_ {std::move(o.path_)}
|
||||
{}
|
||||
|
||||
PluginLoader& PluginLoader::operator=(PluginLoader&& o) noexcept
|
||||
{
|
||||
if (this != &o) {
|
||||
destroy_current();
|
||||
handle_ = std::exchange(o.handle_, nullptr);
|
||||
plugin_ = std::exchange(o.plugin_, nullptr);
|
||||
destroy_ = std::exchange(o.destroy_, nullptr);
|
||||
transfer_ = std::exchange(o.transfer_, nullptr);
|
||||
info_ = o.info_;
|
||||
path_ = std::move(o.path_);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::expected<void, LoadError> PluginLoader::reload()
|
||||
{
|
||||
#ifdef VOID_HAS_DLOPEN
|
||||
// Copy to a unique temp path so dlopen gets a fresh inode (bypasses the
|
||||
// path-based cache) even if the file was replaced in-place.
|
||||
const auto tmp = path_.parent_path()
|
||||
/ (path_.filename().string() + ".void_hotswap");
|
||||
|
||||
{
|
||||
std::error_code ec;
|
||||
std::filesystem::copy_file(path_, tmp,
|
||||
std::filesystem::copy_options::overwrite_existing, ec);
|
||||
if (ec) return std::unexpected{LoadError::FileNotFound};
|
||||
}
|
||||
|
||||
// Load new instance while old is still alive (for state transfer).
|
||||
PluginLoader next;
|
||||
next.path_ = path_;
|
||||
if (auto r = next.load_from(tmp); !r) {
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(tmp, ec);
|
||||
return r;
|
||||
}
|
||||
|
||||
// Transfer state with both instances alive.
|
||||
if (plugin_ && next.plugin_ && next.transfer_)
|
||||
next.transfer_(plugin_, next.plugin_);
|
||||
|
||||
// Destroy old, install new.
|
||||
destroy_current();
|
||||
*this = std::move(next);
|
||||
|
||||
plugin_->on_load();
|
||||
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(tmp, ec);
|
||||
return {};
|
||||
#else
|
||||
return std::unexpected{LoadError::NotSupported};
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace Void::Plugin
|
||||
@@ -0,0 +1,3 @@
|
||||
void_add_module(Plugin
|
||||
DEPS_UNIX dl
|
||||
)
|
||||
Reference in new issue
Block a user