nanowasm/include/nanowasm/types.h

145 lines
2.5 KiB
C

#ifndef NANOWASM_TYPES_H
#define NANOWASM_TYPES_H
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C"
{
#endif
union nw_value
{
long i32;
long long i64;
float f32;
double f64;
/* TODO: define {de}serialization between Wasm and host. */
void *p;
};
enum nw_kind
{
NW_KIND_FUNCTION,
NW_KIND_TABLE,
NW_KIND_MEMORY,
NW_KIND_GLOBAL
};
struct nw_args
{
const union nw_value *args;
size_t n;
};
struct nw_mod_cfg
{
const struct nw_import
{
enum nw_kind kind;
const char *module, *field;
union
{
struct
{
/* This must follow the format below, whitespace ignored:
* [return type]([param type][, ...])
* For convenience, the interpreter can do automatic checks on
* pointer arguments and/or return values if using '*' as type.
* Examples:
* "()"
* "i32(f32)"
* "*(f64, i32, *, i64)"
* As per WebAssembly MVP spec, multiple return types are not
* supported yet. */
const char *signature;
int (*fn)(const struct nw_args *params,
union nw_value *ret, void *user);
} function;
} u;
} *imports;
size_t n_imports;
const char *path;
void *user;
};
struct nw_mod
{
struct
{
long type, import, function, table, memory, global, export,
start, element, code, data;
} sections;
struct nw_mod_cfg cfg;
};
struct nw_buf
{
void *buf;
size_t n;
};
struct nw_inst_cfg
{
struct
{
union nw_value *buf;
size_t n;
} args;
struct nw_buf stack, heap;
const struct nw_mod *m;
};
enum nw_state
{
NW_STATE_AGAIN,
NW_STATE_RETURNED,
NW_STATE_EXCEPTION,
NW_STATE_FATAL
};
struct nw_inst
{
struct nw_interp
{
FILE *f;
const char *exception;
bool exit;
struct
{
union nw_value *buf;
size_t n;
} args;
struct
{
struct nw_buf buf;
size_t i;
} stack, heap;
} interp;
union nw_inst_state
{
int retval;
const char *exception;
} state;
struct nw_inst_cfg cfg;
};
#define NW_BUF(sz) {.buf = (char[sz]){0}, .n = sz}
#define NW_ARGS(sz) {.buf = (union nw_value[sz]){0}, .n = sz}
#ifdef __cplusplus
}
#endif
#endif