Fix Wasm modules built with TinyGo >= 0.19.0
pkedy
released this
24 Jul 01:01
Fix Wasm modules built with tinygo later than 0.19.0 (#7)
Wasm binaries built using with TinyGo >= 0.19.0 did not work properly
The final `.wasm` binary are not importing the waPC functions from the
host, they are instead using the "stub" ones defined inside of the
`imports.go` file.
That happened when building a go program using the `wasi` target:
```
tinygo build -o policy.wasm -target=wasi .
```
That DID NOT happen when building against the `wasm` target:
```
tinygo build -o policy.wasm -target=wasm .
```
This happens because TinyGo changed the build tag used to identify Wasm
binaries. It went from being `wasm` to be `tinygo.wasm`.
Go performs conditional compilation looking **also** at the suffix of
the `.go` files.
As a result of that, the `imports_wasm.go` was no longer loaded by
the `go/build` package. That, associated with the build condition
set via the `// +build` directive, lead the stub functions defined
inside of `imports.go` to be used.
Updating the build directives inside of `imports.go` and
`imports_wasm.go` is **not** enough. That's because, when building with
the `wasi` target, the compiler will not analyze the `imports_wasm.go`;
its suffix indicates it belongs to a different architecture -> it's
ignored.
This commit fixes the issue by:
* Renaming the `imports_wasm.go` to have a "neutral" name
* Extend the `// +build` directives inside of the import files
Note well: the `wasm` directive is kept around to ensure programs
build with TinyGo < 0.19.0 keep working as expected.
Signed-off-by: Flavio Castelli <[email protected]>