Files
marqdo-extension-conformance/plugin/src/plugin.c
T

230 lines
7.5 KiB
C

/*
* Conformance plugin for Marqdo plugin ABI v1 (worker transport).
*
* Independent third-party source: it only depends on the published
* `marqdo_plugin_abi_v1.h` header (copied verbatim into ../include). It is
* built for the host target by build.sh, which also writes the target triple
* and artifact SHA-256 into the generated plugin manifest. The committed
* plugin.yaml is a template with zero digests.
*
* Exports:
* solver.add(Int, Int) -> Int
* solver.neg(Int) -> Int
* quant.scale(Int) -> Int
*/
#include "marqdo_plugin_abi_v1.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static const char METADATA[] = "{\"name\":\"demo.ext\",\"version\":\"0.1.0\"}";
static const MqHostApi *g_host = NULL;
static const char *find_key(const char *json, const char *key) {
size_t key_len = strlen(key);
const char *cursor = json;
while ((cursor = strchr(cursor, '"')) != NULL) {
const char *name_start = cursor + 1;
const char *name_end = strchr(name_start, '"');
const char *after;
if (name_end == NULL) {
return NULL;
}
if ((size_t)(name_end - name_start) != key_len ||
memcmp(name_start, key, key_len) != 0) {
cursor = name_end + 1;
continue;
}
after = name_end + 1;
while (*after == ' ' || *after == '\t' || *after == '\n' || *after == '\r') {
after++;
}
if (*after != ':') {
return NULL;
}
after++;
while (*after == ' ' || *after == '\t' || *after == '\n' || *after == '\r') {
after++;
}
return after;
}
return NULL;
}
static const char *find_object_value(const char *json, const char *name) {
const char *value = find_key(json, name);
return (value != NULL && *value == '{') ? value : NULL;
}
static int read_int_argument(const char *json, const char *name, long long *out) {
const char *args = find_object_value(json, "args");
const char *argument;
const char *decimal;
char buffer[32];
size_t length;
char *end = NULL;
if (args == NULL) {
return -1;
}
argument = find_object_value(args, name);
if (argument == NULL) {
return -1;
}
decimal = find_key(argument, "decimal");
if (decimal == NULL || *decimal != '"') {
return -1;
}
length = (size_t)(strchr(decimal + 1, '"') - (decimal + 1));
if (length == 0 || length + 1 > sizeof(buffer)) {
return -1;
}
memcpy(buffer, decimal + 1, length);
buffer[length] = '\0';
*out = strtoll(buffer, &end, 10);
return (end != NULL && *end == '\0') ? 0 : -1;
}
static int32_t emit(const char *json, size_t len, MqOwnedBytes *out) {
uint8_t *buffer;
if (g_host == NULL || out == NULL) {
return MQ_ERR_INVALID_ARGUMENT;
}
buffer = (uint8_t *)g_host->alloc(g_host->host_data, len);
if (buffer == NULL) {
return MQ_ERR_OUT_OF_MEMORY;
}
memcpy(buffer, json, len);
out->data = buffer;
out->len = len;
return MQ_OK;
}
static int32_t fail(const char *code, const char *message, MqOwnedBytes *out, int32_t status) {
char json[512];
int written = snprintf(
json, sizeof(json),
"{\"wire_version\":1,\"kind\":\"response\",\"error\":"
"{\"kind\":\"invoke\",\"code\":\"%s\",\"message\":\"%s\"}}",
code, message);
if (written < 0 || (size_t)written >= sizeof(json)) {
return MQ_ERR_INTERNAL;
}
if (emit(json, (size_t)written, out) != MQ_OK) {
return MQ_ERR_OUT_OF_MEMORY;
}
return status;
}
static int32_t emit_int(long long value, MqOwnedBytes *out) {
char json[160];
int written = snprintf(
json, sizeof(json),
"{\"wire_version\":1,\"kind\":\"response\",\"value\":"
"{\"type\":\"int\",\"decimal\":\"%lld\"}}",
value);
if (written < 0 || (size_t)written >= sizeof(json)) {
return MQ_ERR_INTERNAL;
}
return emit(json, (size_t)written, out);
}
static int32_t invoke_binary(
MqBytes args_wire,
MqOwnedBytes *result_wire,
MqOwnedBytes *error_wire,
const char *first,
const char *second,
int operation
) {
char *json;
long long a = 0;
long long b = 0;
if (args_wire.data == NULL || args_wire.len == 0) {
return fail("bad_arguments", "request wire is empty", error_wire, MQ_ERR_INVALID_ARGUMENT);
}
json = (char *)malloc(args_wire.len + 1);
if (json == NULL) {
return fail("out_of_memory", "plugin could not copy request", error_wire, MQ_ERR_OUT_OF_MEMORY);
}
memcpy(json, args_wire.data, args_wire.len);
json[args_wire.len] = '\0';
if (read_int_argument(json, first, &a) != 0 ||
(second != NULL && read_int_argument(json, second, &b) != 0)) {
free(json);
return fail("bad_arguments", "invalid Int arguments", error_wire, MQ_ERR_INVALID_ARGUMENT);
}
free(json);
switch (operation) {
case 0: return emit_int(a + b, result_wire);
case 1: return emit_int(-a, result_wire);
default: return emit_int(a * 3, result_wire);
}
}
static int32_t solver_add(MqCallContext *ctx, MqBytes args, MqOwnedBytes *result, MqOwnedBytes *error) {
(void)ctx;
*result = (MqOwnedBytes){NULL, 0};
*error = (MqOwnedBytes){NULL, 0};
return invoke_binary(args, result, error, "a", "b", 0);
}
static int32_t solver_neg(MqCallContext *ctx, MqBytes args, MqOwnedBytes *result, MqOwnedBytes *error) {
(void)ctx;
*result = (MqOwnedBytes){NULL, 0};
*error = (MqOwnedBytes){NULL, 0};
return invoke_binary(args, result, error, "a", NULL, 1);
}
static int32_t quant_scale(MqCallContext *ctx, MqBytes args, MqOwnedBytes *result, MqOwnedBytes *error) {
(void)ctx;
*result = (MqOwnedBytes){NULL, 0};
*error = (MqOwnedBytes){NULL, 0};
return invoke_binary(args, result, error, "a", NULL, 2);
}
static int32_t register_export(const char *name, const char *signature, MqPluginInvoke invoke) {
MqBytes name_bytes;
MqBytes signature_bytes;
char wire[256];
int written = snprintf(wire, sizeof(wire), "{\"signature\":\"%s\"}", signature);
if (written < 0 || (size_t)written >= sizeof(wire)) {
return MQ_ERR_INTERNAL;
}
name_bytes.data = (const uint8_t *)name;
name_bytes.len = strlen(name);
signature_bytes.data = (const uint8_t *)wire;
signature_bytes.len = (size_t)written;
return g_host->register_function(g_host->host_data, name_bytes, signature_bytes, invoke);
}
int32_t mq_plugin_entry_v1(const MqHostApi *host, MqPluginApi *plugin) {
int32_t code;
if (host == NULL || plugin == NULL) {
return MQ_ERR_INVALID_ARGUMENT;
}
if (host->struct_size < sizeof(MqHostApi) || host->abi_major != MQ_ABI_MAJOR) {
return MQ_ERR_ABI_MISMATCH;
}
if (host->alloc == NULL || host->free == NULL || host->register_function == NULL ||
host->request_capability == NULL) {
return MQ_ERR_ABI_MISMATCH;
}
g_host = host;
plugin->struct_size = (uint32_t)sizeof(MqPluginApi);
plugin->abi_major = MQ_ABI_MAJOR;
plugin->abi_minor = MQ_ABI_MINOR;
plugin->plugin_metadata_wire.data = (const uint8_t *)METADATA;
plugin->plugin_metadata_wire.len = sizeof(METADATA) - 1;
code = register_export("solver.add", "($a: Int, $b: Int) -> Int", solver_add);
if (code != MQ_OK) {
return code;
}
code = register_export("solver.neg", "($a: Int) -> Int", solver_neg);
if (code != MQ_OK) {
return code;
}
return register_export("quant.scale", "($a: Int) -> Int", quant_scale);
}