Files
Library/include/Void/Plugin/CAbiAdapter.hpp
T

166 lines
4.7 KiB
C++

#pragma once
#include <Void/Data/Json.hpp>
#include <Void/Plugin/CAbi.h>
#include <Void/Plugin/Module.hpp>
#include <Void/Time/Clock.hpp>
#include <cstdint>
#include <cstring>
#include <exception>
#include <memory>
#include <string>
#include <utility>
namespace Void::Plugin
{
namespace detail
{
inline void free_cabi_cstring(void* /*ctx*/, const char* msg)
{
delete[] msg;
}
inline void set_error_detail(void_error_detail_v1* out_err,
std::int32_t status,
const std::string& msg) noexcept
{
if (out_err == nullptr ||
out_err->struct_size < sizeof(void_error_detail_v1)) {
return;
}
out_err->status = status;
try {
char* buf = new char[msg.size() + 1];
std::memcpy(buf, msg.c_str(), msg.size() + 1);
out_err->message = buf;
out_err->release = &free_cabi_cstring;
out_err->release_ctx = nullptr;
} catch (...) {
out_err->message = nullptr;
out_err->release = nullptr;
out_err->release_ctx = nullptr;
}
}
inline void free_cabi_bytes(void* /*ctx*/, const std::uint8_t* data,
std::uint64_t /*size*/)
{
delete[] data;
}
} // namespace detail
/// Exception-safe bridge adapting a C++ IModule instance to the C11
/// void_module_api_v1 contract without letting C++ exceptions unwind across C.
struct CModuleAdapterBox {
std::unique_ptr<IModule> module;
Service::ServiceRegistry fallback_registry;
ModuleHost host{fallback_registry};
const Void::Time::MonotonicClock* clock{
&Void::Time::SystemMonotonicClock::instance()};
};
template <typename CallbackFn>
[[nodiscard]] inline std::int32_t
invoke_contained(void_error_detail_v1* out_err, CallbackFn&& fn) noexcept
{
try {
return fn();
} catch (const std::exception& ex) {
detail::set_error_detail(out_err, VOID_STATUS_INTERNAL, ex.what());
return VOID_STATUS_INTERNAL;
} catch (...) {
detail::set_error_detail(out_err, VOID_STATUS_INTERNAL,
"CAbiAdapter: unknown C++ exception");
return VOID_STATUS_INTERNAL;
}
}
[[nodiscard]] inline std::int32_t
adapt_checkpoint(CModuleAdapterBox* box,
void_owned_bytes_v1* out_state,
void_error_detail_v1* out_err) noexcept
{
if (box == nullptr || !box->module || out_state == nullptr) {
return VOID_STATUS_INVALID_ARGUMENT;
}
return invoke_contained(out_err, [&]() -> std::int32_t {
const auto json = Void::Json::serialize(box->module->checkpoint());
auto* buf = new std::uint8_t[json.size()];
if (!json.empty()) {
std::memcpy(buf, json.data(), json.size());
}
out_state->data = buf;
out_state->size = static_cast<std::uint64_t>(json.size());
out_state->release = &detail::free_cabi_bytes;
out_state->release_ctx = nullptr;
return VOID_STATUS_OK;
});
}
[[nodiscard]] inline std::int32_t
adapt_restore(CModuleAdapterBox* box, void_bytes_v1 state,
void_error_detail_v1* out_err) noexcept
{
if (box == nullptr || !box->module) {
return VOID_STATUS_INVALID_ARGUMENT;
}
return invoke_contained(out_err, [&]() -> std::int32_t {
const std::string_view sv{
reinterpret_cast<const char*>(state.data),
static_cast<std::size_t>(state.size)};
const auto parsed = Void::Json::parse(sv);
if (!parsed.has_value()) {
detail::set_error_detail(out_err, VOID_STATUS_CORRUPT,
parsed.error().message);
return VOID_STATUS_CORRUPT;
}
const auto res = box->module->restore(*parsed);
if (!res.has_value()) {
detail::set_error_detail(out_err, VOID_STATUS_STATE_REJECTED,
res.error().message());
return VOID_STATUS_STATE_REJECTED;
}
return VOID_STATUS_OK;
});
}
[[nodiscard]] inline std::int32_t
adapt_activate(CModuleAdapterBox* box,
void_error_detail_v1* out_err) noexcept
{
if (box == nullptr || !box->module) {
return VOID_STATUS_INVALID_ARGUMENT;
}
return invoke_contained(out_err, [&]() -> std::int32_t {
const auto r = box->module->activate(box->host);
if (!r.has_value()) {
detail::set_error_detail(out_err, VOID_STATUS_INTERNAL,
r.error().message());
return VOID_STATUS_INTERNAL;
}
return VOID_STATUS_OK;
});
}
[[nodiscard]] inline std::int32_t
adapt_readiness(CModuleAdapterBox* box, std::uint32_t* out_ready,
void_error_detail_v1* out_err) noexcept
{
if (box == nullptr || !box->module || out_ready == nullptr) {
return VOID_STATUS_INVALID_ARGUMENT;
}
*out_ready = 0;
return invoke_contained(out_err, [&]() -> std::int32_t {
const auto r = box->module->readiness();
*out_ready = r.ready ? 1u : 0u;
if (!r.ready && !r.detail.empty()) {
detail::set_error_detail(out_err, VOID_STATUS_NOT_READY,
r.detail);
}
return VOID_STATUS_OK;
});
}
} // namespace Void::Plugin