From 7f256234e34fd4b84f753245d93a5536d5885e86 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Sat, 3 Aug 2024 17:24:36 +0900 Subject: [PATCH] examples/runwasi: add runwasi_module --- examples/runwasi/runwasi.c | 68 +++++++++++++++++++++++--------------- examples/runwasi/runwasi.h | 7 ++++ 2 files changed, 49 insertions(+), 26 deletions(-) diff --git a/examples/runwasi/runwasi.c b/examples/runwasi/runwasi.c index 48acfb73..33e298ec 100644 --- a/examples/runwasi/runwasi.c +++ b/examples/runwasi/runwasi.c @@ -11,38 +11,17 @@ #include "runwasi.h" int -runwasi(struct mem_context *mctx, const char *filename, unsigned int ndirs, - char **dirs, unsigned int nenvs, const char *const *envs, int argc, - const char *const *argv, const int stdio_fds[3], - struct import_object *base_imports, uint32_t *wasi_exit_code_p) +runwasi_module(struct mem_context *mctx, const struct module *m, + unsigned int ndirs, char **dirs, unsigned int nenvs, + const char *const *envs, int argc, const char *const *argv, + const int stdio_fds[3], struct import_object *base_imports, + uint32_t *wasi_exit_code_p) { - struct module *m = NULL; struct wasi_instance *wasi = NULL; struct import_object *wasi_import_object = NULL; struct instance *inst = NULL; int ret; - /* - * load a module - */ - uint8_t *p; - size_t sz; - ret = map_file(filename, (void **)&p, &sz); - if (ret != 0) { - xlog_error("map_file failed with %d", ret); - goto fail; - } - struct load_context lctx; - load_context_init(&lctx, mctx); - ret = module_create(&m, p, p + sz, &lctx); - if (ret != 0) { - xlog_error("module_load failed with %d: %s", ret, - report_getmessage(&lctx.report)); - load_context_clear(&lctx); - goto fail; - } - load_context_clear(&lctx); - /* * find the entry point */ @@ -154,6 +133,43 @@ runwasi(struct mem_context *mctx, const char *filename, unsigned int ndirs, if (wasi != NULL) { wasi_instance_destroy(wasi); } + return ret; +} + +int +runwasi(struct mem_context *mctx, const char *filename, unsigned int ndirs, + char **dirs, unsigned int nenvs, const char *const *envs, int argc, + const char *const *argv, const int stdio_fds[3], + struct import_object *base_imports, uint32_t *wasi_exit_code_p) +{ + struct module *m = NULL; + int ret; + + /* + * load a module + */ + uint8_t *p; + size_t sz; + ret = map_file(filename, (void **)&p, &sz); + if (ret != 0) { + xlog_error("map_file failed with %d", ret); + goto fail; + } + struct load_context lctx; + load_context_init(&lctx, mctx); + ret = module_create(&m, p, p + sz, &lctx); + if (ret != 0) { + xlog_error("module_load failed with %d: %s", ret, + report_getmessage(&lctx.report)); + load_context_clear(&lctx); + goto fail; + } + load_context_clear(&lctx); + + ret = runwasi_module(mctx, m, ndirs, dirs, nenvs, envs, argc, argv, + stdio_fds, base_imports, wasi_exit_code_p); + +fail: if (m != NULL) { module_destroy(mctx, m); } diff --git a/examples/runwasi/runwasi.h b/examples/runwasi/runwasi.h index 9433b7d0..6432b6c5 100644 --- a/examples/runwasi/runwasi.h +++ b/examples/runwasi/runwasi.h @@ -2,6 +2,13 @@ struct mem_context; struct import_object; +struct module; + +int runwasi_module(struct mem_context *mctx, const struct module *m, + unsigned int ndirs, char **dirs, unsigned int nenvs, + const char *const *envs, int argc, const char *const *argv, + const int stdio_fds[3], struct import_object *base_imports, + uint32_t *wasi_exit_code_p); int runwasi(struct mem_context *mctx, const char *filename, unsigned int ndirs, char **dirs, unsigned int nenvs, const char *const *envs, int argc,