Hello! I'm working on a new open source project in Go using WASM, and am considering using wasmtime. I've got the basic demo working, but when I change it to run the WASM from a file, I get an error "panic: expected 14 imports, found 0". Any ideas on how to fix this? Attached is my test code.
hello.go
And here is my test WASM code.
test.go
I might recommend using wasmtime.Linker
instead of wasmtime.NewInstance
since that'll make it a bit easier to understand what's going on here
more-or-less the problem is that the module you're instantiating has imports, and you're not providing any imports, so they'll need to be provided somewhow.
Thank you Alex. Would you have example code for using the linker to load a .wasm file? If the example would have WASI support for giving the .wasm code access to files in a chosen directory, that would be great as that's my next thing to test.
would this perhaps suffice?
I did see that, and unfortunately it uses .wat inside the host code, rather than loading a .wasm file. It's loading a .wasm file that I'm specifically having problems with.
Let me try modifying it to load a .wasm file and see how far I get...
Yeah for the purposes there the wat and wasm formats are interchangeable, so you should be able to change just that one detail
That works. Thank you! Now moving on to test PreopenDir()...
Unfortunately if I do wasiConfig.PreopenDir("/", "/") in the host, and ioutil.WriteFile("/tmp/helloworld.txt", []byte("Hello world\n"), 0644) in the .wasm, the ioutil.WriteFile() returns Error: open /tmp/helloworld.txt: file does not exist. Any ideas?
I've tried a few different combinations of paths in PreopenDir() and paths in the .wasm. All return a file does not exist error. Would anyone have any ideas? Are there any examples of using PreopenDir()?
This may or may not be related to TinyGo's support for wasm, I vaguely recall issues along these lines in the past around things like _initialize
or _start
not being called in the right order or something that. Either that or TinyGo's runtime wasn't initialized and so wasn't picking up preopens.
Beyond that though I unfortunately may not be able to help a whole lot
Okay, thanks.
I now have it working without WASI, using the linker as you recommended. I'm now trying to get WASI to allow the .wasm code to read and write files in a chosen directory.
Last updated: Nov 22 2024 at 16:03 UTC