Dever v0.0.2

This commit is contained in:
Codex committed 2026-11-27 06:03:56 +00:00
commit e6e175ebcf
816 files changed
+120349

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
{
"name": "yyjson",
"version": "0.12.0",
"url": "https://github.com/ibireme/yyjson/archive/refs/tags/0.12.0.tar.gz",
"sha256": "b16246f617b2a136c78d73e5e2647c6f1de1313e46678062985bdcf1f40bb75d",
"license": "MIT"
}
+119
View File
@@ -0,0 +1,119 @@
/* 同步 C ABI;取消由 Dever 进程监管设定宽限,SDK 不新增线程调度。 */
#include "wire.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int text(yyjson_val *value, const char *expected) {
return yyjson_is_str(value) && yyjson_get_len(value) == strlen(expected) && memcmp(yyjson_get_str(value), expected, strlen(expected)) == 0;
}
static int identifier(yyjson_val *message, uint64_t *id) {
yyjson_val *value = yyjson_obj_get(message, "id");
if (!yyjson_is_uint(value) || yyjson_get_uint(value) > INT64_MAX) return 0;
*id = yyjson_get_uint(value);
return 1;
}
static int declared(yyjson_val *operations, yyjson_val *operation) {
size_t i, length; yyjson_val *item;
yyjson_arr_foreach(operations, i, length, item) if (yyjson_equals(item, operation)) return 1;
return 0;
}
static int handshake(const dever_ffi_handler *handler, yyjson_val *contract, int *opened) {
yyjson_doc *doc = dever_receive();
if (doc == NULL) return 0;
yyjson_val *hello = yyjson_doc_get_root(doc);
int okay = yyjson_obj_size(hello) == 8 && text(yyjson_obj_get(hello, "kind"), "hello") && text(yyjson_obj_get(hello, "version"), "dever-component-1");
const char *identity[] = {"port", "schema", "adapter", "operations", "capabilities"};
for (size_t i = 0; okay && i < 5; ++i) okay = yyjson_equals(yyjson_obj_get(hello, identity[i]), yyjson_obj_get(contract, identity[i]));
yyjson_val *setting = yyjson_obj_get(hello, "setting");
if (okay) okay = setting != NULL;
if (okay && handler->open != NULL) {
size_t size; char *data = yyjson_val_write(setting, 0, &size);
okay = data != NULL && handler->open(handler->context, (dever_ffi_slice){data, size}) == DEVER_FFI_OK;
free(data);
}
if (okay) {
*opened = 1;
yyjson_mut_doc *ready = yyjson_doc_mut_copy(doc, NULL);
if (ready == NULL) okay = 0;
else {
yyjson_mut_val *root = yyjson_mut_doc_get_root(ready);
yyjson_mut_obj_remove_key(root, "setting");
yyjson_mut_obj_remove_key(root, "kind");
okay = yyjson_mut_obj_add_str(ready, root, "kind", "ready") && dever_send(ready);
yyjson_mut_doc_free(ready);
}
}
yyjson_doc_free(doc);
return okay;
}
static int invoke(const dever_ffi_handler *handler, yyjson_val *contract, yyjson_val *message, uint64_t id, char *buffer) {
yyjson_val *operation = yyjson_obj_get(message, "operation");
yyjson_val *payload = yyjson_obj_get(message, "payload");
if (payload == NULL || !yyjson_is_str(operation) || !declared(yyjson_obj_get(contract, "operations"), operation)) return 0;
size_t size; char *data = yyjson_val_write(payload, 0, &size);
if (data == NULL) return 0;
dever_ffi_output returned = {buffer, DEVER_MAX_BYTES - 256, 0};
int status = handler->invoke(handler->context, (dever_ffi_slice){yyjson_get_str(operation), yyjson_get_len(operation)}, (dever_ffi_slice){data, size}, &returned);
free(data);
if (returned.data != buffer || returned.capacity != DEVER_MAX_BYTES - 256 || returned.size > returned.capacity || (status != DEVER_FFI_OK && status != DEVER_FFI_ERROR)) return 0;
yyjson_doc *result = dever_parse(buffer, (size_t)returned.size, status == DEVER_FFI_OK ? 3 : 2, status == DEVER_FFI_OK ? 1 : 0);
if (result == NULL) return 0;
yyjson_val *value = yyjson_doc_get_root(result);
yyjson_val *error = NULL;
int okay = 1;
if (status == DEVER_FFI_ERROR) {
error = yyjson_obj_get(value, "error");
payload = yyjson_obj_get(value, "payload");
okay = yyjson_obj_size(value) == 2 && yyjson_is_str(error) && payload != NULL && yyjson_obj_getn(yyjson_obj_get(contract, "errors"), yyjson_get_str(error), yyjson_get_len(error)) != NULL;
} else payload = value;
yyjson_mut_doc *reply = yyjson_mut_doc_new(NULL);
if (reply == NULL) okay = 0;
if (okay) {
yyjson_mut_val *root = yyjson_mut_obj(reply);
yyjson_mut_doc_set_root(reply, root);
okay = root != NULL && yyjson_mut_obj_add_str(reply,root,"kind",error ? "error" : "result") && yyjson_mut_obj_add_uint(reply,root,"id",id) && yyjson_mut_obj_add_val(reply,root,"payload",yyjson_val_mut_copy(reply,payload));
if (okay && error) okay = yyjson_mut_obj_add_val(reply,root,"error",yyjson_val_mut_copy(reply,error));
// callback 解析已经包含外层帧头预算,此处直接编码,不重扫载荷。
if (okay) okay = dever_send(reply);
}
yyjson_mut_doc_free(reply); yyjson_doc_free(result);
return okay;
}
int dever_ffi_serve(const dever_ffi_handler *handler, dever_ffi_slice manifest) {
if (handler == NULL || handler->abi != DEVER_FFI_ABI_V1 || handler->invoke == NULL) return 1;
yyjson_doc *contract = dever_parse(manifest.data, (size_t)manifest.size, 0, 0);
if (contract == NULL) return 1;
int opened = 0;
int okay = handshake(handler, yyjson_doc_get_root(contract), &opened);
char *buffer = okay ? malloc(DEVER_MAX_BYTES - 256) : NULL;
okay = okay && buffer != NULL;
uint64_t next = 1;
int cancelled = 0, stopped = 0;
while (okay && !stopped) {
yyjson_doc *doc = dever_receive();
if (doc == NULL) { okay = 0; break; }
yyjson_val *message = yyjson_doc_get_root(doc), *kind = yyjson_obj_get(message,"kind");
uint64_t id;
if (text(kind,"shutdown")) {
okay = yyjson_obj_size(message) == 1 && dever_send_value(message);
stopped = 1;
} else if (text(kind,"health")) {
okay = yyjson_obj_size(message) == 2 && identifier(message,&id) && id == 0 && dever_send_value(message);
} else if (text(kind,"cancel")) {
okay = yyjson_obj_size(message) == 2 && identifier(message,&id) && id > 0 && id == next-1 && !cancelled;
cancelled = 1;
} else if (text(kind,"call")) {
okay = yyjson_obj_size(message) == 4 && identifier(message,&id) && id == next && invoke(handler,yyjson_doc_get_root(contract),message,id,buffer);
++next; cancelled = 0;
} else okay = 0;
yyjson_doc_free(doc);
}
if (opened && handler->close != NULL) handler->close(handler->context);
free(buffer); yyjson_doc_free(contract);
if (!okay) fputs("native FFI Worker protocol or callback failed\n",stderr);
return okay && stopped ? 0 : 1;
}
+41
View File
@@ -0,0 +1,41 @@
#ifndef DEVER_FFI_H
#define DEVER_FFI_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#define DEVER_FFI_ABI_V1 1
#define DEVER_FFI_OK 0
#define DEVER_FFI_ERROR 1
#define DEVER_FFI_FAULT 2
typedef struct {
const char *data;
uint64_t size;
} dever_ffi_slice;
typedef struct {
char *data;
uint64_t capacity;
uint64_t size;
} dever_ffi_output;
typedef struct {
uint32_t abi;
void *context;
int (*open)(void *context, dever_ffi_slice setting);
int (*invoke)(void *context, dever_ffi_slice operation, dever_ffi_slice payload, dever_ffi_output *output);
void (*close)(void *context);
} dever_ffi_handler;
/* contract 是编译器导出的 manifest,调用期间借用;不读取工作目录或环境。 */
int dever_ffi_serve(const dever_ffi_handler *handler, dever_ffi_slice contract);
#ifdef __cplusplus
}
#endif
#endif
+77
View File
@@ -0,0 +1,77 @@
/* yyjson 完成 JSON 解析与编码;本层只执行 Dever 帧与结构预算。 */
#include "wire.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int key_order(const void *left, const void *right) {
yyjson_val *a = *(yyjson_val *const *)left, *b = *(yyjson_val *const *)right;
size_t an = yyjson_get_len(a), bn = yyjson_get_len(b);
int compare = memcmp(yyjson_get_str(a), yyjson_get_str(b), an < bn ? an : bn);
return compare != 0 ? compare : (an > bn) - (an < bn);
}
static int bounded(yyjson_val *value, unsigned depth, size_t *remaining) {
if (depth > 64 || *remaining == 0) return 0;
--*remaining;
if (yyjson_is_ctn(value) && depth == 64) return 0;
if (yyjson_is_arr(value)) {
size_t i, length; yyjson_val *item;
yyjson_arr_foreach(value, i, length, item) if (!bounded(item, depth + 1, remaining)) return 0;
} else if (yyjson_is_obj(value)) {
size_t length = yyjson_obj_size(value);
if (length > *remaining) return 0;
yyjson_val **keys = length ? malloc(length * sizeof(*keys)) : NULL;
if (length && keys == NULL) return 0;
size_t i, count; yyjson_val *key, *item;
int okay = 1;
yyjson_obj_foreach(value, i, count, key, item) {
keys[i] = key;
if (!bounded(item, depth + 1, remaining)) { okay = 0; break; }
}
if (okay && length > 1) {
qsort(keys, length, sizeof(*keys), key_order);
for (i = 1; i < length; ++i) if (key_order(&keys[i-1], &keys[i]) == 0) { okay = 0; break; }
}
free(keys);
return okay;
}
return 1;
}
yyjson_doc *dever_parse(const char *data, size_t size, size_t extra_values, unsigned depth) {
if (size == 0 || size > DEVER_MAX_BYTES) return NULL;
yyjson_doc *doc = yyjson_read(data, size, YYJSON_READ_BIGNUM_AS_RAW);
if (doc == NULL) return NULL;
size_t remaining = 65536 - extra_values;
if (!bounded(yyjson_doc_get_root(doc), depth, &remaining)) { yyjson_doc_free(doc); return NULL; }
return doc;
}
yyjson_doc *dever_receive(void) {
unsigned char header[4];
if (fread(header, 1, 4, stdin) != 4) return NULL;
size_t size = ((uint32_t)header[0] << 24) | ((uint32_t)header[1] << 16) | ((uint32_t)header[2] << 8) | header[3];
if (size == 0 || size > DEVER_MAX_BYTES) return NULL;
char *data = malloc(size);
if (data == NULL) return NULL;
yyjson_doc *doc = fread(data, 1, size, stdin) == size ? dever_parse(data, size, 0, 0) : NULL;
free(data);
return doc;
}
static int send_bytes(char *data, size_t size) {
if (data == NULL || size == 0 || size > DEVER_MAX_BYTES) { free(data); return 0; }
unsigned char header[4] = {size >> 24, size >> 16, size >> 8, size};
int okay = fwrite(header, 1, 4, stdout) == 4 && fwrite(data, 1, size, stdout) == size && fflush(stdout) == 0;
free(data);
return okay;
}
int dever_send(yyjson_mut_doc *doc) {
size_t size; char *data = yyjson_mut_write(doc, 0, &size);
return send_bytes(data, size);
}
int dever_send_value(yyjson_val *value) {
size_t size; char *data = yyjson_val_write(value, 0, &size);
return send_bytes(data, size);
}
+10
View File
@@ -0,0 +1,10 @@
#ifndef DEVER_WIRE_H
#define DEVER_WIRE_H
#include <yyjson.h>
#include "dever_ffi.h"
#define DEVER_MAX_BYTES (16U * 1024U * 1024U)
yyjson_doc *dever_parse(const char *data, size_t size, size_t extra_values, unsigned depth);
yyjson_doc *dever_receive(void);
int dever_send(yyjson_mut_doc *doc);
int dever_send_value(yyjson_val *value);
#endif