From 4d81846e3aa6f01f97e6aef0a51238fd18f6ecf1 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Sat, 24 Dec 2022 11:32:24 +0900 Subject: [PATCH] cli: automatically create shared memories matching imports this seems required by wasi-threads. --- cli/main.c | 6 +++--- cli/repl.c | 24 ++++++++++++++++++++++-- cli/repl.h | 3 +++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/cli/main.c b/cli/main.c index 38baf0db..becfc557 100644 --- a/cli/main.c +++ b/cli/main.c @@ -240,10 +240,10 @@ main(int argc, char *const *argv) } #if defined(TOYWASM_ENABLE_WASI_THREADS) if (state->wasi_threads != NULL) { + const struct repl_module_state *mod = + &state->modules[state->nmodules - 1]; ret = wasi_threads_instance_set_thread_spawn_args( - state->wasi_threads, - state->modules[state->nmodules - 1].module, - state->imports); + state->wasi_threads, mod->module, mod->extra_import); if (ret != 0) { xlog_error("wasi_threads_instance_set_thread_spawn_" "args failed with %d", diff --git a/cli/repl.c b/cli/repl.c index 380b5b1e..e09019a2 100644 --- a/cli/repl.c +++ b/cli/repl.c @@ -141,6 +141,12 @@ repl_unload(struct repl_module_state *mod) free(mod->name); mod->name = NULL; } +#if defined(TOYWASM_ENABLE_WASI_THREADS) + if (mod->extra_import != NULL) { + import_object_destroy(mod->extra_import); + mod->extra_import = NULL; + } +#endif } void @@ -386,10 +392,24 @@ repl_load_from_buf(struct repl_state *state, const char *modname, xlog_printf("module_load failed\n"); goto fail; } + + struct import_object *imports = state->imports; +#if defined(TOYWASM_ENABLE_WASI_THREADS) + /* create matching shared memory automatically */ + struct import_object *imo; + ret = create_satisfying_shared_memories(mod->module, &imo); + if (ret != 0) { + goto fail; + } + mod->extra_import = imo; + imo->next = imports; + imports = imo; +#endif + struct report report; report_init(&report); - ret = instance_create_no_init(mod->module, &mod->inst, NULL, - state->imports, &report); + ret = instance_create_no_init(mod->module, &mod->inst, NULL, imports, + &report); if (report.msg != NULL) { xlog_error("instance_create: %s", report.msg); printf("instantiation error: %s\n", report.msg); diff --git a/cli/repl.h b/cli/repl.h index 8d9fc16b..333ea37f 100644 --- a/cli/repl.h +++ b/cli/repl.h @@ -17,6 +17,9 @@ struct repl_module_state { bool buf_mapped; struct module *module; struct instance *inst; +#if defined(TOYWASM_ENABLE_WASI_THREADS) + struct import_object *extra_import; +#endif }; /* eg. const.wast has 366 modules */