Skip to content

Commit

Permalink
cli: automatically create shared memories matching imports
Browse files Browse the repository at this point in the history
this seems required by wasi-threads.
  • Loading branch information
yamt committed Dec 24, 2022
1 parent bda7eb7 commit 4d81846
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
6 changes: 3 additions & 3 deletions cli/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 22 additions & 2 deletions cli/repl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions cli/repl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 4d81846

Please sign in to comment.