diff options
Diffstat (limited to 'examples/minimal.c')
| -rw-r--r-- | examples/minimal.c | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/examples/minimal.c b/examples/minimal.c new file mode 100644 index 0000000..8812ac5 --- /dev/null +++ b/examples/minimal.c @@ -0,0 +1,136 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2025 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/. + */ + +#include <nanowasm/nw.h> +#include <stddef.h> +#include <stdlib.h> + +static int io_read(void *const buf, const size_t n, void *const user) +{ + return -1; +} + +static enum nw_state io_seek(const long offset, void *const user) +{ + return -1; +} + +static enum nw_state io_tell(long *const out, void *const user) +{ + return -1; +} + +static int io_eof(void *const user) +{ + return -1; +} + +static int push(const void *const src, const size_t n, void *const user) +{ + return -1; +} + +static int pop(void *const dst, const size_t n, void *const user) +{ + return -1; +} + +static size_t ptr(void *const user) +{ + return 0; +} + +static int load(const nw_varuint32 offset, void *const dst, const size_t n, + void *const user) +{ + return -1; +} + +static int store(const nw_varuint32 offset, const void *const src, + const size_t n, void *const user) +{ + return -1; +} + +int main(int argc, char *argv[]) +{ + const struct nw_io_cfg io = + { + .read = io_read, + .seek = io_seek, + .tell = io_tell, + .eof = io_eof + }; + + const struct nw_mod_cfg cfg = + { + .io = io + }; + + struct nw_mod m; + struct nw_mod_out mout; + + nw_init(&m, &cfg); + +again: + + switch (nw_load(&m, &mout)) + { + case NW_OK: + break; + + case NW_AGAIN: + goto again; + + case NW_FATAL: + return EXIT_FAILURE; + } + + const struct nw_inst_cfg icfg = + { + .interp_cfg = + { + .io = io, + .m = &m, + .stack = + { + .push = push, + .pop = pop, + .ptr = ptr + }, + + .linear = + { + .load = load, + .store = store + } + } + }; + + struct nw_inst inst; + + if (nw_start(&inst, &icfg)) + return EXIT_FAILURE; + +again2: + + switch (nw_run(&inst)) + { + case NW_OK: + break; + + case NW_AGAIN: + goto again2; + + case NW_FATAL: + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} |
