/* * nanowasm, a tiny WebAssembly/Wasm interpreter * Copyright (C) 2023-2024 Xavier Del Campo Romero * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ #ifndef NANOWASM_TYPES_H #define NANOWASM_TYPES_H #include #include #include #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_buf { void *buf; size_t n; }; enum nw_state { NW_STATE_AGAIN, NW_STATE_RETURNED, NW_STATE_EXCEPTION, NW_STATE_FATAL }; 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_interp_cfg { struct nw_buf stack, heap, global; const struct nw_mod *m; }; struct nw_inst_cfg { struct nw_interp_cfg interp; }; struct nw_mod { struct nw_sections { long type, import, function, table, memory, global, export, start, element, code, data; } sections; size_t data_len, heap_len; struct nw_mod_cfg cfg; }; struct nw_inst { struct nw_interp { const struct nw_mod *m; FILE *f; const char *exception; bool exit; int retval; struct nw_interp_cfg cfg; size_t stack_i, global_i; struct nw_frame *fp; struct nw_gframe *gfp; } interp; union nw_inst_state { long retval; const char *exception; } state; }; #define NW_BUF(sz) {.buf = (char [sz]){0}, .n = sz} #ifdef __cplusplus } #endif #endif