wasi: imported and exported functions do not result in identical signatures.
I've created a standalone repository that reproduces this issue.
essentially functions that are imported from the runtime and functions that are exported do not generate the same function signature making them unusable.
//export github.com/james-lawrence/tinygo-wasi-bug/wasi/runtime/env.Boolean
func Boolean(fallback bool, keys ...string) bool {
return false
}
// imported function by a wasm module
func Boolean(fallback bool, keys ...string) bool
the example repository will fail to execute with the following result (output is truncated for brevity):
exported github.com/james-lawrence/tinygo-wasi-bug/wasi/runtime/env.Boolean ( i32, i32, i32, i32 ) i32
imported github.com/james-lawrence/tinygo-wasi-bug/wasi/runtime/env.Boolean ( i32, i32, i32, i32, i32 ) i32
import[4] func[env.github.com/james-lawrence/tinygo-wasi-bug/wasi/runtime/env.Boolean]: signature mismatch: i32i32i32i32i32_i32 != i32i32i32i32_i32
reproduction steps from the repository above:
tinygo build -target wasi -wasm-abi generic -o module1.wasm ./impl/wasi/runtime/runtime.go
tinygo build -target wasi -wasm-abi generic -o module2.wasm main.go
go run ./cmd/wasibug/main.go
Right, I see. The way to solve this is to mark them both as "exported" - which really means to make the function available externally. For example like this:
// imported function by a wasm module
//export github.com/james-lawrence/tinygo-wasi-bug/wasi/runtime/env.Boolean
func Boolean(fallback bool, keys ...string) bool
Functions without an //export pragma will use an internal calling convention that's not stable. Functions with an //export pragma will try to match the C calling convention.
@james-lawrence is there anything else to do on this one?
ack that //export isn't intuitive to mark something as an import, but for today //export is how it is done.
@codefromthecrypt that is very unintuitive. ;) i'll take it for a spin this weekend and see if it works. if it does I'll point out documentation that should probably be updated for others. ;)
@james-lawrence opened https://github.com/tinygo-org/tinygo/issues/3147 for follow-up. maybe close this out unless there's something else to do here?
Sounds good to me; will put my notes in that issue.