aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2023-11-26 22:43:30 +0100
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2024-04-21 01:51:24 +0200
commitf25b015e5b668028c34974bbb22faa4105c26690 (patch)
tree28f2b08c17b3585d06694ad74004d0617eadb785 /include
downloadnanowasm-sync-f25b015e5b668028c34974bbb22faa4105c26690.tar.gz
First commit
Diffstat (limited to 'include')
-rw-r--r--include/nanowasm/nw.h29
-rw-r--r--include/nanowasm/types.h144
2 files changed, 173 insertions, 0 deletions
diff --git a/include/nanowasm/nw.h b/include/nanowasm/nw.h
new file mode 100644
index 0000000..2cdb8a4
--- /dev/null
+++ b/include/nanowasm/nw.h
@@ -0,0 +1,29 @@
+/*
+ * 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_H
+#define NANOWASM_H
+
+#include <nanowasm/types.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+int nw_load(const struct nw_mod_cfg *cfg, struct nw_mod *m);
+int nw_start(const struct nw_inst_cfg *icfg, struct nw_inst *i);
+enum nw_state nw_run(struct nw_inst *i);
+int nw_stop(struct nw_inst *i);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/include/nanowasm/types.h b/include/nanowasm/types.h
new file mode 100644
index 0000000..637dd31
--- /dev/null
+++ b/include/nanowasm/types.h
@@ -0,0 +1,144 @@
+/*
+ * 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 <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_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