aboutsummaryrefslogtreecommitdiff
path: root/test
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 /test
downloadnanowasm-sync-f25b015e5b668028c34974bbb22faa4105c26690.tar.gz
First commit
Diffstat (limited to 'test')
-rw-r--r--test/CMakeLists.txt24
-rwxr-xr-xtest/example.wasmbin0 -> 829 bytes
-rw-r--r--test/main.c105
3 files changed, 129 insertions, 0 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
new file mode 100644
index 0000000..60ebec3
--- /dev/null
+++ b/test/CMakeLists.txt
@@ -0,0 +1,24 @@
+# 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/.
+
+cmake_minimum_required(VERSION 3.18)
+project(test C)
+add_executable(${PROJECT_NAME} main.c)
+target_link_libraries(${PROJECT_NAME} PRIVATE nanowasm)
+
+include(CheckLinkerFlag)
+set(ldflags -Wl,--gc-sections)
+
+foreach(f ${ldflags})
+ string(REPLACE "-" "_" var supported_${f})
+ string(REPLACE "," "_" var ${var})
+ check_linker_flag(C ${f} ${var})
+
+ if(${var})
+ target_link_options(${PROJECT_NAME} PRIVATE ${f})
+ endif()
+endforeach()
diff --git a/test/example.wasm b/test/example.wasm
new file mode 100755
index 0000000..afea5ed
--- /dev/null
+++ b/test/example.wasm
Binary files differ
diff --git a/test/main.c b/test/main.c
new file mode 100644
index 0000000..70cacd8
--- /dev/null
+++ b/test/main.c
@@ -0,0 +1,105 @@
+/*
+ * 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/.
+ */
+
+#include <nanowasm/nw.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static int proc_exit(const struct nw_args *const params,
+ union nw_value *const ret, void *const user)
+{
+ return -1;
+}
+
+int main(int argc, char *argv[])
+{
+ static const struct nw_import imports[] =
+ {
+ {
+ .kind = NW_KIND_FUNCTION,
+ .module = "wasi_snapshot_preview1",
+ .field = "proc_exit",
+ .u.function =
+ {
+ .signature = "i32()",
+ .fn = proc_exit
+ }
+ }
+ };
+
+ static const struct nw_mod_cfg cfg =
+ {
+ .path = "example.wasm",
+ .imports = imports,
+ .n_imports = sizeof imports / sizeof *imports
+ };
+
+ struct nw_mod m;
+
+ if (nw_load(&cfg, &m))
+ {
+ fprintf(stderr, "%s: nw_load failed\n", __func__);
+ return EXIT_FAILURE;
+ }
+
+ const struct nw_inst_cfg icfg =
+ {
+ .interp =
+ {
+ .stack = NW_BUF(256),
+ .heap = NW_BUF(2048),
+ .global = NW_BUF(24),
+ .m = &m
+ }
+ };
+
+ struct nw_inst inst;
+
+ if (nw_start(&icfg, &inst))
+ {
+ fprintf(stderr, "%s: nw_start failed\n", __func__);
+ return EXIT_FAILURE;
+ }
+
+ const union nw_inst_state *s = &inst.state;
+ int ret = EXIT_FAILURE;
+
+again:
+
+ switch (nw_run(&inst))
+ {
+ case NW_STATE_AGAIN:
+ goto again;
+
+ case NW_STATE_RETURNED:
+ fprintf(stderr, "instance returned %ld\n", s->retval);
+ break;
+
+ case NW_STATE_EXCEPTION:
+ fprintf(stderr, "exception: %s\n", s->exception);
+ goto end;
+
+ case NW_STATE_FATAL:
+ goto end;
+ }
+
+ ret = EXIT_SUCCESS;
+
+end:
+
+ if (nw_stop(&inst))
+ {
+ fprintf(stderr, "%s: nw_stop failed\n", __func__);
+ ret = EXIT_FAILURE;
+ }
+
+ return ret;
+}