wasmerio / wasmer-go

🐹🕸️ WebAssembly runtime for Go
https://pkg.go.dev/github.com/wasmerio/wasmer-go
MIT License
2.84k stars 161 forks source link

Pass string value from Go to Wasmer, return error: Missing 1 argument(s) #317

Open daixiang0 opened 2 years ago

daixiang0 commented 2 years ago

Summary

I want to pass string value from Go to Wasmer and get return value in Go.

Additional details

I use below codes with tinygo to build wasm file:

package main

import (
    "strings"
)

func main() {}

func Upper(s string) (string, error) {
    return strings.ToUpper(s), nil
}

Then call Upper function in Go:

package main

import (
        "fmt"
        "io/ioutil"

        "github.com/wasmerio/wasmer-go/wasmer"
)

func main() {
        wasmBytes, err := ioutil.ReadFile("/tmp/test/upper.wasm")
        if err != nil {
                panic(err)
        }

        engine := wasmer.NewEngine()
        store := wasmer.NewStore(engine)

        // Compiles the module
        module, err := wasmer.NewModule(store, wasmBytes)
        if err != nil {
                panic(err)
        }

        // Instantiates the module
        wasiEnv, err := wasmer.NewWasiStateBuilder("wasi-program").Finalize()
        if err != nil {
                panic(err)
        }

        importObject, err := wasiEnv.GenerateImportObject(store, module)
        if err != nil {
                panic(err)
        }

        instance, err := wasmer.NewInstance(module, importObject)
        if err != nil {
                panic(err)
        }

        start, err := instance.Exports.GetWasiStartFunction()
        if err != nil {
                panic(err)
        }

        start()

        handleFunc, err := instance.Exports.GetFunction("Upper")
        if err != nil {
                panic(err)
        }

        r, err := handleFunc("hello")
        if err != nil {
                panic(err)
        }
        result := r.(string)
        fmt.Println(result)
}

Run Go:

$ go run main.go
panic: Missing 1 argument(s) when calling the function; Expected 2 argument(s), received 1

goroutine 1 [running]:
main.main()
        main.go:55 +0x206
exit status 2

I really do not find a good example to follow, please help me.

Murena7 commented 2 years ago

I have the same issue, i think it is a problem with the compiler because when i use wasm file from the repo all work fine, but when using the self-compiled file got the same error, I did all same as in the official repo instruction. I used tinygo.