#ifndef GENERIC_H
#define GENERIC_H
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#define GENERIC_PLUGIN_ABI_VERSION 1
enum GenericFfiStatus
#if defined(__cplusplus) || __STDC_VERSION__ >= 202311L
: uint32_t
#endif {
GENERIC_FFI_STATUS_OK = 0,
GENERIC_FFI_STATUS_EXCEPTION = 1,
GENERIC_FFI_STATUS_FATAL = 99,
};
#ifndef __cplusplus
#if __STDC_VERSION__ >= 202311L
typedef enum GenericFfiStatus GenericFfiStatus;
#else
typedef uint32_t GenericFfiStatus;
#endif #endif
enum GenericValueKind
#if defined(__cplusplus) || __STDC_VERSION__ >= 202311L
: uint32_t
#endif {
GENERIC_VALUE_KIND_NIL = 0,
GENERIC_VALUE_KIND_BOOL = 1,
GENERIC_VALUE_KIND_INT = 2,
GENERIC_VALUE_KIND_BIG_INT = 3,
GENERIC_VALUE_KIND_FLOAT = 4,
GENERIC_VALUE_KIND_RATIONAL = 5,
GENERIC_VALUE_KIND_STRING = 6,
GENERIC_VALUE_KIND_LIST = 7,
GENERIC_VALUE_KIND_TUPLE = 8,
GENERIC_VALUE_KIND_DICT = 9,
GENERIC_VALUE_KIND_SET = 10,
GENERIC_VALUE_KIND_RANGE = 11,
GENERIC_VALUE_KIND_STOP_ITERATION = 12,
GENERIC_VALUE_KIND_INSTANCE = 13,
GENERIC_VALUE_KIND_CLASS = 14,
GENERIC_VALUE_KIND_FUNCTION = 15,
GENERIC_VALUE_KIND_MODULE = 16,
GENERIC_VALUE_KIND_EXCEPTION = 17,
GENERIC_VALUE_KIND_GENERATOR = 18,
GENERIC_VALUE_KIND_ITERATOR = 19,
GENERIC_VALUE_KIND_OTHER = 20,
};
#ifndef __cplusplus
#if __STDC_VERSION__ >= 202311L
typedef enum GenericValueKind GenericValueKind;
#else
typedef uint32_t GenericValueKind;
#endif #endif
typedef struct GenericValue {
uint64_t opaque[4];
} GenericValue;
typedef struct FfiStr {
const uint8_t *ptr;
size_t len;
} FfiStr;
typedef struct FfiReturn {
uint32_t status;
struct GenericValue value;
} FfiReturn;
typedef struct HostApi {
uint32_t abi_version;
void *ctx;
uint32_t (*value_kind)(void *ctx, struct GenericValue value);
bool (*bool_get)(void *ctx, struct GenericValue value, bool *out);
bool (*int_get)(void *ctx, struct GenericValue value, int64_t *out);
bool (*float_get)(void *ctx, struct GenericValue value, double *out);
bool (*string_get)(void *ctx, struct GenericValue value, struct FfiStr *out);
bool (*list_len)(void *ctx, struct GenericValue value, size_t *out);
struct FfiReturn (*list_get)(void *ctx, struct GenericValue value, size_t index);
bool (*tuple_len)(void *ctx, struct GenericValue value, size_t *out);
struct FfiReturn (*tuple_get)(void *ctx, struct GenericValue value, size_t index);
bool (*dict_len)(void *ctx, struct GenericValue value, size_t *out);
bool (*set_len)(void *ctx, struct GenericValue value, size_t *out);
struct FfiReturn (*builtin_get)(void *ctx, struct FfiStr name);
struct FfiReturn (*is_instance)(void *ctx, struct GenericValue value, struct GenericValue of_class);
struct FfiReturn (*class_of)(void *ctx, struct GenericValue value);
struct FfiReturn (*attr_get)(void *ctx, struct GenericValue receiver, struct FfiStr name);
struct FfiReturn (*attr_set)(void *ctx,
struct GenericValue receiver,
struct FfiStr name,
struct GenericValue value);
struct FfiReturn (*attr_has)(void *ctx, struct GenericValue receiver, struct FfiStr name);
struct GenericValue (*nil_new)(void *ctx);
struct GenericValue (*bool_new)(void *ctx, bool value);
struct GenericValue (*int_new)(void *ctx, int64_t value);
struct GenericValue (*float_new)(void *ctx, double value);
struct FfiReturn (*string_new)(void *ctx, struct FfiStr value);
struct GenericValue (*list_new)(void *ctx);
struct FfiReturn (*list_push)(void *ctx, struct GenericValue list, struct GenericValue item);
struct FfiReturn (*list_set)(void *ctx,
struct GenericValue list,
size_t index,
struct GenericValue value);
struct FfiReturn (*exception_new)(void *ctx, struct GenericValue of_class, struct FfiStr message);
struct GenericValue (*value_display)(void *ctx, struct GenericValue value);
struct FfiReturn (*call_value)(void *ctx,
struct GenericValue callee,
const struct GenericValue *args,
size_t nargs);
struct FfiReturn (*invoke_method)(void *ctx,
struct GenericValue receiver,
struct FfiStr name,
const struct GenericValue *args,
size_t nargs);
struct FfiReturn (*value_str)(void *ctx, struct GenericValue value);
struct FfiReturn (*dict_get)(void *ctx, struct GenericValue dict, struct GenericValue key);
struct FfiReturn (*dict_set)(void *ctx,
struct GenericValue dict,
struct GenericValue key,
struct GenericValue value);
struct FfiReturn (*dict_contains)(void *ctx, struct GenericValue dict, struct GenericValue key);
struct FfiReturn (*set_add)(void *ctx, struct GenericValue set, struct GenericValue item);
struct FfiReturn (*set_contains)(void *ctx, struct GenericValue set, struct GenericValue item);
struct FfiReturn (*value_truthy)(void *ctx, struct GenericValue value);
struct FfiReturn (*value_equals)(void *ctx, struct GenericValue a, struct GenericValue b);
struct FfiReturn (*value_hash)(void *ctx, struct GenericValue value);
void (*root)(void *ctx, struct GenericValue value);
void (*unroot)(void *ctx, size_t n);
struct FfiReturn (*instance_set_opaque)(void *ctx, struct GenericValue receiver, void *ptr);
void *(*instance_get_opaque)(void *ctx, struct GenericValue receiver);
} HostApi;
typedef struct FunctionDesc {
struct FfiStr name;
const uint8_t *arities;
size_t arities_len;
struct FfiReturn (*fun)(const struct HostApi *host, const struct GenericValue *args, size_t nargs);
} FunctionDesc;
typedef struct MethodDesc {
struct FfiStr name;
const uint8_t *arities;
size_t arities_len;
struct FfiReturn (*fun)(const struct HostApi *host,
struct GenericValue receiver,
const struct GenericValue *args,
size_t nargs);
} MethodDesc;
typedef void (*PluginVisitFn)(void *ctx, struct GenericValue value);
typedef struct ClassDesc {
struct FfiStr name;
const struct MethodDesc *methods;
size_t methods_len;
void (*drop)(void *opaque_ptr);
int32_t (*traverse)(void *opaque_ptr, PluginVisitFn visit, void *visit_ctx);
} ClassDesc;
typedef struct ValueDesc {
struct FfiStr name;
struct FfiReturn (*fun)(const struct HostApi *host);
} ValueDesc;
typedef struct ModuleDesc {
uint32_t abi_version;
const struct FunctionDesc *functions;
size_t functions_len;
const struct ClassDesc *classes;
size_t classes_len;
const struct ValueDesc *values;
size_t values_len;
} ModuleDesc;
typedef struct FfiReturn (*PluginFn)(const struct HostApi *host,
const struct GenericValue *args,
size_t nargs);
#endif
#ifdef __cplusplus
extern "C" {
#endif
const ModuleDesc *generic_plugin_init(void);
#ifdef __cplusplus
} #endif