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

How to use type/structure? #320

Open SamYuan1990 opened 2 years ago

SamYuan1990 commented 2 years ago

Summary

Happy new year.

A clear and concise summary of your question.

I am study this project and I want to do an echo sample as type defined:

type CryptoDummy struct {
    Context string
}

webassembly code as:

// build with tinygo build -o module.wasm -target wasi .
//export HelloWorld
func HelloWorld(instance webassembly.CryptoDummy) {
    //
    fmt.Println("Hello ", instance.Context)
}

and I want to invoke the module.wasm as

    data := &def.CryptoDummy{Context: "2022"}
    fmt.Println(data)

    HelloWorld, err := instance.Exports.GetFunction("HelloWorld")
    check(err)

    result, err := HelloWorld(data)
// if fact the code returns error as 2 parameters needed.
// so as workaround I make the code as 
    result, err := HelloWorld(1,1)
// I wanna to see Hello 2022 here.
// but just Hello

but it won't work. as far as further research, I know it seems to invoke with a structure, I should pass and invokes as

HelloWorld(memory_begin,memory_end)

but

I hope we can re-enable the echo example in example folder.

Additional details

Provide any additional details here.

I found as: https://github.com/wasmerio/wasmer-go/pull/30/files

But... it seems the sample been removed.

yourlovemyall commented 2 years ago

I need this function too, did you find a solution