3

I have a Golang TCP server running locally and connected to a wasm module. Currently, I can return one value from a wasm function. Through this blog, I noticed that there should be possible to return multiple values rather than one. However, in my Go TCP server, I do not get any response from the wasm function.

This is how I did it for returning one value, and it works fine:

// Rust code for wasm modules
#[no_mangle]
pub extern "C" fn echo(ptr: *mut u8, length: usize) -> *mut u8 {
    ...
    let newptr = get_ptr(); 
    newptr
}

In the Go server:

// Go code to connect to wasm modules and get access to the returned value
engine := wasmtime.NewEngine()
store := wasmtime.NewStore(engine)
linker := wasmtime.NewLinker(store)
...
newPtr, err := server.funcs["echo"].Call(server.ptr, int32(len(recivedBytes)))
check(err)
ptr := newPtr.(int32)

The following is how I try to return multiple values from the wasm function:

// Rust code for wasm modules
#[no_mangle]
pub extern "C" fn echo(ptr: *mut u8, length: usize) -> (*mut u8, i32) {
    ...
    let newptr = get_ptr(); 
    let newlength = get_length();
    (newptr, newlength)
}

In the Go server I have added the following line of code:

    wasmtime.NewConfig().SetWasmMultiValue(true)

And the rest is the same as before:

/* Go code to connect to wasm modules and get access to the returned value */
engine := wasmtime.NewEngine()
store := wasmtime.NewStore(engine)
linker := wasmtime.NewLinker(store)
...
result, err := server.funcs["echo"].Call(server.ptr, int32(len(recivedBytes)))
check(err)
res := result.([]wasmtime.Val)

I compile the wasm modules using the cargo wasi build command, and it compiles fine. But when sending a request to the Go server, which talks to the wasm modules, I get an error that tells got 2 expected 3 arguments. I do not understand what should the third argument be.

Hanif Tadjik
  • 73
  • 1
  • 7
  • 2
    AFAIK, tuples are not FFI safe, so you should not use them for API to other languages. – rodrigo Aug 18 '21 at 15:43
  • Does this answer your question? [WASM from Rust not returning the expected types](https://stackoverflow.com/questions/70641080/wasm-from-rust-not-returning-the-expected-types) – linuskmr Jan 10 '22 at 09:59

0 Answers0