Skip to content

Commit 4d81846

Browse files
committed
cli: automatically create shared memories matching imports
this seems required by wasi-threads.
1 parent bda7eb7 commit 4d81846

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

cli/main.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,10 @@ main(int argc, char *const *argv)
240240
}
241241
#if defined(TOYWASM_ENABLE_WASI_THREADS)
242242
if (state->wasi_threads != NULL) {
243+
const struct repl_module_state *mod =
244+
&state->modules[state->nmodules - 1];
243245
ret = wasi_threads_instance_set_thread_spawn_args(
244-
state->wasi_threads,
245-
state->modules[state->nmodules - 1].module,
246-
state->imports);
246+
state->wasi_threads, mod->module, mod->extra_import);
247247
if (ret != 0) {
248248
xlog_error("wasi_threads_instance_set_thread_spawn_"
249249
"args failed with %d",

cli/repl.c

+22-2
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ repl_unload(struct repl_module_state *mod)
141141
free(mod->name);
142142
mod->name = NULL;
143143
}
144+
#if defined(TOYWASM_ENABLE_WASI_THREADS)
145+
if (mod->extra_import != NULL) {
146+
import_object_destroy(mod->extra_import);
147+
mod->extra_import = NULL;
148+
}
149+
#endif
144150
}
145151

146152
void
@@ -386,10 +392,24 @@ repl_load_from_buf(struct repl_state *state, const char *modname,
386392
xlog_printf("module_load failed\n");
387393
goto fail;
388394
}
395+
396+
struct import_object *imports = state->imports;
397+
#if defined(TOYWASM_ENABLE_WASI_THREADS)
398+
/* create matching shared memory automatically */
399+
struct import_object *imo;
400+
ret = create_satisfying_shared_memories(mod->module, &imo);
401+
if (ret != 0) {
402+
goto fail;
403+
}
404+
mod->extra_import = imo;
405+
imo->next = imports;
406+
imports = imo;
407+
#endif
408+
389409
struct report report;
390410
report_init(&report);
391-
ret = instance_create_no_init(mod->module, &mod->inst, NULL,
392-
state->imports, &report);
411+
ret = instance_create_no_init(mod->module, &mod->inst, NULL, imports,
412+
&report);
393413
if (report.msg != NULL) {
394414
xlog_error("instance_create: %s", report.msg);
395415
printf("instantiation error: %s\n", report.msg);

cli/repl.h

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ struct repl_module_state {
1717
bool buf_mapped;
1818
struct module *module;
1919
struct instance *inst;
20+
#if defined(TOYWASM_ENABLE_WASI_THREADS)
21+
struct import_object *extra_import;
22+
#endif
2023
};
2124

2225
/* eg. const.wast has 366 modules */

0 commit comments

Comments
 (0)