How do I make --import-memory work for WASM compiled by tinygo

362 Views Asked by At

I am trying to compile WASM from go using tinygo for use in go-wasmer. I have the import memory working for AssemblyScript but when I use the flag for the WASM from go and try to create my instance I get this error Host env initialization error: Missing export memory Is there a way to manually export memory that will then be replaced by the imported memory? Here is my target.json

{
    "inherits": ["wasm"],
    "linker": "wasm-ld",
    "libc": "wasi-libc",
    "goarch": "wasm",
    "cflags": [
        "--target=wasm32--wasi",
        "--sysroot={root}/lib/wasi-libc/sysroot",
        "-Oz"
      ],
    "ldflags": [
        "--import-memory",
        "--max-memory=1310720",
        "--initial-memory=131072"
    ]
}
1

There are 1 best solutions below

2
On

Where did you get that target.json from, and why do you need it? Just using -target wasi should give you something that works.

But more specifically: the error is telling you that Missing export memory, but you're trying to --import-memory. You can delete the --import-memory, and the memory import will turn into an export. If that doesn't work, you should probably paste your code setting up go-wasmer. Also, have a look at wasmer inspect yourbinary.wasm, to see what's going on with the list of imports and exports.

By the way, by inheriting from wasm, you get requirements for nasty Go wasm-js implementation specific imports like env.syscall/js.valueGet. There are implementations for those (this e.g.), but since you're already using wasi, you can just inherit from wasi (and delete goarch). Your life will probably be easier.