Add stable C module ABI and component releases
This commit is contained in:
8 files changed
+535
-70
No files matched your search
@@ -0,0 +1,207 @@
|
||||
#pragma once
|
||||
|
||||
#include <echoion/api/module/Module.hpp>
|
||||
#include <echoion/api/module/ModuleAbi.h>
|
||||
|
||||
#include <Void/Data/Json.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace Echoion::Api::Module
|
||||
{
|
||||
namespace Detail
|
||||
{
|
||||
inline echoion_string_view_v1
|
||||
stringView(const std::string_view value) noexcept
|
||||
{
|
||||
return {.data = value.data(), .size = value.size()};
|
||||
}
|
||||
|
||||
inline echoion_status_v1
|
||||
status(const Void::Result::Result<void>& result) noexcept
|
||||
{
|
||||
return result ? ECHOION_STATUS_OK_V1 : ECHOION_STATUS_INTERNAL_V1;
|
||||
}
|
||||
} // namespace Detail
|
||||
|
||||
// Transitional adapter for existing C++ lifecycle implementations. The
|
||||
// exported descriptor and calls are pure C. C++ ownership and exceptions
|
||||
// remain confined to the module that created the opaque instance.
|
||||
template <Void::Plugin::module_factory_fn Factory>
|
||||
struct LegacyModuleAbi final {
|
||||
struct Instance {
|
||||
std::unique_ptr<Void::Plugin::IModule> module;
|
||||
std::string readinessDetail;
|
||||
};
|
||||
|
||||
static echoion_status_v1
|
||||
create(const echoion_module_host_v1* host,
|
||||
echoion_module_instance_v1* output) noexcept
|
||||
{
|
||||
static_cast<void>(host);
|
||||
if (output == nullptr) {
|
||||
return ECHOION_STATUS_INVALID_ARGUMENT_V1;
|
||||
}
|
||||
try {
|
||||
auto module = Factory();
|
||||
if (!module) {
|
||||
return ECHOION_STATUS_INTERNAL_V1;
|
||||
}
|
||||
auto instance = std::make_unique<Instance>(Instance{
|
||||
.module = std::move(module), .readinessDetail = {}});
|
||||
*output = instance.release();
|
||||
return ECHOION_STATUS_OK_V1;
|
||||
} catch (...) {
|
||||
return ECHOION_STATUS_INTERNAL_V1;
|
||||
}
|
||||
}
|
||||
|
||||
static void destroy(echoion_module_instance_v1 value) noexcept
|
||||
{
|
||||
std::unique_ptr<Instance> instance{
|
||||
static_cast<Instance*>(value)};
|
||||
}
|
||||
|
||||
static echoion_status_v1
|
||||
activate(echoion_module_instance_v1 value) noexcept
|
||||
{
|
||||
Instance* instance = checked(value);
|
||||
if (instance == nullptr) {
|
||||
return ECHOION_STATUS_INVALID_ARGUMENT_V1;
|
||||
}
|
||||
try {
|
||||
instance->module->on_load();
|
||||
return ECHOION_STATUS_OK_V1;
|
||||
} catch (...) {
|
||||
return ECHOION_STATUS_INTERNAL_V1;
|
||||
}
|
||||
}
|
||||
|
||||
static echoion_status_v1
|
||||
quiesce(echoion_module_instance_v1 value,
|
||||
const std::int64_t deadlineNanoseconds) noexcept
|
||||
{
|
||||
Instance* instance = checked(value);
|
||||
if (instance == nullptr || deadlineNanoseconds < 0) {
|
||||
return ECHOION_STATUS_INVALID_ARGUMENT_V1;
|
||||
}
|
||||
try {
|
||||
const auto deadline = std::chrono::steady_clock::time_point{
|
||||
std::chrono::nanoseconds{deadlineNanoseconds}};
|
||||
return Detail::status(instance->module->quiesce(deadline));
|
||||
} catch (...) {
|
||||
return ECHOION_STATUS_INTERNAL_V1;
|
||||
}
|
||||
}
|
||||
|
||||
static echoion_status_v1
|
||||
checkpoint(echoion_module_instance_v1 value,
|
||||
echoion_write_bytes_fn_v1 writer,
|
||||
void* writerContext) noexcept
|
||||
{
|
||||
Instance* instance = checked(value);
|
||||
if (instance == nullptr || writer == nullptr) {
|
||||
return ECHOION_STATUS_INVALID_ARGUMENT_V1;
|
||||
}
|
||||
try {
|
||||
const std::string encoded =
|
||||
Void::Json::serialize(instance->module->checkpoint());
|
||||
return writer(
|
||||
writerContext,
|
||||
{.data = reinterpret_cast<const std::uint8_t*>(
|
||||
encoded.data()),
|
||||
.size = encoded.size()});
|
||||
} catch (...) {
|
||||
return ECHOION_STATUS_INTERNAL_V1;
|
||||
}
|
||||
}
|
||||
|
||||
static echoion_status_v1
|
||||
restore(echoion_module_instance_v1 value,
|
||||
const echoion_bytes_view_v1 state) noexcept
|
||||
{
|
||||
Instance* instance = checked(value);
|
||||
if (instance == nullptr ||
|
||||
(state.data == nullptr && state.size != 0)) {
|
||||
return ECHOION_STATUS_INVALID_ARGUMENT_V1;
|
||||
}
|
||||
try {
|
||||
const std::string_view encoded =
|
||||
state.size == 0
|
||||
? std::string_view{}
|
||||
: std::string_view{
|
||||
reinterpret_cast<const char*>(state.data),
|
||||
state.size};
|
||||
const auto parsed = Void::Json::parse(encoded);
|
||||
if (!parsed) {
|
||||
return ECHOION_STATUS_INVALID_ARGUMENT_V1;
|
||||
}
|
||||
return Detail::status(instance->module->restore(*parsed));
|
||||
} catch (...) {
|
||||
return ECHOION_STATUS_INTERNAL_V1;
|
||||
}
|
||||
}
|
||||
|
||||
static echoion_status_v1
|
||||
readiness(echoion_module_instance_v1 value,
|
||||
echoion_module_readiness_v1* output) noexcept
|
||||
{
|
||||
Instance* instance = checked(value);
|
||||
if (instance == nullptr || output == nullptr ||
|
||||
output->struct_size < sizeof(echoion_module_readiness_v1)) {
|
||||
return ECHOION_STATUS_INVALID_ARGUMENT_V1;
|
||||
}
|
||||
try {
|
||||
const auto result = instance->module->readiness();
|
||||
instance->readinessDetail = result.detail;
|
||||
output->ready = result.ready ? 1U : 0U;
|
||||
output->detail =
|
||||
Detail::stringView(instance->readinessDetail);
|
||||
return ECHOION_STATUS_OK_V1;
|
||||
} catch (...) {
|
||||
return ECHOION_STATUS_INTERNAL_V1;
|
||||
}
|
||||
}
|
||||
|
||||
static echoion_status_v1
|
||||
deactivate(echoion_module_instance_v1 value) noexcept
|
||||
{
|
||||
Instance* instance = checked(value);
|
||||
if (instance == nullptr) {
|
||||
return ECHOION_STATUS_INVALID_ARGUMENT_V1;
|
||||
}
|
||||
try {
|
||||
return Detail::status(instance->module->deactivate());
|
||||
} catch (...) {
|
||||
return ECHOION_STATUS_INTERNAL_V1;
|
||||
}
|
||||
}
|
||||
|
||||
inline static const echoion_module_api_v1 api{
|
||||
.struct_size = sizeof(echoion_module_api_v1),
|
||||
.abi_version = ECHOION_MODULE_ABI_VERSION,
|
||||
.create = create,
|
||||
.destroy = destroy,
|
||||
.activate = activate,
|
||||
.quiesce = quiesce,
|
||||
.checkpoint = checkpoint,
|
||||
.restore = restore,
|
||||
.readiness = readiness,
|
||||
.deactivate = deactivate,
|
||||
.reserved = {},
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
[[nodiscard]] static Instance*
|
||||
checked(echoion_module_instance_v1 value) noexcept
|
||||
{
|
||||
return static_cast<Instance*>(value);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace Echoion::Api::Module
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <echoion/api/module/ModuleAbi.h>
|
||||
|
||||
#include <Void/Plugin/Module.hpp>
|
||||
|
||||
#if defined(_WIN32)
|
||||
@@ -26,6 +28,8 @@ namespace Echoion::Api::Module
|
||||
inline constexpr auto descriptorVersion =
|
||||
Void::Plugin::MODULE_DESCRIPTOR_VERSION;
|
||||
inline constexpr const char* querySymbol = "echoion_module_query";
|
||||
inline constexpr const char* stableQuerySymbol =
|
||||
ECHOION_MODULE_ABI_QUERY_SYMBOL;
|
||||
|
||||
} // namespace Echoion::Api::Module
|
||||
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
#ifndef ECHOION_API_MODULE_MODULE_ABI_H
|
||||
#define ECHOION_API_MODULE_MODULE_ABI_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#if defined(ECHOION_MODULE_BUILD)
|
||||
#define ECHOION_MODULE_ABI_EXPORT __declspec(dllexport)
|
||||
#else
|
||||
#define ECHOION_MODULE_ABI_EXPORT __declspec(dllimport)
|
||||
#endif
|
||||
#else
|
||||
#define ECHOION_MODULE_ABI_EXPORT __attribute__((visibility("default")))
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define ECHOION_MODULE_ABI_VERSION 1U
|
||||
#define ECHOION_MODULE_ABI_QUERY_SYMBOL "echoion_module_abi_query"
|
||||
|
||||
typedef struct echoion_string_view_v1 {
|
||||
const char* data;
|
||||
size_t size;
|
||||
} echoion_string_view_v1;
|
||||
|
||||
typedef struct echoion_bytes_view_v1 {
|
||||
const uint8_t* data;
|
||||
size_t size;
|
||||
} echoion_bytes_view_v1;
|
||||
|
||||
typedef enum echoion_status_v1 {
|
||||
ECHOION_STATUS_OK_V1 = 0,
|
||||
ECHOION_STATUS_INVALID_ARGUMENT_V1 = 1,
|
||||
ECHOION_STATUS_INCOMPATIBLE_V1 = 2,
|
||||
ECHOION_STATUS_UNAVAILABLE_V1 = 3,
|
||||
ECHOION_STATUS_INTERNAL_V1 = 4
|
||||
} echoion_status_v1;
|
||||
|
||||
typedef enum echoion_activation_policy_v1 {
|
||||
ECHOION_ACTIVATION_RELOADABLE_V1 = 0,
|
||||
ECHOION_ACTIVATION_RESTART_REQUIRED_V1 = 1,
|
||||
ECHOION_ACTIVATION_INSTALLER_REQUIRED_V1 = 2
|
||||
} echoion_activation_policy_v1;
|
||||
|
||||
typedef void* echoion_module_instance_v1;
|
||||
|
||||
typedef echoion_status_v1 (*echoion_write_bytes_fn_v1)(
|
||||
void* context, echoion_bytes_view_v1 value);
|
||||
|
||||
typedef echoion_status_v1 (*echoion_query_service_fn_v1)(
|
||||
void* context, echoion_string_view_v1 service_id, uint32_t minimum_version,
|
||||
const void** service);
|
||||
|
||||
typedef void (*echoion_log_fn_v1)(void* context, uint32_t level,
|
||||
echoion_string_view_v1 message);
|
||||
|
||||
typedef struct echoion_module_host_v1 {
|
||||
size_t struct_size;
|
||||
uint32_t abi_version;
|
||||
void* context;
|
||||
echoion_query_service_fn_v1 query_service;
|
||||
echoion_log_fn_v1 log;
|
||||
const void* reserved[8];
|
||||
} echoion_module_host_v1;
|
||||
|
||||
typedef struct echoion_module_readiness_v1 {
|
||||
size_t struct_size;
|
||||
uint8_t ready;
|
||||
uint8_t reserved_bytes[7];
|
||||
echoion_string_view_v1 detail;
|
||||
} echoion_module_readiness_v1;
|
||||
|
||||
typedef struct echoion_module_api_v1 {
|
||||
size_t struct_size;
|
||||
uint32_t abi_version;
|
||||
echoion_status_v1 (*create)(const echoion_module_host_v1* host,
|
||||
echoion_module_instance_v1* instance);
|
||||
void (*destroy)(echoion_module_instance_v1 instance);
|
||||
echoion_status_v1 (*activate)(echoion_module_instance_v1 instance);
|
||||
echoion_status_v1 (*quiesce)(echoion_module_instance_v1 instance,
|
||||
int64_t steady_clock_deadline_nanoseconds);
|
||||
echoion_status_v1 (*checkpoint)(echoion_module_instance_v1 instance,
|
||||
echoion_write_bytes_fn_v1 writer,
|
||||
void* writer_context);
|
||||
echoion_status_v1 (*restore)(echoion_module_instance_v1 instance,
|
||||
echoion_bytes_view_v1 state);
|
||||
echoion_status_v1 (*readiness)(echoion_module_instance_v1 instance,
|
||||
echoion_module_readiness_v1* readiness);
|
||||
echoion_status_v1 (*deactivate)(echoion_module_instance_v1 instance);
|
||||
const void* reserved[8];
|
||||
} echoion_module_api_v1;
|
||||
|
||||
typedef struct echoion_module_dependency_v1 {
|
||||
size_t struct_size;
|
||||
echoion_string_view_v1 component_id;
|
||||
echoion_string_view_v1 version_range;
|
||||
echoion_string_view_v1 abi_hash;
|
||||
uint8_t optional;
|
||||
uint8_t reserved_bytes[7];
|
||||
} echoion_module_dependency_v1;
|
||||
|
||||
typedef struct echoion_module_descriptor_v1 {
|
||||
size_t struct_size;
|
||||
uint32_t abi_version;
|
||||
echoion_string_view_v1 component_id;
|
||||
uint32_t version_major;
|
||||
uint32_t version_minor;
|
||||
uint32_t version_patch;
|
||||
echoion_string_view_v1 version_prerelease;
|
||||
echoion_string_view_v1 abi_hash;
|
||||
const echoion_module_dependency_v1* dependencies;
|
||||
size_t dependency_count;
|
||||
echoion_activation_policy_v1 activation_policy;
|
||||
const echoion_module_api_v1* api;
|
||||
const void* reserved[8];
|
||||
} echoion_module_descriptor_v1;
|
||||
|
||||
typedef const echoion_module_descriptor_v1* (*echoion_module_abi_query_fn_v1)(
|
||||
void);
|
||||
|
||||
ECHOION_MODULE_ABI_EXPORT const echoion_module_descriptor_v1*
|
||||
echoion_module_abi_query(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user