Closed
Description
When using the JS API to dynamically link imports to a wasm module, if that module has an autostart e.g. (start $main)
then those imports cannot synchronously access any of the modules exports. Basically you can't have synchronous cyclic dependencies.
There are workarounds:
- Don't autostart
- Have wasm module import everything that is cyclic (depending on what it is, e.g. import the table) instead of it creating and exporting it
const instance = new WebAssembly.Instance(module, {
env: {
callIndirect: index => {
// throws here, instance is not defined yet
// as the `new WebAssembly.Instance` has not
// yet returned
instance.exports.table.get(index)();
}
}
});
(module
(func $callIndirect (import "env" "callIndirect") (param i32))
(func $main
i32.const 0
call $callIndirect
)
(func $callback)
(memory 1)
(table $table anyfunc (elem $callback))
(export "table" (table $table))
(start $main)
)
Demo: example.zip